summaryrefslogtreecommitdiffstats
path: root/gnu/lib/libgmp/gmp.texi
blob: 7aaa88bf2ab2acc7da18fe11461166c7d93acc51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
\input texinfo    @c -*-texinfo-*-
@comment %**start of header
@setfilename gmp.info
@settitle GNU MP 1.3.2
@synindex tp fn
@c footnotestyle separate
@c paragraphindent 2
@comment %**end of header

@c smallbook

@iftex
@finalout
@end iftex

@c Note: the edition number is listed in *three* places; please update
@c all three.  Also, update the month and year where appropriate.

@c ==> Update edition number for settitle and subtitle, and in the
@c ==> following paragraph; update date, too.

@ifinfo
This file documents GNU MP, a library for arbitrary-precision integer
and rational number arithmetic.

This is a draft edition of the documentation, last updated May 20 1993.

Copyright (C) 1991, 1993 Free Software Foundation, Inc.

Permission is granted to make and distribute verbatim copies of
this manual provided the copyright notice and this permission notice
are preserved on all copies.

@ignore
Permission is granted to process this file through TeX and print the
results, provided the printed document carries copying permission
notice identical to this one except for the removal of this paragraph
(this paragraph not being relevant to the printed manual).

@end ignore
Permission is granted to copy and distribute modified versions of this
manual under the conditions for verbatim copying, provided that the entire
resulting derived work is distributed under the terms of a permission
notice identical to this one.

Permission is granted to copy and distribute translations of this manual
into another language, under the above conditions for modified versions,
except that this permission notice may be stated in a translation approved
by the Foundation.
@end ifinfo

@setchapternewpage odd
@titlepage
@c  use the new format for titles

@title GNU MP
@subtitle The GNU Multiple Precision Arithmetic Library
@subtitle Edition 1.3.2
@subtitle May 1993

@author by Torbj@"orn Granlund

@comment   Include the Distribution inside the titlepage so
@c that headings are turned off.

@page
@vskip 0pt plus 1filll
Copyright @copyright{} 1991, 1993 Free Software Foundation, Inc.

@sp 2

Published by the Free Software Foundation @*
675 Massachusetts Avenue, @*
Cambridge, MA 02139 USA @*

Permission is granted to make and distribute verbatim copies of
this manual provided the copyright notice and this permission notice
are preserved on all copies.

Permission is granted to copy and distribute modified versions of this
manual under the conditions for verbatim copying, provided that the entire
resulting derived work is distributed under the terms of a permission
notice identical to this one.

Permission is granted to copy and distribute translations of this manual
into another language, under the above conditions for modified versions,
except that this permission notice may be stated in a translation approved
by the Foundation.
@end titlepage

@ifinfo
@node Top, Copying, (dir), (dir)
@end ifinfo

@menu
* Copying::                  GMP Copying Conditions.
* Intro::                    Introduction to GMP.
* Nomenclature::             Terminology and basic data types.
* Initialization::           Initialization of multi-precision number objects.
* Integer Functions::        Functions for arithmetic on signed integers.
* Rational Number Functions:: Functions for arithmetic on rational numbers.
* Low-level Functions::      Fast functions for natural numbers.
* BSD Compatible Functions:: All functions found in BSD MP (somewhat faster).
* Miscellaneous Functions::  Functions that do particular things.
* Custom Allocation::        How to customize the internal allocation.
* Reporting Bugs::           Help us to improve this library.

* References::
* Concept Index::
* Function Index::
@end menu

@node Copying, Intro, Top, Top
@comment  node-name, next, previous,  up
@unnumbered GNU MP Copying Conditions
@cindex Copying conditions
@cindex Conditions for copying GNU MP

This library is @dfn{free}; this means that everyone is free to use it
and free to redistribute it on a free basis.  The library is not in the
public domain; it is copyrighted and there are restrictions on its
distribution, but these restrictions are designed to permit everything
that a good cooperating citizen would want to do.  What is not allowed
is to try to prevent others from further sharing any version of this
library that they might get from you.@refill

  Specifically, we want to make sure that you have the right to give
away copies of the library, that you receive source code or else can get
it if you want it, that you can change this library or use pieces of it
in new free programs, and that you know you can do these things.@refill

  To make sure that everyone has such rights, we have to forbid you to
deprive anyone else of these rights.  For example, if you distribute
copies of the GMP library, you must give the recipients all the rights
that you have.  You must make sure that they, too, receive or can get
the source code.  And you must tell them their rights.@refill

  Also, for our own protection, we must make certain that everyone finds
out that there is no warranty for the GMP library.  If it is modified by
someone else and passed on, we want their recipients to know that what
they have is not what we distributed, so that any problems introduced by
others will not reflect on our reputation.@refill

  The precise conditions of the license for the GMP library are found in
the General Public License that accompany the source code.@refill

@node Intro, Initialization, Copying, Top
@comment  node-name,  next,  previous,  up
@chapter Introduction to MP
@cindex Introduction
@cindex Overview

GNU MP is a portable library for arbitrary precision integer and
rational number arithmetic.@footnote{The limit of the precision is set by the
available memory in your computer.}  It aims to provide the fastest
possible arithmetic for all applications that need more than two words
of integer precision.

Most often, applications tend to use just a few words of precision;
but some applications may need thousands of words.  GNU MP is designed
to give good performance for both kinds of applications, by choosing
algorithms based on the sizes of the operands.

There are five groups of functions in the MP library:

@enumerate
@item
Functions for signed integer arithmetic, with names
beginning with @code{mpz_}.

@item
Functions for rational number arithmetic, with names beginning with
@code{mpq_}.

@item
Functions compatible with Berkeley MP, such as @code{itom}, @code{madd},
and @code{mult}.

@item
Fast low-level functions that operate on natural numbers.  These are
used by the functions in the preceding groups, and you can also call
them directly from very time-critical user programs.  These functions'
names begin with @code{mpn_}.

@item
Miscellaneous functions.
@end enumerate

As a general rule, all MP functions expect output arguments before input
arguments.  This notation is based on an analogy with the assignment
operator.  (The BSD MP compatibility functions disobey this rule, having
the output argument(s) last.)  Multi-precision numbers, whether
output or input, are always passed as addresses to the declared type.

@menu
* Nomenclature::
* Thanks::
@end menu

@node Nomenclature, Thanks, Intro, Intro
@comment  node-name,  next,  previous,  up
@section Nomenclature and Data Types
@cindex nomenclature

