mini-gmp.c 89 KB

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