objint_longlong.c 7.66 KB
Newer Older
1
2
3
4
5
6
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
7
 * Copyright (c) 2014 Paul Sokolovsky
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

28
29
#include <stdlib.h>
#include <stdint.h>
30
#include <string.h>
31

32
#include "mpconfig.h"
33
34
#include "nlr.h"
#include "misc.h"
35
#include "qstr.h"
36
#include "obj.h"
37
#include "smallint.h"
38
#include "mpz.h"
39
40
#include "objint.h"
#include "runtime0.h"
41
#include "runtime.h"
42

43
44
45
46
#if MICROPY_PY_BUILTINS_FLOAT
#include <math.h>
#endif

47
48
49
50
51
52
53
54
55
56
#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

57
58
#if MICROPY_PY_SYS_MAXSIZE
// Export value for sys.maxsize
59
const mp_obj_int_t mp_maxsize_obj = {{&mp_type_int}, MP_SSIZE_MAX};
60
61
#endif

Damien George's avatar
Damien George committed
62
63
64
65
66
67
68
69
70
71
mp_int_t mp_obj_int_hash(mp_obj_t self_in) {
    if (MP_OBJ_IS_SMALL_INT(self_in)) {
        return MP_OBJ_SMALL_INT_VALUE(self_in);
    }
    mp_obj_int_t *self = self_in;
    // truncate value to fit in mp_int_t, which gives the same hash as
    // small int if the value fits without truncation
    return self->val;
}

72
73
74
75
76
77
78
79
bool mp_obj_int_is_positive(mp_obj_t self_in) {
    if (MP_OBJ_IS_SMALL_INT(self_in)) {
        return MP_OBJ_SMALL_INT_VALUE(self_in) >= 0;
    }
    mp_obj_int_t *self = self_in;
    return self->val >= 0;
}

80
mp_obj_t mp_obj_int_unary_op(mp_uint_t op, mp_obj_t o_in) {
81
82
    mp_obj_int_t *o = o_in;
    switch (op) {
Damien George's avatar
Damien George committed
83
84
85
86
        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);
87
        default: return MP_OBJ_NULL; // op not supported
88
89
90
    }
}

91
mp_obj_t mp_obj_int_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
92
    long long lhs_val;
93
94
    long long rhs_val;

95
96
    if (MP_OBJ_IS_SMALL_INT(lhs_in)) {
        lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs_in);
97
    } else if (MP_OBJ_IS_TYPE(lhs_in, &mp_type_int)) {
98
99
        lhs_val = ((mp_obj_int_t*)lhs_in)->val;
    } else {
100
        return MP_OBJ_NULL; // op not supported
101
    }
102

103
104
    if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
        rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs_in);