@cindex integer
@tindex @code{MP_INT}
In this manual, @dfn{integer} means a multiple precision integer, as
used in the MP package.  The C data type for such integers is
@code{MP_INT}.  For example:

@example
MP_INT sum;

struct foo @{ MP_INT x, y; @};

MP_INT vec[20];
@end example

@cindex rational number
@tindex @code{MP_RAT}
@dfn{Rational number} means a multiple precision fraction.  The C data
type for these fractions is @code{MP_RAT}.  For example:

@example
MP_RAT quotient;
@end example

@cindex limb
A @dfn{limb} means the part of a multi-precision number that fits in a
single word.  (We chose this word because a limb of the human body is
analogous to a digit, only larger, and containing several digits.)
Normally a limb contains 32 bits.

@node Thanks,, Nomenclature, Intro
@comment  node-name,  next,  previous,  up
@section Thanks

I would like to thank Gunnar Sjoedin and Hans Riesel for their help with
mathematical problems, Richard Stallman for his help with design issues
and for revising this manual, Brian Beuning and Doug Lea for their
testing of various versions of the library, and Joachim Hollman for
his many valuable suggestions.

Special thanks to Brian Beuning, he has shaked out many bugs from early
versions of the code!

John Amanatides of York University in Canada contributed the function
@code{mpz_probab_prime_p}.

@node Initialization, Integer Functions, Intro, Top
@comment  node-name,  next,  previous,  up
@chapter Initialization

Before you can use a variable or object of type @code{MP_INT} or
@code{MP_RAT}, you must initialize it.  This fills in the components
that point to dynamically allocated space for the limbs of the number.

When you are finished using the object, you should clear out the object.
This frees the dynamic space that it points to, so the space can be used
again.

Once you have initialized the object, you need not be concerned about
allocating additional space.  The functions in the MP package
automatically allocate additional space when the object does not already
have enough space.  They do not, however, reduce the space in use when a
smaller number is stored in the object.  Most of the time, this policy
is best, since it avoids frequent re-allocation.  If you want to reduce
the space in an object to the minimum needed, you can do
@code{_mpz_realloc (&@var{object}, mpz_size (&@var{object}))}.

The functions to initialize numbers are @code{mpz_init} (for @code{MP_INT}) and
@code{mpq_init} (for @code{MP_RAT}).

@code{mpz_init} allocates space for the limbs, and stores a pointer
to that space in the @code{MP_INT} object.  It also stores the value 0
in the object.

In the same manner, @code{mpq_init} allocates space for the numerator
and denominator limbs, and stores pointers to these spaces in the @code{MP_RAT}
object.

To clear out a number object, use @code{mpz_clear} and @code{mpq_clear},
respectively.

Here is an example of use:

@example
@{
  MP_INT temp;
  mpz_init (&temp);

  @dots{} @r{store and read values in @code{temp} zero or more times} @dots{}

  mpz_clear (&temp):
@}
@end example

You might be tempted to copy an integer from one object to another like
this:

@example
MP_INT x, y;

x = y;
@end example

Although valid C, @strong{this is an error.} Rather than copying the
integer value from @code{y} to @code{x} it will make the two variables
share storage.  Subsequent assignments to one variable would change the
other mysteriously.  And if you were to clear out both variables
subsequently, you would confuse @code{malloc} and cause your program to
crash.

To copy the value properly, you must use the function @code{mpz_set}.
(@pxref{Assigning Integers})

@node Integer Functions, Rational Number Functions, Initialization, Top
@comment  node-name,  next,  previous,  up
@chapter Integer Functions
@cindex Integer functions

This chapter describes the MP functions for performing integer arithmetic.

The integer functions use arguments and values of type
pointer-to-@code{MP_INT} (@pxref{Nomenclature}).  The type @code{MP_INT}
is a structure, but applications should not refer directly to its
components.  Include the header @file{gmp.h} to get the definition of
@code{MP_INT}.

@menu
* Initializing Integers::
* Assigning Integers::
* Simultaneous Integer Init & Assign::
* Converting Integers::
* Integer Arithmetic::
* Logic on Integers::
* I/O of Integers::
@end menu

@node Initializing Integers, Assigning Integers, , Integer Functions
@comment  node-name,  next,  previous,  up
@section Initializing Integer Objects

Most of the functions for integer arithmetic assume that the output is
stored in an object already initialized.  For example, @code{mpz_add}
stores the result of addition (@pxref{Integer Arithmetic}).  Thus, you
must initialize the object before storing the first value in it.  You
can do this separately by calling the function @code{mpz_init}.

@deftypefun void mpz_init (MP_INT *@var{integer})
Initialize @var{integer} with limb space and set the initial numeric
value to 0.  Each variable should normally only be initialized once,
or at least cleared out (using @code{mpz_clear}) between each initialization.
@end deftypefun

Here is an example of using @code{mpz_init}:

@example
@{
  MP_INT integ;
  mpz_init (&integ);
  @dots{}
  mpz_add (&integ, @dots{});
  @dots{}
  mpz_sub (&integ, @dots{});

  /* Unless you are now exiting the program, do ... */
  mpz_clear (&integ);
@}
@end example

@noindent
As you can see, you can store new values any number of times, once an
object is initialized.

@deftypefun void mpz_clear (MP_INT *@var{integer})
Free the limb space occupied by @var{integer}.  Make sure to call this
function for all @code{MP_INT} variables when you are done with them.
@end deftypefun

@deftypefun {void *} _mpz_realloc (MP_INT *@var{integer}, mp_size @var{new_alloc})
Change the limb space allocation to @var{new_alloc} limbs.  This
function is not normally called from user code, but it can be used to
give memory back to the heap, or to increase the space of a variable to
avoid repeated automatic re-allocation.
@end deftypefun

@deftypefun void mpz_array_init (MP_INT @var{integer_array}[], size_t @var{array_size}, mp_size @var{fixed_num_limbs})
Allocate @strong{fixed} limb space for all @var{array_size} integers in
@var{integer_array}.  The fixed allocation for each integer in the array
is @var{fixed_num_limbs}.  This function is useful for decreasing the
working set for some algorithms that use large integer arrays.  If the
fixed space will be insufficient for storing the result of a subsequent
calculation, the result is unpredictable.

There is no way to de-allocate the storage allocated by this function.  Don't
call @code{mpz_clear}!
@end deftypefun


@node Assigning Integers, Simultaneous Integer Init & Assign, Initializing Integers, Integer Functions
@comment  node-name,  next,  previous,  up
@subsection Integer Assignment Functions
@cindex Integer assignment functions

