Skip to content

Commit ed61dff

Browse files
committed
Update to latest MicroPython core, v1.4.6-142.
Main change is ability to store floats within a machine word, at the expense of only having 30-bits precision. This patch also enables this feature, since it saves on RAM when using floating point operations.
1 parent 1904e19 commit ed61dff

File tree

17 files changed

+169
-54
lines changed

17 files changed

+169
-54
lines changed

inc/genhdr/mpversion.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This file was generated by py/makeversionhdr.py
2-
#define MICROPY_GIT_TAG "v1.4.6-110-g4de0e7d"
3-
#define MICROPY_GIT_HASH "4de0e7d"
4-
#define MICROPY_BUILD_DATE "2015-10-19"
2+
#define MICROPY_GIT_TAG "v1.4.6-142-g3eb1c89"
3+
#define MICROPY_GIT_HASH "3eb1c89"
4+
#define MICROPY_BUILD_DATE "2015-10-20"
55
#define MICROPY_VERSION_MAJOR (1)
66
#define MICROPY_VERSION_MINOR (4)
77
#define MICROPY_VERSION_MICRO (6)

inc/microbit/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// options to control how Micro Python is built
44

5+
#define MICROPY_OBJ_REPR (MICROPY_OBJ_REPR_C)
56
#define MICROPY_ALLOC_GC_STACK_SIZE (32)
67
#define MICROPY_ALLOC_PATH_MAX (64)
78
#define MICROPY_QSTR_BYTES_IN_HASH (1)

inc/py/mpconfig.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@
6363
// - xxxx...xxx0 : a pointer to an mp_obj_base_t (unless a fake object)
6464
#define MICROPY_OBJ_REPR_B (1)
6565

66+
// A MicroPython object is a machine word having the following form:
67+
// - iiiiiiii iiiiiiii iiiiiiii iiiiiii1 small int with 31-bit signed value
68+
// - x1111111 1qqqqqqq qqqqqqqq qqqqq110 str with 20-bit qstr value
69+
// - s1111111 10000000 00000000 00000010 +/- inf
70+
// - s1111111 1xxxxxxx xxxxxxxx xxxxx010 nan, x != 0
71+
// - seeeeeee efffffff ffffffff ffffff10 30-bit fp, e != 0xff
72+
// - pppppppp pppppppp pppppppp pppppp00 ptr (4 byte alignment)
73+
// This scheme only works with 32-bit word size and float enabled.
74+
#define MICROPY_OBJ_REPR_C (2)
75+
6676
#ifndef MICROPY_OBJ_REPR
6777
#define MICROPY_OBJ_REPR (MICROPY_OBJ_REPR_A)
6878
#endif

inc/py/obj.h

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
8383
#define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 2)
8484
#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 2) | 2))
8585

86+
#if MICROPY_PY_BUILTINS_FLOAT
87+
#define mp_const_float_e ((mp_obj_t)&mp_const_float_e_obj)
88+
#define mp_const_float_pi ((mp_obj_t)&mp_const_float_pi_obj)
89+
extern const struct _mp_obj_float_t mp_const_float_e_obj;
90+
extern const struct _mp_obj_float_t mp_const_float_pi_obj;
91+
92+
#define mp_obj_is_float(o) MP_OBJ_IS_TYPE((o), &mp_type_float)
93+
mp_float_t mp_obj_float_get(mp_obj_t self_in);
94+
mp_obj_t mp_obj_new_float(mp_float_t value);
95+
#endif
96+
8697
static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
8798
{ return ((((mp_int_t)(o)) & 3) == 0); }
8899

@@ -98,9 +109,55 @@ static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
98109
#define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 2)
99110
#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 2) | 3))
100111

112+
#if MICROPY_PY_BUILTINS_FLOAT
113+
#define mp_const_float_e ((mp_obj_t)&mp_const_float_e_obj)
114+
#define mp_const_float_pi ((mp_obj_t)&mp_const_float_pi_obj)
115+
extern const struct _mp_obj_float_t mp_const_float_e_obj;
116+
extern const struct _mp_obj_float_t mp_const_float_pi_obj;
117+
118+
#define mp_obj_is_float(o) MP_OBJ_IS_TYPE((o), &mp_type_float)
119+
mp_float_t mp_obj_float_get(mp_obj_t self_in);
120+
mp_obj_t mp_obj_new_float(mp_float_t value);
121+
#endif
122+
101123
static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
102124
{ return ((((mp_int_t)(o)) & 1) == 0); }
103125

