blob: cbe60eb8d0571a56951382e6838ed7a4fd227748 [file] [log] [blame]
Damien George438c88d2014-02-22 19:25:23 +00001typedef uint16_t mpz_dig_t;
2typedef uint32_t mpz_dbl_dig_t;
3typedef int32_t mpz_dbl_dig_signed_t;
4
5typedef struct _mpz_t {
Damien George51047752014-02-26 17:40:52 +00006 machine_uint_t neg : 1;
Damien George06201ff2014-03-01 19:50:50 +00007 machine_uint_t fixed_dig : 1;
8 machine_uint_t alloc : 30;
Damien George438c88d2014-02-22 19:25:23 +00009 machine_uint_t len;
10 mpz_dig_t *dig;
11} mpz_t;
12
Damien George06201ff2014-03-01 19:50:50 +000013#define MPZ_DIG_SIZE (15) // see mpn_div for why this needs to be at most 15
14#define MPZ_NUM_DIG_FOR_INT (sizeof(machine_int_t) * 8 / MPZ_DIG_SIZE + 1)
Damien Georgebb4a43f2014-03-12 15:36:06 +000015#define MPZ_NUM_DIG_FOR_LL (sizeof(long long) * 8 / MPZ_DIG_SIZE + 1)
Damien George06201ff2014-03-01 19:50:50 +000016
17// convenience macro to declare an mpz with a digit array from the stack, initialised by an integer
18#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 +000019
20void mpz_init_zero(mpz_t *z);
21void mpz_init_from_int(mpz_t *z, machine_int_t val);
Damien George06201ff2014-03-01 19:50:50 +000022void 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 +000023void mpz_deinit(mpz_t *z);
24
25mpz_t *mpz_zero();
26mpz_t *mpz_from_int(machine_int_t i);
Damien Georgebb4a43f2014-03-12 15:36:06 +000027mpz_t *mpz_from_ll(long long i);
Damien George438c88d2014-02-22 19:25:23 +000028mpz_t *mpz_from_str(const char *str, uint len, bool neg, uint base);
29void mpz_free(mpz_t *z);
30
31mpz_t *mpz_clone(const mpz_t *src);
32
33void mpz_set(mpz_t *dest, const mpz_t *src);
34void mpz_set_from_int(mpz_t *z, machine_int_t src);
Damien Georgebb4a43f2014-03-12 15:36:06 +000035void mpz_set_from_ll(mpz_t *z, long long i);
Damien George438c88d2014-02-22 19:25:23 +000036uint mpz_set_from_str(mpz_t *z, const char *str, uint len, bool neg, uint base);
37
38bool mpz_is_zero(const mpz_t *z);
39bool mpz_is_pos(const mpz_t *z);
40bool mpz_is_neg(const mpz_t *z);
41bool mpz_is_odd(const mpz_t *z);
42bool mpz_is_even(const mpz_t *z);
43
44int mpz_cmp(const mpz_t *lhs, const mpz_t *rhs);
Damien George438c88d2014-02-22 19:25:23 +000045
46mpz_t *mpz_abs(const mpz_t *z);
47mpz_t *mpz_neg(const mpz_t *z);
48mpz_t *mpz_add(const mpz_t *lhs, const mpz_t *rhs);
49mpz_t *mpz_sub(const mpz_t *lhs, const mpz_t *rhs);
50mpz_t *mpz_mul(const mpz_t *lhs, const mpz_t *rhs);
51mpz_t *mpz_pow(const mpz_t *lhs, const mpz_t *rhs);
52
53void mpz_abs_inpl(mpz_t *dest, const mpz_t *z);
54void mpz_neg_inpl(mpz_t *dest, const mpz_t *z);
Damien George06201ff2014-03-01 19:50:50 +000055void mpz_not_inpl(mpz_t *dest, const mpz_t *z);
56void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, machine_int_t rhs);
57void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, machine_int_t rhs);
Damien George438c88d2014-02-22 19:25:23 +000058void mpz_add_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
59void mpz_sub_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
60void mpz_mul_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
61void mpz_pow_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
Paul Sokolovsky57207b82014-03-23 01:52:36 +020062void mpz_and_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
63void mpz_or_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
64void mpz_xor_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
Damien George438c88d2014-02-22 19:25:23 +000065
66mpz_t *mpz_gcd(const mpz_t *z1, const mpz_t *z2);
67mpz_t *mpz_lcm(const mpz_t *z1, const mpz_t *z2);
68void mpz_divmod(const mpz_t *lhs, const mpz_t *rhs, mpz_t **quo, mpz_t **rem);
69void mpz_divmod_inpl(mpz_t *dest_quo, mpz_t *dest_rem, const mpz_t *lhs, const mpz_t *rhs);
70mpz_t *mpz_div(const mpz_t *lhs, const mpz_t *rhs);
71mpz_t *mpz_mod(const mpz_t *lhs, const mpz_t *rhs);
72
Damien Georgeaca14122014-02-24 21:32:52 +000073machine_int_t mpz_as_int(const mpz_t *z);
Damien George8270e382014-04-03 11:00:54 +000074bool mpz_as_int_checked(const mpz_t *z, machine_int_t *value);
Damien George52608102014-03-08 15:04:54 +000075#if MICROPY_ENABLE_FLOAT
76mp_float_t mpz_as_float(const mpz_t *z);
77#endif
Damien George438c88d2014-02-22 19:25:23 +000078uint mpz_as_str_size(const mpz_t *z, uint base);
79char *mpz_as_str(const mpz_t *z, uint base);
80uint mpz_as_str_inpl(const mpz_t *z, uint base, char *str);