These functions assign new values to already initialized integers
(@pxref{Initializing Integers}).

@deftypefun void mpz_set (MP_INT *@var{dest_integer}, MP_INT *@var{src_integer})
Assign @var{dest_integer} from @var{src_integer}.
@end deftypefun

@deftypefun void mpz_set_ui (MP_INT *@var{integer}, unsigned long int @var{initial_value})
Set the value of @var{integer} from @var{initial_value}.
@end deftypefun

@deftypefun void mpz_set_si (MP_INT *@var{integer}, signed long int @var{initial_value})
Set the value of @var{integer} from @var{initial_value}.
@end deftypefun

@deftypefun int mpz_set_str (MP_INT *@var{integer}, char *@var{initial_value}, int @var{base})
Set the value of @var{integer} from @var{initial_value},
a '\0'-terminated C string in base @var{base}.  White space is allowed in
the string, and is simply ignored.  The base may vary from 2 to 36.  If
@var{base} is 0, the actual base is determined from the leading characters: if
the first two characters are `0x' or `0X', hexadecimal is assumed,
otherwise if the first character is `0', octal is assumed, otherwise
decimal is assumed.

This function returns 0 if the entire string up to the '\0' is a valid
number in base @var{base}.  Otherwise it returns @minus{}1.
@end deftypefun


@node Simultaneous Integer Init & Assign, Converting Integers, Assigning Integers, Integer Functions
@comment  node-name,  next,  previous,  up
@subsection Combined Initialization and Assignment Functions
@cindex Initialization and assignment functions, combined

For your convenience, MP provides a parallel series of
initialize-and-set arithmetic 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
@{
  MP_INT integ;
  mpz_init_set_str (&integ, "3141592653589793238462643383279502884", 10);
  @dots{}
  mpz_sub (&integ, @dots{});

  mpz_clear (&integ);
@}
@end example

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 (MP_INT *@var{dest_integer}, MP_INT *@var{src_integer})
Initialize @var{dest_integer} with limb space and set the initial numeric
value from @var{src_integer}.
@end deftypefun

@deftypefun void mpz_init_set_ui (MP_INT *@var{dest_integer}, unsigned long int @var{src_ulong})
Initialize @var{dest_integer} with limb space and set the initial numeric
value from @var{src_ulong}.
@end deftypefun

@deftypefun void mpz_init_set_si (MP_INT *@var{dest_integer}, signed long int @var{src_slong})
Initialize @var{dest_integer} with limb space and set the initial numeric
value from @var{src_slong}.
@end deftypefun

@deftypefun int mpz_init_set_str (MP_INT *@var{dest_integer}, char *@var{src_cstring}, int @var{base})
Initialize @var{dest_integer} with limb space and set the initial
numeric value from @var{src_cstring}, a '\0'-terminated C string in base
@var{base}.  The base may vary from 2 to 36.  There may be white space
in the string.

If the string is a correct base @var{base} number, the function returns
0; if an error occurs it returns @minus{}1.  @var{dest_integer} is
initialized even if an error occurs.  (I.e., you have to call 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 Conversion functions

@deftypefun {unsigned long int} mpz_get_ui (MP_INT *@var{src_integer})
Return the least significant limb from @var{src_integer}.  This
function together with @*
@code{mpz_div_2exp(@dots{}, @var{src_integer}, CHAR_BIT*sizeof(unsigned
long int))} can be used to extract the limbs of an integer efficiently.
@end deftypefun

@deftypefun {signed long int} mpz_get_si (MP_INT *@var{src_integer})
If @var{src_integer} fits into a @code{signed long int} return the value
of @var{src_integer}.  Otherwise return the least significant bits of
@var{src_integer}, with the same sign as @var{src_integer}.
@end deftypefun

@deftypefun {char *} mpz_get_str (char *@var{string}, int @var{base}, MP_INT *@var{integer})
Convert @var{integer} to a '\0'-terminated C string in @var{string},
using base @var{base}.  The base may vary from 2 to 36.  If @var{string}
is NULL, space for the string is allocated using the default allocation
function.

If @var{string} is not NULL, it should point to a block of storage
enough large for the result.  To find out the right amount of space to
provide for @var{string}, use @code{mpz_sizeinbase (@var{integer},
@var{base}) + 2}.  The "+ 2" is for a possible minus sign, and for the
terminating null character.  (@pxref{Miscellaneous Functions}).

This function returns a pointer to the result string.
@end deftypefun


@node     Integer Arithmetic, Logic on Integers, Converting Integers, Integer Functions
@comment  node-name,  next,  previous,  up
@section Integer Arithmetic Functions
@cindex Integer arithmetic functions
@cindex Arithmetic functions

@deftypefun void mpz_add (MP_INT *@var{sum}, MP_INT *@var{addend1}, MP_INT *@var{addend2})
@end deftypefun
@deftypefun void mpz_add_ui (MP_INT *@var{sum}, MP_INT *@var{addend1}, unsigned long int @var{addend2})
Set @var{sum} to @var{addend1} + @var{addend2}.
@end deftypefun

@deftypefun void mpz_sub (MP_INT *@var{difference}, MP_INT *@var{minuend}, MP_INT *@var{subtrahend})
@end deftypefun
@deftypefun void mpz_sub_ui (MP_INT *@var{difference}, MP_INT *@var{minuend}, unsigned long int @var{subtrahend})
Set @var{difference} to @var{minuend} @minus{} @var{subtrahend}.
@end deftypefun

@deftypefun void mpz_mul (MP_INT *@var{product}, MP_INT *@var{multiplicator}, MP_INT *@var{multiplicand})
@end deftypefun
@deftypefun void mpz_mul_ui (MP_INT *@var{product}, MP_INT *@var{multiplicator}, unsigned long int @var{multiplicand})
Set @var{product} to @var{multiplicator} times @var{multiplicand}.
@end deftypefun

Division is undefined if the divisor is zero, and passing a zero divisor
to the divide or modulo functions, as well passing a zero mod argument
to the powm functions, will make these functions intentionally divide by
zero.  This gives the user the possibility to handle arithmetic
exceptions in these functions in the same manner as other arithmetic
exceptions.

@deftypefun void mpz_div (MP_INT *@var{quotient}, MP_INT *@var{dividend}, MP_INT *@var{divisor})
@end deftypefun
@deftypefun void mpz_div_ui (MP_INT *@var{quotient}, MP_INT *@var{dividend}, unsigned long int @var{divisor})
Set @var{quotient} to @var{dividend} / @var{divisor}.  The quotient is
rounded towards 0.
@end deftypefun

@deftypefun void mpz_mod (MP_INT *@var{remainder}, MP_INT *@var{divdend}, MP_INT *@var{divisor})
@end deftypefun
@deftypefun void mpz_mod_ui (MP_INT *@var{remainder}, MP_INT *@var{divdend}, unsigned long int @var{divisor})
Divide @var{dividend} and @var{divisor} and put the remainder in
@var{remainder}.  The remainder has the same sign as the dividend, and
its absolute value is less than the absolute value of the divisor.
@end deftypefun

@deftypefun void mpz_divmod (MP_INT *@var{quotient}, MP_INT *@var{remainder}, MP_INT *@var{dividend}, MP_INT *@var{divisor})
@end deftypefun
@deftypefun void mpz_divmod_ui (MP_INT *@var{quotient}, MP_INT *@var{remainder}, MP_INT *@var{dividend}, unsigned long int @var{divisor})
Divide @var{dividend} and @var{divisor} and put the quotient in
@var{quotient} and the remainder in @var{remainder}.  The quotient is
rounded towards 0.  The remainder has the same sign as the dividend,
and its absolute value is less than the absolute value of the divisor.

If @var{quotient} and @var{remainder} are the same variable, the results
are not defined.
@end deftypefun

@deftypefun void mpz_mdiv (MP_INT *@var{quotient}, MP_INT *@var{dividend}, MP_INT *@var{divisor})
@end deftypefun
@deftypefun void mpz_mdiv_ui (MP_INT *@var{quotient}, MP_INT *@var{dividend}, unsigned long int @var{divisor})
Set @var{quotient} to @var{dividend} / @var{divisor}.  The quotient is
rounded towards @minus{}infinity.
@end deftypefun

@deftypefun void mpz_mmod (MP_INT *@var{remainder}, MP_INT *@var{divdend}, MP_INT *@var{divisor})
@end deftypefun
@deftypefun {unsigned long int} mpz_mmod_ui (MP_INT *@var{remainder}, MP_INT *@var{divdend}, unsigned long int @var{divisor})
Divide @var{dividend} and @var{divisor} and put the remainder in
@var{remainder}.  The remainder is always positive, and its value is
less than the value of the divisor.

For @code{mpz_mmod_ui} the remainder is returned, and if @var{remainder} is
not NULL, also stored there.
@end deftypefun

@deftypefun void mpz_mdivmod (MP_INT *@var{quotient}, MP_INT *@var{remainder}, MP_INT *@var{dividend}, MP_INT *@var{divisor})
@end deftypefun
@deftypefun {unsigned long int} mpz_mdivmod_ui (MP_INT *@var{quotient}, MP_INT *@var{remainder}, MP_INT *@var{dividend}, unsigned long int @var{divisor})
Divide @var{dividend} and @var{divisor} and put the quotient in
@var{quotient} and the remainder in @var{remainder}.  The quotient is
rounded towards @minus{}infinity.  The remainder is always positive, and its
value is less than the value of the divisor.

For @code{mpz_mdivmod_ui} the remainder is small enough to fit in an
@code{unsigned long int}, and is therefore returned.  If @var{remainder}
is not NULL, the remainder is also stored there.

If @var{quotient} and @var{remainder} are the same variable, the results
are not defined.
@end deftypefun

@deftypefun void mpz_sqrt (MP_INT *@var{root}, MP_INT *@var{operand})
Set @var{root} to the square root of @var{operand}.  The result is
rounded towards zero.
@end deftypefun

@deftypefun void mpz_sqrtrem (MP_INT *@var{root}, MP_INT *@var{remainder}, MP_INT *@var{operand})
Set @var{root} to the square root of @var{operand}, as with
@code{mpz_sqrt}.  Set @var{remainder} to
@ifinfo
@var{operand}@minus{}@var{root}*@var{root},
@end ifinfo
@iftex
@tex
$operand - root^2$,
@end tex
@end iftex
(i.e@. zero if @var{operand} is a perfect square).

If @var{root} and @var{remainder} are the same variable, the results are
not defined.
@end deftypefun

@deftypefun int mpz_perfect_square_p (MP_INT *@var{square})
Return non-zero if @var{square} is perfect, i.e@. if the square root of
@var{square} is integral.  Return zero otherwise.
@end deftypefun

@deftypefun int mpz_probab_prime_p (MP_INT *@var{n}, int @var{reps})
An implementation of the probabilistic primality test found in Knuth's
Seminumerical Algorithms book.  If the function
@code{mpz_probab_prime_p(@var{n}, @var{reps})} returns 0 then @var{n} is
not prime.  If it returns 1, then @var{n} is `probably' prime.  The
probability of a false positive is (1/4)**@var{reps}, where @var{reps}
is the number of internal passes of the probabilistic algorithm.  Knuth
indicates that 25 passes are reasonable.
@end deftypefun

