fp-bit.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634
  1. /* This is a software floating point library which can be used
  2. for targets without hardware floating point.
  3. Copyright (C) 1994-2022 Free Software Foundation, Inc.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. /* This implements IEEE 754 format arithmetic, but does not provide a
  21. mechanism for setting the rounding mode, or for generating or handling
  22. exceptions.
  23. The original code by Steve Chamberlain, hacked by Mark Eichin and Jim
  24. Wilson, all of Cygnus Support. */
  25. /* The intended way to use this file is to make two copies, add `#define FLOAT'
  26. to one copy, then compile both copies and add them to libgcc.a. */
  27. #include "tconfig.h"
  28. #include "coretypes.h"
  29. #include "tm.h"
  30. #include "libgcc_tm.h"
  31. #include "fp-bit.h"
  32. /* The following macros can be defined to change the behavior of this file:
  33. FLOAT: Implement a `float', aka SFmode, fp library. If this is not
  34. defined, then this file implements a `double', aka DFmode, fp library.
  35. FLOAT_ONLY: Used with FLOAT, to implement a `float' only library, i.e.
  36. don't include float->double conversion which requires the double library.
  37. This is useful only for machines which can't support doubles, e.g. some
  38. 8-bit processors.
  39. CMPtype: Specify the type that floating point compares should return.
  40. This defaults to SItype, aka int.
  41. _DEBUG_BITFLOAT: This makes debugging the code a little easier, by adding
  42. two integers to the FLO_union_type.
  43. NO_DENORMALS: Disable handling of denormals.
  44. NO_NANS: Disable nan and infinity handling
  45. SMALL_MACHINE: Useful when operations on QIs and HIs are faster
  46. than on an SI */
  47. /* We don't currently support extended floats (long doubles) on machines
  48. without hardware to deal with them.
  49. These stubs are just to keep the linker from complaining about unresolved
  50. references which can be pulled in from libio & libstdc++, even if the
  51. user isn't using long doubles. However, they may generate an unresolved
  52. external to abort if abort is not used by the function, and the stubs
  53. are referenced from within libc, since libgcc goes before and after the
  54. system library. */
  55. #ifdef DECLARE_LIBRARY_RENAMES
  56. DECLARE_LIBRARY_RENAMES
  57. #endif
  58. #ifdef EXTENDED_FLOAT_STUBS
  59. extern void abort (void);
  60. void __extendsfxf2 (void) { abort(); }
  61. void __extenddfxf2 (void) { abort(); }
  62. void __truncxfdf2 (void) { abort(); }
  63. void __truncxfsf2 (void) { abort(); }
  64. void __fixxfsi (void) { abort(); }
  65. void __floatsixf (void) { abort(); }
  66. void __addxf3 (void) { abort(); }
  67. void __subxf3 (void) { abort(); }
  68. void __mulxf3 (void) { abort(); }
  69. void __divxf3 (void) { abort(); }
  70. void __negxf2 (void) { abort(); }
  71. void __eqxf2 (void) { abort(); }
  72. void __nexf2 (void) { abort(); }
  73. void __gtxf2 (void) { abort(); }
  74. void __gexf2 (void) { abort(); }
  75. void __lexf2 (void) { abort(); }
  76. void __ltxf2 (void) { abort(); }
  77. void __extendsftf2 (void) { abort(); }
  78. void __extenddftf2 (void) { abort(); }
  79. void __trunctfdf2 (void) { abort(); }
  80. void __trunctfsf2 (void) { abort(); }
  81. void __fixtfsi (void) { abort(); }
  82. void __floatsitf (void) { abort(); }
  83. void __addtf3 (void) { abort(); }
  84. void __subtf3 (void) { abort(); }
  85. void __multf3 (void) { abort(); }
  86. void __divtf3 (void) { abort(); }
  87. void __negtf2 (void) { abort(); }
  88. void __eqtf2 (void) { abort(); }
  89. void __netf2 (void) { abort(); }
  90. void __gttf2 (void) { abort(); }
  91. void __getf2 (void) { abort(); }
  92. void __letf2 (void) { abort(); }
  93. void __lttf2 (void) { abort(); }
  94. #else /* !EXTENDED_FLOAT_STUBS, rest of file */
  95. /* IEEE "special" number predicates */
  96. #ifdef NO_NANS
  97. #define nan() 0
  98. #define isnan(x) 0
  99. #define isinf(x) 0
  100. #else
  101. #if defined L_thenan_sf
  102. const fp_number_type __thenan_sf = { CLASS_SNAN, 0, 0, {(fractype) 0} };
  103. #elif defined L_thenan_df
  104. const fp_number_type __thenan_df = { CLASS_SNAN, 0, 0, {(fractype) 0} };
  105. #elif defined L_thenan_tf
  106. const fp_number_type __thenan_tf = { CLASS_SNAN, 0, 0, {(fractype) 0} };
  107. #elif defined TFLOAT
  108. extern const fp_number_type __thenan_tf;
  109. #elif defined FLOAT
  110. extern const fp_number_type __thenan_sf;
  111. #else
  112. extern const fp_number_type __thenan_df;
  113. #endif
  114. INLINE
  115. static const fp_number_type *
  116. makenan (void)
  117. {
  118. #ifdef TFLOAT
  119. return & __thenan_tf;
  120. #elif defined FLOAT
  121. return & __thenan_sf;
  122. #else
  123. return & __thenan_df;
  124. #endif
  125. }
  126. INLINE
  127. static int
  128. isnan (const fp_number_type *x)
  129. {
  130. return __builtin_expect (x->class == CLASS_SNAN || x->class == CLASS_QNAN,
  131. 0);
  132. }
  133. INLINE
  134. static int
  135. isinf (const fp_number_type * x)
  136. {
  137. return __builtin_expect (x->class == CLASS_INFINITY, 0);
  138. }
  139. #endif /* NO_NANS */
  140. INLINE
  141. static int
  142. iszero (const fp_number_type * x)
  143. {
  144. return x->class == CLASS_ZERO;
  145. }
  146. INLINE
  147. static void
  148. flip_sign ( fp_number_type * x)
  149. {
  150. x->sign = !x->sign;
  151. }
  152. /* Count leading zeroes in N. */
  153. INLINE
  154. static int
  155. clzusi (USItype n)
  156. {
  157. extern int __clzsi2 (USItype);
  158. if (sizeof (USItype) == sizeof (unsigned int))
  159. return __builtin_clz (n);
  160. else if (sizeof (USItype) == sizeof (unsigned long))
  161. return __builtin_clzl (n);
  162. else if (sizeof (USItype) == sizeof (unsigned long long))
  163. return __builtin_clzll (n);
  164. else
  165. return __clzsi2 (n);
  166. }
  167. extern FLO_type pack_d (const fp_number_type * );
  168. #if defined(L_pack_df) || defined(L_pack_sf) || defined(L_pack_tf)
  169. FLO_type
  170. pack_d (const fp_number_type *src)
  171. {
  172. FLO_union_type dst;
  173. fractype fraction = src->fraction.ll; /* wasn't unsigned before? */
  174. int sign = src->sign;
  175. int exp = 0;
  176. if (isnan (src))
  177. {
  178. exp = EXPMAX;
  179. /* Restore the NaN's payload. */
  180. fraction >>= NGARDS;
  181. fraction &= QUIET_NAN - 1;
  182. if (src->class == CLASS_QNAN || 1)
  183. {
  184. #ifdef QUIET_NAN_NEGATED
  185. /* The quiet/signaling bit remains unset. */
  186. /* Make sure the fraction has a non-zero value. */
  187. if (fraction == 0)
  188. fraction |= QUIET_NAN - 1;
  189. #else
  190. /* Set the quiet/signaling bit. */
  191. fraction |= QUIET_NAN;
  192. #endif
  193. }
  194. }
  195. else if (isinf (src))
  196. {
  197. exp = EXPMAX;
  198. fraction = 0;
  199. }
  200. else if (iszero (src))
  201. {
  202. exp = 0;
  203. fraction = 0;
  204. }
  205. else if (fraction == 0)
  206. {
  207. exp = 0;
  208. }
  209. else
  210. {
  211. if (__builtin_expect (src->normal_exp < NORMAL_EXPMIN, 0))
  212. {
  213. #ifdef NO_DENORMALS
  214. /* Go straight to a zero representation if denormals are not
  215. supported. The denormal handling would be harmless but
  216. isn't unnecessary. */
  217. exp = 0;
  218. fraction = 0;
  219. #else /* NO_DENORMALS */
  220. /* This number's exponent is too low to fit into the bits
  221. available in the number, so we'll store 0 in the exponent and
  222. shift the fraction to the right to make up for it. */
  223. int shift = NORMAL_EXPMIN - src->normal_exp;
  224. exp = 0;
  225. if (shift > FRAC_NBITS - NGARDS)
  226. {
  227. /* No point shifting, since it's more that 64 out. */
  228. fraction = 0;
  229. }
  230. else
  231. {
  232. int lowbit = (fraction & (((fractype)1 << shift) - 1)) ? 1 : 0;
  233. fraction = (fraction >> shift) | lowbit;
  234. }
  235. if ((fraction & GARDMASK) == GARDMSB)
  236. {
  237. if ((fraction & (1 << NGARDS)))
  238. fraction += GARDROUND + 1;
  239. }
  240. else
  241. {
  242. /* Add to the guards to round up. */
  243. fraction += GARDROUND;
  244. }
  245. /* Perhaps the rounding means we now need to change the
  246. exponent, because the fraction is no longer denormal. */
  247. if (fraction >= IMPLICIT_1)
  248. {
  249. exp += 1;
  250. }
  251. fraction >>= NGARDS;
  252. #endif /* NO_DENORMALS */
  253. }
  254. else if (__builtin_expect (src->normal_exp > EXPBIAS, 0))
  255. {
  256. exp = EXPMAX;
  257. fraction = 0;
  258. }
  259. else
  260. {
  261. exp = src->normal_exp + EXPBIAS;
  262. /* IF the gard bits are the all zero, but the first, then we're
  263. half way between two numbers, choose the one which makes the
  264. lsb of the answer 0. */
  265. if ((fraction & GARDMASK) == GARDMSB)
  266. {
  267. if (fraction & (1 << NGARDS))
  268. fraction += GARDROUND + 1;
  269. }
  270. else
  271. {
  272. /* Add a one to the guards to round up */
  273. fraction += GARDROUND;
  274. }
  275. if (fraction >= IMPLICIT_2)
  276. {
  277. fraction >>= 1;
  278. exp += 1;
  279. }
  280. fraction >>= NGARDS;
  281. }
  282. }
  283. /* We previously used bitfields to store the number, but this doesn't
  284. handle little/big endian systems conveniently, so use shifts and
  285. masks */
  286. #if defined TFLOAT && defined HALFFRACBITS
  287. {
  288. halffractype high, low, unity;
  289. int lowsign, lowexp;
  290. unity = (halffractype) 1 << HALFFRACBITS;
  291. /* Set HIGH to the high double's significand, masking out the implicit 1.
  292. Set LOW to the low double's full significand. */
  293. high = (fraction >> (FRACBITS - HALFFRACBITS)) & (unity - 1);
  294. low = fraction & (unity * 2 - 1);
  295. /* Get the initial sign and exponent of the low double. */
  296. lowexp = exp - HALFFRACBITS - 1;
  297. lowsign = sign;
  298. /* HIGH should be rounded like a normal double, making |LOW| <=
  299. 0.5 ULP of HIGH. Assume round-to-nearest. */
  300. if (exp < EXPMAX)
  301. if (low > unity || (low == unity && (high & 1) == 1))
  302. {
  303. /* Round HIGH up and adjust LOW to match. */
  304. high++;
  305. if (high == unity)
  306. {
  307. /* May make it infinite, but that's OK. */
  308. high = 0;
  309. exp++;
  310. }
  311. low = unity * 2 - low;
  312. lowsign ^= 1;
  313. }
  314. high |= (halffractype) exp << HALFFRACBITS;
  315. high |= (halffractype) sign << (HALFFRACBITS + EXPBITS);
  316. if (exp == EXPMAX || exp == 0 || low == 0)
  317. low = 0;
  318. else
  319. {
  320. while (lowexp > 0 && low < unity)
  321. {
  322. low <<= 1;
  323. lowexp--;
  324. }
  325. if (lowexp <= 0)
  326. {
  327. halffractype roundmsb, round;
  328. int shift;
  329. shift = 1 - lowexp;
  330. roundmsb = (1 << (shift - 1));
  331. round = low & ((roundmsb << 1) - 1);
  332. low >>= shift;
  333. lowexp = 0;
  334. if (round > roundmsb || (round == roundmsb && (low & 1) == 1))
  335. {
  336. low++;
  337. if (low == unity)
  338. /* LOW rounds up to the smallest normal number. */
  339. lowexp++;
  340. }
  341. }
  342. low &= unity - 1;
  343. low |= (halffractype) lowexp << HALFFRACBITS;
  344. low |= (halffractype) lowsign << (HALFFRACBITS + EXPBITS);
  345. }
  346. dst.value_raw = ((fractype) high << HALFSHIFT) | low;
  347. }
  348. #else
  349. dst.value_raw = fraction & ((((fractype)1) << FRACBITS) - (fractype)1);
  350. dst.value_raw |= ((fractype) (exp & ((1 << EXPBITS) - 1))) << FRACBITS;
  351. dst.value_raw |= ((fractype) (sign & 1)) << (FRACBITS | EXPBITS);
  352. #endif
  353. #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
  354. #ifdef TFLOAT
  355. {
  356. qrtrfractype tmp1 = dst.words[0];
  357. qrtrfractype tmp2 = dst.words[1];
  358. dst.words[0] = dst.words[3];
  359. dst.words[1] = dst.words[2];
  360. dst.words[2] = tmp2;
  361. dst.words[3] = tmp1;
  362. }
  363. #else
  364. {
  365. halffractype tmp = dst.words[0];
  366. dst.words[0] = dst.words[1];
  367. dst.words[1] = tmp;
  368. }
  369. #endif
  370. #endif
  371. return dst.value;
  372. }
  373. #endif
  374. #if defined(L_unpack_df) || defined(L_unpack_sf) || defined(L_unpack_tf)
  375. void
  376. unpack_d (FLO_union_type * src, fp_number_type * dst)
  377. {
  378. /* We previously used bitfields to store the number, but this doesn't
  379. handle little/big endian systems conveniently, so use shifts and
  380. masks */
  381. fractype fraction;
  382. int exp;
  383. int sign;
  384. #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
  385. FLO_union_type swapped;
  386. #ifdef TFLOAT
  387. swapped.words[0] = src->words[3];
  388. swapped.words[1] = src->words[2];
  389. swapped.words[2] = src->words[1];
  390. swapped.words[3] = src->words[0];
  391. #else
  392. swapped.words[0] = src->words[1];
  393. swapped.words[1] = src->words[0];
  394. #endif
  395. src = &swapped;
  396. #endif
  397. #if defined TFLOAT && defined HALFFRACBITS
  398. {
  399. halffractype high, low;
  400. high = src->value_raw >> HALFSHIFT;
  401. low = src->value_raw & (((fractype)1 << HALFSHIFT) - 1);
  402. fraction = high & ((((fractype)1) << HALFFRACBITS) - 1);
  403. fraction <<= FRACBITS - HALFFRACBITS;
  404. exp = ((int)(high >> HALFFRACBITS)) & ((1 << EXPBITS) - 1);
  405. sign = ((int)(high >> (((HALFFRACBITS + EXPBITS))))) & 1;
  406. if (exp != EXPMAX && exp != 0 && low != 0)
  407. {
  408. int lowexp = ((int)(low >> HALFFRACBITS)) & ((1 << EXPBITS) - 1);
  409. int lowsign = ((int)(low >> (((HALFFRACBITS + EXPBITS))))) & 1;
  410. int shift;
  411. fractype xlow;
  412. xlow = low & ((((fractype)1) << HALFFRACBITS) - 1);
  413. if (lowexp)
  414. xlow |= (((halffractype)1) << HALFFRACBITS);
  415. else
  416. lowexp = 1;
  417. shift = (FRACBITS - HALFFRACBITS) - (exp - lowexp);
  418. if (shift > 0)
  419. xlow <<= shift;
  420. else if (shift < 0)
  421. xlow >>= -shift;
  422. if (sign == lowsign)
  423. fraction += xlow;
  424. else if (fraction >= xlow)
  425. fraction -= xlow;
  426. else
  427. {
  428. /* The high part is a power of two but the full number is lower.
  429. This code will leave the implicit 1 in FRACTION, but we'd
  430. have added that below anyway. */
  431. fraction = (((fractype) 1 << FRACBITS) - xlow) << 1;
  432. exp--;
  433. }
  434. }
  435. }
  436. #else
  437. fraction = src->value_raw & ((((fractype)1) << FRACBITS) - 1);
  438. exp = ((int)(src->value_raw >> FRACBITS)) & ((1 << EXPBITS) - 1);
  439. sign = ((int)(src->value_raw >> (FRACBITS + EXPBITS))) & 1;
  440. #endif
  441. dst->sign = sign;
  442. if (exp == 0)
  443. {
  444. /* Hmm. Looks like 0 */
  445. if (fraction == 0
  446. #ifdef NO_DENORMALS
  447. || 1
  448. #endif
  449. )
  450. {
  451. /* tastes like zero */
  452. dst->class = CLASS_ZERO;
  453. }
  454. else
  455. {
  456. /* Zero exponent with nonzero fraction - it's denormalized,
  457. so there isn't a leading implicit one - we'll shift it so
  458. it gets one. */
  459. dst->normal_exp = exp - EXPBIAS + 1;
  460. fraction <<= NGARDS;
  461. dst->class = CLASS_NUMBER;
  462. #if 1
  463. while (fraction < IMPLICIT_1)
  464. {
  465. fraction <<= 1;
  466. dst->normal_exp--;
  467. }
  468. #endif
  469. dst->fraction.ll = fraction;
  470. }
  471. }
  472. else if (__builtin_expect (exp == EXPMAX, 0))
  473. {
  474. /* Huge exponent*/
  475. if (fraction == 0)
  476. {
  477. /* Attached to a zero fraction - means infinity */
  478. dst->class = CLASS_INFINITY;
  479. }
  480. else
  481. {
  482. /* Nonzero fraction, means nan */
  483. #ifdef QUIET_NAN_NEGATED
  484. if ((fraction & QUIET_NAN) == 0)
  485. #else
  486. if (fraction & QUIET_NAN)
  487. #endif
  488. {
  489. dst->class = CLASS_QNAN;
  490. }
  491. else
  492. {
  493. dst->class = CLASS_SNAN;
  494. }
  495. /* Now that we know which kind of NaN we got, discard the
  496. quiet/signaling bit, but do preserve the NaN payload. */
  497. fraction &= ~QUIET_NAN;
  498. dst->fraction.ll = fraction << NGARDS;
  499. }
  500. }
  501. else
  502. {
  503. /* Nothing strange about this number */
  504. dst->normal_exp = exp - EXPBIAS;
  505. dst->class = CLASS_NUMBER;
  506. dst->fraction.ll = (fraction << NGARDS) | IMPLICIT_1;
  507. }
  508. }
  509. #endif /* L_unpack_df || L_unpack_sf */
  510. #if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
  511. static const fp_number_type *
  512. _fpadd_parts (fp_number_type * a,
  513. fp_number_type * b,
  514. fp_number_type * tmp)
  515. {
  516. intfrac tfraction;
  517. /* Put commonly used fields in local variables. */
  518. int a_normal_exp;
  519. int b_normal_exp;
  520. fractype a_fraction;
  521. fractype b_fraction;
  522. if (isnan (a))
  523. {
  524. return a;
  525. }
  526. if (isnan (b))
  527. {
  528. return b;
  529. }
  530. if (isinf (a))
  531. {
  532. /* Adding infinities with opposite signs yields a NaN. */
  533. if (isinf (b) && a->sign != b->sign)
  534. return makenan ();
  535. return a;
  536. }
  537. if (isinf (b))
  538. {
  539. return b;
  540. }
  541. if (iszero (b))
  542. {
  543. if (iszero (a))
  544. {
  545. *tmp = *a;
  546. tmp->sign = a->sign & b->sign;
  547. return tmp;
  548. }
  549. return a;
  550. }
  551. if (iszero (a))
  552. {
  553. return b;
  554. }
  555. /* Got two numbers. shift the smaller and increment the exponent till
  556. they're the same */
  557. {
  558. int diff;
  559. int sdiff;
  560. a_normal_exp = a->normal_exp;
  561. b_normal_exp = b->normal_exp;
  562. a_fraction = a->fraction.ll;
  563. b_fraction = b->fraction.ll;
  564. diff = a_normal_exp - b_normal_exp;
  565. sdiff = diff;
  566. if (diff < 0)
  567. diff = -diff;
  568. if (diff < FRAC_NBITS)
  569. {
  570. if (sdiff > 0)
  571. {
  572. b_normal_exp += diff;
  573. LSHIFT (b_fraction, diff);
  574. }
  575. else if (sdiff < 0)
  576. {
  577. a_normal_exp += diff;
  578. LSHIFT (a_fraction, diff);
  579. }
  580. }
  581. else
  582. {
  583. /* Somethings's up.. choose the biggest */
  584. if (a_normal_exp > b_normal_exp)
  585. {
  586. b_normal_exp = a_normal_exp;
  587. b_fraction = 0;
  588. }
  589. else
  590. {
  591. a_normal_exp = b_normal_exp;
  592. a_fraction = 0;
  593. }
  594. }
  595. }
  596. if (a->sign != b->sign)
  597. {
  598. if (a->sign)
  599. {
  600. tfraction = -a_fraction + b_fraction;
  601. }
  602. else
  603. {
  604. tfraction = a_fraction - b_fraction;
  605. }
  606. if (tfraction >= 0)
  607. {
  608. tmp->sign = 0;
  609. tmp->normal_exp = a_normal_exp;
  610. tmp->fraction.ll = tfraction;
  611. }
  612. else
  613. {
  614. tmp->sign = 1;
  615. tmp->normal_exp = a_normal_exp;
  616. tmp->fraction.ll = -tfraction;
  617. }
  618. /* and renormalize it */
  619. while (tmp->fraction.ll < IMPLICIT_1 && tmp->fraction.ll)
  620. {
  621. tmp->fraction.ll <<= 1;
  622. tmp->normal_exp--;
  623. }
  624. }
  625. else
  626. {
  627. tmp->sign = a->sign;
  628. tmp->normal_exp = a_normal_exp;
  629. tmp->fraction.ll = a_fraction + b_fraction;
  630. }
  631. tmp->class = CLASS_NUMBER;
  632. /* Now the fraction is added, we have to shift down to renormalize the
  633. number */
  634. if (tmp->fraction.ll >= IMPLICIT_2)
  635. {
  636. LSHIFT (tmp->fraction.ll, 1);
  637. tmp->normal_exp++;
  638. }
  639. return tmp;
  640. }
  641. FLO_type
  642. add (FLO_type arg_a, FLO_type arg_b)
  643. {
  644. fp_number_type a;
  645. fp_number_type b;
  646. fp_number_type tmp;
  647. const fp_number_type *res;
  648. FLO_union_type au, bu;
  649. au.value = arg_a;
  650. bu.value = arg_b;
  651. unpack_d (&au, &a);
  652. unpack_d (&bu, &b);
  653. res = _fpadd_parts (&a, &b, &tmp);
  654. return pack_d (res);
  655. }
  656. FLO_type
  657. sub (FLO_type arg_a, FLO_type arg_b)
  658. {
  659. fp_number_type a;
  660. fp_number_type b;
  661. fp_number_type tmp;
  662. const fp_number_type *res;
  663. FLO_union_type au, bu;
  664. au.value = arg_a;
  665. bu.value = arg_b;
  666. unpack_d (&au, &a);
  667. unpack_d (&bu, &b);
  668. b.sign ^= 1;
  669. res = _fpadd_parts (&a, &b, &tmp);
  670. return pack_d (res);
  671. }
  672. #endif /* L_addsub_sf || L_addsub_df */
  673. #if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
  674. static inline __attribute__ ((__always_inline__)) const fp_number_type *
  675. _fpmul_parts ( fp_number_type * a,
  676. fp_number_type * b,
  677. fp_number_type * tmp)
  678. {
  679. fractype low = 0;
  680. fractype high = 0;
  681. if (isnan (a))
  682. {
  683. a->sign = a->sign != b->sign;
  684. return a;
  685. }
  686. if (isnan (b))
  687. {
  688. b->sign = a->sign != b->sign;
  689. return b;
  690. }
  691. if (isinf (a))
  692. {
  693. if (iszero (b))
  694. return makenan ();
  695. a->sign = a->sign != b->sign;
  696. return a;
  697. }
  698. if (isinf (b))
  699. {
  700. if (iszero (a))
  701. {
  702. return makenan ();
  703. }
  704. b->sign = a->sign != b->sign;
  705. return b;
  706. }
  707. if (iszero (a))
  708. {
  709. a->sign = a->sign != b->sign;
  710. return a;
  711. }
  712. if (iszero (b))
  713. {
  714. b->sign = a->sign != b->sign;
  715. return b;
  716. }
  717. /* Calculate the mantissa by multiplying both numbers to get a
  718. twice-as-wide number. */
  719. {
  720. #if defined(NO_DI_MODE) || defined(TFLOAT)
  721. {
  722. fractype x = a->fraction.ll;
  723. fractype ylow = b->fraction.ll;
  724. fractype yhigh = 0;
  725. int bit;
  726. /* ??? This does multiplies one bit at a time. Optimize. */
  727. for (bit = 0; bit < FRAC_NBITS; bit++)
  728. {
  729. int carry;
  730. if (x & 1)
  731. {
  732. carry = (low += ylow) < ylow;
  733. high += yhigh + carry;
  734. }
  735. yhigh <<= 1;
  736. if (ylow & FRACHIGH)
  737. {
  738. yhigh |= 1;
  739. }
  740. ylow <<= 1;
  741. x >>= 1;
  742. }
  743. }
  744. #elif defined(FLOAT)
  745. /* Multiplying two USIs to get a UDI, we're safe. */
  746. {
  747. UDItype answer = (UDItype)a->fraction.ll * (UDItype)b->fraction.ll;
  748. high = answer >> BITS_PER_SI;
  749. low = answer;
  750. }
  751. #else
  752. /* fractype is DImode, but we need the result to be twice as wide.
  753. Assuming a widening multiply from DImode to TImode is not
  754. available, build one by hand. */
  755. {
  756. USItype nl = a->fraction.ll;
  757. USItype nh = a->fraction.ll >> BITS_PER_SI;
  758. USItype ml = b->fraction.ll;
  759. USItype mh = b->fraction.ll >> BITS_PER_SI;
  760. UDItype pp_ll = (UDItype) ml * nl;
  761. UDItype pp_hl = (UDItype) mh * nl;
  762. UDItype pp_lh = (UDItype) ml * nh;
  763. UDItype pp_hh = (UDItype) mh * nh;
  764. UDItype res2 = 0;
  765. UDItype res0 = 0;
  766. UDItype ps_hh__ = pp_hl + pp_lh;
  767. if (ps_hh__ < pp_hl)
  768. res2 += (UDItype)1 << BITS_PER_SI;
  769. pp_hl = (UDItype)(USItype)ps_hh__ << BITS_PER_SI;
  770. res0 = pp_ll + pp_hl;
  771. if (res0 < pp_ll)
  772. res2++;
  773. res2 += (ps_hh__ >> BITS_PER_SI) + pp_hh;
  774. high = res2;
  775. low = res0;
  776. }
  777. #endif
  778. }
  779. tmp->normal_exp = a->normal_exp + b->normal_exp
  780. + FRAC_NBITS - (FRACBITS + NGARDS);
  781. tmp->sign = a->sign != b->sign;
  782. while (high >= IMPLICIT_2)
  783. {
  784. tmp->normal_exp++;
  785. if (high & 1)
  786. {
  787. low >>= 1;
  788. low |= FRACHIGH;
  789. }
  790. high >>= 1;
  791. }
  792. while (high < IMPLICIT_1)
  793. {
  794. tmp->normal_exp--;
  795. high <<= 1;
  796. if (low & FRACHIGH)
  797. high |= 1;
  798. low <<= 1;
  799. }
  800. if ((high & GARDMASK) == GARDMSB)
  801. {
  802. if (high & (1 << NGARDS))
  803. {
  804. /* Because we're half way, we would round to even by adding
  805. GARDROUND + 1, except that's also done in the packing
  806. function, and rounding twice will lose precision and cause
  807. the result to be too far off. Example: 32-bit floats with
  808. bit patterns 0xfff * 0x3f800400 ~= 0xfff (less than 0.5ulp
  809. off), not 0x1000 (more than 0.5ulp off). */
  810. }
  811. else if (low)
  812. {
  813. /* We're a further than half way by a small amount corresponding
  814. to the bits set in "low". Knowing that, we round here and
  815. not in pack_d, because there we don't have "low" available
  816. anymore. */
  817. high += GARDROUND + 1;
  818. /* Avoid further rounding in pack_d. */
  819. high &= ~(fractype) GARDMASK;
  820. }
  821. }
  822. tmp->fraction.ll = high;
  823. tmp->class = CLASS_NUMBER;
  824. return tmp;
  825. }
  826. FLO_type
  827. multiply (FLO_type arg_a, FLO_type arg_b)
  828. {
  829. fp_number_type a;
  830. fp_number_type b;
  831. fp_number_type tmp;
  832. const fp_number_type *res;
  833. FLO_union_type au, bu;
  834. au.value = arg_a;
  835. bu.value = arg_b;
  836. unpack_d (&au, &a);
  837. unpack_d (&bu, &b);
  838. res = _fpmul_parts (&a, &b, &tmp);
  839. return pack_d (res);
  840. }
  841. #endif /* L_mul_sf || L_mul_df || L_mul_tf */
  842. #if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
  843. static inline __attribute__ ((__always_inline__)) const fp_number_type *
  844. _fpdiv_parts (fp_number_type * a,
  845. fp_number_type * b)
  846. {
  847. fractype bit;
  848. fractype numerator;
  849. fractype denominator;
  850. fractype quotient;
  851. if (isnan (a))
  852. {
  853. return a;
  854. }
  855. if (isnan (b))
  856. {
  857. return b;
  858. }
  859. a->sign = a->sign ^ b->sign;
  860. if (isinf (a) || iszero (a))
  861. {
  862. if (a->class == b->class)
  863. return makenan ();
  864. return a;
  865. }
  866. if (isinf (b))
  867. {
  868. a->fraction.ll = 0;
  869. a->normal_exp = 0;
  870. return a;
  871. }
  872. if (iszero (b))
  873. {
  874. a->class = CLASS_INFINITY;
  875. return a;
  876. }
  877. /* Calculate the mantissa by multiplying both 64bit numbers to get a
  878. 128 bit number */
  879. {
  880. /* quotient =
  881. ( numerator / denominator) * 2^(numerator exponent - denominator exponent)
  882. */
  883. a->normal_exp = a->normal_exp - b->normal_exp;
  884. numerator = a->fraction.ll;
  885. denominator = b->fraction.ll;
  886. if (numerator < denominator)
  887. {
  888. /* Fraction will be less than 1.0 */
  889. numerator *= 2;
  890. a->normal_exp--;
  891. }
  892. bit = IMPLICIT_1;
  893. quotient = 0;
  894. /* ??? Does divide one bit at a time. Optimize. */
  895. while (bit)
  896. {
  897. if (numerator >= denominator)
  898. {
  899. quotient |= bit;
  900. numerator -= denominator;
  901. }
  902. bit >>= 1;
  903. numerator *= 2;
  904. }
  905. if ((quotient & GARDMASK) == GARDMSB)
  906. {
  907. if (quotient & (1 << NGARDS))
  908. {
  909. /* Because we're half way, we would round to even by adding
  910. GARDROUND + 1, except that's also done in the packing
  911. function, and rounding twice will lose precision and cause
  912. the result to be too far off. */
  913. }
  914. else if (numerator)
  915. {
  916. /* We're a further than half way by the small amount
  917. corresponding to the bits set in "numerator". Knowing
  918. that, we round here and not in pack_d, because there we
  919. don't have "numerator" available anymore. */
  920. quotient += GARDROUND + 1;
  921. /* Avoid further rounding in pack_d. */
  922. quotient &= ~(fractype) GARDMASK;
  923. }
  924. }
  925. a->fraction.ll = quotient;
  926. return (a);
  927. }
  928. }
  929. FLO_type
  930. divide (FLO_type arg_a, FLO_type arg_b)
  931. {
  932. fp_number_type a;
  933. fp_number_type b;
  934. const fp_number_type *res;
  935. FLO_union_type au, bu;
  936. au.value = arg_a;
  937. bu.value = arg_b;
  938. unpack_d (&au, &a);
  939. unpack_d (&bu, &b);
  940. res = _fpdiv_parts (&a, &b);
  941. return pack_d (res);
  942. }
  943. #endif /* L_div_sf || L_div_df */
  944. #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df) \
  945. || defined(L_fpcmp_parts_tf)
  946. /* according to the demo, fpcmp returns a comparison with 0... thus
  947. a<b -> -1
  948. a==b -> 0
  949. a>b -> +1
  950. */
  951. int
  952. __fpcmp_parts (fp_number_type * a, fp_number_type * b)
  953. {
  954. #if 0
  955. /* either nan -> unordered. Must be checked outside of this routine. */
  956. if (isnan (a) && isnan (b))
  957. {
  958. return 1; /* still unordered! */
  959. }
  960. #endif
  961. if (isnan (a) || isnan (b))
  962. {
  963. return 1; /* how to indicate unordered compare? */
  964. }
  965. if (isinf (a) && isinf (b))
  966. {
  967. /* +inf > -inf, but +inf != +inf */
  968. /* b \a| +inf(0)| -inf(1)
  969. ______\+--------+--------
  970. +inf(0)| a==b(0)| a<b(-1)
  971. -------+--------+--------
  972. -inf(1)| a>b(1) | a==b(0)
  973. -------+--------+--------
  974. So since unordered must be nonzero, just line up the columns...
  975. */
  976. return b->sign - a->sign;
  977. }
  978. /* but not both... */
  979. if (isinf (a))
  980. {
  981. return a->sign ? -1 : 1;
  982. }
  983. if (isinf (b))
  984. {
  985. return b->sign ? 1 : -1;
  986. }
  987. if (iszero (a) && iszero (b))
  988. {
  989. return 0;
  990. }
  991. if (iszero (a))
  992. {
  993. return b->sign ? 1 : -1;
  994. }
  995. if (iszero (b))
  996. {
  997. return a->sign ? -1 : 1;
  998. }
  999. /* now both are "normal". */
  1000. if (a->sign != b->sign)
  1001. {
  1002. /* opposite signs */
  1003. return a->sign ? -1 : 1;
  1004. }
  1005. /* same sign; exponents? */
  1006. if (a->normal_exp > b->normal_exp)
  1007. {
  1008. return a->sign ? -1 : 1;
  1009. }
  1010. if (a->normal_exp < b->normal_exp)
  1011. {
  1012. return a->sign ? 1 : -1;
  1013. }
  1014. /* same exponents; check size. */
  1015. if (a->fraction.ll > b->fraction.ll)
  1016. {
  1017. return a->sign ? -1 : 1;
  1018. }
  1019. if (a->fraction.ll < b->fraction.ll)
  1020. {
  1021. return a->sign ? 1 : -1;
  1022. }
  1023. /* after all that, they're equal. */
  1024. return 0;
  1025. }
  1026. #endif
  1027. #if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compoare_tf)
  1028. CMPtype
  1029. compare (FLO_type arg_a, FLO_type arg_b)
  1030. {
  1031. fp_number_type a;
  1032. fp_number_type b;
  1033. FLO_union_type au, bu;
  1034. au.value = arg_a;
  1035. bu.value = arg_b;
  1036. unpack_d (&au, &a);
  1037. unpack_d (&bu, &b);
  1038. return __fpcmp_parts (&a, &b);
  1039. }
  1040. #endif /* L_compare_sf || L_compare_df */
  1041. /* These should be optimized for their specific tasks someday. */
  1042. #if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
  1043. CMPtype
  1044. _eq_f2 (FLO_type arg_a, FLO_type arg_b)
  1045. {
  1046. fp_number_type a;
  1047. fp_number_type b;
  1048. FLO_union_type au, bu;
  1049. au.value = arg_a;
  1050. bu.value = arg_b;
  1051. unpack_d (&au, &a);
  1052. unpack_d (&bu, &b);
  1053. if (isnan (&a) || isnan (&b))
  1054. return 1; /* false, truth == 0 */
  1055. return __fpcmp_parts (&a, &b) ;
  1056. }
  1057. #endif /* L_eq_sf || L_eq_df */
  1058. #if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
  1059. CMPtype
  1060. _ne_f2 (FLO_type arg_a, FLO_type arg_b)
  1061. {
  1062. fp_number_type a;
  1063. fp_number_type b;
  1064. FLO_union_type au, bu;
  1065. au.value = arg_a;
  1066. bu.value = arg_b;
  1067. unpack_d (&au, &a);
  1068. unpack_d (&bu, &b);
  1069. if (isnan (&a) || isnan (&b))
  1070. return 1; /* true, truth != 0 */
  1071. return __fpcmp_parts (&a, &b) ;
  1072. }
  1073. #endif /* L_ne_sf || L_ne_df */
  1074. #if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
  1075. CMPtype
  1076. _gt_f2 (FLO_type arg_a, FLO_type arg_b)
  1077. {
  1078. fp_number_type a;
  1079. fp_number_type b;
  1080. FLO_union_type au, bu;
  1081. au.value = arg_a;
  1082. bu.value = arg_b;
  1083. unpack_d (&au, &a);
  1084. unpack_d (&bu, &b);
  1085. if (isnan (&a) || isnan (&b))
  1086. return -1; /* false, truth > 0 */
  1087. return __fpcmp_parts (&a, &b);
  1088. }
  1089. #endif /* L_gt_sf || L_gt_df */
  1090. #if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
  1091. CMPtype
  1092. _ge_f2 (FLO_type arg_a, FLO_type arg_b)
  1093. {
  1094. fp_number_type a;
  1095. fp_number_type b;
  1096. FLO_union_type au, bu;
  1097. au.value = arg_a;
  1098. bu.value = arg_b;
  1099. unpack_d (&au, &a);
  1100. unpack_d (&bu, &b);
  1101. if (isnan (&a) || isnan (&b))
  1102. return -1; /* false, truth >= 0 */
  1103. return __fpcmp_parts (&a, &b) ;
  1104. }
  1105. #endif /* L_ge_sf || L_ge_df */
  1106. #if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
  1107. CMPtype
  1108. _lt_f2 (FLO_type arg_a, FLO_type arg_b)
  1109. {
  1110. fp_number_type a;
  1111. fp_number_type b;
  1112. FLO_union_type au, bu;
  1113. au.value = arg_a;
  1114. bu.value = arg_b;
  1115. unpack_d (&au, &a);
  1116. unpack_d (&bu, &b);
  1117. if (isnan (&a) || isnan (&b))
  1118. return 1; /* false, truth < 0 */
  1119. return __fpcmp_parts (&a, &b);
  1120. }
  1121. #endif /* L_lt_sf || L_lt_df */
  1122. #if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
  1123. CMPtype
  1124. _le_f2 (FLO_type arg_a, FLO_type arg_b)
  1125. {
  1126. fp_number_type a;
  1127. fp_number_type b;
  1128. FLO_union_type au, bu;
  1129. au.value = arg_a;
  1130. bu.value = arg_b;
  1131. unpack_d (&au, &a);
  1132. unpack_d (&bu, &b);
  1133. if (isnan (&a) || isnan (&b))
  1134. return 1; /* false, truth <= 0 */
  1135. return __fpcmp_parts (&a, &b) ;
  1136. }
  1137. #endif /* L_le_sf || L_le_df */
  1138. #if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
  1139. CMPtype
  1140. _unord_f2 (FLO_type arg_a, FLO_type arg_b)
  1141. {
  1142. fp_number_type a;
  1143. fp_number_type b;
  1144. FLO_union_type au, bu;
  1145. au.value = arg_a;
  1146. bu.value = arg_b;
  1147. unpack_d (&au, &a);
  1148. unpack_d (&bu, &b);
  1149. return (isnan (&a) || isnan (&b));
  1150. }
  1151. #endif /* L_unord_sf || L_unord_df */
  1152. #if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
  1153. FLO_type
  1154. si_to_float (SItype arg_a)
  1155. {
  1156. fp_number_type in;
  1157. in.class = CLASS_NUMBER;
  1158. in.sign = arg_a < 0;
  1159. if (!arg_a)
  1160. {
  1161. in.class = CLASS_ZERO;
  1162. }
  1163. else
  1164. {
  1165. USItype uarg;
  1166. int shift;
  1167. in.normal_exp = FRACBITS + NGARDS;
  1168. if (in.sign)
  1169. {
  1170. /* Special case for minint, since there is no +ve integer
  1171. representation for it */
  1172. if (arg_a == (- MAX_SI_INT - 1))
  1173. {
  1174. return (FLO_type)(- MAX_SI_INT - 1);
  1175. }
  1176. uarg = (-arg_a);
  1177. }
  1178. else
  1179. uarg = arg_a;
  1180. in.fraction.ll = uarg;
  1181. shift = clzusi (uarg) - (BITS_PER_SI - 1 - FRACBITS - NGARDS);
  1182. if (shift > 0)
  1183. {
  1184. in.fraction.ll <<= shift;
  1185. in.normal_exp -= shift;
  1186. }
  1187. }
  1188. return pack_d (&in);
  1189. }
  1190. #endif /* L_si_to_sf || L_si_to_df */
  1191. #if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
  1192. FLO_type
  1193. usi_to_float (USItype arg_a)
  1194. {
  1195. fp_number_type in;
  1196. in.sign = 0;
  1197. if (!arg_a)
  1198. {
  1199. in.class = CLASS_ZERO;
  1200. }
  1201. else
  1202. {
  1203. int shift;
  1204. in.class = CLASS_NUMBER;
  1205. in.normal_exp = FRACBITS + NGARDS;
  1206. in.fraction.ll = arg_a;
  1207. shift = clzusi (arg_a) - (BITS_PER_SI - 1 - FRACBITS - NGARDS);
  1208. if (shift < 0)
  1209. {
  1210. fractype guard = in.fraction.ll & (((fractype)1 << -shift) - 1);
  1211. in.fraction.ll >>= -shift;
  1212. in.fraction.ll |= (guard != 0);
  1213. in.normal_exp -= shift;
  1214. }
  1215. else if (shift > 0)
  1216. {
  1217. in.fraction.ll <<= shift;
  1218. in.normal_exp -= shift;
  1219. }
  1220. }
  1221. return pack_d (&in);
  1222. }
  1223. #endif
  1224. #if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
  1225. SItype
  1226. float_to_si (FLO_type arg_a)
  1227. {
  1228. fp_number_type a;
  1229. SItype tmp;
  1230. FLO_union_type au;
  1231. au.value = arg_a;
  1232. unpack_d (&au, &a);
  1233. if (iszero (&a))
  1234. return 0;
  1235. if (isnan (&a))
  1236. return 0;
  1237. /* get reasonable MAX_SI_INT... */
  1238. if (isinf (&a))
  1239. return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
  1240. /* it is a number, but a small one */
  1241. if (a.normal_exp < 0)
  1242. return 0;
  1243. if (a.normal_exp > BITS_PER_SI - 2)
  1244. return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
  1245. tmp = a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
  1246. return a.sign ? (-tmp) : (tmp);
  1247. }
  1248. #endif /* L_sf_to_si || L_df_to_si */
  1249. #if defined(L_tf_to_usi)
  1250. USItype
  1251. float_to_usi (FLO_type arg_a)
  1252. {
  1253. fp_number_type a;
  1254. FLO_union_type au;
  1255. au.value = arg_a;
  1256. unpack_d (&au, &a);
  1257. if (iszero (&a))
  1258. return 0;
  1259. if (isnan (&a))
  1260. return 0;
  1261. /* it is a negative number */
  1262. if (a.sign)
  1263. return 0;
  1264. /* get reasonable MAX_USI_INT... */
  1265. if (isinf (&a))
  1266. return MAX_USI_INT;
  1267. /* it is a number, but a small one */
  1268. if (a.normal_exp < 0)
  1269. return 0;
  1270. if (a.normal_exp > BITS_PER_SI - 1)
  1271. return MAX_USI_INT;
  1272. else if (a.normal_exp > (FRACBITS + NGARDS))
  1273. return a.fraction.ll << (a.normal_exp - (FRACBITS + NGARDS));
  1274. else
  1275. return a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
  1276. }
  1277. #endif /* L_tf_to_usi */
  1278. #if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
  1279. FLO_type
  1280. negate (FLO_type arg_a)
  1281. {
  1282. fp_number_type a;
  1283. FLO_union_type au;
  1284. au.value = arg_a;
  1285. unpack_d (&au, &a);
  1286. flip_sign (&a);
  1287. return pack_d (&a);
  1288. }
  1289. #endif /* L_negate_sf || L_negate_df */
  1290. #ifdef FLOAT
  1291. #if defined(L_make_sf)
  1292. SFtype
  1293. __make_fp(fp_class_type class,
  1294. unsigned int sign,
  1295. int exp,
  1296. USItype frac)
  1297. {
  1298. fp_number_type in;
  1299. in.class = class;
  1300. in.sign = sign;
  1301. in.normal_exp = exp;
  1302. in.fraction.ll = frac;
  1303. return pack_d (&in);
  1304. }
  1305. #endif /* L_make_sf */
  1306. #ifndef FLOAT_ONLY
  1307. /* This enables one to build an fp library that supports float but not double.
  1308. Otherwise, we would get an undefined reference to __make_dp.
  1309. This is needed for some 8-bit ports that can't handle well values that
  1310. are 8-bytes in size, so we just don't support double for them at all. */
  1311. #if defined(L_sf_to_df)
  1312. DFtype
  1313. sf_to_df (SFtype arg_a)
  1314. {
  1315. fp_number_type in;
  1316. FLO_union_type au;
  1317. au.value = arg_a;
  1318. unpack_d (&au, &in);
  1319. return __make_dp (in.class, in.sign, in.normal_exp,
  1320. ((UDItype) in.fraction.ll) << F_D_BITOFF);
  1321. }
  1322. #endif /* L_sf_to_df */
  1323. #if defined(L_sf_to_tf) && defined(TMODES)
  1324. TFtype
  1325. sf_to_tf (SFtype arg_a)
  1326. {
  1327. fp_number_type in;
  1328. FLO_union_type au;
  1329. au.value = arg_a;
  1330. unpack_d (&au, &in);
  1331. return __make_tp (in.class, in.sign, in.normal_exp,
  1332. ((UTItype) in.fraction.ll) << F_T_BITOFF);
  1333. }
  1334. #endif /* L_sf_to_df */
  1335. #endif /* ! FLOAT_ONLY */
  1336. #endif /* FLOAT */
  1337. #ifndef FLOAT
  1338. extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
  1339. #if defined(L_make_df)
  1340. DFtype
  1341. __make_dp (fp_class_type class, unsigned int sign, int exp, UDItype frac)
  1342. {
  1343. fp_number_type in;
  1344. in.class = class;
  1345. in.sign = sign;
  1346. in.normal_exp = exp;
  1347. in.fraction.ll = frac;
  1348. return pack_d (&in);
  1349. }
  1350. #endif /* L_make_df */
  1351. #if defined(L_df_to_sf)
  1352. SFtype
  1353. df_to_sf (DFtype arg_a)
  1354. {
  1355. fp_number_type in;
  1356. USItype sffrac;
  1357. FLO_union_type au;
  1358. au.value = arg_a;
  1359. unpack_d (&au, &in);
  1360. sffrac = in.fraction.ll >> F_D_BITOFF;
  1361. /* We set the lowest guard bit in SFFRAC if we discarded any non
  1362. zero bits. */
  1363. if ((in.fraction.ll & (((USItype) 1 << F_D_BITOFF) - 1)) != 0)
  1364. sffrac |= 1;
  1365. return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
  1366. }
  1367. #endif /* L_df_to_sf */
  1368. #if defined(L_df_to_tf) && defined(TMODES) \
  1369. && !defined(FLOAT) && !defined(TFLOAT)
  1370. TFtype
  1371. df_to_tf (DFtype arg_a)
  1372. {
  1373. fp_number_type in;
  1374. FLO_union_type au;
  1375. au.value = arg_a;
  1376. unpack_d (&au, &in);
  1377. return __make_tp (in.class, in.sign, in.normal_exp,
  1378. ((UTItype) in.fraction.ll) << D_T_BITOFF);
  1379. }
  1380. #endif /* L_sf_to_df */
  1381. #ifdef TFLOAT
  1382. #if defined(L_make_tf)
  1383. TFtype
  1384. __make_tp(fp_class_type class,
  1385. unsigned int sign,
  1386. int exp,
  1387. UTItype frac)
  1388. {
  1389. fp_number_type in;
  1390. in.class = class;
  1391. in.sign = sign;
  1392. in.normal_exp = exp;
  1393. in.fraction.ll = frac;
  1394. return pack_d (&in);
  1395. }
  1396. #endif /* L_make_tf */
  1397. #if defined(L_tf_to_df)
  1398. DFtype
  1399. tf_to_df (TFtype arg_a)
  1400. {
  1401. fp_number_type in;
  1402. UDItype sffrac;
  1403. FLO_union_type au;
  1404. au.value = arg_a;
  1405. unpack_d (&au, &in);
  1406. sffrac = in.fraction.ll >> D_T_BITOFF;
  1407. /* We set the lowest guard bit in SFFRAC if we discarded any non
  1408. zero bits. */
  1409. if ((in.fraction.ll & (((UTItype) 1 << D_T_BITOFF) - 1)) != 0)
  1410. sffrac |= 1;
  1411. return __make_dp (in.class, in.sign, in.normal_exp, sffrac);
  1412. }
  1413. #endif /* L_tf_to_df */
  1414. #if defined(L_tf_to_sf)
  1415. SFtype
  1416. tf_to_sf (TFtype arg_a)
  1417. {
  1418. fp_number_type in;
  1419. USItype sffrac;
  1420. FLO_union_type au;
  1421. au.value = arg_a;
  1422. unpack_d (&au, &in);
  1423. sffrac = in.fraction.ll >> F_T_BITOFF;
  1424. /* We set the lowest guard bit in SFFRAC if we discarded any non
  1425. zero bits. */
  1426. if ((in.fraction.ll & (((UTItype) 1 << F_T_BITOFF) - 1)) != 0)
  1427. sffrac |= 1;
  1428. return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
  1429. }
  1430. #endif /* L_tf_to_sf */
  1431. #endif /* TFLOAT */
  1432. #endif /* ! FLOAT */
  1433. #endif /* !EXTENDED_FLOAT_STUBS */