blob: cf7896f9e109f60e837311fa5fa1f81bafeb6589 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien George438c88d2014-02-22 19:25:23 +000027#include <stdint.h>
28#include <string.h>
Rachel Dowdall56402792014-03-22 20:19:24 +000029#include <stdio.h>
Damien George88d7bba2014-04-08 23:30:46 +010030#include <assert.h>
Damien George438c88d2014-02-22 19:25:23 +000031
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030032#include "mpconfig.h"
Damien George438c88d2014-02-22 19:25:23 +000033#include "nlr.h"
34#include "misc.h"
Damien George438c88d2014-02-22 19:25:23 +000035#include "qstr.h"
Damien George06201ff2014-03-01 19:50:50 +000036#include "parsenumbase.h"
Damien George438c88d2014-02-22 19:25:23 +000037#include "obj.h"
Damien Georged1e355e2014-05-28 14:51:12 +010038#include "smallint.h"
Damien George438c88d2014-02-22 19:25:23 +000039#include "mpz.h"
40#include "objint.h"
41#include "runtime0.h"
Damien George660aef62014-04-02 12:22:07 +010042#include "runtime.h"
Damien George438c88d2014-02-22 19:25:23 +000043
44#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
45
46STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) {
47 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +000048 o->base.type = &mp_type_int;
Damien George438c88d2014-02-22 19:25:23 +000049 mpz_init_zero(&o->mpz);
50 return o;
51}
52
Dave Hylandsc4029e52014-04-07 11:19:51 -070053// This routine expects you to pass in a buffer and size (in *buf and buf_size).
54// If, for some reason, this buffer is too small, then it will allocate a
55// buffer and return the allocated buffer and size in *buf and *buf_size. It
56// is the callers responsibility to free this allocated buffer.
57//
58// The resulting formatted string will be returned from this function and the
59// formatted size will be in *fmt_size.
Damien George88d7bba2014-04-08 23:30:46 +010060//
61// This particular routine should only be called for the mpz representation of the int.
Paul Sokolovskyab7bf282014-05-17 11:08:33 +030062char *mp_obj_int_formatted_impl(char **buf, int *buf_size, int *fmt_size, mp_const_obj_t self_in,
Damien George88d7bba2014-04-08 23:30:46 +010063 int base, const char *prefix, char base_char, char comma) {
64 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
Paul Sokolovskyab7bf282014-05-17 11:08:33 +030065 const mp_obj_int_t *self = self_in;
Dave Hylandsc4029e52014-04-07 11:19:51 -070066
Damien George88d7bba2014-04-08 23:30:46 +010067 uint needed_size = mpz_as_str_size_formatted(&self->mpz, base, prefix, comma);
Dave Hylandsc4029e52014-04-07 11:19:51 -070068 if (needed_size > *buf_size) {
69 *buf = m_new(char, needed_size);
70 *buf_size = needed_size;
71 }
72 char *str = *buf;
73
Damien George88d7bba2014-04-08 23:30:46 +010074 *fmt_size = mpz_as_str_inpl(&self->mpz, base, prefix, base_char, comma, str);
Dave Hylandsc4029e52014-04-07 11:19:51 -070075
76 return str;
77}
78
79bool mp_obj_int_is_positive(mp_obj_t self_in) {
80 if (MP_OBJ_IS_SMALL_INT(self_in)) {
81 return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0;
82 }
83 mp_obj_int_t *self = self_in;
84 return !self->mpz.neg;
Damien George438c88d2014-02-22 19:25:23 +000085}
86
Damien Georgee8208a72014-04-04 15:08:23 +010087mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
Damien George438c88d2014-02-22 19:25:23 +000088 mp_obj_int_t *o = o_in;
89 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010090 case MP_UNARY_OP_BOOL: return MP_BOOL(!mpz_is_zero(&o->mpz));
91 case MP_UNARY_OP_POSITIVE: return o_in;
92 case MP_UNARY_OP_NEGATIVE: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_neg_inpl(&o2->mpz, &o->mpz); return o2; }
93 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 George6ac5dce2014-05-21 19:42:43 +010094 default: return MP_OBJ_NULL; // op not supported
Damien George438c88d2014-02-22 19:25:23 +000095 }
96}
97
Damien Georgee8208a72014-04-04 15:08:23 +010098mp_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 +000099 const mpz_t *zlhs;
Damien George06201ff2014-03-01 19:50:50 +0000100 const mpz_t *zrhs;
101 mpz_t z_int;
102 mpz_dig_t z_int_dig[MPZ_NUM_DIG_FOR_INT];
Damien George438c88d2014-02-22 19:25:23 +0000103
Damien Georgecd8b2ba2014-03-19 23:15:25 +0000104 // lhs could be a small int (eg small-int + mpz)
105 if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
106 mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(lhs_in));
107 zlhs = &z_int;
Damien George3e1a5c12014-03-29 13:43:38 +0000108 } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
Damien Georgecd8b2ba2014-03-19 23:15:25 +0000109 zlhs = &((mp_obj_int_t*)lhs_in)->mpz;
110 } else {
Damien George0aa5d512014-03-29 17:28:20 +0000111 // unsupported type
Damien George6ac5dce2014-05-21 19:42:43 +0100112 return MP_OBJ_NULL;
Damien Georgecd8b2ba2014-03-19 23:15:25 +0000113 }
114
Damien Georged17926d2014-03-30 13:35:08 +0100115 // if rhs is small int, then lhs was not (otherwise mp_binary_op handles it)
Damien George438c88d2014-02-22 19:25:23 +0000116 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
Damien George06201ff2014-03-01 19:50:50 +0000117 mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(rhs_in));
118 zrhs = &z_int;
Damien George3e1a5c12014-03-29 13:43:38 +0000119 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
Damien George438c88d2014-02-22 19:25:23 +0000120 zrhs = &((mp_obj_int_t*)rhs_in)->mpz;
Damien Georgefb510b32014-06-01 13:32:54 +0100121#if MICROPY_PY_BUILTINS_FLOAT
Damien George0aa5d512014-03-29 17:28:20 +0000122 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_float)) {
123 return mp_obj_float_binary_op(op, mpz_as_float(zlhs), rhs_in);
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300124#if MICROPY_PY_BUILTINS_COMPLEX
Damien George0aa5d512014-03-29 17:28:20 +0000125 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
126 return mp_obj_complex_binary_op(op, mpz_as_float(zlhs), 0, rhs_in);
127#endif
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300128#endif
Damien George438c88d2014-02-22 19:25:23 +0000129 } else {
Damien Georgee8208a72014-04-04 15:08:23 +0100130 // delegate to generic function to check for extra cases
131 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Damien George438c88d2014-02-22 19:25:23 +0000132 }
133
Damien George52608102014-03-08 15:04:54 +0000134 if (0) {
Damien Georgefb510b32014-06-01 13:32:54 +0100135#if MICROPY_PY_BUILTINS_FLOAT
Damien Georged17926d2014-03-30 13:35:08 +0100136 } else if (op == MP_BINARY_OP_TRUE_DIVIDE || op == MP_BINARY_OP_INPLACE_TRUE_DIVIDE) {
Damien George52608102014-03-08 15:04:54 +0000137 mp_float_t flhs = mpz_as_float(zlhs);
138 mp_float_t frhs = mpz_as_float(zrhs);
Damien George438c88d2014-02-22 19:25:23 +0000139 return mp_obj_new_float(flhs / frhs);
Damien George52608102014-03-08 15:04:54 +0000140#endif
Damien George438c88d2014-02-22 19:25:23 +0000141
Damien Georged17926d2014-03-30 13:35:08 +0100142 } else if (op <= MP_BINARY_OP_INPLACE_POWER) {
Damien George438c88d2014-02-22 19:25:23 +0000143 mp_obj_int_t *res = mp_obj_int_new_mpz();
144
145 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100146 case MP_BINARY_OP_ADD:
147 case MP_BINARY_OP_INPLACE_ADD:
Damien George438c88d2014-02-22 19:25:23 +0000148 mpz_add_inpl(&res->mpz, zlhs, zrhs);
149 break;
Damien Georged17926d2014-03-30 13:35:08 +0100150 case MP_BINARY_OP_SUBTRACT:
151 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien George438c88d2014-02-22 19:25:23 +0000152 mpz_sub_inpl(&res->mpz, zlhs, zrhs);
153 break;
Damien Georged17926d2014-03-30 13:35:08 +0100154 case MP_BINARY_OP_MULTIPLY:
155 case MP_BINARY_OP_INPLACE_MULTIPLY:
Damien George438c88d2014-02-22 19:25:23 +0000156 mpz_mul_inpl(&res->mpz, zlhs, zrhs);
157 break;
Damien Georged17926d2014-03-30 13:35:08 +0100158 case MP_BINARY_OP_FLOOR_DIVIDE:
159 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: {
Damien George438c88d2014-02-22 19:25:23 +0000160 mpz_t rem; mpz_init_zero(&rem);
161 mpz_divmod_inpl(&res->mpz, &rem, zlhs, zrhs);
Damien Georgeecf5b772014-04-04 11:13:51 +0000162 if (zlhs->neg != zrhs->neg) {
Rachel Dowdall56402792014-03-22 20:19:24 +0000163 if (!mpz_is_zero(&rem)) {
164 mpz_t mpzone; mpz_init_from_int(&mpzone, -1);
165 mpz_add_inpl(&res->mpz, &res->mpz, &mpzone);
166 }
167 }
Damien George438c88d2014-02-22 19:25:23 +0000168 mpz_deinit(&rem);
169 break;
170 }
Damien Georged17926d2014-03-30 13:35:08 +0100171 case MP_BINARY_OP_MODULO:
172 case MP_BINARY_OP_INPLACE_MODULO: {
Damien George2d7ff072014-03-20 16:28:41 +0000173 mpz_t quo; mpz_init_zero(&quo);
174 mpz_divmod_inpl(&quo, &res->mpz, zlhs, zrhs);
175 mpz_deinit(&quo);
Damien Georgeecf5b772014-04-04 11:13:51 +0000176 // Check signs and do Python style modulo
177 if (zlhs->neg != zrhs->neg) {
Rachel Dowdallcde86312014-03-22 17:29:27 +0000178 mpz_add_inpl(&res->mpz, &res->mpz, zrhs);
179 }
Damien George2d7ff072014-03-20 16:28:41 +0000180 break;
181 }
Damien George438c88d2014-02-22 19:25:23 +0000182
Damien Georged17926d2014-03-30 13:35:08 +0100183 case MP_BINARY_OP_AND:
184 case MP_BINARY_OP_INPLACE_AND:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200185 mpz_and_inpl(&res->mpz, zlhs, zrhs);
186 break;
Damien Georged17926d2014-03-30 13:35:08 +0100187 case MP_BINARY_OP_OR:
188 case MP_BINARY_OP_INPLACE_OR:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200189 mpz_or_inpl(&res->mpz, zlhs, zrhs);
190 break;
Damien Georged17926d2014-03-30 13:35:08 +0100191 case MP_BINARY_OP_XOR:
192 case MP_BINARY_OP_INPLACE_XOR:
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200193 mpz_xor_inpl(&res->mpz, zlhs, zrhs);
194 break;
Damien George438c88d2014-02-22 19:25:23 +0000195
Damien Georged17926d2014-03-30 13:35:08 +0100196 case MP_BINARY_OP_LSHIFT:
197 case MP_BINARY_OP_INPLACE_LSHIFT:
198 case MP_BINARY_OP_RSHIFT:
199 case MP_BINARY_OP_INPLACE_RSHIFT: {
Damien George06201ff2014-03-01 19:50:50 +0000200 // TODO check conversion overflow
201 machine_int_t irhs = mpz_as_int(zrhs);
202 if (irhs < 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100203 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
Damien George06201ff2014-03-01 19:50:50 +0000204 }
Damien Georged17926d2014-03-30 13:35:08 +0100205 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
Damien George06201ff2014-03-01 19:50:50 +0000206 mpz_shl_inpl(&res->mpz, zlhs, irhs);
207 } else {
208 mpz_shr_inpl(&res->mpz, zlhs, irhs);
209 }
210 break;
211 }
Damien George438c88d2014-02-22 19:25:23 +0000212
Damien Georged17926d2014-03-30 13:35:08 +0100213 case MP_BINARY_OP_POWER:
214 case MP_BINARY_OP_INPLACE_POWER:
Damien George438c88d2014-02-22 19:25:23 +0000215 mpz_pow_inpl(&res->mpz, zlhs, zrhs);
216 break;
217
218 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100219 return MP_OBJ_NULL; // op not supported
Damien George438c88d2014-02-22 19:25:23 +0000220 }
221
222 return res;
223
224 } else {
225 int cmp = mpz_cmp(zlhs, zrhs);
226 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100227 case MP_BINARY_OP_LESS:
Damien George438c88d2014-02-22 19:25:23 +0000228 return MP_BOOL(cmp < 0);
Damien Georged17926d2014-03-30 13:35:08 +0100229 case MP_BINARY_OP_MORE:
Damien George438c88d2014-02-22 19:25:23 +0000230 return MP_BOOL(cmp > 0);
Damien Georged17926d2014-03-30 13:35:08 +0100231 case MP_BINARY_OP_LESS_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000232 return MP_BOOL(cmp <= 0);
Damien Georged17926d2014-03-30 13:35:08 +0100233 case MP_BINARY_OP_MORE_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000234 return MP_BOOL(cmp >= 0);
Damien Georged17926d2014-03-30 13:35:08 +0100235 case MP_BINARY_OP_EQUAL:
Damien George438c88d2014-02-22 19:25:23 +0000236 return MP_BOOL(cmp == 0);
Damien George438c88d2014-02-22 19:25:23 +0000237
238 default:
Damien George6ac5dce2014-05-21 19:42:43 +0100239 return MP_OBJ_NULL; // op not supported
Damien George438c88d2014-02-22 19:25:23 +0000240 }
241 }
242}
243
244mp_obj_t mp_obj_new_int(machine_int_t value) {
Damien Georged1e355e2014-05-28 14:51:12 +0100245 if (MP_SMALL_INT_FITS(value)) {
Damien George438c88d2014-02-22 19:25:23 +0000246 return MP_OBJ_NEW_SMALL_INT(value);
247 }
248 return mp_obj_new_int_from_ll(value);
249}
250
251mp_obj_t mp_obj_new_int_from_ll(long long val) {
252 mp_obj_int_t *o = mp_obj_int_new_mpz();
Damien George9d68e9c2014-03-12 15:38:15 +0000253 mpz_set_from_ll(&o->mpz, val);
Damien George438c88d2014-02-22 19:25:23 +0000254 return o;
255}
256
257mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
258 // SMALL_INT accepts only signed numbers, of one bit less size
259 // than word size, which totals 2 bits less for unsigned numbers.
260 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
261 return MP_OBJ_NEW_SMALL_INT(value);
262 }
263 return mp_obj_new_int_from_ll(value);
264}
265
Damien George503d6112014-05-28 14:07:21 +0100266mp_obj_t mp_obj_new_int_from_str_len(const char **str, uint len, bool neg, uint base) {
Damien George438c88d2014-02-22 19:25:23 +0000267 mp_obj_int_t *o = mp_obj_int_new_mpz();
Damien George503d6112014-05-28 14:07:21 +0100268 uint n = mpz_set_from_str(&o->mpz, *str, len, neg, base);
269 *str += n;
Damien George438c88d2014-02-22 19:25:23 +0000270 return o;
271}
272
Damien George503d6112014-05-28 14:07:21 +0100273machine_int_t mp_obj_int_get(mp_const_obj_t self_in) {
Damien George438c88d2014-02-22 19:25:23 +0000274 if (MP_OBJ_IS_SMALL_INT(self_in)) {
275 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000276 } else {
Damien George503d6112014-05-28 14:07:21 +0100277 const mp_obj_int_t *self = self_in;
Damien Georgeeabdf672014-03-22 20:54:01 +0000278 return mpz_as_int(&self->mpz);
Damien George438c88d2014-02-22 19:25:23 +0000279 }
Damien George438c88d2014-02-22 19:25:23 +0000280}
281
Paul Sokolovskyab7bf282014-05-17 11:08:33 +0300282machine_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
Damien George8270e382014-04-03 11:00:54 +0000283 if (MP_OBJ_IS_SMALL_INT(self_in)) {
284 return MP_OBJ_SMALL_INT_VALUE(self_in);
285 } else {
Paul Sokolovskyab7bf282014-05-17 11:08:33 +0300286 const mp_obj_int_t *self = self_in;
Damien George8270e382014-04-03 11:00:54 +0000287 machine_int_t value;
288 if (mpz_as_int_checked(&self->mpz, &value)) {
289 return value;
290 } else {
291 // overflow
Damien Georgeea13f402014-04-05 18:32:08 +0100292 nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "overflow converting long int to machine word"));
Damien George8270e382014-04-03 11:00:54 +0000293 }
294 }
Damien George438c88d2014-02-22 19:25:23 +0000295}
296
Damien Georgefb510b32014-06-01 13:32:54 +0100297#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgeeabdf672014-03-22 20:54:01 +0000298mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
299 if (MP_OBJ_IS_SMALL_INT(self_in)) {
300 return MP_OBJ_SMALL_INT_VALUE(self_in);
301 } else {
302 mp_obj_int_t *self = self_in;
303 return mpz_as_float(&self->mpz);
304 }
305}
306#endif
307
Damien George438c88d2014-02-22 19:25:23 +0000308#endif