2014-05-03 18:27:38 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-02-22 14:25:23 -05:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2014-03-22 16:19:24 -04:00
|
|
|
#include <stdio.h>
|
2014-04-08 18:30:46 -04:00
|
|
|
#include <assert.h>
|
2014-02-22 14:25:23 -05:00
|
|
|
|
2014-05-02 10:47:01 -04:00
|
|
|
#include "mpconfig.h"
|
2014-02-22 14:25:23 -05:00
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "qstr.h"
|
2014-03-01 14:50:50 -05:00
|
|
|
#include "parsenumbase.h"
|
2014-02-22 14:25:23 -05:00
|
|
|
#include "obj.h"
|
|
|
|
#include "mpz.h"
|
|
|
|
#include "objint.h"
|
|
|
|
#include "runtime0.h"
|
2014-04-02 07:22:07 -04:00
|
|
|
#include "runtime.h"
|
2014-02-22 14:25:23 -05:00
|
|
|
|
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
|
|
|
|
|
|
|
|
STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) {
|
|
|
|
mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
|
2014-03-29 09:43:38 -04:00
|
|
|
o->base.type = &mp_type_int;
|
2014-02-22 14:25:23 -05:00
|
|
|
mpz_init_zero(&o->mpz);
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2014-04-07 14:19:51 -04:00
|
|
|
// This routine expects you to pass in a buffer and size (in *buf and buf_size).
|
|
|
|
// If, for some reason, this buffer is too small, then it will allocate a
|
|
|
|
// buffer and return the allocated buffer and size in *buf and *buf_size. It
|
|
|
|
// is the callers responsibility to free this allocated buffer.
|
|
|
|
//
|
|
|
|
// The resulting formatted string will be returned from this function and the
|
|
|
|
// formatted size will be in *fmt_size.
|
2014-04-08 18:30:46 -04:00
|
|
|
//
|
|
|
|
// This particular routine should only be called for the mpz representation of the int.
|
2014-05-17 04:08:33 -04:00
|
|
|
char *mp_obj_int_formatted_impl(char **buf, int *buf_size, int *fmt_size, mp_const_obj_t self_in,
|
2014-04-08 18:30:46 -04:00
|
|
|
int base, const char *prefix, char base_char, char comma) {
|
|
|
|
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
|
2014-05-17 04:08:33 -04:00
|
|
|
const mp_obj_int_t *self = self_in;
|
2014-04-07 14:19:51 -04:00
|
|
|
|
2014-04-08 18:30:46 -04:00
|
|
|
uint needed_size = mpz_as_str_size_formatted(&self->mpz, base, prefix, comma);
|
2014-04-07 14:19:51 -04:00
|
|
|
if (needed_size > *buf_size) {
|
|
|
|
*buf = m_new(char, needed_size);
|
|
|
|
*buf_size = needed_size;
|
|
|
|
}
|
|
|
|
char *str = *buf;
|
|
|
|
|
2014-04-08 18:30:46 -04:00
|
|
|
*fmt_size = mpz_as_str_inpl(&self->mpz, base, prefix, base_char, comma, str);
|
2014-04-07 14:19:51 -04:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mp_obj_int_is_positive(mp_obj_t self_in) {
|
|
|
|
if (MP_OBJ_IS_SMALL_INT(self_in)) {
|
|
|
|
return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0;
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
2014-04-07 14:19:51 -04:00
|
|
|
mp_obj_int_t *self = self_in;
|
|
|
|
return !self->mpz.neg;
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
|
|
|
|
2014-04-04 10:08:23 -04:00
|
|
|
mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
|
2014-02-22 14:25:23 -05:00
|
|
|
mp_obj_int_t *o = o_in;
|
|
|
|
switch (op) {
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_UNARY_OP_BOOL: return MP_BOOL(!mpz_is_zero(&o->mpz));
|
|
|
|
case MP_UNARY_OP_POSITIVE: return o_in;
|
|
|
|
case MP_UNARY_OP_NEGATIVE: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_neg_inpl(&o2->mpz, &o->mpz); return o2; }
|
|
|
|
case MP_UNARY_OP_INVERT: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_not_inpl(&o2->mpz, &o->mpz); return o2; }
|
2014-05-10 08:55:11 -04:00
|
|
|
default: return MP_OBJ_NOT_SUPPORTED;
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-04 10:08:23 -04:00
|
|
|
mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
2014-03-19 19:15:25 -04:00
|
|
|
const mpz_t *zlhs;
|
2014-03-01 14:50:50 -05:00
|
|
|
const mpz_t *zrhs;
|
|
|
|
mpz_t z_int;
|
|
|
|
mpz_dig_t z_int_dig[MPZ_NUM_DIG_FOR_INT];
|
2014-02-22 14:25:23 -05:00
|
|
|
|
2014-03-19 19:15:25 -04:00
|
|
|
// lhs could be a small int (eg small-int + mpz)
|
|
|
|
if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
|
|
|
|
mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(lhs_in));
|
|
|
|
zlhs = &z_int;
|
2014-03-29 09:43:38 -04:00
|
|
|
} else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
|
2014-03-19 19:15:25 -04:00
|
|
|
zlhs = &((mp_obj_int_t*)lhs_in)->mpz;
|
|
|
|
} else {
|
2014-03-29 13:28:20 -04:00
|
|
|
// unsupported type
|
2014-04-17 18:19:36 -04:00
|
|
|
return MP_OBJ_NOT_SUPPORTED;
|
2014-03-19 19:15:25 -04:00
|
|
|
}
|
|
|
|
|
2014-03-30 08:35:08 -04:00
|
|
|
// if rhs is small int, then lhs was not (otherwise mp_binary_op handles it)
|
2014-02-22 14:25:23 -05:00
|
|
|
if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
|
2014-03-01 14:50:50 -05:00
|
|
|
mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(rhs_in));
|
|
|
|
zrhs = &z_int;
|
2014-03-29 09:43:38 -04:00
|
|
|
} else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
|
2014-02-22 14:25:23 -05:00
|
|
|
zrhs = &((mp_obj_int_t*)rhs_in)->mpz;
|
2014-03-29 13:28:20 -04:00
|
|
|
#if MICROPY_ENABLE_FLOAT
|
|
|
|
} else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_float)) {
|
|
|
|
return mp_obj_float_binary_op(op, mpz_as_float(zlhs), rhs_in);
|
|
|
|
} else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
|
|
|
|
return mp_obj_complex_binary_op(op, mpz_as_float(zlhs), 0, rhs_in);
|
|
|
|
#endif
|
2014-02-22 14:25:23 -05:00
|
|
|
} else {
|
2014-04-04 10:08:23 -04:00
|
|
|
// delegate to generic function to check for extra cases
|
|
|
|
return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
|
|
|
|
2014-03-08 10:04:54 -05:00
|
|
|
if (0) {
|
|
|
|
#if MICROPY_ENABLE_FLOAT
|
2014-03-30 08:35:08 -04:00
|
|
|
} else if (op == MP_BINARY_OP_TRUE_DIVIDE || op == MP_BINARY_OP_INPLACE_TRUE_DIVIDE) {
|
2014-03-08 10:04:54 -05:00
|
|
|
mp_float_t flhs = mpz_as_float(zlhs);
|
|
|
|
mp_float_t frhs = mpz_as_float(zrhs);
|
2014-02-22 14:25:23 -05:00
|
|
|
return mp_obj_new_float(flhs / frhs);
|
2014-03-08 10:04:54 -05:00
|
|
|
#endif
|
2014-02-22 14:25:23 -05:00
|
|
|
|
2014-03-30 08:35:08 -04:00
|
|
|
} else if (op <= MP_BINARY_OP_INPLACE_POWER) {
|
2014-02-22 14:25:23 -05:00
|
|
|
mp_obj_int_t *res = mp_obj_int_new_mpz();
|
|
|
|
|
|
|
|
switch (op) {
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_ADD:
|
|
|
|
case MP_BINARY_OP_INPLACE_ADD:
|
2014-02-22 14:25:23 -05:00
|
|
|
mpz_add_inpl(&res->mpz, zlhs, zrhs);
|
|
|
|
break;
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_SUBTRACT:
|
|
|
|
case MP_BINARY_OP_INPLACE_SUBTRACT:
|
2014-02-22 14:25:23 -05:00
|
|
|
mpz_sub_inpl(&res->mpz, zlhs, zrhs);
|
|
|
|
break;
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_MULTIPLY:
|
|
|
|
case MP_BINARY_OP_INPLACE_MULTIPLY:
|
2014-02-22 14:25:23 -05:00
|
|
|
mpz_mul_inpl(&res->mpz, zlhs, zrhs);
|
|
|
|
break;
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_FLOOR_DIVIDE:
|
|
|
|
case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: {
|
2014-02-22 14:25:23 -05:00
|
|
|
mpz_t rem; mpz_init_zero(&rem);
|
|
|
|
mpz_divmod_inpl(&res->mpz, &rem, zlhs, zrhs);
|
2014-04-04 07:13:51 -04:00
|
|
|
if (zlhs->neg != zrhs->neg) {
|
2014-03-22 16:19:24 -04:00
|
|
|
if (!mpz_is_zero(&rem)) {
|
|
|
|
mpz_t mpzone; mpz_init_from_int(&mpzone, -1);
|
|
|
|
mpz_add_inpl(&res->mpz, &res->mpz, &mpzone);
|
|
|
|
}
|
|
|
|
}
|
2014-02-22 14:25:23 -05:00
|
|
|
mpz_deinit(&rem);
|
|
|
|
break;
|
|
|
|
}
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_MODULO:
|
|
|
|
case MP_BINARY_OP_INPLACE_MODULO: {
|
2014-03-20 12:28:41 -04:00
|
|
|
mpz_t quo; mpz_init_zero(&quo);
|
|
|
|
mpz_divmod_inpl(&quo, &res->mpz, zlhs, zrhs);
|
|
|
|
mpz_deinit(&quo);
|
2014-04-04 07:13:51 -04:00
|
|
|
// Check signs and do Python style modulo
|
|
|
|
if (zlhs->neg != zrhs->neg) {
|
2014-03-22 13:29:27 -04:00
|
|
|
mpz_add_inpl(&res->mpz, &res->mpz, zrhs);
|
|
|
|
}
|
2014-03-20 12:28:41 -04:00
|
|
|
break;
|
|
|
|
}
|
2014-02-22 14:25:23 -05:00
|
|
|
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_AND:
|
|
|
|
case MP_BINARY_OP_INPLACE_AND:
|
2014-03-22 19:52:36 -04:00
|
|
|
mpz_and_inpl(&res->mpz, zlhs, zrhs);
|
|
|
|
break;
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_OR:
|
|
|
|
case MP_BINARY_OP_INPLACE_OR:
|
2014-03-22 19:52:36 -04:00
|
|
|
mpz_or_inpl(&res->mpz, zlhs, zrhs);
|
|
|
|
break;
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_XOR:
|
|
|
|
case MP_BINARY_OP_INPLACE_XOR:
|
2014-03-22 19:52:36 -04:00
|
|
|
mpz_xor_inpl(&res->mpz, zlhs, zrhs);
|
|
|
|
break;
|
2014-02-22 14:25:23 -05:00
|
|
|
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_LSHIFT:
|
|
|
|
case MP_BINARY_OP_INPLACE_LSHIFT:
|
|
|
|
case MP_BINARY_OP_RSHIFT:
|
|
|
|
case MP_BINARY_OP_INPLACE_RSHIFT: {
|
2014-03-01 14:50:50 -05:00
|
|
|
// TODO check conversion overflow
|
|
|
|
machine_int_t irhs = mpz_as_int(zrhs);
|
|
|
|
if (irhs < 0) {
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
|
2014-03-01 14:50:50 -05:00
|
|
|
}
|
2014-03-30 08:35:08 -04:00
|
|
|
if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
|
2014-03-01 14:50:50 -05:00
|
|
|
mpz_shl_inpl(&res->mpz, zlhs, irhs);
|
|
|
|
} else {
|
|
|
|
mpz_shr_inpl(&res->mpz, zlhs, irhs);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-02-22 14:25:23 -05:00
|
|
|
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_POWER:
|
|
|
|
case MP_BINARY_OP_INPLACE_POWER:
|
2014-02-22 14:25:23 -05:00
|
|
|
mpz_pow_inpl(&res->mpz, zlhs, zrhs);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-04-17 18:19:36 -04:00
|
|
|
return MP_OBJ_NOT_SUPPORTED;
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
int cmp = mpz_cmp(zlhs, zrhs);
|
|
|
|
switch (op) {
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_LESS:
|
2014-02-22 14:25:23 -05:00
|
|
|
return MP_BOOL(cmp < 0);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_MORE:
|
2014-02-22 14:25:23 -05:00
|
|
|
return MP_BOOL(cmp > 0);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_LESS_EQUAL:
|
2014-02-22 14:25:23 -05:00
|
|
|
return MP_BOOL(cmp <= 0);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_MORE_EQUAL:
|
2014-02-22 14:25:23 -05:00
|
|
|
return MP_BOOL(cmp >= 0);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_EQUAL:
|
2014-02-22 14:25:23 -05:00
|
|
|
return MP_BOOL(cmp == 0);
|
|
|
|
|
|
|
|
default:
|
2014-04-17 18:19:36 -04:00
|
|
|
return MP_OBJ_NOT_SUPPORTED;
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t mp_obj_new_int(machine_int_t value) {
|
|
|
|
if (MP_OBJ_FITS_SMALL_INT(value)) {
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(value);
|
|
|
|
}
|
|
|
|
return mp_obj_new_int_from_ll(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t mp_obj_new_int_from_ll(long long val) {
|
|
|
|
mp_obj_int_t *o = mp_obj_int_new_mpz();
|
2014-03-12 11:38:15 -04:00
|
|
|
mpz_set_from_ll(&o->mpz, val);
|
2014-02-22 14:25:23 -05:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
|
|
|
|
// SMALL_INT accepts only signed numbers, of one bit less size
|
|
|
|
// than word size, which totals 2 bits less for unsigned numbers.
|
|
|
|
if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(value);
|
|
|
|
}
|
|
|
|
return mp_obj_new_int_from_ll(value);
|
|
|
|
}
|
|
|
|
|
2014-05-07 13:30:52 -04:00
|
|
|
mp_obj_t mp_obj_new_int_from_qstr(qstr qst) {
|
2014-02-22 14:25:23 -05:00
|
|
|
mp_obj_int_t *o = mp_obj_int_new_mpz();
|
2014-05-07 13:30:52 -04:00
|
|
|
uint len;
|
|
|
|
const char* str = (const char*)qstr_data(qst, &len);
|
2014-03-01 14:50:50 -05:00
|
|
|
int base = 0;
|
|
|
|
int skip = mp_parse_num_base(str, len, &base);
|
|
|
|
str += skip;
|
|
|
|
len -= skip;
|
|
|
|
uint n = mpz_set_from_str(&o->mpz, str, len, false, base);
|
2014-02-22 14:25:23 -05:00
|
|
|
if (n != len) {
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
machine_int_t mp_obj_int_get(mp_obj_t self_in) {
|
|
|
|
if (MP_OBJ_IS_SMALL_INT(self_in)) {
|
|
|
|
return MP_OBJ_SMALL_INT_VALUE(self_in);
|
2014-03-22 16:54:01 -04:00
|
|
|
} else {
|
|
|
|
mp_obj_int_t *self = self_in;
|
|
|
|
return mpz_as_int(&self->mpz);
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-17 04:08:33 -04:00
|
|
|
machine_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
|
2014-04-03 07:00:54 -04:00
|
|
|
if (MP_OBJ_IS_SMALL_INT(self_in)) {
|
|
|
|
return MP_OBJ_SMALL_INT_VALUE(self_in);
|
|
|
|
} else {
|
2014-05-17 04:08:33 -04:00
|
|
|
const mp_obj_int_t *self = self_in;
|
2014-04-03 07:00:54 -04:00
|
|
|
machine_int_t value;
|
|
|
|
if (mpz_as_int_checked(&self->mpz, &value)) {
|
|
|
|
return value;
|
|
|
|
} else {
|
|
|
|
// overflow
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "overflow converting long int to machine word"));
|
2014-04-03 07:00:54 -04:00
|
|
|
}
|
|
|
|
}
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
|
|
|
|
2014-03-22 16:54:01 -04:00
|
|
|
#if MICROPY_ENABLE_FLOAT
|
|
|
|
mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
|
|
|
|
if (MP_OBJ_IS_SMALL_INT(self_in)) {
|
|
|
|
return MP_OBJ_SMALL_INT_VALUE(self_in);
|
|
|
|
} else {
|
|
|
|
mp_obj_int_t *self = self_in;
|
|
|
|
return mpz_as_float(&self->mpz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-22 14:25:23 -05:00
|
|
|
#endif
|