blob: 7f12fbcd0f6ff5bbad9f1990e112c19aac601f38 [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"
14
15#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
16
17STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) {
18 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +000019 o->base.type = &mp_type_int;
Damien George438c88d2014-02-22 19:25:23 +000020 mpz_init_zero(&o->mpz);
21 return o;
22}
23
24void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
25 if (MP_OBJ_IS_SMALL_INT(self_in)) {
26 print(env, INT_FMT, MP_OBJ_SMALL_INT_VALUE(self_in));
27 } else {
28 // TODO would rather not allocate memory to print...
29 mp_obj_int_t *self = self_in;
30 char *str = mpz_as_str(&self->mpz, 10);
31 print(env, "%s", str);
32 m_free(str, 0);
33 }
34}
35
36mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
37 mp_obj_int_t *o = o_in;
38 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010039 case MP_UNARY_OP_BOOL: return MP_BOOL(!mpz_is_zero(&o->mpz));
40 case MP_UNARY_OP_POSITIVE: return o_in;
41 case MP_UNARY_OP_NEGATIVE: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_neg_inpl(&o2->mpz, &o->mpz); return o2; }
42 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 +000043 default: return NULL; // op not supported
44 }
45}
46
47mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georgecd8b2ba2014-03-19 23:15:25 +000048 const mpz_t *zlhs;
Damien George06201ff2014-03-01 19:50:50 +000049 const mpz_t *zrhs;
50 mpz_t z_int;
51 mpz_dig_t z_int_dig[MPZ_NUM_DIG_FOR_INT];
Damien George438c88d2014-02-22 19:25:23 +000052
Damien Georgecd8b2ba2014-03-19 23:15:25 +000053 // lhs could be a small int (eg small-int + mpz)
54 if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
55 mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(lhs_in));
56 zlhs = &z_int;
Damien George3e1a5c12014-03-29 13:43:38 +000057 } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
Damien Georgecd8b2ba2014-03-19 23:15:25 +000058 zlhs = &((mp_obj_int_t*)lhs_in)->mpz;
59 } else {
Damien George0aa5d512014-03-29 17:28:20 +000060 // unsupported type
Damien Georgecd8b2ba2014-03-19 23:15:25 +000061 return MP_OBJ_NULL;
62 }
63
Damien Georged17926d2014-03-30 13:35:08 +010064 // if rhs is small int, then lhs was not (otherwise mp_binary_op handles it)
Damien George438c88d2014-02-22 19:25:23 +000065 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
Damien George06201ff2014-03-01 19:50:50 +000066 mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(rhs_in));
67 zrhs = &z_int;
Damien George3e1a5c12014-03-29 13:43:38 +000068 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
Damien George438c88d2014-02-22 19:25:23 +000069 zrhs = &((mp_obj_int_t*)rhs_in)->mpz;
Damien George0aa5d512014-03-29 17:28:20 +000070#if MICROPY_ENABLE_FLOAT
71 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_float)) {
72 return mp_obj_float_binary_op(op, mpz_as_float(zlhs), rhs_in);
73 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
74 return mp_obj_complex_binary_op(op, mpz_as_float(zlhs), 0, rhs_in);
75#endif
Damien George438c88d2014-02-22 19:25:23 +000076 } else {
Damien George0aa5d512014-03-29 17:28:20 +000077 // unsupported type
Damien George438c88d2014-02-22 19:25:23 +000078 return MP_OBJ_NULL;
79 }
80
Damien George52608102014-03-08 15:04:54 +000081 if (0) {
82#if MICROPY_ENABLE_FLOAT
Damien Georged17926d2014-03-30 13:35:08 +010083 } else if (op == MP_BINARY_OP_TRUE_DIVIDE || op == MP_BINARY_OP_INPLACE_TRUE_DIVIDE) {
Damien George52608102014-03-08 15:04:54 +000084 mp_float_t flhs = mpz_as_float(zlhs);
85 mp_float_t frhs = mpz_as_float(zrhs);
Damien George438c88d2014-02-22 19:25:23 +000086 return mp_obj_new_float(flhs / frhs);
Damien George52608102014-03-08 15:04:54 +000087#endif
Damien George438c88d2014-02-22 19:25:23 +000088
Damien Georged17926d2014-03-30 13:35:08 +010089 } else if (op <= MP_BINARY_OP_INPLACE_POWER) {
Damien George438c88d2014-02-22 19:25:23 +000090 mp_obj_int_t *res = mp_obj_int_new_mpz();
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 George438c88d2014-02-22 19:25:23 +000095 mpz_add_inpl(&res->mpz, zlhs, zrhs);
96 break;
Damien Georged17926d2014-03-30 13:35:08 +010097 case MP_BINARY_OP_SUBTRACT:
98 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien George438c88d2014-02-22 19:25:23 +000099 mpz_sub_inpl(&res->mpz, zlhs, zrhs);
100 break;
Damien Georged17926d2014-03-30 13:35:08 +0100101 case MP_BINARY_OP_MULTIPLY:
102 case MP_BINARY_OP_INPLACE_MULTIPLY:
Damien George438c88d2014-02-22 19:25:23 +0000103 mpz_mul_inpl(&res->mpz, zlhs, zrhs);
104 break;
Damien Georged17926d2014-03-30 13:35:08 +0100105 case MP_BINARY_OP_FLOOR_DIVIDE:
106 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: {
Damien George438c88d2014-02-22 19:25:23 +0000107 mpz_t rem; mpz_init_zero(&rem);
108 mpz_divmod_inpl(&res->mpz, &rem, zlhs, zrhs);
Rachel Dowdall56402792014-03-22 20:19:24 +0000109 if (zlhs->neg != zrhs->neg) {
110 if (!mpz_is_zero(&rem)) {
111 mpz_t mpzone; mpz_init_from_int(&mpzone, -1);
112 mpz_add_inpl(&res->mpz, &res->mpz, &mpzone);
113 }
114 }
Damien George438c88d2014-02-22 19:25:23 +0000115 mpz_deinit(&rem);
116 break;
117 }
Damien Georged17926d2014-03-30 13:35:08 +0100118 case MP_BINARY_OP_MODULO:
119 case MP_BINARY_OP_INPLACE_MODULO: {
Damien George2d7ff072014-03-20 16:28:41 +0000120 mpz_t quo; mpz_init_zero(&quo);
121 mpz_divmod_inpl(&quo, &res->mpz, zlhs, zrhs);
122 mpz_deinit(&quo);
Rachel Dowdall56402792014-03-22 20:19:24 +0000123 // Check signs and do Python style modulo
124 if (zlhs->neg != zrhs->neg) {
Rachel Dowdallcde86312014-03-22 17:29:27 +0000125 mpz_add_inpl(&res->mpz, &res->mpz, zrhs);
126 }
Damien George2d7ff072014-03-20 16:28:41 +0000127 break;
128 }
Damien George438c88d2014-02-22 19:25:23 +0000129
Damien Georged17926d2014-03-30 13:35:08 +0100130 case MP_BINARY_OP_AND:
131 case MP_BINARY_OP_INPLACE_AND:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200132 mpz_and_inpl(&res->mpz, zlhs, zrhs);
133 break;
Damien Georged17926d2014-03-30 13:35:08 +0100134 case MP_BINARY_OP_OR:
135 case MP_BINARY_OP_INPLACE_OR:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200136 mpz_or_inpl(&res->mpz, zlhs, zrhs);
137 break;
Damien Georged17926d2014-03-30 13:35:08 +0100138 case MP_BINARY_OP_XOR:
139 case MP_BINARY_OP_INPLACE_XOR:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200140 mpz_xor_inpl(&res->mpz, zlhs, zrhs);
141 break;
Damien George438c88d2014-02-22 19:25:23 +0000142
Damien Georged17926d2014-03-30 13:35:08 +0100143 case MP_BINARY_OP_LSHIFT:
144 case MP_BINARY_OP_INPLACE_LSHIFT:
145 case MP_BINARY_OP_RSHIFT:
146 case MP_BINARY_OP_INPLACE_RSHIFT: {
Damien George06201ff2014-03-01 19:50:50 +0000147 // TODO check conversion overflow
148 machine_int_t irhs = mpz_as_int(zrhs);
149 if (irhs < 0) {
150 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
151 }
Damien Georged17926d2014-03-30 13:35:08 +0100152 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
Damien George06201ff2014-03-01 19:50:50 +0000153 mpz_shl_inpl(&res->mpz, zlhs, irhs);
154 } else {
155 mpz_shr_inpl(&res->mpz, zlhs, irhs);
156 }
157 break;
158 }
Damien George438c88d2014-02-22 19:25:23 +0000159
Damien Georged17926d2014-03-30 13:35:08 +0100160 case MP_BINARY_OP_POWER:
161 case MP_BINARY_OP_INPLACE_POWER:
Damien George438c88d2014-02-22 19:25:23 +0000162 mpz_pow_inpl(&res->mpz, zlhs, zrhs);
163 break;
164
165 default:
166 return MP_OBJ_NULL;
167 }
168
169 return res;
170
171 } else {
172 int cmp = mpz_cmp(zlhs, zrhs);
173 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100174 case MP_BINARY_OP_LESS:
Damien George438c88d2014-02-22 19:25:23 +0000175 return MP_BOOL(cmp < 0);
Damien Georged17926d2014-03-30 13:35:08 +0100176 case MP_BINARY_OP_MORE:
Damien George438c88d2014-02-22 19:25:23 +0000177 return MP_BOOL(cmp > 0);
Damien Georged17926d2014-03-30 13:35:08 +0100178 case MP_BINARY_OP_LESS_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000179 return MP_BOOL(cmp <= 0);
Damien Georged17926d2014-03-30 13:35:08 +0100180 case MP_BINARY_OP_MORE_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000181 return MP_BOOL(cmp >= 0);
Damien Georged17926d2014-03-30 13:35:08 +0100182 case MP_BINARY_OP_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000183 return MP_BOOL(cmp == 0);
Damien Georged17926d2014-03-30 13:35:08 +0100184 case MP_BINARY_OP_NOT_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000185 return MP_BOOL(cmp != 0);
186
187 default:
188 return MP_OBJ_NULL;
189 }
190 }
191}
192
193mp_obj_t mp_obj_new_int(machine_int_t value) {
194 if (MP_OBJ_FITS_SMALL_INT(value)) {
195 return MP_OBJ_NEW_SMALL_INT(value);
196 }
197 return mp_obj_new_int_from_ll(value);
198}
199
200mp_obj_t mp_obj_new_int_from_ll(long long val) {
201 mp_obj_int_t *o = mp_obj_int_new_mpz();
Damien George9d68e9c2014-03-12 15:38:15 +0000202 mpz_set_from_ll(&o->mpz, val);
Damien George438c88d2014-02-22 19:25:23 +0000203 return o;
204}
205
206mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
207 // SMALL_INT accepts only signed numbers, of one bit less size
208 // than word size, which totals 2 bits less for unsigned numbers.
209 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
210 return MP_OBJ_NEW_SMALL_INT(value);
211 }
212 return mp_obj_new_int_from_ll(value);
213}
214
215mp_obj_t mp_obj_new_int_from_long_str(const char *str) {
216 mp_obj_int_t *o = mp_obj_int_new_mpz();
217 uint len = strlen(str);
Damien George06201ff2014-03-01 19:50:50 +0000218 int base = 0;
219 int skip = mp_parse_num_base(str, len, &base);
220 str += skip;
221 len -= skip;
222 uint n = mpz_set_from_str(&o->mpz, str, len, false, base);
Damien George438c88d2014-02-22 19:25:23 +0000223 if (n != len) {
224 nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
225 }
226 return o;
227}
228
229machine_int_t mp_obj_int_get(mp_obj_t self_in) {
230 if (MP_OBJ_IS_SMALL_INT(self_in)) {
231 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000232 } else {
233 mp_obj_int_t *self = self_in;
234 return mpz_as_int(&self->mpz);
Damien George438c88d2014-02-22 19:25:23 +0000235 }
Damien George438c88d2014-02-22 19:25:23 +0000236}
237
238machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
239 // TODO: Check overflow
240 return mp_obj_int_get(self_in);
241}
242
Damien Georgeeabdf672014-03-22 20:54:01 +0000243#if MICROPY_ENABLE_FLOAT
244mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
245 if (MP_OBJ_IS_SMALL_INT(self_in)) {
246 return MP_OBJ_SMALL_INT_VALUE(self_in);
247 } else {
248 mp_obj_int_t *self = self_in;
249 return mpz_as_float(&self->mpz);
250 }
251}
252#endif
253
Damien George438c88d2014-02-22 19:25:23 +0000254#endif