126+
#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
127+
128+
static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o)
129+
{ return ((((mp_int_t)(o)) & 1) != 0); }
130+
#define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1)
131+
#define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_int_t)(small_int)) << 1) | 1))
132+
133+
#define mp_const_float_e ((mp_obj_t)((0x402df854 & ~3) | 2))
134+
#define mp_const_float_pi ((mp_obj_t)((0x40490fdb & ~3) | 2))
135+
136+
static inline bool mp_obj_is_float(mp_const_obj_t o)
137+
{ return (((mp_uint_t)(o)) & 3) == 2 && (((mp_uint_t)(o)) & 0x7f800004) != 0x7f800004; }
138+
static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) {
139+
union {
140+
mp_float_t f;
141+
mp_uint_t u;
142+
} num = {.u = (mp_uint_t)o & ~3};
143+
return num.f;
144+
}
145+
static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
146+
union {
147+
mp_float_t f;
148+
mp_uint_t u;
149+
} num = {.f = f};
150+
return (mp_obj_t)((num.u & ~0x3) | 2);
151+
}
152+
153+
static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
154+
{ return (((mp_uint_t)(o)) & 0x7f800007) == 0x7f800006; }
155+
#define MP_OBJ_QSTR_VALUE(o) ((((mp_uint_t)(o)) >> 3) & 0xfffff)
156+
#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 3) | 0x7f800006))
157+
158+
static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
159+
{ return ((((mp_int_t)(o)) & 3) == 0); }
160+
104161
#endif
105162

106163
// Macros to convert between mp_obj_t and concrete object types.
@@ -473,7 +530,6 @@ mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items);
473530
mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items);
474531
#if MICROPY_PY_BUILTINS_FLOAT
475532
mp_obj_t mp_obj_new_int_from_float(mp_float_t val);
476-
mp_obj_t mp_obj_new_float(mp_float_t val);
477533
mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
478534
#endif
479535
mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type);
@@ -564,11 +620,6 @@ void mp_str_print_quoted(const mp_print_t *print, const byte *str_data, mp_uint_
564620

565621
#if MICROPY_PY_BUILTINS_FLOAT
566622
// float
567-
typedef struct _mp_obj_float_t {
568-
mp_obj_base_t base;
569-
mp_float_t value;
570-
} mp_obj_float_t;
571-
mp_float_t mp_obj_float_get(mp_obj_t self_in);
572623
mp_obj_t mp_obj_float_binary_op(mp_uint_t op, mp_float_t lhs_val, mp_obj_t rhs); // can return MP_OBJ_NULL if op not supported
573624

574625
// complex

inc/py/objstr.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,16 @@ typedef struct _mp_obj_str_t {
4949
{ str_len = qstr_len(MP_OBJ_QSTR_VALUE(str_obj_in)); } else { str_len = ((mp_obj_str_t*)str_obj_in)->len; }
5050

5151
// use this macro to extract the string data and length
52+
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
53+
const byte *mp_obj_str_get_data_no_check(mp_obj_t self_in, mp_uint_t *len);
54+
#define GET_STR_DATA_LEN(str_obj_in, str_data, str_len) \
55+
mp_uint_t str_len; const byte *str_data = mp_obj_str_get_data_no_check(str_obj_in, &str_len);
56+
#else
5257
#define GET_STR_DATA_LEN(str_obj_in, str_data, str_len) \
5358
const byte *str_data; mp_uint_t str_len; if (MP_OBJ_IS_QSTR(str_obj_in)) \
5459
{ str_data = qstr_data(MP_OBJ_QSTR_VALUE(str_obj_in), &str_len); } \
5560
else { str_len = ((mp_obj_str_t*)str_obj_in)->len; str_data = ((mp_obj_str_t*)str_obj_in)->data; }
61+
#endif
5662

5763
mp_obj_t mp_obj_str_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args);
5864
void mp_str_print_json(const mp_print_t *print, const byte *str_data, mp_uint_t str_len);

inc/py/smallint.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,19 @@
3232
// Functions for small integer arithmetic
3333

3434
// In SMALL_INT, next-to-highest bits is used as sign, so both must match for value in range
35-
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A
35+
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
3636

3737
#define MP_SMALL_INT_MIN ((mp_int_t)(((mp_int_t)WORD_MSBIT_HIGH) >> 1))
3838
#define MP_SMALL_INT_FITS(n) ((((n) ^ ((n) << 1)) & WORD_MSBIT_HIGH) == 0)
39+
// Mask to truncate mp_int_t to positive value
40+
#define MP_SMALL_INT_POSITIVE_MASK ~(WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))
3941

4042
#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B
4143

4244
#define MP_SMALL_INT_MIN ((mp_int_t)(((mp_int_t)WORD_MSBIT_HIGH) >> 2))
4345
#define MP_SMALL_INT_FITS(n) ((((n) & MP_SMALL_INT_MIN) == 0) || (((n) & MP_SMALL_INT_MIN) == MP_SMALL_INT_MIN))
46+
// Mask to truncate mp_int_t to positive value
47+
#define MP_SMALL_INT_POSITIVE_MASK ~(WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1) | (WORD_MSBIT_HIGH >> 2))
4448

4549
#endif
4650

source/lib/readline.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ STATIC void mp_hal_move_cursor_back(uint pos) {
8282
}
8383
}
8484

