blob: b2dd88acf9ed929b61c487a9519c93998ebbf1c6 [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 */
Damien George51dfcb42015-01-01 20:27:54 +000026#ifndef __MICROPY_INCLUDED_PY_MPZ_H__
27#define __MICROPY_INCLUDED_PY_MPZ_H__
28
29#include <stdint.h>
30
31#include "py/mpconfig.h"
32#include "py/misc.h"
Damien George04b91472014-05-03 23:27:38 +010033
Damien George9a21d2e2014-09-06 17:15:34 +010034// This mpz module implements arbitrary precision integers.
35//
36// The storage for each digit is defined by mpz_dig_t. The actual number of
37// bits in mpz_dig_t that are used is defined by MPZ_DIG_SIZE. The machine must
38// also provide a type that is twice as wide as mpz_dig_t, in both signed and
39// unsigned versions.
40//
41// MPZ_DIG_SIZE can be between 4 and 8*sizeof(mpz_dig_t), but it makes most
Damien George3f327cc2015-03-28 21:45:57 +000042// sense to have it as large as possible. If MPZ_DIG_SIZE is not already
43// defined then it is auto-detected below, depending on the machine. The types
44// are then set based on the value of MPZ_DIG_SIZE (although they can be freely
45// changed so long as the constraints mentioned above are met).
Damien George9a21d2e2014-09-06 17:15:34 +010046
Damien George3f327cc2015-03-28 21:45:57 +000047#ifndef MPZ_DIG_SIZE
48 #if defined(__x86_64__) || defined(_WIN64)
49 // 64-bit machine, using 32-bit storage for digits
50 #define MPZ_DIG_SIZE (32)
51 #else
52 // default: 32-bit machine, using 16-bit storage for digits
53 #define MPZ_DIG_SIZE (16)
54 #endif
55#endif
56
57#if MPZ_DIG_SIZE > 16
Damien George9a21d2e2014-09-06 17:15:34 +010058typedef uint32_t mpz_dig_t;
59typedef uint64_t mpz_dbl_dig_t;
60typedef int64_t mpz_dbl_dig_signed_t;
Damien George3f327cc2015-03-28 21:45:57 +000061#elif MPZ_DIG_SIZE > 8
Damien George438c88d2014-02-22 19:25:23 +000062typedef uint16_t mpz_dig_t;
63typedef uint32_t mpz_dbl_dig_t;
64typedef int32_t mpz_dbl_dig_signed_t;
Damien George3f327cc2015-03-28 21:45:57 +000065#elif MPZ_DIG_SIZE > 4
66typedef uint8_t mpz_dig_t;
67typedef uint16_t mpz_dbl_dig_t;
68typedef int16_t mpz_dbl_dig_signed_t;
69#else
70typedef uint8_t mpz_dig_t;
71typedef uint8_t mpz_dbl_dig_t;
72typedef int8_t mpz_dbl_dig_signed_t;
Damien George9a21d2e2014-09-06 17:15:34 +010073#endif
74
stijn0e557fa2014-10-30 14:39:22 +010075#ifdef _WIN64
76 #define MPZ_LONG_1 1i64
77#else
78 #define MPZ_LONG_1 1L
79#endif
80
Damien George8b4fb4f2015-09-15 16:15:57 +010081// these define the maximum storage needed to hold an int or long long
82#define MPZ_NUM_DIG_FOR_INT ((sizeof(mp_int_t) * 8 + MPZ_DIG_SIZE - 1) / MPZ_DIG_SIZE)
83#define MPZ_NUM_DIG_FOR_LL ((sizeof(long long) * 8 + MPZ_DIG_SIZE - 1) / MPZ_DIG_SIZE)
Damien George438c88d2014-02-22 19:25:23 +000084
85typedef struct _mpz_t {
Damien George40f3c022014-07-03 13:25:24 +010086 mp_uint_t neg : 1;
87 mp_uint_t fixed_dig : 1;
Damien George5c670ac2015-01-24 23:12:58 +000088 mp_uint_t alloc : BITS_PER_WORD - 2;
Damien George40f3c022014-07-03 13:25:24 +010089 mp_uint_t len;
Damien George438c88d2014-02-22 19:25:23 +000090 mpz_dig_t *dig;
91} mpz_t;
92
Damien George06201ff2014-03-01 19:50:50 +000093// convenience macro to declare an mpz with a digit array from the stack, initialised by an integer
94#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 +000095
96void mpz_init_zero(mpz_t *z);
Damien George40f3c022014-07-03 13:25:24 +010097void mpz_init_from_int(mpz_t *z, mp_int_t val);
Damien Georgeafb1cf72014-09-05 20:37:06 +010098void mpz_init_fixed_from_int(mpz_t *z, mpz_dig_t *dig, mp_uint_t dig_alloc, mp_int_t val);
Damien George438c88d2014-02-22 19:25:23 +000099void mpz_deinit(mpz_t *z);
100
Damien George438c88d2014-02-22 19:25:23 +0000101void mpz_set(mpz_t *dest, const mpz_t *src);
Damien George40f3c022014-07-03 13:25:24 +0100102void mpz_set_from_int(mpz_t *z, mp_int_t src);
Damien George95307432014-09-10 22:10:33 +0100103void mpz_set_from_ll(mpz_t *z, long long i, bool is_signed);
David Steinberg6e0b6d02015-01-02 12:39:22 +0000104#if MICROPY_PY_BUILTINS_FLOAT
105void mpz_set_from_float(mpz_t *z, mp_float_t src);
106#endif
Damien Georgeafb1cf72014-09-05 20:37:06 +0100107mp_uint_t mpz_set_from_str(mpz_t *z, const char *str, mp_uint_t len, bool neg, mp_uint_t base);
Damien George438c88d2014-02-22 19:25:23 +0000108
109bool mpz_is_zero(const mpz_t *z);
Damien George42f3de92014-10-03 17:44:14 +0000110int mpz_cmp(const mpz_t *lhs, const mpz_t *rhs);
Damien George438c88d2014-02-22 19:25:23 +0000111
Damien George438c88d2014-02-22 19:25:23 +0000112void mpz_abs_inpl(mpz_t *dest, const mpz_t *z);
113void mpz_neg_inpl(mpz_t *dest, const mpz_t *z);
Damien George06201ff2014-03-01 19:50:50 +0000114void mpz_not_inpl(mpz_t *dest, const mpz_t *z);
Damien George2f4e8512015-10-01 18:01:37 +0100115void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, mp_uint_t rhs);
116void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, mp_uint_t rhs);
Damien George438c88d2014-02-22 19:25:23 +0000117void mpz_add_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
118void mpz_sub_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
119void mpz_mul_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
120void mpz_pow_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
Paul Sokolovsky57207b82014-03-23 01:52:36 +0200121void mpz_and_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
122void mpz_or_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
123void mpz_xor_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
Damien George438c88d2014-02-22 19:25:23 +0000124void mpz_divmod_inpl(mpz_t *dest_quo, mpz_t *dest_rem, const mpz_t *lhs, const mpz_t *rhs);
Damien George438c88d2014-02-22 19:25:23 +0000125
Damien Georgeffe911d2014-07-24 14:21:37 +0100126mp_int_t mpz_hash(const mpz_t *z);
Damien George40f3c022014-07-03 13:25:24 +0100127bool mpz_as_int_checked(const mpz_t *z, mp_int_t *value);
Damien Georgec9aa58e2014-07-31 13:41:43 +0000128bool mpz_as_uint_checked(const mpz_t *z, mp_uint_t *value);
Damien George271d18e2015-04-25 23:16:39 +0100129void mpz_as_bytes(const mpz_t *z, bool big_endian, mp_uint_t len, byte *buf);
Damien Georgefb510b32014-06-01 13:32:54 +0100130#if MICROPY_PY_BUILTINS_FLOAT
Damien George52608102014-03-08 15:04:54 +0000131mp_float_t mpz_as_float(const mpz_t *z);
132#endif
Damien Georgeafb1cf72014-09-05 20:37:06 +0100133mp_uint_t mpz_as_str_size(const mpz_t *i, mp_uint_t base, const char *prefix, char comma);
134mp_uint_t mpz_as_str_inpl(const mpz_t *z, mp_uint_t base, const char *prefix, char base_char, char comma, char *str);
Damien George51dfcb42015-01-01 20:27:54 +0000135
136#endif // __MICROPY_INCLUDED_PY_MPZ_H__