blob: dfe069c087e3e75c66ee7fd6d45f7d5559f87dcc [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
Damien George88d7bba2014-04-08 23:30:46 +010053bool mp_obj_int_is_positive(mp_obj_t self_in) {
54 if (MP_OBJ_IS_SMALL_INT(self_in)) {
55 return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0;
56 }
57 mp_obj_int_t *self = self_in;
58 return self->val >= 0;
59}
60
Damien Georgee8208a72014-04-04 15:08:23 +010061mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020062 mp_obj_int_t *o = o_in;
63 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010064 case MP_UNARY_OP_BOOL: return MP_BOOL(o->val != 0);
65 case MP_UNARY_OP_POSITIVE: return o_in;
66 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val);
67 case MP_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val);
Damien George6ac5dce2014-05-21 19:42:43 +010068 default: return MP_OBJ_NULL; // op not supported
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020069 }
70}
71
Damien Georgee8208a72014-04-04 15:08:23 +010072mp_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 +000073 long long lhs_val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +020074 long long rhs_val;
75
Damien Georged02f6ea2014-03-20 19:31:32 +000076 if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
77 lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +000078 } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +000079 lhs_val = ((mp_obj_int_t*)lhs_in)->val;
80 } else {
Damien George6ac5dce2014-05-21 19:42:43 +010081 return MP_OBJ_NULL; // op not supported
Damien Georged02f6ea2014-03-20 19:31:32 +000082 }
Damien Georgec4129982014-03-19 23:17:23 +000083
Damien Georged02f6ea2014-03-20 19:31:32 +000084 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
85 rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +000086 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +000087 rhs_val = ((mp_obj_int_t*)rhs_in)->val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +020088 } else {
Damien Georgee8208a72014-04-04 15:08:23 +010089 // delegate to generic function to check for extra cases
90 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Paul Sokolovsky966879c2014-01-17 20:01:36 +020091 }
92
93 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010094 case MP_BINARY_OP_ADD:
95 case MP_BINARY_OP_INPLACE_ADD:
Damien Georged02f6ea2014-03-20 19:31:32 +000096 return mp_obj_new_int_from_ll(lhs_val + rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010097 case MP_BINARY_OP_SUBTRACT:
98 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien Georged02f6ea2014-03-20 19:31:32 +000099 return mp_obj_new_int_from_ll(lhs_val - rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100100 case MP_BINARY_OP_MULTIPLY:
101 case MP_BINARY_OP_INPLACE_MULTIPLY:
Damien Georged02f6ea2014-03-20 19:31:32 +0000102 return mp_obj_new_int_from_ll(lhs_val * rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100103 case MP_BINARY_OP_FLOOR_DIVIDE:
104 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Damien Georged02f6ea2014-03-20 19:31:32 +0000105 return mp_obj_new_int_from_ll(lhs_val / rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100106 case MP_BINARY_OP_MODULO:
107 case MP_BINARY_OP_INPLACE_MODULO:
Damien Georged02f6ea2014-03-20 19:31:32 +0000108 return mp_obj_new_int_from_ll(lhs_val % rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200109
Damien Georged17926d2014-03-30 13:35:08 +0100110 case MP_BINARY_OP_AND:
111 case MP_BINARY_OP_INPLACE_AND:
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_OR:
114 case MP_BINARY_OP_INPLACE_OR:
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_XOR:
117 case MP_BINARY_OP_INPLACE_XOR:
Damien Georged02f6ea2014-03-20 19:31:32 +0000118 return mp_obj_new_int_from_ll(lhs_val ^ rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200119
Damien Georged17926d2014-03-30 13:35:08 +0100120 case MP_BINARY_OP_LSHIFT:
121 case MP_BINARY_OP_INPLACE_LSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000122 return mp_obj_new_int_from_ll(lhs_val << (int)rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100123 case MP_BINARY_OP_RSHIFT:
124 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000125 return mp_obj_new_int_from_ll(lhs_val >> (int)rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200126
Damien Georged17926d2014-03-30 13:35:08 +0100127 case MP_BINARY_OP_LESS:
Damien Georged02f6ea2014-03-20 19:31:32 +0000128 return MP_BOOL(lhs_val < rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100129 case MP_BINARY_OP_MORE:
Damien Georged02f6ea2014-03-20 19:31:32 +0000130 return MP_BOOL(lhs_val > rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100131 case MP_BINARY_OP_LESS_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000132 return MP_BOOL(lhs_val <= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100133 case MP_BINARY_OP_MORE_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000134 return MP_BOOL(lhs_val >= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100135 case MP_BINARY_OP_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000136 return MP_BOOL(lhs_val == rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200137
138 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100139 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200140 }
141}
142
143mp_obj_t mp_obj_new_int(machine_int_t value) {
Damien Georged1e355e2014-05-28 14:51:12 +0100144 if (MP_SMALL_INT_FITS(value)) {
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200145 return MP_OBJ_NEW_SMALL_INT(value);
146 }
147 return mp_obj_new_int_from_ll(value);
148}
149
150mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
151 // SMALL_INT accepts only signed numbers, of one bit less size
152 // than word size, which totals 2 bits less for unsigned numbers.
153 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
154 return MP_OBJ_NEW_SMALL_INT(value);
155 }
156 return mp_obj_new_int_from_ll(value);
157}
158
159mp_obj_t mp_obj_new_int_from_ll(long long val) {
160 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000161 o->base.type = &mp_type_int;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200162 o->val = val;
163 return o;
164}
165
Damien George503d6112014-05-28 14:07:21 +0100166mp_obj_t mp_obj_new_int_from_str_len(const char **str, uint len, bool neg, uint base) {
167 // TODO this does not honor the given length of the string, but it all cases it should anyway be null terminated
168 // TODO check overflow
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200169 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000170 o->base.type = &mp_type_int;
Damien George503d6112014-05-28 14:07:21 +0100171 char *endptr;
172 o->val = strtoll(*str, &endptr, base);
173 *str = endptr;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200174 return o;
175}
176
Damien George503d6112014-05-28 14:07:21 +0100177machine_int_t mp_obj_int_get(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200178 if (MP_OBJ_IS_SMALL_INT(self_in)) {
179 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000180 } else {
Damien George503d6112014-05-28 14:07:21 +0100181 const mp_obj_int_t *self = self_in;
Damien Georgeeabdf672014-03-22 20:54:01 +0000182 return self->val;
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200183 }
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200184}
185
Paul Sokolovskyab7bf282014-05-17 11:08:33 +0300186machine_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200187 // TODO: Check overflow
188 return mp_obj_int_get(self_in);
189}
190
Damien Georgefb510b32014-06-01 13:32:54 +0100191#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgeeabdf672014-03-22 20:54:01 +0000192mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
193 if (MP_OBJ_IS_SMALL_INT(self_in)) {
194 return MP_OBJ_SMALL_INT_VALUE(self_in);
195 } else {
196 mp_obj_int_t *self = self_in;
197 return self->val;
198 }
199}
200#endif
201
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200202#endif