1 {
2 This file is part of the Free Pascal packages
3 Copyright (c) 2009 by Jan Mercl
4
5 An header for the GMP library
6
7 See the file COPYING.FPC, included in this distribution,
8 for details about the copyright. (LGPL)
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 }
15
16 unit gmp;
17
18 {$mode objfpc}{$h+}
19 {$packrecords c}
20
21 //todo:windows link error on GMP global vars, reason not yet known
22 {$ifdef windows}
23 {$define NO_GMP_GLOBVARS}
24 {$endif}
25
26 {$ifdef darwin}
27 {$linklib gmp.3}
28 {$endif}
29 { Unused symbols exported from GMP:
30
31 Marked preliminary in GMP manual
32 __gmpn_bdivmod
33
34 Marked obsolete in GMP manual
35 __gmpn_divrem
36 __gmpz_random
37 __gmpz_random2
38 __gmp_randinit
39
40 Not documented in GMP manual
41 __gmpf_size
42 __gmpn_divrem_2
43 __gmpn_pow_1
44 __gmpn_preinv_mod_1
45 __gmpz_millerrabin
46
47 Marked for use only within GDB
48 __gmpf_dump
49 __gmpz_dump
50 }
51
52 interface
53
54 uses
55 sysutils;
56
57 const
58 BASE10 = 10;
59 LIB = 'gmp';
60 LOG_10_2 = 0.3010299956639812;
61 ERROR_NONE = 0;
62 ERROR_UNSUPPORTED_ARGUMENT = 1;
63 ERROR_DIVISION_BY_ZERO = 2;
64 ERROR_SQRT_OF_NEGATIVE = 4;
65 ERROR_INVALID_ARGUMENT = 8;
66 RAND_ALG_DEFAULT = 0;
67 RAND_ALG_LC = RAND_ALG_DEFAULT;
68
69 type
70
71 // ---- GMP types ----
72
73 { low level multi precision integer atom = machine size uint }
74 mp_limb_t = valuint;
75 { ^array of mp_limb_t}
76 mpn_ptr = ^mp_limb_t;
77 mp_size_t = sizeint;
78 mp_exp_t = valsint;
79 randalg_t = longint;
80
81 { multi precision integer number record }
82 mpz_t = record
83 alloc: longint;
84 size: longint;
85 data: mpn_ptr;
86 end;
87 mpz_ptr = ^mpz_t;
88
89 { multi precision rational number record }
90 mpq_t = record
91 num: mpz_t;
92 den: mpz_t;
93 end;
94 mpq_ptr = ^mpq_t;
95
96 { multi precision real number record }
97 mpf_t = record
98 prec: longint;
99 size: longint;
100 exp: mp_exp_t;
101 data: mpn_ptr;
102 end;
103 mpf_ptr = ^mpf_t;
104
105 randstate_t = record
106 seed: mpz_t;
107 alg: randalg_t;
108 algdata: record
109 case longint of
110 0 : (lc : pointer);
111 end;
112 end;
113 randstate_ptr = ^randstate_t;
114
115 { Return a pointer to newly allocated space with at least alloc size bytes }
lloc_sizenull116 alloc_func_t = function(alloc_size: sizeuint): pointer; cdecl;
117 { Resize a previously allocated block ptr of old size bytes to be new size bytes }
118 reallocate_func_t = function(p: pointer; old_size, new_size: sizeuint): pointer; cdecl;
119 { De-allocate the space pointed to by ptr }
120 free_proc_t = procedure(p: pointer; size: sizeuint); cdecl;
121 palloc_func = ^alloc_func_t;
122 preallocate_func = ^reallocate_func_t;
123 pfree_proc = ^free_proc_t;
124
125 // ---- ext types with automatic mem mngmt & cow, ~ fpc string type style -----
126
127 IMPBase = interface
128 ['{390336B5-6B78-47E0-BB0B-48F3AF9D5CCC}']
refsnull129 function refs: longint;
130 end;
131
132 MPInteger = interface(IMPBase)
133 ['{F6A977E7-B5E6-42BB-981F-E1A7C7EE0D30}']
ptrnull134 function ptr: mpz_ptr;
135 end;
136
137 MPFloat = interface(IMPBase)
138 ['{73F21043-CC71-425E-9825-1EF0FF4B9B85}']
ptrnull139 function ptr: mpf_ptr;
140 end;
141
142 MPRational = interface(IMPBase)
143 ['{0ACDDB76-5C1A-48E5-96EF-EA8647680FC1}']
ptrnull144 function ptr: mpq_ptr;
145 end;
146
147 MPRandState = interface(IMPBase)
148 ['{0E7EDBB9-E009-4A29-8BAC-8B967404B7B7}']
ptrnull149 function ptr: randstate_ptr;
150 end;
151
152 { TMPBase }
153
154 TMPBase = class(TInterfacedObject, IMPBase)
155 private
refsnull156 function refs: longint; inline;
157 end;
158
159 { TMPInteger }
160
161 TMPInteger = class(TMPBase, MPInteger)
162 private
163 fmpz: mpz_t;
ptrnull164 function ptr: mpz_ptr; inline;
165 public
166 destructor destroy; override;
167 end;
168
169 { TMPFloat }
170
171 TMPFloat = class(TMPBase, MPFloat)
172 private
173 fmpf: mpf_t;
ptrnull174 function ptr: mpf_ptr; inline;
175 public
176 destructor destroy; override;
177 end;
178
179 { TMPRational }
180
181 TMPRational = class(TMPBase, MPRational)
182 private
183 fmpq: mpq_t;
ptrnull184 function ptr: mpq_ptr; inline;
185 public
186 destructor destroy; override;
187 end;
188
189 { TMPRandState }
190
191 TMPRandState = class(TMPBase, MPRandState)
192 private
193 frandstate: randstate_t;
ptrnull194 function ptr: randstate_ptr; inline;
195 public
196 destructor destroy; override;
197 end;
198
199 // ==== GMP bindings ====
200
201 // ---- Custom Allocation ----
202
203 { Replace the current allocation functions from the arguments }
204 procedure mp_set_memory_functions(alloc_func_ptr: alloc_func_t; realloc_func_ptr: reallocate_func_t; free_func_ptr: free_proc_t); cdecl; external LIB name '__gmp_set_memory_functions';
205 { Get the current allocation functions, storing function pointers to the locations given by the arguments }
206 procedure mp_get_memory_functions(alloc_func_ptr: palloc_func; realloc_func_ptr: preallocate_func; free_func_ptr: pfree_proc); cdecl; external LIB name '__gmp_get_memory_functions';
207
208 // ---- Random Number Functions ----
209
210 { Obsolete: Initialize state with an algorithm selected by alg }
211 // procedure randinit(var state: randstate_t; alg: randalg_t; args: array of const); cdecl; external LIB name '__gmp_randinit';
212
213 { Initialize state with a default algorithm }
214 procedure mp_randinit_default(out state: randstate_t); cdecl; external LIB name '__gmp_randinit_default';
215 { Initialize state with a linear congruential algorithm X = (aX + c) mod 2^m2exp }
216 procedure mp_randinit_lc_2exp(out state: randstate_t; var a: mpz_t; c, m2exp: valuint); cdecl; external LIB name '__gmp_randinit_lc_2exp';
217 { Initialize state for a linear congruential algorithm as per gmp_randinit_lc_2exp }
mp_randinit_lc_2exp_sizenull218 function mp_randinit_lc_2exp_size(out state: randstate_t; size: sizeuint): longint; cdecl; external LIB name '__gmp_randinit_lc_2exp_size';
219 { Initialize state for a Mersenne Twister algorithm }
220 procedure mp_randinit_mt(out state: randstate_t); cdecl; external LIB name '__gmp_randinit_mt';
221 { Initialize rop with a copy of the algorithm and state from op }
222 procedure mp_randinit_set(out rop: randstate_t; var op: randstate_t); cdecl; external LIB name '__gmp_randinit_set';
223 { Set an initial seed value into state }
224 procedure mp_randseed(var state: randstate_t; var seed: mpz_t); cdecl; external LIB name '__gmp_randseed';
225 { Set an initial seed value into state }
226 procedure mp_randseed_ui(var state: randstate_t; seed: valuint); cdecl; external LIB name '__gmp_randseed_ui';
227 { Free all memory occupied by state }
228 procedure mp_randclear(var state: randstate_t); cdecl; external LIB name '__gmp_randclear';
229 { Return an uniformly distributed random number of n bits, ie. in the range 0 to 2^n−1 inclusive }
mp_urandomb_uinull230 function mp_urandomb_ui(var state: randstate_t; n: valuint): valuint; cdecl; external LIB name '__gmp_urandomb_ui';
231 { Return an uniformly distributed random number in the range 0 to n − 1, inclusive }
mp_urandomm_uinull232 function mp_urandomm_ui(var state: randstate_t; n: valuint): valuint; cdecl; external LIB name '__gmp_urandomm_ui';
233
234 // ---- Formatted Input/Output ----
235
236 { Form a null-terminated string in a block of memory obtained from the current memory allocation function }
mp_asprintfnull237 function mp_asprintf(out pp: pchar; fmt: pchar; args: array of const): longint; cdecl; external LIB name '__gmp_asprintf';
238 { Form a null-terminated string in a block of memory obtained from the current memory allocation function }
mp_asprintfnull239 function mp_asprintf(out pp: pchar; fmt: pchar): longint; cdecl; varargs; external LIB name '__gmp_asprintf';
240 { Print to the standard output stdout. Return the number of characters written, or −1 if an error occurred. }
mp_printfnull241 function mp_printf(fmt: pchar; args: array of const): longint; cdecl; external LIB name '__gmp_printf';
242 { Print to the standard output stdout. Return the number of characters written, or −1 if an error occurred. }
mp_printfnull243 function mp_printf(fmt: pchar): longint; cdecl; varargs; external LIB name '__gmp_printf';
244 { Form a null-terminated string in buf. No more than size bytes will be written. }
mp_snprintfnull245 function mp_snprintf(buf: pchar; size: sizeuint; fmt: pchar; args: array of const): longint; cdecl; external LIB name '__gmp_snprintf';
246 { Form a null-terminated string in buf. No more than size bytes will be written. }
mp_snprintfnull247 function mp_snprintf(buf: pchar; size: sizeuint; fmt: pchar): longint; cdecl; varargs; external LIB name '__gmp_snprintf';
248 { Form a null-terminated string in buf. Return the number of characters written, excluding the terminating null. }
mp_sprintfnull249 function mp_sprintf(buf, fmt: pchar; args: array of const): longint; cdecl; external LIB name '__gmp_sprintf';
250 { Form a null-terminated string in buf. Return the number of characters written, excluding the terminating null. }
mp_sprintfnull251 function mp_sprintf(buf, fmt: pchar): longint; cdecl; varargs; external LIB name '__gmp_sprintf';
252 { Read from the standard input stdin }
mp_scanfnull253 function mp_scanf(fmt: pchar; args: array of const): longint; cdecl; external LIB name '__gmp_scanf';
254 { Read from the standard input stdin }
mp_scanfnull255 function mp_scanf(fmt: pchar): longint; cdecl; varargs; external LIB name '__gmp_scanf';
256 { Read from a null-terminated string s }
mp_sscanfnull257 function mp_sscanf(s, fmt: pchar; args: array of const): longint; cdecl; external LIB name '__gmp_sscanf';
258 { Read from a null-terminated string s }
mp_sscanfnull259 function mp_sscanf(s, fmt: pchar): longint; cdecl; varargs; external LIB name '__gmp_sscanf';
260
261 // ---- integer Functions ----
262
263 { Change the space for integer to new_alloc limbs }
mpz_reallocnull264 function mpz_realloc(var integer_: mpz_t; new_alloc: mp_size_t): pointer; cdecl; external LIB name '__gmpz_realloc';
265 { Set rop to the absolute value of op }
266 procedure mpz_abs(var rop, op: mpz_t); cdecl; external LIB name '__gmpz_abs';
267 { Set rop to op1 + op2 }
268 procedure mpz_add(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_add';
269 { Set rop to op1 + op2 }
270 procedure mpz_add_ui(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_add_ui';
271 { Set rop to rop + op1 × op2 }
272 procedure mpz_addmul(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_addmul';
273 { Set rop to rop + op1 × op2 }
274 procedure mpz_addmul_ui(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_addmul_ui';
275 { Set rop to op1 bitwise-and op2 }
276 procedure mpz_and(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_and';
277 { _Fixed_ space of fixed_num_bits is allocated to each of the array size integers in integer array }
278 procedure mpz_array_init(var integer_array: mpz_t; array_size, fixed_num_bits: mp_size_t); cdecl; external LIB name '__gmpz_array_init';
279 { Compute the binomial coefficient (n over k) and store the result in rop }
280 procedure mpz_bin_ui(var rop, n: mpz_t; k: valuint); cdecl; external LIB name '__gmpz_bin_ui';
281 { Compute the binomial coefficient (n over k) and store the result in rop }
282 procedure mpz_bin_uiui(var rop: mpz_t; n, k: valuint); cdecl; external LIB name '__gmpz_bin_uiui';
283 { Divide n by d, forming a quotient q. Round mode ceil. }
284 procedure mpz_cdiv_q(var q, n, d: mpz_t); cdecl; external LIB name '__gmpz_cdiv_q';
285 { Divide n by d, forming a quotient q. d = 2^b. Round mode ceil. }
286 procedure mpz_cdiv_q_2exp(var q, n: mpz_t; b: valuint); cdecl; external LIB name '__gmpz_cdiv_q_2exp';
287 { Divide n by d, forming a quotient q. Round mode ceil. }
mpz_cdiv_q_uinull288 function mpz_cdiv_q_ui(var q, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_cdiv_q_ui';
289 { Divide n by d, forming a quotient q and remainder r. Round mode ceil. }
290 procedure mpz_cdiv_qr(var q, r, n, d: mpz_t); cdecl; external LIB name '__gmpz_cdiv_qr';
291 { Divide n by d, forming a quotient q and remainder r. Round mode ceil. }
mpz_cdiv_qr_uinull292 function mpz_cdiv_qr_ui(var q, r, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_cdiv_qr_ui';
293 { Divide n by d, forming a remainder r. Round mode ceil. }
294 procedure mpz_cdiv_r(var r, n, d: mpz_t); cdecl; external LIB name '__gmpz_cdiv_r';
295 { Divide n by d, forming a remainder r. d = 2^b. Round mode ceil. }
296 procedure mpz_cdiv_r_2exp(var r, n: mpz_t; b: valuint); cdecl; external LIB name '__gmpz_cdiv_r_2exp';
297 { Divide n by d, forming a remainder r. Round mode ceil. }
mpz_cdiv_r_uinull298 function mpz_cdiv_r_ui(var r, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_cdiv_r_ui';
299 { Divide n by d. Round mode ceil. }
mpz_cdiv_uinull300 function mpz_cdiv_ui(var n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_cdiv_ui';
301 { Free the space occupied by integer. Call this function for all mpz_t variables when you are done with them. }
302 procedure mpz_clear(var integer_: mpz_t); cdecl; external LIB name '__gmpz_clear';
303 { Clear bit bit_index in rop }
304 procedure mpz_clrbit(var rop: mpz_t; bit_index: valuint); cdecl; external LIB name '__gmpz_clrbit';
305 { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
mpz_cmpnull306 function mpz_cmp(var op1, op2: mpz_t): longint; cdecl; external LIB name '__gmpz_cmp';
307 { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
mpz_cmp_dnull308 function mpz_cmp_d(var op1: mpz_t; op2: double): longint; cdecl; external LIB name '__gmpz_cmp_d';
309 { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
mpz_cmp_sinull310 function mpz_cmp_si(var op1: mpz_t; op2: valsint): longint; cdecl; external LIB name '__gmpz_cmp_si';
311 { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
mpz_cmp_uinull312 function mpz_cmp_ui(var op1: mpz_t; op2: valuint): longint; cdecl; external LIB name '__gmpz_cmp_ui';
313 { Compare the absolute values of op1 and op2. Return a positive value if |op1| > |op2|, zero if |op1| = |op2|, or a negative value if |op1| < |op2|. }
mpz_cmpabsnull314 function mpz_cmpabs(var op1, op2: mpz_t): longint; cdecl; external LIB name '__gmpz_cmpabs';
315 { Compare the absolute values of op1 and op2. Return a positive value if |op1| > |op2|, zero if |op1| = |op2|, or a negative value if |op1| < |op2|. }
mpz_cmpabs_dnull316 function mpz_cmpabs_d(var op1: mpz_t; op2: double): longint; cdecl; external LIB name '__gmpz_cmpabs_d';
317 { Compare the absolute values of op1 and op2. Return a positive value if |op1| > |op2|, zero if |op1| = |op2|, or a negative value if |op1| < |op2|. }
mpz_cmpabs_uinull318 function mpz_cmpabs_ui(var op1: mpz_t; op2: valuint): longint; cdecl; external LIB name '__gmpz_cmpabs_ui';
319 { Set rop to the one’s complement of op }
320 procedure mpz_com(var rop, op: mpz_t); cdecl; external LIB name '__gmpz_com';
321 { Complement bit bit_index in rop }
322 procedure mpz_combit(var rop: mpz_t; bit_index: valuint); cdecl; external LIB name '__gmpz_combit';
323 { Return non-zero if n is congruent to c modulo d }
mpz_congruent_pnull324 function mpz_congruent_p(var n, c, d: mpz_t): longint; cdecl; external LIB name '__gmpz_congruent_p';
325 { Return non-zero if n is congruent to c modulo 2^b }
mpz_congruent_2exp_pnull326 function mpz_congruent_2exp_p(var n, c: mpz_t; b: valuint): longint; cdecl; external LIB name '__gmpz_congruent_2exp_p';
327 { Return non-zero if n is congruent to c modulo d }
mpz_congruent_ui_pnull328 function mpz_congruent_ui_p(var n: mpz_t; c, d: valuint): longint; cdecl; external LIB name '__gmpz_congruent_ui_p';
329 { Set q to n/d }
330 procedure mpz_divexact(var q, n, d: mpz_t); cdecl; external LIB name '__gmpz_divexact';
331 { Set q to n/d }
332 procedure mpz_divexact_ui(var q, n: mpz_t; d: valuint); cdecl; external LIB name '__gmpz_divexact_ui';
333 { Return non-zero if n is exactly divisible by d }
mpz_divisible_pnull334 function mpz_divisible_p(var n, d: mpz_t): longint; cdecl; external LIB name '__gmpz_divisible_p';
335 { Return non-zero if n is exactly divisible by d }
mpz_divisible_ui_pnull336 function mpz_divisible_ui_p(var n: mpz_t; d: valuint): longint; cdecl; external LIB name '__gmpz_divisible_ui_p';
337 { Return non-zero if n is exactly divisible by by 2^b }
mpz_divisible_2exp_pnull338 function mpz_divisible_2exp_p(var n: mpz_t; b: valuint): longint; cdecl; external LIB name '__gmpz_divisible_2exp_p';
339
340 // GDB only: procedure mpz_dump(var _para1: mpz_t); cdecl; external LIB name '__gmpz_dump';
341
342 { Fill buf with word data from op }
mpz_exportnull343 function mpz_export(out buf; out countp: sizeuint; order: longint; size: sizeuint; endian: longint; nails: sizeuint; var op: mpz_t): pointer; cdecl; external LIB name '__gmpz_export';
344 { Set rop to op!, the factorial of op }
345 procedure mpz_fac_ui(var rop: mpz_t; op: valuint); cdecl; external LIB name '__gmpz_fac_ui';
346 { Divide n by d, forming a quotient q. Round mode floor. }
347 procedure mpz_fdiv_q(var q, n, d: mpz_t); cdecl; external LIB name '__gmpz_fdiv_q';
348 { Divide n by d, forming a quotient q. d = 2^b. Round mode floor. }
349 procedure mpz_fdiv_q_2exp(var q, n: mpz_t; b: valuint); cdecl; external LIB name '__gmpz_fdiv_q_2exp';
350 { Divide n by d, forming a quotient q. Round mode floor. }
mpz_fdiv_q_uinull351 function mpz_fdiv_q_ui(var q, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_fdiv_q_ui';
352 { Divide n by d, forming a quotient q and remainder r. Round mode floor. }
353 procedure mpz_fdiv_qr(var q, r, n, d: mpz_t); cdecl; external LIB name '__gmpz_fdiv_qr';
354 { Divide n by d, forming a quotient q and remainder r. Round mode floor. }
mpz_fdiv_qr_uinull355 function mpz_fdiv_qr_ui(var q, r, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_fdiv_qr_ui';
356 { Divide n by d, forming a remainder r. Round mode floor. }
357 procedure mpz_fdiv_r(var r, n, d: mpz_t); cdecl; external LIB name '__gmpz_fdiv_r';
358 { Divide n by d, forming a remainder r. d = 2^b. Round mode floor. }
359 procedure mpz_fdiv_r_2exp(var r, n: mpz_t; b: valuint); cdecl; external LIB name '__gmpz_fdiv_r_2exp';
360 { Divide n by d, forming a remainder r. Round mode floor. }
mpz_fdiv_r_uinull361 function mpz_fdiv_r_ui(var r, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_fdiv_r_ui';
362 { Divide n by d. Round mode floor. }
mpz_fdiv_uinull363 function mpz_fdiv_ui(var n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_fdiv_ui';
364 { Set fn to to Fn, the n’th Fibonacci number }
365 procedure mpz_fib_ui(var fn: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_fib_ui';
366 { Set fn to Fn, and fnsub1 to Fn−1 }
367 procedure mpz_fib2_ui(var fn, fnsub1: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_fib2_ui';
368 { Return non-zero iff the value of op fits in an signed int. Otherwise, return zero. }
mpz_fits_sint_pnull369 function mpz_fits_sint_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_fits_sint_p';
370 { Return non-zero iff the value of op fits in an signed long int. Otherwise, return zero. }
mpz_fits_slong_pnull371 function mpz_fits_slong_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_fits_slong_p';
372 { Return non-zero iff the value of op fits in an signed short int. Otherwise, return zero. }
mpz_fits_sshort_pnull373 function mpz_fits_sshort_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_fits_sshort_p';
374 { Return non-zero iff the value of op fits in an unsigned int. Otherwise, return zero. }
mpz_fits_uint_pnull375 function mpz_fits_uint_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_fits_uint_p';
376 { Return non-zero iff the value of op fits in an unsigned long int. Otherwise, return zero. }
mpz_fits_ulong_pnull377 function mpz_fits_ulong_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_fits_ulong_p';
378 { Return non-zero iff the value of op fits in an unsigned short int. Otherwise, return zero. }
mpz_fits_ushort_pnull379 function mpz_fits_ushort_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_fits_ushort_p';
380 { Set rop to the greatest common divisor of op1 and op2 }
381 procedure mpz_gcd(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_gcd';
382 { Compute the greatest common divisor of op1 and op2. If rop is not NULL, store the result there. }
mpz_gcd_uinull383 function mpz_gcd_ui(var rop, op1: mpz_t; op2: valuint): valuint; cdecl; external LIB name '__gmpz_gcd_ui';
384 { Set g to the greatest common divisor of a and b, and in addition set s and t to coefficients satisfying as + bt = g }
385 procedure mpz_gcdext(var g, s, t, a, b: mpz_t); cdecl; external LIB name '__gmpz_gcdext';
386 { Convert op to a double, truncating if necessary (ie. rounding towards zero) }
mpz_get_dnull387 function mpz_get_d(var op: mpz_t): double; cdecl; external LIB name '__gmpz_get_d';
388 { Convert op to a double, truncating if necessary (ie. rounding towards zero), and returning the exponent separately }
mpz_get_d_2expnull389 function mpz_get_d_2exp(out exp: valsint; var op: mpz_t): double; cdecl; external LIB name '__gmpz_get_d_2exp';
390 { Return the value of op as a signed long }
mpz_get_sinull391 function mpz_get_si(var op: mpz_t): valsint; cdecl; external LIB name '__gmpz_get_si';
392 { Convert op to a string of digits in base base. The base argument may vary from 2 to 62 or from −2 to −36. If str is NULL, the result string is allocated using the current allocation function }
mpz_get_strnull393 function mpz_get_str(str: pchar; base: longint; var op: mpz_t): pchar; cdecl; external LIB name '__gmpz_get_str';
394 { Return the value of op as an unsigned long }
mpz_get_uinull395 function mpz_get_ui(var op: mpz_t): valuint; cdecl; external LIB name '__gmpz_get_ui';
396 { Return limb number n from op }
mpz_getlimbnnull397 function mpz_getlimbn(var op: mpz_t; n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpz_getlimbn';
398 { If op1 and op2 are both >= 0 or both < 0, return the hamming distance between the two operands, which is the number of bit positions where op1 and op2 have different bit values }
mpz_hamdistnull399 function mpz_hamdist(var op1, op2: mpz_t): valuint; cdecl; external LIB name '__gmpz_hamdist';
400 { Set rop from an array of word data at op }
401 procedure mpz_import(var rop: mpz_t; count: sizeuint; order: longint; size: sizeuint; endian: longint; nails: sizeuint; const op); cdecl; external LIB name '__gmpz_import';
402 { Initialize integer, and set its value to 0 }
403 procedure mpz_init(out integer_: mpz_t); cdecl; external LIB name '__gmpz_init';
404 { Initialize integer, with space for n bits, and set its value to 0 }
405 procedure mpz_init2(out integer_: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_init2';
406 { Initialize rop with limb space and set the initial numeric value from op }
407 procedure mpz_init_set(out rop: mpz_t; var op: mpz_t); cdecl; external LIB name '__gmpz_init_set';
408 { Initialize rop with limb space and set the initial numeric value from op }
409 procedure mpz_init_set_d(out rop: mpz_t; op: double); cdecl; external LIB name '__gmpz_init_set_d';
410 { Initialize rop with limb space and set the initial numeric value from op }
411 procedure mpz_init_set_si(out rop: mpz_t; op: valsint); cdecl; external LIB name '__gmpz_init_set_si';
412 { Initialize rop and set its value like mpz_set_str }
mpz_init_set_strnull413 function mpz_init_set_str(out rop: mpz_t; str: pchar; base: longint): longint; cdecl; external LIB name '__gmpz_init_set_str';
414 { Initialize rop with limb space and set the initial numeric value from op }
415 procedure mpz_init_set_ui(out rop: mpz_t; op: valuint); cdecl; external LIB name '__gmpz_init_set_ui';
416 { Compute the inverse of op1 modulo op2 and put the result in rop }
mpz_invertnull417 function mpz_invert(var rop, op1, op2: mpz_t): longint; cdecl; external LIB name '__gmpz_invert';
418 { Set rop to op1 bitwise inclusive-or op2 }
419 procedure mpz_ior(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_ior';
420 { Calculate the Jacobi symbol. This is defined only for b odd }
mpz_jacobinull421 function mpz_jacobi(var a, b: mpz_t): longint; cdecl; external LIB name '__gmpz_jacobi';
422 { Calculate the Jacobi symbol with the Kronecker extension }
mpz_kronecker_sinull423 function mpz_kronecker_si(var a: mpz_t; b: valsint): longint; cdecl; external LIB name '__gmpz_kronecker_si';
424 { Calculate the Jacobi symbol with the Kronecker extension }
mpz_kronecker_uinull425 function mpz_kronecker_ui(var a: mpz_t; b: valuint): longint; cdecl; external LIB name '__gmpz_kronecker_ui';
426 { Calculate the Jacobi symbol with the Kronecker extension }
mpz_si_kroneckernull427 function mpz_si_kronecker(a: valsint; var b: mpz_t): longint; cdecl; external LIB name '__gmpz_si_kronecker';
428 { Calculate the Jacobi symbol with the Kronecker extension }
mpz_ui_kroneckernull429 function mpz_ui_kronecker(a: valuint; var b: mpz_t): longint; cdecl; external LIB name '__gmpz_ui_kronecker';
430 { Set rop to the least common multiple of op1 and op2 }
431 procedure mpz_lcm(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_lcm';
432 { Set rop to the least common multiple of op1 and op2 }
433 procedure mpz_lcm_ui(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_lcm_ui';
434 { Set ln to to Ln, the n’th Lucas number }
435 procedure mpz_lucnum_ui(var ln: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_lucnum_ui';
436 { Set ln to Ln, and lnsub1 to Ln−1 }
437 procedure mpz_lucnum2_ui(var ln, lnsub1: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_lucnum2_ui';
438
mpz_millerrabinnull439 // No docs: function mpz_millerrabin(var _para1: mpz_t; _para2: longint): longint; cdecl; external LIB name '__gmpz_millerrabin';
440
441 { Set r to n mod d. The sign of the divisor is ignored; the result is always non-negative. }
442 procedure mpz_mod(var r, n, d: mpz_t); cdecl; external LIB name '__gmpz_mod';
443 { Set rop to op1 × op2 }
444 procedure mpz_mul(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_mul';
445 { Set rop to op1 × 2^op2. This operation can also be defined as a left shift by op2 bits. }
446 procedure mpz_mul_2exp(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_mul_2exp';
447 { Set rop to op1 × op2 }
448 procedure mpz_mul_si(var rop, op1: mpz_t; op2: valsint); cdecl; external LIB name '__gmpz_mul_si';
449 { Set rop to op1 × op2 }
450 procedure mpz_mul_ui(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_mul_ui';
451 { Set rop to −op }
452 procedure mpz_neg(var rop, op: mpz_t); cdecl; external LIB name '__gmpz_neg';
453 { Set rop to the next prime greater than op }
454 procedure mpz_nextprime(var rop, op: mpz_t); cdecl; external LIB name '__gmpz_nextprime';
455 { Return non-zero if op is a perfect power, i.e., if there exist integers a and b, with b > 1, such that op = a^b }
mpz_perfect_power_pnull456 function mpz_perfect_power_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_perfect_power_p';
457 { Return non-zero if op is a perfect square, i.e., if the square root of op is an integer }
mpz_perfect_square_pnull458 function mpz_perfect_square_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_perfect_square_p';
459 { If op >= 0, return the population count of op, which is the number of 1 bits in the binary representation }
mpz_popcountnull460 function mpz_popcount(var op: mpz_t): valuint; cdecl; external LIB name '__gmpz_popcount';
461 { Set rop to base^exp. The case 0^0 yields 1. }
462 procedure mpz_pow_ui(var rop, base: mpz_t; exp: valuint); cdecl; external LIB name '__gmpz_pow_ui';
463 { Set rop to base^exp mod mod_ }
464 procedure mpz_powm(var rop, base, exp, mod_: mpz_t); cdecl; external LIB name '__gmpz_powm';
465 { Set rop to base^exp mod mod_ }
466 procedure mpz_powm_ui(var rop, base: mpz_t; exp: valuint; var mod_: mpz_t); cdecl; external LIB name '__gmpz_powm_ui';
467 { Determine whether n is prime. Return 2 if n is definitely prime, return 1 if n is probably prime (without being certain), or return 0 if n is definitely composite. }
mpz_probab_prime_pnull468 function mpz_probab_prime_p(var n: mpz_t; reps: longint): longint; cdecl; external LIB name '__gmpz_probab_prime_p';
469
470 { Obsolete: Generate a random integer of at most max_size limbs }
471 // procedure mpz_random(var rop: mpz_t; max_size: mp_size_t); cdecl; external LIB name '__gmpz_random';
472 { Obsolete: Generate a random integer of at most max_size limbs, with long strings of zeros and ones in the binary representation }
473 // procedure mpz_random2(var rop: mpz_t; max_size: mp_size_t); cdecl; external LIB name '__gmpz_random2';
474
475 { Change the space allocated for integer to n bits. The value in integer is preserved if it fits, or is set to 0 if not. }
476 procedure mpz_realloc2(var integer_: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_realloc2';
477 { Remove all occurrences of the factor f from op and store the result in rop }
mpz_removenull478 function mpz_remove(var rop, op, f: mpz_t): valuint; cdecl; external LIB name '__gmpz_remove';
479 { Set rop to trunc(op^(1/n)), the truncated integer part of the nth root of op. Return non-zero if the computation was exact, i.e., if op is rop to the nth power. }
mpz_rootnull480 function mpz_root(var rop, op: mpz_t; n: valuint): longint; cdecl; external LIB name '__gmpz_root';
481 { Set root to trunc(u^(1/n)), the truncated integer part of the nth root of u. Set rem to the remainder, (u − root^n). }
482 procedure mpz_rootrem(var root, rem, u: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_rootrem';
483 { Generate a random integer with long strings of zeros and ones in the binary representation }
484 procedure mpz_rrandomb(var rop: mpz_t; var state: randstate_t; n: valuint); cdecl; external LIB name '__gmpz_rrandomb';
485 { Scan op, starting from bit starting_bit, towards more significant bits, until the first 0 bit is found }
mpz_scan0null486 function mpz_scan0(var op: mpz_t; starting_bit: valuint): valuint; cdecl; external LIB name '__gmpz_scan0';
487 { Scan op, starting from bit starting_bit, towards more significant bits, until the first 1 bit is found }
mpz_scan1null488 function mpz_scan1(var op: mpz_t; starting_bit: valuint): valuint; cdecl; external LIB name '__gmpz_scan1';
489 { Set the value of rop from op }
490 procedure mpz_set(var rop, op: mpz_t); cdecl; external LIB name '__gmpz_set';
491 { Set the value of rop from op }
492 procedure mpz_set_d(var rop: mpz_t; op: double); cdecl; external LIB name '__gmpz_set_d';
493 { Set the value of rop from op }
494 procedure mpz_set_f(var rop: mpz_t; var op: mpf_t); cdecl; external LIB name '__gmpz_set_f';
495 { Set the value of rop from op }
496 procedure mpz_set_q(var rop: mpz_t; var op: mpq_t); cdecl; external LIB name '__gmpz_set_q';
497 { Set the value of rop from op }
498 procedure mpz_set_si(var rop: mpz_t; op: valsint); cdecl; external LIB name '__gmpz_set_si';
499 { Set the value of rop from str, a null-terminated C string in base base. White space is allowed in the string, and is simply ignored. }
mpz_set_strnull500 function mpz_set_str(var rop: mpz_t; str: pchar; base: longint): longint; cdecl; external LIB name '__gmpz_set_str';
501 { Set the value of rop from op }
502 procedure mpz_set_ui(var rop: mpz_t; op: valuint); cdecl; external LIB name '__gmpz_set_ui';
503 { Set bit bit_index in rop }
504 procedure mpz_setbit(var rop: mpz_t; bit_index: valuint); cdecl; external LIB name '__gmpz_setbit';
505 { Return the size of op measured in number of limbs }
mpz_sizenull506 function mpz_size(var op: mpz_t): sizeuint; cdecl; external LIB name '__gmpz_size';
507 { Return the size of op measured in number of digits in the given base }
mpz_sizeinbasenull508 function mpz_sizeinbase(var op: mpz_t; base: longint): sizeuint; cdecl; external LIB name '__gmpz_sizeinbase';
509 { Set rop to trunc(sqrt(op)), the truncated integer part of the square root of op }
510 procedure mpz_sqrt(var rop, op: mpz_t); cdecl; external LIB name '__gmpz_sqrt';
511 { Set rop1 to trunc(sqrt(op)), likempz_sqrt. Set rop2 to the remainder (op − rop1^2), which will be zero if op is a perfect square. }
512 procedure mpz_sqrtrem(var rop1, rop2, op: mpz_t); cdecl; external LIB name '__gmpz_sqrtrem';
513 { Set rop to op1 − op2 }
514 procedure mpz_sub(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_sub';
515 { Set rop to op1 − op2 }
516 procedure mpz_sub_ui(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_sub_ui';
517 { Set rop to op1 − op2 }
518 procedure mpz_ui_sub(var rop: mpz_t; op1: valuint; var op2: mpz_t); cdecl; external LIB name '__gmpz_ui_sub';
519 { Set rop to rop − op1 × op2 }
520 procedure mpz_submul(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_submul';
521 { Set rop to rop − op1 × op2 }
522 procedure mpz_submul_ui(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_submul_ui';
523 { Swap the values rop1 and rop2 efficiently }
524 procedure mpz_swap(var rop1, rop2: mpz_t); cdecl; external LIB name '__gmpz_swap';
525 { Divide n by d, forming a quotient q. Round mode trunc. }
526 procedure mpz_tdiv_q(var q, n, d: mpz_t); cdecl; external LIB name '__gmpz_tdiv_q';
527 { Divide n by d, forming a quotient q. d = 2^b. Round mode trunc. }
528 procedure mpz_tdiv_q_2exp(var q, n: mpz_t; b: valuint); cdecl; external LIB name '__gmpz_tdiv_q_2exp';
529 { Divide n by d, forming a quotient q. Round mode trunc. }
mpz_tdiv_q_uinull530 function mpz_tdiv_q_ui(var q, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_tdiv_q_ui';
531 { Divide n by d, forming a quotient q and remainder r. Round mode trunc. }
532 procedure mpz_tdiv_qr(var q, r, n, d: mpz_t); cdecl; external LIB name '__gmpz_tdiv_qr';
533 { Divide n by d, forming a quotient q and remainder r. Round mode trunc. }
mpz_tdiv_qr_uinull534 function mpz_tdiv_qr_ui(var q, r, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_tdiv_qr_ui';
535 { Divide n by d, forming a remainder r. Round mode trunc. }
536 procedure mpz_tdiv_r(var r, n, d: mpz_t); cdecl; external LIB name '__gmpz_tdiv_r';
537 { Divide n by d, forming a remainder r. d = 2^b. Round mode trunc. }
538 procedure mpz_tdiv_r_2exp(var r, n: mpz_t; b: valuint); cdecl; external LIB name '__gmpz_tdiv_r_2exp';
539 { Divide n by d, forming a remainder r. Round mode trunc. }
mpz_tdiv_r_uinull540 function mpz_tdiv_r_ui(var r, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_tdiv_r_ui';
541 { Divide n by d. Round mode trunc. }
mpz_tdiv_uinull542 function mpz_tdiv_ui(var n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_tdiv_ui';
543 { Test bit bit_index in op and return 0 or 1 accordingly }
mpz_tstbitnull544 function mpz_tstbit(var rop: mpz_t; bit_index: valuint): longint; cdecl; external LIB name '__gmpz_tstbit';
545 { Set rop to base^exp. The case 0^0 yields 1 }
546 procedure mpz_ui_pow_ui(var rop: mpz_t; base, exp: valuint); cdecl; external LIB name '__gmpz_ui_pow_ui';
547 { Generate a uniformly distributed random integer in the range 0 to 2^n − 1, inclusive }
548 procedure mpz_urandomb(var rop: mpz_t; var state: randstate_t; n: valuint); cdecl; external LIB name '__gmpz_urandomb';
549 { Generate a uniform random integer in the range 0 to n − 1, inclusive }
550 procedure mpz_urandomm(var rop: mpz_t; var state: randstate_t; var n: mpz_t); cdecl; external LIB name '__gmpz_urandomm';
551 { Set rop to op1 bitwise exclusive-or op2 }
552 procedure mpz_xor(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_xor';
553
554 // ---- Rational Number Functions ----
555
556 { Set rop to the absolute value of op }
557 procedure mpq_abs(var rop, op: mpq_t); cdecl; external LIB name '__gmpq_abs';
558 { Set sum to addend1 + addend2 }
559 procedure mpq_add(var sum, addend1, addend2: mpq_t); cdecl; external LIB name '__gmpq_add';
560 { Remove any factors that are common to the numerator and denominator of op, and make the denominator positive }
561 procedure mpq_canonicalize(var op: mpq_t); cdecl; external LIB name '__gmpq_canonicalize';
562 { Free the space occupied by rational number }
563 procedure mpq_clear(var rational_number: mpq_t); cdecl; external LIB name '__gmpq_clear';
564 { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2 }
mpq_cmpnull565 function mpq_cmp(var op1, op2: mpq_t): longint; cdecl; external LIB name '__gmpq_cmp';
566 { Compare op1 and num2/den2. Return a positive value if op1 > num2/den2, zero if op1 = num2/den2, and a negative value if op1 < num2/den2 }
mpq_cmp_sinull567 function mpq_cmp_si(var op1: mpq_t; num2: valsint; den2: valuint): longint; cdecl; external LIB name '__gmpq_cmp_si';
568 { Compare op1 and num2/den2. Return a positive value if op1 > num2/den2, zero if op1 = num2/den2, and a negative value if op1 < num2/den2 }
mpq_cmp_uinull569 function mpq_cmp_ui(var op1: mpq_t; num2, den2: valuint): longint; cdecl; external LIB name '__gmpq_cmp_ui';
570 { Set quotient to dividend/divisor }
571 procedure mpq_div(var quotient, dividend, divisor: mpq_t); cdecl; external LIB name '__gmpq_div';
572 { Set rop to op1/(2^op2) }
573 procedure mpq_div_2exp(var rop, op1: mpq_t; op2: valuint); cdecl; external LIB name '__gmpq_div_2exp';
574 { Return non-zero if op1 and op2 are equal, zero if they are non-equal }
mpq_equalnull575 function mpq_equal(var op1, op2: mpq_t): longint; cdecl; external LIB name '__gmpq_equal';
576 { Get the numerator of a rational }
577 procedure mpq_get_num(var numerator: mpz_t; var rational: mpq_t); cdecl; external LIB name '__gmpq_get_num';
578 { Get the denominator of a rational }
579 procedure mpq_get_den(var denominator: mpz_t; var rational: mpq_t); cdecl; external LIB name '__gmpq_get_den';
580 { Convert op to a double, truncating if necessary (ie. rounding towards zero) }
mpq_get_dnull581 function mpq_get_d(var op: mpq_t): double; cdecl; external LIB name '__gmpq_get_d';
582 { Convert op to a string of digits in base base }
mpq_get_strnull583 function mpq_get_str(str: pchar; base: longint; var op: mpq_t): pchar; cdecl; external LIB name '__gmpq_get_str';
584 { Initialize dest rational and set it to 0/1 }
585 procedure mpq_init(out dest_rational: mpq_t); cdecl; external LIB name '__gmpq_init';
586 { Set inverted_number to 1/number }
587 procedure mpq_inv(var inverted_number, number: mpq_t); cdecl; external LIB name '__gmpq_inv';
588 { Set product to multiplier × multiplicand }
589 procedure mpq_mul(var product, multiplier, multiplicand: mpq_t); cdecl; external LIB name '__gmpq_mul';
590 { Set rop to op1 × (2^op2) }
591 procedure mpq_mul_2exp(var rop, op1: mpq_t; op2: valuint); cdecl; external LIB name '__gmpq_mul_2exp';
592 { Set negated_operand to −operand }
593 procedure mpq_neg(var negated_operand, operand: mpq_t); cdecl; external LIB name '__gmpq_neg';
594 { Assign rop from op }
595 procedure mpq_set(var rop, op: mpq_t); cdecl; external LIB name '__gmpq_set';
596 { Set rop to the value of op. There is no rounding, this conversion is exact. }
597 procedure mpq_set_d(var rop: mpq_t; op: double); cdecl; external LIB name '__gmpq_set_d';
598 { Set the denominator of a rational }
599 procedure mpq_set_den(var rational: mpq_t; var denominator: mpz_t); cdecl; external LIB name '__gmpq_set_den';
600 { Set rop to the value of op. There is no rounding, this conversion is exact. }
601 procedure mpq_set_f(var rop: mpq_t; var op: mpf_t); cdecl; external LIB name '__gmpq_set_f';
602 { Set the numerator of a rational }
603 procedure mpq_set_num(var rational: mpq_t; var numerator: mpz_t); cdecl; external LIB name '__gmpq_set_num';
604 { Set the value of rop to op1/op2 }
605 procedure mpq_set_si(var rop: mpq_t; op1: valsint; op2: valuint); cdecl; external LIB name '__gmpq_set_si';
606 { Set rop from a null-terminated string str in the given base }
mpq_set_strnull607 function mpq_set_str(var rop: mpq_t; str: pchar; base: longint): longint; cdecl; external LIB name '__gmpq_set_str';
608 { Set the value of rop to op1/op2 }
609 procedure mpq_set_ui(var rop: mpq_t; op1, op2: valuint); cdecl; external LIB name '__gmpq_set_ui';
610 { Assign rop from op }
611 procedure mpq_set_z(var rop: mpq_t; var op: mpz_t); cdecl; external LIB name '__gmpq_set_z';
612 { Set difference to minuend − subtrahend }
613 procedure mpq_sub(var difference, minuend, subtrahend: mpq_t); cdecl; external LIB name '__gmpq_sub';
614 { Swap the values rop1 and rop2 efficiently }
615 procedure mpq_swap(var rop1, rop2: mpq_t); cdecl; external LIB name '__gmpq_swap';
616
617 // ---- Floating-point Functions ----
618
619 { Set rop to the absolute value of op }
620 procedure mpf_abs(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_abs';
621 { Set rop to op1 + op2 }
622 procedure mpf_add(var rop, op1, op2: mpf_t); cdecl; external LIB name '__gmpf_add';
623 { Set rop to op1 + op2 }
624 procedure mpf_add_ui(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_add_ui';
625 { Set rop to op rounded to the next higher integer }
626 procedure mpf_ceil(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_ceil';
627 { Free the space occupied by x }
628 procedure mpf_clear(var x: mpf_t); cdecl; external LIB name '__gmpf_clear';
629 { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
mpf_cmpnull630 function mpf_cmp(var op1, op2: mpf_t): longint; cdecl; external LIB name '__gmpf_cmp';
631 { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
mpf_cmp_dnull632 function mpf_cmp_d(var op1: mpf_t; op2: double): longint; cdecl; external LIB name '__gmpf_cmp_d';
633 { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
mpf_cmp_sinull634 function mpf_cmp_si(var op1: mpf_t; op2: valsint): longint; cdecl; external LIB name '__gmpf_cmp_si';
635 { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
mpf_cmp_uinull636 function mpf_cmp_ui(var op1: mpf_t; op2: valuint): longint; cdecl; external LIB name '__gmpf_cmp_ui';
637 { Set rop to op1/op2 }
638 procedure mpf_div(var rop, op1, op2: mpf_t); cdecl; external LIB name '__gmpf_div';
639 { Set rop to op1/(2^op2) }
640 procedure mpf_div_2exp(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_div_2exp';
641 { Set rop to op1/op2 }
642 procedure mpf_div_ui(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_div_ui';
643
644 // GDB only: procedure mpf_dump(var _para1: mpf_t); cdecl; external LIB name '__gmpf_dump';
645
646 { Return non-zero if the first op3 bits of op1 and op2 are equal, zero otherwise }
mpf_eqnull647 function mpf_eq(var op1, op2: mpf_t; op3: valuint): longint; cdecl; external LIB name '__gmpf_eq';
648 { Return non-zero if op would fit in the respective C data type, when truncated to an integer }
mpf_fits_sint_pnull649 function mpf_fits_sint_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_fits_sint_p';
650 { Return non-zero if op would fit in the respective C data type, when truncated to an integer }
mpf_fits_slong_pnull651 function mpf_fits_slong_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_fits_slong_p';
652 { Return non-zero if op would fit in the respective C data type, when truncated to an integer }
mpf_fits_sshort_pnull653 function mpf_fits_sshort_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_fits_sshort_p';
654 { Return non-zero if op would fit in the respective C data type, when truncated to an integer }
mpf_fits_uint_pnull655 function mpf_fits_uint_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_fits_uint_p';
656 { Return non-zero if op would fit in the respective C data type, when truncated to an integer }
mpf_fits_ulong_pnull657 function mpf_fits_ulong_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_fits_ulong_p';
658 { Return non-zero if op would fit in the respective C data type, when truncated to an integer }
mpf_fits_ushort_pnull659 function mpf_fits_ushort_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_fits_ushort_p';
660 { Set rop to op rounded to the next lower }
661 procedure mpf_floor(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_floor';
662 { Convert op to a double, truncating if necessary (ie. rounding towards zero) }
mpf_get_dnull663 function mpf_get_d(var op: mpf_t): double; cdecl; external LIB name '__gmpf_get_d';
664 { Convert op to a double, truncating if necessary (ie. rounding towards zero), and with an exponent returned separately }
mpf_get_d_2expnull665 function mpf_get_d_2exp(out exp: valsint; var op: mpf_t): double; cdecl; external LIB name '__gmpf_get_d_2exp';
666 { Return the default precision actually used }
mpf_get_default_precnull667 function mpf_get_default_prec: valuint; cdecl; external LIB name '__gmpf_get_default_prec';
668 { Return the current precision of op, in bits }
mpf_get_precnull669 function mpf_get_prec(var op: mpf_t): valuint; cdecl; external LIB name '__gmpf_get_prec';
670 { Convert op to a long, truncating any fraction part }
mpf_get_sinull671 function mpf_get_si(var op: mpf_t): valsint; cdecl; external LIB name '__gmpf_get_si';
672 { Convert op to a string of digits in base base }
mpf_get_strnull673 function mpf_get_str(str: pchar; out exp: mp_exp_t; base: longint; ndigits: sizeuint; var op: mpf_t): pchar; cdecl; external LIB name '__gmpf_get_str';
674 { Convert op to a unsigned long, truncating any fraction part }
mpf_get_uinull675 function mpf_get_ui(var op: mpf_t): valuint; cdecl; external LIB name '__gmpf_get_ui';
676 { Initialize x to 0 }
677 procedure mpf_init(out x: mpf_t); cdecl; external LIB name '__gmpf_init';
678 { Initialize x to 0 and set its precision to be at least prec bits }
679 procedure mpf_init2(out x: mpf_t; prec: valuint); cdecl; external LIB name '__gmpf_init2';
680 { Initialize rop and set its value from op }
681 procedure mpf_init_set(out rop: mpf_t; var op: mpf_t); cdecl; external LIB name '__gmpf_init_set';
682 { Initialize rop and set its value from op }
683 procedure mpf_init_set_d(out rop: mpf_t; op: double); cdecl; external LIB name '__gmpf_init_set_d';
684 { Initialize rop and set its value from op }
685 procedure mpf_init_set_si(out rop: mpf_t; op: valsint); cdecl; external LIB name '__gmpf_init_set_si';
686 { Initialize rop and set its value from the string in str }
mpf_init_set_strnull687 function mpf_init_set_str(out rop: mpf_t; str: pchar; base: longint): longint; cdecl; external LIB name '__gmpf_init_set_str';
688 { Initialize rop and set its value from op }
689 procedure mpf_init_set_ui(out rop: mpf_t; op: valuint); cdecl; external LIB name '__gmpf_init_set_ui';
690 { Return non-zero if op is an integer }
mpf_integer_pnull691 function mpf_integer_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_integer_p';
692 { Set rop to op1 × op2 }
693 procedure mpf_mul(var rop, op1, op2: mpf_t); cdecl; external LIB name '__gmpf_mul';
694 { Set rop to op1 × (2^op2) }
695 procedure mpf_mul_2exp(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_mul_2exp';
696 { Set rop to op1 × op2 }
697 procedure mpf_mul_ui(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_mul_ui';
698 { Set rop to −op }
699 procedure mpf_neg(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_neg';
700 { Set rop to op1^op2 }
701 procedure mpf_pow_ui(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_pow_ui';
702 { Generate a random float of at most max_size limbs, with long strings of zeros and ones in the binary representation }
703 procedure mpf_random2(var rop: mpf_t; max_size: mp_size_t; exp: mp_exp_t); cdecl; external LIB name '__gmpf_random2';
704 { Compute the relative difference between op1 and op2 and store the result in rop }
705 procedure mpf_reldiff(var rop, op1, op2: mpf_t); cdecl; external LIB name '__gmpf_reldiff';
706 { Set the value of rop from op }
707 procedure mpf_set(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_set';
708 { Set the value of rop from op }
709 procedure mpf_set_d(var rop: mpf_t; op: double); cdecl; external LIB name '__gmpf_set_d';
710 { Set the default precision to be at least prec bits }
711 procedure mpf_set_default_prec(prec: valuint); cdecl; external LIB name '__gmpf_set_default_prec';
712 { Set the precision of rop to be at least prec bits }
713 procedure mpf_set_prec(var rop: mpf_t; prec: valuint); cdecl; external LIB name '__gmpf_set_prec';
714 { Set the precision of rop to be at least prec bits, without changing the memory allocated }
715 procedure mpf_set_prec_raw(var rop: mpf_t; prec: valuint); cdecl; external LIB name '__gmpf_set_prec_raw';
716 { Set the value of rop from op }
717 procedure mpf_set_q(var rop: mpf_t; var op: mpq_t); cdecl; external LIB name '__gmpf_set_q';
718 { Set the value of rop from op }
719 procedure mpf_set_si(var rop: mpf_t; op: valsint); cdecl; external LIB name '__gmpf_set_si';
720 { Set the value of rop from the string in str }
mpf_set_strnull721 function mpf_set_str(var rop: mpf_t; str: pchar; base: longint): longint; cdecl; external LIB name '__gmpf_set_str';
722 { Set the value of rop from op }
723 procedure mpf_set_ui(var rop: mpf_t; op: valuint); cdecl; external LIB name '__gmpf_set_ui';
724 { Set the value of rop from op }
725 procedure mpf_set_z(var rop: mpf_t; var op: mpz_t); cdecl; external LIB name '__gmpf_set_z';
726
mpf_sizenull727 // No docs: function mpf_size(var _para1: mpf_t): size_t; cdecl; external LIB name '__gmpf_size';
728
729 { Set rop to op^(1/2) }
730 procedure mpf_sqrt(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_sqrt';
731 { Set rop to op^(1/2) }
732 procedure mpf_sqrt_ui(var rop: mpf_t; op: valuint); cdecl; external LIB name '__gmpf_sqrt_ui';
733 { Set rop to op1 − op2 }
734 procedure mpf_sub(var rop, op1, op2: mpf_t); cdecl; external LIB name '__gmpf_sub';
735 { Set rop to op1 − op2 }
736 procedure mpf_sub_ui(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_sub_ui';
737 { Swap rop1 and rop2 efficiently }
738 procedure mpf_swap(var rop1, rop2: mpf_t); cdecl; external LIB name '__gmpf_swap';
739 { Set rop to op rounded to the integer towards zero }
740 procedure mpf_trunc(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_trunc';
741 { Set rop to op1/op2 }
742 procedure mpf_ui_div(var rop: mpf_t; op1: valuint; var op2: mpf_t); cdecl; external LIB name '__gmpf_ui_div';
743 { Set rop to op1 − op2 }
744 procedure mpf_ui_sub(var rop: mpf_t; op1: valuint; var op2: mpf_t); cdecl; external LIB name '__gmpf_ui_sub';
745 { Generate a uniformly distributed random float in rop, such that 0 <= rop < 1, with nbits significant bits in the mantissa }
746 procedure mpf_urandomb(var rop: mpf_t; var state: randstate_t; nbits: valuint); cdecl; external LIB name '__gmpf_urandomb';
747
748 // ---- Low-level Functions ----
749
750 { Add [s1p, s1n] and [s2p, s2n], and write the s1n least significant limbs of the result to rp. Return carry, either 0 or 1. }
mpn_addnull751 function mpn_add(rop, s1p: mpn_ptr; s1n: mp_size_t; s2p: mpn_ptr; s2n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpn_add';
752 { Add [s1p, n] and s2limb, and write the n least significant limbs of the result to rp. Return carry, either 0 or 1. }
mpn_add_1null753 function mpn_add_1(rp, s1p: mpn_ptr; n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_add_1';
754 { Add [s1p, n] and [s2p, n], and write the n least significant limbs of the result to rp. Return carry, either 0 or 1. }
mpn_add_nnull755 function mpn_add_n(rop, s1p, s2p: mpn_ptr; n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpn_add_n';
756 { Multiply [s1p, n] and s2limb, and add the n least significant limbs of the product to [rp, n] and write the result to rp. Return the most significant limb of the product, plus carry-out from the addition. }
mpn_addmul_1null757 function mpn_addmul_1(rp, s1p: mpn_ptr; n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_addmul_1';
758
759 { Preliminary: This function puts the low floor(d/mp_bits_per_limb) limbs of q = [s1p, s1n]/[s2p, s2n] mod 2^d at rp, and returns the high d mod mp_bits_per_limb bits of q }
mpn_bdivmodnull760 // function mpn_bdivmod(rp, s1p: mpn_t; s1n: mp_size_t; s2p: mpn_t; s2n: mp_size_t; d: valuint): mp_limb_t; cdecl; external LIB name '__gmpn_bdivmod';
761
762 { Compare [s1p, n] and [s2p, n] and return a positive value if s1 > s2, 0 if they are equal, or a negative value if s1 < s2 }
763 function mpn_cmp(s1p, s2p: mpn_ptr; n: mp_size_t): longint; cdecl; external LIB name '__gmpn_cmp';
764 { Divide [sp, n] by 3, expecting it to divide exactly, and writing the result to [rp, n] }
mpn_divexact_by3cnull765 function mpn_divexact_by3c(rp, sp: mpn_ptr; n: mp_size_t; carry: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_divexact_by3c';
766
767 { Obsolete: Divide [rs2p, rs2n] by [s3p, s3n], and write the quotient at r1p, with the exception of the most significant limb, which is returned }
mpn_divremnull768 // function mpn_divrem(r1p: mpn_t; qxn: mp_size_t; rs2p: mpn_t; rs2n: mp_size_t; s3p: mpn_t; s3n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpn_divrem';
769
770 { Divide [s2p, s2n] by s3limb, and write the quotient at r1p. Return the remainder }
771 function mpn_divrem_1(r1p: mpn_ptr; qxn: mp_size_t; s2p: mpn_ptr; s2n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_divrem_1';
772
mpn_divrem_2null773 // No docs: function mpn_divrem_2(_para1: mpn_t; _para2: mp_size_t; _para3: mpn_t; _para4: mp_size_t; _para5: mpn_t): mp_limb_t; cdecl; external LIB name '__gmpn_divrem_2';
774
775 { Set [rp, retval] to the greatest common divisor of [s1p, s1n] and [s2p, s2n] }
776 function mpn_gcd(rp, s1p: mpn_ptr; s1n: mp_size_t; s2p: mpn_ptr; s2n: mp_size_t): mp_size_t; cdecl; external LIB name '__gmpn_gcd';
777 { Return the greatest common divisor of [s1p, s1n] and s2limb }
mpn_gcd_1null778 function mpn_gcd_1(s1p: mpn_ptr; s1n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_gcd_1';
779 { Calculate the greatest common divisor of [s1p, s1n] and [s2p, s2n] }
mpn_gcdextnull780 function mpn_gcdext(r1p, r2p: mpn_ptr; out r2n: mp_size_t; s1p: mpn_ptr; s1n: mp_size_t; s2p: mpn_ptr; s2n: mp_size_t): mp_size_t; cdecl; external LIB name '__gmpn_gcdext';
781 { Convert [s1p, s1n] to a raw unsigned char array at str in base base, and return the number of characters produced }
mpn_get_strnull782 function mpn_get_str(str: pbyte; base: longint; s1p: mpn_ptr; s1n: mp_size_t):sizeuint; cdecl; external LIB name '__gmpn_get_str';
783 { Compute the hamming distance between [s1p, n] and [s2p, n], which is the number of bit positions where the two operands have different bit values }
mpn_hamdistnull784 function mpn_hamdist(s1p, s2p: mpn_ptr; n: mp_size_t): valuint; cdecl; external LIB name '__gmpn_hamdist';
785 { Shift [sp, n] left by count bits, and write the result to [rp, n] }
mpn_lshiftnull786 function mpn_lshift(rp, sp: mpn_ptr; n: mp_size_t; count: dword): mp_limb_t; cdecl; external LIB name '__gmpn_lshift';
787 { Divide [s1p, s1n] by s2limb, and return the remainder. s1n can be zero. }
mpn_mod_1null788 function mpn_mod_1(s1p: mpn_ptr; s1n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_mod_1';
789 { Multiply [s1p, s1n] and [s2p, s2n], and write the result to rp. Return the most significant limb of the result. }
mpn_mulnull790 function mpn_mul(rp, s1p: mpn_ptr; s1n: mp_size_t; s2p: mpn_ptr; s2n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpn_mul';
791 { Multiply [s1p, n] by s2limb, and write the n least significant limbs of the product to rp. Return the most significant limb of the product. }
mpn_mul_1null792 function mpn_mul_1(rp, s1p: mpn_ptr; n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_mul_1';
793 { Multiply [s1p, n] and [s2p, n], and write the 2*n-limb result to rp }
794 procedure mpn_mul_n(rp, s1p, s2p: mpn_ptr; n: mp_size_t); cdecl; external LIB name '__gmpn_mul_n';
795 { Return non-zero iff [s1p, n] is a perfect square }
mpn_perfect_square_pnull796 function mpn_perfect_square_p(s1p: mpn_ptr; n: mp_size_t): longint; cdecl; external LIB name '__gmpn_perfect_square_p';
797 { Count the number of set bits in [s1p, n] }
mpn_popcountnull798 function mpn_popcount(s1p: mpn_ptr; n: mp_size_t): valuint; cdecl; external LIB name '__gmpn_popcount';
799
mpn_pow_1null800 // No docs: function mpn_pow_1(_para1, _para2: mpn_t; _para3: mp_size_t; _para4, _para5: mpn_t): mp_size_t; cdecl; external LIB name '__gmpn_pow_1';
801 // No docs: function mpn_preinv_mod_1(_para1: mpn_t; _para2: mp_size_t; _para3, _para4: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_preinv_mod_1';
802
803 { Generate a random number of length r1n and store it at r1p }
804 procedure mpn_random(r1p: mpn_ptr; r1n: mp_size_t); cdecl; external LIB name '__gmpn_random';
805 { Generate a random number of length r1n and store it at r1p }
806 procedure mpn_random2(r1p: mpn_ptr; r1n: mp_size_t); cdecl; external LIB name '__gmpn_random2';
807 { Shift [sp, n] right by count bits, and write the result to [rp, n] }
mpn_rshiftnull808 function mpn_rshift(rp, sp: mpn_ptr; n: mp_size_t; count: dword): mp_limb_t; cdecl; external LIB name '__gmpn_rshift';
809 { Scan s1p from bit position bit for the next clear bit }
mpn_scan0null810 function mpn_scan0(s1p: mpn_ptr; bit: valuint): valuint; cdecl; external LIB name '__gmpn_scan0';
811 { Scan s1p from bit position bit for the next set bit }
mpn_scan1null812 function mpn_scan1(s1p: mpn_ptr; bit: valuint): valuint; cdecl; external LIB name '__gmpn_scan1';
813 { Convert bytes [str,strsize] in the given base to limbs at rp }
mpn_set_strnull814 function mpn_set_str(rp: mpn_ptr; str: pbyte; strsize: sizeuint; base:longint): mp_size_t; cdecl; external LIB name '__gmpn_set_str';
815 { Compute the square root of [sp, n] and put the result at [r1p, dn/2e] and the remainder at [r2p, retval] }
mpn_sqrtremnull816 function mpn_sqrtrem(r1p, r2p, sp: mpn_ptr; n: mp_size_t): mp_size_t; cdecl; external LIB name '__gmpn_sqrtrem';
817 { Subtract [s2p, s2n] from [s1p, s1n], and write the s1n least significant limbs of the result to rp. Return borrow, either 0 or 1. }
mpn_subnull818 function mpn_sub(rp, s1p: mpn_ptr; s1n: mp_size_t; s2p: mpn_ptr; s2n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpn_sub';
819 { Subtract s2limb from [s1p, n], and write the n least significant limbs of the result to rp. Return borrow, either 0 or 1. }
mpn_sub_1null820 function mpn_sub_1(rp, s1p: mpn_ptr; n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_sub_1';
821 { Subtract [s2p, n] from [s1p, n], and write the n least significant limbs of the result to rp. Return borrow, either 0 or 1. }
mpn_sub_nnull822 function mpn_sub_n(rp, s1p, s2p: mpn_ptr; n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpn_sub_n';
823 { Multiply [s1p, n] and s2limb, and subtract the n least significant limbs of the product from [rp, n] and write the result to rp. Return the most significant limb of the product, plus borrow-out from the subtraction. }
mpn_submul_1null824 function mpn_submul_1(rp, s1p: mpn_ptr; n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_submul_1';
825 { Divide [np, nn] by [dp, dn] and put the quotient at [qp, nn−dn+1] and the remainder at [rp, dn] }
826 procedure mpn_tdiv_qr(qp, rp: mpn_ptr; qxn: mp_size_t; np: mpn_ptr; nn: mp_size_t; dp: mpn_ptr; dn: mp_size_t); cdecl; external LIB name '__gmpn_tdiv_qr';
827
828 // ---- GMP properties ----
829
830 { Size of a limb on this machine }
bits_per_limbnull831 function bits_per_limb: longint;
832 { Some GMP functions may set this thread unsafe variable. Better avoid using it. }
errnonull833 function errno: longint;
834 { GMP version string a.b.c }
versionnull835 function version: string;
836
837 // ==== ext bindings =====
838
839 // ---- Random Number Functions ----
840
841 { Initialize state with a default algorithm }
842 procedure randinit_default(out state: MPRandState);
843 { Initialize state with a linear congruential algorithm X = (aX + c) mod 2^m2exp }
844 procedure randinit_lc_2exp(out state: MPRandState; var a: MPInteger; c, m2exp: valuint);
845 { Initialize state for a linear congruential algorithm as per gmp_randinit_lc_2exp }
randinit_lc_2exp_sizenull846 function randinit_lc_2exp_size(out state: MPRandState; size: sizeuint): boolean;
847 { Initialize state for a Mersenne Twister algorithm }
848 procedure randinit_mt(out state: MPRandState);
849 { Initialize rop with a copy of the algorithm and state from op }
850 procedure randinit_set(out rop: MPRandState; var op: MPRandState);
851 { Set an initial seed value into state }
852 procedure randseed(var state: MPRandState; var seed: MPInteger);
853 { Set an initial seed value into state }
854 procedure randseed_ui(var state: MPRandState; seed: valuint);
855 { Free all memory occupied by state }
856 procedure randclear(var state: MPRandState);
857 { Return an uniformly distributed random number of n bits, ie. in the range 0 to 2^n−1 inclusive }
urandomb_uinull858 function urandomb_ui(var state: MPRandState; n: valuint): valuint;
859 { Return an uniformly distributed random number in the range 0 to n − 1, inclusive }
urandomm_uinull860 function urandomm_ui(var state: MPRandState; n: valuint): valuint;
861
862 // ---- integer Functions ----
863
864 { Change the space for integer to new_alloc limbs }
z_reallocnull865 function z_realloc(var integer_: MPInteger; new_alloc: mp_size_t): pointer;
866 { Set rop to the absolute value of op }
867 procedure z_abs(var rop, op: MPInteger);
868 { Return the absolute value of op }
z_absnull869 function z_abs(var op: MPInteger): MPInteger;
870 { Set rop to op1 + op2 }
871 procedure z_add(var rop, op1, op2: MPInteger);
872 { Return op1 + op2 }
z_addnull873 function z_add(var op1, op2: MPInteger): MPInteger;
874 { Set rop to op1 + op2 }
875 procedure z_add_ui(var rop, op1: MPInteger; op2: valuint);
876 { Return op1 + op2 }
z_add_uinull877 function z_add_ui(var op1: MPInteger; op2: valuint): MPInteger;
878 { Set rop to rop + op1 × op2 }
879 procedure z_addmul(var rop, op1, op2: MPInteger);
880 { Set rop to rop + op1 × op2 }
881 procedure z_addmul_ui(var rop, op1: MPInteger; op2: valuint);
882 { Set rop to op1 bitwise-and op2 }
883 procedure z_and(var rop, op1, op2: MPInteger);
884 { Return op1 bitwise-and op2 }
z_andnull885 function z_and(var op1, op2: MPInteger): MPInteger;
886 //{ _Fixed_ space of fixed_num_bits is allocated to each of the array size integers in integer array }
887 //procedure z_array_init(var integer_array: MPInteger; array_size, fixed_num_bits: mp_size_t);
888 { Compute the binomial coefficient (n over k) and store the result in rop }
889 procedure z_bin_ui(var rop, n: MPInteger; k: valuint);
890 { Return the binomial coefficient (n over k) }
z_bin_uinull891 function z_bin_ui(var n: MPInteger; k: valuint): MPInteger;
892 { Compute the binomial coefficient (n over k) and store the result in rop }
893 procedure z_bin_uiui(var rop: MPInteger; n, k: valuint);
894 { Return the binomial coefficient (n over k) }
z_bin_uiuinull895 function z_bin_uiui(n, k: valuint): MPInteger;
896 { Divide n by d, forming a quotient q. Round mode ceil. }
897 procedure z_cdiv_q(var q, n, d: MPInteger);
898 { Divide n by d, forming a return quotient. Round mode ceil. }
z_cdiv_qnull899 function z_cdiv_q(var n, d: MPInteger): MPInteger;
900 { Divide n by d, forming a quotient q. d = 2^b. Round mode ceil. }
901 procedure z_cdiv_q_2exp(var q, n: MPInteger; b: valuint);
902 { Divide n by d, forming a return quotient. d = 2^b. Round mode ceil. }
z_cdiv_q_2expnull903 function z_cdiv_q_2exp(var n: MPInteger; b: valuint): MPInteger;
904 { Divide n by d, forming a quotient q. Round mode ceil. }
z_cdiv_q_uinull905 function z_cdiv_q_ui(var q, n: MPInteger; d: valuint): valuint;
906 { Divide n by d, forming a quotient q and remainder r. Round mode ceil. }
907 procedure z_cdiv_qr(var q, r, n, d: MPInteger);
908 { Divide n by d, forming a quotient q and remainder r. Round mode ceil. }
z_cdiv_qr_uinull909 function z_cdiv_qr_ui(var q, r, n: MPInteger; d: valuint): valuint;
910 { Divide n by d, forming a remainder r. Round mode ceil. }
911 procedure z_cdiv_r(var r, n, d: MPInteger);
912 { Divide n by d, forming a return remainder. Round mode ceil. }
z_cdiv_rnull913 function z_cdiv_r(var n, d: MPInteger): MPInteger;
914 { Divide n by d, forming a remainder r. d = 2^b. Round mode ceil. }
915 procedure z_cdiv_r_2exp(var r, n: MPInteger; b: valuint);
916 { Divide n by d, forming a return remainder. d = 2^b. Round mode ceil. }
z_cdiv_r_2expnull917 function z_cdiv_r_2exp(var n: MPInteger; b: valuint): MPInteger;
918 { Divide n by d, forming a remainder r. Round mode ceil. }
z_cdiv_r_uinull919 function z_cdiv_r_ui(var r, n: MPInteger; d: valuint): valuint;
920 { Divide n by d. Round mode ceil. }
z_cdiv_uinull921 function z_cdiv_ui(var n: MPInteger; d: valuint): valuint;
922 { Free the space occupied by integer. Call this function for all MPInteger variables when you are done with them. }
923 procedure z_clear(var integer_: MPInteger);
924 { Clear bit bit_index in rop }
925 procedure z_clrbit(var rop: MPInteger; bit_index: valuint);
926 { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
z_cmpnull927 function z_cmp(var op1, op2: MPInteger): longint;
928 { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
z_cmp_dnull929 function z_cmp_d(var op1: MPInteger; op2: double): longint;
930 { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
z_cmp_sinull931 function z_cmp_si(var op1: MPInteger; op2: valsint): longint;
932 { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
z_cmp_uinull933 function z_cmp_ui(var op1: MPInteger; op2: valuint): longint;
934 { Compare the absolute values of op1 and op2. Return a positive value if |op1| > |op2|, zero if |op1| = |op2|, or a negative value if |op1| < |op2|. }
z_cmpabsnull935 function z_cmpabs(var op1, op2: MPInteger): longint;
936 { Compare the absolute values of op1 and op2. Return a positive value if |op1| > |op2|, zero if |op1| = |op2|, or a negative value if |op1| < |op2|. }
z_cmpabs_dnull937 function z_cmpabs_d(var op1: MPInteger; op2: double): longint;
938 { Compare the absolute values of op1 and op2. Return a positive value if |op1| > |op2|, zero if |op1| = |op2|, or a negative value if |op1| < |op2|. }
z_cmpabs_uinull939 function z_cmpabs_ui(var op1: MPInteger; op2: valuint): longint;
940 { Set rop to the one’s complement of op }
941 procedure z_com(var rop, op: MPInteger);
942 { Return the one’s complement of op }
z_comnull943 function z_com(var op: MPInteger): MPInteger;
944 { Complement bit bit_index in rop }
945 procedure z_combit(var rop: MPInteger; bit_index: valuint);
946 { Return true if n is congruent to c modulo d }
z_congruent_pnull947 function z_congruent_p(var n, c, d: MPInteger): boolean;
948 { Return true if n is congruent to c modulo 2^b }
z_congruent_2exp_pnull949 function z_congruent_2exp_p(var n, c: MPInteger; b: valuint): boolean;
950 { Return true if n is congruent to c modulo d }
z_congruent_ui_pnull951 function z_congruent_ui_p(var n: MPInteger; c, d: valuint): boolean;
952 { Set q to n/d }
953 procedure z_divexact(var q, n, d: MPInteger);
954 { Return n/d }
z_divexactnull955 function z_divexact(var n, d: MPInteger): MPInteger;
956 { Set q to n/d }
957 procedure z_divexact_ui(var q, n: MPInteger; d: valuint);
958 { Return n/d }
z_divexact_uinull959 function z_divexact_ui(var n: MPInteger; d: valuint): MPInteger;
960 { Return true if n is exactly divisible by d }
z_divisible_pnull961 function z_divisible_p(var n, d: MPInteger): boolean;
962 { Return true if n is exactly divisible by d }
z_divisible_ui_pnull963 function z_divisible_ui_p(var n: MPInteger; d: valuint): boolean;
964 { Return true if n is exactly divisible by by 2^b }
z_divisible_2exp_pnull965 function z_divisible_2exp_p(var n: MPInteger; b: valuint): boolean;
966 { Fill buf with word data from op }
z_exportnull967 function z_export(out buf; out countp: sizeuint; order: longint; size: sizeuint; endian: longint; nails: sizeuint; var op: MPInteger): pointer;
968 { Set rop to op!, the factorial of op }
969 procedure z_fac_ui(var rop: MPInteger; op: valuint);
970 { Return op!, the factorial of op }
z_fac_uinull971 function z_fac_ui(op: valuint): MPInteger;
972 { Divide n by d, forming a quotient q. Round mode floor. }
973 procedure z_fdiv_q(var q, n, d: MPInteger);
974 { Divide n by d, forming a return quotient. Round mode floor. }
z_fdiv_qnull975 function z_fdiv_q(var n, d: MPInteger): MPInteger;
976 { Divide n by d, forming a quotient q. d = 2^b. Round mode floor. }
977 procedure z_fdiv_q_2exp(var q, n: MPInteger; b: valuint);
978 { Divide n by d, forming a return quotient. d = 2^b. Round mode floor. }
z_fdiv_q_2expnull979 function z_fdiv_q_2exp(var n: MPInteger; b: valuint): MPInteger;
980 { Divide n by d, forming a quotient q. Round mode floor. }
z_fdiv_q_uinull981 function z_fdiv_q_ui(var q, n: MPInteger; d: valuint): valuint;
982 { Divide n by d, forming a quotient q and remainder r. Round mode floor. }
983 procedure z_fdiv_qr(var q, r, n, d: MPInteger);
984 { Divide n by d, forming a quotient q and remainder r. Round mode floor. }
z_fdiv_qr_uinull985 function z_fdiv_qr_ui(var q, r, n: MPInteger; d: valuint): valuint;
986 { Divide n by d, forming a remainder r. Round mode floor. }
987 procedure z_fdiv_r(var r, n, d: MPInteger);
988 { Divide n by d, forming a return remainder. Round mode floor. }
z_fdiv_rnull989 function z_fdiv_r(var n, d: MPInteger): MPInteger;
990 { Divide n by d, forming a remainder r. d = 2^b. Round mode floor. }
991 procedure z_fdiv_r_2exp(var r, n: MPInteger; b: valuint);
992 { Divide n by d, forming a return remainder. d = 2^b. Round mode floor. }
z_fdiv_r_2expnull993 function z_fdiv_r_2exp(var n: MPInteger; b: valuint): MPInteger;
994 { Divide n by d, forming a remainder r. Round mode floor. }
z_fdiv_r_uinull995 function z_fdiv_r_ui(var r, n: MPInteger; d: valuint): valuint;
996 { Divide n by d. Round mode floor. }
z_fdiv_uinull997 function z_fdiv_ui(var n: MPInteger; d: valuint): valuint;
998 { Set fn to to Fn, the n’th Fibonacci number }
999 procedure z_fib_ui(var fn: MPInteger; n: valuint);
1000 { Return Fn, the n’th Fibonacci number }
z_fib_uinull1001 function z_fib_ui(n: valuint): MPInteger;
1002 { Set fn to Fn, and fnsub1 to Fn−1 }
1003 procedure z_fib2_ui(var fn, fnsub1: MPInteger; n: valuint);
1004 { Return Fn, and fnsub1 = Fn−1 }
z_fib2_uinull1005 function z_fib2_ui(var fnsub1: MPInteger; n: valuint): MPInteger;
1006 { Return true iff the value of op fits in an signed int }
z_fits_sint_pnull1007 function z_fits_sint_p(var op: MPInteger): boolean;
1008 { Return true iff the value of op fits in an signed long int }
z_fits_slong_pnull1009 function z_fits_slong_p(var op: MPInteger): boolean;
1010 { Return true iff the value of op fits in an signed short int }
z_fits_sshort_pnull1011 function z_fits_sshort_p(var op: MPInteger): boolean;
1012 { Return true iff the value of op fits in an unsigned int }
z_fits_uint_pnull1013 function z_fits_uint_p(var op: MPInteger): boolean;
1014 { Return true iff the value of op fits in an unsigned long int }
z_fits_ulong_pnull1015 function z_fits_ulong_p(var op: MPInteger): boolean;
1016 { Return true iff the value of op fits in an unsigned short int }
z_fits_ushort_pnull1017 function z_fits_ushort_p(var op: MPInteger): boolean;
1018 { Set rop to the greatest common divisor of op1 and op2 }
1019 procedure z_gcd(var rop, op1, op2: MPInteger);
1020 { Return the greatest common divisor of op1 and op2 }
z_gcdnull1021 function z_gcd(var op1, op2: MPInteger): MPInteger;
1022 { Compute the greatest common divisor of op1 and op2 }
z_gcd_uinull1023 function z_gcd_ui(var rop, op1: MPInteger; op2: valuint): valuint;
1024 { Set g to the greatest common divisor of a and b, and in addition set s and t to coefficients satisfying as + bt = g }
1025 procedure z_gcdext(var g, s, t, a, b: MPInteger);
1026 { Convert op to a double, truncating if necessary (ie. rounding towards zero) }
z_get_dnull1027 function z_get_d(var op: MPInteger): double;
1028 { Convert op to a double, truncating if necessary (ie. rounding towards zero), and returning the exponent separately }
z_get_d_2expnull1029 function z_get_d_2exp(out exp: valsint; var op: MPInteger): double;
1030 { Return the value of op as a signed long }
z_get_sinull1031 function z_get_si(op: MPInteger): valsint;
1032 { Convert op to a string of digits in base base. The base argument may vary from 2 to 62 or from −2 to −36. }
z_get_strnull1033 function z_get_str(base: longint; var op: MPInteger): string;
1034 { Convert op to a string of digits in base base. The base argument may vary from 2 to 62 or from −2 to −36. If str is NULL, the result string is allocated using the current allocation function }
z_get_strnull1035 function z_get_str(str: pchar; base: longint; var op: MPInteger): pchar;
1036 { Return the value of op as an unsigned long }
z_get_uinull1037 function z_get_ui(op: MPInteger): valuint;
1038 { Return limb number n from op }
z_getlimbnnull1039 function z_getlimbn(var op: MPInteger; n: mp_size_t): mp_limb_t;
1040 { If op1 and op2 are both >= 0 or both < 0, return the hamming distance between the two operands, which is the number of bit positions where op1 and op2 have different bit values }
z_hamdistnull1041 function z_hamdist(var op1, op2: MPInteger): valuint;
1042 { Set rop from an array of word data at op }
1043 procedure z_import(var rop: MPInteger; count: sizeuint; order: longint; size: sizeuint; endian: longint; nails: sizeuint; const op);
1044 { Initialize integer, and set its value to 0 }
1045 procedure z_init(out integer_: MPInteger);
1046 { Initialize integer, with space for n bits, and set its value to 0 }
1047 procedure z_init2(out integer_: MPInteger; n: valuint);
1048 { Initialize rop with limb space and set the initial numeric value from op }
1049 procedure z_init_set(out rop: MPInteger; var op: MPInteger);
1050 { Initialize rop with limb space and set the initial numeric value from op }
1051 procedure z_init_set_d(out rop: MPInteger; op: double);
1052 { Initialize rop with limb space and set the initial numeric value from op }
1053 procedure z_init_set_si(out rop: MPInteger; op: valsint);
1054 { Initialize rop and set its value like z_set_str. If the string is a correct base base number, the function returns true. }
z_init_set_strnull1055 function z_init_set_str(out rop: MPInteger; str: string; base: longint): boolean;
1056 { Initialize rop with limb space and set the initial numeric value from op }
1057 procedure z_init_set_ui(out rop: MPInteger; op: valuint);
1058 { Compute the inverse of op1 modulo op2 and put the result in rop }
z_invertnull1059 function z_invert(var rop, op1, op2: MPInteger): longint;
1060 { Set rop to op1 bitwise inclusive-or op2 }
1061 procedure z_ior(var rop, op1, op2: MPInteger);
1062 { Return bitwise inclusive-or op2 }
z_iornull1063 function z_ior(var op1, op2: MPInteger): MPInteger;
1064 { Calculate the Jacobi symbol. This is defined only for b odd }
z_jacobinull1065 function z_jacobi(var a, b: MPInteger): longint;
1066 { Calculate the Jacobi symbol with the Kronecker extension }
z_kronecker_sinull1067 function z_kronecker_si(var a: MPInteger; b: valsint): longint;
1068 { Calculate the Jacobi symbol with the Kronecker extension }
z_kronecker_uinull1069 function z_kronecker_ui(var a: MPInteger; b: valuint): longint;
1070 { Calculate the Jacobi symbol with the Kronecker extension }
z_si_kroneckernull1071 function z_si_kronecker(a: valsint; var b: MPInteger): longint;
1072 { Calculate the Jacobi symbol with the Kronecker extension }
z_ui_kroneckernull1073 function z_ui_kronecker(a: valuint; var b: MPInteger): longint;
1074 { Set rop to the least common multiple of op1 and op2 }
1075 procedure z_lcm(var rop, op1, op2: MPInteger);
1076 { Return the least common multiple of op1 and op2 }
z_lcmnull1077 function z_lcm(var op1, op2: MPInteger): MPInteger;
1078 { Set rop to the least common multiple of op1 and op2 }
1079 procedure z_lcm_ui(var rop, op1: MPInteger; op2: valuint);
1080 { Return the least common multiple of op1 and op2 }
z_lcm_uinull1081 function z_lcm_ui(var op1: MPInteger; op2: valuint): MPInteger;
1082 { Set ln to to Ln, the n’th Lucas number }
1083 procedure z_lucnum_ui(var ln: MPInteger; n: valuint);
1084 { Return Ln, the n’th Lucas number }
z_lucnum_uinull1085 function z_lucnum_ui(n: valuint): MPInteger;
1086 { Set ln to Ln, and lnsub1 to Ln−1 }
1087 procedure z_lucnum2_ui(var ln, lnsub1: MPInteger; n: valuint);
1088 { Return Ln, and lnsub1 to Ln−1 }
z_lucnum2_uinull1089 function z_lucnum2_ui(var lnsub1: MPInteger; n: valuint): MPInteger;
1090 { Set r to n mod d. The sign of the divisor is ignored; the result is always non-negative. }
1091 procedure z_mod(var r, n, d: MPInteger);
1092 { Return n mod d. The sign of the divisor is ignored; the result is always non-negative. }
z_modnull1093 function z_mod(var n, d: MPInteger): MPInteger;
1094 { Set rop to op1 × op2 }
1095 procedure z_mul(var rop, op1, op2: MPInteger);
1096 { Return op1 × op2 }
z_mulnull1097 function z_mul(var op1, op2: MPInteger): MPInteger;
1098 { Set rop to op1 × 2^op2. This operation can also be defined as a left shift by op2 bits. }
1099 procedure z_mul_2exp(var rop, op1: MPInteger; op2: valuint);
1100 { Return op1 × 2^op2. This operation can also be defined as a left shift by op2 bits. }
z_mul_2expnull1101 function z_mul_2exp(var op1: MPInteger; op2: valuint): MPInteger;
1102 { Set rop to op1 × op2 }
1103 procedure z_mul_si(var rop, op1: MPInteger; op2: valsint);
1104 { Return op1 × op2 }
z_mul_sinull1105 function z_mul_si(var op1: MPInteger; op2: valsint): MPInteger;
1106 { Set rop to op1 × op2 }
1107 procedure z_mul_ui(var rop, op1: MPInteger; op2: valuint);
1108 { Return op1 × op2 }
z_mul_uinull1109 function z_mul_ui(var op1: MPInteger; op2: valuint): MPInteger;
1110 { Set rop to −op }
1111 procedure z_neg(var rop, op: MPInteger);
1112 { Return −op }
z_negnull1113 function z_neg(var op: MPInteger): MPInteger;
1114 { Set rop to the next prime greater than op }
1115 procedure z_nextprime(var rop, op: MPInteger);
1116 { Return the next prime greater than op }
z_nextprimenull1117 function z_nextprime(var op: MPInteger): MPInteger;
1118 { Return true if op is a perfect power, i.e., if there exist integers a and b, with b > 1, such that op = a^b }
z_perfect_power_pnull1119 function z_perfect_power_p(var op: MPInteger): boolean;
1120 { Return true if op is a perfect square, i.e., if the square root of op is an integer }
z_perfect_square_pnull1121 function z_perfect_square_p(var op: MPInteger): boolean;
1122 { If op >= 0, return the population count of op, which is the number of 1 bits in the binary representation }
z_popcountnull1123 function z_popcount(var op: MPInteger): valuint;
1124 { Set rop to base^exp. The case 0^0 yields 1. }
1125 procedure z_pow_ui(var rop, base: MPInteger; exp: valuint);
1126 { Return base^exp. The case 0^0 yields 1. }
z_pow_uinull1127 function z_pow_ui(var base: MPInteger; exp: valuint): MPInteger;
1128 { Set rop to base^exp mod mod_ }
1129 procedure z_powm(var rop, base, exp, mod_: MPInteger);
1130 { Return base^exp mod mod_ }
z_powmnull1131 function z_powm(var base, exp, mod_: MPInteger): MPInteger;
1132 { Set rop to base^exp mod mod_ }
1133 procedure z_powm_ui(var rop, base: MPInteger; exp: valuint; var mod_: MPInteger);
1134 { Return base^exp mod mod_ }
z_powm_uinull1135 function z_powm_ui(var base: MPInteger; exp: valuint; var mod_: MPInteger): MPInteger;
1136 { Determine whether n is prime. Return 2 if n is definitely prime, return 1 if n is probably prime (without being certain), or return 0 if n is definitely composite. }
z_probab_prime_pnull1137 function z_probab_prime_p(var n: MPInteger; reps: longint): longint;
1138 { Change the space allocated for integer to n bits. The value in integer is preserved if it fits, or is set to 0 if not. }
1139 procedure z_realloc2(var integer_: MPInteger; n: valuint);
1140 { Remove all occurrences of the factor f from op and store the result in rop }
z_removenull1141 function z_remove(var rop, op, f: MPInteger): valuint;
1142 { Set rop to trunc(op^(1/n)), the truncated integer part of the nth root of op. Return true if the computation was exact, i.e., if op is rop to the nth power. }
z_rootnull1143 function z_root(var rop, op: MPInteger; n: valuint): boolean;
1144 { Set root to trunc(u^(1/n)), the truncated integer part of the nth root of u. Set rem to the remainder, (u − root^n). }
1145 procedure z_rootrem(var root, rem, u: MPInteger; n: valuint);
1146 { Generate a random integer with long strings of zeros and ones in the binary representation }
1147 procedure z_rrandomb(var rop: MPInteger; var state: MPRandState; n: valuint);
1148 { Return a random integer with long strings of zeros and ones in the binary representation }
z_rrandombnull1149 function z_rrandomb(var state: MPRandState; n: valuint): MPInteger;
1150 { Scan op, starting from bit starting_bit, towards more significant bits, until the first 0 bit is found }
z_scan0null1151 function z_scan0(var op: MPInteger; starting_bit: valuint): valuint;
1152 { Scan op, starting from bit starting_bit, towards more significant bits, until the first 1 bit is found }
z_scan1null1153 function z_scan1(var op: MPInteger; starting_bit: valuint): valuint;
1154 { Set the value of rop from op }
1155 procedure z_set(var rop, op: MPInteger);
1156 { Set the value of rop from op }
1157 procedure z_set_d(var rop: MPInteger; op: double);
1158 { Set the value of rop from op }
1159 procedure z_set_f(var rop: MPInteger; var op: MPFloat);
1160 { Set the value of rop from op }
1161 procedure z_set_q(var rop: MPInteger; var op: MPRational);
1162 { Set the value of rop from op }
1163 procedure z_set_si(var rop: MPInteger; op: valsint);
1164 { Set the value of rop from str, a null-terminated C string in base base. If the string is a correct base base number, the function returns true. }
z_set_strnull1165 function z_set_str(var rop: MPInteger; str: string; base: longint): boolean;
1166 { Set the value of rop from op }
1167 procedure z_set_ui(var rop: MPInteger; op: valuint);
1168 { Set bit bit_index in rop }
1169 procedure z_setbit(var rop: MPInteger; bit_index: valuint);
1170 { Return the size of op measured in number of limbs }
z_sizenull1171 function z_size(var op: MPInteger): sizeuint;
1172 { Return the size of op measured in number of digits in the given base }
z_sizeinbasenull1173 function z_sizeinbase(var op: MPInteger; base: longint): sizeuint;
1174 { Set rop to trunc(sqrt(op)), the truncated integer part of the square root of op }
1175 procedure z_sqrt(var rop, op: MPInteger);
1176 { Return trunc(sqrt(op)), the truncated integer part of the square root of op }
z_sqrtnull1177 function z_sqrt(var op: MPInteger): MPInteger;
1178 { Set rop1 to trunc(sqrt(op)), like z_sqrt. Set rop2 to the remainder (op − rop1^2), which will be zero if op is a perfect square. }
1179 procedure z_sqrtrem(var rop1, rop2, op: MPInteger);
1180 { Set rop to op1 − op2 }
1181 procedure z_sub(var rop, op1, op2: MPInteger);
1182 { Return op1 − op2 }
z_subnull1183 function z_sub(var op1, op2: MPInteger): MPInteger;
1184 { Set rop to op1 − op2 }
1185 procedure z_sub_ui(var rop, op1: MPInteger; op2: valuint);
1186 { Return op1 − op2 }
z_sub_uinull1187 function z_sub_ui(var op1: MPInteger; op2: valuint): MPInteger;
1188 { Set rop to op1 − op2 }
1189 procedure z_ui_sub(var rop: MPInteger; op1: valuint; var op2: MPInteger);
1190 { Return op1 − op2 }
z_ui_subnull1191 function z_ui_sub(op1: valuint; var op2: MPInteger): MPInteger;
1192 { Set rop to rop − op1 × op2 }
1193 procedure z_submul(var rop, op1, op2: MPInteger);
1194 { Set rop to rop − op1 × op2 }
1195 procedure z_submul_ui(var rop, op1: MPInteger; op2: valuint);
1196 { Swap the values rop1 and rop2 efficiently }
1197 procedure z_swap(var rop1, rop2: MPInteger);
1198 { Divide n by d, forming a quotient q. Round mode trunc. }
1199 procedure z_tdiv_q(var q, n, d: MPInteger);
1200 { Divide n by d, forming a return quotient. Round mode trunc. }
z_tdiv_qnull1201 function z_tdiv_q(var n, d: MPInteger): MPInteger;
1202 { Divide n by d, forming a quotient q. d = 2^b. Round mode trunc. }
1203 procedure z_tdiv_q_2exp(var q, n: MPInteger; b: valuint);
1204 { Divide n by d, forming a return quotient. d = 2^b. Round mode trunc. }
z_tdiv_q_2expnull1205 function z_tdiv_q_2exp(var n: MPInteger; b: valuint): MPInteger;
1206 { Divide n by d, forming a quotient q. Round mode trunc. }
z_tdiv_q_uinull1207 function z_tdiv_q_ui(var q, n: MPInteger; d: valuint): valuint;
1208 { Divide n by d, forming a quotient q and remainder r. Round mode trunc. }
1209 procedure z_tdiv_qr(var q, r, n, d: MPInteger);
1210 { Divide n by d, forming a quotient q and remainder r. Round mode trunc. }
z_tdiv_qr_uinull1211 function z_tdiv_qr_ui(var q, r, n: MPInteger; d: valuint): valuint;
1212 { Divide n by d, forming a remainder r. Round mode trunc. }
1213 procedure z_tdiv_r(var r, n, d: MPInteger);
1214 { Divide n by d, forming a return remainder. Round mode trunc. }
z_tdiv_rnull1215 function z_tdiv_r(var n, d: MPInteger): MPInteger;
1216 { Divide n by d, forming a remainder r. d = 2^b. Round mode trunc. }
1217 procedure z_tdiv_r_2exp(var r, n: MPInteger; b: valuint);
1218 { Divide n by d, forming a return remainder. d = 2^b. Round mode trunc. }
z_tdiv_r_2expnull1219 function z_tdiv_r_2exp(var n: MPInteger; b: valuint): MPInteger;
1220 { Divide n by d, forming a remainder r. Round mode trunc. }
z_tdiv_r_uinull1221 function z_tdiv_r_ui(var r, n: MPInteger; d: valuint): valuint;
1222 { Divide n by d. Round mode trunc. }
z_tdiv_uinull1223 function z_tdiv_ui(var n: MPInteger; d: valuint): valuint;
1224 { Test bit bit_index in op and return true or false accordingly }
z_tstbitnull1225 function z_tstbit(var rop: MPInteger; bit_index: valuint): boolean;
1226 { Set rop to base^exp. The case 0^0 yields 1 }
1227 procedure z_ui_pow_ui(var rop: MPInteger; base, exp: valuint);
1228 { Return base^exp. The case 0^0 yields 1 }
z_ui_pow_uinull1229 function z_ui_pow_ui(base, exp: valuint): MPInteger;
1230 { Generate a uniformly distributed random integer in the range 0 to 2^n − 1, inclusive }
1231 procedure z_urandomb(var rop: MPInteger; var state: MPRandState; n: valuint);
1232 { Return a uniformly distributed random integer in the range 0 to 2^n − 1, inclusive }
z_urandombnull1233 function z_urandomb(var state: MPRandState; n: valuint): MPInteger;
1234 { Generate a uniform random integer in the range 0 to n − 1, inclusive }
1235 procedure z_urandomm(var rop: MPInteger; var state: MPRandState; var n: MPInteger);
1236 { Return a uniform random integer in the range 0 to n − 1, inclusive }
z_urandommnull1237 function z_urandomm(var state: MPRandState; var n: MPInteger): MPInteger;
1238 { Set rop to op1 bitwise exclusive-or op2 }
1239 procedure z_xor(var rop, op1, op2: MPInteger);
1240 { Retuen op1 bitwise exclusive-or op2 }
z_xornull1241 function z_xor(var op1, op2: MPInteger): MPInteger;
1242
1243 // ---- Rational Number Functions ----
1244
1245 { Set rop to the absolute value of op }
1246 procedure q_abs(var rop, op: MPRational);
1247 { Return absolute value of op }
q_absnull1248 function q_abs(var op: MPRational): MPRational;
1249 { Set sum to addend1 + addend2 }
1250 procedure q_add(var sum, addend1, addend2: MPRational);
1251 { Return addend1 + addend2 }
q_addnull1252 function q_add(var addend1, addend2: MPRational): MPRational;
1253 { Remove any factors that are common to the numerator and denominator of op, and make the denominator positive }
1254 procedure q_canonicalize(var op: MPRational);
1255 { Free the space occupied by rational number }
1256 procedure q_clear(var rational_number: MPRational);
1257 { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2 }
q_cmpnull1258 function q_cmp(var op1, op2: MPRational): longint;
1259 { Compare op1 and num2/den2. Return a positive value if op1 > num2/den2, zero if op1 = num2/den2, and a negative value if op1 < num2/den2 }
q_cmp_sinull1260 function q_cmp_si(var op1: MPRational; num2: valsint; den2: valuint): longint;
1261 { Compare op1 and num2/den2. Return a positive value if op1 > num2/den2, zero if op1 = num2/den2, and a negative value if op1 < num2/den2 }
q_cmp_uinull1262 function q_cmp_ui(var op1: MPRational; num2, den2: valuint): longint;
1263 { Set quotient to dividend/divisor }
1264 procedure q_div(var quotient, dividend, divisor: MPRational);
1265 { Return dividend/divisor }
q_divnull1266 function q_div(var dividend, divisor: MPRational): MPRational;
1267 { Set rop to op1/(2^op2) }
1268 procedure q_div_2exp(var rop, op1: MPRational; op2: valuint);
1269 { Return op1/(2^op2) }
q_div_2expnull1270 function q_div_2exp(var op1: MPRational; op2: valuint): MPRational;
1271 { Return true if op1 and op2 are equal, false if they are non-equal }
q_equalnull1272 function q_equal(var op1, op2: MPRational): boolean;
1273 { Get the numerator of a rational }
1274 procedure q_get_num(var numerator: MPInteger; var rational: MPRational);
1275 { Return the numerator of a rational }
q_get_numnull1276 function q_get_num(var rational: MPRational): MPInteger;
1277 { Get the denominator of a rational }
1278 procedure q_get_den(var denominator: MPInteger; var rational: MPRational);
1279 { Return the denominator of a rational }
q_get_dennull1280 function q_get_den(var rational: MPRational): MPInteger;
1281 { Convert op to a double, truncating if necessary (ie. rounding towards zero) }
q_get_dnull1282 function q_get_d(var op: MPRational): double;
1283 { Convert op to a string of digits in base base }
q_get_strnull1284 function q_get_str(base: longint; var op: MPRational): string;
1285 { Convert op to a string of digits in base base. If str is NULL, the result string is allocated using the current allocation function. }
q_get_strnull1286 function q_get_str(str: pchar; base: longint; var op: MPRational): pchar;
1287 { Initialize dest rational and set it to 0/1 }
1288 procedure q_init(out dest_rational: MPRational);
1289 { Set inverted_number to 1/number }
1290 procedure q_inv(var inverted_number, number: MPRational);
1291 { Return 1/number }
q_invnull1292 function q_inv(var number: MPRational): MPRational;
1293 { Set product to multiplier × multiplicand }
1294 procedure q_mul(var product, multiplier, multiplicand: MPRational);
1295 { Return multiplier × multiplicand }
q_mulnull1296 function q_mul(var multiplier, multiplicand: MPRational): MPRational;
1297 { Set rop to op1 × (2^op2) }
1298 procedure q_mul_2exp(var rop, op1: MPRational; op2: valuint);
1299 { Return op1 × (2^op2) }
q_mul_2expnull1300 function q_mul_2exp(var op1: MPRational; op2: valuint): MPRational;
1301 { Set negated_operand to −operand }
1302 procedure q_neg(var negated_operand, operand: MPRational);
1303 { Return −operand }
q_negnull1304 function q_neg(var operand: MPRational): MPRational;
1305 { Assign rop from op }
1306 procedure q_set(var rop, op: MPRational);
1307 { Set rop to the value of op. There is no rounding, this conversion is exact. }
1308 procedure q_set_d(var rop: MPRational; op: double);
1309 { Set the denominator of a rational }
1310 procedure q_set_den(var rational: MPRational; var denominator: MPInteger);
1311 { Set rop to the value of op. There is no rounding, this conversion is exact. }
1312 procedure q_set_f(var rop: MPRational; var op: MPFloat);
1313 { Set the numerator of a rational }
1314 procedure q_set_num(var rational: MPRational; var numerator: MPInteger);
1315 { Set the value of rop to op1/op2 }
1316 procedure q_set_si(var rop: MPRational; op1: valsint; op2: valuint);
1317 { Set rop from a null-terminated string str in the given base. The return value is true if the entire string is a valid number. }
q_set_strnull1318 function q_set_str(var rop: MPRational; str: string; base: longint): boolean;
1319 { Set the value of rop to op1/op2 }
1320 procedure q_set_ui(var rop: MPRational; op1, op2: valuint);
1321 { Assign rop from op }
1322 procedure q_set_z(var rop: MPRational; var op: MPInteger);
1323 { Set difference to minuend − subtrahend }
1324 procedure q_sub(var difference, minuend, subtrahend: MPRational);
1325 { Return minuend − subtrahend }
q_subnull1326 function q_sub(var minuend, subtrahend: MPRational): MPRational;
1327 { Swap the values rop1 and rop2 efficiently }
1328 procedure q_swap(var rop1, rop2: MPRational);
1329
1330 // ---- Floating-point Functions ----
1331
1332 { Set rop to the absolute value of op }
1333 procedure f_abs(var rop, op: MPFloat);
1334 { Return the absolute value of op }
f_absnull1335 function f_abs(var op: MPFloat): MPFloat;
1336 { Set rop to op1 + op2 }
1337 procedure f_add(var rop, op1, op2: MPFloat);
1338 { Return op1 + op2 }
f_addnull1339 function f_add(var op1, op2: MPFloat): MPFloat;
1340 { Set rop to op1 + op2 }
1341 procedure f_add_ui(var rop, op1: MPFloat; op2: valuint);
1342 { Return op1 + op2 }
f_add_uinull1343 function f_add_ui(var op1: MPFloat; op2: valuint): MPFloat;
1344 { Set rop to op rounded to the next higher integer }
1345 procedure f_ceil(var rop, op: MPFloat);
1346 { Return op rounded to the next higher integer }
f_ceilnull1347 function f_ceil(var op: MPFloat): MPFloat;
1348 { Free the space occupied by x }
1349 procedure f_clear(var x: MPFloat);
1350 { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
f_cmpnull1351 function f_cmp(var op1, op2: MPFloat): longint;
1352 { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
f_cmp_dnull1353 function f_cmp_d(var op1: MPFloat; op2: double): longint;
1354 { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
f_cmp_sinull1355 function f_cmp_si(var op1: MPFloat; op2: valsint): longint;
1356 { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
f_cmp_uinull1357 function f_cmp_ui(var op1: MPFloat; op2: valuint): longint;
1358 { Set rop to op1/op2 }
1359 procedure f_div(var rop, op1, op2: MPFloat);
1360 { Return op1/op2 }
f_divnull1361 function f_div(var op1, op2: MPFloat): MPFloat;
1362 { Set rop to op1/(2^op2) }
1363 procedure f_div_2exp(var rop, op1: MPFloat; op2: valuint);
1364 { Return op1/(2^op2) }
f_div_2expnull1365 function f_div_2exp(var op1: MPFloat; op2: valuint): MPFloat;
1366 { Set rop to op1/op2 }
1367 procedure f_div_ui(var rop, op1: MPFloat; op2: valuint);
1368 { Return op1/op2 }
f_div_uinull1369 function f_div_ui(var op1: MPFloat; op2: valuint): MPFloat;
1370 { Return true if the first op3 bits of op1 and op2 are equal, false otherwise }
f_eqnull1371 function f_eq(var op1, op2: MPFloat; op3: valuint): boolean;
1372 { Return true if op would fit in the respective C data type, when truncated to an integer }
f_fits_sint_pnull1373 function f_fits_sint_p(var op: MPFloat): boolean;
1374 { Return true if op would fit in the respective C data type, when truncated to an integer }
f_fits_slong_pnull1375 function f_fits_slong_p(var op: MPFloat): boolean;
1376 { Return true if op would fit in the respective C data type, when truncated to an integer }
f_fits_sshort_pnull1377 function f_fits_sshort_p(var op: MPFloat): boolean;
1378 { Return true if op would fit in the respective C data type, when truncated to an integer }
f_fits_uint_pnull1379 function f_fits_uint_p(var op: MPFloat): boolean;
1380 { Return true if op would fit in the respective C data type, when truncated to an integer }
f_fits_ulong_pnull1381 function f_fits_ulong_p(var op: MPFloat): boolean;
1382 { Return true if op would fit in the respective C data type, when truncated to an integer }
f_fits_ushort_pnull1383 function f_fits_ushort_p(var op: MPFloat): boolean;
1384 { Set rop to op rounded to the next lower }
1385 procedure f_floor(var rop, op: MPFloat);
1386 { Return op rounded to the next lower }
f_floornull1387 function f_floor(var op: MPFloat): MPFloat;
1388 { Convert op to a double, truncating if necessary (ie. rounding towards zero) }
f_get_dnull1389 function f_get_d(var op: MPFloat): double;
1390 { Convert op to a double, truncating if necessary (ie. rounding towards zero), and with an exponent returned separately }
f_get_d_2expnull1391 function f_get_d_2exp(out exp: valsint; var op: MPFloat): double;
1392 { Return the default precision actually used }
f_get_default_precnull1393 function f_get_default_prec: valuint;
1394 { Return the current precision of op, in bits }
f_get_precnull1395 function f_get_prec(var op: MPFloat): valuint;
1396 { Convert op to a long, truncating any fraction part }
f_get_sinull1397 function f_get_si(var op: MPFloat): valsint;
1398 { Convert op to a string of digits in base base }
f_get_strnull1399 function f_get_str(out exp: mp_exp_t; base: longint; ndigits: sizeuint; var op: MPFloat): string;
1400 { Convert op to a string of digits in base base. If str is NULL, the result string is allocated using the current allocation function. }
f_get_strnull1401 function f_get_str(str: pchar; out exp: mp_exp_t; base: longint; ndigits: sizeuint; var op: MPFloat): pchar;
1402 { Convert op to a unsigned long, truncating any fraction part }
f_get_uinull1403 function f_get_ui(var op: MPFloat): valuint;
1404 { Initialize x to 0 }
1405 procedure f_init(out x: MPFloat);
1406 { Initialize x to 0 and set its precision to be at least prec bits }
1407 procedure f_init2(out x: MPFloat; prec: valuint);
1408 { Initialize rop and set its value from op }
1409 procedure f_init_set(out rop: MPFloat; var op: MPFloat);
1410 { Initialize rop and set its value from op }
1411 procedure f_init_set_d(out rop: MPFloat; op: double);
1412 { Initialize rop and set its value from op }
1413 procedure f_init_set_si(out rop: MPFloat; op: valsint);
1414 { Initialize rop and set its value from the string in str. Returns true if the entire string is a valid number in base base. }
f_init_set_strnull1415 function f_init_set_str(out rop: MPFloat; str: string; base: longint): boolean;
1416 { Initialize rop and set its value from op }
1417 procedure f_init_set_ui(out rop: MPFloat; op: valuint);
1418 { Return true if op is an integer }
f_integer_pnull1419 function f_integer_p(var op: MPFloat): boolean;
1420 { Set rop to op1 × op2 }
1421 procedure f_mul(var rop, op1, op2: MPFloat);
1422 { Return op1 × op2 }
f_mulnull1423 function f_mul(var op1, op2: MPFloat): MPFloat;
1424 { Set rop to op1 × (2^op2) }
1425 procedure f_mul_2exp(var rop, op1: MPFloat; op2: valuint);
1426 { Return op1 × (2^op2) }
f_mul_2expnull1427 function f_mul_2exp(var op1: MPFloat; op2: valuint): MPFloat;
1428 { Set rop to op1 × op2 }
1429 procedure f_mul_ui(var rop, op1: MPFloat; op2: valuint);
1430 { Return op1 × op2 }
f_mul_uinull1431 function f_mul_ui(var op1: MPFloat; op2: valuint): MPFloat;
1432 { Set rop to −op }
1433 procedure f_neg(var rop, op: MPFloat);
1434 { Return −op }
f_negnull1435 function f_neg(var op: MPFloat): MPFloat;
1436 { Set rop to op1^op2 }
1437 procedure f_pow_ui(var rop, op1: MPFloat; op2: valuint);
1438 { Return op1^op2 }
f_pow_uinull1439 function f_pow_ui(var op1: MPFloat; op2: valuint): MPFloat;
1440 { Generate a random float of at most max_size limbs, with long strings of zeros and ones in the binary representation }
1441 procedure f_random2(var rop: MPFloat; max_size: mp_size_t; exp: mp_exp_t);
1442 { Return a random float of at most max_size limbs, with long strings of zeros and ones in the binary representation }
f_random2null1443 function f_random2(max_size: mp_size_t; exp: mp_exp_t): MPFloat;
1444 { Compute the relative difference between op1 and op2 and store the result in rop }
1445 procedure f_reldiff(var rop, op1, op2: MPFloat);
1446 { Return the relative difference between op1 and op2 }
f_reldiffnull1447 function f_reldiff(var op1, op2: MPFloat): MPFloat;
1448 { Set the value of rop from op }
1449 procedure f_set(var rop, op: MPFloat);
1450 { Set the value of rop from op }
1451 procedure f_set_d(var rop: MPFloat; op: double);
1452 { Set the default precision to be at least prec bits }
1453 procedure f_set_default_prec(prec: valuint);
1454 { Set the precision of rop to be at least prec bits }
1455 procedure f_set_prec(var rop: MPFloat; prec: valuint);
1456 { Set the precision of rop to be at least prec bits, without changing the memory allocated }
1457 procedure f_set_prec_raw(var rop: MPFloat; prec: valuint);
1458 { Set the value of rop from op }
1459 procedure f_set_q(var rop: MPFloat; var op: MPRational);
1460 { Set the value of rop from op }
1461 procedure f_set_si(var rop: MPFloat; op: valsint);
1462 { Set the value of rop from the string in str. Returns true if the entire string is a valid number in base base. }
f_set_strnull1463 function f_set_str(var rop: MPFloat; str: string; base: longint): boolean;
1464 { Set the value of rop from op }
1465 procedure f_set_ui(var rop: MPFloat; op: valuint);
1466 { Set the value of rop from op }
1467 procedure f_set_z(var rop: MPFloat; var op: MPInteger);
1468 { Set rop to op^(1/2) }
1469 procedure f_sqrt(var rop, op: MPFloat);
1470 { Return op^(1/2) }
f_sqrtnull1471 function f_sqrt(var op: MPFloat): MPFloat;
1472 { Set rop to op^(1/2) }
1473 procedure f_sqrt_ui(var rop: MPFloat; op: valuint);
1474 { Return op^(1/2) }
f_sqrt_uinull1475 function f_sqrt_ui(op: valuint): MPFloat;
1476 { Set rop to op1 − op2 }
1477 procedure f_sub(var rop, op1, op2: MPFloat);
1478 { Return op1 − op2 }
f_subnull1479 function f_sub(var op1, op2: MPFloat): MPFloat;
1480 { Set rop to op1 − op2 }
1481 procedure f_sub_ui(var rop, op1: MPFloat; op2: valuint);
1482 { Return op1 − op2 }
f_sub_uinull1483 function f_sub_ui(var op1: MPFloat; op2: valuint): MPFloat;
1484 { Swap rop1 and rop2 efficiently }
1485 procedure f_swap(var rop1, rop2: MPFloat);
1486 { Set rop to op rounded to the integer towards zero }
1487 procedure f_trunc(var rop, op: MPFloat);
1488 { Return op rounded to the integer towards zero }
f_truncnull1489 function f_trunc(var op: MPFloat): MPFloat;
1490 { Set rop to op1/op2 }
1491 procedure f_ui_div(var rop: MPFloat; op1: valuint; var op2: MPFloat);
1492 { Return op1/op2 }
f_ui_divnull1493 function f_ui_div(op1: valuint; var op2: MPFloat): MPFloat;
1494 { Set rop to op1 − op2 }
1495 procedure f_ui_sub(var rop: MPFloat; op1: valuint; var op2: MPFloat);
1496 { Return op1 − op2 }
f_ui_subnull1497 function f_ui_sub(op1: valuint; var op2: MPFloat): MPFloat;
1498 { Generate a uniformly distributed random float in rop, such that 0 <= rop < 1, with nbits significant bits in the mantissa }
1499 procedure f_urandomb(var rop: MPFloat; var state: MPRandState; nbits: valuint);
1500 { Return a uniformly distributed random float, such that 0 <= result < 1, with nbits significant bits in the mantissa }
f_urandombnull1501 function f_urandomb(var state: MPRandState; nbits: valuint): MPFloat;
1502
1503 // ---- operators ----
1504
1505 operator * (op1: MPFloat; op2: MPFloat): MPFloat; inline;
1506 operator * (op1: MPInteger; op2: MPInteger): MPInteger; inline;
1507 operator * (op1: MPRational; op2: MPRational): MPRational; inline;
1508 operator ** (op1: MPFloat; op2: valuint): MPFloat; inline;
1509 operator ** (op1: MPInteger; op2: valuint): MPInteger; inline;
1510 operator + (op1: MPFloat; op2: MPFloat): MPFloat; inline;
1511 operator + (op1: MPInteger; op2: MPInteger): MPInteger; inline;
1512 operator + (op1: MPRational; op2: MPRational): MPRational; inline;
1513 operator - (op: MPFloat): MPFloat; inline;
1514 operator - (op: MPInteger): MPInteger; inline;
1515 operator - (op: MPRational): MPRational; inline;
1516 operator - (op1: MPFloat; op2: MPFloat): MPFloat; inline;
1517 operator - (op1: MPInteger; op2: MPInteger): MPInteger; inline;
1518 operator - (op1: MPRational; op2: MPRational): MPRational; inline;
1519 operator / (op1: MPFloat; op2: MPFloat): MPFloat; inline;
1520 operator / (op1: MPInteger; op2: MPInteger): MPInteger; inline;
1521 operator / (op1: MPRational; op2: MPRational): MPRational; inline;
1522 operator := (op: double): MPFloat; inline;
1523 operator := (op: double): MPInteger; inline;
1524 operator := (op: double): MPRational; inline;
1525 operator := (op: MPFloat): Cardinal; inline;
1526 operator := (op: MPFloat): double; inline;
1527 operator := (op: MPFloat): integer; inline;
1528 operator := (op: MPFloat): mpf_t; inline;
1529 operator := (op: MPFloat): MPInteger; inline;
1530 operator := (op: MPFloat): MPRational; inline;
1531 operator := (op: MPFloat): string; inline;
1532 {$ifdef CPU64}
1533 operator := (op: MPFloat): valsint; inline;
1534 operator := (op: MPFloat): valuint; inline;
1535 {$endif}
1536 operator := (var op: mpf_t): MPFloat; inline;
1537 operator := (op: MPInteger): cardinal; inline;
1538 operator := (op: MPInteger): double; inline;
1539 operator := (op: MPInteger): integer; inline;
1540 operator := (op: MPInteger): MPFloat; inline;
1541 operator := (op: MPInteger): MPRational; inline;
1542 operator := (op: MPInteger): mpz_t; inline;
1543 operator := (op: MPInteger): string; inline;
1544 {$ifdef CPU64}
1545 operator := (op: MPInteger): valsint; inline;
1546 operator := (op: MPInteger): valuint; inline;
1547 {$endif}
1548 operator := (var op: mpq_t): MPRational; inline;
1549 operator := (op: MPRandState): randstate_t; inline;
1550 operator := (op: MPRational): double; inline;
1551 operator := (op: MPRational): MPFloat; inline;
1552 operator := (op: MPRational): MPInteger; inline;
1553 operator := (op: MPRational): mpq_t; inline;
1554 operator := (op: MPRational): string; inline;
1555 operator := (var op: mpz_t): MPInteger; inline;
1556 operator := (var op: randstate_t): MPRandState; inline;
1557 operator := (op: string): MPFloat; inline;
1558 operator := (op: string): MPInteger; inline;
1559 operator := (op: string): MPRational; inline;
1560 operator := (op: valsint): MPFloat; inline;
1561 operator := (op: valsint): MPInteger; inline;
1562 operator := (op: valsint): MPRational; inline;
1563 operator := (op: valuint): MPFloat; inline;
1564 operator := (op: valuint): MPInteger; inline;
1565 operator := (op: valuint): MPRational; inline;
1566 operator < (op1: MPFloat; op2: MPFloat): boolean; inline;
1567 operator < (op1: MPInteger; op2: MPInteger): boolean; inline;
1568 operator < (op1: MPRational; op2: MPRational): boolean; inline;
1569 operator <= (op1: MPFloat; op2: MPFloat): boolean; inline;
1570 operator <= (op1: MPInteger; op2: MPInteger): boolean; inline;
1571 operator <= (op1: MPRational; op2: MPRational): boolean; inline;
1572 operator > (op1: MPFloat; op2: MPFloat): boolean; inline;
1573 operator > (op1: MPInteger; op2: MPInteger): boolean; inline;
1574 operator > (op1: MPRational; op2: MPRational): boolean; inline;
1575 operator >= (op1: MPFloat; op2: MPFloat): boolean; inline;
1576 operator >= (op1: MPInteger; op2: MPInteger): boolean; inline;
1577 operator >= (op1: MPRational; op2: MPRational): boolean; inline;
1578 // compiler doesn't like theese
1579 // operator = (op1: MPFloat; op2: MPFloat): boolean; inline;
1580 // operator = (op1: MPInteger; op2: MPInteger): boolean; inline;
1581 // operator = (op1: MPRational; op2: MPRational): boolean; inline;
1582
1583 implementation
1584
1585 uses
1586 math;
1587
1588 {$ifndef NO_GMP_GLOBVARS}
1589 var
1590 __gmp_bits_per_limb: longint; cvar; external;
1591 __gmp_errno: longint; cvar; external;
1592 __gmp_version: pchar; cvar; external;
1593
1594 function bits_per_limb: longint;
1595 begin
1596 result := __gmp_bits_per_limb;
1597 end;
1598
1599 function errno: longint;
1600 begin
1601 result := __gmp_errno;
1602 end;
1603
1604 function version: string;
1605 begin
1606 result := __gmp_version;
1607 end;
1608
1609 {$else NO_GMP_GLOBVARS}
1610 function bits_per_limb: longint;
1611 const BITS_PER_BYTE = 8;
1612 begin
1613 result := sizeof(mp_limb_t) * BITS_PER_BYTE;
1614 end;
1615
1616 function errno: longint;
1617 begin
1618 result := 0;
1619 end;
1620
1621 function version: string;
1622 const NO_VER = '0.0.0';
1623 begin
1624 result := NO_VER;
1625 end;
1626 {$endif NO_GMP_GLOBVARS}
1627
1628 // ---- ext types ----
1629
1630 { TMPBase }
1631
refsnull1632 function TMPBase.refs: longint;
1633 begin
1634 result := frefcount;
1635 end;
1636
1637 { TMPInteger }
1638
ptrnull1639 function TMPInteger.ptr: mpz_ptr;
1640 begin
1641 result := @fmpz
1642 end;
1643
1644 destructor TMPInteger.destroy;
1645 begin
1646 mpz_clear(fmpz);
1647 inherited destroy;
1648 end;
1649
1650 { TMPFloat }
1651
ptrnull1652 function TMPFloat.ptr: mpf_ptr;
1653 begin
1654 result := @fmpf;
1655 end;
1656
1657 destructor TMPFloat.destroy;
1658 begin
1659 mpf_clear(fmpf);
1660 inherited destroy;
1661 end;
1662
1663 { TMPRational }
1664
ptrnull1665 function TMPRational.ptr: mpq_ptr;
1666 begin
1667 result := @fmpq;
1668 end;
1669
1670 destructor TMPRational.destroy;
1671 begin
1672 mpq_clear(fmpq);
1673 inherited destroy;
1674 end;
1675
1676 { TMPRandState }
1677
ptrnull1678 function TMPRandState.ptr: randstate_ptr;
1679 begin
1680 result := @frandstate;
1681 end;
1682
1683 destructor TMPRandState.destroy;
1684 begin
1685 mp_randclear(frandstate);
1686 inherited destroy;
1687 end;
1688
1689 // --- helpers ----
1690
1691 function dest(var rop: MPInteger): mpz_ptr;
1692 begin
1693 if (not assigned(rop)) or (rop.refs > 1) then
1694 z_init(rop);
1695 result := rop.ptr;
1696 end;
1697
1698 function dest(var rop: MPFloat): mpf_ptr;
1699 begin
1700 if (not assigned(rop)) or (rop.refs > 1) then
1701 f_init(rop);
1702 result := rop.ptr;
1703 end;
1704
1705 function dest(var rop: MPRational): mpq_ptr;
1706 begin
1707 if (not assigned(rop)) or (rop.refs > 1) then
1708 q_init(rop);
1709 result := rop.ptr;
1710 end;
1711
1712 function dest(var rop: MPRandState): randstate_ptr;
1713 begin
1714 if (not assigned(rop)) or (rop.refs > 1) then
1715 randinit_default(rop);
1716 result := rop.ptr;
1717 end;
1718
1719 function src(var rop: MPInteger): mpz_ptr;
1720 begin
1721 if not assigned(rop) then
1722 z_init(rop);
1723 result := rop.ptr;
1724 end;
1725
1726 function src(var rop: MPFloat): mpf_ptr;
1727 begin
1728 if not assigned(rop) then
1729 f_init(rop);
1730 result := rop.ptr;
1731 end;
1732
1733 function src(var rop: MPRational): mpq_ptr;
1734 begin
1735 if not assigned(rop) then
1736 q_init(rop);
1737 result := rop.ptr;
1738 end;
1739
1740 function src(var rop: MPRandState): randstate_ptr;
1741 begin
1742 if not assigned(rop) then
1743 randinit_default(rop);
1744 result := rop.ptr;
1745 end;
1746
1747 procedure propagate_prec(var result, op: MPFloat);
1748 begin
1749 f_set_prec(result, f_get_prec(op));
1750 end;
1751
1752 procedure propagate_prec(var result, op1, op2: MPFloat);
1753 begin
1754 f_set_prec(result, max( int64(f_get_prec(op1)), Int64(f_get_prec(op2)) ) );
1755 end;
1756
1757 // --- ext bindings ----
1758
1759 procedure randinit_default(out state: MPRandState);
1760 begin
1761 state := TMPRandState.Create;
1762 mp_randinit_default(state.ptr^);
1763 end;
1764
1765 procedure randinit_lc_2exp(out state: MPRandState; var a: MPInteger; c, m2exp: valuint);
1766 begin
1767 state := TMPRandState.Create;
1768 mp_randinit_lc_2exp(state.ptr^, src(a)^, c, m2exp);
1769 end;
1770
1771 function randinit_lc_2exp_size(out state: MPRandState; size: sizeuint): boolean;
1772 begin
1773 state := TMPRandState.Create;
1774 result := mp_randinit_lc_2exp_size(state.ptr^, size) <> 0;
1775 end;
1776
1777 procedure randinit_mt(out state: MPRandState);
1778 begin
1779 state := TMPRandState.Create;
1780 mp_randinit_mt(state.ptr^);
1781 end;
1782
1783 procedure randinit_set(out rop: MPRandState; var op: MPRandState);
1784 begin
1785 rop := TMPRandState.Create;
1786 mp_randinit_set(rop.ptr^, src(op)^);
1787 end;
1788
1789 procedure randseed(var state: MPRandState; var seed: MPInteger);
1790 begin
1791 mp_randseed(dest(state)^, src(seed)^);
1792 end;
1793
1794 procedure randseed_ui(var state: MPRandState; seed: valuint);
1795 begin
1796 mp_randseed_ui(dest(state)^, seed);
1797 end;
1798
1799 procedure randclear(var state: MPRandState);
1800 begin
1801 state := nil;
1802 end;
1803
1804 function urandomb_ui(var state: MPRandState; n: valuint): valuint;
1805 begin
1806 result := mp_urandomb_ui(dest(state)^, n);
1807 end;
1808
1809 function urandomm_ui(var state: MPRandState; n: valuint): valuint;
1810 begin
1811 result := mp_urandomb_ui(dest(state)^, n);
1812 end;
1813
1814 function z_realloc(var integer_: MPInteger; new_alloc: mp_size_t): pointer;
1815 begin
1816 result := mpz_realloc(dest(integer_)^, new_alloc);
1817 end;
1818
1819 procedure z_abs(var rop, op: MPInteger);
1820 begin
1821 mpz_abs(dest(rop)^, src(op)^);
1822 end;
1823
1824 function z_abs(var op: MPInteger): MPInteger;
1825 begin
1826 mpz_abs(dest(result)^, src(op)^);
1827 end;
1828
1829 procedure z_add(var rop, op1, op2: MPInteger);
1830 begin
1831 mpz_add(dest(rop)^, src(op1)^, src(op2)^);
1832 end;
1833
1834 function z_add(var op1, op2: MPInteger): MPInteger;
1835 begin
1836 mpz_add(dest(result)^, src(op1)^, src(op2)^);
1837 end;
1838
1839 procedure z_add_ui(var rop, op1: MPInteger; op2: valuint);
1840 begin
1841 mpz_add_ui(dest(rop)^, src(op1)^, op2);
1842 end;
1843
1844 function z_add_ui(var op1: MPInteger; op2: valuint): MPInteger;
1845 begin
1846 mpz_add_ui(dest(result)^, src(op1)^, op2);
1847 end;
1848
1849 procedure z_addmul(var rop, op1, op2: MPInteger);
1850 begin
1851 mpz_addmul(dest(rop)^, src(op1)^, src(op2)^);
1852 end;
1853
1854 procedure z_addmul_ui(var rop, op1: MPInteger; op2: valuint);
1855 begin
1856 mpz_addmul_ui(dest(rop)^, src(op1)^, op2);
1857 end;
1858
1859 procedure z_and(var rop, op1, op2: MPInteger);
1860 begin
1861 mpz_and(dest(rop)^, src(op1)^, src(op2)^);
1862 end;
1863
1864 function z_and(var op1, op2: MPInteger): MPInteger;
1865 begin
1866 mpz_and(dest(result)^, src(op1)^, src(op2)^);
1867 end;
1868
1869 procedure z_bin_ui(var rop, n: MPInteger; k: valuint);
1870 begin
1871 mpz_bin_ui(dest(rop)^, src(n)^, k);
1872 end;
1873
1874 function z_bin_ui(var n: MPInteger; k: valuint): MPInteger;
1875 begin
1876 mpz_bin_ui(dest(result)^, src(n)^, k);
1877 end;
1878
1879 procedure z_bin_uiui(var rop: MPInteger; n, k: valuint);
1880 begin
1881 mpz_bin_uiui(dest(rop)^, n, k);
1882 end;
1883
1884 function z_bin_uiui(n, k: valuint): MPInteger;
1885 begin
1886 mpz_bin_uiui(dest(result)^, n, k);
1887 end;
1888
1889 procedure z_cdiv_q(var q, n, d: MPInteger);
1890 begin
1891 mpz_cdiv_q(dest(q)^, src(n)^, src(d)^);
1892 end;
1893
1894 function z_cdiv_q(var n, d: MPInteger): MPInteger;
1895 begin
1896 mpz_cdiv_q(dest(result)^, src(n)^, src(d)^);
1897 end;
1898
1899 procedure z_cdiv_q_2exp(var q, n: MPInteger; b: valuint);
1900 begin
1901 mpz_cdiv_q_2exp(dest(q)^, src(n)^, b);
1902 end;
1903
1904 function z_cdiv_q_2exp(var n: MPInteger; b: valuint): MPInteger;
1905 begin
1906 mpz_cdiv_q_2exp(dest(result)^, src(n)^, b);
1907 end;
1908
1909 function z_cdiv_q_ui(var q, n: MPInteger; d: valuint): valuint;
1910 begin
1911 result := mpz_cdiv_q_ui(dest(q)^, src(n)^, d);
1912 end;
1913
1914 procedure z_cdiv_qr(var q, r, n, d: MPInteger);
1915 begin
1916 mpz_cdiv_qr(dest(q)^, dest(r)^, src(n)^, src(d)^);
1917 end;
1918
1919 function z_cdiv_qr_ui(var q, r, n: MPInteger; d: valuint): valuint;
1920 begin
1921 result := mpz_cdiv_qr_ui(dest(q)^, dest(r)^, src(n)^, d);
1922 end;
1923
1924 procedure z_cdiv_r(var r, n, d: MPInteger);
1925 begin
1926 mpz_cdiv_r(dest(r)^, src(n)^, src(d)^);
1927 end;
1928
1929 function z_cdiv_r(var n, d: MPInteger): MPInteger;
1930 begin
1931 mpz_cdiv_r(dest(result)^, src(n)^, src(d)^);
1932 end;
1933
1934 procedure z_cdiv_r_2exp(var r, n: MPInteger; b: valuint);
1935 begin
1936 mpz_cdiv_r_2exp(dest(r)^, src(n)^, b);
1937 end;
1938
1939 function z_cdiv_r_2exp(var n: MPInteger; b: valuint): MPInteger;
1940 begin
1941 mpz_cdiv_r_2exp(dest(result)^, src(n)^, b);
1942 end;
1943
1944 function z_cdiv_r_ui(var r, n: MPInteger; d: valuint): valuint;
1945 begin
1946 result := mpz_cdiv_r_ui(dest(r)^, src(n)^, d);
1947 end;
1948
1949 function z_cdiv_ui(var n: MPInteger; d: valuint): valuint;
1950 begin
1951 result := mpz_cdiv_ui(src(n)^, d);
1952 end;
1953
1954 procedure z_clear(var integer_: MPInteger);
1955 begin
1956 integer_ := nil;
1957 end;
1958
1959 procedure z_clrbit(var rop: MPInteger; bit_index: valuint);
1960 begin
1961 mpz_clrbit(dest(rop)^, bit_index);
1962 end;
1963
1964 function z_cmp(var op1, op2: MPInteger): longint;
1965 begin
1966 result := mpz_cmp(src(op1)^, src(op2)^);
1967 end;
1968
1969 function z_cmp_d(var op1: MPInteger; op2: double): longint;
1970 begin
1971 result := mpz_cmp_d(src(op1)^, op2);
1972 end;
1973
1974 function z_cmp_si(var op1: MPInteger; op2: valsint): longint;
1975 begin
1976 result := mpz_cmp_si(src(op1)^, op2);
1977 end;
1978
1979 function z_cmp_ui(var op1: MPInteger; op2: valuint): longint;
1980 begin
1981 result := mpz_cmp_ui(src(op1)^, op2);
1982 end;
1983
1984 function z_cmpabs(var op1, op2: MPInteger): longint;
1985 begin
1986 result := mpz_cmpabs(src(op1)^, src(op2)^);
1987 end;
1988
1989 function z_cmpabs_d(var op1: MPInteger; op2: double): longint;
1990 begin
1991 result := mpz_cmpabs_d(src(op1)^, op2);
1992 end;
1993
1994 function z_cmpabs_ui(var op1: MPInteger; op2: valuint): longint;
1995 begin
1996 result := mpz_cmpabs_ui(src(op1)^, op2);
1997 end;
1998
1999 procedure z_com(var rop, op: MPInteger);
2000 begin
2001 mpz_com(dest(rop)^, src(op)^);
2002 end;
2003
2004 function z_com(var op: MPInteger): MPInteger;
2005 begin
2006 mpz_com(dest(result)^, src(op)^);
2007 end;
2008
2009 procedure z_combit(var rop: MPInteger; bit_index: valuint);
2010 begin
2011 mpz_combit(dest(rop)^, bit_index);
2012 end;
2013
2014 function z_congruent_p(var n, c, d: MPInteger): boolean;
2015 begin
2016 result := mpz_congruent_p(src(n)^, src(c)^, src(d)^) <> 0;
2017 end;
2018
2019 function z_congruent_2exp_p(var n, c: MPInteger; b: valuint): boolean;
2020 begin
2021 result := mpz_congruent_2exp_p(src(n)^, src(c)^, b) <> 0;
2022 end;
2023
2024 function z_congruent_ui_p(var n: MPInteger; c, d: valuint): boolean;
2025 begin
2026 result := mpz_congruent_ui_p(src(n)^, c, d) <> 0;
2027 end;
2028
2029 procedure z_divexact(var q, n, d: MPInteger);
2030 begin
2031 mpz_divexact(dest(q)^, src(n)^, src(d)^);
2032 end;
2033
2034 function z_divexact(var n, d: MPInteger): MPInteger;
2035 begin
2036 mpz_divexact(dest(result)^, src(n)^, src(d)^);
2037 end;
2038
2039 procedure z_divexact_ui(var q, n: MPInteger; d: valuint);
2040 begin
2041 mpz_divexact_ui(dest(q)^, src(n)^, d);
2042 end;
2043
2044 function z_divexact_ui(var n: MPInteger; d: valuint): MPInteger;
2045 begin
2046 mpz_divexact_ui(dest(result)^, src(n)^, d);
2047 end;
2048
2049 function z_divisible_p(var n, d: MPInteger): boolean;
2050 begin
2051 result := mpz_divisible_p(src(n)^, src(d)^) <> 0;
2052 end;
2053
2054 function z_divisible_ui_p(var n: MPInteger; d: valuint): boolean;
2055 begin
2056 result := mpz_divisible_ui_p(src(n)^, d) <> 0;
2057 end;
2058
2059 function z_divisible_2exp_p(var n: MPInteger; b: valuint): boolean;
2060 begin
2061 result := mpz_divisible_2exp_p(src(n)^, b) <> 0;
2062 end;
2063
2064 function z_export(out buf; out countp: sizeuint; order: longint; size: sizeuint; endian: longint; nails: sizeuint; var op: MPInteger): pointer;
2065 begin
2066 result := mpz_export(buf, countp, order, size, endian, nails, src(op)^);
2067 end;
2068
2069 procedure z_fac_ui(var rop: MPInteger; op: valuint);
2070 begin
2071 mpz_fac_ui(dest(rop)^, op);
2072 end;
2073
2074 function z_fac_ui(op: valuint): MPInteger;
2075 begin
2076 mpz_fac_ui(dest(result)^, op);
2077 end;
2078
2079 procedure z_fdiv_q(var q, n, d: MPInteger);
2080 begin
2081 mpz_fdiv_q(dest(q)^, src(n)^, src(d)^);
2082 end;
2083
2084 function z_fdiv_q(var n, d: MPInteger): MPInteger;
2085 begin
2086 mpz_fdiv_q(dest(result)^, src(n)^, src(d)^);
2087 end;
2088
2089 procedure z_fdiv_q_2exp(var q, n: MPInteger; b: valuint);
2090 begin
2091 mpz_fdiv_q_2exp(dest(q)^, src(n)^, b);
2092 end;
2093
2094 function z_fdiv_q_2exp(var n: MPInteger; b: valuint): MPInteger;
2095 begin
2096 mpz_fdiv_q_2exp(dest(result)^, src(n)^, b);
2097 end;
2098
2099 function z_fdiv_q_ui(var q, n: MPInteger; d: valuint): valuint;
2100 begin
2101 result := mpz_fdiv_q_ui(dest(q)^, src(n)^, d);
2102 end;
2103
2104 procedure z_fdiv_qr(var q, r, n, d: MPInteger);
2105 begin
2106 mpz_fdiv_qr(dest(q)^, dest(r)^, src(n)^, src(d)^);
2107 end;
2108
2109 function z_fdiv_qr_ui(var q, r, n: MPInteger; d: valuint): valuint;
2110 begin
2111 result := mpz_fdiv_qr_ui(dest(q)^, dest(r)^, src(n)^, d);
2112 end;
2113
2114 procedure z_fdiv_r(var r, n, d: MPInteger);
2115 begin
2116 mpz_fdiv_r(dest(r)^, src(n)^, src(d)^);
2117 end;
2118
2119 function z_fdiv_r(var n, d: MPInteger): MPInteger;
2120 begin
2121 mpz_fdiv_r(dest(result)^, src(n)^, src(d)^);
2122 end;
2123
2124 procedure z_fdiv_r_2exp(var r, n: MPInteger; b: valuint);
2125 begin
2126 mpz_fdiv_r_2exp(dest(r)^, src(n)^, b);
2127 end;
2128
2129 function z_fdiv_r_2exp(var n: MPInteger; b: valuint): MPInteger;
2130 begin
2131 mpz_fdiv_r_2exp(dest(result)^, src(n)^, b);
2132 end;
2133
2134 function z_fdiv_r_ui(var r, n: MPInteger; d: valuint): valuint;
2135 begin
2136 result := mpz_fdiv_r_ui(dest(r)^, src(n)^, d);
2137 end;
2138
2139 function z_fdiv_ui(var n: MPInteger; d: valuint): valuint;
2140 begin
2141 result := mpz_fdiv_ui(src(n)^, d);
2142 end;
2143
2144 procedure z_fib_ui(var fn: MPInteger; n: valuint);
2145 begin
2146 mpz_fib_ui(dest(fn)^, n);
2147 end;
2148
2149 function z_fib_ui(n: valuint): MPInteger;
2150 begin
2151 mpz_fib_ui(dest(result)^, n);
2152 end;
2153
2154 procedure z_fib2_ui(var fn, fnsub1: MPInteger; n: valuint);
2155 begin
2156 mpz_fib2_ui(dest(fn)^, dest(fnsub1)^, n);
2157 end;
2158
2159 function z_fib2_ui(var fnsub1: MPInteger; n: valuint): MPInteger;
2160 begin
2161 mpz_fib2_ui(dest(result)^, dest(fnsub1)^, n);
2162 end;
2163
2164 function z_fits_sint_p(var op: MPInteger): boolean;
2165 begin
2166 result := mpz_fits_sint_p(src(op)^) <> 0;
2167 end;
2168
2169 function z_fits_slong_p(var op: MPInteger): boolean;
2170 begin
2171 result := mpz_fits_slong_p(src(op)^) <> 0;
2172 end;
2173
2174 function z_fits_sshort_p(var op: MPInteger): boolean;
2175 begin
2176 result := mpz_fits_sshort_p(src(op)^) <> 0;
2177 end;
2178
2179 function z_fits_uint_p(var op: MPInteger): boolean;
2180 begin
2181 result := mpz_fits_uint_p(src(op)^) <> 0;
2182 end;
2183
2184 function z_fits_ulong_p(var op: MPInteger): boolean;
2185 begin
2186 result := mpz_fits_ulong_p(src(op)^) <> 0;
2187 end;
2188
2189 function z_fits_ushort_p(var op: MPInteger): boolean;
2190 begin
2191 result := mpz_fits_ushort_p(src(op)^) <> 0;
2192 end;
2193
2194 procedure z_gcd(var rop, op1, op2: MPInteger);
2195 begin
2196 mpz_gcd(dest(rop)^, src(op1)^, src(op2)^);
2197 end;
2198
2199 function z_gcd(var op1, op2: MPInteger): MPInteger;
2200 begin
2201 mpz_gcd(dest(result)^, src(op1)^, src(op2)^);
2202 end;
2203
2204 function z_gcd_ui(var rop, op1: MPInteger; op2: valuint): valuint;
2205 begin
2206 result := mpz_gcd_ui(dest(rop)^, src(op1)^, op2)
2207 end;
2208
2209 procedure z_gcdext(var g, s, t, a, b: MPInteger);
2210 begin
2211 mpz_gcdext(dest(g)^, dest(s)^, dest(t)^, src(a)^, src(b)^);
2212 end;
2213
2214 function z_get_d(var op: MPInteger): double;
2215 begin
2216 result := mpz_get_d(src(op)^);
2217 end;
2218
2219 function z_get_d_2exp(out exp: valsint; var op: MPInteger): double;
2220 begin
2221 result := mpz_get_d_2exp(exp, src(op)^);
2222 end;
2223
2224 function z_get_si(op: MPInteger): valsint;
2225 begin
2226 result := mpz_get_si(src(op)^);
2227 end;
2228
2229 function z_get_str(base: longint; var op: MPInteger): string;
2230 var p: pchar;
2231 begin
2232 p := mpz_get_str(nil, base, src(op)^);
2233 result := p;
2234 freemem(p);
2235 end;
2236
2237 function z_get_str(str: pchar; base: longint; var op: MPInteger): pchar;
2238 begin
2239 result := mpz_get_str(str, base, src(op)^);
2240 end;
2241
2242 function z_get_ui(op: MPInteger): valuint;
2243 begin
2244 result := mpz_get_ui(src(op)^);
2245 end;
2246
2247 function z_getlimbn(var op: MPInteger; n: mp_size_t): mp_limb_t;
2248 begin
2249 result := mpz_getlimbn(src(op)^, n);
2250 end;
2251
2252 function z_hamdist(var op1, op2: MPInteger): valuint;
2253 begin
2254 result := mpz_hamdist(src(op1)^, src(op2)^);
2255 end;
2256
2257 procedure z_import(var rop: MPInteger; count: sizeuint; order: longint; size: sizeuint; endian: longint; nails: sizeuint; const op);
2258 begin
2259 mpz_import(dest(rop)^, count, order, size, endian, nails, op);
2260 end;
2261
2262 procedure z_init(out integer_: MPInteger);
2263 begin
2264 integer_ := TMPInteger.Create;
2265 mpz_init(integer_.ptr^);
2266 end;
2267
2268 procedure z_init2(out integer_: MPInteger; n: valuint);
2269 begin
2270 integer_ := TMPInteger.Create;
2271 mpz_init2(integer_.ptr^, n);
2272 end;
2273
2274 procedure z_init_set(out rop: MPInteger; var op: MPInteger);
2275 begin
2276 rop := TMPInteger.Create;
2277 mpz_init_set(rop.ptr^, src(op)^);
2278 end;
2279
2280 procedure z_init_set_d(out rop: MPInteger; op: double);
2281 begin
2282 rop := TMPInteger.Create;
2283 mpz_init_set_d(rop.ptr^, op);
2284 end;
2285
2286 procedure z_init_set_si(out rop: MPInteger; op: valsint);
2287 begin
2288 rop := TMPInteger.Create;
2289 mpz_init_set_si(rop.ptr^, op);
2290 end;
2291
2292 function z_init_set_str(out rop: MPInteger; str: string; base: longint): boolean;
2293 begin
2294 rop := TMPInteger.Create;
2295 result := mpz_set_str(rop.ptr^, pchar(str), base) = 0;
2296 end;
2297
2298 procedure z_init_set_ui(out rop: MPInteger; op: valuint);
2299 begin
2300 rop := TMPInteger.Create;
2301 mpz_init_set_ui(rop.ptr^, op);
2302 end;
2303
2304 function z_invert(var rop, op1, op2: MPInteger): longint;
2305 begin
2306 result := mpz_invert(dest(rop)^, src(op1)^, src(op2)^);
2307 end;
2308
2309 procedure z_ior(var rop, op1, op2: MPInteger);
2310 begin
2311 mpz_ior(dest(rop)^, src(op1)^, src(op2)^);
2312 end;
2313
2314 function z_ior(var op1, op2: MPInteger): MPInteger;
2315 begin
2316 mpz_ior(dest(result)^, src(op1)^, src(op2)^);
2317 end;
2318
2319 function z_jacobi(var a, b: MPInteger): longint;
2320 begin
2321 result := mpz_jacobi(src(a)^, src(b)^);
2322 end;
2323
2324 function z_kronecker_si(var a: MPInteger; b: valsint): longint;
2325 begin
2326 result := mpz_kronecker_si(src(a)^, b);
2327 end;
2328
2329 function z_kronecker_ui(var a: MPInteger; b: valuint): longint;
2330 begin
2331 result := mpz_kronecker_ui(src(a)^, b);
2332 end;
2333
2334 function z_si_kronecker(a: valsint; var b: MPInteger): longint;
2335 begin
2336 result := mpz_si_kronecker(a, src(b)^);
2337 end;
2338
2339 function z_ui_kronecker(a: valuint; var b: MPInteger): longint;
2340 begin
2341 result := mpz_ui_kronecker(a, src(b)^);
2342 end;
2343
2344 procedure z_lcm(var rop, op1, op2: MPInteger);
2345 begin
2346 mpz_lcm(dest(rop)^, src(op1)^, src(op2)^);
2347 end;
2348
2349 function z_lcm(var op1, op2: MPInteger): MPInteger;
2350 begin
2351 mpz_lcm(dest(result)^, src(op1)^, src(op2)^);
2352 end;
2353
2354 procedure z_lcm_ui(var rop, op1: MPInteger; op2: valuint);
2355 begin
2356 mpz_lcm_ui(dest(rop)^, src(op1)^, op2);
2357 end;
2358
2359 function z_lcm_ui(var op1: MPInteger; op2: valuint): MPInteger;
2360 begin
2361 mpz_lcm_ui(dest(result)^, src(op1)^, op2);
2362 end;
2363
2364 procedure z_lucnum_ui(var ln: MPInteger; n: valuint);
2365 begin
2366 mpz_lucnum_ui(dest(ln)^, n);
2367 end;
2368
2369 function z_lucnum_ui(n: valuint): MPInteger;
2370 begin
2371 mpz_lucnum_ui(dest(result)^, n);
2372 end;
2373
2374 procedure z_lucnum2_ui(var ln, lnsub1: MPInteger; n: valuint);
2375 begin
2376 mpz_lucnum2_ui(dest(ln)^, dest(lnsub1)^, n);
2377 end;
2378
2379 function z_lucnum2_ui(var lnsub1: MPInteger; n: valuint): MPInteger;
2380 begin
2381 mpz_lucnum2_ui(dest(result)^, dest(lnsub1)^, n);
2382 end;
2383
2384 procedure z_mod(var r, n, d: MPInteger);
2385 begin
2386 mpz_mod(dest(r)^, src(n)^, src(d)^);
2387 end;
2388
2389 function z_mod(var n, d: MPInteger): MPInteger;
2390 begin
2391 mpz_mod(dest(result)^, src(n)^, src(d)^);
2392 end;
2393
2394 procedure z_mul(var rop, op1, op2: MPInteger);
2395 begin
2396 mpz_mul(dest(rop)^, src(op1)^, src(op2)^);
2397 end;
2398
2399 function z_mul(var op1, op2: MPInteger): MPInteger;
2400 begin
2401 mpz_mul(dest(result)^, src(op1)^, src(op2)^);
2402 end;
2403
2404 procedure z_mul_2exp(var rop, op1: MPInteger; op2: valuint);
2405 begin
2406 mpz_mul_2exp(dest(rop)^, src(op1)^, op2);
2407 end;
2408
2409 function z_mul_2exp(var op1: MPInteger; op2: valuint): MPInteger;
2410 begin
2411 mpz_mul_2exp(dest(result)^, src(op1)^, op2);
2412 end;
2413
2414 procedure z_mul_si(var rop, op1: MPInteger; op2: valsint);
2415 begin
2416 mpz_mul_si(dest(rop)^, src(op1)^, op2);
2417 end;
2418
2419 function z_mul_si(var op1: MPInteger; op2: valsint): MPInteger;
2420 begin
2421 mpz_mul_si(dest(result)^, src(op1)^, op2);
2422 end;
2423
2424 procedure z_mul_ui(var rop, op1: MPInteger; op2: valuint);
2425 begin
2426 mpz_mul_ui(dest(rop)^, src(op1)^, op2);
2427 end;
2428
2429 function z_mul_ui(var op1: MPInteger; op2: valuint): MPInteger;
2430 begin
2431 mpz_mul_ui(dest(result)^, src(op1)^, op2);
2432 end;
2433
2434 procedure z_neg(var rop, op: MPInteger);
2435 begin
2436 mpz_neg(dest(rop)^, src(op)^);
2437 end;
2438
2439 function z_neg(var op: MPInteger): MPInteger;
2440 begin
2441 mpz_neg(dest(result)^, src(op)^);
2442 end;
2443
2444 procedure z_nextprime(var rop, op: MPInteger);
2445 begin
2446 mpz_nextprime(dest(rop)^, src(op)^);
2447 end;
2448
2449 function z_nextprime(var op: MPInteger): MPInteger;
2450 begin
2451 mpz_nextprime(dest(result)^, src(op)^);
2452 end;
2453
2454 function z_perfect_power_p(var op: MPInteger): boolean;
2455 begin
2456 result := mpz_perfect_power_p(src(op)^) <> 0;
2457 end;
2458
2459 function z_perfect_square_p(var op: MPInteger): boolean;
2460 begin
2461 result := mpz_perfect_square_p(src(op)^) <> 0;
2462 end;
2463
2464 function z_popcount(var op: MPInteger): valuint;
2465 begin
2466 result := mpz_popcount(src(op)^);
2467 end;
2468
2469 procedure z_pow_ui(var rop, base: MPInteger; exp: valuint);
2470 begin
2471 mpz_pow_ui(dest(rop)^, src(base)^, exp);
2472 end;
2473
2474 function z_pow_ui(var base: MPInteger; exp: valuint): MPInteger;
2475 begin
2476 mpz_pow_ui(dest(result)^, src(base)^, exp);
2477 end;
2478
2479 procedure z_powm(var rop, base, exp, mod_: MPInteger);
2480 begin
2481 mpz_powm(dest(rop)^, src(base)^, src(exp)^, src(mod_)^);
2482 end;
2483
2484 function z_powm(var base, exp, mod_: MPInteger): MPInteger;
2485 begin
2486 mpz_powm(dest(result)^, src(base)^, src(exp)^, src(mod_)^);
2487 end;
2488
2489 procedure z_powm_ui(var rop, base: MPInteger; exp: valuint; var mod_: MPInteger);
2490 begin
2491 mpz_powm_ui(dest(rop)^, src(base)^, exp, src(mod_)^);
2492 end;
2493
2494 function z_powm_ui(var base: MPInteger; exp: valuint; var mod_: MPInteger): MPInteger;
2495 begin
2496 mpz_powm_ui(dest(result)^, src(base)^, exp, src(mod_)^);
2497 end;
2498
2499 function z_probab_prime_p(var n: MPInteger; reps: longint): longint;
2500 begin
2501 result := mpz_probab_prime_p(src(n)^, reps);
2502 end;
2503
2504 procedure z_realloc2(var integer_: MPInteger; n: valuint);
2505 begin
2506 mpz_realloc2(dest(integer_)^, n);
2507 end;
2508
2509 function z_remove(var rop, op, f: MPInteger): valuint;
2510 begin
2511 result := mpz_remove(dest(rop)^, src(op)^, src(f)^);
2512 end;
2513
2514 function z_root(var rop, op: MPInteger; n: valuint): boolean;
2515 begin
2516 result := mpz_root(dest(rop)^, src(op)^, n) <> 0;
2517 end;
2518
2519 procedure z_rootrem(var root, rem, u: MPInteger; n: valuint);
2520 begin
2521 mpz_rootrem(dest(root)^, dest(rem)^, src(u)^, n);
2522 end;
2523
2524 procedure z_rrandomb(var rop: MPInteger; var state: MPRandState; n: valuint);
2525 begin
2526 mpz_rrandomb(dest(rop)^, src(state)^, n);
2527 end;
2528
2529 function z_rrandomb(var state: MPRandState; n: valuint): MPInteger;
2530 begin
2531 mpz_rrandomb(dest(result)^, src(state)^, n);
2532 end;
2533
2534 function z_scan0(var op: MPInteger; starting_bit: valuint): valuint;
2535 begin
2536 result := mpz_scan0(src(op)^, starting_bit);
2537 end;
2538
2539 function z_scan1(var op: MPInteger; starting_bit: valuint): valuint;
2540 begin
2541 result := mpz_scan1(src(op)^, starting_bit);
2542 end;
2543
2544 procedure z_set(var rop, op: MPInteger);
2545 begin
2546 mpz_set(dest(rop)^, src(op)^);
2547 end;
2548
2549 procedure z_set_d(var rop: MPInteger; op: double);
2550 begin
2551 mpz_set_d(dest(rop)^, op);
2552 end;
2553
2554 procedure z_set_f(var rop: MPInteger; var op: MPFloat);
2555 begin
2556 mpz_set_f(dest(rop)^, src(op)^);
2557 end;
2558
2559 procedure z_set_q(var rop: MPInteger; var op: MPRational);
2560 begin
2561 mpz_set_q(dest(rop)^, src(op)^);
2562 end;
2563
2564 procedure z_set_si(var rop: MPInteger; op: valsint);
2565 begin
2566 mpz_set_si(dest(rop)^, op);
2567 end;
2568
2569 function z_set_str(var rop: MPInteger; str: string; base: longint): boolean;
2570 begin
2571 result := mpz_set_str(dest(rop)^, pchar(str), base) = 0;
2572 end;
2573
2574 procedure z_set_ui(var rop: MPInteger; op: valuint);
2575 begin
2576 mpz_set_ui(dest(rop)^, op);
2577 end;
2578
2579 procedure z_setbit(var rop: MPInteger; bit_index: valuint);
2580 begin
2581 mpz_setbit(dest(rop)^, bit_index);
2582 end;
2583
2584 function z_size(var op: MPInteger): sizeuint;
2585 begin
2586 result := mpz_size(src(op)^);
2587 end;
2588
2589 function z_sizeinbase(var op: MPInteger; base: longint): sizeuint;
2590 begin
2591 result := mpz_sizeinbase(src(op)^, base);
2592 end;
2593
2594 procedure z_sqrt(var rop, op: MPInteger);
2595 begin
2596 mpz_sqrt(dest(rop)^, src(op)^);
2597 end;
2598
2599 function z_sqrt(var op: MPInteger): MPInteger;
2600 begin
2601 mpz_sqrt(dest(result)^, src(op)^);
2602 end;
2603
2604 procedure z_sqrtrem(var rop1, rop2, op: MPInteger);
2605 begin
2606 mpz_sqrtrem(dest(rop1)^, dest(rop2)^, src(op)^);
2607 end;
2608
2609 procedure z_sub(var rop, op1, op2: MPInteger);
2610 begin
2611 mpz_sub(dest(rop)^, src(op1)^, src(op2)^);
2612 end;
2613
2614 function z_sub(var op1, op2: MPInteger): MPInteger;
2615 begin
2616 mpz_sub(dest(result)^, src(op1)^, src(op2)^);
2617 end;
2618
2619 procedure z_sub_ui(var rop, op1: MPInteger; op2: valuint);
2620 begin
2621 mpz_sub_ui(dest(rop)^, src(op1)^, op2);
2622 end;
2623
2624 function z_sub_ui(var op1: MPInteger; op2: valuint): MPInteger;
2625 begin
2626 mpz_sub_ui(dest(result)^, src(op1)^, op2);
2627 end;
2628
2629 procedure z_ui_sub(var rop: MPInteger; op1: valuint; var op2: MPInteger);
2630 begin
2631 mpz_ui_sub(dest(rop)^, op1, src(op2)^);
2632 end;
2633
2634 function z_ui_sub(op1: valuint; var op2: MPInteger): MPInteger;
2635 begin
2636 mpz_ui_sub(dest(result)^, op1, src(op2)^);
2637 end;
2638
2639 procedure z_submul(var rop, op1, op2: MPInteger);
2640 begin
2641 mpz_submul(dest(rop)^, src(op1)^, src(op2)^);
2642 end;
2643
2644 procedure z_submul_ui(var rop, op1: MPInteger; op2: valuint);
2645 begin
2646 mpz_submul_ui(dest(rop)^, src(op1)^, op2);
2647 end;
2648
2649 procedure z_swap(var rop1, rop2: MPInteger);
2650 begin
2651 mpz_swap(dest(rop1)^, dest(rop2)^);
2652 end;
2653
2654 procedure z_tdiv_q(var q, n, d: MPInteger);
2655 begin
2656 mpz_tdiv_q(dest(q)^, src(n)^, src(d)^);
2657 end;
2658
2659 function z_tdiv_q(var n, d: MPInteger): MPInteger;
2660 begin
2661 mpz_tdiv_q(dest(result)^, src(n)^, src(d)^);
2662 end;
2663
2664 procedure z_tdiv_q_2exp(var q, n: MPInteger; b: valuint);
2665 begin
2666 mpz_tdiv_q_2exp(dest(q)^, src(n)^, b);
2667 end;
2668
2669 function z_tdiv_q_2exp(var n: MPInteger; b: valuint): MPInteger;
2670 begin
2671 mpz_tdiv_q_2exp(dest(result)^, src(n)^, b);
2672 end;
2673
2674 function z_tdiv_q_ui(var q, n: MPInteger; d: valuint): valuint;
2675 begin
2676 result := mpz_tdiv_q_ui(dest(q)^, src(n)^, d);
2677 end;
2678
2679 procedure z_tdiv_qr(var q, r, n, d: MPInteger);
2680 begin
2681 mpz_tdiv_qr(dest(q)^, dest(r)^, src(n)^, src(d)^);
2682 end;
2683
2684 function z_tdiv_qr_ui(var q, r, n: MPInteger; d: valuint): valuint;
2685 begin
2686 result := mpz_tdiv_qr_ui(dest(q)^, dest(r)^, src(n)^, d);
2687 end;
2688
2689 procedure z_tdiv_r(var r, n, d: MPInteger);
2690 begin
2691 mpz_tdiv_r(dest(r)^, src(n)^, src(d)^);
2692 end;
2693
2694 function z_tdiv_r(var n, d: MPInteger): MPInteger;
2695 begin
2696 mpz_tdiv_r(dest(result)^, src(n)^, src(d)^);
2697 end;
2698
2699 procedure z_tdiv_r_2exp(var r, n: MPInteger; b: valuint);
2700 begin
2701 mpz_tdiv_r_2exp(dest(r)^, src(n)^, b);
2702 end;
2703
2704 function z_tdiv_r_2exp(var n: MPInteger; b: valuint): MPInteger;
2705 begin
2706 mpz_tdiv_r_2exp(dest(result)^, src(n)^, b);
2707 end;
2708
2709 function z_tdiv_r_ui(var r, n: MPInteger; d: valuint): valuint;
2710 begin
2711 result := mpz_tdiv_r_ui(dest(r)^, src(n)^, d);
2712 end;
2713
2714 function z_tdiv_ui(var n: MPInteger; d: valuint): valuint;
2715 begin
2716 result := mpz_tdiv_ui(src(n)^, d);
2717 end;
2718
2719 function z_tstbit(var rop: MPInteger; bit_index: valuint): boolean;
2720 begin
2721 result := mpz_tstbit(src(rop)^, bit_index) <> 0;
2722 end;
2723
2724 procedure z_ui_pow_ui(var rop: MPInteger; base, exp: valuint);
2725 begin
2726 mpz_ui_pow_ui(dest(rop)^, base, exp);
2727 end;
2728
2729 function z_ui_pow_ui(base, exp: valuint): MPInteger;
2730 begin
2731 mpz_ui_pow_ui(dest(result)^, base, exp);
2732 end;
2733
2734 procedure z_urandomb(var rop: MPInteger; var state: MPRandState; n: valuint);
2735 begin
2736 mpz_urandomb(dest(rop)^, src(state)^, n);
2737 end;
2738
2739 function z_urandomb(var state: MPRandState; n: valuint): MPInteger;
2740 begin
2741 mpz_urandomb(dest(result)^, src(state)^, n);
2742 end;
2743
2744 procedure z_urandomm(var rop: MPInteger; var state: MPRandState; var n: MPInteger);
2745 begin
2746 mpz_urandomm(dest(rop)^, src(state)^, src(n)^);
2747 end;
2748
2749 function z_urandomm(var state: MPRandState; var n: MPInteger): MPInteger;
2750 begin
2751 mpz_urandomm(dest(result)^, src(state)^, src(n)^);
2752 end;
2753
2754 procedure z_xor(var rop, op1, op2: MPInteger);
2755 begin
2756 mpz_xor(dest(rop)^, src(op1)^, src(op2)^);
2757 end;
2758
2759 function z_xor(var op1, op2: MPInteger): MPInteger;
2760 begin
2761 mpz_xor(dest(result)^, src(op1)^, src(op2)^);
2762 end;
2763
2764 procedure q_abs(var rop, op: MPRational);
2765 begin
2766 mpq_abs(dest(rop)^, src(op)^);
2767 end;
2768
2769 function q_abs(var op: MPRational): MPRational;
2770 begin
2771 mpq_abs(dest(result)^, src(op)^);
2772 end;
2773
2774 procedure q_add(var sum, addend1, addend2: MPRational);
2775 begin
2776 mpq_add(dest(sum)^, src(addend1)^, src(addend2)^);
2777 end;
2778
2779 function q_add(var addend1, addend2: MPRational): MPRational;
2780 begin
2781 mpq_add(dest(result)^, src(addend1)^, src(addend2)^);
2782 end;
2783
2784 procedure q_canonicalize(var op: MPRational);
2785 begin
2786 mpq_canonicalize(dest(op)^);
2787 end;
2788
2789 procedure q_clear(var rational_number: MPRational);
2790 begin
2791 rational_number := nil;
2792 end;
2793
2794 function q_cmp(var op1, op2: MPRational): longint;
2795 begin
2796 result := mpq_cmp(src(op1)^, src(op2)^);
2797 end;
2798
2799 function q_cmp_si(var op1: MPRational; num2: valsint; den2: valuint): longint;
2800 begin
2801 result := mpq_cmp_si(src(op1)^, num2, den2);
2802 end;
2803
2804 function q_cmp_ui(var op1: MPRational; num2, den2: valuint): longint;
2805 begin
2806 result := mpq_cmp_ui(src(op1)^, num2, den2);
2807 end;
2808
2809 procedure q_div(var quotient, dividend, divisor: MPRational);
2810 begin
2811 mpq_div(dest(quotient)^, src(dividend)^, src(divisor)^);
2812 end;
2813
2814 function q_div(var dividend, divisor: MPRational): MPRational;
2815 begin
2816 mpq_div(dest(result)^, src(dividend)^, src(divisor)^);
2817 end;
2818
2819 procedure q_div_2exp(var rop, op1: MPRational; op2: valuint);
2820 begin
2821 mpq_div_2exp(dest(rop)^, src(op1)^, op2);
2822 end;
2823
2824 function q_div_2exp(var op1: MPRational; op2: valuint): MPRational;
2825 begin
2826 mpq_div_2exp(dest(result)^, src(op1)^, op2);
2827 end;
2828
2829 function q_equal(var op1, op2: MPRational): boolean;
2830 begin
2831 result := mpq_equal(src(op1)^, src(op2)^) <> 0;
2832 end;
2833
2834 procedure q_get_num(var numerator: MPInteger; var rational: MPRational);
2835 begin
2836 mpq_get_num(dest(numerator)^, src(rational)^);
2837 end;
2838
2839 function q_get_num(var rational: MPRational): MPInteger;
2840 begin
2841 mpq_get_num(dest(result)^, src(rational)^);
2842 end;
2843
2844 procedure q_get_den(var denominator: MPInteger; var rational: MPRational);
2845 begin
2846 mpq_get_den(dest(denominator)^, src(rational)^);
2847 end;
2848
2849 function q_get_den(var rational: MPRational): MPInteger;
2850 begin
2851 mpq_get_den(dest(result)^, src(rational)^);
2852 end;
2853
2854 function q_get_d(var op: MPRational): double;
2855 begin
2856 result := mpq_get_d(src(op)^);
2857 end;
2858
2859 function q_get_str(base: longint; var op: MPRational): string;
2860 var p: pchar;
2861 begin
2862 p := mpq_get_str(nil, base, src(op)^);
2863 result := p;
2864 freemem(p);
2865 end;
2866
2867 function q_get_str(str: pchar; base: longint; var op: MPRational): pchar;
2868 begin
2869 result := mpq_get_str(str, base, src(op)^);
2870 end;
2871
2872 procedure q_init(out dest_rational: MPRational);
2873 begin
2874 dest_rational := TMPRational.Create;
2875 mpq_init(dest_rational.ptr^);
2876 end;
2877
2878 procedure q_inv(var inverted_number, number: MPRational);
2879 begin
2880 mpq_inv(dest(inverted_number)^, src(number)^);
2881 end;
2882
2883 function q_inv(var number: MPRational): MPRational;
2884 begin
2885 mpq_inv(dest(result)^, src(number)^);
2886 end;
2887
2888 procedure q_mul(var product, multiplier, multiplicand: MPRational);
2889 begin
2890 mpq_mul(dest(product)^, src(multiplier)^, src(multiplicand)^);
2891 end;
2892
2893 function q_mul(var multiplier, multiplicand: MPRational): MPRational;
2894 begin
2895 mpq_mul(dest(result)^, src(multiplier)^, src(multiplicand)^);
2896 end;
2897
2898 procedure q_mul_2exp(var rop, op1: MPRational; op2: valuint);
2899 begin
2900 mpq_mul_2exp(dest(rop)^, src(op1)^, op2);
2901 end;
2902
2903 function q_mul_2exp(var op1: MPRational; op2: valuint): MPRational;
2904 begin
2905 mpq_mul_2exp(dest(result)^, src(op1)^, op2);
2906 end;
2907
2908 procedure q_neg(var negated_operand, operand: MPRational);
2909 begin
2910 mpq_neg(dest(negated_operand)^, src(operand)^);
2911 end;
2912
2913 function q_neg(var operand: MPRational): MPRational;
2914 begin
2915 mpq_neg(dest(result)^, src(operand)^);
2916 end;
2917
2918 procedure q_set(var rop, op: MPRational);
2919 begin
2920 mpq_set(dest(rop)^, src(op)^);
2921 end;
2922
2923 procedure q_set_d(var rop: MPRational; op: double);
2924 begin
2925 mpq_set_d(dest(rop)^, op);
2926 end;
2927
2928 procedure q_set_den(var rational: MPRational; var denominator: MPInteger);
2929 begin
2930 mpq_set_den(dest(rational)^, src(denominator)^);
2931 end;
2932
2933 procedure q_set_f(var rop: MPRational; var op: MPFloat);
2934 begin
2935 mpq_set_f(dest(rop)^, src(op)^);
2936 end;
2937
2938 procedure q_set_num(var rational: MPRational; var numerator: MPInteger);
2939 begin
2940 mpq_set_num(dest(rational)^, src(numerator)^);
2941 end;
2942
2943 procedure q_set_si(var rop: MPRational; op1: valsint; op2: valuint);
2944 begin
2945 mpq_set_si(dest(rop)^, op1, op2);
2946 end;
2947
2948 function q_set_str(var rop: MPRational; str: string; base: longint): boolean;
2949 begin
2950 result := mpq_set_str(dest(rop)^, pchar(str), base) = 0;
2951 end;
2952
2953 procedure q_set_ui(var rop: MPRational; op1, op2: valuint);
2954 begin
2955 mpq_set_ui(dest(rop)^, op1, op2);
2956 end;
2957
2958 procedure q_set_z(var rop: MPRational; var op: MPInteger);
2959 begin
2960 mpq_set_z(dest(rop)^, src(op)^);
2961 end;
2962
2963 procedure q_sub(var difference, minuend, subtrahend: MPRational);
2964 begin
2965 mpq_sub(dest(difference)^, src(minuend)^, src(subtrahend)^);
2966 end;
2967
2968 function q_sub(var minuend, subtrahend: MPRational): MPRational;
2969 begin
2970 mpq_sub(dest(result)^, src(minuend)^, src(subtrahend)^);
2971 end;
2972
2973 procedure q_swap(var rop1, rop2: MPRational);
2974 begin
2975 mpq_swap(dest(rop1)^, dest(rop2)^);
2976 end;
2977
2978 procedure f_abs(var rop, op: MPFloat);
2979 begin
2980 mpf_abs(dest(rop)^, src(op)^);
2981 end;
2982
2983 function f_abs(var op: MPFloat): MPFloat;
2984 begin
2985 mpf_abs(dest(result)^, src(op)^);
2986 end;
2987
2988 procedure f_add(var rop, op1, op2: MPFloat);
2989 begin
2990 mpf_add(dest(rop)^, src(op1)^, src(op2)^);
2991 end;
2992
2993 function f_add(var op1, op2: MPFloat): MPFloat;
2994 begin
2995 mpf_add(dest(result)^, src(op1)^, src(op2)^);
2996 end;
2997
2998 procedure f_add_ui(var rop, op1: MPFloat; op2: valuint);
2999 begin
3000 mpf_add_ui(dest(rop)^, src(op1)^, op2);
3001 end;
3002
3003 function f_add_ui(var op1: MPFloat; op2: valuint): MPFloat;
3004 begin
3005 mpf_add_ui(dest(result)^, src(op1)^, op2);
3006 end;
3007
3008 procedure f_ceil(var rop, op: MPFloat);
3009 begin
3010 mpf_ceil(dest(rop)^, src(op)^);
3011 end;
3012
3013 function f_ceil(var op: MPFloat): MPFloat;
3014 begin
3015 mpf_ceil(dest(result)^, src(op)^);
3016 end;
3017
3018 procedure f_clear(var x: MPFloat);
3019 begin
3020 x := nil;
3021 end;
3022
3023 function f_cmp(var op1, op2: MPFloat): longint;
3024 begin
3025 result := mpf_cmp(src(op1)^, src(op2)^);
3026 end;
3027
3028 function f_cmp_d(var op1: MPFloat; op2: double): longint;
3029 begin
3030 result := mpf_cmp_d(src(op1)^, op2);
3031 end;
3032
3033 function f_cmp_si(var op1: MPFloat; op2: valsint): longint;
3034 begin
3035 result := mpf_cmp_si(src(op1)^, op2);
3036 end;
3037
3038 function f_cmp_ui(var op1: MPFloat; op2: valuint): longint;
3039 begin
3040 result := mpf_cmp_ui(src(op1)^, op2);
3041 end;
3042
3043 procedure f_div(var rop, op1, op2: MPFloat);
3044 begin
3045 mpf_div(dest(rop)^, src(op1)^, src(op2)^);
3046 end;
3047
3048 function f_div(var op1, op2: MPFloat): MPFloat;
3049 begin
3050 mpf_div(dest(result)^, src(op1)^, src(op2)^);
3051 end;
3052
3053 procedure f_div_2exp(var rop, op1: MPFloat; op2: valuint);
3054 begin
3055 mpf_div_2exp(dest(rop)^, src(op1)^, op2);
3056 end;
3057
3058 function f_div_2exp(var op1: MPFloat; op2: valuint): MPFloat;
3059 begin
3060 mpf_div_2exp(dest(result)^, src(op1)^, op2);
3061 end;
3062
3063 procedure f_div_ui(var rop, op1: MPFloat; op2: valuint);
3064 begin
3065 mpf_div_ui(dest(rop)^, src(op1)^, op2);
3066 end;
3067
3068 function f_div_ui(var op1: MPFloat; op2: valuint): MPFloat;
3069 begin
3070 mpf_div_ui(dest(result)^, src(op1)^, op2);
3071 end;
3072
3073 function f_eq(var op1, op2: MPFloat; op3: valuint): boolean;
3074 begin
3075 result := mpf_eq(src(op1)^, src(op2)^, op3) <> 0;
3076 end;
3077
3078 function f_fits_sint_p(var op: MPFloat): boolean;
3079 begin
3080 result := mpf_fits_sint_p(src(op)^) <> 0;
3081 end;
3082
3083 function f_fits_slong_p(var op: MPFloat): boolean;
3084 begin
3085 result := mpf_fits_slong_p(src(op)^) <> 0;
3086 end;
3087
3088 function f_fits_sshort_p(var op: MPFloat): boolean;
3089 begin
3090 result := mpf_fits_sshort_p(src(op)^) <> 0;
3091 end;
3092
3093 function f_fits_uint_p(var op: MPFloat): boolean;
3094 begin
3095 result := mpf_fits_uint_p(src(op)^) <> 0;
3096 end;
3097
3098 function f_fits_ulong_p(var op: MPFloat): boolean;
3099 begin
3100 result := mpf_fits_ulong_p(src(op)^) <> 0;
3101 end;
3102
3103 function f_fits_ushort_p(var op: MPFloat): boolean;
3104 begin
3105 result := mpf_fits_ushort_p(src(op)^) <> 0;
3106 end;
3107
3108 procedure f_floor(var rop, op: MPFloat);
3109 begin
3110 mpf_floor(dest(rop)^, src(op)^);
3111 end;
3112
3113 function f_floor(var op: MPFloat): MPFloat;
3114 begin
3115 mpf_floor(dest(result)^, src(op)^);
3116 end;
3117
3118 function f_get_d(var op: MPFloat): double;
3119 begin
3120 result := mpf_get_d(src(op)^);
3121 end;
3122
3123 function f_get_d_2exp(out exp: valsint; var op: MPFloat): double;
3124 begin
3125 result := mpf_get_d_2exp(exp, src(op)^);
3126 end;
3127
3128 function f_get_default_prec: valuint;
3129 begin
3130 result := mpf_get_default_prec;
3131 end;
3132
3133 function f_get_prec(var op: MPFloat): valuint;
3134 begin
3135 result := mpf_get_prec(src(op)^);
3136 end;
3137
3138 function f_get_si(var op: MPFloat): valsint;
3139 begin
3140 result := mpf_get_si(src(op)^);
3141 end;
3142
3143 function f_get_str(out exp: mp_exp_t; base: longint; ndigits: sizeuint; var op: MPFloat): string;
3144 var p: pchar;
3145 begin
3146 p := mpf_get_str(nil, exp, base, ndigits, src(op)^);
3147 result := p;
3148 freemem(p);
3149 end;
3150
3151 function f_get_str(str: pchar; out exp: mp_exp_t; base: longint; ndigits: sizeuint; var op: MPFloat): pchar;
3152 begin
3153 result := mpf_get_str(str, exp, base, ndigits, src(op)^);
3154 end;
3155
3156 function f_get_ui(var op: MPFloat): valuint;
3157 begin
3158 result := mpf_get_ui(src(op)^);
3159 end;
3160
3161 procedure f_init(out x: MPFloat);
3162 begin
3163 x := TMPFloat.Create;
3164 mpf_init(x.ptr^);
3165 end;
3166
3167 procedure f_init2(out x: MPFloat; prec: valuint);
3168 begin
3169 x := TMPFloat.Create;
3170 mpf_init2(x.ptr^, prec);
3171 end;
3172
3173 procedure f_init_set(out rop: MPFloat; var op: MPFloat);
3174 begin
3175 rop := TMPFloat.Create;
3176 mpf_init_set(rop.ptr^, src(op)^);
3177 end;
3178
3179 procedure f_init_set_d(out rop: MPFloat; op: double);
3180 begin
3181 rop := TMPFloat.Create;
3182 mpf_init_set_d(rop.ptr^, op);
3183 end;
3184
3185 procedure f_init_set_si(out rop: MPFloat; op: valsint);
3186 begin
3187 rop := TMPFloat.Create;
3188 mpf_init_set_si(rop.ptr^, op);
3189 end;
3190
3191 function f_init_set_str(out rop: MPFloat; str: string; base: longint): boolean;
3192 begin
3193 rop := TMPFloat.Create;
3194 result := mpf_init_set_str(rop.ptr^, pchar(str), base) = 0;
3195 end;
3196
3197 procedure f_init_set_ui(out rop: MPFloat; op: valuint);
3198 begin
3199 rop := TMPFloat.Create;
3200 mpf_init_set_ui(rop.ptr^, op);
3201 end;
3202
3203 function f_integer_p(var op: MPFloat): boolean;
3204 begin
3205 result := mpf_integer_p(src(op)^) <> 0;
3206 end;
3207
3208 procedure f_mul(var rop, op1, op2: MPFloat);
3209 begin
3210 mpf_mul(dest(rop)^, src(op1)^, src(op2)^);
3211 end;
3212
3213 function f_mul(var op1, op2: MPFloat): MPFloat;
3214 begin
3215 mpf_mul(dest(result)^, src(op1)^, src(op2)^);
3216 end;
3217
3218 procedure f_mul_2exp(var rop, op1: MPFloat; op2: valuint);
3219 begin
3220 mpf_mul_2exp(dest(rop)^, src(op1)^, op2);
3221 end;
3222
3223 function f_mul_2exp(var op1: MPFloat; op2: valuint): MPFloat;
3224 begin
3225 mpf_mul_2exp(dest(result)^, src(op1)^, op2);
3226 end;
3227
3228 procedure f_mul_ui(var rop, op1: MPFloat; op2: valuint);
3229 begin
3230 mpf_mul_ui(dest(rop)^, src(op1)^, op2);
3231 end;
3232
3233 function f_mul_ui(var op1: MPFloat; op2: valuint): MPFloat;
3234 begin
3235 mpf_mul_ui(dest(result)^, src(op1)^, op2);
3236 end;
3237
3238 procedure f_neg(var rop, op: MPFloat);
3239 begin
3240 mpf_neg(dest(rop)^, src(op)^);
3241 end;
3242
3243 function f_neg(var op: MPFloat): MPFloat;
3244 begin
3245 mpf_neg(dest(result)^, src(op)^);
3246 end;
3247
3248 procedure f_pow_ui(var rop, op1: MPFloat; op2: valuint);
3249 begin
3250 mpf_pow_ui(dest(rop)^, src(op1)^, op2);
3251 end;
3252
3253 function f_pow_ui(var op1: MPFloat; op2: valuint): MPFloat;
3254 begin
3255 mpf_pow_ui(dest(result)^, src(op1)^, op2);
3256 end;
3257
3258 procedure f_random2(var rop: MPFloat; max_size: mp_size_t; exp: mp_exp_t);
3259 begin
3260 mpf_random2(dest(rop)^, max_size, exp);
3261 end;
3262
3263 function f_random2(max_size: mp_size_t; exp: mp_exp_t): MPFloat;
3264 begin
3265 mpf_random2(dest(result)^, max_size, exp);
3266 end;
3267
3268 procedure f_reldiff(var rop, op1, op2: MPFloat);
3269 begin
3270 mpf_reldiff(dest(rop)^, src(op1)^, src(op2)^);
3271 end;
3272
3273 function f_reldiff(var op1, op2: MPFloat): MPFloat;
3274 begin
3275 mpf_reldiff(dest(result)^, src(op1)^, src(op2)^);
3276 end;
3277
3278 procedure f_set(var rop, op: MPFloat);
3279 begin
3280 mpf_set(dest(rop)^, src(op)^);
3281 end;
3282
3283 procedure f_set_d(var rop: MPFloat; op: double);
3284 begin
3285 mpf_set_d(dest(rop)^, op);
3286 end;
3287
3288 procedure f_set_default_prec(prec: valuint);
3289 begin
3290 mpf_set_default_prec(prec);
3291 end;
3292
3293 procedure f_set_prec(var rop: MPFloat; prec: valuint);
3294 begin
3295 mpf_set_prec(dest(rop)^, prec);
3296 end;
3297
3298 procedure f_set_prec_raw(var rop: MPFloat; prec: valuint);
3299 begin
3300 mpf_set_prec_raw(dest(rop)^, prec);
3301 end;
3302
3303 procedure f_set_q(var rop: MPFloat; var op: MPRational);
3304 begin
3305 mpf_set_q(dest(rop)^, src(op)^);
3306 end;
3307
3308 procedure f_set_si(var rop: MPFloat; op: valsint);
3309 begin
3310 mpf_set_si(dest(rop)^, op);
3311 end;
3312
3313 function f_set_str(var rop: MPFloat; str: string; base: longint): boolean;
3314 begin
3315 result := mpf_set_str(dest(rop)^, pchar(str), base) = 0;
3316 end;
3317
3318 procedure f_set_ui(var rop: MPFloat; op: valuint);
3319 begin
3320 mpf_set_ui(dest(rop)^, op);
3321 end;
3322
3323 procedure f_set_z(var rop: MPFloat; var op: MPInteger);
3324 begin
3325 mpf_set_z(dest(rop)^, src(op)^);
3326 end;
3327
3328 procedure f_sqrt(var rop, op: MPFloat);
3329 begin
3330 mpf_sqrt(dest(rop)^, src(op)^);
3331 end;
3332
3333 function f_sqrt(var op: MPFloat): MPFloat;
3334 begin
3335 mpf_sqrt(dest(result)^, src(op)^);
3336 end;
3337
3338 procedure f_sqrt_ui(var rop: MPFloat; op: valuint);
3339 begin
3340 mpf_sqrt_ui(dest(rop)^, op);
3341 end;
3342
3343 function f_sqrt_ui(op: valuint): MPFloat;
3344 begin
3345 mpf_sqrt_ui(dest(result)^, op);
3346 end;
3347
3348 procedure f_sub(var rop, op1, op2: MPFloat);
3349 begin
3350 mpf_sub(dest(rop)^, src(op1)^, src(op2)^);
3351 end;
3352
3353 function f_sub(var op1, op2: MPFloat): MPFloat;
3354 begin
3355 mpf_sub(dest(result)^, src(op1)^, src(op2)^);
3356 end;
3357
3358 procedure f_sub_ui(var rop, op1: MPFloat; op2: valuint);
3359 begin
3360 mpf_sub_ui(dest(rop)^, src(op1)^, op2);
3361 end;
3362
3363 function f_sub_ui(var op1: MPFloat; op2: valuint): MPFloat;
3364 begin
3365 mpf_sub_ui(dest(result)^, src(op1)^, op2);
3366 end;
3367
3368 procedure f_swap(var rop1, rop2: MPFloat);
3369 begin
3370 mpf_swap(dest(rop1)^, dest(rop2)^);
3371 end;
3372
3373 procedure f_trunc(var rop, op: MPFloat);
3374 begin
3375 mpf_trunc(dest(rop)^, src(op)^);
3376 end;
3377
3378 function f_trunc(var op: MPFloat): MPFloat;
3379 begin
3380 mpf_trunc(dest(result)^, src(op)^);
3381 end;
3382
3383 procedure f_ui_div(var rop: MPFloat; op1: valuint; var op2: MPFloat);
3384 begin
3385 mpf_ui_div(dest(rop)^, op1, src(op2)^);
3386 end;
3387
3388 function f_ui_div(op1: valuint; var op2: MPFloat): MPFloat;
3389 begin
3390 mpf_ui_div(dest(result)^, op1, src(op2)^);
3391 end;
3392
3393 procedure f_ui_sub(var rop: MPFloat; op1: valuint; var op2: MPFloat);
3394 begin
3395 mpf_ui_sub(dest(rop)^, op1, src(op2)^);
3396 end;
3397
3398 function f_ui_sub(op1: valuint; var op2: MPFloat): MPFloat;
3399 begin
3400 mpf_ui_sub(dest(result)^, op1, src(op2)^);
3401 end;
3402
3403 procedure f_urandomb(var rop: MPFloat; var state: MPRandState; nbits: valuint);
3404 begin
3405 mpf_urandomb(dest(rop)^, src(state)^, nbits);
3406 end;
3407
3408 function f_urandomb(var state: MPRandState; nbits: valuint): MPFloat;
3409 begin
3410 mpf_urandomb(dest(result)^, src(state)^, nbits);
3411 end;
3412
3413 // ---- operators ----
3414
3415 operator * (op1: MPFloat; op2: MPFloat): MPFloat;
3416 begin
3417 propagate_prec(result, op1, op2);
3418 f_mul(result, op1, op2);
3419 end;
3420
3421 operator * (op1: MPInteger; op2: MPInteger): MPInteger;
3422 begin
3423 z_mul(result, op1, op2);
3424 end;
3425
3426 operator * (op1: MPRational; op2: MPRational): MPRational;
3427 begin
3428 q_mul(result, op1, op2);
3429 end;
3430
3431 operator ** (op1: MPFloat; op2: valuint): MPFloat;
3432 begin
3433 propagate_prec(result, op1);
3434 f_pow_ui(result, op1, op2);
3435 end;
3436
3437 operator ** (op1: MPInteger; op2: valuint): MPInteger;
3438 begin
3439 z_pow_ui(result, op1, op2);
3440 end;
3441
3442 operator + (op1: MPFloat; op2: MPFloat): MPFloat;
3443 begin
3444 propagate_prec(result, op1, op2);
3445 f_add(result, op1, op2);
3446 end;
3447
3448 operator + (op1: MPInteger; op2: MPInteger): MPInteger;
3449 begin
3450 z_add(result, op1, op2);
3451 end;
3452
3453 operator + (op1: MPRational; op2: MPRational): MPRational;
3454 begin
3455 q_add(result, op1, op2);
3456 end;
3457
3458 operator - (op: MPFloat): MPFloat;
3459 begin
3460 propagate_prec(result, op);
3461 f_neg(result, op);
3462 end;
3463
3464 operator - (op: MPInteger): MPInteger;
3465 begin
3466 z_neg(result, op);
3467 end;
3468
3469 operator - (op: MPRational): MPRational;
3470 begin
3471 q_neg(result, op);
3472 end;
3473
3474 operator - (op1: MPFloat; op2: MPFloat): MPFloat;
3475 begin
3476 propagate_prec(result, op1, op2);
3477 f_sub(result, op1, op2);
3478 end;
3479
3480 operator - (op1: MPInteger; op2: MPInteger): MPInteger;
3481 begin
3482 z_sub(result, op1, op2);
3483 end;
3484
3485 operator - (op1: MPRational; op2: MPRational): MPRational;
3486 begin
3487 q_sub(result, op1, op2);
3488 end;
3489
3490 operator / (op1: MPFloat; op2: MPFloat): MPFloat;
3491 begin
3492 propagate_prec(result, op1, op2);
3493 f_div(result, op1, op2);
3494 end;
3495
3496 operator / (op1: MPInteger; op2: MPInteger): MPInteger;
3497 begin
3498 z_tdiv_q(result, op1, op2);
3499 end;
3500
3501 operator / (op1: MPRational; op2: MPRational): MPRational;
3502 begin
3503 q_div(result, op1, op2);
3504 end;
3505
3506 operator := (op: double): MPFloat;
3507 begin
3508 f_set_d(result, op);
3509 end;
3510
3511 operator := (op: double): MPInteger;
3512 begin
3513 z_set_d(result, op);
3514 end;
3515
3516 operator := (op: double): MPRational;
3517 begin
3518 q_set_d(result, op);
3519 end;
3520
3521 operator := (op: MPFloat): cardinal;
3522 begin
3523 result := f_get_ui(op);
3524 end;
3525
3526 operator := (op: MPFloat): double;
3527 begin
3528 result := f_get_d(op);
3529 end;
3530
3531 operator := (op: MPFloat): integer;
3532 begin
3533 result := f_get_si(op);
3534 end;
3535
3536 operator := (op: MPFloat): mpf_t;
3537 begin
3538 mpf_init_set(result, src(op)^);
3539 end;
3540
3541 operator := (op: MPFloat): MPInteger;
3542 begin
3543 z_set_f(result, op);
3544 end;
3545
3546 operator := (op: MPFloat): MPRational;
3547 begin
3548 q_set_f(result, op);
3549 end;
3550
3551 operator := (op: MPFloat): string;
3552 const FMT = '%.*Fg';
3553 var p: pchar;
3554 begin
3555 mp_asprintf(p, FMT, floor(f_get_prec(op) * LOG_10_2), src(op));
3556 result := p;
3557 freemem(p);
3558 end;
3559
3560 {$ifdef CPU64}
3561 operator := (op: MPFloat): valsint;
3562 begin
3563 result := f_get_si(op);
3564 end;
3565
3566 operator := (op: MPFloat): valuint;
3567 begin
3568 result := f_get_ui(op);
3569 end;
3570 {$endif}
3571
3572 operator := (var op: mpf_t): MPFloat;
3573 begin
3574 mpf_set(dest(result)^, op);
3575 end;
3576
3577 operator := (op: MPInteger): cardinal;
3578 begin
3579 result := z_get_ui(op);
3580 end;
3581
3582 operator := (op: MPInteger): double;
3583 begin
3584 result := z_get_d(op);
3585 end;
3586
3587 operator := (op: MPInteger): integer;
3588 begin
3589 result := z_get_si(op);
3590 end;
3591
3592 operator := (op: MPInteger): MPFloat;
3593 begin
3594 f_set_z(result, op);
3595 end;
3596
3597 operator := (op: MPInteger): MPRational;
3598 begin
3599 q_set_z(result, op);
3600 end;
3601
3602 operator := (op: MPInteger): mpz_t;
3603 begin
3604 mpz_init_set(result, src(op)^);
3605 end;
3606
3607 operator := (op: MPInteger): string;
3608 const FMT = '%Zd';
3609 var p: pchar;
3610 begin
3611 mp_asprintf(p, FMT, src(op));
3612 result := p;
3613 freemem(p);
3614 end;
3615
3616 {$ifdef CPU64}
3617 operator := (op: MPInteger): valsint;
3618 begin
3619 result := z_get_si(op);
3620 end;
3621
3622 operator := (op: MPInteger): valuint;
3623 begin
3624 result := z_get_ui(op);
3625 end;
3626 {$endif}
3627
3628 operator := (var op: mpq_t): MPRational;
3629 begin
3630 mpq_set(dest(result)^, op);
3631 end;
3632
3633 operator := (op: MPRandState): randstate_t;
3634 begin
3635 mp_randinit_set(result, src(op)^);
3636 end;
3637
3638 operator := (op: MPRational): double;
3639 begin
3640 result := q_get_d(op);
3641 end;
3642
3643 operator := (op: MPRational): MPFloat;
3644 begin
3645 f_set_q(result, op);
3646 end;
3647
3648 operator := (op: MPRational): MPInteger;
3649 begin
3650 z_set_q(result, op);
3651 end;
3652
3653 operator := (op: MPRational): mpq_t;
3654 begin
3655 mpq_init(result);
3656 mpq_set(result, src(op)^);
3657 end;
3658
3659 operator := (op: MPRational): string;
3660 const FMT = '%Qd';
3661 var p: pchar;
3662 begin
3663 mp_asprintf(p, FMT, src(op));
3664 result := p;
3665 freemem(p);
3666 end;
3667
3668 operator := (var op: mpz_t): MPInteger;
3669 begin
3670 mpz_set(dest(result)^, op);
3671 end;
3672
3673 operator := (var op: randstate_t): MPRandState;
3674 begin
3675 result := TMPRandState.Create;
3676 mp_randinit_set(result.ptr^, op);
3677 end;
3678
3679 operator := (op: string): MPFloat;
3680 begin
3681 f_set_prec(result, ceil(length(op) / LOG_10_2));
3682 f_set_str(result, op, BASE10);
3683 end;
3684
3685 operator := (op: string): MPInteger;
3686 begin
3687 z_set_str(result, op, BASE10);
3688 end;
3689
3690 operator := (op: string): MPRational;
3691 begin
3692 q_set_str(result, op, BASE10);
3693 end;
3694
3695 operator := (op: valsint): MPFloat;
3696 begin
3697 f_set_si(result, op);
3698 end;
3699
3700 operator := (op: valsint): MPInteger;
3701 begin
3702 z_set_si(result, op);
3703 end;
3704
3705 operator := (op: valsint): MPRational;
3706 begin
3707 q_set_si(result, op, 1);
3708 end;
3709
3710 operator := (op: valuint): MPFloat;
3711 begin
3712 f_set_ui(result, op);
3713 end;
3714
3715 operator := (op: valuint): MPInteger;
3716 begin
3717 z_set_ui(result, op);
3718 end;
3719
3720 operator := (op: valuint): MPRational;
3721 begin
3722 q_set_ui(result, op, 1);
3723 end;
3724
3725 operator < (op1: MPFloat; op2: MPFloat): boolean;
3726 begin
3727 result := f_cmp(op1, op2) < 0;
3728 end;
3729
3730 operator < (op1: MPInteger; op2: MPInteger): boolean;
3731 begin
3732 result := z_cmp(op1, op2) < 0;
3733 end;
3734
3735 operator < (op1: MPRational; op2: MPRational): boolean;
3736 begin
3737 result := q_cmp(op1, op2) < 0;
3738 end;
3739
3740 operator <= (op1: MPFloat; op2: MPFloat): boolean;
3741 begin
3742 result := f_cmp(op1, op2) <= 0;
3743 end;
3744
3745 operator <= (op1: MPInteger; op2: MPInteger): boolean;
3746 begin
3747 result := z_cmp(op1, op2) <= 0;
3748 end;
3749
3750 operator <= (op1: MPRational; op2: MPRational): boolean;
3751 begin
3752 result := q_cmp(op1, op2) <= 0;
3753 end;
3754
3755 //operator = (op1: MPFloat; op2: MPFloat): boolean;
3756 //begin
3757 // result := f_cmp(op1, op2) = 0;
3758 //end;
3759
3760 //operator = (op1: MPInteger; op2: MPInteger): boolean;
3761 //begin
3762 // result := z_cmp(op1, op2) = 0;
3763 //end;
3764
3765 //operator = (op1: MPRational; op2: MPRational): boolean;
3766 //begin
3767 // result := q_cmp(op1, op2) = 0;
3768 //end;
3769
3770 operator > (op1: MPFloat; op2: MPFloat): boolean;
3771 begin
3772 result := f_cmp(op1, op2) > 0;
3773 end;
3774
3775 operator > (op1: MPInteger; op2: MPInteger): boolean;
3776 begin
3777 result := z_cmp(op1, op2) > 0;
3778 end;
3779
3780 operator > (op1: MPRational; op2: MPRational): boolean;
3781 begin
3782 result := q_cmp(op1, op2) > 0;
3783 end;
3784
3785 operator >= (op1: MPFloat; op2: MPFloat): boolean;
3786 begin
3787 result := f_cmp(op1, op2) >= 0;
3788 end;
3789
3790 operator >= (op1: MPInteger; op2: MPInteger): boolean;
3791 begin
3792 result := z_cmp(op1, op2) >= 0;
3793 end;
3794
3795 operator >= (op1: MPRational; op2: MPRational): boolean;
3796 begin
3797 result := q_cmp(op1, op2) >= 0;
3798 end;
3799
3800 // ==== init stuff ====
3801
3802 function alloc_func(alloc_size: sizeuint): pointer; cdecl;
3803 begin
3804 result := getmem(alloc_size);
3805 end;
3806
3807 procedure free_proc(p: pointer; size: sizeuint); cdecl;
3808 begin
3809 assert(size = size); // hint off
3810 freemem(p);
3811 end;
3812
3813 function reallocate_func(p: pointer; old_size, new_size: sizeuint): pointer; cdecl;
3814 begin
3815 assert(old_size = old_size); // hint off
3816 result := reallocmem(p, new_size);
3817 end;
3818
3819 var r1: mp_limb_t;
3820
3821 initialization
3822 // prealloc the GMP's global PRNG state, get rid of the (pseudo) mem leak report
3823 mpn_random(@r1, 1);
3824 mp_set_memory_functions(@alloc_func, @reallocate_func, @free_proc);
3825
3826 end.
3827
3828