blob: 25a069cf9302587cdc2a2870c5103cd7c3804006 [file] [log] [blame]
Damien George438c88d2014-02-22 19:25:23 +00001#include <stdint.h>
2#include <string.h>
Rachel Dowdall56402792014-03-22 20:19:24 +00003#include <stdio.h>
Damien George438c88d2014-02-22 19:25:23 +00004
5#include "nlr.h"
6#include "misc.h"
7#include "mpconfig.h"
8#include "qstr.h"
Damien George06201ff2014-03-01 19:50:50 +00009#include "parsenumbase.h"
Damien George438c88d2014-02-22 19:25:23 +000010#include "obj.h"
11#include "mpz.h"
12#include "objint.h"
13#include "runtime0.h"
Damien George660aef62014-04-02 12:22:07 +010014#include "runtime.h"
Damien George438c88d2014-02-22 19:25:23 +000015
16#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
17
18STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) {
19 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +000020 o->base.type = &mp_type_int;
Damien George438c88d2014-02-22 19:25:23 +000021 mpz_init_zero(&o->mpz);
22 return o;
23}
24
25void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
26 if (MP_OBJ_IS_SMALL_INT(self_in)) {
27 print(env, INT_FMT, MP_OBJ_SMALL_INT_VALUE(self_in));
28 } else {
29 // TODO would rather not allocate memory to print...
30 mp_obj_int_t *self = self_in;
31 char *str = mpz_as_str(&self->mpz, 10);
32 print(env, "%s", str);
33 m_free(str, 0);
34 }
35}
36
37mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
38 mp_obj_int_t *o = o_in;
39 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010040 case MP_UNARY_OP_BOOL: return MP_BOOL(!mpz_is_zero(&o->mpz));
41 case MP_UNARY_OP_POSITIVE: return o_in;
42 case MP_UNARY_OP_NEGATIVE: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_neg_inpl(&o2->mpz, &o->mpz); return o2; }
43 case MP_UNARY_OP_INVERT: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_not_inpl(&o2->mpz, &o->mpz); return o2; }
Damien George438c88d2014-02-22 19:25:23 +000044 default: return NULL; // op not supported
45 }
46}
47
48mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georgecd8b2ba2014-03-19 23:15:25 +000049 const mpz_t *zlhs;
Damien George06201ff2014-03-01 19:50:50 +000050 const mpz_t *zrhs;
51 mpz_t z_int;
52 mpz_dig_t z_int_dig[MPZ_NUM_DIG_FOR_INT];
Damien George438c88d2014-02-22 19:25:23 +000053
Damien Georgecd8b2ba2014-03-19 23:15:25 +000054 // lhs could be a small int (eg small-int + mpz)
55 if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
56 mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(lhs_in));
57 zlhs = &z_int;
Damien George3e1a5c12014-03-29 13:43:38 +000058 } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
Damien Georgecd8b2ba2014-03-19 23:15:25 +000059 zlhs = &((mp_obj_int_t*)lhs_in)->mpz;
60 } else {
Damien George0aa5d512014-03-29 17:28:20 +000061 // unsupported type
Damien Georgecd8b2ba2014-03-19 23:15:25 +000062 return MP_OBJ_NULL;
63 }
64
Damien Georged17926d2014-03-30 13:35:08 +010065 // if rhs is small int, then lhs was not (otherwise mp_binary_op handles it)
Damien George438c88d2014-02-22 19:25:23 +000066 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
Damien George06201ff2014-03-01 19:50:50 +000067 mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(rhs_in));
68 zrhs = &z_int;
Damien George3e1a5c12014-03-29 13:43:38 +000069 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
Damien George438c88d2014-02-22 19:25:23 +000070 zrhs = &((mp_obj_int_t*)rhs_in)->mpz;
Damien George0aa5d512014-03-29 17:28:20 +000071#if MICROPY_ENABLE_FLOAT
72 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_float)) {
73 return mp_obj_float_binary_op(op, mpz_as_float(zlhs), rhs_in);
74 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
75 return mp_obj_complex_binary_op(op, mpz_as_float(zlhs), 0, rhs_in);
76#endif
Damien George438c88d2014-02-22 19:25:23 +000077 } else {
Damien George660aef62014-04-02 12:22:07 +010078 if (op == MP_BINARY_OP_MULTIPLY) {
79 if (MP_OBJ_IS_STR(rhs_in) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_list)) {
80 // multiply is commutative for these types, so delegate to them
81 return mp_binary_op(op, rhs_in, lhs_in);
82 }
83 }
84 // unsupported operation/type
Damien George438c88d2014-02-22 19:25:23 +000085 return MP_OBJ_NULL;
86 }
87
Damien George52608102014-03-08 15:04:54 +000088 if (0) {
89#if MICROPY_ENABLE_FLOAT
Damien Georged17926d2014-03-30 13:35:08 +010090 } else if (op == MP_BINARY_OP_TRUE_DIVIDE || op == MP_BINARY_OP_INPLACE_TRUE_DIVIDE) {
Damien George52608102014-03-08 15:04:54 +000091 mp_float_t flhs = mpz_as_float(zlhs);
92 mp_float_t frhs = mpz_as_float(zrhs);
Damien George438c88d2014-02-22 19:25:23 +000093 return mp_obj_new_float(flhs / frhs);
Damien George52608102014-03-08 15:04:54 +000094#endif
Damien George438c88d2014-02-22 19:25:23 +000095
Damien Georged17926d2014-03-30 13:35:08 +010096 } else if (op <= MP_BINARY_OP_INPLACE_POWER) {
Damien George438c88d2014-02-22 19:25:23 +000097 mp_obj_int_t *res = mp_obj_int_new_mpz();
98
99 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100100 case MP_BINARY_OP_ADD:
101 case MP_BINARY_OP_INPLACE_ADD:
Damien George438c88d2014-02-22 19:25:23 +0000102 mpz_add_inpl(&res->mpz, zlhs, zrhs);
103 break;
Damien Georged17926d2014-03-30 13:35:08 +0100104 case MP_BINARY_OP_SUBTRACT:
105 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien George438c88d2014-02-22 19:25:23 +0000106 mpz_sub_inpl(&res->mpz, zlhs, zrhs);
107 break;
Damien Georged17926d2014-03-30 13:35:08 +0100108 case MP_BINARY_OP_MULTIPLY:
109 case MP_BINARY_OP_INPLACE_MULTIPLY:
Damien George438c88d2014-02-22 19:25:23 +0000110 mpz_mul_inpl(&res->mpz, zlhs, zrhs);
111 break;
Damien Georged17926d2014-03-30 13:35:08 +0100112 case MP_BINARY_OP_FLOOR_DIVIDE:
113 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: {
Damien George438c88d2014-02-22 19:25:23 +0000114 mpz_t rem; mpz_init_zero(&rem);
115 mpz_divmod_inpl(&res->mpz, &rem, zlhs, zrhs);
Rachel Dowdall56402792014-03-22 20:19:24 +0000116 if (zlhs->neg != zrhs->neg) {
117 if (!mpz_is_zero(&rem)) {
118 mpz_t mpzone; mpz_init_from_int(&mpzone, -1);
119 mpz_add_inpl(&res->mpz, &res->mpz, &mpzone);
120 }
121 }
Damien George438c88d2014-02-22 19:25:23 +0000122 mpz_deinit(&rem);
123 break;
124 }
Damien Georged17926d2014-03-30 13:35:08 +0100125 case MP_BINARY_OP_MODULO:
126 case MP_BINARY_OP_INPLACE_MODULO: {
Damien George2d7ff072014-03-20 16:28:41 +0000127 mpz_t quo; mpz_init_zero(&quo);
128 mpz_divmod_inpl(&quo, &res->mpz, zlhs, zrhs);
129 mpz_deinit(&quo);
Rachel Dowdall56402792014-03-22 20:19:24 +0000130 // Check signs and do Python style modulo
131 if (zlhs->neg != zrhs->neg) {
Rachel Dowdallcde86312014-03-22 17:29:27 +0000132 mpz_add_inpl(&res->mpz, &res->mpz, zrhs);
133 }
Damien George2d7ff072014-03-20 16:28:41 +0000134 break;
135 }
Damien George438c88d2014-02-22 19:25:23 +0000136
Damien Georged17926d2014-03-30 13:35:08 +0100137 case MP_BINARY_OP_AND:
138 case MP_BINARY_OP_INPLACE_AND:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200139 mpz_and_inpl(&res->mpz, zlhs, zrhs);
140 break;
Damien Georged17926d2014-03-30 13:35:08 +0100141 case MP_BINARY_OP_OR:
142 case MP_BINARY_OP_INPLACE_OR:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200143 mpz_or_inpl(&res->mpz, zlhs, zrhs);
144 break;
Damien Georged17926d2014-03-30 13:35:08 +0100145 case MP_BINARY_OP_XOR:
146 case MP_BINARY_OP_INPLACE_XOR:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200147 mpz_xor_inpl(&res->mpz, zlhs, zrhs);
148 break;
Damien George438c88d2014-02-22 19:25:23 +0000149
Damien Georged17926d2014-03-30 13:35:08 +0100150 case MP_BINARY_OP_LSHIFT:
151 case MP_BINARY_OP_INPLACE_LSHIFT:
152 case MP_BINARY_OP_RSHIFT:
153 case MP_BINARY_OP_INPLACE_RSHIFT: {
Damien George06201ff2014-03-01 19:50:50 +0000154 // TODO check conversion overflow
155 machine_int_t irhs = mpz_as_int(zrhs);
156 if (irhs < 0) {
157 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
158 }
Damien Georged17926d2014-03-30 13:35:08 +0100159 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
Damien George06201ff2014-03-01 19:50:50 +0000160 mpz_shl_inpl(&res->mpz, zlhs, irhs);
161 } else {
162 mpz_shr_inpl(&res->mpz, zlhs, irhs);
163 }
164 break;
165 }
Damien George438c88d2014-02-22 19:25:23 +0000166
Damien Georged17926d2014-03-30 13:35:08 +0100167 case MP_BINARY_OP_POWER:
168 case MP_BINARY_OP_INPLACE_POWER:
Damien George438c88d2014-02-22 19:25:23 +0000169 mpz_pow_inpl(&res->mpz, zlhs, zrhs);
170 break;
171
172 default:
173 return MP_OBJ_NULL;
174 }
175
176 return res;
177
178 } else {
179 int cmp = mpz_cmp(zlhs, zrhs);
180 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100181 case MP_BINARY_OP_LESS:
Damien George438c88d2014-02-22 19:25:23 +0000182 return MP_BOOL(cmp < 0);
Damien Georged17926d2014-03-30 13:35:08 +0100183 case MP_BINARY_OP_MORE:
Damien George438c88d2014-02-22 19:25:23 +0000184 return MP_BOOL(cmp > 0);
Damien Georged17926d2014-03-30 13:35:08 +0100185 case MP_BINARY_OP_LESS_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000186 return MP_BOOL(cmp <= 0);
Damien Georged17926d2014-03-30 13:35:08 +0100187 case MP_BINARY_OP_MORE_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000188 return MP_BOOL(cmp >= 0);
Damien Georged17926d2014-03-30 13:35:08 +0100189 case MP_BINARY_OP_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000190 return MP_BOOL(cmp == 0);
Damien Georged17926d2014-03-30 13:35:08 +0100191 case MP_BINARY_OP_NOT_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000192 return MP_BOOL(cmp != 0);
193
194 default:
195 return MP_OBJ_NULL;
196 }
197 }
198}
199
200mp_obj_t mp_obj_new_int(machine_int_t value) {
201 if (MP_OBJ_FITS_SMALL_INT(value)) {
202 return MP_OBJ_NEW_SMALL_INT(value);
203 }
204 return mp_obj_new_int_from_ll(value);
205}
206
207mp_obj_t mp_obj_new_int_from_ll(long long val) {
208 mp_obj_int_t *o = mp_obj_int_new_mpz();
Damien George9d68e9c2014-03-12 15:38:15 +0000209 mpz_set_from_ll(&o->mpz, val);
Damien George438c88d2014-02-22 19:25:23 +0000210 return o;
211}
212
213mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
214 // SMALL_INT accepts only signed numbers, of one bit less size
215 // than word size, which totals 2 bits less for unsigned numbers.
216 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
217 return MP_OBJ_NEW_SMALL_INT(value);
218 }
219 return mp_obj_new_int_from_ll(value);
220}
221
222mp_obj_t mp_obj_new_int_from_long_str(const char *str) {
223 mp_obj_int_t *o = mp_obj_int_new_mpz();
224 uint len = strlen(str);
Damien George06201ff2014-03-01 19:50:50 +0000225 int base = 0;
226 int skip = mp_parse_num_base(str, len, &base);
227 str += skip;
228 len -= skip;
229 uint n = mpz_set_from_str(&o->mpz, str, len, false, base);
Damien George438c88d2014-02-22 19:25:23 +0000230 if (n != len) {
231 nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
232 }
233 return o;
234}
235
236machine_int_t mp_obj_int_get(mp_obj_t self_in) {
237 if (MP_OBJ_IS_SMALL_INT(self_in)) {
238 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000239 } else {
240 mp_obj_int_t *self = self_in;
241 return mpz_as_int(&self->mpz);
Damien George438c88d2014-02-22 19:25:23 +0000242 }
Damien George438c88d2014-02-22 19:25:23 +0000243}
244
245machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
Damien George8270e382014-04-03 11:00:54 +0000246 if (MP_OBJ_IS_SMALL_INT(self_in)) {
247 return MP_OBJ_SMALL_INT_VALUE(self_in);
248 } else {
249 mp_obj_int_t *self = self_in;
250 machine_int_t value;
251 if (mpz_as_int_checked(&self->mpz, &value)) {
252 return value;
253 } else {
254 // overflow
255 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "overflow converting long int to machine word"));
256 }
257 }
Damien George438c88d2014-02-22 19:25:23 +0000258}
259
Damien Georgeeabdf672014-03-22 20:54:01 +0000260#if MICROPY_ENABLE_FLOAT
261mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
262 if (MP_OBJ_IS_SMALL_INT(self_in)) {
263 return MP_OBJ_SMALL_INT_VALUE(self_in);
264 } else {
265 mp_obj_int_t *self = self_in;
266 return mpz_as_float(&self->mpz);
267 }
268}
269#endif
270
Damien George438c88d2014-02-22 19:25:23 +0000271#endif