@deftypefun void mpz_powm (MP_INT *@var{res}, MP_INT *@var{base}, MP_INT *@var{exp}, MP_INT *@var{mod})
@end deftypefun
@deftypefun void mpz_powm_ui (MP_INT *@var{res}, MP_INT *@var{base}, unsigned long int @var{exp}, MP_INT *@var{mod})
Set @var{res} to (@var{base} raised to @var{exp}) modulo @var{mod}.
If @var{exp} is negative, the result is undefined.
@end deftypefun

@deftypefun void mpz_pow_ui (MP_INT *@var{res}, MP_INT *@var{base}, unsigned long int @var{exp})
Set @var{res} to @var{base} raised to @var{exp}.
@end deftypefun

@deftypefun void mpz_fac_ui (MP_INT *@var{res}, unsigned long int @var{n})
Set @var{res} @var{n}!, the factorial of n.
@end deftypefun

@deftypefun void mpz_gcd (MP_INT *@var{res}, MP_INT *@var{operand1}, MP_INT *@var{operand2})
Set @var{res} to the greatest common divisor of @var{operand1} and
@var{operand2}.
@end deftypefun

@deftypefun void mpz_gcdext (MP_INT *@var{g}, MP_INT *@var{s}, MP_INT *@var{t}, MP_INT *@var{a}, MP_INT *@var{b})
Compute @var{g}, @var{s}, and @var{t}, such that @var{a}@var{s} +
@var{b}@var{t} = @var{g} = @code{gcd} (@var{a}, @var{b}).  If @var{t} is
NULL, that argument is not computed.
@end deftypefun

