arith01.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. /* arith01.c Copyright (C) 1990-2002 Codemist Ltd */
  2. /*
  3. * Arithmetic functions.
  4. * Addition of generic numbers
  5. * and in particular a lot of bignum support.
  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: 66948772 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. /*
  33. * I start off with a collection of utility functions that create
  34. * Lisp structures to represent various sorts of numbers
  35. * and which extract values from same.
  36. * The typedefs that explain the layout of these structures are in "tags.h"
  37. */
  38. Lisp_Object make_one_word_bignum(int32 n)
  39. /*
  40. * n is an integer - create a bignum representation of it. This is
  41. * done when n is outside the range 0xf8000000 to 0x07ffffff.
  42. */
  43. { Lisp_Object w = getvector(TAG_NUMBERS, TYPE_BIGNUM, CELL+4);
  44. Lisp_Object nil;
  45. errexit();
  46. bignum_digits(w)[0] = n;
  47. #ifdef ADDRESS_64
  48. bignum_digits(w)[1] = 0; /* padding */
  49. #endif
  50. return w;
  51. }
  52. Lisp_Object make_two_word_bignum(int32 a1, unsigned32 a0)
  53. /*
  54. * This make a 2-word bignum from the 2-word value (a1,a0), where it
  55. * must have been arranged already that a1 and a0 are correctly
  56. * normalized to put in the two words as indicated.
  57. */
  58. {
  59. Lisp_Object w = getvector(TAG_NUMBERS, TYPE_BIGNUM, CELL+8);
  60. Lisp_Object nil;
  61. errexit();
  62. bignum_digits(w)[0] = a0;
  63. bignum_digits(w)[1] = a1;
  64. #ifndef ADDRESS_64
  65. bignum_digits(w)[2] = 0;
  66. #endif
  67. return w;
  68. }
  69. #ifdef COMMON
  70. Lisp_Object make_sfloat(double d)
  71. /*
  72. * Turn a regular floating point value into a Lisp "short float", which
  73. * is an immediate object obtained by using the bottom 4 bits of a 32-bit
  74. * word as tag, and the rest as just whatever would stand for a regular
  75. * single precision value. In doing the conversion here I ignore
  76. * rounding etc - short floats are to save heap turn-over, but will
  77. * not give robust numeric results.
  78. */
  79. {
  80. Float_union w;
  81. w.f = (float)d;
  82. return (w.i & ~(int32)0xf) + TAG_SFLOAT;
  83. }
  84. #endif
  85. Lisp_Object make_boxfloat(double a, int32 type)
  86. /*
  87. * Make a boxed float (single, double or long according to the type specifier)
  88. * if type==0 this makes a short float
  89. */
  90. {
  91. Lisp_Object r, nil;
  92. #ifndef COMMON
  93. CSL_IGNORE(type);
  94. #endif
  95. #ifdef COMMON
  96. switch (type)
  97. {
  98. case 0:
  99. { Float_union aa;
  100. aa.f = (float)a;
  101. return (aa.i & ~(intxx)0xf) + TAG_SFLOAT;
  102. }
  103. case TYPE_SINGLE_FLOAT:
  104. r = getvector(TAG_BOXFLOAT, TYPE_SINGLE_FLOAT, sizeof(Single_Float));
  105. errexit();
  106. single_float_val(r) = (float)a;
  107. return r;
  108. default: /* TYPE_DOUBLE_FLOAT I hope */
  109. #endif
  110. r = getvector(TAG_BOXFLOAT, TYPE_DOUBLE_FLOAT, sizeof(Double_Float));
  111. errexit();
  112. double_float_val(r) = a;
  113. return r;
  114. #ifdef COMMON
  115. case TYPE_LONG_FLOAT:
  116. r = getvector(TAG_BOXFLOAT, TYPE_LONG_FLOAT, sizeof(Long_Float));
  117. errexit();
  118. long_float_val(r) = a;
  119. return r;
  120. }
  121. #endif
  122. }
  123. static double bignum_to_float(Lisp_Object v, int32 h, int *xp)
  124. /*
  125. * Convert a Lisp bignum to get a floating point value. This uses at most the
  126. * top 3 digits of the bignum's representation since that is enough to achieve
  127. * full double precision accuracy.
  128. * This can not overflow, because it leaves an exponent-adjustment value
  129. * in *xp. You need ldexp(r, *xp) afterwards.
  130. */
  131. {
  132. int32 n = (h-CELL-4)/4; /* Last index into the data */
  133. int x = 31*(int)n;
  134. int32 msd = (int32)bignum_digits(v)[n];
  135. /* NB signed conversion on next line */
  136. double r = (double)msd;
  137. switch (n)
  138. {
  139. default: /* for very big numbers combine in 3 digits */
  140. r = TWO_31*r + (double)bignum_digits(v)[--n];
  141. x -= 31;
  142. /* drop through */
  143. case 1: r = TWO_31*r + (double)bignum_digits(v)[--n];
  144. x -= 31;
  145. /* drop through */
  146. case 0: break; /* do no more */
  147. }
  148. *xp = x;
  149. return r;
  150. }
  151. double float_of_number(Lisp_Object a)
  152. /*
  153. * Return a (double precision) floating point value for the given Lisp
  154. * number, or 0.0 in case of trouble. This is often called in circumstances
  155. * where I already know the type of its argument and so its type-dispatch
  156. * is unnecessary - in doing so I am trading off performance against
  157. * code repetition.
  158. */
  159. {
  160. if (is_fixnum(a)) return (double)int_of_fixnum(a);
  161. #ifdef COMMON
  162. else if (is_sfloat(a))
  163. { Float_union w;
  164. w.i = a - TAG_SFLOAT;
  165. return (double)w.f;
  166. }
  167. #endif
  168. else if (is_bfloat(a))
  169. { int32 h = type_of_header(flthdr(a));
  170. switch (h)
  171. {
  172. #ifdef COMMON
  173. case TYPE_SINGLE_FLOAT:
  174. return (double)single_float_val(a);
  175. #endif
  176. case TYPE_DOUBLE_FLOAT:
  177. return double_float_val(a);
  178. #ifdef COMMON
  179. case TYPE_LONG_FLOAT:
  180. return (double)long_float_val(a);
  181. #endif
  182. default:
  183. return 0.0;
  184. }
  185. }
  186. else
  187. { Header h = numhdr(a);
  188. int x1;
  189. double r1;
  190. switch (type_of_header(h))
  191. {
  192. case TYPE_BIGNUM:
  193. r1 = bignum_to_float(a, length_of_header(h), &x1);
  194. return ldexp(r1, x1);
  195. #ifdef COMMON
  196. case TYPE_RATNUM:
  197. { int x2;
  198. Lisp_Object na = numerator(a);
  199. a = denominator(a);
  200. if (is_fixnum(na)) r1 = float_of_number(na), x1 = 0;
  201. else r1 = bignum_to_float(na,
  202. length_of_header(numhdr(na)), &x1);
  203. if (is_fixnum(a)) r1 = r1 / float_of_number(a), x2 = 0;
  204. else r1 = r1 / bignum_to_float(a,
  205. length_of_header(numhdr(a)), &x2);
  206. /* Floating point overflow can only arise in this ldexp() */
  207. return ldexp(r1, x1 - x2);
  208. }
  209. #endif
  210. default:
  211. /*
  212. * If the value was non-numeric or a complex number I hand back 0.0,
  213. * and since I am supposed to have checked the object type already
  214. * this OUGHT not to arrive - bit raising an exception seems over-keen.
  215. */
  216. return 0.0;
  217. }
  218. }
  219. }
  220. int32 thirty_two_bits(Lisp_Object a)
  221. /*
  222. * return a 32 bit integer value for the Lisp integer (fixnum or bignum)
  223. * passed down - ignore any higher order bits and return 0 if the arg was
  224. * floating, rational etc or not a number at all. Only really wanted where
  225. * links between C-specific code (that might really want 32-bit values)
  226. * and Lisp are being coded.
  227. */
  228. {
  229. switch ((int)a & TAG_BITS)
  230. {
  231. case TAG_FIXNUM:
  232. return int_of_fixnum(a);
  233. case TAG_NUMBERS:
  234. if (is_bignum(a))
  235. { int len = bignum_length(a);
  236. /*
  237. * Note that I keep 31 bits per word and use a 2s complement representation.
  238. * thus if I have a one-word bignum I just want its contents but in all
  239. * other cases I need just one bit from the next word up.
  240. */
  241. if (len == 8) return bignum_digits(a)[0]; /* One word bignum */
  242. return bignum_digits(a)[0] | (bignum_digits(a)[1] << 31);
  243. }
  244. /* else drop through */
  245. case TAG_BOXFLOAT:
  246. default:
  247. /*
  248. * return 0 for all non-fixnums
  249. */
  250. return 0;
  251. }
  252. }
  253. #ifdef ADDRESS_64
  254. int64 sixty_four_bits(Lisp_Object a)
  255. {
  256. return (int64)thirty_two_bits(a); /* Inadequate really! */
  257. }
  258. #endif
  259. #ifdef COMMON
  260. Lisp_Object make_complex(Lisp_Object r, Lisp_Object i)
  261. {
  262. Lisp_Object v, nil = C_nil;
  263. /*
  264. * Here r and i are expected to be either both rational (which in this
  265. * context includes the case of integer values) or both of the same
  266. * floating point type. It is assumed that this has already been
  267. * arranged by here.
  268. */
  269. if (i == fixnum_of_int(0)) return r;
  270. stackcheck2(0, r, i);
  271. push2(r, i);
  272. v = getvector(TAG_NUMBERS, TYPE_COMPLEX_NUM, sizeof(Complex_Number));
  273. /*
  274. * The vector r has uninitialized contents here - dodgy. If the call
  275. * to getvector succeeded then I fill it in, otherwise I will not
  276. * refer to it again, and I think that unreferenced vectors containing junk
  277. * are OK.
  278. */
  279. pop2(i, r);
  280. errexit();
  281. real_part(v) = r;
  282. imag_part(v) = i;
  283. return v;
  284. }
  285. Lisp_Object make_ratio(Lisp_Object p, Lisp_Object q)
  286. /*
  287. * By the time this is called (p/q) must be in its lowest terms, q>0
  288. */
  289. {
  290. Lisp_Object v, nil = C_nil;
  291. if (q == fixnum_of_int(1)) return p;
  292. stackcheck2(0, p, q);
  293. push2(p, q);
  294. v = getvector(TAG_NUMBERS, TYPE_RATNUM, sizeof(Rational_Number));
  295. pop2(q, p);
  296. errexit();
  297. numerator(v) = p;
  298. denominator(v) = q;
  299. return v;
  300. }
  301. #endif
  302. /*
  303. * The next bit of code seems pretty dreadful, but I think that is just
  304. * what generic arithmetic is all about. The code for plus2 is written
  305. * as a dispatch function into over 30 separate possible type-specific
  306. * versions of the code. In a very few simple (and performance-critical)
  307. * cases the code is written in-line in plus2 - in particular arithmetic
  308. * on fixnums is done that way. Similarly for other cases.
  309. * I Use one-character suffices to remind me about types:
  310. * i fixnum
  311. * b bignum
  312. * r ratio
  313. * s short float
  314. * f boxed float (single/double/long)
  315. * c complex
  316. *
  317. * Throughout this code I am going to IGNORE floating point exceptions,
  318. * at least for a first attempt. Decent detection of and recovery after
  319. * floating point overflow seems an extra unpleasant distraction! Note
  320. * that C allows me to trap the SIGFPE exception, but returning from
  321. * the exception handler gives undefined behaviour - one is expected
  322. * to longjmp out, which means accepting the cost of using setjmp.
  323. *
  324. * It would perhaps be reasonable to write the dispatch code as a big
  325. * macro so that the versions for plus, times etc could all be kept
  326. * in step - I have not done that (a) because the macro would have been
  327. * bigger than I like macros to be (b) it would have involved token-
  328. * splicing (or VERY many parameters) to generate the names of the
  329. * separate type-specific procedures and (c) doing it by hand allows me
  330. * total flexibility about coding various cases in-line.
  331. */
  332. #ifdef COMMON
  333. static Lisp_Object plusis(Lisp_Object a, Lisp_Object b)
  334. {
  335. Float_union bb;
  336. bb.i = b - TAG_SFLOAT;
  337. bb.f = (float)((float)int_of_fixnum(a) + bb.f);
  338. return (bb.i & ~(int32)0xf) + TAG_SFLOAT;
  339. }
  340. #endif
  341. /*
  342. * Bignums are represented as vectors where the most significant 32-bit
  343. * digit is treated as signed, and the remaining ones are unsigned.
  344. */
  345. static Lisp_Object plusib(Lisp_Object a, Lisp_Object b)
  346. /*
  347. * Add a fixnum to a bignum, returning a result as a fixnum or bignum
  348. * depending on its size. This seems much nastier than one would have
  349. * hoped.
  350. */
  351. {
  352. int32 len = bignum_length(b)-CELL, i, sign = int_of_fixnum(a), s;
  353. Lisp_Object c, nil;
  354. len = len/4;
  355. if (len == 1)
  356. { int32 t;
  357. /*
  358. * Partly because it will be a common case and partly because it has
  359. * various special cases I have special purpose code to cope with
  360. * adding a fixnum to a one-word bignum.
  361. */
  362. s = (int32)bignum_digits(b)[0] + sign;
  363. t = s + s;
  364. if (top_bit_set(s ^ t)) /* needs to turn into two-word bignum */
  365. { if (s < 0) return make_two_word_bignum(-1, clear_top_bit(s));
  366. else return make_two_word_bignum(0, s);
  367. }
  368. t = s & fix_mask; /* Will it fit as a fixnum? */
  369. if (t == 0 || t == fix_mask) return fixnum_of_int(s);
  370. /* here the result is a one-word bignum */
  371. return make_one_word_bignum(s);
  372. }
  373. /*
  374. * Now, after all the silly cases have been handled, I have a calculation
  375. * which seems set to give a multi-word result. The result here can at
  376. * least never shrink to a fixnum since subtracting a fixnum can at
  377. * most shrink the length of a number by one word. I omit the stack-
  378. * check here in the hope that code here never nests enough for trouble.
  379. */
  380. push(b);
  381. c = getvector(TAG_NUMBERS, TYPE_BIGNUM, CELL+4*len);
  382. pop(b);
  383. errexit();
  384. s = bignum_digits(b)[0] + clear_top_bit(sign);
  385. bignum_digits(c)[0] = clear_top_bit(s);
  386. if (sign >= 0) sign = 0; else sign = 0x7fffffff; /* extend the sign */
  387. len--;
  388. for (i=1; i<len; i++)
  389. { s = bignum_digits(b)[i] + sign + top_bit(s);
  390. bignum_digits(c)[i] = clear_top_bit(s);
  391. }
  392. /* Now just the most significant digit remains to be processed */
  393. if (sign != 0) sign = -1;
  394. { s = bignum_digits(b)[i] + sign + top_bit(s);
  395. if (!signed_overflow(s)) /* did it overflow? */
  396. {
  397. /*
  398. * Here the most significant digit did not produce an overflow, but maybe
  399. * what we actually had was some cancellation and the MSD is now zero
  400. * or -1, so that the number should shrink...
  401. */
  402. if ((s == 0 && (bignum_digits(c)[i-1] & 0x40000000) == 0) ||
  403. (s == -1 && (bignum_digits(c)[i-1] & 0x40000000) != 0))
  404. { /* shrink the number */
  405. numhdr(c) -= pack_hdrlength(1L);
  406. if (s == -1) bignum_digits(c)[i-1] |= ~0x7fffffff;
  407. /*
  408. * Now sometimes the shrinkage will leave a padding word, sometimes it
  409. * will really allow me to save space. As a jolly joke with a 64-bit
  410. * system I need padding if there have been an odd number of (32-bit)
  411. * words of bignum data while with a 32-bit system the header word is
  412. * 32-bits wide and I need padding if there are ar even number of additional
  413. * data words.
  414. */
  415. #ifdef ADDRESS_64
  416. if ((i & 1) != 0)
  417. #else
  418. if ((i & 1) == 0)
  419. #endif
  420. { bignum_digits(c)[i] = 0; /* leave the unused word tidy */
  421. return c;
  422. }
  423. /*
  424. * Having shrunk the number I am leaving a doubleword of unallocated space
  425. * in the heap. Dump a header word into it to make it look like an
  426. * 8-byte bignum since that will allow the garbage collector to handle it.
  427. * It I left it containing arbitrary junk I could wreck myself. The
  428. * make_bighdr(2L) makes a header for a number that fills 2 32-bit words
  429. * in all.
  430. */
  431. *(Header *)&bignum_digits(c)[i] = make_bighdr(2L);
  432. return c;
  433. }
  434. bignum_digits(c)[i] = s; /* length unchanged */
  435. return c;
  436. }
  437. /*
  438. * Here the result is one word longer than the input-bignum.
  439. * Once again SOMTIMES this will not involve allocating more store,
  440. * but just encroaching into the previously unused word that was padding
  441. * things out to a multiple of 8 bytes.
  442. */
  443. #ifdef ADDRESS_64
  444. if ((i & 1) == 0)
  445. #else
  446. if ((i & 1) == 1)
  447. #endif
  448. { bignum_digits(c)[i++] = clear_top_bit(s);
  449. bignum_digits(c)[i] = top_bit_set(s) ? -1 : 0;
  450. numhdr(c) += pack_hdrlength(1L);
  451. return c;
  452. }
  453. push(c);
  454. b = getvector(TAG_NUMBERS, TYPE_BIGNUM, CELL+8+4*len);
  455. pop(c);
  456. errexit();
  457. for (i=0; i<=len; i++)
  458. bignum_digits(b)[i] = bignum_digits(c)[i];
  459. bignum_digits(b)[i++] = clear_top_bit(s);
  460. bignum_digits(b)[i] = top_bit_set(s) ? -1 : 0;
  461. return b;
  462. }
  463. }
  464. #ifdef COMMON
  465. static Lisp_Object plusir(Lisp_Object a, Lisp_Object b)
  466. /*
  467. * fixnum and ratio, but also valid for bignum and ratio.
  468. * Note that if the inputs were in lowest terms there is no need for
  469. * and GCD calculations here.
  470. */
  471. {
  472. Lisp_Object nil;
  473. push(b);
  474. a = times2(a, denominator(b));
  475. nil = C_nil;
  476. if (!exception_pending()) a = plus2(a, numerator(stack[0]));
  477. pop(b);
  478. errexit();
  479. return make_ratio(a, denominator(b));
  480. }
  481. static Lisp_Object plusic(Lisp_Object a, Lisp_Object b)
  482. /*
  483. * real of any sort plus complex.
  484. */
  485. {
  486. Lisp_Object nil;
  487. push(b);
  488. a = plus2(a, real_part(b));
  489. pop(b);
  490. errexit();
  491. /*
  492. * make_complex() takes responsibility for mapping #C(n 0) onto n
  493. */
  494. return make_complex(a, imag_part(b));
  495. }
  496. #endif
  497. static Lisp_Object plusif(Lisp_Object a, Lisp_Object b)
  498. /*
  499. * Fixnum plus boxed-float.
  500. */
  501. {
  502. double d = (double)int_of_fixnum(a) + float_of_number(b);
  503. return make_boxfloat(d, type_of_header(flthdr(b)));
  504. }
  505. #ifdef COMMON
  506. #define plussi(a, b) plusis(b, a)
  507. #define plussb(a, b) plusbs(b, a)
  508. #define plussr(a, b) plusrs(b, a)
  509. #define plussc(a, b) plusic(a, b)
  510. #endif
  511. static Lisp_Object plussf(Lisp_Object a, Lisp_Object b)
  512. /*
  513. * This can be used for any rational value plus a boxed-float. plusif()
  514. * is separated just for (minor) efficiency reasons.
  515. */
  516. {
  517. double d = float_of_number(a) + float_of_number(b);
  518. return make_boxfloat(d, type_of_header(flthdr(b)));
  519. }
  520. #define plusbi(a, b) plusib(b, a)
  521. #ifdef COMMON
  522. static Lisp_Object plusbs(Lisp_Object a, Lisp_Object b)
  523. {
  524. double d = float_of_number(a) + float_of_number(b);
  525. return make_sfloat(d);
  526. }
  527. #endif
  528. Lisp_Object lengthen_by_one_bit(Lisp_Object a, int32 msd)
  529. /*
  530. * (a) is a bignum, and arithmetic on it has (just) caused overflow
  531. * in its top word - I just need to stick on another word. (msd) is the
  532. * current top word, and its sign will be used to decide on the value
  533. * that must be appended.
  534. */
  535. {
  536. int32 len = bignum_length(a);
  537. /*
  538. * Sometimes I need to allocate a new vector and copy data across into it
  539. */
  540. if ((len & 4) == 0)
  541. { Lisp_Object b, nil;
  542. int32 i;
  543. push(a);
  544. b = getvector(TAG_NUMBERS, TYPE_BIGNUM, len+4);
  545. pop(a);
  546. errexit();
  547. len = (len-CELL)/4;
  548. for (i=0; i<len; i++)
  549. bignum_digits(b)[i] = clear_top_bit(bignum_digits(a)[i]);
  550. bignum_digits(b)[len] = top_bit_set(msd) ? -1 : 0;
  551. bignum_digits(b)[len+1] = 0;
  552. return b;
  553. }
  554. else
  555. /*
  556. * .. whereas sometimes I have a spare word already available.
  557. */
  558. { numhdr(a) += pack_hdrlength(1L);
  559. len = (len-CELL)/4;
  560. bignum_digits(a)[len-1] = clear_top_bit(bignum_digits(a)[len-1]);
  561. bignum_digits(a)[len] = top_bit_set(msd) ? -1 : 0;
  562. return a;
  563. }
  564. }
  565. static Lisp_Object plusbb(Lisp_Object a, Lisp_Object b)
  566. /*
  567. * add two bignums.
  568. */
  569. {
  570. int32 la = bignum_length(a),
  571. lb = bignum_length(b),
  572. i, s, carry;
  573. Lisp_Object c, nil;
  574. if (la < lb) /* maybe swap order of args */
  575. { Lisp_Object t = a;
  576. int32 t1;
  577. a = b; b = t;
  578. t1 = la; la = lb; lb = t1;
  579. }
  580. /*
  581. * now (a) is AT LEAST as long as b. I have special case code for
  582. * when both args are single-word bignums, since I expect that to be
  583. * an especially common case.
  584. */
  585. if (la == CELL+4) /* and hence b also has only 1 digit */
  586. { int32 va = bignum_digits(a)[0],
  587. vb = bignum_digits(b)[0],
  588. vc = va + vb;
  589. if (signed_overflow(vc)) /* we have a 2-word bignum result */
  590. { Lisp_Object w = getvector(TAG_NUMBERS, TYPE_BIGNUM, CELL+8);
  591. errexit();
  592. bignum_digits(w)[0] = clear_top_bit(vc);
  593. bignum_digits(w)[1] = top_bit_set(vc) ? -1 : 0;
  594. #ifndef ADDRESS_64
  595. bignum_digits(w)[2] = 0;
  596. #endif
  597. return w;
  598. }
  599. /*
  600. * here the result fits into one word - maybe it will squash down into
  601. * a fixnum?
  602. */
  603. else
  604. { vb = vc & fix_mask;
  605. if (vb == 0 || vb == fix_mask) return fixnum_of_int(vc);
  606. else return make_one_word_bignum(vc);
  607. }
  608. }
  609. push2(a, b);
  610. c = getvector(TAG_NUMBERS, TYPE_BIGNUM, la);
  611. pop2(b, a);
  612. errexit();
  613. la = (la-CELL)/4 - 1;
  614. lb = (lb-CELL)/4 - 1;
  615. carry = 0;
  616. /*
  617. * Add all but the top digit of b
  618. */
  619. for (i=0; i<lb; i++)
  620. { carry = bignum_digits(a)[i] + bignum_digits(b)[i] + top_bit(carry);
  621. bignum_digits(c)[i] = clear_top_bit(carry);
  622. }
  623. if (la == lb) s = bignum_digits(b)[i];
  624. else
  625. /*
  626. * If a is strictly longer than b I sign extend b here and add in as many
  627. * copies of 0 or -1 as needbe to get up to the length of a.
  628. */
  629. { s = bignum_digits(b)[i];
  630. carry = bignum_digits(a)[i] + clear_top_bit(s) + top_bit(carry);
  631. bignum_digits(c)[i] = clear_top_bit(carry);
  632. if (s < 0) s = -1; else s = 0;
  633. for (i++; i<la; i++)
  634. { carry = bignum_digits(a)[i] + clear_top_bit(s) + top_bit(carry);
  635. bignum_digits(c)[i] = clear_top_bit(carry);
  636. }
  637. }
  638. /*
  639. * the most significant digit is added using signed arithmetic so that I
  640. * can tell if it overflowed.
  641. */
  642. carry = bignum_digits(a)[i] + s + top_bit(carry);
  643. if (!signed_overflow(carry))
  644. {
  645. /*
  646. * Here the number has not expanded - but it may be shrinking, and it can
  647. * shrink by any number of words, all the way down to a fixnum maybe. Note
  648. * that I started with at least a 2-word bignum here.
  649. */
  650. int32 msd;
  651. bignum_digits(c)[i] = carry;
  652. if (carry == 0)
  653. { int32 j = i-1;
  654. while ((msd = bignum_digits(c)[j]) == 0 && j > 0) j--;
  655. /*
  656. * ... but I may need a zero word on the front if the next word down
  657. * has its top bit set... (top of 31 bits, that is)
  658. */
  659. if ((msd & 0x40000000) != 0)
  660. { j++;
  661. if (i == j) return c;
  662. }
  663. if (j == 0)
  664. { int32 s = bignum_digits(c)[0];
  665. if ((s & fix_mask) == 0) return fixnum_of_int(s);
  666. }
  667. /*
  668. * If I am shrinking by one word and had an even length to start with
  669. * I do not have to mess about so much.
  670. */
  671. #ifdef ADDRESS_64
  672. if (j == i-1 && (i & 1) != 0)
  673. #else
  674. if (j == i-1 && (i & 1) == 0)
  675. #endif
  676. { numhdr(c) -= pack_hdrlength(1L);
  677. return c;
  678. }
  679. numhdr(c) -= pack_hdrlength(i - j);
  680. #ifdef ADDRESS_64
  681. i = (i+2) & ~1;
  682. j = (j+2) & ~1; /* Round up to odd index */
  683. #else
  684. i = (i+1) | 1;
  685. j = (j+1) | 1; /* Round up to odd index */
  686. #endif
  687. /*
  688. * I forge a header word to allow the garbage collector to skip over
  689. * (and in due course reclaim) the space that turned out not to be needed.
  690. */
  691. bignum_digits(c)[j] = make_bighdr(i - j);
  692. return c;
  693. }
  694. /*
  695. * Now do all the same sorts of things but this time for negative numbers.
  696. */
  697. else if (carry == -1)
  698. { int32 j = i-1;
  699. msd = carry; /* in case j = 0 */
  700. while ((msd = bignum_digits(c)[j]) == 0x7fffffff && j > 0) j--;
  701. if ((msd & 0x40000000) == 0)
  702. { j++;
  703. if (i == j) return c;
  704. }
  705. if (j == 0)
  706. { int32 s = bignum_digits(c)[0] | ~0x7fffffff;
  707. if ((s & fix_mask) == fix_mask) return fixnum_of_int(s);
  708. }
  709. #ifdef ADDRESS_64
  710. if (j == i-1 && (i & 1) != 0)
  711. #else
  712. if (j == i-1 && (i & 1) == 0)
  713. #endif
  714. { bignum_digits(c)[i] = 0;
  715. bignum_digits(c)[i-1] |= ~0x7fffffff;
  716. numhdr(c) -= pack_hdrlength(1);
  717. return c;
  718. }
  719. numhdr(c) -= pack_hdrlength(i - j);
  720. bignum_digits(c)[j+1] = 0;
  721. bignum_digits(c)[j] |= ~0x7fffffff;
  722. #ifdef ADDRESS_64
  723. i = (i+2) & ~1;
  724. j = (j+2) & ~1; /* Round up to odd index */
  725. #else
  726. i = (i+1) | 1;
  727. j = (j+1) | 1; /* Round up to odd index */
  728. #endif
  729. bignum_digits(c)[j] = make_bighdr(i - j);
  730. return c;
  731. }
  732. return c;
  733. }
  734. else
  735. { bignum_digits(c)[i] = carry;
  736. return lengthen_by_one_bit(c, carry);
  737. }
  738. }
  739. #ifdef COMMON
  740. #define plusbr(a, b) plusir(a, b)
  741. #define plusbc(a, b) plusic(a, b)
  742. #endif
  743. #define plusbf(a, b) plussf(a, b)
  744. #ifdef COMMON
  745. #define plusri(a, b) plusir(b, a)
  746. #define plusrs(a, b) plusbs(a, b)
  747. #define plusrb(a, b) plusri(a, b)
  748. static Lisp_Object plusrr(Lisp_Object a, Lisp_Object b)
  749. /*
  750. * Adding two ratios involves some effort to keep the result in
  751. * lowest terms.
  752. */
  753. {
  754. Lisp_Object nil = C_nil;
  755. Lisp_Object na = numerator(a), nb = numerator(b);
  756. Lisp_Object da = denominator(a), db = denominator(b);
  757. Lisp_Object w = nil;
  758. push5(na, nb, da, db, nil);
  759. #define g stack[0]
  760. #define db stack[-1]
  761. #define da stack[-2]
  762. #define nb stack[-3]
  763. #define na stack[-4]
  764. g = gcd(da, db);
  765. nil = C_nil;
  766. if (exception_pending()) goto fail;
  767. /*
  768. * all the calls to quot2() in this procedure are expected - nay required -
  769. * to give exact integer quotients.
  770. */
  771. db = quot2(db, g);
  772. nil = C_nil;
  773. if (exception_pending()) goto fail;
  774. g = quot2(da, g);
  775. nil = C_nil;
  776. if (exception_pending()) goto fail;
  777. na = times2(na, db);
  778. nil = C_nil;
  779. if (exception_pending()) goto fail;
  780. nb = times2(nb, g);
  781. nil = C_nil;
  782. if (exception_pending()) goto fail;
  783. na = plus2(na, nb);
  784. nil = C_nil;
  785. if (exception_pending()) goto fail;
  786. da = times2(da, db);
  787. nil = C_nil;
  788. if (exception_pending()) goto fail;
  789. g = gcd(na, da);
  790. nil = C_nil;
  791. if (exception_pending()) goto fail;
  792. na = quot2(na, g);
  793. nil = C_nil;
  794. if (exception_pending()) goto fail;
  795. da = quot2(da, g);
  796. nil = C_nil;
  797. if (exception_pending()) goto fail;
  798. w = make_ratio(na, da);
  799. /*
  800. * All the goto statements and the label seem a fair way of expressing
  801. * the common action that has to be taken if an error or interrupt is
  802. * detected during any of the intermediate steps here. Anyone who
  803. * objects can change it if they really want...
  804. */
  805. fail:
  806. popv(5);
  807. return w;
  808. #undef na
  809. #undef nb
  810. #undef da
  811. #undef db
  812. #undef g
  813. }
  814. #define plusrc(a, b) plusic(a, b)
  815. #define plusrf(a, b) plussf(a, b)
  816. #define plusci(a, b) plusic(b, a)
  817. #define pluscs(a, b) plussc(b, a)
  818. #define pluscb(a, b) plusbc(b, a)
  819. #define pluscr(a, b) plusrc(b, a)
  820. static Lisp_Object pluscc(Lisp_Object a, Lisp_Object b)
  821. /*
  822. * Add complex values.
  823. */
  824. {
  825. Lisp_Object c, nil;
  826. push2(a, b);
  827. c = plus2(imag_part(a), imag_part(b));
  828. pop2(b, a);
  829. errexit();
  830. a = plus2(real_part(a), real_part(b));
  831. errexit();
  832. return make_complex(a, c);
  833. }
  834. #define pluscf(a, b) plusfc(b, a)
  835. #endif
  836. #define plusfi(a, b) plusif(b, a)
  837. #ifdef COMMON
  838. #define plusfs(a, b) plussf(b, a)
  839. #endif
  840. #define plusfb(a, b) plusbf(b, a)
  841. #ifdef COMMON
  842. #define plusfr(a, b) plusrf(b, a)
  843. #define plusfc(a, b) plusic(a, b)
  844. #endif
  845. static Lisp_Object plusff(Lisp_Object a, Lisp_Object b)
  846. /*
  847. * Add two boxed floats - the type of the result must match the
  848. * longer of the types of the arguments, hence the extra
  849. * messing about.
  850. */
  851. {
  852. #ifdef COMMON
  853. int32 ha = type_of_header(flthdr(a)), hb = type_of_header(flthdr(b));
  854. #endif
  855. double d;
  856. /*
  857. * This is written as a declaration followed by a separate assignment to
  858. * d because I hit a compiler bug on a VAX once otherwise.
  859. */
  860. d = float_of_number(a) + float_of_number(b);
  861. #ifdef COMMON
  862. if (ha == TYPE_LONG_FLOAT || hb == TYPE_LONG_FLOAT)
  863. ha = TYPE_LONG_FLOAT;
  864. else if (ha == TYPE_DOUBLE_FLOAT || hb == TYPE_DOUBLE_FLOAT)
  865. ha = TYPE_DOUBLE_FLOAT;
  866. else ha = TYPE_SINGLE_FLOAT;
  867. return make_boxfloat(d, ha);
  868. #else
  869. return make_boxfloat(d, TYPE_DOUBLE_FLOAT);
  870. #endif
  871. }
  872. /*
  873. * and now for the dispatch code...
  874. */
  875. /*
  876. * The following verifies that a number is properly formatted - a
  877. * fixnum if small enough or a decently normalised bignum. For use when
  878. * there is suspicion of a bug wrt such matters. Call is
  879. * validate_number("msg", numberToCheck, nX, nY)
  880. * where nX and nY must be numbers and are shown in any
  881. * diagnostic.
  882. */
  883. void validate_number(char *s, Lisp_Object a, Lisp_Object b, Lisp_Object c)
  884. {
  885. int32 la, w, msd;
  886. if (!is_numbers(a)) return;
  887. la = (length_of_header(numhdr(a))-CELL-4)/4;
  888. if (la < 0)
  889. { trace_printf("%s: number with no digits (%.8x)\n", s, numhdr(a));
  890. if (is_number(b)) prin_to_trace(b), trace_printf("\n");
  891. if (is_number(c)) prin_to_trace(c), trace_printf("\n");
  892. my_exit(EXIT_FAILURE);
  893. }
  894. if (la == 0)
  895. { msd = bignum_digits(a)[0];
  896. w = msd & fix_mask;
  897. if (w == 0 || w == fix_mask)
  898. { trace_printf("%s: %.8x should be fixnum\n", s, msd);
  899. if (is_number(b)) prin_to_trace(b), trace_printf("\n");
  900. if (is_number(c)) prin_to_trace(c), trace_printf("\n");
  901. my_exit(EXIT_FAILURE);
  902. }
  903. if (signed_overflow(msd))
  904. { trace_printf("%s: %.8x should be two-word\n", s, msd);
  905. if (is_number(b)) prin_to_trace(b), trace_printf("\n");
  906. if (is_number(c)) prin_to_trace(c), trace_printf("\n");
  907. my_exit(EXIT_FAILURE);
  908. }
  909. return;
  910. }
  911. msd = bignum_digits(a)[la];
  912. if (signed_overflow(msd))
  913. { trace_printf("%s: %.8x should be longer\n", s, msd);
  914. if (is_number(b)) prin_to_trace(b), trace_printf("\n");
  915. if (is_number(c)) prin_to_trace(c), trace_printf("\n");
  916. my_exit(EXIT_FAILURE);
  917. }
  918. if (msd == 0 && ((msd = bignum_digits(a)[la-1]) & 0x40000000) == 0)
  919. { trace_printf("%s: 0: %.8x should be shorter\n", s, msd);
  920. if (is_number(b)) prin_to_trace(b), trace_printf("\n");
  921. if (is_number(c)) prin_to_trace(c), trace_printf("\n");
  922. my_exit(EXIT_FAILURE);
  923. }
  924. if (msd == -1 && ((msd = bignum_digits(a)[la-1]) & 0x40000000) != 0)
  925. { trace_printf("%s: -1: %.8x should be shorter\n", s, msd);
  926. if (is_number(b)) prin_to_trace(b), trace_printf("\n");
  927. if (is_number(c)) prin_to_trace(c), trace_printf("\n");
  928. my_exit(EXIT_FAILURE);
  929. }
  930. }
  931. Lisp_Object plus2(Lisp_Object a, Lisp_Object b)
  932. /*
  933. * I probably want to change the specification of plus2 so that the fixnum +
  934. * fixnum case is always expected to be done before the main body of the code
  935. * is entered. Well maybe even if I do that it then costs very little to
  936. * include the fixnum code here as well, so I will not delete it.
  937. */
  938. {
  939. switch ((int)a & TAG_BITS)
  940. {
  941. case TAG_FIXNUM:
  942. switch ((int)b & TAG_BITS)
  943. {
  944. case TAG_FIXNUM:
  945. /*
  946. * This is where fixnum + fixnum arithmetic happens - the case I most want to
  947. * make efficient.
  948. */
  949. { int32 r = int_of_fixnum(a) + int_of_fixnum(b);
  950. int32 t = r & fix_mask;
  951. if (t == 0 || t == fix_mask) return fixnum_of_int(r);
  952. else return make_one_word_bignum(r);
  953. }
  954. #ifdef COMMON
  955. case TAG_SFLOAT:
  956. return plusis(a, b);
  957. #endif
  958. case TAG_NUMBERS:
  959. { int32 hb = type_of_header(numhdr(b));
  960. switch (hb)
  961. {
  962. case TYPE_BIGNUM:
  963. return plusib(a, b);
  964. #ifdef COMMON
  965. case TYPE_RATNUM:
  966. return plusir(a, b);
  967. case TYPE_COMPLEX_NUM:
  968. return plusic(a, b);
  969. #endif
  970. default:
  971. return aerror1("bad arg for plus", b);
  972. }
  973. }
  974. case TAG_BOXFLOAT:
  975. return plusif(a, b);
  976. default:
  977. return aerror1("bad arg for plus", b);
  978. }
  979. #ifdef COMMON
  980. case TAG_SFLOAT:
  981. switch (b & TAG_BITS)
  982. {
  983. case TAG_FIXNUM:
  984. return plussi(a, b);
  985. case TAG_SFLOAT:
  986. { Float_union aa, bb;
  987. aa.i = a - TAG_SFLOAT;
  988. bb.i = b - TAG_SFLOAT;
  989. aa.f = (float)(aa.f + bb.f);
  990. return (aa.i & ~(int32)0xf) + TAG_SFLOAT;
  991. }
  992. case TAG_NUMBERS:
  993. { int32 hb = type_of_header(numhdr(b));
  994. switch (hb)
  995. {
  996. case TYPE_BIGNUM:
  997. return plussb(a, b);
  998. case TYPE_RATNUM:
  999. return plussr(a, b);
  1000. case TYPE_COMPLEX_NUM:
  1001. return plussc(a, b);
  1002. default:
  1003. return aerror1("bad arg for plus", b);
  1004. }
  1005. }
  1006. case TAG_BOXFLOAT:
  1007. return plussf(a, b);
  1008. default:
  1009. return aerror1("bad arg for plus", b);
  1010. }
  1011. #endif
  1012. case TAG_NUMBERS:
  1013. { int32 ha = type_of_header(numhdr(a));
  1014. switch (ha)
  1015. {
  1016. case TYPE_BIGNUM:
  1017. switch ((int)b & TAG_BITS)
  1018. {
  1019. case TAG_FIXNUM:
  1020. return plusbi(a, b);
  1021. #ifdef COMMON
  1022. case TAG_SFLOAT:
  1023. return plusbs(a, b);
  1024. #endif
  1025. case TAG_NUMBERS:
  1026. { int32 hb = type_of_header(numhdr(b));
  1027. switch (hb)
  1028. {
  1029. case TYPE_BIGNUM:
  1030. return plusbb(a, b);
  1031. #ifdef COMMON
  1032. case TYPE_RATNUM:
  1033. return plusbr(a, b);
  1034. case TYPE_COMPLEX_NUM:
  1035. return plusbc(a, b);
  1036. #endif
  1037. default:
  1038. return aerror1("bad arg for plus", b);
  1039. }
  1040. }
  1041. case TAG_BOXFLOAT:
  1042. return plusbf(a, b);
  1043. default:
  1044. return aerror1("bad arg for plus", b);
  1045. }
  1046. #ifdef COMMON
  1047. case TYPE_RATNUM:
  1048. switch (b & TAG_BITS)
  1049. {
  1050. case TAG_FIXNUM:
  1051. return plusri(a, b);
  1052. case TAG_SFLOAT:
  1053. return plusrs(a, b);
  1054. case TAG_NUMBERS:
  1055. { int32 hb = type_of_header(numhdr(b));
  1056. switch (hb)
  1057. {
  1058. case TYPE_BIGNUM:
  1059. return plusrb(a, b);
  1060. case TYPE_RATNUM:
  1061. return plusrr(a, b);
  1062. case TYPE_COMPLEX_NUM:
  1063. return plusrc(a, b);
  1064. default:
  1065. return aerror1("bad arg for plus", b);
  1066. }
  1067. }
  1068. case TAG_BOXFLOAT:
  1069. return plusrf(a, b);
  1070. default:
  1071. return aerror1("bad arg for plus", b);
  1072. }
  1073. case TYPE_COMPLEX_NUM:
  1074. switch (b & TAG_BITS)
  1075. {
  1076. case TAG_FIXNUM:
  1077. return plusci(a, b);
  1078. case TAG_SFLOAT:
  1079. return pluscs(a, b);
  1080. case TAG_NUMBERS:
  1081. { int32 hb = type_of_header(numhdr(b));
  1082. switch (hb)
  1083. {
  1084. case TYPE_BIGNUM:
  1085. return pluscb(a, b);
  1086. case TYPE_RATNUM:
  1087. return pluscr(a, b);
  1088. case TYPE_COMPLEX_NUM:
  1089. return pluscc(a, b);
  1090. default:
  1091. return aerror1("bad arg for plus", b);
  1092. }
  1093. }
  1094. case TAG_BOXFLOAT:
  1095. return pluscf(a, b);
  1096. default:
  1097. return aerror1("bad arg for plus", b);
  1098. }
  1099. #endif
  1100. default: return aerror1("bad arg for plus", a);
  1101. }
  1102. }
  1103. case TAG_BOXFLOAT:
  1104. switch ((int)b & TAG_BITS)
  1105. {
  1106. case TAG_FIXNUM:
  1107. return plusfi(a, b);
  1108. #ifdef COMMON
  1109. case TAG_SFLOAT:
  1110. return plusfs(a, b);
  1111. #endif
  1112. case TAG_NUMBERS:
  1113. { int32 hb = type_of_header(numhdr(b));
  1114. switch (hb)
  1115. {
  1116. case TYPE_BIGNUM:
  1117. return plusfb(a, b);
  1118. #ifdef COMMON
  1119. case TYPE_RATNUM:
  1120. return plusfr(a, b);
  1121. case TYPE_COMPLEX_NUM:
  1122. return plusfc(a, b);
  1123. #endif
  1124. default:
  1125. return aerror1("bad arg for plus", b);
  1126. }
  1127. }
  1128. case TAG_BOXFLOAT:
  1129. return plusff(a, b);
  1130. default:
  1131. return aerror1("bad arg for plus", b);
  1132. }
  1133. default:
  1134. return aerror1("bad arg for plus", a);
  1135. }
  1136. }
  1137. Lisp_Object difference2(Lisp_Object a, Lisp_Object b)
  1138. {
  1139. Lisp_Object nil;
  1140. switch ((int)b & TAG_BITS)
  1141. {
  1142. case TAG_FIXNUM:
  1143. if (is_fixnum(a))
  1144. {
  1145. int32 r = int_of_fixnum(a) - int_of_fixnum(b);
  1146. int32 t = r & fix_mask;
  1147. if (t == 0 || t == fix_mask) return fixnum_of_int(r);
  1148. else return make_one_word_bignum(r);
  1149. }
  1150. else if (b != ~0x7ffffffe) return plus2(a, 2*TAG_FIXNUM-b);
  1151. else
  1152. { push(a);
  1153. b = make_one_word_bignum(-int_of_fixnum(b));
  1154. break;
  1155. }
  1156. case TAG_NUMBERS:
  1157. push(a);
  1158. if (type_of_header(numhdr(b)) == TYPE_BIGNUM) b = negateb(b);
  1159. else b = negate(b);
  1160. break;
  1161. case TAG_BOXFLOAT:
  1162. default:
  1163. push(a);
  1164. b = negate(b);
  1165. break;
  1166. }
  1167. pop(a);
  1168. errexit();
  1169. return plus2(a, b);
  1170. }
  1171. Lisp_Object add1(Lisp_Object p)
  1172. /*
  1173. * Increment a number. Short cut when the number is a fixnum, otherwise
  1174. * just hand over to the general addition code.
  1175. */
  1176. {
  1177. if (is_fixnum(p))
  1178. { unsigned32 r = (int32)p + 0x10;
  1179. /* fixnums have data shifted left 4 bits */
  1180. if (r == ~0x7ffffffe) /* The ONLY possible overflow case here */
  1181. return make_one_word_bignum(1 + int_of_fixnum(p));
  1182. else return (Lisp_Object)r;
  1183. }
  1184. else return plus2(p, fixnum_of_int(1));
  1185. }
  1186. Lisp_Object sub1(Lisp_Object p)
  1187. /*
  1188. * Decrement a number. Short cut when the number is a fixnum, otherwise
  1189. * just hand over to the general addition code.
  1190. */
  1191. {
  1192. if (is_fixnum(p))
  1193. { if (p == ~0x7ffffffe) /* The ONLY possible overflow case here */
  1194. return make_one_word_bignum(int_of_fixnum(p) - 1);
  1195. else return (Lisp_Object)(p - 0x10);
  1196. }
  1197. else return plus2(p, fixnum_of_int(-1));
  1198. }
  1199. /* end of arith01.c */