xref: /llvm-project/polly/lib/External/isl/imath/doc.md (revision 658eb9e14264d48888ade0e3daf0b648f76c3f0e)
1*658eb9e1SMichael Kruse<!--
2*658eb9e1SMichael Kruse  This file was generated from "doc.md.in" by mkdoc.py
3*658eb9e1SMichael Kruse  DO NOT EDIT
4*658eb9e1SMichael Kruse-->
5*658eb9e1SMichael Kruse
6*658eb9e1SMichael Kruse# User Documentation for the IMath Library
7*658eb9e1SMichael Kruse
8*658eb9e1SMichael KruseAuthor: [M. J. Fromberger](https://github.com/creachadair)
9*658eb9e1SMichael Kruse
10*658eb9e1SMichael Kruse## Installation
11*658eb9e1SMichael Kruse
12*658eb9e1SMichael Kruse1. Edit Makefile to select compiler and options.  The default is to use gcc.
13*658eb9e1SMichael Kruse   You may want to change CC to `clang` instead of `gcc` (and on macOS that
14*658eb9e1SMichael Kruse   what you will get anyway), but you should be able to use the default GCC
15*658eb9e1SMichael Kruse   settings for either.
16*658eb9e1SMichael Kruse
17*658eb9e1SMichael Kruse   By default, the Makefile assumes you can use 64-bit integer types, even
18*658eb9e1SMichael Kruse   though they were not standard in ANSI C90. If you cannot, add
19*658eb9e1SMichael Kruse   `-DUSE_32BIT_WORDS` to the compiler options.
20*658eb9e1SMichael Kruse
21*658eb9e1SMichael Kruse2. Type `make` or `make test` to build the test driver and run the unit tests.
22*658eb9e1SMichael Kruse   None of these should fail.  If they do, see below for how you can report
23*658eb9e1SMichael Kruse   bugs.
24*658eb9e1SMichael Kruse
25*658eb9e1SMichael Kruse   To build with debugging enabled (and optimization disabled), run `make
26*658eb9e1SMichael Kruse   DEBUG=Y`.  This sets the preprocessor macro `DEBUG` to 1, and several other
27*658eb9e1SMichael Kruse   things (see Makefile for details).
28*658eb9e1SMichael Kruse
29*658eb9e1SMichael KruseTo use the library in your code, include "imath.h" wherever you intend to use
30*658eb9e1SMichael Krusethe library's routines.  The integer library is just a single source file, so
31*658eb9e1SMichael Kruseyou can compile it into your project in whatever way makes sense.  If you wish
32*658eb9e1SMichael Kruseto use rational arithmetic, you will also need to include "imrat.h".
33*658eb9e1SMichael Kruse
34*658eb9e1SMichael Kruse## Background
35*658eb9e1SMichael Kruse
36*658eb9e1SMichael KruseThe basic types defined by the imath library are `mpz_t`, an arbitrary
37*658eb9e1SMichael Kruseprecision signed integer, and `mpq_t`, an arbitrary precision signed rational
38*658eb9e1SMichael Krusenumber.  The type `mp_int` is a pointer to an `mpz_t`, and `mp_rat` is a
39*658eb9e1SMichael Krusepointer to an `mpq_t`.
40*658eb9e1SMichael Kruse
41*658eb9e1SMichael KruseMost of the functions in the imath library return a value of type `mp_result`.
42*658eb9e1SMichael KruseThis is a signed integer type which can be used to convey status information
43*658eb9e1SMichael Kruseand also return small values.  Any negative value is considered to be a status
44*658eb9e1SMichael Krusemessage.  The following constants are defined for processing these:
45*658eb9e1SMichael Kruse
46*658eb9e1SMichael Kruse| Status      | Description                                  |
47*658eb9e1SMichael Kruse| ----------- | -------------------------------------------- |
48*658eb9e1SMichael Kruse| `MP_OK`     | operation successful, all is well (= 0)      |
49*658eb9e1SMichael Kruse| `MP_FALSE`  | boolean false (= `MP_OK`)                    |
50*658eb9e1SMichael Kruse| `MP_TRUE`   | boolean true                                 |
51*658eb9e1SMichael Kruse| `MP_MEMORY` | out of memory                                |
52*658eb9e1SMichael Kruse| `MP_RANGE`  | parameter out of range                       |
53*658eb9e1SMichael Kruse| `MP_UNDEF`  | result is undefined (e.g., division by zero) |
54*658eb9e1SMichael Kruse| `MP_TRUNC`  | output value was truncated                   |
55*658eb9e1SMichael Kruse| `MP_BADARG` | an invalid parameter was passed              |
56*658eb9e1SMichael Kruse
57*658eb9e1SMichael KruseIf you obtain a zero or negative value of an `mp_result`, you can use the
58*658eb9e1SMichael Kruse`mp_error_string()` routine to obtain a pointer to a brief human-readable
59*658eb9e1SMichael Krusestring describing the error.  These strings are statically allocated, so they
60*658eb9e1SMichael Kruseneed not be freed by the caller; the same strings are re-used from call to
61*658eb9e1SMichael Krusecall.
62*658eb9e1SMichael Kruse
63*658eb9e1SMichael KruseUnless otherwise noted, it is legal to use the same parameter for both inputs
64*658eb9e1SMichael Kruseand output with most of the functions in this library.  For example, you can
65*658eb9e1SMichael Kruseadd a number to itself and replace the original by writing:
66*658eb9e1SMichael Kruse
67*658eb9e1SMichael Kruse    mp_int_add(a, a, a);  /* a = a + a */
68*658eb9e1SMichael Kruse
69*658eb9e1SMichael KruseAny cases in which this is not legal will be noted in the function summaries
70*658eb9e1SMichael Krusebelow (if you discover that this is not so, please report it as a bug; I will
71*658eb9e1SMichael Krusefix either the function or the documentation :)
72*658eb9e1SMichael Kruse
73*658eb9e1SMichael Kruse## The IMath API
74*658eb9e1SMichael Kruse
75*658eb9e1SMichael KruseEach of the API functions is documented here.  The general format of the
76*658eb9e1SMichael Kruseentries is:
77*658eb9e1SMichael Kruse
78*658eb9e1SMichael Kruse> ------------
79*658eb9e1SMichael Kruse> <pre>
80*658eb9e1SMichael Kruse> return_type function_name(parameters ...)
81*658eb9e1SMichael Kruse> </pre>
82*658eb9e1SMichael Kruse>  -  English description.
83*658eb9e1SMichael Kruse
84*658eb9e1SMichael KruseUnless otherwise noted, any API function that returns `mp_result` may be
85*658eb9e1SMichael Kruseexpected to return `MP_OK`, `MP_BADARG`, or `MP_MEMORY`.  Other return values
86*658eb9e1SMichael Kruseshould be documented in the description.  Please let me know if you discover
87*658eb9e1SMichael Krusethis is not the case.
88*658eb9e1SMichael Kruse
89*658eb9e1SMichael KruseThe following macros are defined in "imath.h", to define the sizes of the
90*658eb9e1SMichael Krusevarious data types used in the library:
91*658eb9e1SMichael Kruse
92*658eb9e1SMichael Kruse| Constant        | Description
93*658eb9e1SMichael Kruse| --------------- | ----------------------------------------
94*658eb9e1SMichael Kruse| `MP_DIGIT_BIT`  | the number of bits in a single `mpz_t` digit.
95*658eb9e1SMichael Kruse| `MP_WORD_BIT`   | the number of bits in a `mpz_t` word.
96*658eb9e1SMichael Kruse| `MP_SMALL_MIN`  | the minimum value representable by an `mp_small`.
97*658eb9e1SMichael Kruse| `MP_SMALL_MAX`  | the maximum value representable by an `mp_small`.
98*658eb9e1SMichael Kruse| `MP_USMALL_MAX` | the maximum value representable by an `mp_usmall`.
99*658eb9e1SMichael Kruse| `MP_MIN_RADIX`  | the minimum radix accepted for base conversion.
100*658eb9e1SMichael Kruse| `MP_MAX_RADIX`  | the maximum radix accepted for base conversion.
101*658eb9e1SMichael Kruse
102*658eb9e1SMichael Kruse#### Initialization
103*658eb9e1SMichael Kruse
104*658eb9e1SMichael KruseAn `mp_int` must be initialized before use. By default, an `mp_int` is
105*658eb9e1SMichael Kruseinitialized with a certain minimum amount of storage for digits, and the
106*658eb9e1SMichael Krusestorage is expanded automatically as needed.  To initialize an `mp_int`, use
107*658eb9e1SMichael Krusethe following functions:
108*658eb9e1SMichael Kruse
109*658eb9e1SMichael Kruse------------
110*658eb9e1SMichael Kruse<a id="mp_int_init"></a><pre>
111*658eb9e1SMichael Krusemp_result <a href="imath.h#L115">mp_int_init</a>(mp_int z);
112*658eb9e1SMichael Kruse</pre>
113*658eb9e1SMichael Kruse -  Initializes `z` with 1-digit precision and sets it to zero.  This function
114*658eb9e1SMichael Kruse    cannot fail unless `z == NULL`.
115*658eb9e1SMichael Kruse
116*658eb9e1SMichael Kruse------------
117*658eb9e1SMichael Kruse<a id="mp_int_alloc"></a><pre>
118*658eb9e1SMichael Krusemp_int <a href="imath.h#L119">mp_int_alloc</a>(void);
119*658eb9e1SMichael Kruse</pre>
120*658eb9e1SMichael Kruse -  Allocates a fresh zero-valued `mpz_t` on the heap, returning NULL in case
121*658eb9e1SMichael Kruse    of error. The only possible error is out-of-memory.
122*658eb9e1SMichael Kruse
123*658eb9e1SMichael Kruse------------
124*658eb9e1SMichael Kruse<a id="mp_int_init_size"></a><pre>
125*658eb9e1SMichael Krusemp_result <a href="imath.h#L124">mp_int_init_size</a>(mp_int z, mp_size prec);
126*658eb9e1SMichael Kruse</pre>
127*658eb9e1SMichael Kruse -  Initializes `z` with at least `prec` digits of storage, and sets it to
128*658eb9e1SMichael Kruse    zero. If `prec` is zero, the default precision is used. In either case the
129*658eb9e1SMichael Kruse    size is rounded up to the nearest multiple of the word size.
130*658eb9e1SMichael Kruse
131*658eb9e1SMichael Kruse------------
132*658eb9e1SMichael Kruse<a id="mp_int_init_copy"></a><pre>
133*658eb9e1SMichael Krusemp_result <a href="imath.h#L128">mp_int_init_copy</a>(mp_int z, mp_int old);
134*658eb9e1SMichael Kruse</pre>
135*658eb9e1SMichael Kruse -  Initializes `z` to be a copy of an already-initialized value in `old`. The
136*658eb9e1SMichael Kruse    new copy does not share storage with the original.
137*658eb9e1SMichael Kruse
138*658eb9e1SMichael Kruse------------
139*658eb9e1SMichael Kruse<a id="mp_int_init_value"></a><pre>
140*658eb9e1SMichael Krusemp_result <a href="imath.h#L131">mp_int_init_value</a>(mp_int z, mp_small value);
141*658eb9e1SMichael Kruse</pre>
142*658eb9e1SMichael Kruse -  Initializes `z` to the specified signed `value` at default precision.
143*658eb9e1SMichael Kruse
144*658eb9e1SMichael Kruse
145*658eb9e1SMichael Kruse
146*658eb9e1SMichael Kruse#### Cleanup
147*658eb9e1SMichael Kruse
148*658eb9e1SMichael KruseWhen you are finished with an `mp_int`, you must free the memory it uses:
149*658eb9e1SMichael Kruse
150*658eb9e1SMichael Kruse------------
151*658eb9e1SMichael Kruse<a id="mp_int_clear"></a><pre>
152*658eb9e1SMichael Krusevoid <a href="imath.h#L143">mp_int_clear</a>(mp_int z);
153*658eb9e1SMichael Kruse</pre>
154*658eb9e1SMichael Kruse -  Releases the storage used by `z`.
155*658eb9e1SMichael Kruse
156*658eb9e1SMichael Kruse------------
157*658eb9e1SMichael Kruse<a id="mp_int_free"></a><pre>
158*658eb9e1SMichael Krusevoid <a href="imath.h#L147">mp_int_free</a>(mp_int z);
159*658eb9e1SMichael Kruse</pre>
160*658eb9e1SMichael Kruse -  Releases the storage used by `z` and also `z` itself.
161*658eb9e1SMichael Kruse    This should only be used for `z` allocated by `mp_int_alloc()`.
162*658eb9e1SMichael Kruse
163*658eb9e1SMichael Kruse
164*658eb9e1SMichael Kruse
165*658eb9e1SMichael Kruse#### Setting Values
166*658eb9e1SMichael Kruse
167*658eb9e1SMichael KruseTo set an `mp_int` which has already been initialized to a small integer value,
168*658eb9e1SMichael Kruseuse:
169*658eb9e1SMichael Kruse
170*658eb9e1SMichael Kruse------------
171*658eb9e1SMichael Kruse<a id="mp_int_set_value"></a><pre>
172*658eb9e1SMichael Krusemp_result <a href="imath.h#L137">mp_int_set_value</a>(mp_int z, mp_small value);
173*658eb9e1SMichael Kruse</pre>
174*658eb9e1SMichael Kruse -  Sets `z` to the value of the specified signed `value`.
175*658eb9e1SMichael Kruse
176*658eb9e1SMichael Kruse------------
177*658eb9e1SMichael Kruse<a id="mp_int_set_uvalue"></a><pre>
178*658eb9e1SMichael Krusemp_result <a href="imath.h#L140">mp_int_set_uvalue</a>(mp_int z, mp_usmall uvalue);
179*658eb9e1SMichael Kruse</pre>
180*658eb9e1SMichael Kruse -  Sets `z` to the value of the specified unsigned `value`.
181*658eb9e1SMichael Kruse
182*658eb9e1SMichael Kruse
183*658eb9e1SMichael Kruse
184*658eb9e1SMichael KruseTo copy one initialized `mp_int` to another, use:
185*658eb9e1SMichael Kruse
186*658eb9e1SMichael Kruse------------
187*658eb9e1SMichael Kruse<a id="mp_int_copy"></a><pre>
188*658eb9e1SMichael Krusemp_result <a href="imath.h#L151">mp_int_copy</a>(mp_int a, mp_int c);
189*658eb9e1SMichael Kruse</pre>
190*658eb9e1SMichael Kruse -  Replaces the value of `c` with a copy of the value of `a`. No new memory is
191*658eb9e1SMichael Kruse    allocated unless `a` has more significant digits than `c` has allocated.
192*658eb9e1SMichael Kruse
193*658eb9e1SMichael Kruse
194*658eb9e1SMichael Kruse
195*658eb9e1SMichael Kruse### Arithmetic Functions
196*658eb9e1SMichael Kruse
197*658eb9e1SMichael Kruse------------
198*658eb9e1SMichael Kruse<a id="mp_int_is_odd"></a><pre>
199*658eb9e1SMichael Krusestatic inline bool <a href="imath.h#L108">mp_int_is_odd</a>(mp_int z);
200*658eb9e1SMichael Kruse</pre>
201*658eb9e1SMichael Kruse -  Reports whether `z` is odd, having remainder 1 when divided by 2.
202*658eb9e1SMichael Kruse
203*658eb9e1SMichael Kruse------------
204*658eb9e1SMichael Kruse<a id="mp_int_is_even"></a><pre>
205*658eb9e1SMichael Krusestatic inline bool <a href="imath.h#L111">mp_int_is_even</a>(mp_int z);
206*658eb9e1SMichael Kruse</pre>
207*658eb9e1SMichael Kruse -  Reports whether `z` is even, having remainder 0 when divided by 2.
208*658eb9e1SMichael Kruse
209*658eb9e1SMichael Kruse------------
210*658eb9e1SMichael Kruse<a id="mp_int_zero"></a><pre>
211*658eb9e1SMichael Krusevoid <a href="imath.h#L157">mp_int_zero</a>(mp_int z);
212*658eb9e1SMichael Kruse</pre>
213*658eb9e1SMichael Kruse -  Sets `z` to zero. The allocated storage of `z` is not changed.
214*658eb9e1SMichael Kruse
215*658eb9e1SMichael Kruse------------
216*658eb9e1SMichael Kruse<a id="mp_int_abs"></a><pre>
217*658eb9e1SMichael Krusemp_result <a href="imath.h#L160">mp_int_abs</a>(mp_int a, mp_int c);
218*658eb9e1SMichael Kruse</pre>
219*658eb9e1SMichael Kruse -  Sets `c` to the absolute value of `a`.
220*658eb9e1SMichael Kruse
221*658eb9e1SMichael Kruse------------
222*658eb9e1SMichael Kruse<a id="mp_int_neg"></a><pre>
223*658eb9e1SMichael Krusemp_result <a href="imath.h#L163">mp_int_neg</a>(mp_int a, mp_int c);
224*658eb9e1SMichael Kruse</pre>
225*658eb9e1SMichael Kruse -  Sets `c` to the additive inverse (negation) of `a`.
226*658eb9e1SMichael Kruse
227*658eb9e1SMichael Kruse------------
228*658eb9e1SMichael Kruse<a id="mp_int_add"></a><pre>
229*658eb9e1SMichael Krusemp_result <a href="imath.h#L166">mp_int_add</a>(mp_int a, mp_int b, mp_int c);
230*658eb9e1SMichael Kruse</pre>
231*658eb9e1SMichael Kruse -  Sets `c` to the sum of `a` and `b`.
232*658eb9e1SMichael Kruse
233*658eb9e1SMichael Kruse------------
234*658eb9e1SMichael Kruse<a id="mp_int_add_value"></a><pre>
235*658eb9e1SMichael Krusemp_result <a href="imath.h#L169">mp_int_add_value</a>(mp_int a, mp_small value, mp_int c);
236*658eb9e1SMichael Kruse</pre>
237*658eb9e1SMichael Kruse -  Sets `c` to the sum of `a` and `value`.
238*658eb9e1SMichael Kruse
239*658eb9e1SMichael Kruse------------
240*658eb9e1SMichael Kruse<a id="mp_int_sub"></a><pre>
241*658eb9e1SMichael Krusemp_result <a href="imath.h#L172">mp_int_sub</a>(mp_int a, mp_int b, mp_int c);
242*658eb9e1SMichael Kruse</pre>
243*658eb9e1SMichael Kruse -  Sets `c` to the difference of `a` less `b`.
244*658eb9e1SMichael Kruse
245*658eb9e1SMichael Kruse------------
246*658eb9e1SMichael Kruse<a id="mp_int_sub_value"></a><pre>
247*658eb9e1SMichael Krusemp_result <a href="imath.h#L175">mp_int_sub_value</a>(mp_int a, mp_small value, mp_int c);
248*658eb9e1SMichael Kruse</pre>
249*658eb9e1SMichael Kruse -  Sets `c` to the difference of `a` less `value`.
250*658eb9e1SMichael Kruse
251*658eb9e1SMichael Kruse------------
252*658eb9e1SMichael Kruse<a id="mp_int_mul"></a><pre>
253*658eb9e1SMichael Krusemp_result <a href="imath.h#L178">mp_int_mul</a>(mp_int a, mp_int b, mp_int c);
254*658eb9e1SMichael Kruse</pre>
255*658eb9e1SMichael Kruse -  Sets `c` to the product of `a` and `b`.
256*658eb9e1SMichael Kruse
257*658eb9e1SMichael Kruse------------
258*658eb9e1SMichael Kruse<a id="mp_int_mul_value"></a><pre>
259*658eb9e1SMichael Krusemp_result <a href="imath.h#L181">mp_int_mul_value</a>(mp_int a, mp_small value, mp_int c);
260*658eb9e1SMichael Kruse</pre>
261*658eb9e1SMichael Kruse -  Sets `c` to the product of `a` and `value`.
262*658eb9e1SMichael Kruse
263*658eb9e1SMichael Kruse------------
264*658eb9e1SMichael Kruse<a id="mp_int_mul_pow2"></a><pre>
265*658eb9e1SMichael Krusemp_result <a href="imath.h#L184">mp_int_mul_pow2</a>(mp_int a, mp_small p2, mp_int c);
266*658eb9e1SMichael Kruse</pre>
267*658eb9e1SMichael Kruse -  Sets `c` to the product of `a` and `2^p2`. Requires `p2 >= 0`.
268*658eb9e1SMichael Kruse
269*658eb9e1SMichael Kruse------------
270*658eb9e1SMichael Kruse<a id="mp_int_sqr"></a><pre>
271*658eb9e1SMichael Krusemp_result <a href="imath.h#L187">mp_int_sqr</a>(mp_int a, mp_int c);
272*658eb9e1SMichael Kruse</pre>
273*658eb9e1SMichael Kruse -  Sets `c` to the square of `a`.
274*658eb9e1SMichael Kruse
275*658eb9e1SMichael Kruse------------
276*658eb9e1SMichael Kruse<a id="mp_int_root"></a><pre>
277*658eb9e1SMichael Krusemp_result <a href="imath.h#L306">mp_int_root</a>(mp_int a, mp_small b, mp_int c);
278*658eb9e1SMichael Kruse</pre>
279*658eb9e1SMichael Kruse -  Sets `c` to the greatest integer not less than the `b`th root of `a`,
280*658eb9e1SMichael Kruse    using Newton's root-finding algorithm.
281*658eb9e1SMichael Kruse    It returns `MP_UNDEF` if `a < 0` and `b` is even.
282*658eb9e1SMichael Kruse
283*658eb9e1SMichael Kruse------------
284*658eb9e1SMichael Kruse<a id="mp_int_sqrt"></a><pre>
285*658eb9e1SMichael Krusestatic inline mp_result <a href="imath.h#L310">mp_int_sqrt</a>(mp_int a, mp_int c);
286*658eb9e1SMichael Kruse</pre>
287*658eb9e1SMichael Kruse -  Sets `c` to the greatest integer not less than the square root of `a`.
288*658eb9e1SMichael Kruse    This is a special case of `mp_int_root()`.
289*658eb9e1SMichael Kruse
290*658eb9e1SMichael Kruse------------
291*658eb9e1SMichael Kruse<a id="mp_int_div"></a><pre>
292*658eb9e1SMichael Krusemp_result <a href="imath.h#L195">mp_int_div</a>(mp_int a, mp_int b, mp_int q, mp_int r);
293*658eb9e1SMichael Kruse</pre>
294*658eb9e1SMichael Kruse -  Sets `q` and `r` to the quotent and remainder of `a / b`. Division by
295*658eb9e1SMichael Kruse    powers of 2 is detected and handled efficiently.  The remainder is pinned
296*658eb9e1SMichael Kruse    to `0 <= r < b`.
297*658eb9e1SMichael Kruse
298*658eb9e1SMichael Kruse    Either of `q` or `r` may be NULL, but not both, and `q` and `r` may not
299*658eb9e1SMichael Kruse    point to the same value.
300*658eb9e1SMichael Kruse
301*658eb9e1SMichael Kruse------------
302*658eb9e1SMichael Kruse<a id="mp_int_div_value"></a><pre>
303*658eb9e1SMichael Krusemp_result <a href="imath.h#L200">mp_int_div_value</a>(mp_int a, mp_small value, mp_int q, mp_small *r);
304*658eb9e1SMichael Kruse</pre>
305*658eb9e1SMichael Kruse -  Sets `q` and `*r` to the quotent and remainder of `a / value`. Division by
306*658eb9e1SMichael Kruse    powers of 2 is detected and handled efficiently. The remainder is pinned to
307*658eb9e1SMichael Kruse    `0 <= *r < b`. Either of `q` or `r` may be NULL.
308*658eb9e1SMichael Kruse
309*658eb9e1SMichael Kruse------------
310*658eb9e1SMichael Kruse<a id="mp_int_div_pow2"></a><pre>
311*658eb9e1SMichael Krusemp_result <a href="imath.h#L206">mp_int_div_pow2</a>(mp_int a, mp_small p2, mp_int q, mp_int r);
312*658eb9e1SMichael Kruse</pre>
313*658eb9e1SMichael Kruse -  Sets `q` and `r` to the quotient and remainder of `a / 2^p2`. This is a
314*658eb9e1SMichael Kruse    special case for division by powers of two that is more efficient than
315*658eb9e1SMichael Kruse    using ordinary division. Note that `mp_int_div()` will automatically handle
316*658eb9e1SMichael Kruse    this case, this function is for cases where you have only the exponent.
317*658eb9e1SMichael Kruse
318*658eb9e1SMichael Kruse------------
319*658eb9e1SMichael Kruse<a id="mp_int_mod"></a><pre>
320*658eb9e1SMichael Krusemp_result <a href="imath.h#L210">mp_int_mod</a>(mp_int a, mp_int m, mp_int c);
321*658eb9e1SMichael Kruse</pre>
322*658eb9e1SMichael Kruse -  Sets `c` to the remainder of `a / m`.
323*658eb9e1SMichael Kruse    The remainder is pinned to `0 <= c < m`.
324*658eb9e1SMichael Kruse
325*658eb9e1SMichael Kruse------------
326*658eb9e1SMichael Kruse<a id="mp_int_mod_value"></a><pre>
327*658eb9e1SMichael Krusestatic inline mp_result <a href="imath.h#L226">mp_int_mod_value</a>(mp_int a, mp_small value, mp_small* r);
328*658eb9e1SMichael Kruse</pre>
329*658eb9e1SMichael Kruse -  Sets `*r` to the remainder of `a / value`.
330*658eb9e1SMichael Kruse    The remainder is pinned to `0 <= r < value`.
331*658eb9e1SMichael Kruse
332*658eb9e1SMichael Kruse------------
333*658eb9e1SMichael Kruse<a id="mp_int_expt"></a><pre>
334*658eb9e1SMichael Krusemp_result <a href="imath.h#L214">mp_int_expt</a>(mp_int a, mp_small b, mp_int c);
335*658eb9e1SMichael Kruse</pre>
336*658eb9e1SMichael Kruse -  Sets `c` to the value of `a` raised to the `b` power.
337*658eb9e1SMichael Kruse    It returns `MP_RANGE` if `b < 0`.
338*658eb9e1SMichael Kruse
339*658eb9e1SMichael Kruse------------
340*658eb9e1SMichael Kruse<a id="mp_int_expt_value"></a><pre>
341*658eb9e1SMichael Krusemp_result <a href="imath.h#L218">mp_int_expt_value</a>(mp_small a, mp_small b, mp_int c);
342*658eb9e1SMichael Kruse</pre>
343*658eb9e1SMichael Kruse -  Sets `c` to the value of `a` raised to the `b` power.
344*658eb9e1SMichael Kruse    It returns `MP_RANGE` if `b < 0`.
345*658eb9e1SMichael Kruse
346*658eb9e1SMichael Kruse------------
347*658eb9e1SMichael Kruse<a id="mp_int_expt_full"></a><pre>
348*658eb9e1SMichael Krusemp_result <a href="imath.h#L222">mp_int_expt_full</a>(mp_int a, mp_int b, mp_int c);
349*658eb9e1SMichael Kruse</pre>
350*658eb9e1SMichael Kruse -  Sets `c` to the value of `a` raised to the `b` power.
351*658eb9e1SMichael Kruse    It returns `MP_RANGE`) if `b < 0`.
352*658eb9e1SMichael Kruse
353*658eb9e1SMichael Kruse
354*658eb9e1SMichael Kruse
355*658eb9e1SMichael Kruse### Comparison Functions
356*658eb9e1SMichael Kruse
357*658eb9e1SMichael KruseUnless otherwise specified, comparison between values `x` and `y` returns a
358*658eb9e1SMichael Kruse**comparator**, an integer value < 0 if `x` is less than `y`, 0 if `x` is equal
359*658eb9e1SMichael Kruseto `y`, and > 0 if `x` is greater than `y`.
360*658eb9e1SMichael Kruse
361*658eb9e1SMichael Kruse------------
362*658eb9e1SMichael Kruse<a id="mp_int_compare"></a><pre>
363*658eb9e1SMichael Kruseint <a href="imath.h#L232">mp_int_compare</a>(mp_int a, mp_int b);
364*658eb9e1SMichael Kruse</pre>
365*658eb9e1SMichael Kruse -  Returns the comparator of `a` and `b`.
366*658eb9e1SMichael Kruse
367*658eb9e1SMichael Kruse------------
368*658eb9e1SMichael Kruse<a id="mp_int_compare_unsigned"></a><pre>
369*658eb9e1SMichael Kruseint <a href="imath.h#L236">mp_int_compare_unsigned</a>(mp_int a, mp_int b);
370*658eb9e1SMichael Kruse</pre>
371*658eb9e1SMichael Kruse -  Returns the comparator of the magnitudes of `a` and `b`, disregarding their
372*658eb9e1SMichael Kruse    signs. Neither `a` nor `b` is modified by the comparison.
373*658eb9e1SMichael Kruse
374*658eb9e1SMichael Kruse------------
375*658eb9e1SMichael Kruse<a id="mp_int_compare_zero"></a><pre>
376*658eb9e1SMichael Kruseint <a href="imath.h#L239">mp_int_compare_zero</a>(mp_int z);
377*658eb9e1SMichael Kruse</pre>
378*658eb9e1SMichael Kruse -  Returns the comparator of `z` and zero.
379*658eb9e1SMichael Kruse
380*658eb9e1SMichael Kruse------------
381*658eb9e1SMichael Kruse<a id="mp_int_compare_value"></a><pre>
382*658eb9e1SMichael Kruseint <a href="imath.h#L242">mp_int_compare_value</a>(mp_int z, mp_small v);
383*658eb9e1SMichael Kruse</pre>
384*658eb9e1SMichael Kruse -  Returns the comparator of `z` and the signed value `v`.
385*658eb9e1SMichael Kruse
386*658eb9e1SMichael Kruse------------
387*658eb9e1SMichael Kruse<a id="mp_int_compare_uvalue"></a><pre>
388*658eb9e1SMichael Kruseint <a href="imath.h#L245">mp_int_compare_uvalue</a>(mp_int z, mp_usmall uv);
389*658eb9e1SMichael Kruse</pre>
390*658eb9e1SMichael Kruse -  Returns the comparator of `z` and the unsigned value `uv`.
391*658eb9e1SMichael Kruse
392*658eb9e1SMichael Kruse------------
393*658eb9e1SMichael Kruse<a id="mp_int_divisible_value"></a><pre>
394*658eb9e1SMichael Krusebool <a href="imath.h#L248">mp_int_divisible_value</a>(mp_int a, mp_small v);
395*658eb9e1SMichael Kruse</pre>
396*658eb9e1SMichael Kruse -  Reports whether `a` is divisible by `v`.
397*658eb9e1SMichael Kruse
398*658eb9e1SMichael Kruse------------
399*658eb9e1SMichael Kruse<a id="mp_int_is_pow2"></a><pre>
400*658eb9e1SMichael Kruseint <a href="imath.h#L252">mp_int_is_pow2</a>(mp_int z);
401*658eb9e1SMichael Kruse</pre>
402*658eb9e1SMichael Kruse -  Returns `k >= 0` such that `z` is `2^k`, if such a `k` exists. If no such
403*658eb9e1SMichael Kruse    `k` exists, the function returns -1.
404*658eb9e1SMichael Kruse
405*658eb9e1SMichael Kruse
406*658eb9e1SMichael Kruse
407*658eb9e1SMichael Kruse### Modular Operations
408*658eb9e1SMichael Kruse
409*658eb9e1SMichael Kruse------------
410*658eb9e1SMichael Kruse<a id="mp_int_exptmod"></a><pre>
411*658eb9e1SMichael Krusemp_result <a href="imath.h#L256">mp_int_exptmod</a>(mp_int a, mp_int b, mp_int m, mp_int c);
412*658eb9e1SMichael Kruse</pre>
413*658eb9e1SMichael Kruse -  Sets `c` to the value of `a` raised to the `b` power, reduced modulo `m`.
414*658eb9e1SMichael Kruse    It returns `MP_RANGE` if `b < 0` or `MP_UNDEF` if `m == 0`.
415*658eb9e1SMichael Kruse
416*658eb9e1SMichael Kruse------------
417*658eb9e1SMichael Kruse<a id="mp_int_exptmod_evalue"></a><pre>
418*658eb9e1SMichael Krusemp_result <a href="imath.h#L260">mp_int_exptmod_evalue</a>(mp_int a, mp_small value, mp_int m, mp_int c);
419*658eb9e1SMichael Kruse</pre>
420*658eb9e1SMichael Kruse -  Sets `c` to the value of `a` raised to the `value` power, modulo `m`.
421*658eb9e1SMichael Kruse    It returns `MP_RANGE` if `value < 0` or `MP_UNDEF` if `m == 0`.
422*658eb9e1SMichael Kruse
423*658eb9e1SMichael Kruse------------
424*658eb9e1SMichael Kruse<a id="mp_int_exptmod_bvalue"></a><pre>
425*658eb9e1SMichael Krusemp_result <a href="imath.h#L264">mp_int_exptmod_bvalue</a>(mp_small value, mp_int b, mp_int m, mp_int c);
426*658eb9e1SMichael Kruse</pre>
427*658eb9e1SMichael Kruse -  Sets `c` to the value of `value` raised to the `b` power, modulo `m`.
428*658eb9e1SMichael Kruse    It returns `MP_RANGE` if `b < 0` or `MP_UNDEF` if `m == 0`.
429*658eb9e1SMichael Kruse
430*658eb9e1SMichael Kruse------------
431*658eb9e1SMichael Kruse<a id="mp_int_exptmod_known"></a><pre>
432*658eb9e1SMichael Krusemp_result <a href="imath.h#L271">mp_int_exptmod_known</a>(mp_int a, mp_int b, mp_int m, mp_int mu, mp_int c);
433*658eb9e1SMichael Kruse</pre>
434*658eb9e1SMichael Kruse -  Sets `c` to the value of `a` raised to the `b` power, reduced modulo `m`,
435*658eb9e1SMichael Kruse    given a precomputed reduction constant `mu` defined for Barrett's modular
436*658eb9e1SMichael Kruse    reduction algorithm.
437*658eb9e1SMichael Kruse
438*658eb9e1SMichael Kruse    It returns `MP_RANGE` if `b < 0` or `MP_UNDEF` if `m == 0`.
439*658eb9e1SMichael Kruse
440*658eb9e1SMichael Kruse------------
441*658eb9e1SMichael Kruse<a id="mp_int_redux_const"></a><pre>
442*658eb9e1SMichael Krusemp_result <a href="imath.h#L275">mp_int_redux_const</a>(mp_int m, mp_int c);
443*658eb9e1SMichael Kruse</pre>
444*658eb9e1SMichael Kruse -  Sets `c` to the reduction constant for Barrett reduction by modulus `m`.
445*658eb9e1SMichael Kruse    Requires that `c` and `m` point to distinct locations.
446*658eb9e1SMichael Kruse
447*658eb9e1SMichael Kruse------------
448*658eb9e1SMichael Kruse<a id="mp_int_invmod"></a><pre>
449*658eb9e1SMichael Krusemp_result <a href="imath.h#L282">mp_int_invmod</a>(mp_int a, mp_int m, mp_int c);
450*658eb9e1SMichael Kruse</pre>
451*658eb9e1SMichael Kruse -  Sets `c` to the multiplicative inverse of `a` modulo `m`, if it exists.
452*658eb9e1SMichael Kruse    The least non-negative representative of the congruence class is computed.
453*658eb9e1SMichael Kruse
454*658eb9e1SMichael Kruse    It returns `MP_UNDEF` if the inverse does not exist, or `MP_RANGE` if `a ==
455*658eb9e1SMichael Kruse    0` or `m <= 0`.
456*658eb9e1SMichael Kruse
457*658eb9e1SMichael Kruse------------
458*658eb9e1SMichael Kruse<a id="mp_int_gcd"></a><pre>
459*658eb9e1SMichael Krusemp_result <a href="imath.h#L288">mp_int_gcd</a>(mp_int a, mp_int b, mp_int c);
460*658eb9e1SMichael Kruse</pre>
461*658eb9e1SMichael Kruse -  Sets `c` to the greatest common divisor of `a` and `b`.
462*658eb9e1SMichael Kruse
463*658eb9e1SMichael Kruse    It returns `MP_UNDEF` if the GCD is undefined, such as for example if `a`
464*658eb9e1SMichael Kruse    and `b` are both zero.
465*658eb9e1SMichael Kruse
466*658eb9e1SMichael Kruse------------
467*658eb9e1SMichael Kruse<a id="mp_int_egcd"></a><pre>
468*658eb9e1SMichael Krusemp_result <a href="imath.h#L295">mp_int_egcd</a>(mp_int a, mp_int b, mp_int c, mp_int x, mp_int y);
469*658eb9e1SMichael Kruse</pre>
470*658eb9e1SMichael Kruse -  Sets `c` to the greatest common divisor of `a` and `b`, and sets `x` and
471*658eb9e1SMichael Kruse    `y` to values satisfying Bezout's identity `gcd(a, b) = ax + by`.
472*658eb9e1SMichael Kruse
473*658eb9e1SMichael Kruse    It returns `MP_UNDEF` if the GCD is undefined, such as for example if `a`
474*658eb9e1SMichael Kruse    and `b` are both zero.
475*658eb9e1SMichael Kruse
476*658eb9e1SMichael Kruse------------
477*658eb9e1SMichael Kruse<a id="mp_int_lcm"></a><pre>
478*658eb9e1SMichael Krusemp_result <a href="imath.h#L301">mp_int_lcm</a>(mp_int a, mp_int b, mp_int c);
479*658eb9e1SMichael Kruse</pre>
480*658eb9e1SMichael Kruse -  Sets `c` to the least common multiple of `a` and `b`.
481*658eb9e1SMichael Kruse
482*658eb9e1SMichael Kruse    It returns `MP_UNDEF` if the LCM is undefined, such as for example if `a`
483*658eb9e1SMichael Kruse    and `b` are both zero.
484*658eb9e1SMichael Kruse
485*658eb9e1SMichael Kruse
486*658eb9e1SMichael Kruse
487*658eb9e1SMichael Kruse### Conversion of Values
488*658eb9e1SMichael Kruse
489*658eb9e1SMichael Kruse------------
490*658eb9e1SMichael Kruse<a id="mp_int_to_int"></a><pre>
491*658eb9e1SMichael Krusemp_result <a href="imath.h#L315">mp_int_to_int</a>(mp_int z, mp_small *out);
492*658eb9e1SMichael Kruse</pre>
493*658eb9e1SMichael Kruse -  Returns `MP_OK` if `z` is representable as `mp_small`, else `MP_RANGE`.
494*658eb9e1SMichael Kruse    If `out` is not NULL, `*out` is set to the value of `z` when `MP_OK`.
495*658eb9e1SMichael Kruse
496*658eb9e1SMichael Kruse------------
497*658eb9e1SMichael Kruse<a id="mp_int_to_uint"></a><pre>
498*658eb9e1SMichael Krusemp_result <a href="imath.h#L319">mp_int_to_uint</a>(mp_int z, mp_usmall *out);
499*658eb9e1SMichael Kruse</pre>
500*658eb9e1SMichael Kruse -  Returns `MP_OK` if `z` is representable as `mp_usmall`, or `MP_RANGE`.
501*658eb9e1SMichael Kruse    If `out` is not NULL, `*out` is set to the value of `z` when `MP_OK`.
502*658eb9e1SMichael Kruse
503*658eb9e1SMichael Kruse------------
504*658eb9e1SMichael Kruse<a id="mp_int_to_string"></a><pre>
505*658eb9e1SMichael Krusemp_result <a href="imath.h#L327">mp_int_to_string</a>(mp_int z, mp_size radix, char *str, int limit);
506*658eb9e1SMichael Kruse</pre>
507*658eb9e1SMichael Kruse -  Converts `z` to a zero-terminated string of characters in the specified
508*658eb9e1SMichael Kruse    `radix`, writing at most `limit` characters to `str` including the
509*658eb9e1SMichael Kruse    terminating NUL value. A leading `-` is used to indicate a negative value.
510*658eb9e1SMichael Kruse
511*658eb9e1SMichael Kruse    Returns `MP_TRUNC` if `limit` was to small to write all of `z`.
512*658eb9e1SMichael Kruse    Requires `MP_MIN_RADIX <= radix <= MP_MAX_RADIX`.
513*658eb9e1SMichael Kruse
514*658eb9e1SMichael Kruse------------
515*658eb9e1SMichael Kruse<a id="mp_int_string_len"></a><pre>
516*658eb9e1SMichael Krusemp_result <a href="imath.h#L332">mp_int_string_len</a>(mp_int z, mp_size radix);
517*658eb9e1SMichael Kruse</pre>
518*658eb9e1SMichael Kruse -  Reports the minimum number of characters required to represent `z` as a
519*658eb9e1SMichael Kruse    zero-terminated string in the given `radix`.
520*658eb9e1SMichael Kruse    Requires `MP_MIN_RADIX <= radix <= MP_MAX_RADIX`.
521*658eb9e1SMichael Kruse
522*658eb9e1SMichael Kruse------------
523*658eb9e1SMichael Kruse<a id="mp_int_read_string"></a><pre>
524*658eb9e1SMichael Krusemp_result <a href="imath.h#L347">mp_int_read_string</a>(mp_int z, mp_size radix, const char *str);
525*658eb9e1SMichael Kruse</pre>
526*658eb9e1SMichael Kruse -  Reads a string of ASCII digits in the specified `radix` from the zero
527*658eb9e1SMichael Kruse    terminated `str` provided into `z`. For values of `radix > 10`, the letters
528*658eb9e1SMichael Kruse    `A`..`Z` or `a`..`z` are accepted. Letters are interpreted without respect
529*658eb9e1SMichael Kruse    to case.
530*658eb9e1SMichael Kruse
531*658eb9e1SMichael Kruse    Leading whitespace is ignored, and a leading `+` or `-` is interpreted as a
532*658eb9e1SMichael Kruse    sign flag. Processing stops when a NUL or any other character out of range
533*658eb9e1SMichael Kruse    for a digit in the given radix is encountered.
534*658eb9e1SMichael Kruse
535*658eb9e1SMichael Kruse    If the whole string was consumed, `MP_OK` is returned; otherwise
536*658eb9e1SMichael Kruse    `MP_TRUNC`. is returned.
537*658eb9e1SMichael Kruse
538*658eb9e1SMichael Kruse    Requires `MP_MIN_RADIX <= radix <= MP_MAX_RADIX`.
539*658eb9e1SMichael Kruse
540*658eb9e1SMichael Kruse------------
541*658eb9e1SMichael Kruse<a id="mp_int_read_cstring"></a><pre>
542*658eb9e1SMichael Krusemp_result <a href="imath.h#L365">mp_int_read_cstring</a>(mp_int z, mp_size radix, const char *str, char **end);
543*658eb9e1SMichael Kruse</pre>
544*658eb9e1SMichael Kruse -  Reads a string of ASCII digits in the specified `radix` from the zero
545*658eb9e1SMichael Kruse    terminated `str` provided into `z`. For values of `radix > 10`, the letters
546*658eb9e1SMichael Kruse    `A`..`Z` or `a`..`z` are accepted. Letters are interpreted without respect
547*658eb9e1SMichael Kruse    to case.
548*658eb9e1SMichael Kruse
549*658eb9e1SMichael Kruse    Leading whitespace is ignored, and a leading `+` or `-` is interpreted as a
550*658eb9e1SMichael Kruse    sign flag. Processing stops when a NUL or any other character out of range
551*658eb9e1SMichael Kruse    for a digit in the given radix is encountered.
552*658eb9e1SMichael Kruse
553*658eb9e1SMichael Kruse    If the whole string was consumed, `MP_OK` is returned; otherwise
554*658eb9e1SMichael Kruse    `MP_TRUNC`. is returned. If `end` is not NULL, `*end` is set to point to
555*658eb9e1SMichael Kruse    the first unconsumed byte of the input string (the NUL byte if the whole
556*658eb9e1SMichael Kruse    string was consumed). This emulates the behavior of the standard C
557*658eb9e1SMichael Kruse    `strtol()` function.
558*658eb9e1SMichael Kruse
559*658eb9e1SMichael Kruse    Requires `MP_MIN_RADIX <= radix <= MP_MAX_RADIX`.
560*658eb9e1SMichael Kruse
561*658eb9e1SMichael Kruse------------
562*658eb9e1SMichael Kruse<a id="mp_int_count_bits"></a><pre>
563*658eb9e1SMichael Krusemp_result <a href="imath.h#L368">mp_int_count_bits</a>(mp_int z);
564*658eb9e1SMichael Kruse</pre>
565*658eb9e1SMichael Kruse -  Returns the number of significant bits in `z`.
566*658eb9e1SMichael Kruse
567*658eb9e1SMichael Kruse------------
568*658eb9e1SMichael Kruse<a id="mp_int_to_binary"></a><pre>
569*658eb9e1SMichael Krusemp_result <a href="imath.h#L383">mp_int_to_binary</a>(mp_int z, unsigned char *buf, int limit);
570*658eb9e1SMichael Kruse</pre>
571*658eb9e1SMichael Kruse -  Converts `z` to 2's complement binary, writing at most `limit` bytes into
572*658eb9e1SMichael Kruse    the given `buf`.  Returns `MP_TRUNC` if the buffer limit was too small to
573*658eb9e1SMichael Kruse    contain the whole value.  If this occurs, the contents of buf will be
574*658eb9e1SMichael Kruse    effectively garbage, as the function uses the buffer as scratch space.
575*658eb9e1SMichael Kruse
576*658eb9e1SMichael Kruse    The binary representation of `z` is in base-256 with digits ordered from
577*658eb9e1SMichael Kruse    most significant to least significant (network byte ordering).  The
578*658eb9e1SMichael Kruse    high-order bit of the first byte is set for negative values, clear for
579*658eb9e1SMichael Kruse    non-negative values.
580*658eb9e1SMichael Kruse
581*658eb9e1SMichael Kruse    As a result, non-negative values will be padded with a leading zero byte if
582*658eb9e1SMichael Kruse    the high-order byte of the base-256 magnitude is set.  This extra byte is
583*658eb9e1SMichael Kruse    accounted for by the `mp_int_binary_len()` function.
584*658eb9e1SMichael Kruse
585*658eb9e1SMichael Kruse------------
586*658eb9e1SMichael Kruse<a id="mp_int_read_binary"></a><pre>
587*658eb9e1SMichael Krusemp_result <a href="imath.h#L388">mp_int_read_binary</a>(mp_int z, unsigned char *buf, int len);
588*658eb9e1SMichael Kruse</pre>
589*658eb9e1SMichael Kruse -  Reads a 2's complement binary value from `buf` into `z`, where `len` is the
590*658eb9e1SMichael Kruse    length of the buffer.  The contents of `buf` may be overwritten during
591*658eb9e1SMichael Kruse    processing, although they will be restored when the function returns.
592*658eb9e1SMichael Kruse
593*658eb9e1SMichael Kruse------------
594*658eb9e1SMichael Kruse<a id="mp_int_binary_len"></a><pre>
595*658eb9e1SMichael Krusemp_result <a href="imath.h#L391">mp_int_binary_len</a>(mp_int z);
596*658eb9e1SMichael Kruse</pre>
597*658eb9e1SMichael Kruse -  Returns the number of bytes to represent `z` in 2's complement binary.
598*658eb9e1SMichael Kruse
599*658eb9e1SMichael Kruse------------
600*658eb9e1SMichael Kruse<a id="mp_int_to_unsigned"></a><pre>
601*658eb9e1SMichael Krusemp_result <a href="imath.h#L402">mp_int_to_unsigned</a>(mp_int z, unsigned char *buf, int limit);
602*658eb9e1SMichael Kruse</pre>
603*658eb9e1SMichael Kruse -  Converts the magnitude of `z` to unsigned binary, writing at most `limit`
604*658eb9e1SMichael Kruse    bytes into the given `buf`.  The sign of `z` is ignored, but `z` is not
605*658eb9e1SMichael Kruse    modified.  Returns `MP_TRUNC` if the buffer limit was too small to contain
606*658eb9e1SMichael Kruse    the whole value.  If this occurs, the contents of `buf` will be effectively
607*658eb9e1SMichael Kruse    garbage, as the function uses the buffer as scratch space during
608*658eb9e1SMichael Kruse    conversion.
609*658eb9e1SMichael Kruse
610*658eb9e1SMichael Kruse    The binary representation of `z` is in base-256 with digits ordered from
611*658eb9e1SMichael Kruse    most significant to least significant (network byte ordering).
612*658eb9e1SMichael Kruse
613*658eb9e1SMichael Kruse------------
614*658eb9e1SMichael Kruse<a id="mp_int_read_unsigned"></a><pre>
615*658eb9e1SMichael Krusemp_result <a href="imath.h#L407">mp_int_read_unsigned</a>(mp_int z, unsigned char *buf, int len);
616*658eb9e1SMichael Kruse</pre>
617*658eb9e1SMichael Kruse -  Reads an unsigned binary value from `buf` into `z`, where `len` is the
618*658eb9e1SMichael Kruse    length of the buffer. The contents of `buf` are not modified during
619*658eb9e1SMichael Kruse    processing.
620*658eb9e1SMichael Kruse
621*658eb9e1SMichael Kruse------------
622*658eb9e1SMichael Kruse<a id="mp_int_unsigned_len"></a><pre>
623*658eb9e1SMichael Krusemp_result <a href="imath.h#L411">mp_int_unsigned_len</a>(mp_int z);
624*658eb9e1SMichael Kruse</pre>
625*658eb9e1SMichael Kruse -  Returns the number of bytes required to represent `z` as an unsigned binary
626*658eb9e1SMichael Kruse    value in base 256.
627*658eb9e1SMichael Kruse
628*658eb9e1SMichael Kruse
629*658eb9e1SMichael Kruse
630*658eb9e1SMichael Kruse### Other Functions
631*658eb9e1SMichael Kruse
632*658eb9e1SMichael KruseOrdinarily, integer multiplication and squaring are done using the simple
633*658eb9e1SMichael Krusequadratic "schoolbook" algorithm.  However, for sufficiently large values,
634*658eb9e1SMichael Krusethere is a more efficient algorithm usually attributed to Karatsuba and Ofman
635*658eb9e1SMichael Krusethat is usually faster.  See Knuth Vol. 2 for more details about how this
636*658eb9e1SMichael Krusealgorithm works.
637*658eb9e1SMichael Kruse
638*658eb9e1SMichael KruseThe breakpoint between the "normal" and the recursive algorithm is controlled
639*658eb9e1SMichael Kruseby a static digit threshold defined in `imath.c`. Values with fewer significant
640*658eb9e1SMichael Krusedigits use the standard algorithm.  This value can be modified by calling
641*658eb9e1SMichael Kruse`mp_int_multiply_threshold(n)`.  The `imtimer` program and the
642*658eb9e1SMichael Kruse`findthreshold.py` script (Python) can help you find a suitable value for for
643*658eb9e1SMichael Kruseyour particular platform.
644*658eb9e1SMichael Kruse
645*658eb9e1SMichael Kruse------------
646*658eb9e1SMichael Kruse<a id="mp_error_string"></a><pre>
647*658eb9e1SMichael Kruseconst char *<a href="imath.h#L416">mp_error_string</a>(mp_result res);
648*658eb9e1SMichael Kruse</pre>
649*658eb9e1SMichael Kruse -  Returns a pointer to a brief, human-readable, zero-terminated string
650*658eb9e1SMichael Kruse    describing `res`. The returned string is statically allocated and must not
651*658eb9e1SMichael Kruse    be freed by the caller.
652*658eb9e1SMichael Kruse
653*658eb9e1SMichael Kruse
654*658eb9e1SMichael Kruse
655*658eb9e1SMichael Kruse## Rational Arithmetic
656*658eb9e1SMichael Kruse
657*658eb9e1SMichael Kruse------------
658*658eb9e1SMichael Kruse<a id="mp_rat_init"></a><pre>
659*658eb9e1SMichael Krusemp_result <a href="imrat.h#L59">mp_rat_init</a>(mp_rat r);
660*658eb9e1SMichael Kruse</pre>
661*658eb9e1SMichael Kruse -  Initializes `r` with 1-digit precision and sets it to zero. This function
662*658eb9e1SMichael Kruse    cannot fail unless `r` is NULL.
663*658eb9e1SMichael Kruse
664*658eb9e1SMichael Kruse------------
665*658eb9e1SMichael Kruse<a id="mp_rat_alloc"></a><pre>
666*658eb9e1SMichael Krusemp_rat <a href="imrat.h#L63">mp_rat_alloc</a>(void);
667*658eb9e1SMichael Kruse</pre>
668*658eb9e1SMichael Kruse -  Allocates a fresh zero-valued `mpq_t` on the heap, returning NULL in case
669*658eb9e1SMichael Kruse    of error. The only possible error is out-of-memory.
670*658eb9e1SMichael Kruse
671*658eb9e1SMichael Kruse------------
672*658eb9e1SMichael Kruse<a id="mp_rat_reduce"></a><pre>
673*658eb9e1SMichael Krusemp_result <a href="imrat.h#L69">mp_rat_reduce</a>(mp_rat r);
674*658eb9e1SMichael Kruse</pre>
675*658eb9e1SMichael Kruse -  Reduces `r` in-place to lowest terms and canonical form.
676*658eb9e1SMichael Kruse
677*658eb9e1SMichael Kruse    Zero is represented as 0/1, one as 1/1, and signs are adjusted so that the
678*658eb9e1SMichael Kruse    sign of the value is carried by the numerator.
679*658eb9e1SMichael Kruse
680*658eb9e1SMichael Kruse------------
681*658eb9e1SMichael Kruse<a id="mp_rat_init_size"></a><pre>
682*658eb9e1SMichael Krusemp_result <a href="imrat.h#L76">mp_rat_init_size</a>(mp_rat r, mp_size n_prec, mp_size d_prec);
683*658eb9e1SMichael Kruse</pre>
684*658eb9e1SMichael Kruse -  Initializes `r` with at least `n_prec` digits of storage for the numerator
685*658eb9e1SMichael Kruse    and `d_prec` digits of storage for the denominator, and value zero.
686*658eb9e1SMichael Kruse
687*658eb9e1SMichael Kruse    If either precision is zero, the default precision is used, rounded up to
688*658eb9e1SMichael Kruse    the nearest word size.
689*658eb9e1SMichael Kruse
690*658eb9e1SMichael Kruse------------
691*658eb9e1SMichael Kruse<a id="mp_rat_init_copy"></a><pre>
692*658eb9e1SMichael Krusemp_result <a href="imrat.h#L80">mp_rat_init_copy</a>(mp_rat r, mp_rat old);
693*658eb9e1SMichael Kruse</pre>
694*658eb9e1SMichael Kruse -  Initializes `r` to be a copy of an already-initialized value in `old`. The
695*658eb9e1SMichael Kruse    new copy does not share storage with the original.
696*658eb9e1SMichael Kruse
697*658eb9e1SMichael Kruse------------
698*658eb9e1SMichael Kruse<a id="mp_rat_set_value"></a><pre>
699*658eb9e1SMichael Krusemp_result <a href="imrat.h#L84">mp_rat_set_value</a>(mp_rat r, mp_small numer, mp_small denom);
700*658eb9e1SMichael Kruse</pre>
701*658eb9e1SMichael Kruse -  Sets the value of `r` to the ratio of signed `numer` to signed `denom`.  It
702*658eb9e1SMichael Kruse    returns `MP_UNDEF` if `denom` is zero.
703*658eb9e1SMichael Kruse
704*658eb9e1SMichael Kruse------------
705*658eb9e1SMichael Kruse<a id="mp_rat_set_uvalue"></a><pre>
706*658eb9e1SMichael Krusemp_result <a href="imrat.h#L88">mp_rat_set_uvalue</a>(mp_rat r, mp_usmall numer, mp_usmall denom);
707*658eb9e1SMichael Kruse</pre>
708*658eb9e1SMichael Kruse -  Sets the value of `r` to the ratio of unsigned `numer` to unsigned
709*658eb9e1SMichael Kruse    `denom`. It returns `MP_UNDEF` if `denom` is zero.
710*658eb9e1SMichael Kruse
711*658eb9e1SMichael Kruse------------
712*658eb9e1SMichael Kruse<a id="mp_rat_clear"></a><pre>
713*658eb9e1SMichael Krusevoid <a href="imrat.h#L91">mp_rat_clear</a>(mp_rat r);
714*658eb9e1SMichael Kruse</pre>
715*658eb9e1SMichael Kruse -  Releases the storage used by `r`.
716*658eb9e1SMichael Kruse
717*658eb9e1SMichael Kruse------------
718*658eb9e1SMichael Kruse<a id="mp_rat_free"></a><pre>
719*658eb9e1SMichael Krusevoid <a href="imrat.h#L95">mp_rat_free</a>(mp_rat r);
720*658eb9e1SMichael Kruse</pre>
721*658eb9e1SMichael Kruse -  Releases the storage used by `r` and also `r` itself.
722*658eb9e1SMichael Kruse    This should only be used for `r` allocated by `mp_rat_alloc()`.
723*658eb9e1SMichael Kruse
724*658eb9e1SMichael Kruse------------
725*658eb9e1SMichael Kruse<a id="mp_rat_numer"></a><pre>
726*658eb9e1SMichael Krusemp_result <a href="imrat.h#L98">mp_rat_numer</a>(mp_rat r, mp_int z);
727*658eb9e1SMichael Kruse</pre>
728*658eb9e1SMichael Kruse -  Sets `z` to a copy of the numerator of `r`.
729*658eb9e1SMichael Kruse
730*658eb9e1SMichael Kruse------------
731*658eb9e1SMichael Kruse<a id="mp_rat_numer_ref"></a><pre>
732*658eb9e1SMichael Krusemp_int <a href="imrat.h#L101">mp_rat_numer_ref</a>(mp_rat r);
733*658eb9e1SMichael Kruse</pre>
734*658eb9e1SMichael Kruse -  Returns a pointer to the numerator of `r`.
735*658eb9e1SMichael Kruse
736*658eb9e1SMichael Kruse------------
737*658eb9e1SMichael Kruse<a id="mp_rat_denom"></a><pre>
738*658eb9e1SMichael Krusemp_result <a href="imrat.h#L104">mp_rat_denom</a>(mp_rat r, mp_int z);
739*658eb9e1SMichael Kruse</pre>
740*658eb9e1SMichael Kruse -  Sets `z` to a copy of the denominator of `r`.
741*658eb9e1SMichael Kruse
742*658eb9e1SMichael Kruse------------
743*658eb9e1SMichael Kruse<a id="mp_rat_denom_ref"></a><pre>
744*658eb9e1SMichael Krusemp_int <a href="imrat.h#L107">mp_rat_denom_ref</a>(mp_rat r);
745*658eb9e1SMichael Kruse</pre>
746*658eb9e1SMichael Kruse -  Returns a pointer to the denominator of `r`.
747*658eb9e1SMichael Kruse
748*658eb9e1SMichael Kruse------------
749*658eb9e1SMichael Kruse<a id="mp_rat_sign"></a><pre>
750*658eb9e1SMichael Krusemp_sign <a href="imrat.h#L110">mp_rat_sign</a>(mp_rat r);
751*658eb9e1SMichael Kruse</pre>
752*658eb9e1SMichael Kruse -  Reports the sign of `r`.
753*658eb9e1SMichael Kruse
754*658eb9e1SMichael Kruse------------
755*658eb9e1SMichael Kruse<a id="mp_rat_copy"></a><pre>
756*658eb9e1SMichael Krusemp_result <a href="imrat.h#L115">mp_rat_copy</a>(mp_rat a, mp_rat c);
757*658eb9e1SMichael Kruse</pre>
758*658eb9e1SMichael Kruse -  Sets `c` to a copy of the value of `a`. No new memory is allocated unless a
759*658eb9e1SMichael Kruse    term of `a` has more significant digits than the corresponding term of `c`
760*658eb9e1SMichael Kruse    has allocated.
761*658eb9e1SMichael Kruse
762*658eb9e1SMichael Kruse------------
763*658eb9e1SMichael Kruse<a id="mp_rat_zero"></a><pre>
764*658eb9e1SMichael Krusevoid <a href="imrat.h#L118">mp_rat_zero</a>(mp_rat r);
765*658eb9e1SMichael Kruse</pre>
766*658eb9e1SMichael Kruse -  Sets `r` to zero. The allocated storage of `r` is not changed.
767*658eb9e1SMichael Kruse
768*658eb9e1SMichael Kruse------------
769*658eb9e1SMichael Kruse<a id="mp_rat_abs"></a><pre>
770*658eb9e1SMichael Krusemp_result <a href="imrat.h#L121">mp_rat_abs</a>(mp_rat a, mp_rat c);
771*658eb9e1SMichael Kruse</pre>
772*658eb9e1SMichael Kruse -  Sets `c` to the absolute value of `a`.
773*658eb9e1SMichael Kruse
774*658eb9e1SMichael Kruse------------
775*658eb9e1SMichael Kruse<a id="mp_rat_neg"></a><pre>
776*658eb9e1SMichael Krusemp_result <a href="imrat.h#L124">mp_rat_neg</a>(mp_rat a, mp_rat c);
777*658eb9e1SMichael Kruse</pre>
778*658eb9e1SMichael Kruse -  Sets `c` to the absolute value of `a`.
779*658eb9e1SMichael Kruse
780*658eb9e1SMichael Kruse------------
781*658eb9e1SMichael Kruse<a id="mp_rat_recip"></a><pre>
782*658eb9e1SMichael Krusemp_result <a href="imrat.h#L128">mp_rat_recip</a>(mp_rat a, mp_rat c);
783*658eb9e1SMichael Kruse</pre>
784*658eb9e1SMichael Kruse -  Sets `c` to the reciprocal of `a` if the reciprocal is defined.
785*658eb9e1SMichael Kruse    It returns `MP_UNDEF` if `a` is zero.
786*658eb9e1SMichael Kruse
787*658eb9e1SMichael Kruse------------
788*658eb9e1SMichael Kruse<a id="mp_rat_add"></a><pre>
789*658eb9e1SMichael Krusemp_result <a href="imrat.h#L131">mp_rat_add</a>(mp_rat a, mp_rat b, mp_rat c);
790*658eb9e1SMichael Kruse</pre>
791*658eb9e1SMichael Kruse -  Sets `c` to the sum of `a` and `b`.
792*658eb9e1SMichael Kruse
793*658eb9e1SMichael Kruse------------
794*658eb9e1SMichael Kruse<a id="mp_rat_sub"></a><pre>
795*658eb9e1SMichael Krusemp_result <a href="imrat.h#L134">mp_rat_sub</a>(mp_rat a, mp_rat b, mp_rat c);
796*658eb9e1SMichael Kruse</pre>
797*658eb9e1SMichael Kruse -  Sets `c` to the difference of `a` less `b`.
798*658eb9e1SMichael Kruse
799*658eb9e1SMichael Kruse------------
800*658eb9e1SMichael Kruse<a id="mp_rat_mul"></a><pre>
801*658eb9e1SMichael Krusemp_result <a href="imrat.h#L137">mp_rat_mul</a>(mp_rat a, mp_rat b, mp_rat c);
802*658eb9e1SMichael Kruse</pre>
803*658eb9e1SMichael Kruse -  Sets `c` to the product of `a` and `b`.
804*658eb9e1SMichael Kruse
805*658eb9e1SMichael Kruse------------
806*658eb9e1SMichael Kruse<a id="mp_rat_div"></a><pre>
807*658eb9e1SMichael Krusemp_result <a href="imrat.h#L141">mp_rat_div</a>(mp_rat a, mp_rat b, mp_rat c);
808*658eb9e1SMichael Kruse</pre>
809*658eb9e1SMichael Kruse -  Sets `c` to the ratio `a / b` if that ratio is defined.
810*658eb9e1SMichael Kruse    It returns `MP_UNDEF` if `b` is zero.
811*658eb9e1SMichael Kruse
812*658eb9e1SMichael Kruse------------
813*658eb9e1SMichael Kruse<a id="mp_rat_add_int"></a><pre>
814*658eb9e1SMichael Krusemp_result <a href="imrat.h#L144">mp_rat_add_int</a>(mp_rat a, mp_int b, mp_rat c);
815*658eb9e1SMichael Kruse</pre>
816*658eb9e1SMichael Kruse -  Sets `c` to the sum of `a` and integer `b`.
817*658eb9e1SMichael Kruse
818*658eb9e1SMichael Kruse------------
819*658eb9e1SMichael Kruse<a id="mp_rat_sub_int"></a><pre>
820*658eb9e1SMichael Krusemp_result <a href="imrat.h#L147">mp_rat_sub_int</a>(mp_rat a, mp_int b, mp_rat c);
821*658eb9e1SMichael Kruse</pre>
822*658eb9e1SMichael Kruse -  Sets `c` to the difference of `a` less integer `b`.
823*658eb9e1SMichael Kruse
824*658eb9e1SMichael Kruse------------
825*658eb9e1SMichael Kruse<a id="mp_rat_mul_int"></a><pre>
826*658eb9e1SMichael Krusemp_result <a href="imrat.h#L150">mp_rat_mul_int</a>(mp_rat a, mp_int b, mp_rat c);
827*658eb9e1SMichael Kruse</pre>
828*658eb9e1SMichael Kruse -  Sets `c` to the product of `a` and integer `b`.
829*658eb9e1SMichael Kruse
830*658eb9e1SMichael Kruse------------
831*658eb9e1SMichael Kruse<a id="mp_rat_div_int"></a><pre>
832*658eb9e1SMichael Krusemp_result <a href="imrat.h#L154">mp_rat_div_int</a>(mp_rat a, mp_int b, mp_rat c);
833*658eb9e1SMichael Kruse</pre>
834*658eb9e1SMichael Kruse -  Sets `c` to the ratio `a / b` if that ratio is defined.
835*658eb9e1SMichael Kruse    It returns `MP_UNDEF` if `b` is zero.
836*658eb9e1SMichael Kruse
837*658eb9e1SMichael Kruse------------
838*658eb9e1SMichael Kruse<a id="mp_rat_expt"></a><pre>
839*658eb9e1SMichael Krusemp_result <a href="imrat.h#L158">mp_rat_expt</a>(mp_rat a, mp_small b, mp_rat c);
840*658eb9e1SMichael Kruse</pre>
841*658eb9e1SMichael Kruse -  Sets `c` to the value of `a` raised to the `b` power.
842*658eb9e1SMichael Kruse    It returns `MP_RANGE` if `b < 0`.
843*658eb9e1SMichael Kruse
844*658eb9e1SMichael Kruse------------
845*658eb9e1SMichael Kruse<a id="mp_rat_compare"></a><pre>
846*658eb9e1SMichael Kruseint <a href="imrat.h#L161">mp_rat_compare</a>(mp_rat a, mp_rat b);
847*658eb9e1SMichael Kruse</pre>
848*658eb9e1SMichael Kruse -  Returns the comparator of `a` and `b`.
849*658eb9e1SMichael Kruse
850*658eb9e1SMichael Kruse------------
851*658eb9e1SMichael Kruse<a id="mp_rat_compare_unsigned"></a><pre>
852*658eb9e1SMichael Kruseint <a href="imrat.h#L165">mp_rat_compare_unsigned</a>(mp_rat a, mp_rat b);
853*658eb9e1SMichael Kruse</pre>
854*658eb9e1SMichael Kruse -  Returns the comparator of the magnitudes of `a` and `b`, disregarding their
855*658eb9e1SMichael Kruse    signs. Neither `a` nor `b` is modified by the comparison.
856*658eb9e1SMichael Kruse
857*658eb9e1SMichael Kruse------------
858*658eb9e1SMichael Kruse<a id="mp_rat_compare_zero"></a><pre>
859*658eb9e1SMichael Kruseint <a href="imrat.h#L168">mp_rat_compare_zero</a>(mp_rat r);
860*658eb9e1SMichael Kruse</pre>
861*658eb9e1SMichael Kruse -  Returns the comparator of `r` and zero.
862*658eb9e1SMichael Kruse
863*658eb9e1SMichael Kruse------------
864*658eb9e1SMichael Kruse<a id="mp_rat_compare_value"></a><pre>
865*658eb9e1SMichael Kruseint <a href="imrat.h#L172">mp_rat_compare_value</a>(mp_rat r, mp_small n, mp_small d);
866*658eb9e1SMichael Kruse</pre>
867*658eb9e1SMichael Kruse -  Returns the comparator of `r` and the signed ratio `n / d`.
868*658eb9e1SMichael Kruse    It returns `MP_UNDEF` if `d` is zero.
869*658eb9e1SMichael Kruse
870*658eb9e1SMichael Kruse------------
871*658eb9e1SMichael Kruse<a id="mp_rat_is_integer"></a><pre>
872*658eb9e1SMichael Krusebool <a href="imrat.h#L175">mp_rat_is_integer</a>(mp_rat r);
873*658eb9e1SMichael Kruse</pre>
874*658eb9e1SMichael Kruse -  Reports whether `r` is an integer, having canonical denominator 1.
875*658eb9e1SMichael Kruse
876*658eb9e1SMichael Kruse------------
877*658eb9e1SMichael Kruse<a id="mp_rat_to_ints"></a><pre>
878*658eb9e1SMichael Krusemp_result <a href="imrat.h#L180">mp_rat_to_ints</a>(mp_rat r, mp_small *num, mp_small *den);
879*658eb9e1SMichael Kruse</pre>
880*658eb9e1SMichael Kruse -  Reports whether the numerator and denominator of `r` can be represented as
881*658eb9e1SMichael Kruse    small signed integers, and if so stores the corresponding values to `num`
882*658eb9e1SMichael Kruse    and `den`. It returns `MP_RANGE` if either cannot be so represented.
883*658eb9e1SMichael Kruse
884*658eb9e1SMichael Kruse------------
885*658eb9e1SMichael Kruse<a id="mp_rat_to_string"></a><pre>
886*658eb9e1SMichael Krusemp_result <a href="imrat.h#L186">mp_rat_to_string</a>(mp_rat r, mp_size radix, char *str, int limit);
887*658eb9e1SMichael Kruse</pre>
888*658eb9e1SMichael Kruse -  Converts `r` to a zero-terminated string of the format `"n/d"` with `n` and
889*658eb9e1SMichael Kruse    `d` in the specified radix and writing no more than `limit` bytes to the
890*658eb9e1SMichael Kruse    given output buffer `str`. The output of the numerator includes a sign flag
891*658eb9e1SMichael Kruse    if `r` is negative.  Requires `MP_MIN_RADIX <= radix <= MP_MAX_RADIX`.
892*658eb9e1SMichael Kruse
893*658eb9e1SMichael Kruse------------
894*658eb9e1SMichael Kruse<a id="mp_rat_to_decimal"></a><pre>
895*658eb9e1SMichael Krusemp_result <a href="imrat.h#L215">mp_rat_to_decimal</a>(mp_rat r, mp_size radix, mp_size prec, mp_round_mode round, char *str, int limit);
896*658eb9e1SMichael Kruse</pre>
897*658eb9e1SMichael Kruse -  Converts the value of `r` to a string in decimal-point notation with the
898*658eb9e1SMichael Kruse    specified radix, writing no more than `limit` bytes of data to the given
899*658eb9e1SMichael Kruse    output buffer.  It generates `prec` digits of precision, and requires
900*658eb9e1SMichael Kruse    `MP_MIN_RADIX <= radix <= MP_MAX_RADIX`.
901*658eb9e1SMichael Kruse
902*658eb9e1SMichael Kruse    Ratios usually must be rounded when they are being converted for output as
903*658eb9e1SMichael Kruse    a decimal value.  There are four rounding modes currently supported:
904*658eb9e1SMichael Kruse
905*658eb9e1SMichael Kruse    ```
906*658eb9e1SMichael Kruse      MP_ROUND_DOWN
907*658eb9e1SMichael Kruse        Truncates the value toward zero.
908*658eb9e1SMichael Kruse        Example:  12.009 to 2dp becomes 12.00
909*658eb9e1SMichael Kruse    ```
910*658eb9e1SMichael Kruse
911*658eb9e1SMichael Kruse    ```
912*658eb9e1SMichael Kruse      MP_ROUND_UP
913*658eb9e1SMichael Kruse        Rounds the value away from zero:
914*658eb9e1SMichael Kruse        Example:  12.001 to 2dp becomes 12.01, but
915*658eb9e1SMichael Kruse                  12.000 to 2dp remains 12.00
916*658eb9e1SMichael Kruse    ```
917*658eb9e1SMichael Kruse
918*658eb9e1SMichael Kruse    ```
919*658eb9e1SMichael Kruse      MP_ROUND_HALF_DOWN
920*658eb9e1SMichael Kruse         Rounds the value to nearest digit, half goes toward zero.
921*658eb9e1SMichael Kruse         Example:  12.005 to 2dp becomes 12.00, but
922*658eb9e1SMichael Kruse                   12.006 to 2dp becomes 12.01
923*658eb9e1SMichael Kruse    ```
924*658eb9e1SMichael Kruse
925*658eb9e1SMichael Kruse    ```
926*658eb9e1SMichael Kruse      MP_ROUND_HALF_UP
927*658eb9e1SMichael Kruse         Rounds the value to nearest digit, half rounds upward.
928*658eb9e1SMichael Kruse         Example:  12.005 to 2dp becomes 12.01, but
929*658eb9e1SMichael Kruse                   12.004 to 2dp becomes 12.00
930*658eb9e1SMichael Kruse    ```
931*658eb9e1SMichael Kruse
932*658eb9e1SMichael Kruse------------
933*658eb9e1SMichael Kruse<a id="mp_rat_string_len"></a><pre>
934*658eb9e1SMichael Krusemp_result <a href="imrat.h#L221">mp_rat_string_len</a>(mp_rat r, mp_size radix);
935*658eb9e1SMichael Kruse</pre>
936*658eb9e1SMichael Kruse -  Reports the minimum number of characters required to represent `r` as a
937*658eb9e1SMichael Kruse    zero-terminated string in the given `radix`.
938*658eb9e1SMichael Kruse    Requires `MP_MIN_RADIX <= radix <= MP_MAX_RADIX`.
939*658eb9e1SMichael Kruse
940*658eb9e1SMichael Kruse------------
941*658eb9e1SMichael Kruse<a id="mp_rat_decimal_len"></a><pre>
942*658eb9e1SMichael Krusemp_result <a href="imrat.h#L226">mp_rat_decimal_len</a>(mp_rat r, mp_size radix, mp_size prec);
943*658eb9e1SMichael Kruse</pre>
944*658eb9e1SMichael Kruse -  Reports the length in bytes of the buffer needed to convert `r` using the
945*658eb9e1SMichael Kruse    `mp_rat_to_decimal()` function with the specified `radix` and `prec`. The
946*658eb9e1SMichael Kruse    buffer size estimate may slightly exceed the actual required capacity.
947*658eb9e1SMichael Kruse
948*658eb9e1SMichael Kruse------------
949*658eb9e1SMichael Kruse<a id="mp_rat_read_string"></a><pre>
950*658eb9e1SMichael Krusemp_result <a href="imrat.h#L231">mp_rat_read_string</a>(mp_rat r, mp_size radix, const char *str);
951*658eb9e1SMichael Kruse</pre>
952*658eb9e1SMichael Kruse -  Sets `r` to the value represented by a zero-terminated string `str` in the
953*658eb9e1SMichael Kruse    format `"n/d"` including a sign flag. It returns `MP_UNDEF` if the encoded
954*658eb9e1SMichael Kruse    denominator has value zero.
955*658eb9e1SMichael Kruse
956*658eb9e1SMichael Kruse------------
957*658eb9e1SMichael Kruse<a id="mp_rat_read_cstring"></a><pre>
958*658eb9e1SMichael Krusemp_result <a href="imrat.h#L238">mp_rat_read_cstring</a>(mp_rat r, mp_size radix, const char *str, char **end);
959*658eb9e1SMichael Kruse</pre>
960*658eb9e1SMichael Kruse -  Sets `r` to the value represented by a zero-terminated string `str` in the
961*658eb9e1SMichael Kruse    format `"n/d"` including a sign flag. It returns `MP_UNDEF` if the encoded
962*658eb9e1SMichael Kruse    denominator has value zero. If `end` is not NULL then `*end` is set to
963*658eb9e1SMichael Kruse    point to the first unconsumed character in the string, after parsing.
964*658eb9e1SMichael Kruse
965*658eb9e1SMichael Kruse------------
966*658eb9e1SMichael Kruse<a id="mp_rat_read_ustring"></a><pre>
967*658eb9e1SMichael Krusemp_result <a href="imrat.h#L252">mp_rat_read_ustring</a>(mp_rat r, mp_size radix, const char *str, char **end);
968*658eb9e1SMichael Kruse</pre>
969*658eb9e1SMichael Kruse -  Sets `r` to the value represented by a zero-terminated string `str` having
970*658eb9e1SMichael Kruse    one of the following formats, each with an optional leading sign flag:
971*658eb9e1SMichael Kruse
972*658eb9e1SMichael Kruse    ```
973*658eb9e1SMichael Kruse       n         : integer format, e.g. "123"
974*658eb9e1SMichael Kruse       n/d       : ratio format, e.g., "-12/5"
975*658eb9e1SMichael Kruse       z.ffff    : decimal format, e.g., "1.627"
976*658eb9e1SMichael Kruse    ```
977*658eb9e1SMichael Kruse
978*658eb9e1SMichael Kruse    It returns `MP_UNDEF` if the effective denominator is zero. If `end` is not
979*658eb9e1SMichael Kruse    NULL then `*end` is set to point to the first unconsumed character in the
980*658eb9e1SMichael Kruse    string, after parsing.
981*658eb9e1SMichael Kruse
982*658eb9e1SMichael Kruse------------
983*658eb9e1SMichael Kruse<a id="mp_rat_read_decimal"></a><pre>
984*658eb9e1SMichael Krusemp_result <a href="imrat.h#L258">mp_rat_read_decimal</a>(mp_rat r, mp_size radix, const char *str);
985*658eb9e1SMichael Kruse</pre>
986*658eb9e1SMichael Kruse -  Sets `r` to the value represented by a zero-terminated string `str` in the
987*658eb9e1SMichael Kruse    format `"z.ffff"` including a sign flag. It returns `MP_UNDEF` if the
988*658eb9e1SMichael Kruse    effective denominator.
989*658eb9e1SMichael Kruse
990*658eb9e1SMichael Kruse------------
991*658eb9e1SMichael Kruse<a id="mp_rat_read_cdecimal"></a><pre>
992*658eb9e1SMichael Krusemp_result <a href="imrat.h#L264">mp_rat_read_cdecimal</a>(mp_rat r, mp_size radix, const char *str, char **end);
993*658eb9e1SMichael Kruse</pre>
994*658eb9e1SMichael Kruse -  Sets `r` to the value represented by a zero-terminated string `str` in the
995*658eb9e1SMichael Kruse    format `"z.ffff"` including a sign flag. It returns `MP_UNDEF` if the
996*658eb9e1SMichael Kruse    effective denominator. If `end` is not NULL then `*end` is set to point to
997*658eb9e1SMichael Kruse    the first unconsumed character in the string, after parsing.
998*658eb9e1SMichael Kruse
999*658eb9e1SMichael Kruse
1000*658eb9e1SMichael Kruse
1001*658eb9e1SMichael Kruse## Representation Details
1002*658eb9e1SMichael Kruse
1003*658eb9e1SMichael Kruse> NOTE: You do not need to read this section to use IMath.  This is provided
1004*658eb9e1SMichael Kruse> for the benefit of developers wishing to extend or modify the internals of
1005*658eb9e1SMichael Kruse> the library.
1006*658eb9e1SMichael Kruse
1007*658eb9e1SMichael KruseIMath uses a signed magnitude representation for arbitrary precision integers.
1008*658eb9e1SMichael KruseThe magnitude is represented as an array of radix-R digits in increasing order
1009*658eb9e1SMichael Kruseof significance; the value of R is chosen to be half the size of the largest
1010*658eb9e1SMichael Kruseavailable unsigned integer type, so typically 16 or 32 bits.  Digits are
1011*658eb9e1SMichael Kruserepresented as mp_digit, which must be an unsigned integral type.
1012*658eb9e1SMichael Kruse
1013*658eb9e1SMichael KruseDigit arrays are allocated using `malloc(3)` and `realloc(3)`.  Because this
1014*658eb9e1SMichael Krusecan be an expensive operation, the library takes pains to avoid allocation as
1015*658eb9e1SMichael Krusemuch as possible.  For this reason, the `mpz_t` structure distinguishes between
1016*658eb9e1SMichael Krusehow many digits are allocated and how many digits are actually consumed by the
1017*658eb9e1SMichael Kruserepresentation.  The fields of an `mpz_t` are:
1018*658eb9e1SMichael Kruse
1019*658eb9e1SMichael Kruse    mp_digit    single;  /* single-digit value (see note) */
1020*658eb9e1SMichael Kruse    mp_digit   *digits;  /* array of digits               */
1021*658eb9e1SMichael Kruse    mp_size     alloc;   /* how many digits are allocated */
1022*658eb9e1SMichael Kruse    mp_size     used;    /* how many digits are in use    */
1023*658eb9e1SMichael Kruse    mp_sign     sign;    /* the sign of the value         */
1024*658eb9e1SMichael Kruse
1025*658eb9e1SMichael KruseThe elements of `digits` at indices less than `used` are the significant
1026*658eb9e1SMichael Krusefigures of the value; the elements at indices greater than or equal to `used`
1027*658eb9e1SMichael Kruseare undefined (and may contain garbage).  At all times, `used` must be at least
1028*658eb9e1SMichael Kruse1 and at most `alloc`.
1029*658eb9e1SMichael Kruse
1030*658eb9e1SMichael KruseTo avoid interaction with the memory allocator, single-digit values are stored
1031*658eb9e1SMichael Krusedirectly in the `mpz_t` structure, in the `single` field.  The semantics of
1032*658eb9e1SMichael Kruseaccess are the same as the more general case.
1033*658eb9e1SMichael Kruse
1034*658eb9e1SMichael KruseThe number of digits allocated for an `mpz_t` is referred to in the library
1035*658eb9e1SMichael Krusedocumentation as its "precision".  Operations that affect an `mpz_t` cause
1036*658eb9e1SMichael Kruseprecision to increase as needed.  In any case, all allocations are measured in
1037*658eb9e1SMichael Krusedigits, and rounded up to the nearest `mp_word` boundary.  There is a default
1038*658eb9e1SMichael Kruseminimum precision stored as a static constant default_precision (`imath.c`).
1039*658eb9e1SMichael KruseThis value can be set using `mp_int_default_precision(n)`.
1040*658eb9e1SMichael Kruse
1041*658eb9e1SMichael KruseNote that the allocated size of an `mpz_t` can only grow; the library never
1042*658eb9e1SMichael Krusereallocates in order to decrease the size.  A simple way to do so explicitly is
1043*658eb9e1SMichael Kruseto use `mp_int_init_copy()`, as in:
1044*658eb9e1SMichael Kruse
1045*658eb9e1SMichael Kruse```
1046*658eb9e1SMichael Krusempz_t big, new;
1047*658eb9e1SMichael Kruse
1048*658eb9e1SMichael Kruse/* ... */
1049*658eb9e1SMichael Krusemp_int_init_copy(&new, &big);
1050*658eb9e1SMichael Krusemp_int_swap(&new, &big);
1051*658eb9e1SMichael Krusemp_int_clear(&new);
1052*658eb9e1SMichael Kruse```
1053*658eb9e1SMichael Kruse
1054*658eb9e1SMichael KruseThe value of `sign` is 0 for positive values and zero, 1 for negative values.
1055*658eb9e1SMichael KruseConstants `MP_ZPOS` and `MP_NEG` are defined for these; no other sign values
1056*658eb9e1SMichael Kruseare used.
1057*658eb9e1SMichael Kruse
1058*658eb9e1SMichael KruseIf you are adding to this library, you should be careful to preserve the
1059*658eb9e1SMichael Kruseconvention that inputs and outputs can overlap, as described above.  So, for
1060*658eb9e1SMichael Kruseexample, `mp_int_add(a, a, a)` is legal.  Often, this means you must maintain
1061*658eb9e1SMichael Kruseone or more temporary mpz_t structures for intermediate values.  The private
1062*658eb9e1SMichael Krusemacros `DECLARE_TEMP(N)`, `CLEANUP_TEMP()`, and `TEMP(K)` can be used to
1063*658eb9e1SMichael Krusemaintain a conventional structure like this:
1064*658eb9e1SMichael Kruse
1065*658eb9e1SMichael Kruse```c
1066*658eb9e1SMichael Kruse{
1067*658eb9e1SMichael Kruse  /* Declare how many temp values you need.
1068*658eb9e1SMichael Kruse	 Use TEMP(i) to access the ith value (0-indexed). */
1069*658eb9e1SMichael Kruse  DECLARE_TEMP(8);
1070*658eb9e1SMichael Kruse  ...
1071*658eb9e1SMichael Kruse
1072*658eb9e1SMichael Kruse  /* Perform actions that must return MP_OK or fail. */
1073*658eb9e1SMichael Kruse  REQUIRE(mp_int_copy(x, TEMP(1)));
1074*658eb9e1SMichael Kruse  ...
1075*658eb9e1SMichael Kruse  REQUIRE(mp_int_expt(TEMP(1), TEMP(2), TEMP(3)));
1076*658eb9e1SMichael Kruse  ...
1077*658eb9e1SMichael Kruse
1078*658eb9e1SMichael Kruse  /* You can also use REQUIRE directly for more complex cases. */
1079*658eb9e1SMichael Kruse  if (some_difficult_question(TEMP(3)) != answer(x)) {
1080*658eb9e1SMichael Kruse	REQUIRE(MP_RANGE);  /* falls through to cleanup (below) */
1081*658eb9e1SMichael Kruse  }
1082*658eb9e1SMichael Kruse
1083*658eb9e1SMichael Kruse  /* Ensure temporary values are cleaned up at exit.
1084*658eb9e1SMichael Kruse
1085*658eb9e1SMichael Kruse     If control reaches here via a REQUIRE failure, the code below
1086*658eb9e1SMichael Kruse	 the cleanup will not be executed.
1087*658eb9e1SMichael Kruse   */
1088*658eb9e1SMichael Kruse  CLEANUP_TEMP();
1089*658eb9e1SMichael Kruse  return MP_OK;
1090*658eb9e1SMichael Kruse}
1091*658eb9e1SMichael Kruse```
1092*658eb9e1SMichael Kruse
1093*658eb9e1SMichael KruseUnder the covers, these macros are just maintaining an array of `mpz_t` values,
1094*658eb9e1SMichael Kruseand a jump label to handle cleanup. You may only have one `DECLARE_TEMP` and
1095*658eb9e1SMichael Kruseits corresponding `CLEANUP_TEMP` per function body.
1096*658eb9e1SMichael Kruse
1097*658eb9e1SMichael Kruse"Small" integer values are represented by the types `mp_small` and `mp_usmall`,
1098*658eb9e1SMichael Krusewhich are mapped to appropriately-sized types on the host system.  The default
1099*658eb9e1SMichael Krusefor `mp_small` is "long" and the default for `mp_usmall` is "unsigned long".
1100*658eb9e1SMichael KruseYou may change these, provided you insure that `mp_small` is signed and
1101*658eb9e1SMichael Kruse`mp_usmall` is unsigned.  You will also need to adjust the size macros:
1102*658eb9e1SMichael Kruse
1103*658eb9e1SMichael Kruse    MP_SMALL_MIN, MP_SMALL_MAX
1104*658eb9e1SMichael Kruse    MP_USMALL_MIN, MP_USMALL_MAX
1105*658eb9e1SMichael Kruse
1106*658eb9e1SMichael Kruse... which are defined in `<imath.h>`, if you change these.
1107*658eb9e1SMichael Kruse
1108*658eb9e1SMichael KruseRational numbers are represented using a pair of arbitrary precision integers,
1109*658eb9e1SMichael Krusewith the convention that the sign of the numerator is the sign of the rational
1110*658eb9e1SMichael Krusevalue, and that the result of any rational operation is always represented in
1111*658eb9e1SMichael Kruselowest terms.  The canonical representation for rational zero is 0/1.  See
1112*658eb9e1SMichael Kruse"imrat.h".
1113*658eb9e1SMichael Kruse
1114*658eb9e1SMichael Kruse## Testing and Reporting of Bugs
1115*658eb9e1SMichael Kruse
1116*658eb9e1SMichael KruseTest vectors are included in the `tests/` subdirectory of the imath
1117*658eb9e1SMichael Krusedistribution.  When you run `make test`, it builds the `imtest` program and
1118*658eb9e1SMichael Kruseruns all available test vectors.  If any tests fail, you will get a line like
1119*658eb9e1SMichael Krusethis:
1120*658eb9e1SMichael Kruse
1121*658eb9e1SMichael Kruse    x    y    FAILED      v
1122*658eb9e1SMichael Kruse
1123*658eb9e1SMichael KruseHere, _x_ is the line number of the test which failed, _y_ is index of the test
1124*658eb9e1SMichael Krusewithin the file, and _v_ is the value(s) actually computed.  The name of the
1125*658eb9e1SMichael Krusefile is printed at the beginning of each test, so you can find out what test
1126*658eb9e1SMichael Krusevector failed by executing the following (with x, y, and v replaced by the
1127*658eb9e1SMichael Kruseabove values, and where "foo.t" is the name of the test file that was being
1128*658eb9e1SMichael Kruseprocessed at the time):
1129*658eb9e1SMichael Kruse
1130*658eb9e1SMichael Kruse    % tail +x tests/foo.t | head -1
1131*658eb9e1SMichael Kruse
1132*658eb9e1SMichael KruseNone of the tests should fail (but see [Note 2](#note2)); if any do, it
1133*658eb9e1SMichael Kruseprobably indicates a bug in the library (or at the very least, some assumption
1134*658eb9e1SMichael KruseI made which I shouldn't have).  Please [file an
1135*658eb9e1SMichael Kruseissue](https://github.com/creachadair/imath/issues/new), including the `FAILED`
1136*658eb9e1SMichael Krusetest line(s), as well as the output of the above `tail` command (so I know what
1137*658eb9e1SMichael Kruseinputs caused the failure).
1138*658eb9e1SMichael Kruse
1139*658eb9e1SMichael KruseIf you build with the preprocessor symbol `DEBUG` defined as a positive
1140*658eb9e1SMichael Kruseinteger, the digit allocators (`s_alloc`, `s_realloc`) fill all new buffers
1141*658eb9e1SMichael Krusewith the value `0xdeadbeefabad1dea`, or as much of it as will fit in a digit,
1142*658eb9e1SMichael Kruseso that you can more easily catch uninitialized reads in the debugger.
1143*658eb9e1SMichael Kruse
1144*658eb9e1SMichael Kruse## Notes
1145*658eb9e1SMichael Kruse
1146*658eb9e1SMichael Kruse1. <a name="note1"></a>You can generally use the same variables for both input
1147*658eb9e1SMichael Kruse   and output.  One exception is that you may not use the same variable for
1148*658eb9e1SMichael Kruse   both the quotient and the remainder of `mp_int_div()`.
1149*658eb9e1SMichael Kruse
1150*658eb9e1SMichael Kruse2. <a name="note2"></a>Many of the tests for this library were written under
1151*658eb9e1SMichael Kruse   the assumption that the `mp_small` type is 32 bits or more.  If you compile
1152*658eb9e1SMichael Kruse   with a smaller type, you may see `MP_RANGE` errors in some of the tests that
1153*658eb9e1SMichael Kruse   otherwise pass (due to conversion failures).  Also, the pi generator (pi.c)
1154*658eb9e1SMichael Kruse   will not work correctly if `mp_small` is too short, as its algorithm for arc
1155*658eb9e1SMichael Kruse   tangent is fairly simple-minded.
1156*658eb9e1SMichael Kruse
1157*658eb9e1SMichael Kruse## Contacts
1158*658eb9e1SMichael Kruse
1159*658eb9e1SMichael KruseThe IMath library was written by Michael J. Fromberger.
1160*658eb9e1SMichael Kruse
1161*658eb9e1SMichael KruseIf you discover any bugs or testing failures, please [open an
1162*658eb9e1SMichael Kruseissue](https://github.com/creachadair/imath/issues/new).  Please be sure to
1163*658eb9e1SMichael Kruseinclude a complete description of what went wrong, and if possible, a test
1164*658eb9e1SMichael Krusevector for `imtest` and/or a minimal test program that will demonstrate the bug
1165*658eb9e1SMichael Kruseon your system.  Please also let me know what hardware, operating system, and
1166*658eb9e1SMichael Krusecompiler you're using.
1167*658eb9e1SMichael Kruse
1168*658eb9e1SMichael Kruse## Acknowledgements
1169*658eb9e1SMichael Kruse
1170*658eb9e1SMichael KruseThe algorithms used in this library came from Vol. 2 of Donald Knuth's "The Art
1171*658eb9e1SMichael Kruseof Computer Programming" (Seminumerical Algorithms).  Thanks to Nelson Bolyard,
1172*658eb9e1SMichael KruseBryan Olson, Tom St. Denis, Tushar Udeshi, and Eric Silva for excellent
1173*658eb9e1SMichael Krusefeedback on earlier versions of this code.  Special thanks to Jonathan Shapiro
1174*658eb9e1SMichael Krusefor some very helpful design advice, as well as feedback and some clever ideas
1175*658eb9e1SMichael Krusefor improving performance in some common use cases.
1176*658eb9e1SMichael Kruse
1177*658eb9e1SMichael Kruse## License and Disclaimers
1178*658eb9e1SMichael Kruse
1179*658eb9e1SMichael KruseIMath is Copyright 2002-2009 Michael J. Fromberger
1180*658eb9e1SMichael KruseYou may use it subject to the following Licensing Terms:
1181*658eb9e1SMichael Kruse
1182*658eb9e1SMichael KrusePermission is hereby granted, free of charge, to any person obtaining a copy of
1183*658eb9e1SMichael Krusethis software and associated documentation files (the "Software"), to deal in
1184*658eb9e1SMichael Krusethe Software without restriction, including without limitation the rights to
1185*658eb9e1SMichael Kruseuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
1186*658eb9e1SMichael Kruseof the Software, and to permit persons to whom the Software is furnished to do
1187*658eb9e1SMichael Kruseso, subject to the following conditions:
1188*658eb9e1SMichael Kruse
1189*658eb9e1SMichael KruseThe above copyright notice and this permission notice shall be included in all
1190*658eb9e1SMichael Krusecopies or substantial portions of the Software.
1191*658eb9e1SMichael Kruse
1192*658eb9e1SMichael KruseTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1193*658eb9e1SMichael KruseIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1194*658eb9e1SMichael KruseFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1195*658eb9e1SMichael KruseAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1196*658eb9e1SMichael KruseLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1197*658eb9e1SMichael KruseOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1198*658eb9e1SMichael KruseSOFTWARE.
1199