mini-gmp.c 87 KB

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