[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / runtime / gmp / gmp.texi
1 \input texinfo    @c -*-texinfo-*-
2 @comment %**start of header
3 @setfilename gmp.info
4 @settitle GNU MP 1.3.2
5 @synindex tp fn
6 @c footnotestyle separate
7 @c paragraphindent 2
8 @comment %**end of header
9
10 @c smallbook
11
12 @iftex
13 @finalout
14 @end iftex
15
16 @c Note: the edition number is listed in *three* places; please update
17 @c all three.  Also, update the month and year where appropriate.
18
19 @c ==> Update edition number for settitle and subtitle, and in the
20 @c ==> following paragraph; update date, too.
21
22 @ifinfo
23 This file documents GNU MP, a library for arbitrary-precision integer
24 and rational number arithmetic.
25
26 This is a draft edition of the documentation, last updated May 20 1993.
27
28 Copyright (C) 1991, 1993 Free Software Foundation, Inc.
29
30 Permission is granted to make and distribute verbatim copies of
31 this manual provided the copyright notice and this permission notice
32 are preserved on all copies.
33
34 @ignore
35 Permission is granted to process this file through TeX and print the
36 results, provided the printed document carries copying permission
37 notice identical to this one except for the removal of this paragraph
38 (this paragraph not being relevant to the printed manual).
39
40 @end ignore
41 Permission is granted to copy and distribute modified versions of this
42 manual under the conditions for verbatim copying, provided that the entire
43 resulting derived work is distributed under the terms of a permission
44 notice identical to this one.
45
46 Permission is granted to copy and distribute translations of this manual
47 into another language, under the above conditions for modified versions,
48 except that this permission notice may be stated in a translation approved
49 by the Foundation.
50 @end ifinfo
51
52 @setchapternewpage odd
53 @titlepage
54 @c  use the new format for titles
55
56 @title GNU MP
57 @subtitle The GNU Multiple Precision Arithmetic Library
58 @subtitle Edition 1.3.2
59 @subtitle May 1993
60
61 @author by Torbj@"orn Granlund
62
63 @comment   Include the Distribution inside the titlepage so
64 @c that headings are turned off.
65
66 @page
67 @vskip 0pt plus 1filll
68 Copyright @copyright{} 1991, 1993 Free Software Foundation, Inc.
69
70 @sp 2
71
72 Published by the Free Software Foundation @*
73 675 Massachusetts Avenue, @*
74 Cambridge, MA 02139 USA @*
75
76 Permission is granted to make and distribute verbatim copies of
77 this manual provided the copyright notice and this permission notice
78 are preserved on all copies.
79
80 Permission is granted to copy and distribute modified versions of this
81 manual under the conditions for verbatim copying, provided that the entire
82 resulting derived work is distributed under the terms of a permission
83 notice identical to this one.
84
85 Permission is granted to copy and distribute translations of this manual
86 into another language, under the above conditions for modified versions,
87 except that this permission notice may be stated in a translation approved
88 by the Foundation.
89 @end titlepage
90
91 @ifinfo
92 @node Top, Copying, (dir), (dir)
93 @end ifinfo
94
95 @menu
96 * Copying::                  GMP Copying Conditions.
97 * Intro::                    Introduction to GMP.
98 * Nomenclature::             Terminology and basic data types.
99 * Initialization::           Initialization of multi-precision number objects.
100 * Integer Functions::        Functions for arithmetic on signed integers.
101 * Rational Number Functions:: Functions for arithmetic on rational numbers.
102 * Low-level Functions::      Fast functions for natural numbers.
103 * BSD Compatible Functions:: All functions found in BSD MP (somewhat faster).
104 * Miscellaneous Functions::  Functions that do particular things.
105 * Custom Allocation::        How to customize the internal allocation.
106 * Reporting Bugs::           Help us to improve this library.
107
108 * References::
109 * Concept Index::
110 * Function Index::
111 @end menu
112
113 @node Copying, Intro, Top, Top
114 @comment  node-name, next, previous,  up
115 @unnumbered GNU MP Copying Conditions
116 @cindex Copying conditions
117 @cindex Conditions for copying GNU MP
118
119 This library is @dfn{free}; this means that everyone is free to use it
120 and free to redistribute it on a free basis.  The library is not in the
121 public domain; it is copyrighted and there are restrictions on its
122 distribution, but these restrictions are designed to permit everything
123 that a good cooperating citizen would want to do.  What is not allowed
124 is to try to prevent others from further sharing any version of this
125 library that they might get from you.@refill
126
127   Specifically, we want to make sure that you have the right to give
128 away copies of the library, that you receive source code or else can get
129 it if you want it, that you can change this library or use pieces of it
130 in new free programs, and that you know you can do these things.@refill
131
132   To make sure that everyone has such rights, we have to forbid you to
133 deprive anyone else of these rights.  For example, if you distribute
134 copies of the GMP library, you must give the recipients all the rights
135 that you have.  You must make sure that they, too, receive or can get
136 the source code.  And you must tell them their rights.@refill
137
138   Also, for our own protection, we must make certain that everyone finds
139 out that there is no warranty for the GMP library.  If it is modified by
140 someone else and passed on, we want their recipients to know that what
141 they have is not what we distributed, so that any problems introduced by
142 others will not reflect on our reputation.@refill
143
144   The precise conditions of the license for the GMP library are found in
145 the General Public License that accompany the source code.@refill
146
147 @node Intro, Initialization, Copying, Top
148 @comment  node-name,  next,  previous,  up
149 @chapter Introduction to MP
150 @cindex Introduction
151 @cindex Overview
152
153 GNU MP is a portable library for arbitrary precision integer and
154 rational number arithmetic.@footnote{The limit of the precision is set by the
155 available memory in your computer.}  It aims to provide the fastest
156 possible arithmetic for all applications that need more than two words
157 of integer precision.
158
159 Most often, applications tend to use just a few words of precision;
160 but some applications may need thousands of words.  GNU MP is designed
161 to give good performance for both kinds of applications, by choosing
162 algorithms based on the sizes of the operands.
163
164 There are five groups of functions in the MP library:
165
166 @enumerate
167 @item
168 Functions for signed integer arithmetic, with names
169 beginning with @code{mpz_}.
170
171 @item
172 Functions for rational number arithmetic, with names beginning with
173 @code{mpq_}.
174
175 @item
176 Functions compatible with Berkeley MP, such as @code{itom}, @code{madd},
177 and @code{mult}.
178
179 @item
180 Fast low-level functions that operate on natural numbers.  These are
181 used by the functions in the preceding groups, and you can also call
182 them directly from very time-critical user programs.  These functions'
183 names begin with @code{mpn_}.
184
185 @item
186 Miscellaneous functions.
187 @end enumerate
188
189 As a general rule, all MP functions expect output arguments before input
190 arguments.  This notation is based on an analogy with the assignment
191 operator.  (The BSD MP compatibility functions disobey this rule, having
192 the output argument(s) last.)  Multi-precision numbers, whether
193 output or input, are always passed as addresses to the declared type.
194
195 @menu
196 * Nomenclature::
197 * Thanks::
198 @end menu
199
200 @node Nomenclature, Thanks, Intro, Intro
201 @comment  node-name,  next,  previous,  up
202 @section Nomenclature and Data Types
203 @cindex nomenclature
204
205 @cindex integer
206 @tindex @code{MP_INT}
207 In this manual, @dfn{integer} means a multiple precision integer, as
208 used in the MP package.  The C data type for such integers is
209 @code{MP_INT}.  For example:
210
211 @example
212 MP_INT sum;
213
214 struct foo @{ MP_INT x, y; @};
215
216 MP_INT vec[20];
217 @end example
218
219 @cindex rational number
220 @tindex @code{MP_RAT}
221 @dfn{Rational number} means a multiple precision fraction.  The C data
222 type for these fractions is @code{MP_RAT}.  For example:
223
224 @example
225 MP_RAT quotient;
226 @end example
227
228 @cindex limb
229 A @dfn{limb} means the part of a multi-precision number that fits in a
230 single word.  (We chose this word because a limb of the human body is
231 analogous to a digit, only larger, and containing several digits.)
232 Normally a limb contains 32 bits.
233
234 @node Thanks,, Nomenclature, Intro
235 @comment  node-name,  next,  previous,  up
236 @section Thanks
237
238 I would like to thank Gunnar Sjoedin and Hans Riesel for their help with
239 mathematical problems, Richard Stallman for his help with design issues
240 and for revising this manual, Brian Beuning and Doug Lea for their
241 testing of various versions of the library, and Joachim Hollman for
242 his many valuable suggestions.
243
244 Special thanks to Brian Beuning, he has shaked out many bugs from early
245 versions of the code!
246
247 John Amanatides of York University in Canada contributed the function
248 @code{mpz_probab_prime_p}.
249
250 @node Initialization, Integer Functions, Intro, Top
251 @comment  node-name,  next,  previous,  up
252 @chapter Initialization
253
254 Before you can use a variable or object of type @code{MP_INT} or
255 @code{MP_RAT}, you must initialize it.  This fills in the components
256 that point to dynamically allocated space for the limbs of the number.
257
258 When you are finished using the object, you should clear out the object.
259 This frees the dynamic space that it points to, so the space can be used
260 again.
261
262 Once you have initialized the object, you need not be concerned about
263 allocating additional space.  The functions in the MP package
264 automatically allocate additional space when the object does not already
265 have enough space.  They do not, however, reduce the space in use when a
266 smaller number is stored in the object.  Most of the time, this policy
267 is best, since it avoids frequent re-allocation.  If you want to reduce
268 the space in an object to the minimum needed, you can do
269 @code{_mpz_realloc (&@var{object}, mpz_size (&@var{object}))}.
270
271 The functions to initialize numbers are @code{mpz_init} (for @code{MP_INT}) and
272 @code{mpq_init} (for @code{MP_RAT}).
273
274 @code{mpz_init} allocates space for the limbs, and stores a pointer
275 to that space in the @code{MP_INT} object.  It also stores the value 0
276 in the object.
277
278 In the same manner, @code{mpq_init} allocates space for the numerator
279 and denominator limbs, and stores pointers to these spaces in the @code{MP_RAT}
280 object.
281
282 To clear out a number object, use @code{mpz_clear} and @code{mpq_clear},
283 respectively.
284
285 Here is an example of use:
286
287 @example
288 @{
289   MP_INT temp;
290   mpz_init (&temp);
291
292   @dots{} @r{store and read values in @code{temp} zero or more times} @dots{}
293
294   mpz_clear (&temp):
295 @}
296 @end example
297
298 You might be tempted to copy an integer from one object to another like
299 this:
300
301 @example
302 MP_INT x, y;
303
304 x = y;
305 @end example
306
307 Although valid C, @strong{this is an error.} Rather than copying the
308 integer value from @code{y} to @code{x} it will make the two variables
309 share storage.  Subsequent assignments to one variable would change the
310 other mysteriously.  And if you were to clear out both variables
311 subsequently, you would confuse @code{malloc} and cause your program to
312 crash.
313
314 To copy the value properly, you must use the function @code{mpz_set}.
315 (@pxref{Assigning Integers})
316
317 @node Integer Functions, Rational Number Functions, Initialization, Top
318 @comment  node-name,  next,  previous,  up
319 @chapter Integer Functions
320 @cindex Integer functions
321
322 This chapter describes the MP functions for performing integer arithmetic.
323
324 The integer functions use arguments and values of type
325 pointer-to-@code{MP_INT} (@pxref{Nomenclature}).  The type @code{MP_INT}
326 is a structure, but applications should not refer directly to its
327 components.  Include the header @file{gmp.h} to get the definition of
328 @code{MP_INT}.
329
330 @menu
331 * Initializing Integers::
332 * Assigning Integers::
333 * Simultaneous Integer Init & Assign::
334 * Converting Integers::
335 * Integer Arithmetic::
336 * Logic on Integers::
337 * I/O of Integers::
338 @end menu
339
340 @node Initializing Integers, Assigning Integers, , Integer Functions
341 @comment  node-name,  next,  previous,  up
342 @section Initializing Integer Objects
343
344 Most of the functions for integer arithmetic assume that the output is
345 stored in an object already initialized.  For example, @code{mpz_add}
346 stores the result of addition (@pxref{Integer Arithmetic}).  Thus, you
347 must initialize the object before storing the first value in it.  You
348 can do this separately by calling the function @code{mpz_init}.
349
350 @deftypefun void mpz_init (MP_INT *@var{integer})
351 Initialize @var{integer} with limb space and set the initial numeric
352 value to 0.  Each variable should normally only be initialized once,
353 or at least cleared out (using @code{mpz_clear}) between each initialization.
354 @end deftypefun
355
356 Here is an example of using @code{mpz_init}:
357
358 @example
359 @{
360   MP_INT integ;
361   mpz_init (&integ);
362   @dots{}
363   mpz_add (&integ, @dots{});
364   @dots{}
365   mpz_sub (&integ, @dots{});
366
367   /* Unless you are now exiting the program, do ... */
368   mpz_clear (&integ);
369 @}
370 @end example
371
372 @noindent
373 As you can see, you can store new values any number of times, once an
374 object is initialized.
375
376 @deftypefun void mpz_clear (MP_INT *@var{integer})
377 Free the limb space occupied by @var{integer}.  Make sure to call this
378 function for all @code{MP_INT} variables when you are done with them.
379 @end deftypefun
380
381 @deftypefun {void *} _mpz_realloc (MP_INT *@var{integer}, mp_size @var{new_alloc})
382 Change the limb space allocation to @var{new_alloc} limbs.  This
383 function is not normally called from user code, but it can be used to
384 give memory back to the heap, or to increase the space of a variable to
385 avoid repeated automatic re-allocation.
386 @end deftypefun
387
388 @deftypefun void mpz_array_init (MP_INT @var{integer_array}[], size_t @var{array_size}, mp_size @var{fixed_num_limbs})
389 Allocate @strong{fixed} limb space for all @var{array_size} integers in
390 @var{integer_array}.  The fixed allocation for each integer in the array
391 is @var{fixed_num_limbs}.  This function is useful for decreasing the
392 working set for some algorithms that use large integer arrays.  If the
393 fixed space will be insufficient for storing the result of a subsequent
394 calculation, the result is unpredictable.
395
396 There is no way to de-allocate the storage allocated by this function.  Don't
397 call @code{mpz_clear}!
398 @end deftypefun
399
400
401 @node Assigning Integers, Simultaneous Integer Init & Assign, Initializing Integers, Integer Functions
402 @comment  node-name,  next,  previous,  up
403 @subsection Integer Assignment Functions
404 @cindex Integer assignment functions
405
406 These functions assign new values to already initialized integers
407 (@pxref{Initializing Integers}).
408
409 @deftypefun void mpz_set (MP_INT *@var{dest_integer}, MP_INT *@var{src_integer})
410 Assign @var{dest_integer} from @var{src_integer}.
411 @end deftypefun
412
413 @deftypefun void mpz_set_ui (MP_INT *@var{integer}, unsigned long int @var{initial_value})
414 Set the value of @var{integer} from @var{initial_value}.
415 @end deftypefun
416
417 @deftypefun void mpz_set_si (MP_INT *@var{integer}, signed long int @var{initial_value})
418 Set the value of @var{integer} from @var{initial_value}.
419 @end deftypefun
420
421 @deftypefun int mpz_set_str (MP_INT *@var{integer}, char *@var{initial_value}, int @var{base})
422 Set the value of @var{integer} from @var{initial_value},
423 a '\0'-terminated C string in base @var{base}.  White space is allowed in
424 the string, and is simply ignored.  The base may vary from 2 to 36.  If
425 @var{base} is 0, the actual base is determined from the leading characters: if
426 the first two characters are `0x' or `0X', hexadecimal is assumed,
427 otherwise if the first character is `0', octal is assumed, otherwise
428 decimal is assumed.
429
430 This function returns 0 if the entire string up to the '\0' is a valid
431 number in base @var{base}.  Otherwise it returns @minus{}1.
432 @end deftypefun
433
434
435 @node Simultaneous Integer Init & Assign, Converting Integers, Assigning Integers, Integer Functions
436 @comment  node-name,  next,  previous,  up
437 @subsection Combined Initialization and Assignment Functions
438 @cindex Initialization and assignment functions, combined
439
440 For your convenience, MP provides a parallel series of
441 initialize-and-set arithmetic functions which initialize the output and
442 then store the value there.  These functions' names have the form
443 @code{mpz_init_set@dots{}}.
444
445 Here is an example of using one:
446
447 @example
448 @{
449   MP_INT integ;
450   mpz_init_set_str (&integ, "3141592653589793238462643383279502884", 10);
451   @dots{}
452   mpz_sub (&integ, @dots{});
453
454   mpz_clear (&integ);
455 @}
456 @end example
457
458 Once the integer has been initialized by any of the
459 @code{mpz_init_set@dots{}} functions, it can be used as the source or
460 destination operand for the ordinary integer functions.  Don't use an
461 initialize-and-set function on a variable already initialized!
462
463 @deftypefun void mpz_init_set (MP_INT *@var{dest_integer}, MP_INT *@var{src_integer})
464 Initialize @var{dest_integer} with limb space and set the initial numeric
465 value from @var{src_integer}.
466 @end deftypefun
467
468 @deftypefun void mpz_init_set_ui (MP_INT *@var{dest_integer}, unsigned long int @var{src_ulong})
469 Initialize @var{dest_integer} with limb space and set the initial numeric
470 value from @var{src_ulong}.
471 @end deftypefun
472
473 @deftypefun void mpz_init_set_si (MP_INT *@var{dest_integer}, signed long int @var{src_slong})
474 Initialize @var{dest_integer} with limb space and set the initial numeric
475 value from @var{src_slong}.
476 @end deftypefun
477
478 @deftypefun int mpz_init_set_str (MP_INT *@var{dest_integer}, char *@var{src_cstring}, int @var{base})
479 Initialize @var{dest_integer} with limb space and set the initial
480 numeric value from @var{src_cstring}, a '\0'-terminated C string in base
481 @var{base}.  The base may vary from 2 to 36.  There may be white space
482 in the string.
483
484 If the string is a correct base @var{base} number, the function returns
485 0; if an error occurs it returns @minus{}1.  @var{dest_integer} is
486 initialized even if an error occurs.  (I.e., you have to call mpz_clear
487 for it.)
488 @end deftypefun
489
490
491 @node Converting Integers,  Integer Arithmetic, Simultaneous Integer Init & Assign, Integer Functions
492 @comment  node-name,  next,  previous,  up
493 @section Conversion Functions
494 @cindex Conversion functions
495
496 @deftypefun {unsigned long int} mpz_get_ui (MP_INT *@var{src_integer})
497 Return the least significant limb from @var{src_integer}.  This
498 function together with @*
499 @code{mpz_div_2exp(@dots{}, @var{src_integer}, CHAR_BIT*sizeof(unsigned
500 long int))} can be used to extract the limbs of an integer efficiently.
501 @end deftypefun
502
503 @deftypefun {signed long int} mpz_get_si (MP_INT *@var{src_integer})
504 If @var{src_integer} fits into a @code{signed long int} return the value
505 of @var{src_integer}.  Otherwise return the least significant bits of
506 @var{src_integer}, with the same sign as @var{src_integer}.
507 @end deftypefun
508
509 @deftypefun {char *} mpz_get_str (char *@var{string}, int @var{base}, MP_INT *@var{integer})
510 Convert @var{integer} to a '\0'-terminated C string in @var{string},
511 using base @var{base}.  The base may vary from 2 to 36.  If @var{string}
512 is NULL, space for the string is allocated using the default allocation
513 function.
514
515 If @var{string} is not NULL, it should point to a block of storage
516 enough large for the result.  To find out the right amount of space to
517 provide for @var{string}, use @code{mpz_sizeinbase (@var{integer},
518 @var{base}) + 2}.  The "+ 2" is for a possible minus sign, and for the
519 terminating null character.  (@pxref{Miscellaneous Functions}).
520
521 This function returns a pointer to the result string.
522 @end deftypefun
523
524
525 @node     Integer Arithmetic, Logic on Integers, Converting Integers, Integer Functions
526 @comment  node-name,  next,  previous,  up
527 @section Integer Arithmetic Functions
528 @cindex Integer arithmetic functions
529 @cindex Arithmetic functions
530
531 @deftypefun void mpz_add (MP_INT *@var{sum}, MP_INT *@var{addend1}, MP_INT *@var{addend2})
532 @end deftypefun
533 @deftypefun void mpz_add_ui (MP_INT *@var{sum}, MP_INT *@var{addend1}, unsigned long int @var{addend2})
534 Set @var{sum} to @var{addend1} + @var{addend2}.
535 @end deftypefun
536
537 @deftypefun void mpz_sub (MP_INT *@var{difference}, MP_INT *@var{minuend}, MP_INT *@var{subtrahend})
538 @end deftypefun
539 @deftypefun void mpz_sub_ui (MP_INT *@var{difference}, MP_INT *@var{minuend}, unsigned long int @var{subtrahend})
540 Set @var{difference} to @var{minuend} @minus{} @var{subtrahend}.
541 @end deftypefun
542
543 @deftypefun void mpz_mul (MP_INT *@var{product}, MP_INT *@var{multiplicator}, MP_INT *@var{multiplicand})
544 @end deftypefun
545 @deftypefun void mpz_mul_ui (MP_INT *@var{product}, MP_INT *@var{multiplicator}, unsigned long int @var{multiplicand})
546 Set @var{product} to @var{multiplicator} times @var{multiplicand}.
547 @end deftypefun
548
549 Division is undefined if the divisor is zero, and passing a zero divisor
550 to the divide or modulo functions, as well passing a zero mod argument
551 to the powm functions, will make these functions intentionally divide by
552 zero.  This gives the user the possibility to handle arithmetic
553 exceptions in these functions in the same manner as other arithmetic
554 exceptions.
555
556 @deftypefun void mpz_div (MP_INT *@var{quotient}, MP_INT *@var{dividend}, MP_INT *@var{divisor})
557 @end deftypefun
558 @deftypefun void mpz_div_ui (MP_INT *@var{quotient}, MP_INT *@var{dividend}, unsigned long int @var{divisor})
559 Set @var{quotient} to @var{dividend} / @var{divisor}.  The quotient is
560 rounded towards 0.
561 @end deftypefun
562
563 @deftypefun void mpz_mod (MP_INT *@var{remainder}, MP_INT *@var{divdend}, MP_INT *@var{divisor})
564 @end deftypefun
565 @deftypefun void mpz_mod_ui (MP_INT *@var{remainder}, MP_INT *@var{divdend}, unsigned long int @var{divisor})
566 Divide @var{dividend} and @var{divisor} and put the remainder in
567 @var{remainder}.  The remainder has the same sign as the dividend, and
568 its absolute value is less than the absolute value of the divisor.
569 @end deftypefun
570
571 @deftypefun void mpz_divmod (MP_INT *@var{quotient}, MP_INT *@var{remainder}, MP_INT *@var{dividend}, MP_INT *@var{divisor})
572 @end deftypefun
573 @deftypefun void mpz_divmod_ui (MP_INT *@var{quotient}, MP_INT *@var{remainder}, MP_INT *@var{dividend}, unsigned long int @var{divisor})
574 Divide @var{dividend} and @var{divisor} and put the quotient in
575 @var{quotient} and the remainder in @var{remainder}.  The quotient is
576 rounded towards 0.  The remainder has the same sign as the dividend,
577 and its absolute value is less than the absolute value of the divisor.
578
579 If @var{quotient} and @var{remainder} are the same variable, the results
580 are not defined.
581 @end deftypefun
582
583 @deftypefun void mpz_mdiv (MP_INT *@var{quotient}, MP_INT *@var{dividend}, MP_INT *@var{divisor})
584 @end deftypefun
585 @deftypefun void mpz_mdiv_ui (MP_INT *@var{quotient}, MP_INT *@var{dividend}, unsigned long int @var{divisor})
586 Set @var{quotient} to @var{dividend} / @var{divisor}.  The quotient is
587 rounded towards @minus{}infinity.
588 @end deftypefun
589
590 @deftypefun void mpz_mmod (MP_INT *@var{remainder}, MP_INT *@var{divdend}, MP_INT *@var{divisor})
591 @end deftypefun
592 @deftypefun {unsigned long int} mpz_mmod_ui (MP_INT *@var{remainder}, MP_INT *@var{divdend}, unsigned long int @var{divisor})
593 Divide @var{dividend} and @var{divisor} and put the remainder in
594 @var{remainder}.  The remainder is always positive, and its value is
595 less than the value of the divisor.
596
597 For @code{mpz_mmod_ui} the remainder is returned, and if @var{remainder} is
598 not NULL, also stored there.
599 @end deftypefun
600
601 @deftypefun void mpz_mdivmod (MP_INT *@var{quotient}, MP_INT *@var{remainder}, MP_INT *@var{dividend}, MP_INT *@var{divisor})
602 @end deftypefun
603 @deftypefun {unsigned long int} mpz_mdivmod_ui (MP_INT *@var{quotient}, MP_INT *@var{remainder}, MP_INT *@var{dividend}, unsigned long int @var{divisor})
604 Divide @var{dividend} and @var{divisor} and put the quotient in
605 @var{quotient} and the remainder in @var{remainder}.  The quotient is
606 rounded towards @minus{}infinity.  The remainder is always positive, and its
607 value is less than the value of the divisor.
608
609 For @code{mpz_mdivmod_ui} the remainder is small enough to fit in an
610 @code{unsigned long int}, and is therefore returned.  If @var{remainder}
611 is not NULL, the remainder is also stored there.
612
613 If @var{quotient} and @var{remainder} are the same variable, the results
614 are not defined.
615 @end deftypefun
616
617 @deftypefun void mpz_sqrt (MP_INT *@var{root}, MP_INT *@var{operand})
618 Set @var{root} to the square root of @var{operand}.  The result is
619 rounded towards zero.
620 @end deftypefun
621
622 @deftypefun void mpz_sqrtrem (MP_INT *@var{root}, MP_INT *@var{remainder}, MP_INT *@var{operand})
623 Set @var{root} to the square root of @var{operand}, as with
624 @code{mpz_sqrt}.  Set @var{remainder} to
625 @ifinfo
626 @var{operand}@minus{}@var{root}*@var{root},
627 @end ifinfo
628 @iftex
629 @tex
630 $operand - root^2$,
631 @end tex
632 @end iftex
633 (i.e@. zero if @var{operand} is a perfect square).
634
635 If @var{root} and @var{remainder} are the same variable, the results are
636 not defined.
637 @end deftypefun
638
639 @deftypefun int mpz_perfect_square_p (MP_INT *@var{square})
640 Return non-zero if @var{square} is perfect, i.e@. if the square root of
641 @var{square} is integral.  Return zero otherwise.
642 @end deftypefun
643
644 @deftypefun int mpz_probab_prime_p (MP_INT *@var{n}, int @var{reps})
645 An implementation of the probabilistic primality test found in Knuth's
646 Seminumerical Algorithms book.  If the function
647 @code{mpz_probab_prime_p(@var{n}, @var{reps})} returns 0 then @var{n} is
648 not prime.  If it returns 1, then @var{n} is `probably' prime.  The
649 probability of a false positive is (1/4)**@var{reps}, where @var{reps}
650 is the number of internal passes of the probabilistic algorithm.  Knuth
651 indicates that 25 passes are reasonable.
652 @end deftypefun
653
654 @deftypefun void mpz_powm (MP_INT *@var{res}, MP_INT *@var{base}, MP_INT *@var{exp}, MP_INT *@var{mod})
655 @end deftypefun
656 @deftypefun void mpz_powm_ui (MP_INT *@var{res}, MP_INT *@var{base}, unsigned long int @var{exp}, MP_INT *@var{mod})
657 Set @var{res} to (@var{base} raised to @var{exp}) modulo @var{mod}.
658 If @var{exp} is negative, the result is undefined.
659 @end deftypefun
660
661 @deftypefun void mpz_pow_ui (MP_INT *@var{res}, MP_INT *@var{base}, unsigned long int @var{exp})
662 Set @var{res} to @var{base} raised to @var{exp}.
663 @end deftypefun
664
665 @deftypefun void mpz_fac_ui (MP_INT *@var{res}, unsigned long int @var{n})
666 Set @var{res} @var{n}!, the factorial of n.
667 @end deftypefun
668
669 @deftypefun void mpz_gcd (MP_INT *@var{res}, MP_INT *@var{operand1}, MP_INT *@var{operand2})
670 Set @var{res} to the greatest common divisor of @var{operand1} and
671 @var{operand2}.
672 @end deftypefun
673
674 @deftypefun void mpz_gcdext (MP_INT *@var{g}, MP_INT *@var{s}, MP_INT *@var{t}, MP_INT *@var{a}, MP_INT *@var{b})
675 Compute @var{g}, @var{s}, and @var{t}, such that @var{a}@var{s} +
676 @var{b}@var{t} = @var{g} = @code{gcd} (@var{a}, @var{b}).  If @var{t} is
677 NULL, that argument is not computed.
678 @end deftypefun
679
680 @deftypefun void mpz_neg (MP_INT *@var{negated_operand}, MP_INT *@var{operand})
681 Set @var{negated_operand} to @minus{}@var{operand}.
682 @end deftypefun
683
684 @deftypefun void mpz_abs (MP_INT *@var{positive_operand}, MP_INT *@var{signed_operand})
685 Set @var{positive_operand} to the absolute value of @var{signed_operand}.
686 @end deftypefun
687
688 @deftypefun int mpz_cmp (MP_INT *@var{operand1}, MP_INT *@var{operand2})
689 @end deftypefun
690 @deftypefun int mpz_cmp_ui (MP_INT *@var{operand1}, unsigned long int @var{operand2})
691 @end deftypefun
692 @deftypefun int mpz_cmp_si (MP_INT *@var{operand1}, signed long int @var{operand2})
693 Compare @var{operand1} and @var{operand2}.  Return a positive value if
694 @var{operand1} > @var{operand2}, zero if @var{operand1} = @var{operand2},
695 and a negative value if @var{operand1} < @var{operand2}.
696 @end deftypefun
697
698 @deftypefun void mpz_mul_2exp (MP_INT *@var{product}, MP_INT *@var{multiplicator}, unsigned long int @var{exponent_of_2})
699 Set @var{product} to @var{multiplicator} times 2 raised to
700 @var{exponent_of_2}.  This operation can also be defined as a left shift,
701 @var{exponent_of_2} steps.
702 @end deftypefun
703
704 @deftypefun void mpz_div_2exp (MP_INT *@var{quotient}, MP_INT *@var{dividend}, unsigned long int @var{exponent_of_2})
705 Set @var{quotient} to @var{dividend} divided by 2 raised to
706 @var{exponent_of_2}.  This operation can also be defined as a right
707 shift, @var{exponent_of_2} steps, but unlike the >> operator in
708 C, the result is rounded towards 0.
709 @end deftypefun
710
711 @deftypefun void mpz_mod_2exp (MP_INT *@var{remainder}, MP_INT *@var{dividend}, unsigned long int @var{exponent_of_2})
712 Set @var{remainder} to @var{dividend} mod (2 raised to
713 @var{exponent_of_2}).  The sign of @var{remainder} will have the same sign
714 as @var{dividend}.
715
716 This operation can also be defined as a masking of the
717 @var{exponent_of_2} least significant bits.
718 @end deftypefun
719
720 @node Logic on Integers, I/O of Integers, Integer Arithmetic, Integer Functions
721 @comment  node-name,  next,  previous,  up
722 @section Logical Functions
723 @cindex Logical functions
724
725 @deftypefun void mpz_and (MP_INT *@var{conjunction}, MP_INT *@var{operand1}, MP_INT *@var{operand2})
726 Set @var{conjunction} to @var{operand1} logical-and @var{operand2}.
727 @end deftypefun
728
729 @deftypefun void mpz_ior (MP_INT *@var{disjunction}, MP_INT *@var{operand1}, MP_INT *@var{operand2})
730 Set @var{disjunction} to @var{operand1} inclusive-or @var{operand2}.
731 @end deftypefun
732
733 @deftypefun void mpz_xor (MP_INT *@var{disjunction}, MP_INT *@var{operand1}, MP_INT *@var{operand2})
734 Set @var{disjunction} to @var{operand1} exclusive-or @var{operand2}.
735
736 This function is missing in the current release.
737 @end deftypefun
738
739 @deftypefun void mpz_com (MP_INT *@var{complemented_operand}, MP_INT *@var{operand})
740 Set @var{complemented_operand} to the one's complement of @var{operand}.
741 @end deftypefun
742
743 @node I/O of Integers,, Logic on Integers, Integer Functions
744 @comment  node-name,  next,  previous,  up
745 @section Input and Output Functions
746 @cindex Input and output functions
747 @cindex Output functions
748 @cindex I/O functions
749
750 Functions that perform input from a standard I/O stream, and functions for
751 output conversion.
752
753 @deftypefun void mpz_inp_raw (MP_INT *@var{integer}, FILE *@var{stream})
754 Input from standard I/O stream @var{stream} in the format written by
755 @code{mpz_out_raw}, and put the result in @var{integer}.
756 @end deftypefun
757
758 @deftypefun void mpz_inp_str (MP_INT *@var{integer}, FILE *@var{stream}, int @var{base})
759 Input a string in base @var{base} from standard I/O stream @var{stream},
760 and put the read integer in @var{integer}.  The base may vary from 2 to
761 36.  If @var{base} is 0, the actual base is determined from the leading
762 characters: if the first two characters are `0x' or `0X', hexadecimal is
763 assumed, otherwise if the first character is `0', octal is assumed,
764 otherwise decimal is assumed.
765 @end deftypefun
766
767
768 @deftypefun void mpz_out_raw (FILE *@var{stream}, MP_INT *@var{integer})
769 Output @var{integer} on standard I/O stream @var{stream}, in raw binary
770 format.  The integer is written in a portable format, with 4 bytes of
771 size information, and that many bytes of limbs.  Both the size and the
772 limbs are written in decreasing significance order.
773 @end deftypefun
774
775 @deftypefun void mpz_out_str (FILE *@var{stream}, int @var{base}, MP_INT *@var{integer})
776 Output @var{integer} on standard I/O stream @var{stream}, as a string of
777 digits in base @var{base}.  The base may vary from 2 to 36.
778 @end deftypefun
779
780
781 @node Rational Number Functions, Low-level Functions, Integer Functions, Top
782 @comment  node-name,  next,  previous,  up
783 @chapter Rational Number Functions
784 @cindex Rational number functions
785
786 All rational arithmetic functions canonicalize the result, so that the
787 denominator and the numerator have no common factors.  Zero has the
788 unique representation 0/1.
789
790 The set of functions is quite small.  Maybe it will be extended in a
791 future release.
792
793 @deftypefun void mpq_init (MP_RAT *@var{dest_rational})
794 Initialize @var{dest_rational} with limb space and set the initial
795 numeric value to 0/1.  Each variable should normally only be initialized
796 once, or at least cleared out (using the function @code{mpq_clear})
797 between each initialization.
798 @end deftypefun
799
800 @deftypefun void mpq_clear (MP_RAT *@var{rational_number})
801 Free the limb space occupied by @var{rational_number}.  Make sure to
802 call this function for all @code{MP_RAT} variables when you are done
803 with them.
804 @end deftypefun
805
806 @deftypefun void mpq_set (MP_RAT *@var{dest_rational}, MP_RAT *@var{src_rational})
807 Assign @var{dest_rational} from @var{src_rational}.
808 @end deftypefun
809
810 @deftypefun void mpq_set_ui (MP_RAT *@var{rational_number}, unsigned long int @var{numerator}, unsigned long int @var{denominator})
811 Set the value of @var{rational_number} to
812 @var{numerator}/@var{denominator}.  If @var{numerator} and
813 @var{denominator} have common factors, they are divided out before
814 @var{rational_number} is assigned.
815 @end deftypefun
816
817 @deftypefun void mpq_set_si (MP_RAT *@var{rational_number}, signed long int @var{numerator}, unsigned long int @var{denominator})
818 Like @code{mpq_set_ui}, but @var{numerator} is signed.
819 @end deftypefun
820
821 @deftypefun void mpq_add (MP_RAT *@var{sum}, MP_RAT *@var{addend1}, MP_RAT *@var{addend2})
822 Set @var{sum} to @var{addend1} + @var{addend2}.
823 @end deftypefun
824
825 @deftypefun void mpq_sub (MP_RAT *@var{difference}, MP_RAT *@var{minuend}, MP_RAT *@var{subtrahend})
826 Set @var{difference} to @var{minuend} @minus{} @var{subtrahend}.
827 @end deftypefun
828
829 @deftypefun void mpq_mul (MP_RAT *@var{product}, MP_RAT *@var{multiplicator}, MP_RAT *@var{multiplicand})
830 Set @var{product} to @var{multiplicator} * @var{multiplicand}
831 @end deftypefun
832
833 @deftypefun void mpq_div (MP_RAT *@var{quotient}, MP_RAT *@var{dividend}, MP_RAT *@var{divisor})
834 Set @var{quotient} to @var{dividend} / @var{divisor}.
835 @end deftypefun
836
837 @deftypefun void mpq_neg (MP_RAT *@var{negated_operand}, MP_RAT *@var{operand})
838 Set @var{negated_operand} to @minus{}@var{operand}.
839 @end deftypefun
840
841 @deftypefun int mpq_cmp (MP_RAT *@var{operand1}, MP_RAT *@var{operand2})
842 Compare @var{operand1} and @var{operand2}.  Return a positive value if
843 @var{operand1} > @var{operand2}, zero if @var{operand1} = @var{operand2},
844 and a negative value if @var{operand1} < @var{operand2}.
845 @end deftypefun
846
847 @deftypefun void mpq_inv (MP_RAT *@var{inverted_number}, MP_RAT *@var{number})
848 Invert @var{number} by swapping the numerator and denominator.  If the
849 new denominator becomes zero, this routine will divide by zero.
850 @end deftypefun
851
852 @deftypefun void mpq_set_num (MP_RAT *@var{rational_number}, MP_INT *@var{numerator})
853 Make @var{numerator} become the numerator of @var{rational_number} by
854 copying.
855 @end deftypefun
856
857 @deftypefun void mpq_set_den (MP_RAT *@var{rational_number}, MP_INT *@var{denominator})
858 Make @var{denominator} become the denominator of @var{rational_number}
859 by copying.  If @var{denominator} < 0 the denominator of
860 @var{rational_number} is set to the absolute value of @var{denominator},
861 and the sign of the numerator of @var{rational_number} is changed.
862 @end deftypefun
863
864 @deftypefun void mpq_get_num (MP_INT *@var{numerator}, MP_RAT *@var{rational_number})
865 Copy the numerator of @var{rational_number} to the integer
866 @var{numerator}, to prepare for integer operations on the numerator.
867 @end deftypefun
868
869 @deftypefun void mpq_get_den (MP_INT *@var{denominator}, MP_RAT *@var{rational_number})
870 Copy the denominator of @var{rational_number} to the integer
871 @var{denominator}, to prepare for integer operations on the denominator.
872 @end deftypefun
873
874
875 @node Low-level Functions, BSD Compatible Functions, Rational Number Functions, Top
876 @comment  node-name,  next,  previous,  up
877 @chapter Low-level Functions
878 @cindex Low-level functions
879
880 @c 1. Some of these function clobber input operands.
881 @c 
882
883 @strong{The next release of the GNU MP library (2.0) will include
884 changes to some mpn functions.  Programs that use these functions
885 according to the descriptions below will therefore not work with the
886 next release.}
887
888 The low-level function layer is designed to be as fast as possible,
889 @strong{not} to provide a coherent calling interface.  The different
890 functions have similar interfaces, but there are variations that might
891 be confusing.  These functions do as little as possible apart from the
892 real multiple precision computation, so that no time is spent on things
893 that not all callers need.
894
895 A source operand is specified by a pointer to the least significant limb
896 and a limb count.  A destination operand is specified by just a pointer.
897 It is the responsability of the caller to ensure that the destination
898 has enough space for storing the result.
899
900 With this way of specifying source operands, it is possible to perform
901 computations on subranges of an argument, and store the result into a
902 subrange of a destination.
903
904 All these functions require that the operands are normalized in the
905 sense that the most significant limb must be non-zero.  (A future release
906 of might drop this requirement.)
907
908 The low-level layer is the base for the implementation of the
909 @code{mpz_} and @code{mpq_} layers.
910
911 The code below adds the number beginning at @var{src1_ptr} and the
912 number beginning at @var{src2_ptr} and writes the sum at @var{dest_ptr}.
913 A constraint for @code{mpn_add} is that @var{src1_size} must not be
914 smaller that @var{src2_size}.
915
916 @example
917 mpn_add (dest_ptr, src1_ptr, src1_size, src2_ptr, src2_size)
918 @end example
919
920 In the description below, a source operand is identified by the pointer
921 to the least significant limb, and the limb count in braces.
922
923 @deftypefun mp_size mpn_add (mp_ptr @var{dest_ptr}, mp_srcptr @var{src1_ptr}, mp_size @var{src1_size}, mp_srcptr @var{src2_ptr}, mp_size @var{src2_size})
924 Add @{@var{src1_ptr}, @var{src1_size}@} and @{@var{src2_ptr},
925 @var{src2_size}@}, and write the @var{src1_size} least significant limbs
926 of the result to @var{dest_ptr}.  Carry-out, either 0 or 1, is returned.
927
928 This function requires that @var{src1_size} is greater than or equal to
929 @var{src2_size}.
930 @end deftypefun
931
932 @deftypefun mp_size mpn_sub (mp_ptr @var{dest_ptr}, mp_srcptr @var{src1_ptr}, mp_size @var{src1_size}, mp_srcptr @var{src2_ptr}, mp_size @var{src2_size})
933 Subtarct @{@var{src2_ptr}, @var{src2_size}@} from @{@var{src1_ptr},
934 @var{src1_size}@}, and write the result to @var{dest_ptr}.
935
936 Return 1 if the minuend < the subtrahend.  Otherwise, return the
937 negative difference between the number of words in the result and the
938 minuend.  I.e@. return 0 if the result has @var{src1_size} words, @minus{}1 if
939 it has @var{src1_size} @minus{} 1 words, etc.
940
941 This function requires that @var{src1_size} is greater than or equal to
942 @var{src2_size}.
943 @end deftypefun
944
945 @deftypefun mp_size mpn_mul (mp_ptr @var{dest_ptr}, mp_srcptr @var{src1_ptr}, mp_size @var{src1_size}, mp_srcptr @var{src2_ptr}, mp_size @var{src2_size})
946 Multiply @{@var{src1_ptr}, @var{src1_size}@} and @{@var{src2_ptr},
947 @var{src2_size}@}, and write the result to @var{dest_ptr}.  The exact
948 size of the result is returned.
949
950 The destination has to have space for @var{src1_size} + @var{src1_size}
951 limbs, even if the result might be one limb smaller.
952
953 This function requires that @var{src1_size} is greater than or equal to
954 @var{src2_size}.  The destination must be distinct from either input
955 operands.
956 @end deftypefun
957
958 @deftypefun mp_size mpn_div (mp_ptr @var{dest_ptr}, mp_ptr @var{src1_ptr}, mp_size @var{src1_size}, mp_srcptr @var{src2_ptr}, mp_size @var{src2_size})
959 Divide @{@var{src1_ptr}, @var{src1_size}@} by @{@var{src2_ptr},
960 @var{src2_size}@}, and write the quotient to @var{dest_ptr}, and the
961 remainder to @var{src1_ptr}.
962
963 Return 0 if the quotient size is at most (@var{src1_size} @minus{}
964 @var{src2_size}), and 1 if the quotient size is at most (@var{src1_size}
965 @minus{} @var{src2_size} + 1).  The caller has to check the most significant limb
966 to find out the exact size.
967
968 The most significant bit of the most significant limb of the divisor
969 has to be set.
970
971 This function requires that @var{src1_size} is greater than or equal to
972 @var{src2_size}.  The quotient, pointed to by @var{dest_ptr}, must be
973 distinct from either input operands.
974 @end deftypefun
975
976 @deftypefun mp_limb mpn_lshift (mp_ptr @var{dest_ptr}, mp_srcptr @var{src_ptr}, mp_size @var{src_size}, unsigned long int @var{count})
977 Shift @{@var{src_ptr}, @var{src_size}@} @var{count} bits to the left, and
978 write the @var{src_size} least significant limbs of the result to
979 @var{dest_ptr}.  @var{count} might be in the range 1 to n @minus{} 1, on an n-bit
980 machine. The limb shifted out is returned.
981
982 Overlapping of the destination space and the source space is allowed in this
983 function, provdied @var{dest_ptr} >= @var{src_ptr}.
984 @end deftypefun
985
986 @deftypefun mp_size mpn_rshift (mp_ptr @var{dest_ptr}, mp_srcptr @var{src_ptr}, mp_size @var{src_size}, unsigned long int @var{count})
987 Shift @{@var{src_ptr}, @var{src_size}@} @var{count} bits to the right, and
988 write the @var{src_size} least significant limbs of the result to
989 @var{dest_ptr}.  @var{count} might be in the range 1 to n @minus{} 1, on an n-bit
990 machine. The size of the result is returned.
991
992 Overlaping of the destination space and the source space is allowed in this
993 function, provdied @var{dest_ptr} <= @var{src_ptr}.
994 @end deftypefun
995
996 @deftypefun mp_size mpn_rshiftci (mp_ptr @var{dest_ptr}, mp_srcptr @var{src_ptr}, mp_size @var{src_size}, unsigned long int @var{count}, mp_limb @var{inlimb})
997 Like mpn_rshift, but use @var{inlimb} to feed the least significant end
998 of the destination.
999 @end deftypefun
1000
1001 @deftypefun int mpn_cmp (mp_srcptr @var{src1_ptr}, mp_srcptr @var{src2_ptr}, mp_size @var{size})
1002 Compare @{@var{src1_ptr}, @var{size}@} and @{@var{src2_ptr}, @var{size}@}
1003 and return a positive value if src1 > src2, 0 of they are equal,
1004 and a negative value if src1 < src2.
1005 @end deftypefun
1006
1007
1008 @node BSD Compatible Functions, Miscellaneous Functions, Low-level Functions, Top
1009 @comment  node-name,  next,  previous,  up
1010 @chapter Berkeley MP Compatible Functions
1011 @cindex BSD MP compatible functions
1012
1013 These functions are intended to be fully compatible with the Berkeley MP
1014 library which is available on many BSD derived U*ix systems.
1015
1016 The original Berkeley MP library has a usage restriction: you cannot use
1017 the same variable as both source and destination in a single function
1018 call.  The compatible functions in GNU MP do not share this
1019 restriction---inputs and outputs may overlap.
1020
1021 It is not recommended that new programs are written using these
1022 functions.  Apart from the incomplete set of functions, the interface
1023 for initializing @code{MINT} objects is more error prone, and the
1024 @code{pow} function collides with @code{pow} in @file{libm.a}.
1025
1026 Include the header @file{mp.h} to get the definition of the necessary
1027 types and functions.  If you are on a BSD derived system, make sure to
1028 include GNU @file{mp.h} if you are going to link the GNU @file{libmp.a}
1029 to you program.  This means that you probably need to give the -I<dir>
1030 option to the compiler, where <dir> is the directory where you have GNU
1031 @file{mp.h}.
1032
1033 @deftypefun {MINT *} itom (signed short int @var{initial_value})
1034 Allocate an integer consisting of a @code{MINT} object and dynamic limb
1035 space.  Initialize the integer to @var{initial_value}.  Return a pointer
1036 to the @code{MINT} object.
1037 @end deftypefun
1038
1039 @deftypefun {MINT *} xtom (char *@var{initial_value})
1040 Allocate an integer consisting of a @code{MINT} object and dynamic limb
1041 space.  Initialize the integer from @var{initial_value}, a hexadecimal,
1042 '\0'-terminate C string.  Return a pointer to the @code{MINT} object.
1043 @end deftypefun
1044
1045 @deftypefun void move (MINT *@var{src}, MINT *@var{dest})
1046 Set @var{dest} to @var{src} by copying.  Both variables must be
1047 previously initialized.
1048 @end deftypefun
1049
1050 @deftypefun void madd (MINT *@var{src_1}, MINT *@var{src_2}, MINT *@var{destination})
1051 Add @var{src_1} and @var{src_2} and put the sum in @var{destination}.
1052 @end deftypefun
1053
1054 @deftypefun void msub (MINT *@var{src_1}, MINT *@var{src_2}, MINT *@var{destination})
1055 Subtract @var{src_2} from @var{src_1} and put the difference in
1056 @var{destination}.
1057 @end deftypefun
1058
1059 @deftypefun void mult (MINT *@var{src_1}, MINT *@var{src_2}, MINT *@var{destination})
1060 Multiply @var{src_1} and @var{src_2} and put the product in
1061 @var{destination}.
1062 @end deftypefun
1063
1064 @deftypefun void mdiv (MINT *@var{dividend}, MINT *@var{divisor}, MINT *@var{quotient}, MINT *@var{remainder})
1065 @end deftypefun
1066 @deftypefun void sdiv (MINT *@var{dividend}, signed short int @var{divisor}, MINT *@var{quotient}, signed short int *@var{remainder})
1067 Set @var{quotient} to @var{dividend} / @var{divisor}, and
1068 @var{remainder} to @var{dividend} mod @var{divisor}.  The quotient is
1069 rounded towards zero; the remainder has the same sign as the dividend.
1070
1071 Some implementations of this function return a remainder whose sign is
1072 inverted if the divisor is negative.  Such a definition makes little
1073 sense from a mathematical point of view.  GNU MP might be considered
1074 incompatible with the traditional MP in this respect.
1075 @end deftypefun
1076
1077 @deftypefun void msqrt (MINT *@var{operand}, MINT *@var{root}, MINT *@var{remainder})
1078 Set @var{root} to the square root of @var{operand}, as with
1079 @code{mpz_sqrt}.  Set @var{remainder} to
1080 @ifinfo
1081 @var{operand}-@var{root}*@var{root},
1082 @end ifinfo
1083 @iftex
1084 @tex
1085 $operand - root^2$,
1086 @end tex
1087 @end iftex
1088 (i.e@. zero if @var{operand} is a perfect square).
1089 @end deftypefun
1090
1091 @deftypefun void pow (MINT *@var{base}, MINT *@var{exp}, MINT *@var{mod}, MINT *@var{dest})
1092 Set @var{dest} to (@var{base} raised to @var{exp}) modulo @var{mod}.
1093 @end deftypefun
1094
1095 @deftypefun void rpow (MINT *@var{base}, signed short int @var{exp}, MINT *@var{dest})
1096 Set @var{dest} to @var{base} raised to @var{exp}.
1097 @end deftypefun
1098
1099 @deftypefun void gcd (MINT *@var{operand1}, MINT *@var{operand2}, MINT *@var{res})
1100 Set @var{res} to the greatest common divisor of @var{operand1} and
1101 @var{operand2}.
1102 @end deftypefun
1103
1104 @deftypefun int mcmp (MINT *@var{operand1}, MINT *@var{operand2})
1105 Compare @var{operand1} and @var{operand2}.  Return a positive value if
1106 @var{operand1} > @var{operand2}, zero if @var{operand1} =
1107 @var{operand2}, and a negative value if @var{operand1} < @var{operand2}.
1108 @end deftypefun
1109
1110 @deftypefun void min (MINT *@var{dest})
1111 Input a decimal string from stdin, and put the read integer in
1112 @var{dest}.  SPC and TAB are allowed in the number string, and are
1113 ignored.
1114 @end deftypefun
1115
1116 @deftypefun void mout (MINT *@var{src})
1117 Output @var{src} to stdout, as a decimal string.  Also output a newline.
1118 @end deftypefun
1119
1120 @deftypefun {char *} mtox (MINT *@var{operand})
1121 Convert @var{operand} to a hexadecimal string, and return a pointer to
1122 the string.  The returned string is allocated using the default memory
1123 allocation function, @code{malloc} by default. (@xref{Initialization},
1124 for an explanation of the memory allocation in MP).
1125 @end deftypefun
1126
1127 @deftypefun void mfree (MINT *@var{operand})
1128 De-allocate, the space used by @var{operand}.  @strong{This function
1129 should only be passed a value returned by @code{itom} or @code{xtom}.}
1130 @end deftypefun
1131
1132 @node Miscellaneous Functions, Custom Allocation, BSD Compatible Functions, Top
1133 @comment  node-name,  next,  previous,  up
1134 @chapter Miscellaneous Functions
1135 @cindex Miscellaneous functions
1136
1137 @deftypefun void mpz_random (MP_INT *@var{random_integer}, mp_size @var{max_size})
1138 Generate a random integer of at most @var{max_size} limbs.  The generated
1139 random number doesn't satisfy any particular requirements of randomness.
1140 @end deftypefun
1141
1142 @deftypefun void mpz_random2 (MP_INT *@var{random_integer}, mp_size @var{max_size})
1143 Generate a random integer of at most @var{max_size} limbs, with long
1144 strings of zeros and ones in the binary representation.  Useful for
1145 testing functions and algorithms, since this kind of random numbers have
1146 proven to be more likely to trigger bugs.
1147 @end deftypefun
1148
1149 @deftypefun size_t mpz_size (MP_INT *@var{integer})
1150 Return the size of @var{integer} measured in number of limbs.  If
1151 @var{integer} is zero, the returned value will be zero, if @var{integer}
1152 has one limb, the returned value will be one, etc.
1153 (@xref{Nomenclature}, for an explanation of the concept @dfn{limb}.)
1154 @end deftypefun
1155
1156 @deftypefun size_t mpz_sizeinbase (MP_INT *@var{integer}, int @var{base})
1157 Return the size of @var{integer} measured in number of digits in base
1158 @var{base}.  The base may vary from 2 to 36.  The returned value will be
1159 exact or 1 too big.  If @var{base} is a power of 2, the returned value
1160 will always be exact.
1161
1162 This function is useful in order to allocate the right amount of space
1163 before converting @var{integer} to a string.  The right amount of
1164 allocation is normally two more than the value returned by
1165 @code{mpz_sizeinbase} (one extra for a minus sign and one for the
1166 terminating '\0').
1167 @end deftypefun
1168
1169 @node Custom Allocation, Reporting Bugs, Miscellaneous Functions, Top
1170 @comment  node-name,  next,  previous,  up
1171 @section Custom Allocation
1172
1173 By default, the initialization functions use @code{malloc},
1174 @code{realloc}, and @code{free} to do their work.  If @code{malloc} or
1175 @code{realloc} fails, the MP package terminates execution after a
1176 printing fatal error message on standard error.
1177
1178 In some applications, you may wish to allocate memory in other ways, or
1179 you may not want to have a fatal error when there is no more memory
1180 available.  To accomplish this, you can specify alternative functions
1181 for allocating and de-allocating memory.  Use
1182 @code{mp_set_memory_functions} to do this.
1183
1184 @findex mp_set_memory_functions
1185 @code{mp_set_memory_functions} has three arguments,
1186 @var{allocate_function}, @var{reallocate_function}, and
1187 @var{deallocate_function}, in that order.  If an argument is NULL,
1188 the corresponding default function is retained.
1189
1190 The functions you supply should fit the following declarations:
1191
1192 @table @code
1193 @item void * @var{allocate_function} (size_t @var{alloc_size})
1194 This function should return a pointer to newly allocated space with at
1195 least @var{alloc_size} storage units.
1196
1197 @item void * @var{reallocate_function} (void *@var{ptr}, size_t @var{old_size}, size_t @var{new_size})
1198 This function should return a pointer to newly allocated space of at
1199 least @var{new_size} storage units, after copying the first
1200 @var{old_size} storage units from @var{ptr}.  It should also de-allocate the
1201 space at @var{ptr}.
1202
1203 You can assume that the space at @var{ptr} was formely returned from
1204 @var{allocate_function} or @var{reallocate_function}, for a
1205 request for @var{old_size} storage units.
1206
1207 @item void @var{deallocate_function} (void *@var{ptr}, size_t @var{size})
1208 De-allocate the space pointed to by @var{ptr}.
1209
1210 You can assume that the space at @var{ptr} was formely returned from
1211 @var{allocate_function} or @var{reallocate_function}, for a
1212 request for @var{size} storage units.
1213 @end table
1214
1215 (A @dfn{storage unit} is the unit in which the @code{sizeof} operator
1216 returns the size of an object, normally an 8 bit byte.)
1217
1218 @strong{NOTE: call @code{mp_set_memory_functions} only before calling
1219 any other MP functions.} Otherwise, the user-defined allocation
1220 functions may be asked to re-allocate or de-allocate something
1221 previously allocated by the default allocation functions.
1222
1223 @node Reporting Bugs, , Custom Allocation, Top
1224 @comment  node-name,  next,  previous,  up
1225 @chapter Reporting Bugs
1226 @cindex Reporting bugs
1227
1228 If you think you have found a bug in the GNU MP library, please
1229 investigate it and report it.  We have made this library available to
1230 you, and it is not to ask too much from you, to ask you to report the
1231 bugs that you find.
1232
1233 Please make sure that the bug is really in the GNU MP library.
1234
1235 You have to send us a test case that makes it possible for us to
1236 reproduce the bug.
1237
1238 You also have to explain what is wrong; if you get a crash, or if the
1239 results printed are not good and in that case, in what way.
1240
1241 Make sure that the bug report includes all information you would
1242 need to fix this kind of bug for someone else.  Think twice.
1243
1244 If your bug report is good, we will do our best to help you to get a
1245 corrected version of the library; if the bug report is poor, we won't do
1246 anything about it (aside of chiding you to send better bug reports).
1247
1248 Send your bug report to: tege@@gnu.ai.mit.edu.
1249
1250 If you think something in this manual is unclear, or downright
1251 incorrect, or if the language needs to be improved, please send a note
1252 to the same address.
1253
1254
1255 @node References, , , Top
1256 @comment  node-name,  next,  previous,  up
1257 @unnumbered References
1258
1259 @itemize @bullet
1260
1261 @item
1262 Donald E@. Knuth, "The Art of Computer Programming", vol 2,
1263 "Seminumerical Algorithms", 2nd edition, Addison-Wesley, 1981.
1264
1265 @item
1266 John D@. Lipson, "Elements of Algebra and Algebraic Computing",
1267 The Benjamin Cummins Publishing Company Inc, 1981.
1268
1269 @item
1270 Richard M@. Stallman, "Using and Porting GCC", Free Software Foundation,
1271 1993.
1272
1273 @item
1274 Peter L@. Montgomery, "Modular Multiplication Without Trial Division",
1275 Mathematics of Computation, volume 44, number 170, April 1985.
1276
1277 @end itemize
1278
1279 @node Concept Index, , , Top
1280 @comment  node-name,  next,  previous,  up
1281 @unnumbered Concept Index
1282 @printindex cp
1283
1284 @node Function Index, , , Top
1285 @comment  node-name,  next,  previous,  up
1286 @unnumbered Function and Type Index
1287 @printindex fn
1288
1289
1290 @contents
1291 @bye