blob: 7d71c5a69173ae18f94e9954b17fcc5a119ea521 [file] [log] [blame]
Paul Sokolovsky966879c2014-01-17 20:01:36 +02001#include <stdlib.h>
2#include <stdint.h>
Dave Hylandsc4029e52014-04-07 11:19:51 -07003#include <string.h>
Paul Sokolovsky966879c2014-01-17 20:01:36 +02004
5#include "nlr.h"
6#include "misc.h"
7#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00008#include "qstr.h"
Paul Sokolovsky966879c2014-01-17 20:01:36 +02009#include "obj.h"
Damien George438c88d2014-02-22 19:25:23 +000010#include "mpz.h"
Paul Sokolovsky966879c2014-01-17 20:01:36 +020011#include "objint.h"
12#include "runtime0.h"
Damien George660aef62014-04-02 12:22:07 +010013#include "runtime.h"
Paul Sokolovsky966879c2014-01-17 20:01:36 +020014
15#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
16
Paul Sokolovsky966879c2014-01-17 20:01:36 +020017// Python3 no longer has "l" suffix for long ints. We allow to use it
18// for debugging purpose though.
19#ifdef DEBUG
20#define SUFFIX "l"
21#else
22#define SUFFIX ""
23#endif
24
Damien George88d7bba2014-04-08 23:30:46 +010025bool mp_obj_int_is_positive(mp_obj_t self_in) {
26 if (MP_OBJ_IS_SMALL_INT(self_in)) {
27 return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0;
28 }
29 mp_obj_int_t *self = self_in;
30 return self->val >= 0;
31}
32
Damien Georgee8208a72014-04-04 15:08:23 +010033mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020034 mp_obj_int_t *o = o_in;
35 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010036 case MP_UNARY_OP_BOOL: return MP_BOOL(o->val != 0);
37 case MP_UNARY_OP_POSITIVE: return o_in;
38 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val);
39 case MP_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val);
Damien Georgeea8d06c2014-04-17 23:19:36 +010040 default: return MP_OBJ_NOT_SUPPORTED;
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020041 }
42}
43
Damien Georgee8208a72014-04-04 15:08:23 +010044mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georged02f6ea2014-03-20 19:31:32 +000045 long long lhs_val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +020046 long long rhs_val;
47
Damien Georged02f6ea2014-03-20 19:31:32 +000048 if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
49 lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +000050 } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +000051 lhs_val = ((mp_obj_int_t*)lhs_in)->val;
52 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +010053 return MP_OBJ_NOT_SUPPORTED;
Damien Georged02f6ea2014-03-20 19:31:32 +000054 }
Damien Georgec4129982014-03-19 23:17:23 +000055
Damien Georged02f6ea2014-03-20 19:31:32 +000056 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
57 rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +000058 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +000059 rhs_val = ((mp_obj_int_t*)rhs_in)->val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +020060 } else {
Damien Georgee8208a72014-04-04 15:08:23 +010061 // delegate to generic function to check for extra cases
62 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Paul Sokolovsky966879c2014-01-17 20:01:36 +020063 }
64
65 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010066 case MP_BINARY_OP_ADD:
67 case MP_BINARY_OP_INPLACE_ADD:
Damien Georged02f6ea2014-03-20 19:31:32 +000068 return mp_obj_new_int_from_ll(lhs_val + rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010069 case MP_BINARY_OP_SUBTRACT:
70 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien Georged02f6ea2014-03-20 19:31:32 +000071 return mp_obj_new_int_from_ll(lhs_val - rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010072 case MP_BINARY_OP_MULTIPLY:
73 case MP_BINARY_OP_INPLACE_MULTIPLY:
Damien Georged02f6ea2014-03-20 19:31:32 +000074 return mp_obj_new_int_from_ll(lhs_val * rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010075 case MP_BINARY_OP_FLOOR_DIVIDE:
76 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Damien Georged02f6ea2014-03-20 19:31:32 +000077 return mp_obj_new_int_from_ll(lhs_val / rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010078 case MP_BINARY_OP_MODULO:
79 case MP_BINARY_OP_INPLACE_MODULO:
Damien Georged02f6ea2014-03-20 19:31:32 +000080 return mp_obj_new_int_from_ll(lhs_val % rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +020081
Damien Georged17926d2014-03-30 13:35:08 +010082 case MP_BINARY_OP_AND:
83 case MP_BINARY_OP_INPLACE_AND:
Damien Georged02f6ea2014-03-20 19:31:32 +000084 return mp_obj_new_int_from_ll(lhs_val & rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010085 case MP_BINARY_OP_OR:
86 case MP_BINARY_OP_INPLACE_OR:
Damien Georged02f6ea2014-03-20 19:31:32 +000087 return mp_obj_new_int_from_ll(lhs_val | rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010088 case MP_BINARY_OP_XOR:
89 case MP_BINARY_OP_INPLACE_XOR:
Damien Georged02f6ea2014-03-20 19:31:32 +000090 return mp_obj_new_int_from_ll(lhs_val ^ rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020091
Damien Georged17926d2014-03-30 13:35:08 +010092 case MP_BINARY_OP_LSHIFT:
93 case MP_BINARY_OP_INPLACE_LSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +000094 return mp_obj_new_int_from_ll(lhs_val << (int)rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010095 case MP_BINARY_OP_RSHIFT:
96 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +000097 return mp_obj_new_int_from_ll(lhs_val >> (int)rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020098
Damien Georged17926d2014-03-30 13:35:08 +010099 case MP_BINARY_OP_LESS:
Damien Georged02f6ea2014-03-20 19:31:32 +0000100 return MP_BOOL(lhs_val < rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100101 case MP_BINARY_OP_MORE:
Damien Georged02f6ea2014-03-20 19:31:32 +0000102 return MP_BOOL(lhs_val > rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100103 case MP_BINARY_OP_LESS_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000104 return MP_BOOL(lhs_val <= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100105 case MP_BINARY_OP_MORE_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000106 return MP_BOOL(lhs_val >= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100107 case MP_BINARY_OP_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000108 return MP_BOOL(lhs_val == rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200109
110 default:
Damien Georgeea8d06c2014-04-17 23:19:36 +0100111 return MP_OBJ_NOT_SUPPORTED;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200112 }
113}
114
115mp_obj_t mp_obj_new_int(machine_int_t value) {
116 if (MP_OBJ_FITS_SMALL_INT(value)) {
117 return MP_OBJ_NEW_SMALL_INT(value);
118 }
119 return mp_obj_new_int_from_ll(value);
120}
121
122mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
123 // SMALL_INT accepts only signed numbers, of one bit less size
124 // than word size, which totals 2 bits less for unsigned numbers.
125 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
126 return MP_OBJ_NEW_SMALL_INT(value);
127 }
128 return mp_obj_new_int_from_ll(value);
129}
130
131mp_obj_t mp_obj_new_int_from_ll(long long val) {
132 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000133 o->base.type = &mp_type_int;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200134 o->val = val;
135 return o;
136}
137
138mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
139 long long v;
140 char *end;
141 // TODO: this doesn't handle Python hacked 0o octal syntax
142 v = strtoll(s, &end, 0);
Paul Sokolovsky4d0588d2014-02-18 00:21:11 +0200143 if (*end != 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100144 nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
Paul Sokolovsky4d0588d2014-02-18 00:21:11 +0200145 }
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200146 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000147 o->base.type = &mp_type_int;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200148 o->val = v;
149 return o;
150}
151
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200152machine_int_t mp_obj_int_get(mp_obj_t self_in) {
153 if (MP_OBJ_IS_SMALL_INT(self_in)) {
154 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000155 } else {
156 mp_obj_int_t *self = self_in;
157 return self->val;
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200158 }
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200159}
160
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200161machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
162 // TODO: Check overflow
163 return mp_obj_int_get(self_in);
164}
165
Damien Georgeeabdf672014-03-22 20:54:01 +0000166#if MICROPY_ENABLE_FLOAT
167mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
168 if (MP_OBJ_IS_SMALL_INT(self_in)) {
169 return MP_OBJ_SMALL_INT_VALUE(self_in);
170 } else {
171 mp_obj_int_t *self = self_in;
172 return self->val;
173 }
174}
175#endif
176
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200177#endif