gmp.texi
上传用户:qaz666999
上传日期:2022-08-06
资源大小:2570k
文件大小:420k
- For C++, add @samp{--enable-cxx CXXFLAGS=-g}.
- @item Checker
- @cindex Checker
- @cindex GCC Checker
- The GCC checker (@uref{http://savannah.nongnu.org/projects/checker/}) can be
- used with GMP@. It contains a stub library which means GMP applications
- compiled with checker can use a normal GMP build.
- A build of GMP with checking within GMP itself can be made. This will run
- very very slowly. On GNU/Linux for example,
- @cindex @command{checkergcc}
- @example
- ./configure --host=none-pc-linux-gnu CC=checkergcc
- @end example
- @samp{--host=none} must be used, since the GMP assembly code doesn't support
- the checking scheme. The GMP C++ features cannot be used, since current
- versions of checker (0.9.9.1) don't yet support the standard C++ library.
- @item Valgrind
- @cindex Valgrind
- The valgrind program (@uref{http://valgrind.org/}) is a memory
- checker for x86s. It translates and emulates machine instructions to do
- strong checks for uninitialized data (at the level of individual bits), memory
- accesses through bad pointers, and memory leaks.
- Recent versions of Valgrind are getting support for MMX and SSE/SSE2
- instructions, for past versions GMP will need to be configured not to use
- those, ie.@: for an x86 without them (for instance plain @samp{i486}).
- @item Other Problems
- Any suspected bug in GMP itself should be isolated to make sure it's not an
- application problem, see @ref{Reporting Bugs}.
- @end table
- @node Profiling, Autoconf, Debugging, GMP Basics
- @section Profiling
- @cindex Profiling
- @cindex Execution profiling
- @cindex @code{--enable-profiling}
- Running a program under a profiler is a good way to find where it's spending
- most time and where improvements can be best sought. The profiling choices
- for a GMP build are as follows.
- @table @asis
- @item @samp{--disable-profiling}
- The default is to add nothing special for profiling.
- It should be possible to just compile the mainline of a program with @code{-p}
- and use @command{prof} to get a profile consisting of timer-based sampling of
- the program counter. Most of the GMP assembly code has the necessary symbol
- information.
- This approach has the advantage of minimizing interference with normal program
- operation, but on most systems the resolution of the sampling is quite low (10
- milliseconds for instance), requiring long runs to get accurate information.
- @item @samp{--enable-profiling=prof}
- @cindex @code{prof}
- Build with support for the system @command{prof}, which means @samp{-p} added
- to the @samp{CFLAGS}.
- This provides call counting in addition to program counter sampling, which
- allows the most frequently called routines to be identified, and an average
- time spent in each routine to be determined.
- The x86 assembly code has support for this option, but on other processors
- the assembly routines will be as if compiled without @samp{-p} and therefore
- won't appear in the call counts.
- On some systems, such as GNU/Linux, @samp{-p} in fact means @samp{-pg} and in
- this case @samp{--enable-profiling=gprof} described below should be used
- instead.
- @item @samp{--enable-profiling=gprof}
- @cindex @code{gprof}
- Build with support for @command{gprof}, which means @samp{-pg} added to the
- @samp{CFLAGS}.
- This provides call graph construction in addition to call counting and program
- counter sampling, which makes it possible to count calls coming from different
- locations. For example the number of calls to @code{mpn_mul} from
- @code{mpz_mul} versus the number from @code{mpf_mul}. The program counter
- sampling is still flat though, so only a total time in @code{mpn_mul} would be
- accumulated, not a separate amount for each call site.
- The x86 assembly code has support for this option, but on other processors
- the assembly routines will be as if compiled without @samp{-pg} and therefore
- not be included in the call counts.
- On x86 and m68k systems @samp{-pg} and @samp{-fomit-frame-pointer} are
- incompatible, so the latter is omitted from the default flags in that case,
- which might result in poorer code generation.
- Incidentally, it should be possible to use the @command{gprof} program with a
- plain @samp{--enable-profiling=prof} build. But in that case only the
- @samp{gprof -p} flat profile and call counts can be expected to be valid, not
- the @samp{gprof -q} call graph.
- @item @samp{--enable-profiling=instrument}
- @cindex @code{-finstrument-functions}
- @cindex @code{instrument-functions}
- Build with the GCC option @samp{-finstrument-functions} added to the
- @samp{CFLAGS} (@pxref{Code Gen Options,, Options for Code Generation, gcc,
- Using the GNU Compiler Collection (GCC)}).
- This inserts special instrumenting calls at the start and end of each
- function, allowing exact timing and full call graph construction.
- This instrumenting is not normally a standard system feature and will require
- support from an external library, such as
- @cindex FunctionCheck
- @cindex fnccheck
- @display
- @uref{http://sourceforge.net/projects/fnccheck/}
- @end display
- This should be included in @samp{LIBS} during the GMP configure so that test
- programs will link. For example,
- @example
- ./configure --enable-profiling=instrument LIBS=-lfc
- @end example
- On a GNU system the C library provides dummy instrumenting functions, so
- programs compiled with this option will link. In this case it's only
- necessary to ensure the correct library is added when linking an application.
- The x86 assembly code supports this option, but on other processors the
- assembly routines will be as if compiled without
- @samp{-finstrument-functions} meaning time spent in them will effectively be
- attributed to their caller.
- @end table
- @node Autoconf, Emacs, Profiling, GMP Basics
- @section Autoconf
- @cindex Autoconf
- Autoconf based applications can easily check whether GMP is installed. The
- only thing to be noted is that GMP library symbols from version 3 onwards have
- prefixes like @code{__gmpz}. The following therefore would be a simple test,
- @cindex @code{AC_CHECK_LIB}
- @example
- AC_CHECK_LIB(gmp, __gmpz_init)
- @end example
- This just uses the default @code{AC_CHECK_LIB} actions for found or not found,
- but an application that must have GMP would want to generate an error if not
- found. For example,
- @example
- AC_CHECK_LIB(gmp, __gmpz_init, ,
- [AC_MSG_ERROR([GNU MP not found, see http://gmplib.org/])])
- @end example
- If functions added in some particular version of GMP are required, then one of
- those can be used when checking. For example @code{mpz_mul_si} was added in
- GMP 3.1,
- @example
- AC_CHECK_LIB(gmp, __gmpz_mul_si, ,
- [AC_MSG_ERROR(
- [GNU MP not found, or not 3.1 or up, see http://gmplib.org/])])
- @end example
- An alternative would be to test the version number in @file{gmp.h} using say
- @code{AC_EGREP_CPP}. That would make it possible to test the exact version,
- if some particular sub-minor release is known to be necessary.
- In general it's recommended that applications should simply demand a new
- enough GMP rather than trying to provide supplements for features not
- available in past versions.
- Occasionally an application will need or want to know the size of a type at
- configuration or preprocessing time, not just with @code{sizeof} in the code.
- This can be done in the normal way with @code{mp_limb_t} etc, but GMP 4.0 or
- up is best for this, since prior versions needed certain @samp{-D} defines on
- systems using a @code{long long} limb. The following would suit Autoconf 2.50
- or up,
- @example
- AC_CHECK_SIZEOF(mp_limb_t, , [#include <gmp.h>])
- @end example
- @node Emacs, , Autoconf, GMP Basics
- @section Emacs
- @cindex Emacs
- @cindex @code{info-lookup-symbol}
- @key{C-h C-i} (@code{info-lookup-symbol}) is a good way to find documentation
- on C functions while editing (@pxref{Info Lookup, , Info Documentation Lookup,
- emacs, The Emacs Editor}).
- The GMP manual can be included in such lookups by putting the following in
- your @file{.emacs},
- @c This isn't pretty, but there doesn't seem to be a better way (in emacs
- @c 21.2 at least). info-lookup->mode-value could be used for the "assoc"s,
- @c but that function isn't documented, whereas info-lookup-alist is.
- @c
- @example
- (eval-after-load "info-look"
- '(let ((mode-value (assoc 'c-mode (assoc 'symbol info-lookup-alist))))
- (setcar (nthcdr 3 mode-value)
- (cons '("(gmp)Function Index" nil "^ -.* " "\>")
- (nth 3 mode-value)))))
- @end example
- @node Reporting Bugs, Integer Functions, GMP Basics, Top
- @comment node-name, next, previous, up
- @chapter Reporting Bugs
- @cindex Reporting bugs
- @cindex Bug reporting
- If you think you have found a bug in the GMP library, please investigate it
- and report it. We have made this library available to you, and it is not too
- much to ask you to report the bugs you find.
- Before you report a bug, check it's not already addressed in @ref{Known Build
- Problems}, or perhaps @ref{Notes for Particular Systems}. You may also want
- to check @uref{http://gmplib.org/} for patches for this release.
- Please include the following in any report,
- @itemize @bullet
- @item
- The GMP version number, and if pre-packaged or patched then say so.
- @item
- A test program that makes it possible for us to reproduce the bug. Include
- instructions on how to run the program.
- @item
- A description of what is wrong. If the results are incorrect, in what way.
- If you get a crash, say so.
- @item
- If you get a crash, include a stack backtrace from the debugger if it's
- informative (@samp{where} in @command{gdb}, or @samp{$C} in @command{adb}).
- @item
- Please do not send core dumps, executables or @command{strace}s.
- @item
- The configuration options you used when building GMP, if any.
- @item
- The name of the compiler and its version. For @command{gcc}, get the version
- with @samp{gcc -v}, otherwise perhaps @samp{what `which cc`}, or similar.
- @item
- The output from running @samp{uname -a}.
- @item
- The output from running @samp{./config.guess}, and from running
- @samp{./configfsf.guess} (might be the same).
- @item
- If the bug is related to @samp{configure}, then the compressed contents of
- @file{config.log}.
- @item
- If the bug is related to an @file{asm} file not assembling, then the contents
- of @file{config.m4} and the offending line or lines from the temporary
- @file{mpn/tmp-<file>.s}.
- @end itemize
- Please make an effort to produce a self-contained report, with something
- definite that can be tested or debugged. Vague queries or piecemeal messages
- are difficult to act on and don't help the development effort.
- It is not uncommon that an observed problem is actually due to a bug in the
- compiler; the GMP code tends to explore interesting corners in compilers.
- If your bug report is good, we will do our best to help you get a corrected
- version of the library; if the bug report is poor, we won't do anything about
- it (except maybe ask you to send a better report).
- Send your report to: @email{gmp-bugs@@gmplib.org}.
- If you think something in this manual is unclear, or downright incorrect, or if
- the language needs to be improved, please send a note to the same address.
- @node Integer Functions, Rational Number Functions, Reporting Bugs, Top
- @comment node-name, next, previous, up
- @chapter Integer Functions
- @cindex Integer functions
- This chapter describes the GMP functions for performing integer arithmetic.
- These functions start with the prefix @code{mpz_}.
- GMP integers are stored in objects of type @code{mpz_t}.
- @menu
- * Initializing Integers::
- * Assigning Integers::
- * Simultaneous Integer Init & Assign::
- * Converting Integers::
- * Integer Arithmetic::
- * Integer Division::
- * Integer Exponentiation::
- * Integer Roots::
- * Number Theoretic Functions::
- * Integer Comparisons::
- * Integer Logic and Bit Fiddling::
- * I/O of Integers::
- * Integer Random Numbers::
- * Integer Import and Export::
- * Miscellaneous Integer Functions::
- * Integer Special Functions::
- @end menu
- @node Initializing Integers, Assigning Integers, Integer Functions, Integer Functions
- @comment node-name, next, previous, up
- @section Initialization Functions
- @cindex Integer initialization functions
- @cindex Initialization functions
- The functions for integer arithmetic assume that all integer objects are
- initialized. You do that by calling the function @code{mpz_init}. For
- example,
- @example
- @{
- mpz_t integ;
- mpz_init (integ);
- @dots{}
- mpz_add (integ, @dots{});
- @dots{}
- mpz_sub (integ, @dots{});
- /* Unless the program is about to exit, do ... */
- mpz_clear (integ);
- @}
- @end example
- As you can see, you can store new values any number of times, once an
- object is initialized.
- @deftypefun void mpz_init (mpz_t @var{x})
- Initialize @var{x}, and set its value to 0.
- @end deftypefun
- @deftypefun void mpz_inits (mpz_t @var{x}, ...)
- Initialize a NULL-terminated list of @code{mpz_t} variables, and set their
- values to 0.
- @end deftypefun
- @deftypefun void mpz_init2 (mpz_t @var{x}, mp_bitcnt_t @var{n})
- Initialize @var{x}, with space for @var{n}-bit numbers, and set its value to 0.
- Calling this function instead of @code{mpz_init} or @code{mpz_inits} is never
- necessary; reallocation is handled automatically by GMP when needed.
- @var{n} is only the initial space, @var{x} will grow automatically in
- the normal way, if necessary, for subsequent values stored. @code{mpz_init2}
- makes it possible to avoid such reallocations if a maximum size is known in
- advance.
- @end deftypefun
- @deftypefun void mpz_clear (mpz_t @var{x})
- Free the space occupied by @var{x}. Call this function for all @code{mpz_t}
- variables when you are done with them.
- @end deftypefun
- @deftypefun void mpz_clears (mpz_t @var{x}, ...)
- Free the space occupied by a NULL-terminated list of @code{mpz_t} variables.
- @end deftypefun
- @deftypefun void mpz_realloc2 (mpz_t @var{x}, mp_bitcnt_t @var{n})
- Change the space allocated for @var{x} to @var{n} bits. The value in @var{x}
- is preserved if it fits, or is set to 0 if not.
- Calling this function is never necessary; reallocation is handled automatically
- by GMP when needed. But this function can be used to increase the space for a
- variable in order to avoid repeated automatic reallocations, or to decrease it
- to give memory back to the heap.
- @end deftypefun
- @node Assigning Integers, Simultaneous Integer Init & Assign, Initializing Integers, Integer Functions
- @comment node-name, next, previous, up
- @section Assignment Functions
- @cindex Integer assignment functions
- @cindex Assignment functions
- These functions assign new values to already initialized integers
- (@pxref{Initializing Integers}).
- @deftypefun void mpz_set (mpz_t @var{rop}, mpz_t @var{op})
- @deftypefunx void mpz_set_ui (mpz_t @var{rop}, unsigned long int @var{op})
- @deftypefunx void mpz_set_si (mpz_t @var{rop}, signed long int @var{op})
- @deftypefunx void mpz_set_d (mpz_t @var{rop}, double @var{op})
- @deftypefunx void mpz_set_q (mpz_t @var{rop}, mpq_t @var{op})
- @deftypefunx void mpz_set_f (mpz_t @var{rop}, mpf_t @var{op})
- Set the value of @var{rop} from @var{op}.
- @code{mpz_set_d}, @code{mpz_set_q} and @code{mpz_set_f} truncate @var{op} to
- make it an integer.
- @end deftypefun
- @deftypefun int mpz_set_str (mpz_t @var{rop}, char *@var{str}, int @var{base})
- Set the value of @var{rop} from @var{str}, a null-terminated C string in base
- @var{base}. White space is allowed in the string, and is simply ignored.
- The @var{base} may vary from 2 to 62, or if @var{base} is 0, then the leading
- characters are used: @code{0x} and @code{0X} for hexadecimal, @code{0b} and
- @code{0B} for binary, @code{0} for octal, or decimal otherwise.
- For bases up to 36, case is ignored; upper-case and lower-case letters have
- the same value. For bases 37 to 62, upper-case letter represent the usual
- 10..35 while lower-case letter represent 36..61.
- This function returns 0 if the entire string is a valid number in base
- @var{base}. Otherwise it returns @minus{}1.
- @c
- @c It turns out that it is not entirely true that this function ignores
- @c white-space. It does ignore it between digits, but not after a minus sign
- @c or within or after ``0x''. Some thought was given to disallowing all
- @c whitespace, but that would be an incompatible change, whitespace has been
- @c documented as ignored ever since GMP 1.
- @c
- @end deftypefun
- @deftypefun void mpz_swap (mpz_t @var{rop1}, mpz_t @var{rop2})
- Swap the values @var{rop1} and @var{rop2} efficiently.
- @end deftypefun
- @node Simultaneous Integer Init & Assign, Converting Integers, Assigning Integers, Integer Functions
- @comment node-name, next, previous, up
- @section Combined Initialization and Assignment Functions
- @cindex Integer assignment functions
- @cindex Assignment functions
- @cindex Integer initialization functions
- @cindex Initialization functions
- For convenience, GMP provides a parallel series of initialize-and-set functions
- which initialize the output and then store the value there. These functions'
- names have the form @code{mpz_init_set@dots{}}
- Here is an example of using one:
- @example
- @{
- mpz_t pie;
- mpz_init_set_str (pie, "3141592653589793238462643383279502884", 10);
- @dots{}
- mpz_sub (pie, @dots{});
- @dots{}
- mpz_clear (pie);
- @}
- @end example
- @noindent
- Once the integer has been initialized by any of the @code{mpz_init_set@dots{}}
- functions, it can be used as the source or destination operand for the ordinary
- integer functions. Don't use an initialize-and-set function on a variable
- already initialized!
- @deftypefun void mpz_init_set (mpz_t @var{rop}, mpz_t @var{op})
- @deftypefunx void mpz_init_set_ui (mpz_t @var{rop}, unsigned long int @var{op})
- @deftypefunx void mpz_init_set_si (mpz_t @var{rop}, signed long int @var{op})
- @deftypefunx void mpz_init_set_d (mpz_t @var{rop}, double @var{op})
- Initialize @var{rop} with limb space and set the initial numeric value from
- @var{op}.
- @end deftypefun
- @deftypefun int mpz_init_set_str (mpz_t @var{rop}, char *@var{str}, int @var{base})
- Initialize @var{rop} and set its value like @code{mpz_set_str} (see its
- documentation above for details).
- If the string is a correct base @var{base} number, the function returns 0;
- if an error occurs it returns @minus{}1. @var{rop} is initialized even if
- an error occurs. (I.e., you have to call @code{mpz_clear} for it.)
- @end deftypefun
- @node Converting Integers, Integer Arithmetic, Simultaneous Integer Init & Assign, Integer Functions
- @comment node-name, next, previous, up
- @section Conversion Functions
- @cindex Integer conversion functions
- @cindex Conversion functions
- This section describes functions for converting GMP integers to standard C
- types. Functions for converting @emph{to} GMP integers are described in
- @ref{Assigning Integers} and @ref{I/O of Integers}.
- @deftypefun {unsigned long int} mpz_get_ui (mpz_t @var{op})
- Return the value of @var{op} as an @code{unsigned long}.
- If @var{op} is too big to fit an @code{unsigned long} then just the least
- significant bits that do fit are returned. The sign of @var{op} is ignored,
- only the absolute value is used.
- @end deftypefun
- @deftypefun {signed long int} mpz_get_si (mpz_t @var{op})
- If @var{op} fits into a @code{signed long int} return the value of @var{op}.
- Otherwise return the least significant part of @var{op}, with the same sign
- as @var{op}.
- If @var{op} is too big to fit in a @code{signed long int}, the returned
- result is probably not very useful. To find out if the value will fit, use
- the function @code{mpz_fits_slong_p}.
- @end deftypefun
- @deftypefun double mpz_get_d (mpz_t @var{op})
- Convert @var{op} to a @code{double}, truncating if necessary (ie.@: rounding
- towards zero).
- If the exponent from the conversion is too big, the result is system
- dependent. An infinity is returned where available. A hardware overflow trap
- may or may not occur.
- @end deftypefun
- @deftypefun double mpz_get_d_2exp (signed long int *@var{exp}, mpz_t @var{op})
- Convert @var{op} to a @code{double}, truncating if necessary (ie.@: rounding
- towards zero), and returning the exponent separately.
- The return value is in the range @math{0.5@le{}@GMPabs{@var{d}}<1} and the
- exponent is stored to @code{*@var{exp}}. @m{@var{d} * 2^{exp}, @var{d} *
- 2^@var{exp}} is the (truncated) @var{op} value. If @var{op} is zero, the
- return is @math{0.0} and 0 is stored to @code{*@var{exp}}.
- @cindex @code{frexp}
- This is similar to the standard C @code{frexp} function (@pxref{Normalization
- Functions,,, libc, The GNU C Library Reference Manual}).
- @end deftypefun
- @deftypefun {char *} mpz_get_str (char *@var{str}, int @var{base}, mpz_t @var{op})
- Convert @var{op} to a string of digits in base @var{base}. The base argument
- may vary from 2 to 62 or from @minus{}2 to @minus{}36.
- For @var{base} in the range 2..36, digits and lower-case letters are used; for
- @minus{}2..@minus{}36, digits and upper-case letters are used; for 37..62,
- digits, upper-case letters, and lower-case letters (in that significance order)
- are used.
- If @var{str} is @code{NULL}, the result string is allocated using the current
- allocation function (@pxref{Custom Allocation}). The block will be
- @code{strlen(str)+1} bytes, that being exactly enough for the string and
- null-terminator.
- If @var{str} is not @code{NULL}, it should point to a block of storage large
- enough for the result, that being @code{mpz_sizeinbase (@var{op}, @var{base})
- + 2}. The two extra bytes are for a possible minus sign, and the
- null-terminator.
- A pointer to the result string is returned, being either the allocated block,
- or the given @var{str}.
- @end deftypefun
- @need 2000
- @node Integer Arithmetic, Integer Division, Converting Integers, Integer Functions
- @comment node-name, next, previous, up
- @section Arithmetic Functions
- @cindex Integer arithmetic functions
- @cindex Arithmetic functions
- @deftypefun void mpz_add (mpz_t @var{rop}, mpz_t @var{op1}, mpz_t @var{op2})
- @deftypefunx void mpz_add_ui (mpz_t @var{rop}, mpz_t @var{op1}, unsigned long int @var{op2})
- Set @var{rop} to @math{@var{op1} + @var{op2}}.
- @end deftypefun
- @deftypefun void mpz_sub (mpz_t @var{rop}, mpz_t @var{op1}, mpz_t @var{op2})
- @deftypefunx void mpz_sub_ui (mpz_t @var{rop}, mpz_t @var{op1}, unsigned long int @var{op2})
- @deftypefunx void mpz_ui_sub (mpz_t @var{rop}, unsigned long int @var{op1}, mpz_t @var{op2})
- Set @var{rop} to @var{op1} @minus{} @var{op2}.
- @end deftypefun
- @deftypefun void mpz_mul (mpz_t @var{rop}, mpz_t @var{op1}, mpz_t @var{op2})
- @deftypefunx void mpz_mul_si (mpz_t @var{rop}, mpz_t @var{op1}, long int @var{op2})
- @deftypefunx void mpz_mul_ui (mpz_t @var{rop}, mpz_t @var{op1}, unsigned long int @var{op2})
- Set @var{rop} to @math{@var{op1} @GMPtimes{} @var{op2}}.
- @end deftypefun
- @deftypefun void mpz_addmul (mpz_t @var{rop}, mpz_t @var{op1}, mpz_t @var{op2})
- @deftypefunx void mpz_addmul_ui (mpz_t @var{rop}, mpz_t @var{op1}, unsigned long int @var{op2})
- Set @var{rop} to @math{@var{rop} + @var{op1} @GMPtimes{} @var{op2}}.
- @end deftypefun
- @deftypefun void mpz_submul (mpz_t @var{rop}, mpz_t @var{op1}, mpz_t @var{op2})
- @deftypefunx void mpz_submul_ui (mpz_t @var{rop}, mpz_t @var{op1}, unsigned long int @var{op2})
- Set @var{rop} to @math{@var{rop} - @var{op1} @GMPtimes{} @var{op2}}.
- @end deftypefun
- @deftypefun void mpz_mul_2exp (mpz_t @var{rop}, mpz_t @var{op1}, mp_bitcnt_t @var{op2})
- @cindex Bit shift left
- Set @var{rop} to @m{@var{op1} times 2^{op2}, @var{op1} times 2 raised to
- @var{op2}}. This operation can also be defined as a left shift by @var{op2}
- bits.
- @end deftypefun
- @deftypefun void mpz_neg (mpz_t @var{rop}, mpz_t @var{op})
- Set @var{rop} to @minus{}@var{op}.
- @end deftypefun
- @deftypefun void mpz_abs (mpz_t @var{rop}, mpz_t @var{op})
- Set @var{rop} to the absolute value of @var{op}.
- @end deftypefun
- @need 2000
- @node Integer Division, Integer Exponentiation, Integer Arithmetic, Integer Functions
- @section Division Functions
- @cindex Integer division functions
- @cindex Division functions
- Division is undefined if the divisor is zero. Passing a zero divisor to the
- division or modulo functions (including the modular powering functions
- @code{mpz_powm} and @code{mpz_powm_ui}), will cause an intentional division by
- zero. This lets a program handle arithmetic exceptions in these functions the
- same way as for normal C @code{int} arithmetic.
- @c Separate deftypefun groups for cdiv, fdiv and tdiv produce a blank line
- @c between each, and seem to let tex do a better job of page breaks than an
- @c @sp 1 in the middle of one big set.
- @deftypefun void mpz_cdiv_q (mpz_t @var{q}, mpz_t @var{n}, mpz_t @var{d})
- @deftypefunx void mpz_cdiv_r (mpz_t @var{r}, mpz_t @var{n}, mpz_t @var{d})
- @deftypefunx void mpz_cdiv_qr (mpz_t @var{q}, mpz_t @var{r}, mpz_t @var{n}, mpz_t @var{d})
- @maybepagebreak
- @deftypefunx {unsigned long int} mpz_cdiv_q_ui (mpz_t @var{q}, mpz_t @var{n}, @w{unsigned long int @var{d}})
- @deftypefunx {unsigned long int} mpz_cdiv_r_ui (mpz_t @var{r}, mpz_t @var{n}, @w{unsigned long int @var{d}})
- @deftypefunx {unsigned long int} mpz_cdiv_qr_ui (mpz_t @var{q}, mpz_t @var{r}, @w{mpz_t @var{n}}, @w{unsigned long int @var{d}})
- @deftypefunx {unsigned long int} mpz_cdiv_ui (mpz_t @var{n}, @w{unsigned long int @var{d}})
- @maybepagebreak
- @deftypefunx void mpz_cdiv_q_2exp (mpz_t @var{q}, mpz_t @var{n}, @w{mp_bitcnt_t @var{b}})
- @deftypefunx void mpz_cdiv_r_2exp (mpz_t @var{r}, mpz_t @var{n}, @w{mp_bitcnt_t @var{b}})
- @end deftypefun
- @deftypefun void mpz_fdiv_q (mpz_t @var{q}, mpz_t @var{n}, mpz_t @var{d})
- @deftypefunx void mpz_fdiv_r (mpz_t @var{r}, mpz_t @var{n}, mpz_t @var{d})
- @deftypefunx void mpz_fdiv_qr (mpz_t @var{q}, mpz_t @var{r}, mpz_t @var{n}, mpz_t @var{d})
- @maybepagebreak
- @deftypefunx {unsigned long int} mpz_fdiv_q_ui (mpz_t @var{q}, mpz_t @var{n}, @w{unsigned long int @var{d}})
- @deftypefunx {unsigned long int} mpz_fdiv_r_ui (mpz_t @var{r}, mpz_t @var{n}, @w{unsigned long int @var{d}})
- @deftypefunx {unsigned long int} mpz_fdiv_qr_ui (mpz_t @var{q}, mpz_t @var{r}, @w{mpz_t @var{n}}, @w{unsigned long int @var{d}})
- @deftypefunx {unsigned long int} mpz_fdiv_ui (mpz_t @var{n}, @w{unsigned long int @var{d}})
- @maybepagebreak
- @deftypefunx void mpz_fdiv_q_2exp (mpz_t @var{q}, mpz_t @var{n}, @w{mp_bitcnt_t @var{b}})
- @deftypefunx void mpz_fdiv_r_2exp (mpz_t @var{r}, mpz_t @var{n}, @w{mp_bitcnt_t @var{b}})
- @end deftypefun
- @deftypefun void mpz_tdiv_q (mpz_t @var{q}, mpz_t @var{n}, mpz_t @var{d})
- @deftypefunx void mpz_tdiv_r (mpz_t @var{r}, mpz_t @var{n}, mpz_t @var{d})
- @deftypefunx void mpz_tdiv_qr (mpz_t @var{q}, mpz_t @var{r}, mpz_t @var{n}, mpz_t @var{d})
- @maybepagebreak
- @deftypefunx {unsigned long int} mpz_tdiv_q_ui (mpz_t @var{q}, mpz_t @var{n}, @w{unsigned long int @var{d}})
- @deftypefunx {unsigned long int} mpz_tdiv_r_ui (mpz_t @var{r}, mpz_t @var{n}, @w{unsigned long int @var{d}})
- @deftypefunx {unsigned long int} mpz_tdiv_qr_ui (mpz_t @var{q}, mpz_t @var{r}, @w{mpz_t @var{n}}, @w{unsigned long int @var{d}})
- @deftypefunx {unsigned long int} mpz_tdiv_ui (mpz_t @var{n}, @w{unsigned long int @var{d}})
- @maybepagebreak
- @deftypefunx void mpz_tdiv_q_2exp (mpz_t @var{q}, mpz_t @var{n}, @w{mp_bitcnt_t @var{b}})
- @deftypefunx void mpz_tdiv_r_2exp (mpz_t @var{r}, mpz_t @var{n}, @w{mp_bitcnt_t @var{b}})
- @cindex Bit shift right
- @sp 1
- Divide @var{n} by @var{d}, forming a quotient @var{q} and/or remainder
- @var{r}. For the @code{2exp} functions, @m{@var{d}=2^b, @var{d}=2^@var{b}}.
- The rounding is in three styles, each suiting different applications.
- @itemize @bullet
- @item
- @code{cdiv} rounds @var{q} up towards @m{+infty, +infinity}, and @var{r} will
- have the opposite sign to @var{d}. The @code{c} stands for ``ceil''.
- @item
- @code{fdiv} rounds @var{q} down towards @m{-infty, @minus{}infinity}, and
- @var{r} will have the same sign as @var{d}. The @code{f} stands for
- ``floor''.
- @item
- @code{tdiv} rounds @var{q} towards zero, and @var{r} will have the same sign
- as @var{n}. The @code{t} stands for ``truncate''.
- @end itemize
- In all cases @var{q} and @var{r} will satisfy
- @m{@var{n}=@var{q}@var{d}+@var{r}, @var{n}=@var{q}*@var{d}+@var{r}}, and
- @var{r} will satisfy @math{0@le{}@GMPabs{@var{r}}<@GMPabs{@var{d}}}.
- The @code{q} functions calculate only the quotient, the @code{r} functions
- only the remainder, and the @code{qr} functions calculate both. Note that for
- @code{qr} the same variable cannot be passed for both @var{q} and @var{r}, or
- results will be unpredictable.
- For the @code{ui} variants the return value is the remainder, and in fact
- returning the remainder is all the @code{div_ui} functions do. For
- @code{tdiv} and @code{cdiv} the remainder can be negative, so for those the
- return value is the absolute value of the remainder.
- For the @code{2exp} variants the divisor is @m{2^b,2^@var{b}}. These
- functions are implemented as right shifts and bit masks, but of course they
- round the same as the other functions.
- For positive @var{n} both @code{mpz_fdiv_q_2exp} and @code{mpz_tdiv_q_2exp}
- are simple bitwise right shifts. For negative @var{n}, @code{mpz_fdiv_q_2exp}
- is effectively an arithmetic right shift treating @var{n} as twos complement
- the same as the bitwise logical functions do, whereas @code{mpz_tdiv_q_2exp}
- effectively treats @var{n} as sign and magnitude.
- @end deftypefun
- @deftypefun void mpz_mod (mpz_t @var{r}, mpz_t @var{n}, mpz_t @var{d})
- @deftypefunx {unsigned long int} mpz_mod_ui (mpz_t @var{r}, mpz_t @var{n}, @w{unsigned long int @var{d}})
- Set @var{r} to @var{n} @code{mod} @var{d}. The sign of the divisor is
- ignored; the result is always non-negative.
- @code{mpz_mod_ui} is identical to @code{mpz_fdiv_r_ui} above, returning the
- remainder as well as setting @var{r}. See @code{mpz_fdiv_ui} above if only
- the return value is wanted.
- @end deftypefun
- @deftypefun void mpz_divexact (mpz_t @var{q}, mpz_t @var{n}, mpz_t @var{d})
- @deftypefunx void mpz_divexact_ui (mpz_t @var{q}, mpz_t @var{n}, unsigned long @var{d})
- @cindex Exact division functions
- Set @var{q} to @var{n}/@var{d}. These functions produce correct results only
- when it is known in advance that @var{d} divides @var{n}.
- These routines are much faster than the other division functions, and are the
- best choice when exact division is known to occur, for example reducing a
- rational to lowest terms.
- @end deftypefun
- @deftypefun int mpz_divisible_p (mpz_t @var{n}, mpz_t @var{d})
- @deftypefunx int mpz_divisible_ui_p (mpz_t @var{n}, unsigned long int @var{d})
- @deftypefunx int mpz_divisible_2exp_p (mpz_t @var{n}, mp_bitcnt_t @var{b})
- @cindex Divisibility functions
- Return non-zero if @var{n} is exactly divisible by @var{d}, or in the case of
- @code{mpz_divisible_2exp_p} by @m{2^b,2^@var{b}}.
- @var{n} is divisible by @var{d} if there exists an integer @var{q} satisfying
- @math{@var{n} = @var{q}@GMPmultiply{}@var{d}}. Unlike the other division
- functions, @math{@var{d}=0} is accepted and following the rule it can be seen
- that only 0 is considered divisible by 0.
- @end deftypefun
- @deftypefun int mpz_congruent_p (mpz_t @var{n}, mpz_t @var{c}, mpz_t @var{d})
- @deftypefunx int mpz_congruent_ui_p (mpz_t @var{n}, unsigned long int @var{c}, unsigned long int @var{d})
- @deftypefunx int mpz_congruent_2exp_p (mpz_t @var{n}, mpz_t @var{c}, mp_bitcnt_t @var{b})
- @cindex Divisibility functions
- @cindex Congruence functions
- Return non-zero if @var{n} is congruent to @var{c} modulo @var{d}, or in the
- case of @code{mpz_congruent_2exp_p} modulo @m{2^b,2^@var{b}}.
- @var{n} is congruent to @var{c} mod @var{d} if there exists an integer @var{q}
- satisfying @math{@var{n} = @var{c} + @var{q}@GMPmultiply{}@var{d}}. Unlike
- the other division functions, @math{@var{d}=0} is accepted and following the
- rule it can be seen that @var{n} and @var{c} are considered congruent mod 0
- only when exactly equal.
- @end deftypefun
- @need 2000
- @node Integer Exponentiation, Integer Roots, Integer Division, Integer Functions
- @section Exponentiation Functions
- @cindex Integer exponentiation functions
- @cindex Exponentiation functions
- @cindex Powering functions
- @deftypefun void mpz_powm (mpz_t @var{rop}, mpz_t @var{base}, mpz_t @var{exp}, mpz_t @var{mod})
- @deftypefunx void mpz_powm_ui (mpz_t @var{rop}, mpz_t @var{base}, unsigned long int @var{exp}, mpz_t @var{mod})
- Set @var{rop} to @m{base^{exp} bmod mod, (@var{base} raised to @var{exp})
- modulo @var{mod}}.
- Negative @var{exp} is supported if an inverse @math{@var{base}^@W{-1} @bmod
- @var{mod}} exists (see @code{mpz_invert} in @ref{Number Theoretic Functions}).
- If an inverse doesn't exist then a divide by zero is raised.
- @end deftypefun
- @deftypefun void mpz_powm_sec (mpz_t @var{rop}, mpz_t @var{base}, mpz_t @var{exp}, mpz_t @var{mod})
- Set @var{rop} to @m{base^{exp} bmod mod, (@var{base} raised to @var{exp})
- modulo @var{mod}}.
- It is required that @math{@var{exp} > 0} and that @var{mod} is odd.
- This function is designed to take the same time and have the same cache access
- patterns for any two same-size arguments, assuming that function arguments are
- placed at the same position and that the machine state is identical upon
- function entry. This function is intended for cryptographic purposes, where
- resilience to side-channel attacks is desired.
- @end deftypefun
- @deftypefun void mpz_pow_ui (mpz_t @var{rop}, mpz_t @var{base}, unsigned long int @var{exp})
- @deftypefunx void mpz_ui_pow_ui (mpz_t @var{rop}, unsigned long int @var{base}, unsigned long int @var{exp})
- Set @var{rop} to @m{base^{exp}, @var{base} raised to @var{exp}}. The case
- @math{0^0} yields 1.
- @end deftypefun
- @need 2000
- @node Integer Roots, Number Theoretic Functions, Integer Exponentiation, Integer Functions
- @section Root Extraction Functions
- @cindex Integer root functions
- @cindex Root extraction functions
- @deftypefun int mpz_root (mpz_t @var{rop}, mpz_t @var{op}, unsigned long int @var{n})
- Set @var{rop} to @m{lfloorroot n of {op}rfloor@C{},} the truncated integer
- part of the @var{n}th root of @var{op}. Return non-zero if the computation
- was exact, i.e., if @var{op} is @var{rop} to the @var{n}th power.
- @end deftypefun
- @deftypefun void mpz_rootrem (mpz_t @var{root}, mpz_t @var{rem}, mpz_t @var{u}, unsigned long int @var{n})
- Set @var{root} to @m{lfloorroot n of {u}rfloor@C{},} the truncated
- integer part of the @var{n}th root of @var{u}. Set @var{rem} to the
- remainder, @m{(@var{u} - @var{root}^n),
- @var{u}@minus{}@var{root}**@var{n}}.
- @end deftypefun
- @deftypefun void mpz_sqrt (mpz_t @var{rop}, mpz_t @var{op})
- Set @var{rop} to @m{lfloorsqrt{@var{op}}rfloor@C{},} the truncated
- integer part of the square root of @var{op}.
- @end deftypefun
- @deftypefun void mpz_sqrtrem (mpz_t @var{rop1}, mpz_t @var{rop2}, mpz_t @var{op})
- Set @var{rop1} to @m{lfloorsqrt{@var{op}}rfloor, the truncated integer part
- of the square root of @var{op}}, like @code{mpz_sqrt}. Set @var{rop2} to the
- remainder @m{(@var{op} - @var{rop1}^2),
- @var{op}@minus{}@var{rop1}*@var{rop1}}, which will be zero if @var{op} is a
- perfect square.
- If @var{rop1} and @var{rop2} are the same variable, the results are
- undefined.
- @end deftypefun
- @deftypefun int mpz_perfect_power_p (mpz_t @var{op})
- @cindex Perfect power functions
- @cindex Root testing functions
- Return non-zero if @var{op} is a perfect power, i.e., if there exist integers
- @m{a,@var{a}} and @m{b,@var{b}}, with @m{b>1, @var{b}>1}, such that
- @m{@var{op}=a^b, @var{op} equals @var{a} raised to the power @var{b}}.
- Under this definition both 0 and 1 are considered to be perfect powers.
- Negative values of @var{op} are accepted, but of course can only be odd
- perfect powers.
- @end deftypefun
- @deftypefun int mpz_perfect_square_p (mpz_t @var{op})
- @cindex Perfect square functions
- @cindex Root testing functions
- Return non-zero if @var{op} is a perfect square, i.e., if the square root of
- @var{op} is an integer. Under this definition both 0 and 1 are considered to
- be perfect squares.
- @end deftypefun
- @need 2000
- @node Number Theoretic Functions, Integer Comparisons, Integer Roots, Integer Functions
- @section Number Theoretic Functions
- @cindex Number theoretic functions
- @deftypefun int mpz_probab_prime_p (mpz_t @var{n}, int @var{reps})
- @cindex Prime testing functions
- @cindex Probable prime testing functions
- Determine whether @var{n} is prime. Return 2 if @var{n} is definitely prime,
- return 1 if @var{n} is probably prime (without being certain), or return 0 if
- @var{n} is definitely composite.
- This function does some trial divisions, then some Miller-Rabin probabilistic
- primality tests. @var{reps} controls how many such tests are done, 5 to 10 is
- a reasonable number, more will reduce the chances of a composite being
- returned as ``probably prime''.
- Miller-Rabin and similar tests can be more properly called compositeness
- tests. Numbers which fail are known to be composite but those which pass
- might be prime or might be composite. Only a few composites pass, hence those
- which pass are considered probably prime.
- @end deftypefun
- @deftypefun void mpz_nextprime (mpz_t @var{rop}, mpz_t @var{op})
- @cindex Next prime function
- Set @var{rop} to the next prime greater than @var{op}.
- This function uses a probabilistic algorithm to identify primes. For
- practical purposes it's adequate, the chance of a composite passing will be
- extremely small.
- @end deftypefun
- @c mpz_prime_p not implemented as of gmp 3.0.
- @c @deftypefun int mpz_prime_p (mpz_t @var{n})
- @c Return non-zero if @var{n} is prime and zero if @var{n} is a non-prime.
- @c This function is far slower than @code{mpz_probab_prime_p}, but then it
- @c never returns non-zero for composite numbers.
- @c (For practical purposes, using @code{mpz_probab_prime_p} is adequate.
- @c The likelihood of a programming error or hardware malfunction is orders
- @c of magnitudes greater than the likelihood for a composite to pass as a
- @c prime, if the @var{reps} argument is in the suggested range.)
- @c @end deftypefun
- @deftypefun void mpz_gcd (mpz_t @var{rop}, mpz_t @var{op1}, mpz_t @var{op2})
- @cindex Greatest common divisor functions
- @cindex GCD functions
- Set @var{rop} to the greatest common divisor of @var{op1} and @var{op2}.
- The result is always positive even if one or both input operands
- are negative.
- @end deftypefun
- @deftypefun {unsigned long int} mpz_gcd_ui (mpz_t @var{rop}, mpz_t @var{op1}, unsigned long int @var{op2})
- Compute the greatest common divisor of @var{op1} and @var{op2}. If
- @var{rop} is not @code{NULL}, store the result there.
- If the result is small enough to fit in an @code{unsigned long int}, it is
- returned. If the result does not fit, 0 is returned, and the result is equal
- to the argument @var{op1}. Note that the result will always fit if @var{op2}
- is non-zero.
- @end deftypefun
- @deftypefun void mpz_gcdext (mpz_t @var{g}, mpz_t @var{s}, mpz_t @var{t}, mpz_t @var{a}, mpz_t @var{b})
- @cindex Extended GCD
- @cindex GCD extended
- Set @var{g} to the greatest common divisor of @var{a} and @var{b}, and in
- addition set @var{s} and @var{t} to coefficients satisfying
- @math{@var{a}@GMPmultiply{}@var{s} + @var{b}@GMPmultiply{}@var{t} = @var{g}}.
- The value in @var{g} is always positive, even if one or both of @var{a} and
- @var{b} are negative. The values in @var{s} and @var{t} are chosen such that
- @math{@GMPabs{@var{s}} @le{} @GMPabs{@var{b}}} and @math{@GMPabs{@var{t}}
- @le{} @GMPabs{@var{a}}}.
- If @var{t} is @code{NULL} then that value is not computed.
- @end deftypefun
- @deftypefun void mpz_lcm (mpz_t @var{rop}, mpz_t @var{op1}, mpz_t @var{op2})
- @deftypefunx void mpz_lcm_ui (mpz_t @var{rop}, mpz_t @var{op1}, unsigned long @var{op2})
- @cindex Least common multiple functions
- @cindex LCM functions
- Set @var{rop} to the least common multiple of @var{op1} and @var{op2}.
- @var{rop} is always positive, irrespective of the signs of @var{op1} and
- @var{op2}. @var{rop} will be zero if either @var{op1} or @var{op2} is zero.
- @end deftypefun
- @deftypefun int mpz_invert (mpz_t @var{rop}, mpz_t @var{op1}, mpz_t @var{op2})
- @cindex Modular inverse functions
- @cindex Inverse modulo functions
- Compute the inverse of @var{op1} modulo @var{op2} and put the result in
- @var{rop}. If the inverse exists, the return value is non-zero and @var{rop}
- will satisfy @math{0 @le{} @var{rop} < @var{op2}}. If an inverse doesn't exist
- the return value is zero and @var{rop} is undefined.
- @end deftypefun
- @deftypefun int mpz_jacobi (mpz_t @var{a}, mpz_t @var{b})
- @cindex Jacobi symbol functions
- Calculate the Jacobi symbol @m{left(a over bright),
- (@var{a}/@var{b})}. This is defined only for @var{b} odd.
- @end deftypefun
- @deftypefun int mpz_legendre (mpz_t @var{a}, mpz_t @var{p})
- @cindex Legendre symbol functions
- Calculate the Legendre symbol @m{left(a over pright),
- (@var{a}/@var{p})}. This is defined only for @var{p} an odd positive
- prime, and for such @var{p} it's identical to the Jacobi symbol.
- @end deftypefun
- @deftypefun int mpz_kronecker (mpz_t @var{a}, mpz_t @var{b})
- @deftypefunx int mpz_kronecker_si (mpz_t @var{a}, long @var{b})
- @deftypefunx int mpz_kronecker_ui (mpz_t @var{a}, unsigned long @var{b})
- @deftypefunx int mpz_si_kronecker (long @var{a}, mpz_t @var{b})
- @deftypefunx int mpz_ui_kronecker (unsigned long @var{a}, mpz_t @var{b})
- @cindex Kronecker symbol functions
- Calculate the Jacobi symbol @m{left(a over bright),
- (@var{a}/@var{b})} with the Kronecker extension @m{left(a over
- 2right) = left(2 over aright), (a/2)=(2/a)} when @math{a} odd, or
- @m{left(a over 2right) = 0, (a/2)=0} when @math{a} even.
- When @var{b} is odd the Jacobi symbol and Kronecker symbol are
- identical, so @code{mpz_kronecker_ui} etc can be used for mixed
- precision Jacobi symbols too.
- For more information see Henri Cohen section 1.4.2 (@pxref{References}),
- or any number theory textbook. See also the example program
- @file{demos/qcn.c} which uses @code{mpz_kronecker_ui}.
- @end deftypefun
- @deftypefun {mp_bitcnt_t} mpz_remove (mpz_t @var{rop}, mpz_t @var{op}, mpz_t @var{f})
- @cindex Remove factor functions
- @cindex Factor removal functions
- Remove all occurrences of the factor @var{f} from @var{op} and store the
- result in @var{rop}. The return value is how many such occurrences were
- removed.
- @end deftypefun
- @deftypefun void mpz_fac_ui (mpz_t @var{rop}, unsigned long int @var{op})
- @cindex Factorial functions
- Set @var{rop} to @var{op}!, the factorial of @var{op}.
- @end deftypefun
- @deftypefun void mpz_bin_ui (mpz_t @var{rop}, mpz_t @var{n}, unsigned long int @var{k})
- @deftypefunx void mpz_bin_uiui (mpz_t @var{rop}, unsigned long int @var{n}, @w{unsigned long int @var{k}})
- @cindex Binomial coefficient functions
- Compute the binomial coefficient @m{left({n}atop{k}right), @var{n} over
- @var{k}} and store the result in @var{rop}. Negative values of @var{n} are
- supported by @code{mpz_bin_ui}, using the identity
- @m{left({-n}atop{k}right) = (-1)^k left({n+k-1}atop{k}right),
- bin(-n@C{}k) = (-1)^k * bin(n+k-1@C{}k)}, see Knuth volume 1 section 1.2.6
- part G.
- @end deftypefun
- @deftypefun void mpz_fib_ui (mpz_t @var{fn}, unsigned long int @var{n})
- @deftypefunx void mpz_fib2_ui (mpz_t @var{fn}, mpz_t @var{fnsub1}, unsigned long int @var{n})
- @cindex Fibonacci sequence functions
- @code{mpz_fib_ui} sets @var{fn} to to @m{F_n,F[n]}, the @var{n}'th Fibonacci
- number. @code{mpz_fib2_ui} sets @var{fn} to @m{F_n,F[n]}, and @var{fnsub1} to
- @m{F_{n-1},F[n-1]}.
- These functions are designed for calculating isolated Fibonacci numbers. When
- a sequence of values is wanted it's best to start with @code{mpz_fib2_ui} and
- iterate the defining @m{F_{n+1} = F_n + F_{n-1}, F[n+1]=F[n]+F[n-1]} or
- similar.
- @end deftypefun
- @deftypefun void mpz_lucnum_ui (mpz_t @var{ln}, unsigned long int @var{n})
- @deftypefunx void mpz_lucnum2_ui (mpz_t @var{ln}, mpz_t @var{lnsub1}, unsigned long int @var{n})
- @cindex Lucas number functions
- @code{mpz_lucnum_ui} sets @var{ln} to to @m{L_n,L[n]}, the @var{n}'th Lucas
- number. @code{mpz_lucnum2_ui} sets @var{ln} to @m{L_n,L[n]}, and @var{lnsub1}
- to @m{L_{n-1},L[n-1]}.
- These functions are designed for calculating isolated Lucas numbers. When a
- sequence of values is wanted it's best to start with @code{mpz_lucnum2_ui} and
- iterate the defining @m{L_{n+1} = L_n + L_{n-1}, L[n+1]=L[n]+L[n-1]} or
- similar.
- The Fibonacci numbers and Lucas numbers are related sequences, so it's never
- necessary to call both @code{mpz_fib2_ui} and @code{mpz_lucnum2_ui}. The
- formulas for going from Fibonacci to Lucas can be found in @ref{Lucas Numbers
- Algorithm}, the reverse is straightforward too.
- @end deftypefun
- @node Integer Comparisons, Integer Logic and Bit Fiddling, Number Theoretic Functions, Integer Functions
- @comment node-name, next, previous, up
- @section Comparison Functions
- @cindex Integer comparison functions
- @cindex Comparison functions
- @deftypefn Function int mpz_cmp (mpz_t @var{op1}, mpz_t @var{op2})
- @deftypefnx Function int mpz_cmp_d (mpz_t @var{op1}, double @var{op2})
- @deftypefnx Macro int mpz_cmp_si (mpz_t @var{op1}, signed long int @var{op2})
- @deftypefnx Macro int mpz_cmp_ui (mpz_t @var{op1}, unsigned long int @var{op2})
- Compare @var{op1} and @var{op2}. Return a positive value if @math{@var{op1} >
- @var{op2}}, zero if @math{@var{op1} = @var{op2}}, or a negative value if
- @math{@var{op1} < @var{op2}}.
- @code{mpz_cmp_ui} and @code{mpz_cmp_si} are macros and will evaluate their
- arguments more than once. @code{mpz_cmp_d} can be called with an infinity,
- but results are undefined for a NaN.
- @end deftypefn
- @deftypefn Function int mpz_cmpabs (mpz_t @var{op1}, mpz_t @var{op2})
- @deftypefnx Function int mpz_cmpabs_d (mpz_t @var{op1}, double @var{op2})
- @deftypefnx Function int mpz_cmpabs_ui (mpz_t @var{op1}, unsigned long int @var{op2})
- Compare the absolute values of @var{op1} and @var{op2}. Return a positive
- value if @math{@GMPabs{@var{op1}} > @GMPabs{@var{op2}}}, zero if
- @math{@GMPabs{@var{op1}} = @GMPabs{@var{op2}}}, or a negative value if
- @math{@GMPabs{@var{op1}} < @GMPabs{@var{op2}}}.
- @code{mpz_cmpabs_d} can be called with an infinity, but results are undefined
- for a NaN.
- @end deftypefn
- @deftypefn Macro int mpz_sgn (mpz_t @var{op})
- @cindex Sign tests
- @cindex Integer sign tests
- Return @math{+1} if @math{@var{op} > 0}, 0 if @math{@var{op} = 0}, and
- @math{-1} if @math{@var{op} < 0}.
- This function is actually implemented as a macro. It evaluates its argument
- multiple times.
- @end deftypefn
- @node Integer Logic and Bit Fiddling, I/O of Integers, Integer Comparisons, Integer Functions
- @comment node-name, next, previous, up
- @section Logical and Bit Manipulation Functions
- @cindex Logical functions
- @cindex Bit manipulation functions
- @cindex Integer logical functions
- @cindex Integer bit manipulation functions
- These functions behave as if twos complement arithmetic were used (although
- sign-magnitude is the actual implementation). The least significant bit is
- number 0.
- @deftypefun void mpz_and (mpz_t @var{rop}, mpz_t @var{op1}, mpz_t @var{op2})
- Set @var{rop} to @var{op1} bitwise-and @var{op2}.
- @end deftypefun
- @deftypefun void mpz_ior (mpz_t @var{rop}, mpz_t @var{op1}, mpz_t @var{op2})
- Set @var{rop} to @var{op1} bitwise inclusive-or @var{op2}.
- @end deftypefun
- @deftypefun void mpz_xor (mpz_t @var{rop}, mpz_t @var{op1}, mpz_t @var{op2})
- Set @var{rop} to @var{op1} bitwise exclusive-or @var{op2}.
- @end deftypefun
- @deftypefun void mpz_com (mpz_t @var{rop}, mpz_t @var{op})
- Set @var{rop} to the one's complement of @var{op}.
- @end deftypefun
- @deftypefun {mp_bitcnt_t} mpz_popcount (mpz_t @var{op})
- If @math{@var{op}@ge{}0}, return the population count of @var{op}, which is the
- number of 1 bits in the binary representation. If @math{@var{op}<0}, the
- number of 1s is infinite, and the return value is the largest possible
- @code{mp_bitcnt_t}.
- @end deftypefun
- @deftypefun {mp_bitcnt_t} mpz_hamdist (mpz_t @var{op1}, mpz_t @var{op2})
- If @var{op1} and @var{op2} are both @math{@ge{}0} or both @math{<0}, return the
- hamming distance between the two operands, which is the number of bit positions
- where @var{op1} and @var{op2} have different bit values. If one operand is
- @math{@ge{}0} and the other @math{<0} then the number of bits different is
- infinite, and the return value is the largest possible @code{mp_bitcnt_t}.
- @end deftypefun
- @deftypefun {mp_bitcnt_t} mpz_scan0 (mpz_t @var{op}, mp_bitcnt_t @var{starting_bit})
- @deftypefunx {mp_bitcnt_t} mpz_scan1 (mpz_t @var{op}, mp_bitcnt_t @var{starting_bit})
- @cindex Bit scanning functions
- @cindex Scan bit functions
- Scan @var{op}, starting from bit @var{starting_bit}, towards more significant
- bits, until the first 0 or 1 bit (respectively) is found. Return the index of
- the found bit.
- If the bit at @var{starting_bit} is already what's sought, then
- @var{starting_bit} is returned.
- If there's no bit found, then the largest possible @code{mp_bitcnt_t} is
- returned. This will happen in @code{mpz_scan0} past the end of a negative
- number, or @code{mpz_scan1} past the end of a nonnegative number.
- @end deftypefun
- @deftypefun void mpz_setbit (mpz_t @var{rop}, mp_bitcnt_t @var{bit_index})
- Set bit @var{bit_index} in @var{rop}.
- @end deftypefun
- @deftypefun void mpz_clrbit (mpz_t @var{rop}, mp_bitcnt_t @var{bit_index})
- Clear bit @var{bit_index} in @var{rop}.
- @end deftypefun
- @deftypefun void mpz_combit (mpz_t @var{rop}, mp_bitcnt_t @var{bit_index})
- Complement bit @var{bit_index} in @var{rop}.
- @end deftypefun
- @deftypefun int mpz_tstbit (mpz_t @var{op}, mp_bitcnt_t @var{bit_index})
- Test bit @var{bit_index} in @var{op} and return 0 or 1 accordingly.
- @end deftypefun
- @node I/O of Integers, Integer Random Numbers, Integer Logic and Bit Fiddling, Integer Functions
- @comment node-name, next, previous, up
- @section Input and Output Functions
- @cindex Integer input and output functions
- @cindex Input functions
- @cindex Output functions
- @cindex I/O functions
- Functions that perform input from a stdio stream, and functions that output to
- a stdio stream. Passing a @code{NULL} pointer for a @var{stream} argument to any of
- these functions will make them read from @code{stdin} and write to
- @code{stdout}, respectively.
- When using any of these functions, it is a good idea to include @file{stdio.h}
- before @file{gmp.h}, since that will allow @file{gmp.h} to define prototypes
- for these functions.
- @deftypefun size_t mpz_out_str (FILE *@var{stream}, int @var{base}, mpz_t @var{op})
- Output @var{op} on stdio stream @var{stream}, as a string of digits in base
- @var{base}. The base argument may vary from 2 to 62 or from @minus{}2 to
- @minus{}36.
- For @var{base} in the range 2..36, digits and lower-case letters are used; for
- @minus{}2..@minus{}36, digits and upper-case letters are used; for 37..62,
- digits, upper-case letters, and lower-case letters (in that significance order)
- are used.
- Return the number of bytes written, or if an error occurred, return 0.
- @end deftypefun
- @deftypefun size_t mpz_inp_str (mpz_t @var{rop}, FILE *@var{stream}, int @var{base})
- Input a possibly white-space preceded string in base @var{base} from stdio
- stream @var{stream}, and put the read integer in @var{rop}.
- The @var{base} may vary from 2 to 62, or if @var{base} is 0, then the leading
- characters are used: @code{0x} and @code{0X} for hexadecimal, @code{0b} and
- @code{0B} for binary, @code{0} for octal, or decimal otherwise.
- For bases up to 36, case is ignored; upper-case and lower-case letters have
- the same value. For bases 37 to 62, upper-case letter represent the usual
- 10..35 while lower-case letter represent 36..61.
- Return the number of bytes read, or if an error occurred, return 0.
- @end deftypefun
- @deftypefun size_t mpz_out_raw (FILE *@var{stream}, mpz_t @var{op})
- Output @var{op} on stdio stream @var{stream}, in raw binary format. The
- integer is written in a portable format, with 4 bytes of size information, and
- that many bytes of limbs. Both the size and the limbs are written in
- decreasing significance order (i.e., in big-endian).
- The output can be read with @code{mpz_inp_raw}.
- Return the number of bytes written, or if an error occurred, return 0.
- The output of this can not be read by @code{mpz_inp_raw} from GMP 1, because
- of changes necessary for compatibility between 32-bit and 64-bit machines.
- @end deftypefun
- @deftypefun size_t mpz_inp_raw (mpz_t @var{rop}, FILE *@var{stream})
- Input from stdio stream @var{stream} in the format written by
- @code{mpz_out_raw}, and put the result in @var{rop}. Return the number of
- bytes read, or if an error occurred, return 0.
- This routine can read the output from @code{mpz_out_raw} also from GMP 1, in
- spite of changes necessary for compatibility between 32-bit and 64-bit
- machines.
- @end deftypefun
- @need 2000
- @node Integer Random Numbers, Integer Import and Export, I/O of Integers, Integer Functions
- @comment node-name, next, previous, up
- @section Random Number Functions
- @cindex Integer random number functions
- @cindex Random number functions
- The random number functions of GMP come in two groups; older function
- that rely on a global state, and newer functions that accept a state
- parameter that is read and modified. Please see the @ref{Random Number
- Functions} for more information on how to use and not to use random
- number functions.
- @deftypefun void mpz_urandomb (mpz_t @var{rop}, gmp_randstate_t @var{state}, mp_bitcnt_t @var{n})
- Generate a uniformly distributed random integer in the range 0 to @m{2^n-1,
- 2^@var{n}@minus{}1}, inclusive.
- The variable @var{state} must be initialized by calling one of the
- @code{gmp_randinit} functions (@ref{Random State Initialization}) before
- invoking this function.
- @end deftypefun
- @deftypefun void mpz_urandomm (mpz_t @var{rop}, gmp_randstate_t @var{state}, mpz_t @var{n})
- Generate a uniform random integer in the range 0 to @math{@var{n}-1},
- inclusive.
- The variable @var{state} must be initialized by calling one of the
- @code{gmp_randinit} functions (@ref{Random State Initialization})
- before invoking this function.
- @end deftypefun
- @deftypefun void mpz_rrandomb (mpz_t @var{rop}, gmp_randstate_t @var{state}, mp_bitcnt_t @var{n})
- Generate a random integer with long strings of zeros and ones in the
- binary representation. Useful for testing functions and algorithms,
- since this kind of random numbers have proven to be more likely to
- trigger corner-case bugs. The random number will be in the range
- 0 to @m{2^n-1, 2^@var{n}@minus{}1}, inclusive.
- The variable @var{state} must be initialized by calling one of the
- @code{gmp_randinit} functions (@ref{Random State Initialization})
- before invoking this function.
- @end deftypefun
- @deftypefun void mpz_random (mpz_t @var{rop}, mp_size_t @var{max_size})
- Generate a random integer of at most @var{max_size} limbs. The generated
- random number doesn't satisfy any particular requirements of randomness.
- Negative random numbers are generated when @var{max_size} is negative.
- This function is obsolete. Use @code{mpz_urandomb} or
- @code{mpz_urandomm} instead.
- @end deftypefun
- @deftypefun void mpz_random2 (mpz_t @var{rop}, mp_size_t @var{max_size})
- Generate a random integer of at most @var{max_size} limbs, with long strings
- of zeros and ones in the binary representation. Useful for testing functions
- and algorithms, since this kind of random numbers have proven to be more
- likely to trigger corner-case bugs. Negative random numbers are generated
- when @var{max_size} is negative.
- This function is obsolete. Use @code{mpz_rrandomb} instead.
- @end deftypefun
- @node Integer Import and Export, Miscellaneous Integer Functions, Integer Random Numbers, Integer Functions
- @section Integer Import and Export
- @code{mpz_t} variables can be converted to and from arbitrary words of binary
- data with the following functions.
- @deftypefun void mpz_import (mpz_t @var{rop}, size_t @var{count}, int @var{order}, size_t @var{size}, int @var{endian}, size_t @var{nails}, const void *@var{op})
- @cindex Integer import
- @cindex Import
- Set @var{rop} from an array of word data at @var{op}.
- The parameters specify the format of the data. @var{count} many words are
- read, each @var{size} bytes. @var{order} can be 1 for most significant word
- first or -1 for least significant first. Within each word @var{endian} can be
- 1 for most significant byte first, -1 for least significant first, or 0 for
- the native endianness of the host CPU@. The most significant @var{nails} bits
- of each word are skipped, this can be 0 to use the full words.
- There is no sign taken from the data, @var{rop} will simply be a positive
- integer. An application can handle any sign itself, and apply it for instance
- with @code{mpz_neg}.
- There are no data alignment restrictions on @var{op}, any address is allowed.
- Here's an example converting an array of @code{unsigned long} data, most
- significant element first, and host byte order within each value.
- @example
- unsigned long a[20];
- /* Initialize @var{z} and @var{a} */
- mpz_import (z, 20, 1, sizeof(a[0]), 0, 0, a);
- @end example
- This example assumes the full @code{sizeof} bytes are used for data in the
- given type, which is usually true, and certainly true for @code{unsigned long}
- everywhere we know of. However on Cray vector systems it may be noted that
- @code{short} and @code{int} are always stored in 8 bytes (and with
- @code{sizeof} indicating that) but use only 32 or 46 bits. The @var{nails}
- feature can account for this, by passing for instance
- @code{8*sizeof(int)-INT_BIT}.
- @end deftypefun
- @deftypefun {void *} mpz_export (void *@var{rop}, size_t *@var{countp}, int @var{order}, size_t @var{size}, int @var{endian}, size_t @var{nails}, mpz_t @var{op})
- @cindex Integer export
- @cindex Export
- Fill @var{rop} with word data from @var{op}.
- The parameters specify the format of the data produced. Each word will be
- @var{size} bytes and @var{order} can be 1 for most significant word first or
- -1 for least significant first. Within each word @var{endian} can be 1 for
- most significant byte first, -1 for least significant first, or 0 for the
- native endianness of the host CPU@. The most significant @var{nails} bits of
- each word are unused and set to zero, this can be 0 to produce full words.
- The number of words produced is written to @code{*@var{countp}}, or
- @var{countp} can be @code{NULL} to discard the count. @var{rop} must have
- enough space for the data, or if @var{rop} is @code{NULL} then a result array
- of the necessary size is allocated using the current GMP allocation function
- (@pxref{Custom Allocation}). In either case the return value is the
- destination used, either @var{rop} or the allocated block.
- If @var{op} is non-zero then the most significant word produced will be
- non-zero. If @var{op} is zero then the count returned will be zero and
- nothing written to @var{rop}. If @var{rop} is @code{NULL} in this case, no
- block is allocated, just @code{NULL} is returned.
- The sign of @var{op} is ignored, just the absolute value is exported. An
- application can use @code{mpz_sgn} to get the sign and handle it as desired.
- (@pxref{Integer Comparisons})
- There are no data alignment restrictions on @var{rop}, any address is allowed.
- When an application is allocating space itself the required size can be
- determined with a calculation like the following. Since @code{mpz_sizeinbase}
- always returns at least 1, @code{count} here will be at least one, which
- avoids any portability problems with @code{malloc(0)}, though if @code{z} is
- zero no space at all is actually needed (or written).
- @example
- numb = 8*size - nail;
- count = (mpz_sizeinbase (z, 2) + numb-1) / numb;
- p = malloc (count * size);
- @end example
- @end deftypefun
- @need 2000
- @node Miscellaneous Integer Functions, Integer Special Functions, Integer Import and Export, Integer Functions
- @comment node-name, next, previous, up
- @section Miscellaneous Functions
- @cindex Miscellaneous integer functions
- @cindex Integer miscellaneous functions
- @deftypefun int mpz_fits_ulong_p (mpz_t @var{op})
- @deftypefunx int mpz_fits_slong_p (mpz_t @var{op})
- @deftypefunx int mpz_fits_uint_p (mpz_t @var{op})
- @deftypefunx int mpz_fits_sint_p (mpz_t @var{op})
- @deftypefunx int mpz_fits_ushort_p (mpz_t @var{op})
- @deftypefunx int mpz_fits_sshort_p (mpz_t @var{op})
- Return non-zero iff the value of @var{op} fits in an @code{unsigned long int},
- @code{signed long int}, @code{unsigned int}, @code{signed int}, @code{unsigned
- short int}, or @code{signed short int}, respectively. Otherwise, return zero.
- @end deftypefun
- @deftypefn Macro int mpz_odd_p (mpz_t @var{op})
- @deftypefnx Macro int mpz_even_p (mpz_t @var{op})
- Determine whether @var{op} is odd or even, respectively. Return non-zero if
- yes, zero if no. These macros evaluate their argument more than once.
- @end deftypefn
- @deftypefun size_t mpz_sizeinbase (mpz_t @var{op}, int @var{base})
- @cindex Size in digits
- @cindex Digits in an integer
- Return the size of @var{op} measured in number of digits in the given
- @var{base}. @var{base} can vary from 2 to 62. The sign of @var{op} is
- ignored, just the absolute value is used. The result will be either exact or
- 1 too big. If @var{base} is a power of 2, the result is always exact. If
- @var{op} is zero the return value is always 1.
- This function can be used to determine the space required when converting
- @var{op} to a string. The right amount of allocation is normally two more
- than the value returned by @code{mpz_sizeinbase}, one extra for a minus sign
- and one for the null-terminator.
- @cindex Most significant bit
- It will be noted that @code{mpz_sizeinbase(@var{op},2)} can be used to locate
- the most significant 1 bit in @var{op}, counting from 1. (Unlike the bitwise
- functions which start from 0, @xref{Integer Logic and Bit Fiddling,, Logical
- and Bit Manipulation Functions}.)
- @end deftypefun
- @node Integer Special Functions, , Miscellaneous Integer Functions, Integer Functions
- @section Special Functions
- @cindex Special integer functions
- @cindex Integer special functions
- The functions in this section are for various special purposes. Most
- applications will not need them.
- @deftypefun void mpz_array_init (mpz_t @var{integer_array}, mp_size_t @var{array_size}, @w{mp_size_t @var{fixed_num_bits}})
- This is a special type of initialization. @strong{Fixed} space of
- @var{fixed_num_bits} is allocated to each of the @var{array_size} integers in
- @var{integer_array}. There is no way to free the storage allocated by this
- function. Don't call @code{mpz_clear}!
- The @var{integer_array} parameter is the first @code{mpz_t} in the array. For
- example,
- @example
- mpz_t arr[20000];
- mpz_array_init (arr[0], 20000, 512);
- @end example
- @c In case anyone's wondering, yes this parameter style is a bit anomalous,
- @c it'd probably be nicer if it was "arr" instead of "arr[0]". Obviously the
- @c two differ only in the declaration, not the pointer value, but changing is
- @c not possible since it'd provoke warnings or errors in existing sources.
- This function is only intended for programs that create a large number
- of integers and need to reduce memory usage by avoiding the overheads of
- allocating and reallocating lots of small blocks. In normal programs this
- function is not recommended.
- The space allocated to each integer by this function will not be automatically
- increased, unlike the normal @code{mpz_init}, so an application must ensure it
- is sufficient for any value stored. The following space requirements apply to
- various routines,
- @itemize @bullet
- @item
- @code{mpz_abs}, @code{mpz_neg}, @code{mpz_set}, @code{mpz_set_si} and
- @code{mpz_set_ui} need room for the value they store.
- @item
- @code{mpz_add}, @code{mpz_add_ui}, @code{mpz_sub} and @code{mpz_sub_ui} need
- room for the larger of the two operands, plus an extra
- @code{mp_bits_per_limb}.
- @item
- @code{mpz_mul}, @code{mpz_mul_ui} and @code{mpz_mul_ui} need room for the sum
- of the number of bits in their operands, but each rounded up to a multiple of
- @code{mp_bits_per_limb}.
- @item
- @code{mpz_swap} can be used between two array variables, but not between an
- array and a normal variable.
- @end itemize
- For other functions, or if in doubt, the suggestion is to calculate in a
- regular @code{mpz_init} variable and copy the result to an array variable with
- @code{mpz_set}.
- @end deftypefun
- @deftypefun {void *} _mpz_realloc (mpz_t @var{integer}, mp_size_t @var{new_alloc})
- Change the space for @var{integer} to @var{new_alloc} limbs. The value in
- @var{integer} is preserved if it fits, or is set to 0 if not. The return
- value is not useful to applications and should be ignored.
- @code{mpz_realloc2} is the preferred way to accomplish allocation changes like
- this. @code{mpz_realloc2} and @code{_mpz_realloc} are the same except that
- @code{_mpz_realloc} takes its size in limbs.
- @end deftypefun
- @deftypefun mp_limb_t mpz_getlimbn (mpz_t @var{op}, mp_size_t @var{n})
- Return limb number @var{n} from @var{op}. The sign of @var{op} is ignored,
- just the absolute value is used. The least significant limb is number 0.
- @code{mpz_size} can be used to find how many limbs make up @var{op}.
- @code{mpz_getlimbn} returns zero if @var{n} is outside the range 0 to
- @code{mpz_size(@var{op})-1}.
- @end deftypefun
- @deftypefun size_t mpz_size (mpz_t @var{op})
- Return the size of @var{op} measured in number of limbs. If @var{op} is zero,
- the returned value will be zero.
- @c (@xref{Nomenclature}, for an explanation of the concept @dfn{limb}.)
- @end deftypefun
- @node Rational Number Functions, Floating-point Functions, Integer Functions, Top
- @comment node-name, next, previous, up
- @chapter Rational Number Functions
- @cindex Rational number functions
- This chapter describes the GMP functions for performing arithmetic on rational
- numbers. These functions start with the prefix @code{mpq_}.
- Rational numbers are stored in objects of type @code{mpq_t}.
- All rational arithmetic functions assume operands have a canonical form, and
- canonicalize their result. The canonical from means that the denominator and
- the numerator have no common factors, and that the denominator is positive.
- Zero has the unique representation 0/1.
- Pure assignment functions do not canonicalize the assigned variable. It is
- the responsibility of the user to canonicalize the assigned variable before
- any arithmetic operations are performed on that variable.
- @deftypefun void mpq_canonicalize (mpq_t @var{op})
- Remove any factors that are common to the numerator and denominator of
- @var{op}, and make the denominator positive.
- @end deftypefun
- @menu
- * Initializing Rationals::
- * Rational Conversions::
- * Rational Arithmetic::
- * Comparing Rationals::
- * Applying Integer Functions::
- * I/O of Rationals::
- @end menu
- @node Initializing Rationals, Rational Conversions, Rational Number Functions, Rational Number Functions
- @comment node-name, next, previous, up
- @section Initialization and Assignment Functions
- @cindex Rational assignment functions
- @cindex Assignment functions
- @cindex Rational initialization functions
- @cindex Initialization functions
- @deftypefun void mpq_init (mpq_t @var{x})
- Initialize @var{x} and set it to 0/1. Each variable should normally only be
- initialized once, or at least cleared out (using the function @code{mpq_clear})
- between each initialization.
- @end deftypefun
- @deftypefun void mpq_inits (mpq_t @var{x}, ...)
- Initialize a NULL-terminated list of @code{mpq_t} variables, and set their
- values to 0/1.
- @end deftypefun
- @deftypefun void mpq_clear (mpq_t @var{x})
- Free the space occupied by @var{x}. Make sure to call this function for all
- @code{mpq_t} variables when you are done with them.
- @end deftypefun
- @deftypefun void mpq_clears (mpq_t @var{x}, ...)
- Free the space occupied by a NULL-terminated list of @code{mpq_t} variables.
- @end deftypefun
- @deftypefun void mpq_set (mpq_t @var{rop}, mpq_t @var{op})
- @deftypefunx void mpq_set_z (mpq_t @var{rop}, mpz_t @var{op})
- Assign @var{rop} from @var{op}.
- @end deftypefun
- @deftypefun void mpq_set_ui (mpq_t @var{rop}, unsigned long int @var{op1}, unsigned long int @var{op2})
- @deftypefunx void mpq_set_si (mpq_t @var{rop}, signed long int @var{op1}, unsigned long int @var{op2})
- Set the value of @var{rop} to @var{op1}/@var{op2}. Note that if @var{op1} and
- @var{op2} have common factors, @var{rop} has to be passed to
- @code{mpq_canonicalize} before any operations are performed on @var{rop}.
- @end deftypefun
- @deftypefun int mpq_set_str (mpq_t @var{rop}, char *@var{str}, int @var{base})
- Set @var{rop} from a null-terminated string @var{str} in the given @var{base}.
- The string can be an integer like ``41'' or a fraction like ``41/152''. The
- fraction must be in canonical form (@pxref{Rational Number Functions}), or if
- not then @code{mpq_canonicalize} must be called.
- The numerator and optional denominator are parsed the same as in
- @code{mpz_set_str} (@pxref{Assigning Integers}). White space is allowed in
- the string, and is simply ignored. The @var{base} can vary from 2 to 62, or
- if @var{base} is 0 then the leading characters are used: @code{0x} or @code{0X} for hex,
- @code{0b} or @code{0B} for binary,
- @code{0} for octal, or decimal otherwise. Note that this is done separately
- for the numerator and denominator, so for instance @code{0xEF/100} is 239/100,
- whereas @code{0xEF/0x100} is 239/256.
- The return value is 0 if the entire string is a valid number, or @minus{}1 if
- not.
- @end deftypefun
- @deftypefun void mpq_swap (mpq_t @var{rop1}, mpq_t @var{rop2})
- Swap the values @var{rop1} and @var{rop2} efficiently.
- @end deftypefun
- @need 2000
- @node Rational Conversions, Rational Arithmetic, Initializing Rationals, Rational Number Functions
- @comment node-name, next, previous, up
- @section Conversion Functions
- @cindex Rational conversion functions
- @cindex Conversion functions
- @deftypefun double mpq_get_d (mpq_t @var{op})
- Convert @var{op} to a @code{double}, truncating if necessary (ie.@: rounding
- towards zero).
- If the exponent from the conversion is too big or too small to fit a
- @code{double} then the result is system dependent. For too big an infinity is
- returned when available. For too small @math{0.0} is normally returned.
- Hardware overflow, underflow and denorm traps may or may not occur.
- @end deftypefun
- @deftypefun void mpq_set_d (mpq_t @var{rop}, double @var{op})
- @deftypefunx void mpq_set_f (mpq_t @var{rop}, mpf_t @var{op})
- Set @var{rop} to the value of @var{op}. There is no rounding, this conversion
- is exact.
- @end deftypefun
- @deftypefun {char *} mpq_get_str (char *@var{str}, int @var{base}, mpq_t @var{op})
- Convert @var{op} to a string of digits in base @var{base}. The base may vary
- from 2 to 36. The string will be of the form @samp{num/den}, or if the
- denominator is 1 then just @samp{num}.
- If @var{str} is @code{NULL}, the result string is allocated using the current
- allocation function (@pxref{Custom Allocation}). The block will be
- @code{strlen(str)+1} bytes, that being exactly enough for the string and
- null-terminator.
- If @var{str} is not @code{NULL}, it should point to a block of storage large
- enough for the result, that being
- @example
- mpz_sizeinbase (mpq_numref(@var{op}), @var{base})
- + mpz_sizeinbase (mpq_denref(@var{op}), @var{base}) + 3
- @end example
- The three extra bytes are for a possible minus sign, possible slash, and the
- null-terminator.
- A pointer to the result string is returned, being either the allocated block,
- or the given @var{str}.
- @end deftypefun
- @node Rational Arithmetic, Comparing Rationals, Rational Conversions, Rational Number Functions
- @comment node-name, next, previous, up
- @section Arithmetic Functions
- @cindex Rational arithmetic functions
- @cindex Arithmetic functions
- @deftypefun void mpq_add (mpq_t @var{sum}, mpq_t @var{addend1}, mpq_t @var{addend2})
- Set @var{sum} to @var{addend1} + @var{addend2}.
- @end deftypefun
- @deftypefun void mpq_sub (mpq_t @var{difference}, mpq_t @var{minuend}, mpq_t @var{subtrahend})
- Set @var{difference} to @var{minuend} @minus{} @var{subtrahend}.
- @end deftypefun
- @deftypefun void mpq_mul (mpq_t @var{product}, mpq_t @var{multiplier}, mpq_t @var{multiplicand})
- Set @var{product} to @math{@var{multiplier} @GMPtimes{} @var{multiplicand}}.
- @end deftypefun
- @deftypefun void mpq_mul_2exp (mpq_t @var{rop}, mpq_t @var{op1}, mp_bitcnt_t @var{op2})
- Set @var{rop} to @m{@var{op1} times 2^{op2}, @var{op1} times 2 raised to
- @var{op2}}.
- @end deftypefun
- @deftypefun void mpq_div (mpq_t @var{quotient}, mpq_t @var{dividend}, mpq_t @var{divisor})
- @cindex Division functions
- Set @var{quotient} to @var{dividend}/@var{divisor}.
- @end deftypefun
- @deftypefun void mpq_div_2exp (mpq_t @var{rop}, mpq_t @var{op1}, mp_bitcnt_t @var{op2})
- Set @var{rop} to @m{@var{op1}/2^{op2}, @var{op1} divided by 2 raised to
- @var{op2}}.
- @end deftypefun
- @deftypefun void mpq_neg (mpq_t @var{negated_operand}, mpq_t @var{operand})
- Set @var{negated_operand} to @minus{}@var{operand}.
- @end deftypefun
- @deftypefun void mpq_abs (mpq_t @var{rop}, mpq_t @var{op})
- Set @var{rop} to the absolute value of @var{op}.
- @end deftypefun
- @deftypefun void mpq_inv (mpq_t @var{inverted_number}, mpq_t @var{number})
- Set @var{inverted_number} to 1/@var{number}. If the new denominator is
- zero, this routine will divide by zero.
- @end deftypefun
- @node Comparing Rationals, Applying Integer Functions, Rational Arithmetic, Rational Number Functions
- @comment node-name, next, previous, up
- @section Comparison Functions
- @cindex Rational comparison functions
- @cindex Comparison functions
- @deftypefun int mpq_cmp (mpq_t @var{op1}, mpq_t @var{op2})
- Compare @var{op1} and @var{op2}. Return a positive value if @math{@var{op1} >
- @var{op2}}, zero if @math{@var{op1} = @var{op2}}, and a negative value if
- @math{@var{op1} < @var{op2}}.
- To determine if two rationals are equal, @code{mpq_equal} is faster than
- @code{mpq_cmp}.
- @end deftypefun
- @deftypefn Macro int mpq_cmp_ui (mpq_t @var{op1}, unsigned long int @var{num2}, unsigned long int @var{den2})
- @deftypefnx Macro int mpq_cmp_si (mpq_t @var{op1}, long int @var{num2}, unsigned long int @var{den2})
- Compare @var{op1} and @var{num2}/@var{den2}. Return a positive value if
- @math{@var{op1} > @var{num2}/@var{den2}}, zero if @math{@var{op1} =
- @var{num2}/@var{den2}}, and a negative value if @math{@var{op1} <
- @var{num2}/@var{den2}}.
- @var{num2} and @var{den2} are allowed to have common factors.
- These functions are implemented as a macros and evaluate their arguments
- multiple times.
- @end deftypefn
- @deftypefn Macro int mpq_sgn (mpq_t @var{op})
- @cindex Sign tests
- @cindex Rational sign tests
- Return @math{+1} if @math{@var{op} > 0}, 0 if @math{@var{op} = 0}, and
- @math{-1} if @math{@var{op} < 0}.
- This function is actually implemented as a macro. It evaluates its
- arguments multiple times.
- @end deftypefn
- @deftypefun int mpq_equal (mpq_t @var{op1}, mpq_t @var{op2})
- Return non-zero if @var{op1} and @var{op2} are equal, zero if they are
- non-equal. Although @code{mpq_cmp} can be used for the same purpose, this
- function is much faster.
- @end deftypefun
- @node Applying Integer Functions, I/O of Rationals, Comparing Rationals, Rational Number Functions
- @comment node-name, next, previous, up
- @section Applying Integer Functions to Rationals
- @cindex Rational numerator and denominator
- @cindex Numerator and denominator
- The set of @code{mpq} functions is quite small. In particular, there are few
- functions for either input or output. The following functions give direct
- access to the numerator and denominator of an @code{mpq_t}.
- Note that if an assignment to the numerator and/or denominator could take an
- @code{mpq_t} out of the canonical form described at the start of this chapter
- (@pxref{Rational Number Functions}) then @code{mpq_canonicalize} must be
- called before any other @code{mpq} functions are applied to that @code{mpq_t}.
- @deftypefn Macro mpz_t mpq_numref (mpq_t @var{op})
- @deftypefnx Macro mpz_t mpq_denref (mpq_t @var{op})
- Return a reference to the numerator and denominator of @var{op}, respectively.
- The @code{mpz} functions can be used on the result of these macros.
- @end deftypefn
- @deftypefun void mpq_get_num (mpz_t @var{numerator}, mpq_t @var{rational})
- @deftypefunx void mpq_get_den (mpz_t @var{denominator}, mpq_t @var{rational})
- @deftypefunx void mpq_set_num (mpq_t @var{rational}, mpz_t @var{numerator})
- @deftypefunx void mpq_set_den (mpq_t @var{rational}, mpz_t @var{denominator})
- Get or set the numerator or denominator of a rational. These functions are
- equivalent to calling @code{mpz_set} with an appropriate @code{mpq_numref} or
- @code{mpq_denref}. Direct use of @code{mpq_numref} or @code{mpq_denref} is
- recommended instead of these functions.
- @end deftypefun
- @need 2000
- @node I/O of Rationals, , Applying Integer Functions, Rational Number Functions
- @comment node-name, next, previous, up
- @section Input and Output Functions
- @cindex Rational input and output functions
- @cindex Input functions
- @cindex Output functions
- @cindex I/O functions
- When using any of these functions, it's a good idea to include @file{stdio.h}
- before @file{gmp.h}, since that will allow @file{gmp.h} to define prototypes
- for these functions.
- Passing a @code{NULL} pointer for a @var{stream} argument to any of these
- functions will make them read from @code{stdin} and write to @code{stdout},
- respectively.
- @deftypefun size_t mpq_out_str (FILE *@var{stream}, int @var{base}, mpq_t @var{op})
- Output @var{op} on stdio stream @var{stream}, as a string of digits in base
- @var{base}. The base may vary from 2 to 36. Output is in the form
- @samp{num/den} or if the denominator is 1 then just @samp{num}.
- Return the number of bytes written, or if an error occurred, return 0.
- @end deftypefun
- @deftypefun size_t mpq_inp_str (mpq_t @var{rop}, FILE *@var{stream}, int @var{base})
- Read a string of digits from @var{stream} and convert them to a rational in
- @var{rop}. Any initial white-space characters are read and discarded. Return
- the number of characters read (including white space), or 0 if a rational
- could not be read.
- The input can be a fraction like @samp{17/63} or just an integer like
- @samp{123}. Reading stops at the first character not in this form, and white
- space is not permitted within the string. If the input might not be in
- canonical form, then @code{mpq_canonicalize} must be called (@pxref{Rational
- Number Functions}).
- The @var{base} can be between 2 and 36, or can be 0 in which case the leading
- characters of the string determine the base, @samp{0x} or @samp{0X} for
- hexadecimal, @samp{0} for octal, or decimal otherwise. The leading characters
- are examined separately for the numerator and denominator of a fraction, so
- for instance @samp{0x10/11} is @math{16/11}, whereas @samp{0x10/0x11} is
- @math{16/17}.
- @end deftypefun
- @node Floating-point Functions, Low-level Functions, Rational Number Functions, Top
- @comment node-name, next, previous, up
- @chapter Floating-point Functions
- @cindex Floating-point functions
- @cindex Float functions
- @cindex User-defined precision
- @cindex Precision of floats
- GMP floating point numbers are stored in objects of type @code{mpf_t} and
- functions operating on them have an @code{mpf_} prefix.
- The mantissa of each float has a user-selectable precision, limited only by
- available memory. Each variable has its own precision, and that can be
- increased or decreased at any time.
- The exponent of each float is a fixed precision, one machine word on most
- systems. In the current implementation the exponent is a count of limbs, so
- for example on a 32-bit system this means a range of roughly
- @math{2^@W{-68719476768}} to @math{2^@W{68719476736}}, or on a 64-bit system
- this will be greater. Note however @code{mpf_get_str} can only return an
- exponent which fits an @code{mp_exp_t} and currently @code{mpf_set_str}
- doesn't accept exponents bigger than a @code{long}.
- Each variable keeps a size for the mantissa data actually in use. This means
- that if a float is exactly represented in only a few bits then only those bits
- will be used in a calculation, even if the selected precision is high.
- All calculations are performed to the precision of the destination variable.
- Each function is defined to calculate with ``infinite precision'' followed by
- a truncation to the destination precision, but of course the work done is only
- what's needed to determine a result under that definition.
- The precision selected for a variable is a minimum value, GMP may increase it
- a little to facilitate efficient calculation. Currently this means rounding
- up to a whole limb, and then sometimes having a further partial limb,
- depending on the high limb of the mantissa. But applications shouldn't be
- concerned by such details.
- The mantissa in stored in binary, as might be imagined from the fact
- precisions are expressed in bits. One consequence of this is that decimal
- fractions like @math{0.1} cannot be represented exactly. The same is true of
- plain IEEE @code{double} floats. This makes both highly unsuitable for
- calculations involving money or other values that should be exact decimal
- fractions. (Suitably scaled integers, or perhaps rationals, are better
- choices.)
- @code{mpf} functions and variables have no special notion of infinity or
- not-a-number, and applications must take care not to overflow the exponent or
- results will be unpredictable. This might change in a future release.
- Note that the @code{mpf} functions are @emph{not} intended as a smooth
- extension to IEEE P754 arithmetic. In particular results obtained on one
- computer often differ from the results on a computer with a different word
- size.
- @menu
- * Initializing Floats::
- * Assigning Floats::
- * Simultaneous Float Init & Assign::
- * Converting Floats::
- * Float Arithmetic::
- * Float Comparison::
- * I/O of Floats::
- * Miscellaneous Float Functions::
- @end menu
- @node Initializing Floats, Assigning Floats, Floating-point Functions, Floating-point Functions
- @comment node-name, next, previous, up
- @section Initialization Functions
- @cindex Float initialization functions
- @cindex Initialization functions
- @deftypefun void mpf_set_default_prec (mp_bitcnt_t @var{prec})
- Set the default precision to be @strong{at least} @var{prec} bits. All
- subsequent calls to @code{mpf_init} will use this precision, but previously
- initialized variables are unaffected.
- @end deftypefun
- @deftypefun {mp_bitcnt_t} mpf_get_default_prec (void)
- Return the default precision actually used.
- @end deftypefun
- An @code{mpf_t} object must be initialized before storing the first value in
- it. The functions @code{mpf_init} and @code{mpf_init2} are used for that
- purpose.
- @deftypefun void mpf_init (mpf_t @var{x})
- Initialize @var{x} to 0. Normally, a variable should be initialized once only
- or at least be cleared, using @code{mpf_clear}, between initializations. The
- precision of @var{x} is undefined unless a default precision has already been
- established by a call to @code{mpf_set_default_prec}.
- @end deftypefun
- @deftypefun void mpf_init2 (mpf_t @var{x}, mp_bitcnt_t @var{prec})
- Initialize @var{x} to 0 and set its precision to be @strong{at least}
- @var{prec} bits. Normally, a variable should be initialized once only or at
- least be cleared, using @code{mpf_clear}, between initializations.
- @end deftypefun
- @deftypefun void mpf_inits (mpf_t @var{x}, ...)
- Initialize a NULL-terminated list of @code{mpf_t} variables, and set their
- values to 0. The precision of the initialized variables is undefined unless a
- default precision has already been established by a call to
- @code{mpf_set_default_prec}.
- @end deftypefun
- @deftypefun void mpf_clear (mpf_t @var{x})
- Free the space occupied by @var{x}. Make sure to call this function for all
- @code{mpf_t} variables when you are done with them.
- @end deftypefun
- @deftypefun void mpf_clears (mpf_t @var{x}, ...)
- Free the space occupied by a NULL-terminated list of @code{mpf_t} variables.
- @end deftypefun
- @need 2000
- Here is an example on how to initialize floating-point variables:
- @example
- @{
- mpf_t x, y;
- mpf_init (x); /* use default precision */
- mpf_init2 (y, 256); /* precision @emph{at least} 256 bits */
- @dots{}
- /* Unless the program is about to exit, do ... */
- mpf_clear (x);
- mpf_clear (y);
- @}
- @end example
- The following three functions are useful for changing the precision during a
- calculation. A typical use would be for adjusting the precision gradually in
- iterative algorithms like Newton-Raphson, making the computation precision
- closely match the actual accurate part of the numbers.
- @deftypefun {mp_bitcnt_t} mpf_get_prec (mpf_t @var{op})
- Return the current precision of @var{op}, in bits.
- @end deftypefun
- @deftypefun void mpf_set_prec (mpf_t @var{rop}, mp_bitcnt_t @var{prec})
- Set the precision of @var{rop} to be @strong{at least} @var{prec} bits. The
- value in @var{rop} will be truncated to the new precision.
- This function requires a call to @code{realloc}, and so should not be used in
- a tight loop.
- @end deftypefun
- @deftypefun void mpf_set_prec_raw (mpf_t @var{rop}, mp_bitcnt_t @var{prec})
- Set the precision of @var{rop} to be @strong{at least} @var{prec} bits,
- without changing the memory allocated.
- @var{prec} must be no more than the allocated precision for @var{rop}, that
- being the precision when @var{rop} was initialized, or in the most recent
- @code{mpf_set_prec}.
- The value in @var{rop} is unchanged, and in particular if it had a higher
- precision than @var{prec} it will retain that higher precision. New values
- written to @var{rop} will use the new @var{prec}.
- Before calling @code{mpf_clear} or the full @code{mpf_set_prec}, another
- @code{mpf_set_prec_raw} call must be made to restore @var{rop} to its original
- allocated precision. Failing to do so will have unpredictable results.
- @code{mpf_get_prec} can be used before @code{mpf_set_prec_raw} to get the
- original allocated precision. After @code{mpf_set_prec_raw} it reflects the
- @var{prec} value set.
- @code{mpf_set_prec_raw} is an efficient way to use an @code{mpf_t} variable at
- different precisions during a calculation, perhaps to gradually increase
- precision in an iteration, or just to use various different precisions for
- different purposes during a calculation.
- @end deftypefun
- @need 2000
- @node Assigning Floats, Simultaneous Float Init & Assign, Initializing Floats, Floating-point Functions
- @comment node-name, next, previous, up
- @section Assignment Functions
- @cindex Float assignment functions
- @cindex Assignment functions
- These functions assign new values to already initialized floats
- (@pxref{Initializing Floats}).
- @deftypefun void mpf_set (mpf_t @var{rop}, mpf_t @var{op})
- @deftypefunx void mpf_set_ui (mpf_t @var{rop}, unsigned long int @var{op})
- @deftypefunx void mpf_set_si (mpf_t @var{rop}, signed long int @var{op})
- @deftypefunx void mpf_set_d (mpf_t @var{rop}, double @var{op})
- @deftypefunx void mpf_set_z (mpf_t @var{rop}, mpz_t @var{op})
- @deftypefunx void mpf_set_q (mpf_t @var{rop}, mpq_t @var{op})
- Set the value of @var{rop} from @var{op}.
- @end deftypefun
- @deftypefun int mpf_set_str (mpf_t @var{rop}, char *@var{str}, int @var{base})
- Set the value of @var{rop} from the string in @var{str}. The string is of the
- form @samp{M@@N} or, if the base is 10 or less, alternatively @samp{MeN}.
- @samp{M} is the mantissa and @samp{N} is the exponent. The mantissa is always
- in the specified base. The exponent is either in the specified base or, if
- @var{base} is negative, in decimal. The decimal point expected is taken from
- the current locale, on systems providing @code{localeconv}.
- The argument @var{base} may be in the ranges 2 to 62, or @minus{}62 to
- @minus{}2. Negative values are used to specify that the exponent is in
- decimal.
- For bases up to 36, case is ignored; upper-case and lower-case letters have
- the same value; for bases 37 to 62, upper-case letter represent the usual
- 10..35 while lower-case letter represent 36..61.
- Unlike the corresponding @code{mpz} function, the base will not be determined
- from the leading characters of the string if @var{base} is 0. This is so that
- numbers like @samp{0.23} are not interpreted as octal.
- White space is allowed in the string, and is simply ignored. [This is not
- really true; white-space is ignored in the beginning of the string and within
- the mantissa, but not in other places, such as after a minus sign or in the
- exponent. We are considering changing the definition of this function, making
- it fail when there is any white-space in the input, since that makes a lot of
- sense. Please tell us your opinion about this change. Do you really want it
- to accept @nicode{"3 14"} as meaning 314 as it does now?]
- This function returns 0 if the entire string is a valid number in base
- @var{base}. Otherwise it returns @minus{}1.
- @end deftypefun
- @deftypefun void mpf_swap (mpf_t @var{rop1}, mpf_t @var{rop2})
- Swap @var{rop1} and @var{rop2} efficiently. Both the values and the
- precisions of the two variables are swapped.
- @end deftypefun
- @node Simultaneous Float Init & Assign, Converting Floats, Assigning Floats, Floating-point Functions
- @comment node-name, next, previous, up
- @section Combined Initialization and Assignment Functions
- @cindex Float assignment functions
- @cindex Assignment functions
- @cindex Float initialization functions
- @cindex Initialization functions
- For convenience, GMP provides a parallel series of initialize-and-set functions
- which initialize the output and then store the value there. These functions'
- names have the form @code{mpf_init_set@dots{}}
- Once the float has been initialized by any of the @code{mpf_init_set@dots{}}
- functions, it can be used as the source or destination operand for the ordinary
- float functions. Don't use an initialize-and-set function on a variable
- already initialized!
- @deftypefun void mpf_init_set (mpf_t @var{rop}, mpf_t @var{op})
- @deftypefunx void mpf_init_set_ui (mpf_t @var{rop}, unsigned long int @var{op})
- @deftypefunx void mpf_init_set_si (mpf_t @var{rop}, signed long int @var{op})
- @deftypefunx void mpf_init_set_d (mpf_t @var{rop}, double @var{op})
- Initialize @var{rop} and set its value from @var{op}.
- The precision of @var{rop} will be taken from the active default precision, as
- set by @code{mpf_set_default_prec}.
- @end deftypefun
- @deftypefun int mpf_init_set_str (mpf_t @var{rop}, char *@var{str}, int @var{base})
- Initialize @var{rop} and set its value from the string in @var{str}. See
- @code{mpf_set_str} above for details on the assignment operation.
- Note that @var{rop} is initialized even if an error occurs. (I.e., you have to
- call @code{mpf_clear} for it.)
- The precision of @var{rop} will be taken from the active default precision, as
- set by @code{mpf_set_default_prec}.
- @end deftypefun
- @node Converting Floats, Float Arithmetic, Simultaneous Float Init & Assign, Floating-point Functions
- @comment node-name, next, previous, up
- @section Conversion Functions
- @cindex Float conversion functions
- @cindex Conversion functions
- @deftypefun double mpf_get_d (mpf_t @var{op})
- Convert @var{op} to a @code{double}, truncating if necessary (ie.@: rounding
- towards zero).
- If the exponent in @var{op} is too big or too small to fit a @code{double}
- then the result is system dependent. For too big an infinity is returned when
- available. For too small @math{0.0} is normally returned. Hardware overflow,
- underflow and denorm traps may or may not occur.
- @end deftypefun
- @deftypefun double mpf_get_d_2exp (signed long int *@var{exp}, mpf_t @var{op})
- Convert @var{op} to a @code{double}, truncating if necessary (ie.@: rounding
- towards zero), and with an exponent returned separately.
- The return value is in the range @math{0.5@le{}@GMPabs{@var{d}}<1} and the
- exponent is stored to @code{*@var{exp}}. @m{@var{d} * 2^{exp}, @var{d} *
- 2^@var{exp}} is the (truncated) @var{op} value. If @var{op} is zero, the
- return is @math{0.0} and 0 is stored to @code{*@var{exp}}.
- @cindex @code{frexp}
- This is similar to the standard C @code{frexp} function (@pxref{Normalization
- Functions,,, libc, The GNU C Library Reference Manual}).
- @end deftypefun
- @deftypefun long mpf_get_si (mpf_t @var{op})
- @deftypefunx {unsigned long} mpf_get_ui (mpf_t @var{op})
- Convert @var{op} to a @code{long} or @code{unsigned long}, truncating any
- fraction part. If @var{op} is too big for the return type, the result is
- undefined.
- See also @code{mpf_fits_slong_p} and @code{mpf_fits_ulong_p}
- (@pxref{Miscellaneous Float Functions}).
- @end deftypefun
- @deftypefun {char *} mpf_get_str (char *@var{str}, mp_exp_t *@var{expptr}, int @var{base}, size_t @var{n_digits}, mpf_t @var{op})
- Convert @var{op} to a string of digits in base @var{base}. The base argument
- may vary from 2 to 62 or from @minus{}2 to @minus{}36. Up to @var{n_digits}
- digits will be generated. Trailing zeros are not returned. No more digits
- than can be accurately represented by @var{op} are ever generated. If
- @var{n_digits} is 0 then that accurate maximum number of digits are generated.
- For @var{base} in the range 2..36, digits and lower-case letters are used; for
- @minus{}2..@minus{}36, digits and upper-case letters are used; for 37..62,
- digits, upper-case letters, and lower-case letters (in that significance order)
- are used.
- If @var{str} is @code{NULL}, the result string is allocated using the current
- allocation function (@pxref{Custom Allocation}). The block will be
- @code{strlen(str)+1} bytes, that being exactly enough for the string and
- null-terminator.
- If @var{str} is not @code{NULL}, it should point to a block of
- @math{@var{n_digits} + 2} bytes, that being enough for the mantissa, a
- possible minus sign, and a null-terminator. When @var{n_digits} is 0 to get
- all significant digits, an application won't be able to know the space
- required, and @var{str} should be @code{NULL} in that case.
- The generated string is a fraction, with an implicit radix point immediately
- to the left of the first digit. The applicable exponent is written through
- the @var{expptr} pointer. For example, the number 3.1416 would be returned as
- string @nicode{"31416"} and exponent 1.
- When @var{op} is zero, an empty string is produced and the exponent returned
- is 0.
- A pointer to the result string is returned, being either the allocated block
- or the given @var{str}.
- @end deftypefun
- @node Float Arithmetic, Float Comparison, Converting Floats, Floating-point Functions
- @comment node-name, next, previous, up
- @section Arithmetic Functions
- @cindex Float arithmetic functions
- @cindex Arithmetic functions
- @deftypefun void mpf_add (mpf_t @var{rop}, mpf_t @var{op1}, mpf_t @var{op2})
- @deftypefunx void mpf_add_ui (mpf_t @var{rop}, mpf_t @var{op1}, unsigned long int @var{op2})
- Set @var{rop} to @math{@var{op1} + @var{op2}}.
- @end deftypefun
- @deftypefun void mpf_sub (mpf_t @var{rop}, mpf_t @var{op1}, mpf_t @var{op2})
- @deftypefunx void mpf_ui_sub (mpf_t @var{rop}, unsigned long int @var{op1}, mpf_t @var{op2})
- @deftypefunx void mpf_sub_ui (mpf_t @var{rop}, mpf_t @var{op1}, unsigned long int @var{op2})
- Set @var{rop} to @var{op1} @minus{} @var{op2}.
- @end deftypefun
- @deftypefun void mpf_mul (mpf_t @var{rop}, mpf_t @var{op1}, mpf_t @var{op2})
- @deftypefunx void mpf_mul_ui (mpf_t @var{rop}, mpf_t @var{op1}, unsigned long int @var{op2})
- Set @var{rop} to @math{@var{op1} @GMPtimes{} @var{op2}}.
- @end deftypefun
- Division is undefined if the divisor is zero, and passing a zero divisor to the
- divide functions will make these functions intentionally divide by zero. This
- lets the user handle arithmetic exceptions in these functions in the same
- manner as other arithmetic exceptions.
- @deftypefun void mpf_div (mpf_t @var{rop}, mpf_t @var{op1}, mpf_t @var{op2})
- @deftypefunx void mpf_ui_div (mpf_t @var{rop}, unsigned long int @var{op1}, mpf_t @var{op2})
- @deftypefunx void mpf_div_ui (mpf_t @var{rop}, mpf_t @var{op1}, unsigned long int @var{op2})
- @cindex Division functions
- Set @var{rop} to @var{op1}/@var{op2}.
- @end deftypefun
- @deftypefun void mpf_sqrt (mpf_t @var{rop}, mpf_t @var{op})
- @deftypefunx void mpf_sqrt_ui (mpf_t @var{rop}, unsigned long int @var{op})
- @cindex Root extraction functions
- Set @var{rop} to @m{sqrt{@var{op}}, the square root of @var{op}}.
- @end deftypefun
- @deftypefun void mpf_pow_ui (mpf_t @var{rop}, mpf_t @var{op1}, unsigned long int @var{op2})
- @cindex Exponentiation functions
- @cindex Powering functions
- Set @var{rop} to @m{@var{op1}^{op2}, @var{op1} raised to the power @var{op2}}.
- @end deftypefun
- @deftypefun void mpf_neg (mpf_t @var{rop}, mpf_t @var{op})
- Set @var{rop} to @minus{}@var{op}.
- @end deftypefun
- @deftypefun void mpf_abs (mpf_t @var{rop}, mpf_t @var{op})
- Set @var{rop} to the absolute value of @var{op}.
- @end deftypefun
- @deftypefun void mpf_mul_2exp (mpf_t @var{rop}, mpf_t @var{op1}, mp_bitcnt_t @var{op2})
- Set @var{rop} to @m{@var{op1} times 2^{op2}, @var{op1} times 2 raised to
- @var{op2}}.
- @end deftypefun
- @deftypefun void mpf_div_2exp (mpf_t @var{rop}, mpf_t @var{op1}, mp_bitcnt_t @var{op2})
- Set @var{rop} to @m{@var{op1}/2^{op2}, @var{op1} divided by 2 raised to
- @var{op2}}.
- @end deftypefun
- @node Float Comparison, I/O of Floats, Float Arithmetic, Floating-point Functions
- @comment node-name, next, previous, up
- @section Comparison Functions
- @cindex Float comparison functions
- @cindex Comparison functions
- @deftypefun int mpf_cmp (mpf_t @var{op1}, mpf_t @var{op2})
- @deftypefunx int mpf_cmp_d (mpf_t @var{op1}, double @var{op2})
- @deftypefunx int mpf_cmp_ui (mpf_t @var{op1}, unsigned long int @var{op2})
- @deftypefunx int mpf_cmp_si (mpf_t @var{op1}, signed long int @var{op2})
- Compare @var{op1} and @var{op2}. Return a positive value if @math{@var{op1} >
- @var{op2}}, zero if @math{@var{op1} = @var{op2}}, and a negative value if
- @math{@var{op1} < @var{op2}}.
- @code{mpf_cmp_d} can be called with an infinity, but results are undefined for
- a NaN.
- @end deftypefun
- @deftypefun int mpf_eq (mpf_t @var{op1}, mpf_t @var{op2}, mp_bitcnt_t op3)
- Return non-zero if the first @var{op3} bits of @var{op1} and @var{op2} are
- equal, zero otherwise. I.e., test if @var{op1} and @var{op2} are approximately
- equal.
- Caution 1: All version of GMP up to version 4.2.4 compared just whole limbs,
- meaning sometimes more than @var{op3} bits, sometimes fewer.
- Caution 2: This function will consider XXX11...111 and XX100...000 different,
- even if ... is replaced by a semi-infinite number of bits. Such numbers are
- really just one ulp off, and should be considered equal.
- @end deftypefun
- @deftypefun void mpf_reldiff (mpf_t @var{rop}, mpf_t @var{op1}, mpf_t @var{op2})
- Compute the relative difference between @var{op1} and @var{op2} and store the
- result in @var{rop}. This is @math{@GMPabs{@var{op1}-@var{op2}}/@var{op1}}.
- @end deftypefun
- @deftypefn Macro int mpf_sgn (mpf_t @var{op})
- @cindex Sign tests
- @cindex Float sign tests
- Return @math{+1} if @math{@var{op} > 0}, 0 if @math{@var{op} = 0}, and
- @math{-1} if @math{@var{op} < 0}.
- This function is actually implemented as a macro. It evaluates its arguments
- multiple times.
- @end deftypefn
- @node I/O of Floats, Miscellaneous Float Functions, Float Comparison, Floating-point Functions
- @comment node-name, next, previous, up
- @section Input and Output Functions
- @cindex Float input and output functions
- @cindex Input functions
- @cindex Output functions
- @cindex I/O functions
- Functions that perform input from a stdio stream, and functions that output to
- a stdio stream. Passing a @code{NULL} pointer for a @var{stream} argument to
- any of these functions will make them read from @code{stdin} and write to
- @code{stdout}, respectively.
- When using any of these functions, it is a good idea to include @file{stdio.h}
- before @file{gmp.h}, since that will allow @file{gmp.h} to define prototypes
- for these functions.
- @deftypefun size_t mpf_out_str (FILE *@var{stream}, int @var{base}, size_t @var{n_digits}, mpf_t @var{op})
- Print @var{op} to @var{stream}, as a string of digits. Return the number of
- bytes written, or if an error occurred, return 0.
- The mantissa is prefixed with an @samp{0.} and is in the given @var{base},
- which may vary from 2 to 62 or from @minus{}2 to @minus{}36. An exponent is
- then printed, separated by an @samp{e}, or if the base is greater than 10 then
- by an @samp{@@}. The exponent is always in decimal. The decimal point follows
- the current locale, on systems providing @code{localeconv}.
- For @var{base} in the range 2..36, digits and lower-case letters are used; for
- @minus{}2..@minus{}36, digits and upper-case letters are used; for 37..62,
- digits, upper-case letters, and lower-case letters (in that significance order)
- are used.
- Up to @var{n_digits} will be printed from the mantissa, except that no more
- digits than are accurately representable by @var{op} will be printed.
- @var{n_digits} can be 0 to select that accurate maximum.
- @end deftypefun
- @deftypefun size_t mpf_inp_str (mpf_t @var{rop}, FILE *@var{stream}, int @var{base})
- Read a string in base @var{base} from @var{stream}, and put the read float in
- @var{rop}. The string is of the form @samp{M@@N} or, if the base is 10 or
- less, alternatively @samp{MeN}. @samp{M} is the mantissa and @samp{N} is the
- exponent. The mantissa is always in the specified base. The exponent is
- either in the specified base or, if @var{base} is negative, in decimal. The
- decimal point expected is taken from the current locale, on systems providing
- @code{localeconv}.
- The argument @var{base} may be in the ranges 2 to 36, or @minus{}36 to
- @minus{}2. Negative values are used to specify that the exponent is in
- decimal.
- Unlike the corresponding @code{mpz} function, the base will not be determined
- from the leading characters of the string if @var{base} is 0. This is so that
- numbers like @samp{0.23} are not interpreted as octal.
- Return the number of bytes read, or if an error occurred, return 0.
- @end deftypefun
- @c @deftypefun void mpf_out_raw (FILE *@var{stream}, mpf_t @var{float})
- @c Output @var{float} on stdio stream @var{stream}, in raw binary
- @c format. The float is written in a portable format, with 4 bytes of
- @c size information, and that many bytes of limbs. Both the size and the
- @c limbs are written in decreasing significance order.
- @c @end deftypefun
- @c @deftypefun void mpf_inp_raw (mpf_t @var{float}, FILE *@var{stream})
- @c Input from stdio stream @var{stream} in the format written by
- @c @code{mpf_out_raw}, and put the result in @var{float}.
- @c @end deftypefun
- @node Miscellaneous Float Functions, , I/O of Floats, Floating-point Functions
- @comment node-name, next, previous, up
- @section Miscellaneous Functions
- @cindex Miscellaneous float functions
- @cindex Float miscellaneous functions
- @deftypefun void mpf_ceil (mpf_t @var{rop}, mpf_t @var{op})
- @deftypefunx void mpf_floor (mpf_t @var{rop}, mpf_t @var{op})
- @deftypefunx void mpf_trunc (mpf_t @var{rop}, mpf_t @var{op})
- @cindex Rounding functions
- @cindex Float rounding functions
- Set @var{rop} to @var{op} rounded to an integer. @code{mpf_ceil} rounds to the
- next higher integer, @code{mpf_floor} to the next lower, and @code{mpf_trunc}
- to the integer towards zero.
- @end deftypefun
- @deftypefun int mpf_integer_p (mpf_t @var{op})
- Return non-zero if @var{op} is an integer.
- @end deftypefun
- @deftypefun int mpf_fits_ulong_p (mpf_t @var{op})
- @deftypefunx int mpf_fits_slong_p (mpf_t @var{op})
- @deftypefunx int mpf_fits_uint_p (mpf_t @var{op})
- @deftypefunx int mpf_fits_sint_p (mpf_t @var{op})
- @deftypefunx int mpf_fits_ushort_p (mpf_t @var{op})
- @deftypefunx int mpf_fits_sshort_p (mpf_t @var{op})
- Return non-zero if @var{op} would fit in the respective C data type, when
- truncated to an integer.
- @end deftypefun
- @deftypefun void mpf_urandomb (mpf_t @var{rop}, gmp_randstate_t @var{state}, mp_bitcnt_t @var{nbits})
- @cindex Random number functions
- @cindex Float random number functions
- Generate a uniformly distributed random float in @var{rop}, such that @math{0
- @le{} @var{rop} < 1}, with @var{nbits} significant bits in the mantissa.
- The variable @var{state} must be initialized by calling one of the
- @code{gmp_randinit} functions (@ref{Random State Initialization}) before
- invoking this function.
- @end deftypefun
- @deftypefun void mpf_random2 (mpf_t @var{rop}, mp_size_t @var{max_size}, mp_exp_t @var{exp})
- Generate a random float of at most @var{max_size} limbs, with long strings of
- zeros and ones in the binary representation. The exponent of the number is in
- the interval @minus{}@var{exp} to @var{exp} (in limbs). This function is
- useful for testing functions and algorithms, since these kind of random
- numbers have proven to be more likely to trigger corner-case bugs. Negative
- random numbers are generated when @var{max_size} is negative.
- @end deftypefun
- @c @deftypefun size_t mpf_size (mpf_t @var{op})
- @c Return the size of @var{op} measured in number of limbs. If @var{op} is
- @c zero, the returned value will be zero. (@xref{Nomenclature}, for an
- @c explanation of the concept @dfn{limb}.)
- @c
- @c @strong{This function is obsolete. It will disappear from future GMP
- @c releases.}
- @c @end deftypefun
- @node Low-level Functions, Random Number Functions, Floating-point Functions, Top
- @comment node-name, next, previous, up
- @chapter Low-level Functions
- @cindex Low-level functions
- This chapter describes low-level GMP functions, used to implement the
- high-level GMP functions, but also intended for time-critical user code.
- These functions start with the prefix @code{mpn_}.
- @c 1. Some of these function clobber input operands.
- @c
- The @code{mpn} functions are designed to be as fast as possible, @strong{not}
- to provide a coherent calling interface. The different functions have somewhat
- similar interfaces, but there are variations that make them hard to use. These
- functions do as little as possible apart from the real multiple precision
- computation, so that no time is spent on things that not all callers need.
- A source operand is specified by a pointer to the least significant limb and a
- limb count. A destination operand is specified by just a pointer. It is the
- responsibility of the caller to ensure that the destination has enough space
- for storing the result.
- With this way of specifying operands, it is possible to perform computations on
- subranges of an argument, and store the result into a subrange of a
- destination.
- A common requirement for all functions is that each source area needs at least
- one limb. No size argument may be zero. Unless otherwise stated, in-place
- operations are allowed where source and destination are the same, but not where
- they only partly overlap.
- The @code{mpn} functions are the base for the implementation of the
- @code{mpz_}, @code{mpf_}, and @code{mpq_} functions.
- This example adds the number beginning at @var{s1p} and the number beginning at
- @var{s2p} and writes the sum at @var{destp}. All areas have @var{n} limbs.
- @example
- cy = mpn_add_n (destp, s1p, s2p, n)
- @end example
- It should be noted that the @code{mpn} functions make no attempt to identify
- high or low zero limbs on their operands, or other special forms. On random
- data such cases will be unlikely and it'd be wasteful for every function to
- check every time. An application knowing something about its data can take
- steps to trim or perhaps split its calculations.
- @c
- @c For reference, within gmp mpz_t operands never have high zero limbs, and
- @c we rate low zero limbs as unlikely too (or something an application should
- @c handle). This is a prime motivation for not stripping zero limbs in say
- @c mpn_mul_n etc.
- @c
- @c Other applications doing variable-length calculations will quite likely do
- @c something similar to mpz. And even if not then it's highly likely zero
- @c limb stripping can be done at just a few judicious points, which will be
- @c more efficient than having lots of mpn functions checking every time.
- @sp 1
- @noindent
- In the notation used below, a source operand is identified by the pointer to
- the least significant limb, and the limb count in braces. For example,
- @{@var{s1p}, @var{s1n}@}.
- @deftypefun mp_limb_t mpn_add_n (mp_limb_t *@var{rp}, const mp_limb_t *@var{s1p}, const mp_limb_t *@var{s2p}, mp_size_t @var{n})
- Add @{@var{s1p}, @var{n}@} and @{@var{s2p}, @var{n}@}, and write the @var{n}
- least significant limbs of the result to @var{rp}. Return carry, either 0 or
- 1.
- This is the lowest-level function for addition. It is the preferred function
- for addition, since it is written in assembly for most CPUs. For addition of
- a variable to itself (i.e., @var{s1p} equals @var{s2p}) use @code{mpn_lshift}
- with a count of 1 for optimal speed.
- @end deftypefun
- @deftypefun mp_limb_t mpn_add_1 (mp_limb_t *@var{rp}, const mp_limb_t *@var{s1p}, mp_size_t @var{n}, mp_limb_t @var{s2limb})
- Add @{@var{s1p}, @var{n}@} and @var{s2limb}, and write the @var{n} least
- significant limbs of the result to @var{rp}. Return carry, either 0 or 1.
- @end deftypefun
- @deftypefun mp_limb_t mpn_add (mp_limb_t *@var{rp}, const mp_limb_t *@var{s1p}, mp_size_t @var{s1n}, const mp_limb_t *@var{s2p}, mp_size_t @var{s2n})
- Add @{@var{s1p}, @var{s1n}@} and @{@var{s2p}, @var{s2n}@}, and write the
- @var{s1n} least significant limbs of the result to @var{rp}. Return carry,
- either 0 or 1.
- This function requires that @var{s1n} is greater than or equal to @var{s2n}.
- @end deftypefun
- @deftypefun mp_limb_t mpn_sub_n (mp_limb_t *@var{rp}, const mp_limb_t *@var{s1p}, const mp_limb_t *@var{s2p}, mp_size_t @var{n})
- Subtract @{@var{s2p}, @var{n}@} from @{@var{s1p}, @var{n}@}, and write the
- @var{n} least significant limbs of the result to @var{rp}. Return borrow,
- either 0 or 1.
- This is the lowest-level function for subtraction. It is the preferred
- function for subtraction, since it is written in assembly for most CPUs.
- @end deftypefun
- @deftypefun mp_limb_t mpn_sub_1 (mp_limb_t *@var{rp}, const mp_limb_t *@var{s1p}, mp_size_t @var{n}, mp_limb_t @var{s2limb})
- Subtract @var{s2limb} from @{@var{s1p}, @var{n}@}, and write the @var{n} least
- significant limbs of the result to @var{rp}. Return borrow, either 0 or 1.
- @end deftypefun
- @deftypefun mp_limb_t mpn_sub (mp_limb_t *@var{rp}, const mp_limb_t *@var{s1p}, mp_size_t @var{s1n}, const mp_limb_t *@var{s2p}, mp_size_t @var{s2n})
- Subtract @{@var{s2p}, @var{s2n}@} from @{@var{s1p}, @var{s1n}@}, and write the
- @var{s1n} least significant limbs of the result to @var{rp}. Return borrow,
- either 0 or 1.
- This function requires that @var{s1n} is greater than or equal to
- @var{s2n}.
- @end deftypefun
- @deftypefun void mpn_neg (mp_limb_t *@var{rp}, const mp_limb_t *@var{sp}, mp_size_t @var{n})
- Perform the negation of @{@var{sp}, @var{n}@}, and write the result to
- @{@var{rp}, @var{n}@}. Return carry-out.
- @end deftypefun
- @deftypefun void mpn_mul_n (mp_limb_t *@var{rp}, const mp_limb_t *@var{s1p}, const mp_limb_t *@var{s2p}, mp_size_t @var{n})
- Multiply @{@var{s1p}, @var{n}@} and @{@var{s2p}, @var{n}@}, and write the
- 2*@var{n}-limb result to @var{rp}.
- The destination has to have space for 2*@var{n} limbs, even if the product's
- most significant limb is zero. No overlap is permitted between the
- destination and either source.
- If the two input operands are the same, use @code{mpn_sqr}.
- @end deftypefun
- @deftypefun mp_limb_t mpn_mul (mp_limb_t *@var{rp}, const mp_limb_t *@var{s1p}, mp_size_t @var{s1n}, const mp_limb_t *@var{s2p}, mp_size_t @var{s2n})
- Multiply @{@var{s1p}, @var{s1n}@} and @{@var{s2p}, @var{s2n}@}, and write the
- (@var{s1n}+@var{s2n})-limb result to @var{rp}. Return the most significant
- limb of the result.
- The destination has to have space for @var{s1n} + @var{s2n} limbs, even if the
- product's most significant limb is zero. No overlap is permitted between the
- destination and either source.
- This function requires that @var{s1n} is greater than or equal to @var{s2n}.
- @end deftypefun
- @deftypefun void mpn_sqr (mp_limb_t *@var{rp}, const mp_limb_t *@var{s1p}, mp_size_t @var{n})
- Compute the square of @{@var{s1p}, @var{n}@} and write the 2*@var{n}-limb
- result to @var{rp}.
- The destination has to have space for 2*@var{n} limbs, even if the result's
- most significant limb is zero. No overlap is permitted between the
- destination and the source.
- @end deftypefun
- @deftypefun mp_limb_t mpn_mul_1 (mp_limb_t *@var{rp}, const mp_limb_t *@var{s1p}, mp_size_t @var{n}, mp_limb_t @var{s2limb})
- Multiply @{@var{s1p}, @var{n}@} by @var{s2limb}, and write the @var{n} least
- significant limbs of the product to @var{rp}. Return the most significant
- limb of the product. @{@var{s1p}, @var{n}@} and @{@var{rp}, @var{n}@} are
- allowed to overlap provided @math{@var{rp} @le{} @var{s1p}}.
- This is a low-level function that is a building block for general
- multiplication as well as other operations in GMP@. It is written in assembly
- for most CPUs.
- Don't call this function if @var{s2limb} is a power of 2; use @code{mpn_lshift}
- with a count equal to the logarithm of @var{s2limb} instead, for optimal speed.
- @end deftypefun
- @deftypefun mp_limb_t mpn_addmul_1 (mp_limb_t *@var{rp}, const mp_limb_t *@var{s1p}, mp_size_t @var{n}, mp_limb_t @var{s2limb})
- Multiply @{@var{s1p}, @var{n}@} and @var{s2limb}, and add the @var{n} least
- significant limbs of the product to @{@var{rp}, @var{n}@} and write the result
- to @var{rp}. Return the most significant limb of the product, plus carry-out
- from the addition.
- This is a low-level function that is a building block for general
- multiplication as well as other operations in GMP@. It is written in assembly
- for most CPUs.
- @end deftypefun
- @deftypefun mp_limb_t mpn_submul_1 (mp_limb_t *@var{rp}, const mp_limb_t *@var{s1p}, mp_size_t @var{n}, mp_limb_t @var{s2limb})
- Multiply @{@var{s1p}, @var{n}@} and @var{s2limb}, and subtract the @var{n}
- least significant limbs of the product from @{@var{rp}, @var{n}@} and write the
- result to @var{rp}. Return the most significant limb of the product, plus
- borrow-out from the subtraction.
- This is a low-level function that is a building block for general
- multiplication and division as well as other operations in GMP@. It is written
- in assembly for most CPUs.
- @end deftypefun
- @deftypefun void mpn_tdiv_qr (mp_limb_t *@var{qp}, mp_limb_t *@var{rp}, mp_size_t @var{qxn}, const mp_limb_t *@var{np}, mp_size_t @var{nn}, const mp_limb_t *@var{dp}, mp_size_t @var{dn})
- Divide @{@var{np}, @var{nn}@} by @{@var{dp}, @var{dn}@} and put the quotient
- at @{@var{qp}, @var{nn}@minus{}@var{dn}+1@} and the remainder at @{@var{rp},
- @var{dn}@}. The quotient is rounded towards 0.
- No overlap is permitted between arguments, except that @var{np} might equal
- @var{rp}. The dividend size @var{nn} must be greater than or equal to divisor
- size @var{dn}. The most significant limb of the divisor must be non-zero. The
- @var{qxn} operand must be zero.
- @end deftypefun
- @deftypefun mp_limb_t mpn_divrem (mp_limb_t *@var{r1p}, mp_size_t @var{qxn}, mp_limb_t *@var{rs2p}, mp_size_t @var{rs2n}, const mp_limb_t *@var{s3p}, mp_size_t @var{s3n})
- [This function is obsolete. Please call @code{mpn_tdiv_qr} instead for best
- performance.]
- Divide @{@var{rs2p}, @var{rs2n}@} by @{@var{s3p}, @var{s3n}@}, and write the
- quotient at @var{r1p}, with the exception of the most significant limb, which
- is returned. The remainder replaces the dividend at @var{rs2p}; it will be
- @var{s3n} limbs long (i.e., as many limbs as the divisor).
- In addition to an integer quotient, @var{qxn} fraction limbs are developed, and
- stored after the integral limbs. For most usages, @var{qxn} will be zero.
- It is required that @var{rs2n} is greater than or equal to @var{s3n}. It is
- required that the most significant bit of the divisor is set.
- If the quotient is not needed, pass @var{rs2p} + @var{s3n} as @var{r1p}. Aside
- from that special case, no overlap between arguments is permitted.
- Return the most significant limb of the quotient, either 0 or 1.
- The area at @var{r1p} needs to be @var{rs2n} @minus{} @var{s3n} + @var{qxn}
- limbs large.
- @end deftypefun
- @deftypefn Function mp_limb_t mpn_divrem_1 (mp_limb_t *@var{r1p}, mp_size_t @var{qxn}, @w{mp_limb_t *@var{s2p}}, mp_size_t @var{s2n}, mp_limb_t @var{s3limb})
- @deftypefnx Macro mp_limb_t mpn_divmod_1 (mp_limb_t *@var{r1p}, mp_limb_t *@var{s2p}, @w{mp_size_t @var{s2n}}, @w{mp_limb_t @var{s3limb}})
- Divide @{@var{s2p}, @var{s2n}@} by @var{s3limb}, and write the quotient at
- @var{r1p}. Return the remainder.
- The integer quotient is written to @{@var{r1p}+@var{qxn}, @var{s2n}@} and in
- addition @var{qxn} fraction limbs are developed and written to @{@var{r1p},
- @var{qxn}@}. Either or both @var{s2n} and @var{qxn} can be zero. For most
- usages, @var{qxn} will be zero.
- @code{mpn_divmod_1} exists for upward source compatibility and is simply a
- macro calling @code{mpn_divrem_1} with a @var{qxn} of 0.
- The areas at @var{r1p} and @var{s2p} have to be identical or completely
- separate, not partially overlapping.
- @end deftypefn