Numworks Epsilon  1.4.1
Graphing Calculator Operating System
mpz.h
Go to the documentation of this file.
1 /*
2  * This file is part of the MicroPython 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 #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"
33 
34 // 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
42 // 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).
46 
47 #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
58 typedef uint32_t mpz_dig_t;
59 typedef uint64_t mpz_dbl_dig_t;
61 #elif MPZ_DIG_SIZE > 8
65 #elif MPZ_DIG_SIZE > 4
66 typedef uint8_t mpz_dig_t;
67 typedef uint16_t mpz_dbl_dig_t;
69 #else
70 typedef uint8_t mpz_dig_t;
71 typedef uint8_t mpz_dbl_dig_t;
73 #endif
74 
75 #ifdef _WIN64
76  #ifdef __MINGW32__
77  #define MPZ_LONG_1 1LL
78  #else
79  #define MPZ_LONG_1 1i64
80  #endif
81 #else
82  #define MPZ_LONG_1 1L
83 #endif
84 
85 // these define the maximum storage needed to hold an int or long long
86 #define MPZ_NUM_DIG_FOR_INT ((sizeof(mp_int_t) * 8 + MPZ_DIG_SIZE - 1) / MPZ_DIG_SIZE)
87 #define MPZ_NUM_DIG_FOR_LL ((sizeof(long long) * 8 + MPZ_DIG_SIZE - 1) / MPZ_DIG_SIZE)
88 
89 typedef struct _mpz_t {
90  size_t neg : 1;
91  size_t fixed_dig : 1;
92  size_t alloc : 8 * sizeof(size_t) - 2;
93  size_t len;
95 } mpz_t;
96 
97 // convenience macro to declare an mpz with a digit array from the stack, initialised by an integer
98 #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);
99 
100 void mpz_init_zero(mpz_t *z);
101 void mpz_init_from_int(mpz_t *z, mp_int_t val);
102 void mpz_init_fixed_from_int(mpz_t *z, mpz_dig_t *dig, size_t dig_alloc, mp_int_t val);
103 void mpz_deinit(mpz_t *z);
104 
105 void mpz_set(mpz_t *dest, const mpz_t *src);
106 void mpz_set_from_int(mpz_t *z, mp_int_t src);
107 void mpz_set_from_ll(mpz_t *z, long long i, bool is_signed);
108 #if MICROPY_PY_BUILTINS_FLOAT
109 void mpz_set_from_float(mpz_t *z, mp_float_t src);
110 #endif
111 size_t mpz_set_from_str(mpz_t *z, const char *str, size_t len, bool neg, unsigned int base);
112 void mpz_set_from_bytes(mpz_t *z, bool big_endian, size_t len, const byte *buf);
113 
114 static inline bool mpz_is_zero(const mpz_t *z) { return z->len == 0; }
115 static inline bool mpz_is_neg(const mpz_t *z) { return z->len != 0 && z->neg != 0; }
116 int mpz_cmp(const mpz_t *lhs, const mpz_t *rhs);
117 
118 void mpz_abs_inpl(mpz_t *dest, const mpz_t *z);
119 void mpz_neg_inpl(mpz_t *dest, const mpz_t *z);
120 void mpz_not_inpl(mpz_t *dest, const mpz_t *z);
121 void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, mp_uint_t rhs);
122 void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, mp_uint_t rhs);
123 void mpz_add_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
124 void mpz_sub_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
125 void mpz_mul_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
126 void mpz_pow_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
127 void mpz_pow3_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs, const mpz_t *mod);
128 void mpz_and_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
129 void mpz_or_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
130 void mpz_xor_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs);
131 void mpz_divmod_inpl(mpz_t *dest_quo, mpz_t *dest_rem, const mpz_t *lhs, const mpz_t *rhs);
132 
133 static inline size_t mpz_max_num_bits(const mpz_t *z) { return z->len * MPZ_DIG_SIZE; }
134 mp_int_t mpz_hash(const mpz_t *z);
135 bool mpz_as_int_checked(const mpz_t *z, mp_int_t *value);
136 bool mpz_as_uint_checked(const mpz_t *z, mp_uint_t *value);
137 void mpz_as_bytes(const mpz_t *z, bool big_endian, size_t len, byte *buf);
138 #if MICROPY_PY_BUILTINS_FLOAT
139 mp_float_t mpz_as_float(const mpz_t *z);
140 #endif
141 size_t mpz_as_str_inpl(const mpz_t *z, unsigned int base, const char *prefix, char base_char, char comma, char *str);
142 
143 #endif // MICROPY_INCLUDED_PY_MPZ_H
struct _mpz_t mpz_t
intptr_t mp_int_t
Definition: mpconfigport.h:73
uintptr_t mp_uint_t
Definition: mpconfigport.h:74
void mpz_pow_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
Definition: mpz.c:1358
void mpz_abs_inpl(mpz_t *dest, const mpz_t *z)
Definition: mpz.c:1076
int mpz_cmp(const mpz_t *lhs, const mpz_t *rhs)
Definition: mpz.c:958
void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, mp_uint_t rhs)
Definition: mpz.c:1120
void mpz_neg_inpl(mpz_t *dest, const mpz_t *z)
Definition: mpz.c:1086
unsigned int size_t
Definition: stddef.h:7
bool mpz_as_int_checked(const mpz_t *z, mp_int_t *value)
Definition: mpz.c:1576
size_t len
Definition: mpz.h:93
unsigned short uint16_t
Definition: stdint.h:5
void mpz_xor_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
Definition: mpz.c:1288
void mpz_and_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
Definition: mpz.c:1220
unsigned char uint8_t
Definition: stdint.h:4
void mpz_init_from_int(mpz_t *z, mp_int_t val)
Definition: mpz.c:656
Definition: mpz.h:89
mpz_dig_t * dig
Definition: mpz.h:94
uint16_t mpz_dig_t
Definition: mpz.h:62
void mpz_or_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
Definition: mpz.c:1254
size_t mpz_as_str_inpl(const mpz_t *z, unsigned int base, const char *prefix, char base_char, char comma, char *str)
Definition: mpz.c:1679
unsigned int uint32_t
Definition: stdint.h:6
mp_int_t mpz_hash(const mpz_t *z)
Definition: mpz.c:1561
signed short int16_t
Definition: stdint.h:10
void mpz_set_from_int(mpz_t *z, mp_int_t src)
Definition: mpz.c:758
unsigned long long uint64_t
Definition: stdint.h:7
#define MPZ_DIG_SIZE
Definition: mpz.h:53
int32_t mpz_dbl_dig_signed_t
Definition: mpz.h:64
size_t mpz_set_from_str(mpz_t *z, const char *str, size_t len, bool neg, unsigned int base)
Definition: mpz.c:873
void mpz_sub_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
Definition: mpz.c:1192
void mpz_set_from_ll(mpz_t *z, long long i, bool is_signed)
Definition: mpz.c:782
signed char int8_t
Definition: stdint.h:9
void mpz_deinit(mpz_t *z)
Definition: mpz.c:670
#define is_signed(typecode)
Definition: binary.c:186
unsigned char byte
Definition: misc.h:37
size_t alloc
Definition: mpz.h:92
void mpz_set(mpz_t *dest, const mpz_t *src)
Definition: mpz.c:751
size_t fixed_dig
Definition: mpz.h:91
void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, mp_uint_t rhs)
Definition: mpz.c:1133
void mpz_mul_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
Definition: mpz.c:1326
void mpz_add_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs)
Definition: mpz.c:1171
uint32_t mpz_dbl_dig_t
Definition: mpz.h:63
signed long long int64_t
Definition: stdint.h:12
void mpz_init_fixed_from_int(mpz_t *z, mpz_dig_t *dig, size_t dig_alloc, mp_int_t val)
Definition: mpz.c:661
void mpz_divmod_inpl(mpz_t *dest_quo, mpz_t *dest_rem, const mpz_t *lhs, const mpz_t *rhs)
Definition: mpz.c:1513
void mpz_pow3_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs, const mpz_t *mod)
Definition: mpz.c:1392
signed int int32_t
Definition: stdint.h:11
bool mpz_as_uint_checked(const mpz_t *z, mp_uint_t *value)
Definition: mpz.c:1596
void mpz_as_bytes(const mpz_t *z, bool big_endian, size_t len, byte *buf)
Definition: mpz.c:1618
size_t neg
Definition: mpz.h:90
void mpz_not_inpl(mpz_t *dest, const mpz_t *z)
Definition: mpz.c:1096
void mpz_init_zero(mpz_t *z)
Definition: mpz.c:648
void mpz_set_from_bytes(mpz_t *z, bool big_endian, size_t len, const byte *buf)
Definition: mpz.c:909