integers.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432
  1. /* Copyright 1995-2016,2018-2022
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <math.h>
  19. #include <stdlib.h>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <verify.h>
  24. #include "boolean.h"
  25. #include "numbers.h"
  26. #include "strings.h"
  27. #include "integers.h"
  28. /* Some functions that use GMP's mpn functions assume that a
  29. non-negative fixnum will always fit in a 'mp_limb_t'. */
  30. verify (SCM_MOST_POSITIVE_FIXNUM <= (mp_limb_t) -1);
  31. #define NLIMBS_MAX (SSIZE_MAX / sizeof(mp_limb_t))
  32. #if !(__MINGW32__ && __x86_64__)
  33. #define L1 1L
  34. #else /* (__MINGW32__ && __x86_64__) */
  35. #define L1 1LL
  36. #endif /* (__MINGW32__ && __x86_64__) */
  37. #ifndef NDEBUG
  38. #define ASSERT(x) \
  39. do { \
  40. if (!(x)) \
  41. { \
  42. fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
  43. abort(); \
  44. } \
  45. } while (0)
  46. #else
  47. #define ASSERT(x) do { } while (0)
  48. #endif
  49. struct scm_bignum
  50. {
  51. scm_t_bits tag;
  52. /* FIXME: In Guile 3.2, replace this union with just a "size" member.
  53. Digits are always allocated inline. */
  54. union {
  55. mpz_t mpz;
  56. struct {
  57. int zero;
  58. int size;
  59. mp_limb_t *limbs;
  60. } z;
  61. } u;
  62. mp_limb_t limbs[];
  63. };
  64. static int
  65. bignum_size (struct scm_bignum *z)
  66. {
  67. return z->u.z.size;
  68. }
  69. static int
  70. bignum_is_negative (struct scm_bignum *z)
  71. {
  72. return bignum_size (z) < 0;
  73. }
  74. static int
  75. bignum_is_positive (struct scm_bignum *z)
  76. {
  77. return bignum_size (z) > 0;
  78. }
  79. static size_t
  80. bignum_limb_count (struct scm_bignum *z)
  81. {
  82. return bignum_is_negative (z) ? -bignum_size (z) : bignum_size (z);
  83. }
  84. static mp_limb_t*
  85. bignum_limbs (struct scm_bignum *z)
  86. {
  87. // FIXME: In the future we can just return z->limbs.
  88. return z->u.z.limbs;
  89. }
  90. static inline uintptr_t
  91. intptr_t_magnitude (intptr_t l)
  92. {
  93. uintptr_t mag = l;
  94. return l < 0 ? ~mag + 1 : mag;
  95. }
  96. static inline intptr_t
  97. negative_intptr_t (uintptr_t mag)
  98. {
  99. ASSERT (mag <= (uintptr_t) INTPTR_MIN);
  100. return ~mag + 1;
  101. }
  102. static inline int64_t
  103. negative_int64 (uint64_t mag)
  104. {
  105. ASSERT (mag <= (uint64_t) INT64_MIN);
  106. return ~mag + 1;
  107. }
  108. static inline uint64_t
  109. int64_magnitude (int64_t i)
  110. {
  111. uint64_t mag = i;
  112. if (i < 0)
  113. mag = ~mag + 1;
  114. return mag;
  115. }
  116. static inline scm_t_bits
  117. inum_magnitude (intptr_t i)
  118. {
  119. scm_t_bits mag = i;
  120. if (i < 0)
  121. mag = ~mag + 1;
  122. return mag;
  123. }
  124. static struct scm_bignum *
  125. allocate_bignum (size_t nlimbs)
  126. {
  127. ASSERT (nlimbs <= (size_t)INT_MAX);
  128. ASSERT (nlimbs <= NLIMBS_MAX);
  129. size_t size = sizeof (struct scm_bignum) + nlimbs * sizeof(mp_limb_t);
  130. struct scm_bignum *z = scm_gc_malloc_pointerless (size, "bignum");
  131. z->tag = scm_tc16_big;
  132. z->u.z.zero = 0;
  133. z->u.z.size = nlimbs;
  134. z->u.z.limbs = z->limbs;
  135. // _mp_alloc == 0 means GMP will never try to free this memory.
  136. ASSERT (z->u.mpz[0]._mp_alloc == 0);
  137. // Our "size" field should alias the mpz's _mp_size field.
  138. ASSERT (z->u.mpz[0]._mp_size == nlimbs);
  139. // Limbs are always allocated inline.
  140. ASSERT (z->u.mpz[0]._mp_d == z->limbs);
  141. // z->limbs left uninitialized.
  142. return z;
  143. }
  144. static struct scm_bignum *
  145. bignum_trim1 (struct scm_bignum *z)
  146. {
  147. ASSERT (z->u.z.size > 0);
  148. z->u.z.size -= (z->limbs[z->u.z.size - 1] == 0);
  149. return z;
  150. }
  151. static struct scm_bignum *
  152. bignum_trimn (struct scm_bignum *z)
  153. {
  154. ASSERT (z->u.z.size > 0);
  155. while (z->u.z.size > 0 && z->limbs[z->u.z.size - 1] == 0)
  156. z->u.z.size--;
  157. return z;
  158. }
  159. static struct scm_bignum *
  160. negate_bignum (struct scm_bignum *z)
  161. {
  162. z->u.z.size = -z->u.z.size;
  163. return z;
  164. }
  165. static struct scm_bignum *
  166. bignum_negate_if (int negate, struct scm_bignum *z)
  167. {
  168. return negate ? negate_bignum (z) : z;
  169. }
  170. static struct scm_bignum *
  171. make_bignum_0 (void)
  172. {
  173. return allocate_bignum (0);
  174. }
  175. static struct scm_bignum *
  176. make_bignum_1 (int is_negative, mp_limb_t limb)
  177. {
  178. struct scm_bignum *z = allocate_bignum (1);
  179. z->limbs[0] = limb;
  180. return is_negative ? negate_bignum(z) : z;
  181. }
  182. static struct scm_bignum *
  183. make_bignum_2 (int is_negative, mp_limb_t lo, mp_limb_t hi)
  184. {
  185. struct scm_bignum *z = allocate_bignum (2);
  186. z->limbs[0] = lo;
  187. z->limbs[1] = hi;
  188. return is_negative ? negate_bignum(z) : z;
  189. }
  190. static struct scm_bignum *
  191. make_bignum_from_uint64 (uint64_t val)
  192. {
  193. #if SCM_SIZEOF_INTPTR_T == 4
  194. if (val > UINT32_MAX)
  195. return make_bignum_2 (0, val, val >> 32);
  196. #endif
  197. return val == 0 ? make_bignum_0 () : make_bignum_1 (0, val);
  198. }
  199. static struct scm_bignum *
  200. make_bignum_from_int64 (int64_t val)
  201. {
  202. return val < 0
  203. ? negate_bignum (make_bignum_from_uint64 (int64_magnitude (val)))
  204. : make_bignum_from_uint64 (val);
  205. }
  206. static struct scm_bignum *
  207. uintptr_t_to_bignum (uintptr_t u)
  208. {
  209. return u == 0 ? make_bignum_0 () : make_bignum_1 (0, u);
  210. };
  211. static struct scm_bignum *
  212. intptr_t_to_bignum (intptr_t i)
  213. {
  214. if (i > 0)
  215. return uintptr_t_to_bignum (i);
  216. return i == 0 ? make_bignum_0 () : make_bignum_1 (1, intptr_t_magnitude (i));
  217. };
  218. static inline SCM
  219. scm_from_bignum (struct scm_bignum *x)
  220. {
  221. return SCM_PACK (x);
  222. }
  223. static SCM
  224. intptr_t_to_scm (intptr_t i)
  225. {
  226. if (SCM_FIXABLE (i))
  227. return SCM_I_MAKINUM (i);
  228. return scm_from_bignum (intptr_t_to_bignum (i));
  229. }
  230. static SCM
  231. uintptr_t_to_scm (uintptr_t i)
  232. {
  233. if (SCM_POSFIXABLE (i))
  234. return SCM_I_MAKINUM (i);
  235. return scm_from_bignum (uintptr_t_to_bignum (i));
  236. }
  237. static struct scm_bignum *
  238. clone_bignum (struct scm_bignum *z)
  239. {
  240. struct scm_bignum *ret = allocate_bignum (bignum_limb_count (z));
  241. mpn_copyi (bignum_limbs (ret), bignum_limbs (z), bignum_limb_count (z));
  242. return bignum_is_negative (z) ? negate_bignum (ret) : ret;
  243. }
  244. static void
  245. alias_bignum_to_mpz (struct scm_bignum *z, mpz_ptr mpz)
  246. {
  247. // No need to clear this mpz.
  248. mpz->_mp_alloc = 0;
  249. mpz->_mp_size = bignum_size (z);
  250. // Gotta be careful to keep z alive.
  251. mpz->_mp_d = bignum_limbs (z);
  252. }
  253. static struct scm_bignum *
  254. make_bignum_from_mpz (mpz_srcptr mpz)
  255. {
  256. size_t nlimbs = mpz_size (mpz);
  257. struct scm_bignum *ret = allocate_bignum (nlimbs);
  258. mpn_copyi (bignum_limbs (ret), mpz_limbs_read (mpz), nlimbs);
  259. return mpz_sgn (mpz) < 0 ? negate_bignum (ret) : ret;
  260. }
  261. static SCM
  262. normalize_bignum (struct scm_bignum *z)
  263. {
  264. switch (bignum_size (z))
  265. {
  266. case -1:
  267. if (bignum_limbs (z)[0] <= inum_magnitude (SCM_MOST_NEGATIVE_FIXNUM))
  268. return SCM_I_MAKINUM (negative_intptr_t (bignum_limbs (z)[0]));
  269. break;
  270. case 0:
  271. return SCM_INUM0;
  272. case 1:
  273. if (bignum_limbs (z)[0] <= SCM_MOST_POSITIVE_FIXNUM)
  274. return SCM_I_MAKINUM (bignum_limbs (z)[0]);
  275. break;
  276. default:
  277. break;
  278. }
  279. return scm_from_bignum (z);
  280. }
  281. static SCM
  282. take_mpz (mpz_ptr mpz)
  283. {
  284. SCM ret;
  285. if (mpz_fits_slong_p (mpz))
  286. ret = intptr_t_to_scm (mpz_get_si (mpz));
  287. else
  288. ret = scm_from_bignum (make_bignum_from_mpz (mpz));
  289. mpz_clear (mpz);
  290. return ret;
  291. }
  292. static int
  293. intptr_t_sign (intptr_t l)
  294. {
  295. if (l < 0) return -1;
  296. if (l == 0) return 0;
  297. return 1;
  298. }
  299. static int
  300. negative_uint64_to_int64 (uint64_t magnitude, int64_t *val)
  301. {
  302. if (magnitude > int64_magnitude (INT64_MIN))
  303. return 0;
  304. *val = negative_int64 (magnitude);
  305. return 1;
  306. }
  307. static int
  308. positive_uint64_to_int64 (uint64_t magnitude, int64_t *val)
  309. {
  310. if (magnitude > INT64_MAX)
  311. return 0;
  312. *val = magnitude;
  313. return 1;
  314. }
  315. static int
  316. bignum_to_int64 (struct scm_bignum *z, int64_t *val)
  317. {
  318. switch (bignum_size (z))
  319. {
  320. #if SCM_SIZEOF_INTPTR_T == 4
  321. case -2:
  322. {
  323. uint64_t mag = bignum_limbs (z)[0];
  324. mag |= ((uint64_t) bignum_limbs (z)[1]) << 32;
  325. return negative_uint64_to_int64 (mag, val);
  326. }
  327. #endif
  328. case -1:
  329. return negative_uint64_to_int64 (bignum_limbs (z)[0], val);
  330. case 0:
  331. *val = 0;
  332. return 1;
  333. case 1:
  334. return positive_uint64_to_int64 (bignum_limbs (z)[0], val);
  335. #if SCM_SIZEOF_INTPTR_T == 4
  336. case 2:
  337. {
  338. uint64_t mag = bignum_limbs (z)[0];
  339. mag |= ((uint64_t) bignum_limbs (z)[1]) << 32;
  340. return positive_uint64_to_int64 (mag, val);
  341. }
  342. #endif
  343. default:
  344. return 0;
  345. }
  346. }
  347. static int
  348. bignum_to_uint64 (struct scm_bignum *z, uint64_t *val)
  349. {
  350. switch (bignum_size (z))
  351. {
  352. case 0:
  353. *val = 0;
  354. return 1;
  355. case 1:
  356. *val = bignum_limbs (z)[0];
  357. return 1;
  358. #if SCM_SIZEOF_INTPTR_T == 4
  359. case 2:
  360. {
  361. uint64_t mag = bignum_limbs (z)[0];
  362. mag |= ((uint64_t) bignum_limbs (z)[1]) << 32;
  363. *val = mag;
  364. return 1;
  365. }
  366. #endif
  367. default:
  368. return 0;
  369. }
  370. }
  371. #if SCM_SIZEOF_INTPTR_T == 4
  372. static int
  373. negative_uint32_to_int32 (uint32_t magnitude, int32_t *val)
  374. {
  375. if (magnitude > intptr_t_magnitude (INT32_MIN))
  376. return 0;
  377. *val = negative_intptr_t (magnitude);
  378. return 1;
  379. }
  380. static int
  381. positive_uint32_to_int32 (uint32_t magnitude, int32_t *val)
  382. {
  383. if (magnitude > INT32_MAX)
  384. return 0;
  385. *val = magnitude;
  386. return 1;
  387. }
  388. static int
  389. bignum_to_int32 (struct scm_bignum *z, int32_t *val)
  390. {
  391. switch (bignum_size (z))
  392. {
  393. case -1:
  394. return negative_uint32_to_int32 (bignum_limbs (z)[0], val);
  395. case 0:
  396. *val = 0;
  397. return 1;
  398. case 1:
  399. return positive_uint32_to_int32 (bignum_limbs (z)[0], val);
  400. default:
  401. return 0;
  402. }
  403. }
  404. static int
  405. bignum_to_uint32 (struct scm_bignum *z, uint32_t *val)
  406. {
  407. switch (bignum_size (z))
  408. {
  409. case 0:
  410. *val = 0;
  411. return 1;
  412. case 1:
  413. *val = bignum_limbs (z)[0];
  414. return 1;
  415. default:
  416. return 0;
  417. }
  418. }
  419. #endif
  420. static int
  421. bignum_cmp_intptr_t (struct scm_bignum *z, intptr_t l)
  422. {
  423. switch (bignum_size (z))
  424. {
  425. case -1:
  426. if (l >= 0)
  427. return -1;
  428. return intptr_t_sign (intptr_t_magnitude (l) - bignum_limbs (z)[0]);
  429. case 0:
  430. return intptr_t_sign (l);
  431. case 1:
  432. if (l <= 0)
  433. return 1;
  434. return intptr_t_sign (bignum_limbs (z)[0] - (uintptr_t) l);
  435. default:
  436. return intptr_t_sign (bignum_size (z));
  437. }
  438. }
  439. SCM
  440. scm_integer_from_mpz (const mpz_t mpz)
  441. {
  442. return normalize_bignum (make_bignum_from_mpz (mpz));
  443. }
  444. int
  445. scm_is_integer_odd_i (intptr_t i)
  446. {
  447. return i & 1;
  448. }
  449. int
  450. scm_is_integer_odd_z (struct scm_bignum *z)
  451. {
  452. return bignum_limbs (z)[0] & 1;
  453. }
  454. SCM
  455. scm_integer_abs_i (intptr_t i)
  456. {
  457. if (i >= 0)
  458. return SCM_I_MAKINUM (i);
  459. return uintptr_t_to_scm (intptr_t_magnitude (i));
  460. }
  461. SCM
  462. scm_integer_abs_z (struct scm_bignum *z)
  463. {
  464. if (!bignum_is_negative (z))
  465. return scm_from_bignum (z);
  466. return scm_integer_negate_z (z);
  467. }
  468. SCM
  469. scm_integer_floor_quotient_ii (intptr_t x, intptr_t y)
  470. {
  471. if (y > 0)
  472. {
  473. if (x < 0)
  474. x = x - y + 1;
  475. }
  476. else if (y == 0)
  477. scm_num_overflow ("floor-quotient");
  478. else if (x > 0)
  479. x = x - y - 1;
  480. intptr_t q = x / y;
  481. return intptr_t_to_scm (q);
  482. }
  483. SCM
  484. scm_integer_floor_quotient_iz (intptr_t x, struct scm_bignum *y)
  485. {
  486. if (x == 0 || ((x < 0) == bignum_is_negative (y)))
  487. return SCM_INUM0;
  488. return SCM_I_MAKINUM (-1);
  489. }
  490. SCM
  491. scm_integer_floor_quotient_zi (struct scm_bignum *x, intptr_t y)
  492. {
  493. if (y == 0)
  494. scm_num_overflow ("floor-quotient");
  495. else if (y == 1)
  496. return scm_from_bignum (x);
  497. mpz_t zx, q;
  498. alias_bignum_to_mpz (x, zx);
  499. mpz_init (q);
  500. if (y > 0)
  501. mpz_fdiv_q_ui (q, zx, y);
  502. else
  503. {
  504. mpz_cdiv_q_ui (q, zx, -y);
  505. mpz_neg (q, q);
  506. }
  507. scm_remember_upto_here_1 (x);
  508. return take_mpz (q);
  509. }
  510. SCM
  511. scm_integer_floor_quotient_zz (struct scm_bignum *x, struct scm_bignum *y)
  512. {
  513. mpz_t zx, zy, q;
  514. alias_bignum_to_mpz (x, zx);
  515. alias_bignum_to_mpz (y, zy);
  516. mpz_init (q);
  517. mpz_fdiv_q (q, zx, zy);
  518. scm_remember_upto_here_2 (x, y);
  519. return take_mpz (q);
  520. }
  521. SCM
  522. scm_integer_floor_remainder_ii (intptr_t x, intptr_t y)
  523. {
  524. if (y == 0)
  525. scm_num_overflow ("floor-remainder");
  526. intptr_t r = x % y;
  527. int needs_adjustment = (y > 0) ? (r < 0) : (r > 0);
  528. if (needs_adjustment)
  529. r += y;
  530. return SCM_I_MAKINUM (r);
  531. }
  532. SCM
  533. scm_integer_floor_remainder_iz (intptr_t x, struct scm_bignum *y)
  534. {
  535. if (bignum_is_positive (y))
  536. {
  537. if (x < 0)
  538. {
  539. mpz_t r, zy;
  540. mpz_init (r);
  541. alias_bignum_to_mpz (y, zy);
  542. mpz_sub_ui (r, zy, -x);
  543. scm_remember_upto_here_1 (y);
  544. return take_mpz (r);
  545. }
  546. else
  547. return SCM_I_MAKINUM (x);
  548. }
  549. else if (x <= 0)
  550. return SCM_I_MAKINUM (x);
  551. else
  552. {
  553. mpz_t r, zy;
  554. mpz_init (r);
  555. alias_bignum_to_mpz (y, zy);
  556. mpz_add_ui (r, zy, x);
  557. scm_remember_upto_here_1 (y);
  558. return take_mpz (r);
  559. }
  560. }
  561. SCM
  562. scm_integer_floor_remainder_zi (struct scm_bignum *x, intptr_t y)
  563. {
  564. if (y == 0)
  565. scm_num_overflow ("floor-remainder");
  566. else
  567. {
  568. intptr_t r;
  569. mpz_t zx;
  570. alias_bignum_to_mpz (x, zx);
  571. if (y > 0)
  572. r = mpz_fdiv_ui (zx, y);
  573. else
  574. r = -mpz_cdiv_ui (zx, -y);
  575. scm_remember_upto_here_1 (x);
  576. return SCM_I_MAKINUM (r);
  577. }
  578. }
  579. SCM
  580. scm_integer_floor_remainder_zz (struct scm_bignum *x, struct scm_bignum *y)
  581. {
  582. mpz_t zx, zy, r;
  583. alias_bignum_to_mpz (x, zx);
  584. alias_bignum_to_mpz (y, zy);
  585. mpz_init (r);
  586. mpz_fdiv_r (r, zx, zy);
  587. scm_remember_upto_here_2 (x, y);
  588. return take_mpz (r);
  589. }
  590. void
  591. scm_integer_floor_divide_ii (intptr_t x, intptr_t y, SCM *qp, SCM *rp)
  592. {
  593. if (y == 0)
  594. scm_num_overflow ("floor-divide");
  595. intptr_t q = x / y;
  596. intptr_t r = x % y;
  597. int needs_adjustment = (y > 0) ? (r < 0) : (r > 0);
  598. if (needs_adjustment)
  599. {
  600. r += y;
  601. q--;
  602. }
  603. *qp = intptr_t_to_scm (q);
  604. *rp = SCM_I_MAKINUM (r);
  605. }
  606. void
  607. scm_integer_floor_divide_iz (intptr_t x, struct scm_bignum *y, SCM *qp, SCM *rp)
  608. {
  609. if (bignum_is_positive (y))
  610. {
  611. if (x < 0)
  612. {
  613. mpz_t zy, r;
  614. alias_bignum_to_mpz (y, zy);
  615. mpz_init (r);
  616. mpz_sub_ui (r, zy, -x);
  617. scm_remember_upto_here_1 (y);
  618. *qp = SCM_I_MAKINUM (-1);
  619. *rp = take_mpz (r);
  620. }
  621. else
  622. {
  623. *qp = SCM_INUM0;
  624. *rp = SCM_I_MAKINUM (x);
  625. }
  626. }
  627. else if (x <= 0)
  628. {
  629. *qp = SCM_INUM0;
  630. *rp = SCM_I_MAKINUM (x);
  631. }
  632. else
  633. {
  634. mpz_t zy, r;
  635. alias_bignum_to_mpz (y, zy);
  636. mpz_init (r);
  637. mpz_add_ui (r, zy, x);
  638. scm_remember_upto_here_1 (y);
  639. *qp = SCM_I_MAKINUM (-1);
  640. *rp = take_mpz (r);
  641. }
  642. }
  643. void
  644. scm_integer_floor_divide_zi (struct scm_bignum *x, intptr_t y, SCM *qp, SCM *rp)
  645. {
  646. if (y == 0)
  647. scm_num_overflow ("floor-divide");
  648. mpz_t zx, q, r;
  649. alias_bignum_to_mpz (x, zx);
  650. mpz_init (q);
  651. mpz_init (r);
  652. if (y > 0)
  653. mpz_fdiv_qr_ui (q, r, zx, y);
  654. else
  655. {
  656. mpz_cdiv_qr_ui (q, r, zx, -y);
  657. mpz_neg (q, q);
  658. }
  659. scm_remember_upto_here_1 (x);
  660. *qp = take_mpz (q);
  661. *rp = take_mpz (r);
  662. }
  663. void
  664. scm_integer_floor_divide_zz (struct scm_bignum *x, struct scm_bignum *y, SCM *qp, SCM *rp)
  665. {
  666. mpz_t zx, zy, q, r;
  667. mpz_init (q);
  668. mpz_init (r);
  669. alias_bignum_to_mpz (x, zx);
  670. alias_bignum_to_mpz (y, zy);
  671. mpz_fdiv_qr (q, r, zx, zy);
  672. scm_remember_upto_here_2 (x, y);
  673. *qp = take_mpz (q);
  674. *rp = take_mpz (r);
  675. }
  676. SCM
  677. scm_integer_ceiling_quotient_ii (intptr_t x, intptr_t y)
  678. {
  679. if (y == 0)
  680. scm_num_overflow ("ceiling-quotient");
  681. if (y > 0)
  682. {
  683. if (x >= 0)
  684. x = x + y - 1;
  685. }
  686. else if (x < 0)
  687. x = x + y + 1;
  688. intptr_t q = x / y;
  689. return intptr_t_to_scm (q);
  690. }
  691. SCM
  692. scm_integer_ceiling_quotient_iz (intptr_t x, struct scm_bignum *y)
  693. {
  694. if (bignum_is_positive (y))
  695. {
  696. if (x > 0)
  697. return SCM_INUM1;
  698. else if (x == SCM_MOST_NEGATIVE_FIXNUM &&
  699. bignum_cmp_intptr_t (y, -SCM_MOST_NEGATIVE_FIXNUM) == 0)
  700. {
  701. /* Special case: x == fixnum-min && y == abs (fixnum-min) */
  702. scm_remember_upto_here_1 (y);
  703. return SCM_I_MAKINUM (-1);
  704. }
  705. else
  706. return SCM_INUM0;
  707. }
  708. else if (x >= 0)
  709. return SCM_INUM0;
  710. else
  711. return SCM_INUM1;
  712. }
  713. SCM
  714. scm_integer_ceiling_quotient_zi (struct scm_bignum *x, intptr_t y)
  715. {
  716. if (y == 0)
  717. scm_num_overflow ("ceiling-quotient");
  718. else if (y == 1)
  719. return scm_from_bignum (x);
  720. else
  721. {
  722. mpz_t q, zx;
  723. mpz_init (q);
  724. alias_bignum_to_mpz (x, zx);
  725. if (y > 0)
  726. mpz_cdiv_q_ui (q, zx, y);
  727. else
  728. {
  729. mpz_fdiv_q_ui (q, zx, -y);
  730. mpz_neg (q, q);
  731. }
  732. scm_remember_upto_here_1 (x);
  733. return take_mpz (q);
  734. }
  735. }
  736. SCM
  737. scm_integer_ceiling_quotient_zz (struct scm_bignum *x, struct scm_bignum *y)
  738. {
  739. mpz_t q, zx, zy;
  740. mpz_init (q);
  741. alias_bignum_to_mpz (x, zx);
  742. alias_bignum_to_mpz (y, zy);
  743. mpz_cdiv_q (q, zx, zy);
  744. scm_remember_upto_here_2 (x, y);
  745. return take_mpz (q);
  746. }
  747. SCM
  748. scm_integer_ceiling_remainder_ii (intptr_t x, intptr_t y)
  749. {
  750. if (y == 0)
  751. scm_num_overflow ("ceiling-remainder");
  752. intptr_t r = x % y;
  753. int needs_adjustment = (y > 0) ? (r > 0) : (r < 0);
  754. if (needs_adjustment)
  755. r -= y;
  756. return SCM_I_MAKINUM (r);
  757. }
  758. SCM
  759. scm_integer_ceiling_remainder_iz (intptr_t x, struct scm_bignum *y)
  760. {
  761. if (bignum_is_positive (y))
  762. {
  763. if (x > 0)
  764. {
  765. mpz_t r, zy;
  766. mpz_init (r);
  767. alias_bignum_to_mpz (y, zy);
  768. mpz_sub_ui (r, zy, x);
  769. scm_remember_upto_here_1 (y);
  770. mpz_neg (r, r);
  771. return take_mpz (r);
  772. }
  773. else if (x == SCM_MOST_NEGATIVE_FIXNUM &&
  774. bignum_cmp_intptr_t (y, -SCM_MOST_NEGATIVE_FIXNUM) == 0)
  775. {
  776. /* Special case: x == fixnum-min && y == abs (fixnum-min) */
  777. scm_remember_upto_here_1 (y);
  778. return SCM_INUM0;
  779. }
  780. else
  781. return SCM_I_MAKINUM (x);
  782. }
  783. else if (x >= 0)
  784. return SCM_I_MAKINUM (x);
  785. else
  786. {
  787. mpz_t r, zy;
  788. mpz_init (r);
  789. alias_bignum_to_mpz (y, zy);
  790. mpz_add_ui (r, zy, -x);
  791. scm_remember_upto_here_1 (y);
  792. mpz_neg (r, r);
  793. return take_mpz (r);
  794. }
  795. }
  796. SCM
  797. scm_integer_ceiling_remainder_zi (struct scm_bignum *x, intptr_t y)
  798. {
  799. if (y == 0)
  800. scm_num_overflow ("ceiling-remainder");
  801. else
  802. {
  803. mpz_t zx;
  804. alias_bignum_to_mpz (x, zx);
  805. intptr_t r;
  806. if (y > 0)
  807. r = -mpz_cdiv_ui (zx, y);
  808. else
  809. r = mpz_fdiv_ui (zx, -y);
  810. scm_remember_upto_here_1 (x);
  811. return SCM_I_MAKINUM (r);
  812. }
  813. }
  814. SCM
  815. scm_integer_ceiling_remainder_zz (struct scm_bignum *x, struct scm_bignum *y)
  816. {
  817. mpz_t r, zx, zy;
  818. mpz_init (r);
  819. alias_bignum_to_mpz (x, zx);
  820. alias_bignum_to_mpz (y, zy);
  821. mpz_cdiv_r (r, zx, zy);
  822. scm_remember_upto_here_2 (x, y);
  823. return take_mpz (r);
  824. }
  825. void
  826. scm_integer_ceiling_divide_ii (intptr_t x, intptr_t y, SCM *qp, SCM *rp)
  827. {
  828. if (y == 0)
  829. scm_num_overflow ("ceiling-divide");
  830. else
  831. {
  832. intptr_t q = x / y;
  833. intptr_t r = x % y;
  834. int needs_adjustment;
  835. if (y > 0)
  836. needs_adjustment = (r > 0);
  837. else
  838. needs_adjustment = (r < 0);
  839. if (needs_adjustment)
  840. {
  841. r -= y;
  842. q++;
  843. }
  844. *qp = intptr_t_to_scm (q);
  845. *rp = SCM_I_MAKINUM (r);
  846. }
  847. }
  848. void
  849. scm_integer_ceiling_divide_iz (intptr_t x, struct scm_bignum *y, SCM *qp, SCM *rp)
  850. {
  851. if (bignum_is_positive (y))
  852. {
  853. if (x > 0)
  854. {
  855. mpz_t r, zy;
  856. mpz_init (r);
  857. alias_bignum_to_mpz (y, zy);
  858. mpz_sub_ui (r, zy, x);
  859. scm_remember_upto_here_1 (y);
  860. mpz_neg (r, r);
  861. *qp = SCM_INUM1;
  862. *rp = take_mpz (r);
  863. }
  864. else if (x == SCM_MOST_NEGATIVE_FIXNUM &&
  865. bignum_cmp_intptr_t (y, -SCM_MOST_NEGATIVE_FIXNUM) == 0)
  866. {
  867. /* Special case: x == fixnum-min && y == abs (fixnum-min) */
  868. scm_remember_upto_here_1 (y);
  869. *qp = SCM_I_MAKINUM (-1);
  870. *rp = SCM_INUM0;
  871. }
  872. else
  873. {
  874. *qp = SCM_INUM0;
  875. *rp = SCM_I_MAKINUM (x);
  876. }
  877. }
  878. else if (x >= 0)
  879. {
  880. *qp = SCM_INUM0;
  881. *rp = SCM_I_MAKINUM (x);
  882. }
  883. else
  884. {
  885. mpz_t r, zy;
  886. mpz_init (r);
  887. alias_bignum_to_mpz (y, zy);
  888. mpz_add_ui (r, zy, -x);
  889. scm_remember_upto_here_1 (y);
  890. mpz_neg (r, r);
  891. *qp = SCM_INUM1;
  892. *rp = take_mpz (r);
  893. }
  894. }
  895. void
  896. scm_integer_ceiling_divide_zi (struct scm_bignum *x, intptr_t y, SCM *qp, SCM *rp)
  897. {
  898. if (y == 0)
  899. scm_num_overflow ("ceiling-divide");
  900. else
  901. {
  902. mpz_t q, r, zx;
  903. mpz_init (q);
  904. mpz_init (r);
  905. alias_bignum_to_mpz (x, zx);
  906. if (y > 0)
  907. mpz_cdiv_qr_ui (q, r, zx, y);
  908. else
  909. {
  910. mpz_fdiv_qr_ui (q, r, zx, -y);
  911. mpz_neg (q, q);
  912. }
  913. scm_remember_upto_here_1 (x);
  914. *qp = take_mpz (q);
  915. *rp = take_mpz (r);
  916. }
  917. }
  918. void
  919. scm_integer_ceiling_divide_zz (struct scm_bignum *x, struct scm_bignum *y, SCM *qp, SCM *rp)
  920. {
  921. mpz_t q, r, zx, zy;
  922. mpz_init (q);
  923. mpz_init (r);
  924. alias_bignum_to_mpz (x, zx);
  925. alias_bignum_to_mpz (y, zy);
  926. mpz_cdiv_qr (q, r, zx, zy);
  927. scm_remember_upto_here_2 (x, y);
  928. *qp = take_mpz (q);
  929. *rp = take_mpz (r);
  930. }
  931. SCM
  932. scm_integer_truncate_quotient_ii (intptr_t x, intptr_t y)
  933. {
  934. if (y == 0)
  935. scm_num_overflow ("truncate-quotient");
  936. else
  937. {
  938. intptr_t q = x / y;
  939. return intptr_t_to_scm (q);
  940. }
  941. }
  942. SCM
  943. scm_integer_truncate_quotient_iz (intptr_t x, struct scm_bignum *y)
  944. {
  945. if (x == SCM_MOST_NEGATIVE_FIXNUM &&
  946. bignum_cmp_intptr_t (y, -SCM_MOST_NEGATIVE_FIXNUM) == 0)
  947. {
  948. /* Special case: x == fixnum-min && y == abs (fixnum-min) */
  949. scm_remember_upto_here_1 (y);
  950. return SCM_I_MAKINUM (-1);
  951. }
  952. else
  953. return SCM_INUM0;
  954. }
  955. SCM
  956. scm_integer_truncate_quotient_zi (struct scm_bignum *x, intptr_t y)
  957. {
  958. if (y == 0)
  959. scm_num_overflow ("truncate-quotient");
  960. else if (y == 1)
  961. return scm_from_bignum (x);
  962. else
  963. {
  964. mpz_t q, zx;
  965. mpz_init (q);
  966. alias_bignum_to_mpz (x, zx);
  967. if (y > 0)
  968. mpz_tdiv_q_ui (q, zx, y);
  969. else
  970. {
  971. mpz_tdiv_q_ui (q, zx, -y);
  972. mpz_neg (q, q);
  973. }
  974. scm_remember_upto_here_1 (x);
  975. return take_mpz (q);
  976. }
  977. }
  978. SCM
  979. scm_integer_truncate_quotient_zz (struct scm_bignum *x, struct scm_bignum *y)
  980. {
  981. mpz_t q, zx, zy;
  982. mpz_init (q);
  983. alias_bignum_to_mpz (x, zx);
  984. alias_bignum_to_mpz (y, zy);
  985. mpz_tdiv_q (q, zx, zy);
  986. scm_remember_upto_here_2 (x, y);
  987. return take_mpz (q);
  988. }
  989. SCM
  990. scm_integer_truncate_remainder_ii (intptr_t x, intptr_t y)
  991. {
  992. if (y == 0)
  993. scm_num_overflow ("truncate-remainder");
  994. else
  995. {
  996. intptr_t q = x % y;
  997. return intptr_t_to_scm (q);
  998. }
  999. }
  1000. SCM
  1001. scm_integer_truncate_remainder_iz (intptr_t x, struct scm_bignum *y)
  1002. {
  1003. if (x == SCM_MOST_NEGATIVE_FIXNUM &&
  1004. bignum_cmp_intptr_t (y, -SCM_MOST_NEGATIVE_FIXNUM) == 0)
  1005. {
  1006. /* Special case: x == fixnum-min && y == abs (fixnum-min) */
  1007. scm_remember_upto_here_1 (y);
  1008. return SCM_INUM0;
  1009. }
  1010. else
  1011. return SCM_I_MAKINUM (x);
  1012. }
  1013. SCM
  1014. scm_integer_truncate_remainder_zi (struct scm_bignum *x, intptr_t y)
  1015. {
  1016. if (y == 0)
  1017. scm_num_overflow ("truncate-remainder");
  1018. else
  1019. {
  1020. mpz_t zx;
  1021. alias_bignum_to_mpz (x, zx);
  1022. intptr_t r = mpz_tdiv_ui (zx, (y > 0) ? y : -y) * mpz_sgn (zx);
  1023. scm_remember_upto_here_1 (x);
  1024. return SCM_I_MAKINUM (r);
  1025. }
  1026. }
  1027. SCM
  1028. scm_integer_truncate_remainder_zz (struct scm_bignum *x, struct scm_bignum *y)
  1029. {
  1030. mpz_t r, zx, zy;
  1031. mpz_init (r);
  1032. alias_bignum_to_mpz (x, zx);
  1033. alias_bignum_to_mpz (y, zy);
  1034. mpz_tdiv_r (r, zx, zy);
  1035. scm_remember_upto_here_2 (x, y);
  1036. return take_mpz (r);
  1037. }
  1038. void
  1039. scm_integer_truncate_divide_ii (intptr_t x, intptr_t y, SCM *qp, SCM *rp)
  1040. {
  1041. if (y == 0)
  1042. scm_num_overflow ("truncate-divide");
  1043. else
  1044. {
  1045. intptr_t q = x / y;
  1046. intptr_t r = x % y;
  1047. *qp = intptr_t_to_scm (q);
  1048. *rp = SCM_I_MAKINUM (r);
  1049. }
  1050. }
  1051. void
  1052. scm_integer_truncate_divide_iz (intptr_t x, struct scm_bignum *y, SCM *qp, SCM *rp)
  1053. {
  1054. if (x == SCM_MOST_NEGATIVE_FIXNUM &&
  1055. bignum_cmp_intptr_t (y, -SCM_MOST_NEGATIVE_FIXNUM) == 0)
  1056. {
  1057. /* Special case: x == fixnum-min && y == abs (fixnum-min) */
  1058. scm_remember_upto_here_1 (y);
  1059. *qp = SCM_I_MAKINUM (-1);
  1060. *rp = SCM_INUM0;
  1061. }
  1062. else
  1063. {
  1064. *qp = SCM_INUM0;
  1065. *rp = SCM_I_MAKINUM (x);
  1066. }
  1067. }
  1068. void
  1069. scm_integer_truncate_divide_zi (struct scm_bignum *x, intptr_t y, SCM *qp, SCM *rp)
  1070. {
  1071. if (y == 0)
  1072. scm_num_overflow ("truncate-divide");
  1073. else
  1074. {
  1075. mpz_t q, zx;
  1076. mpz_init (q);
  1077. alias_bignum_to_mpz (x, zx);
  1078. intptr_t r;
  1079. if (y > 0)
  1080. r = mpz_tdiv_q_ui (q, zx, y);
  1081. else
  1082. {
  1083. r = mpz_tdiv_q_ui (q, zx, -y);
  1084. mpz_neg (q, q);
  1085. }
  1086. r *= mpz_sgn (zx);
  1087. scm_remember_upto_here_1 (x);
  1088. *qp = take_mpz (q);
  1089. *rp = SCM_I_MAKINUM (r);
  1090. }
  1091. }
  1092. void
  1093. scm_integer_truncate_divide_zz (struct scm_bignum *x, struct scm_bignum *y, SCM *qp, SCM *rp)
  1094. {
  1095. mpz_t q, r, zx, zy;
  1096. mpz_init (q);
  1097. mpz_init (r);
  1098. alias_bignum_to_mpz (x, zx);
  1099. alias_bignum_to_mpz (y, zy);
  1100. mpz_tdiv_qr (q, r, zx, zy);
  1101. scm_remember_upto_here_2 (x, y);
  1102. *qp = take_mpz (q);
  1103. *rp = take_mpz (r);
  1104. }
  1105. static SCM
  1106. integer_centered_quotient_zz (struct scm_bignum *x, struct scm_bignum *y)
  1107. {
  1108. mpz_t q, r, min_r, zx, zy;
  1109. mpz_init (q);
  1110. mpz_init (r);
  1111. mpz_init (min_r);
  1112. alias_bignum_to_mpz (x, zx);
  1113. alias_bignum_to_mpz (y, zy);
  1114. /* Note that x might be small enough to fit into a fixnum, so we must
  1115. not let it escape into the wild. */
  1116. /* min_r will eventually become -abs(y)/2 */
  1117. mpz_tdiv_q_2exp (min_r, zy, 1);
  1118. /* Arrange for rr to initially be non-positive, because that
  1119. simplifies the test to see if it is within the needed bounds. */
  1120. if (mpz_sgn (zy) > 0)
  1121. {
  1122. mpz_cdiv_qr (q, r, zx, zy);
  1123. scm_remember_upto_here_2 (x, y);
  1124. mpz_neg (min_r, min_r);
  1125. if (mpz_cmp (r, min_r) < 0)
  1126. mpz_sub_ui (q, q, 1);
  1127. }
  1128. else
  1129. {
  1130. mpz_fdiv_qr (q, r, zx, zy);
  1131. scm_remember_upto_here_2 (x, y);
  1132. if (mpz_cmp (r, min_r) < 0)
  1133. mpz_add_ui (q, q, 1);
  1134. }
  1135. mpz_clear (r);
  1136. mpz_clear (min_r);
  1137. return take_mpz (q);
  1138. }
  1139. SCM
  1140. scm_integer_centered_quotient_ii (intptr_t x, intptr_t y)
  1141. {
  1142. if (y == 0)
  1143. scm_num_overflow ("centered-quotient");
  1144. intptr_t q = x / y;
  1145. intptr_t r = x % y;
  1146. if (x > 0)
  1147. {
  1148. if (y > 0)
  1149. {
  1150. if (r >= (y + 1) / 2)
  1151. q++;
  1152. }
  1153. else
  1154. {
  1155. if (r >= (1 - y) / 2)
  1156. q--;
  1157. }
  1158. }
  1159. else
  1160. {
  1161. if (y > 0)
  1162. {
  1163. if (r < -y / 2)
  1164. q--;
  1165. }
  1166. else
  1167. {
  1168. if (r < y / 2)
  1169. q++;
  1170. }
  1171. }
  1172. return intptr_t_to_scm (q);
  1173. }
  1174. SCM
  1175. scm_integer_centered_quotient_iz (intptr_t x, struct scm_bignum *y)
  1176. {
  1177. return integer_centered_quotient_zz (intptr_t_to_bignum (x),
  1178. y);
  1179. }
  1180. SCM
  1181. scm_integer_centered_quotient_zi (struct scm_bignum *x, intptr_t y)
  1182. {
  1183. if (y == 0)
  1184. scm_num_overflow ("centered-quotient");
  1185. else if (y == 1)
  1186. return scm_from_bignum (x);
  1187. else
  1188. {
  1189. mpz_t q, zx;
  1190. mpz_init (q);
  1191. alias_bignum_to_mpz (x, zx);
  1192. intptr_t r;
  1193. /* Arrange for r to initially be non-positive, because that
  1194. simplifies the test to see if it is within the needed
  1195. bounds. */
  1196. if (y > 0)
  1197. {
  1198. r = - mpz_cdiv_q_ui (q, zx, y);
  1199. scm_remember_upto_here_1 (x);
  1200. if (r < -y / 2)
  1201. mpz_sub_ui (q, q, 1);
  1202. }
  1203. else
  1204. {
  1205. r = - mpz_cdiv_q_ui (q, zx, -y);
  1206. scm_remember_upto_here_1 (x);
  1207. mpz_neg (q, q);
  1208. if (r < y / 2)
  1209. mpz_add_ui (q, q, 1);
  1210. }
  1211. return take_mpz (q);
  1212. }
  1213. }
  1214. SCM
  1215. scm_integer_centered_quotient_zz (struct scm_bignum *x, struct scm_bignum *y)
  1216. {
  1217. return integer_centered_quotient_zz (x, y);
  1218. }
  1219. static SCM
  1220. integer_centered_remainder_zz (struct scm_bignum *x, struct scm_bignum *y)
  1221. {
  1222. mpz_t r, min_r, zx, zy;
  1223. mpz_init (r);
  1224. mpz_init (min_r);
  1225. alias_bignum_to_mpz (x, zx);
  1226. alias_bignum_to_mpz (y, zy);
  1227. /* Note that x might be small enough to fit into a
  1228. fixnum, so we must not let it escape into the wild */
  1229. /* min_r will eventually become -abs(y)/2 */
  1230. mpz_tdiv_q_2exp (min_r, zy, 1);
  1231. /* Arrange for r to initially be non-positive, because that simplifies
  1232. the test to see if it is within the needed bounds. */
  1233. if (mpz_sgn (zy) > 0)
  1234. {
  1235. mpz_cdiv_r (r, zx, zy);
  1236. mpz_neg (min_r, min_r);
  1237. if (mpz_cmp (r, min_r) < 0)
  1238. mpz_add (r, r, zy);
  1239. }
  1240. else
  1241. {
  1242. mpz_fdiv_r (r, zx, zy);
  1243. if (mpz_cmp (r, min_r) < 0)
  1244. mpz_sub (r, r, zy);
  1245. }
  1246. scm_remember_upto_here_2 (x, y);
  1247. mpz_clear (min_r);
  1248. return take_mpz (r);
  1249. }
  1250. SCM
  1251. scm_integer_centered_remainder_ii (intptr_t x, intptr_t y)
  1252. {
  1253. if (y == 0)
  1254. scm_num_overflow ("centered-remainder");
  1255. intptr_t r = x % y;
  1256. if (x > 0)
  1257. {
  1258. if (y > 0)
  1259. {
  1260. if (r >= (y + 1) / 2)
  1261. r -= y;
  1262. }
  1263. else
  1264. {
  1265. if (r >= (1 - y) / 2)
  1266. r += y;
  1267. }
  1268. }
  1269. else
  1270. {
  1271. if (y > 0)
  1272. {
  1273. if (r < -y / 2)
  1274. r += y;
  1275. }
  1276. else
  1277. {
  1278. if (r < y / 2)
  1279. r -= y;
  1280. }
  1281. }
  1282. return SCM_I_MAKINUM (r);
  1283. }
  1284. SCM
  1285. scm_integer_centered_remainder_iz (intptr_t x, struct scm_bignum *y)
  1286. {
  1287. return integer_centered_remainder_zz (intptr_t_to_bignum (x),
  1288. y);
  1289. }
  1290. SCM
  1291. scm_integer_centered_remainder_zi (struct scm_bignum *x, intptr_t y)
  1292. {
  1293. mpz_t zx;
  1294. alias_bignum_to_mpz (x, zx);
  1295. if (y == 0)
  1296. scm_num_overflow ("centered-remainder");
  1297. intptr_t r;
  1298. /* Arrange for r to initially be non-positive, because that simplifies
  1299. the test to see if it is within the needed bounds. */
  1300. if (y > 0)
  1301. {
  1302. r = - mpz_cdiv_ui (zx, y);
  1303. if (r < -y / 2)
  1304. r += y;
  1305. }
  1306. else
  1307. {
  1308. r = - mpz_cdiv_ui (zx, -y);
  1309. if (r < y / 2)
  1310. r -= y;
  1311. }
  1312. scm_remember_upto_here_1 (x);
  1313. return SCM_I_MAKINUM (r);
  1314. }
  1315. SCM
  1316. scm_integer_centered_remainder_zz (struct scm_bignum *x, struct scm_bignum *y)
  1317. {
  1318. return integer_centered_remainder_zz (x, y);
  1319. }
  1320. static void
  1321. integer_centered_divide_zz (struct scm_bignum *x, struct scm_bignum *y,
  1322. SCM *qp, SCM *rp)
  1323. {
  1324. mpz_t q, r, min_r, zx, zy;
  1325. mpz_init (q);
  1326. mpz_init (r);
  1327. mpz_init (min_r);
  1328. alias_bignum_to_mpz (x, zx);
  1329. alias_bignum_to_mpz (y, zy);
  1330. /* Note that x might be small enough to fit into a fixnum, so we must
  1331. not let it escape into the wild */
  1332. /* min_r will eventually become -abs(y/2) */
  1333. mpz_tdiv_q_2exp (min_r, zy, 1);
  1334. /* Arrange for rr to initially be non-positive, because that
  1335. simplifies the test to see if it is within the needed bounds. */
  1336. if (mpz_sgn (zy) > 0)
  1337. {
  1338. mpz_cdiv_qr (q, r, zx, zy);
  1339. mpz_neg (min_r, min_r);
  1340. if (mpz_cmp (r, min_r) < 0)
  1341. {
  1342. mpz_sub_ui (q, q, 1);
  1343. mpz_add (r, r, zy);
  1344. }
  1345. }
  1346. else
  1347. {
  1348. mpz_fdiv_qr (q, r, zx, zy);
  1349. if (mpz_cmp (r, min_r) < 0)
  1350. {
  1351. mpz_add_ui (q, q, 1);
  1352. mpz_sub (r, r, zy);
  1353. }
  1354. }
  1355. scm_remember_upto_here_2 (x, y);
  1356. mpz_clear (min_r);
  1357. *qp = take_mpz (q);
  1358. *rp = take_mpz (r);
  1359. }
  1360. void
  1361. scm_integer_centered_divide_ii (intptr_t x, intptr_t y, SCM *qp, SCM *rp)
  1362. {
  1363. if (y == 0)
  1364. scm_num_overflow ("centered-divide");
  1365. intptr_t q = x / y;
  1366. intptr_t r = x % y;
  1367. if (x > 0)
  1368. {
  1369. if (y > 0)
  1370. {
  1371. if (r >= (y + 1) / 2)
  1372. { q++; r -= y; }
  1373. }
  1374. else
  1375. {
  1376. if (r >= (1 - y) / 2)
  1377. { q--; r += y; }
  1378. }
  1379. }
  1380. else
  1381. {
  1382. if (y > 0)
  1383. {
  1384. if (r < -y / 2)
  1385. { q--; r += y; }
  1386. }
  1387. else
  1388. {
  1389. if (r < y / 2)
  1390. { q++; r -= y; }
  1391. }
  1392. }
  1393. *qp = intptr_t_to_scm (q);
  1394. *rp = SCM_I_MAKINUM (r);
  1395. }
  1396. void
  1397. scm_integer_centered_divide_iz (intptr_t x, struct scm_bignum *y, SCM *qp, SCM *rp)
  1398. {
  1399. integer_centered_divide_zz (intptr_t_to_bignum (x), y, qp, rp);
  1400. }
  1401. void
  1402. scm_integer_centered_divide_zi (struct scm_bignum *x, intptr_t y, SCM *qp, SCM *rp)
  1403. {
  1404. if (y == 0)
  1405. scm_num_overflow ("centered-divide");
  1406. mpz_t q, zx;
  1407. mpz_init (q);
  1408. alias_bignum_to_mpz (x, zx);
  1409. intptr_t r;
  1410. /* Arrange for r to initially be non-positive, because that
  1411. simplifies the test to see if it is within the needed bounds. */
  1412. if (y > 0)
  1413. {
  1414. r = - mpz_cdiv_q_ui (q, zx, y);
  1415. if (r < -y / 2)
  1416. {
  1417. mpz_sub_ui (q, q, 1);
  1418. r += y;
  1419. }
  1420. }
  1421. else
  1422. {
  1423. r = - mpz_cdiv_q_ui (q, zx, -y);
  1424. mpz_neg (q, q);
  1425. if (r < y / 2)
  1426. {
  1427. mpz_add_ui (q, q, 1);
  1428. r -= y;
  1429. }
  1430. }
  1431. scm_remember_upto_here_1 (x);
  1432. *qp = take_mpz (q);
  1433. *rp = SCM_I_MAKINUM (r);
  1434. }
  1435. void
  1436. scm_integer_centered_divide_zz (struct scm_bignum *x, struct scm_bignum *y, SCM *qp, SCM *rp)
  1437. {
  1438. integer_centered_divide_zz (x, y, qp, rp);
  1439. }
  1440. static SCM
  1441. integer_round_quotient_zz (struct scm_bignum *x, struct scm_bignum *y)
  1442. {
  1443. mpz_t q, r, r2, zx, zy;
  1444. int cmp, needs_adjustment;
  1445. /* Note that x might be small enough to fit into a
  1446. fixnum, so we must not let it escape into the wild */
  1447. mpz_init (q);
  1448. mpz_init (r);
  1449. mpz_init (r2);
  1450. alias_bignum_to_mpz (x, zx);
  1451. alias_bignum_to_mpz (y, zy);
  1452. mpz_fdiv_qr (q, r, zx, zy);
  1453. mpz_mul_2exp (r2, r, 1); /* r2 = 2*r */
  1454. scm_remember_upto_here_1 (x);
  1455. cmp = mpz_cmpabs (r2, zy);
  1456. if (mpz_odd_p (q))
  1457. needs_adjustment = (cmp >= 0);
  1458. else
  1459. needs_adjustment = (cmp > 0);
  1460. scm_remember_upto_here_1 (y);
  1461. if (needs_adjustment)
  1462. mpz_add_ui (q, q, 1);
  1463. mpz_clear (r);
  1464. mpz_clear (r2);
  1465. return take_mpz (q);
  1466. }
  1467. SCM
  1468. scm_integer_round_quotient_ii (intptr_t x, intptr_t y)
  1469. {
  1470. if (y == 0)
  1471. scm_num_overflow ("round-quotient");
  1472. intptr_t q = x / y;
  1473. intptr_t r = x % y;
  1474. intptr_t ay = y;
  1475. intptr_t r2 = 2 * r;
  1476. if (y < 0)
  1477. {
  1478. ay = -ay;
  1479. r2 = -r2;
  1480. }
  1481. if (q & L1)
  1482. {
  1483. if (r2 >= ay)
  1484. q++;
  1485. else if (r2 <= -ay)
  1486. q--;
  1487. }
  1488. else
  1489. {
  1490. if (r2 > ay)
  1491. q++;
  1492. else if (r2 < -ay)
  1493. q--;
  1494. }
  1495. return intptr_t_to_scm (q);
  1496. }
  1497. SCM
  1498. scm_integer_round_quotient_iz (intptr_t x, struct scm_bignum *y)
  1499. {
  1500. return integer_round_quotient_zz (intptr_t_to_bignum (x), y);
  1501. }
  1502. SCM
  1503. scm_integer_round_quotient_zi (struct scm_bignum *x, intptr_t y)
  1504. {
  1505. if (y == 0)
  1506. scm_num_overflow ("round-quotient");
  1507. if (y == 1)
  1508. return scm_from_bignum (x);
  1509. mpz_t q, zx;
  1510. mpz_init (q);
  1511. alias_bignum_to_mpz (x, zx);
  1512. intptr_t r;
  1513. int needs_adjustment;
  1514. if (y > 0)
  1515. {
  1516. r = mpz_fdiv_q_ui (q, zx, y);
  1517. if (mpz_odd_p (q))
  1518. needs_adjustment = (2*r >= y);
  1519. else
  1520. needs_adjustment = (2*r > y);
  1521. }
  1522. else
  1523. {
  1524. r = - mpz_cdiv_q_ui (q, zx, -y);
  1525. mpz_neg (q, q);
  1526. if (mpz_odd_p (q))
  1527. needs_adjustment = (2*r <= y);
  1528. else
  1529. needs_adjustment = (2*r < y);
  1530. }
  1531. scm_remember_upto_here_1 (x);
  1532. if (needs_adjustment)
  1533. mpz_add_ui (q, q, 1);
  1534. return take_mpz (q);
  1535. }
  1536. SCM
  1537. scm_integer_round_quotient_zz (struct scm_bignum *x, struct scm_bignum *y)
  1538. {
  1539. mpz_t q, r, zx, zy;
  1540. int cmp, needs_adjustment;
  1541. mpz_init (q);
  1542. mpz_init (r);
  1543. alias_bignum_to_mpz (x, zx);
  1544. alias_bignum_to_mpz (y, zy);
  1545. mpz_fdiv_qr (q, r, zx, zy);
  1546. scm_remember_upto_here_1 (x);
  1547. mpz_mul_2exp (r, r, 1); /* r = 2*r */
  1548. cmp = mpz_cmpabs (r, zy);
  1549. mpz_clear (r);
  1550. scm_remember_upto_here_1 (y);
  1551. if (mpz_odd_p (q))
  1552. needs_adjustment = (cmp >= 0);
  1553. else
  1554. needs_adjustment = (cmp > 0);
  1555. if (needs_adjustment)
  1556. mpz_add_ui (q, q, 1);
  1557. return take_mpz (q);
  1558. }
  1559. static SCM
  1560. integer_round_remainder_zz (struct scm_bignum *x, struct scm_bignum *y)
  1561. {
  1562. mpz_t q, r, r2, zx, zy;
  1563. int cmp, needs_adjustment;
  1564. /* Note that x might be small enough to fit into a
  1565. fixnum, so we must not let it escape into the wild */
  1566. mpz_init (q);
  1567. mpz_init (r);
  1568. mpz_init (r2);
  1569. alias_bignum_to_mpz (x, zx);
  1570. alias_bignum_to_mpz (y, zy);
  1571. mpz_fdiv_qr (q, r, zx, zy);
  1572. scm_remember_upto_here_1 (x);
  1573. mpz_mul_2exp (r2, r, 1); /* r2 = 2*r */
  1574. cmp = mpz_cmpabs (r2, zy);
  1575. if (mpz_odd_p (q))
  1576. needs_adjustment = (cmp >= 0);
  1577. else
  1578. needs_adjustment = (cmp > 0);
  1579. if (needs_adjustment)
  1580. mpz_sub (r, r, zy);
  1581. scm_remember_upto_here_1 (y);
  1582. mpz_clear (q);
  1583. mpz_clear (r2);
  1584. return take_mpz (r);
  1585. }
  1586. SCM
  1587. scm_integer_round_remainder_ii (intptr_t x, intptr_t y)
  1588. {
  1589. if (y == 0)
  1590. scm_num_overflow ("round-remainder");
  1591. intptr_t q = x / y;
  1592. intptr_t r = x % y;
  1593. intptr_t ay = y;
  1594. intptr_t r2 = 2 * r;
  1595. if (y < 0)
  1596. {
  1597. ay = -ay;
  1598. r2 = -r2;
  1599. }
  1600. if (q & L1)
  1601. {
  1602. if (r2 >= ay)
  1603. r -= y;
  1604. else if (r2 <= -ay)
  1605. r += y;
  1606. }
  1607. else
  1608. {
  1609. if (r2 > ay)
  1610. r -= y;
  1611. else if (r2 < -ay)
  1612. r += y;
  1613. }
  1614. return SCM_I_MAKINUM (r);
  1615. }
  1616. SCM
  1617. scm_integer_round_remainder_iz (intptr_t x, struct scm_bignum *y)
  1618. {
  1619. return integer_round_remainder_zz (intptr_t_to_bignum (x), y);
  1620. }
  1621. SCM
  1622. scm_integer_round_remainder_zi (struct scm_bignum *x, intptr_t y)
  1623. {
  1624. if (y == 0)
  1625. scm_num_overflow ("round-remainder");
  1626. mpz_t q, zx;
  1627. intptr_t r;
  1628. int needs_adjustment;
  1629. mpz_init (q);
  1630. alias_bignum_to_mpz (x, zx);
  1631. if (y > 0)
  1632. {
  1633. r = mpz_fdiv_q_ui (q, zx, y);
  1634. if (mpz_odd_p (q))
  1635. needs_adjustment = (2*r >= y);
  1636. else
  1637. needs_adjustment = (2*r > y);
  1638. }
  1639. else
  1640. {
  1641. r = - mpz_cdiv_q_ui (q, zx, -y);
  1642. if (mpz_odd_p (q))
  1643. needs_adjustment = (2*r <= y);
  1644. else
  1645. needs_adjustment = (2*r < y);
  1646. }
  1647. scm_remember_upto_here_1 (x);
  1648. mpz_clear (q);
  1649. if (needs_adjustment)
  1650. r -= y;
  1651. return SCM_I_MAKINUM (r);
  1652. }
  1653. SCM
  1654. scm_integer_round_remainder_zz (struct scm_bignum *x, struct scm_bignum *y)
  1655. {
  1656. return integer_round_remainder_zz (x, y);
  1657. }
  1658. static void
  1659. integer_round_divide_zz (struct scm_bignum *x, struct scm_bignum *y,
  1660. SCM *qp, SCM *rp)
  1661. {
  1662. mpz_t q, r, r2, zx, zy;
  1663. int cmp, needs_adjustment;
  1664. /* Note that x might be small enough to fit into a fixnum, so we must
  1665. not let it escape into the wild */
  1666. mpz_init (q);
  1667. mpz_init (r);
  1668. mpz_init (r2);
  1669. alias_bignum_to_mpz (x, zx);
  1670. alias_bignum_to_mpz (y, zy);
  1671. mpz_fdiv_qr (q, r, zx, zy);
  1672. scm_remember_upto_here_1 (x);
  1673. mpz_mul_2exp (r2, r, 1); /* r2 = 2*r */
  1674. cmp = mpz_cmpabs (r2, zy);
  1675. if (mpz_odd_p (q))
  1676. needs_adjustment = (cmp >= 0);
  1677. else
  1678. needs_adjustment = (cmp > 0);
  1679. if (needs_adjustment)
  1680. {
  1681. mpz_add_ui (q, q, 1);
  1682. mpz_sub (r, r, zy);
  1683. }
  1684. scm_remember_upto_here_1 (y);
  1685. mpz_clear (r2);
  1686. *qp = take_mpz (q);
  1687. *rp = take_mpz (r);
  1688. }
  1689. void
  1690. scm_integer_round_divide_ii (intptr_t x, intptr_t y, SCM *qp, SCM *rp)
  1691. {
  1692. if (y == 0)
  1693. scm_num_overflow ("round-divide");
  1694. intptr_t q = x / y;
  1695. intptr_t r = x % y;
  1696. intptr_t ay = y;
  1697. intptr_t r2 = 2 * r;
  1698. if (y < 0)
  1699. {
  1700. ay = -ay;
  1701. r2 = -r2;
  1702. }
  1703. if (q & L1)
  1704. {
  1705. if (r2 >= ay)
  1706. { q++; r -= y; }
  1707. else if (r2 <= -ay)
  1708. { q--; r += y; }
  1709. }
  1710. else
  1711. {
  1712. if (r2 > ay)
  1713. { q++; r -= y; }
  1714. else if (r2 < -ay)
  1715. { q--; r += y; }
  1716. }
  1717. *qp = intptr_t_to_scm (q);
  1718. *rp = SCM_I_MAKINUM (r);
  1719. }
  1720. void
  1721. scm_integer_round_divide_iz (intptr_t x, struct scm_bignum *y, SCM *qp, SCM *rp)
  1722. {
  1723. integer_round_divide_zz (intptr_t_to_bignum (x), y, qp, rp);
  1724. }
  1725. void
  1726. scm_integer_round_divide_zi (struct scm_bignum *x, intptr_t y, SCM *qp, SCM *rp)
  1727. {
  1728. if (y == 0)
  1729. scm_num_overflow ("round-divide");
  1730. mpz_t q, zx;
  1731. mpz_init (q);
  1732. alias_bignum_to_mpz (x, zx);
  1733. intptr_t r;
  1734. int needs_adjustment;
  1735. if (y > 0)
  1736. {
  1737. r = mpz_fdiv_q_ui (q, zx, y);
  1738. if (mpz_odd_p (q))
  1739. needs_adjustment = (2*r >= y);
  1740. else
  1741. needs_adjustment = (2*r > y);
  1742. }
  1743. else
  1744. {
  1745. r = - mpz_cdiv_q_ui (q, zx, -y);
  1746. mpz_neg (q, q);
  1747. if (mpz_odd_p (q))
  1748. needs_adjustment = (2*r <= y);
  1749. else
  1750. needs_adjustment = (2*r < y);
  1751. }
  1752. scm_remember_upto_here_1 (x);
  1753. if (needs_adjustment)
  1754. {
  1755. mpz_add_ui (q, q, 1);
  1756. r -= y;
  1757. }
  1758. *qp = take_mpz (q);
  1759. *rp = SCM_I_MAKINUM (r);
  1760. }
  1761. void
  1762. scm_integer_round_divide_zz (struct scm_bignum *x, struct scm_bignum *y, SCM *qp, SCM *rp)
  1763. {
  1764. integer_round_divide_zz (x, y, qp, rp);
  1765. }
  1766. SCM
  1767. scm_integer_gcd_ii (intptr_t x, intptr_t y)
  1768. {
  1769. intptr_t u = x < 0 ? -x : x;
  1770. intptr_t v = y < 0 ? -y : y;
  1771. intptr_t result;
  1772. if (x == 0)
  1773. result = v;
  1774. else if (y == 0)
  1775. result = u;
  1776. else
  1777. {
  1778. int k = 0;
  1779. /* Determine a common factor 2^k */
  1780. while (((u | v) & 1) == 0)
  1781. {
  1782. k++;
  1783. u >>= 1;
  1784. v >>= 1;
  1785. }
  1786. /* Now, any factor 2^n can be eliminated */
  1787. if ((u & 1) == 0)
  1788. while ((u & 1) == 0)
  1789. u >>= 1;
  1790. else
  1791. while ((v & 1) == 0)
  1792. v >>= 1;
  1793. /* Both u and v are now odd. Subtract the smaller one
  1794. from the larger one to produce an even number, remove
  1795. more factors of two, and repeat. */
  1796. while (u != v)
  1797. {
  1798. if (u > v)
  1799. {
  1800. u -= v;
  1801. while ((u & 1) == 0)
  1802. u >>= 1;
  1803. }
  1804. else
  1805. {
  1806. v -= u;
  1807. while ((v & 1) == 0)
  1808. v >>= 1;
  1809. }
  1810. }
  1811. result = u << k;
  1812. }
  1813. return uintptr_t_to_scm (result);
  1814. }
  1815. SCM
  1816. scm_integer_gcd_zi (struct scm_bignum *x, intptr_t y)
  1817. {
  1818. scm_t_bits result;
  1819. if (y == 0)
  1820. return scm_integer_abs_z (x);
  1821. if (y < 0)
  1822. y = -y;
  1823. mpz_t zx;
  1824. alias_bignum_to_mpz (x, zx);
  1825. result = mpz_gcd_ui (NULL, zx, y);
  1826. scm_remember_upto_here_1 (x);
  1827. return uintptr_t_to_scm (result);
  1828. }
  1829. SCM
  1830. scm_integer_gcd_zz (struct scm_bignum *x, struct scm_bignum *y)
  1831. {
  1832. mpz_t result, zx, zy;
  1833. mpz_init (result);
  1834. alias_bignum_to_mpz (x, zx);
  1835. alias_bignum_to_mpz (y, zy);
  1836. mpz_gcd (result, zx, zy);
  1837. scm_remember_upto_here_2 (x, y);
  1838. return take_mpz (result);
  1839. }
  1840. SCM
  1841. scm_integer_lcm_ii (intptr_t x, intptr_t y)
  1842. {
  1843. SCM d = scm_integer_gcd_ii (x, y);
  1844. if (scm_is_eq (d, SCM_INUM0))
  1845. return d;
  1846. else
  1847. return scm_abs (scm_product (SCM_I_MAKINUM (x),
  1848. scm_quotient (SCM_I_MAKINUM (y), d)));
  1849. }
  1850. SCM
  1851. scm_integer_lcm_zi (struct scm_bignum *x, intptr_t y)
  1852. {
  1853. if (y == 0) return SCM_INUM0;
  1854. if (y < 0) y = - y;
  1855. mpz_t result, zx;
  1856. mpz_init (result);
  1857. alias_bignum_to_mpz (x, zx);
  1858. mpz_lcm_ui (result, zx, y);
  1859. scm_remember_upto_here_1 (x);
  1860. return take_mpz (result);
  1861. }
  1862. SCM
  1863. scm_integer_lcm_zz (struct scm_bignum *x, struct scm_bignum *y)
  1864. {
  1865. mpz_t result, zx, zy;
  1866. mpz_init (result);
  1867. alias_bignum_to_mpz (x, zx);
  1868. alias_bignum_to_mpz (y, zy);
  1869. mpz_lcm (result, zx, zy);
  1870. scm_remember_upto_here_2 (x, y);
  1871. /* shouldn't need to normalize b/c lcm of 2 bigs should be big */
  1872. return take_mpz (result);
  1873. }
  1874. /* Emulating 2's complement bignums with sign magnitude arithmetic:
  1875. Logand:
  1876. X Y Result Method:
  1877. (len)
  1878. + + + x (map digit:logand X Y)
  1879. + - + x (map digit:logand X (lognot (+ -1 Y)))
  1880. - + + y (map digit:logand (lognot (+ -1 X)) Y)
  1881. - - - (+ 1 (map digit:logior (+ -1 X) (+ -1 Y)))
  1882. Logior:
  1883. X Y Result Method:
  1884. + + + (map digit:logior X Y)
  1885. + - - y (+ 1 (map digit:logand (lognot X) (+ -1 Y)))
  1886. - + - x (+ 1 (map digit:logand (+ -1 X) (lognot Y)))
  1887. - - - x (+ 1 (map digit:logand (+ -1 X) (+ -1 Y)))
  1888. Logxor:
  1889. X Y Result Method:
  1890. + + + (map digit:logxor X Y)
  1891. + - - (+ 1 (map digit:logxor X (+ -1 Y)))
  1892. - + - (+ 1 (map digit:logxor (+ -1 X) Y))
  1893. - - + (map digit:logxor (+ -1 X) (+ -1 Y))
  1894. Logtest:
  1895. X Y Result
  1896. + + (any digit:logand X Y)
  1897. + - (any digit:logand X (lognot (+ -1 Y)))
  1898. - + (any digit:logand (lognot (+ -1 X)) Y)
  1899. - - #t
  1900. */
  1901. SCM
  1902. scm_integer_logand_ii (intptr_t x, intptr_t y)
  1903. {
  1904. return SCM_I_MAKINUM (x & y);
  1905. }
  1906. SCM
  1907. scm_integer_logand_zi (struct scm_bignum *x, intptr_t y)
  1908. {
  1909. if (y == 0)
  1910. return SCM_INUM0;
  1911. if (y > 0)
  1912. {
  1913. mp_limb_t rd = bignum_limbs (x)[0];
  1914. mp_limb_t yd = y;
  1915. if (bignum_is_negative (x))
  1916. rd = ~rd + 1;
  1917. scm_remember_upto_here_1 (x);
  1918. rd &= yd;
  1919. // Result must be a positive inum.
  1920. return SCM_I_MAKINUM (rd);
  1921. }
  1922. mpz_t result, zx, zy;
  1923. mpz_init (result);
  1924. alias_bignum_to_mpz (x, zx);
  1925. mpz_init_set_si (zy, y);
  1926. mpz_and (result, zy, zx);
  1927. scm_remember_upto_here_1 (x);
  1928. mpz_clear (zy);
  1929. return take_mpz (result);
  1930. }
  1931. SCM
  1932. scm_integer_logand_zz (struct scm_bignum *x, struct scm_bignum *y)
  1933. {
  1934. mpz_t result, zx, zy;
  1935. mpz_init (result);
  1936. alias_bignum_to_mpz (x, zx);
  1937. alias_bignum_to_mpz (y, zy);
  1938. mpz_and (result, zx, zy);
  1939. scm_remember_upto_here_2 (x, y);
  1940. return take_mpz (result);
  1941. }
  1942. SCM
  1943. scm_integer_logior_ii (intptr_t x, intptr_t y)
  1944. {
  1945. return SCM_I_MAKINUM (x | y);
  1946. }
  1947. SCM
  1948. scm_integer_logior_zi (struct scm_bignum *x, intptr_t y)
  1949. {
  1950. if (y == 0)
  1951. return scm_from_bignum (x);
  1952. mpz_t result, zx, zy;
  1953. mpz_init (result);
  1954. alias_bignum_to_mpz (x, zx);
  1955. mpz_init_set_si (zy, y);
  1956. mpz_ior (result, zy, zx);
  1957. scm_remember_upto_here_1 (x);
  1958. mpz_clear (zy);
  1959. return take_mpz (result);
  1960. }
  1961. SCM
  1962. scm_integer_logior_zz (struct scm_bignum *x, struct scm_bignum *y)
  1963. {
  1964. mpz_t result, zx, zy;
  1965. mpz_init (result);
  1966. alias_bignum_to_mpz (x, zx);
  1967. alias_bignum_to_mpz (y, zy);
  1968. mpz_ior (result, zy, zx);
  1969. scm_remember_upto_here_2 (x, y);
  1970. return take_mpz (result);
  1971. }
  1972. SCM
  1973. scm_integer_logxor_ii (intptr_t x, intptr_t y)
  1974. {
  1975. return SCM_I_MAKINUM (x ^ y);
  1976. }
  1977. SCM
  1978. scm_integer_logxor_zi (struct scm_bignum *x, intptr_t y)
  1979. {
  1980. mpz_t result, zx, zy;
  1981. mpz_init (result);
  1982. alias_bignum_to_mpz (x, zx);
  1983. mpz_init_set_si (zy, y);
  1984. mpz_xor (result, zy, zx);
  1985. scm_remember_upto_here_1 (x);
  1986. mpz_clear (zy);
  1987. return take_mpz (result);
  1988. }
  1989. SCM
  1990. scm_integer_logxor_zz (struct scm_bignum *x, struct scm_bignum *y)
  1991. {
  1992. mpz_t result, zx, zy;
  1993. mpz_init (result);
  1994. alias_bignum_to_mpz (x, zx);
  1995. alias_bignum_to_mpz (y, zy);
  1996. mpz_xor (result, zy, zx);
  1997. scm_remember_upto_here_2 (x, y);
  1998. return take_mpz (result);
  1999. }
  2000. int
  2001. scm_integer_logtest_ii (intptr_t x, intptr_t y)
  2002. {
  2003. return (x & y) ? 1 : 0;
  2004. }
  2005. int
  2006. scm_integer_logtest_zi (struct scm_bignum *x, intptr_t y)
  2007. {
  2008. return scm_is_eq (scm_integer_logand_zi (x, y), SCM_INUM0);
  2009. }
  2010. int
  2011. scm_integer_logtest_zz (struct scm_bignum *x, struct scm_bignum *y)
  2012. {
  2013. return scm_is_eq (scm_integer_logand_zz (x, y), SCM_INUM0);
  2014. }
  2015. int
  2016. scm_integer_logbit_ui (uintptr_t index, intptr_t n)
  2017. {
  2018. if (index < SCM_INTPTR_T_BIT)
  2019. /* Assume two's complement representation. */
  2020. return (n >> index) & 1;
  2021. else
  2022. return n < 0;
  2023. }
  2024. int
  2025. scm_integer_logbit_uz (uintptr_t index, struct scm_bignum *n)
  2026. {
  2027. mpz_t zn;
  2028. alias_bignum_to_mpz (n, zn);
  2029. int val = mpz_tstbit (zn, index);
  2030. scm_remember_upto_here_1 (n);
  2031. return val;
  2032. }
  2033. SCM
  2034. scm_integer_lognot_i (intptr_t n)
  2035. {
  2036. return SCM_I_MAKINUM (~n);
  2037. }
  2038. SCM
  2039. scm_integer_lognot_z (struct scm_bignum *n)
  2040. {
  2041. mpz_t result, zn;
  2042. mpz_init (result);
  2043. alias_bignum_to_mpz (n, zn);
  2044. mpz_com (result, zn);
  2045. scm_remember_upto_here_1 (n);
  2046. return take_mpz (result);
  2047. }
  2048. SCM
  2049. scm_integer_expt_ii (intptr_t n, intptr_t k)
  2050. {
  2051. ASSERT (k >= 0);
  2052. if (k == 0)
  2053. return SCM_INUM1;
  2054. if (k == 1)
  2055. return SCM_I_MAKINUM (n);
  2056. if (n == -1)
  2057. return scm_is_integer_odd_i (k) ? SCM_I_MAKINUM (-1) : SCM_INUM1;
  2058. if (n == 2)
  2059. {
  2060. if (k < SCM_I_FIXNUM_BIT - 1)
  2061. return SCM_I_MAKINUM (L1 << k);
  2062. if (k < 64)
  2063. return scm_integer_from_uint64 (((uint64_t) 1) << k);
  2064. size_t nlimbs = k / (sizeof (mp_limb_t)*8) + 1;
  2065. size_t high_shift = k & (sizeof (mp_limb_t)*8 - 1);
  2066. struct scm_bignum *result = allocate_bignum (nlimbs);
  2067. mp_limb_t *rd = bignum_limbs (result);
  2068. mpn_zero(rd, nlimbs - 1);
  2069. rd[nlimbs - 1] = ((mp_limb_t) 1) << high_shift;
  2070. return scm_from_bignum (result);
  2071. }
  2072. mpz_t res;
  2073. mpz_init (res);
  2074. mpz_ui_pow_ui (res, inum_magnitude (n), k);
  2075. if (n < 0 && (k & 1))
  2076. mpz_neg (res, res);
  2077. return take_mpz (res);
  2078. }
  2079. SCM
  2080. scm_integer_expt_zi (struct scm_bignum *n, intptr_t k)
  2081. {
  2082. ASSERT (k >= 0);
  2083. mpz_t res, zn;
  2084. mpz_init (res);
  2085. alias_bignum_to_mpz (n, zn);
  2086. mpz_pow_ui (res, zn, k);
  2087. scm_remember_upto_here_1 (n);
  2088. return take_mpz (res);
  2089. }
  2090. static void
  2091. integer_init_mpz (mpz_ptr z, SCM n)
  2092. {
  2093. if (SCM_I_INUMP (n))
  2094. mpz_init_set_si (z, SCM_I_INUM (n));
  2095. else
  2096. {
  2097. ASSERT (SCM_BIGP (n));
  2098. mpz_t zn;
  2099. alias_bignum_to_mpz (scm_bignum (n), zn);
  2100. mpz_init_set (z, zn);
  2101. scm_remember_upto_here_1 (n);
  2102. }
  2103. }
  2104. SCM
  2105. scm_integer_modulo_expt_nnn (SCM n, SCM k, SCM m)
  2106. {
  2107. if (scm_is_eq (m, SCM_INUM0))
  2108. scm_num_overflow ("modulo-expt");
  2109. mpz_t n_tmp, k_tmp, m_tmp;
  2110. integer_init_mpz (n_tmp, n);
  2111. integer_init_mpz (k_tmp, k);
  2112. integer_init_mpz (m_tmp, m);
  2113. /* if the exponent K is negative, and we simply call mpz_powm, we
  2114. will get a divide-by-zero exception when an inverse 1/n mod m
  2115. doesn't exist (or is not unique). Since exceptions are hard to
  2116. handle, we'll attempt the inversion "by hand" -- that way, we get
  2117. a simple failure code, which is easy to handle. */
  2118. if (-1 == mpz_sgn (k_tmp))
  2119. {
  2120. if (!mpz_invert (n_tmp, n_tmp, m_tmp))
  2121. {
  2122. mpz_clear (n_tmp);
  2123. mpz_clear (k_tmp);
  2124. mpz_clear (m_tmp);
  2125. scm_num_overflow ("modulo-expt");
  2126. }
  2127. mpz_neg (k_tmp, k_tmp);
  2128. }
  2129. mpz_powm (n_tmp, n_tmp, k_tmp, m_tmp);
  2130. if (mpz_sgn (m_tmp) < 0 && mpz_sgn (n_tmp) != 0)
  2131. mpz_add (n_tmp, n_tmp, m_tmp);
  2132. mpz_clear (m_tmp);
  2133. mpz_clear (k_tmp);
  2134. return take_mpz (n_tmp);
  2135. }
  2136. /* Efficiently compute (N * 2^COUNT), where N is an exact integer, and
  2137. COUNT > 0. */
  2138. SCM
  2139. scm_integer_lsh_iu (intptr_t n, uintptr_t count)
  2140. {
  2141. ASSERT (count > 0);
  2142. /* Left shift of count >= SCM_I_FIXNUM_BIT-1 will almost[*] always
  2143. overflow a non-zero fixnum. For smaller shifts we check the
  2144. bits going into positions above SCM_I_FIXNUM_BIT-1. If they're
  2145. all 0s for nn>=0, or all 1s for nn<0 then there's no overflow.
  2146. Those bits are "nn >> (SCM_I_FIXNUM_BIT-1 - count)".
  2147. [*] There's one exception:
  2148. (-1) << SCM_I_FIXNUM_BIT-1 == SCM_MOST_NEGATIVE_FIXNUM */
  2149. if (n == 0)
  2150. return SCM_I_MAKINUM (n);
  2151. else if (count < SCM_I_FIXNUM_BIT-1 &&
  2152. ((scm_t_bits) (SCM_SRS (n, (SCM_I_FIXNUM_BIT-1 - count)) + 1)
  2153. <= 1))
  2154. return SCM_I_MAKINUM (n < 0 ? -(-n << count) : (n << count));
  2155. else
  2156. {
  2157. mpz_t result;
  2158. mpz_init_set_si (result, n);
  2159. mpz_mul_2exp (result, result, count);
  2160. return take_mpz (result);
  2161. }
  2162. }
  2163. SCM
  2164. scm_integer_lsh_zu (struct scm_bignum *n, uintptr_t count)
  2165. {
  2166. ASSERT (count > 0);
  2167. mpz_t result, zn;
  2168. mpz_init (result);
  2169. alias_bignum_to_mpz (n, zn);
  2170. mpz_mul_2exp (result, zn, count);
  2171. scm_remember_upto_here_1 (n);
  2172. return take_mpz (result);
  2173. }
  2174. /* Efficiently compute floor (N / 2^COUNT), where N is an exact integer
  2175. and COUNT > 0. */
  2176. SCM
  2177. scm_integer_floor_rsh_iu (intptr_t n, uintptr_t count)
  2178. {
  2179. ASSERT (count > 0);
  2180. if (count >= SCM_I_FIXNUM_BIT)
  2181. return (n >= 0 ? SCM_INUM0 : SCM_I_MAKINUM (-1));
  2182. else
  2183. return SCM_I_MAKINUM (SCM_SRS (n, count));
  2184. }
  2185. SCM
  2186. scm_integer_floor_rsh_zu (struct scm_bignum *n, uintptr_t count)
  2187. {
  2188. ASSERT (count > 0);
  2189. mpz_t result, zn;
  2190. mpz_init (result);
  2191. alias_bignum_to_mpz (n, zn);
  2192. mpz_fdiv_q_2exp (result, zn, count);
  2193. scm_remember_upto_here_1 (n);
  2194. return take_mpz (result);
  2195. }
  2196. /* Efficiently compute round (N / 2^COUNT), where N is an exact integer
  2197. and COUNT > 0. */
  2198. SCM
  2199. scm_integer_round_rsh_iu (intptr_t n, uintptr_t count)
  2200. {
  2201. ASSERT (count > 0);
  2202. if (count >= SCM_I_FIXNUM_BIT)
  2203. return SCM_INUM0;
  2204. else
  2205. {
  2206. intptr_t q = SCM_SRS (n, count);
  2207. if (0 == (n & (L1 << (count-1))))
  2208. return SCM_I_MAKINUM (q); /* round down */
  2209. else if (n & ((L1 << (count-1)) - 1))
  2210. return SCM_I_MAKINUM (q + 1); /* round up */
  2211. else
  2212. return SCM_I_MAKINUM ((~L1) & (q + 1)); /* round to even */
  2213. }
  2214. }
  2215. SCM
  2216. scm_integer_round_rsh_zu (struct scm_bignum *n, uintptr_t count)
  2217. {
  2218. ASSERT (count > 0);
  2219. mpz_t q, zn;
  2220. mpz_init (q);
  2221. alias_bignum_to_mpz (n, zn);
  2222. mpz_fdiv_q_2exp (q, zn, count);
  2223. if (mpz_tstbit (zn, count-1)
  2224. && (mpz_odd_p (q) || mpz_scan1 (zn, 0) < count-1))
  2225. mpz_add_ui (q, q, 1);
  2226. scm_remember_upto_here_1 (n);
  2227. return take_mpz (q);
  2228. }
  2229. #define MIN(A, B) ((A) <= (B) ? (A) : (B))
  2230. SCM
  2231. scm_integer_bit_extract_i (intptr_t n, uintptr_t start,
  2232. uintptr_t bits)
  2233. {
  2234. /* When istart>=SCM_I_FIXNUM_BIT we can just limit the shift to
  2235. SCM_I_FIXNUM_BIT-1 to get either 0 or -1 per the sign of "n". */
  2236. n = SCM_SRS (n, MIN (start, SCM_I_FIXNUM_BIT-1));
  2237. if (n < 0 && bits >= SCM_I_FIXNUM_BIT)
  2238. {
  2239. /* Since we emulate two's complement encoded numbers, this special
  2240. case requires us to produce a result that has more bits than
  2241. can be stored in a fixnum. */
  2242. mpz_t result;
  2243. mpz_init_set_si (result, n);
  2244. mpz_fdiv_r_2exp (result, result, bits);
  2245. return take_mpz (result);
  2246. }
  2247. /* mask down to requisite bits */
  2248. bits = MIN (bits, SCM_I_FIXNUM_BIT);
  2249. return SCM_I_MAKINUM (n & ((L1 << bits) - 1));
  2250. }
  2251. SCM
  2252. scm_integer_bit_extract_z (struct scm_bignum *n, uintptr_t start, uintptr_t bits)
  2253. {
  2254. mpz_t zn;
  2255. alias_bignum_to_mpz (n, zn);
  2256. if (bits == 1)
  2257. {
  2258. int bit = mpz_tstbit (zn, start);
  2259. scm_remember_upto_here_1 (n);
  2260. return SCM_I_MAKINUM (bit);
  2261. }
  2262. /* ENHANCE-ME: It'd be nice not to allocate a new bignum when
  2263. bits<SCM_I_FIXNUM_BIT. Would want some help from GMP to get
  2264. such bits into a ulong. */
  2265. mpz_t result;
  2266. mpz_init (result);
  2267. mpz_fdiv_q_2exp (result, zn, start);
  2268. mpz_fdiv_r_2exp (result, result, bits);
  2269. scm_remember_upto_here_1 (n);
  2270. return take_mpz (result);
  2271. }
  2272. static const char scm_logtab[] = {
  2273. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
  2274. };
  2275. SCM
  2276. scm_integer_logcount_i (intptr_t n)
  2277. {
  2278. uintptr_t c = 0;
  2279. if (n < 0)
  2280. n = -1 - n;
  2281. while (n)
  2282. {
  2283. c += scm_logtab[15 & n];
  2284. n >>= 4;
  2285. }
  2286. return SCM_I_MAKINUM (c);
  2287. }
  2288. SCM
  2289. scm_integer_logcount_z (struct scm_bignum *n)
  2290. {
  2291. uintptr_t count;
  2292. mpz_t zn;
  2293. alias_bignum_to_mpz (n, zn);
  2294. if (mpz_sgn (zn) >= 0)
  2295. count = mpz_popcount (zn);
  2296. else
  2297. {
  2298. mpz_t z_negative_one;
  2299. mpz_init_set_si (z_negative_one, -1);
  2300. count = mpz_hamdist (zn, z_negative_one);
  2301. mpz_clear (z_negative_one);
  2302. }
  2303. scm_remember_upto_here_1 (n);
  2304. return scm_from_uintptr_t (count);
  2305. }
  2306. static const char scm_ilentab[] = {
  2307. 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4
  2308. };
  2309. SCM
  2310. scm_integer_length_i (intptr_t n)
  2311. {
  2312. uintptr_t c = 0;
  2313. unsigned int l = 4;
  2314. if (n < 0)
  2315. n = -1 - n;
  2316. while (n)
  2317. {
  2318. c += 4;
  2319. l = scm_ilentab [15 & n];
  2320. n >>= 4;
  2321. }
  2322. return SCM_I_MAKINUM (c - 4 + l);
  2323. }
  2324. SCM
  2325. scm_integer_length_z (struct scm_bignum *n)
  2326. {
  2327. /* mpz_sizeinbase looks at the absolute value of negatives, whereas we
  2328. want a ones-complement. If n is ...111100..00 then mpz_sizeinbase is
  2329. 1 too big, so check for that and adjust. */
  2330. mpz_t zn;
  2331. alias_bignum_to_mpz (n, zn);
  2332. size_t size = mpz_sizeinbase (zn, 2);
  2333. const mp_bitcnt_t bitcnt_max = (mp_bitcnt_t) ~ (mp_bitcnt_t) 0;
  2334. /* If negative and no 0 bits above the lowest 1, adjust result. */
  2335. if (mpz_sgn (zn) < 0 && mpz_scan0 (zn, mpz_scan1 (zn, 0)) == bitcnt_max)
  2336. size--;
  2337. scm_remember_upto_here_1 (n);
  2338. return scm_from_size_t (size);
  2339. }
  2340. SCM
  2341. scm_integer_to_string_i (intptr_t n, int base)
  2342. {
  2343. // FIXME: Use mpn_get_str instead.
  2344. char num_buf [SCM_INTBUFLEN];
  2345. size_t length = scm_iint2str (n, base, num_buf);
  2346. return scm_from_latin1_stringn (num_buf, length);
  2347. }
  2348. SCM
  2349. scm_integer_to_string_z (struct scm_bignum *n, int base)
  2350. {
  2351. mpz_t zn;
  2352. alias_bignum_to_mpz (n, zn);
  2353. char *str = mpz_get_str (NULL, base, zn);
  2354. scm_remember_upto_here_1 (n);
  2355. size_t len = strlen (str);
  2356. void (*freefunc) (void *, size_t);
  2357. mp_get_memory_functions (NULL, NULL, &freefunc);
  2358. SCM ret = scm_from_latin1_stringn (str, len);
  2359. freefunc (str, len + 1);
  2360. return ret;
  2361. }
  2362. int
  2363. scm_is_integer_equal_ir (intptr_t x, double y)
  2364. {
  2365. /* On a 32-bit system an inum fits a double, we can cast the inum
  2366. to a double and compare.
  2367. But on a 64-bit system an inum is bigger than a double and casting
  2368. it to a double (call that dx) will round. Although dxx will not in
  2369. general be equal to x, dx will always be an integer and within a
  2370. factor of 2 of x, so if dx==y, we know that y is an integer and
  2371. fits in scm_t_signed_bits. So we cast y to scm_t_signed_bits and
  2372. compare with plain x.
  2373. An alternative (for any size system actually) would be to check y
  2374. is an integer (with floor) and is in range of an inum (compare
  2375. against appropriate powers of 2) then test x==(intptr_t)y. It's
  2376. just a matter of which casts/comparisons might be fastest or
  2377. easiest for the cpu. */
  2378. return (double) x == y
  2379. && (DBL_MANT_DIG >= SCM_I_FIXNUM_BIT-1 || x == (intptr_t) y);
  2380. }
  2381. int
  2382. scm_is_integer_equal_ic (intptr_t x, double real, double imag)
  2383. {
  2384. return imag == 0.0 && scm_is_integer_equal_ir (x, real);
  2385. }
  2386. int
  2387. scm_is_integer_equal_zz (struct scm_bignum *x, struct scm_bignum *y)
  2388. {
  2389. mpz_t zx, zy;
  2390. alias_bignum_to_mpz (x, zx);
  2391. alias_bignum_to_mpz (y, zy);
  2392. int cmp = mpz_cmp (zx, zy);
  2393. scm_remember_upto_here_2 (x, y);
  2394. return 0 == cmp;
  2395. }
  2396. int
  2397. scm_is_integer_equal_zr (struct scm_bignum *x, double y)
  2398. {
  2399. if (isnan (y))
  2400. return 0;
  2401. mpz_t zx;
  2402. alias_bignum_to_mpz (x, zx);
  2403. int cmp = mpz_cmp_d (zx, y);
  2404. scm_remember_upto_here_1 (x);
  2405. return 0 == cmp;
  2406. }
  2407. int
  2408. scm_is_integer_equal_zc (struct scm_bignum *x, double real, double imag)
  2409. {
  2410. return imag == 0.0 && scm_is_integer_equal_zr (x, real);
  2411. }
  2412. int
  2413. scm_is_integer_less_than_ir (intptr_t x, double y)
  2414. {
  2415. /* We can safely take the ceiling of y without changing the
  2416. result of x<y, given that x is an integer. */
  2417. y = ceil (y);
  2418. /* In the following comparisons, it's important that the right
  2419. hand side always be a power of 2, so that it can be
  2420. losslessly converted to a double even on 64-bit
  2421. machines. */
  2422. if (y >= (double) (SCM_MOST_POSITIVE_FIXNUM+1))
  2423. return 1;
  2424. else if (!(y > (double) SCM_MOST_NEGATIVE_FIXNUM))
  2425. /* The condition above is carefully written to include the
  2426. case where y==NaN. */
  2427. return 0;
  2428. else
  2429. /* y is a finite integer that fits in an inum. */
  2430. return x < (intptr_t) y;
  2431. }
  2432. int
  2433. scm_is_integer_less_than_ri (double x, intptr_t y)
  2434. {
  2435. /* We can safely take the floor of x without changing the
  2436. result of x<y, given that y is an integer. */
  2437. x = floor (x);
  2438. /* In the following comparisons, it's important that the right
  2439. hand side always be a power of 2, so that it can be
  2440. losslessly converted to a double even on 64-bit
  2441. machines. */
  2442. if (x < (double) SCM_MOST_NEGATIVE_FIXNUM)
  2443. return 1;
  2444. else if (!(x < (double) (SCM_MOST_POSITIVE_FIXNUM+1)))
  2445. /* The condition above is carefully written to include the
  2446. case where x==NaN. */
  2447. return 0;
  2448. else
  2449. /* x is a finite integer that fits in an inum. */
  2450. return (intptr_t) x < y;
  2451. }
  2452. int
  2453. scm_is_integer_less_than_zz (struct scm_bignum *x, struct scm_bignum *y)
  2454. {
  2455. mpz_t zx, zy;
  2456. alias_bignum_to_mpz (x, zx);
  2457. alias_bignum_to_mpz (y, zy);
  2458. int cmp = mpz_cmp (zx, zy);
  2459. scm_remember_upto_here_2 (x, y);
  2460. return cmp < 0;
  2461. }
  2462. int
  2463. scm_is_integer_less_than_zr (struct scm_bignum *x, double y)
  2464. {
  2465. if (isnan (y))
  2466. return 0;
  2467. mpz_t zx;
  2468. alias_bignum_to_mpz (x, zx);
  2469. int cmp = mpz_cmp_d (zx, y);
  2470. scm_remember_upto_here_1 (x);
  2471. return cmp < 0;
  2472. }
  2473. int
  2474. scm_is_integer_less_than_rz (double x, struct scm_bignum *y)
  2475. {
  2476. if (isnan (x))
  2477. return 0;
  2478. mpz_t zy;
  2479. alias_bignum_to_mpz (y, zy);
  2480. int cmp = mpz_cmp_d (zy, x);
  2481. scm_remember_upto_here_1 (y);
  2482. return cmp > 0;
  2483. }
  2484. int
  2485. scm_is_integer_positive_z (struct scm_bignum *x)
  2486. {
  2487. return bignum_is_positive (x);
  2488. }
  2489. int
  2490. scm_is_integer_negative_z (struct scm_bignum *x)
  2491. {
  2492. return bignum_is_negative (x);
  2493. }
  2494. #if SCM_ENABLE_MINI_GMP
  2495. static double
  2496. mpz_get_d_2exp (intptr_t *exp, mpz_srcptr z)
  2497. {
  2498. double signif = mpz_get_d (z);
  2499. int iexp;
  2500. signif = frexp (signif, &iexp);
  2501. *exp = iexp;
  2502. return signif;
  2503. }
  2504. #endif
  2505. double
  2506. scm_integer_frexp_z (struct scm_bignum *x, intptr_t *exp)
  2507. {
  2508. mpz_t zx;
  2509. alias_bignum_to_mpz (x, zx);
  2510. size_t bits = mpz_sizeinbase (zx, 2);
  2511. ASSERT (bits != 0);
  2512. size_t shift = 0;
  2513. if (bits > DBL_MANT_DIG)
  2514. {
  2515. shift = bits - DBL_MANT_DIG;
  2516. SCM xx = scm_integer_round_rsh_zu (x, shift);
  2517. if (SCM_I_INUMP (xx))
  2518. {
  2519. int expon;
  2520. double signif = frexp (SCM_I_INUM (xx), &expon);
  2521. *exp = expon + shift;
  2522. return signif;
  2523. }
  2524. x = scm_bignum (xx);
  2525. alias_bignum_to_mpz (x, zx);
  2526. }
  2527. double significand = mpz_get_d_2exp (exp, zx);
  2528. scm_remember_upto_here_1 (x);
  2529. *exp += shift;
  2530. return significand;
  2531. }
  2532. double
  2533. scm_integer_to_double_z (struct scm_bignum *x)
  2534. {
  2535. intptr_t exponent;
  2536. double significand = scm_integer_frexp_z (x, &exponent);
  2537. return ldexp (significand, exponent);
  2538. }
  2539. SCM
  2540. scm_integer_from_double (double val)
  2541. {
  2542. if (!isfinite (val))
  2543. scm_out_of_range ("inexact->exact", scm_from_double (val));
  2544. if (((double) INT64_MIN) <= val && val <= ((double) INT64_MAX))
  2545. return scm_from_int64 (val);
  2546. mpz_t result;
  2547. mpz_init_set_d (result, val);
  2548. return take_mpz (result);
  2549. }
  2550. SCM
  2551. scm_integer_add_ii (intptr_t x, intptr_t y)
  2552. {
  2553. return intptr_t_to_scm (x + y);
  2554. }
  2555. static SCM
  2556. do_add_1 (int negative, mp_limb_t *xd, size_t xn, mp_limb_t y)
  2557. {
  2558. size_t rn = xn + 1;
  2559. struct scm_bignum *result = allocate_bignum (rn);
  2560. mp_limb_t *rd = bignum_limbs (result);
  2561. if (mpn_add_1 (rd, xd, xn, y))
  2562. rd[xn] = 1;
  2563. else
  2564. result->u.z.size--;
  2565. // No need to normalize as magnitude is increasing and one operand
  2566. // already a bignum.
  2567. return scm_from_bignum (bignum_negate_if (negative, result));
  2568. }
  2569. static SCM
  2570. do_add (int negative, mp_limb_t *xd, size_t xn, mp_limb_t *yd, size_t yn)
  2571. {
  2572. size_t rn = xn + 1;
  2573. struct scm_bignum *result = allocate_bignum (rn);
  2574. mp_limb_t *rd = bignum_limbs (result);
  2575. if (mpn_add (rd, xd, xn, yd, yn))
  2576. rd[xn] = 1;
  2577. else
  2578. result->u.z.size--;
  2579. // No need to normalize as magnitude is increasing and one operand
  2580. // already a bignum.
  2581. return scm_from_bignum (bignum_negate_if (negative, result));
  2582. }
  2583. static SCM
  2584. do_sub_1 (int negative, mp_limb_t *xd, size_t xn, mp_limb_t y)
  2585. {
  2586. size_t rn = xn;
  2587. struct scm_bignum *result = allocate_bignum (rn);
  2588. mp_limb_t *rd = bignum_limbs (result);
  2589. mpn_sub_1 (rd, xd, xn, y);
  2590. return normalize_bignum
  2591. (bignum_negate_if (negative, (bignum_trim1 (result))));
  2592. }
  2593. static SCM
  2594. do_sub (int negative, mp_limb_t *xd, size_t xn, mp_limb_t *yd, size_t yn)
  2595. {
  2596. size_t rn = xn;
  2597. struct scm_bignum *result = allocate_bignum (rn);
  2598. mp_limb_t *rd = bignum_limbs (result);
  2599. mpn_sub (rd, xd, xn, yd, yn);
  2600. return normalize_bignum
  2601. (bignum_negate_if (negative, (bignum_trimn (result))));
  2602. }
  2603. static int
  2604. do_cmp (mp_limb_t *xd, size_t xn, mp_limb_t *yd, size_t yn)
  2605. {
  2606. if (xn < yn)
  2607. return -1;
  2608. if (xn > yn)
  2609. return 1;
  2610. return mpn_cmp (xd, yd, xn);
  2611. }
  2612. SCM
  2613. scm_integer_add_zi (struct scm_bignum *x, intptr_t y)
  2614. {
  2615. if (y == 0)
  2616. return scm_from_bignum (x);
  2617. size_t xn = bignum_limb_count (x);
  2618. if (xn == 0)
  2619. return SCM_I_MAKINUM (y);
  2620. SCM ret;
  2621. if (bignum_is_negative (x) == (y < 0))
  2622. // Magnitude increases, sign stays the same.
  2623. ret = do_add_1 (y < 0, bignum_limbs (x), xn, inum_magnitude (y));
  2624. else
  2625. // Magnitude decreases, but assuming x's magnitude is greater than
  2626. // y's, not changing sign.
  2627. ret = do_sub_1 (bignum_is_negative (x), bignum_limbs (x), xn,
  2628. inum_magnitude (y));
  2629. scm_remember_upto_here_1 (x);
  2630. return ret;
  2631. }
  2632. SCM
  2633. scm_integer_add_zz (struct scm_bignum *x, struct scm_bignum *y)
  2634. {
  2635. size_t xn = bignum_limb_count (x);
  2636. size_t yn = bignum_limb_count (y);
  2637. if (xn == 0)
  2638. return normalize_bignum (y);
  2639. if (yn == 0)
  2640. return normalize_bignum (x);
  2641. mp_limb_t *xd = bignum_limbs (x);
  2642. mp_limb_t *yd = bignum_limbs (y);
  2643. SCM ret;
  2644. if (bignum_is_negative (x) == bignum_is_negative (y))
  2645. // Magnitude increases, sign stays the same.
  2646. ret = xn < yn
  2647. ? do_add (bignum_is_negative (x), yd, yn, xd, xn)
  2648. : do_add (bignum_is_negative (x), xd, xn, yd, yn);
  2649. else
  2650. // Magnitude decreases, changing sign if abs(x) < abs(y).
  2651. ret = do_cmp (xd, xn, yd, yn) < 0
  2652. ? do_sub (!bignum_is_negative (x), yd, yn, xd, xn)
  2653. : do_sub (bignum_is_negative (x), xd, xn, yd, yn);
  2654. scm_remember_upto_here_2 (x, y);
  2655. return ret;
  2656. }
  2657. SCM
  2658. scm_integer_negate_i (intptr_t x)
  2659. {
  2660. return intptr_t_to_scm (-x);
  2661. }
  2662. SCM
  2663. scm_integer_negate_z (struct scm_bignum *x)
  2664. {
  2665. /* Must normalize here because -SCM_MOST_NEGATIVE_FIXNUM is a bignum,
  2666. but negating that gives a fixnum. */
  2667. return normalize_bignum (negate_bignum (clone_bignum (x)));
  2668. }
  2669. SCM
  2670. scm_integer_sub_ii (intptr_t x, intptr_t y)
  2671. {
  2672. // Assumes that -INUM_MIN can fit in a intptr_t, even if that
  2673. // intptr_t is not fixable, and that scm_integer_add_ii can handle
  2674. // intptr_t inputs outside the fixable range.
  2675. return scm_integer_add_ii (x, -y);
  2676. }
  2677. SCM
  2678. scm_integer_sub_iz (intptr_t x, struct scm_bignum *y)
  2679. {
  2680. if (x == 0)
  2681. return scm_integer_negate_z (y);
  2682. size_t yn = bignum_limb_count (y);
  2683. if (yn == 0)
  2684. return SCM_I_MAKINUM (x);
  2685. SCM ret;
  2686. if (bignum_is_negative (y) == (x < 0))
  2687. // Magnitude of result smaller than that of y, but assuming y's
  2688. // magnitude is greater than x's, keeping y's sign.
  2689. ret = do_sub_1 (x > 0, bignum_limbs (y), yn, inum_magnitude (x));
  2690. else
  2691. // Magnitude increases, same sign as x.
  2692. ret = do_add_1 (x < 0, bignum_limbs (y), yn, inum_magnitude (x));
  2693. scm_remember_upto_here_1 (y);
  2694. return ret;
  2695. }
  2696. SCM
  2697. scm_integer_sub_zi (struct scm_bignum *x, intptr_t y)
  2698. {
  2699. if (y == 0)
  2700. return scm_from_bignum (x);
  2701. size_t xn = bignum_limb_count (x);
  2702. if (xn == 0)
  2703. return SCM_I_MAKINUM (y);
  2704. SCM ret;
  2705. if (bignum_is_negative (x) == (y < 0))
  2706. // Magnitude decreases, but assuming x's magnitude is greater than
  2707. // y's, not changing sign.
  2708. ret = do_sub_1 (y < 0, bignum_limbs (x), xn, inum_magnitude (y));
  2709. else
  2710. // Magnitude increases, same sign as x.
  2711. ret = do_add_1 (bignum_is_negative (x), bignum_limbs (x), xn,
  2712. inum_magnitude (y));
  2713. scm_remember_upto_here_1 (x);
  2714. return ret;
  2715. }
  2716. SCM
  2717. scm_integer_sub_zz (struct scm_bignum *x, struct scm_bignum *y)
  2718. {
  2719. size_t xn = bignum_limb_count (x);
  2720. size_t yn = bignum_limb_count (y);
  2721. if (xn == 0)
  2722. return scm_integer_negate_z (y);
  2723. if (yn == 0)
  2724. return scm_from_bignum (x);
  2725. mp_limb_t *xd = bignum_limbs (x);
  2726. mp_limb_t *yd = bignum_limbs (y);
  2727. SCM ret;
  2728. if (bignum_is_negative (x) != bignum_is_negative (y))
  2729. // Magnitude increases, same sign as x.
  2730. ret = xn < yn
  2731. ? do_add (bignum_is_negative (x), yd, yn, xd, xn)
  2732. : do_add (bignum_is_negative (x), xd, xn, yd, yn);
  2733. else
  2734. // Magnitude decreases, changing sign if abs(x) < abs(y).
  2735. ret = do_cmp (xd, xn, yd, yn) < 0
  2736. ? do_sub (!bignum_is_negative (x), yd, yn, xd, xn)
  2737. : do_sub (bignum_is_negative (x), xd, xn, yd, yn);
  2738. scm_remember_upto_here_2 (x, y);
  2739. return ret;
  2740. }
  2741. SCM
  2742. scm_integer_mul_ii (intptr_t x, intptr_t y)
  2743. {
  2744. #if SCM_I_FIXNUM_BIT < 32
  2745. int64_t k = x * (int64_t) y;
  2746. if (SCM_FIXABLE (k))
  2747. return SCM_I_MAKINUM (k);
  2748. #endif
  2749. mp_limb_t xd[1] = { intptr_t_magnitude (x) };
  2750. mp_limb_t lo;
  2751. int negative = (x < 0) != (y < 0);
  2752. mp_limb_t hi = mpn_mul_1 (&lo, xd, 1, intptr_t_magnitude (y));
  2753. if (!hi)
  2754. {
  2755. if (negative)
  2756. {
  2757. if (lo <= intptr_t_magnitude (SCM_MOST_NEGATIVE_FIXNUM))
  2758. return SCM_I_MAKINUM (negative_intptr_t (lo));
  2759. }
  2760. else if (lo <= SCM_MOST_POSITIVE_FIXNUM)
  2761. return SCM_I_MAKINUM (lo);
  2762. return scm_from_bignum (make_bignum_1 (negative, lo));
  2763. }
  2764. return scm_from_bignum (make_bignum_2 (negative, lo, hi));
  2765. }
  2766. SCM
  2767. scm_integer_mul_zi (struct scm_bignum *x, intptr_t y)
  2768. {
  2769. switch (y)
  2770. {
  2771. case -1:
  2772. return scm_integer_negate_z (x);
  2773. case 0:
  2774. return SCM_INUM0;
  2775. case 1:
  2776. return scm_from_bignum (x);
  2777. default:
  2778. {
  2779. size_t xn = bignum_limb_count (x);
  2780. if (xn == 0)
  2781. return SCM_INUM0;
  2782. struct scm_bignum *result = allocate_bignum (xn + 1);
  2783. mp_limb_t *rd = bignum_limbs (result);
  2784. const mp_limb_t *xd = bignum_limbs (x);
  2785. mp_limb_t yd = intptr_t_magnitude (y);
  2786. int negate = bignum_is_negative (x) != (y < 0);
  2787. mp_limb_t hi = mpn_mul_1 (rd, xd, xn, yd);
  2788. if (hi)
  2789. rd[xn] = hi;
  2790. else
  2791. result->u.z.size--;
  2792. scm_remember_upto_here_1 (x);
  2793. return normalize_bignum (bignum_negate_if (negate, (result)));
  2794. }
  2795. }
  2796. }
  2797. SCM
  2798. scm_integer_mul_zz (struct scm_bignum *x, struct scm_bignum *y)
  2799. {
  2800. size_t xn = bignum_limb_count (x);
  2801. size_t yn = bignum_limb_count (y);
  2802. if (xn == 0 || yn == 0)
  2803. return SCM_INUM0;
  2804. struct scm_bignum *result = allocate_bignum (xn + yn);
  2805. mp_limb_t *rd = bignum_limbs (result);
  2806. const mp_limb_t *xd = bignum_limbs (x);
  2807. const mp_limb_t *yd = bignum_limbs (y);
  2808. int negate = bignum_is_negative (x) != bignum_is_negative (y);
  2809. if (xd == yd)
  2810. mpn_sqr (rd, xd, xn);
  2811. else if (xn <= yn)
  2812. mpn_mul (rd, yd, yn, xd, xn);
  2813. else
  2814. mpn_mul (rd, xd, xn, yd, yn);
  2815. scm_remember_upto_here_2 (x, y);
  2816. return normalize_bignum
  2817. (bignum_negate_if (negate, (bignum_trim1 (result))));
  2818. }
  2819. int
  2820. scm_is_integer_divisible_ii (intptr_t x, intptr_t y)
  2821. {
  2822. ASSERT (y != 0);
  2823. return (x % y) == 0;
  2824. }
  2825. int
  2826. scm_is_integer_divisible_zi (struct scm_bignum *x, intptr_t y)
  2827. {
  2828. ASSERT (y != 0);
  2829. switch (y)
  2830. {
  2831. case -1:
  2832. case 1:
  2833. return 1;
  2834. default:
  2835. {
  2836. intptr_t abs_y = y < 0 ? -y : y;
  2837. mpz_t zx;
  2838. alias_bignum_to_mpz (x, zx);
  2839. int divisible = mpz_divisible_ui_p (zx, abs_y);
  2840. scm_remember_upto_here_1 (x);
  2841. return divisible;
  2842. }
  2843. }
  2844. }
  2845. int
  2846. scm_is_integer_divisible_zz (struct scm_bignum *x, struct scm_bignum *y)
  2847. {
  2848. mpz_t zx, zy;
  2849. alias_bignum_to_mpz (x, zx);
  2850. alias_bignum_to_mpz (y, zy);
  2851. int divisible_p = mpz_divisible_p (zx, zy);
  2852. scm_remember_upto_here_2 (x, y);
  2853. return divisible_p;
  2854. }
  2855. SCM
  2856. scm_integer_exact_quotient_ii (intptr_t n, intptr_t d)
  2857. {
  2858. return scm_integer_truncate_quotient_ii (n, d);
  2859. }
  2860. SCM
  2861. scm_integer_exact_quotient_iz (intptr_t n, struct scm_bignum *d)
  2862. {
  2863. // There are only two fixnum numerators that are evenly divided by
  2864. // bignum denominators: 0, which is evenly divided 0 times by
  2865. // anything, and SCM_MOST_NEGATIVE_FIXNUM, which is evenly divided -1
  2866. // time by SCM_MOST_POSITIVE_FIXNUM+1.
  2867. if (n == 0)
  2868. return SCM_INUM0;
  2869. ASSERT (n == SCM_MOST_NEGATIVE_FIXNUM);
  2870. ASSERT (bignum_cmp_intptr_t (d, SCM_MOST_POSITIVE_FIXNUM + 1) == 0);
  2871. return SCM_I_MAKINUM (-1);
  2872. }
  2873. /* Return the exact integer q such that n = q*d, for exact integers n
  2874. and d, where d is known in advance to divide n evenly (with zero
  2875. remainder). For large integers, this can be computed more
  2876. efficiently than when the remainder is unknown. */
  2877. SCM
  2878. scm_integer_exact_quotient_zi (struct scm_bignum *n, intptr_t d)
  2879. {
  2880. if (SCM_UNLIKELY (d == 0))
  2881. scm_num_overflow ("quotient");
  2882. else if (SCM_UNLIKELY (d == 1))
  2883. return scm_from_bignum (n);
  2884. mpz_t q, zn;
  2885. mpz_init (q);
  2886. alias_bignum_to_mpz (n, zn);
  2887. if (d > 0)
  2888. mpz_divexact_ui (q, zn, d);
  2889. else
  2890. {
  2891. mpz_divexact_ui (q, zn, -d);
  2892. mpz_neg (q, q);
  2893. }
  2894. scm_remember_upto_here_1 (n);
  2895. return take_mpz (q);
  2896. }
  2897. SCM
  2898. scm_integer_exact_quotient_zz (struct scm_bignum *n, struct scm_bignum *d)
  2899. {
  2900. mpz_t q, zn, zd;
  2901. mpz_init (q);
  2902. alias_bignum_to_mpz (n, zn);
  2903. alias_bignum_to_mpz (d, zd);
  2904. mpz_divexact (q, zn, zd);
  2905. scm_remember_upto_here_2 (n, d);
  2906. return take_mpz (q);
  2907. }
  2908. #if SCM_SIZEOF_INTPTR_T == 4
  2909. SCM
  2910. scm_integer_from_int32 (int32_t n)
  2911. {
  2912. if (SCM_FIXABLE (n))
  2913. return SCM_I_MAKINUM (n);
  2914. return scm_from_bignum (intptr_t_to_bignum (n));
  2915. }
  2916. SCM
  2917. scm_integer_from_uint32 (uint32_t n)
  2918. {
  2919. if (SCM_POSFIXABLE (n))
  2920. return SCM_I_MAKINUM (n);
  2921. return scm_from_bignum (uintptr_t_to_bignum (n));
  2922. }
  2923. int
  2924. scm_integer_to_int32_z (struct scm_bignum *z, int32_t *val)
  2925. {
  2926. return bignum_to_int32 (z, val);
  2927. }
  2928. int
  2929. scm_integer_to_uint32_z (struct scm_bignum *z, uint32_t *val)
  2930. {
  2931. return bignum_to_uint32 (z, val);
  2932. }
  2933. #endif
  2934. SCM
  2935. scm_integer_from_int64 (int64_t n)
  2936. {
  2937. if (SCM_FIXABLE (n))
  2938. return SCM_I_MAKINUM (n);
  2939. return scm_from_bignum (make_bignum_from_int64 (n));
  2940. }
  2941. SCM
  2942. scm_integer_from_uint64 (uint64_t n)
  2943. {
  2944. if (SCM_POSFIXABLE (n))
  2945. return SCM_I_MAKINUM (n);
  2946. return scm_from_bignum (make_bignum_from_uint64 (n));
  2947. }
  2948. int
  2949. scm_integer_to_int64_z (struct scm_bignum *z, int64_t *val)
  2950. {
  2951. return bignum_to_int64 (z, val);
  2952. }
  2953. int
  2954. scm_integer_to_uint64_z (struct scm_bignum *z, uint64_t *val)
  2955. {
  2956. return bignum_to_uint64 (z, val);
  2957. }
  2958. void
  2959. scm_integer_set_mpz_z (struct scm_bignum *z, mpz_t n)
  2960. {
  2961. mpz_t zn;
  2962. alias_bignum_to_mpz (z, zn);
  2963. mpz_set (n, zn);
  2964. scm_remember_upto_here_1 (z);
  2965. }
  2966. void
  2967. scm_integer_init_set_mpz_z (struct scm_bignum *z, mpz_t n)
  2968. {
  2969. mpz_init (n);
  2970. scm_integer_set_mpz_z (z, n);
  2971. }
  2972. void
  2973. scm_integer_exact_sqrt_i (intptr_t k, SCM *s, SCM *r)
  2974. {
  2975. ASSERT (k >= 0);
  2976. if (k == 0)
  2977. *s = *r = SCM_INUM0;
  2978. else
  2979. {
  2980. mp_limb_t kk = k, ss, rr;
  2981. if (mpn_sqrtrem (&ss, &rr, &kk, 1) == 0)
  2982. rr = 0;
  2983. *s = SCM_I_MAKINUM (ss);
  2984. *r = SCM_I_MAKINUM (rr);
  2985. }
  2986. }
  2987. void
  2988. scm_integer_exact_sqrt_z (struct scm_bignum *k, SCM *s, SCM *r)
  2989. {
  2990. mpz_t zk, zs, zr;
  2991. alias_bignum_to_mpz (k, zk);
  2992. mpz_init (zs);
  2993. mpz_init (zr);
  2994. mpz_sqrtrem (zs, zr, zk);
  2995. scm_remember_upto_here_1 (k);
  2996. *s = take_mpz (zs);
  2997. *r = take_mpz (zr);
  2998. }
  2999. int
  3000. scm_is_integer_perfect_square_i (intptr_t k)
  3001. {
  3002. if (k < 0)
  3003. return 0;
  3004. if (k == 0)
  3005. return 1;
  3006. mp_limb_t kk = k;
  3007. return mpn_perfect_square_p (&kk, 1);
  3008. }
  3009. int
  3010. scm_is_integer_perfect_square_z (struct scm_bignum *k)
  3011. {
  3012. mpz_t zk;
  3013. alias_bignum_to_mpz (k, zk);
  3014. int result = mpz_perfect_square_p (zk);
  3015. scm_remember_upto_here_1 (k);
  3016. return result;
  3017. }
  3018. SCM
  3019. scm_integer_floor_sqrt_i (intptr_t k)
  3020. {
  3021. if (k <= 0)
  3022. return SCM_INUM0;
  3023. mp_limb_t kk = k, ss;
  3024. mpn_sqrtrem (&ss, NULL, &kk, 1);
  3025. return SCM_I_MAKINUM (ss);
  3026. }
  3027. SCM
  3028. scm_integer_floor_sqrt_z (struct scm_bignum *k)
  3029. {
  3030. mpz_t zk, zs;
  3031. alias_bignum_to_mpz (k, zk);
  3032. mpz_init (zs);
  3033. mpz_sqrt (zs, zk);
  3034. scm_remember_upto_here_1 (k);
  3035. return take_mpz (zs);
  3036. }
  3037. double
  3038. scm_integer_inexact_sqrt_i (intptr_t k)
  3039. {
  3040. if (k < 0)
  3041. return -sqrt ((double) -k);
  3042. return sqrt ((double) k);
  3043. }
  3044. double
  3045. scm_integer_inexact_sqrt_z (struct scm_bignum *k)
  3046. {
  3047. intptr_t expon;
  3048. double signif = scm_integer_frexp_z (k, &expon);
  3049. int negative = signif < 0;
  3050. if (negative)
  3051. signif = -signif;
  3052. if (expon & 1)
  3053. {
  3054. signif *= 2;
  3055. expon--;
  3056. }
  3057. double result = ldexp (sqrt (signif), expon / 2);
  3058. return negative ? -result : result;
  3059. }
  3060. SCM
  3061. scm_integer_scan1_i (intptr_t n)
  3062. {
  3063. if (n == 0)
  3064. return SCM_I_MAKINUM (-1);
  3065. n = n ^ (n-1); /* 1 bits for each low 0 and lowest 1 */
  3066. return scm_integer_logcount_i (n >> 1);
  3067. }
  3068. SCM
  3069. scm_integer_scan1_z (struct scm_bignum *n)
  3070. {
  3071. mpz_t zn;
  3072. alias_bignum_to_mpz (n, zn);
  3073. uintptr_t pos = mpz_scan1 (zn, 0L);
  3074. scm_remember_upto_here_1 (n);
  3075. return uintptr_t_to_scm (pos);
  3076. }