objint_longlong.c 5.06 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
12
13
14
15
16
17
18
19
20
21
22
23
#include "objint.h"
#include "runtime0.h"

#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) {
24
    if (MP_OBJ_IS_SMALL_INT(self_in)) {
25
        print(env, INT_FMT, MP_OBJ_SMALL_INT_VALUE(self_in));
26
27
28
29
    } else {
        mp_obj_int_t *self = self_in;
        print(env, "%lld" SUFFIX, self->val);
    }
30
31
}

32
33
34
mp_obj_t int_unary_op(int op, mp_obj_t o_in) {
    mp_obj_int_t *o = o_in;
    switch (op) {
35
        case RT_UNARY_OP_BOOL: return MP_BOOL(o->val != 0);
36
37
38
39
40
41
42
        case RT_UNARY_OP_POSITIVE: return o_in;
        case RT_UNARY_OP_NEGATIVE: return mp_obj_new_int_from_ll(-o->val);
        case RT_UNARY_OP_INVERT: return mp_obj_new_int_from_ll(~o->val);
        default: return NULL; // op not supported
    }
}

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

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

55
56
57
58
    if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
        rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
    } else if (MP_OBJ_IS_TYPE(rhs_in, &int_type)) {
        rhs_val = ((mp_obj_int_t*)rhs_in)->val;
59
60
61
62
63
64
65
    } else {
        return MP_OBJ_NULL;
    }

    switch (op) {
        case RT_BINARY_OP_ADD:
        case RT_BINARY_OP_INPLACE_ADD:
66
67
            return mp_obj_new_int_from_ll(lhs_val + rhs_val);
        case RT_BINARY_OP_SUBTRACT:
68
        case RT_BINARY_OP_INPLACE_SUBTRACT:
69
70
            return mp_obj_new_int_from_ll(lhs_val - rhs_val);
        case RT_BINARY_OP_MULTIPLY:
71
        case RT_BINARY_OP_INPLACE_MULTIPLY:
72
73
            return mp_obj_new_int_from_ll(lhs_val * rhs_val);
        case RT_BINARY_OP_FLOOR_DIVIDE:
74
        case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE:
75
76
            return mp_obj_new_int_from_ll(lhs_val / rhs_val);
        case RT_BINARY_OP_MODULO:
77
        case RT_BINARY_OP_INPLACE_MODULO:
78
            return mp_obj_new_int_from_ll(lhs_val % rhs_val);
79
80

        case RT_BINARY_OP_AND:
81
        case RT_BINARY_OP_INPLACE_AND:
82
83
            return mp_obj_new_int_from_ll(lhs_val & rhs_val);
        case RT_BINARY_OP_OR:
84
        case RT_BINARY_OP_INPLACE_OR:
85
86
            return mp_obj_new_int_from_ll(lhs_val | rhs_val);
        case RT_BINARY_OP_XOR:
87
        case RT_BINARY_OP_INPLACE_XOR:
88
            return mp_obj_new_int_from_ll(lhs_val ^ rhs_val);
89

90
        case RT_BINARY_OP_LSHIFT:
91
        case RT_BINARY_OP_INPLACE_LSHIFT:
92
93
            return mp_obj_new_int_from_ll(lhs_val << (int)rhs_val);
        case RT_BINARY_OP_RSHIFT:
94
        case RT_BINARY_OP_INPLACE_RSHIFT:
95
            return mp_obj_new_int_from_ll(lhs_val >> (int)rhs_val);
96

97
        case RT_BINARY_OP_LESS:
98
            return MP_BOOL(lhs_val < rhs_val);
99
        case RT_BINARY_OP_MORE:
100
            return MP_BOOL(lhs_val > rhs_val);
101
        case RT_BINARY_OP_LESS_EQUAL:
102
            return MP_BOOL(lhs_val <= rhs_val);
103
        case RT_BINARY_OP_MORE_EQUAL:
104
            return MP_BOOL(lhs_val >= rhs_val);
105
        case RT_BINARY_OP_EQUAL:
106
            return MP_BOOL(lhs_val == rhs_val);
107
        case RT_BINARY_OP_NOT_EQUAL:
108
            return MP_BOOL(lhs_val != rhs_val);
109
110
111
112
113
114
115
116
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
142
143

        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);
    o->base.type = &int_type;
    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);
144
145
146
    if (*end != 0) {
        nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
    }
147
148
149
150
151
152
    mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
    o->base.type = &int_type;
    o->val = v;
    return o;
}

153
154
155
156
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);
    }
157
158
159
160
    mp_obj_int_t *self = self_in;
    return self->val;
}

161
162
163
164
165
machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
    // TODO: Check overflow
    return mp_obj_int_get(self_in);
}

166
#endif