| 1 |
GMPAda makes the GNU MultiPrecision library usable with the Ada |
| 2 |
language. |
| 3 |
|
| 4 |
Extensive documentation about the GMP original C library can be found |
| 5 |
at http://gmplib.org/manual. |
| 6 |
|
| 7 |
The latest source is available on https://nicolas.boulenguez.free.fr. |
| 8 |
|
| 9 |
--------------------- Usage -------------------------- |
| 10 |
|
| 11 |
The demo subdirectory contains a test program. It is first intended as |
| 12 |
a test for the library, almost without user-readable output. Though, |
| 13 |
reading the source file "demo.adb" will give you an idea of how this |
| 14 |
library works. Once it is well installed, you can compile your |
| 15 |
programs with the gprbuild tool as described in the installed project, |
| 16 |
or use gnatmake as shown in the Makefile for the test target. |
| 17 |
|
| 18 |
A static library and a dynamic library are provided, but if you want |
| 19 |
the best performances, you should recompile your program using the |
| 20 |
library source and the -O2 -gnatn switches, as inlining is very |
| 21 |
efficient for the thin binding part. |
| 22 |
|
| 23 |
-------------------- Status --------------------- |
| 24 |
|
| 25 |
Big_Integer and Big_Float operations are available and coherent. |
| 26 |
Big_Rationals operations are usable. |
| 27 |
|
| 28 |
--------------- What is GMPAda? --------------- |
| 29 |
|
| 30 |
The GNU_Multiple_Precision package provides the type Big_Integer |
| 31 |
allowing integer computations without other space limitation than your |
| 32 |
computer's memory. |
| 33 |
|
| 34 |
Initialization, assignement and finalization are provided by a |
| 35 |
controlled type. Equivalent of operations and attributes that you |
| 36 |
might expect when used to Ada.Standard.Integer and Ada.Standard.Float |
| 37 |
are provided. |
| 38 |
|
| 39 |
One day, there would be Big_Rationals and Big_Complexes childs, but |
| 40 |
for the moment the former learns philosophy while the later is in |
| 41 |
therapy. |
| 42 |
|
| 43 |
The child packages named Random_Numbers, Text_IO, Wide_Text_IO, |
| 44 |
Wide_Wide_Text_IO provide the services documented in the Ada Reference |
| 45 |
Manual for Ada child packages with the same names (actually, there |
| 46 |
are some subtle differences, see Random_Numbers and Generic_Text_IO |
| 47 |
specifications). |
| 48 |
|
| 49 |
The Text_IO packages are generic instantiations. This allows each |
| 50 |
user to set its own Default_Width and Default_Base by making its own |
| 51 |
instantiation, as with Ada.Text_IO subpackages, if it is ever |
| 52 |
necessary. There are (better) technical reasons too, see the |
| 53 |
Implementation below. |
| 54 |
|
| 55 |
----------- Important performance note ----------------- |
| 56 |
|
| 57 |
You should see these Ada types as limited if you seek performance. |
| 58 |
Each assignement allocates then frees space for temporary objects, |
| 59 |
eating much execution time. |
| 60 |
|
| 61 |
Quickly said: if you mind performance, write |
| 62 |
Set (A, B); instead of A := B; |
| 63 |
Add (A, A, C); instead of A := A + C; |
| 64 |
|
| 65 |
Then, why allow assignment, and equivalent of Ada.Standard attributes |
| 66 |
and primitive operations? For users who need readable code and do not |
| 67 |
care for execution speed. I originally needed long integers and a |
| 68 |
readable language like Ada for educational purpose. |
| 69 |
|
| 70 |
-------------- Data exchange with C streams --------------- |
| 71 |
|
| 72 |
The Ada Stream attributes of Big_Integer respect the raw format used |
| 73 |
by the library for the C mpz_t type. |
| 74 |
|
| 75 |
The Big_Rational type provides sensible Stream attributes, but without |
| 76 |
C raw IO equivalents. This library writes the numerator then the |
| 77 |
denominator (as Big_Integers=mpz_t). |
| 78 |
|
| 79 |
Stream attributes for Big_Float will not do anything useful, and would |
| 80 |
anyway have no C equivalent. |
| 81 |
|
| 82 |
--------- Implementation ----------------- |
| 83 |
|
| 84 |
The GMP hierarchy is for internal purpose. The GNU_Multiple_Precision |
| 85 |
hierarchy exports controlled types. |
| 86 |
|
| 87 |
The GCC C++ compiler generates the gmp_h.ads Ada header, which defines |
| 88 |
types compatible with the C library. It is currently unable to define |
| 89 |
most functions. |
| 90 |
|
| 91 |
The C library "gmp_macros" expands gmp C preprocessor macros into C |
| 92 |
functions, so that they can be imported to Ada and linked. Rewriting |
| 93 |
them is an easy temptation, but the former solution is portable and |
| 94 |
GMP-version independant. It avoids depending on the internal |
| 95 |
structure of mp[tqf]_t C types. There is no significative efficiency |
| 96 |
counterpart since the imported procedures can be explicitely inlined |
| 97 |
if profiling shows it is necessary (and if the compiler does not do it |
| 98 |
during optimization). |
| 99 |
|
| 100 |
The Ada package "GMP.Binding" imports the C types and functions from |
| 101 |
gmp.h. It is only intended as a step to build a controlled |
| 102 |
abstraction. I cannot see who would benefit from a thin binding like |
| 103 |
that anyway. The visible part should only contain informations |
| 104 |
gathered in the normative part of the documentation. With GNAT, an |
| 105 |
array is passed by reference. This is compatible with a C one-element |
| 106 |
array like mpz_t. I hope this won't chage for a while, since "access" |
| 107 |
mode instead of "in, out, in out" modes would be much less |
| 108 |
readable. Types like mpz_t should be limited, but this would make |
| 109 |
another dereference mandatory in the controlled abstraction (a |
| 110 |
significative performance hit), or make it limited too (many users |
| 111 |
would miss slow but readable "+" overloading), and I think both prices |
| 112 |
are too high. |
| 113 |
|
| 114 |
The private part and the body may vary with the gmp version. Namely, |
| 115 |
they depend on mpz_t internal structure (for raw input) and GMP raw IO |
| 116 |
format (for raw input and output). I cannot see how to rewrite the |
| 117 |
Read procedure using only the gmp.h procedures without a huge |
| 118 |
performance hit. That would remove the dependance on the mpz_t |
| 119 |
internal structure. The binding would be simplier and depend less on |
| 120 |
the gmp version (raw IO changes are rare, since C users do not like |
| 121 |
them either). Help appreciated. |
| 122 |
|
| 123 |
Types visible by Ada users are controlled, so that initialization and |
| 124 |
finalization are handled automatically. Text Input-Output is rewritten |
| 125 |
from scratch, with generic packages avoiding code repetition for wide |
| 126 |
versions. It allows giving the IO format described by the ARM. Only |
| 127 |
change with ARM, each expected out mode parameter is made an "in out" |
| 128 |
mode parameter to avoid unuseful Finalization-Initialization. |
| 129 |
|
| 130 |
All types are declared in the same parent package, so that conversions |
| 131 |
of private extensions are possible. It avoids that the conversion |
| 132 |
programs depend on two tagged type and try to dispatch twice, too... |
| 133 |
Suggestions welcome. |
| 134 |
|
| 135 |
------------ Credits ------------ |
| 136 |
|
| 137 |
Thanks to Michael Roe and Gisle Saelensmind for providing their work |
| 138 |
on the net. This work is inspired by theirs, to say the least. |
| 139 |
|
| 140 |
Thanks to Jerome Delcourt for assembling documentation and links |
| 141 |
concerning big numbers in Ada. |
| 142 |
|
| 143 |
Thanks to Ludovic Brenta for providing a readable example of project |
| 144 |
file in its debian packaging of gtkada. |
| 145 |
|
| 146 |
Thanks to Vincent Diemunsch for helping to merge his similar work and |
| 147 |
correcting bugs. |
| 148 |
|
| 149 |
Thanks for Bernard Weisser for reporting some bugs. |
| 150 |
|
| 151 |
----- Alternative bindings (updated 2013) ----- |
| 152 |
|
| 153 |
As mentioned above, this binding should strictly extend Gisle |
| 154 |
Saelensmind's work (not available anymore), and Michael Roe's |
| 155 |
GNU_Multiple_Precision |
| 156 |
http://www.chiark.greenend.org.uk/~mroe/Ada/GMP/index.html |
| 157 |
|
| 158 |
http://code.google.com/p/adabindinggmpmpfr/ |
| 159 |
Vincent Diemunsch has written a similar work, focusing on MPFR. |
| 160 |
|
| 161 |
http://svn.eu.adacore.com/anonsvn/Dev/trunk/gps/gnatlib/src/gmp/gnatcoll-gmp-integers.ads |
| 162 |
Adacore has reimplemented parts of this binding. |