Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame^] | 1 | #include <stdlib.h> |
| 2 | #include <stdint.h> |
| 3 | #include <string.h> |
| 4 | #include <assert.h> |
| 5 | |
| 6 | #include "nlr.h" |
| 7 | #include "misc.h" |
| 8 | #include "mpconfig.h" |
| 9 | #include "qstr.h" |
| 10 | #include "obj.h" |
| 11 | #include "mpz.h" |
| 12 | #include "objint.h" |
| 13 | #include "runtime0.h" |
| 14 | |
| 15 | #if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ |
| 16 | |
| 17 | STATIC mp_obj_int_t *mp_obj_int_new_mpz(void) { |
| 18 | mp_obj_int_t *o = m_new_obj(mp_obj_int_t); |
| 19 | o->base.type = &int_type; |
| 20 | mpz_init_zero(&o->mpz); |
| 21 | return o; |
| 22 | } |
| 23 | |
| 24 | 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 | } else { |
| 28 | // TODO would rather not allocate memory to print... |
| 29 | mp_obj_int_t *self = self_in; |
| 30 | char *str = mpz_as_str(&self->mpz, 10); |
| 31 | print(env, "%s", str); |
| 32 | m_free(str, 0); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | mp_obj_t int_unary_op(int op, mp_obj_t o_in) { |
| 37 | mp_obj_int_t *o = o_in; |
| 38 | switch (op) { |
| 39 | case RT_UNARY_OP_BOOL: return MP_BOOL(!mpz_is_zero(&o->mpz)); |
| 40 | case RT_UNARY_OP_POSITIVE: return o_in; |
| 41 | case RT_UNARY_OP_NEGATIVE: { mp_obj_int_t *o2 = mp_obj_int_new_mpz(); mpz_neg_inpl(&o2->mpz, &o->mpz); return o2; } |
| 42 | //case RT_UNARY_OP_INVERT: ~ not implemented for mpz |
| 43 | default: return NULL; // op not supported |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { |
| 48 | mpz_t *zlhs = &((mp_obj_int_t*)lhs_in)->mpz; |
| 49 | mpz_t *zrhs; |
| 50 | |
| 51 | if (MP_OBJ_IS_SMALL_INT(rhs_in)) { |
| 52 | zrhs = mpz_from_int(MP_OBJ_SMALL_INT_VALUE(rhs_in)); |
| 53 | } else if (MP_OBJ_IS_TYPE(rhs_in, &int_type)) { |
| 54 | zrhs = &((mp_obj_int_t*)rhs_in)->mpz; |
| 55 | } else { |
| 56 | return MP_OBJ_NULL; |
| 57 | } |
| 58 | |
| 59 | if (op == RT_BINARY_OP_TRUE_DIVIDE || op == RT_BINARY_OP_INPLACE_TRUE_DIVIDE) { |
| 60 | machine_float_t flhs = mpz_as_float(zlhs); |
| 61 | machine_float_t frhs = mpz_as_float(zrhs); |
| 62 | return mp_obj_new_float(flhs / frhs); |
| 63 | |
| 64 | } else if (op <= RT_BINARY_OP_POWER) { |
| 65 | mp_obj_int_t *res = mp_obj_int_new_mpz(); |
| 66 | |
| 67 | switch (op) { |
| 68 | case RT_BINARY_OP_ADD: |
| 69 | case RT_BINARY_OP_INPLACE_ADD: |
| 70 | mpz_add_inpl(&res->mpz, zlhs, zrhs); |
| 71 | break; |
| 72 | case RT_BINARY_OP_SUBTRACT: |
| 73 | case RT_BINARY_OP_INPLACE_SUBTRACT: |
| 74 | mpz_sub_inpl(&res->mpz, zlhs, zrhs); |
| 75 | break; |
| 76 | case RT_BINARY_OP_MULTIPLY: |
| 77 | case RT_BINARY_OP_INPLACE_MULTIPLY: |
| 78 | mpz_mul_inpl(&res->mpz, zlhs, zrhs); |
| 79 | break; |
| 80 | case RT_BINARY_OP_FLOOR_DIVIDE: |
| 81 | case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: { |
| 82 | mpz_t rem; mpz_init_zero(&rem); |
| 83 | mpz_divmod_inpl(&res->mpz, &rem, zlhs, zrhs); |
| 84 | mpz_deinit(&rem); |
| 85 | break; |
| 86 | } |
| 87 | |
| 88 | //case RT_BINARY_OP_MODULO: |
| 89 | //case RT_BINARY_OP_INPLACE_MODULO: |
| 90 | |
| 91 | //case RT_BINARY_OP_AND: |
| 92 | //case RT_BINARY_OP_INPLACE_AND: |
| 93 | //case RT_BINARY_OP_OR: |
| 94 | //case RT_BINARY_OP_INPLACE_OR: |
| 95 | //case RT_BINARY_OP_XOR: |
| 96 | //case RT_BINARY_OP_INPLACE_XOR: |
| 97 | |
| 98 | //case RT_BINARY_OP_LSHIFT: |
| 99 | //case RT_BINARY_OP_INPLACE_LSHIFT: |
| 100 | //case RT_BINARY_OP_RSHIFT: |
| 101 | //case RT_BINARY_OP_INPLACE_RSHIFT: |
| 102 | |
| 103 | case RT_BINARY_OP_POWER: |
| 104 | case RT_BINARY_OP_INPLACE_POWER: |
| 105 | mpz_pow_inpl(&res->mpz, zlhs, zrhs); |
| 106 | break; |
| 107 | |
| 108 | default: |
| 109 | return MP_OBJ_NULL; |
| 110 | } |
| 111 | |
| 112 | return res; |
| 113 | |
| 114 | } else { |
| 115 | int cmp = mpz_cmp(zlhs, zrhs); |
| 116 | switch (op) { |
| 117 | case RT_BINARY_OP_LESS: |
| 118 | return MP_BOOL(cmp < 0); |
| 119 | case RT_BINARY_OP_MORE: |
| 120 | return MP_BOOL(cmp > 0); |
| 121 | case RT_BINARY_OP_LESS_EQUAL: |
| 122 | return MP_BOOL(cmp <= 0); |
| 123 | case RT_BINARY_OP_MORE_EQUAL: |
| 124 | return MP_BOOL(cmp >= 0); |
| 125 | case RT_BINARY_OP_EQUAL: |
| 126 | return MP_BOOL(cmp == 0); |
| 127 | case RT_BINARY_OP_NOT_EQUAL: |
| 128 | return MP_BOOL(cmp != 0); |
| 129 | |
| 130 | default: |
| 131 | return MP_OBJ_NULL; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | mp_obj_t mp_obj_new_int(machine_int_t value) { |
| 137 | if (MP_OBJ_FITS_SMALL_INT(value)) { |
| 138 | return MP_OBJ_NEW_SMALL_INT(value); |
| 139 | } |
| 140 | return mp_obj_new_int_from_ll(value); |
| 141 | } |
| 142 | |
| 143 | mp_obj_t mp_obj_new_int_from_ll(long long val) { |
| 144 | mp_obj_int_t *o = mp_obj_int_new_mpz(); |
| 145 | mpz_set_from_int(&o->mpz, val); |
| 146 | return o; |
| 147 | } |
| 148 | |
| 149 | mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value) { |
| 150 | // SMALL_INT accepts only signed numbers, of one bit less size |
| 151 | // than word size, which totals 2 bits less for unsigned numbers. |
| 152 | if ((value & (WORD_MSBIT_HIGH | (WORD_MSBIT_HIGH >> 1))) == 0) { |
| 153 | return MP_OBJ_NEW_SMALL_INT(value); |
| 154 | } |
| 155 | return mp_obj_new_int_from_ll(value); |
| 156 | } |
| 157 | |
| 158 | mp_obj_t mp_obj_new_int_from_long_str(const char *str) { |
| 159 | mp_obj_int_t *o = mp_obj_int_new_mpz(); |
| 160 | uint len = strlen(str); |
| 161 | uint n = mpz_set_from_str(&o->mpz, str, len, false, 10); |
| 162 | if (n != len) { |
| 163 | nlr_jump(mp_obj_new_exception_msg(&mp_type_SyntaxError, "invalid syntax for number")); |
| 164 | } |
| 165 | return o; |
| 166 | } |
| 167 | |
| 168 | machine_int_t mp_obj_int_get(mp_obj_t self_in) { |
| 169 | if (MP_OBJ_IS_SMALL_INT(self_in)) { |
| 170 | return MP_OBJ_SMALL_INT_VALUE(self_in); |
| 171 | } |
| 172 | mp_obj_int_t *self = self_in; |
| 173 | return mpz_as_int(&self->mpz); |
| 174 | } |
| 175 | |
| 176 | machine_int_t mp_obj_int_get_checked(mp_obj_t self_in) { |
| 177 | // TODO: Check overflow |
| 178 | return mp_obj_int_get(self_in); |
| 179 | } |
| 180 | |
| 181 | #endif |