blob: 05269ce3793c97e0615c10451a69496c9e1e217f [file] [log] [blame]
Damien George660aef62014-04-02 12:22:07 +01001#include <stdlib.h>
Damien George45b43c22014-01-05 01:50:45 +00002#include <stdint.h>
Damien George45b43c22014-01-05 01:50:45 +00003#include <assert.h>
Dave Hylandsc4029e52014-04-07 11:19:51 -07004#include <string.h>
Damien George45b43c22014-01-05 01:50:45 +00005
6#include "nlr.h"
7#include "misc.h"
8#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00009#include "qstr.h"
Damien George45b43c22014-01-05 01:50:45 +000010#include "obj.h"
Damien George20773972014-02-22 18:12:43 +000011#include "parsenum.h"
Damien George438c88d2014-02-22 19:25:23 +000012#include "mpz.h"
Paul Sokolovsky76a90f22014-01-13 22:31:01 +020013#include "objint.h"
Damien George660aef62014-04-02 12:22:07 +010014#include "runtime0.h"
15#include "runtime.h"
Paul Sokolovsky48b35722014-01-12 17:30:48 +020016
Damien George6433bd92014-03-30 23:13:16 +010017#if MICROPY_ENABLE_FLOAT
18#include <math.h>
19#endif
20
Damien Georgee8208a72014-04-04 15:08:23 +010021// This dispatcher function is expected to be independent of the implementation of long int
22STATIC mp_obj_t mp_obj_int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien George20006db2014-01-18 14:10:48 +000023 // TODO check n_kw == 0
24
Damien George45b43c22014-01-05 01:50:45 +000025 switch (n_args) {
26 case 0:
27 return MP_OBJ_NEW_SMALL_INT(0);
28
29 case 1:
Damien George5fa93b62014-01-22 14:35:10 +000030 if (MP_OBJ_IS_STR(args[0])) {
Damien George5573f9f2014-01-15 22:58:39 +000031 // a string, parse it
Damien George5fa93b62014-01-22 14:35:10 +000032 uint l;
Damien George698ec212014-02-08 18:17:23 +000033 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George20773972014-02-22 18:12:43 +000034 return mp_parse_num_integer(s, l, 0);
Damien George6433bd92014-03-30 23:13:16 +010035#if MICROPY_ENABLE_FLOAT
36 } else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) {
37 return MP_OBJ_NEW_SMALL_INT((machine_int_t)(MICROPY_FLOAT_C_FUN(trunc)(mp_obj_float_get(args[0]))));
38#endif
Damien George5573f9f2014-01-15 22:58:39 +000039 } else {
40 return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
41 }
Damien George45b43c22014-01-05 01:50:45 +000042
xybc178ea42014-01-14 21:39:05 +080043 case 2:
Damien George5fa93b62014-01-22 14:35:10 +000044 {
Damien George5573f9f2014-01-15 22:58:39 +000045 // should be a string, parse it
46 // TODO proper error checking of argument types
Damien George5fa93b62014-01-22 14:35:10 +000047 uint l;
Damien George698ec212014-02-08 18:17:23 +000048 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George20773972014-02-22 18:12:43 +000049 return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]));
Damien George5fa93b62014-01-22 14:35:10 +000050 }
Damien George45b43c22014-01-05 01:50:45 +000051
52 default:
Damien Georgeea13f402014-04-05 18:32:08 +010053 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "int takes at most 2 arguments, %d given", n_args));
Damien George45b43c22014-01-05 01:50:45 +000054 }
55}
56
Damien Georgee8208a72014-04-04 15:08:23 +010057void mp_obj_int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Dave Hylandsc4029e52014-04-07 11:19:51 -070058 // The size of this buffer is rather arbitrary. If it's not large
59 // enough, a dynamic one will be allocated.
60 char stack_buf[sizeof(machine_int_t) * 4];
61 char *buf = stack_buf;
62 int buf_size = sizeof(stack_buf);
63 int fmt_size;
64
65 char *str = mp_obj_int_formatted(&buf, &buf_size, &fmt_size, self_in, 10, NULL, '\0', '\0');
66 print(env, "%s", str);
67
68 if (buf != stack_buf) {
69 m_free(buf, buf_size);
Damien George5fa93b62014-01-22 14:35:10 +000070 }
Damien George45b43c22014-01-05 01:50:45 +000071}
Paul Sokolovsky48b35722014-01-12 17:30:48 +020072
Dave Hylandsc4029e52014-04-07 11:19:51 -070073#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE || MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
74
75#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
76typedef mp_longint_impl_t fmt_int_t;
77#else
78typedef mp_small_int_t fmt_int_t;
79#endif
80
81static const uint log_base2_floor[] = {
82 0,
83 0, 1, 1, 2,
84 2, 2, 2, 3,
85 3, 3, 3, 3,
86 3, 3, 3, 4,
87 4, 4, 4, 4,
88 4, 4, 4, 4,
89 4, 4, 4, 4,
90 4, 4, 4, 5
91};
92
93uint int_as_str_size_formatted(uint base, const char *prefix, char comma) {
94 if (base < 2 || base > 32) {
95 return 0;
96 }
97
98 uint num_digits = sizeof(fmt_int_t) * 8 / log_base2_floor[base] + 1;
99 uint num_commas = comma ? num_digits / 3: 0;
100 uint prefix_len = prefix ? strlen(prefix) : 0;
101 return num_digits + num_commas + prefix_len + 2; // +1 for sign, +1 for null byte
102}
103
104// This routine expects you to pass in a buffer and size (in *buf and buf_size).
105// If, for some reason, this buffer is too small, then it will allocate a
106// buffer and return the allocated buffer and size in *buf and *buf_size. It
107// is the callers responsibility to free this allocated buffer.
108//
109// The resulting formatted string will be returned from this function and the
110// formatted size will be in *fmt_size.
111char *mp_obj_int_formatted(char **buf, int *buf_size, int *fmt_size, mp_obj_t self_in,
112 int base, const char *prefix, char base_char, char comma) {
113 if (!MP_OBJ_IS_INT(self_in)) {
114 buf[0] = '\0';
115 *fmt_size = 0;
116 return *buf;
117 }
Dave Hylands23dc6d02014-04-07 13:44:12 -0700118 fmt_int_t num;
119#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
120 mp_obj_int_t *self = self_in;
121 if (MP_OBJ_IS_TYPE(self_in, &mp_type_int)) {
122 // mp_obj_get_int truncates to machine_int_t
123 num = self->val;
124 } else
125#endif
126 {
127 num = mp_obj_get_int(self_in);
128 }
Dave Hylandsc4029e52014-04-07 11:19:51 -0700129 char sign = '\0';
130 if (num < 0) {
131 num = -num;
132 sign = '-';
133 }
134
135 uint needed_size = int_as_str_size_formatted(base, prefix, comma);
136 if (needed_size > *buf_size) {
137 *buf = m_new(char, needed_size);
138 *buf_size = needed_size;
139 }
140 char *str = *buf;
141
142 char *b = str + needed_size;
143 *(--b) = '\0';
144 char *last_comma = b;
145
146 if (num == 0) {
147 *(--b) = '0';
148 } else {
149 do {
150 int c = num % base;
151 num /= base;
152 if (c >= 10) {
153 c += base_char - 10;
154 } else {
155 c += '0';
156 }
157 *(--b) = c;
158 if (comma && num != 0 && b > str && (last_comma - b) == 3) {
159 *(--b) = comma;
160 last_comma = b;
161 }
162 }
163 while (b > str && num != 0);
164 }
165 if (prefix) {
166 size_t prefix_len = strlen(prefix);
167 char *p = b - prefix_len;
168 if (p > str) {
169 b = p;
170 while (*prefix) {
171 *p++ = *prefix++;
172 }
173 }
174 }
175 if (sign && b > str) {
176 *(--b) = sign;
177 }
178 *fmt_size = *buf + needed_size - b - 1;
179
180 return b;
181}
182
183bool mp_obj_int_is_positive(mp_obj_t self_in) {
184 return mp_obj_get_int(self_in) >= 0;
185}
186#endif // LONGLONG or NONE
187
188#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
189
Damien George660aef62014-04-02 12:22:07 +0100190// This is called for operations on SMALL_INT that are not handled by mp_unary_op
Damien Georgee8208a72014-04-04 15:08:23 +0100191mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
Damien George660aef62014-04-02 12:22:07 +0100192 return MP_OBJ_NULL;
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +0200193}
194
Damien George660aef62014-04-02 12:22:07 +0100195// This is called for operations on SMALL_INT that are not handled by mp_binary_op
Damien Georgee8208a72014-04-04 15:08:23 +0100196mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
197 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200198}
199
200// This is called only with strings whose value doesn't fit in SMALL_INT
201mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
Damien Georgeea13f402014-04-05 18:32:08 +0100202 nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "long int not supported in this build"));
Damien George23005372014-01-13 19:39:01 +0000203 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200204}
205
Damien George9d68e9c2014-03-12 15:38:15 +0000206// This is called when an integer larger than a SMALL_INT is needed (although val might still fit in a SMALL_INT)
207mp_obj_t mp_obj_new_int_from_ll(long long val) {
Damien Georgeea13f402014-04-05 18:32:08 +0100208 nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
Damien George9d68e9c2014-03-12 15:38:15 +0000209 return mp_const_none;
210}
211
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200212mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
213 // SMALL_INT accepts only signed numbers, of one bit less size
214 // then word size, which totals 2 bits less for unsigned numbers.
215 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
216 return MP_OBJ_NEW_SMALL_INT(value);
217 }
Damien Georgeea13f402014-04-05 18:32:08 +0100218 nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
Damien George23005372014-01-13 19:39:01 +0000219 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200220}
221
222mp_obj_t mp_obj_new_int(machine_int_t value) {
223 if (MP_OBJ_FITS_SMALL_INT(value)) {
224 return MP_OBJ_NEW_SMALL_INT(value);
225 }
Damien Georgeea13f402014-04-05 18:32:08 +0100226 nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
Damien George23005372014-01-13 19:39:01 +0000227 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200228}
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200229
230machine_int_t mp_obj_int_get(mp_obj_t self_in) {
231 return MP_OBJ_SMALL_INT_VALUE(self_in);
232}
233
234machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
235 return MP_OBJ_SMALL_INT_VALUE(self_in);
236}
237
Damien Georgeeabdf672014-03-22 20:54:01 +0000238#if MICROPY_ENABLE_FLOAT
239mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
240 return MP_OBJ_SMALL_INT_VALUE(self_in);
241}
242#endif
243
Damien George5fa93b62014-01-22 14:35:10 +0000244#endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
245
Damien Georgee8208a72014-04-04 15:08:23 +0100246// This dispatcher function is expected to be independent of the implementation of long int
247// It handles the extra cases for integer-like arithmetic
248mp_obj_t mp_obj_int_binary_op_extra_cases(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
249 if (rhs_in == mp_const_false) {
250 // false acts as 0
251 return mp_binary_op(op, lhs_in, MP_OBJ_NEW_SMALL_INT(0));
252 } else if (rhs_in == mp_const_true) {
253 // true acts as 0
254 return mp_binary_op(op, lhs_in, MP_OBJ_NEW_SMALL_INT(1));
255 } else if (op == MP_BINARY_OP_MULTIPLY) {
256 if (MP_OBJ_IS_STR(rhs_in) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_list)) {
257 // multiply is commutative for these types, so delegate to them
258 return mp_binary_op(op, rhs_in, lhs_in);
259 }
260 }
261 return MP_OBJ_NULL;
262}
263
Paul Sokolovskya985b452014-04-09 00:40:58 +0300264STATIC mp_obj_t int_from_bytes(uint n_args, const mp_obj_t *args) {
265 buffer_info_t bufinfo;
266 mp_get_buffer_raise(args[0], &bufinfo);
267
268 assert(bufinfo.len >= sizeof(machine_int_t));
269 // TODO: Support long ints
270 // TODO: Support byteorder param
271 // TODO: Support signed param
272 return mp_obj_new_int_from_uint(*(machine_uint_t*)bufinfo.buf);
273}
274
275STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_obj, 1, 3, int_from_bytes);
276
277STATIC mp_obj_t int_to_bytes(uint n_args, const mp_obj_t *args) {
278 machine_int_t val = mp_obj_int_get_checked(args[0]);
279
280 uint len = MP_OBJ_SMALL_INT_VALUE(args[1]);
281 byte *data;
282
283 // TODO: Support long ints
284 // TODO: Support byteorder param
285 // TODO: Support signed param
286 mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, len, &data);
287 memset(data, 0, len);
288 memcpy(data, &val, len < sizeof(machine_int_t) ? len : sizeof(machine_int_t));
289 return mp_obj_str_builder_end(o);
290}
291
292STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_to_bytes_obj, 2, 4, int_to_bytes);
293
294STATIC const mp_map_elem_t int_locals_dict_table[] = {
295 { MP_OBJ_NEW_QSTR(MP_QSTR_from_bytes), (mp_obj_t)&int_from_bytes_obj },
296 { MP_OBJ_NEW_QSTR(MP_QSTR_to_bytes), (mp_obj_t)&int_to_bytes_obj },
297};
298
299STATIC MP_DEFINE_CONST_DICT(int_locals_dict, int_locals_dict_table);
300
Damien George3e1a5c12014-03-29 13:43:38 +0000301const mp_obj_type_t mp_type_int = {
Damien Georgec5966122014-02-15 16:10:44 +0000302 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000303 .name = MP_QSTR_int,
Damien Georgee8208a72014-04-04 15:08:23 +0100304 .print = mp_obj_int_print,
305 .make_new = mp_obj_int_make_new,
306 .unary_op = mp_obj_int_unary_op,
307 .binary_op = mp_obj_int_binary_op,
Paul Sokolovskya985b452014-04-09 00:40:58 +0300308 .locals_dict = (mp_obj_t)&int_locals_dict,
Damien George5fa93b62014-01-22 14:35:10 +0000309};