arith11.c 35 KB

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