objint_mpz.c 8.68 KB
Newer Older
1
2
#include <stdint.h>
#include <string.h>
3
#include <stdio.h>
4
5
6
7
8

#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
9
#include "parsenumbase.h"
10
11
12
13
#include "obj.h"
#include "mpz.h"
#include "objint.h"
#include "runtime0.h"
14
#include "runtime.h"
15
16
17
18
19

#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ

STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) {
    mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
20
    o->base.type = &mp_type_int;
21
22
23
24
    mpz_init_zero(&o->mpz);
    return o;
}

25
void mp_obj_int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
26
27
28
29
30
31
32
33
34
35
36
    if (MP_OBJ_IS_SMALL_INT(self_in)) {
        print(env, INT_FMT, MP_OBJ_SMALL_INT_VALUE(self_in));
    } else {
        // TODO would rather not allocate memory to print...
        mp_obj_int_t *self = self_in;
        char *str = mpz_as_str(&self->mpz, 10);
        print(env, "%s", str);
        m_free(str, 0);
    }
}

37
mp_obj_t mp_obj_int_unary_op(int op, mp_obj_t o_in) {
38
39
    mp_obj_int_t *o = o_in;
    switch (op) {
Damien George's avatar
Damien George committed
40
41
42
43
        case MP_UNARY_OP_BOOL: return MP_BOOL(!mpz_is_zero(&o->mpz));
        case MP_UNARY_OP_POSITIVE: return o_in;
        case MP_UNARY_OP_NEGATIVE: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_neg_inpl(&o2->mpz, &o->mpz); return o2; }
        case MP_UNARY_OP_INVERT: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_not_inpl(&o2->mpz, &o->mpz); return o2; }
44
45
46
47
        default: return NULL; // op not supported
    }
}

48
mp_obj_t mp_obj_int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
49
    const mpz_t *zlhs;
50
51
52
    const mpz_t *zrhs;
    mpz_t z_int;
    mpz_dig_t z_int_dig[MPZ_NUM_DIG_FOR_INT];
53

54
55
56
57
    // lhs could be a small int (eg small-int + mpz)
    if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
        mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(lhs_in));
        zlhs = &z_int;
58
    } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
59
60
        zlhs = &((mp_obj_int_t*)lhs_in)->mpz;
    } else {
61
        // unsupported type
62
63
64
        return MP_OBJ_NULL;
    }

Damien George's avatar
Damien George committed
65
    // if rhs is small int, then lhs was not (otherwise mp_binary_op handles it)
66
    if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
67
68
        mpz_init_fixed_from_int(&z_int, z_int_dig, MPZ_NUM_DIG_FOR_INT, MP_OBJ_SMALL_INT_VALUE(rhs_in));
        zrhs = &z_int;
69
    } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
70
        zrhs = &((mp_obj_int_t*)rhs_in)->mpz;
71
72
73
74
75
76
#if MICROPY_ENABLE_FLOAT
    } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_float)) {
        return mp_obj_float_binary_op(op, mpz_as_float(zlhs), rhs_in);
    } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
        return mp_obj_complex_binary_op(op, mpz_as_float(zlhs), 0, rhs_in);
#endif
77
    } else {
78
79
        // delegate to generic function to check for extra cases
        return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
80
81
    }