@deftypefun void mpz_neg (MP_INT *@var{negated_operand}, MP_INT *@var{operand})
Set @var{negated_operand} to @minus{}@var{operand}.
@end deftypefun

@deftypefun void mpz_abs (MP_INT *@var{positive_operand}, MP_INT *@var{signed_operand})
Set @var{positive_operand} to the absolute value of @var{signed_operand}.
@end deftypefun

@deftypefun int mpz_cmp (MP_INT *@var{operand1}, MP_INT *@var{operand2})
@end deftypefun
@deftypefun int mpz_cmp_ui (MP_INT *@var{operand1}, unsigned long int @var{operand2})
@end deftypefun
@deftypefun int mpz_cmp_si (MP_INT *@var{operand1}, signed long int @var{operand2})
Compare @var{operand1} and @var{operand2}.  Return a positive value if
@var{operand1} > @var{operand2}, zero if @var{operand1} = @var{operand2},
and a negative value if @var{operand1} < @var{operand2}.
@end deftypefun

@deftypefun void mpz_mul_2exp (MP_INT *@var{product}, MP_INT *@var{multiplicator}, unsigned long int @var{exponent_of_2})
Set @var{product} to @var{multiplicator} times 2 raised to
@var{exponent_of_2}.  This operation can also be defined as a left shift,
@var{exponent_of_2} steps.
@end deftypefun

@deftypefun void mpz_div_2exp (MP_INT *@var{quotient}, MP_INT *@var{dividend}, unsigned long int @var{exponent_of_2})
Set @var{quotient} to @var{dividend} divided by 2 raised to
@var{exponent_of_2}.  This operation can also be defined as a right
shift, @var{exponent_of_2} steps, but unlike the >> operator in
C, the result is rounded towards 0.
@end deftypefun

@deftypefun void mpz_mod_2exp (MP_INT *@var{remainder}, MP_INT *@var{dividend}, unsigned long int @var{exponent_of_2})
Set @var{remainder} to @var{dividend} mod (2 raised to
@var{exponent_of_2}).  The sign of @var{remainder} will have the same sign
as @var{dividend}.

This operation can also be defined as a masking of the
@var{exponent_of_2} least significant bits.
@end deftypefun

@node Logic on Integers, I/O of Integers, Integer Arithmetic, Integer Functions
@comment  node-name,  next,  previous,  up
@section Logical Functions
@cindex Logical functions

@deftypefun void mpz_and (MP_INT *@var{conjunction}, MP_INT *@var{operand1}, MP_INT *@var{operand2})
Set @var{conjunction} to @var{operand1} logical-and @var{operand2}.
@end deftypefun

@deftypefun void mpz_ior (MP_INT *@var{disjunction}, MP_INT *@var{operand1}, MP_INT *@var{operand2})
Set @var{disjunction} to @var{operand1} inclusive-or @var{operand2}.
@end deftypefun

@deftypefun void mpz_xor (MP_INT *@var{disjunction}, MP_INT *@var{operand1}, MP_INT *@var{operand2})
Set @var{disjunction} to @var{operand1} exclusive-or @var{operand2}.

This function is missing in the current release.
@end deftypefun

@deftypefun void mpz_com (MP_INT *@var{complemented_operand}, MP_INT *@var{operand})
Set @var{complemented_operand} to the one's complement of @var{operand}.
@end deftypefun

@node I/O of Integers,, Logic on Integers, Integer Functions
@comment  node-name,  next,  previous,  up
@section Input and Output Functions
@cindex Input and output functions
@cindex Output functions
@cindex I/O functions

Functions that perform input from a standard I/O stream, and functions for
output conversion.

@deftypefun void mpz_inp_raw (MP_INT *@var{integer}, FILE *@var{stream})
Input from standard I/O stream @var{stream} in the format written by
@code{mpz_out_raw}, and put the result in @var{integer}.
@end deftypefun

