Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
Damien George | 9a21d2e | 2014-09-06 17:15:34 +0100 | [diff] [blame] | 27 | // This mpz module implements arbitrary precision integers. |
| 28 | // |
| 29 | // The storage for each digit is defined by mpz_dig_t. The actual number of |
| 30 | // bits in mpz_dig_t that are used is defined by MPZ_DIG_SIZE. The machine must |
| 31 | // also provide a type that is twice as wide as mpz_dig_t, in both signed and |
| 32 | // unsigned versions. |
| 33 | // |
| 34 | // MPZ_DIG_SIZE can be between 4 and 8*sizeof(mpz_dig_t), but it makes most |
| 35 | // sense to have it as large as possible. Below, the type is auto-detected |
| 36 | // depending on the machine, but it (and MPZ_DIG_SIZE) can be freely changed so |
| 37 | // long as the constraints mentioned above are met. |
| 38 | |
stijn | 0e557fa | 2014-10-30 14:39:22 +0100 | [diff] [blame^] | 39 | #if defined(__x86_64__) || defined(_WIN64) |
Damien George | 9a21d2e | 2014-09-06 17:15:34 +0100 | [diff] [blame] | 40 | // 64-bit machine, using 32-bit storage for digits |
| 41 | typedef uint32_t mpz_dig_t; |
| 42 | typedef uint64_t mpz_dbl_dig_t; |
| 43 | typedef int64_t mpz_dbl_dig_signed_t; |
| 44 | #define MPZ_DIG_SIZE (32) |
| 45 | #else |
| 46 | // 32-bit machine, using 16-bit storage for digits |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 47 | typedef uint16_t mpz_dig_t; |
| 48 | typedef uint32_t mpz_dbl_dig_t; |
| 49 | typedef int32_t mpz_dbl_dig_signed_t; |
Damien George | 9a21d2e | 2014-09-06 17:15:34 +0100 | [diff] [blame] | 50 | #define MPZ_DIG_SIZE (16) |
| 51 | #endif |
| 52 | |
stijn | 0e557fa | 2014-10-30 14:39:22 +0100 | [diff] [blame^] | 53 | #ifdef _WIN64 |
| 54 | #define MPZ_LONG_1 1i64 |
| 55 | #else |
| 56 | #define MPZ_LONG_1 1L |
| 57 | #endif |
| 58 | |
Damien George | 9a21d2e | 2014-09-06 17:15:34 +0100 | [diff] [blame] | 59 | #define MPZ_NUM_DIG_FOR_INT (sizeof(mp_int_t) * 8 / MPZ_DIG_SIZE + 1) |
| 60 | #define MPZ_NUM_DIG_FOR_LL (sizeof(long long) * 8 / MPZ_DIG_SIZE + 1) |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 61 | |
| 62 | typedef struct _mpz_t { |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 63 | mp_uint_t neg : 1; |
| 64 | mp_uint_t fixed_dig : 1; |
| 65 | mp_uint_t alloc : 30; |
| 66 | mp_uint_t len; |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 67 | mpz_dig_t *dig; |
| 68 | } mpz_t; |
| 69 | |
Damien George | 06201ff | 2014-03-01 19:50:50 +0000 | [diff] [blame] | 70 | // convenience macro to declare an mpz with a digit array from the stack, initialised by an integer |
| 71 | #define MPZ_CONST_INT(z, val) mpz_t z; mpz_dig_t z ## _digits[MPZ_NUM_DIG_FOR_INT]; mpz_init_fixed_from_int(&z, z_digits, MPZ_NUM_DIG_FOR_INT, val); |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 72 | |
| 73 | void mpz_init_zero(mpz_t *z); |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 74 | void mpz_init_from_int(mpz_t *z, mp_int_t val); |
Damien George | afb1cf7 | 2014-09-05 20:37:06 +0100 | [diff] [blame] | 75 | void mpz_init_fixed_from_int(mpz_t *z, mpz_dig_t *dig, mp_uint_t dig_alloc, mp_int_t val); |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 76 | void mpz_deinit(mpz_t *z); |
| 77 | |
| 78 | mpz_t *mpz_zero(); |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 79 | mpz_t *mpz_from_int(mp_int_t i); |
Damien George | 9530743 | 2014-09-10 22:10:33 +0100 | [diff] [blame] | 80 | mpz_t *mpz_from_ll(long long i, bool is_signed); |
Damien George | afb1cf7 | 2014-09-05 20:37:06 +0100 | [diff] [blame] | 81 | mpz_t *mpz_from_str(const char *str, mp_uint_t len, bool neg, mp_uint_t base); |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 82 | void mpz_free(mpz_t *z); |
| 83 | |
| 84 | mpz_t *mpz_clone(const mpz_t *src); |
| 85 | |
| 86 | void mpz_set(mpz_t *dest, const mpz_t *src); |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 87 | void mpz_set_from_int(mpz_t *z, mp_int_t src); |
Damien George | 9530743 | 2014-09-10 22:10:33 +0100 | [diff] [blame] | 88 | void mpz_set_from_ll(mpz_t *z, long long i, bool is_signed); |
Damien George | afb1cf7 | 2014-09-05 20:37:06 +0100 | [diff] [blame] | 89 | mp_uint_t mpz_set_from_str(mpz_t *z, const char *str, mp_uint_t len, bool neg, mp_uint_t base); |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 90 | |
| 91 | bool mpz_is_zero(const mpz_t *z); |
| 92 | bool mpz_is_pos(const mpz_t *z); |
| 93 | bool mpz_is_neg(const mpz_t *z); |
| 94 | bool mpz_is_odd(const mpz_t *z); |
| 95 | bool mpz_is_even(const mpz_t *z); |
| 96 | |
Damien George | 42f3de9 | 2014-10-03 17:44:14 +0000 | [diff] [blame] | 97 | int mpz_cmp(const mpz_t *lhs, const mpz_t *rhs); |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 98 | |
| 99 | mpz_t *mpz_abs(const mpz_t *z); |
| 100 | mpz_t *mpz_neg(const mpz_t *z); |
| 101 | mpz_t *mpz_add(const mpz_t *lhs, const mpz_t *rhs); |
| 102 | mpz_t *mpz_sub(const mpz_t *lhs, const mpz_t *rhs); |
| 103 | mpz_t *mpz_mul(const mpz_t *lhs, const mpz_t *rhs); |
| 104 | mpz_t *mpz_pow(const mpz_t *lhs, const mpz_t *rhs); |
| 105 | |
| 106 | void mpz_abs_inpl(mpz_t *dest, const mpz_t *z); |
| 107 | void mpz_neg_inpl(mpz_t *dest, const mpz_t *z); |
Damien George | 06201ff | 2014-03-01 19:50:50 +0000 | [diff] [blame] | 108 | void mpz_not_inpl(mpz_t *dest, const mpz_t *z); |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 109 | void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, mp_int_t rhs); |
| 110 | void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, mp_int_t rhs); |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 111 | void mpz_add_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs); |
| 112 | void mpz_sub_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs); |
| 113 | void mpz_mul_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs); |
| 114 | void mpz_pow_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs); |
Paul Sokolovsky | 57207b8 | 2014-03-23 01:52:36 +0200 | [diff] [blame] | 115 | void mpz_and_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs); |
| 116 | void mpz_or_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs); |
| 117 | void mpz_xor_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs); |
Damien George | 438c88d | 2014-02-22 19:25:23 +0000 | [diff] [blame] | 118 | |
| 119 | mpz_t *mpz_gcd(const mpz_t *z1, const mpz_t *z2); |
| 120 | mpz_t *mpz_lcm(const mpz_t *z1, const mpz_t *z2); |
| 121 | void mpz_divmod(const mpz_t *lhs, const mpz_t *rhs, mpz_t **quo, mpz_t **rem); |
| 122 | void mpz_divmod_inpl(mpz_t *dest_quo, mpz_t *dest_rem, const mpz_t *lhs, const mpz_t *rhs); |
| 123 | mpz_t *mpz_div(const mpz_t *lhs, const mpz_t *rhs); |
| 124 | mpz_t *mpz_mod(const mpz_t *lhs, const mpz_t *rhs); |
| 125 | |
Damien George | ffe911d | 2014-07-24 14:21:37 +0100 | [diff] [blame] | 126 | mp_int_t mpz_hash(const mpz_t *z); |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 127 | bool mpz_as_int_checked(const mpz_t *z, mp_int_t *value); |
Damien George | c9aa58e | 2014-07-31 13:41:43 +0000 | [diff] [blame] | 128 | bool mpz_as_uint_checked(const mpz_t *z, mp_uint_t *value); |
Damien George | fb510b3 | 2014-06-01 13:32:54 +0100 | [diff] [blame] | 129 | #if MICROPY_PY_BUILTINS_FLOAT |
Damien George | 5260810 | 2014-03-08 15:04:54 +0000 | [diff] [blame] | 130 | mp_float_t mpz_as_float(const mpz_t *z); |
| 131 | #endif |
Damien George | afb1cf7 | 2014-09-05 20:37:06 +0100 | [diff] [blame] | 132 | mp_uint_t mpz_as_str_size(const mpz_t *i, mp_uint_t base, const char *prefix, char comma); |
| 133 | mp_uint_t mpz_as_str_inpl(const mpz_t *z, mp_uint_t base, const char *prefix, char base_char, char comma, char *str); |