objint.c 3.84 KB
Newer Older
Damien George's avatar
Damien George committed
1
2
3
4
5
6
#include <stdint.h>
#include <assert.h>

#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
7
#include "qstr.h"
Damien George's avatar
Damien George committed
8
#include "obj.h"
9
#include "parsenum.h"
10
#include "mpz.h"
11
#include "objint.h"
12

13
14
15
16
#if MICROPY_ENABLE_FLOAT
#include <math.h>
#endif

17
18
// This dispatcher function is expected to be independent of the implementation
// of long int
19
STATIC mp_obj_t int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
20
21
    // TODO check n_kw == 0

Damien George's avatar
Damien George committed
22
23
24
25
26
    switch (n_args) {
        case 0:
            return MP_OBJ_NEW_SMALL_INT(0);

        case 1:
27
            if (MP_OBJ_IS_STR(args[0])) {
28
                // a string, parse it
29
                uint l;
30
                const char *s = mp_obj_str_get_data(args[0], &l);
31
                return mp_parse_num_integer(s, l, 0);
32
33
34
35
#if MICROPY_ENABLE_FLOAT
            } else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) {
                return MP_OBJ_NEW_SMALL_INT((machine_int_t)(MICROPY_FLOAT_C_FUN(trunc)(mp_obj_float_get(args[0]))));
#endif
36
37
38
            } else {
                return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
            }
Damien George's avatar
Damien George committed
39

xyb's avatar
xyb committed
40
        case 2:
41
        {
42
43
            // should be a string, parse it
            // TODO proper error checking of argument types
44
            uint l;
45
            const char *s = mp_obj_str_get_data(args[0], &l);
46
            return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]));
47
        }
Damien George's avatar
Damien George committed
48
49

        default:
50
            nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "int takes at most 2 arguments, %d given", n_args));
Damien George's avatar
Damien George committed
51
52
53
    }
}

54
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
55

56
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
57
    if (MP_OBJ_IS_SMALL_INT(self_in)) {
58
        print(env, INT_FMT, MP_OBJ_SMALL_INT_VALUE(self_in));
59
    }
60
61
}

62
63
64
65
66
67
// This is called only for non-SMALL_INT
mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
    assert(0);
    return mp_const_none;
}

68
69
70
// This is called only for non-SMALL_INT
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
    assert(0);
71
    return mp_const_none;
72
73
74
75
}

// This is called only with strings whose value doesn't fit in SMALL_INT
mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
76
    nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "long int not supported in this build"));
77
    return mp_const_none;
78
79
}

80
81
82
83
84
85
// This is called when an integer larger than a SMALL_INT is needed (although val might still fit in a SMALL_INT)
mp_obj_t mp_obj_new_int_from_ll(long long val) {
    nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
    return mp_const_none;
}

86
87
88
89
90
91
mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
    // SMALL_INT accepts only signed numbers, of one bit less size
    // then word size, which totals 2 bits less for unsigned numbers.
    if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) {
        return MP_OBJ_NEW_SMALL_INT(value);
    }
92
    nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
93
    return mp_const_none;
94
95
}

Damien George's avatar
Damien George committed
96
mp_obj_t mp_obj_new_int(machine_int_t value) {
97
98
99
    if (MP_OBJ_FITS_SMALL_INT(value)) {
        return MP_OBJ_NEW_SMALL_INT(value);
    }
100
    nlr_jump(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
101
    return mp_const_none;
Damien George's avatar
Damien George committed
102
}
103
104
105
106
107
108
109
110
111

machine_int_t mp_obj_int_get(mp_obj_t self_in) {
    return MP_OBJ_SMALL_INT_VALUE(self_in);
}

machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
    return MP_OBJ_SMALL_INT_VALUE(self_in);
}

112
113
114
115
116
117
#if MICROPY_ENABLE_FLOAT
mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
    return MP_OBJ_SMALL_INT_VALUE(self_in);
}
#endif

118
119
#endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE

120
const mp_obj_type_t mp_type_int = {
121
    { &mp_type_type },
122
    .name = MP_QSTR_int,
123
124
    .print = int_print,
    .make_new = int_make_new,
125
    .unary_op = int_unary_op,
126
127
    .binary_op = int_binary_op,
};