@deftypefun void mpz_inp_str (MP_INT *@var{integer}, FILE *@var{stream}, int @var{base})
Input a string in base @var{base} from standard I/O stream @var{stream},
and put the read integer in @var{integer}.  The base may vary from 2 to
36.  If @var{base} is 0, the actual base is determined from the leading
characters: if the first two characters are `0x' or `0X', hexadecimal is
assumed, otherwise if the first character is `0', octal is assumed,
otherwise decimal is assumed.
@end deftypefun


@deftypefun void mpz_out_raw (FILE *@var{stream}, MP_INT *@var{integer})
Output @var{integer} on standard I/O 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.
@end deftypefun

@deftypefun void mpz_out_str (FILE *@var{stream}, int @var{base}, MP_INT *@var{integer})
Output @var{integer} on standard I/O stream @var{stream}, as a string of
digits in base @var{base}.  The base may vary from 2 to 36.
@end deftypefun


@node Rational Number Functions, Low-level Functions, Integer Functions, Top
@comment  node-name,  next,  previous,  up
@chapter Rational Number Functions
@cindex Rational number functions

All rational arithmetic functions canonicalize the result, so that the
denominator and the numerator have no common factors.  Zero has the
unique representation 0/1.

The set of functions is quite small.  Maybe it will be extended in a
future release.

@deftypefun void mpq_init (MP_RAT *@var{dest_rational})
Initialize @var{dest_rational} with limb space and set the initial
numeric value 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_clear (MP_RAT *@var{rational_number})
Free the limb space occupied by @var{rational_number}.  Make sure to
call this function for all @code{MP_RAT} variables when you are done
with them.
@end deftypefun

@deftypefun void mpq_set (MP_RAT *@var{dest_rational}, MP_RAT *@var{src_rational})
Assign @var{dest_rational} from @var{src_rational}.
@end deftypefun

@deftypefun void mpq_set_ui (MP_RAT *@var{rational_number}, unsigned long int @var{numerator}, unsigned long int @var{denominator})
Set the value of @var{rational_number} to
@var{numerator}/@var{denominator}.  If @var{numerator} and
@var{denominator} have common factors, they are divided out before
@var{rational_number} is assigned.
@end deftypefun

@deftypefun void mpq_set_si (MP_RAT *@var{rational_number}, signed long int @var{numerator}, unsigned long int @var{denominator})
Like @code{mpq_set_ui}, but @var{numerator} is signed.
@end deftypefun

@deftypefun void mpq_add (MP_RAT *@var{sum}, MP_RAT *@var{addend1}, MP_RAT *@var{addend2})
Set @var{sum} to @var{addend1} + @var{addend2}.
@end deftypefun

@deftypefun void mpq_sub (MP_RAT *@var{difference}, MP_RAT *@var{minuend}, MP_RAT *@var{subtrahend})
Set @var{difference} to @var{minuend} @minus{} @var{subtrahend}.
@end deftypefun

@deftypefun void mpq_mul (MP_RAT *@var{product}, MP_RAT *@var{multiplicator}, MP_RAT *@var{multiplicand})
Set @var{product} to @var{multiplicator} * @var{multiplicand}
@end deftypefun

@deftypefun void mpq_div (MP_RAT *@var{quotient}, MP_RAT *@var{dividend}, MP_RAT *@var{divisor})
Set @var{quotient} to @var{dividend} / @var{divisor}.
@end deftypefun

@deftypefun void mpq_neg (MP_RAT *@var{negated_operand}, MP_RAT *@var{operand})
Set @var{negated_operand} to @minus{}@var{operand}.
@end deftypefun

@deftypefun int mpq_cmp (MP_RAT *@var{operand1}, MP_RAT *@var{operand2})
Compare @var{operand1} and @var{operand2}.  Return a positive value if
@var{operand1} > @var{operand2}, zero if @var{operand1} = @var{operand2},
and a negative value if @var{operand1} < @var{operand2}.
@end deftypefun

@deftypefun void mpq_inv (MP_RAT *@var{inverted_number}, MP_RAT *@var{number})
Invert @var{number} by swapping the numerator and denominator.  If the
new denominator becomes zero, this routine will divide by zero.
@end deftypefun

@deftypefun void mpq_set_num (MP_RAT *@var{rational_number}, MP_INT *@var{numerator})
Make @var{numerator} become the numerator of @var{rational_number} by
copying.
@end deftypefun

@deftypefun void mpq_set_den (MP_RAT *@var{rational_number}, MP_INT *@var{denominator})
Make @var{denominator} become the denominator of @var{rational_number}
by copying.  If @var{denominator} < 0 the denominator of
@var{rational_number} is set to the absolute value of @var{denominator},
and the sign of the numerator of @var{rational_number} is changed.
@end deftypefun

@deftypefun void mpq_get_num (MP_INT *@var{numerator}, MP_RAT *@var{rational_number})
Copy the numerator of @var{rational_number} to the integer
@var{numerator}, to prepare for integer operations on the numerator.
@end deftypefun

@deftypefun void mpq_get_den (MP_INT *@var{denominator}, MP_RAT *@var{rational_number})
Copy the denominator of @var{rational_number} to the integer
@var{denominator}, to prepare for integer operations on the denominator.
@end deftypefun


@node Low-level Functions, BSD Compatible Functions, Rational Number Functions, Top
@comment  node-name,  next,  previous,  up
@chapter Low-level Functions
@cindex Low-level functions

@c 1. Some of these function clobber input operands.
@c 

@strong{The next release of the GNU MP library (2.0) will include
changes to some mpn functions.  Programs that use these functions
according to the descriptions below will therefore not work with the
next release.}

The low-level function layer is designed to be as fast as possible,
@strong{not} to provide a coherent calling interface.  The different
functions have similar interfaces, but there are variations that might
be confusing.  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 responsability of the caller to ensure that the destination
has enough space for storing the result.

With this way of specifying source operands, it is possible to perform
computations on subranges of an argument, and store the result into a
subrange of a destination.

All these functions require that the operands are normalized in the
sense that the most significant limb must be non-zero.  (A future release
of might drop this requirement.)

The low-level layer is the base for the implementation of the
@code{mpz_} and @code{mpq_} layers.

The code below adds the number beginning at @var{src1_ptr} and the
number beginning at @var{src2_ptr} and writes the sum at @var{dest_ptr}.
A constraint for @code{mpn_add} is that @var{src1_size} must not be
smaller that @var{src2_size}.

@example
mpn_add (dest_ptr, src1_ptr, src1_size, src2_ptr, src2_size)
@end example

In the description below, a source operand is identified by the pointer
to the least significant limb, and the limb count in braces.

@deftypefun mp_size mpn_add (mp_ptr @var{dest_ptr}, mp_srcptr @var{src1_ptr}, mp_size @var{src1_size}, mp_srcptr @var{src2_ptr}, mp_size @var{src2_size})
Add @{@var{src1_ptr}, @var{src1_size}@} and @{@var{src2_ptr},
@var{src2_size}@}, and write the @var{src1_size} least significant limbs
of the result to @var{dest_ptr}.  Carry-out, either 0 or 1, is returned.

This function requires that @var{src1_size} is greater than or equal to
@var{src2_size}.
@end deftypefun

@deftypefun mp_size mpn_sub (mp_ptr @var{dest_ptr}, mp_srcptr @var{src1_ptr}, mp_size @var{src1_size}, mp_srcptr @var{src2_ptr}, mp_size @var{src2_size})
Subtarct @{@var{src2_ptr}, @var{src2_size}@} from @{@var{src1_ptr},
@var{src1_size}@}, and write the result to @var{dest_ptr}.

Return 1 if the minuend < the subtrahend.  Otherwise, return the
negative difference between the number of words in the result and the
minuend.  I.e@. return 0 if the result has @var{src1_size} words, @minus{}1 if
it has @var{src1_size} @minus{} 1 words, etc.

This function requires that @var{src1_size} is greater than or equal to
@var{src2_size}.
@end deftypefun

@deftypefun mp_size mpn_mul (mp_ptr @var{dest_ptr}, mp_srcptr @var{src1_ptr}, mp_size @var{src1_size}, mp_srcptr @var{src2_ptr}, mp_size @var{src2_size})
Multiply @{@var{src1_ptr}, @var{src1_size}@} and @{@var{src2_ptr},
@var{src2_size}@}, and write the result to @var{dest_ptr}.  The exact
size of the result is returned.

The destination has to have space for @var{src1_size} + @var{src1_size}
limbs, even if the result might be one limb smaller.

This function requires that @var{src1_size} is greater than or equal to
@var{src2_size}.  The destination must be distinct from either input
operands.
@end deftypefun

@deftypefun mp_size mpn_div (mp_ptr @var{dest_ptr}, mp_ptr @var{src1_ptr}, mp_size @var{src1_size}, mp_srcptr @var{src2_ptr}, mp_size @var{src2_size})
Divide @{@var{src1_ptr}, @var{src1_size}@} by @{@var{src2_ptr},
@var{src2_size}@}, and write the quotient to @var{dest_ptr}, and the
remainder to @var{src1_ptr}.

Return 0 if the quotient size is at most (@var{src1_size} @minus{}
@var{src2_size}), and 1 if the quotient size is at most (@var{src1_size}
@minus{} @var{src2_size} + 1).  The caller has to check the most significant limb
to find out the exact size.

The most significant bit of the most significant limb of the divisor
has to be set.

This function requires that @var{src1_size} is greater than or equal to
@var{src2_size}.  The quotient, pointed to by @var{dest_ptr}, must be
distinct from either input operands.
@end deftypefun

@deftypefun mp_limb mpn_lshift (mp_ptr @var{dest_ptr}, mp_srcptr @var{src_ptr}, mp_size @var{src_size}, unsigned long int @var{count})
Shift @{@var{src_ptr}, @var{src_size}@} @var{count} bits to the left, and
write the @var{src_size} least significant limbs of the result to
@var{dest_ptr}.  @var{count} might be in the range 1 to n @minus{} 1, on an n-bit
machine. The limb shifted out is returned.

Overlapping of the destination space and the source space is allowed in this
function, provdied @var{dest_ptr} >= @var{src_ptr}.
@end deftypefun

@deftypefun mp_size mpn_rshift (mp_ptr @var{dest_ptr}, mp_srcptr @var{src_ptr}, mp_size @var{src_size}, unsigned long int @var{count})
Shift @{@var{src_ptr}, @var{src_size}@} @var{count} bits to the right, and
write the @var{src_size} least significant limbs of the result to
@var{dest_ptr}.  @var{count} might be in the range 1 to n @minus{} 1, on an n-bit
machine. The size of the result is returned.

Overlaping of the destination space and the source space is allowed in this
function, provdied @var{dest_ptr} <= @var{src_ptr}.
@end deftypefun

@deftypefun mp_size mpn_rshiftci (mp_ptr @var{dest_ptr}, mp_srcptr @var{src_ptr}, mp_size @var{src_size}, unsigned long int @var{count}, mp_limb @var{inlimb})
Like mpn_rshift, but use @var{inlimb} to feed the least significant end
of the destination.
@end deftypefun

@deftypefun int mpn_cmp (mp_srcptr @var{src1_ptr}, mp_srcptr @var{src2_ptr}, mp_size @var{size})
Compare @{@var{src1_ptr}, @var{size}@} and @{@var{src2_ptr}, @var{size}@}
and return a positive value if src1 > src2, 0 of they are equal,
and a negative value if src1 < src2.
@end deftypefun


@node BSD Compatible Functions, Miscellaneous Functions, Low-level Functions, Top
@comment  node-name,  next,  previous,  up
@chapter Berkeley MP Compatible Functions
@cindex BSD MP compatible functions

These functions are intended to be fully compatible with the Berkeley MP
library which is available on many BSD derived U*ix systems.

The original Berkeley MP library has a usage restriction: you cannot use
the same variable as both source and destination in a single function
call.  The compatible functions in GNU MP do not share this
restriction---inputs and outputs may overlap.

It is not recommended that new programs are written using these
functions.  Apart from the incomplete set of functions, the interface
for initializing @code{MINT} objects is more error prone, and the
@code{pow} function collides with @code{pow} in @file{libm.a}.

Include the header @file{mp.h} to get the definition of the necessary
types and functions.  If you are on a BSD derived system, make sure to
include GNU @file{mp.h} if you are going to link the GNU @file{libmp.a}
to you program.  This means that you probably need to give the -I<dir>
option to the compiler, where <dir> is the directory where you have GNU
@file{mp.h}.

@deftypefun {MINT *} itom (signed short int @var{initial_value})
Allocate an integer consisting of a @code{MINT} object and dynamic limb
space.  Initialize the integer to @var{initial_value}.  Return a pointer
to the @code{MINT} object.
@end deftypefun

@deftypefun {MINT *} xtom (char *@var{initial_value})
Allocate an integer consisting of a @code{MINT} object and dynamic limb
space.  Initialize the integer from @var{initial_value}, a hexadecimal,
'\0'-terminate C string.  Return a pointer to the @code{MINT} object.
@end deftypefun

@deftypefun void move (MINT *@var{src}, MINT *@var{dest})
Set @var{dest} to @var{src} by copying.  Both variables must be
previously initialized.
@end deftypefun

@deftypefun void madd (MINT *@var{src_1}, MINT *@var{src_2}, MINT *@var{destination})
Add @var{src_1} and @var{src_2} and put the sum in @var{destination}.
@end deftypefun

@deftypefun void msub (MINT *@var{src_1}, MINT *@var{src_2}, MINT *@var{destination})
Subtract @var{src_2} from @var{src_1} and put the difference in
@var{destination}.
@end deftypefun

@deftypefun void mult (MINT *@var{src_1}, MINT *@var{src_2}, MINT *@var{destination})
Multiply @var{src_1} and @var{src_2} and put the product in
@var{destination}.
@end deftypefun

@deftypefun void mdiv (MINT *@var{dividend}, MINT *@var{divisor}, MINT *@var{quotient}, MINT *@var{remainder})
@end deftypefun
@deftypefun void sdiv (MINT *@var{dividend}, signed short int @var{divisor}, MINT *@var{quotient}, signed short int *@var{remainder})
Set @var{quotient} to @var{dividend} / @var{divisor}, and
@var{remainder} to @var{dividend} mod @var{divisor}.  The quotient is
rounded towards zero; the remainder has the same sign as the dividend.

Some implementations of this function return a remainder whose sign is
inverted if the divisor is negative.  Such a definition makes little
sense from a mathematical point of view.  GNU MP might be considered
incompatible with the traditional MP in this respect.
@end deftypefun

@deftypefun void msqrt (MINT *@var{operand}, MINT *@var{root}, MINT *@var{remainder})
Set @var{root} to the square root of @var{operand}, as with
@code{mpz_sqrt}.  Set @var{remainder} to
@ifinfo
@var{operand}-@var{root}*@var{root},
@end ifinfo
@iftex
@tex
$operand - root^2$,
@end tex
@end iftex
(i.e@. zero if @var{operand} is a perfect square).
@end deftypefun

@deftypefun void pow (MINT *@var{base}, MINT *@var{exp}, MINT *@var{mod}, MINT *@var{dest})
Set @var{dest} to (@var{base} raised to @var{exp}) modulo @var{mod}.
@end deftypefun

@deftypefun void rpow (MINT *@var{base}, signed short int @var{exp}, MINT *@var{dest})
Set @var{dest} to @var{base} raised to @var{exp}.
@end deftypefun

@deftypefun void gcd (MINT *@var{operand1}, MINT *@var{operand2}, MINT *@var{res})
Set @var{res} to the greatest common divisor of @var{operand1} and
@var{operand2}.
@end deftypefun

@deftypefun int mcmp (MINT *@var{operand1}, MINT *@var{operand2})
Compare @var{operand1} and @var{operand2}.  Return a positive value if
@var{operand1} > @var{operand2}, zero if @var{operand1} =
@var{operand2}, and a negative value if @var{operand1} < @var{operand2}.
@end deftypefun

@deftypefun void min (MINT *@var{dest})
Input a decimal string from stdin, and put the read integer in
@var{dest}.  SPC and TAB are allowed in the number string, and are
ignored.
@end deftypefun

@deftypefun void mout (MINT *@var{src})
Output @var{src} to stdout, as a decimal string.  Also output a newline.
@end deftypefun

@deftypefun {char *} mtox (MINT *@var{operand})
Convert @var{operand} to a hexadecimal string, and return a pointer to
the string.  The returned string is allocated using the default memory
allocation function, @code{malloc} by default. (@xref{Initialization},
for an explanation of the memory allocation in MP).
@end deftypefun

@deftypefun void mfree (MINT *@var{operand})
De-allocate, the space used by @var{operand}.  @strong{This function
should only be passed a value returned by @code{itom} or @code{xtom}.}
@end deftypefun

@node Miscellaneous Functions, Custom Allocation, BSD Compatible Functions, Top
@comment  node-name,  next,  previous,  up
@chapter Miscellaneous Functions
@cindex Miscellaneous functions

@deftypefun void mpz_random (MP_INT *@var{random_integer}, mp_size @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.
@end deftypefun

@deftypefun void mpz_random2 (MP_INT *@var{random_integer}, mp_size @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 bugs.
@end deftypefun

@deftypefun size_t mpz_size (MP_INT *@var{integer})
Return the size of @var{integer} measured in number of limbs.  If
@var{integer} is zero, the returned value will be zero, if @var{integer}
has one limb, the returned value will be one, etc.
(@xref{Nomenclature}, for an explanation of the concept @dfn{limb}.)
@end deftypefun

@deftypefun size_t mpz_sizeinbase (MP_INT *@var{integer}, int @var{base})
Return the size of @var{integer} measured in number of digits in base
@var{base}.  The base may vary from 2 to 36.  The returned value will be
exact or 1 too big.  If @var{base} is a power of 2, the returned value
will always be exact.

This function is useful in order to allocate the right amount of space
before converting @var{integer} 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
terminating '\0').
@end deftypefun

@node Custom Allocation, Reporting Bugs, Miscellaneous Functions, Top
@comment  node-name,  next,  previous,  up
@section Custom Allocation

By default, the initialization functions use @code{malloc},
@code{realloc}, and @code{free} to do their work.  If @code{malloc} or
@code{realloc} fails, the MP package terminates execution after a
printing fatal error message on standard error.

In some applications, you may wish to allocate memory in other ways, or
you may not want to have a fatal error when there is no more memory
available.  To accomplish this, you can specify alternative functions
for allocating and de-allocating memory.  Use
@code{mp_set_memory_functions} to do this.

@findex mp_set_memory_functions
@code{mp_set_memory_functions} has three arguments,
@var{allocate_function}, @var{reallocate_function}, and
@var{deallocate_function}, in that order.  If an argument is NULL,
the corresponding default function is retained.

The functions you supply should fit the following declarations:

@table @code
@item void * @var{allocate_function} (size_t @var{alloc_size})
This function should return a pointer to newly allocated space with at
least @var{alloc_size} storage units.

@item void * @var{reallocate_function} (void *@var{ptr}, size_t @var{old_size}, size_t @var{new_size})
This function should return a pointer to newly allocated space of at
least @var{new_size} storage units, after copying the first
@var{old_size} storage units from @var{ptr}.  It should also de-allocate the
space at @var{ptr}.

You can assume that the space at @var{ptr} was formely returned from
@var{allocate_function} or @var{reallocate_function}, for a
request for @var{old_size} storage units.

@item void @var{deallocate_function} (void *@var{ptr}, size_t @var{size})
De-allocate the space pointed to by @var{ptr}.

You can assume that the space at @var{ptr} was formely returned from
@var{allocate_function} or @var{reallocate_function}, for a
request for @var{size} storage units.
@end table

(A @dfn{storage unit} is the unit in which the @code{sizeof} operator
returns the size of an object, normally an 8 bit byte.)

@strong{NOTE: call @code{mp_set_memory_functions} only before calling
any other MP functions.} Otherwise, the user-defined allocation
functions may be asked to re-allocate or de-allocate something
previously allocated by the default allocation functions.

@node Reporting Bugs, , Custom Allocation, Top
@comment  node-name,  next,  previous,  up
@chapter Reporting Bugs
@cindex Reporting bugs

If you think you have found a bug in the GNU MP library, please
investigate it and report it.  We have made this library available to
you, and it is not to ask too much from you, to ask you to report the
bugs that you find.

Please make sure that the bug is really in the GNU MP library.

You have to send us a test case that makes it possible for us to
reproduce the bug.

You also have to explain what is wrong; if you get a crash, or if the
results printed are not good and in that case, in what way.

Make sure that the bug report includes all information you would
need to fix this kind of bug for someone else.  Think twice.

If your bug report is good, we will do our best to help you to get a
corrected version of the library; if the bug report is poor, we won't do
anything about it (aside of chiding you to send better bug reports).

Send your bug report to: tege@@gnu.ai.mit.edu.

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 References, , , Top
@comment  node-name,  next,  previous,  up
@unnumbered References

@itemize @bullet

@item
Donald E@. Knuth, "The Art of Computer Programming", vol 2,
"Seminumerical Algorithms", 2nd edition, Addison-Wesley, 1981.

@item
John D@. Lipson, "Elements of Algebra and Algebraic Computing",
The Benjamin Cummins Publishing Company Inc, 1981.

@item
Richard M@. Stallman, "Using and Porting GCC", Free Software Foundation,
1993.

@item
Peter L@. Montgomery, "Modular Multiplication Without Trial Division",
Mathematics of Computation, volume 44, number 170, April 1985.

@end itemize

@node Concept Index, , , Top
@comment  node-name,  next,  previous,  up
@unnumbered Concept Index
@printindex cp

@node Function Index, , , Top
@comment  node-name,  next,  previous,  up
@unnumbered Function and Type Index
@printindex fn


@contents
@bye
OpenPOWER on IntegriCloud