arith03.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  1. /* arith03.c Copyright (C) 1990-2002 Codemist Ltd */
  2. /*
  3. * Arithmetic functions.
  4. * quotient.
  5. *
  6. */
  7. /*
  8. * This code may be used and modified, and redistributed in binary
  9. * or source form, subject to the "CCL Public License", which should
  10. * accompany it. This license is a variant on the BSD license, and thus
  11. * permits use of code derived from this in either open and commercial
  12. * projects: but it does require that updates to this code be made
  13. * available back to the originators of the package.
  14. * Before merging other code in with this or linking this code
  15. * with other packages or libraries please check that the license terms
  16. * of the other material are compatible with those of this.
  17. */
  18. /* Signature: 4fa193f7 08-Apr-2002 */
  19. #include <stdarg.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include <math.h>
  23. #include "machine.h"
  24. #include "tags.h"
  25. #include "cslerror.h"
  26. #include "externs.h"
  27. #include "arith.h"
  28. #ifdef TIMEOUT
  29. #include "timeout.h"
  30. #endif
  31. /*
  32. * Division
  33. */
  34. #ifndef IDIVIDE
  35. #ifdef MULDIV64
  36. unsigned32 Idivide(unsigned32 *qp, unsigned32 a, unsigned32 b, unsigned32 c)
  37. /*
  38. * *qp = (a,b) / c, return the remainder
  39. *
  40. * The double-length value (a,b) is given as a 62-bit positive number. Its
  41. * low order 31 bits are in b, and the 0x80000000 bit of b is zero. The
  42. * high order part (in a) is also represented with the 0x80000000 bit zero.
  43. * The divisor c is a positive value that is at most 0x7fffffff, and the
  44. * procedure will only be called when the correct quotient is in the
  45. * (inclusive) range 0 to 0x7fffffff. The above constraints can be thought
  46. * of in two ways: (a) Idivide used a 31-bit word and is working on
  47. * unsigned values. The 31-bit quantities happen to be being passed around
  48. * in 32 bit registers, but the top bit of those registers will never be used
  49. * and will contain zero, or (b) the range of values used is such that
  50. * a 64 by 32-bit division can be performed, and by constraining the range
  51. * of values this 64 by 32-bit division only has to handle positive numbers,
  52. * but depending on the hardware of your computer you are entitled to use
  53. * either signed or unsigned arithmetic.
  54. */
  55. {
  56. unsigned64 p = ((unsigned64)a << 31) | (unsigned64)b;
  57. *qp = (unsigned32)(p / (unsigned64)c);
  58. return (unsigned32)(p % (unsigned64)c);
  59. }
  60. #else
  61. unsigned32 Idivide(unsigned32 *qp, unsigned32 a, unsigned32 b, unsigned32 c)
  62. /*
  63. * *qp = (a,b) / c, return the remainder
  64. *
  65. * The double-length value (a,b) is given as a 62-bit positive number. Its
  66. * low order 31 bits are in b, and the 0x80000000 bit of b is zero. The
  67. * high order part (in a) is also represented with the 0x80000000 bit zero.
  68. * The divisor c is a positive value that is at most 0x7fffffff, and the
  69. * procedure will only be called when the correct quotient is in the
  70. * (inclusive) range 0 to 0x7fffffff. The above constraints can be thought
  71. * of in two ways: (a) Idivide used a 31-bit word and is working on
  72. * unsigned values. The 31-bit quantities happen to be being passed around
  73. * in 32 bit registers, but the top bit of those registers will never be used
  74. * and will contain zero, or (b) the range of values used is such that
  75. * a 64 by 32-bit division can be performed, and by constraining the range
  76. * of values this 64 by 32-bit division only has to handle positive numbers,
  77. * but depending on the hardware of your computer you are entitled to use
  78. * either signed or unsigned arithmetic.
  79. */
  80. {
  81. int i;
  82. unsigned32 q = 0;
  83. /*
  84. * I have a loop that needs to be executed 32 times, with just
  85. * slightly different behaviour the last time around. Since it is
  86. * fairly short, I unwind it three-fold into a loop executed 10 times
  87. * plus a postlude. I also do a test at the start that decides if the
  88. * quotient will be small (less than about 16 bits) an in that case
  89. * loop rather less often - my reasoning is that a plausible distribution
  90. * of quotients is exponential so the short route will be taken about
  91. * half of the time, and will save almost half of the work done here at the
  92. * cost of a not-too-expensive extra test to begin with.
  93. */
  94. if (a < (c >> 15))
  95. { a = (a << 15) | (b >> 16);
  96. b = (b << 15) & 0x7fffffff;
  97. i = 5;
  98. }
  99. else i = 0;
  100. do
  101. { q = q << 3; /* accumulate quotient 3 bits at a time */
  102. if (a >= c) /* bit 1 of 3... */
  103. { a = a - c;
  104. q = q + 4;
  105. }
  106. a = a << 1; /* shift (a,b) doubleword left 1 bit */
  107. b = b << 1;
  108. if (((int32)b) < 0) a = a + 1;
  109. if (a >= c) /* bit 2 of 3... */
  110. { a = a - c;
  111. q = q + 2;
  112. }
  113. a = a << 1;
  114. b = b << 1;
  115. if (((int32)b) < 0) a = a + 1;
  116. if (a >= c) /* bit 3 of 3... */
  117. { a = a - c;
  118. q = q + 1;
  119. }
  120. a = a << 1;
  121. b = b << 1;
  122. if (((int32)b) < 0) a = a + 1;
  123. i++;
  124. } while (i < 10);
  125. q = q << 2; /* 2 more bits to be included */
  126. if (a >= c)
  127. { a = a - c;
  128. q = q + 2;
  129. }
  130. a = a << 1;
  131. b = b << 1;
  132. if (((int32)b) < 0) a = a + 1;
  133. if (a >= c) /* the final bit of the quotient */
  134. { a = a - c; /* leave remainder in a, b should be zero */
  135. q = q + 1;
  136. }
  137. *qp = q;
  138. return a;
  139. }
  140. #endif
  141. #endif /* IDIVIDE */
  142. #ifdef COMMON
  143. static Lisp_Object quotis(Lisp_Object a, Lisp_Object b)
  144. {
  145. Float_union bb;
  146. bb.i = b - TAG_SFLOAT;
  147. if (bb.f == 0.0) return aerror2("bad arg for quotient", a, b);
  148. bb.f = (float) ((float)int_of_fixnum(a) / bb.f);
  149. return (bb.i & ~(int32)0xf) + TAG_SFLOAT;
  150. }
  151. #endif
  152. static Lisp_Object quotib(Lisp_Object a, Lisp_Object b)
  153. {
  154. /*
  155. * a fixnum divided by a bignum always gives 0, regardless of signs etc.,
  156. * save in the one case of (-#x8000000)/(#x8000000) which must yield -1
  157. */
  158. if (int_of_fixnum(a) == fix_mask && bignum_length(b) == 8 &&
  159. bignum_digits(b)[0] == 0x08000000) return fixnum_of_int(-1);
  160. else return fixnum_of_int(0);
  161. }
  162. #ifdef COMMON
  163. static Lisp_Object CLquotib(Lisp_Object a, Lisp_Object b)
  164. {
  165. Lisp_Object g, nil = C_nil;
  166. CSLbool w;
  167. push2(a, b);
  168. w = minusp(b);
  169. errexitn(2);
  170. g = gcd(stack[0], stack[-1]);
  171. errexitn(2);
  172. if (w)
  173. { g = negate(g);
  174. errexitn(2);
  175. }
  176. a = stack[-1];
  177. push(g);
  178. a = quot2(a, g);
  179. errexitn(3);
  180. pop2(g, b);
  181. stack[0] = a;
  182. b = quot2(b, g);
  183. pop(a);
  184. errexit();
  185. return make_ratio(a, b);
  186. }
  187. static Lisp_Object CLquotbi(Lisp_Object a, Lisp_Object b)
  188. {
  189. return CLquotib(a, b);
  190. }
  191. static Lisp_Object CLquotbb(Lisp_Object a, Lisp_Object b)
  192. {
  193. return CLquotib(a, b);
  194. }
  195. static Lisp_Object quotir(Lisp_Object a, Lisp_Object b)
  196. {
  197. Lisp_Object w, nil;
  198. if (a == fixnum_of_int(0)) return a;
  199. push3(b, a, C_nil);
  200. #define g stack[0]
  201. #define a stack[-1]
  202. #define b stack[-2]
  203. g = gcd(a, numerator(b));
  204. /*
  205. * the gcd() function guarantees to hand back a positive result.
  206. */
  207. nil = C_nil;
  208. if (exception_pending()) goto fail;
  209. a = quot2(a, g);
  210. nil = C_nil;
  211. if (exception_pending()) goto fail;
  212. w = minusp(numerator(b));
  213. nil = C_nil;
  214. if (exception_pending()) goto fail;
  215. if (w)
  216. { g = negate(g);
  217. nil = C_nil;
  218. if (exception_pending()) goto fail;
  219. }
  220. g = quot2(numerator(b), g); /* denominator of result will be +ve */
  221. nil = C_nil;
  222. if (exception_pending()) goto fail;
  223. a = times2(a, denominator(b));
  224. nil = C_nil;
  225. if (exception_pending()) goto fail;
  226. w = make_ratio(a, g);
  227. popv(3);
  228. return w;
  229. fail:
  230. popv(3);
  231. return nil;
  232. #undef a
  233. #undef b
  234. #undef g
  235. }
  236. static Lisp_Object quotic(Lisp_Object a, Lisp_Object b)
  237. /*
  238. * Used for all cases of xxx/<p+iq>. This is coded in a fairly naive
  239. * way, multiplying both numerator and denominator of what will end up
  240. * as the result by the complex conjugate of b. If floating point
  241. * arithmetic is used this can lead to grossly premature overflow. For
  242. * the moment I will ignore that miserable fact
  243. */
  244. {
  245. Lisp_Object u, v, nil;
  246. push2(a, b);
  247. #define b stack[0]
  248. #define a stack[-1]
  249. /*
  250. * a / (p + iq) is computed as follows:
  251. * (a * (p - iq)) / (p^2 + q^2)
  252. */
  253. u = negate(imag_part(b));
  254. nil = C_nil;
  255. if (exception_pending()) goto fail;
  256. u = make_complex(real_part(b), u);
  257. nil = C_nil;
  258. if (exception_pending()) goto fail;
  259. a = times2(a, u);
  260. u = real_part(b);
  261. u = times2(u, u);
  262. nil = C_nil;
  263. if (exception_pending()) goto fail;
  264. v = imag_part(b);
  265. b = u;
  266. u = times2(v, v);
  267. nil = C_nil;
  268. if (exception_pending()) goto fail;
  269. u = plus2(u, b);
  270. nil = C_nil;
  271. if (exception_pending()) goto fail;
  272. v = a;
  273. popv(2);
  274. return quot2(v, u);
  275. #undef a
  276. #undef b
  277. fail:
  278. popv(2);
  279. return nil;
  280. }
  281. #endif
  282. static Lisp_Object quotif(Lisp_Object a, Lisp_Object b)
  283. {
  284. double d = float_of_number(b);
  285. if (d == 0.0) return aerror2("bad arg for quotient", a, b);
  286. d = (double)int_of_fixnum(a) / d;
  287. return make_boxfloat(d, type_of_header(flthdr(b)));
  288. }
  289. #ifdef COMMON
  290. static Lisp_Object quotsi(Lisp_Object a, Lisp_Object b)
  291. {
  292. Float_union aa;
  293. if (b == fixnum_of_int(0)) return aerror2("bad arg for quotient", a, b);
  294. aa.i = a - TAG_SFLOAT;
  295. aa.f = (float) (aa.f / (float)int_of_fixnum(b));
  296. return (aa.i & ~(int32)0xf) + TAG_SFLOAT;
  297. }
  298. static Lisp_Object quotsb(Lisp_Object a, Lisp_Object b)
  299. {
  300. double d = float_of_number(b);
  301. if (d == 0.0) return aerror2("bad arg for quotient", a, b);
  302. d = float_of_number(a) / d;
  303. return make_sfloat(d);
  304. }
  305. #define quotsr(a, b) quotsb(a, b)
  306. #define quotsc(a, b) quotic(a, b)
  307. #endif
  308. static Lisp_Object quotsf(Lisp_Object a, Lisp_Object b)
  309. {
  310. double d = float_of_number(b);
  311. if (d == 0.0) return aerror2("bad arg for quotient", a, b);
  312. d = float_of_number(a) / d;
  313. return make_boxfloat(d, type_of_header(flthdr(b)));
  314. }
  315. Lisp_Object quotbn(Lisp_Object a, int32 n)
  316. /*
  317. * Divide a bignum by an integer, where the integer is (by now)
  318. * a natural C int32 but limited to 31 not 32 bits active. I.e.
  319. * this can be used when dividing by a fixnum or by dividing by
  320. * a one-word bignum. I will not call this with n=-1, which would
  321. * be the one case where it could cause the quotient to be bigger
  322. * than the dividend. Leaves nwork set to the remainder.
  323. */
  324. {
  325. Lisp_Object nil;
  326. int sign;
  327. int32 lena = (bignum_length(a)-CELL)/4-1, i, lenc, lenx;
  328. unsigned32 carry;
  329. if (lena == 0) /* one-word bignum as numerator */
  330. { int32 p = (int32)bignum_digits(a)[0], w;
  331. nil = C_nil; /* So I can access nwork */
  332. nwork = p % n;
  333. /*
  334. * C does not define what happens on non-exact division involving
  335. * negative quantities, so I adjust things here so that the remainder
  336. * has the sign that I want and the division I do is exact.
  337. */
  338. if (p < 0)
  339. { if (nwork > 0) nwork -= n;
  340. }
  341. else if (nwork < 0) nwork += n;
  342. /* Remainder should be correct (with correct sign) by now, regardless */
  343. p = (p - nwork) / n;
  344. w = p & fix_mask;
  345. if (w == 0 || w == fix_mask) return fixnum_of_int(p);
  346. else return make_one_word_bignum(p);
  347. }
  348. else if (lena == 1)
  349. /*
  350. * I treat division of a 2-word bignum by a fixnum or 1-word bignum as
  351. * a special case since it can lead to a fixnum result - if the divisor is
  352. * just one word long and the dividend is 3 or more words I would
  353. * certainly have a bignum result. Thus by separating off the code here
  354. * I (a) remove the need for test relating to big- to fixnum conversion
  355. * later on and (b) avoid allocating heap in this tolerably common case
  356. * when sometimes I will not need to.
  357. */
  358. { unsigned32 a0 = bignum_digits(a)[0];
  359. int32 a1 = (int32)bignum_digits(a)[1];
  360. if (a1 < 0)
  361. { sign = 3;
  362. if (a0 == 0) a1 = -a1;
  363. else
  364. { a0 = clear_top_bit(-(int32)a0);
  365. a1 = ~a1;
  366. }
  367. }
  368. else sign = 0;
  369. if (n < 0) sign ^= 1, n = -n;
  370. if (a1 < n) /* result can be calculated in one word - good! */
  371. { unsigned32 q, r;
  372. Ddivide(r, q, (unsigned32)a1, a0, n);
  373. if ((sign & 2) != 0) r = -(int32)r;
  374. nil = C_nil;
  375. nwork = r;
  376. a0 = q;
  377. if (a0 == 0) return fixnum_of_int(0);
  378. if ((sign & 1) == 0)
  379. { if ((a0 & fix_mask) == 0) return fixnum_of_int(a0);
  380. else if ((a0 & 0x40000000) == 0)
  381. return make_one_word_bignum(a0);
  382. else return make_two_word_bignum(0, a0);
  383. }
  384. a0 = -(int32)a0;
  385. if ((a0 & fix_mask) == fix_mask) return fixnum_of_int(a0);
  386. else if ((a0 & 0xc0000000U) == 0xc0000000U)
  387. return make_one_word_bignum(a0);
  388. else return make_two_word_bignum(-1, clear_top_bit(a0));
  389. }
  390. /*
  391. * here the quotient will involve exactly two digits.
  392. */
  393. else
  394. { unsigned32 q1, q0, r;
  395. Ddivide(r, q1, 0, (unsigned32)a1, n);
  396. Ddivide(r, q0, r, a0, n);
  397. if ((sign & 2) != 0) r = -(int32)r;
  398. nil = C_nil;
  399. nwork = r;
  400. if ((sign & 1) != 0)
  401. { if (q0 == 0) q1 = -(int32)q1;
  402. else
  403. { q1 = ~q1;
  404. q0 = clear_top_bit(-(int32)q0);
  405. }
  406. }
  407. if (q1 == 0 && (q0 & 0x40000000) == 0)
  408. return make_one_word_bignum(q0);
  409. return make_two_word_bignum(q1, q0);
  410. }
  411. }
  412. /*
  413. * That has dealt with the special cases - now the input is a bignum
  414. * with at least 3 digits and the quotient will certainly be a bignum.
  415. * Start by allocating a workspace copy of the dividend. negateb will
  416. * leave a a bignum, although it may change its length.
  417. */
  418. if ((int32)bignum_digits(a)[lena] < 0) a = negateb(a), sign = 3;
  419. else a = copyb(a), sign = 0;
  420. errexit();
  421. if (n < 0)
  422. { sign ^= 1;
  423. n = -n;
  424. }
  425. /*
  426. * Now both a and n have been made positive, and their original signs
  427. * have been recorded so that I can tidy up at the end. The 1 bit in sign
  428. * tells me what sign the quotient will have, the 2 bit gives the sign
  429. * for the remainder.
  430. */
  431. lena = (bignum_length(a)-CELL)/4;
  432. carry = 0;
  433. for (i=lena-1; i>=0; i--)
  434. Ddivide(carry, bignum_digits(a)[i], carry, bignum_digits(a)[i], n);
  435. if ((sign & 2) != 0) carry = -(int32)carry;
  436. nil = C_nil;
  437. nwork = carry; /* leave remainder available to caller */
  438. lena--;
  439. while (bignum_digits(a)[lena] == 0) lena--;
  440. if ((bignum_digits(a)[lena] & 0x40000000) != 0) lena++;
  441. if ((sign & 1) != 0) /* quotient will be negative */
  442. { carry = 0xffffffffU;
  443. for (i=0; i<lena; i++)
  444. { carry = clear_top_bit(~bignum_digits(a)[i]) + top_bit(carry);
  445. bignum_digits(a)[i] = clear_top_bit(carry);
  446. }
  447. carry = ~bignum_digits(a)[i] + top_bit(carry);
  448. if (carry == -1 && (bignum_digits(a)[i-1] & 0x40000000) != 0)
  449. { bignum_digits(a)[lena] = 0;
  450. lena--;
  451. bignum_digits(a)[i-1] |= ~0x7fffffff;
  452. }
  453. else bignum_digits(a)[i] = carry;
  454. }
  455. /* I need to free up some space here, I guess */
  456. lenx = (bignum_length(a)-CELL)/4;
  457. #ifdef ADDRESS_64
  458. lenx = (lenx + 1) & ~1;
  459. lenc = (lena + 2) & ~1;
  460. #else
  461. lenx |= 1; /* round up to allow for allocation in doublewords */
  462. lenc = (lena+1) | 1;
  463. #endif
  464. if (lenc != lenx) /* space to discard? */
  465. *(Header *)&bignum_digits(a)[lenc] = make_bighdr(lenx-lenc);
  466. numhdr(a) = make_bighdr(lena+1+CELL/4);
  467. return a;
  468. }
  469. Lisp_Object quotbn1(Lisp_Object a, int32 n)
  470. /*
  471. * Divide a bignum by an integer, where the integer is (by now)
  472. * a natural C int32 but limited to 31 not 32 bits active. I.e.
  473. * this can be used when dividing by a fixnum or by dividing by
  474. * a one-word bignum. I will not call this with n=-1, which would
  475. * be the one case where it could cause the quotient to be bigger
  476. * than the dividend. Leaves nwork set to the remainder.
  477. * This version is JUST the same as quotbn() except that it does not
  478. * hand back a useful quotient - it is intended for use when only
  479. * the remainder is wanted. For consistency I leave that where quotbn() did.
  480. */
  481. {
  482. Lisp_Object nil;
  483. int sign;
  484. int32 lena = (bignum_length(a)-CELL)/4-1, i;
  485. unsigned32 carry;
  486. if (lena == 0) /* one-word bignum as numerator */
  487. { int32 p = (int32)bignum_digits(a)[0];
  488. nil = C_nil; /* So I can access nwork */
  489. nwork = p % n;
  490. /*
  491. * C does not define what happens on non-exact division involving
  492. * negative quantities, so I adjust things here so that the remainder
  493. * has the sign that I want and the division I do is exact.
  494. */
  495. if (p < 0)
  496. { if (nwork > 0) nwork -= n;
  497. }
  498. else if (nwork < 0) nwork += n;
  499. /* Remainder should be correct (with correct sign) by now, regardless */
  500. else return nil;
  501. }
  502. else if (lena == 1)
  503. /*
  504. * I treat division of a 2-word bignum by a fixnum or 1-word bignum as
  505. * a special case since it can lead to a fixnum result - if the divisor is
  506. * just one word long and the dividend is 3 or more words I would
  507. * certainly have a bignum result. Thus by separating off the code here
  508. * I (a) remove the need for test relating to big- to fixnum conversion
  509. * later on and (b) avoid allocating heap in this tolerably common case
  510. * when sometimes I will not need to.
  511. */
  512. { unsigned32 a0 = bignum_digits(a)[0];
  513. int32 a1 = (int32)bignum_digits(a)[1];
  514. if (a1 < 0)
  515. { sign = 3;
  516. if (a0 == 0) a1 = -a1;
  517. else
  518. { a0 = clear_top_bit(-(int32)a0);
  519. a1 = ~a1;
  520. }
  521. }
  522. else sign = 0;
  523. if (n < 0) sign ^= 1, n = -n;
  524. if (a1 < n) /* result can be calculated in one word - good! */
  525. { unsigned32 q, r;
  526. Ddivide(r, q, (unsigned32)a1, a0, n);
  527. if ((sign & 2) != 0) r = -(int32)r;
  528. nil = C_nil;
  529. nwork = r;
  530. return nil;
  531. }
  532. /*
  533. * here the quotient will involve two digits.
  534. */
  535. else
  536. { unsigned32 q1, q0, r;
  537. Ddivide(r, q1, 0, (unsigned32)a1, n);
  538. Ddivide(r, q0, r, a0, n);
  539. if ((sign & 2) != 0) r = -(int32)r;
  540. nil = C_nil;
  541. nwork = r;
  542. return nil;
  543. }
  544. }
  545. /*
  546. * That has dealt with the special cases - now the input is a bignum
  547. * with at least 3 digits and the quotient will certainly be a bignum.
  548. * Start by allocating a workspace copy of the dividend. negateb will
  549. * leave a a bignum, although it may change its length.
  550. */
  551. /*
  552. * Also note that I could fold the negateb() in with the division, and
  553. * thereby save allocating a big hunk of extra memory.
  554. */
  555. if ((int32)bignum_digits(a)[lena] < 0) a = negateb(a), sign = 3;
  556. else a = copyb(a), sign = 0;
  557. errexit();
  558. if (n < 0)
  559. { sign ^= 1;
  560. n = -n;
  561. }
  562. /*
  563. * Now both a and n have been made positive, and their original signs
  564. * have been recorded so that I can tidy up at the end. The 1 bit in sign
  565. * tells me what sign the quotient will have, the 2 bit gives the sign
  566. * for the remainder.
  567. */
  568. lena = (bignum_length(a)-CELL)/4;
  569. carry = 0;
  570. for (i=lena-1; i>=0; i--)
  571. Ddivide(carry, bignum_digits(a)[i], carry, bignum_digits(a)[i], n);
  572. if ((sign & 2) != 0) carry = -(int32)carry;
  573. nil = C_nil;
  574. nwork = carry; /* leave remainder available to caller */
  575. return nil;
  576. }
  577. static Lisp_Object quotbi(Lisp_Object a, Lisp_Object b)
  578. {
  579. /*
  580. * dividing by 1 or -1 seems worth optimising.
  581. */
  582. if (b == fixnum_of_int(1)) return a;
  583. else if (b == fixnum_of_int(-1)) return negateb(a);
  584. else if (b == fixnum_of_int(0))
  585. return aerror2("bad arg for quotient", a, b);
  586. else return quotbn(a, int_of_fixnum(b));
  587. }
  588. #ifdef COMMON
  589. #define quotbs(a, b) quotsb(a, b)
  590. #endif
  591. #ifdef __powerc
  592. /* If you have trouble compiling this just comment it out, please */
  593. #pragma options(!global_optimizer)
  594. #endif
  595. Lisp_Object quotbb(Lisp_Object a, Lisp_Object b)
  596. /*
  597. * Divide one bignum by another. This can compute the
  598. * remainder at the same time as the quotient, and leaves same around
  599. * in mv_2. If it is not needed then setting up the remainder is
  600. * a cost - but usually a modest one (in context), so I think that
  601. * the simplification is worth-while.
  602. */
  603. {
  604. Lisp_Object nil, olda;
  605. int sign;
  606. int32 lena, lenb, lenc, lenx, i;
  607. unsigned32 carry, scale, v1;
  608. Lisp_Object q, r;
  609. /*
  610. * If I am dividing by a one-word bignum I can go a bit faster...
  611. */
  612. lenb = (bignum_length(b)-CELL-4)/4;
  613. if (lenb == 0)
  614. { q = quotbn(a, bignum_digits(b)[0]);
  615. errexit();
  616. /*
  617. * Now lots of frivolity packing up the remainder...
  618. */
  619. r = (Lisp_Object)(nwork & fix_mask);
  620. if ((int32)r == 0 || (int32)r == fix_mask)
  621. mv_2 = fixnum_of_int(nwork);
  622. else
  623. { push(q);
  624. a = make_one_word_bignum(nwork);
  625. pop(q);
  626. errexit();
  627. mv_2 = a;
  628. }
  629. return q;
  630. }
  631. /*
  632. * Convert to sign and magnitude representation
  633. */
  634. push2(a, b);
  635. lena = (bignum_length(a)-CELL-4)/4;
  636. if ((int32)bignum_digits(a)[lena] < 0)
  637. { a = negateb(a);
  638. sign = 3;
  639. }
  640. else
  641. { a = copyb(a);
  642. sign = 0;
  643. }
  644. pop(b);
  645. errexit();
  646. lena = (bignum_length(a)-CELL-4)/4;
  647. push(a);
  648. if ((int32)bignum_digits(b)[lenb] < 0)
  649. { b = negateb(b);
  650. /*
  651. * I MUST NOT do lenb = (bignum_length(b)-CELL-4)/4; here because the
  652. * negateb may have failed and therefore handed back junk rather than
  653. * a bignum. E.g. an interrupt provoked by the user or a store-jam might
  654. * lead to a failure report here. So I must defer re-finding the length until
  655. * I have checked for exceptions.
  656. */
  657. sign ^= 1;
  658. }
  659. else b = copyb(b);
  660. pop2(a, olda); /* original a, with original sign */
  661. errexit();
  662. lenb = (bignum_length(b)-CELL-4)/4;
  663. /*
  664. * Now that the numbers are unsigned it could be that I can drop
  665. * a leading zero that had previously been necessary. That could reveal
  666. * that I have a one-word divisor after all...
  667. */
  668. if (bignum_digits(b)[lenb] == 0) lenb--;
  669. if (lenb == 0)
  670. { a = quotbn(a, bignum_digits(b)[0]);
  671. errexit();
  672. if ((sign & 2) != 0) nwork = -nwork;
  673. if ((sign & 1) != 0) a = negate(a);
  674. errexit();
  675. r = (Lisp_Object)(nwork & fix_mask);
  676. if (r == 0 || (int32)r == fix_mask) mv_2 = fixnum_of_int(nwork);
  677. else
  678. { push(a);
  679. if (signed_overflow(nwork))
  680. { if ((int32)nwork < 0)
  681. b = make_two_word_bignum(-1, clear_top_bit(nwork));
  682. else b = make_two_word_bignum(0, nwork);
  683. }
  684. else b = make_one_word_bignum(nwork);
  685. pop(a);
  686. errexit();
  687. mv_2 = b;
  688. }
  689. return a;
  690. }
  691. /*
  692. * Now the divisor is at least two digits long.
  693. */
  694. if (bignum_digits(a)[lena] == 0) lena--;
  695. /*
  696. * I will take a special case when the lengths of the two numbers
  697. * match since in that case the quotient will be only one digit long.
  698. * Also after I have filtered the lena<=lenb cases out, and have treated
  699. * one-word cases of b specially I will know that the numerator a has
  700. * at least two digits.
  701. */
  702. if (lena < lenb)
  703. { mv_2 = olda;
  704. return fixnum_of_int(0);
  705. }
  706. else if (lenb == lena)
  707. { unsigned32 p0 = bignum_digits(a)[lena-1], p1 = bignum_digits(a)[lena],
  708. q0 = bignum_digits(b)[lenb-1], q1 = bignum_digits(b)[lena],
  709. r0, r1, q, w, carry1;
  710. /*
  711. * If the dividend is smaller than the divisor I can return zero right now.
  712. */
  713. if (p1 < q1 || (p1 == q1 && p0 < q0))
  714. { mv_2 = olda;
  715. return fixnum_of_int(0);
  716. }
  717. /*
  718. * I scale the divisor so that it will have its top bit set (top wrt the
  719. * 31 bit field that is in use, that is), and scale the top two digits
  720. * of the dividend to match. The resulting values can then be divided to get
  721. * a pretty good estimate for the true quotient. Note that the division on
  722. * the next line is UNSIGNED arithmetic.
  723. */
  724. scale = 0x80000000U / ((unsigned32)1 + q1);
  725. Dmultiply(carry, w, q0, scale, 0);
  726. Dmultiply(q, w, q1, scale, carry);
  727. q = w;
  728. Dmultiply(carry, w, p0, scale, 0);
  729. Dmultiply(carry, w, p1, scale, carry);
  730. if (carry == q) q = 0x7fffffff;
  731. else
  732. { Ddivide(q, w, carry, w, q);
  733. q = w;
  734. }
  735. /*
  736. * q is now my estimated quotient, based on a quick look at high digits.
  737. */
  738. Dmultiply(carry, w, q0, q, 0);
  739. r0 = w;
  740. Dmultiply(carry, w, q1, q, carry);
  741. r1 = w;
  742. r0 = r0 - p0;
  743. if ((int32)r0 < 0)
  744. { r0 = clear_top_bit(r0);
  745. r1 = r1 - p1 - 1;
  746. }
  747. else r1 = r1 - p1;
  748. /* /*
  749. * the next line is a cop-out for now - if my estimated quotient
  750. * was close enough to the true value than the residual I get here
  751. * ought to be fairly small - if it is not I have bungled. Over several years
  752. * of testa and use I have not seen the disaster message triggered, but the
  753. * code should stay in until I can write the paragraph of comment that
  754. * should go here explaing why all is well...
  755. */
  756. if (carry != 0 && (int32)r1 < 0 &&
  757. carry != 1 && r1 != ~0x7fffffff)
  758. {
  759. err_printf("\n+++!!!+++ carry needed (line %d of \"%s\")\n",
  760. __LINE__, __FILE__);
  761. my_exit(EXIT_FAILURE);
  762. }
  763. /*
  764. * That obtained the remainder from (p1,p0)/(q1,q0) - now adjust q until
  765. * the remainder has the sign I want it to have.
  766. */
  767. /* /* I do not look at carry here - I have a nasty suspicion that I should.. */
  768. while ((int32)r1 > 0 ||
  769. (r1 == 0 && r0 != 0))
  770. { q--;
  771. r0 = r0 - q0;
  772. if ((int32)r0 < 0)
  773. { r0 = clear_top_bit(r0);
  774. r1 = r1 - q1 - 1;
  775. }
  776. else r1 = r1 - q1;
  777. }
  778. /*
  779. * Now q is (p1,p0)/(q1,q0), which is an over-estimate for the true
  780. * quotient, but by at worst 1. Compute a proper full-length remainder
  781. * now, using the original unscaled input numbers.
  782. */
  783. carry = 0;
  784. carry1 = 0xffffffffU;
  785. for (i=0; i<=lena; i++)
  786. { Dmultiply(carry, w, bignum_digits(b)[i], q, carry);
  787. carry1 = bignum_digits(a)[i] + clear_top_bit(~w) + top_bit(carry1);
  788. bignum_digits(a)[i] = clear_top_bit(carry1);
  789. }
  790. if ((int32)carry1 >= 0) /* need to correct it! */
  791. { q--;
  792. for (i=0; i<=lena; i++)
  793. { carry1 = bignum_digits(a)[i] + bignum_digits(b)[i] +
  794. top_bit(carry1);
  795. bignum_digits(a)[i] = clear_top_bit(carry1);
  796. }
  797. }
  798. /*
  799. * Now the quotient is correct - but since I want to hand back a neat
  800. * remainder I have more to do - I must convert the value stored in a
  801. * into a legitimate result, and store it in mv_2.
  802. */
  803. while (lena > 0 && bignum_digits(a)[lena] == 0) lena--;
  804. if ((bignum_digits(a)[lena] & 0x40000000) != 0) lena++;
  805. if (lena == 0) /* Maybe fixnum remainder? */
  806. { int32 rr = bignum_digits(a)[0];
  807. if (rr != 0 && (sign & 2) != 0)
  808. { rr = -rr;
  809. if ((rr & fix_mask) == fix_mask)
  810. { mv_2 = fixnum_of_int(rr);
  811. goto return_q;
  812. }
  813. }
  814. else if ((rr & fix_mask) == 0)
  815. { mv_2 = fixnum_of_int(rr);
  816. goto return_q;
  817. }
  818. }
  819. if ((sign & 2) != 0)
  820. { carry = 0xffffffffU;
  821. for (i=0; i<lena; i++)
  822. { carry = clear_top_bit(~bignum_digits(a)[i]) + top_bit(carry);
  823. bignum_digits(a)[i] = clear_top_bit(carry);
  824. }
  825. carry = ~bignum_digits(a)[i] + top_bit(carry);
  826. bignum_digits(a)[i] = carry;
  827. /*
  828. * if my remainder is -2^(31*n-1) then I need to shrink lena here, which
  829. * seems like a messy case to have to consider.
  830. */
  831. if (carry == -1 && (bignum_digits(a)[i-1] & 0x40000000) != 0)
  832. { bignum_digits(a)[lena] = 0;
  833. lena--;
  834. bignum_digits(a)[i-1] |= ~0x7fffffff;
  835. }
  836. }
  837. /*
  838. * Now tidy up the space that I am about to discard, so that it will not
  839. * upset the garbage collector.
  840. */
  841. lenx = (bignum_length(a)-CELL)/4;
  842. #ifdef ADDRESS_64
  843. lenx = (lenx+1) & ~1;
  844. lenc = (lena+2) & ~1;
  845. #else
  846. lenx |= 1; /* round up to allow for allocation in doublewords */
  847. lenc = (lena+1) | 1;
  848. #endif
  849. if (lenc != lenx) /* space to discard? */
  850. *(Header *)&bignum_digits(a)[lenc] = make_bighdr(lenx-lenc);
  851. numhdr(a) = make_bighdr(lena+1+CELL/4);
  852. mv_2 = a;
  853. return_q:
  854. if (q != 0 && (sign & 1) != 0)
  855. { q = -(int32)q;
  856. if ((q & fix_mask) == fix_mask) return fixnum_of_int(q);
  857. if ((q & 0x40000000) == 0)
  858. return make_two_word_bignum(-1, clear_top_bit(q));
  859. }
  860. else if ((q & fix_mask) == 0) return fixnum_of_int(q);
  861. else if ((q & 0x40000000) != 0) return make_two_word_bignum(0, q);
  862. return make_one_word_bignum(q);
  863. }
  864. /*
  865. * Now both a and b have been made positive, and their original signs
  866. * have been recorded so that I can tidy up at the end. The 1 bit in sign
  867. * tells me what sign the quotient will have, the 2 bit gives the sign
  868. * for the remainder. Both a and b have been copied so it will be OK to
  869. * overwrite them in the process of doing the division. The length of
  870. * a is strictly greater than that of b, which itself is at least 2 digits,
  871. * so this is a real case of long division, although the quotient may still
  872. * end up small (but non-zero).
  873. */
  874. /*
  875. * I find a scale factor to multiply a and b by so that the leading digit of
  876. * the divisor is fairly large. Again note that the arithmetic is UNSIGNED.
  877. */
  878. scale = 0x80000000U / ((unsigned32)1 + (unsigned32)bignum_digits(b)[lenb]);
  879. carry = 0;
  880. for (i=0; i<=lenb; i++)
  881. Dmultiply(carry, bignum_digits(b)[i],
  882. bignum_digits(b)[i], scale, carry);
  883. v1 = bignum_digits(b)[lenb];
  884. carry = 0;
  885. for (i=0; i<=lena; i++)
  886. Dmultiply(carry, bignum_digits(a)[i],
  887. bignum_digits(a)[i], scale, carry);
  888. /*
  889. * The scaled value of the numerator a may involve an extra digit
  890. * beyond those that I have space for in the vector. Hold this digit
  891. * in the variable 'carry'. Note that scaling the divisor did NOT cause
  892. * it to change length.
  893. */
  894. while (lena >= lenb)
  895. { unsigned32 w, h0, h1, v2, u2, carry1, carry2, qq, rr;
  896. if (carry == v1)
  897. { qq = 0x7fffffff;
  898. rr = carry + bignum_digits(a)[lena];
  899. }
  900. else
  901. { Ddivide(rr, w, carry, bignum_digits(a)[lena], v1);
  902. qq = w;
  903. }
  904. v2 = bignum_digits(b)[lenb-1];
  905. Dmultiply(h1, h0, v2, qq, 0);
  906. u2 = bignum_digits(a)[lena-1];
  907. if (h1 > rr ||
  908. (h1 == rr && h0 > u2))
  909. { qq--;
  910. h0 -= v2;
  911. if ((int32)h0 < 0)
  912. { h0 = clear_top_bit(h0);
  913. h1--;
  914. }
  915. rr += v1;
  916. if (h1 > rr ||
  917. (h1 == rr && h0 > u2)) qq--;
  918. }
  919. /*
  920. * Now subtract off q*b from a, up as far as lena.
  921. */
  922. carry1 = 0;
  923. carry2 = 0xffffffffU;
  924. for (i=0; i<=lenb; i++)
  925. { Dmultiply(carry1, w, bignum_digits(b)[i], qq, carry1);
  926. carry2 = bignum_digits(a)[lena-lenb+i] +
  927. clear_top_bit(~w) + top_bit(carry2);
  928. bignum_digits(a)[lena-lenb+i] = clear_top_bit(carry2);
  929. }
  930. carry2 = carry + clear_top_bit(~carry1) + top_bit(carry2);
  931. if ((int32)carry2 >= 0) /* overshot.. */
  932. { qq--;
  933. carry2 = 0;
  934. for (i=0; i<=lenb; i++)
  935. { carry2 = bignum_digits(a)[lena-lenb+i] +
  936. bignum_digits(b)[i] + top_bit(carry2);
  937. bignum_digits(a)[lena-lenb+i] = clear_top_bit(carry2);
  938. }
  939. }
  940. carry = bignum_digits(a)[lena];
  941. bignum_digits(a)[lena] = qq; /* good place to store the quotient */
  942. lena--;
  943. }
  944. lena = (bignum_length(a)-CELL-4)/4;
  945. /*
  946. * Now the quotient is in the top digits of a, and the remainder is left
  947. * (scaled up) in the low bit (plus carry)
  948. */
  949. bignum_digits(b)[lenb] = carry/scale;
  950. carry = carry%scale;
  951. for (i=lenb-1; i>=0; i--)
  952. Ddivide(carry, bignum_digits(b)[i],
  953. carry, bignum_digits(a)[i], scale);
  954. if (carry != 0)
  955. { err_printf("+++!!!+++ Incorrect unscaling line %d file \"%s\" (%ld)\n",
  956. __LINE__, __FILE__, (long)carry);
  957. my_exit(EXIT_FAILURE);
  958. }
  959. for (i=0; i<=lena-lenb; i++)
  960. bignum_digits(a)[i] = bignum_digits(a)[i+lenb];
  961. for (;i < lena; i++) bignum_digits(a)[i] = 0;
  962. lena = lena - lenb;
  963. while (bignum_digits(a)[lena] == 0) lena--;
  964. /*
  965. * the above loop terminates properly since the quotient will be non-zero,
  966. * which in turn is because I was dividing p by q where p had strictly
  967. * more digits than q, and was hence strictly greater than q.
  968. */
  969. if ((bignum_digits(a)[lena] & 0x40000000) != 0) lena++;
  970. if ((sign & 1) != 0) /* quotient will be negative */
  971. { carry = 0xffffffffU;
  972. for (i=0; i<lena; i++)
  973. { carry = clear_top_bit(~bignum_digits(a)[i]) + top_bit(carry);
  974. bignum_digits(a)[i] = clear_top_bit(carry);
  975. }
  976. carry = ~bignum_digits(a)[i] + top_bit(carry);
  977. if (carry == -1 && (bignum_digits(a)[i-1] & 0x40000000) != 0)
  978. { bignum_digits(a)[lena] = 0;
  979. lena--;
  980. bignum_digits(a)[i-1] |= ~0x7fffffff;
  981. }
  982. else bignum_digits(a)[i] = carry;
  983. }
  984. lenx = (bignum_length(a)-CELL)/4;
  985. #ifdef ADDRESS_64
  986. lenx = (lenx+1) & ~1; /* round up to allow for allocation in doublewords */
  987. lenc = (lena+2) & ~1;
  988. #else
  989. lenx |= 1; /* round up to allow for allocation in doublewords */
  990. lenc = (lena+1) | 1;
  991. #endif
  992. if (lenc != lenx) /* space to discard? */
  993. *(Header *)&bignum_digits(a)[lenc] = make_bighdr(lenx-lenc);
  994. numhdr(a) = make_bighdr(lena+1+CELL/4);
  995. if (lena == 0)
  996. { int32 d = bignum_digits(a)[0];
  997. int32 x = d & fix_mask;
  998. if (x == 0 || x == fix_mask) a = fixnum_of_int(d);
  999. }
  1000. /*
  1001. * Here I need to tidy up the discarded space that could otherwise
  1002. * kill the poor old garbage collector..
  1003. */
  1004. while (lenb >= 0 && bignum_digits(b)[lenb] == 0) lenb--;
  1005. if (lenb < 0) mv_2 = fixnum_of_int(0); /* hah - zero remainder! */
  1006. else
  1007. {
  1008. if ((bignum_digits(b)[lenb] & 0x40000000) != 0) lenb++;
  1009. if ((sign & 2) != 0) /* need to negate the remainder */
  1010. { carry = 0xffffffffU;
  1011. for (i=0; i<lenb; i++)
  1012. { carry = clear_top_bit(~bignum_digits(b)[i]) + top_bit(carry);
  1013. bignum_digits(b)[i] = clear_top_bit(carry);
  1014. }
  1015. carry = ~bignum_digits(b)[i] + top_bit(carry);
  1016. if (carry == -1 && (bignum_digits(b)[i-1] & 0x40000000) != 0)
  1017. { bignum_digits(b)[lenb] = 0;
  1018. lenb--;
  1019. bignum_digits(b)[i-1] |= ~0x7fffffff;
  1020. }
  1021. else bignum_digits(b)[i] = carry;
  1022. }
  1023. lenx = (bignum_length(b)-CELL)/4;
  1024. #ifdef ADDRESS_64
  1025. lenx = (lenx+1) & ~1; /* round up to allow for allocation in doublewords */
  1026. lenc = (lenb+2) & ~1;
  1027. #else
  1028. lenx |= 1; /* round up to allow for allocation in doublewords */
  1029. lenc = (lenb+1) | 1;
  1030. #endif
  1031. if (lenc != lenx) /* space to discard? */
  1032. *(Header *)&bignum_digits(b)[lenc] = make_bighdr(lenx-lenc);
  1033. numhdr(b) = make_bighdr(lenb+1+CELL/4);
  1034. mv_2 = b;
  1035. if (lenb == 0)
  1036. { int32 r1, rr;
  1037. rr = bignum_digits(b)[0];
  1038. r1 = rr & fix_mask;
  1039. if (r1 == 0 || r1 == fix_mask) mv_2 = fixnum_of_int(rr);
  1040. }
  1041. }
  1042. return a;
  1043. }
  1044. #ifdef __powerc
  1045. /* If you have trouble compiling this just comment it out, please */
  1046. #pragma options(global_optimizer)
  1047. #endif
  1048. #ifdef COMMON
  1049. #define quotbr(a, b) quotir(a, b)
  1050. #define quotbc(a, b) quotic(a, b)
  1051. #endif
  1052. #define quotbf(a, b) quotsf(a, b)
  1053. #ifdef COMMON
  1054. static Lisp_Object quotri(Lisp_Object a, Lisp_Object b)
  1055. {
  1056. Lisp_Object w, nil;
  1057. if (b == fixnum_of_int(1)) return a;
  1058. else if (b == fixnum_of_int(0))
  1059. return aerror2("bad arg for quotient", a, b);
  1060. push3(a, b, C_nil);
  1061. #define g stack[0]
  1062. #define b stack[-1]
  1063. #define a stack[-2]
  1064. g = gcd(b, numerator(a));
  1065. nil = C_nil;
  1066. if (exception_pending()) goto fail;
  1067. w = minusp(b);
  1068. nil = C_nil;
  1069. if (exception_pending()) goto fail;
  1070. if (w)
  1071. { g = negate(g); /* ensure denominator is +ve */
  1072. nil = C_nil;
  1073. if (exception_pending()) goto fail;
  1074. }
  1075. b = quot2(b, g);
  1076. nil = C_nil;
  1077. if (exception_pending()) goto fail;
  1078. g = quot2(numerator(a), g);
  1079. nil = C_nil;
  1080. if (exception_pending()) goto fail;
  1081. a = times2(b, denominator(a));
  1082. nil = C_nil;
  1083. if (exception_pending()) goto fail;
  1084. w = make_ratio(g, a);
  1085. popv(3);
  1086. return w;
  1087. fail:
  1088. popv(3);
  1089. return nil;
  1090. #undef a
  1091. #undef b
  1092. #undef g
  1093. }
  1094. #define quotrs(a, b) quotsb(a, b)
  1095. #define quotrb(a, b) quotib(a, b)
  1096. static Lisp_Object quotrr(Lisp_Object a, Lisp_Object b)
  1097. {
  1098. Lisp_Object w, nil;
  1099. push5(numerator(a), denominator(a),
  1100. denominator(b), numerator(b), /* NB switched order */
  1101. C_nil);
  1102. #define g stack[0]
  1103. #define db stack[-1]
  1104. #define nb stack[-2]
  1105. #define da stack[-3]
  1106. #define na stack[-4]
  1107. g = gcd(na, db);
  1108. nil = C_nil;
  1109. if (exception_pending()) goto fail;
  1110. w = minusp(db);
  1111. nil = C_nil;
  1112. if (exception_pending()) goto fail;
  1113. if (w)
  1114. { g = negate(g);
  1115. nil = C_nil;
  1116. if (exception_pending()) goto fail;
  1117. }
  1118. na = quot2(na, g);
  1119. nil = C_nil;
  1120. if (exception_pending()) goto fail;
  1121. db = quot2(db, g);
  1122. nil = C_nil;
  1123. if (exception_pending()) goto fail;
  1124. g = gcd(nb, da);
  1125. if (exception_pending()) goto fail;
  1126. nil = C_nil;
  1127. nb = quot2(nb, g);
  1128. if (exception_pending()) goto fail;
  1129. da = quot2(da, g);
  1130. nil = C_nil;
  1131. if (exception_pending()) goto fail;
  1132. na = times2(na, nb);
  1133. nil = C_nil;
  1134. if (exception_pending()) goto fail;
  1135. da = times2(da, db);
  1136. nil = C_nil;
  1137. if (exception_pending()) goto fail;
  1138. w = make_ratio(na, da);
  1139. popv(5);
  1140. return w;
  1141. fail:
  1142. popv(5);
  1143. return nil;
  1144. #undef g
  1145. #undef db
  1146. #undef nb
  1147. #undef da
  1148. #undef na
  1149. }
  1150. #define quotrc(a, b) quotic(a, b)
  1151. #define quotrf(a, b) quotsf(a, b)
  1152. static Lisp_Object quotci(Lisp_Object a, Lisp_Object b)
  1153. {
  1154. Lisp_Object r = real_part(a), i = imag_part(a), nil;
  1155. if (b == fixnum_of_int(0)) return aerror2("bad arg for quotient", a, b);
  1156. push2(b, r);
  1157. i = quot2(i, b);
  1158. pop2(r, b);
  1159. errexit();
  1160. push(i);
  1161. r = quot2(r, b);
  1162. pop(i);
  1163. errexit();
  1164. return make_complex(r, i);
  1165. }
  1166. #define quotcs(a, b) quotci(a, b)
  1167. #define quotcb(a, b) quotci(a, b)
  1168. #define quotcr(a, b) quotci(a, b)
  1169. #define quotcc(a, b) quotic(a, b)
  1170. #define quotcf(a, b) quotci(a, b)
  1171. #endif
  1172. static Lisp_Object quotfi(Lisp_Object a, Lisp_Object b)
  1173. {
  1174. double d;
  1175. if (b == fixnum_of_int(0)) return aerror2("bad arg for quotient", a, b);
  1176. d = float_of_number(a) / (double)int_of_fixnum(b);
  1177. return make_boxfloat(d, type_of_header(flthdr(a)));
  1178. }
  1179. static Lisp_Object quotfs(Lisp_Object a, Lisp_Object b)
  1180. {
  1181. double d = float_of_number(b);
  1182. if (d == 0.0) return aerror2("bad arg for quotient", a, b);
  1183. d = float_of_number(a) / d;
  1184. return make_boxfloat(d, type_of_header(flthdr(a)));
  1185. }
  1186. #define quotfb(a, b) quotfs(a, b)
  1187. #ifdef COMMON
  1188. #define quotfr(a, b) quotfs(a, b)
  1189. #define quotfc(a, b) quotic(a, b)
  1190. #endif
  1191. static Lisp_Object quotff(Lisp_Object a, Lisp_Object b)
  1192. {
  1193. #ifdef COMMON
  1194. int32 ha = type_of_header(flthdr(a)), hb = type_of_header(flthdr(b));
  1195. #endif
  1196. double d = float_of_number(b);
  1197. if (d == 0.0) return aerror2("bad arg for quotient", a, b);
  1198. d = float_of_number(a) / d;
  1199. #ifdef COMMON
  1200. if (ha == TYPE_LONG_FLOAT || hb == TYPE_LONG_FLOAT)
  1201. ha = TYPE_LONG_FLOAT;
  1202. else if (ha == TYPE_DOUBLE_FLOAT || hb == TYPE_DOUBLE_FLOAT)
  1203. ha = TYPE_DOUBLE_FLOAT;
  1204. else ha = TYPE_SINGLE_FLOAT;
  1205. return make_boxfloat(d, ha);
  1206. #else
  1207. return make_boxfloat(d, TYPE_DOUBLE_FLOAT);
  1208. #endif
  1209. }
  1210. Lisp_Object quot2(Lisp_Object a, Lisp_Object b)
  1211. {
  1212. switch ((int)a & TAG_BITS)
  1213. {
  1214. case TAG_FIXNUM:
  1215. switch ((int)b & TAG_BITS)
  1216. {
  1217. case TAG_FIXNUM:
  1218. /*
  1219. * This is where fixnum / fixnum arithmetic happens - the case I most want to
  1220. * make efficient.
  1221. */
  1222. if (b == fixnum_of_int(0))
  1223. return aerror2("bad arg for quotient", a, b);
  1224. else
  1225. { int32 r, aa, bb;
  1226. aa = int_of_fixnum(a);
  1227. bb = int_of_fixnum(b);
  1228. /* calculate remainder and force its sign to be correct */
  1229. r = aa % bb;
  1230. if (aa < 0)
  1231. { if (r > 0) r -= bb;
  1232. }
  1233. else if (r < 0) r += bb;
  1234. /* then the division can be an exact one, as here */
  1235. r = (aa - r)/bb;
  1236. /*
  1237. * the only case where dividing one fixnum by another can lead to
  1238. * a bignum result is (-0x08000000/(-1)) which JUST overflows.
  1239. */
  1240. if (r != 0x08000000) return fixnum_of_int(r);
  1241. else return make_one_word_bignum(r);
  1242. }
  1243. #ifdef COMMON
  1244. case TAG_SFLOAT:
  1245. return quotis(a, b);
  1246. #endif
  1247. case TAG_NUMBERS:
  1248. { int32 hb = type_of_header(numhdr(b));
  1249. switch (hb)
  1250. {
  1251. case TYPE_BIGNUM:
  1252. return quotib(a, b);
  1253. #ifdef COMMON
  1254. case TYPE_RATNUM:
  1255. return quotir(a, b);
  1256. case TYPE_COMPLEX_NUM:
  1257. return quotic(a, b);
  1258. #endif
  1259. default:
  1260. return aerror1("bad arg for quotient", b);
  1261. }
  1262. }
  1263. case TAG_BOXFLOAT:
  1264. return quotif(a, b);
  1265. default:
  1266. return aerror1("bad arg for quotient", b);
  1267. }
  1268. #ifdef COMMON
  1269. case TAG_SFLOAT:
  1270. switch ((int)b & TAG_BITS)
  1271. {
  1272. case TAG_FIXNUM:
  1273. return quotsi(a, b);
  1274. case TAG_SFLOAT:
  1275. { Float_union aa, bb;
  1276. aa.i = a - TAG_SFLOAT;
  1277. bb.i = b - TAG_SFLOAT;
  1278. aa.f = (float) (aa.f / bb.f);
  1279. return (aa.i & ~(int32)0xf) + TAG_SFLOAT;
  1280. }
  1281. case TAG_NUMBERS:
  1282. { int32 hb = type_of_header(numhdr(b));
  1283. switch (hb)
  1284. {
  1285. case TYPE_BIGNUM:
  1286. return quotsb(a, b);
  1287. case TYPE_RATNUM:
  1288. return quotsr(a, b);
  1289. case TYPE_COMPLEX_NUM:
  1290. return quotsc(a, b);
  1291. default:
  1292. return aerror1("bad arg for quotient", b);
  1293. }
  1294. }
  1295. case TAG_BOXFLOAT:
  1296. return quotsf(a, b);
  1297. default:
  1298. return aerror1("bad arg for quotient", b);
  1299. }
  1300. #endif
  1301. case TAG_NUMBERS:
  1302. { int32 ha = type_of_header(numhdr(a));
  1303. switch (ha)
  1304. {
  1305. case TYPE_BIGNUM:
  1306. switch ((int)b & TAG_BITS)
  1307. {
  1308. case TAG_FIXNUM:
  1309. return quotbi(a, b);
  1310. #ifdef COMMON
  1311. case TAG_SFLOAT:
  1312. return quotbs(a, b);
  1313. #endif
  1314. case TAG_NUMBERS:
  1315. { int32 hb = type_of_header(numhdr(b));
  1316. switch (hb)
  1317. {
  1318. case TYPE_BIGNUM:
  1319. return quotbb(a, b);
  1320. #ifdef COMMON
  1321. case TYPE_RATNUM:
  1322. return quotbr(a, b);
  1323. case TYPE_COMPLEX_NUM:
  1324. return quotbc(a, b);
  1325. #endif
  1326. default:
  1327. return aerror1("bad arg for quotient", b);
  1328. }
  1329. }
  1330. case TAG_BOXFLOAT:
  1331. return quotbf(a, b);
  1332. default:
  1333. return aerror1("bad arg for quotient", b);
  1334. }
  1335. #ifdef COMMON
  1336. case TYPE_RATNUM:
  1337. switch ((int)b & TAG_BITS)
  1338. {
  1339. case TAG_FIXNUM:
  1340. return quotri(a, b);
  1341. case TAG_SFLOAT:
  1342. return quotrs(a, b);
  1343. case TAG_NUMBERS:
  1344. { int32 hb = type_of_header(numhdr(b));
  1345. switch (hb)
  1346. {
  1347. case TYPE_BIGNUM:
  1348. return quotrb(a, b);
  1349. case TYPE_RATNUM:
  1350. return quotrr(a, b);
  1351. case TYPE_COMPLEX_NUM:
  1352. return quotrc(a, b);
  1353. default:
  1354. return aerror1("bad arg for quotient", b);
  1355. }
  1356. }
  1357. case TAG_BOXFLOAT:
  1358. return quotrf(a, b);
  1359. default:
  1360. return aerror1("bad arg for quotient", b);
  1361. }
  1362. case TYPE_COMPLEX_NUM:
  1363. switch ((int)b & TAG_BITS)
  1364. {
  1365. case TAG_FIXNUM:
  1366. return quotci(a, b);
  1367. case TAG_SFLOAT:
  1368. return quotcs(a, b);
  1369. case TAG_NUMBERS:
  1370. { int32 hb = type_of_header(numhdr(b));
  1371. switch (hb)
  1372. {
  1373. case TYPE_BIGNUM:
  1374. return quotcb(a, b);
  1375. case TYPE_RATNUM:
  1376. return quotcr(a, b);
  1377. case TYPE_COMPLEX_NUM:
  1378. return quotcc(a, b);
  1379. default:
  1380. return aerror1("bad arg for quotient", b);
  1381. }
  1382. }
  1383. case TAG_BOXFLOAT:
  1384. return quotcf(a, b);
  1385. default:
  1386. return aerror1("bad arg for quotient", b);
  1387. }
  1388. #endif
  1389. default: return aerror1("bad arg for quotient", a);
  1390. }
  1391. }
  1392. case TAG_BOXFLOAT:
  1393. switch ((int)b & TAG_BITS)
  1394. {
  1395. case TAG_FIXNUM:
  1396. return quotfi(a, b);
  1397. #ifdef COMMON
  1398. case TAG_SFLOAT:
  1399. return quotfs(a, b);
  1400. #endif
  1401. case TAG_NUMBERS:
  1402. { int32 hb = type_of_header(numhdr(b));
  1403. switch (hb)
  1404. {
  1405. case TYPE_BIGNUM:
  1406. return quotfb(a, b);
  1407. #ifdef COMMON
  1408. case TYPE_RATNUM:
  1409. return quotfr(a, b);
  1410. case TYPE_COMPLEX_NUM:
  1411. return quotfc(a, b);
  1412. #endif
  1413. default:
  1414. return aerror1("bad arg for quotient", b);
  1415. }
  1416. }
  1417. case TAG_BOXFLOAT:
  1418. return quotff(a, b);
  1419. default:
  1420. return aerror1("bad arg for quotient", b);
  1421. }
  1422. default:
  1423. return aerror1("bad arg for quotient", a);
  1424. }
  1425. }
  1426. #ifdef COMMON
  1427. /*
  1428. * The above version of quot2 is as required for the Standard Lisp QUOTIENT
  1429. * function, and it is called from various internal places in CSL/CCL, eg
  1430. * from within the code for TRUNCATE. Next I have something that will be very
  1431. * similar in behaviour, but which turns quotients of integers into
  1432. * rational numbers when that is necessary.
  1433. */
  1434. /***** Not reconstructed yet!! */
  1435. Lisp_Object CLquot2(Lisp_Object a, Lisp_Object b)
  1436. {
  1437. switch ((int)a & TAG_BITS)
  1438. {
  1439. case TAG_FIXNUM:
  1440. switch ((int)b & TAG_BITS)
  1441. {
  1442. case TAG_FIXNUM:
  1443. /*
  1444. * This is where fixnum / fixnum arithmetic happens - the case I most want to
  1445. * make efficient.
  1446. */
  1447. if (b == fixnum_of_int(0))
  1448. return aerror2("bad arg for /", a, b);
  1449. else
  1450. { int32 r, aa, bb, w;
  1451. aa = int_of_fixnum(a);
  1452. bb = int_of_fixnum(b);
  1453. if (bb < 0) aa = -aa, bb = -bb;
  1454. r = aa % bb;
  1455. if (r == 0) /* Exact division case */
  1456. { r = aa/bb;
  1457. /*
  1458. * the only case where dividing one fixnum by another can lead to
  1459. * a bignum result is (-0x08000000/(-1)) which JUST overflows.
  1460. */
  1461. if (r != 0x08000000) return fixnum_of_int(r);
  1462. else return make_one_word_bignum(r);
  1463. }
  1464. w = bb;
  1465. if (r < 0) r = -r;
  1466. while (r != 0)
  1467. { int32 w1 = w % r;
  1468. w = r;
  1469. r = w1;
  1470. }
  1471. aa = aa / w;
  1472. bb = bb / w;
  1473. return make_ratio(fixnum_of_int(aa), fixnum_of_int(bb));
  1474. }
  1475. #ifdef COMMON
  1476. case TAG_SFLOAT:
  1477. return quotis(a, b);
  1478. #endif
  1479. case TAG_NUMBERS:
  1480. { int32 hb = type_of_header(numhdr(b));
  1481. switch (hb)
  1482. {
  1483. case TYPE_BIGNUM:
  1484. return CLquotib(a, b);
  1485. #ifdef COMMON
  1486. case TYPE_RATNUM:
  1487. return quotir(a, b);
  1488. case TYPE_COMPLEX_NUM:
  1489. return quotic(a, b);
  1490. #endif
  1491. default:
  1492. return aerror1("bad arg for /", b);
  1493. }
  1494. }
  1495. case TAG_BOXFLOAT:
  1496. return quotif(a, b);
  1497. default:
  1498. return aerror1("bad arg for /", b);
  1499. }
  1500. #ifdef COMMON
  1501. case TAG_SFLOAT:
  1502. switch ((int)b & TAG_BITS)
  1503. {
  1504. case TAG_FIXNUM:
  1505. return quotsi(a, b);
  1506. case TAG_SFLOAT:
  1507. { Float_union aa, bb;
  1508. aa.i = a - TAG_SFLOAT;
  1509. bb.i = b - TAG_SFLOAT;
  1510. aa.f = (float) (aa.f / bb.f);
  1511. return (aa.i & ~(int32)0xf) + TAG_SFLOAT;
  1512. }
  1513. case TAG_NUMBERS:
  1514. { int32 hb = type_of_header(numhdr(b));
  1515. switch (hb)
  1516. {
  1517. case TYPE_BIGNUM:
  1518. return quotsb(a, b);
  1519. case TYPE_RATNUM:
  1520. return quotsr(a, b);
  1521. case TYPE_COMPLEX_NUM:
  1522. return quotsc(a, b);
  1523. default:
  1524. return aerror1("bad arg for /", b);
  1525. }
  1526. }
  1527. case TAG_BOXFLOAT:
  1528. return quotsf(a, b);
  1529. default:
  1530. return aerror1("bad arg for /", b);
  1531. }
  1532. #endif
  1533. case TAG_NUMBERS:
  1534. { int32 ha = type_of_header(numhdr(a));
  1535. switch (ha)
  1536. {
  1537. case TYPE_BIGNUM:
  1538. switch ((int)b & TAG_BITS)
  1539. {
  1540. case TAG_FIXNUM:
  1541. return CLquotbi(a, b);
  1542. #ifdef COMMON
  1543. case TAG_SFLOAT:
  1544. return quotbs(a, b);
  1545. #endif
  1546. case TAG_NUMBERS:
  1547. { int32 hb = type_of_header(numhdr(b));
  1548. switch (hb)
  1549. {
  1550. case TYPE_BIGNUM:
  1551. return CLquotbb(a, b);
  1552. #ifdef COMMON
  1553. case TYPE_RATNUM:
  1554. return quotbr(a, b);
  1555. case TYPE_COMPLEX_NUM:
  1556. return quotbc(a, b);
  1557. #endif
  1558. default:
  1559. return aerror1("bad arg for /", b);
  1560. }
  1561. }
  1562. case TAG_BOXFLOAT:
  1563. return quotbf(a, b);
  1564. default:
  1565. return aerror1("bad arg for /", b);
  1566. }
  1567. #ifdef COMMON
  1568. case TYPE_RATNUM:
  1569. switch ((int)b & TAG_BITS)
  1570. {
  1571. case TAG_FIXNUM:
  1572. return quotri(a, b);
  1573. case TAG_SFLOAT:
  1574. return quotrs(a, b);
  1575. case TAG_NUMBERS:
  1576. { int32 hb = type_of_header(numhdr(b));
  1577. switch (hb)
  1578. {
  1579. case TYPE_BIGNUM:
  1580. return quotrb(a, b);
  1581. case TYPE_RATNUM:
  1582. return quotrr(a, b);
  1583. case TYPE_COMPLEX_NUM:
  1584. return quotrc(a, b);
  1585. default:
  1586. return aerror1("bad arg for /", b);
  1587. }
  1588. }
  1589. case TAG_BOXFLOAT:
  1590. return quotrf(a, b);
  1591. default:
  1592. return aerror1("bad arg for /", b);
  1593. }
  1594. case TYPE_COMPLEX_NUM:
  1595. switch ((int)b & TAG_BITS)
  1596. {
  1597. case TAG_FIXNUM:
  1598. return quotci(a, b);
  1599. case TAG_SFLOAT:
  1600. return quotcs(a, b);
  1601. case TAG_NUMBERS:
  1602. { int32 hb = type_of_header(numhdr(b));
  1603. switch (hb)
  1604. {
  1605. case TYPE_BIGNUM:
  1606. return quotcb(a, b);
  1607. case TYPE_RATNUM:
  1608. return quotcr(a, b);
  1609. case TYPE_COMPLEX_NUM:
  1610. return quotcc(a, b);
  1611. default:
  1612. return aerror1("bad arg for /", b);
  1613. }
  1614. }
  1615. case TAG_BOXFLOAT:
  1616. return quotcf(a, b);
  1617. default:
  1618. return aerror1("bad arg for /", b);
  1619. }
  1620. #endif
  1621. default: return aerror1("bad arg for /", a);
  1622. }
  1623. }
  1624. case TAG_BOXFLOAT:
  1625. switch ((int)b & TAG_BITS)
  1626. {
  1627. case TAG_FIXNUM:
  1628. return quotfi(a, b);
  1629. #ifdef COMMON
  1630. case TAG_SFLOAT:
  1631. return quotfs(a, b);
  1632. #endif
  1633. case TAG_NUMBERS:
  1634. { int32 hb = type_of_header(numhdr(b));
  1635. switch (hb)
  1636. {
  1637. case TYPE_BIGNUM:
  1638. return quotfb(a, b);
  1639. #ifdef COMMON
  1640. case TYPE_RATNUM:
  1641. return quotfr(a, b);
  1642. case TYPE_COMPLEX_NUM:
  1643. return quotfc(a, b);
  1644. #endif
  1645. default:
  1646. return aerror1("bad arg for /", b);
  1647. }
  1648. }
  1649. case TAG_BOXFLOAT:
  1650. return quotff(a, b);
  1651. default:
  1652. return aerror1("bad arg for /", b);
  1653. }
  1654. default:
  1655. return aerror1("bad arg for /", a);
  1656. }
  1657. }
  1658. #endif
  1659. /* end of arith03.c */