blob: 583ce4cb7898fd2dc65d522ceedcfeeaa899569e [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 George88d7bba2014-04-08 23:30:46 +01004#include <assert.h>
Damien George438c88d2014-02-22 19:25:23 +00005
6#include "nlr.h"
7#include "misc.h"
8#include "mpconfig.h"
9#include "qstr.h"
Damien George06201ff2014-03-01 19:50:50 +000010#include "parsenumbase.h"
Damien George438c88d2014-02-22 19:25:23 +000011#include "obj.h"
12#include "mpz.h"
13#include "objint.h"
14#include "runtime0.h"
Damien George660aef62014-04-02 12:22:07 +010015#include "runtime.h"
Damien George438c88d2014-02-22 19:25:23 +000016
17#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
18
19STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) {
20 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +000021 o->base.type = &mp_type_int;
Damien George438c88d2014-02-22 19:25:23 +000022 mpz_init_zero(&o->mpz);
23 return o;
24}
25
Dave Hylandsc4029e52014-04-07 11:19:51 -070026// This routine expects you to pass in a buffer and size (in *buf and buf_size).
27// If, for some reason, this buffer is too small, then it will allocate a
28// buffer and return the allocated buffer and size in *buf and *buf_size. It
29// is the callers responsibility to free this allocated buffer.
30//
31// The resulting formatted string will be returned from this function and the
32// formatted size will be in *fmt_size.
Damien George88d7bba2014-04-08 23:30:46 +010033//
34// This particular routine should only be called for the mpz representation of the int.
35char *mp_obj_int_formatted_impl(char **buf, int *buf_size, int *fmt_size, mp_obj_t self_in,
36 int base, const char *prefix, char base_char, char comma) {
37 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
38 mp_obj_int_t *self = self_in;
Dave Hylandsc4029e52014-04-07 11:19:51 -070039
Damien George88d7bba2014-04-08 23:30:46 +010040 uint needed_size = mpz_as_str_size_formatted(&self->mpz, base, prefix, comma);
Dave Hylandsc4029e52014-04-07 11:19:51 -070041 if (needed_size > *buf_size) {
42 *buf = m_new(char, needed_size);
43 *buf_size = needed_size;
44 }
45 char *str = *buf;
46
Damien George88d7bba2014-04-08 23:30:46 +010047 *fmt_size = mpz_as_str_inpl(&self->mpz, base, prefix, base_char, comma, str);
Dave Hylandsc4029e52014-04-07 11:19:51 -070048
49 return str;
50}
51
52bool 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->mpz.neg;
Damien George438c88d2014-02-22 19:25:23 +000058}
59
Damien Georgee8208a72014-04-04 15:08:23 +010060mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
Damien George438c88d2014-02-22 19:25:23 +000061 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(!mpz_is_zero(&o->mpz));
64 case MP_UNARY_OP_POSITIVE: return o_in;
65 case MP_UNARY_OP_NEGATIVE: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_neg_inpl(&o2->mpz, &o->mpz); return o2; }
66 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 +000067 default: return NULL; // op not supported
68 }
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 Georgecd8b2ba2014-03-19 23:15:25 +000072 const mpz_t *zlhs;
Damien George06201ff2014-03-01 19:50:50 +000073 const mpz_t *zrhs;
74 mpz_t z_int;
75 mpz_dig_t z_int_dig[MPZ_NUM_DIG_FOR_INT];
Damien George438c88d2014-02-22 19:25:23 +000076
Damien Georgecd8b2ba2014-03-19 23:15:25 +000077 // lhs could be a small int (eg small-int + mpz)
78 if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
79 mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(lhs_in));
80 zlhs = &z_int;
Damien George3e1a5c12014-03-29 13:43:38 +000081 } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
Damien Georgecd8b2ba2014-03-19 23:15:25 +000082 zlhs = &((mp_obj_int_t*)lhs_in)->mpz;
83 } else {
Damien George0aa5d512014-03-29 17:28:20 +000084 // unsupported type
Damien Georgecd8b2ba2014-03-19 23:15:25 +000085 return MP_OBJ_NULL;
86 }
87
Damien Georged17926d2014-03-30 13:35:08 +010088 // if rhs is small int, then lhs was not (otherwise mp_binary_op handles it)
Damien George438c88d2014-02-22 19:25:23 +000089 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
Damien George06201ff2014-03-01 19:50:50 +000090 mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(rhs_in));
91 zrhs = &z_int;
Damien George3e1a5c12014-03-29 13:43:38 +000092 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
Damien George438c88d2014-02-22 19:25:23 +000093 zrhs = &((mp_obj_int_t*)rhs_in)->mpz;
Damien George0aa5d512014-03-29 17:28:20 +000094#if MICROPY_ENABLE_FLOAT
95 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_float)) {
96 return mp_obj_float_binary_op(op, mpz_as_float(zlhs), rhs_in);
97 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
98 return mp_obj_complex_binary_op(op, mpz_as_float(zlhs), 0, rhs_in);
99#endif
Damien George438c88d2014-02-22 19:25:23 +0000100 } else {
Damien Georgee8208a72014-04-04 15:08:23 +0100101 // delegate to generic function to check for extra cases
102 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Damien George438c88d2014-02-22 19:25:23 +0000103 }
104
Damien George52608102014-03-08 15:04:54 +0000105 if (0) {
106#if MICROPY_ENABLE_FLOAT
Damien Georged17926d2014-03-30 13:35:08 +0100107 } else if (op == MP_BINARY_OP_TRUE_DIVIDE || op == MP_BINARY_OP_INPLACE_TRUE_DIVIDE) {
Damien George52608102014-03-08 15:04:54 +0000108 mp_float_t flhs = mpz_as_float(zlhs);
109 mp_float_t frhs = mpz_as_float(zrhs);
Damien George438c88d2014-02-22 19:25:23 +0000110 return mp_obj_new_float(flhs / frhs);
Damien George52608102014-03-08 15:04:54 +0000111#endif
Damien George438c88d2014-02-22 19:25:23 +0000112
Damien Georged17926d2014-03-30 13:35:08 +0100113 } else if (op <= MP_BINARY_OP_INPLACE_POWER) {
Damien George438c88d2014-02-22 19:25:23 +0000114 mp_obj_int_t *res = mp_obj_int_new_mpz();
115
116 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100117 case MP_BINARY_OP_ADD:
118 case MP_BINARY_OP_INPLACE_ADD:
Damien George438c88d2014-02-22 19:25:23 +0000119 mpz_add_inpl(&res->mpz, zlhs, zrhs);
120 break;
Damien Georged17926d2014-03-30 13:35:08 +0100121 case MP_BINARY_OP_SUBTRACT:
122 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien George438c88d2014-02-22 19:25:23 +0000123 mpz_sub_inpl(&res->mpz, zlhs, zrhs);
124 break;
Damien Georged17926d2014-03-30 13:35:08 +0100125 case MP_BINARY_OP_MULTIPLY:
126 case MP_BINARY_OP_INPLACE_MULTIPLY:
Damien George438c88d2014-02-22 19:25:23 +0000127 mpz_mul_inpl(&res->mpz, zlhs, zrhs);
128 break;
Damien Georged17926d2014-03-30 13:35:08 +0100129 case MP_BINARY_OP_FLOOR_DIVIDE:
130 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: {
Damien George438c88d2014-02-22 19:25:23 +0000131 mpz_t rem; mpz_init_zero(&rem);
132 mpz_divmod_inpl(&res->mpz, &rem, zlhs, zrhs);
Damien Georgeecf5b772014-04-04 11:13:51 +0000133 if (zlhs->neg != zrhs->neg) {
Rachel Dowdall56402792014-03-22 20:19:24 +0000134 if (!mpz_is_zero(&rem)) {
135 mpz_t mpzone; mpz_init_from_int(&mpzone, -1);
136 mpz_add_inpl(&res->mpz, &res->mpz, &mpzone);
137 }
138 }
Damien George438c88d2014-02-22 19:25:23 +0000139 mpz_deinit(&rem);
140 break;
141 }
Damien Georged17926d2014-03-30 13:35:08 +0100142 case MP_BINARY_OP_MODULO:
143 case MP_BINARY_OP_INPLACE_MODULO: {
Damien George2d7ff072014-03-20 16:28:41 +0000144 mpz_t quo; mpz_init_zero(&quo);
145 mpz_divmod_inpl(&quo, &res->mpz, zlhs, zrhs);
146 mpz_deinit(&quo);
Damien Georgeecf5b772014-04-04 11:13:51 +0000147 // Check signs and do Python style modulo
148 if (zlhs->neg != zrhs->neg) {
Rachel Dowdallcde86312014-03-22 17:29:27 +0000149 mpz_add_inpl(&res->mpz, &res->mpz, zrhs);
150 }
Damien George2d7ff072014-03-20 16:28:41 +0000151 break;
152 }
Damien George438c88d2014-02-22 19:25:23 +0000153
Damien Georged17926d2014-03-30 13:35:08 +0100154 case MP_BINARY_OP_AND:
155 case MP_BINARY_OP_INPLACE_AND:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200156 mpz_and_inpl(&res->mpz, zlhs, zrhs);
157 break;
Damien Georged17926d2014-03-30 13:35:08 +0100158 case MP_BINARY_OP_OR:
159 case MP_BINARY_OP_INPLACE_OR:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200160 mpz_or_inpl(&res->mpz, zlhs, zrhs);
161 break;
Damien Georged17926d2014-03-30 13:35:08 +0100162 case MP_BINARY_OP_XOR:
163 case MP_BINARY_OP_INPLACE_XOR:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200164 mpz_xor_inpl(&res->mpz, zlhs, zrhs);
165 break;
Damien George438c88d2014-02-22 19:25:23 +0000166
Damien Georged17926d2014-03-30 13:35:08 +0100167 case MP_BINARY_OP_LSHIFT:
168 case MP_BINARY_OP_INPLACE_LSHIFT:
169 case MP_BINARY_OP_RSHIFT:
170 case MP_BINARY_OP_INPLACE_RSHIFT: {
Damien George06201ff2014-03-01 19:50:50 +0000171 // TODO check conversion overflow
172 machine_int_t irhs = mpz_as_int(zrhs);
173 if (irhs < 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100174 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
Damien George06201ff2014-03-01 19:50:50 +0000175 }
Damien Georged17926d2014-03-30 13:35:08 +0100176 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
Damien George06201ff2014-03-01 19:50:50 +0000177 mpz_shl_inpl(&res->mpz, zlhs, irhs);
178 } else {
179 mpz_shr_inpl(&res->mpz, zlhs, irhs);
180 }
181 break;
182 }
Damien George438c88d2014-02-22 19:25:23 +0000183
Damien Georged17926d2014-03-30 13:35:08 +0100184 case MP_BINARY_OP_POWER:
185 case MP_BINARY_OP_INPLACE_POWER:
Damien George438c88d2014-02-22 19:25:23 +0000186 mpz_pow_inpl(&res->mpz, zlhs, zrhs);
187 break;
188
189 default:
190 return MP_OBJ_NULL;
191 }
192
193 return res;
194
195 } else {
196 int cmp = mpz_cmp(zlhs, zrhs);
197 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100198 case MP_BINARY_OP_LESS:
Damien George438c88d2014-02-22 19:25:23 +0000199 return MP_BOOL(cmp < 0);
Damien Georged17926d2014-03-30 13:35:08 +0100200 case MP_BINARY_OP_MORE:
Damien George438c88d2014-02-22 19:25:23 +0000201 return MP_BOOL(cmp > 0);
Damien Georged17926d2014-03-30 13:35:08 +0100202 case MP_BINARY_OP_LESS_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000203 return MP_BOOL(cmp <= 0);
Damien Georged17926d2014-03-30 13:35:08 +0100204 case MP_BINARY_OP_MORE_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000205 return MP_BOOL(cmp >= 0);
Damien Georged17926d2014-03-30 13:35:08 +0100206 case MP_BINARY_OP_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000207 return MP_BOOL(cmp == 0);
Damien Georged17926d2014-03-30 13:35:08 +0100208 case MP_BINARY_OP_NOT_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000209 return MP_BOOL(cmp != 0);
210
211 default:
212 return MP_OBJ_NULL;
213 }
214 }
215}
216
217mp_obj_t mp_obj_new_int(machine_int_t value) {
218 if (MP_OBJ_FITS_SMALL_INT(value)) {
219 return MP_OBJ_NEW_SMALL_INT(value);
220 }
221 return mp_obj_new_int_from_ll(value);
222}
223
224mp_obj_t mp_obj_new_int_from_ll(long long val) {
225 mp_obj_int_t *o = mp_obj_int_new_mpz();
Damien George9d68e9c2014-03-12 15:38:15 +0000226 mpz_set_from_ll(&o->mpz, val);
Damien George438c88d2014-02-22 19:25:23 +0000227 return o;
228}
229
230mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
231 // SMALL_INT accepts only signed numbers, of one bit less size
232 // than word size, which totals 2 bits less for unsigned numbers.
233 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
234 return MP_OBJ_NEW_SMALL_INT(value);
235 }
236 return mp_obj_new_int_from_ll(value);
237}
238
239mp_obj_t mp_obj_new_int_from_long_str(const char *str) {
240 mp_obj_int_t *o = mp_obj_int_new_mpz();
241 uint len = strlen(str);
Damien George06201ff2014-03-01 19:50:50 +0000242 int base = 0;
243 int skip = mp_parse_num_base(str, len, &base);
244 str += skip;
245 len -= skip;
246 uint n = mpz_set_from_str(&o->mpz, str, len, false, base);
Damien George438c88d2014-02-22 19:25:23 +0000247 if (n != len) {
Damien Georgeea13f402014-04-05 18:32:08 +0100248 nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
Damien George438c88d2014-02-22 19:25:23 +0000249 }
250 return o;
251}
252
253machine_int_t mp_obj_int_get(mp_obj_t self_in) {
254 if (MP_OBJ_IS_SMALL_INT(self_in)) {
255 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000256 } else {
257 mp_obj_int_t *self = self_in;
258 return mpz_as_int(&self->mpz);
Damien George438c88d2014-02-22 19:25:23 +0000259 }
Damien George438c88d2014-02-22 19:25:23 +0000260}
261
262machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
Damien George8270e382014-04-03 11:00:54 +0000263 if (MP_OBJ_IS_SMALL_INT(self_in)) {
264 return MP_OBJ_SMALL_INT_VALUE(self_in);
265 } else {
266 mp_obj_int_t *self = self_in;
267 machine_int_t value;
268 if (mpz_as_int_checked(&self->mpz, &value)) {
269 return value;
270 } else {
271 // overflow
Damien Georgeea13f402014-04-05 18:32:08 +0100272 nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "overflow converting long int to machine word"));
Damien George8270e382014-04-03 11:00:54 +0000273 }
274 }
Damien George438c88d2014-02-22 19:25:23 +0000275}
276
Damien Georgeeabdf672014-03-22 20:54:01 +0000277#if MICROPY_ENABLE_FLOAT
278mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
279 if (MP_OBJ_IS_SMALL_INT(self_in)) {
280 return MP_OBJ_SMALL_INT_VALUE(self_in);
281 } else {
282 mp_obj_int_t *self = self_in;
283 return mpz_as_float(&self->mpz);
284 }
285}
286#endif
287
Damien George438c88d2014-02-22 19:25:23 +0000288#endif