blob: ec55c7784984529b5503e2d60fb8f70f2056dcbb [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>
29#include <stdint.h>
Dave Hylandsc4029e52014-04-07 11:19:51 -070030#include <string.h>
Paul Sokolovsky966879c2014-01-17 20:01:36 +020031
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030032#include "mpconfig.h"
Paul Sokolovsky966879c2014-01-17 20:01:36 +020033#include "nlr.h"
34#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000035#include "qstr.h"
Paul Sokolovsky966879c2014-01-17 20:01:36 +020036#include "obj.h"
Damien Georged1e355e2014-05-28 14:51:12 +010037#include "smallint.h"
Damien George438c88d2014-02-22 19:25:23 +000038#include "mpz.h"
Paul Sokolovsky966879c2014-01-17 20:01:36 +020039#include "objint.h"
40#include "runtime0.h"
Damien George660aef62014-04-02 12:22:07 +010041#include "runtime.h"
Paul Sokolovsky966879c2014-01-17 20:01:36 +020042
43#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
44
Paul Sokolovsky966879c2014-01-17 20:01:36 +020045// Python3 no longer has "l" suffix for long ints. We allow to use it
46// for debugging purpose though.
47#ifdef DEBUG
48#define SUFFIX "l"
49#else
50#define SUFFIX ""
51#endif
52
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +030053#if MICROPY_PY_SYS_MAXSIZE
54// Export value for sys.maxsize
Paul Sokolovsky722e5622014-09-06 19:17:23 +030055const mp_obj_int_t mp_maxsize_obj = {{&mp_type_int}, MP_SSIZE_MAX};
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +030056#endif
57
Damien Georgeffe911d2014-07-24 14:21:37 +010058mp_int_t mp_obj_int_hash(mp_obj_t self_in) {
59 if (MP_OBJ_IS_SMALL_INT(self_in)) {
60 return MP_OBJ_SMALL_INT_VALUE(self_in);
61 }
62 mp_obj_int_t *self = self_in;
63 // truncate value to fit in mp_int_t, which gives the same hash as
64 // small int if the value fits without truncation
65 return self->val;
66}
67
Damien George88d7bba2014-04-08 23:30:46 +010068bool mp_obj_int_is_positive(mp_obj_t self_in) {
69 if (MP_OBJ_IS_SMALL_INT(self_in)) {
70 return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0;
71 }
72 mp_obj_int_t *self = self_in;
73 return self->val >= 0;
74}
75
Damien Georgeecc88e92014-08-30 00:35:11 +010076mp_obj_t mp_obj_int_unary_op(mp_uint_t op, mp_obj_t o_in) {
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020077 mp_obj_int_t *o = o_in;
78 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010079 case MP_UNARY_OP_BOOL: return MP_BOOL(o->val != 0);
80 case MP_UNARY_OP_POSITIVE: return o_in;
81 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val);
82 case MP_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val);
Damien George6ac5dce2014-05-21 19:42:43 +010083 default: return MP_OBJ_NULL; // op not supported
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020084 }
85}
86
Damien Georgeecc88e92014-08-30 00:35:11 +010087mp_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 +000088 long long lhs_val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +020089 long long rhs_val;
90
Damien Georged02f6ea2014-03-20 19:31:32 +000091 if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
92 lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +000093 } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +000094 lhs_val = ((mp_obj_int_t*)lhs_in)->val;
95 } else {
Damien George6ac5dce2014-05-21 19:42:43 +010096 return MP_OBJ_NULL; // op not supported
Damien Georged02f6ea2014-03-20 19:31:32 +000097 }
Damien Georgec4129982014-03-19 23:17:23 +000098
Damien Georged02f6ea2014-03-20 19:31:32 +000099 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
100 rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +0000101 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +0000102 rhs_val = ((mp_obj_int_t*)rhs_in)->val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200103 } else {
Damien Georgee8208a72014-04-04 15:08:23 +0100104 // delegate to generic function to check for extra cases
105 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200106 }
107
108 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100109 case MP_BINARY_OP_ADD:
110 case MP_BINARY_OP_INPLACE_ADD:
Damien Georged02f6ea2014-03-20 19:31:32 +0000111 return mp_obj_new_int_from_ll(lhs_val + rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100112 case MP_BINARY_OP_SUBTRACT:
113 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000114 return mp_obj_new_int_from_ll(lhs_val - rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100115 case MP_BINARY_OP_MULTIPLY:
116 case MP_BINARY_OP_INPLACE_MULTIPLY:
Damien Georged02f6ea2014-03-20 19:31:32 +0000117 return mp_obj_new_int_from_ll(lhs_val * rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100118 case MP_BINARY_OP_FLOOR_DIVIDE:
119 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Damien Georged02f6ea2014-03-20 19:31:32 +0000120 return mp_obj_new_int_from_ll(lhs_val / rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100121 case MP_BINARY_OP_MODULO:
122 case MP_BINARY_OP_INPLACE_MODULO:
Damien Georged02f6ea2014-03-20 19:31:32 +0000123 return mp_obj_new_int_from_ll(lhs_val % rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200124
Damien Georged17926d2014-03-30 13:35:08 +0100125 case MP_BINARY_OP_AND:
126 case MP_BINARY_OP_INPLACE_AND:
Damien Georged02f6ea2014-03-20 19:31:32 +0000127 return mp_obj_new_int_from_ll(lhs_val & rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100128 case MP_BINARY_OP_OR:
129 case MP_BINARY_OP_INPLACE_OR:
Damien Georged02f6ea2014-03-20 19:31:32 +0000130 return mp_obj_new_int_from_ll(lhs_val | rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100131 case MP_BINARY_OP_XOR:
132 case MP_BINARY_OP_INPLACE_XOR:
Damien Georged02f6ea2014-03-20 19:31:32 +0000133 return mp_obj_new_int_from_ll(lhs_val ^ rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200134
Damien Georged17926d2014-03-30 13:35:08 +0100135 case MP_BINARY_OP_LSHIFT:
136 case MP_BINARY_OP_INPLACE_LSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000137 return mp_obj_new_int_from_ll(lhs_val << (int)rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100138 case MP_BINARY_OP_RSHIFT:
139 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000140 return mp_obj_new_int_from_ll(lhs_val >> (int)rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200141
Damien Georged17926d2014-03-30 13:35:08 +0100142 case MP_BINARY_OP_LESS:
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_MORE:
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_LESS_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_MORE_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000149 return MP_BOOL(lhs_val >= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100150 case MP_BINARY_OP_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000151 return MP_BOOL(lhs_val == rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200152
153 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100154 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200155 }
156}
157
Damien George40f3c022014-07-03 13:25:24 +0100158mp_obj_t mp_obj_new_int(mp_int_t value) {
Damien Georged1e355e2014-05-28 14:51:12 +0100159 if (MP_SMALL_INT_FITS(value)) {
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200160 return MP_OBJ_NEW_SMALL_INT(value);
161 }
162 return mp_obj_new_int_from_ll(value);
163}
164
Damien George40f3c022014-07-03 13:25:24 +0100165mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200166 // SMALL_INT accepts only signed numbers, of one bit less size
167 // than word size, which totals 2 bits less for unsigned numbers.
168 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
169 return MP_OBJ_NEW_SMALL_INT(value);
170 }
171 return mp_obj_new_int_from_ll(value);
172}
173
174mp_obj_t mp_obj_new_int_from_ll(long long val) {
175 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000176 o->base.type = &mp_type_int;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200177 o->val = val;
178 return o;
179}
180
Damien George95307432014-09-10 22:10:33 +0100181mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
182 // TODO raise an exception if the unsigned long long won't fit
183 assert(val >> (sizeof(unsigned long long) * 8 - 1) == 0);
184 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
185 o->base.type = &mp_type_int;
186 o->val = val;
187 return o;
188}
189
190mp_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 +0100191 // TODO this does not honor the given length of the string, but it all cases it should anyway be null terminated
192 // TODO check overflow
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200193 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000194 o->base.type = &mp_type_int;
Damien George503d6112014-05-28 14:07:21 +0100195 char *endptr;
196 o->val = strtoll(*str, &endptr, base);
197 *str = endptr;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200198 return o;
199}
200
Damien Georgebe6d8be2014-12-05 23:13:52 +0000201mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200202 if (MP_OBJ_IS_SMALL_INT(self_in)) {
203 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000204 } else {
Damien George503d6112014-05-28 14:07:21 +0100205 const mp_obj_int_t *self = self_in;
Damien Georgeeabdf672014-03-22 20:54:01 +0000206 return self->val;
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200207 }
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200208}
209
Damien George40f3c022014-07-03 13:25:24 +0100210mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200211 // TODO: Check overflow
Damien Georgebe6d8be2014-12-05 23:13:52 +0000212 return mp_obj_int_get_truncated(self_in);
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200213}
214
Damien Georgefb510b32014-06-01 13:32:54 +0100215#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgeeabdf672014-03-22 20:54:01 +0000216mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
217 if (MP_OBJ_IS_SMALL_INT(self_in)) {
218 return MP_OBJ_SMALL_INT_VALUE(self_in);
219 } else {
220 mp_obj_int_t *self = self_in;
221 return self->val;
222 }
223}
224#endif
225
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200226#endif