arith02.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /* arith02.c Copyright (C) 1990/1991 Codemist Ltd */
  2. /*
  3. * Arithmetic functions.
  4. * Multiplication of generic numbers
  5. * and in particular a lot of bignum support.
  6. *
  7. * Version 1.4 February 1991 - Fast multiplication.
  8. */
  9. /* Signature: 4a0039df 31-May-1997 */
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #include <math.h>
  14. #ifdef __WATCOMC__
  15. #include <float.h>
  16. #endif
  17. #include "machine.h"
  18. #include "tags.h"
  19. #include "cslerror.h"
  20. #include "externs.h"
  21. #include "arith.h"
  22. #ifdef TIMEOUT
  23. #include "timeout.h"
  24. #endif
  25. /*
  26. * Now for multiplication
  27. */
  28. /*
  29. * I provide symbols IMULTIPLY and IDIVIDE which can be asserted if the
  30. * corresponding routines have been provided elsewhere (e.g. in machine
  31. * code for extra speed)
  32. */
  33. #ifndef IMULTIPLY
  34. #ifdef MULDIV64
  35. /*
  36. * If MULDIV64 is asserted this function in not in fact needed
  37. * since a macro in arith.h arranges that the multiplication is done
  38. * in-line. However this version is left here in the source code as
  39. * convenient documentation of what the function needs to do.
  40. */
  41. unsigned32 Imultiply(unsigned32 *rlow, unsigned32 a, unsigned32 b, unsigned32 c)
  42. /*
  43. * (result, *rlow) = a*b + c as 62-bit product
  44. *
  45. * rlow may well be the same as one of a, b or c so the
  46. * assignment to *rlow must not be done until the calculation is
  47. * complete. Inputs a and b are 31-bit values (i.e. they have
  48. * their top bit zero and are to be treated as positive value.
  49. * c may use all 32 bits, but again is treated as unsigned.
  50. * The result is computed and the low 31 bits placed in rlow,
  51. * (the top bit of rlow will end up zero) and the top part
  52. * of the result is returned.
  53. */
  54. {
  55. /* NB the value r cound be int64 or unsigned64 - it does not matter */
  56. unsigned64 r = (unsigned64)a*(unsigned64)b + (unsigned64)c;
  57. *rlow = (unsigned32)(r & 0x7fffffff);
  58. return (unsigned32)(r >> 31);
  59. }
  60. #else
  61. #ifdef OLDER_VERSION
  62. unsigned32 Imultiply(unsigned32 *rlow, unsigned32 a, unsigned32 b, unsigned32 c)
  63. /*
  64. * (result, *rlow) = a*b + c as 62-bit product
  65. *
  66. * The code given here forms the produce by doing four single-precision
  67. * (16*16->32) multiplies and using shifts etc to glue the partial results
  68. * together.
  69. */
  70. {
  71. unsigned32 ah = a >> 16, bh = b >> 16, ch = c >> 16;
  72. unsigned32 w0, w1, w2, w3, w4;
  73. a -= ah << 16;
  74. b -= bh << 16;
  75. c -= ch << 16; /* a, b and c now split into high/low parts */
  76. w0 = a*b; /* One... */
  77. w1 = w0 >> 16;
  78. w0 = w0 - (w1 << 16) + c;
  79. w1 += ch;
  80. w2 = a*bh; /* Two... */
  81. w3 = w2 >> 16;
  82. w1 += w2 - (w3 << 16);
  83. w2 = ah*b; /* Three... */
  84. w4 = w2 >> 16;
  85. w1 += w2 - (w4 << 16);
  86. w2 = w0 >> 16;
  87. w1 += w2;
  88. w0 -= w2 << 16;
  89. w2 = ah*bh + w3 + w4; /* Four 16-bit multiplies done in all. */
  90. w2 += w1 >> 16;
  91. /* Here I do a minor shift to split the result at bit 31 rather than 32 */
  92. *rlow = w0 + ((w1 & 0x7fff) << 16);
  93. return (w2<<1) + ((w1>>15) & 1);
  94. }
  95. #else
  96. unsigned32 Imultiply(unsigned32 *rlow, unsigned32 a, unsigned32 b, unsigned32 c)
  97. /*
  98. * (result, *rlow) = a*b + c as 62-bit product
  99. *
  100. * The code given here forms the produce by doing three single-precision
  101. * (16*16->32) multiplies and using shifts etc to glue the partial results
  102. * together. This is slightly faster than the above (maybe simpler?) code
  103. * on at least some machines.
  104. */
  105. {
  106. unsigned32 ah, bh;
  107. unsigned32 w0, w1, w2;
  108. ah = a >> 16;
  109. /*
  110. * On some machines I know that multi-bit shifts are especially painful,
  111. * while on others it is nasty to access the literal value needed as a
  112. * mask here. Hence I make some show of providing an alternative.
  113. */
  114. #ifdef FAST_SHIFTS
  115. a -= ah << 16;
  116. #else
  117. a &= 0xffff;
  118. #endif
  119. bh = b >> 16;
  120. #ifdef FAST_SHIFTS
  121. b -= bh << 16;
  122. #else
  123. b &= 0xffff;
  124. #endif
  125. /*
  126. * At present I can not see any way of issuing any of these multiplies
  127. * any earlier, or of doing useful work between the times that I launch
  128. * them, or even delaying before I use their results. This will be rather
  129. * sad on machines where multiplication could overlap with other operations.
  130. */
  131. w2 = ah*bh;
  132. w1 = (a + ah)*(b + bh);
  133. w0 = a*b;
  134. /*
  135. * The largest exact result that can be computed by the next line (given
  136. * that a and b start off just 31 bit) is 0xfffd0002
  137. */
  138. w1 = (w1 - w2) - w0; /* == (a*bh + b*ah) */
  139. /*
  140. * I split into 30 bits in the lower word and 32 in the upper, so I have
  141. * 2 bits available for temporary carry effects in the lower part.
  142. */
  143. w2 = (w2 << 2) + (w1 >> 14) + (w0 >> 30) + (c >> 30);
  144. w1 &= 0x3fff;
  145. w0 = (c & 0x3fffffff) + (w0 & 0x3fffffffU) + (w1 << 16);
  146. w0 += ((w2 & 1) << 30);
  147. w2 = (w2 >> 1) + (w0 >> 31);
  148. *rlow = w0 & 0x7fffffffU;
  149. return w2;
  150. }
  151. #endif
  152. #endif
  153. #endif /* IMULTIPLY */
  154. static Lisp_Object timesii(Lisp_Object a, Lisp_Object b)
  155. /*
  156. * multiplying two fixnums together is much messier than adding them,
  157. * mainly because the result can easily be a two-word bignum
  158. */
  159. {
  160. unsigned32 aa = (unsigned32)int_of_fixnum(a),
  161. bb = (unsigned32)int_of_fixnum(b);
  162. unsigned32 temp, low, high;
  163. /*
  164. * Multiplication by 0 or by -1 is just possibly common enough to be worth
  165. * filtering out the following special cases. Avoidance of the tedious
  166. * checks for overflow may make this useful even if Imultiply is very fast.
  167. */
  168. if (aa <= 1)
  169. { if (aa == 0) return fixnum_of_int(0);
  170. else return b;
  171. }
  172. else if (bb <= 1)
  173. { if (bb == 0) return fixnum_of_int(0);
  174. else return a;
  175. }
  176. /*
  177. * I dump the low part of the product in temp then copy to a variable low
  178. * because temp has to have its address taken and so is not a candidate for
  179. * living in a register.
  180. */
  181. Dmultiply(high, temp, clear_top_bit(aa), clear_top_bit(bb), 0);
  182. /*
  183. * The result handed back here has only 31 bits active in its low part.
  184. */
  185. low = temp;
  186. /*
  187. * The next two lines convert the unsigned product produced by Imultiply
  188. * into a signed product.
  189. */
  190. if ((int32)aa < 0) high -= bb;
  191. if ((int32)bb < 0) high -= aa;
  192. if ((high & 0x40000000) == 0) high = clear_top_bit(high);
  193. else high = set_top_bit(high);
  194. if (high == 0 && (low & 0x40000000) == 0)
  195. { /* one word positive result */
  196. if (low <= 0x07ffffff) return fixnum_of_int(low);
  197. else return make_one_word_bignum(low);
  198. }
  199. else if (high == -1 && (low & 0x40000000) != 0)
  200. { /* one word negative result */
  201. low = set_top_bit(low);
  202. if ((low & fix_mask) == fix_mask) return fixnum_of_int(low);
  203. else return make_one_word_bignum(low);
  204. }
  205. else
  206. { /* two-word bignum result needed */
  207. Lisp_Object w = getvector(TAG_NUMBERS, TYPE_BIGNUM, 12), nil;
  208. errexit();
  209. ((int32 *)((char *)w - TAG_NUMBERS))[1] = low;
  210. ((int32 *)((char *)w - TAG_NUMBERS))[2] = high;
  211. ((int32 *)((char *)w - TAG_NUMBERS))[3] = 0; /* padder word */
  212. return w;
  213. }
  214. }
  215. #ifdef COMMON
  216. static Lisp_Object timesis(Lisp_Object a, Lisp_Object b)
  217. {
  218. Float_union bb;
  219. bb.i = b - TAG_SFLOAT;
  220. bb.f = (float) ((float)int_of_fixnum(a) * bb.f);
  221. return (bb.i & ~(int32)0xf) + TAG_SFLOAT;
  222. }
  223. #endif
  224. static Lisp_Object timesib(Lisp_Object a, Lisp_Object b)
  225. {
  226. int32 aa = int_of_fixnum(a), lenb, i;
  227. unsigned32 carry, ms_dig, w;
  228. Lisp_Object c, nil;
  229. /*
  230. * I will split off the (easy) cases of the fixnum being -1, 0 or 1.
  231. */
  232. if (aa == 0) return fixnum_of_int(0);
  233. else if (aa == 1) return b;
  234. else if (aa == -1) return negateb(b);
  235. lenb = bignum_length(b);
  236. push(b);
  237. c = getvector(TAG_NUMBERS, TYPE_BIGNUM, lenb);
  238. pop(b);
  239. errexit();
  240. lenb = (lenb >> 2) - 1;
  241. if (aa < 0)
  242. { aa = -aa;
  243. carry = 0xffffffffU;
  244. for (i=0; i<lenb-1; i++)
  245. { carry = clear_top_bit(~bignum_digits(b)[i]) + top_bit(carry);
  246. bignum_digits(c)[i] = clear_top_bit(carry);
  247. }
  248. /*
  249. * I do the most significant digit separately.
  250. */
  251. carry = clear_top_bit(~bignum_digits(b)[i]) + top_bit(carry);
  252. /*
  253. * there is a special case needed here - if b started off as a number
  254. * like 0xc0000000,0,0,0 then negating it would call for extension of
  255. * the bignum c (this is the usual assymetry in the range of twos-
  256. * complement numbers). But if I detect that case specially I can
  257. * observe that the ONLY case where negation overflows is where the
  258. * negated value is exactly a power of 2 such .. an easy thing to
  259. * multiply a by! Furthermore the power of two involved is known to
  260. * by 30 mod 31.
  261. */
  262. if (carry == 0x40000000)
  263. { bignum_digits(c)[i] = (aa & 1) << 30;
  264. aa = aa >> 1;
  265. goto extend_by_one_word;
  266. }
  267. if ((carry & 0x40000000) != 0) carry = set_top_bit(carry);
  268. bignum_digits(c)[i] = carry;
  269. }
  270. else
  271. { for (i=0; i<lenb; i++) bignum_digits(c)[i] = bignum_digits(b)[i];
  272. }
  273. /*
  274. * Now c is a copy of b (negated if necessary) and I just want to
  275. * multiply it by the positive value a. This is the heart of the
  276. * procedure. Re-write Imultiply in assembly code if you want it
  277. * to go faster. See the top of this file for a portable sample
  278. * implementation of Imultiply, which gives back a result as a pair
  279. * of 31-bit values.
  280. */
  281. carry = 0;
  282. /*
  283. * here aa is > 0, and a fortiori the 0x80000000 bit of aa is clear,
  284. * so I do not have to worry about the difference between 31 and 32
  285. * bit values for aa.
  286. */
  287. for (i=0; i<lenb-1; i++)
  288. Dmultiply(carry, bignum_digits(c)[i], bignum_digits(c)[i],
  289. (unsigned32)aa, carry);
  290. ms_dig = bignum_digits(c)[i];
  291. Dmultiply(carry, w, clear_top_bit(ms_dig), (unsigned32)aa, carry);
  292. /*
  293. * After forming the product to (lenb) digits I need to see if there
  294. * is any overflow. Calculate what the next digit would be, sign
  295. * extending into the 0x80000000 bit as necessary.
  296. */
  297. if ((carry & 0x40000000) != 0) carry = set_top_bit(carry);
  298. if (((int32)ms_dig) < 0) carry -= aa;
  299. aa = (int32)carry;
  300. if (aa == -1 && (w & 0x40000000) != 0)
  301. { bignum_digits(c)[i] = set_top_bit(w);
  302. return c;
  303. }
  304. bignum_digits(c)[i] = w;
  305. if (aa == 0 && (w & 0x40000000) == 0) return c;
  306. /*
  307. * drop through to extend the number by a word - note that because I
  308. * am multiplying by a fixnum it is only possible to have to expand by
  309. * just one word.
  310. */
  311. extend_by_one_word:
  312. if ((lenb & 1) == 0)
  313. /*
  314. * Here there was a padder word that I can expand into.
  315. */
  316. { bignum_digits(c)[lenb] = aa;
  317. numhdr(c) += pack_hdrlength(1);
  318. return c;
  319. }
  320. /*
  321. * Need to allocate more space to grow into.
  322. */
  323. push(c);
  324. a = getvector(TAG_NUMBERS, TYPE_BIGNUM, (lenb<<2)+8);
  325. pop(c);
  326. errexit();
  327. for (i=0; i<lenb; i++)
  328. bignum_digits(a)[i] = bignum_digits(c)[i];
  329. bignum_digits(a)[i] = aa;
  330. bignum_digits(a)[i+1] = 0; /* the padder word */
  331. return a;
  332. }
  333. #ifdef COMMON
  334. static Lisp_Object timesir(Lisp_Object a, Lisp_Object b)
  335. /*
  336. * multiply integer (fixnum or bignum) by ratio.
  337. */
  338. {
  339. Lisp_Object nil = C_nil;
  340. Lisp_Object w = nil;
  341. if (a == fixnum_of_int(0)) return a;
  342. else if (a == fixnum_of_int(1)) return b;
  343. push3(b, a, nil);
  344. #define g stack[0]
  345. #define a stack[-1]
  346. #define b stack[-2]
  347. g = gcd(a, denominator(b));
  348. nil = C_nil;
  349. if (exception_pending()) goto fail;
  350. a = quot2(a, g);
  351. nil = C_nil;
  352. if (exception_pending()) goto fail;
  353. g = quot2(denominator(b), g);
  354. nil = C_nil;
  355. if (exception_pending()) goto fail;
  356. a = times2(a, numerator(b));
  357. nil = C_nil;
  358. if (exception_pending()) goto fail;
  359. /*
  360. * make_ratio tidies things up if the denominator was exactly 1
  361. */
  362. w = make_ratio(a, g);
  363. fail:
  364. popv(3);
  365. return w;
  366. #undef a
  367. #undef b
  368. #undef g
  369. }
  370. static Lisp_Object timesic(Lisp_Object a, Lisp_Object b)
  371. /*
  372. * multiply an arbitrary non-complex number by a complex one
  373. */
  374. {
  375. Lisp_Object nil;
  376. Lisp_Object r = real_part(b), i = imag_part(b);
  377. push2(a, r);
  378. i = times2(a, i);
  379. pop2(r, a);
  380. errexit();
  381. push(i);
  382. r = times2(a, r);
  383. pop(i);
  384. errexit();
  385. return make_complex(r, i);
  386. }
  387. #endif
  388. static Lisp_Object timesif(Lisp_Object a, Lisp_Object b)
  389. {
  390. double d = (double)int_of_fixnum(a) * float_of_number(b);
  391. return make_boxfloat(d, type_of_header(flthdr(b)));
  392. }
  393. #ifdef COMMON
  394. #define timessi(a, b) timesis(b, a)
  395. static Lisp_Object timessb(Lisp_Object a, Lisp_Object b)
  396. {
  397. double d = float_of_number(a) * float_of_number(b);
  398. return make_sfloat(d);
  399. }
  400. #define timessr(a, b) timessb(a, b)
  401. #define timessc(a, b) timesic(a, b)
  402. #endif
  403. static Lisp_Object timessf(Lisp_Object a, Lisp_Object b)
  404. {
  405. double d = float_of_number(a) * float_of_number(b);
  406. return make_boxfloat(d, type_of_header(flthdr(b)));
  407. }
  408. #define timesbi(a, b) timesib(b, a)
  409. #ifdef COMMON
  410. #define timesbs(a, b) timessb(b, a)
  411. #endif
  412. /*
  413. * Now for bignum multiplication - made more than comfortably complicated
  414. * by a desire to make it go fast for very big numbers.
  415. */
  416. #ifndef KARATSUBA_CUTOFF
  417. /*
  418. * I have conducted some experiments on one machine to find out what the
  419. * best cut-off value here is. The exact value chosen is not very
  420. * critical, and the fancy techniques do not pay off until numbers get
  421. * a lot bigger than this length (which is expressed in units of 31-bit
  422. * words, i.e. about 10 decimals). Anyone who wants may recompile with
  423. * alternative values to try to get the system fine-tuned for their
  424. * own computer - but I do not expect it to be possible to achieve much
  425. * by so doing.
  426. */
  427. #define KARATSUBA_CUTOFF 12
  428. #endif
  429. static void long_times(unsigned32 *c, unsigned32 *a, unsigned32 *b,
  430. unsigned32 *d, int32 lena, int32 lenb, int32 lenc);
  431. static void long_times1(unsigned32 *c, unsigned32 *a, unsigned32 *b,
  432. unsigned32 *d, int32 lena, int32 lenb, int32 lenc)
  433. /*
  434. * Here both a and b are big, with lena <= lenb. Split each into two chunks
  435. * of size (lenc/4), say (a1,a2) and (b1,b2), and compute each of
  436. * a1*b1
  437. * a2*b2
  438. * (a1+a2)*(b1+b2)
  439. * where in the last case a1+a2 and b1+b2 can be computed keeping their
  440. * carry bits as k1 and k2 (so that a1+a2 and b1+b2 are restricted to
  441. * size lenb). The chunks can then be combined with a few additions and
  442. * subtractions to form the product that is really wanted. If in fact a
  443. * was shorter than lenc/4 (so that after a is split up the top half is
  444. * all zero) I do things in a more straightforward way. I require that on
  445. * entry to this code lenc<4 < lenb <= lenc/2.
  446. */
  447. {
  448. int32 h = lenc/4; /* lenc must have been made even enough... */
  449. int32 lena1 = lena - h;
  450. int32 lenb1 = lenb - h;
  451. unsigned32 carrya, carryb;
  452. int32 i;
  453. /*
  454. * if the top half of a would be all zero I go through a separate path,
  455. * doing just two subsidiary multiplications.
  456. */
  457. if (lena1 <= 0)
  458. { long_times(c+h, a, b+h, d, lena, lenb-h, 2*h);
  459. for (i=0; i<h; i++) c[3*h+i] = 0;
  460. long_times(d, a, b, c, lena, h, 2*h);
  461. for (i=0; i<h; i++) c[i] = d[i];
  462. carrya = 0;
  463. for (;i<2*h; i++)
  464. { unsigned32 w = c[i] + d[i] + carrya;
  465. c[i] = clear_top_bit(w);
  466. carrya = w >> 31;
  467. }
  468. for (;carrya!=0;i++)
  469. { unsigned32 w = c[i] + 1;
  470. c[i] = clear_top_bit(w);
  471. carrya = w >> 31;
  472. }
  473. return;
  474. }
  475. /*
  476. * form (a1+a2) and (b1+b2).
  477. */
  478. carrya = 0;
  479. for (i=0; i<h; i++)
  480. { unsigned32 w = a[i] + carrya;
  481. if (i < lena1) w += a[h+i];
  482. d[i] = clear_top_bit(w);
  483. carrya = w >> 31;
  484. }
  485. carryb = 0;
  486. for (i=0; i<h; i++)
  487. { unsigned32 w = b[i] + carryb;
  488. if (i < lenb1) w += b[h+i];
  489. d[h+i] = clear_top_bit(w);
  490. carryb = w >> 31;
  491. }
  492. long_times(c+h, d, d+h, c, h, h, 2*h);
  493. /*
  494. * Adjust to allow for the cases of a1+a2 or b1+b2 overflowing
  495. * by a single bit.
  496. */
  497. c[3*h] = carrya & carryb;
  498. if (carrya != 0)
  499. { carrya = 0;
  500. for (i=0; i<h; i++)
  501. { unsigned32 w = c[2*h+i] + d[h+i] + carrya;
  502. c[2*h+i] = clear_top_bit(w);
  503. carrya = w >> 31;
  504. }
  505. }
  506. if (carryb != 0)
  507. { carryb = 0;
  508. for (i=0; i<h; i++)
  509. { unsigned32 w = c[2*h+i] + d[i] + carryb;
  510. c[2*h+i] = clear_top_bit(w);
  511. carryb = w >> 31;
  512. }
  513. }
  514. c[3*h] += carrya + carryb;
  515. for (i=1; i<h; i++) c[3*h+i] = 0;
  516. /*
  517. * Now (a1+a2)*(b1+b2) should have been computed totally properly
  518. */
  519. for (i=0; i<h; i++) d[h+i] = 0;
  520. /*
  521. * multiply out a1*b1, where note that a1 and b1 may be less long
  522. * than h, but not by much.
  523. */
  524. long_times(d, a+h, b+h, c, lena-h, lenb-h, 2*h);
  525. carrya = 0;
  526. for (i=0; i<2*h; i++)
  527. { unsigned32 w = c[2*h+i] + d[i] + carrya;
  528. c[2*h+i] = clear_top_bit(w);
  529. carrya = w >> 31;
  530. }
  531. carrya = 0;
  532. for (i=0; i<2*h; i++)
  533. { unsigned32 w = c[h+i] - d[i] - carrya;
  534. c[h+i] = clear_top_bit(w);
  535. carrya = w >> 31;
  536. }
  537. for (; carrya!=0 && i<3*h; i++)
  538. { unsigned32 w = c[h+i] - 1;
  539. c[h+i] = clear_top_bit(w);
  540. carrya = w >> 31;
  541. }
  542. /*
  543. * multiply out a2*b2
  544. */
  545. long_times(d, a, b, c, h, h, 2*h);
  546. for (i=0; i<h; i++) c[i] = d[i];
  547. carrya = 0;
  548. for (; i<2*h; i++)
  549. { unsigned32 w = c[i] + d[i] + carrya;
  550. c[i] = clear_top_bit(w);
  551. carrya = w >> 31;
  552. }
  553. for (; carrya!=0 && i<4*h; i++)
  554. { unsigned32 w = c[i] + 1;
  555. c[i] = clear_top_bit(w);
  556. carrya = w >> 31;
  557. }
  558. carrya = 0;
  559. for (i=0; i<2*h; i++)
  560. { unsigned32 w = c[h+i] - d[i] - carrya;
  561. c[h+i] = clear_top_bit(w);
  562. carrya = w >> 31;
  563. }
  564. for (; carrya!=0 && i<3*h; i++)
  565. { unsigned32 w = c[h+i] - 1;
  566. c[h+i] = clear_top_bit(w);
  567. carrya = w >> 31;
  568. }
  569. /*
  570. * The product is now complete
  571. */
  572. }
  573. static void long_times2(unsigned32 *c, unsigned32 *a, unsigned32 *b,
  574. int32 lena, int32 lenb, int32 lenc)
  575. /*
  576. * This case is standard old fashioned long multiplication. Dump the
  577. * result into c.
  578. */
  579. {
  580. int32 i;
  581. for (i=0; i<lenc; i++) c[i] = 0;
  582. for (i=0; i<lena; i++)
  583. { unsigned32 carry = 0, da = a[i];
  584. int32 j;
  585. /*
  586. * When I multiply by (for instance) a high power of 2 there will
  587. * be plenty of zero digits in the number being worked with, and
  588. * so the test da!=0 will save something useful.
  589. */
  590. if (da != 0)
  591. { for (j=0; j<lenb; j++)
  592. { int32 k = i + j;
  593. Dmultiply(carry, c[k], da, b[j],
  594. /* NB the addition here is OK and fits into a 32-bit unsigned result */
  595. carry + c[k]);
  596. }
  597. c[i+j] = carry;
  598. }
  599. }
  600. }
  601. static void long_times(unsigned32 *c, unsigned32 *a, unsigned32 *b,
  602. unsigned32 *d, int32 lena, int32 lenb, int32 lenc)
  603. /*
  604. * This decides if a multiplication is big enough to benefit from
  605. * decomposition a la Karatsuba.
  606. * In recursive entries through here out of long_times1() the numbers a
  607. * and b may have shrunk in ways that mean I need to reconsider the
  608. * precision to which I am working. This must leave c filled out all
  609. * the way to lenc, with padding 0s if necessary.
  610. */
  611. {
  612. if (lenb < lena)
  613. { unsigned32 *t1;
  614. int32 t2;
  615. t1 = a; a = b; b = t1;
  616. t2 = lena; lena = lenb; lenb = t2;
  617. }
  618. if (4*lenb <= lenc) /* In this case I should shrink lenc a bit.. */
  619. { int32 newlenc = (lenb+1)/2;
  620. int k = 0;
  621. while (newlenc > KARATSUBA_CUTOFF)
  622. { newlenc = (newlenc + 1)/2;
  623. k++;
  624. }
  625. while (k != 0)
  626. { newlenc = 2*newlenc;
  627. k--;
  628. }
  629. newlenc = 4*newlenc;
  630. while (lenc > newlenc) c[--lenc] = 0;
  631. }
  632. if (lena > KARATSUBA_CUTOFF) long_times1(c, a, b, d, lena, lenb, lenc);
  633. else long_times2(c, a, b, lena, lenb, lenc);
  634. }
  635. static Lisp_Object timesbb(Lisp_Object a, Lisp_Object b)
  636. /*
  637. * a and b are both guaranteed to be bignums when I call this
  638. * procedure.
  639. */
  640. {
  641. int sign = 1;
  642. Lisp_Object c, d, nil;
  643. int32 lena, lenb, lenc, i;
  644. lena = (bignum_length(a) >> 2) - 1;
  645. lenb = (bignum_length(b) >> 2) - 1;
  646. if (lena == 1 && lenb == 1)
  647. /*
  648. * I am going to deem multiplication of two one-word bignums worthy of
  649. * a special case, since it is probably fairly common and it will be cheap
  650. * enough that avoiding overheads might matter. I still need to split
  651. * off the signs, because Imultiply can only deal with 31-bit unsigned values.
  652. * One-word bignums each have value at least 2^27 (or else they would have
  653. * been represented as fixnums) so the result here will always be a two-word
  654. * bignum.
  655. */
  656. { int32 va = (int32)bignum_digits(a)[0],
  657. vb = (int32)bignum_digits(b)[0], vc;
  658. unsigned32 vclow;
  659. if (va < 0)
  660. { if (vb < 0) Dmultiply(vc, vclow, -va, -vb, 0);
  661. else
  662. { Dmultiply(vc, vclow, -va, vb, 0);
  663. if (vclow == 0) vc = -vc;
  664. else
  665. { vclow = clear_top_bit(-(int32)vclow);
  666. vc = ~vc;
  667. }
  668. }
  669. }
  670. else if (vb < 0)
  671. { Dmultiply(vc, vclow, va, -vb, 0);
  672. if (vclow == 0) vc = -vc;
  673. else
  674. { vclow = clear_top_bit(-(int32)vclow);
  675. vc = ~vc;
  676. }
  677. }
  678. else Dmultiply(vc, vclow, va, vb, 0);
  679. return make_two_word_bignum(vc, vclow);
  680. }
  681. /*
  682. * I take the absolute values of the two input values a and b,
  683. * recording what the eventual sign for the product will need to be.
  684. */
  685. if (((int32)bignum_digits(a)[lena-1]) < 0)
  686. { sign = -sign;
  687. push(b);
  688. /*
  689. * Negating a negative bignum can sometimes mean that it will
  690. * have to get longer (because of the twos complement assymmetry),
  691. * but can never cause it to shrink, In particular it can never lead
  692. * to demotion to a fixnum, so after this call to negateb it is still
  693. * OK to assume that a is a bignum.
  694. */
  695. a = negateb(a);
  696. pop(b);
  697. errexit();
  698. lena = (bignum_length(a) >> 2) - 1;
  699. }
  700. if (((int32)bignum_digits(b)[lenb-1]) < 0)
  701. { sign = -sign;
  702. push(a);
  703. /* see above comments about negateb */
  704. b = negateb(b);
  705. pop(a);
  706. errexit();
  707. lenb = (bignum_length(b) >> 2) - 1;
  708. }
  709. if (lenb < lena) /* Commute so that b is at least as long as a */
  710. { c = a;
  711. a = b;
  712. b = c;
  713. lenc = lena;
  714. lena = lenb;
  715. lenb = lenc;
  716. }
  717. push2(a, b);
  718. /*
  719. * For very big numbers I have two special actions called for here. First
  720. * I must round up the size of the result vector to have a big enough power
  721. * of two as a factor so that (recursive) splitting in two does not cause
  722. * trouble later. Then I have to allocate some workspace, the size of that
  723. * being related to the size of the numbers being handled.
  724. */
  725. if (lena > KARATSUBA_CUTOFF)
  726. {
  727. int k = 0;
  728. /*
  729. * I pad lenc up to have a suitably large power of 2 as a factor so
  730. * that splitting numbers in half works neatly for me.
  731. */
  732. lenc = (lenb+1)/2; /* rounded up half-length of longer number */
  733. while (lenc > KARATSUBA_CUTOFF)
  734. { lenc = (lenc + 1)/2;
  735. k++;
  736. }
  737. while (k != 0)
  738. { lenc = 2*lenc;
  739. k--;
  740. }
  741. lenc = 2*lenc;
  742. c = getvector(TAG_NUMBERS, TYPE_BIGNUM, (1 + 2*lenc) << 2);
  743. errexitn(2);
  744. /*
  745. * The next line seems pretty shameless, but it may reduce the amount of
  746. * garbage collection I do. When the workspace vector needed is short enough
  747. * (at present up to 256 bytes) I use the character assembly buffer (boffo)
  748. * as my workspace, relying on an expectation that bignumber multiplication
  749. * can never be triggered from places within the reader where that buffer is
  750. * in use for its normal purpose. I forge tag bits to make boffo look like
  751. * a number here, but can never trigger garbage collection in a way that
  752. * might inspect same, so that too is safe at present.
  753. */
  754. if (((1 + lenc) << 2) <= (int32)length_of_header(vechdr(boffo)))
  755. d = (Lisp_Object)((char *)boffo + TAG_NUMBERS - TAG_VECTOR);
  756. else
  757. { push(c);
  758. d = getvector(TAG_NUMBERS, TYPE_BIGNUM, (1 + lenc) << 2);
  759. pop(c);
  760. }
  761. lenc = 2*lenc;
  762. }
  763. else
  764. {
  765. /*
  766. * In cases where I will use classical long multiplication there is no
  767. * need to waste space with extra padding or with the workspace vector d.
  768. */
  769. lenc = lena + lenb;
  770. c = getvector(TAG_NUMBERS, TYPE_BIGNUM, (1 + lenc) << 2);
  771. d = c; /* set d to avoid dataflow anomaly */
  772. }
  773. pop2(b, a);
  774. errexit();
  775. { unsigned32 *da = &bignum_digits(a)[0],
  776. *db = &bignum_digits(b)[0],
  777. *dc = &bignum_digits(c)[0],
  778. *dd = &bignum_digits(d)[0];
  779. long_times(dc, da, db, dd, lena, lenb, lenc);
  780. }
  781. /*
  782. * Here the absolute value of the product has been computed properly.
  783. * The result can easily have a zero top digit, which will need trimming
  784. * off. If at least one of the input values was a number which had to
  785. * be represented with a zero leading digit (e.g. 0x40000000) then the
  786. * product can have two leading zero digits here. Similarly for negative
  787. * results. Also padding (to allow splitting numbers into two) can have
  788. * resulted in extra padding up at the top. lenc gives the size of vector
  789. * that was allocated, lena+lenb is a much better guess of how much data
  790. * is active in it.
  791. */
  792. errexit();
  793. { int32 newlenc = lena + lenb;
  794. /*
  795. * I tidy up by putting a zero in any padding word above the top of the
  796. * active data, and by inserting a header in space that gets trimmed off
  797. * in such a way that the garbage collector will not get upset. This
  798. * all just roughly trims the numbers - fine adjustment follows.
  799. */
  800. if ((newlenc & 1) == 0)
  801. { bignum_digits(c)[newlenc] = 0;
  802. if (lenc != newlenc) /* i.e. I padded out somewhat */
  803. {
  804. bignum_digits(c)[newlenc+1] = make_bighdr(lenc-newlenc);
  805. lenc = newlenc;
  806. numhdr(c) = make_bighdr(lenc+1);
  807. }
  808. }
  809. else if (lenc != newlenc) /* i.e. I padded out somewhat */
  810. {
  811. bignum_digits(c)[newlenc] = make_bighdr(lenc-newlenc+1);
  812. lenc = newlenc;
  813. numhdr(c) = make_bighdr(lenc+1);
  814. }
  815. }
  816. /*
  817. * Now I am safe against the garbage collector, and the number c has as
  818. * its length just lena+lenb, even if it had been padded out earlier.
  819. */
  820. if (sign < 0)
  821. { unsigned32 carry = 0xffffffffU;
  822. for (i=0; i<lenc-1; i++)
  823. { carry = clear_top_bit(~bignum_digits(c)[i]) + top_bit(carry);
  824. bignum_digits(c)[i] = clear_top_bit(carry);
  825. }
  826. carry = ~bignum_digits(c)[i] + top_bit(carry);
  827. if (carry != -1)
  828. { bignum_digits(c)[i] = carry;
  829. return c; /* no truncation needed */
  830. }
  831. carry = bignum_digits(c)[i-1];
  832. if ((carry & 0x40000000) == 0)
  833. { bignum_digits(c)[i] = 0xffffffffU;
  834. return c; /* no truncation becase of previous digit */
  835. }
  836. /*
  837. * I need to argue that lenc was at least 2, so bignum_digits(c)[i-2]
  838. * could at worst access the header word of the bignum - but it can never
  839. * do that because if it were doing so then the bignum product would
  840. * be about to have a value zero or thereabouts. One-word bignums are not
  841. * allowed to have leading zero digits.
  842. */
  843. if (carry == 0x7fffffff &&
  844. (bignum_digits(c)[i-2] & 0x40000000) != 0) /* chop 2 */
  845. { bignum_digits(c)[i-2] |= ~0x7fffffff;
  846. /*
  847. * I common up the code to chop off two words from the number at label "chop2"
  848. */
  849. goto chop2;
  850. }
  851. bignum_digits(c)[i-1] |= ~0x7fffffff;
  852. /* Drop through to truncate by 1 and sometimes that is easy */
  853. }
  854. else
  855. { unsigned32 w = bignum_digits(c)[lenc-1];
  856. if (w != 0) return c; /* no truncation */
  857. w = bignum_digits(c)[lenc-2];
  858. if ((w & 0x40000000) != 0) return c;
  859. if (w == 0 &&
  860. (bignum_digits(c)[lenc-3] & 0x40000000) == 0) goto chop2;
  861. /* truncate one word */
  862. }
  863. /*
  864. * here the data in the bignum is all correct (even in the most significant
  865. * digit) but I need to shrink the number by one word. Because of all the
  866. * doubleword alignment that is used here this can sometimes be done very
  867. * easily, and other times it involves forging a short bit of dummy data
  868. * to fill in a gap that gets left in the heap.
  869. */
  870. numhdr(c) -= pack_hdrlength(1);
  871. if ((lenc & 1) != 0) bignum_digits(c)[lenc-1] = 0; /* tidy up */
  872. else bignum_digits(c)[lenc-1] = make_bighdr(2);
  873. return c;
  874. chop2:
  875. /*
  876. * Trim two words from the number c
  877. */
  878. numhdr(c) -= pack_hdrlength(2);
  879. lenc -= 2;
  880. bignum_digits(c)[lenc] = 0;
  881. lenc |= 1;
  882. bignum_digits(c)[lenc] = make_bighdr(2);
  883. return c;
  884. }
  885. #ifdef COMMON
  886. #define timesbr(a, b) timesir(a, b)
  887. #define timesbc(a, b) timesic(a, b)
  888. #endif
  889. #define timesbf(a, b) timessf(a, b)
  890. #ifdef COMMON
  891. #define timesri(a, b) timesir(b, a)
  892. #define timesrs(a, b) timessr(b, a)
  893. #define timesrb(a, b) timesbr(b, a)
  894. static Lisp_Object timesrr(Lisp_Object a, Lisp_Object b)
  895. /*
  896. * multiply a pair of rational numbers
  897. */
  898. {
  899. Lisp_Object nil = C_nil;
  900. Lisp_Object w = nil;
  901. push5(numerator(a), denominator(a),
  902. numerator(b), denominator(b), nil);
  903. #define g stack[0]
  904. #define db stack[-1]
  905. #define nb stack[-2]
  906. #define da stack[-3]
  907. #define na stack[-4]
  908. g = gcd(na, db);
  909. nil = C_nil;
  910. if (exception_pending()) goto fail;
  911. na = quot2(na, g);
  912. nil = C_nil;
  913. if (exception_pending()) goto fail;
  914. db = quot2(db, g);
  915. nil = C_nil;
  916. if (exception_pending()) goto fail;
  917. g = gcd(nb, da);
  918. nil = C_nil;
  919. if (exception_pending()) goto fail;
  920. nb = quot2(nb, g);
  921. nil = C_nil;
  922. if (exception_pending()) goto fail;
  923. da = quot2(da, g);
  924. nil = C_nil;
  925. if (exception_pending()) goto fail;
  926. na = times2(na, nb);
  927. nil = C_nil;
  928. if (exception_pending()) goto fail;
  929. da = times2(da, db);
  930. nil = C_nil;
  931. if (exception_pending()) goto fail;
  932. w = make_ratio(na, da);
  933. fail:
  934. popv(5);
  935. return w;
  936. #undef g
  937. #undef db
  938. #undef nb
  939. #undef da
  940. #undef na
  941. }
  942. #define timesrc(a, b) timesic(a, b)
  943. #define timesrf(a, b) timessf(a, b)
  944. #define timesci(a, b) timesic(b, a)
  945. #define timescs(a, b) timessc(b, a)
  946. #define timescb(a, b) timesbc(b, a)
  947. #define timescr(a, b) timesrc(b, a)
  948. static Lisp_Object timescc(Lisp_Object a, Lisp_Object b)
  949. /*
  950. * multiply a pair of complex values
  951. */
  952. {
  953. Lisp_Object nil = C_nil;
  954. Lisp_Object w = nil;
  955. push4(real_part(a), imag_part(a),
  956. real_part(b), imag_part(b));
  957. push2(nil, nil);
  958. #define u stack[0]
  959. #define v stack[-1]
  960. #define ib stack[-2]
  961. #define rb stack[-3]
  962. #define ia stack[-4]
  963. #define ra stack[-5]
  964. u = times2(ra, rb);
  965. nil = C_nil;
  966. if (exception_pending()) goto fail;
  967. v = times2(ia, ib);
  968. nil = C_nil;
  969. if (exception_pending()) goto fail;
  970. v = negate(v);
  971. nil = C_nil;
  972. if (exception_pending()) goto fail;
  973. u = plus2(u, v); /* real part of result */
  974. nil = C_nil;
  975. if (exception_pending()) goto fail;
  976. v = times2(ra, ib);
  977. nil = C_nil;
  978. if (exception_pending()) goto fail;
  979. ib = times2(rb, ia);
  980. nil = C_nil;
  981. if (exception_pending()) goto fail;
  982. v = plus2(v, ib); /* imaginary part */
  983. nil = C_nil;
  984. if (exception_pending()) goto fail;
  985. w = make_complex(u, v);
  986. fail:
  987. popv(6);
  988. return w;
  989. #undef u
  990. #undef v
  991. #undef ib
  992. #undef rb
  993. #undef ia
  994. #undef ra
  995. }
  996. #define timescf(a, b) timesci(a, b)
  997. #endif
  998. #define timesfi(a, b) timesif(b, a)
  999. #ifdef COMMON
  1000. #define timesfs(a, b) timessf(b, a)
  1001. #endif
  1002. #define timesfb(a, b) timesbf(b, a)
  1003. #ifdef COMMON
  1004. #define timesfr(a, b) timesrf(b, a)
  1005. #define timesfc(a, b) timescf(b, a)
  1006. #endif
  1007. static Lisp_Object timesff(Lisp_Object a, Lisp_Object b)
  1008. /*
  1009. * multiply boxed floats - see commentary on plusff()
  1010. */
  1011. {
  1012. #ifdef COMMON
  1013. int32 ha = type_of_header(flthdr(a)), hb = type_of_header(flthdr(b));
  1014. #endif
  1015. double d = float_of_number(a) * float_of_number(b);
  1016. #ifdef COMMON
  1017. if (ha == TYPE_LONG_FLOAT || hb == TYPE_LONG_FLOAT)
  1018. ha = TYPE_LONG_FLOAT;
  1019. else if (ha == TYPE_DOUBLE_FLOAT || hb == TYPE_DOUBLE_FLOAT)
  1020. ha = TYPE_DOUBLE_FLOAT;
  1021. else ha = TYPE_SINGLE_FLOAT;
  1022. return make_boxfloat(d, ha);
  1023. #else
  1024. return make_boxfloat(d, TYPE_DOUBLE_FLOAT);
  1025. #endif
  1026. }
  1027. /*
  1028. * ... and now for the dispatch code that copes with general
  1029. * multiplication.
  1030. */
  1031. Lisp_Object times2(Lisp_Object a, Lisp_Object b)
  1032. {
  1033. switch ((int)a & TAG_BITS)
  1034. {
  1035. case TAG_FIXNUM:
  1036. switch ((int)b & TAG_BITS)
  1037. {
  1038. case TAG_FIXNUM:
  1039. return timesii(a, b);
  1040. #ifdef COMMON
  1041. case TAG_SFLOAT:
  1042. return timesis(a, b);
  1043. #endif
  1044. case TAG_NUMBERS:
  1045. { int32 hb = type_of_header(numhdr(b));
  1046. switch (hb)
  1047. {
  1048. case TYPE_BIGNUM:
  1049. return timesib(a, b);
  1050. #ifdef COMMON
  1051. case TYPE_RATNUM:
  1052. return timesir(a, b);
  1053. case TYPE_COMPLEX_NUM:
  1054. return timesic(a, b);
  1055. #endif
  1056. default:
  1057. return aerror1("bad arg for times", b);
  1058. }
  1059. }
  1060. case TAG_BOXFLOAT:
  1061. return timesif(a, b);
  1062. default:
  1063. return aerror1("bad arg for times", b);
  1064. }
  1065. #ifdef COMMON
  1066. case TAG_SFLOAT:
  1067. switch (b & TAG_BITS)
  1068. {
  1069. case TAG_FIXNUM:
  1070. return timessi(a, b);
  1071. case TAG_SFLOAT:
  1072. { Float_union aa, bb; /* timesss() coded in-line */
  1073. aa.i = a - TAG_SFLOAT;
  1074. bb.i = b - TAG_SFLOAT;
  1075. aa.f = (float) (aa.f * bb.f);
  1076. return (aa.i & ~(int32)0xf) + TAG_SFLOAT;
  1077. }
  1078. case TAG_NUMBERS:
  1079. { int32 hb = type_of_header(numhdr(b));
  1080. switch (hb)
  1081. {
  1082. case TYPE_BIGNUM:
  1083. return timessb(a, b);
  1084. case TYPE_RATNUM:
  1085. return timessr(a, b);
  1086. case TYPE_COMPLEX_NUM:
  1087. return timessc(a, b);
  1088. default:
  1089. return aerror1("bad arg for times", b);
  1090. }
  1091. }
  1092. case TAG_BOXFLOAT:
  1093. return timessf(a, b);
  1094. default:
  1095. return aerror1("bad arg for times", b);
  1096. }
  1097. #endif
  1098. case TAG_NUMBERS:
  1099. { int32 ha = type_of_header(numhdr(a));
  1100. switch (ha)
  1101. {
  1102. case TYPE_BIGNUM:
  1103. switch ((int)b & TAG_BITS)
  1104. {
  1105. case TAG_FIXNUM:
  1106. return timesbi(a, b);
  1107. #ifdef COMMON
  1108. case TAG_SFLOAT:
  1109. return timesbs(a, b);
  1110. #endif
  1111. case TAG_NUMBERS:
  1112. { int32 hb = type_of_header(numhdr(b));
  1113. switch (hb)
  1114. {
  1115. case TYPE_BIGNUM:
  1116. return timesbb(a, b);
  1117. #ifdef COMMON
  1118. case TYPE_RATNUM:
  1119. return timesbr(a, b);
  1120. case TYPE_COMPLEX_NUM:
  1121. return timesbc(a, b);
  1122. #endif
  1123. default:
  1124. return aerror1("bad arg for times", b);
  1125. }
  1126. }
  1127. case TAG_BOXFLOAT:
  1128. return timesbf(a, b);
  1129. default:
  1130. return aerror1("bad arg for times", b);
  1131. }
  1132. #ifdef COMMON
  1133. case TYPE_RATNUM:
  1134. switch (b & TAG_BITS)
  1135. {
  1136. case TAG_FIXNUM:
  1137. return timesri(a, b);
  1138. case TAG_SFLOAT:
  1139. return timesrs(a, b);
  1140. case TAG_NUMBERS:
  1141. { int32 hb = type_of_header(numhdr(b));
  1142. switch (hb)
  1143. {
  1144. case TYPE_BIGNUM:
  1145. return timesrb(a, b);
  1146. case TYPE_RATNUM:
  1147. return timesrr(a, b);
  1148. case TYPE_COMPLEX_NUM:
  1149. return timesrc(a, b);
  1150. default:
  1151. return aerror1("bad arg for times", b);
  1152. }
  1153. }
  1154. case TAG_BOXFLOAT:
  1155. return timesrf(a, b);
  1156. default:
  1157. return aerror1("bad arg for times", b);
  1158. }
  1159. case TYPE_COMPLEX_NUM:
  1160. switch (b & TAG_BITS)
  1161. {
  1162. case TAG_FIXNUM:
  1163. return timesci(a, b);
  1164. case TAG_SFLOAT:
  1165. return timescs(a, b);
  1166. case TAG_NUMBERS:
  1167. { int32 hb = type_of_header(numhdr(b));
  1168. switch (hb)
  1169. {
  1170. case TYPE_BIGNUM:
  1171. return timescb(a, b);
  1172. case TYPE_RATNUM:
  1173. return timescr(a, b);
  1174. case TYPE_COMPLEX_NUM:
  1175. return timescc(a, b);
  1176. default:
  1177. return aerror1("bad arg for times", b);
  1178. }
  1179. }
  1180. case TAG_BOXFLOAT:
  1181. return timescf(a, b);
  1182. default:
  1183. return aerror1("bad arg for times", b);
  1184. }
  1185. #endif
  1186. default: return aerror1("bad arg for times", a);
  1187. }
  1188. }
  1189. case TAG_BOXFLOAT:
  1190. switch ((int)b & TAG_BITS)
  1191. {
  1192. case TAG_FIXNUM:
  1193. return timesfi(a, b);
  1194. #ifdef COMMON
  1195. case TAG_SFLOAT:
  1196. return timesfs(a, b);
  1197. #endif
  1198. case TAG_NUMBERS:
  1199. { int32 hb = type_of_header(numhdr(b));
  1200. switch (hb)
  1201. {
  1202. case TYPE_BIGNUM:
  1203. return timesfb(a, b);
  1204. #ifdef COMMON
  1205. case TYPE_RATNUM:
  1206. return timesfr(a, b);
  1207. case TYPE_COMPLEX_NUM:
  1208. return timesfc(a, b);
  1209. #endif
  1210. default:
  1211. return aerror1("bad arg for times", b);
  1212. }
  1213. }
  1214. case TAG_BOXFLOAT:
  1215. return timesff(a, b);
  1216. default:
  1217. return aerror1("bad arg for times", b);
  1218. }
  1219. default:
  1220. return aerror1("bad arg for times", a);
  1221. }
  1222. }
  1223. /* end of arith02.c */