blob: 637d9c32c9629104bba3a6b4bfd1d97d6b625cbf [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
55const mp_obj_int_t mp_maxsize_obj = {{&mp_type_int}, INT_MAX};
56#endif
57
Damien George88d7bba2014-04-08 23:30:46 +010058bool mp_obj_int_is_positive(mp_obj_t self_in) {
59 if (MP_OBJ_IS_SMALL_INT(self_in)) {
60 return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0;
61 }
62 mp_obj_int_t *self = self_in;
63 return self->val >= 0;
64}
65
Damien Georgee8208a72014-04-04 15:08:23 +010066mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020067 mp_obj_int_t *o = o_in;
68 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010069 case MP_UNARY_OP_BOOL: return MP_BOOL(o->val != 0);
70 case MP_UNARY_OP_POSITIVE: return o_in;
71 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val);
72 case MP_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val);
Damien George6ac5dce2014-05-21 19:42:43 +010073 default: return MP_OBJ_NULL; // op not supported
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020074 }
75}
76
Damien Georgee8208a72014-04-04 15:08:23 +010077mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georged02f6ea2014-03-20 19:31:32 +000078 long long lhs_val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +020079 long long rhs_val;
80
Damien Georged02f6ea2014-03-20 19:31:32 +000081 if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
82 lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +000083 } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +000084 lhs_val = ((mp_obj_int_t*)lhs_in)->val;
85 } else {
Damien George6ac5dce2014-05-21 19:42:43 +010086 return MP_OBJ_NULL; // op not supported
Damien Georged02f6ea2014-03-20 19:31:32 +000087 }
Damien Georgec4129982014-03-19 23:17:23 +000088
Damien Georged02f6ea2014-03-20 19:31:32 +000089 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
90 rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +000091 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +000092 rhs_val = ((mp_obj_int_t*)rhs_in)->val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +020093 } else {
Damien Georgee8208a72014-04-04 15:08:23 +010094 // delegate to generic function to check for extra cases
95 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Paul Sokolovsky966879c2014-01-17 20:01:36 +020096 }
97
98 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010099 case MP_BINARY_OP_ADD:
100 case MP_BINARY_OP_INPLACE_ADD:
Damien Georged02f6ea2014-03-20 19:31:32 +0000101 return mp_obj_new_int_from_ll(lhs_val + rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100102 case MP_BINARY_OP_SUBTRACT:
103 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000104 return mp_obj_new_int_from_ll(lhs_val - rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100105 case MP_BINARY_OP_MULTIPLY:
106 case MP_BINARY_OP_INPLACE_MULTIPLY:
Damien Georged02f6ea2014-03-20 19:31:32 +0000107 return mp_obj_new_int_from_ll(lhs_val * rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100108 case MP_BINARY_OP_FLOOR_DIVIDE:
109 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Damien Georged02f6ea2014-03-20 19:31:32 +0000110 return mp_obj_new_int_from_ll(lhs_val / rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100111 case MP_BINARY_OP_MODULO:
112 case MP_BINARY_OP_INPLACE_MODULO:
Damien Georged02f6ea2014-03-20 19:31:32 +0000113 return mp_obj_new_int_from_ll(lhs_val % rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200114
Damien Georged17926d2014-03-30 13:35:08 +0100115 case MP_BINARY_OP_AND:
116 case MP_BINARY_OP_INPLACE_AND:
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_OR:
119 case MP_BINARY_OP_INPLACE_OR:
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_XOR:
122 case MP_BINARY_OP_INPLACE_XOR:
Damien Georged02f6ea2014-03-20 19:31:32 +0000123 return mp_obj_new_int_from_ll(lhs_val ^ rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200124
Damien Georged17926d2014-03-30 13:35:08 +0100125 case MP_BINARY_OP_LSHIFT:
126 case MP_BINARY_OP_INPLACE_LSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000127 return mp_obj_new_int_from_ll(lhs_val << (int)rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100128 case MP_BINARY_OP_RSHIFT:
129 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000130 return mp_obj_new_int_from_ll(lhs_val >> (int)rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200131
Damien Georged17926d2014-03-30 13:35:08 +0100132 case MP_BINARY_OP_LESS:
Damien Georged02f6ea2014-03-20 19:31:32 +0000133 return MP_BOOL(lhs_val < rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100134 case MP_BINARY_OP_MORE:
Damien Georged02f6ea2014-03-20 19:31:32 +0000135 return MP_BOOL(lhs_val > rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100136 case MP_BINARY_OP_LESS_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000137 return MP_BOOL(lhs_val <= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100138 case MP_BINARY_OP_MORE_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000139 return MP_BOOL(lhs_val >= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100140 case MP_BINARY_OP_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000141 return MP_BOOL(lhs_val == rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200142
143 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100144 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200145 }
146}
147
Damien George40f3c022014-07-03 13:25:24 +0100148mp_obj_t mp_obj_new_int(mp_int_t value) {
Damien Georged1e355e2014-05-28 14:51:12 +0100149 if (MP_SMALL_INT_FITS(value)) {
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200150 return MP_OBJ_NEW_SMALL_INT(value);
151 }
152 return mp_obj_new_int_from_ll(value);
153}
154
Damien George40f3c022014-07-03 13:25:24 +0100155mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200156 // SMALL_INT accepts only signed numbers, of one bit less size
157 // than word size, which totals 2 bits less for unsigned numbers.
158 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
159 return MP_OBJ_NEW_SMALL_INT(value);
160 }
161 return mp_obj_new_int_from_ll(value);
162}
163
164mp_obj_t mp_obj_new_int_from_ll(long long val) {
165 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000166 o->base.type = &mp_type_int;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200167 o->val = val;
168 return o;
169}
170
Damien George503d6112014-05-28 14:07:21 +0100171mp_obj_t mp_obj_new_int_from_str_len(const char **str, uint len, bool neg, uint base) {
172 // TODO this does not honor the given length of the string, but it all cases it should anyway be null terminated
173 // TODO check overflow
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200174 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000175 o->base.type = &mp_type_int;
Damien George503d6112014-05-28 14:07:21 +0100176 char *endptr;
177 o->val = strtoll(*str, &endptr, base);
178 *str = endptr;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200179 return o;
180}
181
Damien George40f3c022014-07-03 13:25:24 +0100182mp_int_t mp_obj_int_get(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200183 if (MP_OBJ_IS_SMALL_INT(self_in)) {
184 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000185 } else {
Damien George503d6112014-05-28 14:07:21 +0100186 const mp_obj_int_t *self = self_in;
Damien Georgeeabdf672014-03-22 20:54:01 +0000187 return self->val;
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200188 }
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200189}
190
Damien George40f3c022014-07-03 13:25:24 +0100191mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200192 // TODO: Check overflow
193 return mp_obj_int_get(self_in);
194}
195
Damien Georgefb510b32014-06-01 13:32:54 +0100196#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgeeabdf672014-03-22 20:54:01 +0000197mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
198 if (MP_OBJ_IS_SMALL_INT(self_in)) {
199 return MP_OBJ_SMALL_INT_VALUE(self_in);
200 } else {
201 mp_obj_int_t *self = self_in;
202 return self->val;
203 }
204}
205#endif
206
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200207#endif