gmp.pas 142 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822
  1. {
  2. This file is part of the Free Pascal packages
  3. Copyright (c) 2009 by the Free Pascal development team
  4. An header for the GMP library
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright. (LGPL)
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. }
  11. unit gmp;
  12. {$mode objfpc}{$h+}
  13. {$packrecords c}
  14. //todo:windows link error on GMP global vars, reason not yet known
  15. {$ifdef windows}
  16. {$define NO_GMP_GLOBVARS}
  17. {$endif}
  18. {$ifdef darwin}
  19. {$linklib gmp.3}
  20. {$endif}
  21. { Unused symbols exported from GMP:
  22. Marked preliminary in GMP manual
  23. __gmpn_bdivmod
  24. Marked obsolete in GMP manual
  25. __gmpn_divrem
  26. __gmpz_random
  27. __gmpz_random2
  28. __gmp_randinit
  29. Not documented in GMP manual
  30. __gmpf_size
  31. __gmpn_divrem_2
  32. __gmpn_pow_1
  33. __gmpn_preinv_mod_1
  34. __gmpz_millerrabin
  35. Marked for use only within GDB
  36. __gmpf_dump
  37. __gmpz_dump
  38. }
  39. interface
  40. uses
  41. sysutils;
  42. const
  43. BASE10 = 10;
  44. LIB = 'gmp';
  45. LOG_10_2 = 0.3010299956639812;
  46. ERROR_NONE = 0;
  47. ERROR_UNSUPPORTED_ARGUMENT = 1;
  48. ERROR_DIVISION_BY_ZERO = 2;
  49. ERROR_SQRT_OF_NEGATIVE = 4;
  50. ERROR_INVALID_ARGUMENT = 8;
  51. RAND_ALG_DEFAULT = 0;
  52. RAND_ALG_LC = RAND_ALG_DEFAULT;
  53. type
  54. // ---- GMP types ----
  55. { low level multi precision integer atom = machine size uint }
  56. mp_limb_t = valuint;
  57. { ^array of mp_limb_t}
  58. mpn_ptr = ^mp_limb_t;
  59. mp_size_t = sizeint;
  60. mp_exp_t = valsint;
  61. randalg_t = longint;
  62. { multi precision integer number record }
  63. mpz_t = record
  64. alloc: longint;
  65. size: longint;
  66. data: mpn_ptr;
  67. end;
  68. mpz_ptr = ^mpz_t;
  69. { multi precision rational number record }
  70. mpq_t = record
  71. num: mpz_t;
  72. den: mpz_t;
  73. end;
  74. mpq_ptr = ^mpq_t;
  75. { multi precision real number record }
  76. mpf_t = record
  77. prec: longint;
  78. size: longint;
  79. exp: mp_exp_t;
  80. data: mpn_ptr;
  81. end;
  82. mpf_ptr = ^mpf_t;
  83. randstate_t = record
  84. seed: mpz_t;
  85. alg: randalg_t;
  86. algdata: record
  87. case longint of
  88. 0 : (lc : pointer);
  89. end;
  90. end;
  91. randstate_ptr = ^randstate_t;
  92. { Return a pointer to newly allocated space with at least alloc size bytes }
  93. alloc_func_t = function(alloc_size: sizeuint): pointer; cdecl;
  94. { Resize a previously allocated block ptr of old size bytes to be new size bytes }
  95. reallocate_func_t = function(p: pointer; old_size, new_size: sizeuint): pointer; cdecl;
  96. { De-allocate the space pointed to by ptr }
  97. free_proc_t = procedure(p: pointer; size: sizeuint); cdecl;
  98. palloc_func = ^alloc_func_t;
  99. preallocate_func = ^reallocate_func_t;
  100. pfree_proc = ^free_proc_t;
  101. // ---- ext types with automatic mem mngmt & cow, ~ fpc string type style -----
  102. IMPBase = interface
  103. function refs: longint;
  104. end;
  105. MPInteger = interface(IMPBase)
  106. function ptr: mpz_ptr;
  107. end;
  108. MPFloat = interface(IMPBase)
  109. function ptr: mpf_ptr;
  110. end;
  111. MPRational = interface(IMPBase)
  112. function ptr: mpq_ptr;
  113. end;
  114. MPRandState = interface(IMPBase)
  115. function ptr: randstate_ptr;
  116. end;
  117. { TMPBase }
  118. TMPBase = class(TInterfacedObject, IMPBase)
  119. private
  120. function refs: longint; inline;
  121. end;
  122. { TMPInteger }
  123. TMPInteger = class(TMPBase, MPInteger)
  124. private
  125. fmpz: mpz_t;
  126. function ptr: mpz_ptr; inline;
  127. public
  128. destructor destroy; override;
  129. end;
  130. { TMPFloat }
  131. TMPFloat = class(TMPBase, MPFloat)
  132. private
  133. fmpf: mpf_t;
  134. function ptr: mpf_ptr; inline;
  135. public
  136. destructor destroy; override;
  137. end;
  138. { TMPRational }
  139. TMPRational = class(TMPBase, MPRational)
  140. private
  141. fmpq: mpq_t;
  142. function ptr: mpq_ptr; inline;
  143. public
  144. destructor destroy; override;
  145. end;
  146. { TMPRandState }
  147. TMPRandState = class(TMPBase, MPRandState)
  148. private
  149. frandstate: randstate_t;
  150. function ptr: randstate_ptr; inline;
  151. public
  152. destructor destroy; override;
  153. end;
  154. // ==== GMP bindings ====
  155. // ---- Custom Allocation ----
  156. { Replace the current allocation functions from the arguments }
  157. procedure mp_set_memory_functions(alloc_func_ptr: alloc_func_t; realloc_func_ptr: reallocate_func_t; free_func_ptr: free_proc_t); cdecl; external LIB name '__gmp_set_memory_functions';
  158. { Get the current allocation functions, storing function pointers to the locations given by the arguments }
  159. procedure mp_get_memory_functions(alloc_func_ptr: palloc_func; realloc_func_ptr: preallocate_func; free_func_ptr: pfree_proc); cdecl; external LIB name '__gmp_get_memory_functions';
  160. // ---- Random Number Functions ----
  161. { Obsolete: Initialize state with an algorithm selected by alg }
  162. // procedure randinit(var state: randstate_t; alg: randalg_t; args: array of const); cdecl; external LIB name '__gmp_randinit';
  163. { Initialize state with a default algorithm }
  164. procedure mp_randinit_default(out state: randstate_t); cdecl; external LIB name '__gmp_randinit_default';
  165. { Initialize state with a linear congruential algorithm X = (aX + c) mod 2^m2exp }
  166. procedure mp_randinit_lc_2exp(out state: randstate_t; var a: mpz_t; c, m2exp: valuint); cdecl; external LIB name '__gmp_randinit_lc_2exp';
  167. { Initialize state for a linear congruential algorithm as per gmp_randinit_lc_2exp }
  168. function mp_randinit_lc_2exp_size(out state: randstate_t; size: sizeuint): longint; cdecl; external LIB name '__gmp_randinit_lc_2exp_size';
  169. { Initialize state for a Mersenne Twister algorithm }
  170. procedure mp_randinit_mt(out state: randstate_t); cdecl; external LIB name '__gmp_randinit_mt';
  171. { Initialize rop with a copy of the algorithm and state from op }
  172. procedure mp_randinit_set(out rop: randstate_t; var op: randstate_t); cdecl; external LIB name '__gmp_randinit_set';
  173. { Set an initial seed value into state }
  174. procedure mp_randseed(var state: randstate_t; var seed: mpz_t); cdecl; external LIB name '__gmp_randseed';
  175. { Set an initial seed value into state }
  176. procedure mp_randseed_ui(var state: randstate_t; seed: valuint); cdecl; external LIB name '__gmp_randseed_ui';
  177. { Free all memory occupied by state }
  178. procedure mp_randclear(var state: randstate_t); cdecl; external LIB name '__gmp_randclear';
  179. { Return an uniformly distributed random number of n bits, ie. in the range 0 to 2^n−1 inclusive }
  180. function mp_urandomb_ui(var state: randstate_t; n: valuint): valuint; cdecl; external LIB name '__gmp_urandomb_ui';
  181. { Return an uniformly distributed random number in the range 0 to n − 1, inclusive }
  182. function mp_urandomm_ui(var state: randstate_t; n: valuint): valuint; cdecl; external LIB name '__gmp_urandomm_ui';
  183. // ---- Formatted Input/Output ----
  184. { Form a null-terminated string in a block of memory obtained from the current memory allocation function }
  185. function mp_asprintf(out pp: pchar; fmt: pchar; args: array of const): longint; cdecl; external LIB name '__gmp_asprintf';
  186. { Form a null-terminated string in a block of memory obtained from the current memory allocation function }
  187. function mp_asprintf(out pp: pchar; fmt: pchar): longint; cdecl; varargs; external LIB name '__gmp_asprintf';
  188. { Print to the standard output stdout. Return the number of characters written, or −1 if an error occurred. }
  189. function mp_printf(fmt: pchar; args: array of const): longint; cdecl; external LIB name '__gmp_printf';
  190. { Print to the standard output stdout. Return the number of characters written, or −1 if an error occurred. }
  191. function mp_printf(fmt: pchar): longint; cdecl; varargs; external LIB name '__gmp_printf';
  192. { Form a null-terminated string in buf. No more than size bytes will be written. }
  193. function mp_snprintf(buf: pchar; size: sizeuint; fmt: pchar; args: array of const): longint; cdecl; external LIB name '__gmp_snprintf';
  194. { Form a null-terminated string in buf. No more than size bytes will be written. }
  195. function mp_snprintf(buf: pchar; size: sizeuint; fmt: pchar): longint; cdecl; varargs; external LIB name '__gmp_snprintf';
  196. { Form a null-terminated string in buf. Return the number of characters written, excluding the terminating null. }
  197. function mp_sprintf(buf, fmt: pchar; args: array of const): longint; cdecl; external LIB name '__gmp_sprintf';
  198. { Form a null-terminated string in buf. Return the number of characters written, excluding the terminating null. }
  199. function mp_sprintf(buf, fmt: pchar): longint; cdecl; varargs; external LIB name '__gmp_sprintf';
  200. { Read from the standard input stdin }
  201. function mp_scanf(fmt: pchar; args: array of const): longint; cdecl; external LIB name '__gmp_scanf';
  202. { Read from the standard input stdin }
  203. function mp_scanf(fmt: pchar): longint; cdecl; varargs; external LIB name '__gmp_scanf';
  204. { Read from a null-terminated string s }
  205. function mp_sscanf(s, fmt: pchar; args: array of const): longint; cdecl; external LIB name '__gmp_sscanf';
  206. { Read from a null-terminated string s }
  207. function mp_sscanf(s, fmt: pchar): longint; cdecl; varargs; external LIB name '__gmp_sscanf';
  208. // ---- integer Functions ----
  209. { Change the space for integer to new_alloc limbs }
  210. function mpz_realloc(var integer_: mpz_t; new_alloc: mp_size_t): pointer; cdecl; external LIB name '__gmpz_realloc';
  211. { Set rop to the absolute value of op }
  212. procedure mpz_abs(var rop, op: mpz_t); cdecl; external LIB name '__gmpz_abs';
  213. { Set rop to op1 + op2 }
  214. procedure mpz_add(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_add';
  215. { Set rop to op1 + op2 }
  216. procedure mpz_add_ui(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_add_ui';
  217. { Set rop to rop + op1 × op2 }
  218. procedure mpz_addmul(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_addmul';
  219. { Set rop to rop + op1 × op2 }
  220. procedure mpz_addmul_ui(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_addmul_ui';
  221. { Set rop to op1 bitwise-and op2 }
  222. procedure mpz_and(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_and';
  223. { _Fixed_ space of fixed_num_bits is allocated to each of the array size integers in integer array }
  224. procedure mpz_array_init(var integer_array: mpz_t; array_size, fixed_num_bits: mp_size_t); cdecl; external LIB name '__gmpz_array_init';
  225. { Compute the binomial coefficient (n over k) and store the result in rop }
  226. procedure mpz_bin_ui(var rop, n: mpz_t; k: valuint); cdecl; external LIB name '__gmpz_bin_ui';
  227. { Compute the binomial coefficient (n over k) and store the result in rop }
  228. procedure mpz_bin_uiui(var rop: mpz_t; n, k: valuint); cdecl; external LIB name '__gmpz_bin_uiui';
  229. { Divide n by d, forming a quotient q. Round mode ceil. }
  230. procedure mpz_cdiv_q(var q, n, d: mpz_t); cdecl; external LIB name '__gmpz_cdiv_q';
  231. { Divide n by d, forming a quotient q. d = 2^b. Round mode ceil. }
  232. procedure mpz_cdiv_q_2exp(var q, n: mpz_t; b: valuint); cdecl; external LIB name '__gmpz_cdiv_q_2exp';
  233. { Divide n by d, forming a quotient q. Round mode ceil. }
  234. function mpz_cdiv_q_ui(var q, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_cdiv_q_ui';
  235. { Divide n by d, forming a quotient q and remainder r. Round mode ceil. }
  236. procedure mpz_cdiv_qr(var q, r, n, d: mpz_t); cdecl; external LIB name '__gmpz_cdiv_qr';
  237. { Divide n by d, forming a quotient q and remainder r. Round mode ceil. }
  238. function mpz_cdiv_qr_ui(var q, r, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_cdiv_qr_ui';
  239. { Divide n by d, forming a remainder r. Round mode ceil. }
  240. procedure mpz_cdiv_r(var r, n, d: mpz_t); cdecl; external LIB name '__gmpz_cdiv_r';
  241. { Divide n by d, forming a remainder r. d = 2^b. Round mode ceil. }
  242. procedure mpz_cdiv_r_2exp(var r, n: mpz_t; b: valuint); cdecl; external LIB name '__gmpz_cdiv_r_2exp';
  243. { Divide n by d, forming a remainder r. Round mode ceil. }
  244. function mpz_cdiv_r_ui(var r, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_cdiv_r_ui';
  245. { Divide n by d. Round mode ceil. }
  246. function mpz_cdiv_ui(var n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_cdiv_ui';
  247. { Free the space occupied by integer. Call this function for all mpz_t variables when you are done with them. }
  248. procedure mpz_clear(var integer_: mpz_t); cdecl; external LIB name '__gmpz_clear';
  249. { Clear bit bit_index in rop }
  250. procedure mpz_clrbit(var rop: mpz_t; bit_index: valuint); cdecl; external LIB name '__gmpz_clrbit';
  251. { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
  252. function mpz_cmp(var op1, op2: mpz_t): longint; cdecl; external LIB name '__gmpz_cmp';
  253. { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
  254. function mpz_cmp_d(var op1: mpz_t; op2: double): longint; cdecl; external LIB name '__gmpz_cmp_d';
  255. { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
  256. function mpz_cmp_si(var op1: mpz_t; op2: valsint): longint; cdecl; external LIB name '__gmpz_cmp_si';
  257. { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
  258. function mpz_cmp_ui(var op1: mpz_t; op2: valuint): longint; cdecl; external LIB name '__gmpz_cmp_ui';
  259. { Compare the absolute values of op1 and op2. Return a positive value if |op1| > |op2|, zero if |op1| = |op2|, or a negative value if |op1| < |op2|. }
  260. function mpz_cmpabs(var op1, op2: mpz_t): longint; cdecl; external LIB name '__gmpz_cmpabs';
  261. { Compare the absolute values of op1 and op2. Return a positive value if |op1| > |op2|, zero if |op1| = |op2|, or a negative value if |op1| < |op2|. }
  262. function mpz_cmpabs_d(var op1: mpz_t; op2: double): longint; cdecl; external LIB name '__gmpz_cmpabs_d';
  263. { Compare the absolute values of op1 and op2. Return a positive value if |op1| > |op2|, zero if |op1| = |op2|, or a negative value if |op1| < |op2|. }
  264. function mpz_cmpabs_ui(var op1: mpz_t; op2: valuint): longint; cdecl; external LIB name '__gmpz_cmpabs_ui';
  265. { Set rop to the one’s complement of op }
  266. procedure mpz_com(var rop, op: mpz_t); cdecl; external LIB name '__gmpz_com';
  267. { Complement bit bit_index in rop }
  268. procedure mpz_combit(var rop: mpz_t; bit_index: valuint); cdecl; external LIB name '__gmpz_combit';
  269. { Return non-zero if n is congruent to c modulo d }
  270. function mpz_congruent_p(var n, c, d: mpz_t): longint; cdecl; external LIB name '__gmpz_congruent_p';
  271. { Return non-zero if n is congruent to c modulo 2^b }
  272. function mpz_congruent_2exp_p(var n, c: mpz_t; b: valuint): longint; cdecl; external LIB name '__gmpz_congruent_2exp_p';
  273. { Return non-zero if n is congruent to c modulo d }
  274. function mpz_congruent_ui_p(var n: mpz_t; c, d: valuint): longint; cdecl; external LIB name '__gmpz_congruent_ui_p';
  275. { Set q to n/d }
  276. procedure mpz_divexact(var q, n, d: mpz_t); cdecl; external LIB name '__gmpz_divexact';
  277. { Set q to n/d }
  278. procedure mpz_divexact_ui(var q, n: mpz_t; d: valuint); cdecl; external LIB name '__gmpz_divexact_ui';
  279. { Return non-zero if n is exactly divisible by d }
  280. function mpz_divisible_p(var n, d: mpz_t): longint; cdecl; external LIB name '__gmpz_divisible_p';
  281. { Return non-zero if n is exactly divisible by d }
  282. function mpz_divisible_ui_p(var n: mpz_t; d: valuint): longint; cdecl; external LIB name '__gmpz_divisible_ui_p';
  283. { Return non-zero if n is exactly divisible by by 2^b }
  284. function mpz_divisible_2exp_p(var n: mpz_t; b: valuint): longint; cdecl; external LIB name '__gmpz_divisible_2exp_p';
  285. // GDB only: procedure mpz_dump(var _para1: mpz_t); cdecl; external LIB name '__gmpz_dump';
  286. { Fill buf with word data from op }
  287. function mpz_export(out buf; out countp: sizeuint; order: longint; size: sizeuint; endian: longint; nails: sizeuint; var op: mpz_t): pointer; cdecl; external LIB name '__gmpz_export';
  288. { Set rop to op!, the factorial of op }
  289. procedure mpz_fac_ui(var rop: mpz_t; op: valuint); cdecl; external LIB name '__gmpz_fac_ui';
  290. { Divide n by d, forming a quotient q. Round mode floor. }
  291. procedure mpz_fdiv_q(var q, n, d: mpz_t); cdecl; external LIB name '__gmpz_fdiv_q';
  292. { Divide n by d, forming a quotient q. d = 2^b. Round mode floor. }
  293. procedure mpz_fdiv_q_2exp(var q, n: mpz_t; b: valuint); cdecl; external LIB name '__gmpz_fdiv_q_2exp';
  294. { Divide n by d, forming a quotient q. Round mode floor. }
  295. function mpz_fdiv_q_ui(var q, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_fdiv_q_ui';
  296. { Divide n by d, forming a quotient q and remainder r. Round mode floor. }
  297. procedure mpz_fdiv_qr(var q, r, n, d: mpz_t); cdecl; external LIB name '__gmpz_fdiv_qr';
  298. { Divide n by d, forming a quotient q and remainder r. Round mode floor. }
  299. function mpz_fdiv_qr_ui(var q, r, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_fdiv_qr_ui';
  300. { Divide n by d, forming a remainder r. Round mode floor. }
  301. procedure mpz_fdiv_r(var r, n, d: mpz_t); cdecl; external LIB name '__gmpz_fdiv_r';
  302. { Divide n by d, forming a remainder r. d = 2^b. Round mode floor. }
  303. procedure mpz_fdiv_r_2exp(var r, n: mpz_t; b: valuint); cdecl; external LIB name '__gmpz_fdiv_r_2exp';
  304. { Divide n by d, forming a remainder r. Round mode floor. }
  305. function mpz_fdiv_r_ui(var r, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_fdiv_r_ui';
  306. { Divide n by d. Round mode floor. }
  307. function mpz_fdiv_ui(var n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_fdiv_ui';
  308. { Set fn to to Fn, the n’th Fibonacci number }
  309. procedure mpz_fib_ui(var fn: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_fib_ui';
  310. { Set fn to Fn, and fnsub1 to Fn−1 }
  311. procedure mpz_fib2_ui(var fn, fnsub1: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_fib2_ui';
  312. { Return non-zero iff the value of op fits in an signed int. Otherwise, return zero. }
  313. function mpz_fits_sint_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_fits_sint_p';
  314. { Return non-zero iff the value of op fits in an signed long int. Otherwise, return zero. }
  315. function mpz_fits_slong_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_fits_slong_p';
  316. { Return non-zero iff the value of op fits in an signed short int. Otherwise, return zero. }
  317. function mpz_fits_sshort_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_fits_sshort_p';
  318. { Return non-zero iff the value of op fits in an unsigned int. Otherwise, return zero. }
  319. function mpz_fits_uint_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_fits_uint_p';
  320. { Return non-zero iff the value of op fits in an unsigned long int. Otherwise, return zero. }
  321. function mpz_fits_ulong_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_fits_ulong_p';
  322. { Return non-zero iff the value of op fits in an unsigned short int. Otherwise, return zero. }
  323. function mpz_fits_ushort_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_fits_ushort_p';
  324. { Set rop to the greatest common divisor of op1 and op2 }
  325. procedure mpz_gcd(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_gcd';
  326. { Compute the greatest common divisor of op1 and op2. If rop is not NULL, store the result there. }
  327. function mpz_gcd_ui(var rop, op1: mpz_t; op2: valuint): valuint; cdecl; external LIB name '__gmpz_gcd_ui';
  328. { Set g to the greatest common divisor of a and b, and in addition set s and t to coefficients satisfying as + bt = g }
  329. procedure mpz_gcdext(var g, s, t, a, b: mpz_t); cdecl; external LIB name '__gmpz_gcdext';
  330. { Convert op to a double, truncating if necessary (ie. rounding towards zero) }
  331. function mpz_get_d(var op: mpz_t): double; cdecl; external LIB name '__gmpz_get_d';
  332. { Convert op to a double, truncating if necessary (ie. rounding towards zero), and returning the exponent separately }
  333. function mpz_get_d_2exp(out exp: valsint; var op: mpz_t): double; cdecl; external LIB name '__gmpz_get_d_2exp';
  334. { Return the value of op as a signed long }
  335. function mpz_get_si(var op: mpz_t): valsint; cdecl; external LIB name '__gmpz_get_si';
  336. { Convert op to a string of digits in base base. The base argument may vary from 2 to 62 or from −2 to −36. If str is NULL, the result string is allocated using the current allocation function }
  337. function mpz_get_str(str: pchar; base: longint; var op: mpz_t): pchar; cdecl; external LIB name '__gmpz_get_str';
  338. { Return the value of op as an unsigned long }
  339. function mpz_get_ui(var op: mpz_t): valuint; cdecl; external LIB name '__gmpz_get_ui';
  340. { Return limb number n from op }
  341. function mpz_getlimbn(var op: mpz_t; n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpz_getlimbn';
  342. { If op1 and op2 are both >= 0 or both < 0, return the hamming distance between the two operands, which is the number of bit positions where op1 and op2 have different bit values }
  343. function mpz_hamdist(var op1, op2: mpz_t): valuint; cdecl; external LIB name '__gmpz_hamdist';
  344. { Set rop from an array of word data at op }
  345. procedure mpz_import(var rop: mpz_t; count: sizeuint; order: longint; size: sizeuint; endian: longint; nails: sizeuint; const op); cdecl; external LIB name '__gmpz_import';
  346. { Initialize integer, and set its value to 0 }
  347. procedure mpz_init(out integer_: mpz_t); cdecl; external LIB name '__gmpz_init';
  348. { Initialize integer, with space for n bits, and set its value to 0 }
  349. procedure mpz_init2(out integer_: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_init2';
  350. { Initialize rop with limb space and set the initial numeric value from op }
  351. procedure mpz_init_set(out rop: mpz_t; var op: mpz_t); cdecl; external LIB name '__gmpz_init_set';
  352. { Initialize rop with limb space and set the initial numeric value from op }
  353. procedure mpz_init_set_d(out rop: mpz_t; op: double); cdecl; external LIB name '__gmpz_init_set_d';
  354. { Initialize rop with limb space and set the initial numeric value from op }
  355. procedure mpz_init_set_si(out rop: mpz_t; op: valsint); cdecl; external LIB name '__gmpz_init_set_si';
  356. { Initialize rop and set its value like mpz_set_str }
  357. function mpz_init_set_str(out rop: mpz_t; str: pchar; base: longint): longint; cdecl; external LIB name '__gmpz_init_set_str';
  358. { Initialize rop with limb space and set the initial numeric value from op }
  359. procedure mpz_init_set_ui(out rop: mpz_t; op: valuint); cdecl; external LIB name '__gmpz_init_set_ui';
  360. { Compute the inverse of op1 modulo op2 and put the result in rop }
  361. function mpz_invert(var rop, op1, op2: mpz_t): longint; cdecl; external LIB name '__gmpz_invert';
  362. { Set rop to op1 bitwise inclusive-or op2 }
  363. procedure mpz_ior(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_ior';
  364. { Calculate the Jacobi symbol. This is defined only for b odd }
  365. function mpz_jacobi(var a, b: mpz_t): longint; cdecl; external LIB name '__gmpz_jacobi';
  366. { Calculate the Jacobi symbol with the Kronecker extension }
  367. function mpz_kronecker_si(var a: mpz_t; b: valsint): longint; cdecl; external LIB name '__gmpz_kronecker_si';
  368. { Calculate the Jacobi symbol with the Kronecker extension }
  369. function mpz_kronecker_ui(var a: mpz_t; b: valuint): longint; cdecl; external LIB name '__gmpz_kronecker_ui';
  370. { Calculate the Jacobi symbol with the Kronecker extension }
  371. function mpz_si_kronecker(a: valsint; var b: mpz_t): longint; cdecl; external LIB name '__gmpz_si_kronecker';
  372. { Calculate the Jacobi symbol with the Kronecker extension }
  373. function mpz_ui_kronecker(a: valuint; var b: mpz_t): longint; cdecl; external LIB name '__gmpz_ui_kronecker';
  374. { Set rop to the least common multiple of op1 and op2 }
  375. procedure mpz_lcm(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_lcm';
  376. { Set rop to the least common multiple of op1 and op2 }
  377. procedure mpz_lcm_ui(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_lcm_ui';
  378. { Set ln to to Ln, the n’th Lucas number }
  379. procedure mpz_lucnum_ui(var ln: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_lucnum_ui';
  380. { Set ln to Ln, and lnsub1 to Ln−1 }
  381. procedure mpz_lucnum2_ui(var ln, lnsub1: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_lucnum2_ui';
  382. // No docs: function mpz_millerrabin(var _para1: mpz_t; _para2: longint): longint; cdecl; external LIB name '__gmpz_millerrabin';
  383. { Set r to n mod d. The sign of the divisor is ignored; the result is always non-negative. }
  384. procedure mpz_mod(var r, n, d: mpz_t); cdecl; external LIB name '__gmpz_mod';
  385. { Set rop to op1 × op2 }
  386. procedure mpz_mul(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_mul';
  387. { Set rop to op1 × 2^op2. This operation can also be defined as a left shift by op2 bits. }
  388. procedure mpz_mul_2exp(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_mul_2exp';
  389. { Set rop to op1 × op2 }
  390. procedure mpz_mul_si(var rop, op1: mpz_t; op2: valsint); cdecl; external LIB name '__gmpz_mul_si';
  391. { Set rop to op1 × op2 }
  392. procedure mpz_mul_ui(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_mul_ui';
  393. { Set rop to −op }
  394. procedure mpz_neg(var rop, op: mpz_t); cdecl; external LIB name '__gmpz_neg';
  395. { Set rop to the next prime greater than op }
  396. procedure mpz_nextprime(var rop, op: mpz_t); cdecl; external LIB name '__gmpz_nextprime';
  397. { Return non-zero if op is a perfect power, i.e., if there exist integers a and b, with b > 1, such that op = a^b }
  398. function mpz_perfect_power_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_perfect_power_p';
  399. { Return non-zero if op is a perfect square, i.e., if the square root of op is an integer }
  400. function mpz_perfect_square_p(var op: mpz_t): longint; cdecl; external LIB name '__gmpz_perfect_square_p';
  401. { If op >= 0, return the population count of op, which is the number of 1 bits in the binary representation }
  402. function mpz_popcount(var op: mpz_t): valuint; cdecl; external LIB name '__gmpz_popcount';
  403. { Set rop to base^exp. The case 0^0 yields 1. }
  404. procedure mpz_pow_ui(var rop, base: mpz_t; exp: valuint); cdecl; external LIB name '__gmpz_pow_ui';
  405. { Set rop to base^exp mod mod_ }
  406. procedure mpz_powm(var rop, base, exp, mod_: mpz_t); cdecl; external LIB name '__gmpz_powm';
  407. { Set rop to base^exp mod mod_ }
  408. procedure mpz_powm_ui(var rop, base: mpz_t; exp: valuint; var mod_: mpz_t); cdecl; external LIB name '__gmpz_powm_ui';
  409. { Determine whether n is prime. Return 2 if n is definitely prime, return 1 if n is probably prime (without being certain), or return 0 if n is definitely composite. }
  410. function mpz_probab_prime_p(var n: mpz_t; reps: longint): longint; cdecl; external LIB name '__gmpz_probab_prime_p';
  411. { Obsolete: Generate a random integer of at most max_size limbs }
  412. // procedure mpz_random(var rop: mpz_t; max_size: mp_size_t); cdecl; external LIB name '__gmpz_random';
  413. { Obsolete: Generate a random integer of at most max_size limbs, with long strings of zeros and ones in the binary representation }
  414. // procedure mpz_random2(var rop: mpz_t; max_size: mp_size_t); cdecl; external LIB name '__gmpz_random2';
  415. { Change the space allocated for integer to n bits. The value in integer is preserved if it fits, or is set to 0 if not. }
  416. procedure mpz_realloc2(var integer_: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_realloc2';
  417. { Remove all occurrences of the factor f from op and store the result in rop }
  418. function mpz_remove(var rop, op, f: mpz_t): valuint; cdecl; external LIB name '__gmpz_remove';
  419. { Set rop to trunc(op^(1/n)), the truncated integer part of the nth root of op. Return non-zero if the computation was exact, i.e., if op is rop to the nth power. }
  420. function mpz_root(var rop, op: mpz_t; n: valuint): longint; cdecl; external LIB name '__gmpz_root';
  421. { Set root to trunc(u^(1/n)), the truncated integer part of the nth root of u. Set rem to the remainder, (u − root^n). }
  422. procedure mpz_rootrem(var root, rem, u: mpz_t; n: valuint); cdecl; external LIB name '__gmpz_rootrem';
  423. { Generate a random integer with long strings of zeros and ones in the binary representation }
  424. procedure mpz_rrandomb(var rop: mpz_t; var state: randstate_t; n: valuint); cdecl; external LIB name '__gmpz_rrandomb';
  425. { Scan op, starting from bit starting_bit, towards more significant bits, until the first 0 bit is found }
  426. function mpz_scan0(var op: mpz_t; starting_bit: valuint): valuint; cdecl; external LIB name '__gmpz_scan0';
  427. { Scan op, starting from bit starting_bit, towards more significant bits, until the first 1 bit is found }
  428. function mpz_scan1(var op: mpz_t; starting_bit: valuint): valuint; cdecl; external LIB name '__gmpz_scan1';
  429. { Set the value of rop from op }
  430. procedure mpz_set(var rop, op: mpz_t); cdecl; external LIB name '__gmpz_set';
  431. { Set the value of rop from op }
  432. procedure mpz_set_d(var rop: mpz_t; op: double); cdecl; external LIB name '__gmpz_set_d';
  433. { Set the value of rop from op }
  434. procedure mpz_set_f(var rop: mpz_t; var op: mpf_t); cdecl; external LIB name '__gmpz_set_f';
  435. { Set the value of rop from op }
  436. procedure mpz_set_q(var rop: mpz_t; var op: mpq_t); cdecl; external LIB name '__gmpz_set_q';
  437. { Set the value of rop from op }
  438. procedure mpz_set_si(var rop: mpz_t; op: valsint); cdecl; external LIB name '__gmpz_set_si';
  439. { Set the value of rop from str, a null-terminated C string in base base. White space is allowed in the string, and is simply ignored. }
  440. function mpz_set_str(var rop: mpz_t; str: pchar; base: longint): longint; cdecl; external LIB name '__gmpz_set_str';
  441. { Set the value of rop from op }
  442. procedure mpz_set_ui(var rop: mpz_t; op: valuint); cdecl; external LIB name '__gmpz_set_ui';
  443. { Set bit bit_index in rop }
  444. procedure mpz_setbit(var rop: mpz_t; bit_index: valuint); cdecl; external LIB name '__gmpz_setbit';
  445. { Return the size of op measured in number of limbs }
  446. function mpz_size(var op: mpz_t): sizeuint; cdecl; external LIB name '__gmpz_size';
  447. { Return the size of op measured in number of digits in the given base }
  448. function mpz_sizeinbase(var op: mpz_t; base: longint): sizeuint; cdecl; external LIB name '__gmpz_sizeinbase';
  449. { Set rop to trunc(sqrt(op)), the truncated integer part of the square root of op }
  450. procedure mpz_sqrt(var rop, op: mpz_t); cdecl; external LIB name '__gmpz_sqrt';
  451. { Set rop1 to trunc(sqrt(op)), likempz_sqrt. Set rop2 to the remainder (op − rop1^2), which will be zero if op is a perfect square. }
  452. procedure mpz_sqrtrem(var rop1, rop2, op: mpz_t); cdecl; external LIB name '__gmpz_sqrtrem';
  453. { Set rop to op1 − op2 }
  454. procedure mpz_sub(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_sub';
  455. { Set rop to op1 − op2 }
  456. procedure mpz_sub_ui(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_sub_ui';
  457. { Set rop to op1 − op2 }
  458. procedure mpz_ui_sub(var rop: mpz_t; op1: valuint; var op2: mpz_t); cdecl; external LIB name '__gmpz_ui_sub';
  459. { Set rop to rop − op1 × op2 }
  460. procedure mpz_submul(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_submul';
  461. { Set rop to rop − op1 × op2 }
  462. procedure mpz_submul_ui(var rop, op1: mpz_t; op2: valuint); cdecl; external LIB name '__gmpz_submul_ui';
  463. { Swap the values rop1 and rop2 efficiently }
  464. procedure mpz_swap(var rop1, rop2: mpz_t); cdecl; external LIB name '__gmpz_swap';
  465. { Divide n by d, forming a quotient q. Round mode trunc. }
  466. procedure mpz_tdiv_q(var q, n, d: mpz_t); cdecl; external LIB name '__gmpz_tdiv_q';
  467. { Divide n by d, forming a quotient q. d = 2^b. Round mode trunc. }
  468. procedure mpz_tdiv_q_2exp(var q, n: mpz_t; b: valuint); cdecl; external LIB name '__gmpz_tdiv_q_2exp';
  469. { Divide n by d, forming a quotient q. Round mode trunc. }
  470. function mpz_tdiv_q_ui(var q, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_tdiv_q_ui';
  471. { Divide n by d, forming a quotient q and remainder r. Round mode trunc. }
  472. procedure mpz_tdiv_qr(var q, r, n, d: mpz_t); cdecl; external LIB name '__gmpz_tdiv_qr';
  473. { Divide n by d, forming a quotient q and remainder r. Round mode trunc. }
  474. function mpz_tdiv_qr_ui(var q, r, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_tdiv_qr_ui';
  475. { Divide n by d, forming a remainder r. Round mode trunc. }
  476. procedure mpz_tdiv_r(var r, n, d: mpz_t); cdecl; external LIB name '__gmpz_tdiv_r';
  477. { Divide n by d, forming a remainder r. d = 2^b. Round mode trunc. }
  478. procedure mpz_tdiv_r_2exp(var r, n: mpz_t; b: valuint); cdecl; external LIB name '__gmpz_tdiv_r_2exp';
  479. { Divide n by d, forming a remainder r. Round mode trunc. }
  480. function mpz_tdiv_r_ui(var r, n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_tdiv_r_ui';
  481. { Divide n by d. Round mode trunc. }
  482. function mpz_tdiv_ui(var n: mpz_t; d: valuint): valuint; cdecl; external LIB name '__gmpz_tdiv_ui';
  483. { Test bit bit_index in op and return 0 or 1 accordingly }
  484. function mpz_tstbit(var rop: mpz_t; bit_index: valuint): longint; cdecl; external LIB name '__gmpz_tstbit';
  485. { Set rop to base^exp. The case 0^0 yields 1 }
  486. procedure mpz_ui_pow_ui(var rop: mpz_t; base, exp: valuint); cdecl; external LIB name '__gmpz_ui_pow_ui';
  487. { Generate a uniformly distributed random integer in the range 0 to 2^n − 1, inclusive }
  488. procedure mpz_urandomb(var rop: mpz_t; var state: randstate_t; n: valuint); cdecl; external LIB name '__gmpz_urandomb';
  489. { Generate a uniform random integer in the range 0 to n − 1, inclusive }
  490. procedure mpz_urandomm(var rop: mpz_t; var state: randstate_t; var n: mpz_t); cdecl; external LIB name '__gmpz_urandomm';
  491. { Set rop to op1 bitwise exclusive-or op2 }
  492. procedure mpz_xor(var rop, op1, op2: mpz_t); cdecl; external LIB name '__gmpz_xor';
  493. // ---- Rational Number Functions ----
  494. { Set rop to the absolute value of op }
  495. procedure mpq_abs(var rop, op: mpq_t); cdecl; external LIB name '__gmpq_abs';
  496. { Set sum to addend1 + addend2 }
  497. procedure mpq_add(var sum, addend1, addend2: mpq_t); cdecl; external LIB name '__gmpq_add';
  498. { Remove any factors that are common to the numerator and denominator of op, and make the denominator positive }
  499. procedure mpq_canonicalize(var op: mpq_t); cdecl; external LIB name '__gmpq_canonicalize';
  500. { Free the space occupied by rational number }
  501. procedure mpq_clear(var rational_number: mpq_t); cdecl; external LIB name '__gmpq_clear';
  502. { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2 }
  503. function mpq_cmp(var op1, op2: mpq_t): longint; cdecl; external LIB name '__gmpq_cmp';
  504. { Compare op1 and num2/den2. Return a positive value if op1 > num2/den2, zero if op1 = num2/den2, and a negative value if op1 < num2/den2 }
  505. function mpq_cmp_si(var op1: mpq_t; num2: valsint; den2: valuint): longint; cdecl; external LIB name '__gmpq_cmp_si';
  506. { Compare op1 and num2/den2. Return a positive value if op1 > num2/den2, zero if op1 = num2/den2, and a negative value if op1 < num2/den2 }
  507. function mpq_cmp_ui(var op1: mpq_t; num2, den2: valuint): longint; cdecl; external LIB name '__gmpq_cmp_ui';
  508. { Set quotient to dividend/divisor }
  509. procedure mpq_div(var quotient, dividend, divisor: mpq_t); cdecl; external LIB name '__gmpq_div';
  510. { Set rop to op1/(2^op2) }
  511. procedure mpq_div_2exp(var rop, op1: mpq_t; op2: valuint); cdecl; external LIB name '__gmpq_div_2exp';
  512. { Return non-zero if op1 and op2 are equal, zero if they are non-equal }
  513. function mpq_equal(var op1, op2: mpq_t): longint; cdecl; external LIB name '__gmpq_equal';
  514. { Get the numerator of a rational }
  515. procedure mpq_get_num(var numerator: mpz_t; var rational: mpq_t); cdecl; external LIB name '__gmpq_get_num';
  516. { Get the denominator of a rational }
  517. procedure mpq_get_den(var denominator: mpz_t; var rational: mpq_t); cdecl; external LIB name '__gmpq_get_den';
  518. { Convert op to a double, truncating if necessary (ie. rounding towards zero) }
  519. function mpq_get_d(var op: mpq_t): double; cdecl; external LIB name '__gmpq_get_d';
  520. { Convert op to a string of digits in base base }
  521. function mpq_get_str(str: pchar; base: longint; var op: mpq_t): pchar; cdecl; external LIB name '__gmpq_get_str';
  522. { Initialize dest rational and set it to 0/1 }
  523. procedure mpq_init(out dest_rational: mpq_t); cdecl; external LIB name '__gmpq_init';
  524. { Set inverted_number to 1/number }
  525. procedure mpq_inv(var inverted_number, number: mpq_t); cdecl; external LIB name '__gmpq_inv';
  526. { Set product to multiplier × multiplicand }
  527. procedure mpq_mul(var product, multiplier, multiplicand: mpq_t); cdecl; external LIB name '__gmpq_mul';
  528. { Set rop to op1 × (2^op2) }
  529. procedure mpq_mul_2exp(var rop, op1: mpq_t; op2: valuint); cdecl; external LIB name '__gmpq_mul_2exp';
  530. { Set negated_operand to −operand }
  531. procedure mpq_neg(var negated_operand, operand: mpq_t); cdecl; external LIB name '__gmpq_neg';
  532. { Assign rop from op }
  533. procedure mpq_set(var rop, op: mpq_t); cdecl; external LIB name '__gmpq_set';
  534. { Set rop to the value of op. There is no rounding, this conversion is exact. }
  535. procedure mpq_set_d(var rop: mpq_t; op: double); cdecl; external LIB name '__gmpq_set_d';
  536. { Set the denominator of a rational }
  537. procedure mpq_set_den(var rational: mpq_t; var denominator: mpz_t); cdecl; external LIB name '__gmpq_set_den';
  538. { Set rop to the value of op. There is no rounding, this conversion is exact. }
  539. procedure mpq_set_f(var rop: mpq_t; var op: mpf_t); cdecl; external LIB name '__gmpq_set_f';
  540. { Set the numerator of a rational }
  541. procedure mpq_set_num(var rational: mpq_t; var numerator: mpz_t); cdecl; external LIB name '__gmpq_set_num';
  542. { Set the value of rop to op1/op2 }
  543. procedure mpq_set_si(var rop: mpq_t; op1: valsint; op2: valuint); cdecl; external LIB name '__gmpq_set_si';
  544. { Set rop from a null-terminated string str in the given base }
  545. function mpq_set_str(var rop: mpq_t; str: pchar; base: longint): longint; cdecl; external LIB name '__gmpq_set_str';
  546. { Set the value of rop to op1/op2 }
  547. procedure mpq_set_ui(var rop: mpq_t; op1, op2: valuint); cdecl; external LIB name '__gmpq_set_ui';
  548. { Assign rop from op }
  549. procedure mpq_set_z(var rop: mpq_t; var op: mpz_t); cdecl; external LIB name '__gmpq_set_z';
  550. { Set difference to minuend − subtrahend }
  551. procedure mpq_sub(var difference, minuend, subtrahend: mpq_t); cdecl; external LIB name '__gmpq_sub';
  552. { Swap the values rop1 and rop2 efficiently }
  553. procedure mpq_swap(var rop1, rop2: mpq_t); cdecl; external LIB name '__gmpq_swap';
  554. // ---- Floating-point Functions ----
  555. { Set rop to the absolute value of op }
  556. procedure mpf_abs(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_abs';
  557. { Set rop to op1 + op2 }
  558. procedure mpf_add(var rop, op1, op2: mpf_t); cdecl; external LIB name '__gmpf_add';
  559. { Set rop to op1 + op2 }
  560. procedure mpf_add_ui(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_add_ui';
  561. { Set rop to op rounded to the next higher integer }
  562. procedure mpf_ceil(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_ceil';
  563. { Free the space occupied by x }
  564. procedure mpf_clear(var x: mpf_t); cdecl; external LIB name '__gmpf_clear';
  565. { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
  566. function mpf_cmp(var op1, op2: mpf_t): longint; cdecl; external LIB name '__gmpf_cmp';
  567. { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
  568. function mpf_cmp_d(var op1: mpf_t; op2: double): longint; cdecl; external LIB name '__gmpf_cmp_d';
  569. { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
  570. function mpf_cmp_si(var op1: mpf_t; op2: valsint): longint; cdecl; external LIB name '__gmpf_cmp_si';
  571. { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
  572. function mpf_cmp_ui(var op1: mpf_t; op2: valuint): longint; cdecl; external LIB name '__gmpf_cmp_ui';
  573. { Set rop to op1/op2 }
  574. procedure mpf_div(var rop, op1, op2: mpf_t); cdecl; external LIB name '__gmpf_div';
  575. { Set rop to op1/(2^op2) }
  576. procedure mpf_div_2exp(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_div_2exp';
  577. { Set rop to op1/op2 }
  578. procedure mpf_div_ui(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_div_ui';
  579. // GDB only: procedure mpf_dump(var _para1: mpf_t); cdecl; external LIB name '__gmpf_dump';
  580. { Return non-zero if the first op3 bits of op1 and op2 are equal, zero otherwise }
  581. function mpf_eq(var op1, op2: mpf_t; op3: valuint): longint; cdecl; external LIB name '__gmpf_eq';
  582. { Return non-zero if op would fit in the respective C data type, when truncated to an integer }
  583. function mpf_fits_sint_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_fits_sint_p';
  584. { Return non-zero if op would fit in the respective C data type, when truncated to an integer }
  585. function mpf_fits_slong_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_fits_slong_p';
  586. { Return non-zero if op would fit in the respective C data type, when truncated to an integer }
  587. function mpf_fits_sshort_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_fits_sshort_p';
  588. { Return non-zero if op would fit in the respective C data type, when truncated to an integer }
  589. function mpf_fits_uint_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_fits_uint_p';
  590. { Return non-zero if op would fit in the respective C data type, when truncated to an integer }
  591. function mpf_fits_ulong_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_fits_ulong_p';
  592. { Return non-zero if op would fit in the respective C data type, when truncated to an integer }
  593. function mpf_fits_ushort_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_fits_ushort_p';
  594. { Set rop to op rounded to the next lower }
  595. procedure mpf_floor(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_floor';
  596. { Convert op to a double, truncating if necessary (ie. rounding towards zero) }
  597. function mpf_get_d(var op: mpf_t): double; cdecl; external LIB name '__gmpf_get_d';
  598. { Convert op to a double, truncating if necessary (ie. rounding towards zero), and with an exponent returned separately }
  599. function mpf_get_d_2exp(out exp: valsint; var op: mpf_t): double; cdecl; external LIB name '__gmpf_get_d_2exp';
  600. { Return the default precision actually used }
  601. function mpf_get_default_prec: valuint; cdecl; external LIB name '__gmpf_get_default_prec';
  602. { Return the current precision of op, in bits }
  603. function mpf_get_prec(var op: mpf_t): valuint; cdecl; external LIB name '__gmpf_get_prec';
  604. { Convert op to a long, truncating any fraction part }
  605. function mpf_get_si(var op: mpf_t): valsint; cdecl; external LIB name '__gmpf_get_si';
  606. { Convert op to a string of digits in base base }
  607. function mpf_get_str(str: pchar; out exp: mp_exp_t; base: longint; ndigits: sizeuint; var op: mpf_t): pchar; cdecl; external LIB name '__gmpf_get_str';
  608. { Convert op to a unsigned long, truncating any fraction part }
  609. function mpf_get_ui(var op: mpf_t): valuint; cdecl; external LIB name '__gmpf_get_ui';
  610. { Initialize x to 0 }
  611. procedure mpf_init(out x: mpf_t); cdecl; external LIB name '__gmpf_init';
  612. { Initialize x to 0 and set its precision to be at least prec bits }
  613. procedure mpf_init2(out x: mpf_t; prec: valuint); cdecl; external LIB name '__gmpf_init2';
  614. { Initialize rop and set its value from op }
  615. procedure mpf_init_set(out rop: mpf_t; var op: mpf_t); cdecl; external LIB name '__gmpf_init_set';
  616. { Initialize rop and set its value from op }
  617. procedure mpf_init_set_d(out rop: mpf_t; op: double); cdecl; external LIB name '__gmpf_init_set_d';
  618. { Initialize rop and set its value from op }
  619. procedure mpf_init_set_si(out rop: mpf_t; op: valsint); cdecl; external LIB name '__gmpf_init_set_si';
  620. { Initialize rop and set its value from the string in str }
  621. function mpf_init_set_str(out rop: mpf_t; str: pchar; base: longint): longint; cdecl; external LIB name '__gmpf_init_set_str';
  622. { Initialize rop and set its value from op }
  623. procedure mpf_init_set_ui(out rop: mpf_t; op: valuint); cdecl; external LIB name '__gmpf_init_set_ui';
  624. { Return non-zero if op is an integer }
  625. function mpf_integer_p(var op: mpf_t): longint; cdecl; external LIB name '__gmpf_integer_p';
  626. { Set rop to op1 × op2 }
  627. procedure mpf_mul(var rop, op1, op2: mpf_t); cdecl; external LIB name '__gmpf_mul';
  628. { Set rop to op1 × (2^op2) }
  629. procedure mpf_mul_2exp(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_mul_2exp';
  630. { Set rop to op1 × op2 }
  631. procedure mpf_mul_ui(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_mul_ui';
  632. { Set rop to −op }
  633. procedure mpf_neg(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_neg';
  634. { Set rop to op1^op2 }
  635. procedure mpf_pow_ui(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_pow_ui';
  636. { Generate a random float of at most max_size limbs, with long strings of zeros and ones in the binary representation }
  637. procedure mpf_random2(var rop: mpf_t; max_size: mp_size_t; exp: mp_exp_t); cdecl; external LIB name '__gmpf_random2';
  638. { Compute the relative difference between op1 and op2 and store the result in rop }
  639. procedure mpf_reldiff(var rop, op1, op2: mpf_t); cdecl; external LIB name '__gmpf_reldiff';
  640. { Set the value of rop from op }
  641. procedure mpf_set(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_set';
  642. { Set the value of rop from op }
  643. procedure mpf_set_d(var rop: mpf_t; op: double); cdecl; external LIB name '__gmpf_set_d';
  644. { Set the default precision to be at least prec bits }
  645. procedure mpf_set_default_prec(prec: valuint); cdecl; external LIB name '__gmpf_set_default_prec';
  646. { Set the precision of rop to be at least prec bits }
  647. procedure mpf_set_prec(var rop: mpf_t; prec: valuint); cdecl; external LIB name '__gmpf_set_prec';
  648. { Set the precision of rop to be at least prec bits, without changing the memory allocated }
  649. procedure mpf_set_prec_raw(var rop: mpf_t; prec: valuint); cdecl; external LIB name '__gmpf_set_prec_raw';
  650. { Set the value of rop from op }
  651. procedure mpf_set_q(var rop: mpf_t; var op: mpq_t); cdecl; external LIB name '__gmpf_set_q';
  652. { Set the value of rop from op }
  653. procedure mpf_set_si(var rop: mpf_t; op: valsint); cdecl; external LIB name '__gmpf_set_si';
  654. { Set the value of rop from the string in str }
  655. function mpf_set_str(var rop: mpf_t; str: pchar; base: longint): longint; cdecl; external LIB name '__gmpf_set_str';
  656. { Set the value of rop from op }
  657. procedure mpf_set_ui(var rop: mpf_t; op: valuint); cdecl; external LIB name '__gmpf_set_ui';
  658. { Set the value of rop from op }
  659. procedure mpf_set_z(var rop: mpf_t; var op: mpz_t); cdecl; external LIB name '__gmpf_set_z';
  660. // No docs: function mpf_size(var _para1: mpf_t): size_t; cdecl; external LIB name '__gmpf_size';
  661. { Set rop to op^(1/2) }
  662. procedure mpf_sqrt(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_sqrt';
  663. { Set rop to op^(1/2) }
  664. procedure mpf_sqrt_ui(var rop: mpf_t; op: valuint); cdecl; external LIB name '__gmpf_sqrt_ui';
  665. { Set rop to op1 − op2 }
  666. procedure mpf_sub(var rop, op1, op2: mpf_t); cdecl; external LIB name '__gmpf_sub';
  667. { Set rop to op1 − op2 }
  668. procedure mpf_sub_ui(var rop, op1: mpf_t; op2: valuint); cdecl; external LIB name '__gmpf_sub_ui';
  669. { Swap rop1 and rop2 efficiently }
  670. procedure mpf_swap(var rop1, rop2: mpf_t); cdecl; external LIB name '__gmpf_swap';
  671. { Set rop to op rounded to the integer towards zero }
  672. procedure mpf_trunc(var rop, op: mpf_t); cdecl; external LIB name '__gmpf_trunc';
  673. { Set rop to op1/op2 }
  674. procedure mpf_ui_div(var rop: mpf_t; op1: valuint; var op2: mpf_t); cdecl; external LIB name '__gmpf_ui_div';
  675. { Set rop to op1 − op2 }
  676. procedure mpf_ui_sub(var rop: mpf_t; op1: valuint; var op2: mpf_t); cdecl; external LIB name '__gmpf_ui_sub';
  677. { Generate a uniformly distributed random float in rop, such that 0 <= rop < 1, with nbits significant bits in the mantissa }
  678. procedure mpf_urandomb(var rop: mpf_t; var state: randstate_t; nbits: valuint); cdecl; external LIB name '__gmpf_urandomb';
  679. // ---- Low-level Functions ----
  680. { Add [s1p, s1n] and [s2p, s2n], and write the s1n least significant limbs of the result to rp. Return carry, either 0 or 1. }
  681. function mpn_add(rop, s1p: mpn_ptr; s1n: mp_size_t; s2p: mpn_ptr; s2n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpn_add';
  682. { Add [s1p, n] and s2limb, and write the n least significant limbs of the result to rp. Return carry, either 0 or 1. }
  683. function mpn_add_1(rp, s1p: mpn_ptr; n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_add_1';
  684. { Add [s1p, n] and [s2p, n], and write the n least significant limbs of the result to rp. Return carry, either 0 or 1. }
  685. function mpn_add_n(rop, s1p, s2p: mpn_ptr; n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpn_add_n';
  686. { Multiply [s1p, n] and s2limb, and add the n least significant limbs of the product to [rp, n] and write the result to rp. Return the most significant limb of the product, plus carry-out from the addition. }
  687. function mpn_addmul_1(rp, s1p: mpn_ptr; n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_addmul_1';
  688. { Preliminary: This function puts the low floor(d/mp_bits_per_limb) limbs of q = [s1p, s1n]/[s2p, s2n] mod 2^d at rp, and returns the high d mod mp_bits_per_limb bits of q }
  689. // function mpn_bdivmod(rp, s1p: mpn_t; s1n: mp_size_t; s2p: mpn_t; s2n: mp_size_t; d: valuint): mp_limb_t; cdecl; external LIB name '__gmpn_bdivmod';
  690. { Compare [s1p, n] and [s2p, n] and return a positive value if s1 > s2, 0 if they are equal, or a negative value if s1 < s2 }
  691. function mpn_cmp(s1p, s2p: mpn_ptr; n: mp_size_t): longint; cdecl; external LIB name '__gmpn_cmp';
  692. { Divide [sp, n] by 3, expecting it to divide exactly, and writing the result to [rp, n] }
  693. function mpn_divexact_by3c(rp, sp: mpn_ptr; n: mp_size_t; carry: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_divexact_by3c';
  694. { Obsolete: Divide [rs2p, rs2n] by [s3p, s3n], and write the quotient at r1p, with the exception of the most significant limb, which is returned }
  695. // function mpn_divrem(r1p: mpn_t; qxn: mp_size_t; rs2p: mpn_t; rs2n: mp_size_t; s3p: mpn_t; s3n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpn_divrem';
  696. { Divide [s2p, s2n] by s3limb, and write the quotient at r1p. Return the remainder }
  697. function mpn_divrem_1(r1p: mpn_ptr; qxn: mp_size_t; s2p: mpn_ptr; s2n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_divrem_1';
  698. // No docs: function mpn_divrem_2(_para1: mpn_t; _para2: mp_size_t; _para3: mpn_t; _para4: mp_size_t; _para5: mpn_t): mp_limb_t; cdecl; external LIB name '__gmpn_divrem_2';
  699. { Set [rp, retval] to the greatest common divisor of [s1p, s1n] and [s2p, s2n] }
  700. function mpn_gcd(rp, s1p: mpn_ptr; s1n: mp_size_t; s2p: mpn_ptr; s2n: mp_size_t): mp_size_t; cdecl; external LIB name '__gmpn_gcd';
  701. { Return the greatest common divisor of [s1p, s1n] and s2limb }
  702. function mpn_gcd_1(s1p: mpn_ptr; s1n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_gcd_1';
  703. { Calculate the greatest common divisor of [s1p, s1n] and [s2p, s2n] }
  704. function mpn_gcdext(r1p, r2p: mpn_ptr; out r2n: mp_size_t; s1p: mpn_ptr; s1n: mp_size_t; s2p: mpn_ptr; s2n: mp_size_t): mp_size_t; cdecl; external LIB name '__gmpn_gcdext';
  705. { Convert [s1p, s1n] to a raw unsigned char array at str in base base, and return the number of characters produced }
  706. function mpn_get_str(str: pbyte; base: longint; s1p: mpn_ptr; s1n: mp_size_t):sizeuint; cdecl; external LIB name '__gmpn_get_str';
  707. { Compute the hamming distance between [s1p, n] and [s2p, n], which is the number of bit positions where the two operands have different bit values }
  708. function mpn_hamdist(s1p, s2p: mpn_ptr; n: mp_size_t): valuint; cdecl; external LIB name '__gmpn_hamdist';
  709. { Shift [sp, n] left by count bits, and write the result to [rp, n] }
  710. function mpn_lshift(rp, sp: mpn_ptr; n: mp_size_t; count: dword): mp_limb_t; cdecl; external LIB name '__gmpn_lshift';
  711. { Divide [s1p, s1n] by s2limb, and return the remainder. s1n can be zero. }
  712. function mpn_mod_1(s1p: mpn_ptr; s1n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_mod_1';
  713. { Multiply [s1p, s1n] and [s2p, s2n], and write the result to rp. Return the most significant limb of the result. }
  714. function mpn_mul(rp, s1p: mpn_ptr; s1n: mp_size_t; s2p: mpn_ptr; s2n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpn_mul';
  715. { Multiply [s1p, n] by s2limb, and write the n least significant limbs of the product to rp. Return the most significant limb of the product. }
  716. function mpn_mul_1(rp, s1p: mpn_ptr; n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_mul_1';
  717. { Multiply [s1p, n] and [s2p, n], and write the 2*n-limb result to rp }
  718. procedure mpn_mul_n(rp, s1p, s2p: mpn_ptr; n: mp_size_t); cdecl; external LIB name '__gmpn_mul_n';
  719. { Return non-zero iff [s1p, n] is a perfect square }
  720. function mpn_perfect_square_p(s1p: mpn_ptr; n: mp_size_t): longint; cdecl; external LIB name '__gmpn_perfect_square_p';
  721. { Count the number of set bits in [s1p, n] }
  722. function mpn_popcount(s1p: mpn_ptr; n: mp_size_t): valuint; cdecl; external LIB name '__gmpn_popcount';
  723. // No docs: function mpn_pow_1(_para1, _para2: mpn_t; _para3: mp_size_t; _para4, _para5: mpn_t): mp_size_t; cdecl; external LIB name '__gmpn_pow_1';
  724. // No docs: function mpn_preinv_mod_1(_para1: mpn_t; _para2: mp_size_t; _para3, _para4: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_preinv_mod_1';
  725. { Generate a random number of length r1n and store it at r1p }
  726. procedure mpn_random(r1p: mpn_ptr; r1n: mp_size_t); cdecl; external LIB name '__gmpn_random';
  727. { Generate a random number of length r1n and store it at r1p }
  728. procedure mpn_random2(r1p: mpn_ptr; r1n: mp_size_t); cdecl; external LIB name '__gmpn_random2';
  729. { Shift [sp, n] right by count bits, and write the result to [rp, n] }
  730. function mpn_rshift(rp, sp: mpn_ptr; n: mp_size_t; count: dword): mp_limb_t; cdecl; external LIB name '__gmpn_rshift';
  731. { Scan s1p from bit position bit for the next clear bit }
  732. function mpn_scan0(s1p: mpn_ptr; bit: valuint): valuint; cdecl; external LIB name '__gmpn_scan0';
  733. { Scan s1p from bit position bit for the next set bit }
  734. function mpn_scan1(s1p: mpn_ptr; bit: valuint): valuint; cdecl; external LIB name '__gmpn_scan1';
  735. { Convert bytes [str,strsize] in the given base to limbs at rp }
  736. function mpn_set_str(rp: mpn_ptr; str: pbyte; strsize: sizeuint; base:longint): mp_size_t; cdecl; external LIB name '__gmpn_set_str';
  737. { Compute the square root of [sp, n] and put the result at [r1p, dn/2e] and the remainder at [r2p, retval] }
  738. function mpn_sqrtrem(r1p, r2p, sp: mpn_ptr; n: mp_size_t): mp_size_t; cdecl; external LIB name '__gmpn_sqrtrem';
  739. { Subtract [s2p, s2n] from [s1p, s1n], and write the s1n least significant limbs of the result to rp. Return borrow, either 0 or 1. }
  740. function mpn_sub(rp, s1p: mpn_ptr; s1n: mp_size_t; s2p: mpn_ptr; s2n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpn_sub';
  741. { Subtract s2limb from [s1p, n], and write the n least significant limbs of the result to rp. Return borrow, either 0 or 1. }
  742. function mpn_sub_1(rp, s1p: mpn_ptr; n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_sub_1';
  743. { Subtract [s2p, n] from [s1p, n], and write the n least significant limbs of the result to rp. Return borrow, either 0 or 1. }
  744. function mpn_sub_n(rp, s1p, s2p: mpn_ptr; n: mp_size_t): mp_limb_t; cdecl; external LIB name '__gmpn_sub_n';
  745. { Multiply [s1p, n] and s2limb, and subtract the n least significant limbs of the product from [rp, n] and write the result to rp. Return the most significant limb of the product, plus borrow-out from the subtraction. }
  746. function mpn_submul_1(rp, s1p: mpn_ptr; n: mp_size_t; s2limb: mp_limb_t): mp_limb_t; cdecl; external LIB name '__gmpn_submul_1';
  747. { Divide [np, nn] by [dp, dn] and put the quotient at [qp, nn−dn+1] and the remainder at [rp, dn] }
  748. procedure mpn_tdiv_qr(qp, rp: mpn_ptr; qxn: mp_size_t; np: mpn_ptr; nn: mp_size_t; dp: mpn_ptr; dn: mp_size_t); cdecl; external LIB name '__gmpn_tdiv_qr';
  749. // ---- GMP properties ----
  750. { Size of a limb on this machine }
  751. function bits_per_limb: longint;
  752. { Some GMP functions may set this thread unsafe variable. Better avoid using it. }
  753. function errno: longint;
  754. { GMP version string a.b.c }
  755. function version: string;
  756. // ==== ext bindings =====
  757. // ---- Random Number Functions ----
  758. { Initialize state with a default algorithm }
  759. procedure randinit_default(out state: MPRandState);
  760. { Initialize state with a linear congruential algorithm X = (aX + c) mod 2^m2exp }
  761. procedure randinit_lc_2exp(out state: MPRandState; var a: MPInteger; c, m2exp: valuint);
  762. { Initialize state for a linear congruential algorithm as per gmp_randinit_lc_2exp }
  763. function randinit_lc_2exp_size(out state: MPRandState; size: sizeuint): boolean;
  764. { Initialize state for a Mersenne Twister algorithm }
  765. procedure randinit_mt(out state: MPRandState);
  766. { Initialize rop with a copy of the algorithm and state from op }
  767. procedure randinit_set(out rop: MPRandState; var op: MPRandState);
  768. { Set an initial seed value into state }
  769. procedure randseed(var state: MPRandState; var seed: MPInteger);
  770. { Set an initial seed value into state }
  771. procedure randseed_ui(var state: MPRandState; seed: valuint);
  772. { Free all memory occupied by state }
  773. procedure randclear(var state: MPRandState);
  774. { Return an uniformly distributed random number of n bits, ie. in the range 0 to 2^n−1 inclusive }
  775. function urandomb_ui(var state: MPRandState; n: valuint): valuint;
  776. { Return an uniformly distributed random number in the range 0 to n − 1, inclusive }
  777. function urandomm_ui(var state: MPRandState; n: valuint): valuint;
  778. // ---- integer Functions ----
  779. { Change the space for integer to new_alloc limbs }
  780. function z_realloc(var integer_: MPInteger; new_alloc: mp_size_t): pointer;
  781. { Set rop to the absolute value of op }
  782. procedure z_abs(var rop, op: MPInteger);
  783. { Return the absolute value of op }
  784. function z_abs(var op: MPInteger): MPInteger;
  785. { Set rop to op1 + op2 }
  786. procedure z_add(var rop, op1, op2: MPInteger);
  787. { Return op1 + op2 }
  788. function z_add(var op1, op2: MPInteger): MPInteger;
  789. { Set rop to op1 + op2 }
  790. procedure z_add_ui(var rop, op1: MPInteger; op2: valuint);
  791. { Return op1 + op2 }
  792. function z_add_ui(var op1: MPInteger; op2: valuint): MPInteger;
  793. { Set rop to rop + op1 × op2 }
  794. procedure z_addmul(var rop, op1, op2: MPInteger);
  795. { Set rop to rop + op1 × op2 }
  796. procedure z_addmul_ui(var rop, op1: MPInteger; op2: valuint);
  797. { Set rop to op1 bitwise-and op2 }
  798. procedure z_and(var rop, op1, op2: MPInteger);
  799. { Return op1 bitwise-and op2 }
  800. function z_and(var op1, op2: MPInteger): MPInteger;
  801. //{ _Fixed_ space of fixed_num_bits is allocated to each of the array size integers in integer array }
  802. //procedure z_array_init(var integer_array: MPInteger; array_size, fixed_num_bits: mp_size_t);
  803. { Compute the binomial coefficient (n over k) and store the result in rop }
  804. procedure z_bin_ui(var rop, n: MPInteger; k: valuint);
  805. { Return the binomial coefficient (n over k) }
  806. function z_bin_ui(var n: MPInteger; k: valuint): MPInteger;
  807. { Compute the binomial coefficient (n over k) and store the result in rop }
  808. procedure z_bin_uiui(var rop: MPInteger; n, k: valuint);
  809. { Return the binomial coefficient (n over k) }
  810. function z_bin_uiui(n, k: valuint): MPInteger;
  811. { Divide n by d, forming a quotient q. Round mode ceil. }
  812. procedure z_cdiv_q(var q, n, d: MPInteger);
  813. { Divide n by d, forming a return quotient. Round mode ceil. }
  814. function z_cdiv_q(var n, d: MPInteger): MPInteger;
  815. { Divide n by d, forming a quotient q. d = 2^b. Round mode ceil. }
  816. procedure z_cdiv_q_2exp(var q, n: MPInteger; b: valuint);
  817. { Divide n by d, forming a return quotient. d = 2^b. Round mode ceil. }
  818. function z_cdiv_q_2exp(var n: MPInteger; b: valuint): MPInteger;
  819. { Divide n by d, forming a quotient q. Round mode ceil. }
  820. function z_cdiv_q_ui(var q, n: MPInteger; d: valuint): valuint;
  821. { Divide n by d, forming a quotient q and remainder r. Round mode ceil. }
  822. procedure z_cdiv_qr(var q, r, n, d: MPInteger);
  823. { Divide n by d, forming a quotient q and remainder r. Round mode ceil. }
  824. function z_cdiv_qr_ui(var q, r, n: MPInteger; d: valuint): valuint;
  825. { Divide n by d, forming a remainder r. Round mode ceil. }
  826. procedure z_cdiv_r(var r, n, d: MPInteger);
  827. { Divide n by d, forming a return remainder. Round mode ceil. }
  828. function z_cdiv_r(var n, d: MPInteger): MPInteger;
  829. { Divide n by d, forming a remainder r. d = 2^b. Round mode ceil. }
  830. procedure z_cdiv_r_2exp(var r, n: MPInteger; b: valuint);
  831. { Divide n by d, forming a return remainder. d = 2^b. Round mode ceil. }
  832. function z_cdiv_r_2exp(var n: MPInteger; b: valuint): MPInteger;
  833. { Divide n by d, forming a remainder r. Round mode ceil. }
  834. function z_cdiv_r_ui(var r, n: MPInteger; d: valuint): valuint;
  835. { Divide n by d. Round mode ceil. }
  836. function z_cdiv_ui(var n: MPInteger; d: valuint): valuint;
  837. { Free the space occupied by integer. Call this function for all MPInteger variables when you are done with them. }
  838. procedure z_clear(var integer_: MPInteger);
  839. { Clear bit bit_index in rop }
  840. procedure z_clrbit(var rop: MPInteger; bit_index: valuint);
  841. { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
  842. function z_cmp(var op1, op2: MPInteger): longint;
  843. { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
  844. function z_cmp_d(var op1: MPInteger; op2: double): longint;
  845. { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
  846. function z_cmp_si(var op1: MPInteger; op2: valsint): longint;
  847. { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. }
  848. function z_cmp_ui(var op1: MPInteger; op2: valuint): longint;
  849. { Compare the absolute values of op1 and op2. Return a positive value if |op1| > |op2|, zero if |op1| = |op2|, or a negative value if |op1| < |op2|. }
  850. function z_cmpabs(var op1, op2: MPInteger): longint;
  851. { Compare the absolute values of op1 and op2. Return a positive value if |op1| > |op2|, zero if |op1| = |op2|, or a negative value if |op1| < |op2|. }
  852. function z_cmpabs_d(var op1: MPInteger; op2: double): longint;
  853. { Compare the absolute values of op1 and op2. Return a positive value if |op1| > |op2|, zero if |op1| = |op2|, or a negative value if |op1| < |op2|. }
  854. function z_cmpabs_ui(var op1: MPInteger; op2: valuint): longint;
  855. { Set rop to the one’s complement of op }
  856. procedure z_com(var rop, op: MPInteger);
  857. { Return the one’s complement of op }
  858. function z_com(var op: MPInteger): MPInteger;
  859. { Complement bit bit_index in rop }
  860. procedure z_combit(var rop: MPInteger; bit_index: valuint);
  861. { Return true if n is congruent to c modulo d }
  862. function z_congruent_p(var n, c, d: MPInteger): boolean;
  863. { Return true if n is congruent to c modulo 2^b }
  864. function z_congruent_2exp_p(var n, c: MPInteger; b: valuint): boolean;
  865. { Return true if n is congruent to c modulo d }
  866. function z_congruent_ui_p(var n: MPInteger; c, d: valuint): boolean;
  867. { Set q to n/d }
  868. procedure z_divexact(var q, n, d: MPInteger);
  869. { Return n/d }
  870. function z_divexact(var n, d: MPInteger): MPInteger;
  871. { Set q to n/d }
  872. procedure z_divexact_ui(var q, n: MPInteger; d: valuint);
  873. { Return n/d }
  874. function z_divexact_ui(var n: MPInteger; d: valuint): MPInteger;
  875. { Return true if n is exactly divisible by d }
  876. function z_divisible_p(var n, d: MPInteger): boolean;
  877. { Return true if n is exactly divisible by d }
  878. function z_divisible_ui_p(var n: MPInteger; d: valuint): boolean;
  879. { Return true if n is exactly divisible by by 2^b }
  880. function z_divisible_2exp_p(var n: MPInteger; b: valuint): boolean;
  881. { Fill buf with word data from op }
  882. function z_export(out buf; out countp: sizeuint; order: longint; size: sizeuint; endian: longint; nails: sizeuint; var op: MPInteger): pointer;
  883. { Set rop to op!, the factorial of op }
  884. procedure z_fac_ui(var rop: MPInteger; op: valuint);
  885. { Return op!, the factorial of op }
  886. function z_fac_ui(op: valuint): MPInteger;
  887. { Divide n by d, forming a quotient q. Round mode floor. }
  888. procedure z_fdiv_q(var q, n, d: MPInteger);
  889. { Divide n by d, forming a return quotient. Round mode floor. }
  890. function z_fdiv_q(var n, d: MPInteger): MPInteger;
  891. { Divide n by d, forming a quotient q. d = 2^b. Round mode floor. }
  892. procedure z_fdiv_q_2exp(var q, n: MPInteger; b: valuint);
  893. { Divide n by d, forming a return quotient. d = 2^b. Round mode floor. }
  894. function z_fdiv_q_2exp(var n: MPInteger; b: valuint): MPInteger;
  895. { Divide n by d, forming a quotient q. Round mode floor. }
  896. function z_fdiv_q_ui(var q, n: MPInteger; d: valuint): valuint;
  897. { Divide n by d, forming a quotient q and remainder r. Round mode floor. }
  898. procedure z_fdiv_qr(var q, r, n, d: MPInteger);
  899. { Divide n by d, forming a quotient q and remainder r. Round mode floor. }
  900. function z_fdiv_qr_ui(var q, r, n: MPInteger; d: valuint): valuint;
  901. { Divide n by d, forming a remainder r. Round mode floor. }
  902. procedure z_fdiv_r(var r, n, d: MPInteger);
  903. { Divide n by d, forming a return remainder. Round mode floor. }
  904. function z_fdiv_r(var n, d: MPInteger): MPInteger;
  905. { Divide n by d, forming a remainder r. d = 2^b. Round mode floor. }
  906. procedure z_fdiv_r_2exp(var r, n: MPInteger; b: valuint);
  907. { Divide n by d, forming a return remainder. d = 2^b. Round mode floor. }
  908. function z_fdiv_r_2exp(var n: MPInteger; b: valuint): MPInteger;
  909. { Divide n by d, forming a remainder r. Round mode floor. }
  910. function z_fdiv_r_ui(var r, n: MPInteger; d: valuint): valuint;
  911. { Divide n by d. Round mode floor. }
  912. function z_fdiv_ui(var n: MPInteger; d: valuint): valuint;
  913. { Set fn to to Fn, the n’th Fibonacci number }
  914. procedure z_fib_ui(var fn: MPInteger; n: valuint);
  915. { Return Fn, the n’th Fibonacci number }
  916. function z_fib_ui(n: valuint): MPInteger;
  917. { Set fn to Fn, and fnsub1 to Fn−1 }
  918. procedure z_fib2_ui(var fn, fnsub1: MPInteger; n: valuint);
  919. { Return Fn, and fnsub1 = Fn−1 }
  920. function z_fib2_ui(var fnsub1: MPInteger; n: valuint): MPInteger;
  921. { Return true iff the value of op fits in an signed int }
  922. function z_fits_sint_p(var op: MPInteger): boolean;
  923. { Return true iff the value of op fits in an signed long int }
  924. function z_fits_slong_p(var op: MPInteger): boolean;
  925. { Return true iff the value of op fits in an signed short int }
  926. function z_fits_sshort_p(var op: MPInteger): boolean;
  927. { Return true iff the value of op fits in an unsigned int }
  928. function z_fits_uint_p(var op: MPInteger): boolean;
  929. { Return true iff the value of op fits in an unsigned long int }
  930. function z_fits_ulong_p(var op: MPInteger): boolean;
  931. { Return true iff the value of op fits in an unsigned short int }
  932. function z_fits_ushort_p(var op: MPInteger): boolean;
  933. { Set rop to the greatest common divisor of op1 and op2 }
  934. procedure z_gcd(var rop, op1, op2: MPInteger);
  935. { Return the greatest common divisor of op1 and op2 }
  936. function z_gcd(var op1, op2: MPInteger): MPInteger;
  937. { Compute the greatest common divisor of op1 and op2 }
  938. function z_gcd_ui(var rop, op1: MPInteger; op2: valuint): valuint;
  939. { Set g to the greatest common divisor of a and b, and in addition set s and t to coefficients satisfying as + bt = g }
  940. procedure z_gcdext(var g, s, t, a, b: MPInteger);
  941. { Convert op to a double, truncating if necessary (ie. rounding towards zero) }
  942. function z_get_d(var op: MPInteger): double;
  943. { Convert op to a double, truncating if necessary (ie. rounding towards zero), and returning the exponent separately }
  944. function z_get_d_2exp(out exp: valsint; var op: MPInteger): double;
  945. { Return the value of op as a signed long }
  946. function z_get_si(op: MPInteger): valsint;
  947. { Convert op to a string of digits in base base. The base argument may vary from 2 to 62 or from −2 to −36. }
  948. function z_get_str(base: longint; var op: MPInteger): string;
  949. { Convert op to a string of digits in base base. The base argument may vary from 2 to 62 or from −2 to −36. If str is NULL, the result string is allocated using the current allocation function }
  950. function z_get_str(str: pchar; base: longint; var op: MPInteger): pchar;
  951. { Return the value of op as an unsigned long }
  952. function z_get_ui(op: MPInteger): valuint;
  953. { Return limb number n from op }
  954. function z_getlimbn(var op: MPInteger; n: mp_size_t): mp_limb_t;
  955. { If op1 and op2 are both >= 0 or both < 0, return the hamming distance between the two operands, which is the number of bit positions where op1 and op2 have different bit values }
  956. function z_hamdist(var op1, op2: MPInteger): valuint;
  957. { Set rop from an array of word data at op }
  958. procedure z_import(var rop: MPInteger; count: sizeuint; order: longint; size: sizeuint; endian: longint; nails: sizeuint; const op);
  959. { Initialize integer, and set its value to 0 }
  960. procedure z_init(out integer_: MPInteger);
  961. { Initialize integer, with space for n bits, and set its value to 0 }
  962. procedure z_init2(out integer_: MPInteger; n: valuint);
  963. { Initialize rop with limb space and set the initial numeric value from op }
  964. procedure z_init_set(out rop: MPInteger; var op: MPInteger);
  965. { Initialize rop with limb space and set the initial numeric value from op }
  966. procedure z_init_set_d(out rop: MPInteger; op: double);
  967. { Initialize rop with limb space and set the initial numeric value from op }
  968. procedure z_init_set_si(out rop: MPInteger; op: valsint);
  969. { Initialize rop and set its value like z_set_str. If the string is a correct base base number, the function returns true. }
  970. function z_init_set_str(out rop: MPInteger; str: string; base: longint): boolean;
  971. { Initialize rop with limb space and set the initial numeric value from op }
  972. procedure z_init_set_ui(out rop: MPInteger; op: valuint);
  973. { Compute the inverse of op1 modulo op2 and put the result in rop }
  974. function z_invert(var rop, op1, op2: MPInteger): longint;
  975. { Set rop to op1 bitwise inclusive-or op2 }
  976. procedure z_ior(var rop, op1, op2: MPInteger);
  977. { Return bitwise inclusive-or op2 }
  978. function z_ior(var op1, op2: MPInteger): MPInteger;
  979. { Calculate the Jacobi symbol. This is defined only for b odd }
  980. function z_jacobi(var a, b: MPInteger): longint;
  981. { Calculate the Jacobi symbol with the Kronecker extension }
  982. function z_kronecker_si(var a: MPInteger; b: valsint): longint;
  983. { Calculate the Jacobi symbol with the Kronecker extension }
  984. function z_kronecker_ui(var a: MPInteger; b: valuint): longint;
  985. { Calculate the Jacobi symbol with the Kronecker extension }
  986. function z_si_kronecker(a: valsint; var b: MPInteger): longint;
  987. { Calculate the Jacobi symbol with the Kronecker extension }
  988. function z_ui_kronecker(a: valuint; var b: MPInteger): longint;
  989. { Set rop to the least common multiple of op1 and op2 }
  990. procedure z_lcm(var rop, op1, op2: MPInteger);
  991. { Return the least common multiple of op1 and op2 }
  992. function z_lcm(var op1, op2: MPInteger): MPInteger;
  993. { Set rop to the least common multiple of op1 and op2 }
  994. procedure z_lcm_ui(var rop, op1: MPInteger; op2: valuint);
  995. { Return the least common multiple of op1 and op2 }
  996. function z_lcm_ui(var op1: MPInteger; op2: valuint): MPInteger;
  997. { Set ln to to Ln, the n’th Lucas number }
  998. procedure z_lucnum_ui(var ln: MPInteger; n: valuint);
  999. { Return Ln, the n’th Lucas number }
  1000. function z_lucnum_ui(n: valuint): MPInteger;
  1001. { Set ln to Ln, and lnsub1 to Ln−1 }
  1002. procedure z_lucnum2_ui(var ln, lnsub1: MPInteger; n: valuint);
  1003. { Return Ln, and lnsub1 to Ln−1 }
  1004. function z_lucnum2_ui(var lnsub1: MPInteger; n: valuint): MPInteger;
  1005. { Set r to n mod d. The sign of the divisor is ignored; the result is always non-negative. }
  1006. procedure z_mod(var r, n, d: MPInteger);
  1007. { Return n mod d. The sign of the divisor is ignored; the result is always non-negative. }
  1008. function z_mod(var n, d: MPInteger): MPInteger;
  1009. { Set rop to op1 × op2 }
  1010. procedure z_mul(var rop, op1, op2: MPInteger);
  1011. { Return op1 × op2 }
  1012. function z_mul(var op1, op2: MPInteger): MPInteger;
  1013. { Set rop to op1 × 2^op2. This operation can also be defined as a left shift by op2 bits. }
  1014. procedure z_mul_2exp(var rop, op1: MPInteger; op2: valuint);
  1015. { Return op1 × 2^op2. This operation can also be defined as a left shift by op2 bits. }
  1016. function z_mul_2exp(var op1: MPInteger; op2: valuint): MPInteger;
  1017. { Set rop to op1 × op2 }
  1018. procedure z_mul_si(var rop, op1: MPInteger; op2: valsint);
  1019. { Return op1 × op2 }
  1020. function z_mul_si(var op1: MPInteger; op2: valsint): MPInteger;
  1021. { Set rop to op1 × op2 }
  1022. procedure z_mul_ui(var rop, op1: MPInteger; op2: valuint);
  1023. { Return op1 × op2 }
  1024. function z_mul_ui(var op1: MPInteger; op2: valuint): MPInteger;
  1025. { Set rop to −op }
  1026. procedure z_neg(var rop, op: MPInteger);
  1027. { Return −op }
  1028. function z_neg(var op: MPInteger): MPInteger;
  1029. { Set rop to the next prime greater than op }
  1030. procedure z_nextprime(var rop, op: MPInteger);
  1031. { Return the next prime greater than op }
  1032. function z_nextprime(var op: MPInteger): MPInteger;
  1033. { Return true if op is a perfect power, i.e., if there exist integers a and b, with b > 1, such that op = a^b }
  1034. function z_perfect_power_p(var op: MPInteger): boolean;
  1035. { Return true if op is a perfect square, i.e., if the square root of op is an integer }
  1036. function z_perfect_square_p(var op: MPInteger): boolean;
  1037. { If op >= 0, return the population count of op, which is the number of 1 bits in the binary representation }
  1038. function z_popcount(var op: MPInteger): valuint;
  1039. { Set rop to base^exp. The case 0^0 yields 1. }
  1040. procedure z_pow_ui(var rop, base: MPInteger; exp: valuint);
  1041. { Return base^exp. The case 0^0 yields 1. }
  1042. function z_pow_ui(var base: MPInteger; exp: valuint): MPInteger;
  1043. { Set rop to base^exp mod mod_ }
  1044. procedure z_powm(var rop, base, exp, mod_: MPInteger);
  1045. { Return base^exp mod mod_ }
  1046. function z_powm(var base, exp, mod_: MPInteger): MPInteger;
  1047. { Set rop to base^exp mod mod_ }
  1048. procedure z_powm_ui(var rop, base: MPInteger; exp: valuint; var mod_: MPInteger);
  1049. { Return base^exp mod mod_ }
  1050. function z_powm_ui(var base: MPInteger; exp: valuint; var mod_: MPInteger): MPInteger;
  1051. { Determine whether n is prime. Return 2 if n is definitely prime, return 1 if n is probably prime (without being certain), or return 0 if n is definitely composite. }
  1052. function z_probab_prime_p(var n: MPInteger; reps: longint): longint;
  1053. { Change the space allocated for integer to n bits. The value in integer is preserved if it fits, or is set to 0 if not. }
  1054. procedure z_realloc2(var integer_: MPInteger; n: valuint);
  1055. { Remove all occurrences of the factor f from op and store the result in rop }
  1056. function z_remove(var rop, op, f: MPInteger): valuint;
  1057. { Set rop to trunc(op^(1/n)), the truncated integer part of the nth root of op. Return true if the computation was exact, i.e., if op is rop to the nth power. }
  1058. function z_root(var rop, op: MPInteger; n: valuint): boolean;
  1059. { Set root to trunc(u^(1/n)), the truncated integer part of the nth root of u. Set rem to the remainder, (u − root^n). }
  1060. procedure z_rootrem(var root, rem, u: MPInteger; n: valuint);
  1061. { Generate a random integer with long strings of zeros and ones in the binary representation }
  1062. procedure z_rrandomb(var rop: MPInteger; var state: MPRandState; n: valuint);
  1063. { Return a random integer with long strings of zeros and ones in the binary representation }
  1064. function z_rrandomb(var state: MPRandState; n: valuint): MPInteger;
  1065. { Scan op, starting from bit starting_bit, towards more significant bits, until the first 0 bit is found }
  1066. function z_scan0(var op: MPInteger; starting_bit: valuint): valuint;
  1067. { Scan op, starting from bit starting_bit, towards more significant bits, until the first 1 bit is found }
  1068. function z_scan1(var op: MPInteger; starting_bit: valuint): valuint;
  1069. { Set the value of rop from op }
  1070. procedure z_set(var rop, op: MPInteger);
  1071. { Set the value of rop from op }
  1072. procedure z_set_d(var rop: MPInteger; op: double);
  1073. { Set the value of rop from op }
  1074. procedure z_set_f(var rop: MPInteger; var op: MPFloat);
  1075. { Set the value of rop from op }
  1076. procedure z_set_q(var rop: MPInteger; var op: MPRational);
  1077. { Set the value of rop from op }
  1078. procedure z_set_si(var rop: MPInteger; op: valsint);
  1079. { Set the value of rop from str, a null-terminated C string in base base. If the string is a correct base base number, the function returns true. }
  1080. function z_set_str(var rop: MPInteger; str: string; base: longint): boolean;
  1081. { Set the value of rop from op }
  1082. procedure z_set_ui(var rop: MPInteger; op: valuint);
  1083. { Set bit bit_index in rop }
  1084. procedure z_setbit(var rop: MPInteger; bit_index: valuint);
  1085. { Return the size of op measured in number of limbs }
  1086. function z_size(var op: MPInteger): sizeuint;
  1087. { Return the size of op measured in number of digits in the given base }
  1088. function z_sizeinbase(var op: MPInteger; base: longint): sizeuint;
  1089. { Set rop to trunc(sqrt(op)), the truncated integer part of the square root of op }
  1090. procedure z_sqrt(var rop, op: MPInteger);
  1091. { Return trunc(sqrt(op)), the truncated integer part of the square root of op }
  1092. function z_sqrt(var op: MPInteger): MPInteger;
  1093. { Set rop1 to trunc(sqrt(op)), like z_sqrt. Set rop2 to the remainder (op − rop1^2), which will be zero if op is a perfect square. }
  1094. procedure z_sqrtrem(var rop1, rop2, op: MPInteger);
  1095. { Set rop to op1 − op2 }
  1096. procedure z_sub(var rop, op1, op2: MPInteger);
  1097. { Return op1 − op2 }
  1098. function z_sub(var op1, op2: MPInteger): MPInteger;
  1099. { Set rop to op1 − op2 }
  1100. procedure z_sub_ui(var rop, op1: MPInteger; op2: valuint);
  1101. { Return op1 − op2 }
  1102. function z_sub_ui(var op1: MPInteger; op2: valuint): MPInteger;
  1103. { Set rop to op1 − op2 }
  1104. procedure z_ui_sub(var rop: MPInteger; op1: valuint; var op2: MPInteger);
  1105. { Return op1 − op2 }
  1106. function z_ui_sub(op1: valuint; var op2: MPInteger): MPInteger;
  1107. { Set rop to rop − op1 × op2 }
  1108. procedure z_submul(var rop, op1, op2: MPInteger);
  1109. { Set rop to rop − op1 × op2 }
  1110. procedure z_submul_ui(var rop, op1: MPInteger; op2: valuint);
  1111. { Swap the values rop1 and rop2 efficiently }
  1112. procedure z_swap(var rop1, rop2: MPInteger);
  1113. { Divide n by d, forming a quotient q. Round mode trunc. }
  1114. procedure z_tdiv_q(var q, n, d: MPInteger);
  1115. { Divide n by d, forming a return quotient. Round mode trunc. }
  1116. function z_tdiv_q(var n, d: MPInteger): MPInteger;
  1117. { Divide n by d, forming a quotient q. d = 2^b. Round mode trunc. }
  1118. procedure z_tdiv_q_2exp(var q, n: MPInteger; b: valuint);
  1119. { Divide n by d, forming a return quotient. d = 2^b. Round mode trunc. }
  1120. function z_tdiv_q_2exp(var n: MPInteger; b: valuint): MPInteger;
  1121. { Divide n by d, forming a quotient q. Round mode trunc. }
  1122. function z_tdiv_q_ui(var q, n: MPInteger; d: valuint): valuint;
  1123. { Divide n by d, forming a quotient q and remainder r. Round mode trunc. }
  1124. procedure z_tdiv_qr(var q, r, n, d: MPInteger);
  1125. { Divide n by d, forming a quotient q and remainder r. Round mode trunc. }
  1126. function z_tdiv_qr_ui(var q, r, n: MPInteger; d: valuint): valuint;
  1127. { Divide n by d, forming a remainder r. Round mode trunc. }
  1128. procedure z_tdiv_r(var r, n, d: MPInteger);
  1129. { Divide n by d, forming a return remainder. Round mode trunc. }
  1130. function z_tdiv_r(var n, d: MPInteger): MPInteger;
  1131. { Divide n by d, forming a remainder r. d = 2^b. Round mode trunc. }
  1132. procedure z_tdiv_r_2exp(var r, n: MPInteger; b: valuint);
  1133. { Divide n by d, forming a return remainder. d = 2^b. Round mode trunc. }
  1134. function z_tdiv_r_2exp(var n: MPInteger; b: valuint): MPInteger;
  1135. { Divide n by d, forming a remainder r. Round mode trunc. }
  1136. function z_tdiv_r_ui(var r, n: MPInteger; d: valuint): valuint;
  1137. { Divide n by d. Round mode trunc. }
  1138. function z_tdiv_ui(var n: MPInteger; d: valuint): valuint;
  1139. { Test bit bit_index in op and return true or false accordingly }
  1140. function z_tstbit(var rop: MPInteger; bit_index: valuint): boolean;
  1141. { Set rop to base^exp. The case 0^0 yields 1 }
  1142. procedure z_ui_pow_ui(var rop: MPInteger; base, exp: valuint);
  1143. { Return base^exp. The case 0^0 yields 1 }
  1144. function z_ui_pow_ui(base, exp: valuint): MPInteger;
  1145. { Generate a uniformly distributed random integer in the range 0 to 2^n − 1, inclusive }
  1146. procedure z_urandomb(var rop: MPInteger; var state: MPRandState; n: valuint);
  1147. { Return a uniformly distributed random integer in the range 0 to 2^n − 1, inclusive }
  1148. function z_urandomb(var state: MPRandState; n: valuint): MPInteger;
  1149. { Generate a uniform random integer in the range 0 to n − 1, inclusive }
  1150. procedure z_urandomm(var rop: MPInteger; var state: MPRandState; var n: MPInteger);
  1151. { Return a uniform random integer in the range 0 to n − 1, inclusive }
  1152. function z_urandomm(var state: MPRandState; var n: MPInteger): MPInteger;
  1153. { Set rop to op1 bitwise exclusive-or op2 }
  1154. procedure z_xor(var rop, op1, op2: MPInteger);
  1155. { Retuen op1 bitwise exclusive-or op2 }
  1156. function z_xor(var op1, op2: MPInteger): MPInteger;
  1157. // ---- Rational Number Functions ----
  1158. { Set rop to the absolute value of op }
  1159. procedure q_abs(var rop, op: MPRational);
  1160. { Return absolute value of op }
  1161. function q_abs(var op: MPRational): MPRational;
  1162. { Set sum to addend1 + addend2 }
  1163. procedure q_add(var sum, addend1, addend2: MPRational);
  1164. { Return addend1 + addend2 }
  1165. function q_add(var addend1, addend2: MPRational): MPRational;
  1166. { Remove any factors that are common to the numerator and denominator of op, and make the denominator positive }
  1167. procedure q_canonicalize(var op: MPRational);
  1168. { Free the space occupied by rational number }
  1169. procedure q_clear(var rational_number: MPRational);
  1170. { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2 }
  1171. function q_cmp(var op1, op2: MPRational): longint;
  1172. { Compare op1 and num2/den2. Return a positive value if op1 > num2/den2, zero if op1 = num2/den2, and a negative value if op1 < num2/den2 }
  1173. function q_cmp_si(var op1: MPRational; num2: valsint; den2: valuint): longint;
  1174. { Compare op1 and num2/den2. Return a positive value if op1 > num2/den2, zero if op1 = num2/den2, and a negative value if op1 < num2/den2 }
  1175. function q_cmp_ui(var op1: MPRational; num2, den2: valuint): longint;
  1176. { Set quotient to dividend/divisor }
  1177. procedure q_div(var quotient, dividend, divisor: MPRational);
  1178. { Return dividend/divisor }
  1179. function q_div(var dividend, divisor: MPRational): MPRational;
  1180. { Set rop to op1/(2^op2) }
  1181. procedure q_div_2exp(var rop, op1: MPRational; op2: valuint);
  1182. { Return op1/(2^op2) }
  1183. function q_div_2exp(var op1: MPRational; op2: valuint): MPRational;
  1184. { Return true if op1 and op2 are equal, false if they are non-equal }
  1185. function q_equal(var op1, op2: MPRational): boolean;
  1186. { Get the numerator of a rational }
  1187. procedure q_get_num(var numerator: MPInteger; var rational: MPRational);
  1188. { Return the numerator of a rational }
  1189. function q_get_num(var rational: MPRational): MPInteger;
  1190. { Get the denominator of a rational }
  1191. procedure q_get_den(var denominator: MPInteger; var rational: MPRational);
  1192. { Return the denominator of a rational }
  1193. function q_get_den(var rational: MPRational): MPInteger;
  1194. { Convert op to a double, truncating if necessary (ie. rounding towards zero) }
  1195. function q_get_d(var op: MPRational): double;
  1196. { Convert op to a string of digits in base base }
  1197. function q_get_str(base: longint; var op: MPRational): string;
  1198. { Convert op to a string of digits in base base. If str is NULL, the result string is allocated using the current allocation function. }
  1199. function q_get_str(str: pchar; base: longint; var op: MPRational): pchar;
  1200. { Initialize dest rational and set it to 0/1 }
  1201. procedure q_init(out dest_rational: MPRational);
  1202. { Set inverted_number to 1/number }
  1203. procedure q_inv(var inverted_number, number: MPRational);
  1204. { Return 1/number }
  1205. function q_inv(var number: MPRational): MPRational;
  1206. { Set product to multiplier × multiplicand }
  1207. procedure q_mul(var product, multiplier, multiplicand: MPRational);
  1208. { Return multiplier × multiplicand }
  1209. function q_mul(var multiplier, multiplicand: MPRational): MPRational;
  1210. { Set rop to op1 × (2^op2) }
  1211. procedure q_mul_2exp(var rop, op1: MPRational; op2: valuint);
  1212. { Return op1 × (2^op2) }
  1213. function q_mul_2exp(var op1: MPRational; op2: valuint): MPRational;
  1214. { Set negated_operand to −operand }
  1215. procedure q_neg(var negated_operand, operand: MPRational);
  1216. { Return −operand }
  1217. function q_neg(var operand: MPRational): MPRational;
  1218. { Assign rop from op }
  1219. procedure q_set(var rop, op: MPRational);
  1220. { Set rop to the value of op. There is no rounding, this conversion is exact. }
  1221. procedure q_set_d(var rop: MPRational; op: double);
  1222. { Set the denominator of a rational }
  1223. procedure q_set_den(var rational: MPRational; var denominator: MPInteger);
  1224. { Set rop to the value of op. There is no rounding, this conversion is exact. }
  1225. procedure q_set_f(var rop: MPRational; var op: MPFloat);
  1226. { Set the numerator of a rational }
  1227. procedure q_set_num(var rational: MPRational; var numerator: MPInteger);
  1228. { Set the value of rop to op1/op2 }
  1229. procedure q_set_si(var rop: MPRational; op1: valsint; op2: valuint);
  1230. { Set rop from a null-terminated string str in the given base. The return value is true if the entire string is a valid number. }
  1231. function q_set_str(var rop: MPRational; str: string; base: longint): boolean;
  1232. { Set the value of rop to op1/op2 }
  1233. procedure q_set_ui(var rop: MPRational; op1, op2: valuint);
  1234. { Assign rop from op }
  1235. procedure q_set_z(var rop: MPRational; var op: MPInteger);
  1236. { Set difference to minuend − subtrahend }
  1237. procedure q_sub(var difference, minuend, subtrahend: MPRational);
  1238. { Return minuend − subtrahend }
  1239. function q_sub(var minuend, subtrahend: MPRational): MPRational;
  1240. { Swap the values rop1 and rop2 efficiently }
  1241. procedure q_swap(var rop1, rop2: MPRational);
  1242. // ---- Floating-point Functions ----
  1243. { Set rop to the absolute value of op }
  1244. procedure f_abs(var rop, op: MPFloat);
  1245. { Return the absolute value of op }
  1246. function f_abs(var op: MPFloat): MPFloat;
  1247. { Set rop to op1 + op2 }
  1248. procedure f_add(var rop, op1, op2: MPFloat);
  1249. { Return op1 + op2 }
  1250. function f_add(var op1, op2: MPFloat): MPFloat;
  1251. { Set rop to op1 + op2 }
  1252. procedure f_add_ui(var rop, op1: MPFloat; op2: valuint);
  1253. { Return op1 + op2 }
  1254. function f_add_ui(var op1: MPFloat; op2: valuint): MPFloat;
  1255. { Set rop to op rounded to the next higher integer }
  1256. procedure f_ceil(var rop, op: MPFloat);
  1257. { Return op rounded to the next higher integer }
  1258. function f_ceil(var op: MPFloat): MPFloat;
  1259. { Free the space occupied by x }
  1260. procedure f_clear(var x: MPFloat);
  1261. { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
  1262. function f_cmp(var op1, op2: MPFloat): longint;
  1263. { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
  1264. function f_cmp_d(var op1: MPFloat; op2: double): longint;
  1265. { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
  1266. function f_cmp_si(var op1: MPFloat; op2: valsint): longint;
  1267. { Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, and a negative value if op1 < op2. }
  1268. function f_cmp_ui(var op1: MPFloat; op2: valuint): longint;
  1269. { Set rop to op1/op2 }
  1270. procedure f_div(var rop, op1, op2: MPFloat);
  1271. { Return op1/op2 }
  1272. function f_div(var op1, op2: MPFloat): MPFloat;
  1273. { Set rop to op1/(2^op2) }
  1274. procedure f_div_2exp(var rop, op1: MPFloat; op2: valuint);
  1275. { Return op1/(2^op2) }
  1276. function f_div_2exp(var op1: MPFloat; op2: valuint): MPFloat;
  1277. { Set rop to op1/op2 }
  1278. procedure f_div_ui(var rop, op1: MPFloat; op2: valuint);
  1279. { Return op1/op2 }
  1280. function f_div_ui(var op1: MPFloat; op2: valuint): MPFloat;
  1281. { Return true if the first op3 bits of op1 and op2 are equal, false otherwise }
  1282. function f_eq(var op1, op2: MPFloat; op3: valuint): boolean;
  1283. { Return true if op would fit in the respective C data type, when truncated to an integer }
  1284. function f_fits_sint_p(var op: MPFloat): boolean;
  1285. { Return true if op would fit in the respective C data type, when truncated to an integer }
  1286. function f_fits_slong_p(var op: MPFloat): boolean;
  1287. { Return true if op would fit in the respective C data type, when truncated to an integer }
  1288. function f_fits_sshort_p(var op: MPFloat): boolean;
  1289. { Return true if op would fit in the respective C data type, when truncated to an integer }
  1290. function f_fits_uint_p(var op: MPFloat): boolean;
  1291. { Return true if op would fit in the respective C data type, when truncated to an integer }
  1292. function f_fits_ulong_p(var op: MPFloat): boolean;
  1293. { Return true if op would fit in the respective C data type, when truncated to an integer }
  1294. function f_fits_ushort_p(var op: MPFloat): boolean;
  1295. { Set rop to op rounded to the next lower }
  1296. procedure f_floor(var rop, op: MPFloat);
  1297. { Return op rounded to the next lower }
  1298. function f_floor(var op: MPFloat): MPFloat;
  1299. { Convert op to a double, truncating if necessary (ie. rounding towards zero) }
  1300. function f_get_d(var op: MPFloat): double;
  1301. { Convert op to a double, truncating if necessary (ie. rounding towards zero), and with an exponent returned separately }
  1302. function f_get_d_2exp(out exp: valsint; var op: MPFloat): double;
  1303. { Return the default precision actually used }
  1304. function f_get_default_prec: valuint;
  1305. { Return the current precision of op, in bits }
  1306. function f_get_prec(var op: MPFloat): valuint;
  1307. { Convert op to a long, truncating any fraction part }
  1308. function f_get_si(var op: MPFloat): valsint;
  1309. { Convert op to a string of digits in base base }
  1310. function f_get_str(out exp: mp_exp_t; base: longint; ndigits: sizeuint; var op: MPFloat): string;
  1311. { Convert op to a string of digits in base base. If str is NULL, the result string is allocated using the current allocation function. }
  1312. function f_get_str(str: pchar; out exp: mp_exp_t; base: longint; ndigits: sizeuint; var op: MPFloat): pchar;
  1313. { Convert op to a unsigned long, truncating any fraction part }
  1314. function f_get_ui(var op: MPFloat): valuint;
  1315. { Initialize x to 0 }
  1316. procedure f_init(out x: MPFloat);
  1317. { Initialize x to 0 and set its precision to be at least prec bits }
  1318. procedure f_init2(out x: MPFloat; prec: valuint);
  1319. { Initialize rop and set its value from op }
  1320. procedure f_init_set(out rop: MPFloat; var op: MPFloat);
  1321. { Initialize rop and set its value from op }
  1322. procedure f_init_set_d(out rop: MPFloat; op: double);
  1323. { Initialize rop and set its value from op }
  1324. procedure f_init_set_si(out rop: MPFloat; op: valsint);
  1325. { Initialize rop and set its value from the string in str. Returns true if the entire string is a valid number in base base. }
  1326. function f_init_set_str(out rop: MPFloat; str: string; base: longint): boolean;
  1327. { Initialize rop and set its value from op }
  1328. procedure f_init_set_ui(out rop: MPFloat; op: valuint);
  1329. { Return true if op is an integer }
  1330. function f_integer_p(var op: MPFloat): boolean;
  1331. { Set rop to op1 × op2 }
  1332. procedure f_mul(var rop, op1, op2: MPFloat);
  1333. { Return op1 × op2 }
  1334. function f_mul(var op1, op2: MPFloat): MPFloat;
  1335. { Set rop to op1 × (2^op2) }
  1336. procedure f_mul_2exp(var rop, op1: MPFloat; op2: valuint);
  1337. { Return op1 × (2^op2) }
  1338. function f_mul_2exp(var op1: MPFloat; op2: valuint): MPFloat;
  1339. { Set rop to op1 × op2 }
  1340. procedure f_mul_ui(var rop, op1: MPFloat; op2: valuint);
  1341. { Return op1 × op2 }
  1342. function f_mul_ui(var op1: MPFloat; op2: valuint): MPFloat;
  1343. { Set rop to −op }
  1344. procedure f_neg(var rop, op: MPFloat);
  1345. { Return −op }
  1346. function f_neg(var op: MPFloat): MPFloat;
  1347. { Set rop to op1^op2 }
  1348. procedure f_pow_ui(var rop, op1: MPFloat; op2: valuint);
  1349. { Return op1^op2 }
  1350. function f_pow_ui(var op1: MPFloat; op2: valuint): MPFloat;
  1351. { Generate a random float of at most max_size limbs, with long strings of zeros and ones in the binary representation }
  1352. procedure f_random2(var rop: MPFloat; max_size: mp_size_t; exp: mp_exp_t);
  1353. { Return a random float of at most max_size limbs, with long strings of zeros and ones in the binary representation }
  1354. function f_random2(max_size: mp_size_t; exp: mp_exp_t): MPFloat;
  1355. { Compute the relative difference between op1 and op2 and store the result in rop }
  1356. procedure f_reldiff(var rop, op1, op2: MPFloat);
  1357. { Return the relative difference between op1 and op2 }
  1358. function f_reldiff(var op1, op2: MPFloat): MPFloat;
  1359. { Set the value of rop from op }
  1360. procedure f_set(var rop, op: MPFloat);
  1361. { Set the value of rop from op }
  1362. procedure f_set_d(var rop: MPFloat; op: double);
  1363. { Set the default precision to be at least prec bits }
  1364. procedure f_set_default_prec(prec: valuint);
  1365. { Set the precision of rop to be at least prec bits }
  1366. procedure f_set_prec(var rop: MPFloat; prec: valuint);
  1367. { Set the precision of rop to be at least prec bits, without changing the memory allocated }
  1368. procedure f_set_prec_raw(var rop: MPFloat; prec: valuint);
  1369. { Set the value of rop from op }
  1370. procedure f_set_q(var rop: MPFloat; var op: MPRational);
  1371. { Set the value of rop from op }
  1372. procedure f_set_si(var rop: MPFloat; op: valsint);
  1373. { Set the value of rop from the string in str. Returns true if the entire string is a valid number in base base. }
  1374. function f_set_str(var rop: MPFloat; str: string; base: longint): boolean;
  1375. { Set the value of rop from op }
  1376. procedure f_set_ui(var rop: MPFloat; op: valuint);
  1377. { Set the value of rop from op }
  1378. procedure f_set_z(var rop: MPFloat; var op: MPInteger);
  1379. { Set rop to op^(1/2) }
  1380. procedure f_sqrt(var rop, op: MPFloat);
  1381. { Return op^(1/2) }
  1382. function f_sqrt(var op: MPFloat): MPFloat;
  1383. { Set rop to op^(1/2) }
  1384. procedure f_sqrt_ui(var rop: MPFloat; op: valuint);
  1385. { Return op^(1/2) }
  1386. function f_sqrt_ui(op: valuint): MPFloat;
  1387. { Set rop to op1 − op2 }
  1388. procedure f_sub(var rop, op1, op2: MPFloat);
  1389. { Return op1 − op2 }
  1390. function f_sub(var op1, op2: MPFloat): MPFloat;
  1391. { Set rop to op1 − op2 }
  1392. procedure f_sub_ui(var rop, op1: MPFloat; op2: valuint);
  1393. { Return op1 − op2 }
  1394. function f_sub_ui(var op1: MPFloat; op2: valuint): MPFloat;
  1395. { Swap rop1 and rop2 efficiently }
  1396. procedure f_swap(var rop1, rop2: MPFloat);
  1397. { Set rop to op rounded to the integer towards zero }
  1398. procedure f_trunc(var rop, op: MPFloat);
  1399. { Return op rounded to the integer towards zero }
  1400. function f_trunc(var op: MPFloat): MPFloat;
  1401. { Set rop to op1/op2 }
  1402. procedure f_ui_div(var rop: MPFloat; op1: valuint; var op2: MPFloat);
  1403. { Return op1/op2 }
  1404. function f_ui_div(op1: valuint; var op2: MPFloat): MPFloat;
  1405. { Set rop to op1 − op2 }
  1406. procedure f_ui_sub(var rop: MPFloat; op1: valuint; var op2: MPFloat);
  1407. { Return op1 − op2 }
  1408. function f_ui_sub(op1: valuint; var op2: MPFloat): MPFloat;
  1409. { Generate a uniformly distributed random float in rop, such that 0 <= rop < 1, with nbits significant bits in the mantissa }
  1410. procedure f_urandomb(var rop: MPFloat; var state: MPRandState; nbits: valuint);
  1411. { Return a uniformly distributed random float, such that 0 <= result < 1, with nbits significant bits in the mantissa }
  1412. function f_urandomb(var state: MPRandState; nbits: valuint): MPFloat;
  1413. // ---- operators ----
  1414. operator * (op1: MPFloat; op2: MPFloat): MPFloat; inline;
  1415. operator * (op1: MPInteger; op2: MPInteger): MPInteger; inline;
  1416. operator * (op1: MPRational; op2: MPRational): MPRational; inline;
  1417. operator ** (op1: MPFloat; op2: valuint): MPFloat; inline;
  1418. operator ** (op1: MPInteger; op2: valuint): MPInteger; inline;
  1419. operator + (op1: MPFloat; op2: MPFloat): MPFloat; inline;
  1420. operator + (op1: MPInteger; op2: MPInteger): MPInteger; inline;
  1421. operator + (op1: MPRational; op2: MPRational): MPRational; inline;
  1422. operator - (op: MPFloat): MPFloat; inline;
  1423. operator - (op: MPInteger): MPInteger; inline;
  1424. operator - (op: MPRational): MPRational; inline;
  1425. operator - (op1: MPFloat; op2: MPFloat): MPFloat; inline;
  1426. operator - (op1: MPInteger; op2: MPInteger): MPInteger; inline;
  1427. operator - (op1: MPRational; op2: MPRational): MPRational; inline;
  1428. operator / (op1: MPFloat; op2: MPFloat): MPFloat; inline;
  1429. operator / (op1: MPInteger; op2: MPInteger): MPInteger; inline;
  1430. operator / (op1: MPRational; op2: MPRational): MPRational; inline;
  1431. operator := (op: double): MPFloat; inline;
  1432. operator := (op: double): MPInteger; inline;
  1433. operator := (op: double): MPRational; inline;
  1434. operator := (op: MPFloat): Cardinal; inline;
  1435. operator := (op: MPFloat): double; inline;
  1436. operator := (op: MPFloat): integer; inline;
  1437. operator := (op: MPFloat): mpf_t; inline;
  1438. operator := (op: MPFloat): MPInteger; inline;
  1439. operator := (op: MPFloat): MPRational; inline;
  1440. operator := (op: MPFloat): string; inline;
  1441. {$ifdef CPU64}
  1442. operator := (op: MPFloat): valsint; inline;
  1443. operator := (op: MPFloat): valuint; inline;
  1444. {$endif}
  1445. operator := (var op: mpf_t): MPFloat; inline;
  1446. operator := (op: MPInteger): cardinal; inline;
  1447. operator := (op: MPInteger): double; inline;
  1448. operator := (op: MPInteger): integer; inline;
  1449. operator := (op: MPInteger): MPFloat; inline;
  1450. operator := (op: MPInteger): MPRational; inline;
  1451. operator := (op: MPInteger): mpz_t; inline;
  1452. operator := (op: MPInteger): string; inline;
  1453. {$ifdef CPU64}
  1454. operator := (op: MPInteger): valsint; inline;
  1455. operator := (op: MPInteger): valuint; inline;
  1456. {$endif}
  1457. operator := (var op: mpq_t): MPRational; inline;
  1458. operator := (op: MPRandState): randstate_t; inline;
  1459. operator := (op: MPRational): double; inline;
  1460. operator := (op: MPRational): MPFloat; inline;
  1461. operator := (op: MPRational): MPInteger; inline;
  1462. operator := (op: MPRational): mpq_t; inline;
  1463. operator := (op: MPRational): string; inline;
  1464. operator := (var op: mpz_t): MPInteger; inline;
  1465. operator := (var op: randstate_t): MPRandState; inline;
  1466. operator := (op: string): MPFloat; inline;
  1467. operator := (op: string): MPInteger; inline;
  1468. operator := (op: string): MPRational; inline;
  1469. operator := (op: valsint): MPFloat; inline;
  1470. operator := (op: valsint): MPInteger; inline;
  1471. operator := (op: valsint): MPRational; inline;
  1472. operator := (op: valuint): MPFloat; inline;
  1473. operator := (op: valuint): MPInteger; inline;
  1474. operator := (op: valuint): MPRational; inline;
  1475. operator < (op1: MPFloat; op2: MPFloat): boolean; inline;
  1476. operator < (op1: MPInteger; op2: MPInteger): boolean; inline;
  1477. operator < (op1: MPRational; op2: MPRational): boolean; inline;
  1478. operator <= (op1: MPFloat; op2: MPFloat): boolean; inline;
  1479. operator <= (op1: MPInteger; op2: MPInteger): boolean; inline;
  1480. operator <= (op1: MPRational; op2: MPRational): boolean; inline;
  1481. operator > (op1: MPFloat; op2: MPFloat): boolean; inline;
  1482. operator > (op1: MPInteger; op2: MPInteger): boolean; inline;
  1483. operator > (op1: MPRational; op2: MPRational): boolean; inline;
  1484. operator >= (op1: MPFloat; op2: MPFloat): boolean; inline;
  1485. operator >= (op1: MPInteger; op2: MPInteger): boolean; inline;
  1486. operator >= (op1: MPRational; op2: MPRational): boolean; inline;
  1487. // compiler doesn't like theese
  1488. // operator = (op1: MPFloat; op2: MPFloat): boolean; inline;
  1489. // operator = (op1: MPInteger; op2: MPInteger): boolean; inline;
  1490. // operator = (op1: MPRational; op2: MPRational): boolean; inline;
  1491. implementation
  1492. uses
  1493. math;
  1494. {$ifndef NO_GMP_GLOBVARS}
  1495. var
  1496. __gmp_bits_per_limb: longint; cvar; external;
  1497. __gmp_errno: longint; cvar; external;
  1498. __gmp_version: pchar; cvar; external;
  1499. function bits_per_limb: longint;
  1500. begin
  1501. result := __gmp_bits_per_limb;
  1502. end;
  1503. function errno: longint;
  1504. begin
  1505. result := __gmp_errno;
  1506. end;
  1507. function version: string;
  1508. begin
  1509. result := __gmp_version;
  1510. end;
  1511. {$else NO_GMP_GLOBVARS}
  1512. function bits_per_limb: longint;
  1513. const BITS_PER_BYTE = 8;
  1514. begin
  1515. result := sizeof(mp_limb_t) * BITS_PER_BYTE;
  1516. end;
  1517. function errno: longint;
  1518. begin
  1519. result := 0;
  1520. end;
  1521. function version: string;
  1522. const NO_VER = '0.0.0';
  1523. begin
  1524. result := NO_VER;
  1525. end;
  1526. {$endif NO_GMP_GLOBVARS}
  1527. // ---- ext types ----
  1528. { TMPBase }
  1529. function TMPBase.refs: longint;
  1530. begin
  1531. result := frefcount;
  1532. end;
  1533. { TMPInteger }
  1534. function TMPInteger.ptr: mpz_ptr;
  1535. begin
  1536. result := @fmpz
  1537. end;
  1538. destructor TMPInteger.destroy;
  1539. begin
  1540. mpz_clear(fmpz);
  1541. inherited destroy;
  1542. end;
  1543. { TMPFloat }
  1544. function TMPFloat.ptr: mpf_ptr;
  1545. begin
  1546. result := @fmpf;
  1547. end;
  1548. destructor TMPFloat.destroy;
  1549. begin
  1550. mpf_clear(fmpf);
  1551. inherited destroy;
  1552. end;
  1553. { TMPRational }
  1554. function TMPRational.ptr: mpq_ptr;
  1555. begin
  1556. result := @fmpq;
  1557. end;
  1558. destructor TMPRational.destroy;
  1559. begin
  1560. mpq_clear(fmpq);
  1561. inherited destroy;
  1562. end;
  1563. { TMPRandState }
  1564. function TMPRandState.ptr: randstate_ptr;
  1565. begin
  1566. result := @frandstate;
  1567. end;
  1568. destructor TMPRandState.destroy;
  1569. begin
  1570. mp_randclear(frandstate);
  1571. inherited destroy;
  1572. end;
  1573. // --- helpers ----
  1574. function dest(var rop: MPInteger): mpz_ptr;
  1575. begin
  1576. if (not assigned(rop)) or (rop.refs > 1) then
  1577. z_init(rop);
  1578. result := rop.ptr;
  1579. end;
  1580. function dest(var rop: MPFloat): mpf_ptr;
  1581. begin
  1582. if (not assigned(rop)) or (rop.refs > 1) then
  1583. f_init(rop);
  1584. result := rop.ptr;
  1585. end;
  1586. function dest(var rop: MPRational): mpq_ptr;
  1587. begin
  1588. if (not assigned(rop)) or (rop.refs > 1) then
  1589. q_init(rop);
  1590. result := rop.ptr;
  1591. end;
  1592. function dest(var rop: MPRandState): randstate_ptr;
  1593. begin
  1594. if (not assigned(rop)) or (rop.refs > 1) then
  1595. randinit_default(rop);
  1596. result := rop.ptr;
  1597. end;
  1598. function src(var rop: MPInteger): mpz_ptr;
  1599. begin
  1600. if not assigned(rop) then
  1601. z_init(rop);
  1602. result := rop.ptr;
  1603. end;
  1604. function src(var rop: MPFloat): mpf_ptr;
  1605. begin
  1606. if not assigned(rop) then
  1607. f_init(rop);
  1608. result := rop.ptr;
  1609. end;
  1610. function src(var rop: MPRational): mpq_ptr;
  1611. begin
  1612. if not assigned(rop) then
  1613. q_init(rop);
  1614. result := rop.ptr;
  1615. end;
  1616. function src(var rop: MPRandState): randstate_ptr;
  1617. begin
  1618. if not assigned(rop) then
  1619. randinit_default(rop);
  1620. result := rop.ptr;
  1621. end;
  1622. procedure propagate_prec(var result, op: MPFloat);
  1623. begin
  1624. f_set_prec(result, f_get_prec(op));
  1625. end;
  1626. procedure propagate_prec(var result, op1, op2: MPFloat);
  1627. begin
  1628. f_set_prec(result, max(valsint(f_get_prec(op1)), f_get_prec(op2)));
  1629. end;
  1630. // --- ext bindings ----
  1631. procedure randinit_default(out state: MPRandState);
  1632. begin
  1633. state := TMPRandState.Create;
  1634. mp_randinit_default(state.ptr^);
  1635. end;
  1636. procedure randinit_lc_2exp(out state: MPRandState; var a: MPInteger; c, m2exp: valuint);
  1637. begin
  1638. state := TMPRandState.Create;
  1639. mp_randinit_lc_2exp(state.ptr^, src(a)^, c, m2exp);
  1640. end;
  1641. function randinit_lc_2exp_size(out state: MPRandState; size: sizeuint): boolean;
  1642. begin
  1643. state := TMPRandState.Create;
  1644. result := mp_randinit_lc_2exp_size(state.ptr^, size) <> 0;
  1645. end;
  1646. procedure randinit_mt(out state: MPRandState);
  1647. begin
  1648. state := TMPRandState.Create;
  1649. mp_randinit_mt(state.ptr^);
  1650. end;
  1651. procedure randinit_set(out rop: MPRandState; var op: MPRandState);
  1652. begin
  1653. rop := TMPRandState.Create;
  1654. mp_randinit_set(rop.ptr^, src(op)^);
  1655. end;
  1656. procedure randseed(var state: MPRandState; var seed: MPInteger);
  1657. begin
  1658. mp_randseed(dest(state)^, src(seed)^);
  1659. end;
  1660. procedure randseed_ui(var state: MPRandState; seed: valuint);
  1661. begin
  1662. mp_randseed_ui(dest(state)^, seed);
  1663. end;
  1664. procedure randclear(var state: MPRandState);
  1665. begin
  1666. state := nil;
  1667. end;
  1668. function urandomb_ui(var state: MPRandState; n: valuint): valuint;
  1669. begin
  1670. result := mp_urandomb_ui(dest(state)^, n);
  1671. end;
  1672. function urandomm_ui(var state: MPRandState; n: valuint): valuint;
  1673. begin
  1674. result := mp_urandomb_ui(dest(state)^, n);
  1675. end;
  1676. function z_realloc(var integer_: MPInteger; new_alloc: mp_size_t): pointer;
  1677. begin
  1678. result := mpz_realloc(dest(integer_)^, new_alloc);
  1679. end;
  1680. procedure z_abs(var rop, op: MPInteger);
  1681. begin
  1682. mpz_abs(dest(rop)^, src(op)^);
  1683. end;
  1684. function z_abs(var op: MPInteger): MPInteger;
  1685. begin
  1686. mpz_abs(dest(result)^, src(op)^);
  1687. end;
  1688. procedure z_add(var rop, op1, op2: MPInteger);
  1689. begin
  1690. mpz_add(dest(rop)^, src(op1)^, src(op2)^);
  1691. end;
  1692. function z_add(var op1, op2: MPInteger): MPInteger;
  1693. begin
  1694. mpz_add(dest(result)^, src(op1)^, src(op2)^);
  1695. end;
  1696. procedure z_add_ui(var rop, op1: MPInteger; op2: valuint);
  1697. begin
  1698. mpz_add_ui(dest(rop)^, src(op1)^, op2);
  1699. end;
  1700. function z_add_ui(var op1: MPInteger; op2: valuint): MPInteger;
  1701. begin
  1702. mpz_add_ui(dest(result)^, src(op1)^, op2);
  1703. end;
  1704. procedure z_addmul(var rop, op1, op2: MPInteger);
  1705. begin
  1706. mpz_addmul(dest(rop)^, src(op1)^, src(op2)^);
  1707. end;
  1708. procedure z_addmul_ui(var rop, op1: MPInteger; op2: valuint);
  1709. begin
  1710. mpz_addmul_ui(dest(rop)^, src(op1)^, op2);
  1711. end;
  1712. procedure z_and(var rop, op1, op2: MPInteger);
  1713. begin
  1714. mpz_and(dest(rop)^, src(op1)^, src(op2)^);
  1715. end;
  1716. function z_and(var op1, op2: MPInteger): MPInteger;
  1717. begin
  1718. mpz_and(dest(result)^, src(op1)^, src(op2)^);
  1719. end;
  1720. procedure z_bin_ui(var rop, n: MPInteger; k: valuint);
  1721. begin
  1722. mpz_bin_ui(dest(rop)^, src(n)^, k);
  1723. end;
  1724. function z_bin_ui(var n: MPInteger; k: valuint): MPInteger;
  1725. begin
  1726. mpz_bin_ui(dest(result)^, src(n)^, k);
  1727. end;
  1728. procedure z_bin_uiui(var rop: MPInteger; n, k: valuint);
  1729. begin
  1730. mpz_bin_uiui(dest(rop)^, n, k);
  1731. end;
  1732. function z_bin_uiui(n, k: valuint): MPInteger;
  1733. begin
  1734. mpz_bin_uiui(dest(result)^, n, k);
  1735. end;
  1736. procedure z_cdiv_q(var q, n, d: MPInteger);
  1737. begin
  1738. mpz_cdiv_q(dest(q)^, src(n)^, src(d)^);
  1739. end;
  1740. function z_cdiv_q(var n, d: MPInteger): MPInteger;
  1741. begin
  1742. mpz_cdiv_q(dest(result)^, src(n)^, src(d)^);
  1743. end;
  1744. procedure z_cdiv_q_2exp(var q, n: MPInteger; b: valuint);
  1745. begin
  1746. mpz_cdiv_q_2exp(dest(q)^, src(n)^, b);
  1747. end;
  1748. function z_cdiv_q_2exp(var n: MPInteger; b: valuint): MPInteger;
  1749. begin
  1750. mpz_cdiv_q_2exp(dest(result)^, src(n)^, b);
  1751. end;
  1752. function z_cdiv_q_ui(var q, n: MPInteger; d: valuint): valuint;
  1753. begin
  1754. result := mpz_cdiv_q_ui(dest(q)^, src(n)^, d);
  1755. end;
  1756. procedure z_cdiv_qr(var q, r, n, d: MPInteger);
  1757. begin
  1758. mpz_cdiv_qr(dest(q)^, dest(r)^, src(n)^, src(d)^);
  1759. end;
  1760. function z_cdiv_qr_ui(var q, r, n: MPInteger; d: valuint): valuint;
  1761. begin
  1762. result := mpz_cdiv_qr_ui(dest(q)^, dest(r)^, src(n)^, d);
  1763. end;
  1764. procedure z_cdiv_r(var r, n, d: MPInteger);
  1765. begin
  1766. mpz_cdiv_r(dest(r)^, src(n)^, src(d)^);
  1767. end;
  1768. function z_cdiv_r(var n, d: MPInteger): MPInteger;
  1769. begin
  1770. mpz_cdiv_r(dest(result)^, src(n)^, src(d)^);
  1771. end;
  1772. procedure z_cdiv_r_2exp(var r, n: MPInteger; b: valuint);
  1773. begin
  1774. mpz_cdiv_r_2exp(dest(r)^, src(n)^, b);
  1775. end;
  1776. function z_cdiv_r_2exp(var n: MPInteger; b: valuint): MPInteger;
  1777. begin
  1778. mpz_cdiv_r_2exp(dest(result)^, src(n)^, b);
  1779. end;
  1780. function z_cdiv_r_ui(var r, n: MPInteger; d: valuint): valuint;
  1781. begin
  1782. result := mpz_cdiv_r_ui(dest(r)^, src(n)^, d);
  1783. end;
  1784. function z_cdiv_ui(var n: MPInteger; d: valuint): valuint;
  1785. begin
  1786. result := mpz_cdiv_ui(src(n)^, d);
  1787. end;
  1788. procedure z_clear(var integer_: MPInteger);
  1789. begin
  1790. integer_ := nil;
  1791. end;
  1792. procedure z_clrbit(var rop: MPInteger; bit_index: valuint);
  1793. begin
  1794. mpz_clrbit(dest(rop)^, bit_index);
  1795. end;
  1796. function z_cmp(var op1, op2: MPInteger): longint;
  1797. begin
  1798. result := mpz_cmp(src(op1)^, src(op2)^);
  1799. end;
  1800. function z_cmp_d(var op1: MPInteger; op2: double): longint;
  1801. begin
  1802. result := mpz_cmp_d(src(op1)^, op2);
  1803. end;
  1804. function z_cmp_si(var op1: MPInteger; op2: valsint): longint;
  1805. begin
  1806. result := mpz_cmp_si(src(op1)^, op2);
  1807. end;
  1808. function z_cmp_ui(var op1: MPInteger; op2: valuint): longint;
  1809. begin
  1810. result := mpz_cmp_ui(src(op1)^, op2);
  1811. end;
  1812. function z_cmpabs(var op1, op2: MPInteger): longint;
  1813. begin
  1814. result := mpz_cmpabs(src(op1)^, src(op2)^);
  1815. end;
  1816. function z_cmpabs_d(var op1: MPInteger; op2: double): longint;
  1817. begin
  1818. result := mpz_cmpabs_d(src(op1)^, op2);
  1819. end;
  1820. function z_cmpabs_ui(var op1: MPInteger; op2: valuint): longint;
  1821. begin
  1822. result := mpz_cmpabs_ui(src(op1)^, op2);
  1823. end;
  1824. procedure z_com(var rop, op: MPInteger);
  1825. begin
  1826. mpz_com(dest(rop)^, src(op)^);
  1827. end;
  1828. function z_com(var op: MPInteger): MPInteger;
  1829. begin
  1830. mpz_com(dest(result)^, src(op)^);
  1831. end;
  1832. procedure z_combit(var rop: MPInteger; bit_index: valuint);
  1833. begin
  1834. mpz_combit(dest(rop)^, bit_index);
  1835. end;
  1836. function z_congruent_p(var n, c, d: MPInteger): boolean;
  1837. begin
  1838. result := mpz_congruent_p(src(n)^, src(c)^, src(d)^) <> 0;
  1839. end;
  1840. function z_congruent_2exp_p(var n, c: MPInteger; b: valuint): boolean;
  1841. begin
  1842. result := mpz_congruent_2exp_p(src(n)^, src(c)^, b) <> 0;
  1843. end;
  1844. function z_congruent_ui_p(var n: MPInteger; c, d: valuint): boolean;
  1845. begin
  1846. result := mpz_congruent_ui_p(src(n)^, c, d) <> 0;
  1847. end;
  1848. procedure z_divexact(var q, n, d: MPInteger);
  1849. begin
  1850. mpz_divexact(dest(q)^, src(n)^, src(d)^);
  1851. end;
  1852. function z_divexact(var n, d: MPInteger): MPInteger;
  1853. begin
  1854. mpz_divexact(dest(result)^, src(n)^, src(d)^);
  1855. end;
  1856. procedure z_divexact_ui(var q, n: MPInteger; d: valuint);
  1857. begin
  1858. mpz_divexact_ui(dest(q)^, src(n)^, d);
  1859. end;
  1860. function z_divexact_ui(var n: MPInteger; d: valuint): MPInteger;
  1861. begin
  1862. mpz_divexact_ui(dest(result)^, src(n)^, d);
  1863. end;
  1864. function z_divisible_p(var n, d: MPInteger): boolean;
  1865. begin
  1866. result := mpz_divisible_p(src(n)^, src(d)^) <> 0;
  1867. end;
  1868. function z_divisible_ui_p(var n: MPInteger; d: valuint): boolean;
  1869. begin
  1870. result := mpz_divisible_ui_p(src(n)^, d) <> 0;
  1871. end;
  1872. function z_divisible_2exp_p(var n: MPInteger; b: valuint): boolean;
  1873. begin
  1874. result := mpz_divisible_2exp_p(src(n)^, b) <> 0;
  1875. end;
  1876. function z_export(out buf; out countp: sizeuint; order: longint; size: sizeuint; endian: longint; nails: sizeuint; var op: MPInteger): pointer;
  1877. begin
  1878. result := mpz_export(buf, countp, order, size, endian, nails, src(op)^);
  1879. end;
  1880. procedure z_fac_ui(var rop: MPInteger; op: valuint);
  1881. begin
  1882. mpz_fac_ui(dest(rop)^, op);
  1883. end;
  1884. function z_fac_ui(op: valuint): MPInteger;
  1885. begin
  1886. mpz_fac_ui(dest(result)^, op);
  1887. end;
  1888. procedure z_fdiv_q(var q, n, d: MPInteger);
  1889. begin
  1890. mpz_fdiv_q(dest(q)^, src(n)^, src(d)^);
  1891. end;
  1892. function z_fdiv_q(var n, d: MPInteger): MPInteger;
  1893. begin
  1894. mpz_fdiv_q(dest(result)^, src(n)^, src(d)^);
  1895. end;
  1896. procedure z_fdiv_q_2exp(var q, n: MPInteger; b: valuint);
  1897. begin
  1898. mpz_fdiv_q_2exp(dest(q)^, src(n)^, b);
  1899. end;
  1900. function z_fdiv_q_2exp(var n: MPInteger; b: valuint): MPInteger;
  1901. begin
  1902. mpz_fdiv_q_2exp(dest(result)^, src(n)^, b);
  1903. end;
  1904. function z_fdiv_q_ui(var q, n: MPInteger; d: valuint): valuint;
  1905. begin
  1906. result := mpz_fdiv_q_ui(dest(q)^, src(n)^, d);
  1907. end;
  1908. procedure z_fdiv_qr(var q, r, n, d: MPInteger);
  1909. begin
  1910. mpz_fdiv_qr(dest(q)^, dest(r)^, src(n)^, src(d)^);
  1911. end;
  1912. function z_fdiv_qr_ui(var q, r, n: MPInteger; d: valuint): valuint;
  1913. begin
  1914. result := mpz_fdiv_qr_ui(dest(q)^, dest(r)^, src(n)^, d);
  1915. end;
  1916. procedure z_fdiv_r(var r, n, d: MPInteger);
  1917. begin
  1918. mpz_fdiv_r(dest(r)^, src(n)^, src(d)^);
  1919. end;
  1920. function z_fdiv_r(var n, d: MPInteger): MPInteger;
  1921. begin
  1922. mpz_fdiv_r(dest(result)^, src(n)^, src(d)^);
  1923. end;
  1924. procedure z_fdiv_r_2exp(var r, n: MPInteger; b: valuint);
  1925. begin
  1926. mpz_fdiv_r_2exp(dest(r)^, src(n)^, b);
  1927. end;
  1928. function z_fdiv_r_2exp(var n: MPInteger; b: valuint): MPInteger;
  1929. begin
  1930. mpz_fdiv_r_2exp(dest(result)^, src(n)^, b);
  1931. end;
  1932. function z_fdiv_r_ui(var r, n: MPInteger; d: valuint): valuint;
  1933. begin
  1934. result := mpz_fdiv_r_ui(dest(r)^, src(n)^, d);
  1935. end;
  1936. function z_fdiv_ui(var n: MPInteger; d: valuint): valuint;
  1937. begin
  1938. result := mpz_fdiv_ui(src(n)^, d);
  1939. end;
  1940. procedure z_fib_ui(var fn: MPInteger; n: valuint);
  1941. begin
  1942. mpz_fib_ui(dest(fn)^, n);
  1943. end;
  1944. function z_fib_ui(n: valuint): MPInteger;
  1945. begin
  1946. mpz_fib_ui(dest(result)^, n);
  1947. end;
  1948. procedure z_fib2_ui(var fn, fnsub1: MPInteger; n: valuint);
  1949. begin
  1950. mpz_fib2_ui(dest(fn)^, dest(fnsub1)^, n);
  1951. end;
  1952. function z_fib2_ui(var fnsub1: MPInteger; n: valuint): MPInteger;
  1953. begin
  1954. mpz_fib2_ui(dest(result)^, dest(fnsub1)^, n);
  1955. end;
  1956. function z_fits_sint_p(var op: MPInteger): boolean;
  1957. begin
  1958. result := mpz_fits_sint_p(src(op)^) <> 0;
  1959. end;
  1960. function z_fits_slong_p(var op: MPInteger): boolean;
  1961. begin
  1962. result := mpz_fits_slong_p(src(op)^) <> 0;
  1963. end;
  1964. function z_fits_sshort_p(var op: MPInteger): boolean;
  1965. begin
  1966. result := mpz_fits_sshort_p(src(op)^) <> 0;
  1967. end;
  1968. function z_fits_uint_p(var op: MPInteger): boolean;
  1969. begin
  1970. result := mpz_fits_uint_p(src(op)^) <> 0;
  1971. end;
  1972. function z_fits_ulong_p(var op: MPInteger): boolean;
  1973. begin
  1974. result := mpz_fits_ulong_p(src(op)^) <> 0;
  1975. end;
  1976. function z_fits_ushort_p(var op: MPInteger): boolean;
  1977. begin
  1978. result := mpz_fits_ushort_p(src(op)^) <> 0;
  1979. end;
  1980. procedure z_gcd(var rop, op1, op2: MPInteger);
  1981. begin
  1982. mpz_gcd(dest(rop)^, src(op1)^, src(op2)^);
  1983. end;
  1984. function z_gcd(var op1, op2: MPInteger): MPInteger;
  1985. begin
  1986. mpz_gcd(dest(result)^, src(op1)^, src(op2)^);
  1987. end;
  1988. function z_gcd_ui(var rop, op1: MPInteger; op2: valuint): valuint;
  1989. begin
  1990. result := mpz_gcd_ui(dest(rop)^, src(op1)^, op2)
  1991. end;
  1992. procedure z_gcdext(var g, s, t, a, b: MPInteger);
  1993. begin
  1994. mpz_gcdext(dest(g)^, dest(s)^, dest(t)^, src(a)^, src(b)^);
  1995. end;
  1996. function z_get_d(var op: MPInteger): double;
  1997. begin
  1998. result := mpz_get_d(src(op)^);
  1999. end;
  2000. function z_get_d_2exp(out exp: valsint; var op: MPInteger): double;
  2001. begin
  2002. result := mpz_get_d_2exp(exp, src(op)^);
  2003. end;
  2004. function z_get_si(op: MPInteger): valsint;
  2005. begin
  2006. result := mpz_get_si(src(op)^);
  2007. end;
  2008. function z_get_str(base: longint; var op: MPInteger): string;
  2009. var p: pchar;
  2010. begin
  2011. p := mpz_get_str(nil, base, src(op)^);
  2012. result := p;
  2013. freemem(p);
  2014. end;
  2015. function z_get_str(str: pchar; base: longint; var op: MPInteger): pchar;
  2016. begin
  2017. result := mpz_get_str(str, base, src(op)^);
  2018. end;
  2019. function z_get_ui(op: MPInteger): valuint;
  2020. begin
  2021. result := mpz_get_ui(src(op)^);
  2022. end;
  2023. function z_getlimbn(var op: MPInteger; n: mp_size_t): mp_limb_t;
  2024. begin
  2025. result := mpz_getlimbn(src(op)^, n);
  2026. end;
  2027. function z_hamdist(var op1, op2: MPInteger): valuint;
  2028. begin
  2029. result := mpz_hamdist(src(op1)^, src(op2)^);
  2030. end;
  2031. procedure z_import(var rop: MPInteger; count: sizeuint; order: longint; size: sizeuint; endian: longint; nails: sizeuint; const op);
  2032. begin
  2033. mpz_import(dest(rop)^, count, order, size, endian, nails, op);
  2034. end;
  2035. procedure z_init(out integer_: MPInteger);
  2036. begin
  2037. integer_ := TMPInteger.Create;
  2038. mpz_init(integer_.ptr^);
  2039. end;
  2040. procedure z_init2(out integer_: MPInteger; n: valuint);
  2041. begin
  2042. integer_ := TMPInteger.Create;
  2043. mpz_init2(integer_.ptr^, n);
  2044. end;
  2045. procedure z_init_set(out rop: MPInteger; var op: MPInteger);
  2046. begin
  2047. rop := TMPInteger.Create;
  2048. mpz_init_set(rop.ptr^, src(op)^);
  2049. end;
  2050. procedure z_init_set_d(out rop: MPInteger; op: double);
  2051. begin
  2052. rop := TMPInteger.Create;
  2053. mpz_init_set_d(rop.ptr^, op);
  2054. end;
  2055. procedure z_init_set_si(out rop: MPInteger; op: valsint);
  2056. begin
  2057. rop := TMPInteger.Create;
  2058. mpz_init_set_si(rop.ptr^, op);
  2059. end;
  2060. function z_init_set_str(out rop: MPInteger; str: string; base: longint): boolean;
  2061. begin
  2062. rop := TMPInteger.Create;
  2063. result := mpz_set_str(rop.ptr^, pchar(str), base) = 0;
  2064. end;
  2065. procedure z_init_set_ui(out rop: MPInteger; op: valuint);
  2066. begin
  2067. rop := TMPInteger.Create;
  2068. mpz_init_set_ui(rop.ptr^, op);
  2069. end;
  2070. function z_invert(var rop, op1, op2: MPInteger): longint;
  2071. begin
  2072. result := mpz_invert(dest(rop)^, src(op1)^, src(op2)^);
  2073. end;
  2074. procedure z_ior(var rop, op1, op2: MPInteger);
  2075. begin
  2076. mpz_ior(dest(rop)^, src(op1)^, src(op2)^);
  2077. end;
  2078. function z_ior(var op1, op2: MPInteger): MPInteger;
  2079. begin
  2080. mpz_ior(dest(result)^, src(op1)^, src(op2)^);
  2081. end;
  2082. function z_jacobi(var a, b: MPInteger): longint;
  2083. begin
  2084. result := mpz_jacobi(src(a)^, src(b)^);
  2085. end;
  2086. function z_kronecker_si(var a: MPInteger; b: valsint): longint;
  2087. begin
  2088. result := mpz_kronecker_si(src(a)^, b);
  2089. end;
  2090. function z_kronecker_ui(var a: MPInteger; b: valuint): longint;
  2091. begin
  2092. result := mpz_kronecker_ui(src(a)^, b);
  2093. end;
  2094. function z_si_kronecker(a: valsint; var b: MPInteger): longint;
  2095. begin
  2096. result := mpz_si_kronecker(a, src(b)^);
  2097. end;
  2098. function z_ui_kronecker(a: valuint; var b: MPInteger): longint;
  2099. begin
  2100. result := mpz_ui_kronecker(a, src(b)^);
  2101. end;
  2102. procedure z_lcm(var rop, op1, op2: MPInteger);
  2103. begin
  2104. mpz_lcm(dest(rop)^, src(op1)^, src(op2)^);
  2105. end;
  2106. function z_lcm(var op1, op2: MPInteger): MPInteger;
  2107. begin
  2108. mpz_lcm(dest(result)^, src(op1)^, src(op2)^);
  2109. end;
  2110. procedure z_lcm_ui(var rop, op1: MPInteger; op2: valuint);
  2111. begin
  2112. mpz_lcm_ui(dest(rop)^, src(op1)^, op2);
  2113. end;
  2114. function z_lcm_ui(var op1: MPInteger; op2: valuint): MPInteger;
  2115. begin
  2116. mpz_lcm_ui(dest(result)^, src(op1)^, op2);
  2117. end;
  2118. procedure z_lucnum_ui(var ln: MPInteger; n: valuint);
  2119. begin
  2120. mpz_lucnum_ui(dest(ln)^, n);
  2121. end;
  2122. function z_lucnum_ui(n: valuint): MPInteger;
  2123. begin
  2124. mpz_lucnum_ui(dest(result)^, n);
  2125. end;
  2126. procedure z_lucnum2_ui(var ln, lnsub1: MPInteger; n: valuint);
  2127. begin
  2128. mpz_lucnum2_ui(dest(ln)^, dest(lnsub1)^, n);
  2129. end;
  2130. function z_lucnum2_ui(var lnsub1: MPInteger; n: valuint): MPInteger;
  2131. begin
  2132. mpz_lucnum2_ui(dest(result)^, dest(lnsub1)^, n);
  2133. end;
  2134. procedure z_mod(var r, n, d: MPInteger);
  2135. begin
  2136. mpz_mod(dest(r)^, src(n)^, src(d)^);
  2137. end;
  2138. function z_mod(var n, d: MPInteger): MPInteger;
  2139. begin
  2140. mpz_mod(dest(result)^, src(n)^, src(d)^);
  2141. end;
  2142. procedure z_mul(var rop, op1, op2: MPInteger);
  2143. begin
  2144. mpz_mul(dest(rop)^, src(op1)^, src(op2)^);
  2145. end;
  2146. function z_mul(var op1, op2: MPInteger): MPInteger;
  2147. begin
  2148. mpz_mul(dest(result)^, src(op1)^, src(op2)^);
  2149. end;
  2150. procedure z_mul_2exp(var rop, op1: MPInteger; op2: valuint);
  2151. begin
  2152. mpz_mul_2exp(dest(rop)^, src(op1)^, op2);
  2153. end;
  2154. function z_mul_2exp(var op1: MPInteger; op2: valuint): MPInteger;
  2155. begin
  2156. mpz_mul_2exp(dest(result)^, src(op1)^, op2);
  2157. end;
  2158. procedure z_mul_si(var rop, op1: MPInteger; op2: valsint);
  2159. begin
  2160. mpz_mul_si(dest(rop)^, src(op1)^, op2);
  2161. end;
  2162. function z_mul_si(var op1: MPInteger; op2: valsint): MPInteger;
  2163. begin
  2164. mpz_mul_si(dest(result)^, src(op1)^, op2);
  2165. end;
  2166. procedure z_mul_ui(var rop, op1: MPInteger; op2: valuint);
  2167. begin
  2168. mpz_mul_ui(dest(rop)^, src(op1)^, op2);
  2169. end;
  2170. function z_mul_ui(var op1: MPInteger; op2: valuint): MPInteger;
  2171. begin
  2172. mpz_mul_ui(dest(result)^, src(op1)^, op2);
  2173. end;
  2174. procedure z_neg(var rop, op: MPInteger);
  2175. begin
  2176. mpz_neg(dest(rop)^, src(op)^);
  2177. end;
  2178. function z_neg(var op: MPInteger): MPInteger;
  2179. begin
  2180. mpz_neg(dest(result)^, src(op)^);
  2181. end;
  2182. procedure z_nextprime(var rop, op: MPInteger);
  2183. begin
  2184. mpz_nextprime(dest(rop)^, src(op)^);
  2185. end;
  2186. function z_nextprime(var op: MPInteger): MPInteger;
  2187. begin
  2188. mpz_nextprime(dest(result)^, src(op)^);
  2189. end;
  2190. function z_perfect_power_p(var op: MPInteger): boolean;
  2191. begin
  2192. result := mpz_perfect_power_p(src(op)^) <> 0;
  2193. end;
  2194. function z_perfect_square_p(var op: MPInteger): boolean;
  2195. begin
  2196. result := mpz_perfect_square_p(src(op)^) <> 0;
  2197. end;
  2198. function z_popcount(var op: MPInteger): valuint;
  2199. begin
  2200. result := mpz_popcount(src(op)^);
  2201. end;
  2202. procedure z_pow_ui(var rop, base: MPInteger; exp: valuint);
  2203. begin
  2204. mpz_pow_ui(dest(rop)^, src(base)^, exp);
  2205. end;
  2206. function z_pow_ui(var base: MPInteger; exp: valuint): MPInteger;
  2207. begin
  2208. mpz_pow_ui(dest(result)^, src(base)^, exp);
  2209. end;
  2210. procedure z_powm(var rop, base, exp, mod_: MPInteger);
  2211. begin
  2212. mpz_powm(dest(rop)^, src(base)^, src(exp)^, src(mod_)^);
  2213. end;
  2214. function z_powm(var base, exp, mod_: MPInteger): MPInteger;
  2215. begin
  2216. mpz_powm(dest(result)^, src(base)^, src(exp)^, src(mod_)^);
  2217. end;
  2218. procedure z_powm_ui(var rop, base: MPInteger; exp: valuint; var mod_: MPInteger);
  2219. begin
  2220. mpz_powm_ui(dest(rop)^, src(base)^, exp, src(mod_)^);
  2221. end;
  2222. function z_powm_ui(var base: MPInteger; exp: valuint; var mod_: MPInteger): MPInteger;
  2223. begin
  2224. mpz_powm_ui(dest(result)^, src(base)^, exp, src(mod_)^);
  2225. end;
  2226. function z_probab_prime_p(var n: MPInteger; reps: longint): longint;
  2227. begin
  2228. result := mpz_probab_prime_p(src(n)^, reps);
  2229. end;
  2230. procedure z_realloc2(var integer_: MPInteger; n: valuint);
  2231. begin
  2232. mpz_realloc2(dest(integer_)^, n);
  2233. end;
  2234. function z_remove(var rop, op, f: MPInteger): valuint;
  2235. begin
  2236. result := mpz_remove(dest(rop)^, src(op)^, src(f)^);
  2237. end;
  2238. function z_root(var rop, op: MPInteger; n: valuint): boolean;
  2239. begin
  2240. result := mpz_root(dest(rop)^, src(op)^, n) <> 0;
  2241. end;
  2242. procedure z_rootrem(var root, rem, u: MPInteger; n: valuint);
  2243. begin
  2244. mpz_rootrem(dest(root)^, dest(rem)^, src(u)^, n);
  2245. end;
  2246. procedure z_rrandomb(var rop: MPInteger; var state: MPRandState; n: valuint);
  2247. begin
  2248. mpz_rrandomb(dest(rop)^, src(state)^, n);
  2249. end;
  2250. function z_rrandomb(var state: MPRandState; n: valuint): MPInteger;
  2251. begin
  2252. mpz_rrandomb(dest(result)^, src(state)^, n);
  2253. end;
  2254. function z_scan0(var op: MPInteger; starting_bit: valuint): valuint;
  2255. begin
  2256. result := mpz_scan0(src(op)^, starting_bit);
  2257. end;
  2258. function z_scan1(var op: MPInteger; starting_bit: valuint): valuint;
  2259. begin
  2260. result := mpz_scan1(src(op)^, starting_bit);
  2261. end;
  2262. procedure z_set(var rop, op: MPInteger);
  2263. begin
  2264. mpz_set(dest(rop)^, src(op)^);
  2265. end;
  2266. procedure z_set_d(var rop: MPInteger; op: double);
  2267. begin
  2268. mpz_set_d(dest(rop)^, op);
  2269. end;
  2270. procedure z_set_f(var rop: MPInteger; var op: MPFloat);
  2271. begin
  2272. mpz_set_f(dest(rop)^, src(op)^);
  2273. end;
  2274. procedure z_set_q(var rop: MPInteger; var op: MPRational);
  2275. begin
  2276. mpz_set_q(dest(rop)^, src(op)^);
  2277. end;
  2278. procedure z_set_si(var rop: MPInteger; op: valsint);
  2279. begin
  2280. mpz_set_si(dest(rop)^, op);
  2281. end;
  2282. function z_set_str(var rop: MPInteger; str: string; base: longint): boolean;
  2283. begin
  2284. result := mpz_set_str(dest(rop)^, pchar(str), base) = 0;
  2285. end;
  2286. procedure z_set_ui(var rop: MPInteger; op: valuint);
  2287. begin
  2288. mpz_set_ui(dest(rop)^, op);
  2289. end;
  2290. procedure z_setbit(var rop: MPInteger; bit_index: valuint);
  2291. begin
  2292. mpz_setbit(dest(rop)^, bit_index);
  2293. end;
  2294. function z_size(var op: MPInteger): sizeuint;
  2295. begin
  2296. result := mpz_size(src(op)^);
  2297. end;
  2298. function z_sizeinbase(var op: MPInteger; base: longint): sizeuint;
  2299. begin
  2300. result := mpz_sizeinbase(src(op)^, base);
  2301. end;
  2302. procedure z_sqrt(var rop, op: MPInteger);
  2303. begin
  2304. mpz_sqrt(dest(rop)^, src(op)^);
  2305. end;
  2306. function z_sqrt(var op: MPInteger): MPInteger;
  2307. begin
  2308. mpz_sqrt(dest(result)^, src(op)^);
  2309. end;
  2310. procedure z_sqrtrem(var rop1, rop2, op: MPInteger);
  2311. begin
  2312. mpz_sqrtrem(dest(rop1)^, dest(rop2)^, src(op)^);
  2313. end;
  2314. procedure z_sub(var rop, op1, op2: MPInteger);
  2315. begin
  2316. mpz_sub(dest(rop)^, src(op1)^, src(op2)^);
  2317. end;
  2318. function z_sub(var op1, op2: MPInteger): MPInteger;
  2319. begin
  2320. mpz_sub(dest(result)^, src(op1)^, src(op2)^);
  2321. end;
  2322. procedure z_sub_ui(var rop, op1: MPInteger; op2: valuint);
  2323. begin
  2324. mpz_sub_ui(dest(rop)^, src(op1)^, op2);
  2325. end;
  2326. function z_sub_ui(var op1: MPInteger; op2: valuint): MPInteger;
  2327. begin
  2328. mpz_sub_ui(dest(result)^, src(op1)^, op2);
  2329. end;
  2330. procedure z_ui_sub(var rop: MPInteger; op1: valuint; var op2: MPInteger);
  2331. begin
  2332. mpz_ui_sub(dest(rop)^, op1, src(op2)^);
  2333. end;
  2334. function z_ui_sub(op1: valuint; var op2: MPInteger): MPInteger;
  2335. begin
  2336. mpz_ui_sub(dest(result)^, op1, src(op2)^);
  2337. end;
  2338. procedure z_submul(var rop, op1, op2: MPInteger);
  2339. begin
  2340. mpz_submul(dest(rop)^, src(op1)^, src(op2)^);
  2341. end;
  2342. procedure z_submul_ui(var rop, op1: MPInteger; op2: valuint);
  2343. begin
  2344. mpz_submul_ui(dest(rop)^, src(op1)^, op2);
  2345. end;
  2346. procedure z_swap(var rop1, rop2: MPInteger);
  2347. begin
  2348. mpz_swap(dest(rop1)^, dest(rop2)^);
  2349. end;
  2350. procedure z_tdiv_q(var q, n, d: MPInteger);
  2351. begin
  2352. mpz_tdiv_q(dest(q)^, src(n)^, src(d)^);
  2353. end;
  2354. function z_tdiv_q(var n, d: MPInteger): MPInteger;
  2355. begin
  2356. mpz_tdiv_q(dest(result)^, src(n)^, src(d)^);
  2357. end;
  2358. procedure z_tdiv_q_2exp(var q, n: MPInteger; b: valuint);
  2359. begin
  2360. mpz_tdiv_q_2exp(dest(q)^, src(n)^, b);
  2361. end;
  2362. function z_tdiv_q_2exp(var n: MPInteger; b: valuint): MPInteger;
  2363. begin
  2364. mpz_tdiv_q_2exp(dest(result)^, src(n)^, b);
  2365. end;
  2366. function z_tdiv_q_ui(var q, n: MPInteger; d: valuint): valuint;
  2367. begin
  2368. result := mpz_tdiv_q_ui(dest(q)^, src(n)^, d);
  2369. end;
  2370. procedure z_tdiv_qr(var q, r, n, d: MPInteger);
  2371. begin
  2372. mpz_tdiv_qr(dest(q)^, dest(r)^, src(n)^, src(d)^);
  2373. end;
  2374. function z_tdiv_qr_ui(var q, r, n: MPInteger; d: valuint): valuint;
  2375. begin
  2376. result := mpz_tdiv_qr_ui(dest(q)^, dest(r)^, src(n)^, d);
  2377. end;
  2378. procedure z_tdiv_r(var r, n, d: MPInteger);
  2379. begin
  2380. mpz_tdiv_r(dest(r)^, src(n)^, src(d)^);
  2381. end;
  2382. function z_tdiv_r(var n, d: MPInteger): MPInteger;
  2383. begin
  2384. mpz_tdiv_r(dest(result)^, src(n)^, src(d)^);
  2385. end;
  2386. procedure z_tdiv_r_2exp(var r, n: MPInteger; b: valuint);
  2387. begin
  2388. mpz_tdiv_r_2exp(dest(r)^, src(n)^, b);
  2389. end;
  2390. function z_tdiv_r_2exp(var n: MPInteger; b: valuint): MPInteger;
  2391. begin
  2392. mpz_tdiv_r_2exp(dest(result)^, src(n)^, b);
  2393. end;
  2394. function z_tdiv_r_ui(var r, n: MPInteger; d: valuint): valuint;
  2395. begin
  2396. result := mpz_tdiv_r_ui(dest(r)^, src(n)^, d);
  2397. end;
  2398. function z_tdiv_ui(var n: MPInteger; d: valuint): valuint;
  2399. begin
  2400. result := mpz_tdiv_ui(src(n)^, d);
  2401. end;
  2402. function z_tstbit(var rop: MPInteger; bit_index: valuint): boolean;
  2403. begin
  2404. result := mpz_tstbit(src(rop)^, bit_index) <> 0;
  2405. end;
  2406. procedure z_ui_pow_ui(var rop: MPInteger; base, exp: valuint);
  2407. begin
  2408. mpz_ui_pow_ui(dest(rop)^, base, exp);
  2409. end;
  2410. function z_ui_pow_ui(base, exp: valuint): MPInteger;
  2411. begin
  2412. mpz_ui_pow_ui(dest(result)^, base, exp);
  2413. end;
  2414. procedure z_urandomb(var rop: MPInteger; var state: MPRandState; n: valuint);
  2415. begin
  2416. mpz_urandomb(dest(rop)^, src(state)^, n);
  2417. end;
  2418. function z_urandomb(var state: MPRandState; n: valuint): MPInteger;
  2419. begin
  2420. mpz_urandomb(dest(result)^, src(state)^, n);
  2421. end;
  2422. procedure z_urandomm(var rop: MPInteger; var state: MPRandState; var n: MPInteger);
  2423. begin
  2424. mpz_urandomm(dest(rop)^, src(state)^, src(n)^);
  2425. end;
  2426. function z_urandomm(var state: MPRandState; var n: MPInteger): MPInteger;
  2427. begin
  2428. mpz_urandomm(dest(result)^, src(state)^, src(n)^);
  2429. end;
  2430. procedure z_xor(var rop, op1, op2: MPInteger);
  2431. begin
  2432. mpz_xor(dest(rop)^, src(op1)^, src(op2)^);
  2433. end;
  2434. function z_xor(var op1, op2: MPInteger): MPInteger;
  2435. begin
  2436. mpz_xor(dest(result)^, src(op1)^, src(op2)^);
  2437. end;
  2438. procedure q_abs(var rop, op: MPRational);
  2439. begin
  2440. mpq_abs(dest(rop)^, src(op)^);
  2441. end;
  2442. function q_abs(var op: MPRational): MPRational;
  2443. begin
  2444. mpq_abs(dest(result)^, src(op)^);
  2445. end;
  2446. procedure q_add(var sum, addend1, addend2: MPRational);
  2447. begin
  2448. mpq_add(dest(sum)^, src(addend1)^, src(addend2)^);
  2449. end;
  2450. function q_add(var addend1, addend2: MPRational): MPRational;
  2451. begin
  2452. mpq_add(dest(result)^, src(addend1)^, src(addend2)^);
  2453. end;
  2454. procedure q_canonicalize(var op: MPRational);
  2455. begin
  2456. mpq_canonicalize(dest(op)^);
  2457. end;
  2458. procedure q_clear(var rational_number: MPRational);
  2459. begin
  2460. rational_number := nil;
  2461. end;
  2462. function q_cmp(var op1, op2: MPRational): longint;
  2463. begin
  2464. result := mpq_cmp(src(op1)^, src(op2)^);
  2465. end;
  2466. function q_cmp_si(var op1: MPRational; num2: valsint; den2: valuint): longint;
  2467. begin
  2468. result := mpq_cmp_si(src(op1)^, num2, den2);
  2469. end;
  2470. function q_cmp_ui(var op1: MPRational; num2, den2: valuint): longint;
  2471. begin
  2472. result := mpq_cmp_ui(src(op1)^, num2, den2);
  2473. end;
  2474. procedure q_div(var quotient, dividend, divisor: MPRational);
  2475. begin
  2476. mpq_div(dest(quotient)^, src(dividend)^, src(divisor)^);
  2477. end;
  2478. function q_div(var dividend, divisor: MPRational): MPRational;
  2479. begin
  2480. mpq_div(dest(result)^, src(dividend)^, src(divisor)^);
  2481. end;
  2482. procedure q_div_2exp(var rop, op1: MPRational; op2: valuint);
  2483. begin
  2484. mpq_div_2exp(dest(rop)^, src(op1)^, op2);
  2485. end;
  2486. function q_div_2exp(var op1: MPRational; op2: valuint): MPRational;
  2487. begin
  2488. mpq_div_2exp(dest(result)^, src(op1)^, op2);
  2489. end;
  2490. function q_equal(var op1, op2: MPRational): boolean;
  2491. begin
  2492. result := mpq_equal(src(op1)^, src(op2)^) <> 0;
  2493. end;
  2494. procedure q_get_num(var numerator: MPInteger; var rational: MPRational);
  2495. begin
  2496. mpq_get_num(dest(numerator)^, src(rational)^);
  2497. end;
  2498. function q_get_num(var rational: MPRational): MPInteger;
  2499. begin
  2500. mpq_get_num(dest(result)^, src(rational)^);
  2501. end;
  2502. procedure q_get_den(var denominator: MPInteger; var rational: MPRational);
  2503. begin
  2504. mpq_get_den(dest(denominator)^, src(rational)^);
  2505. end;
  2506. function q_get_den(var rational: MPRational): MPInteger;
  2507. begin
  2508. mpq_get_den(dest(result)^, src(rational)^);
  2509. end;
  2510. function q_get_d(var op: MPRational): double;
  2511. begin
  2512. result := mpq_get_d(src(op)^);
  2513. end;
  2514. function q_get_str(base: longint; var op: MPRational): string;
  2515. var p: pchar;
  2516. begin
  2517. p := mpq_get_str(nil, base, src(op)^);
  2518. result := p;
  2519. freemem(p);
  2520. end;
  2521. function q_get_str(str: pchar; base: longint; var op: MPRational): pchar;
  2522. begin
  2523. result := mpq_get_str(str, base, src(op)^);
  2524. end;
  2525. procedure q_init(out dest_rational: MPRational);
  2526. begin
  2527. dest_rational := TMPRational.Create;
  2528. mpq_init(dest_rational.ptr^);
  2529. end;
  2530. procedure q_inv(var inverted_number, number: MPRational);
  2531. begin
  2532. mpq_inv(dest(inverted_number)^, src(number)^);
  2533. end;
  2534. function q_inv(var number: MPRational): MPRational;
  2535. begin
  2536. mpq_inv(dest(result)^, src(number)^);
  2537. end;
  2538. procedure q_mul(var product, multiplier, multiplicand: MPRational);
  2539. begin
  2540. mpq_mul(dest(product)^, src(multiplier)^, src(multiplicand)^);
  2541. end;
  2542. function q_mul(var multiplier, multiplicand: MPRational): MPRational;
  2543. begin
  2544. mpq_mul(dest(result)^, src(multiplier)^, src(multiplicand)^);
  2545. end;
  2546. procedure q_mul_2exp(var rop, op1: MPRational; op2: valuint);
  2547. begin
  2548. mpq_mul_2exp(dest(rop)^, src(op1)^, op2);
  2549. end;
  2550. function q_mul_2exp(var op1: MPRational; op2: valuint): MPRational;
  2551. begin
  2552. mpq_mul_2exp(dest(result)^, src(op1)^, op2);
  2553. end;
  2554. procedure q_neg(var negated_operand, operand: MPRational);
  2555. begin
  2556. mpq_neg(dest(negated_operand)^, src(operand)^);
  2557. end;
  2558. function q_neg(var operand: MPRational): MPRational;
  2559. begin
  2560. mpq_neg(dest(result)^, src(operand)^);
  2561. end;
  2562. procedure q_set(var rop, op: MPRational);
  2563. begin
  2564. mpq_set(dest(rop)^, src(op)^);
  2565. end;
  2566. procedure q_set_d(var rop: MPRational; op: double);
  2567. begin
  2568. mpq_set_d(dest(rop)^, op);
  2569. end;
  2570. procedure q_set_den(var rational: MPRational; var denominator: MPInteger);
  2571. begin
  2572. mpq_set_den(dest(rational)^, src(denominator)^);
  2573. end;
  2574. procedure q_set_f(var rop: MPRational; var op: MPFloat);
  2575. begin
  2576. mpq_set_f(dest(rop)^, src(op)^);
  2577. end;
  2578. procedure q_set_num(var rational: MPRational; var numerator: MPInteger);
  2579. begin
  2580. mpq_set_num(dest(rational)^, src(numerator)^);
  2581. end;
  2582. procedure q_set_si(var rop: MPRational; op1: valsint; op2: valuint);
  2583. begin
  2584. mpq_set_si(dest(rop)^, op1, op2);
  2585. end;
  2586. function q_set_str(var rop: MPRational; str: string; base: longint): boolean;
  2587. begin
  2588. result := mpq_set_str(dest(rop)^, pchar(str), base) = 0;
  2589. end;
  2590. procedure q_set_ui(var rop: MPRational; op1, op2: valuint);
  2591. begin
  2592. mpq_set_ui(dest(rop)^, op1, op2);
  2593. end;
  2594. procedure q_set_z(var rop: MPRational; var op: MPInteger);
  2595. begin
  2596. mpq_set_z(dest(rop)^, src(op)^);
  2597. end;
  2598. procedure q_sub(var difference, minuend, subtrahend: MPRational);
  2599. begin
  2600. mpq_sub(dest(difference)^, src(minuend)^, src(subtrahend)^);
  2601. end;
  2602. function q_sub(var minuend, subtrahend: MPRational): MPRational;
  2603. begin
  2604. mpq_sub(dest(result)^, src(minuend)^, src(subtrahend)^);
  2605. end;
  2606. procedure q_swap(var rop1, rop2: MPRational);
  2607. begin
  2608. mpq_swap(dest(rop1)^, dest(rop2)^);
  2609. end;
  2610. procedure f_abs(var rop, op: MPFloat);
  2611. begin
  2612. mpf_abs(dest(rop)^, src(op)^);
  2613. end;
  2614. function f_abs(var op: MPFloat): MPFloat;
  2615. begin
  2616. mpf_abs(dest(result)^, src(op)^);
  2617. end;
  2618. procedure f_add(var rop, op1, op2: MPFloat);
  2619. begin
  2620. mpf_add(dest(rop)^, src(op1)^, src(op2)^);
  2621. end;
  2622. function f_add(var op1, op2: MPFloat): MPFloat;
  2623. begin
  2624. mpf_add(dest(result)^, src(op1)^, src(op2)^);
  2625. end;
  2626. procedure f_add_ui(var rop, op1: MPFloat; op2: valuint);
  2627. begin
  2628. mpf_add_ui(dest(rop)^, src(op1)^, op2);
  2629. end;
  2630. function f_add_ui(var op1: MPFloat; op2: valuint): MPFloat;
  2631. begin
  2632. mpf_add_ui(dest(result)^, src(op1)^, op2);
  2633. end;
  2634. procedure f_ceil(var rop, op: MPFloat);
  2635. begin
  2636. mpf_ceil(dest(rop)^, src(op)^);
  2637. end;
  2638. function f_ceil(var op: MPFloat): MPFloat;
  2639. begin
  2640. mpf_ceil(dest(result)^, src(op)^);
  2641. end;
  2642. procedure f_clear(var x: MPFloat);
  2643. begin
  2644. x := nil;
  2645. end;
  2646. function f_cmp(var op1, op2: MPFloat): longint;
  2647. begin
  2648. result := mpf_cmp(src(op1)^, src(op2)^);
  2649. end;
  2650. function f_cmp_d(var op1: MPFloat; op2: double): longint;
  2651. begin
  2652. result := mpf_cmp_d(src(op1)^, op2);
  2653. end;
  2654. function f_cmp_si(var op1: MPFloat; op2: valsint): longint;
  2655. begin
  2656. result := mpf_cmp_si(src(op1)^, op2);
  2657. end;
  2658. function f_cmp_ui(var op1: MPFloat; op2: valuint): longint;
  2659. begin
  2660. result := mpf_cmp_ui(src(op1)^, op2);
  2661. end;
  2662. procedure f_div(var rop, op1, op2: MPFloat);
  2663. begin
  2664. mpf_div(dest(rop)^, src(op1)^, src(op2)^);
  2665. end;
  2666. function f_div(var op1, op2: MPFloat): MPFloat;
  2667. begin
  2668. mpf_div(dest(result)^, src(op1)^, src(op2)^);
  2669. end;
  2670. procedure f_div_2exp(var rop, op1: MPFloat; op2: valuint);
  2671. begin
  2672. mpf_div_2exp(dest(rop)^, src(op1)^, op2);
  2673. end;
  2674. function f_div_2exp(var op1: MPFloat; op2: valuint): MPFloat;
  2675. begin
  2676. mpf_div_2exp(dest(result)^, src(op1)^, op2);
  2677. end;
  2678. procedure f_div_ui(var rop, op1: MPFloat; op2: valuint);
  2679. begin
  2680. mpf_div_ui(dest(rop)^, src(op1)^, op2);
  2681. end;
  2682. function f_div_ui(var op1: MPFloat; op2: valuint): MPFloat;
  2683. begin
  2684. mpf_div_ui(dest(result)^, src(op1)^, op2);
  2685. end;
  2686. function f_eq(var op1, op2: MPFloat; op3: valuint): boolean;
  2687. begin
  2688. result := mpf_eq(src(op1)^, src(op2)^, op3) <> 0;
  2689. end;
  2690. function f_fits_sint_p(var op: MPFloat): boolean;
  2691. begin
  2692. result := mpf_fits_sint_p(src(op)^) <> 0;
  2693. end;
  2694. function f_fits_slong_p(var op: MPFloat): boolean;
  2695. begin
  2696. result := mpf_fits_slong_p(src(op)^) <> 0;
  2697. end;
  2698. function f_fits_sshort_p(var op: MPFloat): boolean;
  2699. begin
  2700. result := mpf_fits_sshort_p(src(op)^) <> 0;
  2701. end;
  2702. function f_fits_uint_p(var op: MPFloat): boolean;
  2703. begin
  2704. result := mpf_fits_uint_p(src(op)^) <> 0;
  2705. end;
  2706. function f_fits_ulong_p(var op: MPFloat): boolean;
  2707. begin
  2708. result := mpf_fits_ulong_p(src(op)^) <> 0;
  2709. end;
  2710. function f_fits_ushort_p(var op: MPFloat): boolean;
  2711. begin
  2712. result := mpf_fits_ushort_p(src(op)^) <> 0;
  2713. end;
  2714. procedure f_floor(var rop, op: MPFloat);
  2715. begin
  2716. mpf_floor(dest(rop)^, src(op)^);
  2717. end;
  2718. function f_floor(var op: MPFloat): MPFloat;
  2719. begin
  2720. mpf_floor(dest(result)^, src(op)^);
  2721. end;
  2722. function f_get_d(var op: MPFloat): double;
  2723. begin
  2724. result := mpf_get_d(src(op)^);
  2725. end;
  2726. function f_get_d_2exp(out exp: valsint; var op: MPFloat): double;
  2727. begin
  2728. result := mpf_get_d_2exp(exp, src(op)^);
  2729. end;
  2730. function f_get_default_prec: valuint;
  2731. begin
  2732. result := mpf_get_default_prec;
  2733. end;
  2734. function f_get_prec(var op: MPFloat): valuint;
  2735. begin
  2736. result := mpf_get_prec(src(op)^);
  2737. end;
  2738. function f_get_si(var op: MPFloat): valsint;
  2739. begin
  2740. result := mpf_get_si(src(op)^);
  2741. end;
  2742. function f_get_str(out exp: mp_exp_t; base: longint; ndigits: sizeuint; var op: MPFloat): string;
  2743. var p: pchar;
  2744. begin
  2745. p := mpf_get_str(nil, exp, base, ndigits, src(op)^);
  2746. result := p;
  2747. freemem(p);
  2748. end;
  2749. function f_get_str(str: pchar; out exp: mp_exp_t; base: longint; ndigits: sizeuint; var op: MPFloat): pchar;
  2750. begin
  2751. result := mpf_get_str(str, exp, base, ndigits, src(op)^);
  2752. end;
  2753. function f_get_ui(var op: MPFloat): valuint;
  2754. begin
  2755. result := mpf_get_ui(src(op)^);
  2756. end;
  2757. procedure f_init(out x: MPFloat);
  2758. begin
  2759. x := TMPFloat.Create;
  2760. mpf_init(x.ptr^);
  2761. end;
  2762. procedure f_init2(out x: MPFloat; prec: valuint);
  2763. begin
  2764. x := TMPFloat.Create;
  2765. mpf_init2(x.ptr^, prec);
  2766. end;
  2767. procedure f_init_set(out rop: MPFloat; var op: MPFloat);
  2768. begin
  2769. rop := TMPFloat.Create;
  2770. mpf_init_set(rop.ptr^, src(op)^);
  2771. end;
  2772. procedure f_init_set_d(out rop: MPFloat; op: double);
  2773. begin
  2774. rop := TMPFloat.Create;
  2775. mpf_init_set_d(rop.ptr^, op);
  2776. end;
  2777. procedure f_init_set_si(out rop: MPFloat; op: valsint);
  2778. begin
  2779. rop := TMPFloat.Create;
  2780. mpf_init_set_si(rop.ptr^, op);
  2781. end;
  2782. function f_init_set_str(out rop: MPFloat; str: string; base: longint): boolean;
  2783. begin
  2784. rop := TMPFloat.Create;
  2785. result := mpf_init_set_str(rop.ptr^, pchar(str), base) = 0;
  2786. end;
  2787. procedure f_init_set_ui(out rop: MPFloat; op: valuint);
  2788. begin
  2789. rop := TMPFloat.Create;
  2790. mpf_init_set_ui(rop.ptr^, op);
  2791. end;
  2792. function f_integer_p(var op: MPFloat): boolean;
  2793. begin
  2794. result := mpf_integer_p(src(op)^) <> 0;
  2795. end;
  2796. procedure f_mul(var rop, op1, op2: MPFloat);
  2797. begin
  2798. mpf_mul(dest(rop)^, src(op1)^, src(op2)^);
  2799. end;
  2800. function f_mul(var op1, op2: MPFloat): MPFloat;
  2801. begin
  2802. mpf_mul(dest(result)^, src(op1)^, src(op2)^);
  2803. end;
  2804. procedure f_mul_2exp(var rop, op1: MPFloat; op2: valuint);
  2805. begin
  2806. mpf_mul_2exp(dest(rop)^, src(op1)^, op2);
  2807. end;
  2808. function f_mul_2exp(var op1: MPFloat; op2: valuint): MPFloat;
  2809. begin
  2810. mpf_mul_2exp(dest(result)^, src(op1)^, op2);
  2811. end;
  2812. procedure f_mul_ui(var rop, op1: MPFloat; op2: valuint);
  2813. begin
  2814. mpf_mul_ui(dest(rop)^, src(op1)^, op2);
  2815. end;
  2816. function f_mul_ui(var op1: MPFloat; op2: valuint): MPFloat;
  2817. begin
  2818. mpf_mul_ui(dest(result)^, src(op1)^, op2);
  2819. end;
  2820. procedure f_neg(var rop, op: MPFloat);
  2821. begin
  2822. mpf_neg(dest(rop)^, src(op)^);
  2823. end;
  2824. function f_neg(var op: MPFloat): MPFloat;
  2825. begin
  2826. mpf_neg(dest(result)^, src(op)^);
  2827. end;
  2828. procedure f_pow_ui(var rop, op1: MPFloat; op2: valuint);
  2829. begin
  2830. mpf_pow_ui(dest(rop)^, src(op1)^, op2);
  2831. end;
  2832. function f_pow_ui(var op1: MPFloat; op2: valuint): MPFloat;
  2833. begin
  2834. mpf_pow_ui(dest(result)^, src(op1)^, op2);
  2835. end;
  2836. procedure f_random2(var rop: MPFloat; max_size: mp_size_t; exp: mp_exp_t);
  2837. begin
  2838. mpf_random2(dest(rop)^, max_size, exp);
  2839. end;
  2840. function f_random2(max_size: mp_size_t; exp: mp_exp_t): MPFloat;
  2841. begin
  2842. mpf_random2(dest(result)^, max_size, exp);
  2843. end;
  2844. procedure f_reldiff(var rop, op1, op2: MPFloat);
  2845. begin
  2846. mpf_reldiff(dest(rop)^, src(op1)^, src(op2)^);
  2847. end;
  2848. function f_reldiff(var op1, op2: MPFloat): MPFloat;
  2849. begin
  2850. mpf_reldiff(dest(result)^, src(op1)^, src(op2)^);
  2851. end;
  2852. procedure f_set(var rop, op: MPFloat);
  2853. begin
  2854. mpf_set(dest(rop)^, src(op)^);
  2855. end;
  2856. procedure f_set_d(var rop: MPFloat; op: double);
  2857. begin
  2858. mpf_set_d(dest(rop)^, op);
  2859. end;
  2860. procedure f_set_default_prec(prec: valuint);
  2861. begin
  2862. mpf_set_default_prec(prec);
  2863. end;
  2864. procedure f_set_prec(var rop: MPFloat; prec: valuint);
  2865. begin
  2866. mpf_set_prec(dest(rop)^, prec);
  2867. end;
  2868. procedure f_set_prec_raw(var rop: MPFloat; prec: valuint);
  2869. begin
  2870. mpf_set_prec_raw(dest(rop)^, prec);
  2871. end;
  2872. procedure f_set_q(var rop: MPFloat; var op: MPRational);
  2873. begin
  2874. mpf_set_q(dest(rop)^, src(op)^);
  2875. end;
  2876. procedure f_set_si(var rop: MPFloat; op: valsint);
  2877. begin
  2878. mpf_set_si(dest(rop)^, op);
  2879. end;
  2880. function f_set_str(var rop: MPFloat; str: string; base: longint): boolean;
  2881. begin
  2882. result := mpf_set_str(dest(rop)^, pchar(str), base) = 0;
  2883. end;
  2884. procedure f_set_ui(var rop: MPFloat; op: valuint);
  2885. begin
  2886. mpf_set_ui(dest(rop)^, op);
  2887. end;
  2888. procedure f_set_z(var rop: MPFloat; var op: MPInteger);
  2889. begin
  2890. mpf_set_z(dest(rop)^, src(op)^);
  2891. end;
  2892. procedure f_sqrt(var rop, op: MPFloat);
  2893. begin
  2894. mpf_sqrt(dest(rop)^, src(op)^);
  2895. end;
  2896. function f_sqrt(var op: MPFloat): MPFloat;
  2897. begin
  2898. mpf_sqrt(dest(result)^, src(op)^);
  2899. end;
  2900. procedure f_sqrt_ui(var rop: MPFloat; op: valuint);
  2901. begin
  2902. mpf_sqrt_ui(dest(rop)^, op);
  2903. end;
  2904. function f_sqrt_ui(op: valuint): MPFloat;
  2905. begin
  2906. mpf_sqrt_ui(dest(result)^, op);
  2907. end;
  2908. procedure f_sub(var rop, op1, op2: MPFloat);
  2909. begin
  2910. mpf_sub(dest(rop)^, src(op1)^, src(op2)^);
  2911. end;
  2912. function f_sub(var op1, op2: MPFloat): MPFloat;
  2913. begin
  2914. mpf_sub(dest(result)^, src(op1)^, src(op2)^);
  2915. end;
  2916. procedure f_sub_ui(var rop, op1: MPFloat; op2: valuint);
  2917. begin
  2918. mpf_sub_ui(dest(rop)^, src(op1)^, op2);
  2919. end;
  2920. function f_sub_ui(var op1: MPFloat; op2: valuint): MPFloat;
  2921. begin
  2922. mpf_sub_ui(dest(result)^, src(op1)^, op2);
  2923. end;
  2924. procedure f_swap(var rop1, rop2: MPFloat);
  2925. begin
  2926. mpf_swap(dest(rop1)^, dest(rop2)^);
  2927. end;
  2928. procedure f_trunc(var rop, op: MPFloat);
  2929. begin
  2930. mpf_trunc(dest(rop)^, src(op)^);
  2931. end;
  2932. function f_trunc(var op: MPFloat): MPFloat;
  2933. begin
  2934. mpf_trunc(dest(result)^, src(op)^);
  2935. end;
  2936. procedure f_ui_div(var rop: MPFloat; op1: valuint; var op2: MPFloat);
  2937. begin
  2938. mpf_ui_div(dest(rop)^, op1, src(op2)^);
  2939. end;
  2940. function f_ui_div(op1: valuint; var op2: MPFloat): MPFloat;
  2941. begin
  2942. mpf_ui_div(dest(result)^, op1, src(op2)^);
  2943. end;
  2944. procedure f_ui_sub(var rop: MPFloat; op1: valuint; var op2: MPFloat);
  2945. begin
  2946. mpf_ui_sub(dest(rop)^, op1, src(op2)^);
  2947. end;
  2948. function f_ui_sub(op1: valuint; var op2: MPFloat): MPFloat;
  2949. begin
  2950. mpf_ui_sub(dest(result)^, op1, src(op2)^);
  2951. end;
  2952. procedure f_urandomb(var rop: MPFloat; var state: MPRandState; nbits: valuint);
  2953. begin
  2954. mpf_urandomb(dest(rop)^, src(state)^, nbits);
  2955. end;
  2956. function f_urandomb(var state: MPRandState; nbits: valuint): MPFloat;
  2957. begin
  2958. mpf_urandomb(dest(result)^, src(state)^, nbits);
  2959. end;
  2960. // ---- operators ----
  2961. operator * (op1: MPFloat; op2: MPFloat): MPFloat;
  2962. begin
  2963. propagate_prec(result, op1, op2);
  2964. f_mul(result, op1, op2);
  2965. end;
  2966. operator * (op1: MPInteger; op2: MPInteger): MPInteger;
  2967. begin
  2968. z_mul(result, op1, op2);
  2969. end;
  2970. operator * (op1: MPRational; op2: MPRational): MPRational;
  2971. begin
  2972. q_mul(result, op1, op2);
  2973. end;
  2974. operator ** (op1: MPFloat; op2: valuint): MPFloat;
  2975. begin
  2976. propagate_prec(result, op1);
  2977. f_pow_ui(result, op1, op2);
  2978. end;
  2979. operator ** (op1: MPInteger; op2: valuint): MPInteger;
  2980. begin
  2981. z_pow_ui(result, op1, op2);
  2982. end;
  2983. operator + (op1: MPFloat; op2: MPFloat): MPFloat;
  2984. begin
  2985. propagate_prec(result, op1, op2);
  2986. f_add(result, op1, op2);
  2987. end;
  2988. operator + (op1: MPInteger; op2: MPInteger): MPInteger;
  2989. begin
  2990. z_add(result, op1, op2);
  2991. end;
  2992. operator + (op1: MPRational; op2: MPRational): MPRational;
  2993. begin
  2994. q_add(result, op1, op2);
  2995. end;
  2996. operator - (op: MPFloat): MPFloat;
  2997. begin
  2998. propagate_prec(result, op);
  2999. f_neg(result, op);
  3000. end;
  3001. operator - (op: MPInteger): MPInteger;
  3002. begin
  3003. z_neg(result, op);
  3004. end;
  3005. operator - (op: MPRational): MPRational;
  3006. begin
  3007. q_neg(result, op);
  3008. end;
  3009. operator - (op1: MPFloat; op2: MPFloat): MPFloat;
  3010. begin
  3011. propagate_prec(result, op1, op2);
  3012. f_sub(result, op1, op2);
  3013. end;
  3014. operator - (op1: MPInteger; op2: MPInteger): MPInteger;
  3015. begin
  3016. z_sub(result, op1, op2);
  3017. end;
  3018. operator - (op1: MPRational; op2: MPRational): MPRational;
  3019. begin
  3020. q_sub(result, op1, op2);
  3021. end;
  3022. operator / (op1: MPFloat; op2: MPFloat): MPFloat;
  3023. begin
  3024. propagate_prec(result, op1, op2);
  3025. f_div(result, op1, op2);
  3026. end;
  3027. operator / (op1: MPInteger; op2: MPInteger): MPInteger;
  3028. begin
  3029. z_tdiv_q(result, op1, op2);
  3030. end;
  3031. operator / (op1: MPRational; op2: MPRational): MPRational;
  3032. begin
  3033. q_div(result, op1, op2);
  3034. end;
  3035. operator := (op: double): MPFloat;
  3036. begin
  3037. f_set_d(result, op);
  3038. end;
  3039. operator := (op: double): MPInteger;
  3040. begin
  3041. z_set_d(result, op);
  3042. end;
  3043. operator := (op: double): MPRational;
  3044. begin
  3045. q_set_d(result, op);
  3046. end;
  3047. operator := (op: MPFloat): cardinal;
  3048. begin
  3049. result := f_get_ui(op);
  3050. end;
  3051. operator := (op: MPFloat): double;
  3052. begin
  3053. result := f_get_d(op);
  3054. end;
  3055. operator := (op: MPFloat): integer;
  3056. begin
  3057. result := f_get_si(op);
  3058. end;
  3059. operator := (op: MPFloat): mpf_t;
  3060. begin
  3061. mpf_init_set(result, src(op)^);
  3062. end;
  3063. operator := (op: MPFloat): MPInteger;
  3064. begin
  3065. z_set_f(result, op);
  3066. end;
  3067. operator := (op: MPFloat): MPRational;
  3068. begin
  3069. q_set_f(result, op);
  3070. end;
  3071. operator := (op: MPFloat): string;
  3072. const FMT = '%.*Fg';
  3073. var p: pchar;
  3074. begin
  3075. mp_asprintf(p, FMT, floor(f_get_prec(op) * LOG_10_2), src(op));
  3076. result := p;
  3077. freemem(p);
  3078. end;
  3079. {$ifdef CPU64}
  3080. operator := (op: MPFloat): valsint;
  3081. begin
  3082. result := f_get_si(op);
  3083. end;
  3084. operator := (op: MPFloat): valuint;
  3085. begin
  3086. result := f_get_ui(op);
  3087. end;
  3088. {$endif}
  3089. operator := (var op: mpf_t): MPFloat;
  3090. begin
  3091. mpf_set(dest(result)^, op);
  3092. end;
  3093. operator := (op: MPInteger): cardinal;
  3094. begin
  3095. result := z_get_ui(op);
  3096. end;
  3097. operator := (op: MPInteger): double;
  3098. begin
  3099. result := z_get_d(op);
  3100. end;
  3101. operator := (op: MPInteger): integer;
  3102. begin
  3103. result := z_get_si(op);
  3104. end;
  3105. operator := (op: MPInteger): MPFloat;
  3106. begin
  3107. f_set_z(result, op);
  3108. end;
  3109. operator := (op: MPInteger): MPRational;
  3110. begin
  3111. q_set_z(result, op);
  3112. end;
  3113. operator := (op: MPInteger): mpz_t;
  3114. begin
  3115. mpz_init_set(result, src(op)^);
  3116. end;
  3117. operator := (op: MPInteger): string;
  3118. const FMT = '%Zd';
  3119. var p: pchar;
  3120. begin
  3121. mp_asprintf(p, FMT, src(op));
  3122. result := p;
  3123. freemem(p);
  3124. end;
  3125. {$ifdef CPU64}
  3126. operator := (op: MPInteger): valsint;
  3127. begin
  3128. result := z_get_si(op);
  3129. end;
  3130. operator := (op: MPInteger): valuint;
  3131. begin
  3132. result := z_get_ui(op);
  3133. end;
  3134. {$endif}
  3135. operator := (var op: mpq_t): MPRational;
  3136. begin
  3137. mpq_set(dest(result)^, op);
  3138. end;
  3139. operator := (op: MPRandState): randstate_t;
  3140. begin
  3141. mp_randinit_set(result, src(op)^);
  3142. end;
  3143. operator := (op: MPRational): double;
  3144. begin
  3145. result := q_get_d(op);
  3146. end;
  3147. operator := (op: MPRational): MPFloat;
  3148. begin
  3149. f_set_q(result, op);
  3150. end;
  3151. operator := (op: MPRational): MPInteger;
  3152. begin
  3153. z_set_q(result, op);
  3154. end;
  3155. operator := (op: MPRational): mpq_t;
  3156. begin
  3157. mpq_init(result);
  3158. mpq_set(result, src(op)^);
  3159. end;
  3160. operator := (op: MPRational): string;
  3161. const FMT = '%Qd';
  3162. var p: pchar;
  3163. begin
  3164. mp_asprintf(p, FMT, src(op));
  3165. result := p;
  3166. freemem(p);
  3167. end;
  3168. operator := (var op: mpz_t): MPInteger;
  3169. begin
  3170. mpz_set(dest(result)^, op);
  3171. end;
  3172. operator := (var op: randstate_t): MPRandState;
  3173. begin
  3174. result := TMPRandState.Create;
  3175. mp_randinit_set(result.ptr^, op);
  3176. end;
  3177. operator := (op: string): MPFloat;
  3178. begin
  3179. f_set_prec(result, ceil(length(op) / LOG_10_2));
  3180. f_set_str(result, op, BASE10);
  3181. end;
  3182. operator := (op: string): MPInteger;
  3183. begin
  3184. z_set_str(result, op, BASE10);
  3185. end;
  3186. operator := (op: string): MPRational;
  3187. begin
  3188. q_set_str(result, op, BASE10);
  3189. end;
  3190. operator := (op: valsint): MPFloat;
  3191. begin
  3192. f_set_si(result, op);
  3193. end;
  3194. operator := (op: valsint): MPInteger;
  3195. begin
  3196. z_set_si(result, op);
  3197. end;
  3198. operator := (op: valsint): MPRational;
  3199. begin
  3200. q_set_si(result, op, 1);
  3201. end;
  3202. operator := (op: valuint): MPFloat;
  3203. begin
  3204. f_set_ui(result, op);
  3205. end;
  3206. operator := (op: valuint): MPInteger;
  3207. begin
  3208. z_set_ui(result, op);
  3209. end;
  3210. operator := (op: valuint): MPRational;
  3211. begin
  3212. q_set_ui(result, op, 1);
  3213. end;
  3214. operator < (op1: MPFloat; op2: MPFloat): boolean;
  3215. begin
  3216. result := f_cmp(op1, op2) < 0;
  3217. end;
  3218. operator < (op1: MPInteger; op2: MPInteger): boolean;
  3219. begin
  3220. result := z_cmp(op1, op2) < 0;
  3221. end;
  3222. operator < (op1: MPRational; op2: MPRational): boolean;
  3223. begin
  3224. result := q_cmp(op1, op2) < 0;
  3225. end;
  3226. operator <= (op1: MPFloat; op2: MPFloat): boolean;
  3227. begin
  3228. result := f_cmp(op1, op2) <= 0;
  3229. end;
  3230. operator <= (op1: MPInteger; op2: MPInteger): boolean;
  3231. begin
  3232. result := z_cmp(op1, op2) <= 0;
  3233. end;
  3234. operator <= (op1: MPRational; op2: MPRational): boolean;
  3235. begin
  3236. result := q_cmp(op1, op2) <= 0;
  3237. end;
  3238. //operator = (op1: MPFloat; op2: MPFloat): boolean;
  3239. //begin
  3240. // result := f_cmp(op1, op2) = 0;
  3241. //end;
  3242. //operator = (op1: MPInteger; op2: MPInteger): boolean;
  3243. //begin
  3244. // result := z_cmp(op1, op2) = 0;
  3245. //end;
  3246. //operator = (op1: MPRational; op2: MPRational): boolean;
  3247. //begin
  3248. // result := q_cmp(op1, op2) = 0;
  3249. //end;
  3250. operator > (op1: MPFloat; op2: MPFloat): boolean;
  3251. begin
  3252. result := f_cmp(op1, op2) > 0;
  3253. end;
  3254. operator > (op1: MPInteger; op2: MPInteger): boolean;
  3255. begin
  3256. result := z_cmp(op1, op2) > 0;
  3257. end;
  3258. operator > (op1: MPRational; op2: MPRational): boolean;
  3259. begin
  3260. result := q_cmp(op1, op2) > 0;
  3261. end;
  3262. operator >= (op1: MPFloat; op2: MPFloat): boolean;
  3263. begin
  3264. result := f_cmp(op1, op2) >= 0;
  3265. end;
  3266. operator >= (op1: MPInteger; op2: MPInteger): boolean;
  3267. begin
  3268. result := z_cmp(op1, op2) >= 0;
  3269. end;
  3270. operator >= (op1: MPRational; op2: MPRational): boolean;
  3271. begin
  3272. result := q_cmp(op1, op2) >= 0;
  3273. end;
  3274. // ==== init stuff ====
  3275. function alloc_func(alloc_size: sizeuint): pointer; cdecl;
  3276. begin
  3277. result := getmem(alloc_size);
  3278. end;
  3279. procedure free_proc(p: pointer; size: sizeuint); cdecl;
  3280. begin
  3281. assert(size = size); // hint off
  3282. freemem(p);
  3283. end;
  3284. function reallocate_func(p: pointer; old_size, new_size: sizeuint): pointer; cdecl;
  3285. begin
  3286. assert(old_size = old_size); // hint off
  3287. result := reallocmem(p, new_size);
  3288. end;
  3289. var r1: mp_limb_t;
  3290. initialization
  3291. // prealloc the GMP's global PRNG state, get rid of the (pseudo) mem leak report
  3292. mpn_random(@r1, 1);
  3293. mp_set_memory_functions(@alloc_func, @reallocate_func, @free_proc);
  3294. end.