gmp.pas 142 KB

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