blob: 21e3202a95245590819fa6fbfe79cfae40dae405 [file] [log] [blame]
Damien George438c88d2014-02-22 19:25:23 +00001#include <stdint.h>
2#include <string.h>
Damien George438c88d2014-02-22 19:25:23 +00003
4#include "nlr.h"
5#include "misc.h"
6#include "mpconfig.h"
7#include "qstr.h"
Damien George06201ff2014-03-01 19:50:50 +00008#include "parsenumbase.h"
Damien George438c88d2014-02-22 19:25:23 +00009#include "obj.h"
10#include "mpz.h"
11#include "objint.h"
12#include "runtime0.h"
13
14#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
15
16STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) {
17 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
18 o->base.type = &int_type;
19 mpz_init_zero(&o->mpz);
20 return o;
21}
22
23void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
24 if (MP_OBJ_IS_SMALL_INT(self_in)) {
25 print(env, INT_FMT, MP_OBJ_SMALL_INT_VALUE(self_in));
26 } else {
27 // TODO would rather not allocate memory to print...
28 mp_obj_int_t *self = self_in;
29 char *str = mpz_as_str(&self->mpz, 10);
30 print(env, "%s", str);
31 m_free(str, 0);
32 }
33}
34
35mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
36 mp_obj_int_t *o = o_in;
37 switch (op) {
38 case RT_UNARY_OP_BOOL: return MP_BOOL(!mpz_is_zero(&o->mpz));
39 case RT_UNARY_OP_POSITIVE: return o_in;
40 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 +000041 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 +000042 default: return NULL; // op not supported
43 }
44}
45
46mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georgecd8b2ba2014-03-19 23:15:25 +000047 const mpz_t *zlhs;
Damien George06201ff2014-03-01 19:50:50 +000048 const mpz_t *zrhs;
49 mpz_t z_int;
50 mpz_dig_t z_int_dig[MPZ_NUM_DIG_FOR_INT];
Damien George438c88d2014-02-22 19:25:23 +000051
Damien Georgecd8b2ba2014-03-19 23:15:25 +000052 // lhs could be a small int (eg small-int + mpz)
53 if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
54 mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(lhs_in));
55 zlhs = &z_int;
56 } else if (MP_OBJ_IS_TYPE(lhs_in, &int_type)) {
57 zlhs = &((mp_obj_int_t*)lhs_in)->mpz;
58 } else {
59 return MP_OBJ_NULL;
60 }
61
62 // if rhs is small int, then lhs was not (otherwise rt_binary_op handles it)
Damien George438c88d2014-02-22 19:25:23 +000063 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
Damien George06201ff2014-03-01 19:50:50 +000064 mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(rhs_in));
65 zrhs = &z_int;
Damien George438c88d2014-02-22 19:25:23 +000066 } else if (MP_OBJ_IS_TYPE(rhs_in, &int_type)) {
67 zrhs = &((mp_obj_int_t*)rhs_in)->mpz;
68 } else {
69 return MP_OBJ_NULL;
70 }
71
Damien George52608102014-03-08 15:04:54 +000072 if (0) {
73#if MICROPY_ENABLE_FLOAT
74 } else if (op == RT_BINARY_OP_TRUE_DIVIDE || op == RT_BINARY_OP_INPLACE_TRUE_DIVIDE) {
75 mp_float_t flhs = mpz_as_float(zlhs);
76 mp_float_t frhs = mpz_as_float(zrhs);
Damien George438c88d2014-02-22 19:25:23 +000077 return mp_obj_new_float(flhs / frhs);
Damien George52608102014-03-08 15:04:54 +000078#endif
Damien George438c88d2014-02-22 19:25:23 +000079
80 } else if (op <= RT_BINARY_OP_POWER) {
81 mp_obj_int_t *res = mp_obj_int_new_mpz();
82
83 switch (op) {
84 case RT_BINARY_OP_ADD:
85 case RT_BINARY_OP_INPLACE_ADD:
86 mpz_add_inpl(&res->mpz, zlhs, zrhs);
87 break;
88 case RT_BINARY_OP_SUBTRACT:
89 case RT_BINARY_OP_INPLACE_SUBTRACT:
90 mpz_sub_inpl(&res->mpz, zlhs, zrhs);
91 break;
92 case RT_BINARY_OP_MULTIPLY:
93 case RT_BINARY_OP_INPLACE_MULTIPLY:
94 mpz_mul_inpl(&res->mpz, zlhs, zrhs);
95 break;
96 case RT_BINARY_OP_FLOOR_DIVIDE:
97 case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: {
98 mpz_t rem; mpz_init_zero(&rem);
99 mpz_divmod_inpl(&res->mpz, &rem, zlhs, zrhs);
100 mpz_deinit(&rem);
101 break;
102 }
Damien George2d7ff072014-03-20 16:28:41 +0000103 case RT_BINARY_OP_MODULO:
104 case RT_BINARY_OP_INPLACE_MODULO: {
105 // TODO check that this operation matches the CPython operation
106 mpz_t quo; mpz_init_zero(&quo);
107 mpz_divmod_inpl(&quo, &res->mpz, zlhs, zrhs);
108 mpz_deinit(&quo);
109 break;
110 }
Damien George438c88d2014-02-22 19:25:23 +0000111
112 //case RT_BINARY_OP_AND:
113 //case RT_BINARY_OP_INPLACE_AND:
114 //case RT_BINARY_OP_OR:
115 //case RT_BINARY_OP_INPLACE_OR:
116 //case RT_BINARY_OP_XOR:
117 //case RT_BINARY_OP_INPLACE_XOR:
118
Damien George06201ff2014-03-01 19:50:50 +0000119 case RT_BINARY_OP_LSHIFT:
120 case RT_BINARY_OP_INPLACE_LSHIFT:
121 case RT_BINARY_OP_RSHIFT:
122 case RT_BINARY_OP_INPLACE_RSHIFT: {
123 // TODO check conversion overflow
124 machine_int_t irhs = mpz_as_int(zrhs);
125 if (irhs < 0) {
126 nlr_jump(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
127 }
128 if (op == RT_BINARY_OP_LSHIFT || op == RT_BINARY_OP_INPLACE_LSHIFT) {
129 mpz_shl_inpl(&res->mpz, zlhs, irhs);
130 } else {
131 mpz_shr_inpl(&res->mpz, zlhs, irhs);
132 }
133 break;
134 }
Damien George438c88d2014-02-22 19:25:23 +0000135
136 case RT_BINARY_OP_POWER:
137 case RT_BINARY_OP_INPLACE_POWER:
138 mpz_pow_inpl(&res->mpz, zlhs, zrhs);
139 break;
140
141 default:
142 return MP_OBJ_NULL;
143 }
144
145 return res;
146
147 } else {
148 int cmp = mpz_cmp(zlhs, zrhs);
149 switch (op) {
150 case RT_BINARY_OP_LESS:
151 return MP_BOOL(cmp < 0);
152 case RT_BINARY_OP_MORE:
153 return MP_BOOL(cmp > 0);
154 case RT_BINARY_OP_LESS_EQUAL:
155 return MP_BOOL(cmp <= 0);
156 case RT_BINARY_OP_MORE_EQUAL:
157 return MP_BOOL(cmp >= 0);
158 case RT_BINARY_OP_EQUAL:
159 return MP_BOOL(cmp == 0);
160 case RT_BINARY_OP_NOT_EQUAL:
161 return MP_BOOL(cmp != 0);
162
163 default:
164 return MP_OBJ_NULL;
165 }
166 }
167}
168
169mp_obj_t mp_obj_new_int(machine_int_t value) {
170 if (MP_OBJ_FITS_SMALL_INT(value)) {
171 return MP_OBJ_NEW_SMALL_INT(value);
172 }
173 return mp_obj_new_int_from_ll(value);
174}
175
176mp_obj_t mp_obj_new_int_from_ll(long long val) {
177 mp_obj_int_t *o = mp_obj_int_new_mpz();
Damien George9d68e9c2014-03-12 15:38:15 +0000178 mpz_set_from_ll(&o->mpz, val);
Damien George438c88d2014-02-22 19:25:23 +0000179 return o;
180}
181
182mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
183 // SMALL_INT accepts only signed numbers, of one bit less size
184 // than word size, which totals 2 bits less for unsigned numbers.
185 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
186 return MP_OBJ_NEW_SMALL_INT(value);
187 }
188 return mp_obj_new_int_from_ll(value);
189}
190
191mp_obj_t mp_obj_new_int_from_long_str(const char *str) {
192 mp_obj_int_t *o = mp_obj_int_new_mpz();
193 uint len = strlen(str);
Damien George06201ff2014-03-01 19:50:50 +0000194 int base = 0;
195 int skip = mp_parse_num_base(str, len, &base);
196 str += skip;
197 len -= skip;
198 uint n = mpz_set_from_str(&o->mpz, str, len, false, base);
Damien George438c88d2014-02-22 19:25:23 +0000199 if (n != len) {
200 nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
201 }
202 return o;
203}
204
205machine_int_t mp_obj_int_get(mp_obj_t self_in) {
206 if (MP_OBJ_IS_SMALL_INT(self_in)) {
207 return MP_OBJ_SMALL_INT_VALUE(self_in);
208 }
209 mp_obj_int_t *self = self_in;
210 return mpz_as_int(&self->mpz);
211}
212
213machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
214 // TODO: Check overflow
215 return mp_obj_int_get(self_in);
216}
217
218#endif