blob: 332f0bbb8a6578c43ce295681de2384176c547ea [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 Georgee8208a72014-04-04 15:08:23 +010025mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020026 mp_obj_int_t *o = o_in;
27 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010028 case MP_UNARY_OP_BOOL: return MP_BOOL(o->val != 0);
29 case MP_UNARY_OP_POSITIVE: return o_in;
30 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val);
31 case MP_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020032 default: return NULL; // op not supported
33 }
34}
35
Damien Georgee8208a72014-04-04 15:08:23 +010036mp_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 +000037 long long lhs_val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +020038 long long rhs_val;
39
Damien Georged02f6ea2014-03-20 19:31:32 +000040 if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
41 lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +000042 } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +000043 lhs_val = ((mp_obj_int_t*)lhs_in)->val;
44 } else {
45 return MP_OBJ_NULL;
46 }
Damien Georgec4129982014-03-19 23:17:23 +000047
Damien Georged02f6ea2014-03-20 19:31:32 +000048 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
49 rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +000050 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +000051 rhs_val = ((mp_obj_int_t*)rhs_in)->val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +020052 } else {
Damien Georgee8208a72014-04-04 15:08:23 +010053 // delegate to generic function to check for extra cases
54 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Paul Sokolovsky966879c2014-01-17 20:01:36 +020055 }
56
57 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010058 case MP_BINARY_OP_ADD:
59 case MP_BINARY_OP_INPLACE_ADD:
Damien Georged02f6ea2014-03-20 19:31:32 +000060 return mp_obj_new_int_from_ll(lhs_val + rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010061 case MP_BINARY_OP_SUBTRACT:
62 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien Georged02f6ea2014-03-20 19:31:32 +000063 return mp_obj_new_int_from_ll(lhs_val - rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010064 case MP_BINARY_OP_MULTIPLY:
65 case MP_BINARY_OP_INPLACE_MULTIPLY:
Damien Georged02f6ea2014-03-20 19:31:32 +000066 return mp_obj_new_int_from_ll(lhs_val * rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010067 case MP_BINARY_OP_FLOOR_DIVIDE:
68 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Damien Georged02f6ea2014-03-20 19:31:32 +000069 return mp_obj_new_int_from_ll(lhs_val / rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010070 case MP_BINARY_OP_MODULO:
71 case MP_BINARY_OP_INPLACE_MODULO:
Damien Georged02f6ea2014-03-20 19:31:32 +000072 return mp_obj_new_int_from_ll(lhs_val % rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +020073
Damien Georged17926d2014-03-30 13:35:08 +010074 case MP_BINARY_OP_AND:
75 case MP_BINARY_OP_INPLACE_AND:
Damien Georged02f6ea2014-03-20 19:31:32 +000076 return mp_obj_new_int_from_ll(lhs_val & rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010077 case MP_BINARY_OP_OR:
78 case MP_BINARY_OP_INPLACE_OR:
Damien Georged02f6ea2014-03-20 19:31:32 +000079 return mp_obj_new_int_from_ll(lhs_val | rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010080 case MP_BINARY_OP_XOR:
81 case MP_BINARY_OP_INPLACE_XOR:
Damien Georged02f6ea2014-03-20 19:31:32 +000082 return mp_obj_new_int_from_ll(lhs_val ^ rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020083
Damien Georged17926d2014-03-30 13:35:08 +010084 case MP_BINARY_OP_LSHIFT:
85 case MP_BINARY_OP_INPLACE_LSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +000086 return mp_obj_new_int_from_ll(lhs_val << (int)rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010087 case MP_BINARY_OP_RSHIFT:
88 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +000089 return mp_obj_new_int_from_ll(lhs_val >> (int)rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020090
Damien Georged17926d2014-03-30 13:35:08 +010091 case MP_BINARY_OP_LESS:
Damien Georged02f6ea2014-03-20 19:31:32 +000092 return MP_BOOL(lhs_val < rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010093 case MP_BINARY_OP_MORE:
Damien Georged02f6ea2014-03-20 19:31:32 +000094 return MP_BOOL(lhs_val > rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010095 case MP_BINARY_OP_LESS_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +000096 return MP_BOOL(lhs_val <= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010097 case MP_BINARY_OP_MORE_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +000098 return MP_BOOL(lhs_val >= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010099 case MP_BINARY_OP_EQUAL:
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_NOT_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000102 return MP_BOOL(lhs_val != rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200103
104 default:
105 // op not supported
106 return MP_OBJ_NULL;
107 }
108}
109
110mp_obj_t mp_obj_new_int(machine_int_t value) {
111 if (MP_OBJ_FITS_SMALL_INT(value)) {
112 return MP_OBJ_NEW_SMALL_INT(value);
113 }
114 return mp_obj_new_int_from_ll(value);
115}
116
117mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
118 // SMALL_INT accepts only signed numbers, of one bit less size
119 // than word size, which totals 2 bits less for unsigned numbers.
120 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
121 return MP_OBJ_NEW_SMALL_INT(value);
122 }
123 return mp_obj_new_int_from_ll(value);
124}
125
126mp_obj_t mp_obj_new_int_from_ll(long long val) {
127 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000128 o->base.type = &mp_type_int;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200129 o->val = val;
130 return o;
131}
132
133mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
134 long long v;
135 char *end;
136 // TODO: this doesn't handle Python hacked 0o octal syntax
137 v = strtoll(s, &end, 0);
Paul Sokolovsky4d0588d2014-02-18 00:21:11 +0200138 if (*end != 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100139 nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
Paul Sokolovsky4d0588d2014-02-18 00:21:11 +0200140 }
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200141 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000142 o->base.type = &mp_type_int;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200143 o->val = v;
144 return o;
145}
146
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200147machine_int_t mp_obj_int_get(mp_obj_t self_in) {
148 if (MP_OBJ_IS_SMALL_INT(self_in)) {
149 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000150 } else {
151 mp_obj_int_t *self = self_in;
152 return self->val;
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200153 }
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200154}
155
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200156machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
157 // TODO: Check overflow
158 return mp_obj_int_get(self_in);
159}
160
Damien Georgeeabdf672014-03-22 20:54:01 +0000161#if MICROPY_ENABLE_FLOAT
162mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
163 if (MP_OBJ_IS_SMALL_INT(self_in)) {
164 return MP_OBJ_SMALL_INT_VALUE(self_in);
165 } else {
166 mp_obj_int_t *self = self_in;
167 return self->val;
168 }
169}
170#endif
171
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200172#endif