85-
STATIC void mp_hal_erase_line_from_cursor(uint n_chars) {
85+
STATIC void mp_hal_erase_line_from_cursor(uint n_chars_to_erase) {
86+
(void)n_chars_to_erase;
8687
mp_hal_stdout_tx_strn("\x1b[K", 3);
8788
}
8889
#endif
@@ -338,7 +339,6 @@ int readline_process_char(int c) {
338339
if (redraw_from_cursor) {
339340
if (rl.line->len < last_line_len) {
340341
// erase old chars
341-
// (number of chars to erase: last_line_len - rl.cursor_pos)
342342
mp_hal_erase_line_from_cursor(last_line_len - rl.cursor_pos);
343343
}
344344
// draw new chars

source/py/modbuiltins.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ STATIC mp_obj_t mp_builtin_abs(mp_obj_t o_in) {
9393
if (0) {
9494
// dummy
9595
#if MICROPY_PY_BUILTINS_FLOAT
96-
} else if (MP_OBJ_IS_TYPE(o_in, &mp_type_float)) {
96+
} else if (mp_obj_is_float(o_in)) {
9797
mp_float_t value = mp_obj_float_get(o_in);
9898
// TODO check for NaN etc
9999
if (value < 0) {

source/py/modcmath.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@
3535
/// The `cmath` module provides some basic mathematical funtions for
3636
/// working with complex numbers.
3737

38-
// These are defined in modmath.c
39-
/// \constant e - base of the natural logarithm
40-
extern const mp_obj_float_t mp_math_e_obj;
41-
/// \constant pi - the ratio of a circle's circumference to its diameter
42-
extern const mp_obj_float_t mp_math_pi_obj;
43-
4438
/// \function phase(z)
4539
/// Returns the phase of the number `z`, in the range (-pi, +pi].
4640
STATIC mp_obj_t mp_cmath_phase(mp_obj_t z_obj) {
@@ -132,8 +126,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sin_obj, mp_cmath_sin);
132126

133127
STATIC const mp_map_elem_t mp_module_cmath_globals_table[] = {
134128
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_cmath) },
135-
{ MP_OBJ_NEW_QSTR(MP_QSTR_e), (mp_obj_t)&mp_math_e_obj },
136-
{ MP_OBJ_NEW_QSTR(MP_QSTR_pi), (mp_obj_t)&mp_math_pi_obj },
129+
{ MP_OBJ_NEW_QSTR(MP_QSTR_e), mp_const_float_e },
130+
{ MP_OBJ_NEW_QSTR(MP_QSTR_pi), mp_const_float_pi },
137131
{ MP_OBJ_NEW_QSTR(MP_QSTR_phase), (mp_obj_t)&mp_cmath_phase_obj },
138132
{ MP_OBJ_NEW_QSTR(MP_QSTR_polar), (mp_obj_t)&mp_cmath_polar_obj },
139133
{ MP_OBJ_NEW_QSTR(MP_QSTR_rect), (mp_obj_t)&mp_cmath_rect_obj },

source/py/modmath.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@
5252
STATIC mp_obj_t mp_math_ ## py_name(mp_obj_t x_obj) { mp_int_t x = MICROPY_FLOAT_C_FUN(c_name)(mp_obj_get_float(x_obj)); return mp_obj_new_int(x); } \
5353
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_## py_name ## _obj, mp_math_ ## py_name);
5454

55-
// These are also used by cmath.c
56-
/// \constant e - base of the natural logarithm
57-
const mp_obj_float_t mp_math_e_obj = {{&mp_type_float}, M_E};
58-
/// \constant pi - the ratio of a circle's circumference to its diameter
59-
const mp_obj_float_t mp_math_pi_obj = {{&mp_type_float}, M_PI};
60-
6155
/// \function sqrt(x)
6256
/// Returns the square root of `x`.
6357
MATH_FUN_1(sqrt, sqrt)
@@ -188,8 +182,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_math_degrees_obj, mp_math_degrees);
188182

189183
STATIC const mp_map_elem_t mp_module_math_globals_table[] = {
190184
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_math) },
191-
{ MP_OBJ_NEW_QSTR(MP_QSTR_e), (mp_obj_t)&mp_math_e_obj },
192-
{ MP_OBJ_NEW_QSTR(MP_QSTR_pi), (mp_obj_t)&mp_math_pi_obj },
185+
{ MP_OBJ_NEW_QSTR(MP_QSTR_e), mp_const_float_e },
186+
{ MP_OBJ_NEW_QSTR(MP_QSTR_pi), mp_const_float_pi },
193187
{ MP_OBJ_NEW_QSTR(MP_QSTR_sqrt), (mp_obj_t)&mp_math_sqrt_obj },
194188
{ MP_OBJ_NEW_QSTR(MP_QSTR_pow), (mp_obj_t)&mp_math_pow_obj },
195189
{ MP_OBJ_NEW_QSTR(MP_QSTR_exp), (mp_obj_t)&mp_math_exp_obj },

0 commit comments

Comments
 (0)