8000 Merge pull request #160 from pfalcon/elaborate-int · ronc/micropython@ec3e14e · GitHub
[go: up one dir, main page]

Skip to content

Commit ec3e14e

Browse files
committed
Merge pull request micropython#160 from pfalcon/elaborate-int
Elaborate small-int/long-int
2 parents 45eb6ea + 48b3572 commit ec3e14e

File tree

7 files changed

+105
-23
lines changed

7 files changed

+105
-23
lines changed

py/gc.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,8 @@
88

99
#if MICROPY_ENABLE_GC
1010

11-
// a machine word is big enough to hold a pointer
12-
/*
13-
#define BYTES_PER_WORD (8)
14-
typedef unsigned long machine_uint_t;
15-
*/
1611
typedef unsigned char byte;
1712

18-
#define BITS_PER_BYTE (8)
19-
#define BITS_PER_WORD (BITS_PER_BYTE * BYTES_PER_WORD)
2013
#define WORDS_PER_BLOCK (4)
2114
#define BYTES_PER_BLOCK (WORDS_PER_BLOCK * BYTES_PER_WORD)
2215
#define STACK_SIZE (64) // tunable; minimum is 1

py/mpconfig.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@
6262
#define MICROPY_ENABLE_LEXER_UNIX (0)
6363
#endif
6464

65+
// Long int implementation
66+
#define MICROPY_LONGINT_IMPL_NONE (0)
67+
#define MICROPY_LONGINT_IMPL_LONGLONG (1)
68+
69+
#ifndef MICROPY_LONGINT_IMPL
70+
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE)
71+
#endif
72+
73+
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
74+
typedef long long mp_longint_impl_t;
75+
#endif
76+
6577
// Whether to support float and complex types
6678
#ifndef MICROPY_ENABLE_FLOAT
6779
#define MICROPY_ENABLE_FLOAT (0)
@@ -76,6 +88,11 @@
7688
/*****************************************************************************/
7789
/* Miscellaneous settings */
7890

91+
#define BITS_PER_BYTE (8)
92+
#define BITS_PER_WORD (BITS_PER_BYTE * BYTES_PER_WORD)
93+
// machine_int_t value with most significant bit set
94+
#define WORD_MSBIT_HIGH (1 << (BYTES_PER_WORD * 8 - 1))
95+
7996
// printf format spec to use for machine_int_t and friends
8097
#ifndef INT_FMT
8198
#ifdef __LP64__

py/obj.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ typedef struct _mp_obj_base_t mp_obj_base_t;
3434
// - xxxx...xx10: a qstr, bits 2 and above are the value
3535
// - xxxx...xx00: a pointer to an mp_obj_base_t
3636

37+
// In SMALL_INT, next-to-highest bits is used as sign, so both must match for value in range
38+
#define MP_OBJ_FITS_SMALL_INT(n) ((((n) ^ ((n) << 1)) & WORD_MSBIT_HIGH) == 0)
3739
#define MP_OBJ_IS_SMALL_INT(o) ((((mp_small_int_t)(o)) & 1) != 0)
3840
#define MP_OBJ_IS_QSTR(o) ((((mp_small_int_t)(o)) & 3) == 2)
3941
#define MP_OBJ_IS_OBJ(o) ((((mp_small_int_t)(o)) & 3) == 0)
@@ -196,6 +198,8 @@ mp_obj_t mp_obj_new_none(void);
196198
mp_obj_t mp_obj_new_bool(bool value);
197199
mp_obj_t mp_obj_new_cell(mp_obj_t obj);
198200
mp_obj_t mp_obj_new_int(machine_int_t value);
201+
mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value);
202+
mp_obj_t mp_obj_new_int_from_long_str(const char *s);
199203
mp_obj_t mp_obj_new_str(qstr qstr);
200204
#if MICROPY_ENABLE_FLOAT
201205
mp_obj_t mp_obj_new_float(mp_float_t val);

py/objint.c

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,24 @@
1111

1212
typedef struct _mp_obj_int_t {
1313
mp_obj_base_t base;
14+
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
15+
mp_longint_impl_t val;
16+
#endif
1417
} mp_obj_int_t;
1518

19+
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in);
20+
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);
21+
22+
// This dispatcher function is expected to be independent of the implementation
23+
// of long int
1624
static mp_obj_t int_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
1725
switch (n_args) {
1826
case 0:
1927
return MP_OBJ_NEW_SMALL_INT(0);
2028

2129
case 1:
2230
// TODO allow string as arg and parse it
23-
return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
31+
return mp_obj_new_int(mp_obj_get_int(args[0]));
2432

2533
//case 2:
2634
// TODO, parse with given base
@@ -33,9 +41,41 @@ static mp_obj_t int_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args)
3341
const mp_obj_type_t int_type = {
3442
{ &mp_const_type },
3543
"int",
44+
.print = int_print,
3645
.make_new = int_make_new,
46+
.binary_op = int_binary_op,
3747
};
3848

