objint_longlong.c 5.72 KB
Newer Older
1
2
3
4
5
6
#include <stdlib.h>
#include <stdint.h>

#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
7
#include "qstr.h"
8
#include "obj.h"
9
#include "mpz.h"
10
11
#include "objint.h"
#include "runtime0.h"
12
#include "runtime.h"
13
14
15
16
17
18
19
20
21
22
23
24

#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG

// Python3 no longer has "l" suffix for long ints. We allow to use it
// for debugging purpose though.
#ifdef DEBUG
#define SUFFIX "l"
#else
#define SUFFIX ""
#endif

void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
25
    if (MP_OBJ_IS_SMALL_INT(self_in)) {
26
        print(env, INT_FMT, MP_OBJ_SMALL_INT_VALUE(self_in));
27
28
29
30
    } else {
        mp_obj_int_t *self = self_in;
        print(env, "%lld" SUFFIX, self->val);
    }
31
32
}

33
34
35
mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
    mp_obj_int_t *o = o_in;
    switch (op) {
Damien George's avatar
Damien George committed
36
37
38
39
        case MP_UNARY_OP_BOOL: return MP_BOOL(o->val != 0);
        case MP_UNARY_OP_POSITIVE: return o_in;
        case MP_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val);
        case MP_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val);
40
41
42
43
        default: return NULL; // op not supported
    }
}

44
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
45
    long long lhs_val;
46
47
    long long rhs_val;

48
49
    if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
        lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs_in);
50
    } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
51
52
53
54
        lhs_val = ((mp_obj_int_t*)lhs_in)->val;
    } else {
        return MP_OBJ_NULL;
    }
55

56
57
    if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
        rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
58
    } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
59
        rhs_val = ((mp_obj_int_t*)rhs_in)->val;
60
    } else {
61
62
63
64
65
66
67
        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);
            }
        }
        // unsupported operation/type
68
69
70
71
        return MP_OBJ_NULL;
    }

    switch (op) {
Damien George's avatar
Damien George committed
72
73
        case MP_BINARY_OP_ADD:
        case MP_BINARY_OP_INPLACE_ADD:
74
            return mp_obj_new_int_from_ll(lhs_val + rhs_val);
Damien George's avatar
Damien George committed
75
76
        case MP_BINARY_OP_SUBTRACT:
        case MP_BINARY_OP_INPLACE_SUBTRACT:
77
            return mp_obj_new_int_from_ll(lhs_val - rhs_val);
Damien George's avatar
Damien George committed
78
79
        case MP_BINARY_OP_MULTIPLY:
        case MP_BINARY_OP_INPLACE_MULTIPLY:
80
            return mp_obj_new_int_from_ll(lhs_val * rhs_val);
Damien George's avatar
Damien George committed
81
82
        case MP_BINARY_OP_FLOOR_DIVIDE:
        case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
83
            return mp_obj_new_int_from_ll(lhs_val / rhs_val);
Damien George's avatar
Damien George committed
84
85
        case MP_BINARY_OP_MODULO:
        case MP_BINARY_OP_INPLACE_MODULO:
86
            return mp_obj_new_int_from_ll(lhs_val % rhs_val);
87

Damien George's avatar
Damien George committed
88
89
        case MP_BINARY_OP_AND:
        case MP_BINARY_OP_INPLACE_AND:
90
            return mp_obj_new_int_from_ll(lhs_val & rhs_val);
Damien George's avatar
Damien George committed
91
92
        case MP_BINARY_OP_OR:
        case MP_BINARY_OP_INPLACE_OR:
93
            return mp_obj_new_int_from_ll(lhs_val | rhs_val);
Damien George's avatar
Damien George committed
94
95
        case MP_BINARY_OP_XOR:
        case MP_BINARY_OP_INPLACE_XOR:
96
            return mp_obj_new_int_from_ll(lhs_val ^ rhs_val);
97

Damien George's avatar
Damien George committed
98
99
        case MP_BINARY_OP_LSHIFT:
        case MP_BINARY_OP_INPLACE_LSHIFT:
100
            return mp_obj_new_int_from_ll(lhs_val << (int)rhs_val);
Damien George's avatar
Damien George committed
101
102
        case MP_BINARY_OP_RSHIFT:
        case MP_BINARY_OP_INPLACE_RSHIFT:
103
            return mp_obj_new_int_from_ll(lhs_val >> (int)rhs_val);
104

Damien George's avatar
Damien George committed
105
        case MP_BINARY_OP_LESS:
106
            return MP_BOOL(lhs_val < rhs_val);
Damien George's avatar
Damien George committed
107
        case MP_BINARY_OP_MORE:
108
            return MP_BOOL(lhs_val > rhs_val);
Damien George's avatar
Damien George committed
109
        case MP_BINARY_OP_LESS_EQUAL:
110
            return MP_BOOL(lhs_val <= rhs_val);
Damien George's avatar
Damien George committed
111
        case MP_BINARY_OP_MORE_EQUAL:
112
            return MP_BOOL(lhs_val >= rhs_val);
Damien George's avatar
Damien George committed
113
        case MP_BINARY_OP_EQUAL:
114
            return MP_BOOL(lhs_val == rhs_val);
Damien George's avatar
Damien George committed
115
        case MP_BINARY_OP_NOT_EQUAL:
116
            return MP_BOOL(lhs_val != rhs_val);
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

        default:
            // op not supported
            return MP_OBJ_NULL;
    }
}

mp_obj_t mp_obj_new_int(machine_int_t value) {
    if (MP_OBJ_FITS_SMALL_INT(value)) {
        return MP_OBJ_NEW_SMALL_INT(value);
    }
    return mp_obj_new_int_from_ll(value);
}

mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) {
    // SMALL_INT accepts only signed numbers, of one bit less size
    // than 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);
    }
    return mp_obj_new_int_from_ll(value);
}

mp_obj_t mp_obj_new_int_from_ll(long long val) {
    mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
142
    o->base.type = &mp_type_int;
143
144
145
146
147
148
149
150
151
    o->val = val;
    return o;
}

mp_obj_t mp_obj_new_int_from_long_str(const char *s) {
    long long v;
    char *end;
    // TODO: this doesn't handle Python hacked 0o octal syntax
    v = strtoll(s, &end, 0);
152
153
154
    if (*end != 0) {
        nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
    }
155
    mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
156
    o->base.type = &mp_type_int;
157
158
159
160
    o->val = v;
    return o;
}

161
162
163
machine_int_t mp_obj_int_get(mp_obj_t self_in) {
    if (MP_OBJ_IS_SMALL_INT(self_in)) {
        return MP_OBJ_SMALL_INT_VALUE(self_in);
164
165
166
    } else {
        mp_obj_int_t *self = self_in;
        return self->val;
167
    }
168
169
}

170
171
172
173
174
machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
    // TODO: Check overflow
    return mp_obj_int_get(self_in);
}

175
176
177
178
179
180
181
182
183
184
185
#if MICROPY_ENABLE_FLOAT
mp_float_t mp_obj_int_as_float(mp_obj_t self_in) {
    if (MP_OBJ_IS_SMALL_INT(self_in)) {
        return MP_OBJ_SMALL_INT_VALUE(self_in);
    } else {
        mp_obj_int_t *self = self_in;
        return self->val;
    }
}
#endif

186
#endif