arith11.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  1. /* arith11.c Copyright (C) 1990-95 Codemist Ltd */
  2. /*
  3. * Arithmetic functions.
  4. * remainder, =,
  5. * minusp, plusp
  6. *
  7. * Version 1.4 November 1990.
  8. */
  9. /* Signature: 3391041c 07-Mar-2000 */
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #include <math.h>
  14. #include "machine.h"
  15. #include "tags.h"
  16. #include "cslerror.h"
  17. #include "externs.h"
  18. #include "arith.h"
  19. #ifdef TIMEOUT
  20. #include "timeout.h"
  21. #endif
  22. Lisp_Object rembi(Lisp_Object a, Lisp_Object b)
  23. {
  24. Lisp_Object nil;
  25. if (b == fixnum_of_int(0)) return aerror2("bad arg for remainder", a, b);
  26. else if (b == fixnum_of_int(1) ||
  27. b == fixnum_of_int(-1)) return fixnum_of_int(0);
  28. quotbn1(a, int_of_fixnum(b));
  29. /*
  30. * If the divisor was a fixnum then the remainder will be a fixnum too.
  31. */
  32. errexit();
  33. return fixnum_of_int(nwork);
  34. }
  35. Lisp_Object rembb(Lisp_Object a, Lisp_Object b)
  36. {
  37. Lisp_Object nil;
  38. quotbb(a, b);
  39. errexit();
  40. return mv_2;
  41. }
  42. #ifdef COMMON
  43. static Lisp_Object remis(Lisp_Object a, Lisp_Object b)
  44. {
  45. return aerror2("bad arg for remainder", a, b);
  46. }
  47. static Lisp_Object remir(Lisp_Object a, Lisp_Object b)
  48. {
  49. return aerror2("bad arg for remainder", a, b);
  50. }
  51. static Lisp_Object remif(Lisp_Object a, Lisp_Object b)
  52. {
  53. return aerror2("bad arg for remainder", a, b);
  54. }
  55. static Lisp_Object remsi(Lisp_Object a, Lisp_Object b)
  56. {
  57. return aerror2("bad arg for remainder", a, b);
  58. }
  59. static Lisp_Object remsb(Lisp_Object a, Lisp_Object b)
  60. {
  61. return aerror2("bad arg for remainder", a, b);
  62. }
  63. static Lisp_Object remsr(Lisp_Object a, Lisp_Object b)
  64. {
  65. return aerror2("bad arg for remainder", a, b);
  66. }
  67. static Lisp_Object remsf(Lisp_Object a, Lisp_Object b)
  68. {
  69. return aerror2("bad arg for remainder", a, b);
  70. }
  71. static Lisp_Object rembs(Lisp_Object a, Lisp_Object b)
  72. {
  73. return aerror2("bad arg for remainder", a, b);
  74. }
  75. static Lisp_Object rembr(Lisp_Object a, Lisp_Object b)
  76. {
  77. return aerror2("bad arg for remainder", a, b);
  78. }
  79. static Lisp_Object rembf(Lisp_Object a, Lisp_Object b)
  80. {
  81. return aerror2("bad arg for remainder", a, b);
  82. }
  83. static Lisp_Object remri(Lisp_Object a, Lisp_Object b)
  84. {
  85. return aerror2("bad arg for remainder", a, b);
  86. }
  87. static Lisp_Object remrs(Lisp_Object a, Lisp_Object b)
  88. {
  89. return aerror2("bad arg for remainder", a, b);
  90. }
  91. static Lisp_Object remrb(Lisp_Object a, Lisp_Object b)
  92. {
  93. return aerror2("bad arg for remainder", a, b);
  94. }
  95. static Lisp_Object remrr(Lisp_Object a, Lisp_Object b)
  96. {
  97. return aerror2("bad arg for remainder", a, b);
  98. }
  99. static Lisp_Object remrf(Lisp_Object a, Lisp_Object b)
  100. {
  101. return aerror2("bad arg for remainder", a, b);
  102. }
  103. static Lisp_Object remfi(Lisp_Object a, Lisp_Object b)
  104. {
  105. return aerror2("bad arg for remainder", a, b);
  106. }
  107. static Lisp_Object remfs(Lisp_Object a, Lisp_Object b)
  108. {
  109. return aerror2("bad arg for remainder", a, b);
  110. }
  111. static Lisp_Object remfb(Lisp_Object a, Lisp_Object b)
  112. {
  113. return aerror2("bad arg for remainder", a, b);
  114. }
  115. static Lisp_Object remfr(Lisp_Object a, Lisp_Object b)
  116. {
  117. return aerror2("bad arg for remainder", a, b);
  118. }
  119. static Lisp_Object remff(Lisp_Object a, Lisp_Object b)
  120. {
  121. return aerror2("bad arg for remainder", a, b);
  122. }
  123. #endif /* COMMON */
  124. Lisp_Object Cremainder(Lisp_Object a, Lisp_Object b)
  125. {
  126. int32 c;
  127. switch ((int)a & TAG_BITS)
  128. {
  129. case TAG_FIXNUM:
  130. switch ((int)b & TAG_BITS)
  131. {
  132. case TAG_FIXNUM:
  133. /*
  134. * This is where fixnum % fixnum arithmetic happens - the case I most want to
  135. * make efficient.
  136. */
  137. if (b == fixnum_of_int(0))
  138. return aerror2("bad arg for remainder", a, b);
  139. /* No overflow is possible in a remaindering operation */
  140. { int32 aa = int_of_fixnum(a);
  141. int32 bb = int_of_fixnum(b);
  142. c = aa % bb;
  143. /*
  144. * C does not specify just what happens when % is used with negative
  145. * operands (except maybe if the division went exactly), so here I do
  146. * some adjusting, assuming that the quotient returned was one of the
  147. * integral values surrounding the exact result.
  148. */
  149. if (aa < 0)
  150. { if (c > 0) c -= bb;
  151. }
  152. else if (c < 0) c += bb;
  153. return fixnum_of_int(c);
  154. }
  155. #ifdef COMMON
  156. /*
  157. * Common Lisp defines a meaning for the remainder function when applied
  158. * to floating point values - so there is a whole pile of mess here to
  159. * support that. Standard Lisp is only concerned with fixnums and
  160. * bignums.
  161. */
  162. case TAG_SFLOAT:
  163. return remis(a, b);
  164. #endif
  165. case TAG_NUMBERS:
  166. { int32 hb = type_of_header(numhdr(b));
  167. switch (hb)
  168. {
  169. case TYPE_BIGNUM:
  170. /*
  171. * When I divide a fixnum a by a bignum b the remainder is a except in
  172. * the case that a = 0xf8000000 and b = 0x08000000 in which case the
  173. * answer is zero.
  174. */
  175. if (int_of_fixnum(a) == fix_mask &&
  176. bignum_length(b) == 8 &&
  177. bignum_digits(b)[0] == 0x08000000)
  178. return fixnum_of_int(0);
  179. else return a;
  180. #ifdef COMMON
  181. case TYPE_RATNUM:
  182. return remir(a, b);
  183. #endif
  184. default:
  185. return aerror1("Bad arg for remainder", b);
  186. }
  187. }
  188. #ifdef COMMON
  189. case TAG_BOXFLOAT:
  190. return remif(a, b);
  191. #else
  192. case TAG_BOXFLOAT:
  193. { double v = (double) int_of_fixnum(a);
  194. double u = float_of_number(b);
  195. v = v - (v/u)*u;
  196. return make_boxfloat(v, TYPE_DOUBLE_FLOAT);
  197. }
  198. #endif
  199. default:
  200. return aerror1("Bad arg for remainder", b);
  201. }
  202. #ifdef COMMON
  203. case TAG_SFLOAT:
  204. switch ((int)b & TAG_BITS)
  205. {
  206. case TAG_FIXNUM:
  207. return remsi(a, b);
  208. case TAG_SFLOAT:
  209. { Float_union aa, bb;
  210. aa.i = a - TAG_SFLOAT;
  211. bb.i = b - TAG_SFLOAT;
  212. aa.f = (float) (aa.f + bb.f);
  213. return (aa.i & ~(int32)0xf) + TAG_SFLOAT;
  214. }
  215. case TAG_NUMBERS:
  216. { int32 hb = type_of_header(numhdr(b));
  217. switch (hb)
  218. {
  219. case TYPE_BIGNUM:
  220. return remsb(a, b);
  221. case TYPE_RATNUM:
  222. return remsr(a, b);
  223. default:
  224. return aerror1("Bad arg for remainder", b);
  225. }
  226. }
  227. case TAG_BOXFLOAT:
  228. return remsf(a, b);
  229. default:
  230. return aerror1("Bad arg for remainder", b);
  231. }
  232. #endif
  233. case TAG_NUMBERS:
  234. { int32 ha = type_of_header(numhdr(a));
  235. switch (ha)
  236. {
  237. case TYPE_BIGNUM:
  238. switch ((int)b & TAG_BITS)
  239. {
  240. case TAG_FIXNUM:
  241. return rembi(a, b);
  242. #ifdef COMMON
  243. case TAG_SFLOAT:
  244. return rembs(a, b);
  245. #endif
  246. case TAG_NUMBERS:
  247. { int32 hb = type_of_header(numhdr(b));
  248. switch (hb)
  249. {
  250. case TYPE_BIGNUM:
  251. return rembb(a, b);
  252. #ifdef COMMON
  253. case TYPE_RATNUM:
  254. return rembr(a, b);
  255. #endif
  256. default:
  257. return aerror1("Bad arg for remainder", b);
  258. }
  259. }
  260. #ifdef COMMON
  261. case TAG_BOXFLOAT:
  262. return rembf(a, b);
  263. #endif
  264. default:
  265. return aerror1("Bad arg for remainder", b);
  266. }
  267. #ifdef COMMON
  268. case TYPE_RATNUM:
  269. switch ((int)b & TAG_BITS)
  270. {
  271. case TAG_FIXNUM:
  272. return remri(a, b);
  273. case TAG_SFLOAT:
  274. return remrs(a, b);
  275. case TAG_NUMBERS:
  276. { int32 hb = type_of_header(numhdr(b));
  277. switch (hb)
  278. {
  279. case TYPE_BIGNUM:
  280. return remrb(a, b);
  281. case TYPE_RATNUM:
  282. return remrr(a, b);
  283. default:
  284. return aerror1("Bad arg for remainder", b);
  285. }
  286. }
  287. case TAG_BOXFLOAT:
  288. return remrf(a, b);
  289. default:
  290. return aerror1("Bad arg for remainder", b);
  291. }
  292. #endif
  293. default: return aerror1("Bad arg for remainder", a);
  294. }
  295. }
  296. case TAG_BOXFLOAT:
  297. switch ((int)b & TAG_BITS)
  298. {
  299. #ifndef COMMON
  300. case TAG_FIXNUM:
  301. { double u = (double) int_of_fixnum(b);
  302. double v = float_of_number(a);
  303. v = v - (v/u)*u;
  304. return make_boxfloat(v, TYPE_DOUBLE_FLOAT);
  305. }
  306. case TAG_BOXFLOAT:
  307. { double u = float_of_number(b);
  308. double v = float_of_number(a);
  309. v = v - (v/u)*u;
  310. return make_boxfloat(v, TYPE_DOUBLE_FLOAT);
  311. }
  312. default:
  313. return aerror1("Bad arg for remainder", b);
  314. #else
  315. case TAG_FIXNUM:
  316. return remfi(a, b);
  317. case TAG_SFLOAT:
  318. return remfs(a, b);
  319. case TAG_NUMBERS:
  320. { int32 hb = type_of_header(numhdr(b));
  321. switch (hb)
  322. {
  323. case TYPE_BIGNUM:
  324. return remfb(a, b);
  325. case TYPE_RATNUM:
  326. return remfr(a, b);
  327. default:
  328. return aerror1("Bad arg for remainder", b);
  329. }
  330. }
  331. case TAG_BOXFLOAT:
  332. return remff(a, b);
  333. default:
  334. return aerror1("Bad arg for remainder", b);
  335. #endif
  336. }
  337. default:
  338. return aerror1("Bad arg for remainder", a);
  339. }
  340. }
  341. /*
  342. * In the cases that I expect to be most speed-critical I will
  343. * implement "mod" directly. But in a load of other cases I will just
  344. * activate the existing "remainder" code and then make a few final
  345. * adjustments. This MAY lead to error messages (on modulus by zero)
  346. * mentioning remainder rather than mod....
  347. * I will leave in the whole structure of separate functions for each
  348. * case since that will be useful if I ever need to come back here and
  349. * fine-tune more of the type-combinations. As a first pass I give
  350. * special treatment to (fixnum,fixnum) and (bignum,fixnum)
  351. */
  352. static Lisp_Object mod_by_rem(Lisp_Object a, Lisp_Object b)
  353. {
  354. Lisp_Object nil;
  355. CSLbool sb = minusp(b);
  356. errexit();
  357. a = Cremainder(a, b); /* Repeats dispatch on argument type. Sorry */
  358. errexit();
  359. if (sb)
  360. { if (plusp(a))
  361. { errexit();
  362. a = plus2(a, b);
  363. }
  364. }
  365. else if (minusp(a))
  366. { errexit();
  367. a = plus2(a, b);
  368. }
  369. return a;
  370. }
  371. static Lisp_Object modib(Lisp_Object a, Lisp_Object b)
  372. {
  373. return mod_by_rem(a, b);
  374. }
  375. static Lisp_Object modbi(Lisp_Object a, Lisp_Object b)
  376. {
  377. Lisp_Object nil = C_nil;
  378. int32 bb = int_of_fixnum(b);
  379. if (b == fixnum_of_int(0)) return aerror2("bad arg for mod", a, b);
  380. if (bb == 1 || bb == -1) nwork = 0;
  381. else quotbn1(a, bb);
  382. /*
  383. * If the divisor was a fixnum then the remainder will be a fixnum too.
  384. */
  385. errexit();
  386. if (bb < 0)
  387. { if (nwork > 0) nwork += bb;
  388. }
  389. else if (nwork < 0) nwork += bb;
  390. return fixnum_of_int(nwork);
  391. }
  392. static Lisp_Object modbb(Lisp_Object a, Lisp_Object b)
  393. {
  394. return mod_by_rem(a, b);
  395. }
  396. #ifdef COMMON
  397. static Lisp_Object modis(Lisp_Object a, Lisp_Object b)
  398. {
  399. return mod_by_rem(a, b);
  400. }
  401. static Lisp_Object modir(Lisp_Object a, Lisp_Object b)
  402. {
  403. return mod_by_rem(a, b);
  404. }
  405. static Lisp_Object modif(Lisp_Object a, Lisp_Object b)
  406. {
  407. return mod_by_rem(a, b);
  408. }
  409. static Lisp_Object modsi(Lisp_Object a, Lisp_Object b)
  410. {
  411. return mod_by_rem(a, b);
  412. }
  413. static Lisp_Object modsb(Lisp_Object a, Lisp_Object b)
  414. {
  415. return mod_by_rem(a, b);
  416. }
  417. static Lisp_Object modsr(Lisp_Object a, Lisp_Object b)
  418. {
  419. return mod_by_rem(a, b);
  420. }
  421. static Lisp_Object modsf(Lisp_Object a, Lisp_Object b)
  422. {
  423. return mod_by_rem(a, b);
  424. }
  425. static Lisp_Object modbs(Lisp_Object a, Lisp_Object b)
  426. {
  427. return mod_by_rem(a, b);
  428. }
  429. static Lisp_Object modbr(Lisp_Object a, Lisp_Object b)
  430. {
  431. return mod_by_rem(a, b);
  432. }
  433. static Lisp_Object modbf(Lisp_Object a, Lisp_Object b)
  434. {
  435. return mod_by_rem(a, b);
  436. }
  437. static Lisp_Object modri(Lisp_Object a, Lisp_Object b)
  438. {
  439. return mod_by_rem(a, b);
  440. }
  441. static Lisp_Object modrs(Lisp_Object a, Lisp_Object b)
  442. {
  443. return mod_by_rem(a, b);
  444. }
  445. static Lisp_Object modrb(Lisp_Object a, Lisp_Object b)
  446. {
  447. return mod_by_rem(a, b);
  448. }
  449. static Lisp_Object modrr(Lisp_Object a, Lisp_Object b)
  450. {
  451. return mod_by_rem(a, b);
  452. }
  453. static Lisp_Object modrf(Lisp_Object a, Lisp_Object b)
  454. {
  455. return mod_by_rem(a, b);
  456. }
  457. static Lisp_Object modfi(Lisp_Object a, Lisp_Object b)
  458. {
  459. return mod_by_rem(a, b);
  460. }
  461. static Lisp_Object modfs(Lisp_Object a, Lisp_Object b)
  462. {
  463. return mod_by_rem(a, b);
  464. }
  465. static Lisp_Object modfb(Lisp_Object a, Lisp_Object b)
  466. {
  467. return mod_by_rem(a, b);
  468. }
  469. static Lisp_Object modfr(Lisp_Object a, Lisp_Object b)
  470. {
  471. return mod_by_rem(a, b);
  472. }
  473. static Lisp_Object ccl_modff(Lisp_Object a, Lisp_Object b)
  474. {
  475. return mod_by_rem(a, b);
  476. }
  477. #endif /* COMMON */
  478. Lisp_Object modulus(Lisp_Object a, Lisp_Object b)
  479. {
  480. switch ((int)a & TAG_BITS)
  481. {
  482. case TAG_FIXNUM:
  483. switch ((int)b & TAG_BITS)
  484. {
  485. case TAG_FIXNUM:
  486. /*
  487. * This is where fixnum % fixnum arithmetic happens - the case I most want to
  488. * make efficient.
  489. */
  490. { int32 p = int_of_fixnum(a);
  491. int32 q = int_of_fixnum(b);
  492. if (q == 0) return aerror2("bad arg for mod", a, b);
  493. p = p % q;
  494. if (q < 0)
  495. { if (p > 0) p += q;
  496. }
  497. else if (p < 0) p += q;
  498. /* No overflow is possible in a modulus operation */
  499. return fixnum_of_int(p);
  500. }
  501. #ifdef COMMON
  502. /*
  503. * Common Lisp defines a meaning for the modulus function when applied
  504. * to floating point values - so there is a whole pile of mess here to
  505. * support that. Standard Lisp is only concerned with fixnums and
  506. * bignums.
  507. */
  508. case TAG_SFLOAT:
  509. return modis(a, b);
  510. #endif
  511. case TAG_NUMBERS:
  512. { int32 hb = type_of_header(numhdr(b));
  513. switch (hb)
  514. {
  515. case TYPE_BIGNUM:
  516. return modib(a, b);
  517. #ifdef COMMON
  518. case TYPE_RATNUM:
  519. return modir(a, b);
  520. #endif
  521. default:
  522. return aerror1("Bad arg for mod", b);
  523. }
  524. }
  525. #ifdef COMMON
  526. case TAG_BOXFLOAT:
  527. return modif(a, b);
  528. #endif
  529. default:
  530. return aerror1("Bad arg for mod", b);
  531. }
  532. #ifdef COMMON
  533. case TAG_SFLOAT:
  534. switch ((int)b & TAG_BITS)
  535. {
  536. case TAG_FIXNUM:
  537. return modsi(a, b);
  538. case TAG_SFLOAT:
  539. { Float_union aa, bb;
  540. aa.i = a - TAG_SFLOAT;
  541. bb.i = b - TAG_SFLOAT;
  542. aa.f = (float) (aa.f + bb.f);
  543. return (aa.i & ~(int32)0xf) + TAG_SFLOAT;
  544. }
  545. case TAG_NUMBERS:
  546. { int32 hb = type_of_header(numhdr(b));
  547. switch (hb)
  548. {
  549. case TYPE_BIGNUM:
  550. return modsb(a, b);
  551. case TYPE_RATNUM:
  552. return modsr(a, b);
  553. default:
  554. return aerror1("Bad arg for mod", b);
  555. }
  556. }
  557. case TAG_BOXFLOAT:
  558. return modsf(a, b);
  559. default:
  560. return aerror1("Bad arg for mod", b);
  561. }
  562. #endif
  563. case TAG_NUMBERS:
  564. { int32 ha = type_of_header(numhdr(a));
  565. switch (ha)
  566. {
  567. case TYPE_BIGNUM:
  568. switch ((int)b & TAG_BITS)
  569. {
  570. case TAG_FIXNUM:
  571. return modbi(a, b);
  572. #ifdef COMMON
  573. case TAG_SFLOAT:
  574. return modbs(a, b);
  575. #endif
  576. case TAG_NUMBERS:
  577. { int32 hb = type_of_header(numhdr(b));
  578. switch (hb)
  579. {
  580. case TYPE_BIGNUM:
  581. return modbb(a, b);
  582. #ifdef COMMON
  583. case TYPE_RATNUM:
  584. return modbr(a, b);
  585. #endif
  586. default:
  587. return aerror1("Bad arg for mod", b);
  588. }
  589. }
  590. #ifdef COMMON
  591. case TAG_BOXFLOAT:
  592. return modbf(a, b);
  593. #endif
  594. default:
  595. return aerror1("Bad arg for mod", b);
  596. }
  597. #ifdef COMMON
  598. case TYPE_RATNUM:
  599. switch ((int)b & TAG_BITS)
  600. {
  601. case TAG_FIXNUM:
  602. return modri(a, b);
  603. case TAG_SFLOAT:
  604. return modrs(a, b);
  605. case TAG_NUMBERS:
  606. { int32 hb = type_of_header(numhdr(b));
  607. switch (hb)
  608. {
  609. case TYPE_BIGNUM:
  610. return modrb(a, b);
  611. case TYPE_RATNUM:
  612. return modrr(a, b);
  613. default:
  614. return aerror1("Bad arg for mod", b);
  615. }
  616. }
  617. case TAG_BOXFLOAT:
  618. return modrf(a, b);
  619. default:
  620. return aerror1("Bad arg for mod", b);
  621. }
  622. #endif
  623. default: return aerror1("Bad arg for mod", a);
  624. }
  625. }
  626. #ifdef COMMON
  627. case TAG_BOXFLOAT:
  628. switch ((int)b & TAG_BITS)
  629. {
  630. case TAG_FIXNUM:
  631. return modfi(a, b);
  632. case TAG_SFLOAT:
  633. return modfs(a, b);
  634. case TAG_NUMBERS:
  635. { int32 hb = type_of_header(numhdr(b));
  636. switch (hb)
  637. {
  638. case TYPE_BIGNUM:
  639. return modfb(a, b);
  640. case TYPE_RATNUM:
  641. return modfr(a, b);
  642. default:
  643. return aerror1("Bad arg for mod", b);
  644. }
  645. }
  646. case TAG_BOXFLOAT:
  647. return ccl_modff(a, b);
  648. default:
  649. return aerror1("Bad arg for mod", b);
  650. }
  651. #endif
  652. default:
  653. return aerror1("Bad arg for mod", a);
  654. }
  655. }
  656. CSLbool zerop(Lisp_Object a)
  657. {
  658. switch ((int)a & TAG_BITS)
  659. {
  660. case TAG_FIXNUM:
  661. return (a == fixnum_of_int(0));
  662. #ifdef COMMON
  663. case TAG_NUMBERS:
  664. /* #C(r i) must satisfy zerop is r and i both do */
  665. if (is_complex(a) && zerop(real_part(a)))
  666. return zerop(imag_part(a));
  667. else return NO;
  668. case TAG_SFLOAT:
  669. /*
  670. * The code here assumes that the the floating point number zero
  671. * is represented by a zero bit-pattern... see onep() for a more
  672. * cautious way of coding things.
  673. */
  674. return ((a & 0x7ffffff8) == 0); /* Strip sign bit as well as tags */
  675. #endif
  676. case TAG_BOXFLOAT:
  677. return (float_of_number(a) == 0.0);
  678. default:
  679. return NO;
  680. }
  681. }
  682. CSLbool onep(Lisp_Object a)
  683. {
  684. switch ((int)a & TAG_BITS)
  685. {
  686. case TAG_FIXNUM:
  687. return (a == fixnum_of_int(1));
  688. #ifdef COMMON
  689. case TAG_NUMBERS:
  690. /* #C(r i) must satisfy onep(r) and zerop(i) */
  691. if (is_complex(a) && onep(real_part(a)))
  692. return zerop(imag_part(a));
  693. else return NO;
  694. case TAG_SFLOAT:
  695. { Float_union w;
  696. w.f = (float)1.0;
  697. return (a == (w.i & ~(int32)0xf) + TAG_SFLOAT);
  698. }
  699. #endif
  700. case TAG_BOXFLOAT:
  701. return (float_of_number(a) == 1.0);
  702. default:
  703. return NO;
  704. }
  705. }
  706. /*
  707. * sign testing
  708. */
  709. CSLbool minusp(Lisp_Object a)
  710. {
  711. switch ((int)a & TAG_BITS)
  712. {
  713. case TAG_FIXNUM:
  714. return ((int32)a < 0);
  715. #ifdef COMMON
  716. case TAG_SFLOAT:
  717. { Float_union aa;
  718. aa.i = a - TAG_SFLOAT;
  719. return (aa.f < 0.0);
  720. }
  721. #endif
  722. case TAG_NUMBERS:
  723. { int32 ha = type_of_header(numhdr(a));
  724. switch (ha)
  725. {
  726. case TYPE_BIGNUM:
  727. { int32 l = (bignum_length(a) >> 2) - 2;
  728. return ((int32)bignum_digits(a)[l] < (int32)0);
  729. }
  730. #ifdef COMMON
  731. case TYPE_RATNUM:
  732. return minusp(numerator(a));
  733. #endif
  734. default:
  735. aerror1("Bad arg for minusp", a);
  736. return 0;
  737. }
  738. }
  739. case TAG_BOXFLOAT:
  740. { double d = float_of_number(a);
  741. return (d < 0.0);
  742. }
  743. default:
  744. aerror1("Bad arg for minusp", a);
  745. return 0;
  746. }
  747. }
  748. CSLbool plusp(Lisp_Object a)
  749. {
  750. switch ((int)a & TAG_BITS)
  751. {
  752. case TAG_FIXNUM:
  753. return (a > fixnum_of_int(0));
  754. #ifdef COMMON
  755. case TAG_SFLOAT:
  756. { Float_union aa;
  757. aa.i = a - TAG_SFLOAT;
  758. return (aa.f > 0.0);
  759. }
  760. #endif
  761. case TAG_NUMBERS:
  762. { int32 ha = type_of_header(numhdr(a));
  763. switch (ha)
  764. {
  765. case TYPE_BIGNUM:
  766. { int32 l = (bignum_length(a) >> 2) - 2;
  767. /* This is OK because a bignum can never have the value zero */
  768. return ((int32)bignum_digits(a)[l] >= (int32)0);
  769. }
  770. #ifdef COMMON
  771. case TYPE_RATNUM:
  772. return plusp(numerator(a));
  773. #endif
  774. default:
  775. aerror1("Bad arg for plusp", a);
  776. return 0;
  777. }
  778. }
  779. case TAG_BOXFLOAT:
  780. { double d = float_of_number(a);
  781. return (d > 0.0);
  782. }
  783. default:
  784. aerror1("Bad arg for plusp", a);
  785. return 0;
  786. }
  787. }
  788. /*
  789. * Numeric equality - note that comparisons involving non-numbers
  790. * are errors here (unlike the position in eql, equal, equalp). Also
  791. * this must be coded so that it never provokes garbage collection.
  792. */
  793. #ifdef COMMON
  794. static CSLbool numeqis(Lisp_Object a, Lisp_Object b)
  795. {
  796. Float_union bb;
  797. bb.i = b - TAG_SFLOAT;
  798. return ((double)int_of_fixnum(a) == (double)bb.f);
  799. }
  800. static CSLbool numeqic(Lisp_Object a, Lisp_Object b)
  801. {
  802. if (!zerop(imag_part(b))) return NO;
  803. else return numeq2(a, real_part(b));
  804. }
  805. #endif
  806. #define numeqif(a,b) ((double)int_of_fixnum(a) == float_of_number(b))
  807. #ifdef COMMON
  808. #define numeqsi(a, b) numeqis(b, a)
  809. #endif
  810. static CSLbool numeqsb(Lisp_Object a, Lisp_Object b)
  811. /*
  812. * This is coded to allow comparison of any floating type
  813. * with a bignum
  814. */
  815. {
  816. double d = float_of_number(a), d1;
  817. int x;
  818. int32 w, len;
  819. unsigned32 u;
  820. if (-1.0e8 < d && d < 1.0e8) return NO; /* fixnum range (approx) */
  821. len = (bignum_length(b) >> 2) - 2;
  822. if (len == 0) /* One word bignums can be treated specially */
  823. { int32 v = bignum_digits(b)[0];
  824. return (d == (double)v);
  825. }
  826. d1 = frexp(d, &x); /* separate exponent from mantissa */
  827. if (d1 == 1.0) d1 = 0.5, x++; /* For Zortech */
  828. /* The exponent x must be positive here, hence the % operation is defined */
  829. d1 = ldexp(d1, x % 31);
  830. /*
  831. * At most 3 words in the bignum may contain nonzero data - I subtract
  832. * the (double) value of those bits off and check that (a) the floating
  833. * result left is zero and (b) there are no more bits left.
  834. */
  835. x = x / 31;
  836. if (x != len) return NO;
  837. w = bignum_digits(b)[len];
  838. d1 = (d1 - (double)w) * TWO_31;
  839. u = bignum_digits(b)[--len];
  840. d1 = (d1 - (double)u) * TWO_31;
  841. if (len > 0)
  842. { u = bignum_digits(b)[--len];
  843. d1 = d1 - (double)u;
  844. }
  845. if (d1 != 0.0) return NO;
  846. while (--len >= 0)
  847. if (bignum_digits(b)[len] != 0) return NO;
  848. return YES;
  849. }
  850. #ifdef COMMON
  851. static CSLbool numeqsr(Lisp_Object a, Lisp_Object b)
  852. /*
  853. * Here I will rely somewhat on the use of IEEE floating point values
  854. * (an in particular the weaker supposition that I have floating point
  855. * with a binary radix). Then for equality the denominator of b must
  856. * be a power of 2, which I can test for and then account for.
  857. */
  858. {
  859. Lisp_Object nb = numerator(b), db = denominator(b);
  860. double d = float_of_number(a), d1;
  861. int x;
  862. int32 dx, w, len;
  863. unsigned32 u, bit;
  864. /*
  865. * first I will check that db (which will be positive) is a power of 2,
  866. * and set dx to indicate what power of two it is.
  867. * Note that db != 0 and that one of the top two words of a bignum
  868. * must be nonzero (for normalisation) so I end up with a nonzero
  869. * value in the variable 'bit'
  870. */
  871. if (is_fixnum(db))
  872. { bit = int_of_fixnum(db);
  873. w = bit;
  874. if (w != (w & (-w))) return NO; /* not a power of 2 */
  875. dx = 0;
  876. }
  877. else if (is_numbers(db) && is_bignum(db))
  878. { int32 lenb = (bignum_length(db) >> 2) - 2;
  879. bit = bignum_digits(db)[lenb];
  880. /*
  881. * I need to cope with bignums where the leading digits is zero because
  882. * the 0x80000000 bit of the next word down is 1. To do this I treat
  883. * the number as having one fewer digits.
  884. */
  885. if (bit == 0) bit = bignum_digits(db)[--lenb];
  886. w = bit;
  887. if (w != (w & (-w))) return NO; /* not a power of 2 */
  888. dx = 31*lenb;
  889. while (--lenb >= 0) /* check that the rest of db is zero */
  890. if (bignum_digits(db)[lenb] != 0) return NO;
  891. }
  892. else return NO; /* Odd - what type IS db here? Maybe error. */
  893. if ((bit & 0xffffU) == 0) dx += 16, bit = bit >> 16;
  894. if ((bit & 0xff) == 0) dx += 8, bit = bit >> 8;
  895. if ((bit & 0xf) == 0) dx += 4, bit = bit >> 4;
  896. if ((bit & 0x3) == 0) dx += 2, bit = bit >> 2;
  897. if ((bit & 0x1) == 0) dx += 1;
  898. if (is_fixnum(nb))
  899. { double d1 = (double)int_of_fixnum(nb);
  900. /*
  901. * The ldexp on the next line could potentially underflow. In that case C
  902. * defines that the result 0.0 be returned. To avoid trouble I put in a
  903. * special test the relies on that fact that a value represented as a rational
  904. * would not have been zero.
  905. */
  906. if (dx > 10000) return NO; /* Avoid gross underflow */
  907. d1 = ldexp(d1, (int)-dx);
  908. return (d == d1 && d != 0.0);
  909. }
  910. len = (bignum_length(nb) >> 2) - 2;
  911. if (len == 0) /* One word bignums can be treated specially */
  912. { int32 v = bignum_digits(nb)[0];
  913. double d1;
  914. if (dx > 10000) return NO; /* Avoid gross underflow */
  915. d1 = ldexp((double)v, (int)-dx);
  916. return (d == d1 && d != 0.0);
  917. }
  918. d1 = frexp(d, &x); /* separate exponent from mantissa */
  919. if (d1 == 1.0) d1 = 0.5, x++; /* For Zortech */
  920. dx += x; /* adjust to allow for the denominator */
  921. d1 = ldexp(d1, (int)(dx % 31));
  922. /* can neither underflow nor overflow here */
  923. /*
  924. * At most 3 words in the bignum may contain nonzero data - I subtract
  925. * the (double) value of those bits off and check that (a) the floating
  926. * result left is zero and (b) there are no more bits left.
  927. */
  928. dx = dx / 31;
  929. if (dx != len) return NO;
  930. w = bignum_digits(nb)[len];
  931. d1 = (d1 - (double)w) * TWO_31;
  932. u = bignum_digits(nb)[--len];
  933. d1 = (d1 - (double)u) * TWO_31;
  934. if (len > 0)
  935. { u = bignum_digits(nb)[--len];
  936. d1 = d1 - (double)u;
  937. }
  938. if (d1 != 0.0) return NO;
  939. while (--len >= 0)
  940. if (bignum_digits(nb)[len] != 0) return NO;
  941. return YES;
  942. }
  943. #define numeqsc(a, b) numeqic(a, b)
  944. static CSLbool numeqsf(Lisp_Object a, Lisp_Object b)
  945. {
  946. Float_union aa;
  947. aa.i = a - TAG_SFLOAT;
  948. return ((double)aa.f == float_of_number(b));
  949. }
  950. #define numeqbs(a, b) numeqsb(b, a)
  951. #endif
  952. static CSLbool numeqbb(Lisp_Object a, Lisp_Object b)
  953. {
  954. int32 la = bignum_length(a);
  955. if (la != (int32)bignum_length(b)) return NO;
  956. la = (la >> 2) - 2;
  957. while (la >= 0)
  958. { if (bignum_digits(a)[la] != bignum_digits(b)[la]) return NO;
  959. else la--;
  960. }
  961. return YES;
  962. }
  963. #ifdef COMMON
  964. #define numeqbc(a, b) numeqic(a, b)
  965. #endif
  966. #define numeqbf(a, b) numeqsb(b, a)
  967. #ifdef COMMON
  968. #define numeqrs(a, b) numeqsr(b, a)
  969. static CSLbool numeqrr(Lisp_Object a, Lisp_Object b)
  970. {
  971. return numeq2(numerator(a), numerator(b)) &&
  972. numeq2(denominator(a), denominator(b));
  973. }
  974. #define numeqrc(a, b) numeqic(a, b)
  975. #define numeqrf(a, b) numeqsr(b, a)
  976. #define numeqci(a, b) numeqic(b, a)
  977. #define numeqcs(a, b) numeqic(b, a)
  978. #define numeqcb(a, b) numeqic(b, a)
  979. #define numeqcr(a, b) numeqic(b, a)
  980. static CSLbool numeqcc(Lisp_Object a, Lisp_Object b)
  981. {
  982. return numeq2(real_part(a), real_part(b)) &&
  983. numeq2(imag_part(a), imag_part(b));
  984. }
  985. #define numeqcf(a, b) numeqic(b, a)
  986. #endif
  987. #define numeqfi(a, b) numeqif(b, a)
  988. #ifdef COMMON
  989. #define numeqfs(a, b) numeqsf(b, a)
  990. #endif
  991. #define numeqfb(a, b) numeqbf(b, a)
  992. #ifdef COMMON
  993. #define numeqfr(a, b) numeqrf(b, a)
  994. #define numeqfc(a, b) numeqic(a, b)
  995. #endif
  996. static CSLbool numeqff(Lisp_Object a, Lisp_Object b)
  997. {
  998. return (float_of_number(a) == float_of_number(b));
  999. }
  1000. /*
  1001. * This comparison must signal an error on non-numeric operands in
  1002. * Common Lisp mode, but behave as EQ in CSL mode.
  1003. */
  1004. #ifdef COMMON
  1005. # define differenta aerror1("Bad arg for =", a); return 0
  1006. # define differentb aerror1("Bad arg for =", b); return 0
  1007. #else
  1008. # define differenta return NO
  1009. # define differentb return NO
  1010. #endif
  1011. CSLbool numeq2(Lisp_Object a, Lisp_Object b)
  1012. {
  1013. #ifndef COMMON
  1014. if (a == b) return YES;
  1015. #endif
  1016. switch ((int)a & TAG_BITS)
  1017. {
  1018. case TAG_FIXNUM:
  1019. switch ((int)b & TAG_BITS)
  1020. {
  1021. case TAG_FIXNUM:
  1022. #ifdef COMMON
  1023. return (a == b);
  1024. #else
  1025. return NO;
  1026. #endif
  1027. #ifdef COMMON
  1028. case TAG_SFLOAT:
  1029. return numeqis(a, b);
  1030. #endif
  1031. case TAG_NUMBERS:
  1032. { int32 hb = type_of_header(numhdr(b));
  1033. switch (hb)
  1034. {
  1035. case TYPE_BIGNUM:
  1036. return 0;
  1037. #ifdef COMMON
  1038. case TYPE_RATNUM:
  1039. return 0;
  1040. case TYPE_COMPLEX_NUM:
  1041. return numeqic(a, b); /* (= 2 #C(2.0 0.0))? Yuk */
  1042. #endif
  1043. default:
  1044. differentb;
  1045. }
  1046. }
  1047. case TAG_BOXFLOAT:
  1048. return numeqif(a, b);
  1049. default:
  1050. differentb;
  1051. }
  1052. #ifdef COMMON
  1053. case TAG_SFLOAT:
  1054. switch ((int)b & TAG_BITS)
  1055. {
  1056. case TAG_FIXNUM:
  1057. return numeqsi(a, b);
  1058. case TAG_SFLOAT:
  1059. return (a == b) ||
  1060. (a == TAG_SFLOAT && b == TAG_SFLOAT|0x80000000) ||
  1061. (a == TAG_SFLOAT|0x80000000 && b == TAG_SFLOAT); /* !!! */
  1062. case TAG_NUMBERS:
  1063. { int32 hb = type_of_header(numhdr(b));
  1064. switch (hb)
  1065. {
  1066. case TYPE_BIGNUM:
  1067. return numeqsb(a, b);
  1068. case TYPE_RATNUM:
  1069. return numeqsr(a, b);
  1070. case TYPE_COMPLEX_NUM:
  1071. return numeqsc(a, b);
  1072. default:
  1073. differentb;
  1074. }
  1075. }
  1076. case TAG_BOXFLOAT:
  1077. return numeqsf(a, b);
  1078. default:
  1079. differentb;
  1080. }
  1081. #endif
  1082. case TAG_NUMBERS:
  1083. { int32 ha = type_of_header(numhdr(a));
  1084. switch (ha)
  1085. {
  1086. case TYPE_BIGNUM:
  1087. switch ((int)b & TAG_BITS)
  1088. {
  1089. case TAG_FIXNUM:
  1090. return 0;
  1091. #ifdef COMMON
  1092. case TAG_SFLOAT:
  1093. return numeqbs(a, b);
  1094. #endif
  1095. case TAG_NUMBERS:
  1096. { int32 hb = type_of_header(numhdr(b));
  1097. switch (hb)
  1098. {
  1099. case TYPE_BIGNUM:
  1100. return numeqbb(a, b);
  1101. #ifdef COMMON
  1102. case TYPE_RATNUM:
  1103. return 0;
  1104. case TYPE_COMPLEX_NUM:
  1105. return numeqbc(a, b);
  1106. #endif
  1107. default:
  1108. differentb;
  1109. }
  1110. }
  1111. case TAG_BOXFLOAT:
  1112. return numeqbf(a, b);
  1113. default:
  1114. differentb;
  1115. }
  1116. #ifdef COMMON
  1117. case TYPE_RATNUM:
  1118. switch ((int)b & TAG_BITS)
  1119. {
  1120. case TAG_FIXNUM:
  1121. return 0;
  1122. case TAG_SFLOAT:
  1123. return numeqrs(a, b);
  1124. case TAG_NUMBERS:
  1125. { int32 hb = type_of_header(numhdr(b));
  1126. switch (hb)
  1127. {
  1128. case TYPE_BIGNUM:
  1129. return 0;
  1130. case TYPE_RATNUM:
  1131. return numeqrr(a, b);
  1132. case TYPE_COMPLEX_NUM:
  1133. return numeqrc(a, b);
  1134. default:
  1135. differentb;
  1136. }
  1137. }
  1138. case TAG_BOXFLOAT:
  1139. return numeqrf(a, b);
  1140. default:
  1141. differentb;
  1142. }
  1143. case TYPE_COMPLEX_NUM:
  1144. switch ((int)b & TAG_BITS)
  1145. {
  1146. case TAG_FIXNUM:
  1147. return numeqci(a, b);
  1148. case TAG_SFLOAT:
  1149. return numeqcs(a, b);
  1150. case TAG_NUMBERS:
  1151. { int32 hb = type_of_header(numhdr(b));
  1152. switch (hb)
  1153. {
  1154. case TYPE_BIGNUM:
  1155. return numeqcb(a, b);
  1156. case TYPE_RATNUM:
  1157. return numeqcr(a, b);
  1158. case TYPE_COMPLEX_NUM:
  1159. return numeqcc(a, b);
  1160. default:
  1161. differentb;
  1162. }
  1163. }
  1164. case TAG_BOXFLOAT:
  1165. return numeqcf(a, b);
  1166. default:
  1167. differentb;
  1168. }
  1169. #endif
  1170. default: differenta;
  1171. }
  1172. }
  1173. case TAG_BOXFLOAT:
  1174. switch ((int)b & TAG_BITS)
  1175. {
  1176. case TAG_FIXNUM:
  1177. return numeqfi(a, b);
  1178. #ifdef COMMON
  1179. case TAG_SFLOAT:
  1180. return numeqfs(a, b);
  1181. #endif
  1182. case TAG_NUMBERS:
  1183. { int32 hb = type_of_header(numhdr(b));
  1184. switch (hb)
  1185. {
  1186. case TYPE_BIGNUM:
  1187. return numeqfb(a, b);
  1188. #ifdef COMMON
  1189. case TYPE_RATNUM:
  1190. return numeqfr(a, b);
  1191. case TYPE_COMPLEX_NUM:
  1192. return numeqfc(a, b);
  1193. #endif
  1194. default:
  1195. differentb;
  1196. }
  1197. }
  1198. case TAG_BOXFLOAT:
  1199. return numeqff(a, b);
  1200. default:
  1201. differentb;
  1202. }
  1203. default:
  1204. differenta;
  1205. }
  1206. }
  1207. /* end of arith11.c */