2014-05-03 23:27:38 +01:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 23:27:38 +01:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
2014-05-13 06:44:45 +01:00
|
|
|
* Copyright (c) 2014 Paul Sokolovsky
|
2014-05-03 23:27:38 +01: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-01-17 18:01:36 +00:00
|
|
|
#include <stdlib.h>
|
2014-04-07 19:19:51 +01:00
|
|
|
#include <string.h>
|
2014-01-17 18:01:36 +00:00
|
|
|
|
2015-01-01 20:27:54 +00:00
|
|
|
#include "py/smallint.h"
|
|
|
|
#include "py/objint.h"
|
|
|
|
#include "py/runtime.h"
|
2014-01-17 18:01:36 +00:00
|
|
|
|
2014-12-29 22:22:10 +00:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
#include <math.h>
|
|
|
|
#endif
|
|
|
|
|
2014-01-17 18:01:36 +00:00
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
|
|
|
|
|
2014-07-03 14:50:11 +01:00
|
|
|
#if MICROPY_PY_SYS_MAXSIZE
|
|
|
|
// Export value for sys.maxsize
|
2014-09-06 17:17:23 +01:00
|
|
|
const mp_obj_int_t mp_maxsize_obj = {{&mp_type_int}, MP_SSIZE_MAX};
|
2014-07-03 14:50:11 +01:00
|
|
|
#endif
|
|
|
|
|
2017-01-21 17:15:05 +00:00
|
|
|
mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf) {
|
2017-03-09 23:22:53 +00:00
|
|
|
int delta = 1;
|
|
|
|
if (!big_endian) {
|
|
|
|
buf += len - 1;
|
|
|
|
delta = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_longint_impl_t value = 0;
|
|
|
|
for (; len--; buf += delta) {
|
|
|
|
value = (value << 8) | *buf;
|
|
|
|
}
|
|
|
|
return mp_obj_new_int_from_ll(value);
|
2017-01-21 17:15:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-11 03:20:11 +01:00
|
|
|
void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf) {
|
2019-01-30 07:49:52 +00:00
|
|
|
assert(mp_obj_is_type(self_in, &mp_type_int));
|
2015-04-25 23:16:39 +01:00
|
|
|
mp_obj_int_t *self = self_in;
|
|
|
|
long long val = self->val;
|
|
|
|
if (big_endian) {
|
|
|
|
byte *b = buf + len;
|
|
|
|
while (b > buf) {
|
|
|
|
*--b = val;
|
|
|
|
val >>= 8;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (; len > 0; --len) {
|
|
|
|
*buf++ = val;
|
|
|
|
val >>= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 14:29:12 +00:00
|
|
|
int mp_obj_int_sign(mp_obj_t self_in) {
|
|
|
|
mp_longint_impl_t val;
|
2019-01-30 07:49:52 +00:00
|
|
|
if (mp_obj_is_small_int(self_in)) {
|
2016-01-07 14:29:12 +00:00
|
|
|
val = MP_OBJ_SMALL_INT_VALUE(self_in);
|
|
|
|
} else {
|
|
|
|
mp_obj_int_t *self = self_in;
|
|
|
|
val = self->val;
|
|
|
|
}
|
|
|
|
if (val < 0) {
|
|
|
|
return -1;
|
|
|
|
} else if (val > 0) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
2014-04-08 23:30:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 04:04:01 +01:00
|
|
|
mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
|
2014-01-27 07:05:50 +00:00
|
|
|
mp_obj_int_t *o = o_in;
|
|
|
|
switch (op) {
|
2020-02-27 04:36:53 +00:00
|
|
|
case MP_UNARY_OP_BOOL:
|
|
|
|
return mp_obj_new_bool(o->val != 0);
|
2015-05-11 13:25:19 +01:00
|
|
|
|
|
|
|
// truncate value to fit in mp_int_t, which gives the same hash as
|
|
|
|
// small int if the value fits without truncation
|
2020-02-27 04:36:53 +00:00
|
|
|
case MP_UNARY_OP_HASH:
|
|
|
|
return MP_OBJ_NEW_SMALL_INT((mp_int_t)o->val);
|
2015-05-11 13:25:19 +01:00
|
|
|
|
2020-02-27 04:36:53 +00:00
|
|
|
case MP_UNARY_OP_POSITIVE:
|
|
|
|
return o_in;
|
|
|
|
case MP_UNARY_OP_NEGATIVE:
|
|
|
|
return mp_obj_new_int_from_ll(-o->val);
|
|
|
|
case MP_UNARY_OP_INVERT:
|
|
|
|
return mp_obj_new_int_from_ll(~o->val);
|
2017-09-17 22:06:43 +01:00
|
|
|
case MP_UNARY_OP_ABS: {
|
|
|
|
mp_obj_int_t *self = MP_OBJ_TO_PTR(o_in);
|
|
|
|
if (self->val >= 0) {
|
|
|
|
return o_in;
|
|
|
|
}
|
|
|
|
self = mp_obj_new_int_from_ll(self->val);
|
|
|
|
// TODO could overflow long long
|
|
|
|
self->val = -self->val;
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
2020-02-27 04:36:53 +00:00
|
|
|
default:
|
|
|
|
return MP_OBJ_NULL; // op not supported
|
2014-01-27 07:05:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 04:04:01 +01: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-20 19:31:32 +00:00
|
|
|
long long lhs_val;
|
2014-01-17 18:01:36 +00:00
|
|
|
long long rhs_val;
|
|
|
|
|
2019-01-30 07:49:52 +00:00
|
|
|
if (mp_obj_is_small_int(lhs_in)) {
|
2014-03-20 19:31:32 +00:00
|
|
|
lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs_in);
|
|
|
|
} else {
|
2019-01-30 07:49:52 +00:00
|
|
|
assert(mp_obj_is_type(lhs_in, &mp_type_int));
|
2020-02-27 04:36:53 +00:00
|
|
|
lhs_val = ((mp_obj_int_t *)lhs_in)->val;
|
2014-03-20 19:31:32 +00:00
|
|
|
}
|
2014-03-19 23:17:23 +00:00
|
|
|
|
2019-01-30 07:49:52 +00:00
|
|
|
if (mp_obj_is_small_int(rhs_in)) {
|
2014-03-20 19:31:32 +00:00
|
|
|
rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
|
2019-01-30 07:49:52 +00:00
|
|
|
} else if (mp_obj_is_type(rhs_in, &mp_type_int)) {
|
2020-02-27 04:36:53 +00:00
|
|
|
rhs_val = ((mp_obj_int_t *)rhs_in)->val;
|
2014-01-17 18:01:36 +00:00
|
|
|
} else {
|
2014-04-04 15:08:23 +01:00
|
|
|
// delegate to generic function to check for extra cases
|
|
|
|
return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
|
2014-01-17 18:01:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (op) {
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_ADD:
|
|
|
|
case MP_BINARY_OP_INPLACE_ADD:
|
2014-03-20 19:31:32 +00:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val + rhs_val);
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_SUBTRACT:
|
|
|
|
case MP_BINARY_OP_INPLACE_SUBTRACT:
|
2014-03-20 19:31:32 +00:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val - rhs_val);
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_MULTIPLY:
|
|
|
|
case MP_BINARY_OP_INPLACE_MULTIPLY:
|
2014-03-20 19:31:32 +00:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val * rhs_val);
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_FLOOR_DIVIDE:
|
|
|
|
case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
|
2017-12-08 18:40:55 +00:00
|
|
|
if (rhs_val == 0) {
|
|
|
|
goto zero_division;
|
|
|
|
}
|
2014-03-20 19:31:32 +00:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val / rhs_val);
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_MODULO:
|
|
|
|
case MP_BINARY_OP_INPLACE_MODULO:
|
2017-12-08 18:40:55 +00:00
|
|
|
if (rhs_val == 0) {
|
|
|
|
goto zero_division;
|
|
|
|
}
|
2014-03-20 19:31:32 +00:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val % rhs_val);
|
2014-01-17 18:01:36 +00:00
|
|
|
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_AND:
|
|
|
|
case MP_BINARY_OP_INPLACE_AND:
|
2014-03-20 19:31:32 +00:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val & rhs_val);
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_OR:
|
|
|
|
case MP_BINARY_OP_INPLACE_OR:
|
2014-03-20 19:31:32 +00:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val | rhs_val);
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_XOR:
|
|
|
|
case MP_BINARY_OP_INPLACE_XOR:
|
2014-03-20 19:31:32 +00:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val ^ rhs_val);
|
2014-01-27 07:05:50 +00:00
|
|
|
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_LSHIFT:
|
|
|
|
case MP_BINARY_OP_INPLACE_LSHIFT:
|
2014-03-20 19:31:32 +00:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val << (int)rhs_val);
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_RSHIFT:
|
|
|
|
case MP_BINARY_OP_INPLACE_RSHIFT:
|
2014-03-20 19:31:32 +00:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val >> (int)rhs_val);
|
2014-01-27 07:05:50 +00:00
|
|
|
|
2015-04-25 23:28:10 +01:00
|
|
|
case MP_BINARY_OP_POWER:
|
|
|
|
case MP_BINARY_OP_INPLACE_POWER: {
|
2017-07-25 02:49:22 +01:00
|
|
|
if (rhs_val < 0) {
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
return mp_obj_float_binary_op(op, lhs_val, rhs_in);
|
|
|
|
#else
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("negative power with no float support"));
|
2017-07-25 02:49:22 +01:00
|
|
|
#endif
|
|
|
|
}
|
2015-04-25 23:28:10 +01:00
|
|
|
long long ans = 1;
|
|
|
|
while (rhs_val > 0) {
|
|
|
|
if (rhs_val & 1) {
|
|
|
|
ans *= lhs_val;
|
|
|
|
}
|
|
|
|
if (rhs_val == 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rhs_val /= 2;
|
|
|
|
lhs_val *= lhs_val;
|
|
|
|
}
|
|
|
|
return mp_obj_new_int_from_ll(ans);
|
|
|
|
}
|
|
|
|
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_LESS:
|
2015-10-11 10:09:43 +01:00
|
|
|
return mp_obj_new_bool(lhs_val < rhs_val);
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_MORE:
|
2015-10-11 10:09:43 +01:00
|
|
|
return mp_obj_new_bool(lhs_val > rhs_val);
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_LESS_EQUAL:
|
2015-10-11 10:09:43 +01:00
|
|
|
return mp_obj_new_bool(lhs_val <= rhs_val);
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_MORE_EQUAL:
|
2015-10-11 10:09:43 +01:00
|
|
|
return mp_obj_new_bool(lhs_val >= rhs_val);
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_EQUAL:
|
2015-10-11 10:09:43 +01:00
|
|
|
return mp_obj_new_bool(lhs_val == rhs_val);
|
2014-01-17 18:01:36 +00:00
|
|
|
|
|
|
|
default:
|
2014-05-21 19:42:43 +01:00
|
|
|
return MP_OBJ_NULL; // op not supported
|
2014-01-17 18:01:36 +00:00
|
|
|
}
|
2017-12-08 18:40:55 +00:00
|
|
|
|
|
|
|
zero_division:
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg(&mp_type_ZeroDivisionError, MP_ERROR_TEXT("divide by zero"));
|
2014-01-17 18:01:36 +00:00
|
|
|
}
|
|
|
|
|
2014-07-03 13:25:24 +01:00
|
|
|
mp_obj_t mp_obj_new_int(mp_int_t value) {
|
2014-05-28 14:51:12 +01:00
|
|
|
if (MP_SMALL_INT_FITS(value)) {
|
2014-01-17 18:01:36 +00:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(value);
|
|
|
|
}
|
|
|
|
return mp_obj_new_int_from_ll(value);
|
|
|
|
}
|
|
|
|
|
2014-07-03 13:25:24 +01:00
|
|
|
mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
|
2016-03-10 21:52:56 +00: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-01-17 18:01:36 +00: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 = m_new_obj(mp_obj_int_t);
|
2014-03-29 13:43:38 +00:00
|
|
|
o->base.type = &mp_type_int;
|
2014-01-17 18:01:36 +00:00
|
|
|
o->val = val;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2014-09-10 22:10:33 +01:00
|
|
|
mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
|
|
|
|
// TODO raise an exception if the unsigned long long won't fit
|
2015-11-08 23:34:13 +00:00
|
|
|
if (val >> (sizeof(unsigned long long) * 8 - 1) != 0) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("ulonglong too large"));
|
2015-11-08 23:34:13 +00:00
|
|
|
}
|
2014-09-10 22:10:33 +01:00
|
|
|
mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
|
|
|
|
o->base.type = &mp_type_int;
|
|
|
|
o->val = val;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2017-02-16 05:41:43 +00:00
|
|
|
mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base) {
|
2014-05-28 14:07:21 +01:00
|
|
|
// TODO this does not honor the given length of the string, but it all cases it should anyway be null terminated
|
|
|
|
// TODO check overflow
|
2014-01-17 18:01:36 +00:00
|
|
|
mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
|
2014-03-29 13:43:38 +00:00
|
|
|
o->base.type = &mp_type_int;
|
2014-05-28 14:07:21 +01:00
|
|
|
char *endptr;
|
|
|
|
o->val = strtoll(*str, &endptr, base);
|
|
|
|
*str = endptr;
|
2014-01-17 18:01:36 +00:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2014-12-05 23:13:52 +00:00
|
|
|
mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
|
2019-01-30 07:49:52 +00:00
|
|
|
if (mp_obj_is_small_int(self_in)) {
|
2014-01-18 14:07:16 +00:00
|
|
|
return MP_OBJ_SMALL_INT_VALUE(self_in);
|
2014-03-22 20:54:01 +00:00
|
|
|
} else {
|
2014-05-28 14:07:21 +01:00
|
|
|
const mp_obj_int_t *self = self_in;
|
2014-03-22 20:54:01 +00:00
|
|
|
return self->val;
|
2014-01-18 14:07:16 +00:00
|
|
|
}
|
2014-01-17 18:01:36 +00:00
|
|
|
}
|
|
|
|
|
2014-07-03 13:25:24 +01:00
|
|
|
mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
|
2014-01-18 14:07:16 +00:00
|
|
|
// TODO: Check overflow
|
2014-12-05 23:13:52 +00:00
|
|
|
return mp_obj_int_get_truncated(self_in);
|
2014-01-18 14:07:16 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 13:32:54 +01:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2016-12-21 00:46:27 +00:00
|
|
|
mp_float_t mp_obj_int_as_float_impl(mp_obj_t self_in) {
|
2019-01-30 07:49:52 +00:00
|
|
|
assert(mp_obj_is_type(self_in, &mp_type_int));
|
2016-12-21 00:46:27 +00:00
|
|
|
mp_obj_int_t *self = self_in;
|
|
|
|
return self->val;
|
2014-03-22 20:54:01 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-17 18:01:36 +00:00
|
|
|
#endif
|