blob: 654be21c0974f9bd0bc935b1b6821027efa96853 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
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 George438c88d2014-02-22 19:25:23 +000027typedef uint16_t mpz_dig_t;
28typedef uint32_t mpz_dbl_dig_t;
29typedef int32_t mpz_dbl_dig_signed_t;
30
31typedef struct _mpz_t {
Damien George51047752014-02-26 17:40:52 +000032 machine_uint_t neg : 1;
Damien George06201ff2014-03-01 19:50:50 +000033 machine_uint_t fixed_dig : 1;
34 machine_uint_t alloc : 30;
Damien George438c88d2014-02-22 19:25:23 +000035 machine_uint_t len;
36 mpz_dig_t *dig;
37} mpz_t;
38
Damien George06201ff2014-03-01 19:50:50 +000039#define MPZ_DIG_SIZE (15) // see mpn_div for why this needs to be at most 15
40#define MPZ_NUM_DIG_FOR_INT (sizeof(machine_int_t) * 8 / MPZ_DIG_SIZE + 1)
Damien Georgebb4a43f2014-03-12 15:36:06 +000041#define MPZ_NUM_DIG_FOR_LL (sizeof(long long) * 8 / MPZ_DIG_SIZE + 1)
Damien George06201ff2014-03-01 19:50:50 +000042
43// convenience macro to declare an mpz with a digit array from the stack, initialised by an integer
44#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 George438c88d2014-02-22 19:25:23 +000045
46void mpz_init_zero(mpz_t *z);
47void mpz_init_from_int(mpz_t *z, machine_int_t val);
Damien George06201ff2014-03-01 19:50:50 +000048void mpz_init_fixed_from_int(mpz_t *z, mpz_dig_t *dig, uint dig_alloc, machine_int_t val);
Damien George438c88d2014-02-22 19:25:23 +000049void mpz_deinit(mpz_t *z);
50
51mpz_t *mpz_zero();
52mpz_t *mpz_from_int(machine_int_t i);
Damien Georgebb4a43f2014-03-12 15:36:06 +000053mpz_t *mpz_from_ll(long long i);
Damien George438c88d2014-02-22 19:25:23 +000054mpz_t *mpz_from_str(const char *str, uint len, bool neg, uint base);
55void mpz_free(mpz_t *z);
56
57mpz_t *mpz_clone(const mpz_t *src);
58
59void mpz_set(mpz_t *dest, const mpz_t *src);
60void mpz_set_from_int(mpz_t *z, machine_int_t src);
Damien Georgebb4a43f2014-03-12 15:36:06 +000061void mpz_set_from_ll(mpz_t *z, long long i);
Damien George438c88d2014-02-22 19:25:23 +000062uint mpz_set_from_str(mpz_t *z, const char *str, uint len, bool neg, uint base);
63
64bool mpz_is_zero(const mpz_t *z);
65bool mpz_is_pos(const mpz_t *z);
66bool mpz_is_neg(const mpz_t *z);
67bool mpz_is_odd(const mpz_t *z);
68bool mpz_is_even(const mpz_t *z);
69
70int mpz_cmp(const mpz_t *lhs, const mpz_t *rhs);
Damien George438c88d2014-02-22 19:25:23 +000071
72mpz_t *mpz_abs(const mpz_t *z);
73mpz_t *mpz_neg(const mpz_t *z);
74mpz_t *mpz_add(const mpz_t *lhs, const mpz_t *rhs);
75mpz_t *mpz_sub(const mpz_t *lhs, const mpz_t *rhs);
76mpz_t *mpz_mul(const mpz_t *lhs, const mpz_t *rhs);
77mpz_t *mpz_pow(const mpz_t *lhs, const mpz_t *rhs);
78
79void mpz_abs_inpl(mpz_t *dest, const mpz_t *z);
80void mpz_neg_inpl(mpz_t *dest, const mpz_t *z);
Damien George06201ff2014-03-01 19:50:50 +000081void mpz_not_inpl(mpz_t *dest, const mpz_t *z);
82void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, machine_int_t rhs);
83void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, machine_int_t rhs);
Damien George438c88d2014-02-22 19:25:23 +000084void mpz_add_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
85void mpz_sub_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
86void mpz_mul_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
87void mpz_pow_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
Paul Sokolovsky57207b82014-03-23 01:52:36 +020088void mpz_and_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
89void mpz_or_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
90void mpz_xor_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
Damien George438c88d2014-02-22 19:25:23 +000091
92mpz_t *mpz_gcd(const mpz_t *z1, const mpz_t *z2);
93mpz_t *mpz_lcm(const mpz_t *z1, const mpz_t *z2);
94void mpz_divmod(const mpz_t *lhs, const mpz_t *rhs, mpz_t **quo, mpz_t **rem);
95void mpz_divmod_inpl(mpz_t *dest_quo, mpz_t *dest_rem, const mpz_t *lhs, const mpz_t *rhs);
96mpz_t *mpz_div(const mpz_t *lhs, const mpz_t *rhs);
97mpz_t *mpz_mod(const mpz_t *lhs, const mpz_t *rhs);
98
Damien Georgeaca14122014-02-24 21:32:52 +000099machine_int_t mpz_as_int(const mpz_t *z);
Damien George8270e382014-04-03 11:00:54 +0000100bool mpz_as_int_checked(const mpz_t *z, machine_int_t *value);
Damien George52608102014-03-08 15:04:54 +0000101#if MICROPY_ENABLE_FLOAT
102mp_float_t mpz_as_float(const mpz_t *z);
103#endif
Damien George438c88d2014-02-22 19:25:23 +0000104uint mpz_as_str_size(const mpz_t *z, uint base);
Dave Hylandsc4029e52014-04-07 11:19:51 -0700105uint mpz_as_str_size_formatted(const mpz_t *i, uint base, const char *prefix, char comma);
Damien George438c88d2014-02-22 19:25:23 +0000106char *mpz_as_str(const mpz_t *z, uint base);
Dave Hylandsc4029e52014-04-07 11:19:51 -0700107uint mpz_as_str_inpl(const mpz_t *z, uint base, const char *prefix, char base_char, char comma, char *str);