2014-05-03 23:27:38 +01: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-01-17 18:01:36 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2014-04-07 19:19:51 +01:00
|
|
|
#include <string.h>
|
2014-01-17 18:01:36 +00:00
|
|
|
|
2014-05-02 15:47:01 +01:00
|
|
|
#include "mpconfig.h"
|
2014-01-17 18:01:36 +00:00
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.h"
|
2014-01-21 21:40:13 +00:00
|
|
|
#include "qstr.h"
|
2014-01-17 18:01:36 +00:00
|
|
|
#include "obj.h"
|
2014-02-22 19:25:23 +00:00
|
|
|
#include "mpz.h"
|
2014-01-17 18:01:36 +00:00
|
|
|
#include "objint.h"
|
|
|
|
#include "runtime0.h"
|
2014-04-02 12:22:07 +01:00
|
|
|
#include "runtime.h"
|
2014-01-17 18:01:36 +00: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-04-08 23:30:46 +01: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;
|
|
|
|
}
|
|
|
|
|
2014-04-04 15:08:23 +01:00
|
|
|
mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
|
2014-01-27 07:05:50 +00:00
|
|
|
mp_obj_int_t *o = o_in;
|
|
|
|
switch (op) {
|
2014-03-30 13:35:08 +01: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-04-17 23:19:36 +01:00
|
|
|
default: return MP_OBJ_NOT_SUPPORTED;
|
2014-01-27 07:05:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-04 15:08:23 +01:00
|
|
|
mp_obj_t mp_obj_int_binary_op(int 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;
|
|
|
|
|
2014-03-20 19:31:32 +00:00
|
|
|
if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
|
|
|
|
lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs_in);
|
2014-03-29 13:43:38 +00:00
|
|
|
} else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
|
2014-03-20 19:31:32 +00:00
|
|
|
lhs_val = ((mp_obj_int_t*)lhs_in)->val;
|
|
|
|
} else {
|
2014-04-17 23:19:36 +01:00
|
|
|
return MP_OBJ_NOT_SUPPORTED;
|
2014-03-20 19:31:32 +00:00
|
|
|
}
|
2014-03-19 23:17:23 +00:00
|
|
|
|
2014-03-20 19:31:32 +00:00
|
|
|
if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
|
|
|
|
rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
|
2014-03-29 13:43:38 +00:00
|
|
|
} else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
|
2014-03-20 19:31:32 +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:
|
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:
|
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
|
|
|
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_LESS:
|
2014-03-20 19:31:32 +00:00
|
|
|
return MP_BOOL(lhs_val < rhs_val);
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_MORE:
|
2014-03-20 19:31:32 +00:00
|
|
|
return MP_BOOL(lhs_val > rhs_val);
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_LESS_EQUAL:
|
2014-03-20 19:31:32 +00:00
|
|
|
return MP_BOOL(lhs_val <= rhs_val);
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_MORE_EQUAL:
|
2014-03-20 19:31:32 +00:00
|
|
|
return MP_BOOL(lhs_val >= rhs_val);
|
2014-03-30 13:35:08 +01:00
|
|
|
case MP_BINARY_OP_EQUAL:
|
2014-03-20 19:31:32 +00:00
|
|
|
return MP_BOOL(lhs_val == rhs_val);
|
2014-01-17 18:01:36 +00:00
|
|
|
|
|
|
|
default:
|
2014-04-17 23:19:36 +01:00
|
|
|
return MP_OBJ_NOT_SUPPORTED;
|
2014-01-17 18:01:36 +00: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_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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
|
|
|
|
long long v;
|
|
|
|
char *end;
|
|
|
|
// TODO: this doesn't handle Python hacked 0o octal syntax
|
|
|
|
v = strtoll(s, &end, 0);
|
2014-02-17 22:21:11 +00:00
|
|
|
if (*end != 0) {
|
2014-04-05 18:32:08 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
|
2014-02-17 22:21:11 +00:00
|
|
|
}
|
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-01-17 18:01:36 +00:00
|
|
|
o->val = v;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2014-01-18 14:07:16 +00:00
|
|
|
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 20:54:01 +00:00
|
|
|
} else {
|
|
|
|
mp_obj_int_t *self = self_in;
|
|
|
|
return self->val;
|
2014-01-18 14:07:16 +00:00
|
|
|
}
|
2014-01-17 18:01:36 +00:00
|
|
|
}
|
|
|
|
|
2014-01-18 14:07:16 +00:00
|
|
|
machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
|
|
|
|
// TODO: Check overflow
|
|
|
|
return mp_obj_int_get(self_in);
|
|
|
|
}
|
|
|
|
|
2014-03-22 20:54:01 +00: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 self->val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-17 18:01:36 +00:00
|
|
|
#endif
|