blob: 394ae111adaa226e1b5f25a891680a8fa3f2c0a0 [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 Georgeecc88e92014-08-30 00:35:11 +010074mp_obj_t mp_obj_int_unary_op(mp_uint_t op, mp_obj_t o_in) {
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020075 mp_obj_int_t *o = o_in;
76 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010077 case MP_UNARY_OP_BOOL: return MP_BOOL(o->val != 0);
78 case MP_UNARY_OP_POSITIVE: return o_in;
79 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val);
80 case MP_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val);
Damien George6ac5dce2014-05-21 19:42:43 +010081 default: return MP_OBJ_NULL; // op not supported
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020082 }
83}
84
Damien Georgeecc88e92014-08-30 00:35:11 +010085mp_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 +000086 long long lhs_val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +020087 long long rhs_val;
88
Damien Georged02f6ea2014-03-20 19:31:32 +000089 if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
90 lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +000091 } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +000092 lhs_val = ((mp_obj_int_t*)lhs_in)->val;
93 } else {
Damien George6ac5dce2014-05-21 19:42:43 +010094 return MP_OBJ_NULL; // op not supported
Damien Georged02f6ea2014-03-20 19:31:32 +000095 }
Damien Georgec4129982014-03-19 23:17:23 +000096
Damien Georged02f6ea2014-03-20 19:31:32 +000097 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
98 rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +000099 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +0000100 rhs_val = ((mp_obj_int_t*)rhs_in)->val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200101 } else {
Damien Georgee8208a72014-04-04 15:08:23 +0100102 // delegate to generic function to check for extra cases
103 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200104 }
105
106 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100107 case MP_BINARY_OP_ADD:
108 case MP_BINARY_OP_INPLACE_ADD:
Damien Georged02f6ea2014-03-20 19:31:32 +0000109 return mp_obj_new_int_from_ll(lhs_val + rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100110 case MP_BINARY_OP_SUBTRACT:
111 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000112 return mp_obj_new_int_from_ll(lhs_val - rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100113 case MP_BINARY_OP_MULTIPLY:
114 case MP_BINARY_OP_INPLACE_MULTIPLY:
Damien Georged02f6ea2014-03-20 19:31:32 +0000115 return mp_obj_new_int_from_ll(lhs_val * rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100116 case MP_BINARY_OP_FLOOR_DIVIDE:
117 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Damien Georged02f6ea2014-03-20 19:31:32 +0000118 return mp_obj_new_int_from_ll(lhs_val / rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100119 case MP_BINARY_OP_MODULO:
120 case MP_BINARY_OP_INPLACE_MODULO:
Damien Georged02f6ea2014-03-20 19:31:32 +0000121 return mp_obj_new_int_from_ll(lhs_val % rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200122
Damien Georged17926d2014-03-30 13:35:08 +0100123 case MP_BINARY_OP_AND:
124 case MP_BINARY_OP_INPLACE_AND:
Damien Georged02f6ea2014-03-20 19:31:32 +0000125 return mp_obj_new_int_from_ll(lhs_val & rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100126 case MP_BINARY_OP_OR:
127 case MP_BINARY_OP_INPLACE_OR:
Damien Georged02f6ea2014-03-20 19:31:32 +0000128 return mp_obj_new_int_from_ll(lhs_val | rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100129 case MP_BINARY_OP_XOR:
130 case MP_BINARY_OP_INPLACE_XOR:
Damien Georged02f6ea2014-03-20 19:31:32 +0000131 return mp_obj_new_int_from_ll(lhs_val ^ rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200132
Damien Georged17926d2014-03-30 13:35:08 +0100133 case MP_BINARY_OP_LSHIFT:
134 case MP_BINARY_OP_INPLACE_LSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000135 return mp_obj_new_int_from_ll(lhs_val << (int)rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100136 case MP_BINARY_OP_RSHIFT:
137 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000138 return mp_obj_new_int_from_ll(lhs_val >> (int)rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200139
Damien Georged17926d2014-03-30 13:35:08 +0100140 case MP_BINARY_OP_LESS:
Damien Georged02f6ea2014-03-20 19:31:32 +0000141 return MP_BOOL(lhs_val < rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100142 case MP_BINARY_OP_MORE:
Damien Georged02f6ea2014-03-20 19:31:32 +0000143 return MP_BOOL(lhs_val > rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100144 case MP_BINARY_OP_LESS_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000145 return MP_BOOL(lhs_val <= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100146 case MP_BINARY_OP_MORE_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000147 return MP_BOOL(lhs_val >= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100148 case MP_BINARY_OP_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000149 return MP_BOOL(lhs_val == rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200150
151 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100152 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200153 }
154}
155
Damien George40f3c022014-07-03 13:25:24 +0100156mp_obj_t mp_obj_new_int(mp_int_t value) {
Damien Georged1e355e2014-05-28 14:51:12 +0100157 if (MP_SMALL_INT_FITS(value)) {
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200158 return MP_OBJ_NEW_SMALL_INT(value);
159 }
160 return mp_obj_new_int_from_ll(value);
161}
162
Damien George40f3c022014-07-03 13:25:24 +0100163mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200164 // SMALL_INT accepts only signed numbers, of one bit less size
165 // than word size, which totals 2 bits less for unsigned numbers.
166 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
167 return MP_OBJ_NEW_SMALL_INT(value);
168 }
169 return mp_obj_new_int_from_ll(value);
170}
171
172mp_obj_t mp_obj_new_int_from_ll(long long val) {
173 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000174 o->base.type = &mp_type_int;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200175 o->val = val;
176 return o;
177}
178
Damien George95307432014-09-10 22:10:33 +0100179mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
180 // TODO raise an exception if the unsigned long long won't fit
181 assert(val >> (sizeof(unsigned long long) * 8 - 1) == 0);
182 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
183 o->base.type = &mp_type_int;
184 o->val = val;
185 return o;
186}
187
Paul Sokolovsky12033df2014-12-30 00:22:10 +0200188#if MICROPY_PY_BUILTINS_FLOAT
189mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
190 // TODO raise an exception if the unsigned long long won't fit
191 long long i = MICROPY_FLOAT_C_FUN(trunc)(val);
192 return mp_obj_new_int_from_ll(i);
193}
194#endif
195
Damien George95307432014-09-10 22:10:33 +0100196mp_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 +0100197 // TODO this does not honor the given length of the string, but it all cases it should anyway be null terminated
198 // TODO check overflow
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200199 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000200 o->base.type = &mp_type_int;
Damien George503d6112014-05-28 14:07:21 +0100201 char *endptr;
202 o->val = strtoll(*str, &endptr, base);
203 *str = endptr;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200204 return o;
205}
206
Damien Georgebe6d8be2014-12-05 23:13:52 +0000207mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200208 if (MP_OBJ_IS_SMALL_INT(self_in)) {
209 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000210 } else {
Damien George503d6112014-05-28 14:07:21 +0100211 const mp_obj_int_t *self = self_in;
Damien Georgeeabdf672014-03-22 20:54:01 +0000212 return self->val;
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200213 }
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200214}
215
Damien George40f3c022014-07-03 13:25:24 +0100216mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200217 // TODO: Check overflow
Damien Georgebe6d8be2014-12-05 23:13:52 +0000218 return mp_obj_int_get_truncated(self_in);
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200219}
220
Damien Georgefb510b32014-06-01 13:32:54 +0100221#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgeeabdf672014-03-22 20:54:01 +0000222mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
223 if (MP_OBJ_IS_SMALL_INT(self_in)) {
224 return MP_OBJ_SMALL_INT_VALUE(self_in);
225 } else {
226 mp_obj_int_t *self = self_in;
227 return self->val;
228 }
229}
230#endif
231
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200232#endif