2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* 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 <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
|
|
|
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/parsenumbase.h"
|
|
|
|
#include "py/smallint.h"
|
|
|
|
#include "py/objint.h"
|
|
|
|
#include "py/runtime.h"
|
2014-02-22 14:25:23 -05:00
|
|
|
|
2022-05-27 15:59:54 -04:00
|
|
|
#include "supervisor/shared/translate/translate.h"
|
2018-08-08 21:24:49 -04:00
|
|
|
|
2014-12-29 17:22:10 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
#include <math.h>
|
|
|
|
#endif
|
|
|
|
|
2014-02-22 14:25:23 -05:00
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
|
|
|
|
|
2014-07-03 09:50:11 -04:00
|
|
|
#if MICROPY_PY_SYS_MAXSIZE
|
|
|
|
// Export value for sys.maxsize
|
2020-02-25 19:58:42 -05:00
|
|
|
// *FORMAT-OFF*
|
2014-10-30 09:39:22 -04:00
|
|
|
#define DIG_MASK ((MPZ_LONG_1 << MPZ_DIG_SIZE) - 1)
|
2015-09-15 11:15:57 -04:00
|
|
|
STATIC const mpz_dig_t maxsize_dig[] = {
|
|
|
|
#define NUM_DIG 1
|
2014-09-06 12:17:23 -04:00
|
|
|
(MP_SSIZE_MAX >> MPZ_DIG_SIZE * 0) & DIG_MASK,
|
|
|
|
#if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 0) > DIG_MASK
|
2021-03-15 09:57:36 -04:00
|
|
|
#undef NUM_DIG
|
2015-09-15 11:15:57 -04:00
|
|
|
#define NUM_DIG 2
|
2021-03-15 09:57:36 -04:00
|
|
|
(MP_SSIZE_MAX >> MPZ_DIG_SIZE * 1) & DIG_MASK,
|
|
|
|
#if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 1) > DIG_MASK
|
|
|
|
#undef NUM_DIG
|
2015-09-15 11:15:57 -04:00
|
|
|
#define NUM_DIG 3
|
2021-03-15 09:57:36 -04:00
|
|
|
(MP_SSIZE_MAX >> MPZ_DIG_SIZE * 2) & DIG_MASK,
|
|
|
|
#if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 2) > DIG_MASK
|
|
|
|
#undef NUM_DIG
|
2015-09-15 11:15:57 -04:00
|
|
|
#define NUM_DIG 4
|
2021-03-15 09:57:36 -04:00
|
|
|
(MP_SSIZE_MAX >> MPZ_DIG_SIZE * 3) & DIG_MASK,
|
|
|
|
#if (MP_SSIZE_MAX >> MPZ_DIG_SIZE * 3) > DIG_MASK
|
|
|
|
#error cannot encode MP_SSIZE_MAX as mpz
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
2014-09-06 12:37:29 -04:00
|
|
|
#endif
|
2014-07-03 09:50:11 -04:00
|
|
|
};
|
2020-02-25 19:58:42 -05:00
|
|
|
// *FORMAT-ON*
|
2020-05-22 12:10:15 -04:00
|
|
|
const mp_obj_int_t mp_sys_maxsize_obj = {
|
2014-07-03 09:50:11 -04:00
|
|
|
{&mp_type_int},
|
2021-03-15 09:57:36 -04:00
|
|
|
{.fixed_dig = 1, .len = NUM_DIG, .alloc = NUM_DIG, .dig = (mpz_dig_t *)maxsize_dig}
|
2014-07-03 09:50:11 -04:00
|
|
|
};
|
|
|
|
#undef DIG_MASK
|
2015-09-15 11:15:57 -04:00
|
|
|
#undef NUM_DIG
|
2014-07-03 09:50:11 -04:00
|
|
|
#endif
|
|
|
|
|
2017-04-04 02:45:49 -04:00
|
|
|
mp_obj_int_t *mp_obj_int_new_mpz(void) {
|
2014-02-22 14:25:23 -05:00
|
|
|
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.
|
2016-10-10 22:20:11 -04:00
|
|
|
char *mp_obj_int_formatted_impl(char **buf, size_t *buf_size, size_t *fmt_size, mp_const_obj_t self_in,
|
2021-03-15 09:57:36 -04:00
|
|
|
int base, const char *prefix, char base_char, char comma) {
|
2021-04-22 20:55:39 -04:00
|
|
|
assert(mp_obj_is_type(self_in, &mp_type_int));
|
2015-11-27 12:01:44 -05:00
|
|
|
const mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-04-07 14:19:51 -04:00
|
|
|
|
2016-10-10 22:20:11 -04:00
|
|
|
size_t needed_size = mp_int_format_size(mpz_max_num_bits(&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;
|
|
|
|
}
|
|
|
|
|
2020-04-28 10:13:49 -04:00
|
|
|
mp_obj_t mp_obj_int_bit_length_impl(mp_obj_t self_in) {
|
2021-04-22 20:55:39 -04:00
|
|
|
assert(mp_obj_is_type(self_in, &mp_type_int));
|
2020-04-28 10:13:49 -04:00
|
|
|
mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(mpz_num_bits(&self->mpz));
|
|
|
|
}
|
|
|
|
|
2017-01-21 12:14:18 -05:00
|
|
|
mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf) {
|
|
|
|
mp_obj_int_t *o = mp_obj_int_new_mpz();
|
|
|
|
mpz_set_from_bytes(&o->mpz, big_endian, len, buf);
|
|
|
|
return MP_OBJ_FROM_PTR(o);
|
|
|
|
}
|
|
|
|
|
2016-10-10 22:20:11 -04:00
|
|
|
void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf) {
|
2021-04-22 20:55:39 -04:00
|
|
|
assert(mp_obj_is_type(self_in, &mp_type_int));
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-04-25 18:16:39 -04:00
|
|
|
mpz_as_bytes(&self->mpz, big_endian, len, buf);
|
|
|
|
}
|
|
|
|
|
2016-01-07 09:29:12 -05:00
|
|
|
int mp_obj_int_sign(mp_obj_t self_in) {
|
2021-04-22 20:55:39 -04:00
|
|
|
if (mp_obj_is_small_int(self_in)) {
|
2016-01-07 09:29:12 -05:00
|
|
|
mp_int_t val = MP_OBJ_SMALL_INT_VALUE(self_in);
|
|
|
|
if (val < 0) {
|
|
|
|
return -1;
|
|
|
|
} else if (val > 0) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
|
2016-01-07 09:29:12 -05:00
|
|
|
if (self->mpz.len == 0) {
|
|
|
|
return 0;
|
|
|
|
} else if (self->mpz.neg == 0) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
|
|
|
|
2017-08-28 23:04:01 -04:00
|
|
|
mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_int_t *o = MP_OBJ_TO_PTR(o_in);
|
2014-02-22 14:25:23 -05:00
|
|
|
switch (op) {
|
2021-03-15 09:57:36 -04:00
|
|
|
case MP_UNARY_OP_BOOL:
|
|
|
|
return mp_obj_new_bool(!mpz_is_zero(&o->mpz));
|
|
|
|
case MP_UNARY_OP_HASH:
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(mpz_hash(&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 MP_OBJ_FROM_PTR(o2);
|
|
|
|
}
|
|
|
|
case MP_UNARY_OP_INVERT: { mp_obj_int_t *o2 = mp_obj_int_new_mpz();
|
|
|
|
mpz_not_inpl(&o2->mpz, &o->mpz);
|
|
|
|
return MP_OBJ_FROM_PTR(o2);
|
|
|
|
}
|
2017-09-17 17:06:43 -04:00
|
|
|
case MP_UNARY_OP_ABS: {
|
|
|
|
mp_obj_int_t *self = MP_OBJ_TO_PTR(o_in);
|
|
|
|
if (self->mpz.neg == 0) {
|
|
|
|
return o_in;
|
|
|
|
}
|
|
|
|
mp_obj_int_t *self2 = mp_obj_int_new_mpz();
|
|
|
|
mpz_abs_inpl(&self2->mpz, &self->mpz);
|
|
|
|
return MP_OBJ_FROM_PTR(self2);
|
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
default:
|
|
|
|
return MP_OBJ_NULL; // op not supported
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 23:04:01 -04:00
|
|
|
mp_obj_t mp_obj_int_binary_op(mp_binary_op_t 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)
|
2021-04-22 20:55:39 -04:00
|
|
|
if (mp_obj_is_small_int(lhs_in)) {
|
2014-03-19 19:15:25 -04:00
|
|
|
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;
|
|
|
|
} else {
|
2021-04-22 20:55:39 -04:00
|
|
|
assert(mp_obj_is_type(lhs_in, &mp_type_int));
|
2021-03-15 09:57:36 -04:00
|
|
|
zlhs = &((mp_obj_int_t *)MP_OBJ_TO_PTR(lhs_in))->mpz;
|
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)
|
2021-04-22 20:55:39 -04: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;
|
2021-04-22 20:55:39 -04:00
|
|
|
} else if (mp_obj_is_type(rhs_in, &mp_type_int)) {
|
2021-03-15 09:57:36 -04:00
|
|
|
zrhs = &((mp_obj_int_t *)MP_OBJ_TO_PTR(rhs_in))->mpz;
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2015-08-20 18:30:12 -04:00
|
|
|
} else if (mp_obj_is_float(rhs_in)) {
|
2014-03-29 13:28:20 -04:00
|
|
|
return mp_obj_float_binary_op(op, mpz_as_float(zlhs), rhs_in);
|
2020-02-26 23:36:53 -05:00
|
|
|
#endif
|
2021-03-15 09:57:36 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_COMPLEX
|
2021-04-22 20:55:39 -04:00
|
|
|
} else if (mp_obj_is_type(rhs_in, &mp_type_complex)) {
|
2014-03-29 13:28:20 -04:00
|
|
|
return mp_obj_complex_binary_op(op, mpz_as_float(zlhs), 0, rhs_in);
|
2021-03-15 09:57:36 -04:00
|
|
|
#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
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2021-04-22 20:55:39 -04:00
|
|
|
if (op == MP_BINARY_OP_TRUE_DIVIDE || op == MP_BINARY_OP_INPLACE_TRUE_DIVIDE) {
|
2015-10-01 17:48:48 -04:00
|
|
|
if (mpz_is_zero(zrhs)) {
|
|
|
|
goto zero_division_error;
|
|
|
|
}
|
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);
|
2020-02-19 08:17:13 -05:00
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2014-02-22 14:25:23 -05:00
|
|
|
|
2021-04-22 20:55:39 -04:00
|
|
|
if (op >= MP_BINARY_OP_INPLACE_OR && op < MP_BINARY_OP_CONTAINS) {
|
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: {
|
2015-10-01 17:48:48 -04:00
|
|
|
if (mpz_is_zero(zrhs)) {
|
2021-03-15 09:57:36 -04:00
|
|
|
zero_division_error:
|
2022-11-08 16:24:49 -05:00
|
|
|
mp_raise_ZeroDivisionError();
|
2015-10-01 17:48:48 -04:00
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
mpz_t rem;
|
|
|
|
mpz_init_zero(&rem);
|
2014-02-22 14:25:23 -05:00
|
|
|
mpz_divmod_inpl(&res->mpz, &rem, zlhs, zrhs);
|
|
|
|
mpz_deinit(&rem);
|
|
|
|
break;
|
|
|
|
}
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_MODULO:
|
|
|
|
case MP_BINARY_OP_INPLACE_MODULO: {
|
2015-10-01 17:48:48 -04:00
|
|
|
if (mpz_is_zero(zrhs)) {
|
|
|
|
goto zero_division_error;
|
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
mpz_t quo;
|
|
|
|
mpz_init_zero(&quo);
|
2014-03-20 12:28:41 -04:00
|
|
|
mpz_divmod_inpl(&quo, &res->mpz, zlhs, zrhs);
|
|
|
|
mpz_deinit(&quo);
|
|
|
|
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-07-31 09:41:43 -04:00
|
|
|
mp_int_t irhs = mp_obj_int_get_checked(rhs_in);
|
2014-03-01 14:50:50 -05:00
|
|
|
if (irhs < 0) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("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:
|
2017-07-24 21:49:22 -04:00
|
|
|
if (mpz_is_neg(zrhs)) {
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
return mp_obj_float_binary_op(op, mpz_as_float(zlhs), rhs_in);
|
|
|
|
#else
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("negative power with no float support"));
|
2017-07-24 21:49:22 -04:00
|
|
|
#endif
|
|
|
|
}
|
2014-02-22 14:25:23 -05:00
|
|
|
mpz_pow_inpl(&res->mpz, zlhs, zrhs);
|
|
|
|
break;
|
|
|
|
|
2017-01-19 07:35:45 -05:00
|
|
|
default: {
|
|
|
|
assert(op == MP_BINARY_OP_DIVMOD);
|
2015-10-01 17:48:48 -04:00
|
|
|
if (mpz_is_zero(zrhs)) {
|
|
|
|
goto zero_division_error;
|
|
|
|
}
|
2015-06-13 17:40:50 -04:00
|
|
|
mp_obj_int_t *quo = mp_obj_int_new_mpz();
|
|
|
|
mpz_divmod_inpl(&quo->mpz, &res->mpz, zlhs, zrhs);
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_t tuple[2] = {MP_OBJ_FROM_PTR(quo), MP_OBJ_FROM_PTR(res)};
|
2015-06-13 17:40:50 -04:00
|
|
|
return mp_obj_new_tuple(2, tuple);
|
|
|
|
}
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(res);
|
2014-02-22 14:25:23 -05:00
|
|
|
|
|
|
|
} else {
|
|
|
|
int cmp = mpz_cmp(zlhs, zrhs);
|
|
|
|
switch (op) {
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_LESS:
|
2015-10-11 05:09:43 -04:00
|
|
|
return mp_obj_new_bool(cmp < 0);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_MORE:
|
2015-10-11 05:09:43 -04:00
|
|
|
return mp_obj_new_bool(cmp > 0);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_LESS_EQUAL:
|
2015-10-11 05:09:43 -04:00
|
|
|
return mp_obj_new_bool(cmp <= 0);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_MORE_EQUAL:
|
2015-10-11 05:09:43 -04:00
|
|
|
return mp_obj_new_bool(cmp >= 0);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_EQUAL:
|
2015-10-11 05:09:43 -04:00
|
|
|
return mp_obj_new_bool(cmp == 0);
|
2014-02-22 14:25:23 -05:00
|
|
|
|
|
|
|
default:
|
2014-05-21 14:42:43 -04:00
|
|
|
return MP_OBJ_NULL; // op not supported
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 18:41:22 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_POW3
|
|
|
|
STATIC mpz_t *mp_mpz_for_int(mp_obj_t arg, mpz_t *temp) {
|
2021-04-22 20:55:39 -04:00
|
|
|
if (mp_obj_is_small_int(arg)) {
|
2017-02-01 18:41:22 -05:00
|
|
|
mpz_init_from_int(temp, MP_OBJ_SMALL_INT_VALUE(arg));
|
|
|
|
return temp;
|
|
|
|
} else {
|
|
|
|
mp_obj_int_t *arp_p = MP_OBJ_TO_PTR(arg);
|
|
|
|
return &(arp_p->mpz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t mp_obj_int_pow3(mp_obj_t base, mp_obj_t exponent, mp_obj_t modulus) {
|
2021-04-22 20:55:39 -04:00
|
|
|
if (!mp_obj_is_int(base) || !mp_obj_is_int(exponent) || !mp_obj_is_int(modulus)) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("pow() with 3 arguments requires integers"));
|
2017-02-01 18:41:22 -05:00
|
|
|
} else {
|
|
|
|
mp_obj_t result = mp_obj_new_int_from_ull(0); // Use the _from_ull version as this forces an mpz int
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_obj_int_t *res_p = (mp_obj_int_t *)MP_OBJ_TO_PTR(result);
|
2017-02-01 18:41:22 -05:00
|
|
|
|
|
|
|
mpz_t l_temp, r_temp, m_temp;
|
|
|
|
mpz_t *lhs = mp_mpz_for_int(base, &l_temp);
|
|
|
|
mpz_t *rhs = mp_mpz_for_int(exponent, &r_temp);
|
|
|
|
mpz_t *mod = mp_mpz_for_int(modulus, &m_temp);
|
|
|
|
|
2018-03-25 17:01:06 -04:00
|
|
|
if (mpz_is_zero(mod)) {
|
2021-05-05 20:51:52 -04:00
|
|
|
mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("pow() 3rd argument cannot be 0"));
|
2018-03-25 17:01:06 -04:00
|
|
|
}
|
|
|
|
|
2017-02-01 18:41:22 -05:00
|
|
|
mpz_pow3_inpl(&(res_p->mpz), lhs, rhs, mod);
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
if (lhs == &l_temp) {
|
|
|
|
mpz_deinit(lhs);
|
|
|
|
}
|
|
|
|
if (rhs == &r_temp) {
|
|
|
|
mpz_deinit(rhs);
|
|
|
|
}
|
|
|
|
if (mod == &m_temp) {
|
|
|
|
mpz_deinit(mod);
|
|
|
|
}
|
2017-02-01 18:41:22 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_obj_t mp_obj_new_int(mp_int_t value) {
|
2014-05-28 09:51:12 -04:00
|
|
|
if (MP_SMALL_INT_FITS(value)) {
|
2014-02-22 14:25:23 -05:00
|
|
|
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-09-10 17:10:33 -04:00
|
|
|
mpz_set_from_ll(&o->mpz, val, true);
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-09-10 17:10:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
|
|
|
|
mp_obj_int_t *o = mp_obj_int_new_mpz();
|
|
|
|
mpz_set_from_ll(&o->mpz, val, false);
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
|
|
|
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
|
2016-03-10 16:52:56 -05:00
|
|
|
// SMALL_INT accepts only signed numbers, so make sure the input
|
|
|
|
// value fits completely in the small-int positive range.
|
|
|
|
if ((value & ~MP_SMALL_INT_POSITIVE_MASK) == 0) {
|
2014-02-22 14:25:23 -05:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(value);
|
|
|
|
}
|
2015-04-22 18:17:34 -04:00
|
|
|
return mp_obj_new_int_from_ull(value);
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
|
|
|
|
2017-02-16 00:41:43 -05:00
|
|
|
mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base) {
|
2014-02-22 14:25:23 -05:00
|
|
|
mp_obj_int_t *o = mp_obj_int_new_mpz();
|
2017-02-16 00:41:43 -05:00
|
|
|
size_t n = mpz_set_from_str(&o->mpz, *str, len, neg, base);
|
2014-05-28 09:07:21 -04:00
|
|
|
*str += n;
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
|
|
|
|
2014-12-05 18:13:52 -05:00
|
|
|
mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
|
2021-04-22 20:55:39 -04:00
|
|
|
if (mp_obj_is_small_int(self_in)) {
|
2014-02-22 14:25:23 -05:00
|
|
|
return MP_OBJ_SMALL_INT_VALUE(self_in);
|
2014-03-22 16:54:01 -04:00
|
|
|
} else {
|
2015-11-27 12:01:44 -05:00
|
|
|
const mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-12-05 18:13:52 -05:00
|
|
|
// hash returns actual int value if it fits in mp_int_t
|
2014-07-31 09:41:43 -04:00
|
|
|
return mpz_hash(&self->mpz);
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
|
2021-04-22 20:55:39 -04:00
|
|
|
if (mp_obj_is_small_int(self_in)) {
|
2014-04-03 07:00:54 -04:00
|
|
|
return MP_OBJ_SMALL_INT_VALUE(self_in);
|
|
|
|
} else {
|
2015-11-27 12:01:44 -05:00
|
|
|
const mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_int_t value;
|
2014-04-03 07:00:54 -04:00
|
|
|
if (mpz_as_int_checked(&self->mpz, &value)) {
|
|
|
|
return value;
|
|
|
|
} else {
|
|
|
|
// overflow
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("overflow converting long int to machine word"));
|
2014-04-03 07:00:54 -04:00
|
|
|
}
|
|
|
|
}
|
2014-02-22 14:25:23 -05:00
|
|
|
}
|
|
|
|
|
2020-01-13 16:35:07 -05:00
|
|
|
mp_uint_t mp_obj_int_get_uint_checked(mp_const_obj_t self_in) {
|
|
|
|
if (mp_obj_is_small_int(self_in)) {
|
|
|
|
if (MP_OBJ_SMALL_INT_VALUE(self_in) >= 0) {
|
|
|
|
return MP_OBJ_SMALL_INT_VALUE(self_in);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
mp_uint_t value;
|
|
|
|
if (mpz_as_uint_checked(&self->mpz, &value)) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("overflow converting long int to machine word"));
|
2020-01-13 16:35:07 -05:00
|
|
|
}
|
|
|
|
|
2014-06-01 08:32:54 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2016-12-20 19:46:27 -05:00
|
|
|
mp_float_t mp_obj_int_as_float_impl(mp_obj_t self_in) {
|
2021-04-22 20:55:39 -04:00
|
|
|
assert(mp_obj_is_type(self_in, &mp_type_int));
|
2016-12-20 19:46:27 -05:00
|
|
|
mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return mpz_as_float(&self->mpz);
|
2014-03-22 16:54:01 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-22 14:25:23 -05:00
|
|
|
#endif
|