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
|
2014-05-13 01:44:45 -04:00
|
|
|
* Copyright (c) 2014 Paul Sokolovsky
|
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-01-17 13:01:36 -05:00
|
|
|
#include <stdlib.h>
|
2014-04-07 14:19:51 -04:00
|
|
|
#include <string.h>
|
2014-01-17 13:01:36 -05:00
|
|
|
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/smallint.h"
|
|
|
|
#include "py/objint.h"
|
|
|
|
#include "py/runtime0.h"
|
|
|
|
#include "py/runtime.h"
|
2014-01-17 13:01:36 -05:00
|
|
|
|
2014-12-29 17:22:10 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
#include <math.h>
|
|
|
|
#endif
|
|
|
|
|
2014-01-17 13:01:36 -05:00
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
|
|
|
|
|
|
|
|
// Python3 no longer has "l" suffix for long ints. We allow to use it
|
|
|
|
// for debugging purpose though.
|
|
|
|
#ifdef DEBUG
|
|
|
|
#define SUFFIX "l"
|
|
|
|
#else
|
|
|
|
#define SUFFIX ""
|
|
|
|
#endif
|
|
|
|
|
2014-07-03 09:50:11 -04:00
|
|
|
#if MICROPY_PY_SYS_MAXSIZE
|
|
|
|
// Export value for sys.maxsize
|
2014-09-06 12:17:23 -04:00
|
|
|
const mp_obj_int_t mp_maxsize_obj = {{&mp_type_int}, MP_SSIZE_MAX};
|
2014-07-03 09:50:11 -04:00
|
|
|
#endif
|
|
|
|
|
2014-07-24 09:21:37 -04:00
|
|
|
mp_int_t mp_obj_int_hash(mp_obj_t self_in) {
|
|
|
|
if (MP_OBJ_IS_SMALL_INT(self_in)) {
|
|
|
|
return MP_OBJ_SMALL_INT_VALUE(self_in);
|
|
|
|
}
|
|
|
|
mp_obj_int_t *self = self_in;
|
|
|
|
// truncate value to fit in mp_int_t, which gives the same hash as
|
|
|
|
// small int if the value fits without truncation
|
|
|
|
return self->val;
|
|
|
|
}
|
|
|
|
|
2014-04-08 18:30:46 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
mp_obj_int_t *self = self_in;
|
|
|
|
return self->val >= 0;
|
|
|
|
}
|
|
|
|
|
2015-03-14 18:07:30 -04:00
|
|
|
// This must handle int and bool types, and must raise a
|
|
|
|
// TypeError if the argument is not integral
|
|
|
|
mp_obj_t mp_obj_int_abs(mp_obj_t self_in) {
|
|
|
|
if (MP_OBJ_IS_TYPE(self_in, &mp_type_int)) {
|
|
|
|
mp_obj_int_t *self = self_in;
|
|
|
|
self = mp_obj_new_int_from_ll(self->val);
|
|
|
|
if (self->val < 0) {
|
|
|
|
// TODO could overflow long long
|
|
|
|
self->val = -self->val;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
} else {
|
|
|
|
mp_int_t val = mp_obj_get_int(self_in);
|
|
|
|
if (val == MP_SMALL_INT_MIN) {
|
|
|
|
return mp_obj_new_int_from_ll(-val);
|
|
|
|
} else {
|
|
|
|
if (val < 0) {
|
|
|
|
val = -val;
|
|
|
|
}
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-29 19:35:11 -04:00
|
|
|
mp_obj_t mp_obj_int_unary_op(mp_uint_t op, mp_obj_t o_in) {
|
2014-01-27 02:05:50 -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(o->val != 0);
|
|
|
|
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);
|
2014-05-21 14:42:43 -04:00
|
|
|
default: return MP_OBJ_NULL; // op not supported
|
2014-01-27 02:05:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-29 19:35:11 -04:00
|
|
|
mp_obj_t mp_obj_int_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
2014-03-20 15:31:32 -04:00
|
|
|
long long lhs_val;
|
2014-01-17 13:01:36 -05:00
|
|
|
long long rhs_val;
|
|
|
|
|
2014-03-20 15:31:32 -04:00
|
|
|
if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
|
|
|
|
lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs_in);
|
2014-03-29 09:43:38 -04:00
|
|
|
} else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
|
2014-03-20 15:31:32 -04:00
|
|
|
lhs_val = ((mp_obj_int_t*)lhs_in)->val;
|
|
|
|
} else {
|
2014-05-21 14:42:43 -04:00
|
|
|
return MP_OBJ_NULL; // op not supported
|
2014-03-20 15:31:32 -04:00
|
|
|
}
|
2014-03-19 19:17:23 -04:00
|
|
|
|
2014-03-20 15:31:32 -04:00
|
|
|
if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
|
|
|
|
rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
|
2014-03-29 09:43:38 -04:00
|
|
|
} else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
|
2014-03-20 15:31:32 -04:00
|
|
|
rhs_val = ((mp_obj_int_t*)rhs_in)->val;
|
2014-01-17 13:01:36 -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-01-17 13:01:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (op) {
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_ADD:
|
|
|
|
case MP_BINARY_OP_INPLACE_ADD:
|
2014-03-20 15:31:32 -04:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val + rhs_val);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_SUBTRACT:
|
|
|
|
case MP_BINARY_OP_INPLACE_SUBTRACT:
|
2014-03-20 15:31:32 -04:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val - rhs_val);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_MULTIPLY:
|
|
|
|
case MP_BINARY_OP_INPLACE_MULTIPLY:
|
2014-03-20 15:31:32 -04:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val * rhs_val);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_FLOOR_DIVIDE:
|
|
|
|
case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
|
2014-03-20 15:31:32 -04:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val / rhs_val);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_MODULO:
|
|
|
|
case MP_BINARY_OP_INPLACE_MODULO:
|
2014-03-20 15:31:32 -04:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val % rhs_val);
|
2014-01-17 13:01:36 -05:00
|
|
|
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_AND:
|
|
|
|
case MP_BINARY_OP_INPLACE_AND:
|
2014-03-20 15:31:32 -04:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val & rhs_val);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_OR:
|
|
|
|
case MP_BINARY_OP_INPLACE_OR:
|
2014-03-20 15:31:32 -04:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val | rhs_val);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_XOR:
|
|
|
|
case MP_BINARY_OP_INPLACE_XOR:
|
2014-03-20 15:31:32 -04:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val ^ rhs_val);
|
2014-01-27 02:05:50 -05:00
|
|
|
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_LSHIFT:
|
|
|
|
case MP_BINARY_OP_INPLACE_LSHIFT:
|
2014-03-20 15:31:32 -04:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val << (int)rhs_val);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_RSHIFT:
|
|
|
|
case MP_BINARY_OP_INPLACE_RSHIFT:
|
2014-03-20 15:31:32 -04:00
|
|
|
return mp_obj_new_int_from_ll(lhs_val >> (int)rhs_val);
|
2014-01-27 02:05:50 -05:00
|
|
|
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_LESS:
|
2014-03-20 15:31:32 -04:00
|
|
|
return MP_BOOL(lhs_val < rhs_val);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_MORE:
|
2014-03-20 15:31:32 -04:00
|
|
|
return MP_BOOL(lhs_val > rhs_val);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_LESS_EQUAL:
|
2014-03-20 15:31:32 -04:00
|
|
|
return MP_BOOL(lhs_val <= rhs_val);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_MORE_EQUAL:
|
2014-03-20 15:31:32 -04:00
|
|
|
return MP_BOOL(lhs_val >= rhs_val);
|
2014-03-30 08:35:08 -04:00
|
|
|
case MP_BINARY_OP_EQUAL:
|
2014-03-20 15:31:32 -04:00
|
|
|
return MP_BOOL(lhs_val == rhs_val);
|
2014-01-17 13:01:36 -05:00
|
|
|
|
|
|
|
default:
|
2014-05-21 14:42:43 -04:00
|
|
|
return MP_OBJ_NULL; // op not supported
|
2014-01-17 13:01:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-17 13:01:36 -05:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(value);
|
|
|
|
}
|
|
|
|
return mp_obj_new_int_from_ll(value);
|
|
|
|
}
|
|
|
|
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
|
2014-01-17 13:01:36 -05:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 09:43:38 -04:00
|
|
|
o->base.type = &mp_type_int;
|
2014-01-17 13:01:36 -05:00
|
|
|
o->val = val;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2014-09-10 17:10:33 -04: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
|
|
|
|
assert(val >> (sizeof(unsigned long long) * 8 - 1) == 0);
|
|
|
|
mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
|
|
|
|
o->base.type = &mp_type_int;
|
|
|
|
o->val = val;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2014-12-29 17:22:10 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
|
2015-01-13 10:22:30 -05:00
|
|
|
int cl = fpclassify(val);
|
|
|
|
if (cl == FP_INFINITE) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OverflowError, "can't convert inf to int"));
|
|
|
|
} else if (cl == FP_NAN) {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "can't convert NaN to int"));
|
|
|
|
} else {
|
|
|
|
mp_fp_as_int_class_t icl = mp_classify_fp_as_int(val);
|
|
|
|
if (icl == MP_FP_CLASS_FIT_SMALLINT) {
|
|
|
|
return MP_OBJ_NEW_SMALL_INT((mp_int_t)val);
|
|
|
|
} else if (icl == MP_FP_CLASS_FIT_LONGINT) {
|
|
|
|
return mp_obj_new_int_from_ll((long long)val);
|
|
|
|
} else {
|
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "float too big"));
|
|
|
|
}
|
|
|
|
}
|
2014-12-29 17:22:10 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-09-10 17:10:33 -04:00
|
|
|
mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg, mp_uint_t base) {
|
2014-05-28 09:07:21 -04: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 13:01:36 -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-05-28 09:07:21 -04:00
|
|
|
char *endptr;
|
|
|
|
o->val = strtoll(*str, &endptr, base);
|
|
|
|
*str = endptr;
|
2014-01-17 13:01:36 -05:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2014-12-05 18:13:52 -05:00
|
|
|
mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
|
2014-01-18 09:07:16 -05:00
|
|
|
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 {
|
2014-05-28 09:07:21 -04:00
|
|
|
const mp_obj_int_t *self = self_in;
|
2014-03-22 16:54:01 -04:00
|
|
|
return self->val;
|
2014-01-18 09:07:16 -05:00
|
|
|
}
|
2014-01-17 13:01:36 -05:00
|
|
|
}
|
|
|
|
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
|
2014-01-18 09:07:16 -05:00
|
|
|
// TODO: Check overflow
|
2014-12-05 18:13:52 -05:00
|
|
|
return mp_obj_int_get_truncated(self_in);
|
2014-01-18 09:07:16 -05:00
|
|
|
}
|
|
|
|
|
2014-06-01 08:32:54 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-03-22 16:54:01 -04:00
|
|
|
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 self->val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-17 13:01:36 -05:00
|
|
|
#endif
|