blob: 381246dfe46f12f3c1ce0e1ff6b47426bbcea523 [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 George438c88d2014-02-22 19:25:23 +000037#include "mpz.h"
Paul Sokolovsky966879c2014-01-17 20:01:36 +020038#include "objint.h"
39#include "runtime0.h"
Damien George660aef62014-04-02 12:22:07 +010040#include "runtime.h"
Paul Sokolovsky966879c2014-01-17 20:01:36 +020041
42#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
43
Paul Sokolovsky966879c2014-01-17 20:01:36 +020044// Python3 no longer has "l" suffix for long ints. We allow to use it
45// for debugging purpose though.
46#ifdef DEBUG
47#define SUFFIX "l"
48#else
49#define SUFFIX ""
50#endif
51
Damien George88d7bba2014-04-08 23:30:46 +010052bool mp_obj_int_is_positive(mp_obj_t self_in) {
53 if (MP_OBJ_IS_SMALL_INT(self_in)) {
54 return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0;
55 }
56 mp_obj_int_t *self = self_in;
57 return self->val >= 0;
58}
59
Damien Georgee8208a72014-04-04 15:08:23 +010060mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020061 mp_obj_int_t *o = o_in;
62 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010063 case MP_UNARY_OP_BOOL: return MP_BOOL(o->val != 0);
64 case MP_UNARY_OP_POSITIVE: return o_in;
65 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val);
66 case MP_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val);
Damien George6ac5dce2014-05-21 19:42:43 +010067 default: return MP_OBJ_NULL; // op not supported
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020068 }
69}
70
Damien Georgee8208a72014-04-04 15:08:23 +010071mp_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 +000072 long long lhs_val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +020073 long long rhs_val;
74
Damien Georged02f6ea2014-03-20 19:31:32 +000075 if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
76 lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +000077 } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +000078 lhs_val = ((mp_obj_int_t*)lhs_in)->val;
79 } else {
Damien George6ac5dce2014-05-21 19:42:43 +010080 return MP_OBJ_NULL; // op not supported
Damien Georged02f6ea2014-03-20 19:31:32 +000081 }
Damien Georgec4129982014-03-19 23:17:23 +000082
Damien Georged02f6ea2014-03-20 19:31:32 +000083 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
84 rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +000085 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +000086 rhs_val = ((mp_obj_int_t*)rhs_in)->val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +020087 } else {
Damien Georgee8208a72014-04-04 15:08:23 +010088 // delegate to generic function to check for extra cases
89 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Paul Sokolovsky966879c2014-01-17 20:01:36 +020090 }
91
92 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010093 case MP_BINARY_OP_ADD:
94 case MP_BINARY_OP_INPLACE_ADD:
Damien Georged02f6ea2014-03-20 19:31:32 +000095 return mp_obj_new_int_from_ll(lhs_val + rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010096 case MP_BINARY_OP_SUBTRACT:
97 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien Georged02f6ea2014-03-20 19:31:32 +000098 return mp_obj_new_int_from_ll(lhs_val - rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010099 case MP_BINARY_OP_MULTIPLY:
100 case MP_BINARY_OP_INPLACE_MULTIPLY:
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_FLOOR_DIVIDE:
103 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
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_MODULO:
106 case MP_BINARY_OP_INPLACE_MODULO:
Damien Georged02f6ea2014-03-20 19:31:32 +0000107 return mp_obj_new_int_from_ll(lhs_val % rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200108
Damien Georged17926d2014-03-30 13:35:08 +0100109 case MP_BINARY_OP_AND:
110 case MP_BINARY_OP_INPLACE_AND:
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_OR:
113 case MP_BINARY_OP_INPLACE_OR:
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_XOR:
116 case MP_BINARY_OP_INPLACE_XOR:
Damien Georged02f6ea2014-03-20 19:31:32 +0000117 return mp_obj_new_int_from_ll(lhs_val ^ rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200118
Damien Georged17926d2014-03-30 13:35:08 +0100119 case MP_BINARY_OP_LSHIFT:
120 case MP_BINARY_OP_INPLACE_LSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000121 return mp_obj_new_int_from_ll(lhs_val << (int)rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100122 case MP_BINARY_OP_RSHIFT:
123 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000124 return mp_obj_new_int_from_ll(lhs_val >> (int)rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200125
Damien Georged17926d2014-03-30 13:35:08 +0100126 case MP_BINARY_OP_LESS:
Damien Georged02f6ea2014-03-20 19:31:32 +0000127 return MP_BOOL(lhs_val < rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100128 case MP_BINARY_OP_MORE:
Damien Georged02f6ea2014-03-20 19:31:32 +0000129 return MP_BOOL(lhs_val > rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100130 case MP_BINARY_OP_LESS_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000131 return MP_BOOL(lhs_val <= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100132 case MP_BINARY_OP_MORE_EQUAL:
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_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000135 return MP_BOOL(lhs_val == rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200136
137 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100138 return MP_OBJ_NULL; // op not supported
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200139 }
140}
141
142mp_obj_t mp_obj_new_int(machine_int_t value) {
143 if (MP_OBJ_FITS_SMALL_INT(value)) {
144 return MP_OBJ_NEW_SMALL_INT(value);
145 }
146 return mp_obj_new_int_from_ll(value);
147}
148
149mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
150 // SMALL_INT accepts only signed numbers, of one bit less size
151 // than word size, which totals 2 bits less for unsigned numbers.
152 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
153 return MP_OBJ_NEW_SMALL_INT(value);
154 }
155 return mp_obj_new_int_from_ll(value);
156}
157
158mp_obj_t mp_obj_new_int_from_ll(long long val) {
159 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000160 o->base.type = &mp_type_int;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200161 o->val = val;
162 return o;
163}
164
Damien George503d6112014-05-28 14:07:21 +0100165mp_obj_t mp_obj_new_int_from_str_len(const char **str, uint len, bool neg, uint base) {
166 // TODO this does not honor the given length of the string, but it all cases it should anyway be null terminated
167 // TODO check overflow
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200168 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000169 o->base.type = &mp_type_int;
Damien George503d6112014-05-28 14:07:21 +0100170 char *endptr;
171 o->val = strtoll(*str, &endptr, base);
172 *str = endptr;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200173 return o;
174}
175
Damien George503d6112014-05-28 14:07:21 +0100176machine_int_t mp_obj_int_get(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200177 if (MP_OBJ_IS_SMALL_INT(self_in)) {
178 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000179 } else {
Damien George503d6112014-05-28 14:07:21 +0100180 const mp_obj_int_t *self = self_in;
Damien Georgeeabdf672014-03-22 20:54:01 +0000181 return self->val;
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200182 }
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200183}
184
Paul Sokolovskyab7bf282014-05-17 11:08:33 +0300185machine_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200186 // TODO: Check overflow
187 return mp_obj_int_get(self_in);
188}
189
Damien Georgeeabdf672014-03-22 20:54:01 +0000190#if MICROPY_ENABLE_FLOAT
191mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
192 if (MP_OBJ_IS_SMALL_INT(self_in)) {
193 return MP_OBJ_SMALL_INT_VALUE(self_in);
194 } else {
195 mp_obj_int_t *self = self_in;
196 return self->val;
197 }
198}
199#endif
200
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200201#endif