blob: 5f48b71340cf2b726724d6d1b316abd7fffc61d5 [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
Paul Sokolovsky12033df2014-12-30 00:22:10 +020043#if MICROPY_PY_BUILTINS_FLOAT
44#include <math.h>
45#endif
46
Paul Sokolovsky966879c2014-01-17 20:01:36 +020047#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
48
Paul Sokolovsky966879c2014-01-17 20:01:36 +020049// Python3 no longer has "l" suffix for long ints. We allow to use it
50// for debugging purpose though.
51#ifdef DEBUG
52#define SUFFIX "l"
53#else
54#define SUFFIX ""
55#endif
56
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +030057#if MICROPY_PY_SYS_MAXSIZE
58// Export value for sys.maxsize
Paul Sokolovsky722e5622014-09-06 19:17:23 +030059const mp_obj_int_t mp_maxsize_obj = {{&mp_type_int}, MP_SSIZE_MAX};
Paul Sokolovsky4e0eeeb2014-07-03 16:50:11 +030060#endif
61
Damien Georgeffe911d2014-07-24 14:21:37 +010062mp_int_t mp_obj_int_hash(mp_obj_t self_in) {
63 if (MP_OBJ_IS_SMALL_INT(self_in)) {
64 return MP_OBJ_SMALL_INT_VALUE(self_in);
65 }
66 mp_obj_int_t *self = self_in;
67 // truncate value to fit in mp_int_t, which gives the same hash as
68 // small int if the value fits without truncation
69 return self->val;
70}
71
Damien George88d7bba2014-04-08 23:30:46 +010072bool mp_obj_int_is_positive(mp_obj_t self_in) {
73 if (MP_OBJ_IS_SMALL_INT(self_in)) {
74 return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0;
75 }
76 mp_obj_int_t *self = self_in;
77 return self->val >= 0;
78}
79
Damien Georgeecc88e92014-08-30 00:35:11 +010080mp_obj_t mp_obj_int_unary_op(mp_uint_t op, mp_obj_t o_in) {
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020081 mp_obj_int_t *o = o_in;
82 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010083 case MP_UNARY_OP_BOOL: return MP_BOOL(o->val != 0);
84 case MP_UNARY_OP_POSITIVE: return o_in;
85 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val);
86 case MP_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val);
Damien George6ac5dce2014-05-21 19:42:43 +010087 default: return MP_OBJ_NULL; // op not supported
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020088 }
89}
90
Damien Georgeecc88e92014-08-30 00:35:11 +010091mp_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 +000092 long long lhs_val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +020093 long long rhs_val;
94
Damien Georged02f6ea2014-03-20 19:31:32 +000095 if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
96 lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +000097 } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +000098 lhs_val = ((mp_obj_int_t*)lhs_in)->val;
99 } else {
Damien George6ac5dce2014-05-21 19:42:43 +0100100 return MP_OBJ_NULL; // op not supported
Damien Georged02f6ea2014-03-20 19:31:32 +0000101 }
Damien Georgec4129982014-03-19 23:17:23 +0000102
Damien Georged02f6ea2014-03-20 19:31:32 +0000103 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
104 rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +0000105 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +0000106 rhs_val = ((mp_obj_int_t*)rhs_in)->val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200107 } else {
Damien Georgee8208a72014-04-04 15:08:23 +0100108 // delegate to generic function to check for extra cases
109 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200110 }
111
112 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100113 case MP_BINARY_OP_ADD:
114 case MP_BINARY_OP_INPLACE_ADD:
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_SUBTRACT:
117 case MP_BINARY_OP_INPLACE_SUBTRACT:
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_MULTIPLY:
120 case MP_BINARY_OP_INPLACE_MULTIPLY:
Damien Georged02f6ea2014-03-20 19:31:32 +0000121 return mp_obj_new_int_from_ll(lhs_val * rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100122 case MP_BINARY_OP_FLOOR_DIVIDE:
123 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Damien Georged02f6ea2014-03-20 19:31:32 +0000124 return mp_obj_new_int_from_ll(lhs_val / rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100125 case MP_BINARY_OP_MODULO:
126 case MP_BINARY_OP_INPLACE_MODULO:
Damien Georged02f6ea2014-03-20 19:31:32 +0000127 return mp_obj_new_int_from_ll(lhs_val % rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200128
Damien Georged17926d2014-03-30 13:35:08 +0100129 case MP_BINARY_OP_AND:
130 case MP_BINARY_OP_INPLACE_AND:
Damien Georged02f6ea2014-03-20 19:31:32 +0000131 return mp_obj_new_int_from_ll(lhs_val & rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100132 case MP_BINARY_OP_OR:
133 case MP_BINARY_OP_INPLACE_OR:
Damien Georged02f6ea2014-03-20 19:31:32 +0000134 return mp_obj_new_int_from_ll(lhs_val | rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100135 case MP_BINARY_OP_XOR:
136 case MP_BINARY_OP_INPLACE_XOR:
Damien Georged02f6ea2014-03-20 19:31:32 +0000137 return mp_obj_new_int_from_ll(lhs_val ^ rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200138
Damien Georged17926d2014-03-30 13:35:08 +0100139 case MP_BINARY_OP_LSHIFT:
140 case MP_BINARY_OP_INPLACE_LSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000141 return mp_obj_new_int_from_ll(lhs_val << (int)rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100142 case MP_BINARY_OP_RSHIFT:
143 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000144 return mp_obj_new_int_from_ll(lhs_val >> (int)rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200145
Damien Georged17926d2014-03-30 13:35:08 +0100146 case MP_BINARY_OP_LESS:
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:
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_LESS_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000151 return MP_BOOL(lhs_val <= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100152 case MP_BINARY_OP_MORE_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000153 return MP_BOOL(lhs_val >= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100154 case MP_BINARY_OP_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000155 return MP_BOOL(lhs_val == rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200156
157 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100158 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200159 }
160}
161
Damien George40f3c022014-07-03 13:25:24 +0100162mp_obj_t mp_obj_new_int(mp_int_t value) {
Damien Georged1e355e2014-05-28 14:51:12 +0100163 if (MP_SMALL_INT_FITS(value)) {
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200164 return MP_OBJ_NEW_SMALL_INT(value);
165 }
166 return mp_obj_new_int_from_ll(value);
167}
168
Damien George40f3c022014-07-03 13:25:24 +0100169mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200170 // SMALL_INT accepts only signed numbers, of one bit less size
171 // than word size, which totals 2 bits less for unsigned numbers.
172 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
173 return MP_OBJ_NEW_SMALL_INT(value);
174 }
175 return mp_obj_new_int_from_ll(value);
176}
177
178mp_obj_t mp_obj_new_int_from_ll(long long val) {
179 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000180 o->base.type = &mp_type_int;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200181 o->val = val;
182 return o;
183}
184
Damien George95307432014-09-10 22:10:33 +0100185mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
186 // TODO raise an exception if the unsigned long long won't fit
187 assert(val >> (sizeof(unsigned long long) * 8 - 1) == 0);
188 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
189 o->base.type = &mp_type_int;
190 o->val = val;
191 return o;
192}
193
Paul Sokolovsky12033df2014-12-30 00:22:10 +0200194#if MICROPY_PY_BUILTINS_FLOAT
195mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
196 // TODO raise an exception if the unsigned long long won't fit
197 long long i = MICROPY_FLOAT_C_FUN(trunc)(val);
198 return mp_obj_new_int_from_ll(i);
199}
200#endif
201
Damien George95307432014-09-10 22:10:33 +0100202mp_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 +0100203 // TODO this does not honor the given length of the string, but it all cases it should anyway be null terminated
204 // TODO check overflow
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200205 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000206 o->base.type = &mp_type_int;
Damien George503d6112014-05-28 14:07:21 +0100207 char *endptr;
208 o->val = strtoll(*str, &endptr, base);
209 *str = endptr;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200210 return o;
211}
212
Damien Georgebe6d8be2014-12-05 23:13:52 +0000213mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200214 if (MP_OBJ_IS_SMALL_INT(self_in)) {
215 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000216 } else {
Damien George503d6112014-05-28 14:07:21 +0100217 const mp_obj_int_t *self = self_in;
Damien Georgeeabdf672014-03-22 20:54:01 +0000218 return self->val;
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200219 }
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200220}
221
Damien George40f3c022014-07-03 13:25:24 +0100222mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200223 // TODO: Check overflow
Damien Georgebe6d8be2014-12-05 23:13:52 +0000224 return mp_obj_int_get_truncated(self_in);
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200225}
226
Damien Georgefb510b32014-06-01 13:32:54 +0100227#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgeeabdf672014-03-22 20:54:01 +0000228mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
229 if (MP_OBJ_IS_SMALL_INT(self_in)) {
230 return MP_OBJ_SMALL_INT_VALUE(self_in);
231 } else {
232 mp_obj_int_t *self = self_in;
233 return self->val;
234 }
235}
236#endif
237
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200238#endif