arith02.c 40 KB

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