dp-bit.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  1. /* This is a software floating point library which can be used instead of
  2. the floating point routines in libgcc1.c for targets without hardware
  3. floating point. */
  4. /* Copyright (C) 1994-2022 Free Software Foundation, Inc.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* As a special exception, if you link this library with other files,
  16. some of which are compiled with GCC, to produce an executable,
  17. this library does not by itself cause the resulting executable
  18. to be covered by the GNU General Public License.
  19. This exception does not however invalidate any other reasons why
  20. the executable file might be covered by the GNU General Public License. */
  21. /* This implements IEEE 754 format arithmetic, but does not provide a
  22. mechanism for setting the rounding mode, or for generating or handling
  23. exceptions.
  24. The original code by Steve Chamberlain, hacked by Mark Eichin and Jim
  25. Wilson, all of Cygnus Support. */
  26. /* The intended way to use this file is to make two copies, add `#define FLOAT'
  27. to one copy, then compile both copies and add them to libgcc.a. */
  28. /* The following macros can be defined to change the behaviour of this file:
  29. FLOAT: Implement a `float', aka SFmode, fp library. If this is not
  30. defined, then this file implements a `double', aka DFmode, fp library.
  31. FLOAT_ONLY: Used with FLOAT, to implement a `float' only library, i.e.
  32. don't include float->double conversion which requires the double library.
  33. This is useful only for machines which can't support doubles, e.g. some
  34. 8-bit processors.
  35. CMPtype: Specify the type that floating point compares should return.
  36. This defaults to SItype, aka int.
  37. US_SOFTWARE_GOFAST: This makes all entry points use the same names as the
  38. US Software goFast library. If this is not defined, the entry points use
  39. the same names as libgcc1.c.
  40. _DEBUG_BITFLOAT: This makes debugging the code a little easier, by adding
  41. two integers to the FLO_union_type.
  42. NO_NANS: Disable nan and infinity handling
  43. SMALL_MACHINE: Useful when operations on QIs and HIs are faster
  44. than on an SI */
  45. #ifndef SFtype
  46. typedef SFtype __attribute__ ((mode (SF)));
  47. #endif
  48. #ifndef DFtype
  49. typedef DFtype __attribute__ ((mode (DF)));
  50. #endif
  51. #ifndef HItype
  52. typedef int HItype __attribute__ ((mode (HI)));
  53. #endif
  54. #ifndef SItype
  55. typedef int SItype __attribute__ ((mode (SI)));
  56. #endif
  57. #ifndef DItype
  58. typedef int DItype __attribute__ ((mode (DI)));
  59. #endif
  60. /* The type of the result of a fp compare */
  61. #ifndef CMPtype
  62. #define CMPtype SItype
  63. #endif
  64. #ifndef UHItype
  65. typedef unsigned int UHItype __attribute__ ((mode (HI)));
  66. #endif
  67. #ifndef USItype
  68. typedef unsigned int USItype __attribute__ ((mode (SI)));
  69. #endif
  70. #ifndef UDItype
  71. typedef unsigned int UDItype __attribute__ ((mode (DI)));
  72. #endif
  73. #define MAX_SI_INT ((SItype) ((unsigned) (~0)>>1))
  74. #define MAX_USI_INT ((USItype) ~0)
  75. #ifdef FLOAT_ONLY
  76. #define NO_DI_MODE
  77. #endif
  78. #ifdef FLOAT
  79. # define NGARDS 7L
  80. # define GARDROUND 0x3f
  81. # define GARDMASK 0x7f
  82. # define GARDMSB 0x40
  83. # define EXPBITS 8
  84. # define EXPBIAS 127
  85. # define FRACBITS 23
  86. # define EXPMAX (0xff)
  87. # define QUIET_NAN 0x100000L
  88. # define FRAC_NBITS 32
  89. # define FRACHIGH 0x80000000L
  90. # define FRACHIGH2 0xc0000000L
  91. typedef USItype fractype;
  92. typedef UHItype halffractype;
  93. typedef SFtype FLO_type;
  94. typedef SItype intfrac;
  95. #else
  96. # define PREFIXFPDP dp
  97. # define PREFIXSFDF df
  98. # define NGARDS 8L
  99. # define GARDROUND 0x7f
  100. # define GARDMASK 0xff
  101. # define GARDMSB 0x80
  102. # define EXPBITS 11
  103. # define EXPBIAS 1023
  104. # define FRACBITS 52
  105. # define EXPMAX (0x7ff)
  106. # define QUIET_NAN 0x8000000000000LL
  107. # define FRAC_NBITS 64
  108. # define FRACHIGH 0x8000000000000000LL
  109. # define FRACHIGH2 0xc000000000000000LL
  110. typedef UDItype fractype;
  111. typedef USItype halffractype;
  112. typedef DFtype FLO_type;
  113. typedef DItype intfrac;
  114. #endif
  115. #ifdef US_SOFTWARE_GOFAST
  116. # ifdef FLOAT
  117. # define add fpadd
  118. # define sub fpsub
  119. # define multiply fpmul
  120. # define divide fpdiv
  121. # define compare fpcmp
  122. # define si_to_float sitofp
  123. # define float_to_si fptosi
  124. # define float_to_usi fptoui
  125. # define negate __negsf2
  126. # define sf_to_df fptodp
  127. # define dptofp dptofp
  128. #else
  129. # define add dpadd
  130. # define sub dpsub
  131. # define multiply dpmul
  132. # define divide dpdiv
  133. # define compare dpcmp
  134. # define si_to_float litodp
  135. # define float_to_si dptoli
  136. # define float_to_usi dptoul
  137. # define negate __negdf2
  138. # define df_to_sf dptofp
  139. #endif
  140. #else
  141. # ifdef FLOAT
  142. # define add __addsf3
  143. # define sub __subsf3
  144. # define multiply __mulsf3
  145. # define divide __divsf3
  146. # define compare __cmpsf2
  147. # define _eq_f2 __eqsf2
  148. # define _ne_f2 __nesf2
  149. # define _gt_f2 __gtsf2
  150. # define _ge_f2 __gesf2
  151. # define _lt_f2 __ltsf2
  152. # define _le_f2 __lesf2
  153. # define si_to_float __floatsisf
  154. # define float_to_si __fixsfsi
  155. # define float_to_usi __fixunssfsi
  156. # define negate __negsf2
  157. # define sf_to_df __extendsfdf2
  158. #else
  159. # define add __adddf3
  160. # define sub __subdf3
  161. # define multiply __muldf3
  162. # define divide __divdf3
  163. # define compare __cmpdf2
  164. # define _eq_f2 __eqdf2
  165. # define _ne_f2 __nedf2
  166. # define _gt_f2 __gtdf2
  167. # define _ge_f2 __gedf2
  168. # define _lt_f2 __ltdf2
  169. # define _le_f2 __ledf2
  170. # define si_to_float __floatsidf
  171. # define float_to_si __fixdfsi
  172. # define float_to_usi __fixunsdfsi
  173. # define negate __negdf2
  174. # define df_to_sf __truncdfsf2
  175. # endif
  176. #endif
  177. #ifndef INLINE
  178. #define INLINE __inline__
  179. #endif
  180. /* Preserve the sticky-bit when shifting fractions to the right. */
  181. #define LSHIFT(a) { a = (a & 1) | (a >> 1); }
  182. /* numeric parameters */
  183. /* F_D_BITOFF is the number of bits offset between the MSB of the mantissa
  184. of a float and of a double. Assumes there are only two float types.
  185. (double::FRAC_BITS+double::NGARGS-(float::FRAC_BITS-float::NGARDS))
  186. */
  187. #define F_D_BITOFF (52+8-(23+7))
  188. #define NORMAL_EXPMIN (-(EXPBIAS)+1)
  189. #define IMPLICIT_1 (1LL<<(FRACBITS+NGARDS))
  190. #define IMPLICIT_2 (1LL<<(FRACBITS+1+NGARDS))
  191. /* common types */
  192. typedef enum
  193. {
  194. CLASS_SNAN,
  195. CLASS_QNAN,
  196. CLASS_ZERO,
  197. CLASS_NUMBER,
  198. CLASS_INFINITY
  199. } fp_class_type;
  200. typedef struct
  201. {
  202. #ifdef SMALL_MACHINE
  203. char class;
  204. unsigned char sign;
  205. short normal_exp;
  206. #else
  207. fp_class_type class;
  208. unsigned int sign;
  209. int normal_exp;
  210. #endif
  211. union
  212. {
  213. fractype ll;
  214. halffractype l[2];
  215. } fraction;
  216. } fp_number_type;
  217. typedef union
  218. {
  219. FLO_type value;
  220. #ifdef _DEBUG_BITFLOAT
  221. int l[2];
  222. #endif
  223. struct
  224. {
  225. #ifndef FLOAT_BIT_ORDER_MISMATCH
  226. unsigned int sign:1 ATTRIBUTE_PACKED;
  227. unsigned int exp:EXPBITS ATTRIBUTE_PACKED;
  228. fractype fraction:FRACBITS ATTRIBUTE_PACKED;
  229. #else
  230. fractype fraction:FRACBITS ATTRIBUTE_PACKED;
  231. unsigned int exp:EXPBITS ATTRIBUTE_PACKED;
  232. unsigned int sign:1 ATTRIBUTE_PACKED;
  233. #endif
  234. }
  235. bits;
  236. }
  237. FLO_union_type;
  238. /* end of header */
  239. /* IEEE "special" number predicates */
  240. #ifdef NO_NANS
  241. #define nan() 0
  242. #define isnan(x) 0
  243. #define isinf(x) 0
  244. #else
  245. INLINE
  246. static fp_number_type *
  247. nan ()
  248. {
  249. static fp_number_type thenan;
  250. return &thenan;
  251. }
  252. INLINE
  253. static int
  254. isnan ( fp_number_type * x)
  255. {
  256. return x->class == CLASS_SNAN || x->class == CLASS_QNAN;
  257. }
  258. INLINE
  259. static int
  260. isinf ( fp_number_type * x)
  261. {
  262. return x->class == CLASS_INFINITY;
  263. }
  264. #endif
  265. INLINE
  266. static int
  267. iszero ( fp_number_type * x)
  268. {
  269. return x->class == CLASS_ZERO;
  270. }
  271. INLINE
  272. static void
  273. flip_sign ( fp_number_type * x)
  274. {
  275. x->sign = !x->sign;
  276. }
  277. static FLO_type
  278. pack_d ( fp_number_type * src)
  279. {
  280. FLO_union_type dst;
  281. fractype fraction = src->fraction.ll; /* wasn't unsigned before? */
  282. dst.bits.sign = src->sign;
  283. if (isnan (src))
  284. {
  285. dst.bits.exp = EXPMAX;
  286. dst.bits.fraction = src->fraction.ll;
  287. if (src->class == CLASS_QNAN || 1)
  288. {
  289. dst.bits.fraction |= QUIET_NAN;
  290. }
  291. }
  292. else if (isinf (src))
  293. {
  294. dst.bits.exp = EXPMAX;
  295. dst.bits.fraction = 0;
  296. }
  297. else if (iszero (src))
  298. {
  299. dst.bits.exp = 0;
  300. dst.bits.fraction = 0;
  301. }
  302. else if (fraction == 0)
  303. {
  304. dst.value = 0;
  305. }
  306. else
  307. {
  308. if (src->normal_exp < NORMAL_EXPMIN)
  309. {
  310. /* This number's exponent is too low to fit into the bits
  311. available in the number, so we'll store 0 in the exponent and
  312. shift the fraction to the right to make up for it. */
  313. int shift = NORMAL_EXPMIN - src->normal_exp;
  314. dst.bits.exp = 0;
  315. if (shift > FRAC_NBITS - NGARDS)
  316. {
  317. /* No point shifting, since it's more that 64 out. */
  318. fraction = 0;
  319. }
  320. else
  321. {
  322. /* Shift by the value */
  323. fraction >>= shift;
  324. }
  325. fraction >>= NGARDS;
  326. dst.bits.fraction = fraction;
  327. }
  328. else if (src->normal_exp > EXPBIAS)
  329. {
  330. dst.bits.exp = EXPMAX;
  331. dst.bits.fraction = 0;
  332. }
  333. else
  334. {
  335. dst.bits.exp = src->normal_exp + EXPBIAS;
  336. /* IF the gard bits are the all zero, but the first, then we're
  337. half way between two numbers, choose the one which makes the
  338. lsb of the answer 0. */
  339. if ((fraction & GARDMASK) == GARDMSB)
  340. {
  341. if (fraction & (1 << NGARDS))
  342. fraction += GARDROUND + 1;
  343. }
  344. else
  345. {
  346. /* Add a one to the guards to round up */
  347. fraction += GARDROUND;
  348. }
  349. if (fraction >= IMPLICIT_2)
  350. {
  351. fraction >>= 1;
  352. dst.bits.exp += 1;
  353. }
  354. fraction >>= NGARDS;
  355. dst.bits.fraction = fraction;
  356. }
  357. }
  358. return dst.value;
  359. }
  360. static void
  361. unpack_d (FLO_union_type * src, fp_number_type * dst)
  362. {
  363. fractype fraction = src->bits.fraction;
  364. dst->sign = src->bits.sign;
  365. if (src->bits.exp == 0)
  366. {
  367. /* Hmm. Looks like 0 */
  368. if (fraction == 0)
  369. {
  370. /* tastes like zero */
  371. dst->class = CLASS_ZERO;
  372. }
  373. else
  374. {
  375. /* Zero exponent with non zero fraction - it's denormalized,
  376. so there isn't a leading implicit one - we'll shift it so
  377. it gets one. */
  378. dst->normal_exp = src->bits.exp - EXPBIAS + 1;
  379. fraction <<= NGARDS;
  380. dst->class = CLASS_NUMBER;
  381. #if 1
  382. while (fraction < IMPLICIT_1)
  383. {
  384. fraction <<= 1;
  385. dst->normal_exp--;
  386. }
  387. #endif
  388. dst->fraction.ll = fraction;
  389. }
  390. }
  391. else if (src->bits.exp == EXPMAX)
  392. {
  393. /* Huge exponent*/
  394. if (fraction == 0)
  395. {
  396. /* Attached to a zero fraction - means infinity */
  397. dst->class = CLASS_INFINITY;
  398. }
  399. else
  400. {
  401. /* Non zero fraction, means nan */
  402. if (dst->sign)
  403. {
  404. dst->class = CLASS_SNAN;
  405. }
  406. else
  407. {
  408. dst->class = CLASS_QNAN;
  409. }
  410. /* Keep the fraction part as the nan number */
  411. dst->fraction.ll = fraction;
  412. }
  413. }
  414. else
  415. {
  416. /* Nothing strange about this number */
  417. dst->normal_exp = src->bits.exp - EXPBIAS;
  418. dst->class = CLASS_NUMBER;
  419. dst->fraction.ll = (fraction << NGARDS) | IMPLICIT_1;
  420. }
  421. }
  422. static fp_number_type *
  423. _fpadd_parts (fp_number_type * a,
  424. fp_number_type * b,
  425. fp_number_type * tmp)
  426. {
  427. intfrac tfraction;
  428. /* Put commonly used fields in local variables. */
  429. int a_normal_exp;
  430. int b_normal_exp;
  431. fractype a_fraction;
  432. fractype b_fraction;
  433. if (isnan (a))
  434. {
  435. return a;
  436. }
  437. if (isnan (b))
  438. {
  439. return b;
  440. }
  441. if (isinf (a))
  442. {
  443. /* Adding infinities with opposite signs yields a NaN. */
  444. if (isinf (b) && a->sign != b->sign)
  445. return nan ();
  446. return a;
  447. }
  448. if (isinf (b))
  449. {
  450. return b;
  451. }
  452. if (iszero (b))
  453. {
  454. return a;
  455. }
  456. if (iszero (a))
  457. {
  458. return b;
  459. }
  460. /* Got two numbers. shift the smaller and increment the exponent till
  461. they're the same */
  462. {
  463. int diff;
  464. a_normal_exp = a->normal_exp;
  465. b_normal_exp = b->normal_exp;
  466. a_fraction = a->fraction.ll;
  467. b_fraction = b->fraction.ll;
  468. diff = a_normal_exp - b_normal_exp;
  469. if (diff < 0)
  470. diff = -diff;
  471. if (diff < FRAC_NBITS)
  472. {
  473. /* ??? This does shifts one bit at a time. Optimize. */
  474. while (a_normal_exp > b_normal_exp)
  475. {
  476. b_normal_exp++;
  477. LSHIFT (b_fraction);
  478. }
  479. while (b_normal_exp > a_normal_exp)
  480. {
  481. a_normal_exp++;
  482. LSHIFT (a_fraction);
  483. }
  484. }
  485. else
  486. {
  487. /* Somethings's up.. choose the biggest */
  488. if (a_normal_exp > b_normal_exp)
  489. {
  490. b_normal_exp = a_normal_exp;
  491. b_fraction = 0;
  492. }
  493. else
  494. {
  495. a_normal_exp = b_normal_exp;
  496. a_fraction = 0;
  497. }
  498. }
  499. }
  500. if (a->sign != b->sign)
  501. {
  502. if (a->sign)
  503. {
  504. tfraction = -a_fraction + b_fraction;
  505. }
  506. else
  507. {
  508. tfraction = a_fraction - b_fraction;
  509. }
  510. if (tfraction > 0)
  511. {
  512. tmp->sign = 0;
  513. tmp->normal_exp = a_normal_exp;
  514. tmp->fraction.ll = tfraction;
  515. }
  516. else
  517. {
  518. tmp->sign = 1;
  519. tmp->normal_exp = a_normal_exp;
  520. tmp->fraction.ll = -tfraction;
  521. }
  522. /* and renormalize it */
  523. while (tmp->fraction.ll < IMPLICIT_1 && tmp->fraction.ll)
  524. {
  525. tmp->fraction.ll <<= 1;
  526. tmp->normal_exp--;
  527. }
  528. }
  529. else
  530. {
  531. tmp->sign = a->sign;
  532. tmp->normal_exp = a_normal_exp;
  533. tmp->fraction.ll = a_fraction + b_fraction;
  534. }
  535. tmp->class = CLASS_NUMBER;
  536. /* Now the fraction is added, we have to shift down to renormalize the
  537. number */
  538. if (tmp->fraction.ll >= IMPLICIT_2)
  539. {
  540. LSHIFT (tmp->fraction.ll);
  541. tmp->normal_exp++;
  542. }
  543. return tmp;
  544. }
  545. FLO_type
  546. add (FLO_type arg_a, FLO_type arg_b)
  547. {
  548. fp_number_type a;
  549. fp_number_type b;
  550. fp_number_type tmp;
  551. fp_number_type *res;
  552. unpack_d ((FLO_union_type *) & arg_a, &a);
  553. unpack_d ((FLO_union_type *) & arg_b, &b);
  554. res = _fpadd_parts (&a, &b, &tmp);
  555. return pack_d (res);
  556. }
  557. FLO_type
  558. sub (FLO_type arg_a, FLO_type arg_b)
  559. {
  560. fp_number_type a;
  561. fp_number_type b;
  562. fp_number_type tmp;
  563. fp_number_type *res;
  564. unpack_d ((FLO_union_type *) & arg_a, &a);
  565. unpack_d ((FLO_union_type *) & arg_b, &b);
  566. b.sign ^= 1;
  567. res = _fpadd_parts (&a, &b, &tmp);
  568. return pack_d (res);
  569. }
  570. static fp_number_type *
  571. _fpmul_parts ( fp_number_type * a,
  572. fp_number_type * b,
  573. fp_number_type * tmp)
  574. {
  575. fractype low = 0;
  576. fractype high = 0;
  577. if (isnan (a))
  578. {
  579. a->sign = a->sign != b->sign;
  580. return a;
  581. }
  582. if (isnan (b))
  583. {
  584. b->sign = a->sign != b->sign;
  585. return b;
  586. }
  587. if (isinf (a))
  588. {
  589. if (iszero (b))
  590. return nan ();
  591. a->sign = a->sign != b->sign;
  592. return a;
  593. }
  594. if (isinf (b))
  595. {
  596. if (iszero (a))
  597. {
  598. return nan ();
  599. }
  600. b->sign = a->sign != b->sign;
  601. return b;
  602. }
  603. if (iszero (a))
  604. {
  605. a->sign = a->sign != b->sign;
  606. return a;
  607. }
  608. if (iszero (b))
  609. {
  610. b->sign = a->sign != b->sign;
  611. return b;
  612. }
  613. /* Calculate the mantissa by multiplying both 64bit numbers to get a
  614. 128 bit number */
  615. {
  616. fractype x = a->fraction.ll;
  617. fractype ylow = b->fraction.ll;
  618. fractype yhigh = 0;
  619. int bit;
  620. #if defined(NO_DI_MODE)
  621. {
  622. /* ??? This does multiplies one bit at a time. Optimize. */
  623. for (bit = 0; bit < FRAC_NBITS; bit++)
  624. {
  625. int carry;
  626. if (x & 1)
  627. {
  628. carry = (low += ylow) < ylow;
  629. high += yhigh + carry;
  630. }
  631. yhigh <<= 1;
  632. if (ylow & FRACHIGH)
  633. {
  634. yhigh |= 1;
  635. }
  636. ylow <<= 1;
  637. x >>= 1;
  638. }
  639. }
  640. #elif defined(FLOAT)
  641. {
  642. /* Multiplying two 32 bit numbers to get a 64 bit number on
  643. a machine with DI, so we're safe */
  644. DItype answer = (DItype)(a->fraction.ll) * (DItype)(b->fraction.ll);
  645. high = answer >> 32;
  646. low = answer;
  647. }
  648. #else
  649. /* Doing a 64*64 to 128 */
  650. {
  651. UDItype nl = a->fraction.ll & 0xffffffff;
  652. UDItype nh = a->fraction.ll >> 32;
  653. UDItype ml = b->fraction.ll & 0xffffffff;
  654. UDItype mh = b->fraction.ll >>32;
  655. UDItype pp_ll = ml * nl;
  656. UDItype pp_hl = mh * nl;
  657. UDItype pp_lh = ml * nh;
  658. UDItype pp_hh = mh * nh;
  659. UDItype res2 = 0;
  660. UDItype res0 = 0;
  661. UDItype ps_hh__ = pp_hl + pp_lh;
  662. if (ps_hh__ < pp_hl)
  663. res2 += 0x100000000LL;
  664. pp_hl = (ps_hh__ << 32) & 0xffffffff00000000LL;
  665. res0 = pp_ll + pp_hl;
  666. if (res0 < pp_ll)
  667. res2++;
  668. res2 += ((ps_hh__ >> 32) & 0xffffffffL) + pp_hh;
  669. high = res2;
  670. low = res0;
  671. }
  672. #endif
  673. }
  674. tmp->normal_exp = a->normal_exp + b->normal_exp;
  675. tmp->sign = a->sign != b->sign;
  676. #ifdef FLOAT
  677. tmp->normal_exp += 2; /* ??????????????? */
  678. #else
  679. tmp->normal_exp += 4; /* ??????????????? */
  680. #endif
  681. while (high >= IMPLICIT_2)
  682. {
  683. tmp->normal_exp++;
  684. if (high & 1)
  685. {
  686. low >>= 1;
  687. low |= FRACHIGH;
  688. }
  689. high >>= 1;
  690. }
  691. while (high < IMPLICIT_1)
  692. {
  693. tmp->normal_exp--;
  694. high <<= 1;
  695. if (low & FRACHIGH)
  696. high |= 1;
  697. low <<= 1;
  698. }
  699. /* rounding is tricky. if we only round if it won't make us round later. */
  700. #if 0
  701. if (low & FRACHIGH2)
  702. {
  703. if (((high & GARDMASK) != GARDMSB)
  704. && (((high + 1) & GARDMASK) == GARDMSB))
  705. {
  706. /* don't round, it gets done again later. */
  707. }
  708. else
  709. {
  710. high++;
  711. }
  712. }
  713. #endif
  714. if ((high & GARDMASK) == GARDMSB)
  715. {
  716. if (high & (1 << NGARDS))
  717. {
  718. /* half way, so round to even */
  719. high += GARDROUND + 1;
  720. }
  721. else if (low)
  722. {
  723. /* but we really weren't half way */
  724. high += GARDROUND + 1;
  725. }
  726. }
  727. tmp->fraction.ll = high;
  728. tmp->class = CLASS_NUMBER;
  729. return tmp;
  730. }
  731. FLO_type
  732. multiply (FLO_type arg_a, FLO_type arg_b)
  733. {
  734. fp_number_type a;
  735. fp_number_type b;
  736. fp_number_type tmp;
  737. fp_number_type *res;
  738. unpack_d ((FLO_union_type *) & arg_a, &a);
  739. unpack_d ((FLO_union_type *) & arg_b, &b);
  740. res = _fpmul_parts (&a, &b, &tmp);
  741. return pack_d (res);
  742. }
  743. static fp_number_type *
  744. _fpdiv_parts (fp_number_type * a,
  745. fp_number_type * b,
  746. fp_number_type * tmp)
  747. {
  748. fractype low = 0;
  749. fractype high = 0;
  750. fractype r0, r1, y0, y1, bit;
  751. fractype q;
  752. fractype numerator;
  753. fractype denominator;
  754. fractype quotient;
  755. fractype remainder;
  756. if (isnan (a))
  757. {
  758. return a;
  759. }
  760. if (isnan (b))
  761. {
  762. return b;
  763. }
  764. if (isinf (a) || iszero (a))
  765. {
  766. if (a->class == b->class)
  767. return nan ();
  768. return a;
  769. }
  770. a->sign = a->sign ^ b->sign;
  771. if (isinf (b))
  772. {
  773. a->fraction.ll = 0;
  774. a->normal_exp = 0;
  775. return a;
  776. }
  777. if (iszero (b))
  778. {
  779. a->class = CLASS_INFINITY;
  780. return b;
  781. }
  782. /* Calculate the mantissa by multiplying both 64bit numbers to get a
  783. 128 bit number */
  784. {
  785. int carry;
  786. intfrac d0, d1; /* weren't unsigned before ??? */
  787. /* quotient =
  788. ( numerator / denominator) * 2^(numerator exponent - denominator exponent)
  789. */
  790. a->normal_exp = a->normal_exp - b->normal_exp;
  791. numerator = a->fraction.ll;
  792. denominator = b->fraction.ll;
  793. if (numerator < denominator)
  794. {
  795. /* Fraction will be less than 1.0 */
  796. numerator *= 2;
  797. a->normal_exp--;
  798. }
  799. bit = IMPLICIT_1;
  800. quotient = 0;
  801. /* ??? Does divide one bit at a time. Optimize. */
  802. while (bit)
  803. {
  804. if (numerator >= denominator)
  805. {
  806. quotient |= bit;
  807. numerator -= denominator;
  808. }
  809. bit >>= 1;
  810. numerator *= 2;
  811. }
  812. if ((quotient & GARDMASK) == GARDMSB)
  813. {
  814. if (quotient & (1 << NGARDS))
  815. {
  816. /* half way, so round to even */
  817. quotient += GARDROUND + 1;
  818. }
  819. else if (numerator)
  820. {
  821. /* but we really weren't half way, more bits exist */
  822. quotient += GARDROUND + 1;
  823. }
  824. }
  825. a->fraction.ll = quotient;
  826. return (a);
  827. }
  828. }
  829. FLO_type
  830. divide (FLO_type arg_a, FLO_type arg_b)
  831. {
  832. fp_number_type a;
  833. fp_number_type b;
  834. fp_number_type tmp;
  835. fp_number_type *res;
  836. unpack_d ((FLO_union_type *) & arg_a, &a);
  837. unpack_d ((FLO_union_type *) & arg_b, &b);
  838. res = _fpdiv_parts (&a, &b, &tmp);
  839. return pack_d (res);
  840. }
  841. /* according to the demo, fpcmp returns a comparison with 0... thus
  842. a<b -> -1
  843. a==b -> 0
  844. a>b -> +1
  845. */
  846. static int
  847. _fpcmp_parts (fp_number_type * a, fp_number_type * b)
  848. {
  849. #if 0
  850. /* either nan -> unordered. Must be checked outside of this routine. */
  851. if (isnan (a) && isnan (b))
  852. {
  853. return 1; /* still unordered! */
  854. }
  855. #endif
  856. if (isnan (a) || isnan (b))
  857. {
  858. return 1; /* how to indicate unordered compare? */
  859. }
  860. if (isinf (a) && isinf (b))
  861. {
  862. /* +inf > -inf, but +inf != +inf */
  863. /* b \a| +inf(0)| -inf(1)
  864. ______\+--------+--------
  865. +inf(0)| a==b(0)| a<b(-1)
  866. -------+--------+--------
  867. -inf(1)| a>b(1) | a==b(0)
  868. -------+--------+--------
  869. So since unordered must be non zero, just line up the columns...
  870. */
  871. return b->sign - a->sign;
  872. }
  873. /* but not both... */
  874. if (isinf (a))
  875. {
  876. return a->sign ? -1 : 1;
  877. }
  878. if (isinf (b))
  879. {
  880. return b->sign ? 1 : -1;
  881. }
  882. if (iszero (a) && iszero (b))
  883. {
  884. return 0;
  885. }
  886. if (iszero (a))
  887. {
  888. return b->sign ? 1 : -1;
  889. }
  890. if (iszero (b))
  891. {
  892. return a->sign ? -1 : 1;
  893. }
  894. /* now both are "normal". */
  895. if (a->sign != b->sign)
  896. {
  897. /* opposite signs */
  898. return a->sign ? -1 : 1;
  899. }
  900. /* same sign; exponents? */
  901. if (a->normal_exp > b->normal_exp)
  902. {
  903. return a->sign ? -1 : 1;
  904. }
  905. if (a->normal_exp < b->normal_exp)
  906. {
  907. return a->sign ? 1 : -1;
  908. }
  909. /* same exponents; check size. */
  910. if (a->fraction.ll > b->fraction.ll)
  911. {
  912. return a->sign ? -1 : 1;
  913. }
  914. if (a->fraction.ll < b->fraction.ll)
  915. {
  916. return a->sign ? 1 : -1;
  917. }
  918. /* after all that, they're equal. */
  919. return 0;
  920. }
  921. CMPtype
  922. compare (FLO_type arg_a, FLO_type arg_b)
  923. {
  924. fp_number_type a;
  925. fp_number_type b;
  926. unpack_d ((FLO_union_type *) & arg_a, &a);
  927. unpack_d ((FLO_union_type *) & arg_b, &b);
  928. return _fpcmp_parts (&a, &b);
  929. }
  930. #ifndef US_SOFTWARE_GOFAST
  931. /* These should be optimized for their specific tasks someday. */
  932. CMPtype
  933. _eq_f2 (FLO_type arg_a, FLO_type arg_b)
  934. {
  935. fp_number_type a;
  936. fp_number_type b;
  937. unpack_d ((FLO_union_type *) & arg_a, &a);
  938. unpack_d ((FLO_union_type *) & arg_b, &b);
  939. if (isnan (&a) || isnan (&b))
  940. return 1; /* false, truth == 0 */
  941. return _fpcmp_parts (&a, &b) ;
  942. }
  943. CMPtype
  944. _ne_f2 (FLO_type arg_a, FLO_type arg_b)
  945. {
  946. fp_number_type a;
  947. fp_number_type b;
  948. unpack_d ((FLO_union_type *) & arg_a, &a);
  949. unpack_d ((FLO_union_type *) & arg_b, &b);
  950. if (isnan (&a) || isnan (&b))
  951. return 1; /* true, truth != 0 */
  952. return _fpcmp_parts (&a, &b) ;
  953. }
  954. CMPtype
  955. _gt_f2 (FLO_type arg_a, FLO_type arg_b)
  956. {
  957. fp_number_type a;
  958. fp_number_type b;
  959. unpack_d ((FLO_union_type *) & arg_a, &a);
  960. unpack_d ((FLO_union_type *) & arg_b, &b);
  961. if (isnan (&a) || isnan (&b))
  962. return -1; /* false, truth > 0 */
  963. return _fpcmp_parts (&a, &b);
  964. }
  965. CMPtype
  966. _ge_f2 (FLO_type arg_a, FLO_type arg_b)
  967. {
  968. fp_number_type a;
  969. fp_number_type b;
  970. unpack_d ((FLO_union_type *) & arg_a, &a);
  971. unpack_d ((FLO_union_type *) & arg_b, &b);
  972. if (isnan (&a) || isnan (&b))
  973. return -1; /* false, truth >= 0 */
  974. return _fpcmp_parts (&a, &b) ;
  975. }
  976. CMPtype
  977. _lt_f2 (FLO_type arg_a, FLO_type arg_b)
  978. {
  979. fp_number_type a;
  980. fp_number_type b;
  981. unpack_d ((FLO_union_type *) & arg_a, &a);
  982. unpack_d ((FLO_union_type *) & arg_b, &b);
  983. if (isnan (&a) || isnan (&b))
  984. return 1; /* false, truth < 0 */
  985. return _fpcmp_parts (&a, &b);
  986. }
  987. CMPtype
  988. _le_f2 (FLO_type arg_a, FLO_type arg_b)
  989. {
  990. fp_number_type a;
  991. fp_number_type b;
  992. unpack_d ((FLO_union_type *) & arg_a, &a);
  993. unpack_d ((FLO_union_type *) & arg_b, &b);
  994. if (isnan (&a) || isnan (&b))
  995. return 1; /* false, truth <= 0 */
  996. return _fpcmp_parts (&a, &b) ;
  997. }
  998. #endif /* ! US_SOFTWARE_GOFAST */
  999. FLO_type
  1000. si_to_float (SItype arg_a)
  1001. {
  1002. fp_number_type in;
  1003. in.class = CLASS_NUMBER;
  1004. in.sign = arg_a < 0;
  1005. if (!arg_a)
  1006. {
  1007. in.class = CLASS_ZERO;
  1008. }
  1009. else
  1010. {
  1011. in.normal_exp = FRACBITS + NGARDS;
  1012. if (in.sign)
  1013. {
  1014. /* Special case for minint, since there is no +ve integer
  1015. representation for it */
  1016. if (arg_a == 0x80000000)
  1017. {
  1018. return -2147483648.0;
  1019. }
  1020. in.fraction.ll = (-arg_a);
  1021. }
  1022. else
  1023. in.fraction.ll = arg_a;
  1024. while (in.fraction.ll < (1LL << (FRACBITS + NGARDS)))
  1025. {
  1026. in.fraction.ll <<= 1;
  1027. in.normal_exp -= 1;
  1028. }
  1029. }
  1030. return pack_d (&in);
  1031. }
  1032. SItype
  1033. float_to_si (FLO_type arg_a)
  1034. {
  1035. fp_number_type a;
  1036. SItype tmp;
  1037. unpack_d ((FLO_union_type *) & arg_a, &a);
  1038. if (iszero (&a))
  1039. return 0;
  1040. if (isnan (&a))
  1041. return 0;
  1042. /* get reasonable MAX_SI_INT... */
  1043. if (isinf (&a))
  1044. return a.sign ? MAX_SI_INT : (-MAX_SI_INT)-1;
  1045. /* it is a number, but a small one */
  1046. if (a.normal_exp < 0)
  1047. return 0;
  1048. if (a.normal_exp > 30)
  1049. return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
  1050. tmp = a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
  1051. return a.sign ? (-tmp) : (tmp);
  1052. }
  1053. #ifdef US_SOFTWARE_GOFAST
  1054. /* While libgcc2.c defines its own __fixunssfsi and __fixunsdfsi routines,
  1055. we also define them for GOFAST because the ones in libgcc2.c have the
  1056. wrong names and I'd rather define these here and keep GOFAST CYG-LOC's
  1057. out of libgcc2.c. We can't define these here if not GOFAST because then
  1058. there'd be duplicate copies. */
  1059. USItype
  1060. float_to_usi (FLO_type arg_a)
  1061. {
  1062. fp_number_type a;
  1063. unpack_d ((FLO_union_type *) & arg_a, &a);
  1064. if (iszero (&a))
  1065. return 0;
  1066. if (isnan (&a))
  1067. return 0;
  1068. /* get reasonable MAX_USI_INT... */
  1069. if (isinf (&a))
  1070. return a.sign ? MAX_USI_INT : 0;
  1071. /* it is a negative number */
  1072. if (a.sign)
  1073. return 0;
  1074. /* it is a number, but a small one */
  1075. if (a.normal_exp < 0)
  1076. return 0;
  1077. if (a.normal_exp > 31)
  1078. return MAX_USI_INT;
  1079. else if (a.normal_exp > (FRACBITS + NGARDS))
  1080. return a.fraction.ll << ((FRACBITS + NGARDS) - a.normal_exp);
  1081. else
  1082. return a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
  1083. }
  1084. #endif
  1085. FLO_type
  1086. negate (FLO_type arg_a)
  1087. {
  1088. fp_number_type a;
  1089. unpack_d ((FLO_union_type *) & arg_a, &a);
  1090. flip_sign (&a);
  1091. return pack_d (&a);
  1092. }
  1093. #ifdef FLOAT
  1094. SFtype
  1095. __make_fp(fp_class_type class,
  1096. unsigned int sign,
  1097. int exp,
  1098. USItype frac)
  1099. {
  1100. fp_number_type in;
  1101. in.class = class;
  1102. in.sign = sign;
  1103. in.normal_exp = exp;
  1104. in.fraction.ll = frac;
  1105. return pack_d (&in);
  1106. }
  1107. #ifndef FLOAT_ONLY
  1108. /* This enables one to build an fp library that supports float but not double.
  1109. Otherwise, we would get an undefined reference to __make_dp.
  1110. This is needed for some 8-bit ports that can't handle well values that
  1111. are 8-bytes in size, so we just don't support double for them at all. */
  1112. extern DFtype __make_dp (fp_class_type, unsigned int, int, UDItype frac);
  1113. DFtype
  1114. sf_to_df (SFtype arg_a)
  1115. {
  1116. fp_number_type in;
  1117. unpack_d ((FLO_union_type *) & arg_a, &in);
  1118. return __make_dp (in.class, in.sign, in.normal_exp,
  1119. ((UDItype) in.fraction.ll) << F_D_BITOFF);
  1120. }
  1121. #endif
  1122. #endif
  1123. #ifndef FLOAT
  1124. extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
  1125. DFtype
  1126. __make_dp (fp_class_type class, unsigned int sign, int exp, UDItype frac)
  1127. {
  1128. fp_number_type in;
  1129. in.class = class;
  1130. in.sign = sign;
  1131. in.normal_exp = exp;
  1132. in.fraction.ll = frac;
  1133. return pack_d (&in);
  1134. }
  1135. SFtype
  1136. df_to_sf (DFtype arg_a)
  1137. {
  1138. fp_number_type in;
  1139. unpack_d ((FLO_union_type *) & arg_a, &in);
  1140. return __make_fp (in.class, in.sign, in.normal_exp,
  1141. in.fraction.ll >> F_D_BITOFF);
  1142. }
  1143. #endif