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

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

16
17
18
19
#if MICROPY_ENABLE_FLOAT
#include <math.h>
#endif

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

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

        case 1:
29
            if (MP_OBJ_IS_STR(args[0])) {
30
                // a string, parse it
31
                uint l;
32
                const char *s = mp_obj_str_get_data(args[0], &l);
33
                return mp_parse_num_integer(s, l, 0);
34
35
36
37
#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
38
39
40
            } else {
                return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
            }
Damien George's avatar
Damien George committed
41

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

        default:
52
            nlr_raise(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
53
54
55
    }
}

56
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_NONE
57

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

64
// This is called for operations on SMALL_INT that are not handled by mp_unary_op
65
mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
66
    return MP_OBJ_NULL;
67
68
}

69
// This is called for operations on SMALL_INT that are not handled by mp_binary_op
70
71
mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
    return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
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_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "long int not supported in this build"));
77
    return mp_const_none;
78
79
}

80
81
// 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) {
82
    nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "small int overflow"));
83
84
85
    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_raise(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_raise(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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// This dispatcher function is expected to be independent of the implementation of long int
// It handles the extra cases for integer-like arithmetic
mp_obj_t mp_obj_int_binary_op_extra_cases(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
    if (rhs_in == mp_const_false) {
        // false acts as 0
        return mp_binary_op(op, lhs_in, MP_OBJ_NEW_SMALL_INT(0));
    } else if (rhs_in == mp_const_true) {
        // true acts as 0
        return mp_binary_op(op, lhs_in, MP_OBJ_NEW_SMALL_INT(1));
    } else if (op == MP_BINARY_OP_MULTIPLY) {
        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)) {
            // multiply is commutative for these types, so delegate to them
            return mp_binary_op(op, rhs_in, lhs_in);
        }
    }
    return MP_OBJ_NULL;
}

138
const mp_obj_type_t mp_type_int = {
139
    { &mp_type_type },
140
    .name = MP_QSTR_int,
141
142
143
144
    .print = mp_obj_int_print,
    .make_new = mp_obj_int_make_new,
    .unary_op = mp_obj_int_unary_op,
    .binary_op = mp_obj_int_binary_op,
145
};