blob: 25d8af70ee5d906ac14403b76e98b61a1beb47cd [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);
19 o->base.type = &int_type;
20 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) {
39 case RT_UNARY_OP_BOOL: return MP_BOOL(!mpz_is_zero(&o->mpz));
40 case RT_UNARY_OP_POSITIVE: return o_in;
41 case RT_UNARY_OP_NEGATIVE: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_neg_inpl(&o2->mpz, &o->mpz); return o2; }
Damien George06201ff2014-03-01 19:50:50 +000042 case RT_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;
57 } else if (MP_OBJ_IS_TYPE(lhs_in, &int_type)) {
58 zlhs = &((mp_obj_int_t*)lhs_in)->mpz;
59 } else {
60 return MP_OBJ_NULL;
61 }
62
63 // if rhs is small int, then lhs was not (otherwise rt_binary_op handles it)
Damien George438c88d2014-02-22 19:25:23 +000064 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
Damien George06201ff2014-03-01 19:50:50 +000065 mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(rhs_in));
66 zrhs = &z_int;
Damien George438c88d2014-02-22 19:25:23 +000067 } else if (MP_OBJ_IS_TYPE(rhs_in, &int_type)) {
68 zrhs = &((mp_obj_int_t*)rhs_in)->mpz;
69 } else {
70 return MP_OBJ_NULL;
71 }
72
Damien George52608102014-03-08 15:04:54 +000073 if (0) {
74#if MICROPY_ENABLE_FLOAT
75 } else if (op == RT_BINARY_OP_TRUE_DIVIDE || op == RT_BINARY_OP_INPLACE_TRUE_DIVIDE) {
76 mp_float_t flhs = mpz_as_float(zlhs);
77 mp_float_t frhs = mpz_as_float(zrhs);
Damien George438c88d2014-02-22 19:25:23 +000078 return mp_obj_new_float(flhs / frhs);
Damien George52608102014-03-08 15:04:54 +000079#endif
Damien George438c88d2014-02-22 19:25:23 +000080
Paul Sokolovsky57207b82014-03-23 01:52:36 +020081 } else if (op <= RT_BINARY_OP_INPLACE_POWER) {
Damien George438c88d2014-02-22 19:25:23 +000082 mp_obj_int_t *res = mp_obj_int_new_mpz();
83
84 switch (op) {
85 case RT_BINARY_OP_ADD:
86 case RT_BINARY_OP_INPLACE_ADD:
87 mpz_add_inpl(&res->mpz, zlhs, zrhs);
88 break;
89 case RT_BINARY_OP_SUBTRACT:
90 case RT_BINARY_OP_INPLACE_SUBTRACT:
91 mpz_sub_inpl(&res->mpz, zlhs, zrhs);
92 break;
93 case RT_BINARY_OP_MULTIPLY:
94 case RT_BINARY_OP_INPLACE_MULTIPLY:
95 mpz_mul_inpl(&res->mpz, zlhs, zrhs);
96 break;
97 case RT_BINARY_OP_FLOOR_DIVIDE:
98 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: {
99 mpz_t rem; mpz_init_zero(&rem);
100 mpz_divmod_inpl(&res->mpz, &rem, zlhs, zrhs);
Rachel Dowdall56402792014-03-22 20:19:24 +0000101 if (zlhs->neg != zrhs->neg) {
102 if (!mpz_is_zero(&rem)) {
103 mpz_t mpzone; mpz_init_from_int(&mpzone, -1);
104 mpz_add_inpl(&res->mpz, &res->mpz, &mpzone);
105 }
106 }
Damien George438c88d2014-02-22 19:25:23 +0000107 mpz_deinit(&rem);
108 break;
109 }
Damien George2d7ff072014-03-20 16:28:41 +0000110 case RT_BINARY_OP_MODULO:
111 case RT_BINARY_OP_INPLACE_MODULO: {
Damien George2d7ff072014-03-20 16:28:41 +0000112 mpz_t quo; mpz_init_zero(&quo);
113 mpz_divmod_inpl(&quo, &res->mpz, zlhs, zrhs);
114 mpz_deinit(&quo);
Rachel Dowdall56402792014-03-22 20:19:24 +0000115 // Check signs and do Python style modulo
116 if (zlhs->neg != zrhs->neg) {
Rachel Dowdallcde86312014-03-22 17:29:27 +0000117 mpz_add_inpl(&res->mpz, &res->mpz, zrhs);
118 }
Damien George2d7ff072014-03-20 16:28:41 +0000119 break;
120 }
Damien George438c88d2014-02-22 19:25:23 +0000121
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200122 case RT_BINARY_OP_AND:
123 case RT_BINARY_OP_INPLACE_AND:
124 mpz_and_inpl(&res->mpz, zlhs, zrhs);
125 break;
126 case RT_BINARY_OP_OR:
127 case RT_BINARY_OP_INPLACE_OR:
128 mpz_or_inpl(&res->mpz, zlhs, zrhs);
129 break;
130 case RT_BINARY_OP_XOR:
131 case RT_BINARY_OP_INPLACE_XOR:
132 mpz_xor_inpl(&res->mpz, zlhs, zrhs);
133 break;
Damien George438c88d2014-02-22 19:25:23 +0000134
Damien George06201ff2014-03-01 19:50:50 +0000135 case RT_BINARY_OP_LSHIFT:
136 case RT_BINARY_OP_INPLACE_LSHIFT:
137 case RT_BINARY_OP_RSHIFT:
138 case RT_BINARY_OP_INPLACE_RSHIFT: {
139 // TODO check conversion overflow
140 machine_int_t irhs = mpz_as_int(zrhs);
141 if (irhs < 0) {
142 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
143 }
144 if (op == RT_BINARY_OP_LSHIFT || op == RT_BINARY_OP_INPLACE_LSHIFT) {
145 mpz_shl_inpl(&res->mpz, zlhs, irhs);
146 } else {
147 mpz_shr_inpl(&res->mpz, zlhs, irhs);
148 }
149 break;
150 }
Damien George438c88d2014-02-22 19:25:23 +0000151
152 case RT_BINARY_OP_POWER:
153 case RT_BINARY_OP_INPLACE_POWER:
154 mpz_pow_inpl(&res->mpz, zlhs, zrhs);
155 break;
156
157 default:
158 return MP_OBJ_NULL;
159 }
160
161 return res;
162
163 } else {
164 int cmp = mpz_cmp(zlhs, zrhs);
165 switch (op) {
166 case RT_BINARY_OP_LESS:
167 return MP_BOOL(cmp < 0);
168 case RT_BINARY_OP_MORE:
169 return MP_BOOL(cmp > 0);
170 case RT_BINARY_OP_LESS_EQUAL:
171 return MP_BOOL(cmp <= 0);
172 case RT_BINARY_OP_MORE_EQUAL:
173 return MP_BOOL(cmp >= 0);
174 case RT_BINARY_OP_EQUAL:
175 return MP_BOOL(cmp == 0);
176 case RT_BINARY_OP_NOT_EQUAL:
177 return MP_BOOL(cmp != 0);
178
179 default:
180 return MP_OBJ_NULL;
181 }
182 }
183}
184
185mp_obj_t mp_obj_new_int(machine_int_t value) {
186 if (MP_OBJ_FITS_SMALL_INT(value)) {
187 return MP_OBJ_NEW_SMALL_INT(value);
188 }
189 return mp_obj_new_int_from_ll(value);
190}
191
192mp_obj_t mp_obj_new_int_from_ll(long long val) {
193 mp_obj_int_t *o = mp_obj_int_new_mpz();
Damien George9d68e9c2014-03-12 15:38:15 +0000194 mpz_set_from_ll(&o->mpz, val);
Damien George438c88d2014-02-22 19:25:23 +0000195 return o;
196}
197
198mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
199 // SMALL_INT accepts only signed numbers, of one bit less size
200 // than word size, which totals 2 bits less for unsigned numbers.
201 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
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_long_str(const char *str) {
208 mp_obj_int_t *o = mp_obj_int_new_mpz();
209 uint len = strlen(str);
Damien George06201ff2014-03-01 19:50:50 +0000210 int base = 0;
211 int skip = mp_parse_num_base(str, len, &base);
212 str += skip;
213 len -= skip;
214 uint n = mpz_set_from_str(&o->mpz, str, len, false, base);
Damien George438c88d2014-02-22 19:25:23 +0000215 if (n != len) {
216 nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
217 }
218 return o;
219}
220
221machine_int_t mp_obj_int_get(mp_obj_t self_in) {
222 if (MP_OBJ_IS_SMALL_INT(self_in)) {
223 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000224 } else {
225 mp_obj_int_t *self = self_in;
226 return mpz_as_int(&self->mpz);
Damien George438c88d2014-02-22 19:25:23 +0000227 }
Damien George438c88d2014-02-22 19:25:23 +0000228}
229
230machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
231 // TODO: Check overflow
232 return mp_obj_int_get(self_in);
233}
234
Damien Georgeeabdf672014-03-22 20:54:01 +0000235#if MICROPY_ENABLE_FLOAT
236mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
237 if (MP_OBJ_IS_SMALL_INT(self_in)) {
238 return MP_OBJ_SMALL_INT_VALUE(self_in);
239 } else {
240 mp_obj_int_t *self = self_in;
241 return mpz_as_float(&self->mpz);
242 }
243}
244#endif
245
Damien George438c88d2014-02-22 19:25:23 +0000246#endif