105
    } else if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_int)) {
106
        rhs_val = ((mp_obj_int_t*)rhs_in)->val;
107
    } else {
108
109
        // delegate to generic function to check for extra cases
        return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
110
111
112
    }

    switch (op) {
Damien George's avatar
Damien George committed
113
114
        case MP_BINARY_OP_ADD:
        case MP_BINARY_OP_INPLACE_ADD:
115
            return mp_obj_new_int_from_ll(lhs_val + rhs_val);
Damien George's avatar
Damien George committed
116
117
        case MP_BINARY_OP_SUBTRACT:
        case MP_BINARY_OP_INPLACE_SUBTRACT:
118
            return mp_obj_new_int_from_ll(lhs_val - rhs_val);
Damien George's avatar
Damien George committed
119
120
        case MP_BINARY_OP_MULTIPLY:
        case MP_BINARY_OP_INPLACE_MULTIPLY:
121
            return mp_obj_new_int_from_ll(lhs_val * rhs_val);
Damien George's avatar
Damien George committed
122
123
        case MP_BINARY_OP_FLOOR_DIVIDE:
        case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
124
            return mp_obj_new_int_from_ll(lhs_val / rhs_val);
Damien George's avatar
Damien George committed
125
126
        case MP_BINARY_OP_MODULO:
        case MP_BINARY_OP_INPLACE_MODULO:
127
            return mp_obj_new_int_from_ll(lhs_val % rhs_val);
128

Damien George's avatar
Damien George committed
129
130
        case MP_BINARY_OP_AND:
        case MP_BINARY_OP_INPLACE_AND:
131
            return mp_obj_new_int_from_ll(lhs_val & rhs_val);
Damien George's avatar
Damien George committed
132
133
        case MP_BINARY_OP_OR:
        case MP_BINARY_OP_INPLACE_OR:
134
            return mp_obj_new_int_from_ll(lhs_val | rhs_val);
Damien George's avatar
Damien George committed
135
136
        case MP_BINARY_OP_XOR:
        case MP_BINARY_OP_INPLACE_XOR:
137
            return mp_obj_new_int_from_ll(lhs_val ^ rhs_val);
138

Damien George's avatar
Damien George committed
139
140
        case MP_BINARY_OP_LSHIFT:
        case MP_BINARY_OP_INPLACE_LSHIFT:
141
            return mp_obj_new_int_from_ll(lhs_val << (int)rhs_val);
Damien George's avatar
Damien George committed
142
143
        case MP_BINARY_OP_RSHIFT:
        case MP_BINARY_OP_INPLACE_RSHIFT:
144
            return mp_obj_new_int_from_ll(lhs_val >> (int)rhs_val);
145

Damien George's avatar
Damien George committed
146
        case MP_BINARY_OP_LESS:
147
            return MP_BOOL(lhs_val < rhs_val);
Damien George's avatar
Damien George committed
148
        case MP_BINARY_OP_MORE:
149
            return MP_BOOL(lhs_val > rhs_val);
Damien George's avatar
Damien George committed
150
        case MP_BINARY_OP_LESS_EQUAL:
151
            return MP_BOOL(lhs_val <= rhs_val);
Damien George's avatar
Damien George committed
152
        case MP_BINARY_OP_MORE_EQUAL:
153
            return MP_BOOL(lhs_val >= rhs_val);
Damien George's avatar
Damien George committed
154
        case MP_BINARY_OP_EQUAL:
155
            return MP_BOOL(lhs_val == rhs_val);
156
157

        default:
158
            return MP_OBJ_NULL; // op not supported
159
160
161
    }
}

162
mp_obj_t mp_obj_new_int(mp_int_t value) {
163
    if (MP_SMALL_INT_FITS(value)) {
164
165
166
167
168
        return MP_OBJ_NEW_SMALL_INT(value);
    }
    return mp_obj_new_int_from_ll(value);
}

169
mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
170
171
172
173
174
175
176
177
178
179
    // 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);
180
    o->base.type = &mp_type_int;
181
182
183
184
    o->val = val;
    return o;
}

185
186
187
188
189
190
191
192
193
mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
    // TODO raise an exception if the unsigned long long won't fit
    assert(val >> (sizeof(unsigned long long) * 8 - 1) == 0);
    mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
    o->base.type = &mp_type_int;
    o->val = val;
    return o;
}

194
195
196
197
198
199
200
201
#if MICROPY_PY_BUILTINS_FLOAT
mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
    // TODO raise an exception if the unsigned long long won't fit
    long long i = MICROPY_FLOAT_C_FUN(trunc)(val);
    return mp_obj_new_int_from_ll(i);
}
#endif

202
mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg, mp_uint_t base) {
203
204
    // TODO this does not honor the given length of the string, but it all cases it should anyway be null terminated
    // TODO check overflow
205
    mp_obj_int_t *o = m_new_obj(mp_obj_int_t);
206
    o->base.type = &mp_type_int;
207
208
209
    char *endptr;
    o->val = strtoll(*str, &endptr, base);
    *str = endptr;
210
211
212
    return o;
}

213
mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in) {
214
215
    if (MP_OBJ_IS_SMALL_INT(self_in)) {
        return MP_OBJ_SMALL_INT_VALUE(self_in);
216
    } else {
217
        const mp_obj_int_t *self = self_in;
218
        return self->val;
219
    }
220
221
}

222
mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
223
    // TODO: Check overflow
224
    return mp_obj_int_get_truncated(self_in);
225
226
}

227
#if MICROPY_PY_BUILTINS_FLOAT
228
229
230
231
232
233
234
235
236
237
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

238
#endif