[Gmp-commit] /var/hg/gmp: Document foo_ptr and foo_srcptr

mercurial at gmplib.org mercurial at gmplib.org
Sun Jun 20 12:21:56 UTC 2021


details:   /var/hg/gmp/rev/143e9a7f3ece
changeset: 18225:143e9a7f3ece
user:      Marc Glisse <marc.glisse at inria.fr>
date:      Sun Jun 20 14:21:52 2021 +0200
description:
Document foo_ptr and foo_srcptr

diffstat:

 ChangeLog    |   4 +++
 doc/gmp.texi |  74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 74 insertions(+), 4 deletions(-)

diffs (117 lines):

diff -r eac10a64bc99 -r 143e9a7f3ece ChangeLog
--- a/ChangeLog	Sun Jun 06 23:29:28 2021 +0200
+++ b/ChangeLog	Sun Jun 20 14:21:52 2021 +0200
@@ -1,3 +1,7 @@
+2021-06-20  Niels Möller  <nisse at lysator.liu.se>
+
+	* doc/gmp.texi: Document foo_ptr and foo_srcptr.
+
 2021-06-06  Paul Eggert  <eggert at cs.ucla.edu>
 
 	* configure.ac (AC_INIT): Avoid comma in BUG-REPORT field.
diff -r eac10a64bc99 -r 143e9a7f3ece doc/gmp.texi
--- a/doc/gmp.texi	Sun Jun 06 23:29:28 2021 +0200
+++ b/doc/gmp.texi	Sun Jun 20 14:21:52 2021 +0200
@@ -1982,6 +1982,60 @@
 Also, in general @code{mp_bitcnt_t} is used for bit counts and ranges, and
 @code{size_t} is used for byte or character counts.
 
+ at sp 1
+
+ at cindex Pointer types
+ at tindex @code{mpz_ptr}
+ at tindex @code{mpz_srcptr}
+ at tindex @code{mpq_ptr}
+ at tindex @code{mpq_srcptr}
+ at tindex @code{mpf_ptr}
+ at tindex @code{mpf_srcptr}
+ at tindex @code{gmp_randstate_ptr}
+ at tindex @code{gmp_randstate_srcptr}
+Internally, GMP data types such as @code{mpz_t} are defined as one-element
+arrays, whose element type is part of the GMP internals (@pxref{Internals}).
+
+When an array is used as a function argument in C, it is not passed by value,
+instead its value is a pointer to the first element. In C jargon, this is
+sometimes referred to as the array "decaying" to a pointer. For GMP types like
+ at code{mpz_t}, that means that the function called gets a pointer to the
+caller's @code{mpz_t} value, which is why no explicit @code{&} operator is
+needed when passing output arguments (@pxref{Parameter Conventions}).
+
+GMP defines names for these pointer types, e.g., @code{mpz_ptr} corresponding
+to @code{mpz_t}, and @code{mpz_srcptr} corresponding to @code{const mpz_t}.
+Most functions don't need to use these pointer types directly; it works fine to
+declare a function using the @code{mpz_t} or @code{const mpz_t} as the argument
+types, the same "pointer decay" happens in the background regardless.
+
+Occasionally, it is useful to manipulate pointers directly, e.g, to
+conditionally swap @emph{references} to a function's inputs without changing
+the @emph{values} as seen by the caller, or returning a pointer to an
+ at code{mpz_t} which is part of a larger structure. For these cases, the pointer
+types are necessary. And a @code{mpz_ptr} can be passed as argument to any GMP
+function declared to take an @code{mpz_t} argument.
+
+Their definition is equivalent to the following code, which is given for
+illustratory purposes only:
+
+ at example
+    typedef foo_internal foo_t[1];
+    typedef foo_internal * foo_ptr;
+    typedef const foo_internal * foo_srcptr;
+ at end example
+
+The following pointer types are defined by GMP:
+ at itemize
+ at item @code{mpz_ptr} for pointers to the element type in @code{mpz_t}
+ at item @code{mpz_srcptr} for @code{const} pointers to the element type in @code{mpz_t}
+ at item @code{mpq_ptr} for pointers to the element type in @code{mpq_t}
+ at item @code{mpq_srcptr} for @code{const} pointers to the element type in @code{mpq_t}
+ at item @code{mpf_ptr} for pointers to the element type in @code{mpf_t}
+ at item @code{mpf_srcptr} for @code{const} pointers to the element type in @code{mpf_t}
+ at item @code{gmp_randstate_ptr} for pointers to the element type in @code{gmp_randstate_t}
+ at item @code{gmp_randstate_srcptr} for @code{const} pointers to the element type in @code{gmp_randstate_t}
+ at end itemize
 
 @node Function Classes, Variable Conventions, Nomenclature and Types, GMP Basics
 @section Function Classes
@@ -2071,7 +2125,9 @@
 
 GMP types like @code{mpz_t} are implemented as one-element arrays of certain
 structures.  Declaring a variable creates an object with the fields GMP needs,
-but variables are normally manipulated by using the pointer to the object.  For
+but variables are normally manipulated by using the pointer to the
+object.  The appropriate pointer types (@ref{Nomenclature and Types}) may
+be used to explicitly manipulate the pointer.  For
 both behavior and efficiency reasons, it is discouraged to make copies of the
 GMP object itself (either directly or via aggregate objects containing such GMP
 objects).  If copies are done, all of them must be used read-only; using a copy
@@ -2129,6 +2185,13 @@
 
 Since GMP types are implemented as one-element arrays, using a GMP variable as
 a parameter passes a pointer to the object. Hence the call-by-reference.
+A more explicit (and equivalent) prototype for our function @code{foo}
+could be:
+
+ at example
+void foo (mpz_ptr result, mpz_srcptr param, unsigned long n);
+ at end example
+
 
 
 @need 1000
@@ -4519,10 +4582,13 @@
 (@pxref{Rational Number Functions}) then @code{mpq_canonicalize} must be
 called before any other @code{mpq} functions are applied to that @code{mpq_t}.
 
- at deftypefn Macro mpz_t mpq_numref (const mpq_t @var{op})
- at deftypefnx Macro mpz_t mpq_denref (const mpq_t @var{op})
+ at deftypefn Macro mpz_ptr mpq_numref (const mpq_t @var{op})
+ at deftypefnx Macro mpz_ptr mpq_denref (const 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.
+The @code{mpz} functions can be used on the result of these macros.  Such
+calls may modify the numerator or denominator. However, care
+should be taken so that @var{op} remains in canonical form prior to a
+possible later call to an @code{mpq} function.
 @end deftypefn
 
 @deftypefun void mpq_get_num (mpz_t @var{numerator}, const mpq_t @var{rational})


More information about the gmp-commit mailing list