blob: f4504415d67a7fb2edcae3cbd9b24b635a3e7138 [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
Damien Georgee8208a72014-04-04 15:08:23 +010025void mp_obj_int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Damien George438c88d2014-02-22 19:25:23 +000026 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
Damien Georgee8208a72014-04-04 15:08:23 +010037mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
Damien George438c88d2014-02-22 19:25:23 +000038 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
Damien Georgee8208a72014-04-04 15:08:23 +010048mp_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 +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 Georgee8208a72014-04-04 15:08:23 +010078 // delegate to generic function to check for extra cases
79 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Damien George438c88d2014-02-22 19:25:23 +000080 }
81
Damien George52608102014-03-08 15:04:54 +000082 if (0) {
83#if MICROPY_ENABLE_FLOAT
Damien Georged17926d2014-03-30 13:35:08 +010084 } else if (op == MP_BINARY_OP_TRUE_DIVIDE || op == MP_BINARY_OP_INPLACE_TRUE_DIVIDE) {
Damien George52608102014-03-08 15:04:54 +000085 mp_float_t flhs = mpz_as_float(zlhs);
86 mp_float_t frhs = mpz_as_float(zrhs);
Damien George438c88d2014-02-22 19:25:23 +000087 return mp_obj_new_float(flhs / frhs);
Damien George52608102014-03-08 15:04:54 +000088#endif
Damien George438c88d2014-02-22 19:25:23 +000089
Damien Georged17926d2014-03-30 13:35:08 +010090 } else if (op <= MP_BINARY_OP_INPLACE_POWER) {
Damien George438c88d2014-02-22 19:25:23 +000091 mp_obj_int_t *res = mp_obj_int_new_mpz();
92
93 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010094 case MP_BINARY_OP_ADD:
95 case MP_BINARY_OP_INPLACE_ADD:
Damien George438c88d2014-02-22 19:25:23 +000096 mpz_add_inpl(&res->mpz, zlhs, zrhs);
97 break;
Damien Georged17926d2014-03-30 13:35:08 +010098 case MP_BINARY_OP_SUBTRACT:
99 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien George438c88d2014-02-22 19:25:23 +0000100 mpz_sub_inpl(&res->mpz, zlhs, zrhs);
101 break;
Damien Georged17926d2014-03-30 13:35:08 +0100102 case MP_BINARY_OP_MULTIPLY:
103 case MP_BINARY_OP_INPLACE_MULTIPLY:
Damien George438c88d2014-02-22 19:25:23 +0000104 mpz_mul_inpl(&res->mpz, zlhs, zrhs);
105 break;
Damien Georged17926d2014-03-30 13:35:08 +0100106 case MP_BINARY_OP_FLOOR_DIVIDE:
107 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: {
Damien George438c88d2014-02-22 19:25:23 +0000108 mpz_t rem; mpz_init_zero(&rem);
109 mpz_divmod_inpl(&res->mpz, &rem, zlhs, zrhs);
Damien Georgeecf5b772014-04-04 11:13:51 +0000110 if (zlhs->neg != zrhs->neg) {
Rachel Dowdall56402792014-03-22 20:19:24 +0000111 if (!mpz_is_zero(&rem)) {
112 mpz_t mpzone; mpz_init_from_int(&mpzone, -1);
113 mpz_add_inpl(&res->mpz, &res->mpz, &mpzone);
114 }
115 }
Damien George438c88d2014-02-22 19:25:23 +0000116 mpz_deinit(&rem);
117 break;
118 }
Damien Georged17926d2014-03-30 13:35:08 +0100119 case MP_BINARY_OP_MODULO:
120 case MP_BINARY_OP_INPLACE_MODULO: {
Damien George2d7ff072014-03-20 16:28:41 +0000121 mpz_t quo; mpz_init_zero(&quo);
122 mpz_divmod_inpl(&quo, &res->mpz, zlhs, zrhs);
123 mpz_deinit(&quo);
Damien Georgeecf5b772014-04-04 11:13:51 +0000124 // Check signs and do Python style modulo
125 if (zlhs->neg != zrhs->neg) {
Rachel Dowdallcde86312014-03-22 17:29:27 +0000126 mpz_add_inpl(&res->mpz, &res->mpz, zrhs);
127 }
Damien George2d7ff072014-03-20 16:28:41 +0000128 break;
129 }
Damien George438c88d2014-02-22 19:25:23 +0000130
Damien Georged17926d2014-03-30 13:35:08 +0100131 case MP_BINARY_OP_AND:
132 case MP_BINARY_OP_INPLACE_AND:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200133 mpz_and_inpl(&res->mpz, zlhs, zrhs);
134 break;
Damien Georged17926d2014-03-30 13:35:08 +0100135 case MP_BINARY_OP_OR:
136 case MP_BINARY_OP_INPLACE_OR:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200137 mpz_or_inpl(&res->mpz, zlhs, zrhs);
138 break;
Damien Georged17926d2014-03-30 13:35:08 +0100139 case MP_BINARY_OP_XOR:
140 case MP_BINARY_OP_INPLACE_XOR:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200141 mpz_xor_inpl(&res->mpz, zlhs, zrhs);
142 break;
Damien George438c88d2014-02-22 19:25:23 +0000143
Damien Georged17926d2014-03-30 13:35:08 +0100144 case MP_BINARY_OP_LSHIFT:
145 case MP_BINARY_OP_INPLACE_LSHIFT:
146 case MP_BINARY_OP_RSHIFT:
147 case MP_BINARY_OP_INPLACE_RSHIFT: {
Damien George06201ff2014-03-01 19:50:50 +0000148 // TODO check conversion overflow
149 machine_int_t irhs = mpz_as_int(zrhs);
150 if (irhs < 0) {
151 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
152 }
Damien Georged17926d2014-03-30 13:35:08 +0100153 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
Damien George06201ff2014-03-01 19:50:50 +0000154 mpz_shl_inpl(&res->mpz, zlhs, irhs);
155 } else {
156 mpz_shr_inpl(&res->mpz, zlhs, irhs);
157 }
158 break;
159 }
Damien George438c88d2014-02-22 19:25:23 +0000160
Damien Georged17926d2014-03-30 13:35:08 +0100161 case MP_BINARY_OP_POWER:
162 case MP_BINARY_OP_INPLACE_POWER:
Damien George438c88d2014-02-22 19:25:23 +0000163 mpz_pow_inpl(&res->mpz, zlhs, zrhs);
164 break;
165
166 default:
167 return MP_OBJ_NULL;
168 }
169
170 return res;
171
172 } else {
173 int cmp = mpz_cmp(zlhs, zrhs);
174 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100175 case MP_BINARY_OP_LESS:
Damien George438c88d2014-02-22 19:25:23 +0000176 return MP_BOOL(cmp < 0);
Damien Georged17926d2014-03-30 13:35:08 +0100177 case MP_BINARY_OP_MORE:
Damien George438c88d2014-02-22 19:25:23 +0000178 return MP_BOOL(cmp > 0);
Damien Georged17926d2014-03-30 13:35:08 +0100179 case MP_BINARY_OP_LESS_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000180 return MP_BOOL(cmp <= 0);
Damien Georged17926d2014-03-30 13:35:08 +0100181 case MP_BINARY_OP_MORE_EQUAL:
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_EQUAL:
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_NOT_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000186 return MP_BOOL(cmp != 0);
187
188 default:
189 return MP_OBJ_NULL;
190 }
191 }
192}
193
194mp_obj_t mp_obj_new_int(machine_int_t value) {
195 if (MP_OBJ_FITS_SMALL_INT(value)) {
196 return MP_OBJ_NEW_SMALL_INT(value);
197 }
198 return mp_obj_new_int_from_ll(value);
199}
200
201mp_obj_t mp_obj_new_int_from_ll(long long val) {
202 mp_obj_int_t *o = mp_obj_int_new_mpz();
Damien George9d68e9c2014-03-12 15:38:15 +0000203 mpz_set_from_ll(&o->mpz, val);
Damien George438c88d2014-02-22 19:25:23 +0000204 return o;
205}
206
207mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
208 // SMALL_INT accepts only signed numbers, of one bit less size
209 // than word size, which totals 2 bits less for unsigned numbers.
210 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
211 return MP_OBJ_NEW_SMALL_INT(value);
212 }
213 return mp_obj_new_int_from_ll(value);
214}
215
216mp_obj_t mp_obj_new_int_from_long_str(const char *str) {
217 mp_obj_int_t *o = mp_obj_int_new_mpz();
218 uint len = strlen(str);
Damien George06201ff2014-03-01 19:50:50 +0000219 int base = 0;
220 int skip = mp_parse_num_base(str, len, &base);
221 str += skip;
222 len -= skip;
223 uint n = mpz_set_from_str(&o->mpz, str, len, false, base);
Damien George438c88d2014-02-22 19:25:23 +0000224 if (n != len) {
225 nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
226 }
227 return o;
228}
229
230machine_int_t mp_obj_int_get(mp_obj_t self_in) {
231 if (MP_OBJ_IS_SMALL_INT(self_in)) {
232 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000233 } else {
234 mp_obj_int_t *self = self_in;
235 return mpz_as_int(&self->mpz);
Damien George438c88d2014-02-22 19:25:23 +0000236 }
Damien George438c88d2014-02-22 19:25:23 +0000237}
238
239machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
Damien George8270e382014-04-03 11:00:54 +0000240 if (MP_OBJ_IS_SMALL_INT(self_in)) {
241 return MP_OBJ_SMALL_INT_VALUE(self_in);
242 } else {
243 mp_obj_int_t *self = self_in;
244 machine_int_t value;
245 if (mpz_as_int_checked(&self->mpz, &value)) {
246 return value;
247 } else {
248 // overflow
249 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "overflow converting long int to machine word"));
250 }
251 }
Damien George438c88d2014-02-22 19:25:23 +0000252}
253
Damien Georgeeabdf672014-03-22 20:54:01 +0000254#if MICROPY_ENABLE_FLOAT
255mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
256 if (MP_OBJ_IS_SMALL_INT(self_in)) {
257 return MP_OBJ_SMALL_INT_VALUE(self_in);
258 } else {
259 mp_obj_int_t *self = self_in;
260 return mpz_as_float(&self->mpz);
261 }
262}
263#endif
264
Damien George438c88d2014-02-22 19:25:23 +0000265#endif