blob: f648615a73b1685aa4120c232ef7a393ba2f82be [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Paul Sokolovsky966879c2014-01-17 20:01:36 +020027#include <stdlib.h>
28#include <stdint.h>
Dave Hylandsc4029e52014-04-07 11:19:51 -070029#include <string.h>
Paul Sokolovsky966879c2014-01-17 20:01:36 +020030
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030031#include "mpconfig.h"
Paul Sokolovsky966879c2014-01-17 20:01:36 +020032#include "nlr.h"
33#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000034#include "qstr.h"
Paul Sokolovsky966879c2014-01-17 20:01:36 +020035#include "obj.h"
Damien George438c88d2014-02-22 19:25:23 +000036#include "mpz.h"
Paul Sokolovsky966879c2014-01-17 20:01:36 +020037#include "objint.h"
38#include "runtime0.h"
Damien George660aef62014-04-02 12:22:07 +010039#include "runtime.h"
Paul Sokolovsky966879c2014-01-17 20:01:36 +020040
41#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
42
Paul Sokolovsky966879c2014-01-17 20:01:36 +020043// Python3 no longer has "l" suffix for long ints. We allow to use it
44// for debugging purpose though.
45#ifdef DEBUG
46#define SUFFIX "l"
47#else
48#define SUFFIX ""
49#endif
50
Damien George88d7bba2014-04-08 23:30:46 +010051bool mp_obj_int_is_positive(mp_obj_t self_in) {
52 if (MP_OBJ_IS_SMALL_INT(self_in)) {
53 return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0;
54 }
55 mp_obj_int_t *self = self_in;
56 return self->val >= 0;
57}
58
Damien Georgee8208a72014-04-04 15:08:23 +010059mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020060 mp_obj_int_t *o = o_in;
61 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010062 case MP_UNARY_OP_BOOL: return MP_BOOL(o->val != 0);
63 case MP_UNARY_OP_POSITIVE: return o_in;
64 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val);
65 case MP_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val);
Damien Georgeea8d06c2014-04-17 23:19:36 +010066 default: return MP_OBJ_NOT_SUPPORTED;
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020067 }
68}
69
Damien Georgee8208a72014-04-04 15:08:23 +010070mp_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 +000071 long long lhs_val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +020072 long long rhs_val;
73
Damien Georged02f6ea2014-03-20 19:31:32 +000074 if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
75 lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +000076 } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +000077 lhs_val = ((mp_obj_int_t*)lhs_in)->val;
78 } else {
Damien Georgeea8d06c2014-04-17 23:19:36 +010079 return MP_OBJ_NOT_SUPPORTED;
Damien Georged02f6ea2014-03-20 19:31:32 +000080 }
Damien Georgec4129982014-03-19 23:17:23 +000081
Damien Georged02f6ea2014-03-20 19:31:32 +000082 if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
83 rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
Damien George3e1a5c12014-03-29 13:43:38 +000084 } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
Damien Georged02f6ea2014-03-20 19:31:32 +000085 rhs_val = ((mp_obj_int_t*)rhs_in)->val;
Paul Sokolovsky966879c2014-01-17 20:01:36 +020086 } else {
Damien Georgee8208a72014-04-04 15:08:23 +010087 // delegate to generic function to check for extra cases
88 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Paul Sokolovsky966879c2014-01-17 20:01:36 +020089 }
90
91 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010092 case MP_BINARY_OP_ADD:
93 case MP_BINARY_OP_INPLACE_ADD:
Damien Georged02f6ea2014-03-20 19:31:32 +000094 return mp_obj_new_int_from_ll(lhs_val + rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010095 case MP_BINARY_OP_SUBTRACT:
96 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damien Georged02f6ea2014-03-20 19:31:32 +000097 return mp_obj_new_int_from_ll(lhs_val - rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +010098 case MP_BINARY_OP_MULTIPLY:
99 case MP_BINARY_OP_INPLACE_MULTIPLY:
Damien Georged02f6ea2014-03-20 19:31:32 +0000100 return mp_obj_new_int_from_ll(lhs_val * rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100101 case MP_BINARY_OP_FLOOR_DIVIDE:
102 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Damien Georged02f6ea2014-03-20 19:31:32 +0000103 return mp_obj_new_int_from_ll(lhs_val / rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100104 case MP_BINARY_OP_MODULO:
105 case MP_BINARY_OP_INPLACE_MODULO:
Damien Georged02f6ea2014-03-20 19:31:32 +0000106 return mp_obj_new_int_from_ll(lhs_val % rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200107
Damien Georged17926d2014-03-30 13:35:08 +0100108 case MP_BINARY_OP_AND:
109 case MP_BINARY_OP_INPLACE_AND:
Damien Georged02f6ea2014-03-20 19:31:32 +0000110 return mp_obj_new_int_from_ll(lhs_val & rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100111 case MP_BINARY_OP_OR:
112 case MP_BINARY_OP_INPLACE_OR:
Damien Georged02f6ea2014-03-20 19:31:32 +0000113 return mp_obj_new_int_from_ll(lhs_val | rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100114 case MP_BINARY_OP_XOR:
115 case MP_BINARY_OP_INPLACE_XOR:
Damien Georged02f6ea2014-03-20 19:31:32 +0000116 return mp_obj_new_int_from_ll(lhs_val ^ rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200117
Damien Georged17926d2014-03-30 13:35:08 +0100118 case MP_BINARY_OP_LSHIFT:
119 case MP_BINARY_OP_INPLACE_LSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000120 return mp_obj_new_int_from_ll(lhs_val << (int)rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100121 case MP_BINARY_OP_RSHIFT:
122 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien Georged02f6ea2014-03-20 19:31:32 +0000123 return mp_obj_new_int_from_ll(lhs_val >> (int)rhs_val);
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200124
Damien Georged17926d2014-03-30 13:35:08 +0100125 case MP_BINARY_OP_LESS:
Damien Georged02f6ea2014-03-20 19:31:32 +0000126 return MP_BOOL(lhs_val < rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100127 case MP_BINARY_OP_MORE:
Damien Georged02f6ea2014-03-20 19:31:32 +0000128 return MP_BOOL(lhs_val > rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100129 case MP_BINARY_OP_LESS_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000130 return MP_BOOL(lhs_val <= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100131 case MP_BINARY_OP_MORE_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000132 return MP_BOOL(lhs_val >= rhs_val);
Damien Georged17926d2014-03-30 13:35:08 +0100133 case MP_BINARY_OP_EQUAL:
Damien Georged02f6ea2014-03-20 19:31:32 +0000134 return MP_BOOL(lhs_val == rhs_val);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200135
136 default:
Damien Georgeea8d06c2014-04-17 23:19:36 +0100137 return MP_OBJ_NOT_SUPPORTED;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200138 }
139}
140
141mp_obj_t mp_obj_new_int(machine_int_t value) {
142 if (MP_OBJ_FITS_SMALL_INT(value)) {
143 return MP_OBJ_NEW_SMALL_INT(value);
144 }
145 return mp_obj_new_int_from_ll(value);
146}
147
148mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
149 // SMALL_INT accepts only signed numbers, of one bit less size
150 // than word size, which totals 2 bits less for unsigned numbers.
151 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
152 return MP_OBJ_NEW_SMALL_INT(value);
153 }
154 return mp_obj_new_int_from_ll(value);
155}
156
157mp_obj_t mp_obj_new_int_from_ll(long long val) {
158 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000159 o->base.type = &mp_type_int;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200160 o->val = val;
161 return o;
162}
163
Damien Georgea32c1e42014-05-07 18:30:52 +0100164mp_obj_t mp_obj_new_int_from_qstr(qstr qst) {
165 const char *s = qstr_str(qst);
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200166 long long v;
167 char *end;
168 // TODO: this doesn't handle Python hacked 0o octal syntax
169 v = strtoll(s, &end, 0);
Paul Sokolovsky4d0588d2014-02-18 00:21:11 +0200170 if (*end != 0) {
Damien Georgeea13f402014-04-05 18:32:08 +0100171 nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
Paul Sokolovsky4d0588d2014-02-18 00:21:11 +0200172 }
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200173 mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
Damien George3e1a5c12014-03-29 13:43:38 +0000174 o->base.type = &mp_type_int;
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200175 o->val = v;
176 return o;
177}
178
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200179machine_int_t mp_obj_int_get(mp_obj_t self_in) {
180 if (MP_OBJ_IS_SMALL_INT(self_in)) {
181 return MP_OBJ_SMALL_INT_VALUE(self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000182 } else {
183 mp_obj_int_t *self = self_in;
184 return self->val;
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200185 }
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200186}
187
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200188machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
189 // TODO: Check overflow
190 return mp_obj_int_get(self_in);
191}
192
Damien Georgeeabdf672014-03-22 20:54:01 +0000193#if MICROPY_ENABLE_FLOAT
194mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
195 if (MP_OBJ_IS_SMALL_INT(self_in)) {
196 return MP_OBJ_SMALL_INT_VALUE(self_in);
197 } else {
198 mp_obj_int_t *self = self_in;
199 return self->val;
200 }
201}
202#endif
203
Paul Sokolovsky966879c2014-01-17 20:01:36 +0200204#endif