blob: 837889704aa90b8ba04401cc772490d9eb023ab1 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
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 Sokolovsky966879c2014-01-17 20:01:36 +020043// Python3 no longer has "l" suffix for long ints. We allow to use it
44// for debugging purpose though.
45#ifdef DEBUG
46#define SUFFIX "l"
47#else
48#define SUFFIX ""
49#endif
50
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +030051#if MICROPY_PY_SYS_MAXSIZE
52// Export value for sys.maxsize
Paul Sokolovsky722e5622014-09-06 19:17:23 +030053const mp_obj_int_t mp_maxsize_obj = {{&mp_type_int}, MP_SSIZE_MAX};
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +030054#endif
55
Damien Georgeffe911d2014-07-24 14:21:37 +010056mp_int_t mp_obj_int_hash(mp_obj_t self_in) {
57 if (MP_OBJ_IS_SMALL_INT(self_in)) {
58 return MP_OBJ_SMALL_INT_VALUE(self_in);
59 }
60 mp_obj_int_t *self = self_in;
61 // truncate value to fit in mp_int_t, which gives the same hash as
62 // small int if the value fits without truncation
63 return self->val;
64}
65
Damien George88d7bba2014-04-08 23:30:46 +010066bool mp_obj_int_is_positive(mp_obj_t self_in) {
67 if (MP_OBJ_IS_SMALL_INT(self_in)) {
68 return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0;
69 }
70 mp_obj_int_t *self = self_in;
71 return self->val >= 0;
72}
73
Damien George6837d462015-03-14 22:07:30 +000074// This must handle int and bool types, and must raise a
75// TypeError if the argument is not integral
76mp_obj_t mp_obj_int_abs(mp_obj_t self_in) {
77 if (MP_OBJ_IS_TYPE(self_in, &mp_type_int)) {
78 mp_obj_int_t *self = self_in;
79 self = mp_obj_new_int_from_ll(self->val);
80 if (self->val < 0) {
81 // TODO could overflow long long
82 self->val = -self->val;
83 }
84 return self;
85 } else {
86 mp_int_t val = mp_obj_get_int(self_in);
87 if (val == MP_SMALL_INT_MIN) {
88 return mp_obj_new_int_from_ll(-val);
89 } else {
90 if (val < 0) {
91 val = -val;
92 }
93 return MP_OBJ_NEW_SMALL_INT(val);
94 }
95 }
96}
97
Damien Georgeecc88e92014-08-30 00:35:11 +010098mp_obj_t mp_obj_int_unary_op(mp_uint_t op, mp_obj_t o_in) {
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020099 mp_obj_int_t *o = o_in;
100 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100101 case MP_UNARY_OP_BOOL: return MP_BOOL(o->val != 0);
102 case MP_UNARY_OP_POSITIVE: return o_in;
103 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val);
104 case MP_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val);
Damien George6ac5dce2014-05-21 19:42:43 +0100105 default: return MP_OBJ_NULL; // op not supported
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200106 }
107}
108
Damien Georgeecc88e92014-08-30 00:35:11 +0100109mp_obj_t mp_obj_int_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georged02f6ea2014-03-20 19:31:32 +0000110 long long lhs_val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200111 long long rhs_val;
112
Damien Georged02f6ea2014-03-20 19:31:32 +0000113 if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
114 lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +0000115 } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +0000116 lhs_val = ((mp_obj_int_t*)lhs_in)->val;
117 } else {
Damien George6ac5dce2014-05-21 19:42:43 +0100118 return MP_OBJ_NULL; // op not supported
Damien Georged02f6ea2014-03-20 19:31:32 +0000119 }
Damien Georgec4129982014-03-19 23:17:23 +0000120
Damien Georged02f6ea2014-03-20 19:31:32 +0000121 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
122 rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +0000123 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +0000124 rhs_val = ((mp_obj_int_t*)rhs_in)->val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200125 } else {
Damien Georgee8208a72014-04-04 15:08:23 +0100126 // delegate to generic function to check for extra cases
127 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200128 }
129
130 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100131 case MP_BINARY_OP_ADD:
132 case MP_BINARY_OP_INPLACE_ADD:
Damien Georged02f6ea2014-03-20 19:31:32 +0000133 return mp_obj_new_int_from_ll(lhs_val + rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100134 case MP_BINARY_OP_SUBTRACT:
135 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000136 return mp_obj_new_int_from_ll(lhs_val - rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100137 case MP_BINARY_OP_MULTIPLY:
138 case MP_BINARY_OP_INPLACE_MULTIPLY:
Damien Georged02f6ea2014-03-20 19:31:32 +0000139 return mp_obj_new_int_from_ll(lhs_val * rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100140 case MP_BINARY_OP_FLOOR_DIVIDE:
141 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Damien Georged02f6ea2014-03-20 19:31:32 +0000142 return mp_obj_new_int_from_ll(lhs_val / rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100143 case MP_BINARY_OP_MODULO:
144 case MP_BINARY_OP_INPLACE_MODULO:
Damien Georged02f6ea2014-03-20 19:31:32 +0000145 return mp_obj_new_int_from_ll(lhs_val % rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200146
Damien Georged17926d2014-03-30 13:35:08 +0100147 case MP_BINARY_OP_AND:
148 case MP_BINARY_OP_INPLACE_AND:
Damien Georged02f6ea2014-03-20 19:31:32 +0000149 return mp_obj_new_int_from_ll(lhs_val & rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100150 case MP_BINARY_OP_OR:
151 case MP_BINARY_OP_INPLACE_OR:
Damien Georged02f6ea2014-03-20 19:31:32 +0000152 return mp_obj_new_int_from_ll(lhs_val | rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100153 case MP_BINARY_OP_XOR:
154 case MP_BINARY_OP_INPLACE_XOR:
Damien Georged02f6ea2014-03-20 19:31:32 +0000155 return mp_obj_new_int_from_ll(lhs_val ^ rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200156
Damien Georged17926d2014-03-30 13:35:08 +0100157 case MP_BINARY_OP_LSHIFT:
158 case MP_BINARY_OP_INPLACE_LSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000159 return mp_obj_new_int_from_ll(lhs_val << (int)rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100160 case MP_BINARY_OP_RSHIFT:
161 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000162 return mp_obj_new_int_from_ll(lhs_val >> (int)rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200163
Damien Georged17926d2014-03-30 13:35:08 +0100164 case MP_BINARY_OP_LESS:
Damien Georged02f6ea2014-03-20 19:31:32 +0000165 return MP_BOOL(lhs_val < rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100166 case MP_BINARY_OP_MORE:
Damien Georged02f6ea2014-03-20 19:31:32 +0000167 return MP_BOOL(lhs_val > rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100168 case MP_BINARY_OP_LESS_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000169 return MP_BOOL(lhs_val <= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100170 case MP_BINARY_OP_MORE_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000171 return MP_BOOL(lhs_val >= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100172 case MP_BINARY_OP_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000173 return MP_BOOL(lhs_val == rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200174
175 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100176 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200177 }
178}
179
Damien George40f3c022014-07-03 13:25:24 +0100180mp_obj_t mp_obj_new_int(mp_int_t value) {
Damien Georged1e355e2014-05-28 14:51:12 +0100181 if (MP_SMALL_INT_FITS(value)) {
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200182 return MP_OBJ_NEW_SMALL_INT(value);
183 }
184 return mp_obj_new_int_from_ll(value);
185}
186
Damien George40f3c022014-07-03 13:25:24 +0100187mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200188 // SMALL_INT accepts only signed numbers, of one bit less size
189 // than word size, which totals 2 bits less for unsigned numbers.
190 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
191 return MP_OBJ_NEW_SMALL_INT(value);
192 }
193 return mp_obj_new_int_from_ll(value);
194}
195
196mp_obj_t mp_obj_new_int_from_ll(long long val) {
197 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000198 o->base.type = &mp_type_int;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200199 o->val = val;
200 return o;
201}
202
Damien George95307432014-09-10 22:10:33 +0100203mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
204 // TODO raise an exception if the unsigned long long won't fit
205 assert(val >> (sizeof(unsigned long long) * 8 - 1) == 0);
206 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
207 o->base.type = &mp_type_int;
208 o->val = val;
209 return o;
210}
211
Paul Sokolovsky12033df2014-12-30 00:22:10 +0200212#if MICROPY_PY_BUILTINS_FLOAT
213mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
David Steinberg0fb17f62015-01-13 15:22:30 +0000214 int cl = fpclassify(val);
215 if (cl == FP_INFINITE) {
216 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OverflowError, "can't convert inf to int"));
217 } else if (cl == FP_NAN) {
218 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "can't convert NaN to int"));
219 } else {
220 mp_fp_as_int_class_t icl = mp_classify_fp_as_int(val);
221 if (icl == MP_FP_CLASS_FIT_SMALLINT) {
222 return MP_OBJ_NEW_SMALL_INT((mp_int_t)val);
223 } else if (icl == MP_FP_CLASS_FIT_LONGINT) {
224 return mp_obj_new_int_from_ll((long long)val);
225 } else {
226 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "float too big"));
227 }
228 }
Paul Sokolovsky12033df2014-12-30 00:22:10 +0200229}
230#endif
231
Damien George95307432014-09-10 22:10:33 +0100232mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg, mp_uint_t base) {
Damien George503d6112014-05-28 14:07:21 +0100233 // TODO this does not honor the given length of the string, but it all cases it should anyway be null terminated
234 // TODO check overflow
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200235 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000236 o->base.type = &mp_type_int;
Damien George503d6112014-05-28 14:07:21 +0100237 char *endptr;
238 o->val = strtoll(*str, &endptr, base);
239 *str = endptr;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200240 return o;
241}
242
Damien Georgebe6d8be2014-12-05 23:13:52 +0000243mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200244 if (MP_OBJ_IS_SMALL_INT(self_in)) {
245 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000246 } else {
Damien George503d6112014-05-28 14:07:21 +0100247 const mp_obj_int_t *self = self_in;
Damien Georgeeabdf672014-03-22 20:54:01 +0000248 return self->val;
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200249 }
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200250}
251
Damien George40f3c022014-07-03 13:25:24 +0100252mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200253 // TODO: Check overflow
Damien Georgebe6d8be2014-12-05 23:13:52 +0000254 return mp_obj_int_get_truncated(self_in);
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200255}
256
Damien Georgefb510b32014-06-01 13:32:54 +0100257#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgeeabdf672014-03-22 20:54:01 +0000258mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
259 if (MP_OBJ_IS_SMALL_INT(self_in)) {
260 return MP_OBJ_SMALL_INT_VALUE(self_in);
261 } else {
262 mp_obj_int_t *self = self_in;
263 return self->val;
264 }
265}
266#endif
267
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200268#endif