blob: 8d8ca1b2ce212046635da490a1da9350912c1588 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
Paul Sokolovskyda9f0922014-05-13 08:44:45 +03007 * Copyright (c) 2014 Paul Sokolovsky
Damien George04b91472014-05-03 23:27:38 +01008 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
Paul Sokolovsky966879c2014-01-17 20:01:36 +020028#include <stdlib.h>
Dave Hylandsc4029e52014-04-07 11:19:51 -070029#include <string.h>
Paul Sokolovsky966879c2014-01-17 20:01:36 +020030
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/nlr.h"
32#include "py/smallint.h"
33#include "py/objint.h"
34#include "py/runtime0.h"
35#include "py/runtime.h"
Paul Sokolovsky966879c2014-01-17 20:01:36 +020036
Paul Sokolovsky12033df2014-12-30 00:22:10 +020037#if MICROPY_PY_BUILTINS_FLOAT
38#include <math.h>
39#endif
40
Paul Sokolovsky966879c2014-01-17 20:01:36 +020041#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
42
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +030043#if MICROPY_PY_SYS_MAXSIZE
44// Export value for sys.maxsize
Paul Sokolovsky722e5622014-09-06 19:17:23 +030045const mp_obj_int_t mp_maxsize_obj = {{&mp_type_int}, MP_SSIZE_MAX};
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +030046#endif
47
Paul Sokolovskye6ab43e2017-01-21 20:15:05 +030048mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf) {
Paul Sokolovsky776883c2017-03-10 00:22:53 +010049 int delta = 1;
50 if (!big_endian) {
51 buf += len - 1;
52 delta = -1;
53 }
54
55 mp_longint_impl_t value = 0;
56 for (; len--; buf += delta) {
57 value = (value << 8) | *buf;
58 }
59 return mp_obj_new_int_from_ll(value);
Paul Sokolovskye6ab43e2017-01-21 20:15:05 +030060}
61
Damien George6dff3df2016-10-11 13:20:11 +110062void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf) {
Damien George271d18e2015-04-25 23:16:39 +010063 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
64 mp_obj_int_t *self = self_in;
65 long long val = self->val;
66 if (big_endian) {
67 byte *b = buf + len;
68 while (b > buf) {
69 *--b = val;
70 val >>= 8;
71 }
72 } else {
73 for (; len > 0; --len) {
74 *buf++ = val;
75 val >>= 8;
76 }
77 }
78}
79
Damien Georged6b31e42016-01-07 14:29:12 +000080int mp_obj_int_sign(mp_obj_t self_in) {
81 mp_longint_impl_t val;
Damien George88d7bba2014-04-08 23:30:46 +010082 if (MP_OBJ_IS_SMALL_INT(self_in)) {
Damien Georged6b31e42016-01-07 14:29:12 +000083 val = MP_OBJ_SMALL_INT_VALUE(self_in);
84 } else {
85 mp_obj_int_t *self = self_in;
86 val = self->val;
Damien George88d7bba2014-04-08 23:30:46 +010087 }
Damien Georged6b31e42016-01-07 14:29:12 +000088 if (val < 0) {
89 return -1;
90 } else if (val > 0) {
91 return 1;
92 } else {
93 return 0;
94 }
Damien George88d7bba2014-04-08 23:30:46 +010095}
96
Damien George58321dd2017-08-29 13:04:01 +100097mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020098 mp_obj_int_t *o = o_in;
99 switch (op) {
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300100 case MP_UNARY_OP_BOOL: return mp_obj_new_bool(o->val != 0);
Damien Georgec2a4e4e2015-05-11 12:25:19 +0000101
102 // truncate value to fit in mp_int_t, which gives the same hash as
103 // small int if the value fits without truncation
104 case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT((mp_int_t)o->val);
105
Damien Georged17926d2014-03-30 13:35:08 +0100106 case MP_UNARY_OP_POSITIVE: return o_in;
107 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val);
108 case MP_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val);
Paul Sokolovsky9dce8232017-09-18 00:06:43 +0300109 case MP_UNARY_OP_ABS: {
110 mp_obj_int_t *self = MP_OBJ_TO_PTR(o_in);
111 if (self->val >= 0) {
112 return o_in;
113 }
114 self = mp_obj_new_int_from_ll(self->val);
115 // TODO could overflow long long
116 self->val = -self->val;
117 return MP_OBJ_FROM_PTR(self);
118 }
Damien George6ac5dce2014-05-21 19:42:43 +0100119 default: return MP_OBJ_NULL; // op not supported
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200120 }
121}
122
Damien George58321dd2017-08-29 13:04:01 +1000123mp_obj_t mp_obj_int_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georged02f6ea2014-03-20 19:31:32 +0000124 long long lhs_val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200125 long long rhs_val;
126
Damien Georged02f6ea2014-03-20 19:31:32 +0000127 if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
128 lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +0000129 } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +0000130 lhs_val = ((mp_obj_int_t*)lhs_in)->val;
131 } else {
Damien George6ac5dce2014-05-21 19:42:43 +0100132 return MP_OBJ_NULL; // op not supported
Damien Georged02f6ea2014-03-20 19:31:32 +0000133 }
Damien Georgec4129982014-03-19 23:17:23 +0000134
Damien Georged02f6ea2014-03-20 19:31:32 +0000135 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
136 rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +0000137 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +0000138 rhs_val = ((mp_obj_int_t*)rhs_in)->val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200139 } else {
Damien Georgee8208a72014-04-04 15:08:23 +0100140 // delegate to generic function to check for extra cases
141 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200142 }
143
144 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100145 case MP_BINARY_OP_ADD:
146 case MP_BINARY_OP_INPLACE_ADD:
Damien Georged02f6ea2014-03-20 19:31:32 +0000147 return mp_obj_new_int_from_ll(lhs_val + rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100148 case MP_BINARY_OP_SUBTRACT:
149 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000150 return mp_obj_new_int_from_ll(lhs_val - rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100151 case MP_BINARY_OP_MULTIPLY:
152 case MP_BINARY_OP_INPLACE_MULTIPLY:
Damien Georged02f6ea2014-03-20 19:31:32 +0000153 return mp_obj_new_int_from_ll(lhs_val * rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100154 case MP_BINARY_OP_FLOOR_DIVIDE:
155 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Damien Georged02f6ea2014-03-20 19:31:32 +0000156 return mp_obj_new_int_from_ll(lhs_val / rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100157 case MP_BINARY_OP_MODULO:
158 case MP_BINARY_OP_INPLACE_MODULO:
Damien Georged02f6ea2014-03-20 19:31:32 +0000159 return mp_obj_new_int_from_ll(lhs_val % rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200160
Damien Georged17926d2014-03-30 13:35:08 +0100161 case MP_BINARY_OP_AND:
162 case MP_BINARY_OP_INPLACE_AND:
Damien Georged02f6ea2014-03-20 19:31:32 +0000163 return mp_obj_new_int_from_ll(lhs_val & rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100164 case MP_BINARY_OP_OR:
165 case MP_BINARY_OP_INPLACE_OR:
Damien Georged02f6ea2014-03-20 19:31:32 +0000166 return mp_obj_new_int_from_ll(lhs_val | rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100167 case MP_BINARY_OP_XOR:
168 case MP_BINARY_OP_INPLACE_XOR:
Damien Georged02f6ea2014-03-20 19:31:32 +0000169 return mp_obj_new_int_from_ll(lhs_val ^ rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200170
Damien Georged17926d2014-03-30 13:35:08 +0100171 case MP_BINARY_OP_LSHIFT:
172 case MP_BINARY_OP_INPLACE_LSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000173 return mp_obj_new_int_from_ll(lhs_val << (int)rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100174 case MP_BINARY_OP_RSHIFT:
175 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000176 return mp_obj_new_int_from_ll(lhs_val >> (int)rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200177
Damien George0f553fe2015-04-25 23:28:10 +0100178 case MP_BINARY_OP_POWER:
179 case MP_BINARY_OP_INPLACE_POWER: {
Damien George04552ff2017-07-25 11:49:22 +1000180 if (rhs_val < 0) {
181 #if MICROPY_PY_BUILTINS_FLOAT
182 return mp_obj_float_binary_op(op, lhs_val, rhs_in);
183 #else
184 mp_raise_ValueError("negative power with no float support");
185 #endif
186 }
Damien George0f553fe2015-04-25 23:28:10 +0100187 long long ans = 1;
188 while (rhs_val > 0) {
189 if (rhs_val & 1) {
190 ans *= lhs_val;
191 }
192 if (rhs_val == 1) {
193 break;
194 }
195 rhs_val /= 2;
196 lhs_val *= lhs_val;
197 }
198 return mp_obj_new_int_from_ll(ans);
199 }
200
Damien Georged17926d2014-03-30 13:35:08 +0100201 case MP_BINARY_OP_LESS:
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300202 return mp_obj_new_bool(lhs_val < rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100203 case MP_BINARY_OP_MORE:
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300204 return mp_obj_new_bool(lhs_val > rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100205 case MP_BINARY_OP_LESS_EQUAL:
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300206 return mp_obj_new_bool(lhs_val <= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100207 case MP_BINARY_OP_MORE_EQUAL:
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300208 return mp_obj_new_bool(lhs_val >= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100209 case MP_BINARY_OP_EQUAL:
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300210 return mp_obj_new_bool(lhs_val == rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200211
212 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100213 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200214 }
215}
216
Damien George40f3c022014-07-03 13:25:24 +0100217mp_obj_t mp_obj_new_int(mp_int_t value) {
Damien Georged1e355e2014-05-28 14:51:12 +0100218 if (MP_SMALL_INT_FITS(value)) {
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200219 return MP_OBJ_NEW_SMALL_INT(value);
220 }
221 return mp_obj_new_int_from_ll(value);
222}
223
Damien George40f3c022014-07-03 13:25:24 +0100224mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
Damien George9ae51252016-03-10 21:52:56 +0000225 // SMALL_INT accepts only signed numbers, so make sure the input
226 // value fits completely in the small-int positive range.
227 if ((value & ~MP_SMALL_INT_POSITIVE_MASK) == 0) {
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200228 return MP_OBJ_NEW_SMALL_INT(value);
229 }
230 return mp_obj_new_int_from_ll(value);
231}
232
233mp_obj_t mp_obj_new_int_from_ll(long long val) {
234 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000235 o->base.type = &mp_type_int;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200236 o->val = val;
237 return o;
238}
239
Damien George95307432014-09-10 22:10:33 +0100240mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
241 // TODO raise an exception if the unsigned long long won't fit
Paul Sokolovsky50f56222015-11-09 01:34:13 +0200242 if (val >> (sizeof(unsigned long long) * 8 - 1) != 0) {
Damien George48d867b2017-06-15 11:54:41 +1000243 mp_raise_msg(&mp_type_OverflowError, "ulonglong too large");
Paul Sokolovsky50f56222015-11-09 01:34:13 +0200244 }
Damien George95307432014-09-10 22:10:33 +0100245 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
246 o->base.type = &mp_type_int;
247 o->val = val;
248 return o;
249}
250
Damien Georgeda36f522017-02-16 16:41:43 +1100251mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base) {
Damien George503d6112014-05-28 14:07:21 +0100252 // TODO this does not honor the given length of the string, but it all cases it should anyway be null terminated
253 // TODO check overflow
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200254 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000255 o->base.type = &mp_type_int;
Damien George503d6112014-05-28 14:07:21 +0100256 char *endptr;
257 o->val = strtoll(*str, &endptr, base);
258 *str = endptr;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200259 return o;
260}
261
Damien Georgebe6d8be2014-12-05 23:13:52 +0000262mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200263 if (MP_OBJ_IS_SMALL_INT(self_in)) {
264 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000265 } else {
Damien George503d6112014-05-28 14:07:21 +0100266 const mp_obj_int_t *self = self_in;
Damien Georgeeabdf672014-03-22 20:54:01 +0000267 return self->val;
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200268 }
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200269}
270
Damien George40f3c022014-07-03 13:25:24 +0100271mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200272 // TODO: Check overflow
Damien Georgebe6d8be2014-12-05 23:13:52 +0000273 return mp_obj_int_get_truncated(self_in);
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200274}
275
Damien Georgefb510b32014-06-01 13:32:54 +0100276#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgee4af7122016-12-21 11:46:27 +1100277mp_float_t mp_obj_int_as_float_impl(mp_obj_t self_in) {
278 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
279 mp_obj_int_t *self = self_in;
280 return self->val;
Damien Georgeeabdf672014-03-22 20:54:01 +0000281}
282#endif
283
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200284#endif