integers.c 74 KB

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