mini-gmp.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578
  1. /* mini-gmp, a minimalistic implementation of a GNU GMP subset.
  2. Contributed to the GNU project by Niels Möller
  3. Copyright 1991-1997, 1999-2019 Free Software Foundation, Inc.
  4. This file is part of the GNU MP Library.
  5. The GNU MP Library is free software; you can redistribute it and/or modify
  6. it under the terms of either:
  7. * the GNU Lesser General Public License as published by the Free
  8. Software Foundation; either version 3 of the License, or (at your
  9. option) any later version.
  10. or
  11. * the GNU General Public License as published by the Free Software
  12. Foundation; either version 2 of the License, or (at your option) any
  13. later version.
  14. or both in parallel, as here.
  15. The GNU MP Library is distributed in the hope that it will be useful, but
  16. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. for more details.
  19. You should have received copies of the GNU General Public License and the
  20. GNU Lesser General Public License along with the GNU MP Library. If not,
  21. see https://www.gnu.org/licenses/. */
  22. /* NOTE: All functions in this file which are not declared in
  23. mini-gmp.h are internal, and are not intended to be compatible
  24. with GMP or with future versions of mini-gmp. */
  25. /* Much of the material copied from GMP files, including: gmp-impl.h,
  26. longlong.h, mpn/generic/add_n.c, mpn/generic/addmul_1.c,
  27. mpn/generic/lshift.c, mpn/generic/mul_1.c,
  28. mpn/generic/mul_basecase.c, mpn/generic/rshift.c,
  29. mpn/generic/sbpi1_div_qr.c, mpn/generic/sub_n.c,
  30. mpn/generic/submul_1.c. */
  31. #ifdef HAVE_CONFIG_H
  32. # include <config.h>
  33. #endif
  34. #include <assert.h>
  35. #include <ctype.h>
  36. #include <limits.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include "mini-gmp.h"
  41. #if !defined(MINI_GMP_DONT_USE_FLOAT_H)
  42. #include <float.h>
  43. #endif
  44. /* Macros */
  45. #define GMP_LIMB_BITS (sizeof(mp_limb_t) * CHAR_BIT)
  46. #define GMP_LIMB_MAX ((mp_limb_t) ~ (mp_limb_t) 0)
  47. #define GMP_LIMB_HIGHBIT ((mp_limb_t) 1 << (GMP_LIMB_BITS - 1))
  48. #define GMP_HLIMB_BIT ((mp_limb_t) 1 << (GMP_LIMB_BITS / 2))
  49. #define GMP_LLIMB_MASK (GMP_HLIMB_BIT - 1)
  50. #define GMP_ULONG_BITS (sizeof(unsigned long) * CHAR_BIT)
  51. #define GMP_ULONG_HIGHBIT ((unsigned long) 1 << (GMP_ULONG_BITS - 1))
  52. #define GMP_ABS(x) ((x) >= 0 ? (x) : -(x))
  53. #define GMP_NEG_CAST(T,x) (-((T)((x) + 1) - 1))
  54. #define GMP_MIN(a, b) ((a) < (b) ? (a) : (b))
  55. #define GMP_MAX(a, b) ((a) > (b) ? (a) : (b))
  56. #define GMP_CMP(a,b) (((a) > (b)) - ((a) < (b)))
  57. #if defined(DBL_MANT_DIG) && FLT_RADIX == 2
  58. #define GMP_DBL_MANT_BITS DBL_MANT_DIG
  59. #else
  60. #define GMP_DBL_MANT_BITS (53)
  61. #endif
  62. /* Return non-zero if xp,xsize and yp,ysize overlap.
  63. If xp+xsize<=yp there's no overlap, or if yp+ysize<=xp there's no
  64. overlap. If both these are false, there's an overlap. */
  65. #define GMP_MPN_OVERLAP_P(xp, xsize, yp, ysize) \
  66. ((xp) + (xsize) > (yp) && (yp) + (ysize) > (xp))
  67. #define gmp_assert_nocarry(x) do { \
  68. mp_limb_t __cy = (x); \
  69. assert (__cy == 0); \
  70. } while (0)
  71. #define gmp_clz(count, x) do { \
  72. mp_limb_t __clz_x = (x); \
  73. unsigned __clz_c = 0; \
  74. int LOCAL_SHIFT_BITS = 8; \
  75. if (GMP_LIMB_BITS > LOCAL_SHIFT_BITS) \
  76. for (; \
  77. (__clz_x & ((mp_limb_t) 0xff << (GMP_LIMB_BITS - 8))) == 0; \
  78. __clz_c += 8) \
  79. { __clz_x <<= LOCAL_SHIFT_BITS; } \
  80. for (; (__clz_x & GMP_LIMB_HIGHBIT) == 0; __clz_c++) \
  81. __clz_x <<= 1; \
  82. (count) = __clz_c; \
  83. } while (0)
  84. #define gmp_ctz(count, x) do { \
  85. mp_limb_t __ctz_x = (x); \
  86. unsigned __ctz_c = 0; \
  87. gmp_clz (__ctz_c, __ctz_x & - __ctz_x); \
  88. (count) = GMP_LIMB_BITS - 1 - __ctz_c; \
  89. } while (0)
  90. #define gmp_add_ssaaaa(sh, sl, ah, al, bh, bl) \
  91. do { \
  92. mp_limb_t __x; \
  93. __x = (al) + (bl); \
  94. (sh) = (ah) + (bh) + (__x < (al)); \
  95. (sl) = __x; \
  96. } while (0)
  97. #define gmp_sub_ddmmss(sh, sl, ah, al, bh, bl) \
  98. do { \
  99. mp_limb_t __x; \
  100. __x = (al) - (bl); \
  101. (sh) = (ah) - (bh) - ((al) < (bl)); \
  102. (sl) = __x; \
  103. } while (0)
  104. #define gmp_umul_ppmm(w1, w0, u, v) \
  105. do { \
  106. int LOCAL_GMP_LIMB_BITS = GMP_LIMB_BITS; \
  107. if (sizeof(unsigned int) * CHAR_BIT >= 2 * GMP_LIMB_BITS) \
  108. { \
  109. unsigned int __ww = (unsigned int) (u) * (v); \
  110. w0 = (mp_limb_t) __ww; \
  111. w1 = (mp_limb_t) (__ww >> LOCAL_GMP_LIMB_BITS); \
  112. } \
  113. else if (GMP_ULONG_BITS >= 2 * GMP_LIMB_BITS) \
  114. { \
  115. unsigned long int __ww = (unsigned long int) (u) * (v); \
  116. w0 = (mp_limb_t) __ww; \
  117. w1 = (mp_limb_t) (__ww >> LOCAL_GMP_LIMB_BITS); \
  118. } \
  119. else { \
  120. mp_limb_t __x0, __x1, __x2, __x3; \
  121. unsigned __ul, __vl, __uh, __vh; \
  122. mp_limb_t __u = (u), __v = (v); \
  123. \
  124. __ul = __u & GMP_LLIMB_MASK; \
  125. __uh = __u >> (GMP_LIMB_BITS / 2); \
  126. __vl = __v & GMP_LLIMB_MASK; \
  127. __vh = __v >> (GMP_LIMB_BITS / 2); \
  128. \
  129. __x0 = (mp_limb_t) __ul * __vl; \
  130. __x1 = (mp_limb_t) __ul * __vh; \
  131. __x2 = (mp_limb_t) __uh * __vl; \
  132. __x3 = (mp_limb_t) __uh * __vh; \
  133. \
  134. __x1 += __x0 >> (GMP_LIMB_BITS / 2);/* this can't give carry */ \
  135. __x1 += __x2; /* but this indeed can */ \
  136. if (__x1 < __x2) /* did we get it? */ \
  137. __x3 += GMP_HLIMB_BIT; /* yes, add it in the proper pos. */ \
  138. \
  139. (w1) = __x3 + (__x1 >> (GMP_LIMB_BITS / 2)); \
  140. (w0) = (__x1 << (GMP_LIMB_BITS / 2)) + (__x0 & GMP_LLIMB_MASK); \
  141. } \
  142. } while (0)
  143. #define gmp_udiv_qrnnd_preinv(q, r, nh, nl, d, di) \
  144. do { \
  145. mp_limb_t _qh, _ql, _r, _mask; \
  146. gmp_umul_ppmm (_qh, _ql, (nh), (di)); \
  147. gmp_add_ssaaaa (_qh, _ql, _qh, _ql, (nh) + 1, (nl)); \
  148. _r = (nl) - _qh * (d); \
  149. _mask = -(mp_limb_t) (_r > _ql); /* both > and >= are OK */ \
  150. _qh += _mask; \
  151. _r += _mask & (d); \
  152. if (_r >= (d)) \
  153. { \
  154. _r -= (d); \
  155. _qh++; \
  156. } \
  157. \
  158. (r) = _r; \
  159. (q) = _qh; \
  160. } while (0)
  161. #define gmp_udiv_qr_3by2(q, r1, r0, n2, n1, n0, d1, d0, dinv) \
  162. do { \
  163. mp_limb_t _q0, _t1, _t0, _mask; \
  164. gmp_umul_ppmm ((q), _q0, (n2), (dinv)); \
  165. gmp_add_ssaaaa ((q), _q0, (q), _q0, (n2), (n1)); \
  166. \
  167. /* Compute the two most significant limbs of n - q'd */ \
  168. (r1) = (n1) - (d1) * (q); \
  169. gmp_sub_ddmmss ((r1), (r0), (r1), (n0), (d1), (d0)); \
  170. gmp_umul_ppmm (_t1, _t0, (d0), (q)); \
  171. gmp_sub_ddmmss ((r1), (r0), (r1), (r0), _t1, _t0); \
  172. (q)++; \
  173. \
  174. /* Conditionally adjust q and the remainders */ \
  175. _mask = - (mp_limb_t) ((r1) >= _q0); \
  176. (q) += _mask; \
  177. gmp_add_ssaaaa ((r1), (r0), (r1), (r0), _mask & (d1), _mask & (d0)); \
  178. if ((r1) >= (d1)) \
  179. { \
  180. if ((r1) > (d1) || (r0) >= (d0)) \
  181. { \
  182. (q)++; \
  183. gmp_sub_ddmmss ((r1), (r0), (r1), (r0), (d1), (d0)); \
  184. } \
  185. } \
  186. } while (0)
  187. /* Swap macros. */
  188. #define MP_LIMB_T_SWAP(x, y) \
  189. do { \
  190. mp_limb_t __mp_limb_t_swap__tmp = (x); \
  191. (x) = (y); \
  192. (y) = __mp_limb_t_swap__tmp; \
  193. } while (0)
  194. #define MP_SIZE_T_SWAP(x, y) \
  195. do { \
  196. mp_size_t __mp_size_t_swap__tmp = (x); \
  197. (x) = (y); \
  198. (y) = __mp_size_t_swap__tmp; \
  199. } while (0)
  200. #define MP_BITCNT_T_SWAP(x,y) \
  201. do { \
  202. mp_bitcnt_t __mp_bitcnt_t_swap__tmp = (x); \
  203. (x) = (y); \
  204. (y) = __mp_bitcnt_t_swap__tmp; \
  205. } while (0)
  206. #define MP_PTR_SWAP(x, y) \
  207. do { \
  208. mp_ptr __mp_ptr_swap__tmp = (x); \
  209. (x) = (y); \
  210. (y) = __mp_ptr_swap__tmp; \
  211. } while (0)
  212. #define MP_SRCPTR_SWAP(x, y) \
  213. do { \
  214. mp_srcptr __mp_srcptr_swap__tmp = (x); \
  215. (x) = (y); \
  216. (y) = __mp_srcptr_swap__tmp; \
  217. } while (0)
  218. #define MPN_PTR_SWAP(xp,xs, yp,ys) \
  219. do { \
  220. MP_PTR_SWAP (xp, yp); \
  221. MP_SIZE_T_SWAP (xs, ys); \
  222. } while(0)
  223. #define MPN_SRCPTR_SWAP(xp,xs, yp,ys) \
  224. do { \
  225. MP_SRCPTR_SWAP (xp, yp); \
  226. MP_SIZE_T_SWAP (xs, ys); \
  227. } while(0)
  228. #define MPZ_PTR_SWAP(x, y) \
  229. do { \
  230. mpz_ptr __mpz_ptr_swap__tmp = (x); \
  231. (x) = (y); \
  232. (y) = __mpz_ptr_swap__tmp; \
  233. } while (0)
  234. #define MPZ_SRCPTR_SWAP(x, y) \
  235. do { \
  236. mpz_srcptr __mpz_srcptr_swap__tmp = (x); \
  237. (x) = (y); \
  238. (y) = __mpz_srcptr_swap__tmp; \
  239. } while (0)
  240. const int mp_bits_per_limb = GMP_LIMB_BITS;
  241. /* Memory allocation and other helper functions. */
  242. static void
  243. gmp_die (const char *msg)
  244. {
  245. fprintf (stderr, "%s\n", msg);
  246. abort();
  247. }
  248. static void *
  249. gmp_default_alloc (size_t size)
  250. {
  251. void *p;
  252. assert (size > 0);
  253. p = malloc (size);
  254. if (!p)
  255. gmp_die("gmp_default_alloc: Virtual memory exhausted.");
  256. return p;
  257. }
  258. static void *
  259. gmp_default_realloc (void *old, size_t unused_old_size, size_t new_size)
  260. {
  261. void * p;
  262. p = realloc (old, new_size);
  263. if (!p)
  264. gmp_die("gmp_default_realloc: Virtual memory exhausted.");
  265. return p;
  266. }
  267. static void
  268. gmp_default_free (void *p, size_t unused_size)
  269. {
  270. free (p);
  271. }
  272. static void * (*gmp_allocate_func) (size_t) = gmp_default_alloc;
  273. static void * (*gmp_reallocate_func) (void *, size_t, size_t) = gmp_default_realloc;
  274. static void (*gmp_free_func) (void *, size_t) = gmp_default_free;
  275. void
  276. mp_get_memory_functions (void *(**alloc_func) (size_t),
  277. void *(**realloc_func) (void *, size_t, size_t),
  278. void (**free_func) (void *, size_t))
  279. {
  280. if (alloc_func)
  281. *alloc_func = gmp_allocate_func;
  282. if (realloc_func)
  283. *realloc_func = gmp_reallocate_func;
  284. if (free_func)
  285. *free_func = gmp_free_func;
  286. }
  287. void
  288. mp_set_memory_functions (void *(*alloc_func) (size_t),
  289. void *(*realloc_func) (void *, size_t, size_t),
  290. void (*free_func) (void *, size_t))
  291. {
  292. if (!alloc_func)
  293. alloc_func = gmp_default_alloc;
  294. if (!realloc_func)
  295. realloc_func = gmp_default_realloc;
  296. if (!free_func)
  297. free_func = gmp_default_free;
  298. gmp_allocate_func = alloc_func;
  299. gmp_reallocate_func = realloc_func;
  300. gmp_free_func = free_func;
  301. }
  302. #define gmp_xalloc(size) ((*gmp_allocate_func)((size)))
  303. #define gmp_free(p) ((*gmp_free_func) ((p), 0))
  304. static mp_ptr
  305. gmp_xalloc_limbs (mp_size_t size)
  306. {
  307. return (mp_ptr) gmp_xalloc (size * sizeof (mp_limb_t));
  308. }
  309. static mp_ptr
  310. gmp_xrealloc_limbs (mp_ptr old, mp_size_t size)
  311. {
  312. assert (size > 0);
  313. return (mp_ptr) (*gmp_reallocate_func) (old, 0, size * sizeof (mp_limb_t));
  314. }
  315. /* MPN interface */
  316. void
  317. mpn_copyi (mp_ptr d, mp_srcptr s, mp_size_t n)
  318. {
  319. mp_size_t i;
  320. for (i = 0; i < n; i++)
  321. d[i] = s[i];
  322. }
  323. void
  324. mpn_copyd (mp_ptr d, mp_srcptr s, mp_size_t n)
  325. {
  326. while (--n >= 0)
  327. d[n] = s[n];
  328. }
  329. int
  330. mpn_cmp (mp_srcptr ap, mp_srcptr bp, mp_size_t n)
  331. {
  332. while (--n >= 0)
  333. {
  334. if (ap[n] != bp[n])
  335. return ap[n] > bp[n] ? 1 : -1;
  336. }
  337. return 0;
  338. }
  339. static int
  340. mpn_cmp4 (mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
  341. {
  342. if (an != bn)
  343. return an < bn ? -1 : 1;
  344. else
  345. return mpn_cmp (ap, bp, an);
  346. }
  347. static mp_size_t
  348. mpn_normalized_size (mp_srcptr xp, mp_size_t n)
  349. {
  350. while (n > 0 && xp[n-1] == 0)
  351. --n;
  352. return n;
  353. }
  354. int
  355. mpn_zero_p(mp_srcptr rp, mp_size_t n)
  356. {
  357. return mpn_normalized_size (rp, n) == 0;
  358. }
  359. void
  360. mpn_zero (mp_ptr rp, mp_size_t n)
  361. {
  362. while (--n >= 0)
  363. rp[n] = 0;
  364. }
  365. mp_limb_t
  366. mpn_add_1 (mp_ptr rp, mp_srcptr ap, mp_size_t n, mp_limb_t b)
  367. {
  368. mp_size_t i;
  369. assert (n > 0);
  370. i = 0;
  371. do
  372. {
  373. mp_limb_t r = ap[i] + b;
  374. /* Carry out */
  375. b = (r < b);
  376. rp[i] = r;
  377. }
  378. while (++i < n);
  379. return b;
  380. }
  381. mp_limb_t
  382. mpn_add_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
  383. {
  384. mp_size_t i;
  385. mp_limb_t cy;
  386. for (i = 0, cy = 0; i < n; i++)
  387. {
  388. mp_limb_t a, b, r;
  389. a = ap[i]; b = bp[i];
  390. r = a + cy;
  391. cy = (r < cy);
  392. r += b;
  393. cy += (r < b);
  394. rp[i] = r;
  395. }
  396. return cy;
  397. }
  398. mp_limb_t
  399. mpn_add (mp_ptr rp, mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
  400. {
  401. mp_limb_t cy;
  402. assert (an >= bn);
  403. cy = mpn_add_n (rp, ap, bp, bn);
  404. if (an > bn)
  405. cy = mpn_add_1 (rp + bn, ap + bn, an - bn, cy);
  406. return cy;
  407. }
  408. mp_limb_t
  409. mpn_sub_1 (mp_ptr rp, mp_srcptr ap, mp_size_t n, mp_limb_t b)
  410. {
  411. mp_size_t i;
  412. assert (n > 0);
  413. i = 0;
  414. do
  415. {
  416. mp_limb_t a = ap[i];
  417. /* Carry out */
  418. mp_limb_t cy = a < b;
  419. rp[i] = a - b;
  420. b = cy;
  421. }
  422. while (++i < n);
  423. return b;
  424. }
  425. mp_limb_t
  426. mpn_sub_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
  427. {
  428. mp_size_t i;
  429. mp_limb_t cy;
  430. for (i = 0, cy = 0; i < n; i++)
  431. {
  432. mp_limb_t a, b;
  433. a = ap[i]; b = bp[i];
  434. b += cy;
  435. cy = (b < cy);
  436. cy += (a < b);
  437. rp[i] = a - b;
  438. }
  439. return cy;
  440. }
  441. mp_limb_t
  442. mpn_sub (mp_ptr rp, mp_srcptr ap, mp_size_t an, mp_srcptr bp, mp_size_t bn)
  443. {
  444. mp_limb_t cy;
  445. assert (an >= bn);
  446. cy = mpn_sub_n (rp, ap, bp, bn);
  447. if (an > bn)
  448. cy = mpn_sub_1 (rp + bn, ap + bn, an - bn, cy);
  449. return cy;
  450. }
  451. mp_limb_t
  452. mpn_mul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
  453. {
  454. mp_limb_t ul, cl, hpl, lpl;
  455. assert (n >= 1);
  456. cl = 0;
  457. do
  458. {
  459. ul = *up++;
  460. gmp_umul_ppmm (hpl, lpl, ul, vl);
  461. lpl += cl;
  462. cl = (lpl < cl) + hpl;
  463. *rp++ = lpl;
  464. }
  465. while (--n != 0);
  466. return cl;
  467. }
  468. mp_limb_t
  469. mpn_addmul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
  470. {
  471. mp_limb_t ul, cl, hpl, lpl, rl;
  472. assert (n >= 1);
  473. cl = 0;
  474. do
  475. {
  476. ul = *up++;
  477. gmp_umul_ppmm (hpl, lpl, ul, vl);
  478. lpl += cl;
  479. cl = (lpl < cl) + hpl;
  480. rl = *rp;
  481. lpl = rl + lpl;
  482. cl += lpl < rl;
  483. *rp++ = lpl;
  484. }
  485. while (--n != 0);
  486. return cl;
  487. }
  488. mp_limb_t
  489. mpn_submul_1 (mp_ptr rp, mp_srcptr up, mp_size_t n, mp_limb_t vl)
  490. {
  491. mp_limb_t ul, cl, hpl, lpl, rl;
  492. assert (n >= 1);
  493. cl = 0;
  494. do
  495. {
  496. ul = *up++;
  497. gmp_umul_ppmm (hpl, lpl, ul, vl);
  498. lpl += cl;
  499. cl = (lpl < cl) + hpl;
  500. rl = *rp;
  501. lpl = rl - lpl;
  502. cl += lpl > rl;
  503. *rp++ = lpl;
  504. }
  505. while (--n != 0);
  506. return cl;
  507. }
  508. mp_limb_t
  509. mpn_mul (mp_ptr rp, mp_srcptr up, mp_size_t un, mp_srcptr vp, mp_size_t vn)
  510. {
  511. assert (un >= vn);
  512. assert (vn >= 1);
  513. assert (!GMP_MPN_OVERLAP_P(rp, un + vn, up, un));
  514. assert (!GMP_MPN_OVERLAP_P(rp, un + vn, vp, vn));
  515. /* We first multiply by the low order limb. This result can be
  516. stored, not added, to rp. We also avoid a loop for zeroing this
  517. way. */
  518. rp[un] = mpn_mul_1 (rp, up, un, vp[0]);
  519. /* Now accumulate the product of up[] and the next higher limb from
  520. vp[]. */
  521. while (--vn >= 1)
  522. {
  523. rp += 1, vp += 1;
  524. rp[un] = mpn_addmul_1 (rp, up, un, vp[0]);
  525. }
  526. return rp[un];
  527. }
  528. void
  529. mpn_mul_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
  530. {
  531. mpn_mul (rp, ap, n, bp, n);
  532. }
  533. void
  534. mpn_sqr (mp_ptr rp, mp_srcptr ap, mp_size_t n)
  535. {
  536. mpn_mul (rp, ap, n, ap, n);
  537. }
  538. mp_limb_t
  539. mpn_lshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt)
  540. {
  541. mp_limb_t high_limb, low_limb;
  542. unsigned int tnc;
  543. mp_limb_t retval;
  544. assert (n >= 1);
  545. assert (cnt >= 1);
  546. assert (cnt < GMP_LIMB_BITS);
  547. up += n;
  548. rp += n;
  549. tnc = GMP_LIMB_BITS - cnt;
  550. low_limb = *--up;
  551. retval = low_limb >> tnc;
  552. high_limb = (low_limb << cnt);
  553. while (--n != 0)
  554. {
  555. low_limb = *--up;
  556. *--rp = high_limb | (low_limb >> tnc);
  557. high_limb = (low_limb << cnt);
  558. }
  559. *--rp = high_limb;
  560. return retval;
  561. }
  562. mp_limb_t
  563. mpn_rshift (mp_ptr rp, mp_srcptr up, mp_size_t n, unsigned int cnt)
  564. {
  565. mp_limb_t high_limb, low_limb;
  566. unsigned int tnc;
  567. mp_limb_t retval;
  568. assert (n >= 1);
  569. assert (cnt >= 1);
  570. assert (cnt < GMP_LIMB_BITS);
  571. tnc = GMP_LIMB_BITS - cnt;
  572. high_limb = *up++;
  573. retval = (high_limb << tnc);
  574. low_limb = high_limb >> cnt;
  575. while (--n != 0)
  576. {
  577. high_limb = *up++;
  578. *rp++ = low_limb | (high_limb << tnc);
  579. low_limb = high_limb >> cnt;
  580. }
  581. *rp = low_limb;
  582. return retval;
  583. }
  584. static mp_bitcnt_t
  585. mpn_common_scan (mp_limb_t limb, mp_size_t i, mp_srcptr up, mp_size_t un,
  586. mp_limb_t ux)
  587. {
  588. unsigned cnt;
  589. assert (ux == 0 || ux == GMP_LIMB_MAX);
  590. assert (0 <= i && i <= un );
  591. while (limb == 0)
  592. {
  593. i++;
  594. if (i == un)
  595. return (ux == 0 ? ~(mp_bitcnt_t) 0 : un * GMP_LIMB_BITS);
  596. limb = ux ^ up[i];
  597. }
  598. gmp_ctz (cnt, limb);
  599. return (mp_bitcnt_t) i * GMP_LIMB_BITS + cnt;
  600. }
  601. mp_bitcnt_t
  602. mpn_scan1 (mp_srcptr ptr, mp_bitcnt_t bit)
  603. {
  604. mp_size_t i;
  605. i = bit / GMP_LIMB_BITS;
  606. return mpn_common_scan ( ptr[i] & (GMP_LIMB_MAX << (bit % GMP_LIMB_BITS)),
  607. i, ptr, i, 0);
  608. }
  609. mp_bitcnt_t
  610. mpn_scan0 (mp_srcptr ptr, mp_bitcnt_t bit)
  611. {
  612. mp_size_t i;
  613. i = bit / GMP_LIMB_BITS;
  614. return mpn_common_scan (~ptr[i] & (GMP_LIMB_MAX << (bit % GMP_LIMB_BITS)),
  615. i, ptr, i, GMP_LIMB_MAX);
  616. }
  617. void
  618. mpn_com (mp_ptr rp, mp_srcptr up, mp_size_t n)
  619. {
  620. while (--n >= 0)
  621. *rp++ = ~ *up++;
  622. }
  623. mp_limb_t
  624. mpn_neg (mp_ptr rp, mp_srcptr up, mp_size_t n)
  625. {
  626. while (*up == 0)
  627. {
  628. *rp = 0;
  629. if (!--n)
  630. return 0;
  631. ++up; ++rp;
  632. }
  633. *rp = - *up;
  634. mpn_com (++rp, ++up, --n);
  635. return 1;
  636. }
  637. /* MPN division interface. */
  638. /* The 3/2 inverse is defined as
  639. m = floor( (B^3-1) / (B u1 + u0)) - B
  640. */
  641. mp_limb_t
  642. mpn_invert_3by2 (mp_limb_t u1, mp_limb_t u0)
  643. {
  644. mp_limb_t r, m;
  645. {
  646. mp_limb_t p, ql;
  647. unsigned ul, uh, qh;
  648. /* For notation, let b denote the half-limb base, so that B = b^2.
  649. Split u1 = b uh + ul. */
  650. ul = u1 & GMP_LLIMB_MASK;
  651. uh = u1 >> (GMP_LIMB_BITS / 2);
  652. /* Approximation of the high half of quotient. Differs from the 2/1
  653. inverse of the half limb uh, since we have already subtracted
  654. u0. */
  655. qh = (u1 ^ GMP_LIMB_MAX) / uh;
  656. /* Adjust to get a half-limb 3/2 inverse, i.e., we want
  657. qh' = floor( (b^3 - 1) / u) - b = floor ((b^3 - b u - 1) / u
  658. = floor( (b (~u) + b-1) / u),
  659. and the remainder
  660. r = b (~u) + b-1 - qh (b uh + ul)
  661. = b (~u - qh uh) + b-1 - qh ul
  662. Subtraction of qh ul may underflow, which implies adjustments.
  663. But by normalization, 2 u >= B > qh ul, so we need to adjust by
  664. at most 2.
  665. */
  666. r = ((~u1 - (mp_limb_t) qh * uh) << (GMP_LIMB_BITS / 2)) | GMP_LLIMB_MASK;
  667. p = (mp_limb_t) qh * ul;
  668. /* Adjustment steps taken from udiv_qrnnd_c */
  669. if (r < p)
  670. {
  671. qh--;
  672. r += u1;
  673. if (r >= u1) /* i.e. we didn't get carry when adding to r */
  674. if (r < p)
  675. {
  676. qh--;
  677. r += u1;
  678. }
  679. }
  680. r -= p;
  681. /* Low half of the quotient is
  682. ql = floor ( (b r + b-1) / u1).
  683. This is a 3/2 division (on half-limbs), for which qh is a
  684. suitable inverse. */
  685. p = (r >> (GMP_LIMB_BITS / 2)) * qh + r;
  686. /* Unlike full-limb 3/2, we can add 1 without overflow. For this to
  687. work, it is essential that ql is a full mp_limb_t. */
  688. ql = (p >> (GMP_LIMB_BITS / 2)) + 1;
  689. /* By the 3/2 trick, we don't need the high half limb. */
  690. r = (r << (GMP_LIMB_BITS / 2)) + GMP_LLIMB_MASK - ql * u1;
  691. if (r >= (GMP_LIMB_MAX & (p << (GMP_LIMB_BITS / 2))))
  692. {
  693. ql--;
  694. r += u1;
  695. }
  696. m = ((mp_limb_t) qh << (GMP_LIMB_BITS / 2)) + ql;
  697. if (r >= u1)
  698. {
  699. m++;
  700. r -= u1;
  701. }
  702. }
  703. /* Now m is the 2/1 inverse of u1. If u0 > 0, adjust it to become a
  704. 3/2 inverse. */
  705. if (u0 > 0)
  706. {
  707. mp_limb_t th, tl;
  708. r = ~r;
  709. r += u0;
  710. if (r < u0)
  711. {
  712. m--;
  713. if (r >= u1)
  714. {
  715. m--;
  716. r -= u1;
  717. }
  718. r -= u1;
  719. }
  720. gmp_umul_ppmm (th, tl, u0, m);
  721. r += th;
  722. if (r < th)
  723. {
  724. m--;
  725. m -= ((r > u1) | ((r == u1) & (tl > u0)));
  726. }
  727. }
  728. return m;
  729. }
  730. struct gmp_div_inverse
  731. {
  732. /* Normalization shift count. */
  733. unsigned shift;
  734. /* Normalized divisor (d0 unused for mpn_div_qr_1) */
  735. mp_limb_t d1, d0;
  736. /* Inverse, for 2/1 or 3/2. */
  737. mp_limb_t di;
  738. };
  739. static void
  740. mpn_div_qr_1_invert (struct gmp_div_inverse *inv, mp_limb_t d)
  741. {
  742. unsigned shift;
  743. assert (d > 0);
  744. gmp_clz (shift, d);
  745. inv->shift = shift;
  746. inv->d1 = d << shift;
  747. inv->di = mpn_invert_limb (inv->d1);
  748. }
  749. static void
  750. mpn_div_qr_2_invert (struct gmp_div_inverse *inv,
  751. mp_limb_t d1, mp_limb_t d0)
  752. {
  753. unsigned shift;
  754. assert (d1 > 0);
  755. gmp_clz (shift, d1);
  756. inv->shift = shift;
  757. if (shift > 0)
  758. {
  759. d1 = (d1 << shift) | (d0 >> (GMP_LIMB_BITS - shift));
  760. d0 <<= shift;
  761. }
  762. inv->d1 = d1;
  763. inv->d0 = d0;
  764. inv->di = mpn_invert_3by2 (d1, d0);
  765. }
  766. static void
  767. mpn_div_qr_invert (struct gmp_div_inverse *inv,
  768. mp_srcptr dp, mp_size_t dn)
  769. {
  770. assert (dn > 0);
  771. if (dn == 1)
  772. mpn_div_qr_1_invert (inv, dp[0]);
  773. else if (dn == 2)
  774. mpn_div_qr_2_invert (inv, dp[1], dp[0]);
  775. else
  776. {
  777. unsigned shift;
  778. mp_limb_t d1, d0;
  779. d1 = dp[dn-1];
  780. d0 = dp[dn-2];
  781. assert (d1 > 0);
  782. gmp_clz (shift, d1);
  783. inv->shift = shift;
  784. if (shift > 0)
  785. {
  786. d1 = (d1 << shift) | (d0 >> (GMP_LIMB_BITS - shift));
  787. d0 = (d0 << shift) | (dp[dn-3] >> (GMP_LIMB_BITS - shift));
  788. }
  789. inv->d1 = d1;
  790. inv->d0 = d0;
  791. inv->di = mpn_invert_3by2 (d1, d0);
  792. }
  793. }
  794. /* Not matching current public gmp interface, rather corresponding to
  795. the sbpi1_div_* functions. */
  796. static mp_limb_t
  797. mpn_div_qr_1_preinv (mp_ptr qp, mp_srcptr np, mp_size_t nn,
  798. const struct gmp_div_inverse *inv)
  799. {
  800. mp_limb_t d, di;
  801. mp_limb_t r;
  802. mp_ptr tp = NULL;
  803. if (inv->shift > 0)
  804. {
  805. /* Shift, reusing qp area if possible. In-place shift if qp == np. */
  806. tp = qp ? qp : gmp_xalloc_limbs (nn);
  807. r = mpn_lshift (tp, np, nn, inv->shift);
  808. np = tp;
  809. }
  810. else
  811. r = 0;
  812. d = inv->d1;
  813. di = inv->di;
  814. while (--nn >= 0)
  815. {
  816. mp_limb_t q;
  817. gmp_udiv_qrnnd_preinv (q, r, r, np[nn], d, di);
  818. if (qp)
  819. qp[nn] = q;
  820. }
  821. if ((inv->shift > 0) && (tp != qp))
  822. gmp_free (tp);
  823. return r >> inv->shift;
  824. }
  825. static void
  826. mpn_div_qr_2_preinv (mp_ptr qp, mp_ptr np, mp_size_t nn,
  827. const struct gmp_div_inverse *inv)
  828. {
  829. unsigned shift;
  830. mp_size_t i;
  831. mp_limb_t d1, d0, di, r1, r0;
  832. assert (nn >= 2);
  833. shift = inv->shift;
  834. d1 = inv->d1;
  835. d0 = inv->d0;
  836. di = inv->di;
  837. if (shift > 0)
  838. r1 = mpn_lshift (np, np, nn, shift);
  839. else
  840. r1 = 0;
  841. r0 = np[nn - 1];
  842. i = nn - 2;
  843. do
  844. {
  845. mp_limb_t n0, q;
  846. n0 = np[i];
  847. gmp_udiv_qr_3by2 (q, r1, r0, r1, r0, n0, d1, d0, di);
  848. if (qp)
  849. qp[i] = q;
  850. }
  851. while (--i >= 0);
  852. if (shift > 0)
  853. {
  854. assert ((r0 & (GMP_LIMB_MAX >> (GMP_LIMB_BITS - shift))) == 0);
  855. r0 = (r0 >> shift) | (r1 << (GMP_LIMB_BITS - shift));
  856. r1 >>= shift;
  857. }
  858. np[1] = r1;
  859. np[0] = r0;
  860. }
  861. static void
  862. mpn_div_qr_pi1 (mp_ptr qp,
  863. mp_ptr np, mp_size_t nn, mp_limb_t n1,
  864. mp_srcptr dp, mp_size_t dn,
  865. mp_limb_t dinv)
  866. {
  867. mp_size_t i;
  868. mp_limb_t d1, d0;
  869. mp_limb_t cy, cy1;
  870. mp_limb_t q;
  871. assert (dn > 2);
  872. assert (nn >= dn);
  873. d1 = dp[dn - 1];
  874. d0 = dp[dn - 2];
  875. assert ((d1 & GMP_LIMB_HIGHBIT) != 0);
  876. /* Iteration variable is the index of the q limb.
  877. *
  878. * We divide <n1, np[dn-1+i], np[dn-2+i], np[dn-3+i],..., np[i]>
  879. * by <d1, d0, dp[dn-3], ..., dp[0] >
  880. */
  881. i = nn - dn;
  882. do
  883. {
  884. mp_limb_t n0 = np[dn-1+i];
  885. if (n1 == d1 && n0 == d0)
  886. {
  887. q = GMP_LIMB_MAX;
  888. mpn_submul_1 (np+i, dp, dn, q);
  889. n1 = np[dn-1+i]; /* update n1, last loop's value will now be invalid */
  890. }
  891. else
  892. {
  893. gmp_udiv_qr_3by2 (q, n1, n0, n1, n0, np[dn-2+i], d1, d0, dinv);
  894. cy = mpn_submul_1 (np + i, dp, dn-2, q);
  895. cy1 = n0 < cy;
  896. n0 = n0 - cy;
  897. cy = n1 < cy1;
  898. n1 = n1 - cy1;
  899. np[dn-2+i] = n0;
  900. if (cy != 0)
  901. {
  902. n1 += d1 + mpn_add_n (np + i, np + i, dp, dn - 1);
  903. q--;
  904. }
  905. }
  906. if (qp)
  907. qp[i] = q;
  908. }
  909. while (--i >= 0);
  910. np[dn - 1] = n1;
  911. }
  912. static void
  913. mpn_div_qr_preinv (mp_ptr qp, mp_ptr np, mp_size_t nn,
  914. mp_srcptr dp, mp_size_t dn,
  915. const struct gmp_div_inverse *inv)
  916. {
  917. assert (dn > 0);
  918. assert (nn >= dn);
  919. if (dn == 1)
  920. np[0] = mpn_div_qr_1_preinv (qp, np, nn, inv);
  921. else if (dn == 2)
  922. mpn_div_qr_2_preinv (qp, np, nn, inv);
  923. else
  924. {
  925. mp_limb_t nh;
  926. unsigned shift;
  927. assert (inv->d1 == dp[dn-1]);
  928. assert (inv->d0 == dp[dn-2]);
  929. assert ((inv->d1 & GMP_LIMB_HIGHBIT) != 0);
  930. shift = inv->shift;
  931. if (shift > 0)
  932. nh = mpn_lshift (np, np, nn, shift);
  933. else
  934. nh = 0;
  935. mpn_div_qr_pi1 (qp, np, nn, nh, dp, dn, inv->di);
  936. if (shift > 0)
  937. gmp_assert_nocarry (mpn_rshift (np, np, dn, shift));
  938. }
  939. }
  940. static void
  941. mpn_div_qr (mp_ptr qp, mp_ptr np, mp_size_t nn, mp_srcptr dp, mp_size_t dn)
  942. {
  943. struct gmp_div_inverse inv;
  944. mp_ptr tp = NULL;
  945. assert (dn > 0);
  946. assert (nn >= dn);
  947. mpn_div_qr_invert (&inv, dp, dn);
  948. if (dn > 2 && inv.shift > 0)
  949. {
  950. tp = gmp_xalloc_limbs (dn);
  951. gmp_assert_nocarry (mpn_lshift (tp, dp, dn, inv.shift));
  952. dp = tp;
  953. }
  954. mpn_div_qr_preinv (qp, np, nn, dp, dn, &inv);
  955. if (tp)
  956. gmp_free (tp);
  957. }
  958. /* MPN base conversion. */
  959. static unsigned
  960. mpn_base_power_of_two_p (unsigned b)
  961. {
  962. switch (b)
  963. {
  964. case 2: return 1;
  965. case 4: return 2;
  966. case 8: return 3;
  967. case 16: return 4;
  968. case 32: return 5;
  969. case 64: return 6;
  970. case 128: return 7;
  971. case 256: return 8;
  972. default: return 0;
  973. }
  974. }
  975. struct mpn_base_info
  976. {
  977. /* bb is the largest power of the base which fits in one limb, and
  978. exp is the corresponding exponent. */
  979. unsigned exp;
  980. mp_limb_t bb;
  981. };
  982. static void
  983. mpn_get_base_info (struct mpn_base_info *info, mp_limb_t b)
  984. {
  985. mp_limb_t m;
  986. mp_limb_t p;
  987. unsigned exp;
  988. m = GMP_LIMB_MAX / b;
  989. for (exp = 1, p = b; p <= m; exp++)
  990. p *= b;
  991. info->exp = exp;
  992. info->bb = p;
  993. }
  994. static mp_bitcnt_t
  995. mpn_limb_size_in_base_2 (mp_limb_t u)
  996. {
  997. unsigned shift;
  998. assert (u > 0);
  999. gmp_clz (shift, u);
  1000. return GMP_LIMB_BITS - shift;
  1001. }
  1002. static size_t
  1003. mpn_get_str_bits (unsigned char *sp, unsigned bits, mp_srcptr up, mp_size_t un)
  1004. {
  1005. unsigned char mask;
  1006. size_t sn, j;
  1007. mp_size_t i;
  1008. unsigned shift;
  1009. sn = ((un - 1) * GMP_LIMB_BITS + mpn_limb_size_in_base_2 (up[un-1])
  1010. + bits - 1) / bits;
  1011. mask = (1U << bits) - 1;
  1012. for (i = 0, j = sn, shift = 0; j-- > 0;)
  1013. {
  1014. unsigned char digit = up[i] >> shift;
  1015. shift += bits;
  1016. if (shift >= GMP_LIMB_BITS && ++i < un)
  1017. {
  1018. shift -= GMP_LIMB_BITS;
  1019. digit |= up[i] << (bits - shift);
  1020. }
  1021. sp[j] = digit & mask;
  1022. }
  1023. return sn;
  1024. }
  1025. /* We generate digits from the least significant end, and reverse at
  1026. the end. */
  1027. static size_t
  1028. mpn_limb_get_str (unsigned char *sp, mp_limb_t w,
  1029. const struct gmp_div_inverse *binv)
  1030. {
  1031. mp_size_t i;
  1032. for (i = 0; w > 0; i++)
  1033. {
  1034. mp_limb_t h, l, r;
  1035. h = w >> (GMP_LIMB_BITS - binv->shift);
  1036. l = w << binv->shift;
  1037. gmp_udiv_qrnnd_preinv (w, r, h, l, binv->d1, binv->di);
  1038. assert ((r & (GMP_LIMB_MAX >> (GMP_LIMB_BITS - binv->shift))) == 0);
  1039. r >>= binv->shift;
  1040. sp[i] = r;
  1041. }
  1042. return i;
  1043. }
  1044. static size_t
  1045. mpn_get_str_other (unsigned char *sp,
  1046. int base, const struct mpn_base_info *info,
  1047. mp_ptr up, mp_size_t un)
  1048. {
  1049. struct gmp_div_inverse binv;
  1050. size_t sn;
  1051. size_t i;
  1052. mpn_div_qr_1_invert (&binv, base);
  1053. sn = 0;
  1054. if (un > 1)
  1055. {
  1056. struct gmp_div_inverse bbinv;
  1057. mpn_div_qr_1_invert (&bbinv, info->bb);
  1058. do
  1059. {
  1060. mp_limb_t w;
  1061. size_t done;
  1062. w = mpn_div_qr_1_preinv (up, up, un, &bbinv);
  1063. un -= (up[un-1] == 0);
  1064. done = mpn_limb_get_str (sp + sn, w, &binv);
  1065. for (sn += done; done < info->exp; done++)
  1066. sp[sn++] = 0;
  1067. }
  1068. while (un > 1);
  1069. }
  1070. sn += mpn_limb_get_str (sp + sn, up[0], &binv);
  1071. /* Reverse order */
  1072. for (i = 0; 2*i + 1 < sn; i++)
  1073. {
  1074. unsigned char t = sp[i];
  1075. sp[i] = sp[sn - i - 1];
  1076. sp[sn - i - 1] = t;
  1077. }
  1078. return sn;
  1079. }
  1080. size_t
  1081. mpn_get_str (unsigned char *sp, int base, mp_ptr up, mp_size_t un)
  1082. {
  1083. unsigned bits;
  1084. assert (un > 0);
  1085. assert (up[un-1] > 0);
  1086. bits = mpn_base_power_of_two_p (base);
  1087. if (bits)
  1088. return mpn_get_str_bits (sp, bits, up, un);
  1089. else
  1090. {
  1091. struct mpn_base_info info;
  1092. mpn_get_base_info (&info, base);
  1093. return mpn_get_str_other (sp, base, &info, up, un);
  1094. }
  1095. }
  1096. static mp_size_t
  1097. mpn_set_str_bits (mp_ptr rp, const unsigned char *sp, size_t sn,
  1098. unsigned bits)
  1099. {
  1100. mp_size_t rn;
  1101. size_t j;
  1102. unsigned shift;
  1103. for (j = sn, rn = 0, shift = 0; j-- > 0; )
  1104. {
  1105. if (shift == 0)
  1106. {
  1107. rp[rn++] = sp[j];
  1108. shift += bits;
  1109. }
  1110. else
  1111. {
  1112. rp[rn-1] |= (mp_limb_t) sp[j] << shift;
  1113. shift += bits;
  1114. if (shift >= GMP_LIMB_BITS)
  1115. {
  1116. shift -= GMP_LIMB_BITS;
  1117. if (shift > 0)
  1118. rp[rn++] = (mp_limb_t) sp[j] >> (bits - shift);
  1119. }
  1120. }
  1121. }
  1122. rn = mpn_normalized_size (rp, rn);
  1123. return rn;
  1124. }
  1125. /* Result is usually normalized, except for all-zero input, in which
  1126. case a single zero limb is written at *RP, and 1 is returned. */
  1127. static mp_size_t
  1128. mpn_set_str_other (mp_ptr rp, const unsigned char *sp, size_t sn,
  1129. mp_limb_t b, const struct mpn_base_info *info)
  1130. {
  1131. mp_size_t rn;
  1132. mp_limb_t w;
  1133. unsigned k;
  1134. size_t j;
  1135. assert (sn > 0);
  1136. k = 1 + (sn - 1) % info->exp;
  1137. j = 0;
  1138. w = sp[j++];
  1139. while (--k != 0)
  1140. w = w * b + sp[j++];
  1141. rp[0] = w;
  1142. for (rn = 1; j < sn;)
  1143. {
  1144. mp_limb_t cy;
  1145. w = sp[j++];
  1146. for (k = 1; k < info->exp; k++)
  1147. w = w * b + sp[j++];
  1148. cy = mpn_mul_1 (rp, rp, rn, info->bb);
  1149. cy += mpn_add_1 (rp, rp, rn, w);
  1150. if (cy > 0)
  1151. rp[rn++] = cy;
  1152. }
  1153. assert (j == sn);
  1154. return rn;
  1155. }
  1156. mp_size_t
  1157. mpn_set_str (mp_ptr rp, const unsigned char *sp, size_t sn, int base)
  1158. {
  1159. unsigned bits;
  1160. if (sn == 0)
  1161. return 0;
  1162. bits = mpn_base_power_of_two_p (base);
  1163. if (bits)
  1164. return mpn_set_str_bits (rp, sp, sn, bits);
  1165. else
  1166. {
  1167. struct mpn_base_info info;
  1168. mpn_get_base_info (&info, base);
  1169. return mpn_set_str_other (rp, sp, sn, base, &info);
  1170. }
  1171. }
  1172. /* MPZ interface */
  1173. void
  1174. mpz_init (mpz_t r)
  1175. {
  1176. static const mp_limb_t dummy_limb = GMP_LIMB_MAX & 0xc1a0;
  1177. r->_mp_alloc = 0;
  1178. r->_mp_size = 0;
  1179. r->_mp_d = (mp_ptr) &dummy_limb;
  1180. }
  1181. /* The utility of this function is a bit limited, since many functions
  1182. assigns the result variable using mpz_swap. */
  1183. void
  1184. mpz_init2 (mpz_t r, mp_bitcnt_t bits)
  1185. {
  1186. mp_size_t rn;
  1187. bits -= (bits != 0); /* Round down, except if 0 */
  1188. rn = 1 + bits / GMP_LIMB_BITS;
  1189. r->_mp_alloc = rn;
  1190. r->_mp_size = 0;
  1191. r->_mp_d = gmp_xalloc_limbs (rn);
  1192. }
  1193. void
  1194. mpz_clear (mpz_t r)
  1195. {
  1196. if (r->_mp_alloc)
  1197. gmp_free (r->_mp_d);
  1198. }
  1199. static mp_ptr
  1200. mpz_realloc (mpz_t r, mp_size_t size)
  1201. {
  1202. size = GMP_MAX (size, 1);
  1203. if (r->_mp_alloc)
  1204. r->_mp_d = gmp_xrealloc_limbs (r->_mp_d, size);
  1205. else
  1206. r->_mp_d = gmp_xalloc_limbs (size);
  1207. r->_mp_alloc = size;
  1208. if (GMP_ABS (r->_mp_size) > size)
  1209. r->_mp_size = 0;
  1210. return r->_mp_d;
  1211. }
  1212. /* Realloc for an mpz_t WHAT if it has less than NEEDED limbs. */
  1213. #define MPZ_REALLOC(z,n) ((n) > (z)->_mp_alloc \
  1214. ? mpz_realloc(z,n) \
  1215. : (z)->_mp_d)
  1216. /* MPZ assignment and basic conversions. */
  1217. void
  1218. mpz_set_si (mpz_t r, signed long int x)
  1219. {
  1220. if (x >= 0)
  1221. mpz_set_ui (r, x);
  1222. else /* (x < 0) */
  1223. if (GMP_LIMB_BITS < GMP_ULONG_BITS)
  1224. {
  1225. mpz_set_ui (r, GMP_NEG_CAST (unsigned long int, x));
  1226. mpz_neg (r, r);
  1227. }
  1228. else
  1229. {
  1230. r->_mp_size = -1;
  1231. MPZ_REALLOC (r, 1)[0] = GMP_NEG_CAST (unsigned long int, x);
  1232. }
  1233. }
  1234. void
  1235. mpz_set_ui (mpz_t r, unsigned long int x)
  1236. {
  1237. if (x > 0)
  1238. {
  1239. r->_mp_size = 1;
  1240. MPZ_REALLOC (r, 1)[0] = x;
  1241. if (GMP_LIMB_BITS < GMP_ULONG_BITS)
  1242. {
  1243. int LOCAL_GMP_LIMB_BITS = GMP_LIMB_BITS;
  1244. while (x >>= LOCAL_GMP_LIMB_BITS)
  1245. {
  1246. ++ r->_mp_size;
  1247. MPZ_REALLOC (r, r->_mp_size)[r->_mp_size - 1] = x;
  1248. }
  1249. }
  1250. }
  1251. else
  1252. r->_mp_size = 0;
  1253. }
  1254. void
  1255. mpz_set (mpz_t r, const mpz_t x)
  1256. {
  1257. /* Allow the NOP r == x */
  1258. if (r != x)
  1259. {
  1260. mp_size_t n;
  1261. mp_ptr rp;
  1262. n = GMP_ABS (x->_mp_size);
  1263. rp = MPZ_REALLOC (r, n);
  1264. mpn_copyi (rp, x->_mp_d, n);
  1265. r->_mp_size = x->_mp_size;
  1266. }
  1267. }
  1268. void
  1269. mpz_init_set_si (mpz_t r, signed long int x)
  1270. {
  1271. mpz_init (r);
  1272. mpz_set_si (r, x);
  1273. }
  1274. void
  1275. mpz_init_set_ui (mpz_t r, unsigned long int x)
  1276. {
  1277. mpz_init (r);
  1278. mpz_set_ui (r, x);
  1279. }
  1280. void
  1281. mpz_init_set (mpz_t r, const mpz_t x)
  1282. {
  1283. mpz_init (r);
  1284. mpz_set (r, x);
  1285. }
  1286. int
  1287. mpz_fits_slong_p (const mpz_t u)
  1288. {
  1289. return (LONG_MAX + LONG_MIN == 0 || mpz_cmp_ui (u, LONG_MAX) <= 0) &&
  1290. mpz_cmpabs_ui (u, GMP_NEG_CAST (unsigned long int, LONG_MIN)) <= 0;
  1291. }
  1292. static int
  1293. mpn_absfits_ulong_p (mp_srcptr up, mp_size_t un)
  1294. {
  1295. int ulongsize = GMP_ULONG_BITS / GMP_LIMB_BITS;
  1296. mp_limb_t ulongrem = 0;
  1297. if (GMP_ULONG_BITS % GMP_LIMB_BITS != 0)
  1298. ulongrem = (mp_limb_t) (ULONG_MAX >> GMP_LIMB_BITS * ulongsize) + 1;
  1299. return un <= ulongsize || (up[ulongsize] < ulongrem && un == ulongsize + 1);
  1300. }
  1301. int
  1302. mpz_fits_ulong_p (const mpz_t u)
  1303. {
  1304. mp_size_t us = u->_mp_size;
  1305. return us >= 0 && mpn_absfits_ulong_p (u->_mp_d, us);
  1306. }
  1307. long int
  1308. mpz_get_si (const mpz_t u)
  1309. {
  1310. unsigned long r = mpz_get_ui (u);
  1311. unsigned long c = -LONG_MAX - LONG_MIN;
  1312. if (u->_mp_size < 0)
  1313. /* This expression is necessary to properly handle -LONG_MIN */
  1314. return -(long) c - (long) ((r - c) & LONG_MAX);
  1315. else
  1316. return (long) (r & LONG_MAX);
  1317. }
  1318. unsigned long int
  1319. mpz_get_ui (const mpz_t u)
  1320. {
  1321. if (GMP_LIMB_BITS < GMP_ULONG_BITS)
  1322. {
  1323. int LOCAL_GMP_LIMB_BITS = GMP_LIMB_BITS;
  1324. unsigned long r = 0;
  1325. mp_size_t n = GMP_ABS (u->_mp_size);
  1326. n = GMP_MIN (n, 1 + (mp_size_t) (GMP_ULONG_BITS - 1) / GMP_LIMB_BITS);
  1327. while (--n >= 0)
  1328. r = (r << LOCAL_GMP_LIMB_BITS) + u->_mp_d[n];
  1329. return r;
  1330. }
  1331. return u->_mp_size == 0 ? 0 : u->_mp_d[0];
  1332. }
  1333. size_t
  1334. mpz_size (const mpz_t u)
  1335. {
  1336. return GMP_ABS (u->_mp_size);
  1337. }
  1338. mp_limb_t
  1339. mpz_getlimbn (const mpz_t u, mp_size_t n)
  1340. {
  1341. if (n >= 0 && n < GMP_ABS (u->_mp_size))
  1342. return u->_mp_d[n];
  1343. else
  1344. return 0;
  1345. }
  1346. void
  1347. mpz_realloc2 (mpz_t x, mp_bitcnt_t n)
  1348. {
  1349. mpz_realloc (x, 1 + (n - (n != 0)) / GMP_LIMB_BITS);
  1350. }
  1351. mp_srcptr
  1352. mpz_limbs_read (mpz_srcptr x)
  1353. {
  1354. return x->_mp_d;
  1355. }
  1356. mp_ptr
  1357. mpz_limbs_modify (mpz_t x, mp_size_t n)
  1358. {
  1359. assert (n > 0);
  1360. return MPZ_REALLOC (x, n);
  1361. }
  1362. mp_ptr
  1363. mpz_limbs_write (mpz_t x, mp_size_t n)
  1364. {
  1365. return mpz_limbs_modify (x, n);
  1366. }
  1367. void
  1368. mpz_limbs_finish (mpz_t x, mp_size_t xs)
  1369. {
  1370. mp_size_t xn;
  1371. xn = mpn_normalized_size (x->_mp_d, GMP_ABS (xs));
  1372. x->_mp_size = xs < 0 ? -xn : xn;
  1373. }
  1374. static mpz_srcptr
  1375. mpz_roinit_normal_n (mpz_t x, mp_srcptr xp, mp_size_t xs)
  1376. {
  1377. x->_mp_alloc = 0;
  1378. x->_mp_d = (mp_ptr) xp;
  1379. x->_mp_size = xs;
  1380. return x;
  1381. }
  1382. mpz_srcptr
  1383. mpz_roinit_n (mpz_t x, mp_srcptr xp, mp_size_t xs)
  1384. {
  1385. mpz_roinit_normal_n (x, xp, xs);
  1386. mpz_limbs_finish (x, xs);
  1387. return x;
  1388. }
  1389. /* Conversions and comparison to double. */
  1390. void
  1391. mpz_set_d (mpz_t r, double x)
  1392. {
  1393. int sign;
  1394. mp_ptr rp;
  1395. mp_size_t rn, i;
  1396. double B;
  1397. double Bi;
  1398. mp_limb_t f;
  1399. /* x != x is true when x is a NaN, and x == x * 0.5 is true when x is
  1400. zero or infinity. */
  1401. if (x != x || x == x * 0.5)
  1402. {
  1403. r->_mp_size = 0;
  1404. return;
  1405. }
  1406. sign = x < 0.0 ;
  1407. if (sign)
  1408. x = - x;
  1409. if (x < 1.0)
  1410. {
  1411. r->_mp_size = 0;
  1412. return;
  1413. }
  1414. B = 4.0 * (double) (GMP_LIMB_HIGHBIT >> 1);
  1415. Bi = 1.0 / B;
  1416. for (rn = 1; x >= B; rn++)
  1417. x *= Bi;
  1418. rp = MPZ_REALLOC (r, rn);
  1419. f = (mp_limb_t) x;
  1420. x -= f;
  1421. assert (x < 1.0);
  1422. i = rn-1;
  1423. rp[i] = f;
  1424. while (--i >= 0)
  1425. {
  1426. x = B * x;
  1427. f = (mp_limb_t) x;
  1428. x -= f;
  1429. assert (x < 1.0);
  1430. rp[i] = f;
  1431. }
  1432. r->_mp_size = sign ? - rn : rn;
  1433. }
  1434. void
  1435. mpz_init_set_d (mpz_t r, double x)
  1436. {
  1437. mpz_init (r);
  1438. mpz_set_d (r, x);
  1439. }
  1440. double
  1441. mpz_get_d (const mpz_t u)
  1442. {
  1443. int m;
  1444. mp_limb_t l;
  1445. mp_size_t un;
  1446. double x;
  1447. double B = 4.0 * (double) (GMP_LIMB_HIGHBIT >> 1);
  1448. un = GMP_ABS (u->_mp_size);
  1449. if (un == 0)
  1450. return 0.0;
  1451. l = u->_mp_d[--un];
  1452. gmp_clz (m, l);
  1453. m = m + GMP_DBL_MANT_BITS - GMP_LIMB_BITS;
  1454. if (m < 0)
  1455. l &= GMP_LIMB_MAX << -m;
  1456. for (x = l; --un >= 0;)
  1457. {
  1458. x = B*x;
  1459. if (m > 0) {
  1460. l = u->_mp_d[un];
  1461. m -= GMP_LIMB_BITS;
  1462. if (m < 0)
  1463. l &= GMP_LIMB_MAX << -m;
  1464. x += l;
  1465. }
  1466. }
  1467. if (u->_mp_size < 0)
  1468. x = -x;
  1469. return x;
  1470. }
  1471. int
  1472. mpz_cmpabs_d (const mpz_t x, double d)
  1473. {
  1474. mp_size_t xn;
  1475. double B, Bi;
  1476. mp_size_t i;
  1477. xn = x->_mp_size;
  1478. d = GMP_ABS (d);
  1479. if (xn != 0)
  1480. {
  1481. xn = GMP_ABS (xn);
  1482. B = 4.0 * (double) (GMP_LIMB_HIGHBIT >> 1);
  1483. Bi = 1.0 / B;
  1484. /* Scale d so it can be compared with the top limb. */
  1485. for (i = 1; i < xn; i++)
  1486. d *= Bi;
  1487. if (d >= B)
  1488. return -1;
  1489. /* Compare floor(d) to top limb, subtract and cancel when equal. */
  1490. for (i = xn; i-- > 0;)
  1491. {
  1492. mp_limb_t f, xl;
  1493. f = (mp_limb_t) d;
  1494. xl = x->_mp_d[i];
  1495. if (xl > f)
  1496. return 1;
  1497. else if (xl < f)
  1498. return -1;
  1499. d = B * (d - f);
  1500. }
  1501. }
  1502. return - (d > 0.0);
  1503. }
  1504. int
  1505. mpz_cmp_d (const mpz_t x, double d)
  1506. {
  1507. if (x->_mp_size < 0)
  1508. {
  1509. if (d >= 0.0)
  1510. return -1;
  1511. else
  1512. return -mpz_cmpabs_d (x, d);
  1513. }
  1514. else
  1515. {
  1516. if (d < 0.0)
  1517. return 1;
  1518. else
  1519. return mpz_cmpabs_d (x, d);
  1520. }
  1521. }
  1522. /* MPZ comparisons and the like. */
  1523. int
  1524. mpz_sgn (const mpz_t u)
  1525. {
  1526. return GMP_CMP (u->_mp_size, 0);
  1527. }
  1528. int
  1529. mpz_cmp_si (const mpz_t u, long v)
  1530. {
  1531. mp_size_t usize = u->_mp_size;
  1532. if (v >= 0)
  1533. return mpz_cmp_ui (u, v);
  1534. else if (usize >= 0)
  1535. return 1;
  1536. else
  1537. return - mpz_cmpabs_ui (u, GMP_NEG_CAST (unsigned long int, v));
  1538. }
  1539. int
  1540. mpz_cmp_ui (const mpz_t u, unsigned long v)
  1541. {
  1542. mp_size_t usize = u->_mp_size;
  1543. if (usize < 0)
  1544. return -1;
  1545. else
  1546. return mpz_cmpabs_ui (u, v);
  1547. }
  1548. int
  1549. mpz_cmp (const mpz_t a, const mpz_t b)
  1550. {
  1551. mp_size_t asize = a->_mp_size;
  1552. mp_size_t bsize = b->_mp_size;
  1553. if (asize != bsize)
  1554. return (asize < bsize) ? -1 : 1;
  1555. else if (asize >= 0)
  1556. return mpn_cmp (a->_mp_d, b->_mp_d, asize);
  1557. else
  1558. return mpn_cmp (b->_mp_d, a->_mp_d, -asize);
  1559. }
  1560. int
  1561. mpz_cmpabs_ui (const mpz_t u, unsigned long v)
  1562. {
  1563. mp_size_t un = GMP_ABS (u->_mp_size);
  1564. if (! mpn_absfits_ulong_p (u->_mp_d, un))
  1565. return 1;
  1566. else
  1567. {
  1568. unsigned long uu = mpz_get_ui (u);
  1569. return GMP_CMP(uu, v);
  1570. }
  1571. }
  1572. int
  1573. mpz_cmpabs (const mpz_t u, const mpz_t v)
  1574. {
  1575. return mpn_cmp4 (u->_mp_d, GMP_ABS (u->_mp_size),
  1576. v->_mp_d, GMP_ABS (v->_mp_size));
  1577. }
  1578. void
  1579. mpz_abs (mpz_t r, const mpz_t u)
  1580. {
  1581. mpz_set (r, u);
  1582. r->_mp_size = GMP_ABS (r->_mp_size);
  1583. }
  1584. void
  1585. mpz_neg (mpz_t r, const mpz_t u)
  1586. {
  1587. mpz_set (r, u);
  1588. r->_mp_size = -r->_mp_size;
  1589. }
  1590. void
  1591. mpz_swap (mpz_t u, mpz_t v)
  1592. {
  1593. MP_SIZE_T_SWAP (u->_mp_size, v->_mp_size);
  1594. MP_SIZE_T_SWAP (u->_mp_alloc, v->_mp_alloc);
  1595. MP_PTR_SWAP (u->_mp_d, v->_mp_d);
  1596. }
  1597. /* MPZ addition and subtraction */
  1598. void
  1599. mpz_add_ui (mpz_t r, const mpz_t a, unsigned long b)
  1600. {
  1601. mpz_t bb;
  1602. mpz_init_set_ui (bb, b);
  1603. mpz_add (r, a, bb);
  1604. mpz_clear (bb);
  1605. }
  1606. void
  1607. mpz_sub_ui (mpz_t r, const mpz_t a, unsigned long b)
  1608. {
  1609. mpz_ui_sub (r, b, a);
  1610. mpz_neg (r, r);
  1611. }
  1612. void
  1613. mpz_ui_sub (mpz_t r, unsigned long a, const mpz_t b)
  1614. {
  1615. mpz_neg (r, b);
  1616. mpz_add_ui (r, r, a);
  1617. }
  1618. static mp_size_t
  1619. mpz_abs_add (mpz_t r, const mpz_t a, const mpz_t b)
  1620. {
  1621. mp_size_t an = GMP_ABS (a->_mp_size);
  1622. mp_size_t bn = GMP_ABS (b->_mp_size);
  1623. mp_ptr rp;
  1624. mp_limb_t cy;
  1625. if (an < bn)
  1626. {
  1627. MPZ_SRCPTR_SWAP (a, b);
  1628. MP_SIZE_T_SWAP (an, bn);
  1629. }
  1630. rp = MPZ_REALLOC (r, an + 1);
  1631. cy = mpn_add (rp, a->_mp_d, an, b->_mp_d, bn);
  1632. rp[an] = cy;
  1633. return an + cy;
  1634. }
  1635. static mp_size_t
  1636. mpz_abs_sub (mpz_t r, const mpz_t a, const mpz_t b)
  1637. {
  1638. mp_size_t an = GMP_ABS (a->_mp_size);
  1639. mp_size_t bn = GMP_ABS (b->_mp_size);
  1640. int cmp;
  1641. mp_ptr rp;
  1642. cmp = mpn_cmp4 (a->_mp_d, an, b->_mp_d, bn);
  1643. if (cmp > 0)
  1644. {
  1645. rp = MPZ_REALLOC (r, an);
  1646. gmp_assert_nocarry (mpn_sub (rp, a->_mp_d, an, b->_mp_d, bn));
  1647. return mpn_normalized_size (rp, an);
  1648. }
  1649. else if (cmp < 0)
  1650. {
  1651. rp = MPZ_REALLOC (r, bn);
  1652. gmp_assert_nocarry (mpn_sub (rp, b->_mp_d, bn, a->_mp_d, an));
  1653. return -mpn_normalized_size (rp, bn);
  1654. }
  1655. else
  1656. return 0;
  1657. }
  1658. void
  1659. mpz_add (mpz_t r, const mpz_t a, const mpz_t b)
  1660. {
  1661. mp_size_t rn;
  1662. if ( (a->_mp_size ^ b->_mp_size) >= 0)
  1663. rn = mpz_abs_add (r, a, b);
  1664. else
  1665. rn = mpz_abs_sub (r, a, b);
  1666. r->_mp_size = a->_mp_size >= 0 ? rn : - rn;
  1667. }
  1668. void
  1669. mpz_sub (mpz_t r, const mpz_t a, const mpz_t b)
  1670. {
  1671. mp_size_t rn;
  1672. if ( (a->_mp_size ^ b->_mp_size) >= 0)
  1673. rn = mpz_abs_sub (r, a, b);
  1674. else
  1675. rn = mpz_abs_add (r, a, b);
  1676. r->_mp_size = a->_mp_size >= 0 ? rn : - rn;
  1677. }
  1678. /* MPZ multiplication */
  1679. void
  1680. mpz_mul_si (mpz_t r, const mpz_t u, long int v)
  1681. {
  1682. if (v < 0)
  1683. {
  1684. mpz_mul_ui (r, u, GMP_NEG_CAST (unsigned long int, v));
  1685. mpz_neg (r, r);
  1686. }
  1687. else
  1688. mpz_mul_ui (r, u, v);
  1689. }
  1690. void
  1691. mpz_mul_ui (mpz_t r, const mpz_t u, unsigned long int v)
  1692. {
  1693. mpz_t vv;
  1694. mpz_init_set_ui (vv, v);
  1695. mpz_mul (r, u, vv);
  1696. mpz_clear (vv);
  1697. return;
  1698. }
  1699. void
  1700. mpz_mul (mpz_t r, const mpz_t u, const mpz_t v)
  1701. {
  1702. int sign;
  1703. mp_size_t un, vn, rn;
  1704. mpz_t t;
  1705. mp_ptr tp;
  1706. un = u->_mp_size;
  1707. vn = v->_mp_size;
  1708. if (un == 0 || vn == 0)
  1709. {
  1710. r->_mp_size = 0;
  1711. return;
  1712. }
  1713. sign = (un ^ vn) < 0;
  1714. un = GMP_ABS (un);
  1715. vn = GMP_ABS (vn);
  1716. mpz_init2 (t, (un + vn) * GMP_LIMB_BITS);
  1717. tp = t->_mp_d;
  1718. if (un >= vn)
  1719. mpn_mul (tp, u->_mp_d, un, v->_mp_d, vn);
  1720. else
  1721. mpn_mul (tp, v->_mp_d, vn, u->_mp_d, un);
  1722. rn = un + vn;
  1723. rn -= tp[rn-1] == 0;
  1724. t->_mp_size = sign ? - rn : rn;
  1725. mpz_swap (r, t);
  1726. mpz_clear (t);
  1727. }
  1728. void
  1729. mpz_mul_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t bits)
  1730. {
  1731. mp_size_t un, rn;
  1732. mp_size_t limbs;
  1733. unsigned shift;
  1734. mp_ptr rp;
  1735. un = GMP_ABS (u->_mp_size);
  1736. if (un == 0)
  1737. {
  1738. r->_mp_size = 0;
  1739. return;
  1740. }
  1741. limbs = bits / GMP_LIMB_BITS;
  1742. shift = bits % GMP_LIMB_BITS;
  1743. rn = un + limbs + (shift > 0);
  1744. rp = MPZ_REALLOC (r, rn);
  1745. if (shift > 0)
  1746. {
  1747. mp_limb_t cy = mpn_lshift (rp + limbs, u->_mp_d, un, shift);
  1748. rp[rn-1] = cy;
  1749. rn -= (cy == 0);
  1750. }
  1751. else
  1752. mpn_copyd (rp + limbs, u->_mp_d, un);
  1753. mpn_zero (rp, limbs);
  1754. r->_mp_size = (u->_mp_size < 0) ? - rn : rn;
  1755. }
  1756. void
  1757. mpz_addmul_ui (mpz_t r, const mpz_t u, unsigned long int v)
  1758. {
  1759. mpz_t t;
  1760. mpz_init_set_ui (t, v);
  1761. mpz_mul (t, u, t);
  1762. mpz_add (r, r, t);
  1763. mpz_clear (t);
  1764. }
  1765. void
  1766. mpz_submul_ui (mpz_t r, const mpz_t u, unsigned long int v)
  1767. {
  1768. mpz_t t;
  1769. mpz_init_set_ui (t, v);
  1770. mpz_mul (t, u, t);
  1771. mpz_sub (r, r, t);
  1772. mpz_clear (t);
  1773. }
  1774. void
  1775. mpz_addmul (mpz_t r, const mpz_t u, const mpz_t v)
  1776. {
  1777. mpz_t t;
  1778. mpz_init (t);
  1779. mpz_mul (t, u, v);
  1780. mpz_add (r, r, t);
  1781. mpz_clear (t);
  1782. }
  1783. void
  1784. mpz_submul (mpz_t r, const mpz_t u, const mpz_t v)
  1785. {
  1786. mpz_t t;
  1787. mpz_init (t);
  1788. mpz_mul (t, u, v);
  1789. mpz_sub (r, r, t);
  1790. mpz_clear (t);
  1791. }
  1792. /* MPZ division */
  1793. enum mpz_div_round_mode { GMP_DIV_FLOOR, GMP_DIV_CEIL, GMP_DIV_TRUNC };
  1794. /* Allows q or r to be zero. Returns 1 iff remainder is non-zero. */
  1795. static int
  1796. mpz_div_qr (mpz_t q, mpz_t r,
  1797. const mpz_t n, const mpz_t d, enum mpz_div_round_mode mode)
  1798. {
  1799. mp_size_t ns, ds, nn, dn, qs;
  1800. ns = n->_mp_size;
  1801. ds = d->_mp_size;
  1802. if (ds == 0)
  1803. gmp_die("mpz_div_qr: Divide by zero.");
  1804. if (ns == 0)
  1805. {
  1806. if (q)
  1807. q->_mp_size = 0;
  1808. if (r)
  1809. r->_mp_size = 0;
  1810. return 0;
  1811. }
  1812. nn = GMP_ABS (ns);
  1813. dn = GMP_ABS (ds);
  1814. qs = ds ^ ns;
  1815. if (nn < dn)
  1816. {
  1817. if (mode == GMP_DIV_CEIL && qs >= 0)
  1818. {
  1819. /* q = 1, r = n - d */
  1820. if (r)
  1821. mpz_sub (r, n, d);
  1822. if (q)
  1823. mpz_set_ui (q, 1);
  1824. }
  1825. else if (mode == GMP_DIV_FLOOR && qs < 0)
  1826. {
  1827. /* q = -1, r = n + d */
  1828. if (r)
  1829. mpz_add (r, n, d);
  1830. if (q)
  1831. mpz_set_si (q, -1);
  1832. }
  1833. else
  1834. {
  1835. /* q = 0, r = d */
  1836. if (r)
  1837. mpz_set (r, n);
  1838. if (q)
  1839. q->_mp_size = 0;
  1840. }
  1841. return 1;
  1842. }
  1843. else
  1844. {
  1845. mp_ptr np, qp;
  1846. mp_size_t qn, rn;
  1847. mpz_t tq, tr;
  1848. mpz_init_set (tr, n);
  1849. np = tr->_mp_d;
  1850. qn = nn - dn + 1;
  1851. if (q)
  1852. {
  1853. mpz_init2 (tq, qn * GMP_LIMB_BITS);
  1854. qp = tq->_mp_d;
  1855. }
  1856. else
  1857. qp = NULL;
  1858. mpn_div_qr (qp, np, nn, d->_mp_d, dn);
  1859. if (qp)
  1860. {
  1861. qn -= (qp[qn-1] == 0);
  1862. tq->_mp_size = qs < 0 ? -qn : qn;
  1863. }
  1864. rn = mpn_normalized_size (np, dn);
  1865. tr->_mp_size = ns < 0 ? - rn : rn;
  1866. if (mode == GMP_DIV_FLOOR && qs < 0 && rn != 0)
  1867. {
  1868. if (q)
  1869. mpz_sub_ui (tq, tq, 1);
  1870. if (r)
  1871. mpz_add (tr, tr, d);
  1872. }
  1873. else if (mode == GMP_DIV_CEIL && qs >= 0 && rn != 0)
  1874. {
  1875. if (q)
  1876. mpz_add_ui (tq, tq, 1);
  1877. if (r)
  1878. mpz_sub (tr, tr, d);
  1879. }
  1880. if (q)
  1881. {
  1882. mpz_swap (tq, q);
  1883. mpz_clear (tq);
  1884. }
  1885. if (r)
  1886. mpz_swap (tr, r);
  1887. mpz_clear (tr);
  1888. return rn != 0;
  1889. }
  1890. }
  1891. void
  1892. mpz_cdiv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
  1893. {
  1894. mpz_div_qr (q, r, n, d, GMP_DIV_CEIL);
  1895. }
  1896. void
  1897. mpz_fdiv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
  1898. {
  1899. mpz_div_qr (q, r, n, d, GMP_DIV_FLOOR);
  1900. }
  1901. void
  1902. mpz_tdiv_qr (mpz_t q, mpz_t r, const mpz_t n, const mpz_t d)
  1903. {
  1904. mpz_div_qr (q, r, n, d, GMP_DIV_TRUNC);
  1905. }
  1906. void
  1907. mpz_cdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
  1908. {
  1909. mpz_div_qr (q, NULL, n, d, GMP_DIV_CEIL);
  1910. }
  1911. void
  1912. mpz_fdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
  1913. {
  1914. mpz_div_qr (q, NULL, n, d, GMP_DIV_FLOOR);
  1915. }
  1916. void
  1917. mpz_tdiv_q (mpz_t q, const mpz_t n, const mpz_t d)
  1918. {
  1919. mpz_div_qr (q, NULL, n, d, GMP_DIV_TRUNC);
  1920. }
  1921. void
  1922. mpz_cdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
  1923. {
  1924. mpz_div_qr (NULL, r, n, d, GMP_DIV_CEIL);
  1925. }
  1926. void
  1927. mpz_fdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
  1928. {
  1929. mpz_div_qr (NULL, r, n, d, GMP_DIV_FLOOR);
  1930. }
  1931. void
  1932. mpz_tdiv_r (mpz_t r, const mpz_t n, const mpz_t d)
  1933. {
  1934. mpz_div_qr (NULL, r, n, d, GMP_DIV_TRUNC);
  1935. }
  1936. void
  1937. mpz_mod (mpz_t r, const mpz_t n, const mpz_t d)
  1938. {
  1939. mpz_div_qr (NULL, r, n, d, d->_mp_size >= 0 ? GMP_DIV_FLOOR : GMP_DIV_CEIL);
  1940. }
  1941. static void
  1942. mpz_div_q_2exp (mpz_t q, const mpz_t u, mp_bitcnt_t bit_index,
  1943. enum mpz_div_round_mode mode)
  1944. {
  1945. mp_size_t un, qn;
  1946. mp_size_t limb_cnt;
  1947. mp_ptr qp;
  1948. int adjust;
  1949. un = u->_mp_size;
  1950. if (un == 0)
  1951. {
  1952. q->_mp_size = 0;
  1953. return;
  1954. }
  1955. limb_cnt = bit_index / GMP_LIMB_BITS;
  1956. qn = GMP_ABS (un) - limb_cnt;
  1957. bit_index %= GMP_LIMB_BITS;
  1958. if (mode == ((un > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* un != 0 here. */
  1959. /* Note: Below, the final indexing at limb_cnt is valid because at
  1960. that point we have qn > 0. */
  1961. adjust = (qn <= 0
  1962. || !mpn_zero_p (u->_mp_d, limb_cnt)
  1963. || (u->_mp_d[limb_cnt]
  1964. & (((mp_limb_t) 1 << bit_index) - 1)));
  1965. else
  1966. adjust = 0;
  1967. if (qn <= 0)
  1968. qn = 0;
  1969. else
  1970. {
  1971. qp = MPZ_REALLOC (q, qn);
  1972. if (bit_index != 0)
  1973. {
  1974. mpn_rshift (qp, u->_mp_d + limb_cnt, qn, bit_index);
  1975. qn -= qp[qn - 1] == 0;
  1976. }
  1977. else
  1978. {
  1979. mpn_copyi (qp, u->_mp_d + limb_cnt, qn);
  1980. }
  1981. }
  1982. q->_mp_size = qn;
  1983. if (adjust)
  1984. mpz_add_ui (q, q, 1);
  1985. if (un < 0)
  1986. mpz_neg (q, q);
  1987. }
  1988. static void
  1989. mpz_div_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t bit_index,
  1990. enum mpz_div_round_mode mode)
  1991. {
  1992. mp_size_t us, un, rn;
  1993. mp_ptr rp;
  1994. mp_limb_t mask;
  1995. us = u->_mp_size;
  1996. if (us == 0 || bit_index == 0)
  1997. {
  1998. r->_mp_size = 0;
  1999. return;
  2000. }
  2001. rn = (bit_index + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
  2002. assert (rn > 0);
  2003. rp = MPZ_REALLOC (r, rn);
  2004. un = GMP_ABS (us);
  2005. mask = GMP_LIMB_MAX >> (rn * GMP_LIMB_BITS - bit_index);
  2006. if (rn > un)
  2007. {
  2008. /* Quotient (with truncation) is zero, and remainder is
  2009. non-zero */
  2010. if (mode == ((us > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* us != 0 here. */
  2011. {
  2012. /* Have to negate and sign extend. */
  2013. mp_size_t i;
  2014. gmp_assert_nocarry (! mpn_neg (rp, u->_mp_d, un));
  2015. for (i = un; i < rn - 1; i++)
  2016. rp[i] = GMP_LIMB_MAX;
  2017. rp[rn-1] = mask;
  2018. us = -us;
  2019. }
  2020. else
  2021. {
  2022. /* Just copy */
  2023. if (r != u)
  2024. mpn_copyi (rp, u->_mp_d, un);
  2025. rn = un;
  2026. }
  2027. }
  2028. else
  2029. {
  2030. if (r != u)
  2031. mpn_copyi (rp, u->_mp_d, rn - 1);
  2032. rp[rn-1] = u->_mp_d[rn-1] & mask;
  2033. if (mode == ((us > 0) ? GMP_DIV_CEIL : GMP_DIV_FLOOR)) /* us != 0 here. */
  2034. {
  2035. /* If r != 0, compute 2^{bit_count} - r. */
  2036. mpn_neg (rp, rp, rn);
  2037. rp[rn-1] &= mask;
  2038. /* us is not used for anything else, so we can modify it
  2039. here to indicate flipped sign. */
  2040. us = -us;
  2041. }
  2042. }
  2043. rn = mpn_normalized_size (rp, rn);
  2044. r->_mp_size = us < 0 ? -rn : rn;
  2045. }
  2046. void
  2047. mpz_cdiv_q_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
  2048. {
  2049. mpz_div_q_2exp (r, u, cnt, GMP_DIV_CEIL);
  2050. }
  2051. void
  2052. mpz_fdiv_q_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
  2053. {
  2054. mpz_div_q_2exp (r, u, cnt, GMP_DIV_FLOOR);
  2055. }
  2056. void
  2057. mpz_tdiv_q_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
  2058. {
  2059. mpz_div_q_2exp (r, u, cnt, GMP_DIV_TRUNC);
  2060. }
  2061. void
  2062. mpz_cdiv_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
  2063. {
  2064. mpz_div_r_2exp (r, u, cnt, GMP_DIV_CEIL);
  2065. }
  2066. void
  2067. mpz_fdiv_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
  2068. {
  2069. mpz_div_r_2exp (r, u, cnt, GMP_DIV_FLOOR);
  2070. }
  2071. void
  2072. mpz_tdiv_r_2exp (mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
  2073. {
  2074. mpz_div_r_2exp (r, u, cnt, GMP_DIV_TRUNC);
  2075. }
  2076. void
  2077. mpz_divexact (mpz_t q, const mpz_t n, const mpz_t d)
  2078. {
  2079. gmp_assert_nocarry (mpz_div_qr (q, NULL, n, d, GMP_DIV_TRUNC));
  2080. }
  2081. int
  2082. mpz_divisible_p (const mpz_t n, const mpz_t d)
  2083. {
  2084. return mpz_div_qr (NULL, NULL, n, d, GMP_DIV_TRUNC) == 0;
  2085. }
  2086. int
  2087. mpz_congruent_p (const mpz_t a, const mpz_t b, const mpz_t m)
  2088. {
  2089. mpz_t t;
  2090. int res;
  2091. /* a == b (mod 0) iff a == b */
  2092. if (mpz_sgn (m) == 0)
  2093. return (mpz_cmp (a, b) == 0);
  2094. mpz_init (t);
  2095. mpz_sub (t, a, b);
  2096. res = mpz_divisible_p (t, m);
  2097. mpz_clear (t);
  2098. return res;
  2099. }
  2100. static unsigned long
  2101. mpz_div_qr_ui (mpz_t q, mpz_t r,
  2102. const mpz_t n, unsigned long d, enum mpz_div_round_mode mode)
  2103. {
  2104. unsigned long ret;
  2105. mpz_t rr, dd;
  2106. mpz_init (rr);
  2107. mpz_init_set_ui (dd, d);
  2108. mpz_div_qr (q, rr, n, dd, mode);
  2109. mpz_clear (dd);
  2110. ret = mpz_get_ui (rr);
  2111. if (r)
  2112. mpz_swap (r, rr);
  2113. mpz_clear (rr);
  2114. return ret;
  2115. }
  2116. unsigned long
  2117. mpz_cdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, unsigned long d)
  2118. {
  2119. return mpz_div_qr_ui (q, r, n, d, GMP_DIV_CEIL);
  2120. }
  2121. unsigned long
  2122. mpz_fdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, unsigned long d)
  2123. {
  2124. return mpz_div_qr_ui (q, r, n, d, GMP_DIV_FLOOR);
  2125. }
  2126. unsigned long
  2127. mpz_tdiv_qr_ui (mpz_t q, mpz_t r, const mpz_t n, unsigned long d)
  2128. {
  2129. return mpz_div_qr_ui (q, r, n, d, GMP_DIV_TRUNC);
  2130. }
  2131. unsigned long
  2132. mpz_cdiv_q_ui (mpz_t q, const mpz_t n, unsigned long d)
  2133. {
  2134. return mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_CEIL);
  2135. }
  2136. unsigned long
  2137. mpz_fdiv_q_ui (mpz_t q, const mpz_t n, unsigned long d)
  2138. {
  2139. return mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_FLOOR);
  2140. }
  2141. unsigned long
  2142. mpz_tdiv_q_ui (mpz_t q, const mpz_t n, unsigned long d)
  2143. {
  2144. return mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_TRUNC);
  2145. }
  2146. unsigned long
  2147. mpz_cdiv_r_ui (mpz_t r, const mpz_t n, unsigned long d)
  2148. {
  2149. return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_CEIL);
  2150. }
  2151. unsigned long
  2152. mpz_fdiv_r_ui (mpz_t r, const mpz_t n, unsigned long d)
  2153. {
  2154. return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_FLOOR);
  2155. }
  2156. unsigned long
  2157. mpz_tdiv_r_ui (mpz_t r, const mpz_t n, unsigned long d)
  2158. {
  2159. return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_TRUNC);
  2160. }
  2161. unsigned long
  2162. mpz_cdiv_ui (const mpz_t n, unsigned long d)
  2163. {
  2164. return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_CEIL);
  2165. }
  2166. unsigned long
  2167. mpz_fdiv_ui (const mpz_t n, unsigned long d)
  2168. {
  2169. return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_FLOOR);
  2170. }
  2171. unsigned long
  2172. mpz_tdiv_ui (const mpz_t n, unsigned long d)
  2173. {
  2174. return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_TRUNC);
  2175. }
  2176. unsigned long
  2177. mpz_mod_ui (mpz_t r, const mpz_t n, unsigned long d)
  2178. {
  2179. return mpz_div_qr_ui (NULL, r, n, d, GMP_DIV_FLOOR);
  2180. }
  2181. void
  2182. mpz_divexact_ui (mpz_t q, const mpz_t n, unsigned long d)
  2183. {
  2184. gmp_assert_nocarry (mpz_div_qr_ui (q, NULL, n, d, GMP_DIV_TRUNC));
  2185. }
  2186. int
  2187. mpz_divisible_ui_p (const mpz_t n, unsigned long d)
  2188. {
  2189. return mpz_div_qr_ui (NULL, NULL, n, d, GMP_DIV_TRUNC) == 0;
  2190. }
  2191. /* GCD */
  2192. static mp_limb_t
  2193. mpn_gcd_11 (mp_limb_t u, mp_limb_t v)
  2194. {
  2195. unsigned shift;
  2196. assert ( (u | v) > 0);
  2197. if (u == 0)
  2198. return v;
  2199. else if (v == 0)
  2200. return u;
  2201. gmp_ctz (shift, u | v);
  2202. u >>= shift;
  2203. v >>= shift;
  2204. if ( (u & 1) == 0)
  2205. MP_LIMB_T_SWAP (u, v);
  2206. while ( (v & 1) == 0)
  2207. v >>= 1;
  2208. while (u != v)
  2209. {
  2210. if (u > v)
  2211. {
  2212. u -= v;
  2213. do
  2214. u >>= 1;
  2215. while ( (u & 1) == 0);
  2216. }
  2217. else
  2218. {
  2219. v -= u;
  2220. do
  2221. v >>= 1;
  2222. while ( (v & 1) == 0);
  2223. }
  2224. }
  2225. return u << shift;
  2226. }
  2227. unsigned long
  2228. mpz_gcd_ui (mpz_t g, const mpz_t u, unsigned long v)
  2229. {
  2230. mpz_t t;
  2231. mpz_init_set_ui(t, v);
  2232. mpz_gcd (t, u, t);
  2233. if (v > 0)
  2234. v = mpz_get_ui (t);
  2235. if (g)
  2236. mpz_swap (t, g);
  2237. mpz_clear (t);
  2238. return v;
  2239. }
  2240. static mp_bitcnt_t
  2241. mpz_make_odd (mpz_t r)
  2242. {
  2243. mp_bitcnt_t shift;
  2244. assert (r->_mp_size > 0);
  2245. /* Count trailing zeros, equivalent to mpn_scan1, because we know that there is a 1 */
  2246. shift = mpn_common_scan (r->_mp_d[0], 0, r->_mp_d, 0, 0);
  2247. mpz_tdiv_q_2exp (r, r, shift);
  2248. return shift;
  2249. }
  2250. void
  2251. mpz_gcd (mpz_t g, const mpz_t u, const mpz_t v)
  2252. {
  2253. mpz_t tu, tv;
  2254. mp_bitcnt_t uz, vz, gz;
  2255. if (u->_mp_size == 0)
  2256. {
  2257. mpz_abs (g, v);
  2258. return;
  2259. }
  2260. if (v->_mp_size == 0)
  2261. {
  2262. mpz_abs (g, u);
  2263. return;
  2264. }
  2265. mpz_init (tu);
  2266. mpz_init (tv);
  2267. mpz_abs (tu, u);
  2268. uz = mpz_make_odd (tu);
  2269. mpz_abs (tv, v);
  2270. vz = mpz_make_odd (tv);
  2271. gz = GMP_MIN (uz, vz);
  2272. if (tu->_mp_size < tv->_mp_size)
  2273. mpz_swap (tu, tv);
  2274. mpz_tdiv_r (tu, tu, tv);
  2275. if (tu->_mp_size == 0)
  2276. {
  2277. mpz_swap (g, tv);
  2278. }
  2279. else
  2280. for (;;)
  2281. {
  2282. int c;
  2283. mpz_make_odd (tu);
  2284. c = mpz_cmp (tu, tv);
  2285. if (c == 0)
  2286. {
  2287. mpz_swap (g, tu);
  2288. break;
  2289. }
  2290. if (c < 0)
  2291. mpz_swap (tu, tv);
  2292. if (tv->_mp_size == 1)
  2293. {
  2294. mp_limb_t vl = tv->_mp_d[0];
  2295. mp_limb_t ul = mpz_tdiv_ui (tu, vl);
  2296. mpz_set_ui (g, mpn_gcd_11 (ul, vl));
  2297. break;
  2298. }
  2299. mpz_sub (tu, tu, tv);
  2300. }
  2301. mpz_clear (tu);
  2302. mpz_clear (tv);
  2303. mpz_mul_2exp (g, g, gz);
  2304. }
  2305. void
  2306. mpz_gcdext (mpz_t g, mpz_t s, mpz_t t, const mpz_t u, const mpz_t v)
  2307. {
  2308. mpz_t tu, tv, s0, s1, t0, t1;
  2309. mp_bitcnt_t uz, vz, gz;
  2310. mp_bitcnt_t power;
  2311. if (u->_mp_size == 0)
  2312. {
  2313. /* g = 0 u + sgn(v) v */
  2314. signed long sign = mpz_sgn (v);
  2315. mpz_abs (g, v);
  2316. if (s)
  2317. s->_mp_size = 0;
  2318. if (t)
  2319. mpz_set_si (t, sign);
  2320. return;
  2321. }
  2322. if (v->_mp_size == 0)
  2323. {
  2324. /* g = sgn(u) u + 0 v */
  2325. signed long sign = mpz_sgn (u);
  2326. mpz_abs (g, u);
  2327. if (s)
  2328. mpz_set_si (s, sign);
  2329. if (t)
  2330. t->_mp_size = 0;
  2331. return;
  2332. }
  2333. mpz_init (tu);
  2334. mpz_init (tv);
  2335. mpz_init (s0);
  2336. mpz_init (s1);
  2337. mpz_init (t0);
  2338. mpz_init (t1);
  2339. mpz_abs (tu, u);
  2340. uz = mpz_make_odd (tu);
  2341. mpz_abs (tv, v);
  2342. vz = mpz_make_odd (tv);
  2343. gz = GMP_MIN (uz, vz);
  2344. uz -= gz;
  2345. vz -= gz;
  2346. /* Cofactors corresponding to odd gcd. gz handled later. */
  2347. if (tu->_mp_size < tv->_mp_size)
  2348. {
  2349. mpz_swap (tu, tv);
  2350. MPZ_SRCPTR_SWAP (u, v);
  2351. MPZ_PTR_SWAP (s, t);
  2352. MP_BITCNT_T_SWAP (uz, vz);
  2353. }
  2354. /* Maintain
  2355. *
  2356. * u = t0 tu + t1 tv
  2357. * v = s0 tu + s1 tv
  2358. *
  2359. * where u and v denote the inputs with common factors of two
  2360. * eliminated, and det (s0, t0; s1, t1) = 2^p. Then
  2361. *
  2362. * 2^p tu = s1 u - t1 v
  2363. * 2^p tv = -s0 u + t0 v
  2364. */
  2365. /* After initial division, tu = q tv + tu', we have
  2366. *
  2367. * u = 2^uz (tu' + q tv)
  2368. * v = 2^vz tv
  2369. *
  2370. * or
  2371. *
  2372. * t0 = 2^uz, t1 = 2^uz q
  2373. * s0 = 0, s1 = 2^vz
  2374. */
  2375. mpz_setbit (t0, uz);
  2376. mpz_tdiv_qr (t1, tu, tu, tv);
  2377. mpz_mul_2exp (t1, t1, uz);
  2378. mpz_setbit (s1, vz);
  2379. power = uz + vz;
  2380. if (tu->_mp_size > 0)
  2381. {
  2382. mp_bitcnt_t shift;
  2383. shift = mpz_make_odd (tu);
  2384. mpz_mul_2exp (t0, t0, shift);
  2385. mpz_mul_2exp (s0, s0, shift);
  2386. power += shift;
  2387. for (;;)
  2388. {
  2389. int c;
  2390. c = mpz_cmp (tu, tv);
  2391. if (c == 0)
  2392. break;
  2393. if (c < 0)
  2394. {
  2395. /* tv = tv' + tu
  2396. *
  2397. * u = t0 tu + t1 (tv' + tu) = (t0 + t1) tu + t1 tv'
  2398. * v = s0 tu + s1 (tv' + tu) = (s0 + s1) tu + s1 tv' */
  2399. mpz_sub (tv, tv, tu);
  2400. mpz_add (t0, t0, t1);
  2401. mpz_add (s0, s0, s1);
  2402. shift = mpz_make_odd (tv);
  2403. mpz_mul_2exp (t1, t1, shift);
  2404. mpz_mul_2exp (s1, s1, shift);
  2405. }
  2406. else
  2407. {
  2408. mpz_sub (tu, tu, tv);
  2409. mpz_add (t1, t0, t1);
  2410. mpz_add (s1, s0, s1);
  2411. shift = mpz_make_odd (tu);
  2412. mpz_mul_2exp (t0, t0, shift);
  2413. mpz_mul_2exp (s0, s0, shift);
  2414. }
  2415. power += shift;
  2416. }
  2417. }
  2418. /* Now tv = odd part of gcd, and -s0 and t0 are corresponding
  2419. cofactors. */
  2420. mpz_mul_2exp (tv, tv, gz);
  2421. mpz_neg (s0, s0);
  2422. /* 2^p g = s0 u + t0 v. Eliminate one factor of two at a time. To
  2423. adjust cofactors, we need u / g and v / g */
  2424. mpz_divexact (s1, v, tv);
  2425. mpz_abs (s1, s1);
  2426. mpz_divexact (t1, u, tv);
  2427. mpz_abs (t1, t1);
  2428. while (power-- > 0)
  2429. {
  2430. /* s0 u + t0 v = (s0 - v/g) u - (t0 + u/g) v */
  2431. if (mpz_odd_p (s0) || mpz_odd_p (t0))
  2432. {
  2433. mpz_sub (s0, s0, s1);
  2434. mpz_add (t0, t0, t1);
  2435. }
  2436. assert (mpz_even_p (t0) && mpz_even_p (s0));
  2437. mpz_tdiv_q_2exp (s0, s0, 1);
  2438. mpz_tdiv_q_2exp (t0, t0, 1);
  2439. }
  2440. /* Arrange so that |s| < |u| / 2g */
  2441. mpz_add (s1, s0, s1);
  2442. if (mpz_cmpabs (s0, s1) > 0)
  2443. {
  2444. mpz_swap (s0, s1);
  2445. mpz_sub (t0, t0, t1);
  2446. }
  2447. if (u->_mp_size < 0)
  2448. mpz_neg (s0, s0);
  2449. if (v->_mp_size < 0)
  2450. mpz_neg (t0, t0);
  2451. mpz_swap (g, tv);
  2452. if (s)
  2453. mpz_swap (s, s0);
  2454. if (t)
  2455. mpz_swap (t, t0);
  2456. mpz_clear (tu);
  2457. mpz_clear (tv);
  2458. mpz_clear (s0);
  2459. mpz_clear (s1);
  2460. mpz_clear (t0);
  2461. mpz_clear (t1);
  2462. }
  2463. void
  2464. mpz_lcm (mpz_t r, const mpz_t u, const mpz_t v)
  2465. {
  2466. mpz_t g;
  2467. if (u->_mp_size == 0 || v->_mp_size == 0)
  2468. {
  2469. r->_mp_size = 0;
  2470. return;
  2471. }
  2472. mpz_init (g);
  2473. mpz_gcd (g, u, v);
  2474. mpz_divexact (g, u, g);
  2475. mpz_mul (r, g, v);
  2476. mpz_clear (g);
  2477. mpz_abs (r, r);
  2478. }
  2479. void
  2480. mpz_lcm_ui (mpz_t r, const mpz_t u, unsigned long v)
  2481. {
  2482. if (v == 0 || u->_mp_size == 0)
  2483. {
  2484. r->_mp_size = 0;
  2485. return;
  2486. }
  2487. v /= mpz_gcd_ui (NULL, u, v);
  2488. mpz_mul_ui (r, u, v);
  2489. mpz_abs (r, r);
  2490. }
  2491. int
  2492. mpz_invert (mpz_t r, const mpz_t u, const mpz_t m)
  2493. {
  2494. mpz_t g, tr;
  2495. int invertible;
  2496. if (u->_mp_size == 0 || mpz_cmpabs_ui (m, 1) <= 0)
  2497. return 0;
  2498. mpz_init (g);
  2499. mpz_init (tr);
  2500. mpz_gcdext (g, tr, NULL, u, m);
  2501. invertible = (mpz_cmp_ui (g, 1) == 0);
  2502. if (invertible)
  2503. {
  2504. if (tr->_mp_size < 0)
  2505. {
  2506. if (m->_mp_size >= 0)
  2507. mpz_add (tr, tr, m);
  2508. else
  2509. mpz_sub (tr, tr, m);
  2510. }
  2511. mpz_swap (r, tr);
  2512. }
  2513. mpz_clear (g);
  2514. mpz_clear (tr);
  2515. return invertible;
  2516. }
  2517. /* Higher level operations (sqrt, pow and root) */
  2518. void
  2519. mpz_pow_ui (mpz_t r, const mpz_t b, unsigned long e)
  2520. {
  2521. unsigned long bit;
  2522. mpz_t tr;
  2523. mpz_init_set_ui (tr, 1);
  2524. bit = GMP_ULONG_HIGHBIT;
  2525. do
  2526. {
  2527. mpz_mul (tr, tr, tr);
  2528. if (e & bit)
  2529. mpz_mul (tr, tr, b);
  2530. bit >>= 1;
  2531. }
  2532. while (bit > 0);
  2533. mpz_swap (r, tr);
  2534. mpz_clear (tr);
  2535. }
  2536. void
  2537. mpz_ui_pow_ui (mpz_t r, unsigned long blimb, unsigned long e)
  2538. {
  2539. mpz_t b;
  2540. mpz_init_set_ui (b, blimb);
  2541. mpz_pow_ui (r, b, e);
  2542. mpz_clear (b);
  2543. }
  2544. void
  2545. mpz_powm (mpz_t r, const mpz_t b, const mpz_t e, const mpz_t m)
  2546. {
  2547. mpz_t tr;
  2548. mpz_t base;
  2549. mp_size_t en, mn;
  2550. mp_srcptr mp;
  2551. struct gmp_div_inverse minv;
  2552. unsigned shift;
  2553. mp_ptr tp = NULL;
  2554. en = GMP_ABS (e->_mp_size);
  2555. mn = GMP_ABS (m->_mp_size);
  2556. if (mn == 0)
  2557. gmp_die ("mpz_powm: Zero modulo.");
  2558. if (en == 0)
  2559. {
  2560. mpz_set_ui (r, 1);
  2561. return;
  2562. }
  2563. mp = m->_mp_d;
  2564. mpn_div_qr_invert (&minv, mp, mn);
  2565. shift = minv.shift;
  2566. if (shift > 0)
  2567. {
  2568. /* To avoid shifts, we do all our reductions, except the final
  2569. one, using a *normalized* m. */
  2570. minv.shift = 0;
  2571. tp = gmp_xalloc_limbs (mn);
  2572. gmp_assert_nocarry (mpn_lshift (tp, mp, mn, shift));
  2573. mp = tp;
  2574. }
  2575. mpz_init (base);
  2576. if (e->_mp_size < 0)
  2577. {
  2578. if (!mpz_invert (base, b, m))
  2579. gmp_die ("mpz_powm: Negative exponent and non-invertible base.");
  2580. }
  2581. else
  2582. {
  2583. mp_size_t bn;
  2584. mpz_abs (base, b);
  2585. bn = base->_mp_size;
  2586. if (bn >= mn)
  2587. {
  2588. mpn_div_qr_preinv (NULL, base->_mp_d, base->_mp_size, mp, mn, &minv);
  2589. bn = mn;
  2590. }
  2591. /* We have reduced the absolute value. Now take care of the
  2592. sign. Note that we get zero represented non-canonically as
  2593. m. */
  2594. if (b->_mp_size < 0)
  2595. {
  2596. mp_ptr bp = MPZ_REALLOC (base, mn);
  2597. gmp_assert_nocarry (mpn_sub (bp, mp, mn, bp, bn));
  2598. bn = mn;
  2599. }
  2600. base->_mp_size = mpn_normalized_size (base->_mp_d, bn);
  2601. }
  2602. mpz_init_set_ui (tr, 1);
  2603. while (--en >= 0)
  2604. {
  2605. mp_limb_t w = e->_mp_d[en];
  2606. mp_limb_t bit;
  2607. bit = GMP_LIMB_HIGHBIT;
  2608. do
  2609. {
  2610. mpz_mul (tr, tr, tr);
  2611. if (w & bit)
  2612. mpz_mul (tr, tr, base);
  2613. if (tr->_mp_size > mn)
  2614. {
  2615. mpn_div_qr_preinv (NULL, tr->_mp_d, tr->_mp_size, mp, mn, &minv);
  2616. tr->_mp_size = mpn_normalized_size (tr->_mp_d, mn);
  2617. }
  2618. bit >>= 1;
  2619. }
  2620. while (bit > 0);
  2621. }
  2622. /* Final reduction */
  2623. if (tr->_mp_size >= mn)
  2624. {
  2625. minv.shift = shift;
  2626. mpn_div_qr_preinv (NULL, tr->_mp_d, tr->_mp_size, mp, mn, &minv);
  2627. tr->_mp_size = mpn_normalized_size (tr->_mp_d, mn);
  2628. }
  2629. if (tp)
  2630. gmp_free (tp);
  2631. mpz_swap (r, tr);
  2632. mpz_clear (tr);
  2633. mpz_clear (base);
  2634. }
  2635. void
  2636. mpz_powm_ui (mpz_t r, const mpz_t b, unsigned long elimb, const mpz_t m)
  2637. {
  2638. mpz_t e;
  2639. mpz_init_set_ui (e, elimb);
  2640. mpz_powm (r, b, e, m);
  2641. mpz_clear (e);
  2642. }
  2643. /* x=trunc(y^(1/z)), r=y-x^z */
  2644. void
  2645. mpz_rootrem (mpz_t x, mpz_t r, const mpz_t y, unsigned long z)
  2646. {
  2647. int sgn;
  2648. mpz_t t, u;
  2649. sgn = y->_mp_size < 0;
  2650. if ((~z & sgn) != 0)
  2651. gmp_die ("mpz_rootrem: Negative argument, with even root.");
  2652. if (z == 0)
  2653. gmp_die ("mpz_rootrem: Zeroth root.");
  2654. if (mpz_cmpabs_ui (y, 1) <= 0) {
  2655. if (x)
  2656. mpz_set (x, y);
  2657. if (r)
  2658. r->_mp_size = 0;
  2659. return;
  2660. }
  2661. mpz_init (u);
  2662. mpz_init (t);
  2663. mpz_setbit (t, mpz_sizeinbase (y, 2) / z + 1);
  2664. if (z == 2) /* simplify sqrt loop: z-1 == 1 */
  2665. do {
  2666. mpz_swap (u, t); /* u = x */
  2667. mpz_tdiv_q (t, y, u); /* t = y/x */
  2668. mpz_add (t, t, u); /* t = y/x + x */
  2669. mpz_tdiv_q_2exp (t, t, 1); /* x'= (y/x + x)/2 */
  2670. } while (mpz_cmpabs (t, u) < 0); /* |x'| < |x| */
  2671. else /* z != 2 */ {
  2672. mpz_t v;
  2673. mpz_init (v);
  2674. if (sgn)
  2675. mpz_neg (t, t);
  2676. do {
  2677. mpz_swap (u, t); /* u = x */
  2678. mpz_pow_ui (t, u, z - 1); /* t = x^(z-1) */
  2679. mpz_tdiv_q (t, y, t); /* t = y/x^(z-1) */
  2680. mpz_mul_ui (v, u, z - 1); /* v = x*(z-1) */
  2681. mpz_add (t, t, v); /* t = y/x^(z-1) + x*(z-1) */
  2682. mpz_tdiv_q_ui (t, t, z); /* x'=(y/x^(z-1) + x*(z-1))/z */
  2683. } while (mpz_cmpabs (t, u) < 0); /* |x'| < |x| */
  2684. mpz_clear (v);
  2685. }
  2686. if (r) {
  2687. mpz_pow_ui (t, u, z);
  2688. mpz_sub (r, y, t);
  2689. }
  2690. if (x)
  2691. mpz_swap (x, u);
  2692. mpz_clear (u);
  2693. mpz_clear (t);
  2694. }
  2695. int
  2696. mpz_root (mpz_t x, const mpz_t y, unsigned long z)
  2697. {
  2698. int res;
  2699. mpz_t r;
  2700. mpz_init (r);
  2701. mpz_rootrem (x, r, y, z);
  2702. res = r->_mp_size == 0;
  2703. mpz_clear (r);
  2704. return res;
  2705. }
  2706. /* Compute s = floor(sqrt(u)) and r = u - s^2. Allows r == NULL */
  2707. void
  2708. mpz_sqrtrem (mpz_t s, mpz_t r, const mpz_t u)
  2709. {
  2710. mpz_rootrem (s, r, u, 2);
  2711. }
  2712. void
  2713. mpz_sqrt (mpz_t s, const mpz_t u)
  2714. {
  2715. mpz_rootrem (s, NULL, u, 2);
  2716. }
  2717. int
  2718. mpz_perfect_square_p (const mpz_t u)
  2719. {
  2720. if (u->_mp_size <= 0)
  2721. return (u->_mp_size == 0);
  2722. else
  2723. return mpz_root (NULL, u, 2);
  2724. }
  2725. int
  2726. mpn_perfect_square_p (mp_srcptr p, mp_size_t n)
  2727. {
  2728. mpz_t t;
  2729. assert (n > 0);
  2730. assert (p [n-1] != 0);
  2731. return mpz_root (NULL, mpz_roinit_normal_n (t, p, n), 2);
  2732. }
  2733. mp_size_t
  2734. mpn_sqrtrem (mp_ptr sp, mp_ptr rp, mp_srcptr p, mp_size_t n)
  2735. {
  2736. mpz_t s, r, u;
  2737. mp_size_t res;
  2738. assert (n > 0);
  2739. assert (p [n-1] != 0);
  2740. mpz_init (r);
  2741. mpz_init (s);
  2742. mpz_rootrem (s, r, mpz_roinit_normal_n (u, p, n), 2);
  2743. assert (s->_mp_size == (n+1)/2);
  2744. mpn_copyd (sp, s->_mp_d, s->_mp_size);
  2745. mpz_clear (s);
  2746. res = r->_mp_size;
  2747. if (rp)
  2748. mpn_copyd (rp, r->_mp_d, res);
  2749. mpz_clear (r);
  2750. return res;
  2751. }
  2752. /* Combinatorics */
  2753. void
  2754. mpz_mfac_uiui (mpz_t x, unsigned long n, unsigned long m)
  2755. {
  2756. mpz_set_ui (x, n + (n == 0));
  2757. if (m + 1 < 2) return;
  2758. while (n > m + 1)
  2759. mpz_mul_ui (x, x, n -= m);
  2760. }
  2761. void
  2762. mpz_2fac_ui (mpz_t x, unsigned long n)
  2763. {
  2764. mpz_mfac_uiui (x, n, 2);
  2765. }
  2766. void
  2767. mpz_fac_ui (mpz_t x, unsigned long n)
  2768. {
  2769. mpz_mfac_uiui (x, n, 1);
  2770. }
  2771. void
  2772. mpz_bin_uiui (mpz_t r, unsigned long n, unsigned long k)
  2773. {
  2774. mpz_t t;
  2775. mpz_set_ui (r, k <= n);
  2776. if (k > (n >> 1))
  2777. k = (k <= n) ? n - k : 0;
  2778. mpz_init (t);
  2779. mpz_fac_ui (t, k);
  2780. for (; k > 0; --k)
  2781. mpz_mul_ui (r, r, n--);
  2782. mpz_divexact (r, r, t);
  2783. mpz_clear (t);
  2784. }
  2785. /* Primality testing */
  2786. /* Computes Kronecker (a/b) with odd b, a!=0 and GCD(a,b) = 1 */
  2787. /* Adapted from JACOBI_BASE_METHOD==4 in mpn/generic/jacbase.c */
  2788. static int
  2789. gmp_jacobi_coprime (mp_limb_t a, mp_limb_t b)
  2790. {
  2791. int c, bit = 0;
  2792. assert (b & 1);
  2793. assert (a != 0);
  2794. /* assert (mpn_gcd_11 (a, b) == 1); */
  2795. /* Below, we represent a and b shifted right so that the least
  2796. significant one bit is implicit. */
  2797. b >>= 1;
  2798. gmp_ctz(c, a);
  2799. a >>= 1;
  2800. do
  2801. {
  2802. a >>= c;
  2803. /* (2/b) = -1 if b = 3 or 5 mod 8 */
  2804. bit ^= c & (b ^ (b >> 1));
  2805. if (a < b)
  2806. {
  2807. bit ^= a & b;
  2808. a = b - a;
  2809. b -= a;
  2810. }
  2811. else
  2812. {
  2813. a -= b;
  2814. assert (a != 0);
  2815. }
  2816. gmp_ctz(c, a);
  2817. ++c;
  2818. }
  2819. while (b > 0);
  2820. return bit & 1 ? -1 : 1;
  2821. }
  2822. static void
  2823. gmp_lucas_step_k_2k (mpz_t V, mpz_t Qk, const mpz_t n)
  2824. {
  2825. mpz_mod (Qk, Qk, n);
  2826. /* V_{2k} <- V_k ^ 2 - 2Q^k */
  2827. mpz_mul (V, V, V);
  2828. mpz_submul_ui (V, Qk, 2);
  2829. mpz_tdiv_r (V, V, n);
  2830. /* Q^{2k} = (Q^k)^2 */
  2831. mpz_mul (Qk, Qk, Qk);
  2832. }
  2833. /* Computes V_k, Q^k (mod n) for the Lucas' sequence */
  2834. /* with P=1, Q=Q; k = (n>>b0)|1. */
  2835. /* Requires an odd n > 4; b0 > 0; -2*Q must not overflow a long */
  2836. /* Returns (U_k == 0) and sets V=V_k and Qk=Q^k. */
  2837. static int
  2838. gmp_lucas_mod (mpz_t V, mpz_t Qk, long Q,
  2839. mp_bitcnt_t b0, const mpz_t n)
  2840. {
  2841. mp_bitcnt_t bs;
  2842. mpz_t U;
  2843. int res;
  2844. assert (b0 > 0);
  2845. assert (Q <= - (LONG_MIN / 2));
  2846. assert (Q >= - (LONG_MAX / 2));
  2847. assert (mpz_cmp_ui (n, 4) > 0);
  2848. assert (mpz_odd_p (n));
  2849. mpz_init_set_ui (U, 1); /* U1 = 1 */
  2850. mpz_set_ui (V, 1); /* V1 = 1 */
  2851. mpz_set_si (Qk, Q);
  2852. for (bs = mpz_sizeinbase (n, 2) - 1; --bs >= b0;)
  2853. {
  2854. /* U_{2k} <- U_k * V_k */
  2855. mpz_mul (U, U, V);
  2856. /* V_{2k} <- V_k ^ 2 - 2Q^k */
  2857. /* Q^{2k} = (Q^k)^2 */
  2858. gmp_lucas_step_k_2k (V, Qk, n);
  2859. /* A step k->k+1 is performed if the bit in $n$ is 1 */
  2860. /* mpz_tstbit(n,bs) or the bit is 0 in $n$ but */
  2861. /* should be 1 in $n+1$ (bs == b0) */
  2862. if (b0 == bs || mpz_tstbit (n, bs))
  2863. {
  2864. /* Q^{k+1} <- Q^k * Q */
  2865. mpz_mul_si (Qk, Qk, Q);
  2866. /* U_{k+1} <- (U_k + V_k) / 2 */
  2867. mpz_swap (U, V); /* Keep in V the old value of U_k */
  2868. mpz_add (U, U, V);
  2869. /* We have to compute U/2, so we need an even value, */
  2870. /* equivalent (mod n) */
  2871. if (mpz_odd_p (U))
  2872. mpz_add (U, U, n);
  2873. mpz_tdiv_q_2exp (U, U, 1);
  2874. /* V_{k+1} <-(D*U_k + V_k) / 2 =
  2875. U_{k+1} + (D-1)/2*U_k = U_{k+1} - 2Q*U_k */
  2876. mpz_mul_si (V, V, -2*Q);
  2877. mpz_add (V, U, V);
  2878. mpz_tdiv_r (V, V, n);
  2879. }
  2880. mpz_tdiv_r (U, U, n);
  2881. }
  2882. res = U->_mp_size == 0;
  2883. mpz_clear (U);
  2884. return res;
  2885. }
  2886. /* Performs strong Lucas' test on x, with parameters suggested */
  2887. /* for the BPSW test. Qk is only passed to recycle a variable. */
  2888. /* Requires GCD (x,6) = 1.*/
  2889. static int
  2890. gmp_stronglucas (const mpz_t x, mpz_t Qk)
  2891. {
  2892. mp_bitcnt_t b0;
  2893. mpz_t V, n;
  2894. mp_limb_t maxD, D; /* The absolute value is stored. */
  2895. long Q;
  2896. mp_limb_t tl;
  2897. /* Test on the absolute value. */
  2898. mpz_roinit_normal_n (n, x->_mp_d, GMP_ABS (x->_mp_size));
  2899. assert (mpz_odd_p (n));
  2900. /* assert (mpz_gcd_ui (NULL, n, 6) == 1); */
  2901. if (mpz_root (Qk, n, 2))
  2902. return 0; /* A square is composite. */
  2903. /* Check Ds up to square root (in case, n is prime)
  2904. or avoid overflows */
  2905. maxD = (Qk->_mp_size == 1) ? Qk->_mp_d [0] - 1 : GMP_LIMB_MAX;
  2906. D = 3;
  2907. /* Search a D such that (D/n) = -1 in the sequence 5,-7,9,-11,.. */
  2908. /* For those Ds we have (D/n) = (n/|D|) */
  2909. do
  2910. {
  2911. if (D >= maxD)
  2912. return 1 + (D != GMP_LIMB_MAX); /* (1 + ! ~ D) */
  2913. D += 2;
  2914. tl = mpz_tdiv_ui (n, D);
  2915. if (tl == 0)
  2916. return 0;
  2917. }
  2918. while (gmp_jacobi_coprime (tl, D) == 1);
  2919. mpz_init (V);
  2920. /* n-(D/n) = n+1 = d*2^{b0}, with d = (n>>b0) | 1 */
  2921. b0 = mpz_scan0 (n, 0);
  2922. /* D= P^2 - 4Q; P = 1; Q = (1-D)/4 */
  2923. Q = (D & 2) ? (long) (D >> 2) + 1 : -(long) (D >> 2);
  2924. if (! gmp_lucas_mod (V, Qk, Q, b0, n)) /* If Ud != 0 */
  2925. while (V->_mp_size != 0 && --b0 != 0) /* while Vk != 0 */
  2926. /* V <- V ^ 2 - 2Q^k */
  2927. /* Q^{2k} = (Q^k)^2 */
  2928. gmp_lucas_step_k_2k (V, Qk, n);
  2929. mpz_clear (V);
  2930. return (b0 != 0);
  2931. }
  2932. static int
  2933. gmp_millerrabin (const mpz_t n, const mpz_t nm1, mpz_t y,
  2934. const mpz_t q, mp_bitcnt_t k)
  2935. {
  2936. assert (k > 0);
  2937. /* Caller must initialize y to the base. */
  2938. mpz_powm (y, y, q, n);
  2939. if (mpz_cmp_ui (y, 1) == 0 || mpz_cmp (y, nm1) == 0)
  2940. return 1;
  2941. while (--k > 0)
  2942. {
  2943. mpz_powm_ui (y, y, 2, n);
  2944. if (mpz_cmp (y, nm1) == 0)
  2945. return 1;
  2946. /* y == 1 means that the previous y was a non-trivial square root
  2947. of 1 (mod n). y == 0 means that n is a power of the base.
  2948. In either case, n is not prime. */
  2949. if (mpz_cmp_ui (y, 1) <= 0)
  2950. return 0;
  2951. }
  2952. return 0;
  2953. }
  2954. /* This product is 0xc0cfd797, and fits in 32 bits. */
  2955. #define GMP_PRIME_PRODUCT \
  2956. (3UL*5UL*7UL*11UL*13UL*17UL*19UL*23UL*29UL)
  2957. /* Bit (p+1)/2 is set, for each odd prime <= 61 */
  2958. #define GMP_PRIME_MASK 0xc96996dcUL
  2959. int
  2960. mpz_probab_prime_p (const mpz_t n, int reps)
  2961. {
  2962. mpz_t nm1;
  2963. mpz_t q;
  2964. mpz_t y;
  2965. mp_bitcnt_t k;
  2966. int is_prime;
  2967. int j;
  2968. /* Note that we use the absolute value of n only, for compatibility
  2969. with the real GMP. */
  2970. if (mpz_even_p (n))
  2971. return (mpz_cmpabs_ui (n, 2) == 0) ? 2 : 0;
  2972. /* Above test excludes n == 0 */
  2973. assert (n->_mp_size != 0);
  2974. if (mpz_cmpabs_ui (n, 64) < 0)
  2975. return (GMP_PRIME_MASK >> (n->_mp_d[0] >> 1)) & 2;
  2976. if (mpz_gcd_ui (NULL, n, GMP_PRIME_PRODUCT) != 1)
  2977. return 0;
  2978. /* All prime factors are >= 31. */
  2979. if (mpz_cmpabs_ui (n, 31*31) < 0)
  2980. return 2;
  2981. mpz_init (nm1);
  2982. mpz_init (q);
  2983. /* Find q and k, where q is odd and n = 1 + 2**k * q. */
  2984. mpz_abs (nm1, n);
  2985. nm1->_mp_d[0] -= 1;
  2986. k = mpz_scan1 (nm1, 0);
  2987. mpz_tdiv_q_2exp (q, nm1, k);
  2988. /* BPSW test */
  2989. mpz_init_set_ui (y, 2);
  2990. is_prime = gmp_millerrabin (n, nm1, y, q, k) && gmp_stronglucas (n, y);
  2991. reps -= 24; /* skip the first 24 repetitions */
  2992. /* Use Miller-Rabin, with a deterministic sequence of bases, a[j] =
  2993. j^2 + j + 41 using Euler's polynomial. We potentially stop early,
  2994. if a[j] >= n - 1. Since n >= 31*31, this can happen only if reps >
  2995. 30 (a[30] == 971 > 31*31 == 961). */
  2996. for (j = 0; is_prime & (j < reps); j++)
  2997. {
  2998. mpz_set_ui (y, (unsigned long) j*j+j+41);
  2999. if (mpz_cmp (y, nm1) >= 0)
  3000. {
  3001. /* Don't try any further bases. This "early" break does not affect
  3002. the result for any reasonable reps value (<=5000 was tested) */
  3003. assert (j >= 30);
  3004. break;
  3005. }
  3006. is_prime = gmp_millerrabin (n, nm1, y, q, k);
  3007. }
  3008. mpz_clear (nm1);
  3009. mpz_clear (q);
  3010. mpz_clear (y);
  3011. return is_prime;
  3012. }
  3013. /* Logical operations and bit manipulation. */
  3014. /* Numbers are treated as if represented in two's complement (and
  3015. infinitely sign extended). For a negative values we get the two's
  3016. complement from -x = ~x + 1, where ~ is bitwise complement.
  3017. Negation transforms
  3018. xxxx10...0
  3019. into
  3020. yyyy10...0
  3021. where yyyy is the bitwise complement of xxxx. So least significant
  3022. bits, up to and including the first one bit, are unchanged, and
  3023. the more significant bits are all complemented.
  3024. To change a bit from zero to one in a negative number, subtract the
  3025. corresponding power of two from the absolute value. This can never
  3026. underflow. To change a bit from one to zero, add the corresponding
  3027. power of two, and this might overflow. E.g., if x = -001111, the
  3028. two's complement is 110001. Clearing the least significant bit, we
  3029. get two's complement 110000, and -010000. */
  3030. int
  3031. mpz_tstbit (const mpz_t d, mp_bitcnt_t bit_index)
  3032. {
  3033. mp_size_t limb_index;
  3034. unsigned shift;
  3035. mp_size_t ds;
  3036. mp_size_t dn;
  3037. mp_limb_t w;
  3038. int bit;
  3039. ds = d->_mp_size;
  3040. dn = GMP_ABS (ds);
  3041. limb_index = bit_index / GMP_LIMB_BITS;
  3042. if (limb_index >= dn)
  3043. return ds < 0;
  3044. shift = bit_index % GMP_LIMB_BITS;
  3045. w = d->_mp_d[limb_index];
  3046. bit = (w >> shift) & 1;
  3047. if (ds < 0)
  3048. {
  3049. /* d < 0. Check if any of the bits below is set: If so, our bit
  3050. must be complemented. */
  3051. if (shift > 0 && (mp_limb_t) (w << (GMP_LIMB_BITS - shift)) > 0)
  3052. return bit ^ 1;
  3053. while (--limb_index >= 0)
  3054. if (d->_mp_d[limb_index] > 0)
  3055. return bit ^ 1;
  3056. }
  3057. return bit;
  3058. }
  3059. static void
  3060. mpz_abs_add_bit (mpz_t d, mp_bitcnt_t bit_index)
  3061. {
  3062. mp_size_t dn, limb_index;
  3063. mp_limb_t bit;
  3064. mp_ptr dp;
  3065. dn = GMP_ABS (d->_mp_size);
  3066. limb_index = bit_index / GMP_LIMB_BITS;
  3067. bit = (mp_limb_t) 1 << (bit_index % GMP_LIMB_BITS);
  3068. if (limb_index >= dn)
  3069. {
  3070. mp_size_t i;
  3071. /* The bit should be set outside of the end of the number.
  3072. We have to increase the size of the number. */
  3073. dp = MPZ_REALLOC (d, limb_index + 1);
  3074. dp[limb_index] = bit;
  3075. for (i = dn; i < limb_index; i++)
  3076. dp[i] = 0;
  3077. dn = limb_index + 1;
  3078. }
  3079. else
  3080. {
  3081. mp_limb_t cy;
  3082. dp = d->_mp_d;
  3083. cy = mpn_add_1 (dp + limb_index, dp + limb_index, dn - limb_index, bit);
  3084. if (cy > 0)
  3085. {
  3086. dp = MPZ_REALLOC (d, dn + 1);
  3087. dp[dn++] = cy;
  3088. }
  3089. }
  3090. d->_mp_size = (d->_mp_size < 0) ? - dn : dn;
  3091. }
  3092. static void
  3093. mpz_abs_sub_bit (mpz_t d, mp_bitcnt_t bit_index)
  3094. {
  3095. mp_size_t dn, limb_index;
  3096. mp_ptr dp;
  3097. mp_limb_t bit;
  3098. dn = GMP_ABS (d->_mp_size);
  3099. dp = d->_mp_d;
  3100. limb_index = bit_index / GMP_LIMB_BITS;
  3101. bit = (mp_limb_t) 1 << (bit_index % GMP_LIMB_BITS);
  3102. assert (limb_index < dn);
  3103. gmp_assert_nocarry (mpn_sub_1 (dp + limb_index, dp + limb_index,
  3104. dn - limb_index, bit));
  3105. dn = mpn_normalized_size (dp, dn);
  3106. d->_mp_size = (d->_mp_size < 0) ? - dn : dn;
  3107. }
  3108. void
  3109. mpz_setbit (mpz_t d, mp_bitcnt_t bit_index)
  3110. {
  3111. if (!mpz_tstbit (d, bit_index))
  3112. {
  3113. if (d->_mp_size >= 0)
  3114. mpz_abs_add_bit (d, bit_index);
  3115. else
  3116. mpz_abs_sub_bit (d, bit_index);
  3117. }
  3118. }
  3119. void
  3120. mpz_clrbit (mpz_t d, mp_bitcnt_t bit_index)
  3121. {
  3122. if (mpz_tstbit (d, bit_index))
  3123. {
  3124. if (d->_mp_size >= 0)
  3125. mpz_abs_sub_bit (d, bit_index);
  3126. else
  3127. mpz_abs_add_bit (d, bit_index);
  3128. }
  3129. }
  3130. void
  3131. mpz_combit (mpz_t d, mp_bitcnt_t bit_index)
  3132. {
  3133. if (mpz_tstbit (d, bit_index) ^ (d->_mp_size < 0))
  3134. mpz_abs_sub_bit (d, bit_index);
  3135. else
  3136. mpz_abs_add_bit (d, bit_index);
  3137. }
  3138. void
  3139. mpz_com (mpz_t r, const mpz_t u)
  3140. {
  3141. mpz_add_ui (r, u, 1);
  3142. mpz_neg (r, r);
  3143. }
  3144. void
  3145. mpz_and (mpz_t r, const mpz_t u, const mpz_t v)
  3146. {
  3147. mp_size_t un, vn, rn, i;
  3148. mp_ptr up, vp, rp;
  3149. mp_limb_t ux, vx, rx;
  3150. mp_limb_t uc, vc, rc;
  3151. mp_limb_t ul, vl, rl;
  3152. un = GMP_ABS (u->_mp_size);
  3153. vn = GMP_ABS (v->_mp_size);
  3154. if (un < vn)
  3155. {
  3156. MPZ_SRCPTR_SWAP (u, v);
  3157. MP_SIZE_T_SWAP (un, vn);
  3158. }
  3159. if (vn == 0)
  3160. {
  3161. r->_mp_size = 0;
  3162. return;
  3163. }
  3164. uc = u->_mp_size < 0;
  3165. vc = v->_mp_size < 0;
  3166. rc = uc & vc;
  3167. ux = -uc;
  3168. vx = -vc;
  3169. rx = -rc;
  3170. /* If the smaller input is positive, higher limbs don't matter. */
  3171. rn = vx ? un : vn;
  3172. rp = MPZ_REALLOC (r, rn + (mp_size_t) rc);
  3173. up = u->_mp_d;
  3174. vp = v->_mp_d;
  3175. i = 0;
  3176. do
  3177. {
  3178. ul = (up[i] ^ ux) + uc;
  3179. uc = ul < uc;
  3180. vl = (vp[i] ^ vx) + vc;
  3181. vc = vl < vc;
  3182. rl = ( (ul & vl) ^ rx) + rc;
  3183. rc = rl < rc;
  3184. rp[i] = rl;
  3185. }
  3186. while (++i < vn);
  3187. assert (vc == 0);
  3188. for (; i < rn; i++)
  3189. {
  3190. ul = (up[i] ^ ux) + uc;
  3191. uc = ul < uc;
  3192. rl = ( (ul & vx) ^ rx) + rc;
  3193. rc = rl < rc;
  3194. rp[i] = rl;
  3195. }
  3196. if (rc)
  3197. rp[rn++] = rc;
  3198. else
  3199. rn = mpn_normalized_size (rp, rn);
  3200. r->_mp_size = rx ? -rn : rn;
  3201. }
  3202. void
  3203. mpz_ior (mpz_t r, const mpz_t u, const mpz_t v)
  3204. {
  3205. mp_size_t un, vn, rn, i;
  3206. mp_ptr up, vp, rp;
  3207. mp_limb_t ux, vx, rx;
  3208. mp_limb_t uc, vc, rc;
  3209. mp_limb_t ul, vl, rl;
  3210. un = GMP_ABS (u->_mp_size);
  3211. vn = GMP_ABS (v->_mp_size);
  3212. if (un < vn)
  3213. {
  3214. MPZ_SRCPTR_SWAP (u, v);
  3215. MP_SIZE_T_SWAP (un, vn);
  3216. }
  3217. if (vn == 0)
  3218. {
  3219. mpz_set (r, u);
  3220. return;
  3221. }
  3222. uc = u->_mp_size < 0;
  3223. vc = v->_mp_size < 0;
  3224. rc = uc | vc;
  3225. ux = -uc;
  3226. vx = -vc;
  3227. rx = -rc;
  3228. /* If the smaller input is negative, by sign extension higher limbs
  3229. don't matter. */
  3230. rn = vx ? vn : un;
  3231. rp = MPZ_REALLOC (r, rn + (mp_size_t) rc);
  3232. up = u->_mp_d;
  3233. vp = v->_mp_d;
  3234. i = 0;
  3235. do
  3236. {
  3237. ul = (up[i] ^ ux) + uc;
  3238. uc = ul < uc;
  3239. vl = (vp[i] ^ vx) + vc;
  3240. vc = vl < vc;
  3241. rl = ( (ul | vl) ^ rx) + rc;
  3242. rc = rl < rc;
  3243. rp[i] = rl;
  3244. }
  3245. while (++i < vn);
  3246. assert (vc == 0);
  3247. for (; i < rn; i++)
  3248. {
  3249. ul = (up[i] ^ ux) + uc;
  3250. uc = ul < uc;
  3251. rl = ( (ul | vx) ^ rx) + rc;
  3252. rc = rl < rc;
  3253. rp[i] = rl;
  3254. }
  3255. if (rc)
  3256. rp[rn++] = rc;
  3257. else
  3258. rn = mpn_normalized_size (rp, rn);
  3259. r->_mp_size = rx ? -rn : rn;
  3260. }
  3261. void
  3262. mpz_xor (mpz_t r, const mpz_t u, const mpz_t v)
  3263. {
  3264. mp_size_t un, vn, i;
  3265. mp_ptr up, vp, rp;
  3266. mp_limb_t ux, vx, rx;
  3267. mp_limb_t uc, vc, rc;
  3268. mp_limb_t ul, vl, rl;
  3269. un = GMP_ABS (u->_mp_size);
  3270. vn = GMP_ABS (v->_mp_size);
  3271. if (un < vn)
  3272. {
  3273. MPZ_SRCPTR_SWAP (u, v);
  3274. MP_SIZE_T_SWAP (un, vn);
  3275. }
  3276. if (vn == 0)
  3277. {
  3278. mpz_set (r, u);
  3279. return;
  3280. }
  3281. uc = u->_mp_size < 0;
  3282. vc = v->_mp_size < 0;
  3283. rc = uc ^ vc;
  3284. ux = -uc;
  3285. vx = -vc;
  3286. rx = -rc;
  3287. rp = MPZ_REALLOC (r, un + (mp_size_t) rc);
  3288. up = u->_mp_d;
  3289. vp = v->_mp_d;
  3290. i = 0;
  3291. do
  3292. {
  3293. ul = (up[i] ^ ux) + uc;
  3294. uc = ul < uc;
  3295. vl = (vp[i] ^ vx) + vc;
  3296. vc = vl < vc;
  3297. rl = (ul ^ vl ^ rx) + rc;
  3298. rc = rl < rc;
  3299. rp[i] = rl;
  3300. }
  3301. while (++i < vn);
  3302. assert (vc == 0);
  3303. for (; i < un; i++)
  3304. {
  3305. ul = (up[i] ^ ux) + uc;
  3306. uc = ul < uc;
  3307. rl = (ul ^ ux) + rc;
  3308. rc = rl < rc;
  3309. rp[i] = rl;
  3310. }
  3311. if (rc)
  3312. rp[un++] = rc;
  3313. else
  3314. un = mpn_normalized_size (rp, un);
  3315. r->_mp_size = rx ? -un : un;
  3316. }
  3317. static unsigned
  3318. gmp_popcount_limb (mp_limb_t x)
  3319. {
  3320. unsigned c;
  3321. /* Do 16 bits at a time, to avoid limb-sized constants. */
  3322. int LOCAL_SHIFT_BITS = 16;
  3323. for (c = 0; x > 0;)
  3324. {
  3325. unsigned w = x - ((x >> 1) & 0x5555);
  3326. w = ((w >> 2) & 0x3333) + (w & 0x3333);
  3327. w = (w >> 4) + w;
  3328. w = ((w >> 8) & 0x000f) + (w & 0x000f);
  3329. c += w;
  3330. if (GMP_LIMB_BITS > LOCAL_SHIFT_BITS)
  3331. x >>= LOCAL_SHIFT_BITS;
  3332. else
  3333. x = 0;
  3334. }
  3335. return c;
  3336. }
  3337. mp_bitcnt_t
  3338. mpn_popcount (mp_srcptr p, mp_size_t n)
  3339. {
  3340. mp_size_t i;
  3341. mp_bitcnt_t c;
  3342. for (c = 0, i = 0; i < n; i++)
  3343. c += gmp_popcount_limb (p[i]);
  3344. return c;
  3345. }
  3346. mp_bitcnt_t
  3347. mpz_popcount (const mpz_t u)
  3348. {
  3349. mp_size_t un;
  3350. un = u->_mp_size;
  3351. if (un < 0)
  3352. return ~(mp_bitcnt_t) 0;
  3353. return mpn_popcount (u->_mp_d, un);
  3354. }
  3355. mp_bitcnt_t
  3356. mpz_hamdist (const mpz_t u, const mpz_t v)
  3357. {
  3358. mp_size_t un, vn, i;
  3359. mp_limb_t uc, vc, ul, vl, comp;
  3360. mp_srcptr up, vp;
  3361. mp_bitcnt_t c;
  3362. un = u->_mp_size;
  3363. vn = v->_mp_size;
  3364. if ( (un ^ vn) < 0)
  3365. return ~(mp_bitcnt_t) 0;
  3366. comp = - (uc = vc = (un < 0));
  3367. if (uc)
  3368. {
  3369. assert (vn < 0);
  3370. un = -un;
  3371. vn = -vn;
  3372. }
  3373. up = u->_mp_d;
  3374. vp = v->_mp_d;
  3375. if (un < vn)
  3376. MPN_SRCPTR_SWAP (up, un, vp, vn);
  3377. for (i = 0, c = 0; i < vn; i++)
  3378. {
  3379. ul = (up[i] ^ comp) + uc;
  3380. uc = ul < uc;
  3381. vl = (vp[i] ^ comp) + vc;
  3382. vc = vl < vc;
  3383. c += gmp_popcount_limb (ul ^ vl);
  3384. }
  3385. assert (vc == 0);
  3386. for (; i < un; i++)
  3387. {
  3388. ul = (up[i] ^ comp) + uc;
  3389. uc = ul < uc;
  3390. c += gmp_popcount_limb (ul ^ comp);
  3391. }
  3392. return c;
  3393. }
  3394. mp_bitcnt_t
  3395. mpz_scan1 (const mpz_t u, mp_bitcnt_t starting_bit)
  3396. {
  3397. mp_ptr up;
  3398. mp_size_t us, un, i;
  3399. mp_limb_t limb, ux;
  3400. us = u->_mp_size;
  3401. un = GMP_ABS (us);
  3402. i = starting_bit / GMP_LIMB_BITS;
  3403. /* Past the end there's no 1 bits for u>=0, or an immediate 1 bit
  3404. for u<0. Notice this test picks up any u==0 too. */
  3405. if (i >= un)
  3406. return (us >= 0 ? ~(mp_bitcnt_t) 0 : starting_bit);
  3407. up = u->_mp_d;
  3408. ux = 0;
  3409. limb = up[i];
  3410. if (starting_bit != 0)
  3411. {
  3412. if (us < 0)
  3413. {
  3414. ux = mpn_zero_p (up, i);
  3415. limb = ~ limb + ux;
  3416. ux = - (mp_limb_t) (limb >= ux);
  3417. }
  3418. /* Mask to 0 all bits before starting_bit, thus ignoring them. */
  3419. limb &= GMP_LIMB_MAX << (starting_bit % GMP_LIMB_BITS);
  3420. }
  3421. return mpn_common_scan (limb, i, up, un, ux);
  3422. }
  3423. mp_bitcnt_t
  3424. mpz_scan0 (const mpz_t u, mp_bitcnt_t starting_bit)
  3425. {
  3426. mp_ptr up;
  3427. mp_size_t us, un, i;
  3428. mp_limb_t limb, ux;
  3429. us = u->_mp_size;
  3430. ux = - (mp_limb_t) (us >= 0);
  3431. un = GMP_ABS (us);
  3432. i = starting_bit / GMP_LIMB_BITS;
  3433. /* When past end, there's an immediate 0 bit for u>=0, or no 0 bits for
  3434. u<0. Notice this test picks up all cases of u==0 too. */
  3435. if (i >= un)
  3436. return (ux ? starting_bit : ~(mp_bitcnt_t) 0);
  3437. up = u->_mp_d;
  3438. limb = up[i] ^ ux;
  3439. if (ux == 0)
  3440. limb -= mpn_zero_p (up, i); /* limb = ~(~limb + zero_p) */
  3441. /* Mask all bits before starting_bit, thus ignoring them. */
  3442. limb &= GMP_LIMB_MAX << (starting_bit % GMP_LIMB_BITS);
  3443. return mpn_common_scan (limb, i, up, un, ux);
  3444. }
  3445. /* MPZ base conversion. */
  3446. size_t
  3447. mpz_sizeinbase (const mpz_t u, int base)
  3448. {
  3449. mp_size_t un;
  3450. mp_srcptr up;
  3451. mp_ptr tp;
  3452. mp_bitcnt_t bits;
  3453. struct gmp_div_inverse bi;
  3454. size_t ndigits;
  3455. assert (base >= 2);
  3456. assert (base <= 62);
  3457. un = GMP_ABS (u->_mp_size);
  3458. if (un == 0)
  3459. return 1;
  3460. up = u->_mp_d;
  3461. bits = (un - 1) * GMP_LIMB_BITS + mpn_limb_size_in_base_2 (up[un-1]);
  3462. switch (base)
  3463. {
  3464. case 2:
  3465. return bits;
  3466. case 4:
  3467. return (bits + 1) / 2;
  3468. case 8:
  3469. return (bits + 2) / 3;
  3470. case 16:
  3471. return (bits + 3) / 4;
  3472. case 32:
  3473. return (bits + 4) / 5;
  3474. /* FIXME: Do something more clever for the common case of base
  3475. 10. */
  3476. }
  3477. tp = gmp_xalloc_limbs (un);
  3478. mpn_copyi (tp, up, un);
  3479. mpn_div_qr_1_invert (&bi, base);
  3480. ndigits = 0;
  3481. do
  3482. {
  3483. ndigits++;
  3484. mpn_div_qr_1_preinv (tp, tp, un, &bi);
  3485. un -= (tp[un-1] == 0);
  3486. }
  3487. while (un > 0);
  3488. gmp_free (tp);
  3489. return ndigits;
  3490. }
  3491. char *
  3492. mpz_get_str (char *sp, int base, const mpz_t u)
  3493. {
  3494. unsigned bits;
  3495. const char *digits;
  3496. mp_size_t un;
  3497. size_t i, sn;
  3498. digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  3499. if (base > 1)
  3500. {
  3501. if (base <= 36)
  3502. digits = "0123456789abcdefghijklmnopqrstuvwxyz";
  3503. else if (base > 62)
  3504. return NULL;
  3505. }
  3506. else if (base >= -1)
  3507. base = 10;
  3508. else
  3509. {
  3510. base = -base;
  3511. if (base > 36)
  3512. return NULL;
  3513. }
  3514. sn = 1 + mpz_sizeinbase (u, base);
  3515. if (!sp)
  3516. sp = (char *) gmp_xalloc (1 + sn);
  3517. un = GMP_ABS (u->_mp_size);
  3518. if (un == 0)
  3519. {
  3520. sp[0] = '0';
  3521. sp[1] = '\0';
  3522. return sp;
  3523. }
  3524. i = 0;
  3525. if (u->_mp_size < 0)
  3526. sp[i++] = '-';
  3527. bits = mpn_base_power_of_two_p (base);
  3528. if (bits)
  3529. /* Not modified in this case. */
  3530. sn = i + mpn_get_str_bits ((unsigned char *) sp + i, bits, u->_mp_d, un);
  3531. else
  3532. {
  3533. struct mpn_base_info info;
  3534. mp_ptr tp;
  3535. mpn_get_base_info (&info, base);
  3536. tp = gmp_xalloc_limbs (un);
  3537. mpn_copyi (tp, u->_mp_d, un);
  3538. sn = i + mpn_get_str_other ((unsigned char *) sp + i, base, &info, tp, un);
  3539. gmp_free (tp);
  3540. }
  3541. for (; i < sn; i++)
  3542. sp[i] = digits[(unsigned char) sp[i]];
  3543. sp[sn] = '\0';
  3544. return sp;
  3545. }
  3546. int
  3547. mpz_set_str (mpz_t r, const char *sp, int base)
  3548. {
  3549. unsigned bits, value_of_a;
  3550. mp_size_t rn, alloc;
  3551. mp_ptr rp;
  3552. size_t dn;
  3553. int sign;
  3554. unsigned char *dp;
  3555. assert (base == 0 || (base >= 2 && base <= 62));
  3556. while (isspace( (unsigned char) *sp))
  3557. sp++;
  3558. sign = (*sp == '-');
  3559. sp += sign;
  3560. if (base == 0)
  3561. {
  3562. if (sp[0] == '0')
  3563. {
  3564. if (sp[1] == 'x' || sp[1] == 'X')
  3565. {
  3566. base = 16;
  3567. sp += 2;
  3568. }
  3569. else if (sp[1] == 'b' || sp[1] == 'B')
  3570. {
  3571. base = 2;
  3572. sp += 2;
  3573. }
  3574. else
  3575. base = 8;
  3576. }
  3577. else
  3578. base = 10;
  3579. }
  3580. if (!*sp)
  3581. {
  3582. r->_mp_size = 0;
  3583. return -1;
  3584. }
  3585. dp = (unsigned char *) gmp_xalloc (strlen (sp));
  3586. value_of_a = (base > 36) ? 36 : 10;
  3587. for (dn = 0; *sp; sp++)
  3588. {
  3589. unsigned digit;
  3590. if (isspace ((unsigned char) *sp))
  3591. continue;
  3592. else if (*sp >= '0' && *sp <= '9')
  3593. digit = *sp - '0';
  3594. else if (*sp >= 'a' && *sp <= 'z')
  3595. digit = *sp - 'a' + value_of_a;
  3596. else if (*sp >= 'A' && *sp <= 'Z')
  3597. digit = *sp - 'A' + 10;
  3598. else
  3599. digit = base; /* fail */
  3600. if (digit >= (unsigned) base)
  3601. {
  3602. gmp_free (dp);
  3603. r->_mp_size = 0;
  3604. return -1;
  3605. }
  3606. dp[dn++] = digit;
  3607. }
  3608. if (!dn)
  3609. {
  3610. gmp_free (dp);
  3611. r->_mp_size = 0;
  3612. return -1;
  3613. }
  3614. bits = mpn_base_power_of_two_p (base);
  3615. if (bits > 0)
  3616. {
  3617. alloc = (dn * bits + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
  3618. rp = MPZ_REALLOC (r, alloc);
  3619. rn = mpn_set_str_bits (rp, dp, dn, bits);
  3620. }
  3621. else
  3622. {
  3623. struct mpn_base_info info;
  3624. mpn_get_base_info (&info, base);
  3625. alloc = (dn + info.exp - 1) / info.exp;
  3626. rp = MPZ_REALLOC (r, alloc);
  3627. rn = mpn_set_str_other (rp, dp, dn, base, &info);
  3628. /* Normalization, needed for all-zero input. */
  3629. assert (rn > 0);
  3630. rn -= rp[rn-1] == 0;
  3631. }
  3632. assert (rn <= alloc);
  3633. gmp_free (dp);
  3634. r->_mp_size = sign ? - rn : rn;
  3635. return 0;
  3636. }
  3637. int
  3638. mpz_init_set_str (mpz_t r, const char *sp, int base)
  3639. {
  3640. mpz_init (r);
  3641. return mpz_set_str (r, sp, base);
  3642. }
  3643. size_t
  3644. mpz_out_str (FILE *stream, int base, const mpz_t x)
  3645. {
  3646. char *str;
  3647. size_t len;
  3648. str = mpz_get_str (NULL, base, x);
  3649. if (!str)
  3650. return 0;
  3651. len = strlen (str);
  3652. len = fwrite (str, 1, len, stream);
  3653. gmp_free (str);
  3654. return len;
  3655. }
  3656. static int
  3657. gmp_detect_endian (void)
  3658. {
  3659. static const int i = 2;
  3660. const unsigned char *p = (const unsigned char *) &i;
  3661. return 1 - *p;
  3662. }
  3663. /* Import and export. Does not support nails. */
  3664. void
  3665. mpz_import (mpz_t r, size_t count, int order, size_t size, int endian,
  3666. size_t nails, const void *src)
  3667. {
  3668. const unsigned char *p;
  3669. ptrdiff_t word_step;
  3670. mp_ptr rp;
  3671. mp_size_t rn;
  3672. /* The current (partial) limb. */
  3673. mp_limb_t limb;
  3674. /* The number of bytes already copied to this limb (starting from
  3675. the low end). */
  3676. size_t bytes;
  3677. /* The index where the limb should be stored, when completed. */
  3678. mp_size_t i;
  3679. if (nails != 0)
  3680. gmp_die ("mpz_import: Nails not supported.");
  3681. assert (order == 1 || order == -1);
  3682. assert (endian >= -1 && endian <= 1);
  3683. if (endian == 0)
  3684. endian = gmp_detect_endian ();
  3685. p = (unsigned char *) src;
  3686. word_step = (order != endian) ? 2 * size : 0;
  3687. /* Process bytes from the least significant end, so point p at the
  3688. least significant word. */
  3689. if (order == 1)
  3690. {
  3691. p += size * (count - 1);
  3692. word_step = - word_step;
  3693. }
  3694. /* And at least significant byte of that word. */
  3695. if (endian == 1)
  3696. p += (size - 1);
  3697. rn = (size * count + sizeof(mp_limb_t) - 1) / sizeof(mp_limb_t);
  3698. rp = MPZ_REALLOC (r, rn);
  3699. for (limb = 0, bytes = 0, i = 0; count > 0; count--, p += word_step)
  3700. {
  3701. size_t j;
  3702. for (j = 0; j < size; j++, p -= (ptrdiff_t) endian)
  3703. {
  3704. limb |= (mp_limb_t) *p << (bytes++ * CHAR_BIT);
  3705. if (bytes == sizeof(mp_limb_t))
  3706. {
  3707. rp[i++] = limb;
  3708. bytes = 0;
  3709. limb = 0;
  3710. }
  3711. }
  3712. }
  3713. assert (i + (bytes > 0) == rn);
  3714. if (limb != 0)
  3715. rp[i++] = limb;
  3716. else
  3717. i = mpn_normalized_size (rp, i);
  3718. r->_mp_size = i;
  3719. }
  3720. void *
  3721. mpz_export (void *r, size_t *countp, int order, size_t size, int endian,
  3722. size_t nails, const mpz_t u)
  3723. {
  3724. size_t count;
  3725. mp_size_t un;
  3726. if (nails != 0)
  3727. gmp_die ("mpz_import: Nails not supported.");
  3728. assert (order == 1 || order == -1);
  3729. assert (endian >= -1 && endian <= 1);
  3730. assert (size > 0 || u->_mp_size == 0);
  3731. un = u->_mp_size;
  3732. count = 0;
  3733. if (un != 0)
  3734. {
  3735. size_t k;
  3736. unsigned char *p;
  3737. ptrdiff_t word_step;
  3738. /* The current (partial) limb. */
  3739. mp_limb_t limb;
  3740. /* The number of bytes left to do in this limb. */
  3741. size_t bytes;
  3742. /* The index where the limb was read. */
  3743. mp_size_t i;
  3744. un = GMP_ABS (un);
  3745. /* Count bytes in top limb. */
  3746. limb = u->_mp_d[un-1];
  3747. assert (limb != 0);
  3748. k = (GMP_LIMB_BITS <= CHAR_BIT);
  3749. if (!k)
  3750. {
  3751. do {
  3752. int LOCAL_CHAR_BIT = CHAR_BIT;
  3753. k++; limb >>= LOCAL_CHAR_BIT;
  3754. } while (limb != 0);
  3755. }
  3756. /* else limb = 0; */
  3757. count = (k + (un-1) * sizeof (mp_limb_t) + size - 1) / size;
  3758. if (!r)
  3759. r = gmp_xalloc (count * size);
  3760. if (endian == 0)
  3761. endian = gmp_detect_endian ();
  3762. p = (unsigned char *) r;
  3763. word_step = (order != endian) ? 2 * size : 0;
  3764. /* Process bytes from the least significant end, so point p at the
  3765. least significant word. */
  3766. if (order == 1)
  3767. {
  3768. p += size * (count - 1);
  3769. word_step = - word_step;
  3770. }
  3771. /* And at least significant byte of that word. */
  3772. if (endian == 1)
  3773. p += (size - 1);
  3774. for (bytes = 0, i = 0, k = 0; k < count; k++, p += word_step)
  3775. {
  3776. size_t j;
  3777. for (j = 0; j < size; ++j, p -= (ptrdiff_t) endian)
  3778. {
  3779. if (sizeof (mp_limb_t) == 1)
  3780. {
  3781. if (i < un)
  3782. *p = u->_mp_d[i++];
  3783. else
  3784. *p = 0;
  3785. }
  3786. else
  3787. {
  3788. int LOCAL_CHAR_BIT = CHAR_BIT;
  3789. if (bytes == 0)
  3790. {
  3791. if (i < un)
  3792. limb = u->_mp_d[i++];
  3793. bytes = sizeof (mp_limb_t);
  3794. }
  3795. *p = limb;
  3796. limb >>= LOCAL_CHAR_BIT;
  3797. bytes--;
  3798. }
  3799. }
  3800. }
  3801. assert (i == un);
  3802. assert (k == count);
  3803. }
  3804. if (countp)
  3805. *countp = count;
  3806. return r;
  3807. }