blob: 1ea6a4f647c2d7c48310f551e09f27af37c07619 [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>
4
5#include "nlr.h"
6#include "misc.h"
7#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00008#include "qstr.h"
Damien George45b43c22014-01-05 01:50:45 +00009#include "obj.h"
Damien George20773972014-02-22 18:12:43 +000010#include "parsenum.h"
Damien George438c88d2014-02-22 19:25:23 +000011#include "mpz.h"
Paul Sokolovsky76a90f22014-01-13 22:31:01 +020012#include "objint.h"
Damien George660aef62014-04-02 12:22:07 +010013#include "runtime0.h"
14#include "runtime.h"
Paul Sokolovsky48b35722014-01-12 17:30:48 +020015
Damien George6433bd92014-03-30 23:13:16 +010016#if MICROPY_ENABLE_FLOAT
17#include <math.h>
18#endif
19
Damien Georgee8208a72014-04-04 15:08:23 +010020// This dispatcher function is expected to be independent of the implementation of long int
21STATIC 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 +000022 // TODO check n_kw == 0
23
Damien George45b43c22014-01-05 01:50:45 +000024 switch (n_args) {
25 case 0:
26 return MP_OBJ_NEW_SMALL_INT(0);
27
28 case 1:
Damien George5fa93b62014-01-22 14:35:10 +000029 if (MP_OBJ_IS_STR(args[0])) {
Damien George5573f9f2014-01-15 22:58:39 +000030 // a string, parse it
Damien George5fa93b62014-01-22 14:35:10 +000031 uint l;
Damien George698ec212014-02-08 18:17:23 +000032 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George20773972014-02-22 18:12:43 +000033 return mp_parse_num_integer(s, l, 0);
Damien George6433bd92014-03-30 23:13:16 +010034#if MICROPY_ENABLE_FLOAT
35 } else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) {
36 return MP_OBJ_NEW_SMALL_INT((machine_int_t)(MICROPY_FLOAT_C_FUN(trunc)(mp_obj_float_get(args[0]))));
37#endif
Damien George5573f9f2014-01-15 22:58:39 +000038 } else {
39 return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
40 }
Damien George45b43c22014-01-05 01:50:45 +000041
xybc178ea42014-01-14 21:39:05 +080042 case 2:
Damien George5fa93b62014-01-22 14:35:10 +000043 {
Damien George5573f9f2014-01-15 22:58:39 +000044 // should be a string, parse it
45 // TODO proper error checking of argument types
Damien George5fa93b62014-01-22 14:35:10 +000046 uint l;
Damien George698ec212014-02-08 18:17:23 +000047 const char *s = mp_obj_str_get_data(args[0], &l);
Damien George20773972014-02-22 18:12:43 +000048 return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]));
Damien George5fa93b62014-01-22 14:35:10 +000049 }
Damien George45b43c22014-01-05 01:50:45 +000050
51 default:
Damien Georgec5966122014-02-15 16:10:44 +000052 nlr_jump(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 +000053 }
54}
55
Paul Sokolovsky48b35722014-01-12 17:30:48 +020056#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
Damien George5fa93b62014-01-22 14:35:10 +000057
Damien Georgee8208a72014-04-04 15:08:23 +010058void mp_obj_int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Damien George5fa93b62014-01-22 14:35:10 +000059 if (MP_OBJ_IS_SMALL_INT(self_in)) {
Damien George0379b552014-02-22 17:34:09 +000060 print(env, INT_FMT, MP_OBJ_SMALL_INT_VALUE(self_in));
Damien George5fa93b62014-01-22 14:35:10 +000061 }
Damien George45b43c22014-01-05 01:50:45 +000062}
Paul Sokolovsky48b35722014-01-12 17:30:48 +020063
Damien George660aef62014-04-02 12:22:07 +010064// This is called for operations on SMALL_INT that are not handled by mp_unary_op
Damien Georgee8208a72014-04-04 15:08:23 +010065mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
Damien George660aef62014-04-02 12:22:07 +010066 return MP_OBJ_NULL;
Paul Sokolovsky9b00dad2014-01-27 09:05:50 +020067}
68
Damien George660aef62014-04-02 12:22:07 +010069// This is called for operations on SMALL_INT that are not handled by mp_binary_op
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) {
71 return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
Paul Sokolovsky48b35722014-01-12 17:30:48 +020072}
73
74// This is called only with strings whose value doesn't fit in SMALL_INT
75mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
Damien Georgec5966122014-02-15 16:10:44 +000076 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "long int not supported in this build"));
Damien George23005372014-01-13 19:39:01 +000077 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020078}
79
Damien George9d68e9c2014-03-12 15:38:15 +000080// This is called when an integer larger than a SMALL_INT is needed (although val might still fit in a SMALL_INT)
81mp_obj_t mp_obj_new_int_from_ll(long long val) {
82 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
83 return mp_const_none;
84}
85
Paul Sokolovsky48b35722014-01-12 17:30:48 +020086mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
87 // SMALL_INT accepts only signed numbers, of one bit less size
88 // then word size, which totals 2 bits less for unsigned numbers.
89 if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
90 return MP_OBJ_NEW_SMALL_INT(value);
91 }
Damien Georgec5966122014-02-15 16:10:44 +000092 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
Damien George23005372014-01-13 19:39:01 +000093 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +020094}
95
96mp_obj_t mp_obj_new_int(machine_int_t value) {
97 if (MP_OBJ_FITS_SMALL_INT(value)) {
98 return MP_OBJ_NEW_SMALL_INT(value);
99 }
Damien Georgec5966122014-02-15 16:10:44 +0000100 nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
Damien George23005372014-01-13 19:39:01 +0000101 return mp_const_none;
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200102}
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200103
104machine_int_t mp_obj_int_get(mp_obj_t self_in) {
105 return MP_OBJ_SMALL_INT_VALUE(self_in);
106}
107
108machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
109 return MP_OBJ_SMALL_INT_VALUE(self_in);
110}
111
Damien Georgeeabdf672014-03-22 20:54:01 +0000112#if MICROPY_ENABLE_FLOAT
113mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
114 return MP_OBJ_SMALL_INT_VALUE(self_in);
115}
116#endif
117
Damien George5fa93b62014-01-22 14:35:10 +0000118#endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
119
Damien Georgee8208a72014-04-04 15:08:23 +0100120// This dispatcher function is expected to be independent of the implementation of long int
121// It handles the extra cases for integer-like arithmetic
122mp_obj_t mp_obj_int_binary_op_extra_cases(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
123 if (rhs_in == mp_const_false) {
124 // false acts as 0
125 return mp_binary_op(op, lhs_in, MP_OBJ_NEW_SMALL_INT(0));
126 } else if (rhs_in == mp_const_true) {
127 // true acts as 0
128 return mp_binary_op(op, lhs_in, MP_OBJ_NEW_SMALL_INT(1));
129 } else if (op == MP_BINARY_OP_MULTIPLY) {
130 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)) {
131 // multiply is commutative for these types, so delegate to them
132 return mp_binary_op(op, rhs_in, lhs_in);
133 }
134 }
135 return MP_OBJ_NULL;
136}
137
Damien George3e1a5c12014-03-29 13:43:38 +0000138const mp_obj_type_t mp_type_int = {
Damien Georgec5966122014-02-15 16:10:44 +0000139 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000140 .name = MP_QSTR_int,
Damien Georgee8208a72014-04-04 15:08:23 +0100141 .print = mp_obj_int_print,
142 .make_new = mp_obj_int_make_new,
143 .unary_op = mp_obj_int_unary_op,
144 .binary_op = mp_obj_int_binary_op,
Damien George5fa93b62014-01-22 14:35:10 +0000145};