82
83
    if (0) {
#if MICROPY_ENABLE_FLOAT
Damien George's avatar
Damien George committed
84
    } else if (op == MP_BINARY_OP_TRUE_DIVIDE || op == MP_BINARY_OP_INPLACE_TRUE_DIVIDE) {
85
86
        mp_float_t flhs = mpz_as_float(zlhs);
        mp_float_t frhs = mpz_as_float(zrhs);
87
        return mp_obj_new_float(flhs / frhs);
88
#endif
89

Damien George's avatar
Damien George committed
90
    } else if (op <= MP_BINARY_OP_INPLACE_POWER) {
91
92
93
        mp_obj_int_t *res = mp_obj_int_new_mpz();

        switch (op) {
Damien George's avatar
Damien George committed
94
95
            case MP_BINARY_OP_ADD:
            case MP_BINARY_OP_INPLACE_ADD:
96
97
                mpz_add_inpl(&res->mpz, zlhs, zrhs);
                break;
Damien George's avatar
Damien George committed
98
99
            case MP_BINARY_OP_SUBTRACT:
            case MP_BINARY_OP_INPLACE_SUBTRACT:
100
101
                mpz_sub_inpl(&res->mpz, zlhs, zrhs);
                break;
Damien George's avatar
Damien George committed
102
103
            case MP_BINARY_OP_MULTIPLY:
            case MP_BINARY_OP_INPLACE_MULTIPLY:
104
105
                mpz_mul_inpl(&res->mpz, zlhs, zrhs);
                break;
Damien George's avatar
Damien George committed
106
107
            case MP_BINARY_OP_FLOOR_DIVIDE:
            case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: {
108
109
                mpz_t rem; mpz_init_zero(&rem);
                mpz_divmod_inpl(&res->mpz, &rem, zlhs, zrhs);
110
                if (zlhs->neg != zrhs->neg) {
111
112
113
114
115
                    if (!mpz_is_zero(&rem)) {
                        mpz_t mpzone; mpz_init_from_int(&mpzone, -1);
                        mpz_add_inpl(&res->mpz, &res->mpz, &mpzone);
                    }
                }
116
117
118
                mpz_deinit(&rem);
                break;
            }
Damien George's avatar
Damien George committed
119
120
            case MP_BINARY_OP_MODULO:
            case MP_BINARY_OP_INPLACE_MODULO: {
Damien George's avatar
Damien George committed
121
122
123
                mpz_t quo; mpz_init_zero(&quo);
                mpz_divmod_inpl(&quo, &res->mpz, zlhs, zrhs);
                mpz_deinit(&quo);
124
125
                // Check signs and do Python style modulo
                if (zlhs->neg != zrhs->neg) {
126
127
                    mpz_add_inpl(&res->mpz, &res->mpz, zrhs);
                }
Damien George's avatar
Damien George committed
128
129
                break;
            }
130

Damien George's avatar
Damien George committed
131
132
            case MP_BINARY_OP_AND:
            case MP_BINARY_OP_INPLACE_AND:
133
134
                mpz_and_inpl(&res->mpz, zlhs, zrhs);
                break;
Damien George's avatar
Damien George committed
135
136
            case MP_BINARY_OP_OR:
            case MP_BINARY_OP_INPLACE_OR:
137
138
                mpz_or_inpl(&res->mpz, zlhs, zrhs);
                break;
Damien George's avatar
Damien George committed
139
140
            case MP_BINARY_OP_XOR:
            case MP_BINARY_OP_INPLACE_XOR:
141
142
                mpz_xor_inpl(&res->mpz, zlhs, zrhs);
                break;
143

Damien George's avatar
Damien George committed
144
145
146
147
            case MP_BINARY_OP_LSHIFT:
            case MP_BINARY_OP_INPLACE_LSHIFT:
            case MP_BINARY_OP_RSHIFT:
            case MP_BINARY_OP_INPLACE_RSHIFT: {
148
149
150
                // TODO check conversion overflow
                machine_int_t irhs = mpz_as_int(zrhs);
                if (irhs < 0) {
151
                    nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
152
                }
Damien George's avatar
Damien George committed
153
                if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
154
155
156
157
158
159
                    mpz_shl_inpl(&res->mpz, zlhs, irhs);
                } else {
                    mpz_shr_inpl(&res->mpz, zlhs, irhs);
                }
                break;
            }
160

Damien George's avatar
Damien George committed
161
162
            case MP_BINARY_OP_POWER:
            case MP_BINARY_OP_INPLACE_POWER:
163
164
165
166
167
168
169
170
171
172
173
174
                mpz_pow_inpl(&res->mpz, zlhs, zrhs);
                break;

            default:
                return MP_OBJ_NULL;
        }

        return res;

    } else {
        int cmp = mpz_cmp(zlhs, zrhs);
        switch (op) {
Damien George's avatar
Damien George committed
175
            case MP_BINARY_OP_LESS:
176
                return MP_BOOL(cmp < 0);
Damien George's avatar
Damien George committed
177
            case MP_BINARY_OP_MORE:
178
                return MP_BOOL(cmp > 0);
Damien George's avatar
Damien George committed
179
            case MP_BINARY_OP_LESS_EQUAL:
180
                return MP_BOOL(cmp <= 0);
Damien George's avatar
Damien George committed
181
            case MP_BINARY_OP_MORE_EQUAL:
182
                return MP_BOOL(cmp >= 0);
Damien George's avatar
Damien George committed
183
            case MP_BINARY_OP_EQUAL:
184
                return MP_BOOL(cmp == 0);
Damien George's avatar
Damien George committed
185
            case MP_BINARY_OP_NOT_EQUAL:
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
                return MP_BOOL(cmp != 0);

            default:
                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_ll(long long val) {
    mp_obj_int_t *o = mp_obj_int_new_mpz();
203
    mpz_set_from_ll(&o->mpz, val);
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
    return o;
}

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_long_str(const char *str) {
    mp_obj_int_t *o = mp_obj_int_new_mpz();
    uint len = strlen(str);
219
220
221
222
223
    int base = 0;
    int skip = mp_parse_num_base(str, len, &base);
    str += skip;
    len -= skip;
    uint n = mpz_set_from_str(&o->mpz, str, len, false, base);
224
    if (n != len) {
225
        nlr_raise(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number"));
226
227
228
229
230
231
232
    }
    return o;
}

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);
233
234
235
    } else {
        mp_obj_int_t *self = self_in;
        return mpz_as_int(&self->mpz);
236
237
238
239
    }
}

machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) {
240
241
242
243
244
245
246
247
248
    if (MP_OBJ_IS_SMALL_INT(self_in)) {
        return MP_OBJ_SMALL_INT_VALUE(self_in);
    } else {
        mp_obj_int_t *self = self_in;
        machine_int_t value;
        if (mpz_as_int_checked(&self->mpz, &value)) {
            return value;
        } else {
            // overflow
249
            nlr_raise(mp_obj_new_exception_msg(&mp_type_OverflowError, "overflow converting long int to machine word"));
250
251
        }
    }
252
253
}

254
255
256
257
258
259
260
261
262
263
264
#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 mpz_as_float(&self->mpz);
    }
}
#endif

265
#endif