xref: /netbsd-src/external/lgpl3/gmp/dist/doc/gmp.info-1 (revision b83ebeba7f767758d2778bb0f9d7a76534253621)
1This is ../../gmp/doc/gmp.info, produced by makeinfo version 4.13 from
2../../gmp/doc/gmp.texi.
3
4This manual describes how to install and use the GNU multiple precision
5arithmetic library, version 5.1.3.
6
7   Copyright 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
82001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
92013 Free Software Foundation, Inc.
10
11   Permission is granted to copy, distribute and/or modify this
12document under the terms of the GNU Free Documentation License, Version
131.3 or any later version published by the Free Software Foundation;
14with no Invariant Sections, with the Front-Cover Texts being "A GNU
15Manual", and with the Back-Cover Texts being "You have freedom to copy
16and modify this GNU Manual, like GNU software".  A copy of the license
17is included in *note GNU Free Documentation License::.
18
19INFO-DIR-SECTION GNU libraries
20START-INFO-DIR-ENTRY
21* gmp: (gmp).                   GNU Multiple Precision Arithmetic Library.
22END-INFO-DIR-ENTRY
23
24
25File: gmp.info,  Node: Top,  Next: Copying,  Prev: (dir),  Up: (dir)
26
27GNU MP
28******
29
30   This manual describes how to install and use the GNU multiple
31precision arithmetic library, version 5.1.3.
32
33   Copyright 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
342001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
352013 Free Software Foundation, Inc.
36
37   Permission is granted to copy, distribute and/or modify this
38document under the terms of the GNU Free Documentation License, Version
391.3 or any later version published by the Free Software Foundation;
40with no Invariant Sections, with the Front-Cover Texts being "A GNU
41Manual", and with the Back-Cover Texts being "You have freedom to copy
42and modify this GNU Manual, like GNU software".  A copy of the license
43is included in *note GNU Free Documentation License::.
44
45
46* Menu:
47
48* Copying::                    GMP Copying Conditions (LGPL).
49* Introduction to GMP::        Brief introduction to GNU MP.
50* Installing GMP::             How to configure and compile the GMP library.
51* GMP Basics::                 What every GMP user should know.
52* Reporting Bugs::             How to usefully report bugs.
53* Integer Functions::          Functions for arithmetic on signed integers.
54* Rational Number Functions::  Functions for arithmetic on rational numbers.
55* Floating-point Functions::   Functions for arithmetic on floats.
56* Low-level Functions::        Fast functions for natural numbers.
57* Random Number Functions::    Functions for generating random numbers.
58* Formatted Output::           `printf' style output.
59* Formatted Input::            `scanf' style input.
60* C++ Class Interface::        Class wrappers around GMP types.
61* Custom Allocation::          How to customize the internal allocation.
62* Language Bindings::          Using GMP from other languages.
63* Algorithms::                 What happens behind the scenes.
64* Internals::                  How values are represented behind the scenes.
65
66* Contributors::               Who brings you this library?
67* References::                 Some useful papers and books to read.
68* GNU Free Documentation License::
69* Concept Index::
70* Function Index::
71
72
73File: gmp.info,  Node: Copying,  Next: Introduction to GMP,  Prev: Top,  Up: Top
74
75GNU MP Copying Conditions
76*************************
77
78This library is "free"; this means that everyone is free to use it and
79free to redistribute it on a free basis.  The library is not in the
80public domain; it is copyrighted and there are restrictions on its
81distribution, but these restrictions are designed to permit everything
82that a good cooperating citizen would want to do.  What is not allowed
83is to try to prevent others from further sharing any version of this
84library that they might get from you.
85
86   Specifically, we want to make sure that you have the right to give
87away copies of the library, that you receive source code or else can
88get it if you want it, that you can change this library or use pieces
89of it in new free programs, and that you know you can do these things.
90
91   To make sure that everyone has such rights, we have to forbid you to
92deprive anyone else of these rights.  For example, if you distribute
93copies of the GNU MP library, you must give the recipients all the
94rights that you have.  You must make sure that they, too, receive or
95can get the source code.  And you must tell them their rights.
96
97   Also, for our own protection, we must make certain that everyone
98finds out that there is no warranty for the GNU MP library.  If it is
99modified by someone else and passed on, we want their recipients to
100know that what they have is not what we distributed, so that any
101problems introduced by others will not reflect on our reputation.
102
103   The precise conditions of the license for the GNU MP library are
104found in the Lesser General Public License version 3 that accompanies
105the source code, see `COPYING.LIB'.  Certain demonstration programs are
106provided under the terms of the plain General Public License version 3,
107see `COPYING'.
108
109
110File: gmp.info,  Node: Introduction to GMP,  Next: Installing GMP,  Prev: Copying,  Up: Top
111
1121 Introduction to GNU MP
113************************
114
115GNU MP is a portable library written in C for arbitrary precision
116arithmetic on integers, rational numbers, and floating-point numbers.
117It aims to provide the fastest possible arithmetic for all applications
118that need higher precision than is directly supported by the basic C
119types.
120
121   Many applications use just a few hundred bits of precision; but some
122applications may need thousands or even millions of bits.  GMP is
123designed to give good performance for both, by choosing algorithms
124based on the sizes of the operands, and by carefully keeping the
125overhead at a minimum.
126
127   The speed of GMP is achieved by using fullwords as the basic
128arithmetic type, by using sophisticated algorithms, by including
129carefully optimized assembly code for the most common inner loops for
130many different CPUs, and by a general emphasis on speed (as opposed to
131simplicity or elegance).
132
133   There is assembly code for these CPUs: ARM, DEC Alpha 21064, 21164,
134and 21264, AMD 29000, AMD K6, K6-2, Athlon, and Athlon64, Hitachi
135SuperH and SH-2, HPPA 1.0, 1.1 and 2.0, Intel Pentium, Pentium
136Pro/II/III, Pentium 4, generic x86, Intel IA-64, i960, Motorola
137MC68000, MC68020, MC88100, and MC88110, Motorola/IBM PowerPC 32 and 64,
138National NS32000, IBM POWER, MIPS R3000, R4000, SPARCv7, SuperSPARC,
139generic SPARCv8, UltraSPARC, DEC VAX, and Zilog Z8000.  Some
140optimizations also for Cray vector systems, Clipper, IBM ROMP (RT), and
141Pyramid AP/XP.
142
143For up-to-date information on GMP, please see the GMP web pages at
144
145     `http://gmplib.org/'
146
147The latest version of the library is available at
148
149     `ftp://ftp.gnu.org/gnu/gmp/'
150
151   Many sites around the world mirror `ftp.gnu.org', please use a mirror
152near you, see `http://www.gnu.org/order/ftp.html' for a full list.
153
154   There are three public mailing lists of interest.  One for release
155announcements, one for general questions and discussions about usage of
156the GMP library and one for bug reports.  For more information, see
157
158     `http://gmplib.org/mailman/listinfo/'.
159
160   The proper place for bug reports is <gmp-bugs@gmplib.org>.  See
161*note Reporting Bugs:: for information about reporting bugs.
162
163
1641.1 How to use this Manual
165==========================
166
167Everyone should read *note GMP Basics::.  If you need to install the
168library yourself, then read *note Installing GMP::.  If you have a
169system with multiple ABIs, then read *note ABI and ISA::, for the
170compiler options that must be used on applications.
171
172   The rest of the manual can be used for later reference, although it
173is probably a good idea to glance through it.
174
175
176File: gmp.info,  Node: Installing GMP,  Next: GMP Basics,  Prev: Introduction to GMP,  Up: Top
177
1782 Installing GMP
179****************
180
181GMP has an autoconf/automake/libtool based configuration system.  On a
182Unix-like system a basic build can be done with
183
184     ./configure
185     make
186
187Some self-tests can be run with
188
189     make check
190
191And you can install (under `/usr/local' by default) with
192
193     make install
194
195   If you experience problems, please report them to
196<gmp-bugs@gmplib.org>.  See *note Reporting Bugs::, for information on
197what to include in useful bug reports.
198
199* Menu:
200
201* Build Options::
202* ABI and ISA::
203* Notes for Package Builds::
204* Notes for Particular Systems::
205* Known Build Problems::
206* Performance optimization::
207
208
209File: gmp.info,  Node: Build Options,  Next: ABI and ISA,  Prev: Installing GMP,  Up: Installing GMP
210
2112.1 Build Options
212=================
213
214All the usual autoconf configure options are available, run `./configure
215--help' for a summary.  The file `INSTALL.autoconf' has some generic
216installation information too.
217
218Tools
219     `configure' requires various Unix-like tools.  See *note Notes for
220     Particular Systems::, for some options on non-Unix systems.
221
222     It might be possible to build without the help of `configure',
223     certainly all the code is there, but unfortunately you'll be on
224     your own.
225
226Build Directory
227     To compile in a separate build directory, `cd' to that directory,
228     and prefix the configure command with the path to the GMP source
229     directory.  For example
230
231          cd /my/build/dir
232          /my/sources/gmp-5.1.3/configure
233
234     Not all `make' programs have the necessary features (`VPATH') to
235     support this.  In particular, SunOS and Slowaris `make' have bugs
236     that make them unable to build in a separate directory.  Use GNU
237     `make' instead.
238
239`--prefix' and `--exec-prefix'
240     The `--prefix' option can be used in the normal way to direct GMP
241     to install under a particular tree.  The default is `/usr/local'.
242
243     `--exec-prefix' can be used to direct architecture-dependent files
244     like `libgmp.a' to a different location.  This can be used to share
245     architecture-independent parts like the documentation, but
246     separate the dependent parts.  Note however that `gmp.h' and
247     `mp.h' are architecture-dependent since they encode certain
248     aspects of `libgmp', so it will be necessary to ensure both
249     `$prefix/include' and `$exec_prefix/include' are available to the
250     compiler.
251
252`--disable-shared', `--disable-static'
253     By default both shared and static libraries are built (where
254     possible), but one or other can be disabled.  Shared libraries
255     result in smaller executables and permit code sharing between
256     separate running processes, but on some CPUs are slightly slower,
257     having a small cost on each function call.
258
259Native Compilation, `--build=CPU-VENDOR-OS'
260     For normal native compilation, the system can be specified with
261     `--build'.  By default `./configure' uses the output from running
262     `./config.guess'.  On some systems `./config.guess' can determine
263     the exact CPU type, on others it will be necessary to give it
264     explicitly.  For example,
265
266          ./configure --build=ultrasparc-sun-solaris2.7
267
268     In all cases the `OS' part is important, since it controls how
269     libtool generates shared libraries.  Running `./config.guess' is
270     the simplest way to see what it should be, if you don't know
271     already.
272
273Cross Compilation, `--host=CPU-VENDOR-OS'
274     When cross-compiling, the system used for compiling is given by
275     `--build' and the system where the library will run is given by
276     `--host'.  For example when using a FreeBSD Athlon system to build
277     GNU/Linux m68k binaries,
278
279          ./configure --build=athlon-pc-freebsd3.5 --host=m68k-mac-linux-gnu
280
281     Compiler tools are sought first with the host system type as a
282     prefix.  For example `m68k-mac-linux-gnu-ranlib' is tried, then
283     plain `ranlib'.  This makes it possible for a set of
284     cross-compiling tools to co-exist with native tools.  The prefix
285     is the argument to `--host', and this can be an alias, such as
286     `m68k-linux'.  But note that tools don't have to be setup this
287     way, it's enough to just have a `PATH' with a suitable
288     cross-compiling `cc' etc.
289
290     Compiling for a different CPU in the same family as the build
291     system is a form of cross-compilation, though very possibly this
292     would merely be special options on a native compiler.  In any case
293     `./configure' avoids depending on being able to run code on the
294     build system, which is important when creating binaries for a
295     newer CPU since they very possibly won't run on the build system.
296
297     In all cases the compiler must be able to produce an executable
298     (of whatever format) from a standard C `main'.  Although only
299     object files will go to make up `libgmp', `./configure' uses
300     linking tests for various purposes, such as determining what
301     functions are available on the host system.
302
303     Currently a warning is given unless an explicit `--build' is used
304     when cross-compiling, because it may not be possible to correctly
305     guess the build system type if the `PATH' has only a
306     cross-compiling `cc'.
307
308     Note that the `--target' option is not appropriate for GMP.  It's
309     for use when building compiler tools, with `--host' being where
310     they will run, and `--target' what they'll produce code for.
311     Ordinary programs or libraries like GMP are only interested in the
312     `--host' part, being where they'll run.  (Some past versions of
313     GMP used `--target' incorrectly.)
314
315CPU types
316     In general, if you want a library that runs as fast as possible,
317     you should configure GMP for the exact CPU type your system uses.
318     However, this may mean the binaries won't run on older members of
319     the family, and might run slower on other members, older or newer.
320     The best idea is always to build GMP for the exact machine type
321     you intend to run it on.
322
323     The following CPUs have specific support.  See `configure.ac' for
324     details of what code and compiler options they select.
325
326        * Alpha: alpha, alphaev5, alphaev56, alphapca56, alphapca57,
327          alphaev6, alphaev67, alphaev68 alphaev7
328
329        * Cray: c90, j90, t90, sv1
330
331        * HPPA: hppa1.0, hppa1.1, hppa2.0, hppa2.0n, hppa2.0w, hppa64
332
333        * IA-64: ia64, itanium, itanium2
334
335        * MIPS: mips, mips3, mips64
336
337        * Motorola: m68k, m68000, m68010, m68020, m68030, m68040,
338          m68060, m68302, m68360, m88k, m88110
339
340        * POWER: power, power1, power2, power2sc
341
342        * PowerPC: powerpc, powerpc64, powerpc401, powerpc403,
343          powerpc405, powerpc505, powerpc601, powerpc602, powerpc603,
344          powerpc603e, powerpc604, powerpc604e, powerpc620, powerpc630,
345          powerpc740, powerpc7400, powerpc7450, powerpc750, powerpc801,
346          powerpc821, powerpc823, powerpc860, powerpc970
347
348        * SPARC: sparc, sparcv8, microsparc, supersparc, sparcv9,
349          ultrasparc, ultrasparc2, ultrasparc2i, ultrasparc3, sparc64
350
351        * x86 family: i386, i486, i586, pentium, pentiummmx, pentiumpro,
352          pentium2, pentium3, pentium4, k6, k62, k63, athlon, amd64,
353          viac3, viac32
354
355        * Other: a29k, arm, clipper, i960, ns32k, pyramid, sh, sh2, vax,
356          z8k
357
358     CPUs not listed will use generic C code.
359
360Generic C Build
361     If some of the assembly code causes problems, or if otherwise
362     desired, the generic C code can be selected with the configure
363     `--disable-assembly'.
364
365     Note that this will run quite slowly, but it should be portable
366     and should at least make it possible to get something running if
367     all else fails.
368
369Fat binary, `--enable-fat'
370     Using `--enable-fat' selects a "fat binary" build on x86, where
371     optimized low level subroutines are chosen at runtime according to
372     the CPU detected.  This means more code, but gives good
373     performance on all x86 chips.  (This option might become available
374     for more architectures in the future.)
375
376`ABI'
377     On some systems GMP supports multiple ABIs (application binary
378     interfaces), meaning data type sizes and calling conventions.  By
379     default GMP chooses the best ABI available, but a particular ABI
380     can be selected.  For example
381
382          ./configure --host=mips64-sgi-irix6 ABI=n32
383
384     See *note ABI and ISA::, for the available choices on relevant
385     CPUs, and what applications need to do.
386
387`CC', `CFLAGS'
388     By default the C compiler used is chosen from among some likely
389     candidates, with `gcc' normally preferred if it's present.  The
390     usual `CC=whatever' can be passed to `./configure' to choose
391     something different.
392
393     For various systems, default compiler flags are set based on the
394     CPU and compiler.  The usual `CFLAGS="-whatever"' can be passed to
395     `./configure' to use something different or to set good flags for
396     systems GMP doesn't otherwise know.
397
398     The `CC' and `CFLAGS' used are printed during `./configure', and
399     can be found in each generated `Makefile'.  This is the easiest way
400     to check the defaults when considering changing or adding
401     something.
402
403     Note that when `CC' and `CFLAGS' are specified on a system
404     supporting multiple ABIs it's important to give an explicit
405     `ABI=whatever', since GMP can't determine the ABI just from the
406     flags and won't be able to select the correct assembly code.
407
408     If just `CC' is selected then normal default `CFLAGS' for that
409     compiler will be used (if GMP recognises it).  For example
410     `CC=gcc' can be used to force the use of GCC, with default flags
411     (and default ABI).
412
413`CPPFLAGS'
414     Any flags like `-D' defines or `-I' includes required by the
415     preprocessor should be set in `CPPFLAGS' rather than `CFLAGS'.
416     Compiling is done with both `CPPFLAGS' and `CFLAGS', but
417     preprocessing uses just `CPPFLAGS'.  This distinction is because
418     most preprocessors won't accept all the flags the compiler does.
419     Preprocessing is done separately in some configure tests.
420
421`CC_FOR_BUILD'
422     Some build-time programs are compiled and run to generate
423     host-specific data tables.  `CC_FOR_BUILD' is the compiler used
424     for this.  It doesn't need to be in any particular ABI or mode, it
425     merely needs to generate executables that can run.  The default is
426     to try the selected `CC' and some likely candidates such as `cc'
427     and `gcc', looking for something that works.
428
429     No flags are used with `CC_FOR_BUILD' because a simple invocation
430     like `cc foo.c' should be enough.  If some particular options are
431     required they can be included as for instance `CC_FOR_BUILD="cc
432     -whatever"'.
433
434C++ Support, `--enable-cxx'
435     C++ support in GMP can be enabled with `--enable-cxx', in which
436     case a C++ compiler will be required.  As a convenience
437     `--enable-cxx=detect' can be used to enable C++ support only if a
438     compiler can be found.  The C++ support consists of a library
439     `libgmpxx.la' and header file `gmpxx.h' (*note Headers and
440     Libraries::).
441
442     A separate `libgmpxx.la' has been adopted rather than having C++
443     objects within `libgmp.la' in order to ensure dynamic linked C
444     programs aren't bloated by a dependency on the C++ standard
445     library, and to avoid any chance that the C++ compiler could be
446     required when linking plain C programs.
447
448     `libgmpxx.la' will use certain internals from `libgmp.la' and can
449     only be expected to work with `libgmp.la' from the same GMP
450     version.  Future changes to the relevant internals will be
451     accompanied by renaming, so a mismatch will cause unresolved
452     symbols rather than perhaps mysterious misbehaviour.
453
454     In general `libgmpxx.la' will be usable only with the C++ compiler
455     that built it, since name mangling and runtime support are usually
456     incompatible between different compilers.
457
458`CXX', `CXXFLAGS'
459     When C++ support is enabled, the C++ compiler and its flags can be
460     set with variables `CXX' and `CXXFLAGS' in the usual way.  The
461     default for `CXX' is the first compiler that works from a list of
462     likely candidates, with `g++' normally preferred when available.
463     The default for `CXXFLAGS' is to try `CFLAGS', `CFLAGS' without
464     `-g', then for `g++' either `-g -O2' or `-O2', or for other
465     compilers `-g' or nothing.  Trying `CFLAGS' this way is convenient
466     when using `gcc' and `g++' together, since the flags for `gcc' will
467     usually suit `g++'.
468
469     It's important that the C and C++ compilers match, meaning their
470     startup and runtime support routines are compatible and that they
471     generate code in the same ABI (if there's a choice of ABIs on the
472     system).  `./configure' isn't currently able to check these things
473     very well itself, so for that reason `--disable-cxx' is the
474     default, to avoid a build failure due to a compiler mismatch.
475     Perhaps this will change in the future.
476
477     Incidentally, it's normally not good enough to set `CXX' to the
478     same as `CC'.  Although `gcc' for instance recognises `foo.cc' as
479     C++ code, only `g++' will invoke the linker the right way when
480     building an executable or shared library from C++ object files.
481
482Temporary Memory, `--enable-alloca=<choice>'
483     GMP allocates temporary workspace using one of the following three
484     methods, which can be selected with for instance
485     `--enable-alloca=malloc-reentrant'.
486
487        * `alloca' - C library or compiler builtin.
488
489        * `malloc-reentrant' - the heap, in a re-entrant fashion.
490
491        * `malloc-notreentrant' - the heap, with global variables.
492
493     For convenience, the following choices are also available.
494     `--disable-alloca' is the same as `no'.
495
496        * `yes' - a synonym for `alloca'.
497
498        * `no' - a synonym for `malloc-reentrant'.
499
500        * `reentrant' - `alloca' if available, otherwise
501          `malloc-reentrant'.  This is the default.
502
503        * `notreentrant' - `alloca' if available, otherwise
504          `malloc-notreentrant'.
505
506     `alloca' is reentrant and fast, and is recommended.  It actually
507     allocates just small blocks on the stack; larger ones use
508     malloc-reentrant.
509
510     `malloc-reentrant' is, as the name suggests, reentrant and thread
511     safe, but `malloc-notreentrant' is faster and should be used if
512     reentrancy is not required.
513
514     The two malloc methods in fact use the memory allocation functions
515     selected by `mp_set_memory_functions', these being `malloc' and
516     friends by default.  *Note Custom Allocation::.
517
518     An additional choice `--enable-alloca=debug' is available, to help
519     when debugging memory related problems (*note Debugging::).
520
521FFT Multiplication, `--disable-fft'
522     By default multiplications are done using Karatsuba, 3-way Toom,
523     higher degree Toom, and Fermat FFT.  The FFT is only used on large
524     to very large operands and can be disabled to save code size if
525     desired.
526
527Assertion Checking, `--enable-assert'
528     This option enables some consistency checking within the library.
529     This can be of use while debugging, *note Debugging::.
530
531Execution Profiling, `--enable-profiling=prof/gprof/instrument'
532     Enable profiling support, in one of various styles, *note
533     Profiling::.
534
535`MPN_PATH'
536     Various assembly versions of each mpn subroutines are provided.
537     For a given CPU, a search is made though a path to choose a
538     version of each.  For example `sparcv8' has
539
540          MPN_PATH="sparc32/v8 sparc32 generic"
541
542     which means look first for v8 code, then plain sparc32 (which is
543     v7), and finally fall back on generic C.  Knowledgeable users with
544     special requirements can specify a different path.  Normally this
545     is completely unnecessary.
546
547Documentation
548     The source for the document you're now reading is `doc/gmp.texi',
549     in Texinfo format, see *note Texinfo: (texinfo)Top.
550
551     Info format `doc/gmp.info' is included in the distribution.  The
552     usual automake targets are available to make PostScript, DVI, PDF
553     and HTML (these will require various TeX and Texinfo tools).
554
555     DocBook and XML can be generated by the Texinfo `makeinfo' program
556     too, see *note Options for `makeinfo': (texinfo)makeinfo options.
557
558     Some supplementary notes can also be found in the `doc'
559     subdirectory.
560
561
562
563File: gmp.info,  Node: ABI and ISA,  Next: Notes for Package Builds,  Prev: Build Options,  Up: Installing GMP
564
5652.2 ABI and ISA
566===============
567
568ABI (Application Binary Interface) refers to the calling conventions
569between functions, meaning what registers are used and what sizes the
570various C data types are.  ISA (Instruction Set Architecture) refers to
571the instructions and registers a CPU has available.
572
573   Some 64-bit ISA CPUs have both a 64-bit ABI and a 32-bit ABI
574defined, the latter for compatibility with older CPUs in the family.
575GMP supports some CPUs like this in both ABIs.  In fact within GMP
576`ABI' means a combination of chip ABI, plus how GMP chooses to use it.
577For example in some 32-bit ABIs, GMP may support a limb as either a
57832-bit `long' or a 64-bit `long long'.
579
580   By default GMP chooses the best ABI available for a given system,
581and this generally gives significantly greater speed.  But an ABI can
582be chosen explicitly to make GMP compatible with other libraries, or
583particular application requirements.  For example,
584
585     ./configure ABI=32
586
587   In all cases it's vital that all object code used in a given program
588is compiled for the same ABI.
589
590   Usually a limb is implemented as a `long'.  When a `long long' limb
591is used this is encoded in the generated `gmp.h'.  This is convenient
592for applications, but it does mean that `gmp.h' will vary, and can't be
593just copied around.  `gmp.h' remains compiler independent though, since
594all compilers for a particular ABI will be expected to use the same
595limb type.
596
597   Currently no attempt is made to follow whatever conventions a system
598has for installing library or header files built for a particular ABI.
599This will probably only matter when installing multiple builds of GMP,
600and it might be as simple as configuring with a special `libdir', or it
601might require more than that.  Note that builds for different ABIs need
602to done separately, with a fresh `./configure' and `make' each.
603
604
605AMD64 (`x86_64')
606     On AMD64 systems supporting both 32-bit and 64-bit modes for
607     applications, the following ABI choices are available.
608
609    `ABI=64'
610          The 64-bit ABI uses 64-bit limbs and pointers and makes full
611          use of the chip architecture.  This is the default.
612          Applications will usually not need special compiler flags,
613          but for reference the option is
614
615               gcc  -m64
616
617    `ABI=32'
618          The 32-bit ABI is the usual i386 conventions.  This will be
619          slower, and is not recommended except for inter-operating
620          with other code not yet 64-bit capable.  Applications must be
621          compiled with
622
623               gcc  -m32
624
625          (In GCC 2.95 and earlier there's no `-m32' option, it's the
626          only mode.)
627
628
629HPPA 2.0 (`hppa2.0*', `hppa64')
630
631    `ABI=2.0w'
632          The 2.0w ABI uses 64-bit limbs and pointers and is available
633          on HP-UX 11 or up.  Applications must be compiled with
634
635               gcc [built for 2.0w]
636               cc  +DD64
637
638    `ABI=2.0n'
639          The 2.0n ABI means the 32-bit HPPA 1.0 ABI and all its normal
640          calling conventions, but with 64-bit instructions permitted
641          within functions.  GMP uses a 64-bit `long long' for a limb.
642          This ABI is available on hppa64 GNU/Linux and on HP-UX 10 or
643          higher.  Applications must be compiled with
644
645               gcc [built for 2.0n]
646               cc  +DA2.0 +e
647
648          Note that current versions of GCC (eg. 3.2) don't generate
649          64-bit instructions for `long long' operations and so may be
650          slower than for 2.0w.  (The GMP assembly code is the same
651          though.)
652
653    `ABI=1.0'
654          HPPA 2.0 CPUs can run all HPPA 1.0 and 1.1 code in the 32-bit
655          HPPA 1.0 ABI.  No special compiler options are needed for
656          applications.
657
658     All three ABIs are available for CPU types `hppa2.0w', `hppa2.0'
659     and `hppa64', but for CPU type `hppa2.0n' only 2.0n or 1.0 are
660     considered.
661
662     Note that GCC on HP-UX has no options to choose between 2.0n and
663     2.0w modes, unlike HP `cc'.  Instead it must be built for one or
664     the other ABI.  GMP will detect how it was built, and skip to the
665     corresponding `ABI'.
666
667
668IA-64 under HP-UX (`ia64*-*-hpux*', `itanium*-*-hpux*')
669     HP-UX supports two ABIs for IA-64.  GMP performance is the same in
670     both.
671
672    `ABI=32'
673          In the 32-bit ABI, pointers, `int's and `long's are 32 bits
674          and GMP uses a 64 bit `long long' for a limb.  Applications
675          can be compiled without any special flags since this ABI is
676          the default in both HP C and GCC, but for reference the flags
677          are
678
679               gcc  -milp32
680               cc   +DD32
681
682    `ABI=64'
683          In the 64-bit ABI, `long's and pointers are 64 bits and GMP
684          uses a `long' for a limb.  Applications must be compiled with
685
686               gcc  -mlp64
687               cc   +DD64
688
689     On other IA-64 systems, GNU/Linux for instance, `ABI=64' is the
690     only choice.
691
692
693MIPS under IRIX 6 (`mips*-*-irix[6789]')
694     IRIX 6 always has a 64-bit MIPS 3 or better CPU, and supports ABIs
695     o32, n32, and 64.  n32 or 64 are recommended, and GMP performance
696     will be the same in each.  The default is n32.
697
698    `ABI=o32'
699          The o32 ABI is 32-bit pointers and integers, and no 64-bit
700          operations.  GMP will be slower than in n32 or 64, this
701          option only exists to support old compilers, eg. GCC 2.7.2.
702          Applications can be compiled with no special flags on an old
703          compiler, or on a newer compiler with
704
705               gcc  -mabi=32
706               cc   -32
707
708    `ABI=n32'
709          The n32 ABI is 32-bit pointers and integers, but with a
710          64-bit limb using a `long long'.  Applications must be
711          compiled with
712
713               gcc  -mabi=n32
714               cc   -n32
715
716    `ABI=64'
717          The 64-bit ABI is 64-bit pointers and integers.  Applications
718          must be compiled with
719
720               gcc  -mabi=64
721               cc   -64
722
723     Note that MIPS GNU/Linux, as of kernel version 2.2, doesn't have
724     the necessary support for n32 or 64 and so only gets a 32-bit limb
725     and the MIPS 2 code.
726
727
728PowerPC 64 (`powerpc64', `powerpc620', `powerpc630', `powerpc970', `power4', `power5')
729
730    `ABI=mode64'
731          The AIX 64 ABI uses 64-bit limbs and pointers and is the
732          default on PowerPC 64 `*-*-aix*' systems.  Applications must
733          be compiled with
734
735               gcc  -maix64
736               xlc  -q64
737
738          On 64-bit GNU/Linux, BSD, and Mac OS X/Darwin systems, the
739          applications must be compiled with
740
741               gcc  -m64
742
743    `ABI=mode32'
744          The `mode32' ABI uses a 64-bit `long long' limb but with the
745          chip still in 32-bit mode and using 32-bit calling
746          conventions.  This is the default for systems where the true
747          64-bit ABI is unavailable.  No special compiler options are
748          typically needed for applications.  This ABI is not available
749          under AIX.
750
751    `ABI=32'
752          This is the basic 32-bit PowerPC ABI, with a 32-bit limb.  No
753          special compiler options are needed for applications.
754
755     GMP's speed is greatest for the `mode64' ABI, the `mode32' ABI is
756     2nd best.  In `ABI=32' only the 32-bit ISA is used and this
757     doesn't make full use of a 64-bit chip.
758
759
760Sparc V9 (`sparc64', `sparcv9', `ultrasparc*')
761
762    `ABI=64'
763          The 64-bit V9 ABI is available on the various BSD sparc64
764          ports, recent versions of Sparc64 GNU/Linux, and Solaris 2.7
765          and up (when the kernel is in 64-bit mode).  GCC 3.2 or
766          higher, or Sun `cc' is required.  On GNU/Linux, depending on
767          the default `gcc' mode, applications must be compiled with
768
769               gcc  -m64
770
771          On Solaris applications must be compiled with
772
773               gcc  -m64 -mptr64 -Wa,-xarch=v9 -mcpu=v9
774               cc   -xarch=v9
775
776          On the BSD sparc64 systems no special options are required,
777          since 64-bits is the only ABI available.
778
779    `ABI=32'
780          For the basic 32-bit ABI, GMP still uses as much of the V9
781          ISA as it can.  In the Sun documentation this combination is
782          known as "v8plus".  On GNU/Linux, depending on the default
783          `gcc' mode, applications may need to be compiled with
784
785               gcc  -m32
786
787          On Solaris, no special compiler options are required for
788          applications, though using something like the following is
789          recommended.  (`gcc' 2.8 and earlier only support `-mv8'
790          though.)
791
792               gcc  -mv8plus
793               cc   -xarch=v8plus
794
795     GMP speed is greatest in `ABI=64', so it's the default where
796     available.  The speed is partly because there are extra registers
797     available and partly because 64-bits is considered the more
798     important case and has therefore had better code written for it.
799
800     Don't be confused by the names of the `-m' and `-x' compiler
801     options, they're called `arch' but effectively control both ABI
802     and ISA.
803
804     On Solaris 2.6 and earlier, only `ABI=32' is available since the
805     kernel doesn't save all registers.
806
807     On Solaris 2.7 with the kernel in 32-bit mode, a normal native
808     build will reject `ABI=64' because the resulting executables won't
809     run.  `ABI=64' can still be built if desired by making it look
810     like a cross-compile, for example
811
812          ./configure --build=none --host=sparcv9-sun-solaris2.7 ABI=64
813
814
815File: gmp.info,  Node: Notes for Package Builds,  Next: Notes for Particular Systems,  Prev: ABI and ISA,  Up: Installing GMP
816
8172.3 Notes for Package Builds
818============================
819
820GMP should present no great difficulties for packaging in a binary
821distribution.
822
823   Libtool is used to build the library and `-version-info' is set
824appropriately, having started from `3:0:0' in GMP 3.0 (*note Library
825interface versions: (libtool)Versioning.).
826
827   The GMP 4 series will be upwardly binary compatible in each release
828and will be upwardly binary compatible with all of the GMP 3 series.
829Additional function interfaces may be added in each release, so on
830systems where libtool versioning is not fully checked by the loader an
831auxiliary mechanism may be needed to express that a dynamic linked
832application depends on a new enough GMP.
833
834   An auxiliary mechanism may also be needed to express that
835`libgmpxx.la' (from `--enable-cxx', *note Build Options::) requires
836`libgmp.la' from the same GMP version, since this is not done by the
837libtool versioning, nor otherwise.  A mismatch will result in
838unresolved symbols from the linker, or perhaps the loader.
839
840   When building a package for a CPU family, care should be taken to use
841`--host' (or `--build') to choose the least common denominator among
842the CPUs which might use the package.  For example this might mean plain
843`sparc' (meaning V7) for SPARCs.
844
845   For x86s, `--enable-fat' sets things up for a fat binary build,
846making a runtime selection of optimized low level routines.  This is a
847good choice for packaging to run on a range of x86 chips.
848
849   Users who care about speed will want GMP built for their exact CPU
850type, to make best use of the available optimizations.  Providing a way
851to suitably rebuild a package may be useful.  This could be as simple
852as making it possible for a user to omit `--build' (and `--host') so
853`./config.guess' will detect the CPU.  But a way to manually specify a
854`--build' will be wanted for systems where `./config.guess' is inexact.
855
856   On systems with multiple ABIs, a packaged build will need to decide
857which among the choices is to be provided, see *note ABI and ISA::.  A
858given run of `./configure' etc will only build one ABI.  If a second
859ABI is also required then a second run of `./configure' etc must be
860made, starting from a clean directory tree (`make distclean').
861
862   As noted under "ABI and ISA", currently no attempt is made to follow
863system conventions for install locations that vary with ABI, such as
864`/usr/lib/sparcv9' for `ABI=64' as opposed to `/usr/lib' for `ABI=32'.
865A package build can override `libdir' and other standard variables as
866necessary.
867
868   Note that `gmp.h' is a generated file, and will be architecture and
869ABI dependent.  When attempting to install two ABIs simultaneously it
870will be important that an application compile gets the correct `gmp.h'
871for its desired ABI.  If compiler include paths don't vary with ABI
872options then it might be necessary to create a `/usr/include/gmp.h'
873which tests preprocessor symbols and chooses the correct actual `gmp.h'.
874
875
876File: gmp.info,  Node: Notes for Particular Systems,  Next: Known Build Problems,  Prev: Notes for Package Builds,  Up: Installing GMP
877
8782.4 Notes for Particular Systems
879================================
880
881AIX 3 and 4
882     On systems `*-*-aix[34]*' shared libraries are disabled by
883     default, since some versions of the native `ar' fail on the
884     convenience libraries used.  A shared build can be attempted with
885
886          ./configure --enable-shared --disable-static
887
888     Note that the `--disable-static' is necessary because in a shared
889     build libtool makes `libgmp.a' a symlink to `libgmp.so',
890     apparently for the benefit of old versions of `ld' which only
891     recognise `.a', but unfortunately this is done even if a fully
892     functional `ld' is available.
893
894ARM
895     On systems `arm*-*-*', versions of GCC up to and including 2.95.3
896     have a bug in unsigned division, giving wrong results for some
897     operands.  GMP `./configure' will demand GCC 2.95.4 or later.
898
899Compaq C++
900     Compaq C++ on OSF 5.1 has two flavours of `iostream', a standard
901     one and an old pre-standard one (see `man iostream_intro').  GMP
902     can only use the standard one, which unfortunately is not the
903     default but must be selected by defining `__USE_STD_IOSTREAM'.
904     Configure with for instance
905
906          ./configure --enable-cxx CPPFLAGS=-D__USE_STD_IOSTREAM
907
908Floating Point Mode
909     On some systems, the hardware floating point has a control mode
910     which can set all operations to be done in a particular precision,
911     for instance single, double or extended on x86 systems (x87
912     floating point).  The GMP functions involving a `double' cannot be
913     expected to operate to their full precision when the hardware is
914     in single precision mode.  Of course this affects all code,
915     including application code, not just GMP.
916
917MS-DOS and MS Windows
918     On an MS-DOS system DJGPP can be used to build GMP, and on an MS
919     Windows system Cygwin, DJGPP and MINGW can be used.  All three are
920     excellent ports of GCC and the various GNU tools.
921
922          `http://www.cygwin.com/'
923          `http://www.delorie.com/djgpp/'
924          `http://www.mingw.org/'
925
926     Microsoft also publishes an Interix "Services for Unix" which can
927     be used to build GMP on Windows (with a normal `./configure'), but
928     it's not free software.
929
930MS Windows DLLs
931     On systems `*-*-cygwin*', `*-*-mingw*' and `*-*-pw32*' by default
932     GMP builds only a static library, but a DLL can be built instead
933     using
934
935          ./configure --disable-static --enable-shared
936
937     Static and DLL libraries can't both be built, since certain export
938     directives in `gmp.h' must be different.
939
940     A MINGW DLL build of GMP can be used with Microsoft C.  Libtool
941     doesn't install a `.lib' format import library, but it can be
942     created with MS `lib' as follows, and copied to the install
943     directory.  Similarly for `libmp' and `libgmpxx'.
944
945          cd .libs
946          lib /def:libgmp-3.dll.def /out:libgmp-3.lib
947
948     MINGW uses the C runtime library `msvcrt.dll' for I/O, so
949     applications wanting to use the GMP I/O routines must be compiled
950     with `cl /MD' to do the same.  If one of the other C runtime
951     library choices provided by MS C is desired then the suggestion is
952     to use the GMP string functions and confine I/O to the application.
953
954Motorola 68k CPU Types
955     `m68k' is taken to mean 68000.  `m68020' or higher will give a
956     performance boost on applicable CPUs.  `m68360' can be used for
957     CPU32 series chips.  `m68302' can be used for "Dragonball" series
958     chips, though this is merely a synonym for `m68000'.
959
960OpenBSD 2.6
961     `m4' in this release of OpenBSD has a bug in `eval' that makes it
962     unsuitable for `.asm' file processing.  `./configure' will detect
963     the problem and either abort or choose another m4 in the `PATH'.
964     The bug is fixed in OpenBSD 2.7, so either upgrade or use GNU m4.
965
966Power CPU Types
967     In GMP, CPU types `power*' and `powerpc*' will each use
968     instructions not available on the other, so it's important to
969     choose the right one for the CPU that will be used.  Currently GMP
970     has no assembly code support for using just the common instruction
971     subset.  To get executables that run on both, the current
972     suggestion is to use the generic C code (`--disable-assembly'),
973     possibly with appropriate compiler options (like `-mcpu=common' for
974     `gcc').  CPU `rs6000' (which is not a CPU but a family of
975     workstations) is accepted by `config.sub', but is currently
976     equivalent to `--disable-assembly'.
977
978Sparc CPU Types
979     `sparcv8' or `supersparc' on relevant systems will give a
980     significant performance increase over the V7 code selected by plain
981     `sparc'.
982
983Sparc App Regs
984     The GMP assembly code for both 32-bit and 64-bit Sparc clobbers the
985     "application registers" `g2', `g3' and `g4', the same way that the
986     GCC default `-mapp-regs' does (*note SPARC Options: (gcc)SPARC
987     Options.).
988
989     This makes that code unsuitable for use with the special V9
990     `-mcmodel=embmedany' (which uses `g4' as a data segment pointer),
991     and for applications wanting to use those registers for special
992     purposes.  In these cases the only suggestion currently is to
993     build GMP with `--disable-assembly' to avoid the assembly code.
994
995SunOS 4
996     `/usr/bin/m4' lacks various features needed to process `.asm'
997     files, and instead `./configure' will automatically use
998     `/usr/5bin/m4', which we believe is always available (if not then
999     use GNU m4).
1000
1001x86 CPU Types
1002     `i586', `pentium' or `pentiummmx' code is good for its intended P5
1003     Pentium chips, but quite slow when run on Intel P6 class chips
1004     (PPro, P-II, P-III).  `i386' is a better choice when making
1005     binaries that must run on both.
1006
1007x86 MMX and SSE2 Code
1008     If the CPU selected has MMX code but the assembler doesn't support
1009     it, a warning is given and non-MMX code is used instead.  This
1010     will be an inferior build, since the MMX code that's present is
1011     there because it's faster than the corresponding plain integer
1012     code.  The same applies to SSE2.
1013
1014     Old versions of `gas' don't support MMX instructions, in particular
1015     version 1.92.3 that comes with FreeBSD 2.2.8 or the more recent
1016     OpenBSD 3.1 doesn't.
1017
1018     Solaris 2.6 and 2.7 `as' generate incorrect object code for
1019     register to register `movq' instructions, and so can't be used for
1020     MMX code.  Install a recent `gas' if MMX code is wanted on these
1021     systems.
1022
1023
1024File: gmp.info,  Node: Known Build Problems,  Next: Performance optimization,  Prev: Notes for Particular Systems,  Up: Installing GMP
1025
10262.5 Known Build Problems
1027========================
1028
1029You might find more up-to-date information at `http://gmplib.org/'.
1030
1031Compiler link options
1032     The version of libtool currently in use rather aggressively strips
1033     compiler options when linking a shared library.  This will
1034     hopefully be relaxed in the future, but for now if this is a
1035     problem the suggestion is to create a little script to hide them,
1036     and for instance configure with
1037
1038          ./configure CC=gcc-with-my-options
1039
1040DJGPP (`*-*-msdosdjgpp*')
1041     The DJGPP port of `bash' 2.03 is unable to run the `configure'
1042     script, it exits silently, having died writing a preamble to
1043     `config.log'.  Use `bash' 2.04 or higher.
1044
1045     `make all' was found to run out of memory during the final
1046     `libgmp.la' link on one system tested, despite having 64Mb
1047     available.  Running `make libgmp.la' directly helped, perhaps
1048     recursing into the various subdirectories uses up memory.
1049
1050GNU binutils `strip' prior to 2.12
1051     `strip' from GNU binutils 2.11 and earlier should not be used on
1052     the static libraries `libgmp.a' and `libmp.a' since it will
1053     discard all but the last of multiple archive members with the same
1054     name, like the three versions of `init.o' in `libgmp.a'.  Binutils
1055     2.12 or higher can be used successfully.
1056
1057     The shared libraries `libgmp.so' and `libmp.so' are not affected by
1058     this and any version of `strip' can be used on them.
1059
1060`make' syntax error
1061     On certain versions of SCO OpenServer 5 and IRIX 6.5 the native
1062     `make' is unable to handle the long dependencies list for
1063     `libgmp.la'.  The symptom is a "syntax error" on the following
1064     line of the top-level `Makefile'.
1065
1066          libgmp.la: $(libgmp_la_OBJECTS) $(libgmp_la_DEPENDENCIES)
1067
1068     Either use GNU Make, or as a workaround remove
1069     `$(libgmp_la_DEPENDENCIES)' from that line (which will make the
1070     initial build work, but if any recompiling is done `libgmp.la'
1071     might not be rebuilt).
1072
1073MacOS X (`*-*-darwin*')
1074     Libtool currently only knows how to create shared libraries on
1075     MacOS X using the native `cc' (which is a modified GCC), not a
1076     plain GCC.  A static-only build should work though
1077     (`--disable-shared').
1078
1079NeXT prior to 3.3
1080     The system compiler on old versions of NeXT was a massacred and
1081     old GCC, even if it called itself `cc'.  This compiler cannot be
1082     used to build GMP, you need to get a real GCC, and install that.
1083     (NeXT may have fixed this in release 3.3 of their system.)
1084
1085POWER and PowerPC
1086     Bugs in GCC 2.7.2 (and 2.6.3) mean it can't be used to compile GMP
1087     on POWER or PowerPC.  If you want to use GCC for these machines,
1088     get GCC 2.7.2.1 (or later).
1089
1090Sequent Symmetry
1091     Use the GNU assembler instead of the system assembler, since the
1092     latter has serious bugs.
1093
1094Solaris 2.6
1095     The system `sed' prints an error "Output line too long" when
1096     libtool builds `libgmp.la'.  This doesn't seem to cause any
1097     obvious ill effects, but GNU `sed' is recommended, to avoid any
1098     doubt.
1099
1100Sparc Solaris 2.7 with gcc 2.95.2 in `ABI=32'
1101     A shared library build of GMP seems to fail in this combination,
1102     it builds but then fails the tests, apparently due to some
1103     incorrect data relocations within `gmp_randinit_lc_2exp_size'.
1104     The exact cause is unknown, `--disable-shared' is recommended.
1105
1106
1107File: gmp.info,  Node: Performance optimization,  Prev: Known Build Problems,  Up: Installing GMP
1108
11092.6 Performance optimization
1110============================
1111
1112For optimal performance, build GMP for the exact CPU type of the target
1113computer, see *note Build Options::.
1114
1115   Unlike what is the case for most other programs, the compiler
1116typically doesn't matter much, since GMP uses assembly language for the
1117most critical operation.
1118
1119   In particular for long-running GMP applications, and applications
1120demanding extremely large numbers, building and running the `tuneup'
1121program in the `tune' subdirectory, can be important.  For example,
1122
1123     cd tune
1124     make tuneup
1125     ./tuneup
1126
1127   will generate better contents for the `gmp-mparam.h' parameter file.
1128
1129   To use the results, put the output in the file indicated in the
1130`Parameters for ...' header.  Then recompile from scratch.
1131
1132   The `tuneup' program takes one useful parameter, `-f NNN', which
1133instructs the program how long to check FFT multiply parameters.  If
1134you're going to use GMP for extremely large numbers, you may want to
1135run `tuneup' with a large NNN value.
1136
1137
1138File: gmp.info,  Node: GMP Basics,  Next: Reporting Bugs,  Prev: Installing GMP,  Up: Top
1139
11403 GMP Basics
1141************
1142
1143*Using functions, macros, data types, etc. not documented in this
1144manual is strongly discouraged.  If you do so your application is
1145guaranteed to be incompatible with future versions of GMP.*
1146
1147* Menu:
1148
1149* Headers and Libraries::
1150* Nomenclature and Types::
1151* Function Classes::
1152* Variable Conventions::
1153* Parameter Conventions::
1154* Memory Management::
1155* Reentrancy::
1156* Useful Macros and Constants::
1157* Compatibility with older versions::
1158* Demonstration Programs::
1159* Efficiency::
1160* Debugging::
1161* Profiling::
1162* Autoconf::
1163* Emacs::
1164
1165
1166File: gmp.info,  Node: Headers and Libraries,  Next: Nomenclature and Types,  Prev: GMP Basics,  Up: GMP Basics
1167
11683.1 Headers and Libraries
1169=========================
1170
1171All declarations needed to use GMP are collected in the include file
1172`gmp.h'.  It is designed to work with both C and C++ compilers.
1173
1174     #include <gmp.h>
1175
1176   Note however that prototypes for GMP functions with `FILE *'
1177parameters are only provided if `<stdio.h>' is included too.
1178
1179     #include <stdio.h>
1180     #include <gmp.h>
1181
1182   Likewise `<stdarg.h>' (or `<varargs.h>') is required for prototypes
1183with `va_list' parameters, such as `gmp_vprintf'.  And `<obstack.h>'
1184for prototypes with `struct obstack' parameters, such as
1185`gmp_obstack_printf', when available.
1186
1187   All programs using GMP must link against the `libgmp' library.  On a
1188typical Unix-like system this can be done with `-lgmp', for example
1189
1190     gcc myprogram.c -lgmp
1191
1192   GMP C++ functions are in a separate `libgmpxx' library.  This is
1193built and installed if C++ support has been enabled (*note Build
1194Options::).  For example,
1195
1196     g++ mycxxprog.cc -lgmpxx -lgmp
1197
1198   GMP is built using Libtool and an application can use that to link
1199if desired, *note GNU Libtool: (libtool)Top.
1200
1201   If GMP has been installed to a non-standard location then it may be
1202necessary to use `-I' and `-L' compiler options to point to the right
1203directories, and some sort of run-time path for a shared library.
1204
1205
1206File: gmp.info,  Node: Nomenclature and Types,  Next: Function Classes,  Prev: Headers and Libraries,  Up: GMP Basics
1207
12083.2 Nomenclature and Types
1209==========================
1210
1211In this manual, "integer" usually means a multiple precision integer, as
1212defined by the GMP library.  The C data type for such integers is
1213`mpz_t'.  Here are some examples of how to declare such integers:
1214
1215     mpz_t sum;
1216
1217     struct foo { mpz_t x, y; };
1218
1219     mpz_t vec[20];
1220
1221   "Rational number" means a multiple precision fraction.  The C data
1222type for these fractions is `mpq_t'.  For example:
1223
1224     mpq_t quotient;
1225
1226   "Floating point number" or "Float" for short, is an arbitrary
1227precision mantissa with a limited precision exponent.  The C data type
1228for such objects is `mpf_t'.  For example:
1229
1230     mpf_t fp;
1231
1232   The floating point functions accept and return exponents in the C
1233type `mp_exp_t'.  Currently this is usually a `long', but on some
1234systems it's an `int' for efficiency.
1235
1236   A "limb" means the part of a multi-precision number that fits in a
1237single machine word.  (We chose this word because a limb of the human
1238body is analogous to a digit, only larger, and containing several
1239digits.)  Normally a limb is 32 or 64 bits.  The C data type for a limb
1240is `mp_limb_t'.
1241
1242   Counts of limbs of a multi-precision number represented in the C type
1243`mp_size_t'.  Currently this is normally a `long', but on some systems
1244it's an `int' for efficiency, and on some systems it will be `long
1245long' in the future.
1246
1247   Counts of bits of a multi-precision number are represented in the C
1248type `mp_bitcnt_t'.  Currently this is always an `unsigned long', but on
1249some systems it will be an `unsigned long long' in the future.
1250
1251   "Random state" means an algorithm selection and current state data.
1252The C data type for such objects is `gmp_randstate_t'.  For example:
1253
1254     gmp_randstate_t rstate;
1255
1256   Also, in general `mp_bitcnt_t' is used for bit counts and ranges, and
1257`size_t' is used for byte or character counts.
1258
1259
1260File: gmp.info,  Node: Function Classes,  Next: Variable Conventions,  Prev: Nomenclature and Types,  Up: GMP Basics
1261
12623.3 Function Classes
1263====================
1264
1265There are six classes of functions in the GMP library:
1266
1267  1. Functions for signed integer arithmetic, with names beginning with
1268     `mpz_'.  The associated type is `mpz_t'.  There are about 150
1269     functions in this class.  (*note Integer Functions::)
1270
1271  2. Functions for rational number arithmetic, with names beginning with
1272     `mpq_'.  The associated type is `mpq_t'.  There are about 40
1273     functions in this class, but the integer functions can be used for
1274     arithmetic on the numerator and denominator separately.  (*note
1275     Rational Number Functions::)
1276
1277  3. Functions for floating-point arithmetic, with names beginning with
1278     `mpf_'.  The associated type is `mpf_t'.  There are about 60
1279     functions is this class.  (*note Floating-point Functions::)
1280
1281  4. Fast low-level functions that operate on natural numbers.  These
1282     are used by the functions in the preceding groups, and you can
1283     also call them directly from very time-critical user programs.
1284     These functions' names begin with `mpn_'.  The associated type is
1285     array of `mp_limb_t'.  There are about 30 (hard-to-use) functions
1286     in this class.  (*note Low-level Functions::)
1287
1288  5. Miscellaneous functions.  Functions for setting up custom
1289     allocation and functions for generating random numbers.  (*note
1290     Custom Allocation::, and *note Random Number Functions::)
1291
1292
1293File: gmp.info,  Node: Variable Conventions,  Next: Parameter Conventions,  Prev: Function Classes,  Up: GMP Basics
1294
12953.4 Variable Conventions
1296========================
1297
1298GMP functions generally have output arguments before input arguments.
1299This notation is by analogy with the assignment operator.  The BSD MP
1300compatibility functions are exceptions, having the output arguments
1301last.
1302
1303   GMP lets you use the same variable for both input and output in one
1304call.  For example, the main function for integer multiplication,
1305`mpz_mul', can be used to square `x' and put the result back in `x' with
1306
1307     mpz_mul (x, x, x);
1308
1309   Before you can assign to a GMP variable, you need to initialize it
1310by calling one of the special initialization functions.  When you're
1311done with a variable, you need to clear it out, using one of the
1312functions for that purpose.  Which function to use depends on the type
1313of variable.  See the chapters on integer functions, rational number
1314functions, and floating-point functions for details.
1315
1316   A variable should only be initialized once, or at least cleared
1317between each initialization.  After a variable has been initialized, it
1318may be assigned to any number of times.
1319
1320   For efficiency reasons, avoid excessive initializing and clearing.
1321In general, initialize near the start of a function and clear near the
1322end.  For example,
1323
1324     void
1325     foo (void)
1326     {
1327       mpz_t  n;
1328       int    i;
1329       mpz_init (n);
1330       for (i = 1; i < 100; i++)
1331         {
1332           mpz_mul (n, ...);
1333           mpz_fdiv_q (n, ...);
1334           ...
1335         }
1336       mpz_clear (n);
1337     }
1338
1339
1340File: gmp.info,  Node: Parameter Conventions,  Next: Memory Management,  Prev: Variable Conventions,  Up: GMP Basics
1341
13423.5 Parameter Conventions
1343=========================
1344
1345When a GMP variable is used as a function parameter, it's effectively a
1346call-by-reference, meaning if the function stores a value there it will
1347change the original in the caller.  Parameters which are input-only can
1348be designated `const' to provoke a compiler error or warning on
1349attempting to modify them.
1350
1351   When a function is going to return a GMP result, it should designate
1352a parameter that it sets, like the library functions do.  More than one
1353value can be returned by having more than one output parameter, again
1354like the library functions.  A `return' of an `mpz_t' etc doesn't
1355return the object, only a pointer, and this is almost certainly not
1356what's wanted.
1357
1358   Here's an example accepting an `mpz_t' parameter, doing a
1359calculation, and storing the result to the indicated parameter.
1360
1361     void
1362     foo (mpz_t result, const mpz_t param, unsigned long n)
1363     {
1364       unsigned long  i;
1365       mpz_mul_ui (result, param, n);
1366       for (i = 1; i < n; i++)
1367         mpz_add_ui (result, result, i*7);
1368     }
1369
1370     int
1371     main (void)
1372     {
1373       mpz_t  r, n;
1374       mpz_init (r);
1375       mpz_init_set_str (n, "123456", 0);
1376       foo (r, n, 20L);
1377       gmp_printf ("%Zd\n", r);
1378       return 0;
1379     }
1380
1381   `foo' works even if the mainline passes the same variable for
1382`param' and `result', just like the library functions.  But sometimes
1383it's tricky to make that work, and an application might not want to
1384bother supporting that sort of thing.
1385
1386   For interest, the GMP types `mpz_t' etc are implemented as
1387one-element arrays of certain structures.  This is why declaring a
1388variable creates an object with the fields GMP needs, but then using it
1389as a parameter passes a pointer to the object.  Note that the actual
1390fields in each `mpz_t' etc are for internal use only and should not be
1391accessed directly by code that expects to be compatible with future GMP
1392releases.
1393
1394
1395File: gmp.info,  Node: Memory Management,  Next: Reentrancy,  Prev: Parameter Conventions,  Up: GMP Basics
1396
13973.6 Memory Management
1398=====================
1399
1400The GMP types like `mpz_t' are small, containing only a couple of sizes,
1401and pointers to allocated data.  Once a variable is initialized, GMP
1402takes care of all space allocation.  Additional space is allocated
1403whenever a variable doesn't have enough.
1404
1405   `mpz_t' and `mpq_t' variables never reduce their allocated space.
1406Normally this is the best policy, since it avoids frequent reallocation.
1407Applications that need to return memory to the heap at some particular
1408point can use `mpz_realloc2', or clear variables no longer needed.
1409
1410   `mpf_t' variables, in the current implementation, use a fixed amount
1411of space, determined by the chosen precision and allocated at
1412initialization, so their size doesn't change.
1413
1414   All memory is allocated using `malloc' and friends by default, but
1415this can be changed, see *note Custom Allocation::.  Temporary memory
1416on the stack is also used (via `alloca'), but this can be changed at
1417build-time if desired, see *note Build Options::.
1418
1419
1420File: gmp.info,  Node: Reentrancy,  Next: Useful Macros and Constants,  Prev: Memory Management,  Up: GMP Basics
1421
14223.7 Reentrancy
1423==============
1424
1425GMP is reentrant and thread-safe, with some exceptions:
1426
1427   * If configured with `--enable-alloca=malloc-notreentrant' (or with
1428     `--enable-alloca=notreentrant' when `alloca' is not available),
1429     then naturally GMP is not reentrant.
1430
1431   * `mpf_set_default_prec' and `mpf_init' use a global variable for the
1432     selected precision.  `mpf_init2' can be used instead, and in the
1433     C++ interface an explicit precision to the `mpf_class' constructor.
1434
1435   * `mpz_random' and the other old random number functions use a global
1436     random state and are hence not reentrant.  The newer random number
1437     functions that accept a `gmp_randstate_t' parameter can be used
1438     instead.
1439
1440   * `gmp_randinit' (obsolete) returns an error indication through a
1441     global variable, which is not thread safe.  Applications are
1442     advised to use `gmp_randinit_default' or `gmp_randinit_lc_2exp'
1443     instead.
1444
1445   * `mp_set_memory_functions' uses global variables to store the
1446     selected memory allocation functions.
1447
1448   * If the memory allocation functions set by a call to
1449     `mp_set_memory_functions' (or `malloc' and friends by default) are
1450     not reentrant, then GMP will not be reentrant either.
1451
1452   * If the standard I/O functions such as `fwrite' are not reentrant
1453     then the GMP I/O functions using them will not be reentrant either.
1454
1455   * It's safe for two threads to read from the same GMP variable
1456     simultaneously, but it's not safe for one to read while the
1457     another might be writing, nor for two threads to write
1458     simultaneously.  It's not safe for two threads to generate a
1459     random number from the same `gmp_randstate_t' simultaneously,
1460     since this involves an update of that variable.
1461
1462
1463File: gmp.info,  Node: Useful Macros and Constants,  Next: Compatibility with older versions,  Prev: Reentrancy,  Up: GMP Basics
1464
14653.8 Useful Macros and Constants
1466===============================
1467
1468 -- Global Constant: const int mp_bits_per_limb
1469     The number of bits per limb.
1470
1471 -- Macro: __GNU_MP_VERSION
1472 -- Macro: __GNU_MP_VERSION_MINOR
1473 -- Macro: __GNU_MP_VERSION_PATCHLEVEL
1474     The major and minor GMP version, and patch level, respectively, as
1475     integers.  For GMP i.j, these numbers will be i, j, and 0,
1476     respectively.  For GMP i.j.k, these numbers will be i, j, and k,
1477     respectively.
1478
1479 -- Global Constant: const char * const gmp_version
1480     The GMP version number, as a null-terminated string, in the form
1481     "i.j.k".  This release is "5.1.3".  Note that the format "i.j" was
1482     used, before version 4.3.0, when k was zero.
1483
1484 -- Macro: __GMP_CC
1485 -- Macro: __GMP_CFLAGS
1486     The compiler and compiler flags, respectively, used when compiling
1487     GMP, as strings.
1488
1489
1490File: gmp.info,  Node: Compatibility with older versions,  Next: Demonstration Programs,  Prev: Useful Macros and Constants,  Up: GMP Basics
1491
14923.9 Compatibility with older versions
1493=====================================
1494
1495This version of GMP is upwardly binary compatible with all 5.x, 4.x,
1496and 3.x versions, and upwardly compatible at the source level with all
14972.x versions, with the following exceptions.
1498
1499   * `mpn_gcd' had its source arguments swapped as of GMP 3.0, for
1500     consistency with other `mpn' functions.
1501
1502   * `mpf_get_prec' counted precision slightly differently in GMP 3.0
1503     and 3.0.1, but in 3.1 reverted to the 2.x style.
1504
1505   * `mpn_bdivmod', documented as preliminary in GMP 4, has been
1506     removed.
1507
1508   There are a number of compatibility issues between GMP 1 and GMP 2
1509that of course also apply when porting applications from GMP 1 to GMP
15105.  Please see the GMP 2 manual for details.
1511
1512
1513File: gmp.info,  Node: Demonstration Programs,  Next: Efficiency,  Prev: Compatibility with older versions,  Up: GMP Basics
1514
15153.10 Demonstration programs
1516===========================
1517
1518The `demos' subdirectory has some sample programs using GMP.  These
1519aren't built or installed, but there's a `Makefile' with rules for them.
1520For instance,
1521
1522     make pexpr
1523     ./pexpr 68^975+10
1524
1525The following programs are provided
1526
1527   * `pexpr' is an expression evaluator, the program used on the GMP
1528     web page.
1529
1530   * The `calc' subdirectory has a similar but simpler evaluator using
1531     `lex' and `yacc'.
1532
1533   * The `expr' subdirectory is yet another expression evaluator, a
1534     library designed for ease of use within a C program.  See
1535     `demos/expr/README' for more information.
1536
1537   * `factorize' is a Pollard-Rho factorization program.
1538
1539   * `isprime' is a command-line interface to the `mpz_probab_prime_p'
1540     function.
1541
1542   * `primes' counts or lists primes in an interval, using a sieve.
1543
1544   * `qcn' is an example use of `mpz_kronecker_ui' to estimate quadratic
1545     class numbers.
1546
1547   * The `perl' subdirectory is a comprehensive perl interface to GMP.
1548     See `demos/perl/INSTALL' for more information.  Documentation is
1549     in POD format in `demos/perl/GMP.pm'.
1550
1551   As an aside, consideration has been given at various times to some
1552sort of expression evaluation within the main GMP library.  Going
1553beyond something minimal quickly leads to matters like user-defined
1554functions, looping, fixnums for control variables, etc, which are
1555considered outside the scope of GMP (much closer to language
1556interpreters or compilers, *Note Language Bindings::.)  Something
1557simple for program input convenience may yet be a possibility, a
1558combination of the `expr' demo and the `pexpr' tree back-end perhaps.
1559But for now the above evaluators are offered as illustrations.
1560
1561
1562File: gmp.info,  Node: Efficiency,  Next: Debugging,  Prev: Demonstration Programs,  Up: GMP Basics
1563
15643.11 Efficiency
1565===============
1566
1567Small Operands
1568     On small operands, the time for function call overheads and memory
1569     allocation can be significant in comparison to actual calculation.
1570     This is unavoidable in a general purpose variable precision
1571     library, although GMP attempts to be as efficient as it can on
1572     both large and small operands.
1573
1574Static Linking
1575     On some CPUs, in particular the x86s, the static `libgmp.a' should
1576     be used for maximum speed, since the PIC code in the shared
1577     `libgmp.so' will have a small overhead on each function call and
1578     global data address.  For many programs this will be
1579     insignificant, but for long calculations there's a gain to be had.
1580
1581Initializing and Clearing
1582     Avoid excessive initializing and clearing of variables, since this
1583     can be quite time consuming, especially in comparison to otherwise
1584     fast operations like addition.
1585
1586     A language interpreter might want to keep a free list or stack of
1587     initialized variables ready for use.  It should be possible to
1588     integrate something like that with a garbage collector too.
1589
1590Reallocations
1591     An `mpz_t' or `mpq_t' variable used to hold successively increasing
1592     values will have its memory repeatedly `realloc'ed, which could be
1593     quite slow or could fragment memory, depending on the C library.
1594     If an application can estimate the final size then `mpz_init2' or
1595     `mpz_realloc2' can be called to allocate the necessary space from
1596     the beginning (*note Initializing Integers::).
1597
1598     It doesn't matter if a size set with `mpz_init2' or `mpz_realloc2'
1599     is too small, since all functions will do a further reallocation
1600     if necessary.  Badly overestimating memory required will waste
1601     space though.
1602
1603`2exp' Functions
1604     It's up to an application to call functions like `mpz_mul_2exp'
1605     when appropriate.  General purpose functions like `mpz_mul' make
1606     no attempt to identify powers of two or other special forms,
1607     because such inputs will usually be very rare and testing every
1608     time would be wasteful.
1609
1610`ui' and `si' Functions
1611     The `ui' functions and the small number of `si' functions exist for
1612     convenience and should be used where applicable.  But if for
1613     example an `mpz_t' contains a value that fits in an `unsigned
1614     long' there's no need extract it and call a `ui' function, just
1615     use the regular `mpz' function.
1616
1617In-Place Operations
1618     `mpz_abs', `mpq_abs', `mpf_abs', `mpz_neg', `mpq_neg' and
1619     `mpf_neg' are fast when used for in-place operations like
1620     `mpz_abs(x,x)', since in the current implementation only a single
1621     field of `x' needs changing.  On suitable compilers (GCC for
1622     instance) this is inlined too.
1623
1624     `mpz_add_ui', `mpz_sub_ui', `mpf_add_ui' and `mpf_sub_ui' benefit
1625     from an in-place operation like `mpz_add_ui(x,x,y)', since usually
1626     only one or two limbs of `x' will need to be changed.  The same
1627     applies to the full precision `mpz_add' etc if `y' is small.  If
1628     `y' is big then cache locality may be helped, but that's all.
1629
1630     `mpz_mul' is currently the opposite, a separate destination is
1631     slightly better.  A call like `mpz_mul(x,x,y)' will, unless `y' is
1632     only one limb, make a temporary copy of `x' before forming the
1633     result.  Normally that copying will only be a tiny fraction of the
1634     time for the multiply, so this is not a particularly important
1635     consideration.
1636
1637     `mpz_set', `mpq_set', `mpq_set_num', `mpf_set', etc, make no
1638     attempt to recognise a copy of something to itself, so a call like
1639     `mpz_set(x,x)' will be wasteful.  Naturally that would never be
1640     written deliberately, but if it might arise from two pointers to
1641     the same object then a test to avoid it might be desirable.
1642
1643          if (x != y)
1644            mpz_set (x, y);
1645
1646     Note that it's never worth introducing extra `mpz_set' calls just
1647     to get in-place operations.  If a result should go to a particular
1648     variable then just direct it there and let GMP take care of data
1649     movement.
1650
1651Divisibility Testing (Small Integers)
1652     `mpz_divisible_ui_p' and `mpz_congruent_ui_p' are the best
1653     functions for testing whether an `mpz_t' is divisible by an
1654     individual small integer.  They use an algorithm which is faster
1655     than `mpz_tdiv_ui', but which gives no useful information about
1656     the actual remainder, only whether it's zero (or a particular
1657     value).
1658
1659     However when testing divisibility by several small integers, it's
1660     best to take a remainder modulo their product, to save
1661     multi-precision operations.  For instance to test whether a number
1662     is divisible by any of 23, 29 or 31 take a remainder modulo
1663     23*29*31 = 20677 and then test that.
1664
1665     The division functions like `mpz_tdiv_q_ui' which give a quotient
1666     as well as a remainder are generally a little slower than the
1667     remainder-only functions like `mpz_tdiv_ui'.  If the quotient is
1668     only rarely wanted then it's probably best to just take a
1669     remainder and then go back and calculate the quotient if and when
1670     it's wanted (`mpz_divexact_ui' can be used if the remainder is
1671     zero).
1672
1673Rational Arithmetic
1674     The `mpq' functions operate on `mpq_t' values with no common
1675     factors in the numerator and denominator.  Common factors are
1676     checked-for and cast out as necessary.  In general, cancelling
1677     factors every time is the best approach since it minimizes the
1678     sizes for subsequent operations.
1679
1680     However, applications that know something about the factorization
1681     of the values they're working with might be able to avoid some of
1682     the GCDs used for canonicalization, or swap them for divisions.
1683     For example when multiplying by a prime it's enough to check for
1684     factors of it in the denominator instead of doing a full GCD.  Or
1685     when forming a big product it might be known that very little
1686     cancellation will be possible, and so canonicalization can be left
1687     to the end.
1688
1689     The `mpq_numref' and `mpq_denref' macros give access to the
1690     numerator and denominator to do things outside the scope of the
1691     supplied `mpq' functions.  *Note Applying Integer Functions::.
1692
1693     The canonical form for rationals allows mixed-type `mpq_t' and
1694     integer additions or subtractions to be done directly with
1695     multiples of the denominator.  This will be somewhat faster than
1696     `mpq_add'.  For example,
1697
1698          /* mpq increment */
1699          mpz_add (mpq_numref(q), mpq_numref(q), mpq_denref(q));
1700
1701          /* mpq += unsigned long */
1702          mpz_addmul_ui (mpq_numref(q), mpq_denref(q), 123UL);
1703
1704          /* mpq -= mpz */
1705          mpz_submul (mpq_numref(q), mpq_denref(q), z);
1706
1707Number Sequences
1708     Functions like `mpz_fac_ui', `mpz_fib_ui' and `mpz_bin_uiui' are
1709     designed for calculating isolated values.  If a range of values is
1710     wanted it's probably best to call to get a starting point and
1711     iterate from there.
1712
1713Text Input/Output
1714     Hexadecimal or octal are suggested for input or output in text
1715     form.  Power-of-2 bases like these can be converted much more
1716     efficiently than other bases, like decimal.  For big numbers
1717     there's usually nothing of particular interest to be seen in the
1718     digits, so the base doesn't matter much.
1719
1720     Maybe we can hope octal will one day become the normal base for
1721     everyday use, as proposed by King Charles XII of Sweden and later
1722     reformers.
1723
1724
1725File: gmp.info,  Node: Debugging,  Next: Profiling,  Prev: Efficiency,  Up: GMP Basics
1726
17273.12 Debugging
1728==============
1729
1730Stack Overflow
1731     Depending on the system, a segmentation violation or bus error
1732     might be the only indication of stack overflow.  See
1733     `--enable-alloca' choices in *note Build Options::, for how to
1734     address this.
1735
1736     In new enough versions of GCC, `-fstack-check' may be able to
1737     ensure an overflow is recognised by the system before too much
1738     damage is done, or `-fstack-limit-symbol' or
1739     `-fstack-limit-register' may be able to add checking if the system
1740     itself doesn't do any (*note Options for Code Generation:
1741     (gcc)Code Gen Options.).  These options must be added to the
1742     `CFLAGS' used in the GMP build (*note Build Options::), adding
1743     them just to an application will have no effect.  Note also
1744     they're a slowdown, adding overhead to each function call and each
1745     stack allocation.
1746
1747Heap Problems
1748     The most likely cause of application problems with GMP is heap
1749     corruption.  Failing to `init' GMP variables will have
1750     unpredictable effects, and corruption arising elsewhere in a
1751     program may well affect GMP.  Initializing GMP variables more than
1752     once or failing to clear them will cause memory leaks.
1753
1754     In all such cases a `malloc' debugger is recommended.  On a GNU or
1755     BSD system the standard C library `malloc' has some diagnostic
1756     facilities, see *note Allocation Debugging: (libc)Allocation
1757     Debugging, or `man 3 malloc'.  Other possibilities, in no
1758     particular order, include
1759
1760          `http://www.inf.ethz.ch/personal/biere/projects/ccmalloc/'
1761          `http://dmalloc.com/'
1762          `http://www.perens.com/FreeSoftware/'  (electric fence)
1763          `http://packages.debian.org/stable/devel/fda'
1764          `http://www.gnupdate.org/components/leakbug/'
1765          `http://people.redhat.com/~otaylor/memprof/'
1766          `http://www.cbmamiga.demon.co.uk/mpatrol/'
1767
1768     The GMP default allocation routines in `memory.c' also have a
1769     simple sentinel scheme which can be enabled with `#define DEBUG'
1770     in that file.  This is mainly designed for detecting buffer
1771     overruns during GMP development, but might find other uses.
1772
1773Stack Backtraces
1774     On some systems the compiler options GMP uses by default can
1775     interfere with debugging.  In particular on x86 and 68k systems
1776     `-fomit-frame-pointer' is used and this generally inhibits stack
1777     backtracing.  Recompiling without such options may help while
1778     debugging, though the usual caveats about it potentially moving a
1779     memory problem or hiding a compiler bug will apply.
1780
1781GDB, the GNU Debugger
1782     A sample `.gdbinit' is included in the distribution, showing how
1783     to call some undocumented dump functions to print GMP variables
1784     from within GDB.  Note that these functions shouldn't be used in
1785     final application code since they're undocumented and may be
1786     subject to incompatible changes in future versions of GMP.
1787
1788Source File Paths
1789     GMP has multiple source files with the same name, in different
1790     directories.  For example `mpz', `mpq' and `mpf' each have an
1791     `init.c'.  If the debugger can't already determine the right one
1792     it may help to build with absolute paths on each C file.  One way
1793     to do that is to use a separate object directory with an absolute
1794     path to the source directory.
1795
1796          cd /my/build/dir
1797          /my/source/dir/gmp-5.1.3/configure
1798
1799     This works via `VPATH', and might require GNU `make'.  Alternately
1800     it might be possible to change the `.c.lo' rules appropriately.
1801
1802Assertion Checking
1803     The build option `--enable-assert' is available to add some
1804     consistency checks to the library (see *note Build Options::).
1805     These are likely to be of limited value to most applications.
1806     Assertion failures are just as likely to indicate memory
1807     corruption as a library or compiler bug.
1808
1809     Applications using the low-level `mpn' functions, however, will
1810     benefit from `--enable-assert' since it adds checks on the
1811     parameters of most such functions, many of which have subtle
1812     restrictions on their usage.  Note however that only the generic C
1813     code has checks, not the assembly code, so `--disable-assembly'
1814     should be used for maximum checking.
1815
1816Temporary Memory Checking
1817     The build option `--enable-alloca=debug' arranges that each block
1818     of temporary memory in GMP is allocated with a separate call to
1819     `malloc' (or the allocation function set with
1820     `mp_set_memory_functions').
1821
1822     This can help a malloc debugger detect accesses outside the
1823     intended bounds, or detect memory not released.  In a normal
1824     build, on the other hand, temporary memory is allocated in blocks
1825     which GMP divides up for its own use, or may be allocated with a
1826     compiler builtin `alloca' which will go nowhere near any malloc
1827     debugger hooks.
1828
1829Maximum Debuggability
1830     To summarize the above, a GMP build for maximum debuggability
1831     would be
1832
1833          ./configure --disable-shared --enable-assert \
1834            --enable-alloca=debug --disable-assembly CFLAGS=-g
1835
1836     For C++, add `--enable-cxx CXXFLAGS=-g'.
1837
1838Checker
1839     The GCC checker (`http://savannah.nongnu.org/projects/checker/')
1840     can be used with GMP.  It contains a stub library which means GMP
1841     applications compiled with checker can use a normal GMP build.
1842
1843     A build of GMP with checking within GMP itself can be made.  This
1844     will run very very slowly.  On GNU/Linux for example,
1845
1846          ./configure --disable-assembly CC=checkergcc
1847
1848     `--disable-assembly' must be used, since the GMP assembly code
1849     doesn't support the checking scheme.  The GMP C++ features cannot
1850     be used, since current versions of checker (0.9.9.1) don't yet
1851     support the standard C++ library.
1852
1853Valgrind
1854     Valgrind (`http://valgrind.org/') is a memory checker for x86,
1855     ARM, MIPS, PowerPC, and S/390.  It translates and emulates machine
1856     instructions to do strong checks for uninitialized data (at the
1857     level of individual bits), memory accesses through bad pointers,
1858     and memory leaks.
1859
1860     Valgrind does not always support every possible instruction, in
1861     particular ones recently added to an ISA.  Valgrind might
1862     therefore be incompatible with a recent GMP or even a less recent
1863     GMP which is compiled using a recent GCC.
1864
1865     GMP's assembly code sometimes promotes a read of the limbs to some
1866     larger size, for efficiency.  GMP will do this even at the start
1867     and end of a multilimb operand, using naturally aligned operations
1868     on the larger type.  This may lead to benign reads outside of
1869     allocated areas, triggering complaints from Valgrind.  Valgrind's
1870     option `--partial-loads-ok=yes' should help.
1871
1872Other Problems
1873     Any suspected bug in GMP itself should be isolated to make sure
1874     it's not an application problem, see *note Reporting Bugs::.
1875
1876
1877File: gmp.info,  Node: Profiling,  Next: Autoconf,  Prev: Debugging,  Up: GMP Basics
1878
18793.13 Profiling
1880==============
1881
1882Running a program under a profiler is a good way to find where it's
1883spending most time and where improvements can be best sought.  The
1884profiling choices for a GMP build are as follows.
1885
1886`--disable-profiling'
1887     The default is to add nothing special for profiling.
1888
1889     It should be possible to just compile the mainline of a program
1890     with `-p' and use `prof' to get a profile consisting of
1891     timer-based sampling of the program counter.  Most of the GMP
1892     assembly code has the necessary symbol information.
1893
1894     This approach has the advantage of minimizing interference with
1895     normal program operation, but on most systems the resolution of
1896     the sampling is quite low (10 milliseconds for instance),
1897     requiring long runs to get accurate information.
1898
1899`--enable-profiling=prof'
1900     Build with support for the system `prof', which means `-p' added
1901     to the `CFLAGS'.
1902
1903     This provides call counting in addition to program counter
1904     sampling, which allows the most frequently called routines to be
1905     identified, and an average time spent in each routine to be
1906     determined.
1907
1908     The x86 assembly code has support for this option, but on other
1909     processors the assembly routines will be as if compiled without
1910     `-p' and therefore won't appear in the call counts.
1911
1912     On some systems, such as GNU/Linux, `-p' in fact means `-pg' and in
1913     this case `--enable-profiling=gprof' described below should be used
1914     instead.
1915
1916`--enable-profiling=gprof'
1917     Build with support for `gprof', which means `-pg' added to the
1918     `CFLAGS'.
1919
1920     This provides call graph construction in addition to call counting
1921     and program counter sampling, which makes it possible to count
1922     calls coming from different locations.  For example the number of
1923     calls to `mpn_mul' from `mpz_mul' versus the number from
1924     `mpf_mul'.  The program counter sampling is still flat though, so
1925     only a total time in `mpn_mul' would be accumulated, not a
1926     separate amount for each call site.
1927
1928     The x86 assembly code has support for this option, but on other
1929     processors the assembly routines will be as if compiled without
1930     `-pg' and therefore not be included in the call counts.
1931
1932     On x86 and m68k systems `-pg' and `-fomit-frame-pointer' are
1933     incompatible, so the latter is omitted from the default flags in
1934     that case, which might result in poorer code generation.
1935
1936     Incidentally, it should be possible to use the `gprof' program
1937     with a plain `--enable-profiling=prof' build.  But in that case
1938     only the `gprof -p' flat profile and call counts can be expected
1939     to be valid, not the `gprof -q' call graph.
1940
1941`--enable-profiling=instrument'
1942     Build with the GCC option `-finstrument-functions' added to the
1943     `CFLAGS' (*note Options for Code Generation: (gcc)Code Gen
1944     Options.).
1945
1946     This inserts special instrumenting calls at the start and end of
1947     each function, allowing exact timing and full call graph
1948     construction.
1949
1950     This instrumenting is not normally a standard system feature and
1951     will require support from an external library, such as
1952
1953          `http://sourceforge.net/projects/fnccheck/'
1954
1955     This should be included in `LIBS' during the GMP configure so that
1956     test programs will link.  For example,
1957
1958          ./configure --enable-profiling=instrument LIBS=-lfc
1959
1960     On a GNU system the C library provides dummy instrumenting
1961     functions, so programs compiled with this option will link.  In
1962     this case it's only necessary to ensure the correct library is
1963     added when linking an application.
1964
1965     The x86 assembly code supports this option, but on other
1966     processors the assembly routines will be as if compiled without
1967     `-finstrument-functions' meaning time spent in them will
1968     effectively be attributed to their caller.
1969
1970
1971File: gmp.info,  Node: Autoconf,  Next: Emacs,  Prev: Profiling,  Up: GMP Basics
1972
19733.14 Autoconf
1974=============
1975
1976Autoconf based applications can easily check whether GMP is installed.
1977The only thing to be noted is that GMP library symbols from version 3
1978onwards have prefixes like `__gmpz'.  The following therefore would be
1979a simple test,
1980
1981     AC_CHECK_LIB(gmp, __gmpz_init)
1982
1983   This just uses the default `AC_CHECK_LIB' actions for found or not
1984found, but an application that must have GMP would want to generate an
1985error if not found.  For example,
1986
1987     AC_CHECK_LIB(gmp, __gmpz_init, ,
1988       [AC_MSG_ERROR([GNU MP not found, see http://gmplib.org/])])
1989
1990   If functions added in some particular version of GMP are required,
1991then one of those can be used when checking.  For example `mpz_mul_si'
1992was added in GMP 3.1,
1993
1994     AC_CHECK_LIB(gmp, __gmpz_mul_si, ,
1995       [AC_MSG_ERROR(
1996       [GNU MP not found, or not 3.1 or up, see http://gmplib.org/])])
1997
1998   An alternative would be to test the version number in `gmp.h' using
1999say `AC_EGREP_CPP'.  That would make it possible to test the exact
2000version, if some particular sub-minor release is known to be necessary.
2001
2002   In general it's recommended that applications should simply demand a
2003new enough GMP rather than trying to provide supplements for features
2004not available in past versions.
2005
2006   Occasionally an application will need or want to know the size of a
2007type at configuration or preprocessing time, not just with `sizeof' in
2008the code.  This can be done in the normal way with `mp_limb_t' etc, but
2009GMP 4.0 or up is best for this, since prior versions needed certain
2010`-D' defines on systems using a `long long' limb.  The following would
2011suit Autoconf 2.50 or up,
2012
2013     AC_CHECK_SIZEOF(mp_limb_t, , [#include <gmp.h>])
2014
2015
2016File: gmp.info,  Node: Emacs,  Prev: Autoconf,  Up: GMP Basics
2017
20183.15 Emacs
2019==========
2020
2021<C-h C-i> (`info-lookup-symbol') is a good way to find documentation on
2022C functions while editing (*note Info Documentation Lookup: (emacs)Info
2023Lookup.).
2024
2025   The GMP manual can be included in such lookups by putting the
2026following in your `.emacs',
2027
2028     (eval-after-load "info-look"
2029       '(let ((mode-value (assoc 'c-mode (assoc 'symbol info-lookup-alist))))
2030          (setcar (nthcdr 3 mode-value)
2031                  (cons '("(gmp)Function Index" nil "^ -.* " "\\>")
2032                        (nth 3 mode-value)))))
2033
2034
2035File: gmp.info,  Node: Reporting Bugs,  Next: Integer Functions,  Prev: GMP Basics,  Up: Top
2036
20374 Reporting Bugs
2038****************
2039
2040If you think you have found a bug in the GMP library, please
2041investigate it and report it.  We have made this library available to
2042you, and it is not too much to ask you to report the bugs you find.
2043
2044   Before you report a bug, check it's not already addressed in *note
2045Known Build Problems::, or perhaps *note Notes for Particular
2046Systems::.  You may also want to check `http://gmplib.org/' for patches
2047for this release.
2048
2049   Please include the following in any report,
2050
2051   * The GMP version number, and if pre-packaged or patched then say so.
2052
2053   * A test program that makes it possible for us to reproduce the bug.
2054     Include instructions on how to run the program.
2055
2056   * A description of what is wrong.  If the results are incorrect, in
2057     what way.  If you get a crash, say so.
2058
2059   * If you get a crash, include a stack backtrace from the debugger if
2060     it's informative (`where' in `gdb', or `$C' in `adb').
2061
2062   * Please do not send core dumps, executables or `strace's.
2063
2064   * The `configure' options you used when building GMP, if any.
2065
2066   * The output from `configure', as printed to stdout, with any
2067     options used.
2068
2069   * The name of the compiler and its version.  For `gcc', get the
2070     version with `gcc -v', otherwise perhaps `what `which cc`', or
2071     similar.
2072
2073   * The output from running `uname -a'.
2074
2075   * The output from running `./config.guess', and from running
2076     `./configfsf.guess' (might be the same).
2077
2078   * If the bug is related to `configure', then the compressed contents
2079     of `config.log'.
2080
2081   * If the bug is related to an `asm' file not assembling, then the
2082     contents of `config.m4' and the offending line or lines from the
2083     temporary `mpn/tmp-<file>.s'.
2084
2085   Please make an effort to produce a self-contained report, with
2086something definite that can be tested or debugged.  Vague queries or
2087piecemeal messages are difficult to act on and don't help the
2088development effort.
2089
2090   It is not uncommon that an observed problem is actually due to a bug
2091in the compiler; the GMP code tends to explore interesting corners in
2092compilers.
2093
2094   If your bug report is good, we will do our best to help you get a
2095corrected version of the library; if the bug report is poor, we won't
2096do anything about it (except maybe ask you to send a better report).
2097
2098   Send your report to: <gmp-bugs@gmplib.org>.
2099
2100   If you think something in this manual is unclear, or downright
2101incorrect, or if the language needs to be improved, please send a note
2102to the same address.
2103
2104
2105File: gmp.info,  Node: Integer Functions,  Next: Rational Number Functions,  Prev: Reporting Bugs,  Up: Top
2106
21075 Integer Functions
2108*******************
2109
2110This chapter describes the GMP functions for performing integer
2111arithmetic.  These functions start with the prefix `mpz_'.
2112
2113   GMP integers are stored in objects of type `mpz_t'.
2114
2115* Menu:
2116
2117* Initializing Integers::
2118* Assigning Integers::
2119* Simultaneous Integer Init & Assign::
2120* Converting Integers::
2121* Integer Arithmetic::
2122* Integer Division::
2123* Integer Exponentiation::
2124* Integer Roots::
2125* Number Theoretic Functions::
2126* Integer Comparisons::
2127* Integer Logic and Bit Fiddling::
2128* I/O of Integers::
2129* Integer Random Numbers::
2130* Integer Import and Export::
2131* Miscellaneous Integer Functions::
2132* Integer Special Functions::
2133
2134
2135File: gmp.info,  Node: Initializing Integers,  Next: Assigning Integers,  Prev: Integer Functions,  Up: Integer Functions
2136
21375.1 Initialization Functions
2138============================
2139
2140The functions for integer arithmetic assume that all integer objects are
2141initialized.  You do that by calling the function `mpz_init'.  For
2142example,
2143
2144     {
2145       mpz_t integ;
2146       mpz_init (integ);
2147       ...
2148       mpz_add (integ, ...);
2149       ...
2150       mpz_sub (integ, ...);
2151
2152       /* Unless the program is about to exit, do ... */
2153       mpz_clear (integ);
2154     }
2155
2156   As you can see, you can store new values any number of times, once an
2157object is initialized.
2158
2159 -- Function: void mpz_init (mpz_t X)
2160     Initialize X, and set its value to 0.
2161
2162 -- Function: void mpz_inits (mpz_t X, ...)
2163     Initialize a NULL-terminated list of `mpz_t' variables, and set
2164     their values to 0.
2165
2166 -- Function: void mpz_init2 (mpz_t X, mp_bitcnt_t N)
2167     Initialize X, with space for N-bit numbers, and set its value to 0.
2168     Calling this function instead of `mpz_init' or `mpz_inits' is never
2169     necessary; reallocation is handled automatically by GMP when
2170     needed.
2171
2172     While N defines the initial space, X will grow automatically in the
2173     normal way, if necessary, for subsequent values stored.
2174     `mpz_init2' makes it possible to avoid such reallocations if a
2175     maximum size is known in advance.
2176
2177     In preparation for an operation, GMP often allocates one limb more
2178     than ultimately needed.  To make sure GMP will not perform
2179     reallocation for X, you need to add the number of bits in
2180     `mp_limb_t' to N.
2181
2182 -- Function: void mpz_clear (mpz_t X)
2183     Free the space occupied by X.  Call this function for all `mpz_t'
2184     variables when you are done with them.
2185
2186 -- Function: void mpz_clears (mpz_t X, ...)
2187     Free the space occupied by a NULL-terminated list of `mpz_t'
2188     variables.
2189
2190 -- Function: void mpz_realloc2 (mpz_t X, mp_bitcnt_t N)
2191     Change the space allocated for X to N bits.  The value in X is
2192     preserved if it fits, or is set to 0 if not.
2193
2194     Calling this function is never necessary; reallocation is handled
2195     automatically by GMP when needed.  But this function can be used
2196     to increase the space for a variable in order to avoid repeated
2197     automatic reallocations, or to decrease it to give memory back to
2198     the heap.
2199
2200
2201File: gmp.info,  Node: Assigning Integers,  Next: Simultaneous Integer Init & Assign,  Prev: Initializing Integers,  Up: Integer Functions
2202
22035.2 Assignment Functions
2204========================
2205
2206These functions assign new values to already initialized integers
2207(*note Initializing Integers::).
2208
2209 -- Function: void mpz_set (mpz_t ROP, const mpz_t OP)
2210 -- Function: void mpz_set_ui (mpz_t ROP, unsigned long int OP)
2211 -- Function: void mpz_set_si (mpz_t ROP, signed long int OP)
2212 -- Function: void mpz_set_d (mpz_t ROP, double OP)
2213 -- Function: void mpz_set_q (mpz_t ROP, const mpq_t OP)
2214 -- Function: void mpz_set_f (mpz_t ROP, const mpf_t OP)
2215     Set the value of ROP from OP.
2216
2217     `mpz_set_d', `mpz_set_q' and `mpz_set_f' truncate OP to make it an
2218     integer.
2219
2220 -- Function: int mpz_set_str (mpz_t ROP, const char *STR, int BASE)
2221     Set the value of ROP from STR, a null-terminated C string in base
2222     BASE.  White space is allowed in the string, and is simply ignored.
2223
2224     The BASE may vary from 2 to 62, or if BASE is 0, then the leading
2225     characters are used: `0x' and `0X' for hexadecimal, `0b' and `0B'
2226     for binary, `0' for octal, or decimal otherwise.
2227
2228     For bases up to 36, case is ignored; upper-case and lower-case
2229     letters have the same value.  For bases 37 to 62, upper-case
2230     letter represent the usual 10..35 while lower-case letter
2231     represent 36..61.
2232
2233     This function returns 0 if the entire string is a valid number in
2234     base BASE.  Otherwise it returns -1.
2235
2236 -- Function: void mpz_swap (mpz_t ROP1, mpz_t ROP2)
2237     Swap the values ROP1 and ROP2 efficiently.
2238
2239
2240File: gmp.info,  Node: Simultaneous Integer Init & Assign,  Next: Converting Integers,  Prev: Assigning Integers,  Up: Integer Functions
2241
22425.3 Combined Initialization and Assignment Functions
2243====================================================
2244
2245For convenience, GMP provides a parallel series of initialize-and-set
2246functions which initialize the output and then store the value there.
2247These functions' names have the form `mpz_init_set...'
2248
2249   Here is an example of using one:
2250
2251     {
2252       mpz_t pie;
2253       mpz_init_set_str (pie, "3141592653589793238462643383279502884", 10);
2254       ...
2255       mpz_sub (pie, ...);
2256       ...
2257       mpz_clear (pie);
2258     }
2259
2260Once the integer has been initialized by any of the `mpz_init_set...'
2261functions, it can be used as the source or destination operand for the
2262ordinary integer functions.  Don't use an initialize-and-set function
2263on a variable already initialized!
2264
2265 -- Function: void mpz_init_set (mpz_t ROP, const mpz_t OP)
2266 -- Function: void mpz_init_set_ui (mpz_t ROP, unsigned long int OP)
2267 -- Function: void mpz_init_set_si (mpz_t ROP, signed long int OP)
2268 -- Function: void mpz_init_set_d (mpz_t ROP, double OP)
2269     Initialize ROP with limb space and set the initial numeric value
2270     from OP.
2271
2272 -- Function: int mpz_init_set_str (mpz_t ROP, const char *STR, int
2273          BASE)
2274     Initialize ROP and set its value like `mpz_set_str' (see its
2275     documentation above for details).
2276
2277     If the string is a correct base BASE number, the function returns
2278     0; if an error occurs it returns -1.  ROP is initialized even if
2279     an error occurs.  (I.e., you have to call `mpz_clear' for it.)
2280
2281
2282File: gmp.info,  Node: Converting Integers,  Next: Integer Arithmetic,  Prev: Simultaneous Integer Init & Assign,  Up: Integer Functions
2283
22845.4 Conversion Functions
2285========================
2286
2287This section describes functions for converting GMP integers to
2288standard C types.  Functions for converting _to_ GMP integers are
2289described in *note Assigning Integers:: and *note I/O of Integers::.
2290
2291 -- Function: unsigned long int mpz_get_ui (const mpz_t OP)
2292     Return the value of OP as an `unsigned long'.
2293
2294     If OP is too big to fit an `unsigned long' then just the least
2295     significant bits that do fit are returned.  The sign of OP is
2296     ignored, only the absolute value is used.
2297
2298 -- Function: signed long int mpz_get_si (const mpz_t OP)
2299     If OP fits into a `signed long int' return the value of OP.
2300     Otherwise return the least significant part of OP, with the same
2301     sign as OP.
2302
2303     If OP is too big to fit in a `signed long int', the returned
2304     result is probably not very useful.  To find out if the value will
2305     fit, use the function `mpz_fits_slong_p'.
2306
2307 -- Function: double mpz_get_d (const mpz_t OP)
2308     Convert OP to a `double', truncating if necessary (i.e. rounding
2309     towards zero).
2310
2311     If the exponent from the conversion is too big, the result is
2312     system dependent.  An infinity is returned where available.  A
2313     hardware overflow trap may or may not occur.
2314
2315 -- Function: double mpz_get_d_2exp (signed long int *EXP, const mpz_t
2316          OP)
2317     Convert OP to a `double', truncating if necessary (i.e. rounding
2318     towards zero), and returning the exponent separately.
2319
2320     The return value is in the range 0.5<=abs(D)<1 and the exponent is
2321     stored to `*EXP'.  D * 2^EXP is the (truncated) OP value.  If OP
2322     is zero, the return is 0.0 and 0 is stored to `*EXP'.
2323
2324     This is similar to the standard C `frexp' function (*note
2325     Normalization Functions: (libc)Normalization Functions.).
2326
2327 -- Function: char * mpz_get_str (char *STR, int BASE, const mpz_t OP)
2328     Convert OP to a string of digits in base BASE.  The base argument
2329     may vary from 2 to 62 or from -2 to -36.
2330
2331     For BASE in the range 2..36, digits and lower-case letters are
2332     used; for -2..-36, digits and upper-case letters are used; for
2333     37..62, digits, upper-case letters, and lower-case letters (in
2334     that significance order) are used.
2335
2336     If STR is `NULL', the result string is allocated using the current
2337     allocation function (*note Custom Allocation::).  The block will be
2338     `strlen(str)+1' bytes, that being exactly enough for the string and
2339     null-terminator.
2340
2341     If STR is not `NULL', it should point to a block of storage large
2342     enough for the result, that being `mpz_sizeinbase (OP, BASE) + 2'.
2343     The two extra bytes are for a possible minus sign, and the
2344     null-terminator.
2345
2346     A pointer to the result string is returned, being either the
2347     allocated block, or the given STR.
2348
2349
2350File: gmp.info,  Node: Integer Arithmetic,  Next: Integer Division,  Prev: Converting Integers,  Up: Integer Functions
2351
23525.5 Arithmetic Functions
2353========================
2354
2355 -- Function: void mpz_add (mpz_t ROP, const mpz_t OP1, const mpz_t OP2)
2356 -- Function: void mpz_add_ui (mpz_t ROP, const mpz_t OP1, unsigned
2357          long int OP2)
2358     Set ROP to OP1 + OP2.
2359
2360 -- Function: void mpz_sub (mpz_t ROP, const mpz_t OP1, const mpz_t OP2)
2361 -- Function: void mpz_sub_ui (mpz_t ROP, const mpz_t OP1, unsigned
2362          long int OP2)
2363 -- Function: void mpz_ui_sub (mpz_t ROP, unsigned long int OP1, const
2364          mpz_t OP2)
2365     Set ROP to OP1 - OP2.
2366
2367 -- Function: void mpz_mul (mpz_t ROP, const mpz_t OP1, const mpz_t OP2)
2368 -- Function: void mpz_mul_si (mpz_t ROP, const mpz_t OP1, long int OP2)
2369 -- Function: void mpz_mul_ui (mpz_t ROP, const mpz_t OP1, unsigned
2370          long int OP2)
2371     Set ROP to OP1 times OP2.
2372
2373 -- Function: void mpz_addmul (mpz_t ROP, const mpz_t OP1, const mpz_t
2374          OP2)
2375 -- Function: void mpz_addmul_ui (mpz_t ROP, const mpz_t OP1, unsigned
2376          long int OP2)
2377     Set ROP to ROP + OP1 times OP2.
2378
2379 -- Function: void mpz_submul (mpz_t ROP, const mpz_t OP1, const mpz_t
2380          OP2)
2381 -- Function: void mpz_submul_ui (mpz_t ROP, const mpz_t OP1, unsigned
2382          long int OP2)
2383     Set ROP to ROP - OP1 times OP2.
2384
2385 -- Function: void mpz_mul_2exp (mpz_t ROP, const mpz_t OP1,
2386          mp_bitcnt_t OP2)
2387     Set ROP to OP1 times 2 raised to OP2.  This operation can also be
2388     defined as a left shift by OP2 bits.
2389
2390 -- Function: void mpz_neg (mpz_t ROP, const mpz_t OP)
2391     Set ROP to -OP.
2392
2393 -- Function: void mpz_abs (mpz_t ROP, const mpz_t OP)
2394     Set ROP to the absolute value of OP.
2395
2396
2397File: gmp.info,  Node: Integer Division,  Next: Integer Exponentiation,  Prev: Integer Arithmetic,  Up: Integer Functions
2398
23995.6 Division Functions
2400======================
2401
2402Division is undefined if the divisor is zero.  Passing a zero divisor
2403to the division or modulo functions (including the modular powering
2404functions `mpz_powm' and `mpz_powm_ui'), will cause an intentional
2405division by zero.  This lets a program handle arithmetic exceptions in
2406these functions the same way as for normal C `int' arithmetic.
2407
2408 -- Function: void mpz_cdiv_q (mpz_t Q, const mpz_t N, const mpz_t D)
2409 -- Function: void mpz_cdiv_r (mpz_t R, const mpz_t N, const mpz_t D)
2410 -- Function: void mpz_cdiv_qr (mpz_t Q, mpz_t R, const mpz_t N, const
2411          mpz_t D)
2412 -- Function: unsigned long int mpz_cdiv_q_ui (mpz_t Q, const mpz_t N,
2413          unsigned long int D)
2414 -- Function: unsigned long int mpz_cdiv_r_ui (mpz_t R, const mpz_t N,
2415          unsigned long int D)
2416 -- Function: unsigned long int mpz_cdiv_qr_ui (mpz_t Q, mpz_t R,
2417          const mpz_t N, unsigned long int D)
2418 -- Function: unsigned long int mpz_cdiv_ui (const mpz_t N,
2419          unsigned long int D)
2420 -- Function: void mpz_cdiv_q_2exp (mpz_t Q, const mpz_t N,
2421          mp_bitcnt_t B)
2422 -- Function: void mpz_cdiv_r_2exp (mpz_t R, const mpz_t N,
2423          mp_bitcnt_t B)
2424
2425 -- Function: void mpz_fdiv_q (mpz_t Q, const mpz_t N, const mpz_t D)
2426 -- Function: void mpz_fdiv_r (mpz_t R, const mpz_t N, const mpz_t D)
2427 -- Function: void mpz_fdiv_qr (mpz_t Q, mpz_t R, const mpz_t N, const
2428          mpz_t D)
2429 -- Function: unsigned long int mpz_fdiv_q_ui (mpz_t Q, const mpz_t N,
2430          unsigned long int D)
2431 -- Function: unsigned long int mpz_fdiv_r_ui (mpz_t R, const mpz_t N,
2432          unsigned long int D)
2433 -- Function: unsigned long int mpz_fdiv_qr_ui (mpz_t Q, mpz_t R,
2434          const mpz_t N, unsigned long int D)
2435 -- Function: unsigned long int mpz_fdiv_ui (const mpz_t N,
2436          unsigned long int D)
2437 -- Function: void mpz_fdiv_q_2exp (mpz_t Q, const mpz_t N,
2438          mp_bitcnt_t B)
2439 -- Function: void mpz_fdiv_r_2exp (mpz_t R, const mpz_t N,
2440          mp_bitcnt_t B)
2441
2442 -- Function: void mpz_tdiv_q (mpz_t Q, const mpz_t N, const mpz_t D)
2443 -- Function: void mpz_tdiv_r (mpz_t R, const mpz_t N, const mpz_t D)
2444 -- Function: void mpz_tdiv_qr (mpz_t Q, mpz_t R, const mpz_t N, const
2445          mpz_t D)
2446 -- Function: unsigned long int mpz_tdiv_q_ui (mpz_t Q, const mpz_t N,
2447          unsigned long int D)
2448 -- Function: unsigned long int mpz_tdiv_r_ui (mpz_t R, const mpz_t N,
2449          unsigned long int D)
2450 -- Function: unsigned long int mpz_tdiv_qr_ui (mpz_t Q, mpz_t R,
2451          const mpz_t N, unsigned long int D)
2452 -- Function: unsigned long int mpz_tdiv_ui (const mpz_t N,
2453          unsigned long int D)
2454 -- Function: void mpz_tdiv_q_2exp (mpz_t Q, const mpz_t N,
2455          mp_bitcnt_t B)
2456 -- Function: void mpz_tdiv_r_2exp (mpz_t R, const mpz_t N,
2457          mp_bitcnt_t B)
2458
2459     Divide N by D, forming a quotient Q and/or remainder R.  For the
2460     `2exp' functions, D=2^B.  The rounding is in three styles, each
2461     suiting different applications.
2462
2463        * `cdiv' rounds Q up towards +infinity, and R will have the
2464          opposite sign to D.  The `c' stands for "ceil".
2465
2466        * `fdiv' rounds Q down towards -infinity, and R will have the
2467          same sign as D.  The `f' stands for "floor".
2468
2469        * `tdiv' rounds Q towards zero, and R will have the same sign
2470          as N.  The `t' stands for "truncate".
2471
2472     In all cases Q and R will satisfy N=Q*D+R, and R will satisfy
2473     0<=abs(R)<abs(D).
2474
2475     The `q' functions calculate only the quotient, the `r' functions
2476     only the remainder, and the `qr' functions calculate both.  Note
2477     that for `qr' the same variable cannot be passed for both Q and R,
2478     or results will be unpredictable.
2479
2480     For the `ui' variants the return value is the remainder, and in
2481     fact returning the remainder is all the `div_ui' functions do.  For
2482     `tdiv' and `cdiv' the remainder can be negative, so for those the
2483     return value is the absolute value of the remainder.
2484
2485     For the `2exp' variants the divisor is 2^B.  These functions are
2486     implemented as right shifts and bit masks, but of course they
2487     round the same as the other functions.
2488
2489     For positive N both `mpz_fdiv_q_2exp' and `mpz_tdiv_q_2exp' are
2490     simple bitwise right shifts.  For negative N, `mpz_fdiv_q_2exp' is
2491     effectively an arithmetic right shift treating N as twos complement
2492     the same as the bitwise logical functions do, whereas
2493     `mpz_tdiv_q_2exp' effectively treats N as sign and magnitude.
2494
2495 -- Function: void mpz_mod (mpz_t R, const mpz_t N, const mpz_t D)
2496 -- Function: unsigned long int mpz_mod_ui (mpz_t R, const mpz_t N,
2497          unsigned long int D)
2498     Set R to N `mod' D.  The sign of the divisor is ignored; the
2499     result is always non-negative.
2500
2501     `mpz_mod_ui' is identical to `mpz_fdiv_r_ui' above, returning the
2502     remainder as well as setting R.  See `mpz_fdiv_ui' above if only
2503     the return value is wanted.
2504
2505 -- Function: void mpz_divexact (mpz_t Q, const mpz_t N, const mpz_t D)
2506 -- Function: void mpz_divexact_ui (mpz_t Q, const mpz_t N, unsigned
2507          long D)
2508     Set Q to N/D.  These functions produce correct results only when
2509     it is known in advance that D divides N.
2510
2511     These routines are much faster than the other division functions,
2512     and are the best choice when exact division is known to occur, for
2513     example reducing a rational to lowest terms.
2514
2515 -- Function: int mpz_divisible_p (const mpz_t N, const mpz_t D)
2516 -- Function: int mpz_divisible_ui_p (const mpz_t N, unsigned long int
2517          D)
2518 -- Function: int mpz_divisible_2exp_p (const mpz_t N, mp_bitcnt_t B)
2519     Return non-zero if N is exactly divisible by D, or in the case of
2520     `mpz_divisible_2exp_p' by 2^B.
2521
2522     N is divisible by D if there exists an integer Q satisfying N =
2523     Q*D.  Unlike the other division functions, D=0 is accepted and
2524     following the rule it can be seen that only 0 is considered
2525     divisible by 0.
2526
2527 -- Function: int mpz_congruent_p (const mpz_t N, const mpz_t C, const
2528          mpz_t D)
2529 -- Function: int mpz_congruent_ui_p (const mpz_t N, unsigned long int
2530          C, unsigned long int D)
2531 -- Function: int mpz_congruent_2exp_p (const mpz_t N, const mpz_t C,
2532          mp_bitcnt_t B)
2533     Return non-zero if N is congruent to C modulo D, or in the case of
2534     `mpz_congruent_2exp_p' modulo 2^B.
2535
2536     N is congruent to C mod D if there exists an integer Q satisfying
2537     N = C + Q*D.  Unlike the other division functions, D=0 is accepted
2538     and following the rule it can be seen that N and C are considered
2539     congruent mod 0 only when exactly equal.
2540
2541
2542File: gmp.info,  Node: Integer Exponentiation,  Next: Integer Roots,  Prev: Integer Division,  Up: Integer Functions
2543
25445.7 Exponentiation Functions
2545============================
2546
2547 -- Function: void mpz_powm (mpz_t ROP, const mpz_t BASE, const mpz_t
2548          EXP, const mpz_t MOD)
2549 -- Function: void mpz_powm_ui (mpz_t ROP, const mpz_t BASE, unsigned
2550          long int EXP, const mpz_t MOD)
2551     Set ROP to (BASE raised to EXP) modulo MOD.
2552
2553     Negative EXP is supported if an inverse BASE^-1 mod MOD exists
2554     (see `mpz_invert' in *note Number Theoretic Functions::).  If an
2555     inverse doesn't exist then a divide by zero is raised.
2556
2557 -- Function: void mpz_powm_sec (mpz_t ROP, const mpz_t BASE, const
2558          mpz_t EXP, const mpz_t MOD)
2559     Set ROP to (BASE raised to EXP) modulo MOD.
2560
2561     It is required that EXP > 0 and that MOD is odd.
2562
2563     This function is designed to take the same time and have the same
2564     cache access patterns for any two same-size arguments, assuming
2565     that function arguments are placed at the same position and that
2566     the machine state is identical upon function entry.  This function
2567     is intended for cryptographic purposes, where resilience to
2568     side-channel attacks is desired.
2569
2570 -- Function: void mpz_pow_ui (mpz_t ROP, const mpz_t BASE, unsigned
2571          long int EXP)
2572 -- Function: void mpz_ui_pow_ui (mpz_t ROP, unsigned long int BASE,
2573          unsigned long int EXP)
2574     Set ROP to BASE raised to EXP.  The case 0^0 yields 1.
2575
2576
2577File: gmp.info,  Node: Integer Roots,  Next: Number Theoretic Functions,  Prev: Integer Exponentiation,  Up: Integer Functions
2578
25795.8 Root Extraction Functions
2580=============================
2581
2582 -- Function: int mpz_root (mpz_t ROP, const mpz_t OP, unsigned long
2583          int N)
2584     Set ROP to  the truncated integer part of the Nth root of OP.
2585     Return non-zero if the computation was exact, i.e., if OP is ROP
2586     to the Nth power.
2587
2588 -- Function: void mpz_rootrem (mpz_t ROOT, mpz_t REM, const mpz_t U,
2589          unsigned long int N)
2590     Set ROOT to  the truncated integer part of the Nth root of U.  Set
2591     REM to the remainder, U-ROOT**N.
2592
2593 -- Function: void mpz_sqrt (mpz_t ROP, const mpz_t OP)
2594     Set ROP to  the truncated integer part of the square root of OP.
2595
2596 -- Function: void mpz_sqrtrem (mpz_t ROP1, mpz_t ROP2, const mpz_t OP)
2597     Set ROP1 to the truncated integer part of the square root of OP,
2598     like `mpz_sqrt'.  Set ROP2 to the remainder OP-ROP1*ROP1, which
2599     will be zero if OP is a perfect square.
2600
2601     If ROP1 and ROP2 are the same variable, the results are undefined.
2602
2603 -- Function: int mpz_perfect_power_p (const mpz_t OP)
2604     Return non-zero if OP is a perfect power, i.e., if there exist
2605     integers A and B, with B>1, such that OP equals A raised to the
2606     power B.
2607
2608     Under this definition both 0 and 1 are considered to be perfect
2609     powers.  Negative values of OP are accepted, but of course can
2610     only be odd perfect powers.
2611
2612 -- Function: int mpz_perfect_square_p (const mpz_t OP)
2613     Return non-zero if OP is a perfect square, i.e., if the square
2614     root of OP is an integer.  Under this definition both 0 and 1 are
2615     considered to be perfect squares.
2616
2617
2618File: gmp.info,  Node: Number Theoretic Functions,  Next: Integer Comparisons,  Prev: Integer Roots,  Up: Integer Functions
2619
26205.9 Number Theoretic Functions
2621==============================
2622
2623 -- Function: int mpz_probab_prime_p (const mpz_t N, int REPS)
2624     Determine whether N is prime.  Return 2 if N is definitely prime,
2625     return 1 if N is probably prime (without being certain), or return
2626     0 if N is definitely composite.
2627
2628     This function does some trial divisions, then some Miller-Rabin
2629     probabilistic primality tests.  The argument REPS controls how
2630     many such tests are done; a higher value will reduce the chances
2631     of a composite being returned as "probably prime".  25 is a
2632     reasonable number; a composite number will then be identified as a
2633     prime with a probability of less than 2^(-50).
2634
2635     Miller-Rabin and similar tests can be more properly called
2636     compositeness tests.  Numbers which fail are known to be composite
2637     but those which pass might be prime or might be composite.  Only a
2638     few composites pass, hence those which pass are considered
2639     probably prime.
2640
2641 -- Function: void mpz_nextprime (mpz_t ROP, const mpz_t OP)
2642     Set ROP to the next prime greater than OP.
2643
2644     This function uses a probabilistic algorithm to identify primes.
2645     For practical purposes it's adequate, the chance of a composite
2646     passing will be extremely small.
2647
2648 -- Function: void mpz_gcd (mpz_t ROP, const mpz_t OP1, const mpz_t OP2)
2649     Set ROP to the greatest common divisor of OP1 and OP2.  The result
2650     is always positive even if one or both input operands are negative.
2651     Except if both inputs are zero; then this function defines
2652     gcd(0,0) = 0.
2653
2654 -- Function: unsigned long int mpz_gcd_ui (mpz_t ROP, const mpz_t OP1,
2655          unsigned long int OP2)
2656     Compute the greatest common divisor of OP1 and OP2.  If ROP is not
2657     `NULL', store the result there.
2658
2659     If the result is small enough to fit in an `unsigned long int', it
2660     is returned.  If the result does not fit, 0 is returned, and the
2661     result is equal to the argument OP1.  Note that the result will
2662     always fit if OP2 is non-zero.
2663
2664 -- Function: void mpz_gcdext (mpz_t G, mpz_t S, mpz_t T, const mpz_t
2665          A, const mpz_t B)
2666     Set G to the greatest common divisor of A and B, and in addition
2667     set S and T to coefficients satisfying A*S + B*T = G.  The value
2668     in G is always positive, even if one or both of A and B are
2669     negative (or zero if both inputs are zero).  The values in S and T
2670     are chosen such that normally, abs(S) < abs(B) / (2 G) and abs(T)
2671     < abs(A) / (2 G), and these relations define S and T uniquely.
2672     There are a few exceptional cases:
2673
2674     If abs(A) = abs(B), then S = 0, T = sgn(B).
2675
2676     Otherwise, S = sgn(A) if B = 0 or abs(B) = 2 G, and T = sgn(B) if
2677     A = 0 or abs(A) = 2 G.
2678
2679     In all cases, S = 0 if and only if G = abs(B), i.e., if B divides
2680     A or A = B = 0.
2681
2682     If T is `NULL' then that value is not computed.
2683
2684 -- Function: void mpz_lcm (mpz_t ROP, const mpz_t OP1, const mpz_t OP2)
2685 -- Function: void mpz_lcm_ui (mpz_t ROP, const mpz_t OP1, unsigned
2686          long OP2)
2687     Set ROP to the least common multiple of OP1 and OP2.  ROP is
2688     always positive, irrespective of the signs of OP1 and OP2.  ROP
2689     will be zero if either OP1 or OP2 is zero.
2690
2691 -- Function: int mpz_invert (mpz_t ROP, const mpz_t OP1, const mpz_t
2692          OP2)
2693     Compute the inverse of OP1 modulo OP2 and put the result in ROP.
2694     If the inverse exists, the return value is non-zero and ROP will
2695     satisfy 0 < ROP < abs(OP2).  If an inverse doesn't exist the
2696     return value is zero and ROP is undefined.  The behaviour of this
2697     function is undefined when OP2 is zero.
2698
2699 -- Function: int mpz_jacobi (const mpz_t A, const mpz_t B)
2700     Calculate the Jacobi symbol (A/B).  This is defined only for B odd.
2701
2702 -- Function: int mpz_legendre (const mpz_t A, const mpz_t P)
2703     Calculate the Legendre symbol (A/P).  This is defined only for P
2704     an odd positive prime, and for such P it's identical to the Jacobi
2705     symbol.
2706
2707 -- Function: int mpz_kronecker (const mpz_t A, const mpz_t B)
2708 -- Function: int mpz_kronecker_si (const mpz_t A, long B)
2709 -- Function: int mpz_kronecker_ui (const mpz_t A, unsigned long B)
2710 -- Function: int mpz_si_kronecker (long A, const mpz_t B)
2711 -- Function: int mpz_ui_kronecker (unsigned long A, const mpz_t B)
2712     Calculate the Jacobi symbol (A/B) with the Kronecker extension
2713     (a/2)=(2/a) when a odd, or (a/2)=0 when a even.
2714
2715     When B is odd the Jacobi symbol and Kronecker symbol are
2716     identical, so `mpz_kronecker_ui' etc can be used for mixed
2717     precision Jacobi symbols too.
2718
2719     For more information see Henri Cohen section 1.4.2 (*note
2720     References::), or any number theory textbook.  See also the
2721     example program `demos/qcn.c' which uses `mpz_kronecker_ui'.
2722
2723 -- Function: mp_bitcnt_t mpz_remove (mpz_t ROP, const mpz_t OP, const
2724          mpz_t F)
2725     Remove all occurrences of the factor F from OP and store the
2726     result in ROP.  The return value is how many such occurrences were
2727     removed.
2728
2729 -- Function: void mpz_fac_ui (mpz_t ROP, unsigned long int N)
2730 -- Function: void mpz_2fac_ui (mpz_t ROP, unsigned long int N)
2731 -- Function: void mpz_mfac_uiui (mpz_t ROP, unsigned long int N,
2732          unsigned long int M)
2733     Set ROP to the factorial of N: `mpz_fac_ui' computes the plain
2734     factorial N!, `mpz_2fac_ui' computes the double-factorial N!!, and
2735     `mpz_mfac_uiui' the M-multi-factorial N!^(M).
2736
2737 -- Function: void mpz_primorial_ui (mpz_t ROP, unsigned long int N)
2738     Set ROP to the primorial of N, i.e. the product of all positive
2739     prime numbers <=N.
2740
2741 -- Function: void mpz_bin_ui (mpz_t ROP, const mpz_t N, unsigned long
2742          int K)
2743 -- Function: void mpz_bin_uiui (mpz_t ROP, unsigned long int N,
2744          unsigned long int K)
2745     Compute the binomial coefficient N over K and store the result in
2746     ROP.  Negative values of N are supported by `mpz_bin_ui', using
2747     the identity bin(-n,k) = (-1)^k * bin(n+k-1,k), see Knuth volume 1
2748     section 1.2.6 part G.
2749
2750 -- Function: void mpz_fib_ui (mpz_t FN, unsigned long int N)
2751 -- Function: void mpz_fib2_ui (mpz_t FN, mpz_t FNSUB1, unsigned long
2752          int N)
2753     `mpz_fib_ui' sets FN to to F[n], the N'th Fibonacci number.
2754     `mpz_fib2_ui' sets FN to F[n], and FNSUB1 to F[n-1].
2755
2756     These functions are designed for calculating isolated Fibonacci
2757     numbers.  When a sequence of values is wanted it's best to start
2758     with `mpz_fib2_ui' and iterate the defining F[n+1]=F[n]+F[n-1] or
2759     similar.
2760
2761 -- Function: void mpz_lucnum_ui (mpz_t LN, unsigned long int N)
2762 -- Function: void mpz_lucnum2_ui (mpz_t LN, mpz_t LNSUB1, unsigned
2763          long int N)
2764     `mpz_lucnum_ui' sets LN to to L[n], the N'th Lucas number.
2765     `mpz_lucnum2_ui' sets LN to L[n], and LNSUB1 to L[n-1].
2766
2767     These functions are designed for calculating isolated Lucas
2768     numbers.  When a sequence of values is wanted it's best to start
2769     with `mpz_lucnum2_ui' and iterate the defining L[n+1]=L[n]+L[n-1]
2770     or similar.
2771
2772     The Fibonacci numbers and Lucas numbers are related sequences, so
2773     it's never necessary to call both `mpz_fib2_ui' and
2774     `mpz_lucnum2_ui'.  The formulas for going from Fibonacci to Lucas
2775     can be found in *note Lucas Numbers Algorithm::, the reverse is
2776     straightforward too.
2777
2778
2779File: gmp.info,  Node: Integer Comparisons,  Next: Integer Logic and Bit Fiddling,  Prev: Number Theoretic Functions,  Up: Integer Functions
2780
27815.10 Comparison Functions
2782=========================
2783
2784 -- Function: int mpz_cmp (const mpz_t OP1, const mpz_t OP2)
2785 -- Function: int mpz_cmp_d (const mpz_t OP1, double OP2)
2786 -- Macro: int mpz_cmp_si (const mpz_t OP1, signed long int OP2)
2787 -- Macro: int mpz_cmp_ui (const mpz_t OP1, unsigned long int OP2)
2788     Compare OP1 and OP2.  Return a positive value if OP1 > OP2, zero
2789     if OP1 = OP2, or a negative value if OP1 < OP2.
2790
2791     `mpz_cmp_ui' and `mpz_cmp_si' are macros and will evaluate their
2792     arguments more than once.  `mpz_cmp_d' can be called with an
2793     infinity, but results are undefined for a NaN.
2794
2795 -- Function: int mpz_cmpabs (const mpz_t OP1, const mpz_t OP2)
2796 -- Function: int mpz_cmpabs_d (const mpz_t OP1, double OP2)
2797 -- Function: int mpz_cmpabs_ui (const mpz_t OP1, unsigned long int OP2)
2798     Compare the absolute values of OP1 and OP2.  Return a positive
2799     value if abs(OP1) > abs(OP2), zero if abs(OP1) = abs(OP2), or a
2800     negative value if abs(OP1) < abs(OP2).
2801
2802     `mpz_cmpabs_d' can be called with an infinity, but results are
2803     undefined for a NaN.
2804
2805 -- Macro: int mpz_sgn (const mpz_t OP)
2806     Return +1 if OP > 0, 0 if OP = 0, and -1 if OP < 0.
2807
2808     This function is actually implemented as a macro.  It evaluates
2809     its argument multiple times.
2810
2811
2812File: gmp.info,  Node: Integer Logic and Bit Fiddling,  Next: I/O of Integers,  Prev: Integer Comparisons,  Up: Integer Functions
2813
28145.11 Logical and Bit Manipulation Functions
2815===========================================
2816
2817These functions behave as if twos complement arithmetic were used
2818(although sign-magnitude is the actual implementation).  The least
2819significant bit is number 0.
2820
2821 -- Function: void mpz_and (mpz_t ROP, const mpz_t OP1, const mpz_t OP2)
2822     Set ROP to OP1 bitwise-and OP2.
2823
2824 -- Function: void mpz_ior (mpz_t ROP, const mpz_t OP1, const mpz_t OP2)
2825     Set ROP to OP1 bitwise inclusive-or OP2.
2826
2827 -- Function: void mpz_xor (mpz_t ROP, const mpz_t OP1, const mpz_t OP2)
2828     Set ROP to OP1 bitwise exclusive-or OP2.
2829
2830 -- Function: void mpz_com (mpz_t ROP, const mpz_t OP)
2831     Set ROP to the one's complement of OP.
2832
2833 -- Function: mp_bitcnt_t mpz_popcount (const mpz_t OP)
2834     If OP>=0, return the population count of OP, which is the number
2835     of 1 bits in the binary representation.  If OP<0, the number of 1s
2836     is infinite, and the return value is the largest possible
2837     `mp_bitcnt_t'.
2838
2839 -- Function: mp_bitcnt_t mpz_hamdist (const mpz_t OP1, const mpz_t OP2)
2840     If OP1 and OP2 are both >=0 or both <0, return the hamming
2841     distance between the two operands, which is the number of bit
2842     positions where OP1 and OP2 have different bit values.  If one
2843     operand is >=0 and the other <0 then the number of bits different
2844     is infinite, and the return value is the largest possible
2845     `mp_bitcnt_t'.
2846
2847 -- Function: mp_bitcnt_t mpz_scan0 (const mpz_t OP, mp_bitcnt_t
2848          STARTING_BIT)
2849 -- Function: mp_bitcnt_t mpz_scan1 (const mpz_t OP, mp_bitcnt_t
2850          STARTING_BIT)
2851     Scan OP, starting from bit STARTING_BIT, towards more significant
2852     bits, until the first 0 or 1 bit (respectively) is found.  Return
2853     the index of the found bit.
2854
2855     If the bit at STARTING_BIT is already what's sought, then
2856     STARTING_BIT is returned.
2857
2858     If there's no bit found, then the largest possible `mp_bitcnt_t' is
2859     returned.  This will happen in `mpz_scan0' past the end of a
2860     negative number, or `mpz_scan1' past the end of a nonnegative
2861     number.
2862
2863 -- Function: void mpz_setbit (mpz_t ROP, mp_bitcnt_t BIT_INDEX)
2864     Set bit BIT_INDEX in ROP.
2865
2866 -- Function: void mpz_clrbit (mpz_t ROP, mp_bitcnt_t BIT_INDEX)
2867     Clear bit BIT_INDEX in ROP.
2868
2869 -- Function: void mpz_combit (mpz_t ROP, mp_bitcnt_t BIT_INDEX)
2870     Complement bit BIT_INDEX in ROP.
2871
2872 -- Function: int mpz_tstbit (const mpz_t OP, mp_bitcnt_t BIT_INDEX)
2873     Test bit BIT_INDEX in OP and return 0 or 1 accordingly.
2874
2875
2876File: gmp.info,  Node: I/O of Integers,  Next: Integer Random Numbers,  Prev: Integer Logic and Bit Fiddling,  Up: Integer Functions
2877
28785.12 Input and Output Functions
2879===============================
2880
2881Functions that perform input from a stdio stream, and functions that
2882output to a stdio stream, of `mpz' numbers.  Passing a `NULL' pointer
2883for a STREAM argument to any of these functions will make them read from
2884`stdin' and write to `stdout', respectively.
2885
2886   When using any of these functions, it is a good idea to include
2887`stdio.h' before `gmp.h', since that will allow `gmp.h' to define
2888prototypes for these functions.
2889
2890   See also *note Formatted Output:: and *note Formatted Input::.
2891
2892 -- Function: size_t mpz_out_str (FILE *STREAM, int BASE, const mpz_t
2893          OP)
2894     Output OP on stdio stream STREAM, as a string of digits in base
2895     BASE.  The base argument may vary from 2 to 62 or from -2 to -36.
2896
2897     For BASE in the range 2..36, digits and lower-case letters are
2898     used; for -2..-36, digits and upper-case letters are used; for
2899     37..62, digits, upper-case letters, and lower-case letters (in
2900     that significance order) are used.
2901
2902     Return the number of bytes written, or if an error occurred,
2903     return 0.
2904
2905 -- Function: size_t mpz_inp_str (mpz_t ROP, FILE *STREAM, int BASE)
2906     Input a possibly white-space preceded string in base BASE from
2907     stdio stream STREAM, and put the read integer in ROP.
2908
2909     The BASE may vary from 2 to 62, or if BASE is 0, then the leading
2910     characters are used: `0x' and `0X' for hexadecimal, `0b' and `0B'
2911     for binary, `0' for octal, or decimal otherwise.
2912
2913     For bases up to 36, case is ignored; upper-case and lower-case
2914     letters have the same value.  For bases 37 to 62, upper-case
2915     letter represent the usual 10..35 while lower-case letter
2916     represent 36..61.
2917
2918     Return the number of bytes read, or if an error occurred, return 0.
2919
2920 -- Function: size_t mpz_out_raw (FILE *STREAM, const mpz_t OP)
2921     Output OP on stdio stream STREAM, in raw binary format.  The
2922     integer is written in a portable format, with 4 bytes of size
2923     information, and that many bytes of limbs.  Both the size and the
2924     limbs are written in decreasing significance order (i.e., in
2925     big-endian).
2926
2927     The output can be read with `mpz_inp_raw'.
2928
2929     Return the number of bytes written, or if an error occurred,
2930     return 0.
2931
2932     The output of this can not be read by `mpz_inp_raw' from GMP 1,
2933     because of changes necessary for compatibility between 32-bit and
2934     64-bit machines.
2935
2936 -- Function: size_t mpz_inp_raw (mpz_t ROP, FILE *STREAM)
2937     Input from stdio stream STREAM in the format written by
2938     `mpz_out_raw', and put the result in ROP.  Return the number of
2939     bytes read, or if an error occurred, return 0.
2940
2941     This routine can read the output from `mpz_out_raw' also from GMP
2942     1, in spite of changes necessary for compatibility between 32-bit
2943     and 64-bit machines.
2944
2945
2946File: gmp.info,  Node: Integer Random Numbers,  Next: Integer Import and Export,  Prev: I/O of Integers,  Up: Integer Functions
2947
29485.13 Random Number Functions
2949============================
2950
2951The random number functions of GMP come in two groups; older function
2952that rely on a global state, and newer functions that accept a state
2953parameter that is read and modified.  Please see the *note Random
2954Number Functions:: for more information on how to use and not to use
2955random number functions.
2956
2957 -- Function: void mpz_urandomb (mpz_t ROP, gmp_randstate_t STATE,
2958          mp_bitcnt_t N)
2959     Generate a uniformly distributed random integer in the range 0 to
2960     2^N-1, inclusive.
2961
2962     The variable STATE must be initialized by calling one of the
2963     `gmp_randinit' functions (*note Random State Initialization::)
2964     before invoking this function.
2965
2966 -- Function: void mpz_urandomm (mpz_t ROP, gmp_randstate_t STATE,
2967          const mpz_t N)
2968     Generate a uniform random integer in the range 0 to N-1, inclusive.
2969
2970     The variable STATE must be initialized by calling one of the
2971     `gmp_randinit' functions (*note Random State Initialization::)
2972     before invoking this function.
2973
2974 -- Function: void mpz_rrandomb (mpz_t ROP, gmp_randstate_t STATE,
2975          mp_bitcnt_t N)
2976     Generate a random integer with long strings of zeros and ones in
2977     the binary representation.  Useful for testing functions and
2978     algorithms, since this kind of random numbers have proven to be
2979     more likely to trigger corner-case bugs.  The random number will
2980     be in the range 0 to 2^N-1, inclusive.
2981
2982     The variable STATE must be initialized by calling one of the
2983     `gmp_randinit' functions (*note Random State Initialization::)
2984     before invoking this function.
2985
2986 -- Function: void mpz_random (mpz_t ROP, mp_size_t MAX_SIZE)
2987     Generate a random integer of at most MAX_SIZE limbs.  The generated
2988     random number doesn't satisfy any particular requirements of
2989     randomness.  Negative random numbers are generated when MAX_SIZE
2990     is negative.
2991
2992     This function is obsolete.  Use `mpz_urandomb' or `mpz_urandomm'
2993     instead.
2994
2995 -- Function: void mpz_random2 (mpz_t ROP, mp_size_t MAX_SIZE)
2996     Generate a random integer of at most MAX_SIZE limbs, with long
2997     strings of zeros and ones in the binary representation.  Useful
2998     for testing functions and algorithms, since this kind of random
2999     numbers have proven to be more likely to trigger corner-case bugs.
3000     Negative random numbers are generated when MAX_SIZE is negative.
3001
3002     This function is obsolete.  Use `mpz_rrandomb' instead.
3003
3004
3005File: gmp.info,  Node: Integer Import and Export,  Next: Miscellaneous Integer Functions,  Prev: Integer Random Numbers,  Up: Integer Functions
3006
30075.14 Integer Import and Export
3008==============================
3009
3010`mpz_t' variables can be converted to and from arbitrary words of binary
3011data with the following functions.
3012
3013 -- Function: void mpz_import (mpz_t ROP, size_t COUNT, int ORDER,
3014          size_t SIZE, int ENDIAN, size_t NAILS, const void *OP)
3015     Set ROP from an array of word data at OP.
3016
3017     The parameters specify the format of the data.  COUNT many words
3018     are read, each SIZE bytes.  ORDER can be 1 for most significant
3019     word first or -1 for least significant first.  Within each word
3020     ENDIAN can be 1 for most significant byte first, -1 for least
3021     significant first, or 0 for the native endianness of the host CPU.
3022     The most significant NAILS bits of each word are skipped, this can
3023     be 0 to use the full words.
3024
3025     There is no sign taken from the data, ROP will simply be a positive
3026     integer.  An application can handle any sign itself, and apply it
3027     for instance with `mpz_neg'.
3028
3029     There are no data alignment restrictions on OP, any address is
3030     allowed.
3031
3032     Here's an example converting an array of `unsigned long' data, most
3033     significant element first, and host byte order within each value.
3034
3035          unsigned long  a[20];
3036          /* Initialize Z and A */
3037          mpz_import (z, 20, 1, sizeof(a[0]), 0, 0, a);
3038
3039     This example assumes the full `sizeof' bytes are used for data in
3040     the given type, which is usually true, and certainly true for
3041     `unsigned long' everywhere we know of.  However on Cray vector
3042     systems it may be noted that `short' and `int' are always stored
3043     in 8 bytes (and with `sizeof' indicating that) but use only 32 or
3044     46 bits.  The NAILS feature can account for this, by passing for
3045     instance `8*sizeof(int)-INT_BIT'.
3046
3047 -- Function: void * mpz_export (void *ROP, size_t *COUNTP, int ORDER,
3048          size_t SIZE, int ENDIAN, size_t NAILS, const mpz_t OP)
3049     Fill ROP with word data from OP.
3050
3051     The parameters specify the format of the data produced.  Each word
3052     will be SIZE bytes and ORDER can be 1 for most significant word
3053     first or -1 for least significant first.  Within each word ENDIAN
3054     can be 1 for most significant byte first, -1 for least significant
3055     first, or 0 for the native endianness of the host CPU.  The most
3056     significant NAILS bits of each word are unused and set to zero,
3057     this can be 0 to produce full words.
3058
3059     The number of words produced is written to `*COUNTP', or COUNTP
3060     can be `NULL' to discard the count.  ROP must have enough space
3061     for the data, or if ROP is `NULL' then a result array of the
3062     necessary size is allocated using the current GMP allocation
3063     function (*note Custom Allocation::).  In either case the return
3064     value is the destination used, either ROP or the allocated block.
3065
3066     If OP is non-zero then the most significant word produced will be
3067     non-zero.  If OP is zero then the count returned will be zero and
3068     nothing written to ROP.  If ROP is `NULL' in this case, no block
3069     is allocated, just `NULL' is returned.
3070
3071     The sign of OP is ignored, just the absolute value is exported.  An
3072     application can use `mpz_sgn' to get the sign and handle it as
3073     desired.  (*note Integer Comparisons::)
3074
3075     There are no data alignment restrictions on ROP, any address is
3076     allowed.
3077
3078     When an application is allocating space itself the required size
3079     can be determined with a calculation like the following.  Since
3080     `mpz_sizeinbase' always returns at least 1, `count' here will be
3081     at least one, which avoids any portability problems with
3082     `malloc(0)', though if `z' is zero no space at all is actually
3083     needed (or written).
3084
3085          numb = 8*size - nail;
3086          count = (mpz_sizeinbase (z, 2) + numb-1) / numb;
3087          p = malloc (count * size);
3088
3089
3090File: gmp.info,  Node: Miscellaneous Integer Functions,  Next: Integer Special Functions,  Prev: Integer Import and Export,  Up: Integer Functions
3091
30925.15 Miscellaneous Functions
3093============================
3094
3095 -- Function: int mpz_fits_ulong_p (const mpz_t OP)
3096 -- Function: int mpz_fits_slong_p (const mpz_t OP)
3097 -- Function: int mpz_fits_uint_p (const mpz_t OP)
3098 -- Function: int mpz_fits_sint_p (const mpz_t OP)
3099 -- Function: int mpz_fits_ushort_p (const mpz_t OP)
3100 -- Function: int mpz_fits_sshort_p (const mpz_t OP)
3101     Return non-zero iff the value of OP fits in an `unsigned long int',
3102     `signed long int', `unsigned int', `signed int', `unsigned short
3103     int', or `signed short int', respectively.  Otherwise, return zero.
3104
3105 -- Macro: int mpz_odd_p (const mpz_t OP)
3106 -- Macro: int mpz_even_p (const mpz_t OP)
3107     Determine whether OP is odd or even, respectively.  Return
3108     non-zero if yes, zero if no.  These macros evaluate their argument
3109     more than once.
3110
3111 -- Function: size_t mpz_sizeinbase (const mpz_t OP, int BASE)
3112     Return the size of OP measured in number of digits in the given
3113     BASE.  BASE can vary from 2 to 62.  The sign of OP is ignored,
3114     just the absolute value is used.  The result will be either exact
3115     or 1 too big.  If BASE is a power of 2, the result is always
3116     exact.  If OP is zero the return value is always 1.
3117
3118     This function can be used to determine the space required when
3119     converting OP to a string.  The right amount of allocation is
3120     normally two more than the value returned by `mpz_sizeinbase', one
3121     extra for a minus sign and one for the null-terminator.
3122
3123     It will be noted that `mpz_sizeinbase(OP,2)' can be used to locate
3124     the most significant 1 bit in OP, counting from 1.  (Unlike the
3125     bitwise functions which start from 0, *Note Logical and Bit
3126     Manipulation Functions: Integer Logic and Bit Fiddling.)
3127
3128
3129File: gmp.info,  Node: Integer Special Functions,  Prev: Miscellaneous Integer Functions,  Up: Integer Functions
3130
31315.16 Special Functions
3132======================
3133
3134The functions in this section are for various special purposes.  Most
3135applications will not need them.
3136
3137 -- Function: void mpz_array_init (mpz_t INTEGER_ARRAY, mp_size_t
3138          ARRAY_SIZE, mp_size_t FIXED_NUM_BITS)
3139     This is a special type of initialization.  *Fixed* space of
3140     FIXED_NUM_BITS is allocated to each of the ARRAY_SIZE integers in
3141     INTEGER_ARRAY.  There is no way to free the storage allocated by
3142     this function.  Don't call `mpz_clear'!
3143
3144     The INTEGER_ARRAY parameter is the first `mpz_t' in the array.  For
3145     example,
3146
3147          mpz_t  arr[20000];
3148          mpz_array_init (arr[0], 20000, 512);
3149
3150     This function is only intended for programs that create a large
3151     number of integers and need to reduce memory usage by avoiding the
3152     overheads of allocating and reallocating lots of small blocks.  In
3153     normal programs this function is not recommended.
3154
3155     The space allocated to each integer by this function will not be
3156     automatically increased, unlike the normal `mpz_init', so an
3157     application must ensure it is sufficient for any value stored.
3158     The following space requirements apply to various routines,
3159
3160        * `mpz_abs', `mpz_neg', `mpz_set', `mpz_set_si' and
3161          `mpz_set_ui' need room for the value they store.
3162
3163        * `mpz_add', `mpz_add_ui', `mpz_sub' and `mpz_sub_ui' need room
3164          for the larger of the two operands, plus an extra
3165          `mp_bits_per_limb'.
3166
3167        * `mpz_mul', `mpz_mul_ui' and `mpz_mul_si' need room for the sum
3168          of the number of bits in their operands, but each rounded up
3169          to a multiple of `mp_bits_per_limb'.
3170
3171        * `mpz_swap' can be used between two array variables, but not
3172          between an array and a normal variable.
3173
3174     For other functions, or if in doubt, the suggestion is to
3175     calculate in a regular `mpz_init' variable and copy the result to
3176     an array variable with `mpz_set'.
3177
3178 -- Function: void * _mpz_realloc (mpz_t INTEGER, mp_size_t NEW_ALLOC)
3179     Change the space for INTEGER to NEW_ALLOC limbs.  The value in
3180     INTEGER is preserved if it fits, or is set to 0 if not.  The return
3181     value is not useful to applications and should be ignored.
3182
3183     `mpz_realloc2' is the preferred way to accomplish allocation
3184     changes like this.  `mpz_realloc2' and `_mpz_realloc' are the same
3185     except that `_mpz_realloc' takes its size in limbs.
3186
3187 -- Function: mp_limb_t mpz_getlimbn (const mpz_t OP, mp_size_t N)
3188     Return limb number N from OP.  The sign of OP is ignored, just the
3189     absolute value is used.  The least significant limb is number 0.
3190
3191     `mpz_size' can be used to find how many limbs make up OP.
3192     `mpz_getlimbn' returns zero if N is outside the range 0 to
3193     `mpz_size(OP)-1'.
3194
3195 -- Function: size_t mpz_size (const mpz_t OP)
3196     Return the size of OP measured in number of limbs.  If OP is zero,
3197     the returned value will be zero.
3198
3199
3200File: gmp.info,  Node: Rational Number Functions,  Next: Floating-point Functions,  Prev: Integer Functions,  Up: Top
3201
32026 Rational Number Functions
3203***************************
3204
3205This chapter describes the GMP functions for performing arithmetic on
3206rational numbers.  These functions start with the prefix `mpq_'.
3207
3208   Rational numbers are stored in objects of type `mpq_t'.
3209
3210   All rational arithmetic functions assume operands have a canonical
3211form, and canonicalize their result.  The canonical from means that the
3212denominator and the numerator have no common factors, and that the
3213denominator is positive.  Zero has the unique representation 0/1.
3214
3215   Pure assignment functions do not canonicalize the assigned variable.
3216It is the responsibility of the user to canonicalize the assigned
3217variable before any arithmetic operations are performed on that
3218variable.
3219
3220 -- Function: void mpq_canonicalize (mpq_t OP)
3221     Remove any factors that are common to the numerator and
3222     denominator of OP, and make the denominator positive.
3223
3224* Menu:
3225
3226* Initializing Rationals::
3227* Rational Conversions::
3228* Rational Arithmetic::
3229* Comparing Rationals::
3230* Applying Integer Functions::
3231* I/O of Rationals::
3232
3233
3234File: gmp.info,  Node: Initializing Rationals,  Next: Rational Conversions,  Prev: Rational Number Functions,  Up: Rational Number Functions
3235
32366.1 Initialization and Assignment Functions
3237===========================================
3238
3239 -- Function: void mpq_init (mpq_t X)
3240     Initialize X and set it to 0/1.  Each variable should normally
3241     only be initialized once, or at least cleared out (using the
3242     function `mpq_clear') between each initialization.
3243
3244 -- Function: void mpq_inits (mpq_t X, ...)
3245     Initialize a NULL-terminated list of `mpq_t' variables, and set
3246     their values to 0/1.
3247
3248 -- Function: void mpq_clear (mpq_t X)
3249     Free the space occupied by X.  Make sure to call this function for
3250     all `mpq_t' variables when you are done with them.
3251
3252 -- Function: void mpq_clears (mpq_t X, ...)
3253     Free the space occupied by a NULL-terminated list of `mpq_t'
3254     variables.
3255
3256 -- Function: void mpq_set (mpq_t ROP, const mpq_t OP)
3257 -- Function: void mpq_set_z (mpq_t ROP, const mpz_t OP)
3258     Assign ROP from OP.
3259
3260 -- Function: void mpq_set_ui (mpq_t ROP, unsigned long int OP1,
3261          unsigned long int OP2)
3262 -- Function: void mpq_set_si (mpq_t ROP, signed long int OP1, unsigned
3263          long int OP2)
3264     Set the value of ROP to OP1/OP2.  Note that if OP1 and OP2 have
3265     common factors, ROP has to be passed to `mpq_canonicalize' before
3266     any operations are performed on ROP.
3267
3268 -- Function: int mpq_set_str (mpq_t ROP, const char *STR, int BASE)
3269     Set ROP from a null-terminated string STR in the given BASE.
3270
3271     The string can be an integer like "41" or a fraction like
3272     "41/152".  The fraction must be in canonical form (*note Rational
3273     Number Functions::), or if not then `mpq_canonicalize' must be
3274     called.
3275
3276     The numerator and optional denominator are parsed the same as in
3277     `mpz_set_str' (*note Assigning Integers::).  White space is
3278     allowed in the string, and is simply ignored.  The BASE can vary
3279     from 2 to 62, or if BASE is 0 then the leading characters are
3280     used: `0x' or `0X' for hex, `0b' or `0B' for binary, `0' for
3281     octal, or decimal otherwise.  Note that this is done separately
3282     for the numerator and denominator, so for instance `0xEF/100' is
3283     239/100, whereas `0xEF/0x100' is 239/256.
3284
3285     The return value is 0 if the entire string is a valid number, or
3286     -1 if not.
3287
3288 -- Function: void mpq_swap (mpq_t ROP1, mpq_t ROP2)
3289     Swap the values ROP1 and ROP2 efficiently.
3290
3291
3292File: gmp.info,  Node: Rational Conversions,  Next: Rational Arithmetic,  Prev: Initializing Rationals,  Up: Rational Number Functions
3293
32946.2 Conversion Functions
3295========================
3296
3297 -- Function: double mpq_get_d (const mpq_t OP)
3298     Convert OP to a `double', truncating if necessary (i.e. rounding
3299     towards zero).
3300
3301     If the exponent from the conversion is too big or too small to fit
3302     a `double' then the result is system dependent.  For too big an
3303     infinity is returned when available.  For too small 0.0 is
3304     normally returned.  Hardware overflow, underflow and denorm traps
3305     may or may not occur.
3306
3307 -- Function: void mpq_set_d (mpq_t ROP, double OP)
3308 -- Function: void mpq_set_f (mpq_t ROP, const mpf_t OP)
3309     Set ROP to the value of OP.  There is no rounding, this conversion
3310     is exact.
3311
3312 -- Function: char * mpq_get_str (char *STR, int BASE, const mpq_t OP)
3313     Convert OP to a string of digits in base BASE.  The base may vary
3314     from 2 to 36.  The string will be of the form `num/den', or if the
3315     denominator is 1 then just `num'.
3316
3317     If STR is `NULL', the result string is allocated using the current
3318     allocation function (*note Custom Allocation::).  The block will be
3319     `strlen(str)+1' bytes, that being exactly enough for the string and
3320     null-terminator.
3321
3322     If STR is not `NULL', it should point to a block of storage large
3323     enough for the result, that being
3324
3325          mpz_sizeinbase (mpq_numref(OP), BASE)
3326          + mpz_sizeinbase (mpq_denref(OP), BASE) + 3
3327
3328     The three extra bytes are for a possible minus sign, possible
3329     slash, and the null-terminator.
3330
3331     A pointer to the result string is returned, being either the
3332     allocated block, or the given STR.
3333
3334
3335File: gmp.info,  Node: Rational Arithmetic,  Next: Comparing Rationals,  Prev: Rational Conversions,  Up: Rational Number Functions
3336
33376.3 Arithmetic Functions
3338========================
3339
3340 -- Function: void mpq_add (mpq_t SUM, const mpq_t ADDEND1, const mpq_t
3341          ADDEND2)
3342     Set SUM to ADDEND1 + ADDEND2.
3343
3344 -- Function: void mpq_sub (mpq_t DIFFERENCE, const mpq_t MINUEND,
3345          const mpq_t SUBTRAHEND)
3346     Set DIFFERENCE to MINUEND - SUBTRAHEND.
3347
3348 -- Function: void mpq_mul (mpq_t PRODUCT, const mpq_t MULTIPLIER,
3349          const mpq_t MULTIPLICAND)
3350     Set PRODUCT to MULTIPLIER times MULTIPLICAND.
3351
3352 -- Function: void mpq_mul_2exp (mpq_t ROP, const mpq_t OP1,
3353          mp_bitcnt_t OP2)
3354     Set ROP to OP1 times 2 raised to OP2.
3355
3356 -- Function: void mpq_div (mpq_t QUOTIENT, const mpq_t DIVIDEND, const
3357          mpq_t DIVISOR)
3358     Set QUOTIENT to DIVIDEND/DIVISOR.
3359
3360 -- Function: void mpq_div_2exp (mpq_t ROP, const mpq_t OP1,
3361          mp_bitcnt_t OP2)
3362     Set ROP to OP1 divided by 2 raised to OP2.
3363
3364 -- Function: void mpq_neg (mpq_t NEGATED_OPERAND, const mpq_t OPERAND)
3365     Set NEGATED_OPERAND to -OPERAND.
3366
3367 -- Function: void mpq_abs (mpq_t ROP, const mpq_t OP)
3368     Set ROP to the absolute value of OP.
3369
3370 -- Function: void mpq_inv (mpq_t INVERTED_NUMBER, const mpq_t NUMBER)
3371     Set INVERTED_NUMBER to 1/NUMBER.  If the new denominator is zero,
3372     this routine will divide by zero.
3373
3374
3375File: gmp.info,  Node: Comparing Rationals,  Next: Applying Integer Functions,  Prev: Rational Arithmetic,  Up: Rational Number Functions
3376
33776.4 Comparison Functions
3378========================
3379
3380 -- Function: int mpq_cmp (const mpq_t OP1, const mpq_t OP2)
3381     Compare OP1 and OP2.  Return a positive value if OP1 > OP2, zero
3382     if OP1 = OP2, and a negative value if OP1 < OP2.
3383
3384     To determine if two rationals are equal, `mpq_equal' is faster than
3385     `mpq_cmp'.
3386
3387 -- Macro: int mpq_cmp_ui (const mpq_t OP1, unsigned long int NUM2,
3388          unsigned long int DEN2)
3389 -- Macro: int mpq_cmp_si (const mpq_t OP1, long int NUM2, unsigned
3390          long int DEN2)
3391     Compare OP1 and NUM2/DEN2.  Return a positive value if OP1 >
3392     NUM2/DEN2, zero if OP1 = NUM2/DEN2, and a negative value if OP1 <
3393     NUM2/DEN2.
3394
3395     NUM2 and DEN2 are allowed to have common factors.
3396
3397     These functions are implemented as a macros and evaluate their
3398     arguments multiple times.
3399
3400 -- Macro: int mpq_sgn (const mpq_t OP)
3401     Return +1 if OP > 0, 0 if OP = 0, and -1 if OP < 0.
3402
3403     This function is actually implemented as a macro.  It evaluates its
3404     argument multiple times.
3405
3406 -- Function: int mpq_equal (const mpq_t OP1, const mpq_t OP2)
3407     Return non-zero if OP1 and OP2 are equal, zero if they are
3408     non-equal.  Although `mpq_cmp' can be used for the same purpose,
3409     this function is much faster.
3410
3411
3412File: gmp.info,  Node: Applying Integer Functions,  Next: I/O of Rationals,  Prev: Comparing Rationals,  Up: Rational Number Functions
3413
34146.5 Applying Integer Functions to Rationals
3415===========================================
3416
3417The set of `mpq' functions is quite small.  In particular, there are few
3418functions for either input or output.  The following functions give
3419direct access to the numerator and denominator of an `mpq_t'.
3420
3421   Note that if an assignment to the numerator and/or denominator could
3422take an `mpq_t' out of the canonical form described at the start of
3423this chapter (*note Rational Number Functions::) then
3424`mpq_canonicalize' must be called before any other `mpq' functions are
3425applied to that `mpq_t'.
3426
3427 -- Macro: mpz_t mpq_numref (const mpq_t OP)
3428 -- Macro: mpz_t mpq_denref (const mpq_t OP)
3429     Return a reference to the numerator and denominator of OP,
3430     respectively.  The `mpz' functions can be used on the result of
3431     these macros.
3432
3433 -- Function: void mpq_get_num (mpz_t NUMERATOR, const mpq_t RATIONAL)
3434 -- Function: void mpq_get_den (mpz_t DENOMINATOR, const mpq_t RATIONAL)
3435 -- Function: void mpq_set_num (mpq_t RATIONAL, const mpz_t NUMERATOR)
3436 -- Function: void mpq_set_den (mpq_t RATIONAL, const mpz_t DENOMINATOR)
3437     Get or set the numerator or denominator of a rational.  These
3438     functions are equivalent to calling `mpz_set' with an appropriate
3439     `mpq_numref' or `mpq_denref'.  Direct use of `mpq_numref' or
3440     `mpq_denref' is recommended instead of these functions.
3441
3442
3443File: gmp.info,  Node: I/O of Rationals,  Prev: Applying Integer Functions,  Up: Rational Number Functions
3444
34456.6 Input and Output Functions
3446==============================
3447
3448Functions that perform input from a stdio stream, and functions that
3449output to a stdio stream, of `mpq' numbers.  Passing a `NULL' pointer
3450for a STREAM argument to any of these functions will make them read from
3451`stdin' and write to `stdout', respectively.
3452
3453   When using any of these functions, it is a good idea to include
3454`stdio.h' before `gmp.h', since that will allow `gmp.h' to define
3455prototypes for these functions.
3456
3457   See also *note Formatted Output:: and *note Formatted Input::.
3458
3459 -- Function: size_t mpq_out_str (FILE *STREAM, int BASE, const mpq_t
3460          OP)
3461     Output OP on stdio stream STREAM, as a string of digits in base
3462     BASE.  The base may vary from 2 to 36.  Output is in the form
3463     `num/den' or if the denominator is 1 then just `num'.
3464
3465     Return the number of bytes written, or if an error occurred,
3466     return 0.
3467
3468 -- Function: size_t mpq_inp_str (mpq_t ROP, FILE *STREAM, int BASE)
3469     Read a string of digits from STREAM and convert them to a rational
3470     in ROP.  Any initial white-space characters are read and
3471     discarded.  Return the number of characters read (including white
3472     space), or 0 if a rational could not be read.
3473
3474     The input can be a fraction like `17/63' or just an integer like
3475     `123'.  Reading stops at the first character not in this form, and
3476     white space is not permitted within the string.  If the input
3477     might not be in canonical form, then `mpq_canonicalize' must be
3478     called (*note Rational Number Functions::).
3479
3480     The BASE can be between 2 and 36, or can be 0 in which case the
3481     leading characters of the string determine the base, `0x' or `0X'
3482     for hexadecimal, `0' for octal, or decimal otherwise.  The leading
3483     characters are examined separately for the numerator and
3484     denominator of a fraction, so for instance `0x10/11' is 16/11,
3485     whereas `0x10/0x11' is 16/17.
3486
3487
3488File: gmp.info,  Node: Floating-point Functions,  Next: Low-level Functions,  Prev: Rational Number Functions,  Up: Top
3489
34907 Floating-point Functions
3491**************************
3492
3493GMP floating point numbers are stored in objects of type `mpf_t' and
3494functions operating on them have an `mpf_' prefix.
3495
3496   The mantissa of each float has a user-selectable precision, limited
3497only by available memory.  Each variable has its own precision, and
3498that can be increased or decreased at any time.
3499
3500   The exponent of each float is a fixed precision, one machine word on
3501most systems.  In the current implementation the exponent is a count of
3502limbs, so for example on a 32-bit system this means a range of roughly
35032^-68719476768 to 2^68719476736, or on a 64-bit system this will be
3504greater.  Note however `mpf_get_str' can only return an exponent which
3505fits an `mp_exp_t' and currently `mpf_set_str' doesn't accept exponents
3506bigger than a `long'.
3507
3508   Each variable keeps a size for the mantissa data actually in use.
3509This means that if a float is exactly represented in only a few bits
3510then only those bits will be used in a calculation, even if the
3511selected precision is high.
3512
3513   All calculations are performed to the precision of the destination
3514variable.  Each function is defined to calculate with "infinite
3515precision" followed by a truncation to the destination precision, but
3516of course the work done is only what's needed to determine a result
3517under that definition.
3518
3519   The precision selected for a variable is a minimum value, GMP may
3520increase it a little to facilitate efficient calculation.  Currently
3521this means rounding up to a whole limb, and then sometimes having a
3522further partial limb, depending on the high limb of the mantissa.  But
3523applications shouldn't be concerned by such details.
3524
3525   The mantissa in stored in binary, as might be imagined from the fact
3526precisions are expressed in bits.  One consequence of this is that
3527decimal fractions like 0.1 cannot be represented exactly.  The same is
3528true of plain IEEE `double' floats.  This makes both highly unsuitable
3529for calculations involving money or other values that should be exact
3530decimal fractions.  (Suitably scaled integers, or perhaps rationals,
3531are better choices.)
3532
3533   `mpf' functions and variables have no special notion of infinity or
3534not-a-number, and applications must take care not to overflow the
3535exponent or results will be unpredictable.  This might change in a
3536future release.
3537
3538   Note that the `mpf' functions are _not_ intended as a smooth
3539extension to IEEE P754 arithmetic.  In particular results obtained on
3540one computer often differ from the results on a computer with a
3541different word size.
3542
3543* Menu:
3544
3545* Initializing Floats::
3546* Assigning Floats::
3547* Simultaneous Float Init & Assign::
3548* Converting Floats::
3549* Float Arithmetic::
3550* Float Comparison::
3551* I/O of Floats::
3552* Miscellaneous Float Functions::
3553
3554
3555File: gmp.info,  Node: Initializing Floats,  Next: Assigning Floats,  Prev: Floating-point Functions,  Up: Floating-point Functions
3556
35577.1 Initialization Functions
3558============================
3559
3560 -- Function: void mpf_set_default_prec (mp_bitcnt_t PREC)
3561     Set the default precision to be *at least* PREC bits.  All
3562     subsequent calls to `mpf_init' will use this precision, but
3563     previously initialized variables are unaffected.
3564
3565 -- Function: mp_bitcnt_t mpf_get_default_prec (void)
3566     Return the default precision actually used.
3567
3568   An `mpf_t' object must be initialized before storing the first value
3569in it.  The functions `mpf_init' and `mpf_init2' are used for that
3570purpose.
3571
3572 -- Function: void mpf_init (mpf_t X)
3573     Initialize X to 0.  Normally, a variable should be initialized
3574     once only or at least be cleared, using `mpf_clear', between
3575     initializations.  The precision of X is undefined unless a default
3576     precision has already been established by a call to
3577     `mpf_set_default_prec'.
3578
3579 -- Function: void mpf_init2 (mpf_t X, mp_bitcnt_t PREC)
3580     Initialize X to 0 and set its precision to be *at least* PREC
3581     bits.  Normally, a variable should be initialized once only or at
3582     least be cleared, using `mpf_clear', between initializations.
3583
3584 -- Function: void mpf_inits (mpf_t X, ...)
3585     Initialize a NULL-terminated list of `mpf_t' variables, and set
3586     their values to 0.  The precision of the initialized variables is
3587     undefined unless a default precision has already been established
3588     by a call to `mpf_set_default_prec'.
3589
3590 -- Function: void mpf_clear (mpf_t X)
3591     Free the space occupied by X.  Make sure to call this function for
3592     all `mpf_t' variables when you are done with them.
3593
3594 -- Function: void mpf_clears (mpf_t X, ...)
3595     Free the space occupied by a NULL-terminated list of `mpf_t'
3596     variables.
3597
3598   Here is an example on how to initialize floating-point variables:
3599     {
3600       mpf_t x, y;
3601       mpf_init (x);           /* use default precision */
3602       mpf_init2 (y, 256);     /* precision _at least_ 256 bits */
3603       ...
3604       /* Unless the program is about to exit, do ... */
3605       mpf_clear (x);
3606       mpf_clear (y);
3607     }
3608
3609   The following three functions are useful for changing the precision
3610during a calculation.  A typical use would be for adjusting the
3611precision gradually in iterative algorithms like Newton-Raphson, making
3612the computation precision closely match the actual accurate part of the
3613numbers.
3614
3615 -- Function: mp_bitcnt_t mpf_get_prec (const mpf_t OP)
3616     Return the current precision of OP, in bits.
3617
3618 -- Function: void mpf_set_prec (mpf_t ROP, mp_bitcnt_t PREC)
3619     Set the precision of ROP to be *at least* PREC bits.  The value in
3620     ROP will be truncated to the new precision.
3621
3622     This function requires a call to `realloc', and so should not be
3623     used in a tight loop.
3624
3625 -- Function: void mpf_set_prec_raw (mpf_t ROP, mp_bitcnt_t PREC)
3626     Set the precision of ROP to be *at least* PREC bits, without
3627     changing the memory allocated.
3628
3629     PREC must be no more than the allocated precision for ROP, that
3630     being the precision when ROP was initialized, or in the most recent
3631     `mpf_set_prec'.
3632
3633     The value in ROP is unchanged, and in particular if it had a higher
3634     precision than PREC it will retain that higher precision.  New
3635     values written to ROP will use the new PREC.
3636
3637     Before calling `mpf_clear' or the full `mpf_set_prec', another
3638     `mpf_set_prec_raw' call must be made to restore ROP to its original
3639     allocated precision.  Failing to do so will have unpredictable
3640     results.
3641
3642     `mpf_get_prec' can be used before `mpf_set_prec_raw' to get the
3643     original allocated precision.  After `mpf_set_prec_raw' it
3644     reflects the PREC value set.
3645
3646     `mpf_set_prec_raw' is an efficient way to use an `mpf_t' variable
3647     at different precisions during a calculation, perhaps to gradually
3648     increase precision in an iteration, or just to use various
3649     different precisions for different purposes during a calculation.
3650
3651
3652File: gmp.info,  Node: Assigning Floats,  Next: Simultaneous Float Init & Assign,  Prev: Initializing Floats,  Up: Floating-point Functions
3653
36547.2 Assignment Functions
3655========================
3656
3657These functions assign new values to already initialized floats (*note
3658Initializing Floats::).
3659
3660 -- Function: void mpf_set (mpf_t ROP, const mpf_t OP)
3661 -- Function: void mpf_set_ui (mpf_t ROP, unsigned long int OP)
3662 -- Function: void mpf_set_si (mpf_t ROP, signed long int OP)
3663 -- Function: void mpf_set_d (mpf_t ROP, double OP)
3664 -- Function: void mpf_set_z (mpf_t ROP, const mpz_t OP)
3665 -- Function: void mpf_set_q (mpf_t ROP, const mpq_t OP)
3666     Set the value of ROP from OP.
3667
3668 -- Function: int mpf_set_str (mpf_t ROP, const char *STR, int BASE)
3669     Set the value of ROP from the string in STR.  The string is of the
3670     form `M@N' or, if the base is 10 or less, alternatively `MeN'.
3671     `M' is the mantissa and `N' is the exponent.  The mantissa is
3672     always in the specified base.  The exponent is either in the
3673     specified base or, if BASE is negative, in decimal.  The decimal
3674     point expected is taken from the current locale, on systems
3675     providing `localeconv'.
3676
3677     The argument BASE may be in the ranges 2 to 62, or -62 to -2.
3678     Negative values are used to specify that the exponent is in
3679     decimal.
3680
3681     For bases up to 36, case is ignored; upper-case and lower-case
3682     letters have the same value; for bases 37 to 62, upper-case letter
3683     represent the usual 10..35 while lower-case letter represent
3684     36..61.
3685
3686     Unlike the corresponding `mpz' function, the base will not be
3687     determined from the leading characters of the string if BASE is 0.
3688     This is so that numbers like `0.23' are not interpreted as octal.
3689
3690     White space is allowed in the string, and is simply ignored.
3691     [This is not really true; white-space is ignored in the beginning
3692     of the string and within the mantissa, but not in other places,
3693     such as after a minus sign or in the exponent.  We are considering
3694     changing the definition of this function, making it fail when
3695     there is any white-space in the input, since that makes a lot of
3696     sense.  Please tell us your opinion about this change.  Do you
3697     really want it to accept "3 14" as meaning 314 as it does now?]
3698
3699     This function returns 0 if the entire string is a valid number in
3700     base BASE.  Otherwise it returns -1.
3701
3702 -- Function: void mpf_swap (mpf_t ROP1, mpf_t ROP2)
3703     Swap ROP1 and ROP2 efficiently.  Both the values and the
3704     precisions of the two variables are swapped.
3705
3706
3707File: gmp.info,  Node: Simultaneous Float Init & Assign,  Next: Converting Floats,  Prev: Assigning Floats,  Up: Floating-point Functions
3708
37097.3 Combined Initialization and Assignment Functions
3710====================================================
3711
3712For convenience, GMP provides a parallel series of initialize-and-set
3713functions which initialize the output and then store the value there.
3714These functions' names have the form `mpf_init_set...'
3715
3716   Once the float has been initialized by any of the `mpf_init_set...'
3717functions, it can be used as the source or destination operand for the
3718ordinary float functions.  Don't use an initialize-and-set function on
3719a variable already initialized!
3720
3721 -- Function: void mpf_init_set (mpf_t ROP, const mpf_t OP)
3722 -- Function: void mpf_init_set_ui (mpf_t ROP, unsigned long int OP)
3723 -- Function: void mpf_init_set_si (mpf_t ROP, signed long int OP)
3724 -- Function: void mpf_init_set_d (mpf_t ROP, double OP)
3725     Initialize ROP and set its value from OP.
3726
3727     The precision of ROP will be taken from the active default
3728     precision, as set by `mpf_set_default_prec'.
3729
3730 -- Function: int mpf_init_set_str (mpf_t ROP, const char *STR, int
3731          BASE)
3732     Initialize ROP and set its value from the string in STR.  See
3733     `mpf_set_str' above for details on the assignment operation.
3734
3735     Note that ROP is initialized even if an error occurs.  (I.e., you
3736     have to call `mpf_clear' for it.)
3737
3738     The precision of ROP will be taken from the active default
3739     precision, as set by `mpf_set_default_prec'.
3740
3741
3742File: gmp.info,  Node: Converting Floats,  Next: Float Arithmetic,  Prev: Simultaneous Float Init & Assign,  Up: Floating-point Functions
3743
37447.4 Conversion Functions
3745========================
3746
3747 -- Function: double mpf_get_d (const mpf_t OP)
3748     Convert OP to a `double', truncating if necessary (i.e. rounding
3749     towards zero).
3750
3751     If the exponent in OP is too big or too small to fit a `double'
3752     then the result is system dependent.  For too big an infinity is
3753     returned when available.  For too small 0.0 is normally returned.
3754     Hardware overflow, underflow and denorm traps may or may not occur.
3755
3756 -- Function: double mpf_get_d_2exp (signed long int *EXP, const mpf_t
3757          OP)
3758     Convert OP to a `double', truncating if necessary (i.e. rounding
3759     towards zero), and with an exponent returned separately.
3760
3761     The return value is in the range 0.5<=abs(D)<1 and the exponent is
3762     stored to `*EXP'.  D * 2^EXP is the (truncated) OP value.  If OP
3763     is zero, the return is 0.0 and 0 is stored to `*EXP'.
3764
3765     This is similar to the standard C `frexp' function (*note
3766     Normalization Functions: (libc)Normalization Functions.).
3767
3768 -- Function: long mpf_get_si (const mpf_t OP)
3769 -- Function: unsigned long mpf_get_ui (const mpf_t OP)
3770     Convert OP to a `long' or `unsigned long', truncating any fraction
3771     part.  If OP is too big for the return type, the result is
3772     undefined.
3773
3774     See also `mpf_fits_slong_p' and `mpf_fits_ulong_p' (*note
3775     Miscellaneous Float Functions::).
3776
3777 -- Function: char * mpf_get_str (char *STR, mp_exp_t *EXPPTR, int
3778          BASE, size_t N_DIGITS, const mpf_t OP)
3779     Convert OP to a string of digits in base BASE.  The base argument
3780     may vary from 2 to 62 or from -2 to -36.  Up to N_DIGITS digits
3781     will be generated.  Trailing zeros are not returned.  No more
3782     digits than can be accurately represented by OP are ever
3783     generated.  If N_DIGITS is 0 then that accurate maximum number of
3784     digits are generated.
3785
3786     For BASE in the range 2..36, digits and lower-case letters are
3787     used; for -2..-36, digits and upper-case letters are used; for
3788     37..62, digits, upper-case letters, and lower-case letters (in
3789     that significance order) are used.
3790
3791     If STR is `NULL', the result string is allocated using the current
3792     allocation function (*note Custom Allocation::).  The block will be
3793     `strlen(str)+1' bytes, that being exactly enough for the string and
3794     null-terminator.
3795
3796     If STR is not `NULL', it should point to a block of N_DIGITS + 2
3797     bytes, that being enough for the mantissa, a possible minus sign,
3798     and a null-terminator.  When N_DIGITS is 0 to get all significant
3799     digits, an application won't be able to know the space required,
3800     and STR should be `NULL' in that case.
3801
3802     The generated string is a fraction, with an implicit radix point
3803     immediately to the left of the first digit.  The applicable
3804     exponent is written through the EXPPTR pointer.  For example, the
3805     number 3.1416 would be returned as string "31416" and exponent 1.
3806
3807     When OP is zero, an empty string is produced and the exponent
3808     returned is 0.
3809
3810     A pointer to the result string is returned, being either the
3811     allocated block or the given STR.
3812
3813
3814File: gmp.info,  Node: Float Arithmetic,  Next: Float Comparison,  Prev: Converting Floats,  Up: Floating-point Functions
3815
38167.5 Arithmetic Functions
3817========================
3818
3819 -- Function: void mpf_add (mpf_t ROP, const mpf_t OP1, const mpf_t OP2)
3820 -- Function: void mpf_add_ui (mpf_t ROP, const mpf_t OP1, unsigned
3821          long int OP2)
3822     Set ROP to OP1 + OP2.
3823
3824 -- Function: void mpf_sub (mpf_t ROP, const mpf_t OP1, const mpf_t OP2)
3825 -- Function: void mpf_ui_sub (mpf_t ROP, unsigned long int OP1, const
3826          mpf_t OP2)
3827 -- Function: void mpf_sub_ui (mpf_t ROP, const mpf_t OP1, unsigned
3828          long int OP2)
3829     Set ROP to OP1 - OP2.
3830
3831 -- Function: void mpf_mul (mpf_t ROP, const mpf_t OP1, const mpf_t OP2)
3832 -- Function: void mpf_mul_ui (mpf_t ROP, const mpf_t OP1, unsigned
3833          long int OP2)
3834     Set ROP to OP1 times OP2.
3835
3836   Division is undefined if the divisor is zero, and passing a zero
3837divisor to the divide functions will make these functions intentionally
3838divide by zero.  This lets the user handle arithmetic exceptions in
3839these functions in the same manner as other arithmetic exceptions.
3840
3841 -- Function: void mpf_div (mpf_t ROP, const mpf_t OP1, const mpf_t OP2)
3842 -- Function: void mpf_ui_div (mpf_t ROP, unsigned long int OP1, const
3843          mpf_t OP2)
3844 -- Function: void mpf_div_ui (mpf_t ROP, const mpf_t OP1, unsigned
3845          long int OP2)
3846     Set ROP to OP1/OP2.
3847
3848 -- Function: void mpf_sqrt (mpf_t ROP, const mpf_t OP)
3849 -- Function: void mpf_sqrt_ui (mpf_t ROP, unsigned long int OP)
3850     Set ROP to the square root of OP.
3851
3852 -- Function: void mpf_pow_ui (mpf_t ROP, const mpf_t OP1, unsigned
3853          long int OP2)
3854     Set ROP to OP1 raised to the power OP2.
3855
3856 -- Function: void mpf_neg (mpf_t ROP, const mpf_t OP)
3857     Set ROP to -OP.
3858
3859 -- Function: void mpf_abs (mpf_t ROP, const mpf_t OP)
3860     Set ROP to the absolute value of OP.
3861
3862 -- Function: void mpf_mul_2exp (mpf_t ROP, const mpf_t OP1,
3863          mp_bitcnt_t OP2)
3864     Set ROP to OP1 times 2 raised to OP2.
3865
3866 -- Function: void mpf_div_2exp (mpf_t ROP, const mpf_t OP1,
3867          mp_bitcnt_t OP2)
3868     Set ROP to OP1 divided by 2 raised to OP2.
3869
3870
3871File: gmp.info,  Node: Float Comparison,  Next: I/O of Floats,  Prev: Float Arithmetic,  Up: Floating-point Functions
3872
38737.6 Comparison Functions
3874========================
3875
3876 -- Function: int mpf_cmp (const mpf_t OP1, const mpf_t OP2)
3877 -- Function: int mpf_cmp_d (const mpf_t OP1, double OP2)
3878 -- Function: int mpf_cmp_ui (const mpf_t OP1, unsigned long int OP2)
3879 -- Function: int mpf_cmp_si (const mpf_t OP1, signed long int OP2)
3880     Compare OP1 and OP2.  Return a positive value if OP1 > OP2, zero
3881     if OP1 = OP2, and a negative value if OP1 < OP2.
3882
3883     `mpf_cmp_d' can be called with an infinity, but results are
3884     undefined for a NaN.
3885
3886 -- Function: int mpf_eq (const mpf_t OP1, const mpf_t OP2, mp_bitcnt_t
3887          op3)
3888     Return non-zero if the first OP3 bits of OP1 and OP2 are equal,
3889     zero otherwise.  I.e., test if OP1 and OP2 are approximately equal.
3890
3891     Caution 1: All version of GMP up to version 4.2.4 compared just
3892     whole limbs, meaning sometimes more than OP3 bits, sometimes fewer.
3893
3894     Caution 2: This function will consider XXX11...111 and XX100...000
3895     different, even if ... is replaced by a semi-infinite number of
3896     bits.  Such numbers are really just one ulp off, and should be
3897     considered equal.
3898
3899 -- Function: void mpf_reldiff (mpf_t ROP, const mpf_t OP1, const mpf_t
3900          OP2)
3901     Compute the relative difference between OP1 and OP2 and store the
3902     result in ROP.  This is abs(OP1-OP2)/OP1.
3903
3904 -- Macro: int mpf_sgn (const mpf_t OP)
3905     Return +1 if OP > 0, 0 if OP = 0, and -1 if OP < 0.
3906
3907     This function is actually implemented as a macro.  It evaluates
3908     its argument multiple times.
3909
3910
3911File: gmp.info,  Node: I/O of Floats,  Next: Miscellaneous Float Functions,  Prev: Float Comparison,  Up: Floating-point Functions
3912
39137.7 Input and Output Functions
3914==============================
3915
3916Functions that perform input from a stdio stream, and functions that
3917output to a stdio stream, of `mpf' numbers.  Passing a `NULL' pointer
3918for a STREAM argument to any of these functions will make them read from
3919`stdin' and write to `stdout', respectively.
3920
3921   When using any of these functions, it is a good idea to include
3922`stdio.h' before `gmp.h', since that will allow `gmp.h' to define
3923prototypes for these functions.
3924
3925   See also *note Formatted Output:: and *note Formatted Input::.
3926
3927 -- Function: size_t mpf_out_str (FILE *STREAM, int BASE, size_t
3928          N_DIGITS, const mpf_t OP)
3929     Print OP to STREAM, as a string of digits.  Return the number of
3930     bytes written, or if an error occurred, return 0.
3931
3932     The mantissa is prefixed with an `0.' and is in the given BASE,
3933     which may vary from 2 to 62 or from -2 to -36.  An exponent is
3934     then printed, separated by an `e', or if the base is greater than
3935     10 then by an `@'.  The exponent is always in decimal.  The
3936     decimal point follows the current locale, on systems providing
3937     `localeconv'.
3938
3939     For BASE in the range 2..36, digits and lower-case letters are
3940     used; for -2..-36, digits and upper-case letters are used; for
3941     37..62, digits, upper-case letters, and lower-case letters (in
3942     that significance order) are used.
3943
3944     Up to N_DIGITS will be printed from the mantissa, except that no
3945     more digits than are accurately representable by OP will be
3946     printed.  N_DIGITS can be 0 to select that accurate maximum.
3947
3948 -- Function: size_t mpf_inp_str (mpf_t ROP, FILE *STREAM, int BASE)
3949     Read a string in base BASE from STREAM, and put the read float in
3950     ROP.  The string is of the form `M@N' or, if the base is 10 or
3951     less, alternatively `MeN'.  `M' is the mantissa and `N' is the
3952     exponent.  The mantissa is always in the specified base.  The
3953     exponent is either in the specified base or, if BASE is negative,
3954     in decimal.  The decimal point expected is taken from the current
3955     locale, on systems providing `localeconv'.
3956
3957     The argument BASE may be in the ranges 2 to 36, or -36 to -2.
3958     Negative values are used to specify that the exponent is in
3959     decimal.
3960
3961     Unlike the corresponding `mpz' function, the base will not be
3962     determined from the leading characters of the string if BASE is 0.
3963     This is so that numbers like `0.23' are not interpreted as octal.
3964
3965     Return the number of bytes read, or if an error occurred, return 0.
3966
3967
3968File: gmp.info,  Node: Miscellaneous Float Functions,  Prev: I/O of Floats,  Up: Floating-point Functions
3969
39707.8 Miscellaneous Functions
3971===========================
3972
3973 -- Function: void mpf_ceil (mpf_t ROP, const mpf_t OP)
3974 -- Function: void mpf_floor (mpf_t ROP, const mpf_t OP)
3975 -- Function: void mpf_trunc (mpf_t ROP, const mpf_t OP)
3976     Set ROP to OP rounded to an integer.  `mpf_ceil' rounds to the
3977     next higher integer, `mpf_floor' to the next lower, and `mpf_trunc'
3978     to the integer towards zero.
3979
3980 -- Function: int mpf_integer_p (const mpf_t OP)
3981     Return non-zero if OP is an integer.
3982
3983 -- Function: int mpf_fits_ulong_p (const mpf_t OP)
3984 -- Function: int mpf_fits_slong_p (const mpf_t OP)
3985 -- Function: int mpf_fits_uint_p (const mpf_t OP)
3986 -- Function: int mpf_fits_sint_p (const mpf_t OP)
3987 -- Function: int mpf_fits_ushort_p (const mpf_t OP)
3988 -- Function: int mpf_fits_sshort_p (const mpf_t OP)
3989     Return non-zero if OP would fit in the respective C data type, when
3990     truncated to an integer.
3991
3992 -- Function: void mpf_urandomb (mpf_t ROP, gmp_randstate_t STATE,
3993          mp_bitcnt_t NBITS)
3994     Generate a uniformly distributed random float in ROP, such that 0
3995     <= ROP < 1, with NBITS significant bits in the mantissa or less if
3996     the precision of ROP is smaller.
3997
3998     The variable STATE must be initialized by calling one of the
3999     `gmp_randinit' functions (*note Random State Initialization::)
4000     before invoking this function.
4001
4002 -- Function: void mpf_random2 (mpf_t ROP, mp_size_t MAX_SIZE, mp_exp_t
4003          EXP)
4004     Generate a random float of at most MAX_SIZE limbs, with long
4005     strings of zeros and ones in the binary representation.  The
4006     exponent of the number is in the interval -EXP to EXP (in limbs).
4007     This function is useful for testing functions and algorithms,
4008     since these kind of random numbers have proven to be more likely
4009     to trigger corner-case bugs.  Negative random numbers are
4010     generated when MAX_SIZE is negative.
4011
4012
4013File: gmp.info,  Node: Low-level Functions,  Next: Random Number Functions,  Prev: Floating-point Functions,  Up: Top
4014
40158 Low-level Functions
4016*********************
4017
4018This chapter describes low-level GMP functions, used to implement the
4019high-level GMP functions, but also intended for time-critical user code.
4020
4021   These functions start with the prefix `mpn_'.
4022
4023   The `mpn' functions are designed to be as fast as possible, *not* to
4024provide a coherent calling interface.  The different functions have
4025somewhat similar interfaces, but there are variations that make them
4026hard to use.  These functions do as little as possible apart from the
4027real multiple precision computation, so that no time is spent on things
4028that not all callers need.
4029
4030   A source operand is specified by a pointer to the least significant
4031limb and a limb count.  A destination operand is specified by just a
4032pointer.  It is the responsibility of the caller to ensure that the
4033destination has enough space for storing the result.
4034
4035   With this way of specifying operands, it is possible to perform
4036computations on subranges of an argument, and store the result into a
4037subrange of a destination.
4038
4039   A common requirement for all functions is that each source area
4040needs at least one limb.  No size argument may be zero.  Unless
4041otherwise stated, in-place operations are allowed where source and
4042destination are the same, but not where they only partly overlap.
4043
4044   The `mpn' functions are the base for the implementation of the
4045`mpz_', `mpf_', and `mpq_' functions.
4046
4047   This example adds the number beginning at S1P and the number
4048beginning at S2P and writes the sum at DESTP.  All areas have N limbs.
4049
4050     cy = mpn_add_n (destp, s1p, s2p, n)
4051
4052   It should be noted that the `mpn' functions make no attempt to
4053identify high or low zero limbs on their operands, or other special
4054forms.  On random data such cases will be unlikely and it'd be wasteful
4055for every function to check every time.  An application knowing
4056something about its data can take steps to trim or perhaps split its
4057calculations.
4058
4059
4060In the notation used below, a source operand is identified by the
4061pointer to the least significant limb, and the limb count in braces.
4062For example, {S1P, S1N}.
4063
4064 -- Function: mp_limb_t mpn_add_n (mp_limb_t *RP, const mp_limb_t *S1P,
4065          const mp_limb_t *S2P, mp_size_t N)
4066     Add {S1P, N} and {S2P, N}, and write the N least significant limbs
4067     of the result to RP.  Return carry, either 0 or 1.
4068
4069     This is the lowest-level function for addition.  It is the
4070     preferred function for addition, since it is written in assembly
4071     for most CPUs.  For addition of a variable to itself (i.e., S1P
4072     equals S2P) use `mpn_lshift' with a count of 1 for optimal speed.
4073
4074 -- Function: mp_limb_t mpn_add_1 (mp_limb_t *RP, const mp_limb_t *S1P,
4075          mp_size_t N, mp_limb_t S2LIMB)
4076     Add {S1P, N} and S2LIMB, and write the N least significant limbs
4077     of the result to RP.  Return carry, either 0 or 1.
4078
4079 -- Function: mp_limb_t mpn_add (mp_limb_t *RP, const mp_limb_t *S1P,
4080          mp_size_t S1N, const mp_limb_t *S2P, mp_size_t S2N)
4081     Add {S1P, S1N} and {S2P, S2N}, and write the S1N least significant
4082     limbs of the result to RP.  Return carry, either 0 or 1.
4083
4084     This function requires that S1N is greater than or equal to S2N.
4085
4086 -- Function: mp_limb_t mpn_sub_n (mp_limb_t *RP, const mp_limb_t *S1P,
4087          const mp_limb_t *S2P, mp_size_t N)
4088     Subtract {S2P, N} from {S1P, N}, and write the N least significant
4089     limbs of the result to RP.  Return borrow, either 0 or 1.
4090
4091     This is the lowest-level function for subtraction.  It is the
4092     preferred function for subtraction, since it is written in
4093     assembly for most CPUs.
4094
4095 -- Function: mp_limb_t mpn_sub_1 (mp_limb_t *RP, const mp_limb_t *S1P,
4096          mp_size_t N, mp_limb_t S2LIMB)
4097     Subtract S2LIMB from {S1P, N}, and write the N least significant
4098     limbs of the result to RP.  Return borrow, either 0 or 1.
4099
4100 -- Function: mp_limb_t mpn_sub (mp_limb_t *RP, const mp_limb_t *S1P,
4101          mp_size_t S1N, const mp_limb_t *S2P, mp_size_t S2N)
4102     Subtract {S2P, S2N} from {S1P, S1N}, and write the S1N least
4103     significant limbs of the result to RP.  Return borrow, either 0 or
4104     1.
4105
4106     This function requires that S1N is greater than or equal to S2N.
4107
4108 -- Function: mp_limb_t mpn_neg (mp_limb_t *RP, const mp_limb_t *SP,
4109          mp_size_t N)
4110     Perform the negation of {SP, N}, and write the result to {RP, N}.
4111     Return carry-out.
4112
4113 -- Function: void mpn_mul_n (mp_limb_t *RP, const mp_limb_t *S1P,
4114          const mp_limb_t *S2P, mp_size_t N)
4115     Multiply {S1P, N} and {S2P, N}, and write the 2*N-limb result to
4116     RP.
4117
4118     The destination has to have space for 2*N limbs, even if the
4119     product's most significant limb is zero.  No overlap is permitted
4120     between the destination and either source.
4121
4122     If the two input operands are the same, use `mpn_sqr'.
4123
4124 -- Function: mp_limb_t mpn_mul (mp_limb_t *RP, const mp_limb_t *S1P,
4125          mp_size_t S1N, const mp_limb_t *S2P, mp_size_t S2N)
4126     Multiply {S1P, S1N} and {S2P, S2N}, and write the (S1N+S2N)-limb
4127     result to RP.  Return the most significant limb of the result.
4128
4129     The destination has to have space for S1N + S2N limbs, even if the
4130     product's most significant limb is zero.  No overlap is permitted
4131     between the destination and either source.
4132
4133     This function requires that S1N is greater than or equal to S2N.
4134
4135 -- Function: void mpn_sqr (mp_limb_t *RP, const mp_limb_t *S1P,
4136          mp_size_t N)
4137     Compute the square of {S1P, N} and write the 2*N-limb result to RP.
4138
4139     The destination has to have space for 2*N limbs, even if the
4140     result's most significant limb is zero.  No overlap is permitted
4141     between the destination and the source.
4142
4143 -- Function: mp_limb_t mpn_mul_1 (mp_limb_t *RP, const mp_limb_t *S1P,
4144          mp_size_t N, mp_limb_t S2LIMB)
4145     Multiply {S1P, N} by S2LIMB, and write the N least significant
4146     limbs of the product to RP.  Return the most significant limb of
4147     the product.  {S1P, N} and {RP, N} are allowed to overlap provided
4148     RP <= S1P.
4149
4150     This is a low-level function that is a building block for general
4151     multiplication as well as other operations in GMP.  It is written
4152     in assembly for most CPUs.
4153
4154     Don't call this function if S2LIMB is a power of 2; use
4155     `mpn_lshift' with a count equal to the logarithm of S2LIMB
4156     instead, for optimal speed.
4157
4158 -- Function: mp_limb_t mpn_addmul_1 (mp_limb_t *RP, const mp_limb_t
4159          *S1P, mp_size_t N, mp_limb_t S2LIMB)
4160     Multiply {S1P, N} and S2LIMB, and add the N least significant
4161     limbs of the product to {RP, N} and write the result to RP.
4162     Return the most significant limb of the product, plus carry-out
4163     from the addition.
4164
4165     This is a low-level function that is a building block for general
4166     multiplication as well as other operations in GMP.  It is written
4167     in assembly for most CPUs.
4168
4169 -- Function: mp_limb_t mpn_submul_1 (mp_limb_t *RP, const mp_limb_t
4170          *S1P, mp_size_t N, mp_limb_t S2LIMB)
4171     Multiply {S1P, N} and S2LIMB, and subtract the N least significant
4172     limbs of the product from {RP, N} and write the result to RP.
4173     Return the most significant limb of the product, plus borrow-out
4174     from the subtraction.
4175
4176     This is a low-level function that is a building block for general
4177     multiplication and division as well as other operations in GMP.
4178     It is written in assembly for most CPUs.
4179
4180 -- Function: void mpn_tdiv_qr (mp_limb_t *QP, mp_limb_t *RP, mp_size_t
4181          QXN, const mp_limb_t *NP, mp_size_t NN, const mp_limb_t *DP,
4182          mp_size_t DN)
4183     Divide {NP, NN} by {DP, DN} and put the quotient at {QP, NN-DN+1}
4184     and the remainder at {RP, DN}.  The quotient is rounded towards 0.
4185
4186     No overlap is permitted between arguments, except that NP might
4187     equal RP.  The dividend size NN must be greater than or equal to
4188     divisor size DN.  The most significant limb of the divisor must be
4189     non-zero.  The QXN operand must be zero.
4190
4191 -- Function: mp_limb_t mpn_divrem (mp_limb_t *R1P, mp_size_t QXN,
4192          mp_limb_t *RS2P, mp_size_t RS2N, const mp_limb_t *S3P,
4193          mp_size_t S3N)
4194     [This function is obsolete.  Please call `mpn_tdiv_qr' instead for
4195     best performance.]
4196
4197     Divide {RS2P, RS2N} by {S3P, S3N}, and write the quotient at R1P,
4198     with the exception of the most significant limb, which is
4199     returned.  The remainder replaces the dividend at RS2P; it will be
4200     S3N limbs long (i.e., as many limbs as the divisor).
4201
4202     In addition to an integer quotient, QXN fraction limbs are
4203     developed, and stored after the integral limbs.  For most usages,
4204     QXN will be zero.
4205
4206     It is required that RS2N is greater than or equal to S3N.  It is
4207     required that the most significant bit of the divisor is set.
4208
4209     If the quotient is not needed, pass RS2P + S3N as R1P.  Aside from
4210     that special case, no overlap between arguments is permitted.
4211
4212     Return the most significant limb of the quotient, either 0 or 1.
4213
4214     The area at R1P needs to be RS2N - S3N + QXN limbs large.
4215
4216 -- Function: mp_limb_t mpn_divrem_1 (mp_limb_t *R1P, mp_size_t QXN,
4217          mp_limb_t *S2P, mp_size_t S2N, mp_limb_t S3LIMB)
4218 -- Macro: mp_limb_t mpn_divmod_1 (mp_limb_t *R1P, mp_limb_t *S2P,
4219          mp_size_t S2N, mp_limb_t S3LIMB)
4220     Divide {S2P, S2N} by S3LIMB, and write the quotient at R1P.
4221     Return the remainder.
4222
4223     The integer quotient is written to {R1P+QXN, S2N} and in addition
4224     QXN fraction limbs are developed and written to {R1P, QXN}.
4225     Either or both S2N and QXN can be zero.  For most usages, QXN will
4226     be zero.
4227
4228     `mpn_divmod_1' exists for upward source compatibility and is
4229     simply a macro calling `mpn_divrem_1' with a QXN of 0.
4230
4231     The areas at R1P and S2P have to be identical or completely
4232     separate, not partially overlapping.
4233
4234 -- Function: mp_limb_t mpn_divmod (mp_limb_t *R1P, mp_limb_t *RS2P,
4235          mp_size_t RS2N, const mp_limb_t *S3P, mp_size_t S3N)
4236     [This function is obsolete.  Please call `mpn_tdiv_qr' instead for
4237     best performance.]
4238
4239 -- Macro: mp_limb_t mpn_divexact_by3 (mp_limb_t *RP, mp_limb_t *SP,
4240          mp_size_t N)
4241 -- Function: mp_limb_t mpn_divexact_by3c (mp_limb_t *RP, mp_limb_t
4242          *SP, mp_size_t N, mp_limb_t CARRY)
4243     Divide {SP, N} by 3, expecting it to divide exactly, and writing
4244     the result to {RP, N}.  If 3 divides exactly, the return value is
4245     zero and the result is the quotient.  If not, the return value is
4246     non-zero and the result won't be anything useful.
4247
4248     `mpn_divexact_by3c' takes an initial carry parameter, which can be
4249     the return value from a previous call, so a large calculation can
4250     be done piece by piece from low to high.  `mpn_divexact_by3' is
4251     simply a macro calling `mpn_divexact_by3c' with a 0 carry
4252     parameter.
4253
4254     These routines use a multiply-by-inverse and will be faster than
4255     `mpn_divrem_1' on CPUs with fast multiplication but slow division.
4256
4257     The source a, result q, size n, initial carry i, and return value
4258     c satisfy c*b^n + a-i = 3*q, where b=2^GMP_NUMB_BITS.  The return
4259     c is always 0, 1 or 2, and the initial carry i must also be 0, 1
4260     or 2 (these are both borrows really).  When c=0 clearly q=(a-i)/3.
4261     When c!=0, the remainder (a-i) mod 3 is given by 3-c, because b ==
4262     1 mod 3 (when `mp_bits_per_limb' is even, which is always so
4263     currently).
4264
4265 -- Function: mp_limb_t mpn_mod_1 (const mp_limb_t *S1P, mp_size_t S1N,
4266          mp_limb_t S2LIMB)
4267     Divide {S1P, S1N} by S2LIMB, and return the remainder.  S1N can be
4268     zero.
4269
4270 -- Function: mp_limb_t mpn_lshift (mp_limb_t *RP, const mp_limb_t *SP,
4271          mp_size_t N, unsigned int COUNT)
4272     Shift {SP, N} left by COUNT bits, and write the result to {RP, N}.
4273     The bits shifted out at the left are returned in the least
4274     significant COUNT bits of the return value (the rest of the return
4275     value is zero).
4276
4277     COUNT must be in the range 1 to mp_bits_per_limb-1.  The regions
4278     {SP, N} and {RP, N} may overlap, provided RP >= SP.
4279
4280     This function is written in assembly for most CPUs.
4281
4282 -- Function: mp_limb_t mpn_rshift (mp_limb_t *RP, const mp_limb_t *SP,
4283          mp_size_t N, unsigned int COUNT)
4284     Shift {SP, N} right by COUNT bits, and write the result to {RP,
4285     N}.  The bits shifted out at the right are returned in the most
4286     significant COUNT bits of the return value (the rest of the return
4287     value is zero).
4288
4289     COUNT must be in the range 1 to mp_bits_per_limb-1.  The regions
4290     {SP, N} and {RP, N} may overlap, provided RP <= SP.
4291
4292     This function is written in assembly for most CPUs.
4293
4294 -- Function: int mpn_cmp (const mp_limb_t *S1P, const mp_limb_t *S2P,
4295          mp_size_t N)
4296     Compare {S1P, N} and {S2P, N} and return a positive value if S1 >
4297     S2, 0 if they are equal, or a negative value if S1 < S2.
4298
4299 -- Function: mp_size_t mpn_gcd (mp_limb_t *RP, mp_limb_t *XP,
4300          mp_size_t XN, mp_limb_t *YP, mp_size_t YN)
4301     Set {RP, RETVAL} to the greatest common divisor of {XP, XN} and
4302     {YP, YN}.  The result can be up to YN limbs, the return value is
4303     the actual number produced.  Both source operands are destroyed.
4304
4305     It is required that XN >= YN > 0, and the most significant limb of
4306     {YP, YN} must be non-zero.  No overlap is permitted between {XP,
4307     XN} and {YP, YN}.
4308
4309 -- Function: mp_limb_t mpn_gcd_1 (const mp_limb_t *XP, mp_size_t XN,
4310          mp_limb_t YLIMB)
4311     Return the greatest common divisor of {XP, XN} and YLIMB.  Both
4312     operands must be non-zero.
4313
4314 -- Function: mp_size_t mpn_gcdext (mp_limb_t *GP, mp_limb_t *SP,
4315          mp_size_t *SN, mp_limb_t *UP, mp_size_t UN, mp_limb_t *VP,
4316          mp_size_t VN)
4317     Let U be defined by {UP, UN} and let V be defined by {VP, VN}.
4318
4319     Compute the greatest common divisor G of U and V.  Compute a
4320     cofactor S such that G = US + VT.  The second cofactor T is not
4321     computed but can easily be obtained from (G - U*S) / V (the
4322     division will be exact).  It is required that UN >= VN > 0, and
4323     the most significant limb of {VP, VN} must be non-zero.
4324
4325     S satisfies S = 1 or abs(S) < V / (2 G). S = 0 if and only if V
4326     divides U (i.e., G = V).
4327
4328     Store G at GP and let the return value define its limb count.
4329     Store S at SP and let |*SN| define its limb count.  S can be
4330     negative; when this happens *SN will be negative.  The area at GP
4331     should have room for VN limbs and the area at SP should have room
4332     for VN+1 limbs.
4333
4334     Both source operands are destroyed.
4335
4336     Compatibility notes: GMP 4.3.0 and 4.3.1 defined S less strictly.
4337     Earlier as well as later GMP releases define S as described here.
4338     GMP releases before GMP 4.3.0 required additional space for both
4339     input and output areas. More precisely, the areas {UP, UN+1} and
4340     {VP, VN+1} were destroyed (i.e. the operands plus an extra limb
4341     past the end of each), and the areas pointed to by GP and SP
4342     should each have room for UN+1 limbs.
4343
4344 -- Function: mp_size_t mpn_sqrtrem (mp_limb_t *R1P, mp_limb_t *R2P,
4345          const mp_limb_t *SP, mp_size_t N)
4346     Compute the square root of {SP, N} and put the result at {R1P,
4347     ceil(N/2)} and the remainder at {R2P, RETVAL}.  R2P needs space
4348     for N limbs, but the return value indicates how many are produced.
4349
4350     The most significant limb of {SP, N} must be non-zero.  The areas
4351     {R1P, ceil(N/2)} and {SP, N} must be completely separate.  The
4352     areas {R2P, N} and {SP, N} must be either identical or completely
4353     separate.
4354
4355     If the remainder is not wanted then R2P can be `NULL', and in this
4356     case the return value is zero or non-zero according to whether the
4357     remainder would have been zero or non-zero.
4358
4359     A return value of zero indicates a perfect square.  See also
4360     `mpn_perfect_square_p'.
4361
4362 -- Function: mp_size_t mpn_get_str (unsigned char *STR, int BASE,
4363          mp_limb_t *S1P, mp_size_t S1N)
4364     Convert {S1P, S1N} to a raw unsigned char array at STR in base
4365     BASE, and return the number of characters produced.  There may be
4366     leading zeros in the string.  The string is not in ASCII; to
4367     convert it to printable format, add the ASCII codes for `0' or
4368     `A', depending on the base and range.  BASE can vary from 2 to 256.
4369
4370     The most significant limb of the input {S1P, S1N} must be
4371     non-zero.  The input {S1P, S1N} is clobbered, except when BASE is
4372     a power of 2, in which case it's unchanged.
4373
4374     The area at STR has to have space for the largest possible number
4375     represented by a S1N long limb array, plus one extra character.
4376
4377 -- Function: mp_size_t mpn_set_str (mp_limb_t *RP, const unsigned char
4378          *STR, size_t STRSIZE, int BASE)
4379     Convert bytes {STR,STRSIZE} in the given BASE to limbs at RP.
4380
4381     STR[0] is the most significant byte and STR[STRSIZE-1] is the
4382     least significant.  Each byte should be a value in the range 0 to
4383     BASE-1, not an ASCII character.  BASE can vary from 2 to 256.
4384
4385     The return value is the number of limbs written to RP.  If the most
4386     significant input byte is non-zero then the high limb at RP will be
4387     non-zero, and only that exact number of limbs will be required
4388     there.
4389
4390     If the most significant input byte is zero then there may be high
4391     zero limbs written to RP and included in the return value.
4392
4393     STRSIZE must be at least 1, and no overlap is permitted between
4394     {STR,STRSIZE} and the result at RP.
4395
4396 -- Function: mp_bitcnt_t mpn_scan0 (const mp_limb_t *S1P, mp_bitcnt_t
4397          BIT)
4398     Scan S1P from bit position BIT for the next clear bit.
4399
4400     It is required that there be a clear bit within the area at S1P at
4401     or beyond bit position BIT, so that the function has something to
4402     return.
4403
4404 -- Function: mp_bitcnt_t mpn_scan1 (const mp_limb_t *S1P, mp_bitcnt_t
4405          BIT)
4406     Scan S1P from bit position BIT for the next set bit.
4407
4408     It is required that there be a set bit within the area at S1P at or
4409     beyond bit position BIT, so that the function has something to
4410     return.
4411
4412 -- Function: void mpn_random (mp_limb_t *R1P, mp_size_t R1N)
4413 -- Function: void mpn_random2 (mp_limb_t *R1P, mp_size_t R1N)
4414     Generate a random number of length R1N and store it at R1P.  The
4415     most significant limb is always non-zero.  `mpn_random' generates
4416     uniformly distributed limb data, `mpn_random2' generates long
4417     strings of zeros and ones in the binary representation.
4418
4419     `mpn_random2' is intended for testing the correctness of the `mpn'
4420     routines.
4421
4422 -- Function: mp_bitcnt_t mpn_popcount (const mp_limb_t *S1P, mp_size_t
4423          N)
4424     Count the number of set bits in {S1P, N}.
4425
4426 -- Function: mp_bitcnt_t mpn_hamdist (const mp_limb_t *S1P, const
4427          mp_limb_t *S2P, mp_size_t N)
4428     Compute the hamming distance between {S1P, N} and {S2P, N}, which
4429     is the number of bit positions where the two operands have
4430     different bit values.
4431
4432 -- Function: int mpn_perfect_square_p (const mp_limb_t *S1P, mp_size_t
4433          N)
4434     Return non-zero iff {S1P, N} is a perfect square.  The most
4435     significant limb of the input {S1P, N} must be non-zero.
4436
4437 -- Function: void mpn_and_n (mp_limb_t *RP, const mp_limb_t *S1P,
4438          const mp_limb_t *S2P, mp_size_t N)
4439     Perform the bitwise logical and of {S1P, N} and {S2P, N}, and
4440     write the result to {RP, N}.
4441
4442 -- Function: void mpn_ior_n (mp_limb_t *RP, const mp_limb_t *S1P,
4443          const mp_limb_t *S2P, mp_size_t N)
4444     Perform the bitwise logical inclusive or of {S1P, N} and {S2P, N},
4445     and write the result to {RP, N}.
4446
4447 -- Function: void mpn_xor_n (mp_limb_t *RP, const mp_limb_t *S1P,
4448          const mp_limb_t *S2P, mp_size_t N)
4449     Perform the bitwise logical exclusive or of {S1P, N} and {S2P, N},
4450     and write the result to {RP, N}.
4451
4452 -- Function: void mpn_andn_n (mp_limb_t *RP, const mp_limb_t *S1P,
4453          const mp_limb_t *S2P, mp_size_t N)
4454     Perform the bitwise logical and of {S1P, N} and the bitwise
4455     complement of {S2P, N}, and write the result to {RP, N}.
4456
4457 -- Function: void mpn_iorn_n (mp_limb_t *RP, const mp_limb_t *S1P,
4458          const mp_limb_t *S2P, mp_size_t N)
4459     Perform the bitwise logical inclusive or of {S1P, N} and the
4460     bitwise complement of {S2P, N}, and write the result to {RP, N}.
4461
4462 -- Function: void mpn_nand_n (mp_limb_t *RP, const mp_limb_t *S1P,
4463          const mp_limb_t *S2P, mp_size_t N)
4464     Perform the bitwise logical and of {S1P, N} and {S2P, N}, and
4465     write the bitwise complement of the result to {RP, N}.
4466
4467 -- Function: void mpn_nior_n (mp_limb_t *RP, const mp_limb_t *S1P,
4468          const mp_limb_t *S2P, mp_size_t N)
4469     Perform the bitwise logical inclusive or of {S1P, N} and {S2P, N},
4470     and write the bitwise complement of the result to {RP, N}.
4471
4472 -- Function: void mpn_xnor_n (mp_limb_t *RP, const mp_limb_t *S1P,
4473          const mp_limb_t *S2P, mp_size_t N)
4474     Perform the bitwise logical exclusive or of {S1P, N} and {S2P, N},
4475     and write the bitwise complement of the result to {RP, N}.
4476
4477 -- Function: void mpn_com (mp_limb_t *RP, const mp_limb_t *SP,
4478          mp_size_t N)
4479     Perform the bitwise complement of {SP, N}, and write the result to
4480     {RP, N}.
4481
4482 -- Function: void mpn_copyi (mp_limb_t *RP, const mp_limb_t *S1P,
4483          mp_size_t N)
4484     Copy from {S1P, N} to {RP, N}, increasingly.
4485
4486 -- Function: void mpn_copyd (mp_limb_t *RP, const mp_limb_t *S1P,
4487          mp_size_t N)
4488     Copy from {S1P, N} to {RP, N}, decreasingly.
4489
4490 -- Function: void mpn_zero (mp_limb_t *RP, mp_size_t N)
4491     Zero {RP, N}.
4492
4493
44948.1 Nails
4495=========
4496
4497*Everything in this section is highly experimental and may disappear or
4498be subject to incompatible changes in a future version of GMP.*
4499
4500   Nails are an experimental feature whereby a few bits are left unused
4501at the top of each `mp_limb_t'.  This can significantly improve carry
4502handling on some processors.
4503
4504   All the `mpn' functions accepting limb data will expect the nail
4505bits to be zero on entry, and will return data with the nails similarly
4506all zero.  This applies both to limb vectors and to single limb
4507arguments.
4508
4509   Nails can be enabled by configuring with `--enable-nails'.  By
4510default the number of bits will be chosen according to what suits the
4511host processor, but a particular number can be selected with
4512`--enable-nails=N'.
4513
4514   At the mpn level, a nail build is neither source nor binary
4515compatible with a non-nail build, strictly speaking.  But programs
4516acting on limbs only through the mpn functions are likely to work
4517equally well with either build, and judicious use of the definitions
4518below should make any program compatible with either build, at the
4519source level.
4520
4521   For the higher level routines, meaning `mpz' etc, a nail build
4522should be fully source and binary compatible with a non-nail build.
4523
4524 -- Macro: GMP_NAIL_BITS
4525 -- Macro: GMP_NUMB_BITS
4526 -- Macro: GMP_LIMB_BITS
4527     `GMP_NAIL_BITS' is the number of nail bits, or 0 when nails are
4528     not in use.  `GMP_NUMB_BITS' is the number of data bits in a limb.
4529     `GMP_LIMB_BITS' is the total number of bits in an `mp_limb_t'.  In
4530     all cases
4531
4532          GMP_LIMB_BITS == GMP_NAIL_BITS + GMP_NUMB_BITS
4533
4534 -- Macro: GMP_NAIL_MASK
4535 -- Macro: GMP_NUMB_MASK
4536     Bit masks for the nail and number parts of a limb.
4537     `GMP_NAIL_MASK' is 0 when nails are not in use.
4538
4539     `GMP_NAIL_MASK' is not often needed, since the nail part can be
4540     obtained with `x >> GMP_NUMB_BITS', and that means one less large
4541     constant, which can help various RISC chips.
4542
4543 -- Macro: GMP_NUMB_MAX
4544     The maximum value that can be stored in the number part of a limb.
4545     This is the same as `GMP_NUMB_MASK', but can be used for clarity
4546     when doing comparisons rather than bit-wise operations.
4547
4548   The term "nails" comes from finger or toe nails, which are at the
4549ends of a limb (arm or leg).  "numb" is short for number, but is also
4550how the developers felt after trying for a long time to come up with
4551sensible names for these things.
4552
4553   In the future (the distant future most likely) a non-zero nail might
4554be permitted, giving non-unique representations for numbers in a limb
4555vector.  This would help vector processors since carries would only
4556ever need to propagate one or two limbs.
4557
4558
4559File: gmp.info,  Node: Random Number Functions,  Next: Formatted Output,  Prev: Low-level Functions,  Up: Top
4560
45619 Random Number Functions
4562*************************
4563
4564Sequences of pseudo-random numbers in GMP are generated using a
4565variable of type `gmp_randstate_t', which holds an algorithm selection
4566and a current state.  Such a variable must be initialized by a call to
4567one of the `gmp_randinit' functions, and can be seeded with one of the
4568`gmp_randseed' functions.
4569
4570   The functions actually generating random numbers are described in
4571*note Integer Random Numbers::, and *note Miscellaneous Float
4572Functions::.
4573
4574   The older style random number functions don't accept a
4575`gmp_randstate_t' parameter but instead share a global variable of that
4576type.  They use a default algorithm and are currently not seeded
4577(though perhaps that will change in the future).  The new functions
4578accepting a `gmp_randstate_t' are recommended for applications that
4579care about randomness.
4580
4581* Menu:
4582
4583* Random State Initialization::
4584* Random State Seeding::
4585* Random State Miscellaneous::
4586
4587
4588File: gmp.info,  Node: Random State Initialization,  Next: Random State Seeding,  Prev: Random Number Functions,  Up: Random Number Functions
4589
45909.1 Random State Initialization
4591===============================
4592
4593 -- Function: void gmp_randinit_default (gmp_randstate_t STATE)
4594     Initialize STATE with a default algorithm.  This will be a
4595     compromise between speed and randomness, and is recommended for
4596     applications with no special requirements.  Currently this is
4597     `gmp_randinit_mt'.
4598
4599 -- Function: void gmp_randinit_mt (gmp_randstate_t STATE)
4600     Initialize STATE for a Mersenne Twister algorithm.  This algorithm
4601     is fast and has good randomness properties.
4602
4603 -- Function: void gmp_randinit_lc_2exp (gmp_randstate_t STATE, const
4604          mpz_t A, unsigned long C, mp_bitcnt_t M2EXP)
4605     Initialize STATE with a linear congruential algorithm X = (A*X +
4606     C) mod 2^M2EXP.
4607
4608     The low bits of X in this algorithm are not very random.  The least
4609     significant bit will have a period no more than 2, and the second
4610     bit no more than 4, etc.  For this reason only the high half of
4611     each X is actually used.
4612
4613     When a random number of more than M2EXP/2 bits is to be generated,
4614     multiple iterations of the recurrence are used and the results
4615     concatenated.
4616
4617 -- Function: int gmp_randinit_lc_2exp_size (gmp_randstate_t STATE,
4618          mp_bitcnt_t SIZE)
4619     Initialize STATE for a linear congruential algorithm as per
4620     `gmp_randinit_lc_2exp'.  A, C and M2EXP are selected from a table,
4621     chosen so that SIZE bits (or more) of each X will be used, i.e.
4622     M2EXP/2 >= SIZE.
4623
4624     If successful the return value is non-zero.  If SIZE is bigger
4625     than the table data provides then the return value is zero.  The
4626     maximum SIZE currently supported is 128.
4627
4628 -- Function: void gmp_randinit_set (gmp_randstate_t ROP,
4629          gmp_randstate_t OP)
4630     Initialize ROP with a copy of the algorithm and state from OP.
4631
4632 -- Function: void gmp_randinit (gmp_randstate_t STATE,
4633          gmp_randalg_t ALG, ...)
4634     *This function is obsolete.*
4635
4636     Initialize STATE with an algorithm selected by ALG.  The only
4637     choice is `GMP_RAND_ALG_LC', which is `gmp_randinit_lc_2exp_size'
4638     described above.  A third parameter of type `unsigned long' is
4639     required, this is the SIZE for that function.
4640     `GMP_RAND_ALG_DEFAULT' or 0 are the same as `GMP_RAND_ALG_LC'.
4641
4642     `gmp_randinit' sets bits in the global variable `gmp_errno' to
4643     indicate an error.  `GMP_ERROR_UNSUPPORTED_ARGUMENT' if ALG is
4644     unsupported, or `GMP_ERROR_INVALID_ARGUMENT' if the SIZE parameter
4645     is too big.  It may be noted this error reporting is not thread
4646     safe (a good reason to use `gmp_randinit_lc_2exp_size' instead).
4647
4648 -- Function: void gmp_randclear (gmp_randstate_t STATE)
4649     Free all memory occupied by STATE.
4650
4651
4652File: gmp.info,  Node: Random State Seeding,  Next: Random State Miscellaneous,  Prev: Random State Initialization,  Up: Random Number Functions
4653
46549.2 Random State Seeding
4655========================
4656
4657 -- Function: void gmp_randseed (gmp_randstate_t STATE, const mpz_t
4658          SEED)
4659 -- Function: void gmp_randseed_ui (gmp_randstate_t STATE,
4660          unsigned long int SEED)
4661     Set an initial seed value into STATE.
4662
4663     The size of a seed determines how many different sequences of
4664     random numbers that it's possible to generate.  The "quality" of
4665     the seed is the randomness of a given seed compared to the
4666     previous seed used, and this affects the randomness of separate
4667     number sequences.  The method for choosing a seed is critical if
4668     the generated numbers are to be used for important applications,
4669     such as generating cryptographic keys.
4670
4671     Traditionally the system time has been used to seed, but care
4672     needs to be taken with this.  If an application seeds often and
4673     the resolution of the system clock is low, then the same sequence
4674     of numbers might be repeated.  Also, the system time is quite easy
4675     to guess, so if unpredictability is required then it should
4676     definitely not be the only source for the seed value.  On some
4677     systems there's a special device `/dev/random' which provides
4678     random data better suited for use as a seed.
4679
4680
4681File: gmp.info,  Node: Random State Miscellaneous,  Prev: Random State Seeding,  Up: Random Number Functions
4682
46839.3 Random State Miscellaneous
4684==============================
4685
4686 -- Function: unsigned long gmp_urandomb_ui (gmp_randstate_t STATE,
4687          unsigned long N)
4688     Return a uniformly distributed random number of N bits, i.e. in the
4689     range 0 to 2^N-1 inclusive.  N must be less than or equal to the
4690     number of bits in an `unsigned long'.
4691
4692 -- Function: unsigned long gmp_urandomm_ui (gmp_randstate_t STATE,
4693          unsigned long N)
4694     Return a uniformly distributed random number in the range 0 to
4695     N-1, inclusive.
4696
4697
4698File: gmp.info,  Node: Formatted Output,  Next: Formatted Input,  Prev: Random Number Functions,  Up: Top
4699
470010 Formatted Output
4701*******************
4702
4703* Menu:
4704
4705* Formatted Output Strings::
4706* Formatted Output Functions::
4707* C++ Formatted Output::
4708
4709
4710File: gmp.info,  Node: Formatted Output Strings,  Next: Formatted Output Functions,  Prev: Formatted Output,  Up: Formatted Output
4711
471210.1 Format Strings
4713===================
4714
4715`gmp_printf' and friends accept format strings similar to the standard C
4716`printf' (*note Formatted Output: (libc)Formatted Output.).  A format
4717specification is of the form
4718
4719     % [flags] [width] [.[precision]] [type] conv
4720
4721   GMP adds types `Z', `Q' and `F' for `mpz_t', `mpq_t' and `mpf_t'
4722respectively, `M' for `mp_limb_t', and `N' for an `mp_limb_t' array.
4723`Z', `Q', `M' and `N' behave like integers.  `Q' will print a `/' and a
4724denominator, if needed.  `F' behaves like a float.  For example,
4725
4726     mpz_t z;
4727     gmp_printf ("%s is an mpz %Zd\n", "here", z);
4728
4729     mpq_t q;
4730     gmp_printf ("a hex rational: %#40Qx\n", q);
4731
4732     mpf_t f;
4733     int   n;
4734     gmp_printf ("fixed point mpf %.*Ff with %d digits\n", n, f, n);
4735
4736     mp_limb_t l;
4737     gmp_printf ("limb %Mu\n", l);
4738
4739     const mp_limb_t *ptr;
4740     mp_size_t       size;
4741     gmp_printf ("limb array %Nx\n", ptr, size);
4742
4743   For `N' the limbs are expected least significant first, as per the
4744`mpn' functions (*note Low-level Functions::).  A negative size can be
4745given to print the value as a negative.
4746
4747   All the standard C `printf' types behave the same as the C library
4748`printf', and can be freely intermixed with the GMP extensions.  In the
4749current implementation the standard parts of the format string are
4750simply handed to `printf' and only the GMP extensions handled directly.
4751
4752   The flags accepted are as follows.  GLIBC style ' is only for the
4753standard C types (not the GMP types), and only if the C library
4754supports it.
4755
4756     0         pad with zeros (rather than spaces)
4757     #         show the base with `0x', `0X' or `0'
4758     +         always show a sign
4759     (space)   show a space or a `-' sign
4760     '         group digits, GLIBC style (not GMP types)
4761
4762   The optional width and precision can be given as a number within the
4763format string, or as a `*' to take an extra parameter of type `int', the
4764same as the standard `printf'.
4765
4766   The standard types accepted are as follows.  `h' and `l' are
4767portable, the rest will depend on the compiler (or include files) for
4768the type and the C library for the output.
4769
4770     h         short
4771     hh        char
4772     j         intmax_t or uintmax_t
4773     l         long or wchar_t
4774     ll        long long
4775     L         long double
4776     q         quad_t or u_quad_t
4777     t         ptrdiff_t
4778     z         size_t
4779
4780The GMP types are
4781
4782     F         mpf_t, float conversions
4783     Q         mpq_t, integer conversions
4784     M         mp_limb_t, integer conversions
4785     N         mp_limb_t array, integer conversions
4786     Z         mpz_t, integer conversions
4787
4788   The conversions accepted are as follows.  `a' and `A' are always
4789supported for `mpf_t' but depend on the C library for standard C float
4790types.  `m' and `p' depend on the C library.
4791
4792     a A       hex floats, C99 style
4793     c         character
4794     d         decimal integer
4795     e E       scientific format float
4796     f         fixed point float
4797     i         same as d
4798     g G       fixed or scientific float
4799     m         `strerror' string, GLIBC style
4800     n         store characters written so far
4801     o         octal integer
4802     p         pointer
4803     s         string
4804     u         unsigned integer
4805     x X       hex integer
4806
4807   `o', `x' and `X' are unsigned for the standard C types, but for
4808types `Z', `Q' and `N' they are signed.  `u' is not meaningful for `Z',
4809`Q' and `N'.
4810
4811   `M' is a proxy for the C library `l' or `L', according to the size
4812of `mp_limb_t'.  Unsigned conversions will be usual, but a signed
4813conversion can be used and will interpret the value as a twos complement
4814negative.
4815
4816   `n' can be used with any type, even the GMP types.
4817
4818   Other types or conversions that might be accepted by the C library
4819`printf' cannot be used through `gmp_printf', this includes for
4820instance extensions registered with GLIBC `register_printf_function'.
4821Also currently there's no support for POSIX `$' style numbered arguments
4822(perhaps this will be added in the future).
4823
4824   The precision field has its usual meaning for integer `Z' and float
4825`F' types, but is currently undefined for `Q' and should not be used
4826with that.
4827
4828   `mpf_t' conversions only ever generate as many digits as can be
4829accurately represented by the operand, the same as `mpf_get_str' does.
4830Zeros will be used if necessary to pad to the requested precision.  This
4831happens even for an `f' conversion of an `mpf_t' which is an integer,
4832for instance 2^1024 in an `mpf_t' of 128 bits precision will only
4833produce about 40 digits, then pad with zeros to the decimal point.  An
4834empty precision field like `%.Fe' or `%.Ff' can be used to specifically
4835request just the significant digits.  Without any dot and thus no
4836precision field, a precision value of 6 will be used.  Note that these
4837rules mean that `%Ff', `%.Ff', and `%.0Ff' will all be different.
4838
4839   The decimal point character (or string) is taken from the current
4840locale settings on systems which provide `localeconv' (*note Locales
4841and Internationalization: (libc)Locales.).  The C library will normally
4842do the same for standard float output.
4843
4844   The format string is only interpreted as plain `char's, multibyte
4845characters are not recognised.  Perhaps this will change in the future.
4846
4847
4848File: gmp.info,  Node: Formatted Output Functions,  Next: C++ Formatted Output,  Prev: Formatted Output Strings,  Up: Formatted Output
4849
485010.2 Functions
4851==============
4852
4853Each of the following functions is similar to the corresponding C
4854library function.  The basic `printf' forms take a variable argument
4855list.  The `vprintf' forms take an argument pointer, see *note Variadic
4856Functions: (libc)Variadic Functions, or `man 3 va_start'.
4857
4858   It should be emphasised that if a format string is invalid, or the
4859arguments don't match what the format specifies, then the behaviour of
4860any of these functions will be unpredictable.  GCC format string
4861checking is not available, since it doesn't recognise the GMP
4862extensions.
4863
4864   The file based functions `gmp_printf' and `gmp_fprintf' will return
4865-1 to indicate a write error.  Output is not "atomic", so partial
4866output may be produced if a write error occurs.  All the functions can
4867return -1 if the C library `printf' variant in use returns -1, but this
4868shouldn't normally occur.
4869
4870 -- Function: int gmp_printf (const char *FMT, ...)
4871 -- Function: int gmp_vprintf (const char *FMT, va_list AP)
4872     Print to the standard output `stdout'.  Return the number of
4873     characters written, or -1 if an error occurred.
4874
4875 -- Function: int gmp_fprintf (FILE *FP, const char *FMT, ...)
4876 -- Function: int gmp_vfprintf (FILE *FP, const char *FMT, va_list AP)
4877     Print to the stream FP.  Return the number of characters written,
4878     or -1 if an error occurred.
4879
4880 -- Function: int gmp_sprintf (char *BUF, const char *FMT, ...)
4881 -- Function: int gmp_vsprintf (char *BUF, const char *FMT, va_list AP)
4882     Form a null-terminated string in BUF.  Return the number of
4883     characters written, excluding the terminating null.
4884
4885     No overlap is permitted between the space at BUF and the string
4886     FMT.
4887
4888     These functions are not recommended, since there's no protection
4889     against exceeding the space available at BUF.
4890
4891 -- Function: int gmp_snprintf (char *BUF, size_t SIZE, const char
4892          *FMT, ...)
4893 -- Function: int gmp_vsnprintf (char *BUF, size_t SIZE, const char
4894          *FMT, va_list AP)
4895     Form a null-terminated string in BUF.  No more than SIZE bytes
4896     will be written.  To get the full output, SIZE must be enough for
4897     the string and null-terminator.
4898
4899     The return value is the total number of characters which ought to
4900     have been produced, excluding the terminating null.  If RETVAL >=
4901     SIZE then the actual output has been truncated to the first SIZE-1
4902     characters, and a null appended.
4903
4904     No overlap is permitted between the region {BUF,SIZE} and the FMT
4905     string.
4906
4907     Notice the return value is in ISO C99 `snprintf' style.  This is
4908     so even if the C library `vsnprintf' is the older GLIBC 2.0.x
4909     style.
4910
4911 -- Function: int gmp_asprintf (char **PP, const char *FMT, ...)
4912 -- Function: int gmp_vasprintf (char **PP, const char *FMT, va_list AP)
4913     Form a null-terminated string in a block of memory obtained from
4914     the current memory allocation function (*note Custom
4915     Allocation::).  The block will be the size of the string and
4916     null-terminator.  The address of the block in stored to *PP.  The
4917     return value is the number of characters produced, excluding the
4918     null-terminator.
4919
4920     Unlike the C library `asprintf', `gmp_asprintf' doesn't return -1
4921     if there's no more memory available, it lets the current allocation
4922     function handle that.
4923
4924 -- Function: int gmp_obstack_printf (struct obstack *OB, const char
4925          *FMT, ...)
4926 -- Function: int gmp_obstack_vprintf (struct obstack *OB, const char
4927          *FMT, va_list AP)
4928     Append to the current object in OB.  The return value is the
4929     number of characters written.  A null-terminator is not written.
4930
4931     FMT cannot be within the current object in OB, since that object
4932     might move as it grows.
4933
4934     These functions are available only when the C library provides the
4935     obstack feature, which probably means only on GNU systems, see
4936     *note Obstacks: (libc)Obstacks.
4937
4938
4939File: gmp.info,  Node: C++ Formatted Output,  Prev: Formatted Output Functions,  Up: Formatted Output
4940
494110.3 C++ Formatted Output
4942=========================
4943
4944The following functions are provided in `libgmpxx' (*note Headers and
4945Libraries::), which is built if C++ support is enabled (*note Build
4946Options::).  Prototypes are available from `<gmp.h>'.
4947
4948 -- Function: ostream& operator<< (ostream& STREAM, mpz_t OP)
4949     Print OP to STREAM, using its `ios' formatting settings.
4950     `ios::width' is reset to 0 after output, the same as the standard
4951     `ostream operator<<' routines do.
4952
4953     In hex or octal, OP is printed as a signed number, the same as for
4954     decimal.  This is unlike the standard `operator<<' routines on
4955     `int' etc, which instead give twos complement.
4956
4957 -- Function: ostream& operator<< (ostream& STREAM, mpq_t OP)
4958     Print OP to STREAM, using its `ios' formatting settings.
4959     `ios::width' is reset to 0 after output, the same as the standard
4960     `ostream operator<<' routines do.
4961
4962     Output will be a fraction like `5/9', or if the denominator is 1
4963     then just a plain integer like `123'.
4964
4965     In hex or octal, OP is printed as a signed value, the same as for
4966     decimal.  If `ios::showbase' is set then a base indicator is shown
4967     on both the numerator and denominator (if the denominator is
4968     required).
4969
4970 -- Function: ostream& operator<< (ostream& STREAM, mpf_t OP)
4971     Print OP to STREAM, using its `ios' formatting settings.
4972     `ios::width' is reset to 0 after output, the same as the standard
4973     `ostream operator<<' routines do.
4974
4975     The decimal point follows the standard library float `operator<<',
4976     which on recent systems means the `std::locale' imbued on STREAM.
4977
4978     Hex and octal are supported, unlike the standard `operator<<' on
4979     `double'.  The mantissa will be in hex or octal, the exponent will
4980     be in decimal.  For hex the exponent delimiter is an `@'.  This is
4981     as per `mpf_out_str'.
4982
4983     `ios::showbase' is supported, and will put a base on the mantissa,
4984     for example hex `0x1.8' or `0x0.8', or octal `01.4' or `00.4'.
4985     This last form is slightly strange, but at least differentiates
4986     itself from decimal.
4987
4988   These operators mean that GMP types can be printed in the usual C++
4989way, for example,
4990
4991     mpz_t  z;
4992     int    n;
4993     ...
4994     cout << "iteration " << n << " value " << z << "\n";
4995
4996   But note that `ostream' output (and `istream' input, *note C++
4997Formatted Input::) is the only overloading available for the GMP types
4998and that for instance using `+' with an `mpz_t' will have unpredictable
4999results.  For classes with overloading, see *note C++ Class Interface::.
5000
5001
5002File: gmp.info,  Node: Formatted Input,  Next: C++ Class Interface,  Prev: Formatted Output,  Up: Top
5003
500411 Formatted Input
5005******************
5006
5007* Menu:
5008
5009* Formatted Input Strings::
5010* Formatted Input Functions::
5011* C++ Formatted Input::
5012
5013
5014File: gmp.info,  Node: Formatted Input Strings,  Next: Formatted Input Functions,  Prev: Formatted Input,  Up: Formatted Input
5015
501611.1 Formatted Input Strings
5017============================
5018
5019`gmp_scanf' and friends accept format strings similar to the standard C
5020`scanf' (*note Formatted Input: (libc)Formatted Input.).  A format
5021specification is of the form
5022
5023     % [flags] [width] [type] conv
5024
5025   GMP adds types `Z', `Q' and `F' for `mpz_t', `mpq_t' and `mpf_t'
5026respectively.  `Z' and `Q' behave like integers.  `Q' will read a `/'
5027and a denominator, if present.  `F' behaves like a float.
5028
5029   GMP variables don't require an `&' when passed to `gmp_scanf', since
5030they're already "call-by-reference".  For example,
5031
5032     /* to read say "a(5) = 1234" */
5033     int   n;
5034     mpz_t z;
5035     gmp_scanf ("a(%d) = %Zd\n", &n, z);
5036
5037     mpq_t q1, q2;
5038     gmp_sscanf ("0377 + 0x10/0x11", "%Qi + %Qi", q1, q2);
5039
5040     /* to read say "topleft (1.55,-2.66)" */
5041     mpf_t x, y;
5042     char  buf[32];
5043     gmp_scanf ("%31s (%Ff,%Ff)", buf, x, y);
5044
5045   All the standard C `scanf' types behave the same as in the C library
5046`scanf', and can be freely intermixed with the GMP extensions.  In the
5047current implementation the standard parts of the format string are
5048simply handed to `scanf' and only the GMP extensions handled directly.
5049
5050   The flags accepted are as follows.  `a' and `'' will depend on
5051support from the C library, and `'' cannot be used with GMP types.
5052
5053     *         read but don't store
5054     a         allocate a buffer (string conversions)
5055     '         grouped digits, GLIBC style (not GMP
5056               types)
5057
5058   The standard types accepted are as follows.  `h' and `l' are
5059portable, the rest will depend on the compiler (or include files) for
5060the type and the C library for the input.
5061
5062     h         short
5063     hh        char
5064     j         intmax_t or uintmax_t
5065     l         long int, double or wchar_t
5066     ll        long long
5067     L         long double
5068     q         quad_t or u_quad_t
5069     t         ptrdiff_t
5070     z         size_t
5071
5072The GMP types are
5073
5074     F         mpf_t, float conversions
5075     Q         mpq_t, integer conversions
5076     Z         mpz_t, integer conversions
5077
5078   The conversions accepted are as follows.  `p' and `[' will depend on
5079support from the C library, the rest are standard.
5080
5081     c         character or characters
5082     d         decimal integer
5083     e E f g G float
5084     i         integer with base indicator
5085     n         characters read so far
5086     o         octal integer
5087     p         pointer
5088     s         string of non-whitespace characters
5089     u         decimal integer
5090     x X       hex integer
5091     [         string of characters in a set
5092
5093   `e', `E', `f', `g' and `G' are identical, they all read either fixed
5094point or scientific format, and either upper or lower case `e' for the
5095exponent in scientific format.
5096
5097   C99 style hex float format (`printf %a', *note Formatted Output
5098Strings::) is always accepted for `mpf_t', but for the standard float
5099types it will depend on the C library.
5100
5101   `x' and `X' are identical, both accept both upper and lower case
5102hexadecimal.
5103
5104   `o', `u', `x' and `X' all read positive or negative values.  For the
5105standard C types these are described as "unsigned" conversions, but
5106that merely affects certain overflow handling, negatives are still
5107allowed (per `strtoul', *note Parsing of Integers: (libc)Parsing of
5108Integers.).  For GMP types there are no overflows, so `d' and `u' are
5109identical.
5110
5111   `Q' type reads the numerator and (optional) denominator as given.
5112If the value might not be in canonical form then `mpq_canonicalize'
5113must be called before using it in any calculations (*note Rational
5114Number Functions::).
5115
5116   `Qi' will read a base specification separately for the numerator and
5117denominator.  For example `0x10/11' would be 16/11, whereas `0x10/0x11'
5118would be 16/17.
5119
5120   `n' can be used with any of the types above, even the GMP types.
5121`*' to suppress assignment is allowed, though in that case it would do
5122nothing at all.
5123
5124   Other conversions or types that might be accepted by the C library
5125`scanf' cannot be used through `gmp_scanf'.
5126
5127   Whitespace is read and discarded before a field, except for `c' and
5128`[' conversions.
5129
5130   For float conversions, the decimal point character (or string)
5131expected is taken from the current locale settings on systems which
5132provide `localeconv' (*note Locales and Internationalization:
5133(libc)Locales.).  The C library will normally do the same for standard
5134float input.
5135
5136   The format string is only interpreted as plain `char's, multibyte
5137characters are not recognised.  Perhaps this will change in the future.
5138
5139
5140File: gmp.info,  Node: Formatted Input Functions,  Next: C++ Formatted Input,  Prev: Formatted Input Strings,  Up: Formatted Input
5141
514211.2 Formatted Input Functions
5143==============================
5144
5145Each of the following functions is similar to the corresponding C
5146library function.  The plain `scanf' forms take a variable argument
5147list.  The `vscanf' forms take an argument pointer, see *note Variadic
5148Functions: (libc)Variadic Functions, or `man 3 va_start'.
5149
5150   It should be emphasised that if a format string is invalid, or the
5151arguments don't match what the format specifies, then the behaviour of
5152any of these functions will be unpredictable.  GCC format string
5153checking is not available, since it doesn't recognise the GMP
5154extensions.
5155
5156   No overlap is permitted between the FMT string and any of the results
5157produced.
5158
5159 -- Function: int gmp_scanf (const char *FMT, ...)
5160 -- Function: int gmp_vscanf (const char *FMT, va_list AP)
5161     Read from the standard input `stdin'.
5162
5163 -- Function: int gmp_fscanf (FILE *FP, const char *FMT, ...)
5164 -- Function: int gmp_vfscanf (FILE *FP, const char *FMT, va_list AP)
5165     Read from the stream FP.
5166
5167 -- Function: int gmp_sscanf (const char *S, const char *FMT, ...)
5168 -- Function: int gmp_vsscanf (const char *S, const char *FMT, va_list
5169          AP)
5170     Read from a null-terminated string S.
5171
5172   The return value from each of these functions is the same as the
5173standard C99 `scanf', namely the number of fields successfully parsed
5174and stored.  `%n' fields and fields read but suppressed by `*' don't
5175count towards the return value.
5176
5177   If end of input (or a file error) is reached before a character for
5178a field or a literal, and if no previous non-suppressed fields have
5179matched, then the return value is `EOF' instead of 0.  A whitespace
5180character in the format string is only an optional match and doesn't
5181induce an `EOF' in this fashion.  Leading whitespace read and discarded
5182for a field don't count as characters for that field.
5183
5184   For the GMP types, input parsing follows C99 rules, namely one
5185character of lookahead is used and characters are read while they
5186continue to meet the format requirements.  If this doesn't provide a
5187complete number then the function terminates, with that field not
5188stored nor counted towards the return value.  For instance with `mpf_t'
5189an input `1.23e-XYZ' would be read up to the `X' and that character
5190pushed back since it's not a digit.  The string `1.23e-' would then be
5191considered invalid since an `e' must be followed by at least one digit.
5192
5193   For the standard C types, in the current implementation GMP calls
5194the C library `scanf' functions, which might have looser rules about
5195what constitutes a valid input.
5196
5197   Note that `gmp_sscanf' is the same as `gmp_fscanf' and only does one
5198character of lookahead when parsing.  Although clearly it could look at
5199its entire input, it is deliberately made identical to `gmp_fscanf',
5200the same way C99 `sscanf' is the same as `fscanf'.
5201
5202
5203File: gmp.info,  Node: C++ Formatted Input,  Prev: Formatted Input Functions,  Up: Formatted Input
5204
520511.3 C++ Formatted Input
5206========================
5207
5208The following functions are provided in `libgmpxx' (*note Headers and
5209Libraries::), which is built only if C++ support is enabled (*note
5210Build Options::).  Prototypes are available from `<gmp.h>'.
5211
5212 -- Function: istream& operator>> (istream& STREAM, mpz_t ROP)
5213     Read ROP from STREAM, using its `ios' formatting settings.
5214
5215 -- Function: istream& operator>> (istream& STREAM, mpq_t ROP)
5216     An integer like `123' will be read, or a fraction like `5/9'.  No
5217     whitespace is allowed around the `/'.  If the fraction is not in
5218     canonical form then `mpq_canonicalize' must be called (*note
5219     Rational Number Functions::) before operating on it.
5220
5221     As per integer input, an `0' or `0x' base indicator is read when
5222     none of `ios::dec', `ios::oct' or `ios::hex' are set.  This is
5223     done separately for numerator and denominator, so that for instance
5224     `0x10/11' is 16/11 and `0x10/0x11' is 16/17.
5225
5226 -- Function: istream& operator>> (istream& STREAM, mpf_t ROP)
5227     Read ROP from STREAM, using its `ios' formatting settings.
5228
5229     Hex or octal floats are not supported, but might be in the future,
5230     or perhaps it's best to accept only what the standard float
5231     `operator>>' does.
5232
5233   Note that digit grouping specified by the `istream' locale is
5234currently not accepted.  Perhaps this will change in the future.
5235
5236
5237   These operators mean that GMP types can be read in the usual C++
5238way, for example,
5239
5240     mpz_t  z;
5241     ...
5242     cin >> z;
5243
5244   But note that `istream' input (and `ostream' output, *note C++
5245Formatted Output::) is the only overloading available for the GMP types
5246and that for instance using `+' with an `mpz_t' will have unpredictable
5247results.  For classes with overloading, see *note C++ Class Interface::.
5248
5249
5250File: gmp.info,  Node: C++ Class Interface,  Next: Custom Allocation,  Prev: Formatted Input,  Up: Top
5251
525212 C++ Class Interface
5253**********************
5254
5255This chapter describes the C++ class based interface to GMP.
5256
5257   All GMP C language types and functions can be used in C++ programs,
5258since `gmp.h' has `extern "C"' qualifiers, but the class interface
5259offers overloaded functions and operators which may be more convenient.
5260
5261   Due to the implementation of this interface, a reasonably recent C++
5262compiler is required, one supporting namespaces, partial specialization
5263of templates and member templates.  For GCC this means version 2.91 or
5264later.
5265
5266   *Everything described in this chapter is to be considered preliminary
5267and might be subject to incompatible changes if some unforeseen
5268difficulty reveals itself.*
5269
5270* Menu:
5271
5272* C++ Interface General::
5273* C++ Interface Integers::
5274* C++ Interface Rationals::
5275* C++ Interface Floats::
5276* C++ Interface Random Numbers::
5277* C++ Interface Limitations::
5278
5279
5280File: gmp.info,  Node: C++ Interface General,  Next: C++ Interface Integers,  Prev: C++ Class Interface,  Up: C++ Class Interface
5281
528212.1 C++ Interface General
5283==========================
5284
5285All the C++ classes and functions are available with
5286
5287     #include <gmpxx.h>
5288
5289   Programs should be linked with the `libgmpxx' and `libgmp'
5290libraries.  For example,
5291
5292     g++ mycxxprog.cc -lgmpxx -lgmp
5293
5294The classes defined are
5295
5296 -- Class: mpz_class
5297 -- Class: mpq_class
5298 -- Class: mpf_class
5299
5300   The standard operators and various standard functions are overloaded
5301to allow arithmetic with these classes.  For example,
5302
5303     int
5304     main (void)
5305     {
5306       mpz_class a, b, c;
5307
5308       a = 1234;
5309       b = "-5678";
5310       c = a+b;
5311       cout << "sum is " << c << "\n";
5312       cout << "absolute value is " << abs(c) << "\n";
5313
5314       return 0;
5315     }
5316
5317   An important feature of the implementation is that an expression like
5318`a=b+c' results in a single call to the corresponding `mpz_add',
5319without using a temporary for the `b+c' part.  Expressions which by
5320their nature imply intermediate values, like `a=b*c+d*e', still use
5321temporaries though.
5322
5323   The classes can be freely intermixed in expressions, as can the
5324classes and the standard types `long', `unsigned long' and `double'.
5325Smaller types like `int' or `float' can also be intermixed, since C++
5326will promote them.
5327
5328   Note that `bool' is not accepted directly, but must be explicitly
5329cast to an `int' first.  This is because C++ will automatically convert
5330any pointer to a `bool', so if GMP accepted `bool' it would make all
5331sorts of invalid class and pointer combinations compile but almost
5332certainly not do anything sensible.
5333
5334   Conversions back from the classes to standard C++ types aren't done
5335automatically, instead member functions like `get_si' are provided (see
5336the following sections for details).
5337
5338   Also there are no automatic conversions from the classes to the
5339corresponding GMP C types, instead a reference to the underlying C
5340object can be obtained with the following functions,
5341
5342 -- Function: mpz_t mpz_class::get_mpz_t ()
5343 -- Function: mpq_t mpq_class::get_mpq_t ()
5344 -- Function: mpf_t mpf_class::get_mpf_t ()
5345
5346   These can be used to call a C function which doesn't have a C++ class
5347interface.  For example to set `a' to the GCD of `b' and `c',
5348
5349     mpz_class a, b, c;
5350     ...
5351     mpz_gcd (a.get_mpz_t(), b.get_mpz_t(), c.get_mpz_t());
5352
5353   In the other direction, a class can be initialized from the
5354corresponding GMP C type, or assigned to if an explicit constructor is
5355used.  In both cases this makes a copy of the value, it doesn't create
5356any sort of association.  For example,
5357
5358     mpz_t z;
5359     // ... init and calculate z ...
5360     mpz_class x(z);
5361     mpz_class y;
5362     y = mpz_class (z);
5363
5364   There are no namespace setups in `gmpxx.h', all types and functions
5365are simply put into the global namespace.  This is what `gmp.h' has
5366done in the past, and continues to do for compatibility.  The extras
5367provided by `gmpxx.h' follow GMP naming conventions and are unlikely to
5368clash with anything.
5369
5370
5371File: gmp.info,  Node: C++ Interface Integers,  Next: C++ Interface Rationals,  Prev: C++ Interface General,  Up: C++ Class Interface
5372
537312.2 C++ Interface Integers
5374===========================
5375
5376 -- Function:  mpz_class::mpz_class (type N)
5377     Construct an `mpz_class'.  All the standard C++ types may be used,
5378     except `long long' and `long double', and all the GMP C++ classes
5379     can be used, although conversions from `mpq_class' and `mpf_class'
5380     are `explicit'.  Any necessary conversion follows the
5381     corresponding C function, for example `double' follows `mpz_set_d'
5382     (*note Assigning Integers::).
5383
5384 -- Function: explicit mpz_class::mpz_class (mpz_t Z)
5385     Construct an `mpz_class' from an `mpz_t'.  The value in Z is
5386     copied into the new `mpz_class', there won't be any permanent
5387     association between it and Z.
5388
5389 -- Function: explicit mpz_class::mpz_class (const char *S, int BASE =
5390          0)
5391 -- Function: explicit mpz_class::mpz_class (const string& S, int BASE
5392          = 0)
5393     Construct an `mpz_class' converted from a string using
5394     `mpz_set_str' (*note Assigning Integers::).
5395
5396     If the string is not a valid integer, an `std::invalid_argument'
5397     exception is thrown.  The same applies to `operator='.
5398
5399 -- Function: mpz_class operator"" _mpz (const char *STR)
5400     With C++11 compilers, integers can be constructed with the syntax
5401     `123_mpz' which is equivalent to `mpz_class("123")'.
5402
5403 -- Function: mpz_class operator/ (mpz_class A, mpz_class D)
5404 -- Function: mpz_class operator% (mpz_class A, mpz_class D)
5405     Divisions involving `mpz_class' round towards zero, as per the
5406     `mpz_tdiv_q' and `mpz_tdiv_r' functions (*note Integer Division::).
5407     This is the same as the C99 `/' and `%' operators.
5408
5409     The `mpz_fdiv...' or `mpz_cdiv...' functions can always be called
5410     directly if desired.  For example,
5411
5412          mpz_class q, a, d;
5413          ...
5414          mpz_fdiv_q (q.get_mpz_t(), a.get_mpz_t(), d.get_mpz_t());
5415
5416 -- Function: mpz_class abs (mpz_class OP)
5417 -- Function: int cmp (mpz_class OP1, type OP2)
5418 -- Function: int cmp (type OP1, mpz_class OP2)
5419 -- Function: bool mpz_class::fits_sint_p (void)
5420 -- Function: bool mpz_class::fits_slong_p (void)
5421 -- Function: bool mpz_class::fits_sshort_p (void)
5422 -- Function: bool mpz_class::fits_uint_p (void)
5423 -- Function: bool mpz_class::fits_ulong_p (void)
5424 -- Function: bool mpz_class::fits_ushort_p (void)
5425 -- Function: double mpz_class::get_d (void)
5426 -- Function: long mpz_class::get_si (void)
5427 -- Function: string mpz_class::get_str (int BASE = 10)
5428 -- Function: unsigned long mpz_class::get_ui (void)
5429 -- Function: int mpz_class::set_str (const char *STR, int BASE)
5430 -- Function: int mpz_class::set_str (const string& STR, int BASE)
5431 -- Function: int sgn (mpz_class OP)
5432 -- Function: mpz_class sqrt (mpz_class OP)
5433 -- Function: void mpz_class::swap (mpz_class& OP)
5434 -- Function: void swap (mpz_class& OP1, mpz_class& OP2)
5435     These functions provide a C++ class interface to the corresponding
5436     GMP C routines.
5437
5438     `cmp' can be used with any of the classes or the standard C++
5439     types, except `long long' and `long double'.
5440
5441
5442   Overloaded operators for combinations of `mpz_class' and `double'
5443are provided for completeness, but it should be noted that if the given
5444`double' is not an integer then the way any rounding is done is
5445currently unspecified.  The rounding might take place at the start, in
5446the middle, or at the end of the operation, and it might change in the
5447future.
5448
5449   Conversions between `mpz_class' and `double', however, are defined
5450to follow the corresponding C functions `mpz_get_d' and `mpz_set_d'.
5451And comparisons are always made exactly, as per `mpz_cmp_d'.
5452
5453
5454File: gmp.info,  Node: C++ Interface Rationals,  Next: C++ Interface Floats,  Prev: C++ Interface Integers,  Up: C++ Class Interface
5455
545612.3 C++ Interface Rationals
5457============================
5458
5459In all the following constructors, if a fraction is given then it
5460should be in canonical form, or if not then `mpq_class::canonicalize'
5461called.
5462
5463 -- Function:  mpq_class::mpq_class (type OP)
5464 -- Function:  mpq_class::mpq_class (integer NUM, integer DEN)
5465     Construct an `mpq_class'.  The initial value can be a single value
5466     of any type (conversion from `mpf_class' is `explicit'), or a pair
5467     of integers (`mpz_class' or standard C++ integer types)
5468     representing a fraction, except that `long long' and `long double'
5469     are not supported.  For example,
5470
5471          mpq_class q (99);
5472          mpq_class q (1.75);
5473          mpq_class q (1, 3);
5474
5475 -- Function: explicit mpq_class::mpq_class (mpq_t Q)
5476     Construct an `mpq_class' from an `mpq_t'.  The value in Q is
5477     copied into the new `mpq_class', there won't be any permanent
5478     association between it and Q.
5479
5480 -- Function: explicit mpq_class::mpq_class (const char *S, int BASE =
5481          0)
5482 -- Function: explicit mpq_class::mpq_class (const string& S, int BASE
5483          = 0)
5484     Construct an `mpq_class' converted from a string using
5485     `mpq_set_str' (*note Initializing Rationals::).
5486
5487     If the string is not a valid rational, an `std::invalid_argument'
5488     exception is thrown.  The same applies to `operator='.
5489
5490 -- Function: mpq_class operator"" _mpq (const char *STR)
5491     With C++11 compilers, integral rationals can be constructed with
5492     the syntax `123_mpq' which is equivalent to `mpq_class(123_mpz)'.
5493     Other rationals can be built as `-1_mpq/2' or `0xb_mpq/123456_mpz'.
5494
5495 -- Function: void mpq_class::canonicalize ()
5496     Put an `mpq_class' into canonical form, as per *note Rational
5497     Number Functions::.  All arithmetic operators require their
5498     operands in canonical form, and will return results in canonical
5499     form.
5500
5501 -- Function: mpq_class abs (mpq_class OP)
5502 -- Function: int cmp (mpq_class OP1, type OP2)
5503 -- Function: int cmp (type OP1, mpq_class OP2)
5504 -- Function: double mpq_class::get_d (void)
5505 -- Function: string mpq_class::get_str (int BASE = 10)
5506 -- Function: int mpq_class::set_str (const char *STR, int BASE)
5507 -- Function: int mpq_class::set_str (const string& STR, int BASE)
5508 -- Function: int sgn (mpq_class OP)
5509 -- Function: void mpq_class::swap (mpq_class& OP)
5510 -- Function: void swap (mpq_class& OP1, mpq_class& OP2)
5511     These functions provide a C++ class interface to the corresponding
5512     GMP C routines.
5513
5514     `cmp' can be used with any of the classes or the standard C++
5515     types, except `long long' and `long double'.
5516
5517 -- Function: mpz_class& mpq_class::get_num ()
5518 -- Function: mpz_class& mpq_class::get_den ()
5519     Get a reference to an `mpz_class' which is the numerator or
5520     denominator of an `mpq_class'.  This can be used both for read and
5521     write access.  If the object returned is modified, it modifies the
5522     original `mpq_class'.
5523
5524     If direct manipulation might produce a non-canonical value, then
5525     `mpq_class::canonicalize' must be called before further operations.
5526
5527 -- Function: mpz_t mpq_class::get_num_mpz_t ()
5528 -- Function: mpz_t mpq_class::get_den_mpz_t ()
5529     Get a reference to the underlying `mpz_t' numerator or denominator
5530     of an `mpq_class'.  This can be passed to C functions expecting an
5531     `mpz_t'.  Any modifications made to the `mpz_t' will modify the
5532     original `mpq_class'.
5533
5534     If direct manipulation might produce a non-canonical value, then
5535     `mpq_class::canonicalize' must be called before further operations.
5536
5537 -- Function: istream& operator>> (istream& STREAM, mpq_class& ROP);
5538     Read ROP from STREAM, using its `ios' formatting settings, the
5539     same as `mpq_t operator>>' (*note C++ Formatted Input::).
5540
5541     If the ROP read might not be in canonical form then
5542     `mpq_class::canonicalize' must be called.
5543
5544
5545File: gmp.info,  Node: C++ Interface Floats,  Next: C++ Interface Random Numbers,  Prev: C++ Interface Rationals,  Up: C++ Class Interface
5546
554712.4 C++ Interface Floats
5548=========================
5549
5550When an expression requires the use of temporary intermediate
5551`mpf_class' values, like `f=g*h+x*y', those temporaries will have the
5552same precision as the destination `f'.  Explicit constructors can be
5553used if this doesn't suit.
5554
5555 -- Function:  mpf_class::mpf_class (type OP)
5556 -- Function:  mpf_class::mpf_class (type OP, mp_bitcnt_t PREC)
5557     Construct an `mpf_class'.  Any standard C++ type can be used,
5558     except `long long' and `long double', and any of the GMP C++
5559     classes can be used.
5560
5561     If PREC is given, the initial precision is that value, in bits.  If
5562     PREC is not given, then the initial precision is determined by the
5563     type of OP given.  An `mpz_class', `mpq_class', or C++ builtin
5564     type will give the default `mpf' precision (*note Initializing
5565     Floats::).  An `mpf_class' or expression will give the precision
5566     of that value.  The precision of a binary expression is the higher
5567     of the two operands.
5568
5569          mpf_class f(1.5);        // default precision
5570          mpf_class f(1.5, 500);   // 500 bits (at least)
5571          mpf_class f(x);          // precision of x
5572          mpf_class f(abs(x));     // precision of x
5573          mpf_class f(-g, 1000);   // 1000 bits (at least)
5574          mpf_class f(x+y);        // greater of precisions of x and y
5575
5576 -- Function: explicit mpf_class::mpf_class (mpf_t F)
5577 -- Function:  mpf_class::mpf_class (mpf_t F, mp_bitcnt_t PREC)
5578     Construct an `mpf_class' from an `mpf_t'.  The value in F is
5579     copied into the new `mpf_class', there won't be any permanent
5580     association between it and F.
5581
5582     If PREC is given, the initial precision is that value, in bits.  If
5583     PREC is not given, then the initial precision is that of F.
5584
5585 -- Function: explicit mpf_class::mpf_class (const char *S)
5586 -- Function:  mpf_class::mpf_class (const char *S, mp_bitcnt_t PREC,
5587          int BASE = 0)
5588 -- Function: explicit mpf_class::mpf_class (const string& S)
5589 -- Function:  mpf_class::mpf_class (const string& S, mp_bitcnt_t PREC,
5590          int BASE = 0)
5591     Construct an `mpf_class' converted from a string using
5592     `mpf_set_str' (*note Assigning Floats::).  If PREC is given, the
5593     initial precision is that value, in bits.  If not, the default
5594     `mpf' precision (*note Initializing Floats::) is used.
5595
5596     If the string is not a valid float, an `std::invalid_argument'
5597     exception is thrown.  The same applies to `operator='.
5598
5599 -- Function: mpf_class operator"" _mpf (const char *STR)
5600     With C++11 compilers, floats can be constructed with the syntax
5601     `1.23e-1_mpf' which is equivalent to `mpf_class("1.23e-1")'.
5602
5603 -- Function: mpf_class& mpf_class::operator= (type OP)
5604     Convert and store the given OP value to an `mpf_class' object.  The
5605     same types are accepted as for the constructors above.
5606
5607     Note that `operator=' only stores a new value, it doesn't copy or
5608     change the precision of the destination, instead the value is
5609     truncated if necessary.  This is the same as `mpf_set' etc.  Note
5610     in particular this means for `mpf_class' a copy constructor is not
5611     the same as a default constructor plus assignment.
5612
5613          mpf_class x (y);   // x created with precision of y
5614
5615          mpf_class x;       // x created with default precision
5616          x = y;             // value truncated to that precision
5617
5618     Applications using templated code may need to be careful about the
5619     assumptions the code makes in this area, when working with
5620     `mpf_class' values of various different or non-default precisions.
5621     For instance implementations of the standard `complex' template
5622     have been seen in both styles above, though of course `complex' is
5623     normally only actually specified for use with the builtin float
5624     types.
5625
5626 -- Function: mpf_class abs (mpf_class OP)
5627 -- Function: mpf_class ceil (mpf_class OP)
5628 -- Function: int cmp (mpf_class OP1, type OP2)
5629 -- Function: int cmp (type OP1, mpf_class OP2)
5630 -- Function: bool mpf_class::fits_sint_p (void)
5631 -- Function: bool mpf_class::fits_slong_p (void)
5632 -- Function: bool mpf_class::fits_sshort_p (void)
5633 -- Function: bool mpf_class::fits_uint_p (void)
5634 -- Function: bool mpf_class::fits_ulong_p (void)
5635 -- Function: bool mpf_class::fits_ushort_p (void)
5636 -- Function: mpf_class floor (mpf_class OP)
5637 -- Function: mpf_class hypot (mpf_class OP1, mpf_class OP2)
5638 -- Function: double mpf_class::get_d (void)
5639 -- Function: long mpf_class::get_si (void)
5640 -- Function: string mpf_class::get_str (mp_exp_t& EXP, int BASE = 10,
5641          size_t DIGITS = 0)
5642 -- Function: unsigned long mpf_class::get_ui (void)
5643 -- Function: int mpf_class::set_str (const char *STR, int BASE)
5644 -- Function: int mpf_class::set_str (const string& STR, int BASE)
5645 -- Function: int sgn (mpf_class OP)
5646 -- Function: mpf_class sqrt (mpf_class OP)
5647 -- Function: void mpf_class::swap (mpf_class& OP)
5648 -- Function: void swap (mpf_class& OP1, mpf_class& OP2)
5649 -- Function: mpf_class trunc (mpf_class OP)
5650     These functions provide a C++ class interface to the corresponding
5651     GMP C routines.
5652
5653     `cmp' can be used with any of the classes or the standard C++
5654     types, except `long long' and `long double'.
5655
5656     The accuracy provided by `hypot' is not currently guaranteed.
5657
5658 -- Function: mp_bitcnt_t mpf_class::get_prec ()
5659 -- Function: void mpf_class::set_prec (mp_bitcnt_t PREC)
5660 -- Function: void mpf_class::set_prec_raw (mp_bitcnt_t PREC)
5661     Get or set the current precision of an `mpf_class'.
5662
5663     The restrictions described for `mpf_set_prec_raw' (*note
5664     Initializing Floats::) apply to `mpf_class::set_prec_raw'.  Note
5665     in particular that the `mpf_class' must be restored to it's
5666     allocated precision before being destroyed.  This must be done by
5667     application code, there's no automatic mechanism for it.
5668
5669
5670File: gmp.info,  Node: C++ Interface Random Numbers,  Next: C++ Interface Limitations,  Prev: C++ Interface Floats,  Up: C++ Class Interface
5671
567212.5 C++ Interface Random Numbers
5673=================================
5674
5675 -- Class: gmp_randclass
5676     The C++ class interface to the GMP random number functions uses
5677     `gmp_randclass' to hold an algorithm selection and current state,
5678     as per `gmp_randstate_t'.
5679
5680 -- Function:  gmp_randclass::gmp_randclass (void (*RANDINIT)
5681          (gmp_randstate_t, ...), ...)
5682     Construct a `gmp_randclass', using a call to the given RANDINIT
5683     function (*note Random State Initialization::).  The arguments
5684     expected are the same as RANDINIT, but with `mpz_class' instead of
5685     `mpz_t'.  For example,
5686
5687          gmp_randclass r1 (gmp_randinit_default);
5688          gmp_randclass r2 (gmp_randinit_lc_2exp_size, 32);
5689          gmp_randclass r3 (gmp_randinit_lc_2exp, a, c, m2exp);
5690          gmp_randclass r4 (gmp_randinit_mt);
5691
5692     `gmp_randinit_lc_2exp_size' will fail if the size requested is too
5693     big, an `std::length_error' exception is thrown in that case.
5694
5695 -- Function:  gmp_randclass::gmp_randclass (gmp_randalg_t ALG, ...)
5696     Construct a `gmp_randclass' using the same parameters as
5697     `gmp_randinit' (*note Random State Initialization::).  This
5698     function is obsolete and the above RANDINIT style should be
5699     preferred.
5700
5701 -- Function: void gmp_randclass::seed (unsigned long int S)
5702 -- Function: void gmp_randclass::seed (mpz_class S)
5703     Seed a random number generator.  See *note Random Number
5704     Functions::, for how to choose a good seed.
5705
5706 -- Function: mpz_class gmp_randclass::get_z_bits (mp_bitcnt_t BITS)
5707 -- Function: mpz_class gmp_randclass::get_z_bits (mpz_class BITS)
5708     Generate a random integer with a specified number of bits.
5709
5710 -- Function: mpz_class gmp_randclass::get_z_range (mpz_class N)
5711     Generate a random integer in the range 0 to N-1 inclusive.
5712
5713 -- Function: mpf_class gmp_randclass::get_f ()
5714 -- Function: mpf_class gmp_randclass::get_f (mp_bitcnt_t PREC)
5715     Generate a random float F in the range 0 <= F < 1.  F will be to
5716     PREC bits precision, or if PREC is not given then to the precision
5717     of the destination.  For example,
5718
5719          gmp_randclass  r;
5720          ...
5721          mpf_class  f (0, 512);   // 512 bits precision
5722          f = r.get_f();           // random number, 512 bits
5723
5724
5725File: gmp.info,  Node: C++ Interface Limitations,  Prev: C++ Interface Random Numbers,  Up: C++ Class Interface
5726
572712.6 C++ Interface Limitations
5728==============================
5729
5730`mpq_class' and Templated Reading
5731     A generic piece of template code probably won't know that
5732     `mpq_class' requires a `canonicalize' call if inputs read with
5733     `operator>>' might be non-canonical.  This can lead to incorrect
5734     results.
5735
5736     `operator>>' behaves as it does for reasons of efficiency.  A
5737     canonicalize can be quite time consuming on large operands, and is
5738     best avoided if it's not necessary.
5739
5740     But this potential difficulty reduces the usefulness of
5741     `mpq_class'.  Perhaps a mechanism to tell `operator>>' what to do
5742     will be adopted in the future, maybe a preprocessor define, a
5743     global flag, or an `ios' flag pressed into service.  Or maybe, at
5744     the risk of inconsistency, the `mpq_class' `operator>>' could
5745     canonicalize and leave `mpq_t' `operator>>' not doing so, for use
5746     on those occasions when that's acceptable.  Send feedback or
5747     alternate ideas to <gmp-bugs@gmplib.org>.
5748
5749Subclassing
5750     Subclassing the GMP C++ classes works, but is not currently
5751     recommended.
5752
5753     Expressions involving subclasses resolve correctly (or seem to),
5754     but in normal C++ fashion the subclass doesn't inherit
5755     constructors and assignments.  There's many of those in the GMP
5756     classes, and a good way to reestablish them in a subclass is not
5757     yet provided.
5758
5759Templated Expressions
5760     A subtle difficulty exists when using expressions together with
5761     application-defined template functions.  Consider the following,
5762     with `T' intended to be some numeric type,
5763
5764          template <class T>
5765          T fun (const T &, const T &);
5766
5767     When used with, say, plain `mpz_class' variables, it works fine:
5768     `T' is resolved as `mpz_class'.
5769
5770          mpz_class f(1), g(2);
5771          fun (f, g);    // Good
5772
5773     But when one of the arguments is an expression, it doesn't work.
5774
5775          mpz_class f(1), g(2), h(3);
5776          fun (f, g+h);  // Bad
5777
5778     This is because `g+h' ends up being a certain expression template
5779     type internal to `gmpxx.h', which the C++ template resolution
5780     rules are unable to automatically convert to `mpz_class'.  The
5781     workaround is simply to add an explicit cast.
5782
5783          mpz_class f(1), g(2), h(3);
5784          fun (f, mpz_class(g+h));  // Good
5785
5786     Similarly, within `fun' it may be necessary to cast an expression
5787     to type `T' when calling a templated `fun2'.
5788
5789          template <class T>
5790          void fun (T f, T g)
5791          {
5792            fun2 (f, f+g);     // Bad
5793          }
5794
5795          template <class T>
5796          void fun (T f, T g)
5797          {
5798            fun2 (f, T(f+g));  // Good
5799          }
5800
5801
5802File: gmp.info,  Node: Custom Allocation,  Next: Language Bindings,  Prev: C++ Class Interface,  Up: Top
5803
580413 Custom Allocation
5805********************
5806
5807By default GMP uses `malloc', `realloc' and `free' for memory
5808allocation, and if they fail GMP prints a message to the standard error
5809output and terminates the program.
5810
5811   Alternate functions can be specified, to allocate memory in a
5812different way or to have a different error action on running out of
5813memory.
5814
5815 -- Function: void mp_set_memory_functions (
5816          void *(*ALLOC_FUNC_PTR) (size_t),
5817          void *(*REALLOC_FUNC_PTR) (void *, size_t, size_t),
5818          void (*FREE_FUNC_PTR) (void *, size_t))
5819     Replace the current allocation functions from the arguments.  If
5820     an argument is `NULL', the corresponding default function is used.
5821
5822     These functions will be used for all memory allocation done by
5823     GMP, apart from temporary space from `alloca' if that function is
5824     available and GMP is configured to use it (*note Build Options::).
5825
5826     *Be sure to call `mp_set_memory_functions' only when there are no
5827     active GMP objects allocated using the previous memory functions!
5828     Usually that means calling it before any other GMP function.*
5829
5830   The functions supplied should fit the following declarations:
5831
5832 -- Function: void * allocate_function (size_t ALLOC_SIZE)
5833     Return a pointer to newly allocated space with at least ALLOC_SIZE
5834     bytes.
5835
5836 -- Function: void * reallocate_function (void *PTR, size_t OLD_SIZE,
5837          size_t NEW_SIZE)
5838     Resize a previously allocated block PTR of OLD_SIZE bytes to be
5839     NEW_SIZE bytes.
5840
5841     The block may be moved if necessary or if desired, and in that
5842     case the smaller of OLD_SIZE and NEW_SIZE bytes must be copied to
5843     the new location.  The return value is a pointer to the resized
5844     block, that being the new location if moved or just PTR if not.
5845
5846     PTR is never `NULL', it's always a previously allocated block.
5847     NEW_SIZE may be bigger or smaller than OLD_SIZE.
5848
5849 -- Function: void free_function (void *PTR, size_t SIZE)
5850     De-allocate the space pointed to by PTR.
5851
5852     PTR is never `NULL', it's always a previously allocated block of
5853     SIZE bytes.
5854
5855   A "byte" here means the unit used by the `sizeof' operator.
5856
5857   The REALLOCATE_FUNCTION parameter OLD_SIZE and the FREE_FUNCTION
5858parameter SIZE are passed for convenience, but of course they can be
5859ignored if not needed by an implementation.  The default functions
5860using `malloc' and friends for instance don't use them.
5861
5862   No error return is allowed from any of these functions, if they
5863return then they must have performed the specified operation.  In
5864particular note that ALLOCATE_FUNCTION or REALLOCATE_FUNCTION mustn't
5865return `NULL'.
5866
5867   Getting a different fatal error action is a good use for custom
5868allocation functions, for example giving a graphical dialog rather than
5869the default print to `stderr'.  How much is possible when genuinely out
5870of memory is another question though.
5871
5872   There's currently no defined way for the allocation functions to
5873recover from an error such as out of memory, they must terminate
5874program execution.  A `longjmp' or throwing a C++ exception will have
5875undefined results.  This may change in the future.
5876
5877   GMP may use allocated blocks to hold pointers to other allocated
5878blocks.  This will limit the assumptions a conservative garbage
5879collection scheme can make.
5880
5881   Since the default GMP allocation uses `malloc' and friends, those
5882functions will be linked in even if the first thing a program does is an
5883`mp_set_memory_functions'.  It's necessary to change the GMP sources if
5884this is a problem.
5885
5886
5887 -- Function: void mp_get_memory_functions (
5888          void *(**ALLOC_FUNC_PTR) (size_t),
5889          void *(**REALLOC_FUNC_PTR) (void *, size_t, size_t),
5890          void (**FREE_FUNC_PTR) (void *, size_t))
5891     Get the current allocation functions, storing function pointers to
5892     the locations given by the arguments.  If an argument is `NULL',
5893     that function pointer is not stored.
5894
5895     For example, to get just the current free function,
5896
5897          void (*freefunc) (void *, size_t);
5898
5899          mp_get_memory_functions (NULL, NULL, &freefunc);
5900
5901
5902File: gmp.info,  Node: Language Bindings,  Next: Algorithms,  Prev: Custom Allocation,  Up: Top
5903
590414 Language Bindings
5905********************
5906
5907The following packages and projects offer access to GMP from languages
5908other than C, though perhaps with varying levels of functionality and
5909efficiency.
5910
5911
5912C++
5913        * GMP C++ class interface, *note C++ Class Interface::
5914          Straightforward interface, expression templates to eliminate
5915          temporaries.
5916
5917        * ALP `http://www-sop.inria.fr/saga/logiciels/ALP/'
5918          Linear algebra and polynomials using templates.
5919
5920        * Arithmos `http://cant.ua.ac.be/old/arithmos/'
5921          Rationals with infinities and square roots.
5922
5923        * CLN `http://www.ginac.de/CLN/'
5924          High level classes for arithmetic.
5925
5926        * LiDIA `http://www.cdc.informatik.tu-darmstadt.de/TI/LiDIA/'
5927          A C++ library for computational number theory.
5928
5929        * Linbox `http://www.linalg.org/'
5930          Sparse vectors and matrices.
5931
5932        * NTL `http://www.shoup.net/ntl/'
5933          A C++ number theory library.
5934
5935Eiffel
5936        * Eiffelroom `http://www.eiffelroom.org/node/442'
5937
5938Fortran
5939        * Omni F77 `http://phase.hpcc.jp/Omni/home.html'
5940          Arbitrary precision floats.
5941
5942Haskell
5943        * Glasgow Haskell Compiler `http://www.haskell.org/ghc/'
5944
5945Java
5946        * Kaffe `http://www.kaffe.org/'
5947
5948        * Kissme `http://kissme.sourceforge.net/'
5949
5950Lisp
5951        * GNU Common Lisp `http://www.gnu.org/software/gcl/gcl.html'
5952
5953        * Librep `http://librep.sourceforge.net/'
5954
5955        * XEmacs (21.5.18 beta and up) `http://www.xemacs.org'
5956          Optional big integers, rationals and floats using GMP.
5957
5958M4
5959        * GNU m4 betas `http://www.seindal.dk/rene/gnu/'
5960          Optionally provides an arbitrary precision `mpeval'.
5961
5962ML
5963        * MLton compiler `http://mlton.org/'
5964
5965Objective Caml
5966        * MLGMP `http://www.di.ens.fr/~monniaux/programmes.html.en'
5967
5968        * Numerix `http://pauillac.inria.fr/~quercia/'
5969          Optionally using GMP.
5970
5971Oz
5972        * Mozart `http://www.mozart-oz.org/'
5973
5974Pascal
5975        * GNU Pascal Compiler `http://www.gnu-pascal.de/'
5976          GMP unit.
5977
5978        * Numerix `http://pauillac.inria.fr/~quercia/'
5979          For Free Pascal, optionally using GMP.
5980
5981Perl
5982        * GMP module, see `demos/perl' in the GMP sources (*note
5983          Demonstration Programs::).
5984
5985        * Math::GMP `http://www.cpan.org/'
5986          Compatible with Math::BigInt, but not as many functions as
5987          the GMP module above.
5988
5989        * Math::BigInt::GMP `http://www.cpan.org/'
5990          Plug Math::GMP into normal Math::BigInt operations.
5991
5992Pike
5993        * mpz module in the standard distribution,
5994          `http://pike.ida.liu.se/'
5995
5996Prolog
5997        * SWI Prolog `http://www.swi-prolog.org/'
5998          Arbitrary precision floats.
5999
6000Python
6001        * GMPY `http://code.google.com/p/gmpy/'
6002
6003Ruby
6004        * http://rubygems.org/gems/gmp
6005
6006Scheme
6007        * GNU Guile (upcoming 1.8)
6008          `http://www.gnu.org/software/guile/guile.html'
6009
6010        * RScheme `http://www.rscheme.org/'
6011
6012        * STklos `http://www.stklos.org/'
6013
6014Smalltalk
6015        * GNU Smalltalk
6016          `http://www.smalltalk.org/versions/GNUSmalltalk.html'
6017
6018Other
6019        * Axiom `http://savannah.nongnu.org/projects/axiom'
6020          Computer algebra using GCL.
6021
6022        * DrGenius `http://drgenius.seul.org/'
6023          Geometry system and mathematical programming language.
6024
6025        * GiNaC `http://www.ginac.de/'
6026          C++ computer algebra using CLN.
6027
6028        * GOO `http://www.googoogaga.org/'
6029          Dynamic object oriented language.
6030
6031        * Maxima `http://www.ma.utexas.edu/users/wfs/maxima.html'
6032          Macsyma computer algebra using GCL.
6033
6034        * Q `http://q-lang.sourceforge.net/'
6035          Equational programming system.
6036
6037        * Regina `http://regina.sourceforge.net/'
6038          Topological calculator.
6039
6040        * Yacas `yacas.sourceforge.net'
6041          Yet another computer algebra system.
6042
6043
6044
6045File: gmp.info,  Node: Algorithms,  Next: Internals,  Prev: Language Bindings,  Up: Top
6046
604715 Algorithms
6048*************
6049
6050This chapter is an introduction to some of the algorithms used for
6051various GMP operations.  The code is likely to be hard to understand
6052without knowing something about the algorithms.
6053
6054   Some GMP internals are mentioned, but applications that expect to be
6055compatible with future GMP releases should take care to use only the
6056documented functions.
6057
6058* Menu:
6059
6060* Multiplication Algorithms::
6061* Division Algorithms::
6062* Greatest Common Divisor Algorithms::
6063* Powering Algorithms::
6064* Root Extraction Algorithms::
6065* Radix Conversion Algorithms::
6066* Other Algorithms::
6067* Assembly Coding::
6068
6069
6070File: gmp.info,  Node: Multiplication Algorithms,  Next: Division Algorithms,  Prev: Algorithms,  Up: Algorithms
6071
607215.1 Multiplication
6073===================
6074
6075NxN limb multiplications and squares are done using one of seven
6076algorithms, as the size N increases.
6077
6078     Algorithm      Threshold
6079     Basecase       (none)
6080     Karatsuba      `MUL_TOOM22_THRESHOLD'
6081     Toom-3         `MUL_TOOM33_THRESHOLD'
6082     Toom-4         `MUL_TOOM44_THRESHOLD'
6083     Toom-6.5       `MUL_TOOM6H_THRESHOLD'
6084     Toom-8.5       `MUL_TOOM8H_THRESHOLD'
6085     FFT            `MUL_FFT_THRESHOLD'
6086
6087   Similarly for squaring, with the `SQR' thresholds.
6088
6089   NxM multiplications of operands with different sizes above
6090`MUL_TOOM22_THRESHOLD' are currently done by special Toom-inspired
6091algorithms or directly with FFT, depending on operand size (*note
6092Unbalanced Multiplication::).
6093
6094* Menu:
6095
6096* Basecase Multiplication::
6097* Karatsuba Multiplication::
6098* Toom 3-Way Multiplication::
6099* Toom 4-Way Multiplication::
6100* Higher degree Toom'n'half::
6101* FFT Multiplication::
6102* Other Multiplication::
6103* Unbalanced Multiplication::
6104
6105
6106File: gmp.info,  Node: Basecase Multiplication,  Next: Karatsuba Multiplication,  Prev: Multiplication Algorithms,  Up: Multiplication Algorithms
6107
610815.1.1 Basecase Multiplication
6109------------------------------
6110
6111Basecase NxM multiplication is a straightforward rectangular set of
6112cross-products, the same as long multiplication done by hand and for
6113that reason sometimes known as the schoolbook or grammar school method.
6114This is an O(N*M) algorithm.  See Knuth section 4.3.1 algorithm M
6115(*note References::), and the `mpn/generic/mul_basecase.c' code.
6116
6117   Assembly implementations of `mpn_mul_basecase' are essentially the
6118same as the generic C code, but have all the usual assembly tricks and
6119obscurities introduced for speed.
6120
6121   A square can be done in roughly half the time of a multiply, by
6122using the fact that the cross products above and below the diagonal are
6123the same.  A triangle of products below the diagonal is formed, doubled
6124(left shift by one bit), and then the products on the diagonal added.
6125This can be seen in `mpn/generic/sqr_basecase.c'.  Again the assembly
6126implementations take essentially the same approach.
6127
6128          u0  u1  u2  u3  u4
6129        +---+---+---+---+---+
6130     u0 | d |   |   |   |   |
6131        +---+---+---+---+---+
6132     u1 |   | d |   |   |   |
6133        +---+---+---+---+---+
6134     u2 |   |   | d |   |   |
6135        +---+---+---+---+---+
6136     u3 |   |   |   | d |   |
6137        +---+---+---+---+---+
6138     u4 |   |   |   |   | d |
6139        +---+---+---+---+---+
6140
6141   In practice squaring isn't a full 2x faster than multiplying, it's
6142usually around 1.5x.  Less than 1.5x probably indicates
6143`mpn_sqr_basecase' wants improving on that CPU.
6144
6145   On some CPUs `mpn_mul_basecase' can be faster than the generic C
6146`mpn_sqr_basecase' on some small sizes.  `SQR_BASECASE_THRESHOLD' is
6147the size at which to use `mpn_sqr_basecase', this will be zero if that
6148routine should be used always.
6149
6150
6151File: gmp.info,  Node: Karatsuba Multiplication,  Next: Toom 3-Way Multiplication,  Prev: Basecase Multiplication,  Up: Multiplication Algorithms
6152
615315.1.2 Karatsuba Multiplication
6154-------------------------------
6155
6156The Karatsuba multiplication algorithm is described in Knuth section
61574.3.3 part A, and various other textbooks.  A brief description is
6158given here.
6159
6160   The inputs x and y are treated as each split into two parts of equal
6161length (or the most significant part one limb shorter if N is odd).
6162
6163      high              low
6164     +----------+----------+
6165     |    x1    |    x0    |
6166     +----------+----------+
6167
6168     +----------+----------+
6169     |    y1    |    y0    |
6170     +----------+----------+
6171
6172   Let b be the power of 2 where the split occurs, i.e. if x0 is k
6173limbs (y0 the same) then b=2^(k*mp_bits_per_limb).  With that x=x1*b+x0
6174and y=y1*b+y0, and the following holds,
6175
6176     x*y = (b^2+b)*x1*y1 - b*(x1-x0)*(y1-y0) + (b+1)*x0*y0
6177
6178   This formula means doing only three multiplies of (N/2)x(N/2) limbs,
6179whereas a basecase multiply of NxN limbs is equivalent to four
6180multiplies of (N/2)x(N/2).  The factors (b^2+b) etc represent the
6181positions where the three products must be added.
6182
6183      high                              low
6184     +--------+--------+ +--------+--------+
6185     |      x1*y1      | |      x0*y0      |
6186     +--------+--------+ +--------+--------+
6187               +--------+--------+
6188           add |      x1*y1      |
6189               +--------+--------+
6190               +--------+--------+
6191           add |      x0*y0      |
6192               +--------+--------+
6193               +--------+--------+
6194           sub | (x1-x0)*(y1-y0) |
6195               +--------+--------+
6196
6197   The term (x1-x0)*(y1-y0) is best calculated as an absolute value,
6198and the sign used to choose to add or subtract.  Notice the sum
6199high(x0*y0)+low(x1*y1) occurs twice, so it's possible to do 5*k limb
6200additions, rather than 6*k, but in GMP extra function call overheads
6201outweigh the saving.
6202
6203   Squaring is similar to multiplying, but with x=y the formula reduces
6204to an equivalent with three squares,
6205
6206     x^2 = (b^2+b)*x1^2 - b*(x1-x0)^2 + (b+1)*x0^2
6207
6208   The final result is accumulated from those three squares the same
6209way as for the three multiplies above.  The middle term (x1-x0)^2 is now
6210always positive.
6211
6212   A similar formula for both multiplying and squaring can be
6213constructed with a middle term (x1+x0)*(y1+y0).  But those sums can
6214exceed k limbs, leading to more carry handling and additions than the
6215form above.
6216
6217   Karatsuba multiplication is asymptotically an O(N^1.585) algorithm,
6218the exponent being log(3)/log(2), representing 3 multiplies each 1/2
6219the size of the inputs.  This is a big improvement over the basecase
6220multiply at O(N^2) and the advantage soon overcomes the extra additions
6221Karatsuba performs.  `MUL_TOOM22_THRESHOLD' can be as little as 10
6222limbs.  The `SQR' threshold is usually about twice the `MUL'.
6223
6224   The basecase algorithm will take a time of the form M(N) = a*N^2 +
6225b*N + c and the Karatsuba algorithm K(N) = 3*M(N/2) + d*N + e, which
6226expands to K(N) = 3/4*a*N^2 + 3/2*b*N + 3*c + d*N + e.  The factor 3/4
6227for a means per-crossproduct speedups in the basecase code will
6228increase the threshold since they benefit M(N) more than K(N).  And
6229conversely the 3/2 for b means linear style speedups of b will increase
6230the threshold since they benefit K(N) more than M(N).  The latter can
6231be seen for instance when adding an optimized `mpn_sqr_diagonal' to
6232`mpn_sqr_basecase'.  Of course all speedups reduce total time, and in
6233that sense the algorithm thresholds are merely of academic interest.
6234
6235
6236File: gmp.info,  Node: Toom 3-Way Multiplication,  Next: Toom 4-Way Multiplication,  Prev: Karatsuba Multiplication,  Up: Multiplication Algorithms
6237
623815.1.3 Toom 3-Way Multiplication
6239--------------------------------
6240
6241The Karatsuba formula is the simplest case of a general approach to
6242splitting inputs that leads to both Toom and FFT algorithms.  A
6243description of Toom can be found in Knuth section 4.3.3, with an
6244example 3-way calculation after Theorem A.  The 3-way form used in GMP
6245is described here.
6246
6247   The operands are each considered split into 3 pieces of equal length
6248(or the most significant part 1 or 2 limbs shorter than the other two).
6249
6250      high                         low
6251     +----------+----------+----------+
6252     |    x2    |    x1    |    x0    |
6253     +----------+----------+----------+
6254
6255     +----------+----------+----------+
6256     |    y2    |    y1    |    y0    |
6257     +----------+----------+----------+
6258
6259These parts are treated as the coefficients of two polynomials
6260
6261     X(t) = x2*t^2 + x1*t + x0
6262     Y(t) = y2*t^2 + y1*t + y0
6263
6264   Let b equal the power of 2 which is the size of the x0, x1, y0 and
6265y1 pieces, i.e. if they're k limbs each then b=2^(k*mp_bits_per_limb).
6266With this x=X(b) and y=Y(b).
6267
6268   Let a polynomial W(t)=X(t)*Y(t) and suppose its coefficients are
6269
6270     W(t) = w4*t^4 + w3*t^3 + w2*t^2 + w1*t + w0
6271
6272   The w[i] are going to be determined, and when they are they'll give
6273the final result using w=W(b), since x*y=X(b)*Y(b)=W(b).  The
6274coefficients will be roughly b^2 each, and the final W(b) will be an
6275addition like,
6276
6277      high                                        low
6278     +-------+-------+
6279     |       w4      |
6280     +-------+-------+
6281            +--------+-------+
6282            |        w3      |
6283            +--------+-------+
6284                    +--------+-------+
6285                    |        w2      |
6286                    +--------+-------+
6287                            +--------+-------+
6288                            |        w1      |
6289                            +--------+-------+
6290                                     +-------+-------+
6291                                     |       w0      |
6292                                     +-------+-------+
6293
6294   The w[i] coefficients could be formed by a simple set of cross
6295products, like w4=x2*y2, w3=x2*y1+x1*y2, w2=x2*y0+x1*y1+x0*y2 etc, but
6296this would need all nine x[i]*y[j] for i,j=0,1,2, and would be
6297equivalent merely to a basecase multiply.  Instead the following
6298approach is used.
6299
6300   X(t) and Y(t) are evaluated and multiplied at 5 points, giving
6301values of W(t) at those points.  In GMP the following points are used,
6302
6303     Point    Value
6304     t=0      x0 * y0, which gives w0 immediately
6305     t=1      (x2+x1+x0) * (y2+y1+y0)
6306     t=-1     (x2-x1+x0) * (y2-y1+y0)
6307     t=2      (4*x2+2*x1+x0) * (4*y2+2*y1+y0)
6308     t=inf    x2 * y2, which gives w4 immediately
6309
6310   At t=-1 the values can be negative and that's handled using the
6311absolute values and tracking the sign separately.  At t=inf the value
6312is actually X(t)*Y(t)/t^4 in the limit as t approaches infinity, but
6313it's much easier to think of as simply x2*y2 giving w4 immediately
6314(much like x0*y0 at t=0 gives w0 immediately).
6315
6316   Each of the points substituted into W(t)=w4*t^4+...+w0 gives a
6317linear combination of the w[i] coefficients, and the value of those
6318combinations has just been calculated.
6319
6320     W(0)   =                              w0
6321     W(1)   =    w4 +   w3 +   w2 +   w1 + w0
6322     W(-1)  =    w4 -   w3 +   w2 -   w1 + w0
6323     W(2)   = 16*w4 + 8*w3 + 4*w2 + 2*w1 + w0
6324     W(inf) =    w4
6325
6326   This is a set of five equations in five unknowns, and some
6327elementary linear algebra quickly isolates each w[i].  This involves
6328adding or subtracting one W(t) value from another, and a couple of
6329divisions by powers of 2 and one division by 3, the latter using the
6330special `mpn_divexact_by3' (*note Exact Division::).
6331
6332   The conversion of W(t) values to the coefficients is interpolation.
6333A polynomial of degree 4 like W(t) is uniquely determined by values
6334known at 5 different points.  The points are arbitrary and can be
6335chosen to make the linear equations come out with a convenient set of
6336steps for quickly isolating the w[i].
6337
6338   Squaring follows the same procedure as multiplication, but there's
6339only one X(t) and it's evaluated at the 5 points, and those values
6340squared to give values of W(t).  The interpolation is then identical,
6341and in fact the same `toom_interpolate_5pts' subroutine is used for
6342both squaring and multiplying.
6343
6344   Toom-3 is asymptotically O(N^1.465), the exponent being
6345log(5)/log(3), representing 5 recursive multiplies of 1/3 the original
6346size each.  This is an improvement over Karatsuba at O(N^1.585), though
6347Toom does more work in the evaluation and interpolation and so it only
6348realizes its advantage above a certain size.
6349
6350   Near the crossover between Toom-3 and Karatsuba there's generally a
6351range of sizes where the difference between the two is small.
6352`MUL_TOOM33_THRESHOLD' is a somewhat arbitrary point in that range and
6353successive runs of the tune program can give different values due to
6354small variations in measuring.  A graph of time versus size for the two
6355shows the effect, see `tune/README'.
6356
6357   At the fairly small sizes where the Toom-3 thresholds occur it's
6358worth remembering that the asymptotic behaviour for Karatsuba and
6359Toom-3 can't be expected to make accurate predictions, due of course to
6360the big influence of all sorts of overheads, and the fact that only a
6361few recursions of each are being performed.  Even at large sizes
6362there's a good chance machine dependent effects like cache architecture
6363will mean actual performance deviates from what might be predicted.
6364
6365   The formula given for the Karatsuba algorithm (*note Karatsuba
6366Multiplication::) has an equivalent for Toom-3 involving only five
6367multiplies, but this would be complicated and unenlightening.
6368
6369   An alternate view of Toom-3 can be found in Zuras (*note
6370References::), using a vector to represent the x and y splits and a
6371matrix multiplication for the evaluation and interpolation stages.  The
6372matrix inverses are not meant to be actually used, and they have
6373elements with values much greater than in fact arise in the
6374interpolation steps.  The diagram shown for the 3-way is attractive,
6375but again doesn't have to be implemented that way and for example with
6376a bit of rearrangement just one division by 6 can be done.
6377
6378
6379File: gmp.info,  Node: Toom 4-Way Multiplication,  Next: Higher degree Toom'n'half,  Prev: Toom 3-Way Multiplication,  Up: Multiplication Algorithms
6380
638115.1.4 Toom 4-Way Multiplication
6382--------------------------------
6383
6384Karatsuba and Toom-3 split the operands into 2 and 3 coefficients,
6385respectively.  Toom-4 analogously splits the operands into 4
6386coefficients.  Using the notation from the section on Toom-3
6387multiplication, we form two polynomials:
6388
6389     X(t) = x3*t^3 + x2*t^2 + x1*t + x0
6390     Y(t) = y3*t^3 + y2*t^2 + y1*t + y0
6391
6392   X(t) and Y(t) are evaluated and multiplied at 7 points, giving
6393values of W(t) at those points.  In GMP the following points are used,
6394
6395     Point    Value
6396     t=0      x0 * y0, which gives w0 immediately
6397     t=1/2    (x3+2*x2+4*x1+8*x0) * (y3+2*y2+4*y1+8*y0)
6398     t=-1/2   (-x3+2*x2-4*x1+8*x0) * (-y3+2*y2-4*y1+8*y0)
6399     t=1      (x3+x2+x1+x0) * (y3+y2+y1+y0)
6400     t=-1     (-x3+x2-x1+x0) * (-y3+y2-y1+y0)
6401     t=2      (8*x3+4*x2+2*x1+x0) * (8*y3+4*y2+2*y1+y0)
6402     t=inf    x3 * y3, which gives w6 immediately
6403
6404   The number of additions and subtractions for Toom-4 is much larger
6405than for Toom-3.  But several subexpressions occur multiple times, for
6406example x2+x0, occurs for both t=1 and t=-1.
6407
6408   Toom-4 is asymptotically O(N^1.404), the exponent being
6409log(7)/log(4), representing 7 recursive multiplies of 1/4 the original
6410size each.
6411
6412
6413File: gmp.info,  Node: Higher degree Toom'n'half,  Next: FFT Multiplication,  Prev: Toom 4-Way Multiplication,  Up: Multiplication Algorithms
6414
641515.1.5 Higher degree Toom'n'half
6416--------------------------------
6417
6418The Toom algorithms described above (*note Toom 3-Way Multiplication::,
6419*note Toom 4-Way Multiplication::) generalizes to split into an
6420arbitrary number of pieces. In general a split of two equally long
6421operands into r pieces leads to evaluations and pointwise
6422multiplications done at 2*r-1 points. To fully exploit symmetries it
6423would be better to have a multiple of 4 points, that's why for higher
6424degree Toom'n'half is used.
6425
6426   Toom'n'half means that the existence of one more piece is considered
6427for a single operand. It can be virtual, i.e. zero, or real, when the
6428two operand are not exactly balanced. By chosing an even r, Toom-r+1/2
6429requires 2r points, a multiple of four.
6430
6431   The four-plets of points inlcude 0, inf, +1, -1 and +-2^i, +-2^-i .
6432Each of them giving shortcuts for the evaluation phase and for some
6433steps in the interpolation phase. Further tricks are used to reduce the
6434memory footprint of the whole multiplication algorithm to a memory
6435buffer equanl in size to the result of the product.
6436
6437   Current GMP uses both Toom-6'n'half and Toom-8'n'half.
6438
6439
6440File: gmp.info,  Node: FFT Multiplication,  Next: Other Multiplication,  Prev: Higher degree Toom'n'half,  Up: Multiplication Algorithms
6441
644215.1.6 FFT Multiplication
6443-------------------------
6444
6445At large to very large sizes a Fermat style FFT multiplication is used,
6446following Sch�nhage and Strassen (*note References::).  Descriptions of
6447FFTs in various forms can be found in many textbooks, for instance
6448Knuth section 4.3.3 part C or Lipson chapter IX.  A brief description
6449of the form used in GMP is given here.
6450
6451   The multiplication done is x*y mod 2^N+1, for a given N.  A full
6452product x*y is obtained by choosing N>=bits(x)+bits(y) and padding x
6453and y with high zero limbs.  The modular product is the native form for
6454the algorithm, so padding to get a full product is unavoidable.
6455
6456   The algorithm follows a split, evaluate, pointwise multiply,
6457interpolate and combine similar to that described above for Karatsuba
6458and Toom-3.  A k parameter controls the split, with an FFT-k splitting
6459into 2^k pieces of M=N/2^k bits each.  N must be a multiple of
6460(2^k)*mp_bits_per_limb so the split falls on limb boundaries, avoiding
6461bit shifts in the split and combine stages.
6462
6463   The evaluations, pointwise multiplications, and interpolation, are
6464all done modulo 2^N'+1 where N' is 2M+k+3 rounded up to a multiple of
64652^k and of `mp_bits_per_limb'.  The results of interpolation will be
6466the following negacyclic convolution of the input pieces, and the
6467choice of N' ensures these sums aren't truncated.
6468
6469                ---
6470                \         b
6471     w[n] =     /     (-1) * x[i] * y[j]
6472                ---
6473            i+j==b*2^k+n
6474               b=0,1
6475
6476   The points used for the evaluation are g^i for i=0 to 2^k-1 where
6477g=2^(2N'/2^k).  g is a 2^k'th root of unity mod 2^N'+1, which produces
6478necessary cancellations at the interpolation stage, and it's also a
6479power of 2 so the fast Fourier transforms used for the evaluation and
6480interpolation do only shifts, adds and negations.
6481
6482   The pointwise multiplications are done modulo 2^N'+1 and either
6483recurse into a further FFT or use a plain multiplication (Toom-3,
6484Karatsuba or basecase), whichever is optimal at the size N'.  The
6485interpolation is an inverse fast Fourier transform.  The resulting set
6486of sums of x[i]*y[j] are added at appropriate offsets to give the final
6487result.
6488
6489   Squaring is the same, but x is the only input so it's one transform
6490at the evaluate stage and the pointwise multiplies are squares.  The
6491interpolation is the same.
6492
6493   For a mod 2^N+1 product, an FFT-k is an O(N^(k/(k-1))) algorithm,
6494the exponent representing 2^k recursed modular multiplies each
64951/2^(k-1) the size of the original.  Each successive k is an asymptotic
6496improvement, but overheads mean each is only faster at bigger and
6497bigger sizes.  In the code, `MUL_FFT_TABLE' and `SQR_FFT_TABLE' are the
6498thresholds where each k is used.  Each new k effectively swaps some
6499multiplying for some shifts, adds and overheads.
6500
6501   A mod 2^N+1 product can be formed with a normal NxN->2N bit multiply
6502plus a subtraction, so an FFT and Toom-3 etc can be compared directly.
6503A k=4 FFT at O(N^1.333) can be expected to be the first faster than
6504Toom-3 at O(N^1.465).  In practice this is what's found, with
6505`MUL_FFT_MODF_THRESHOLD' and `SQR_FFT_MODF_THRESHOLD' being between 300
6506and 1000 limbs, depending on the CPU.  So far it's been found that only
6507very large FFTs recurse into pointwise multiplies above these sizes.
6508
6509   When an FFT is to give a full product, the change of N to 2N doesn't
6510alter the theoretical complexity for a given k, but for the purposes of
6511considering where an FFT might be first used it can be assumed that the
6512FFT is recursing into a normal multiply and that on that basis it's
6513doing 2^k recursed multiplies each 1/2^(k-2) the size of the inputs,
6514making it O(N^(k/(k-2))).  This would mean k=7 at O(N^1.4) would be the
6515first FFT faster than Toom-3.  In practice `MUL_FFT_THRESHOLD' and
6516`SQR_FFT_THRESHOLD' have been found to be in the k=8 range, somewhere
6517between 3000 and 10000 limbs.
6518
6519   The way N is split into 2^k pieces and then 2M+k+3 is rounded up to
6520a multiple of 2^k and `mp_bits_per_limb' means that when
65212^k>=mp_bits_per_limb the effective N is a multiple of 2^(2k-1) bits.
6522The +k+3 means some values of N just under such a multiple will be
6523rounded to the next.  The complexity calculations above assume that a
6524favourable size is used, meaning one which isn't padded through
6525rounding, and it's also assumed that the extra +k+3 bits are negligible
6526at typical FFT sizes.
6527
6528   The practical effect of the 2^(2k-1) constraint is to introduce a
6529step-effect into measured speeds.  For example k=8 will round N up to a
6530multiple of 32768 bits, so for a 32-bit limb there'll be 512 limb
6531groups of sizes for which `mpn_mul_n' runs at the same speed.  Or for
6532k=9 groups of 2048 limbs, k=10 groups of 8192 limbs, etc.  In practice
6533it's been found each k is used at quite small multiples of its size
6534constraint and so the step effect is quite noticeable in a time versus
6535size graph.
6536
6537   The threshold determinations currently measure at the mid-points of
6538size steps, but this is sub-optimal since at the start of a new step it
6539can happen that it's better to go back to the previous k for a while.
6540Something more sophisticated for `MUL_FFT_TABLE' and `SQR_FFT_TABLE'
6541will be needed.
6542
6543
6544File: gmp.info,  Node: Other Multiplication,  Next: Unbalanced Multiplication,  Prev: FFT Multiplication,  Up: Multiplication Algorithms
6545
654615.1.7 Other Multiplication
6547---------------------------
6548
6549The Toom algorithms described above (*note Toom 3-Way Multiplication::,
6550*note Toom 4-Way Multiplication::) generalizes to split into an
6551arbitrary number of pieces, as per Knuth section 4.3.3 algorithm C.
6552This is not currently used.  The notes here are merely for interest.
6553
6554   In general a split into r+1 pieces is made, and evaluations and
6555pointwise multiplications done at 2*r+1 points.  A 4-way split does 7
6556pointwise multiplies, 5-way does 9, etc.  Asymptotically an (r+1)-way
6557algorithm is O(N^(log(2*r+1)/log(r+1))).  Only the pointwise
6558multiplications count towards big-O complexity, but the time spent in
6559the evaluate and interpolate stages grows with r and has a significant
6560practical impact, with the asymptotic advantage of each r realized only
6561at bigger and bigger sizes.  The overheads grow as O(N*r), whereas in
6562an r=2^k FFT they grow only as O(N*log(r)).
6563
6564   Knuth algorithm C evaluates at points 0,1,2,...,2*r, but exercise 4
6565uses -r,...,0,...,r and the latter saves some small multiplies in the
6566evaluate stage (or rather trades them for additions), and has a further
6567saving of nearly half the interpolate steps.  The idea is to separate
6568odd and even final coefficients and then perform algorithm C steps C7
6569and C8 on them separately.  The divisors at step C7 become j^2 and the
6570multipliers at C8 become 2*t*j-j^2.
6571
6572   Splitting odd and even parts through positive and negative points
6573can be thought of as using -1 as a square root of unity.  If a 4th root
6574of unity was available then a further split and speedup would be
6575possible, but no such root exists for plain integers.  Going to complex
6576integers with i=sqrt(-1) doesn't help, essentially because in Cartesian
6577form it takes three real multiplies to do a complex multiply.  The
6578existence of 2^k'th roots of unity in a suitable ring or field lets the
6579fast Fourier transform keep splitting and get to O(N*log(r)).
6580
6581   Floating point FFTs use complex numbers approximating Nth roots of
6582unity.  Some processors have special support for such FFTs.  But these
6583are not used in GMP since it's very difficult to guarantee an exact
6584result (to some number of bits).  An occasional difference of 1 in the
6585last bit might not matter to a typical signal processing algorithm, but
6586is of course of vital importance to GMP.
6587
6588
6589File: gmp.info,  Node: Unbalanced Multiplication,  Prev: Other Multiplication,  Up: Multiplication Algorithms
6590
659115.1.8 Unbalanced Multiplication
6592--------------------------------
6593
6594Multiplication of operands with different sizes, both below
6595`MUL_TOOM22_THRESHOLD' are done with plain schoolbook multiplication
6596(*note Basecase Multiplication::).
6597
6598   For really large operands, we invoke FFT directly.
6599
6600   For operands between these sizes, we use Toom inspired algorithms
6601suggested by Alberto Zanoni and Marco Bodrato.  The idea is to split
6602the operands into polynomials of different degree.  GMP currently
6603splits the smaller operand onto 2 coefficients, i.e., a polynomial of
6604degree 1, but the larger operand can be split into 2, 3, or 4
6605coefficients, i.e., a polynomial of degree 1 to 3.
6606
6607
6608File: gmp.info,  Node: Division Algorithms,  Next: Greatest Common Divisor Algorithms,  Prev: Multiplication Algorithms,  Up: Algorithms
6609
661015.2 Division Algorithms
6611========================
6612
6613* Menu:
6614
6615* Single Limb Division::
6616* Basecase Division::
6617* Divide and Conquer Division::
6618* Block-Wise Barrett Division::
6619* Exact Division::
6620* Exact Remainder::
6621* Small Quotient Division::
6622
6623
6624File: gmp.info,  Node: Single Limb Division,  Next: Basecase Division,  Prev: Division Algorithms,  Up: Division Algorithms
6625
662615.2.1 Single Limb Division
6627---------------------------
6628
6629Nx1 division is implemented using repeated 2x1 divisions from high to
6630low, either with a hardware divide instruction or a multiplication by
6631inverse, whichever is best on a given CPU.
6632
6633   The multiply by inverse follows "Improved division by invariant
6634integers" by M�ller and Granlund (*note References::) and is
6635implemented as `udiv_qrnnd_preinv' in `gmp-impl.h'.  The idea is to
6636have a fixed-point approximation to 1/d (see `invert_limb') and then
6637multiply by the high limb (plus one bit) of the dividend to get a
6638quotient q.  With d normalized (high bit set), q is no more than 1 too
6639small.  Subtracting q*d from the dividend gives a remainder, and
6640reveals whether q or q-1 is correct.
6641
6642   The result is a division done with two multiplications and four or
6643five arithmetic operations.  On CPUs with low latency multipliers this
6644can be much faster than a hardware divide, though the cost of
6645calculating the inverse at the start may mean it's only better on
6646inputs bigger than say 4 or 5 limbs.
6647
6648   When a divisor must be normalized, either for the generic C
6649`__udiv_qrnnd_c' or the multiply by inverse, the division performed is
6650actually a*2^k by d*2^k where a is the dividend and k is the power
6651necessary to have the high bit of d*2^k set.  The bit shifts for the
6652dividend are usually accomplished "on the fly" meaning by extracting
6653the appropriate bits at each step.  Done this way the quotient limbs
6654come out aligned ready to store.  When only the remainder is wanted, an
6655alternative is to take the dividend limbs unshifted and calculate r = a
6656mod d*2^k followed by an extra final step r*2^k mod d*2^k.  This can
6657help on CPUs with poor bit shifts or few registers.
6658
6659   The multiply by inverse can be done two limbs at a time.  The
6660calculation is basically the same, but the inverse is two limbs and the
6661divisor treated as if padded with a low zero limb.  This means more
6662work, since the inverse will need a 2x2 multiply, but the four 1x1s to
6663do that are independent and can therefore be done partly or wholly in
6664parallel.  Likewise for a 2x1 calculating q*d.  The net effect is to
6665process two limbs with roughly the same two multiplies worth of latency
6666that one limb at a time gives.  This extends to 3 or 4 limbs at a time,
6667though the extra work to apply the inverse will almost certainly soon
6668reach the limits of multiplier throughput.
6669
6670   A similar approach in reverse can be taken to process just half a
6671limb at a time if the divisor is only a half limb.  In this case the
66721x1 multiply for the inverse effectively becomes two (1/2)x1 for each
6673limb, which can be a saving on CPUs with a fast half limb multiply, or
6674in fact if the only multiply is a half limb, and especially if it's not
6675pipelined.
6676
6677
6678File: gmp.info,  Node: Basecase Division,  Next: Divide and Conquer Division,  Prev: Single Limb Division,  Up: Division Algorithms
6679
668015.2.2 Basecase Division
6681------------------------
6682
6683Basecase NxM division is like long division done by hand, but in base
66842^mp_bits_per_limb.  See Knuth section 4.3.1 algorithm D, and
6685`mpn/generic/sb_divrem_mn.c'.
6686
6687   Briefly stated, while the dividend remains larger than the divisor,
6688a high quotient limb is formed and the Nx1 product q*d subtracted at
6689the top end of the dividend.  With a normalized divisor (most
6690significant bit set), each quotient limb can be formed with a 2x1
6691division and a 1x1 multiplication plus some subtractions.  The 2x1
6692division is by the high limb of the divisor and is done either with a
6693hardware divide or a multiply by inverse (the same as in *note Single
6694Limb Division::) whichever is faster.  Such a quotient is sometimes one
6695too big, requiring an addback of the divisor, but that happens rarely.
6696
6697   With Q=N-M being the number of quotient limbs, this is an O(Q*M)
6698algorithm and will run at a speed similar to a basecase QxM
6699multiplication, differing in fact only in the extra multiply and divide
6700for each of the Q quotient limbs.
6701
6702
6703File: gmp.info,  Node: Divide and Conquer Division,  Next: Block-Wise Barrett Division,  Prev: Basecase Division,  Up: Division Algorithms
6704
670515.2.3 Divide and Conquer Division
6706----------------------------------
6707
6708For divisors larger than `DC_DIV_QR_THRESHOLD', division is done by
6709dividing.  Or to be precise by a recursive divide and conquer algorithm
6710based on work by Moenck and Borodin, Jebelean, and Burnikel and Ziegler
6711(*note References::).
6712
6713   The algorithm consists essentially of recognising that a 2NxN
6714division can be done with the basecase division algorithm (*note
6715Basecase Division::), but using N/2 limbs as a base, not just a single
6716limb.  This way the multiplications that arise are (N/2)x(N/2) and can
6717take advantage of Karatsuba and higher multiplication algorithms (*note
6718Multiplication Algorithms::).  The two "digits" of the quotient are
6719formed by recursive Nx(N/2) divisions.
6720
6721   If the (N/2)x(N/2) multiplies are done with a basecase multiplication
6722then the work is about the same as a basecase division, but with more
6723function call overheads and with some subtractions separated from the
6724multiplies.  These overheads mean that it's only when N/2 is above
6725`MUL_TOOM22_THRESHOLD' that divide and conquer is of use.
6726
6727   `DC_DIV_QR_THRESHOLD' is based on the divisor size N, so it will be
6728somewhere above twice `MUL_TOOM22_THRESHOLD', but how much above
6729depends on the CPU.  An optimized `mpn_mul_basecase' can lower
6730`DC_DIV_QR_THRESHOLD' a little by offering a ready-made advantage over
6731repeated `mpn_submul_1' calls.
6732
6733   Divide and conquer is asymptotically O(M(N)*log(N)) where M(N) is
6734the time for an NxN multiplication done with FFTs.  The actual time is
6735a sum over multiplications of the recursed sizes, as can be seen near
6736the end of section 2.2 of Burnikel and Ziegler.  For example, within
6737the Toom-3 range, divide and conquer is 2.63*M(N).  With higher
6738algorithms the M(N) term improves and the multiplier tends to log(N).
6739In practice, at moderate to large sizes, a 2NxN division is about 2 to
67404 times slower than an NxN multiplication.
6741
6742
6743File: gmp.info,  Node: Block-Wise Barrett Division,  Next: Exact Division,  Prev: Divide and Conquer Division,  Up: Division Algorithms
6744
674515.2.4 Block-Wise Barrett Division
6746----------------------------------
6747
6748For the largest divisions, a block-wise Barrett division algorithm is
6749used.  Here, the divisor is inverted to a precision determined by the
6750relative size of the dividend and divisor.  Blocks of quotient limbs
6751are then generated by multiplying blocks from the dividend by the
6752inverse.
6753
6754   Our block-wise algorithm computes a smaller inverse than in the
6755plain Barrett algorithm.  For a 2n/n division, the inverse will be just
6756ceil(n/2) limbs.
6757
6758
6759File: gmp.info,  Node: Exact Division,  Next: Exact Remainder,  Prev: Block-Wise Barrett Division,  Up: Division Algorithms
6760
676115.2.5 Exact Division
6762---------------------
6763
6764A so-called exact division is when the dividend is known to be an exact
6765multiple of the divisor.  Jebelean's exact division algorithm uses this
6766knowledge to make some significant optimizations (*note References::).
6767
6768   The idea can be illustrated in decimal for example with 368154
6769divided by 543.  Because the low digit of the dividend is 4, the low
6770digit of the quotient must be 8.  This is arrived at from 4*7 mod 10,
6771using the fact 7 is the modular inverse of 3 (the low digit of the
6772divisor), since 3*7 == 1 mod 10.  So 8*543=4344 can be subtracted from
6773the dividend leaving 363810.  Notice the low digit has become zero.
6774
6775   The procedure is repeated at the second digit, with the next
6776quotient digit 7 (7 == 1*7 mod 10), subtracting 7*543=3801, leaving
6777325800.  And finally at the third digit with quotient digit 6 (8*7 mod
677810), subtracting 6*543=3258 leaving 0.  So the quotient is 678.
6779
6780   Notice however that the multiplies and subtractions don't need to
6781extend past the low three digits of the dividend, since that's enough
6782to determine the three quotient digits.  For the last quotient digit no
6783subtraction is needed at all.  On a 2NxN division like this one, only
6784about half the work of a normal basecase division is necessary.
6785
6786   For an NxM exact division producing Q=N-M quotient limbs, the saving
6787over a normal basecase division is in two parts.  Firstly, each of the
6788Q quotient limbs needs only one multiply, not a 2x1 divide and
6789multiply.  Secondly, the crossproducts are reduced when Q>M to
6790Q*M-M*(M+1)/2, or when Q<=M to Q*(Q-1)/2.  Notice the savings are
6791complementary.  If Q is big then many divisions are saved, or if Q is
6792small then the crossproducts reduce to a small number.
6793
6794   The modular inverse used is calculated efficiently by `binvert_limb'
6795in `gmp-impl.h'.  This does four multiplies for a 32-bit limb, or six
6796for a 64-bit limb.  `tune/modlinv.c' has some alternate implementations
6797that might suit processors better at bit twiddling than multiplying.
6798
6799   The sub-quadratic exact division described by Jebelean in "Exact
6800Division with Karatsuba Complexity" is not currently implemented.  It
6801uses a rearrangement similar to the divide and conquer for normal
6802division (*note Divide and Conquer Division::), but operating from low
6803to high.  A further possibility not currently implemented is
6804"Bidirectional Exact Integer Division" by Krandick and Jebelean which
6805forms quotient limbs from both the high and low ends of the dividend,
6806and can halve once more the number of crossproducts needed in a 2NxN
6807division.
6808
6809   A special case exact division by 3 exists in `mpn_divexact_by3',
6810supporting Toom-3 multiplication and `mpq' canonicalizations.  It forms
6811quotient digits with a multiply by the modular inverse of 3 (which is
6812`0xAA..AAB') and uses two comparisons to determine a borrow for the next
6813limb.  The multiplications don't need to be on the dependent chain, as
6814long as the effect of the borrows is applied, which can help chips with
6815pipelined multipliers.
6816
6817
6818File: gmp.info,  Node: Exact Remainder,  Next: Small Quotient Division,  Prev: Exact Division,  Up: Division Algorithms
6819
682015.2.6 Exact Remainder
6821----------------------
6822
6823If the exact division algorithm is done with a full subtraction at each
6824stage and the dividend isn't a multiple of the divisor, then low zero
6825limbs are produced but with a remainder in the high limbs.  For
6826dividend a, divisor d, quotient q, and b = 2^mp_bits_per_limb, this
6827remainder r is of the form
6828
6829     a = q*d + r*b^n
6830
6831   n represents the number of zero limbs produced by the subtractions,
6832that being the number of limbs produced for q.  r will be in the range
68330<=r<d and can be viewed as a remainder, but one shifted up by a factor
6834of b^n.
6835
6836   Carrying out full subtractions at each stage means the same number
6837of cross products must be done as a normal division, but there's still
6838some single limb divisions saved.  When d is a single limb some
6839simplifications arise, providing good speedups on a number of
6840processors.
6841
6842   The functions `mpn_divexact_by3', `mpn_modexact_1_odd' and the
6843internal `mpn_redc_X' functions differ subtly in how they return r,
6844leading to some negations in the above formula, but all are essentially
6845the same.
6846
6847   Clearly r is zero when a is a multiple of d, and this leads to
6848divisibility or congruence tests which are potentially more efficient
6849than a normal division.
6850
6851   The factor of b^n on r can be ignored in a GCD when d is odd, hence
6852the use of `mpn_modexact_1_odd' by `mpn_gcd_1' and `mpz_kronecker_ui'
6853etc (*note Greatest Common Divisor Algorithms::).
6854
6855   Montgomery's REDC method for modular multiplications uses operands
6856of the form of x*b^-n and y*b^-n and on calculating (x*b^-n)*(y*b^-n)
6857uses the factor of b^n in the exact remainder to reach a product in the
6858same form (x*y)*b^-n (*note Modular Powering Algorithm::).
6859
6860   Notice that r generally gives no useful information about the
6861ordinary remainder a mod d since b^n mod d could be anything.  If
6862however b^n == 1 mod d, then r is the negative of the ordinary
6863remainder.  This occurs whenever d is a factor of b^n-1, as for example
6864with 3 in `mpn_divexact_by3'.  For a 32 or 64 bit limb other such
6865factors include 5, 17 and 257, but no particular use has been found for
6866this.
6867
6868
6869File: gmp.info,  Node: Small Quotient Division,  Prev: Exact Remainder,  Up: Division Algorithms
6870
687115.2.7 Small Quotient Division
6872------------------------------
6873
6874An NxM division where the number of quotient limbs Q=N-M is small can
6875be optimized somewhat.
6876
6877   An ordinary basecase division normalizes the divisor by shifting it
6878to make the high bit set, shifting the dividend accordingly, and
6879shifting the remainder back down at the end of the calculation.  This
6880is wasteful if only a few quotient limbs are to be formed.  Instead a
6881division of just the top 2*Q limbs of the dividend by the top Q limbs
6882of the divisor can be used to form a trial quotient.  This requires
6883only those limbs normalized, not the whole of the divisor and dividend.
6884
6885   A multiply and subtract then applies the trial quotient to the M-Q
6886unused limbs of the divisor and N-Q dividend limbs (which includes Q
6887limbs remaining from the trial quotient division).  The starting trial
6888quotient can be 1 or 2 too big, but all cases of 2 too big and most
6889cases of 1 too big are detected by first comparing the most significant
6890limbs that will arise from the subtraction.  An addback is done if the
6891quotient still turns out to be 1 too big.
6892
6893   This whole procedure is essentially the same as one step of the
6894basecase algorithm done in a Q limb base, though with the trial
6895quotient test done only with the high limbs, not an entire Q limb
6896"digit" product.  The correctness of this weaker test can be
6897established by following the argument of Knuth section 4.3.1 exercise
689820 but with the v2*q>b*r+u2 condition appropriately relaxed.
6899
6900
6901File: gmp.info,  Node: Greatest Common Divisor Algorithms,  Next: Powering Algorithms,  Prev: Division Algorithms,  Up: Algorithms
6902
690315.3 Greatest Common Divisor
6904============================
6905
6906* Menu:
6907
6908* Binary GCD::
6909* Lehmer's Algorithm::
6910* Subquadratic GCD::
6911* Extended GCD::
6912* Jacobi Symbol::
6913
6914
6915File: gmp.info,  Node: Binary GCD,  Next: Lehmer's Algorithm,  Prev: Greatest Common Divisor Algorithms,  Up: Greatest Common Divisor Algorithms
6916
691715.3.1 Binary GCD
6918-----------------
6919
6920At small sizes GMP uses an O(N^2) binary style GCD.  This is described
6921in many textbooks, for example Knuth section 4.5.2 algorithm B.  It
6922simply consists of successively reducing odd operands a and b using
6923
6924     a,b = abs(a-b),min(a,b)
6925     strip factors of 2 from a
6926
6927   The Euclidean GCD algorithm, as per Knuth algorithms E and A,
6928repeatedly computes the quotient q = floor(a/b) and replaces a,b by v,
6929u - q v. The binary algorithm has so far been found to be faster than
6930the Euclidean algorithm everywhere.  One reason the binary method does
6931well is that the implied quotient at each step is usually small, so
6932often only one or two subtractions are needed to get the same effect as
6933a division.  Quotients 1, 2 and 3 for example occur 67.7% of the time,
6934see Knuth section 4.5.3 Theorem E.
6935
6936   When the implied quotient is large, meaning b is much smaller than
6937a, then a division is worthwhile.  This is the basis for the initial a
6938mod b reductions in `mpn_gcd' and `mpn_gcd_1' (the latter for both Nx1
6939and 1x1 cases).  But after that initial reduction, big quotients occur
6940too rarely to make it worth checking for them.
6941
6942
6943   The final 1x1 GCD in `mpn_gcd_1' is done in the generic C code as
6944described above.  For two N-bit operands, the algorithm takes about
69450.68 iterations per bit.  For optimum performance some attention needs
6946to be paid to the way the factors of 2 are stripped from a.
6947
6948   Firstly it may be noted that in twos complement the number of low
6949zero bits on a-b is the same as b-a, so counting or testing can begin on
6950a-b without waiting for abs(a-b) to be determined.
6951
6952   A loop stripping low zero bits tends not to branch predict well,
6953since the condition is data dependent.  But on average there's only a
6954few low zeros, so an option is to strip one or two bits arithmetically
6955then loop for more (as done for AMD K6).  Or use a lookup table to get
6956a count for several bits then loop for more (as done for AMD K7).  An
6957alternative approach is to keep just one of a or b odd and iterate
6958
6959     a,b = abs(a-b), min(a,b)
6960     a = a/2 if even
6961     b = b/2 if even
6962
6963   This requires about 1.25 iterations per bit, but stripping of a
6964single bit at each step avoids any branching.  Repeating the bit strip
6965reduces to about 0.9 iterations per bit, which may be a worthwhile
6966tradeoff.
6967
6968   Generally with the above approaches a speed of perhaps 6 cycles per
6969bit can be achieved, which is still not terribly fast with for instance
6970a 64-bit GCD taking nearly 400 cycles.  It's this sort of time which
6971means it's not usually advantageous to combine a set of divisibility
6972tests into a GCD.
6973
6974   Currently, the binary algorithm is used for GCD only when N < 3.
6975
6976
6977File: gmp.info,  Node: Lehmer's Algorithm,  Next: Subquadratic GCD,  Prev: Binary GCD,  Up: Greatest Common Divisor Algorithms
6978
697915.3.2 Lehmer's algorithm
6980-------------------------
6981
6982Lehmer's improvement of the Euclidean algorithms is based on the
6983observation that the initial part of the quotient sequence depends only
6984on the most significant parts of the inputs. The variant of Lehmer's
6985algorithm used in GMP splits off the most significant two limbs, as
6986suggested, e.g., in "A Double-Digit Lehmer-Euclid Algorithm" by
6987Jebelean (*note References::). The quotients of two double-limb inputs
6988are collected as a 2 by 2 matrix with single-limb elements. This is
6989done by the function `mpn_hgcd2'. The resulting matrix is applied to
6990the inputs using `mpn_mul_1' and `mpn_submul_1'. Each iteration usually
6991reduces the inputs by almost one limb. In the rare case of a large
6992quotient, no progress can be made by examining just the most
6993significant two limbs, and the quotient is computed using plain
6994division.
6995
6996   The resulting algorithm is asymptotically O(N^2), just as the
6997Euclidean algorithm and the binary algorithm. The quadratic part of the
6998work are the calls to `mpn_mul_1' and `mpn_submul_1'. For small sizes,
6999the linear work is also significant. There are roughly N calls to the
7000`mpn_hgcd2' function. This function uses a couple of important
7001optimizations:
7002
7003   * It uses the same relaxed notion of correctness as `mpn_hgcd' (see
7004     next section). This means that when called with the most
7005     significant two limbs of two large numbers, the returned matrix
7006     does not always correspond exactly to the initial quotient
7007     sequence for the two large numbers; the final quotient may
7008     sometimes be one off.
7009
7010   * It takes advantage of the fact the quotients are usually small.
7011     The division operator is not used, since the corresponding
7012     assembler instruction is very slow on most architectures. (This
7013     code could probably be improved further, it uses many branches
7014     that are unfriendly to prediction).
7015
7016   * It switches from double-limb calculations to single-limb
7017     calculations half-way through, when the input numbers have been
7018     reduced in size from two limbs to one and a half.
7019
7020
7021
7022File: gmp.info,  Node: Subquadratic GCD,  Next: Extended GCD,  Prev: Lehmer's Algorithm,  Up: Greatest Common Divisor Algorithms
7023
702415.3.3 Subquadratic GCD
7025-----------------------
7026
7027For inputs larger than `GCD_DC_THRESHOLD', GCD is computed via the HGCD
7028(Half GCD) function, as a generalization to Lehmer's algorithm.
7029
7030   Let the inputs a,b be of size N limbs each. Put S = floor(N/2) + 1.
7031Then HGCD(a,b) returns a transformation matrix T with non-negative
7032elements, and reduced numbers (c;d) = T^-1 (a;b). The reduced numbers
7033c,d must be larger than S limbs, while their difference abs(c-d) must
7034fit in S limbs. The matrix elements will also be of size roughly N/2.
7035
7036   The HGCD base case uses Lehmer's algorithm, but with the above stop
7037condition that returns reduced numbers and the corresponding
7038transformation matrix half-way through. For inputs larger than
7039`HGCD_THRESHOLD', HGCD is computed recursively, using the divide and
7040conquer algorithm in "On Sch�nhage's algorithm and subquadratic integer
7041GCD computation" by M�ller (*note References::). The recursive
7042algorithm consists of these main steps.
7043
7044   * Call HGCD recursively, on the most significant N/2 limbs. Apply the
7045     resulting matrix T_1 to the full numbers, reducing them to a size
7046     just above 3N/2.
7047
7048   * Perform a small number of division or subtraction steps to reduce
7049     the numbers to size below 3N/2. This is essential mainly for the
7050     unlikely case of large quotients.
7051
7052   * Call HGCD recursively, on the most significant N/2 limbs of the
7053     reduced numbers. Apply the resulting matrix T_2 to the full
7054     numbers, reducing them to a size just above N/2.
7055
7056   * Compute T = T_1 T_2.
7057
7058   * Perform a small number of division and subtraction steps to
7059     satisfy the requirements, and return.
7060
7061   GCD is then implemented as a loop around HGCD, similarly to Lehmer's
7062algorithm. Where Lehmer repeatedly chops off the top two limbs, calls
7063`mpn_hgcd2', and applies the resulting matrix to the full numbers, the
7064subquadratic GCD chops off the most significant third of the limbs (the
7065proportion is a tuning parameter, and 1/3 seems to be more efficient
7066than, e.g, 1/2), calls `mpn_hgcd', and applies the resulting matrix.
7067Once the input numbers are reduced to size below `GCD_DC_THRESHOLD',
7068Lehmer's algorithm is used for the rest of the work.
7069
7070   The asymptotic running time of both HGCD and GCD is O(M(N)*log(N)),
7071where M(N) is the time for multiplying two N-limb numbers.
7072
7073
7074
7075Local Variables:
7076coding: iso-8859-1
7077End:
7078