49+
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
50+
// This is called only for non-SMALL_INT
51+
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
52+
}
53+
54+
// This is called only for non-SMALL_INT
55+
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
56+
assert(0);
57+
}
58+
59+
// This is called only with strings whose value doesn't fit in SMALL_INT
60+
mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
61+
assert(0);
62+
}
63+
64+
mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
65+
// SMALL_INT accepts only signed numbers, of one bit less size
66+
// then word size, which totals 2 bits less for unsigned numbers.
67+
if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
68+
return MP_OBJ_NEW_SMALL_INT(value);
69+
}
70+
// TODO: Raise exception
71+
assert(0);
72+
}
73+
3974
mp_obj_t mp_obj_new_int(machine_int_t value) {
40-
return MP_OBJ_NEW_SMALL_INT(value);
75+
if (MP_OBJ_FITS_SMALL_INT(value)) {
76+
return MP_OBJ_NEW_SMALL_INT(value);
77+
}
78+
// TODO: Raise exception
79+
assert(0);
4180
}
81+
#endif

py/parse.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ static void push_result_token(parser_t *parser, const mp_lexer_t *lex) {
196196
} else if (tok->kind == MP_TOKEN_NUMBER) {
197197
bool dec = false;
198198
bool small_int = true;
199-
int int_val = 0;
199+
machine_int_t int_val = 0;
200200
int len = tok->len;
201201
const char *str = tok->str;
202202
int base = 10;
@@ -216,7 +216,9 @@ static void push_result_token(parser_t *parser, const mp_lexer_t *lex) {
216216
i = 2;
217217
}
218218
}
219+
bool overflow = false;
219220
for (; i < len; i++) {
221+
machine_int_t old_val = int_val;
220222
if (unichar_isdigit(str[i]) && str[i] - '0' < base) {
221223
int_val = base * int_val + str[i] - '0';
222224
} else if (base == 16 && 'a' <= str[i] && str[i] <= 'f') {
@@ -230,10 +232,17 @@ static void push_result_token(parser_t *parser, const mp_lexer_t *lex) {
230232
small_int = false;
231233
break;
232234
}
235+
if (int_val < old_val) {
236+
// If new value became less than previous, it's overflow
237+
overflow = true;
238+
} else if ((old_val ^ int_val) & WORD_MSBIT_HIGH) {
239+
// If signed number changed sign - it's overflow
240+
overflow = true;
241+
}
233242
}
234243
if (dec) {
235244
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_DECIMAL, qstr_from_strn_copy(str, len));
236-
} else if (small_int && MP_FIT_SMALL_INT(int_val)) {
245+
} else if (small_int && !overflow && MP_FIT_SMALL_INT(int_val)) {
237246
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, int_val);
238247
} else {
239248
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_INTEGER, qstr_from_strn_copy(str, len));

py/runtime.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,6 @@ void rt_assign_inline_asm_code(int unique_code_id, void *fun, uint len, int n_ar
268268
#endif
269269
}
270270

271-
static bool fit_small_int(mp_small_int_t o) {
272-
return true;
273-
}
274-
275271
int rt_is_true(mp_obj_t arg) {
276272
DEBUG_OP_printf("is true %p\n", arg);
277273
if (MP_OBJ_IS_SMALL_INT(arg)) {
@@ -436,13 +432,10 @@ mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
436432
case RT_UNARY_OP_INVERT: val = ~val; break;
437433
default: assert(0); val = 0;
438434
}
439-
if (fit_small_int(val)) {
435+
if (MP_OBJ_FITS_SMALL_INT(val)) {
440436
return MP_OBJ_NEW_SMALL_INT(val);
441-
} else {
442-
// TODO make a bignum
443-
assert(0);
444-
return mp_const_none;
445437
}
438+
return mp_obj_new_int(val);
446439
} else { // will be an object (small ints are caught in previous if)
447440
mp_obj_base_t *o = arg;
448441
if (o->type->unary_op != NULL) {
@@ -551,11 +544,11 @@ mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
551544

552545
default: assert(0);
553546
}
554-
if (fit_small_int(lhs_val)) {
547+
// TODO: We just should make mp_obj_new_int() inline and use that
548+
if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
555549
return MP_OBJ_NEW_SMALL_INT(lhs_val);
556550
}
557-
// TODO: return long int
558-
assert(0);
551+
return mp_obj_new_int(lhs_val);
559552
} else if (MP_OBJ_IS_TYPE(rhs, &float_type)) {
560553
return mp_obj_float_binary_op(op, lhs_val, rhs);
561554
} else if (MP_OBJ_IS_TYPE(rhs, &complex_type)) {

tests/basics/tests/int-small.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This test small int range for 32-bit machine
2+
3+
a = 0x3fffff
4+
print(a)
5+
a *= 0x10
6+
print(a)
7+
a *= 0x10
8+
print(a)
9+
a += 0xff
10+
print(a)
11+
# This would overflow
12+
#a += 1
13+
14+
a = -0x3fffff
15+
print(a)
16+
a *= 0x10
17+
print(a)
18+
a *= 0x10
19+
print(a)
20+
a -= 0xff
21+
print(a)
22+
# This still doesn't overflow
23+
a -= 1
24+
print(a)
25+
# This would overflow
26+
#a -= 1

0 commit comments

Comments
 (0)
0