fp-bit.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  1. /* This is a software floating point library which can be used
  2. for targets without hardware floating point.
  3. Copyright (C) 1994-2015 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. #ifdef FLOAT_BIT_ORDER_MISMATCH
  287. dst.bits.fraction = fraction;
  288. dst.bits.exp = exp;
  289. dst.bits.sign = sign;
  290. #else
  291. # if defined TFLOAT && defined HALFFRACBITS
  292. {
  293. halffractype high, low, unity;
  294. int lowsign, lowexp;
  295. unity = (halffractype) 1 << HALFFRACBITS;
  296. /* Set HIGH to the high double's significand, masking out the implicit 1.
  297. Set LOW to the low double's full significand. */
  298. high = (fraction >> (FRACBITS - HALFFRACBITS)) & (unity - 1);
  299. low = fraction & (unity * 2 - 1);
  300. /* Get the initial sign and exponent of the low double. */
  301. lowexp = exp - HALFFRACBITS - 1;
  302. lowsign = sign;
  303. /* HIGH should be rounded like a normal double, making |LOW| <=
  304. 0.5 ULP of HIGH. Assume round-to-nearest. */
  305. if (exp < EXPMAX)
  306. if (low > unity || (low == unity && (high & 1) == 1))
  307. {
  308. /* Round HIGH up and adjust LOW to match. */
  309. high++;
  310. if (high == unity)
  311. {
  312. /* May make it infinite, but that's OK. */
  313. high = 0;
  314. exp++;
  315. }
  316. low = unity * 2 - low;
  317. lowsign ^= 1;
  318. }
  319. high |= (halffractype) exp << HALFFRACBITS;
  320. high |= (halffractype) sign << (HALFFRACBITS + EXPBITS);
  321. if (exp == EXPMAX || exp == 0 || low == 0)
  322. low = 0;
  323. else
  324. {
  325. while (lowexp > 0 && low < unity)
  326. {
  327. low <<= 1;
  328. lowexp--;
  329. }
  330. if (lowexp <= 0)
  331. {
  332. halffractype roundmsb, round;
  333. int shift;
  334. shift = 1 - lowexp;
  335. roundmsb = (1 << (shift - 1));
  336. round = low & ((roundmsb << 1) - 1);
  337. low >>= shift;
  338. lowexp = 0;
  339. if (round > roundmsb || (round == roundmsb && (low & 1) == 1))
  340. {
  341. low++;
  342. if (low == unity)
  343. /* LOW rounds up to the smallest normal number. */
  344. lowexp++;
  345. }
  346. }
  347. low &= unity - 1;
  348. low |= (halffractype) lowexp << HALFFRACBITS;
  349. low |= (halffractype) lowsign << (HALFFRACBITS + EXPBITS);
  350. }
  351. dst.value_raw = ((fractype) high << HALFSHIFT) | low;
  352. }
  353. # else
  354. dst.value_raw = fraction & ((((fractype)1) << FRACBITS) - (fractype)1);
  355. dst.value_raw |= ((fractype) (exp & ((1 << EXPBITS) - 1))) << FRACBITS;
  356. dst.value_raw |= ((fractype) (sign & 1)) << (FRACBITS | EXPBITS);
  357. # endif
  358. #endif
  359. #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
  360. #ifdef TFLOAT
  361. {
  362. qrtrfractype tmp1 = dst.words[0];
  363. qrtrfractype tmp2 = dst.words[1];
  364. dst.words[0] = dst.words[3];
  365. dst.words[1] = dst.words[2];
  366. dst.words[2] = tmp2;
  367. dst.words[3] = tmp1;
  368. }
  369. #else
  370. {
  371. halffractype tmp = dst.words[0];
  372. dst.words[0] = dst.words[1];
  373. dst.words[1] = tmp;
  374. }
  375. #endif
  376. #endif
  377. return dst.value;
  378. }
  379. #endif
  380. #if defined(L_unpack_df) || defined(L_unpack_sf) || defined(L_unpack_tf)
  381. void
  382. unpack_d (FLO_union_type * src, fp_number_type * dst)
  383. {
  384. /* We previously used bitfields to store the number, but this doesn't
  385. handle little/big endian systems conveniently, so use shifts and
  386. masks */
  387. fractype fraction;
  388. int exp;
  389. int sign;
  390. #if defined(FLOAT_WORD_ORDER_MISMATCH) && !defined(FLOAT)
  391. FLO_union_type swapped;
  392. #ifdef TFLOAT
  393. swapped.words[0] = src->words[3];
  394. swapped.words[1] = src->words[2];
  395. swapped.words[2] = src->words[1];
  396. swapped.words[3] = src->words[0];
  397. #else
  398. swapped.words[0] = src->words[1];
  399. swapped.words[1] = src->words[0];
  400. #endif
  401. src = &swapped;
  402. #endif
  403. #ifdef FLOAT_BIT_ORDER_MISMATCH
  404. fraction = src->bits.fraction;
  405. exp = src->bits.exp;
  406. sign = src->bits.sign;
  407. #else
  408. # if defined TFLOAT && defined HALFFRACBITS
  409. {
  410. halffractype high, low;
  411. high = src->value_raw >> HALFSHIFT;
  412. low = src->value_raw & (((fractype)1 << HALFSHIFT) - 1);
  413. fraction = high & ((((fractype)1) << HALFFRACBITS) - 1);
  414. fraction <<= FRACBITS - HALFFRACBITS;
  415. exp = ((int)(high >> HALFFRACBITS)) & ((1 << EXPBITS) - 1);
  416. sign = ((int)(high >> (((HALFFRACBITS + EXPBITS))))) & 1;
  417. if (exp != EXPMAX && exp != 0 && low != 0)
  418. {
  419. int lowexp = ((int)(low >> HALFFRACBITS)) & ((1 << EXPBITS) - 1);
  420. int lowsign = ((int)(low >> (((HALFFRACBITS + EXPBITS))))) & 1;
  421. int shift;
  422. fractype xlow;
  423. xlow = low & ((((fractype)1) << HALFFRACBITS) - 1);
  424. if (lowexp)
  425. xlow |= (((halffractype)1) << HALFFRACBITS);
  426. else
  427. lowexp = 1;
  428. shift = (FRACBITS - HALFFRACBITS) - (exp - lowexp);
  429. if (shift > 0)
  430. xlow <<= shift;
  431. else if (shift < 0)
  432. xlow >>= -shift;
  433. if (sign == lowsign)
  434. fraction += xlow;
  435. else if (fraction >= xlow)
  436. fraction -= xlow;
  437. else
  438. {
  439. /* The high part is a power of two but the full number is lower.
  440. This code will leave the implicit 1 in FRACTION, but we'd
  441. have added that below anyway. */
  442. fraction = (((fractype) 1 << FRACBITS) - xlow) << 1;
  443. exp--;
  444. }
  445. }
  446. }
  447. # else
  448. fraction = src->value_raw & ((((fractype)1) << FRACBITS) - 1);
  449. exp = ((int)(src->value_raw >> FRACBITS)) & ((1 << EXPBITS) - 1);
  450. sign = ((int)(src->value_raw >> (FRACBITS + EXPBITS))) & 1;
  451. # endif
  452. #endif
  453. dst->sign = sign;
  454. if (exp == 0)
  455. {
  456. /* Hmm. Looks like 0 */
  457. if (fraction == 0
  458. #ifdef NO_DENORMALS
  459. || 1
  460. #endif
  461. )
  462. {
  463. /* tastes like zero */
  464. dst->class = CLASS_ZERO;
  465. }
  466. else
  467. {
  468. /* Zero exponent with nonzero fraction - it's denormalized,
  469. so there isn't a leading implicit one - we'll shift it so
  470. it gets one. */
  471. dst->normal_exp = exp - EXPBIAS + 1;
  472. fraction <<= NGARDS;
  473. dst->class = CLASS_NUMBER;
  474. #if 1
  475. while (fraction < IMPLICIT_1)
  476. {
  477. fraction <<= 1;
  478. dst->normal_exp--;
  479. }
  480. #endif
  481. dst->fraction.ll = fraction;
  482. }
  483. }
  484. else if (__builtin_expect (exp == EXPMAX, 0))
  485. {
  486. /* Huge exponent*/
  487. if (fraction == 0)
  488. {
  489. /* Attached to a zero fraction - means infinity */
  490. dst->class = CLASS_INFINITY;
  491. }
  492. else
  493. {
  494. /* Nonzero fraction, means nan */
  495. #ifdef QUIET_NAN_NEGATED
  496. if ((fraction & QUIET_NAN) == 0)
  497. #else
  498. if (fraction & QUIET_NAN)
  499. #endif
  500. {
  501. dst->class = CLASS_QNAN;
  502. }
  503. else
  504. {
  505. dst->class = CLASS_SNAN;
  506. }
  507. /* Now that we know which kind of NaN we got, discard the
  508. quiet/signaling bit, but do preserve the NaN payload. */
  509. fraction &= ~QUIET_NAN;
  510. dst->fraction.ll = fraction << NGARDS;
  511. }
  512. }
  513. else
  514. {
  515. /* Nothing strange about this number */
  516. dst->normal_exp = exp - EXPBIAS;
  517. dst->class = CLASS_NUMBER;
  518. dst->fraction.ll = (fraction << NGARDS) | IMPLICIT_1;
  519. }
  520. }
  521. #endif /* L_unpack_df || L_unpack_sf */
  522. #if defined(L_addsub_sf) || defined(L_addsub_df) || defined(L_addsub_tf)
  523. static const fp_number_type *
  524. _fpadd_parts (fp_number_type * a,
  525. fp_number_type * b,
  526. fp_number_type * tmp)
  527. {
  528. intfrac tfraction;
  529. /* Put commonly used fields in local variables. */
  530. int a_normal_exp;
  531. int b_normal_exp;
  532. fractype a_fraction;
  533. fractype b_fraction;
  534. if (isnan (a))
  535. {
  536. return a;
  537. }
  538. if (isnan (b))
  539. {
  540. return b;
  541. }
  542. if (isinf (a))
  543. {
  544. /* Adding infinities with opposite signs yields a NaN. */
  545. if (isinf (b) && a->sign != b->sign)
  546. return makenan ();
  547. return a;
  548. }
  549. if (isinf (b))
  550. {
  551. return b;
  552. }
  553. if (iszero (b))
  554. {
  555. if (iszero (a))
  556. {
  557. *tmp = *a;
  558. tmp->sign = a->sign & b->sign;
  559. return tmp;
  560. }
  561. return a;
  562. }
  563. if (iszero (a))
  564. {
  565. return b;
  566. }
  567. /* Got two numbers. shift the smaller and increment the exponent till
  568. they're the same */
  569. {
  570. int diff;
  571. int sdiff;
  572. a_normal_exp = a->normal_exp;
  573. b_normal_exp = b->normal_exp;
  574. a_fraction = a->fraction.ll;
  575. b_fraction = b->fraction.ll;
  576. diff = a_normal_exp - b_normal_exp;
  577. sdiff = diff;
  578. if (diff < 0)
  579. diff = -diff;
  580. if (diff < FRAC_NBITS)
  581. {
  582. if (sdiff > 0)
  583. {
  584. b_normal_exp += diff;
  585. LSHIFT (b_fraction, diff);
  586. }
  587. else if (sdiff < 0)
  588. {
  589. a_normal_exp += diff;
  590. LSHIFT (a_fraction, diff);
  591. }
  592. }
  593. else
  594. {
  595. /* Somethings's up.. choose the biggest */
  596. if (a_normal_exp > b_normal_exp)
  597. {
  598. b_normal_exp = a_normal_exp;
  599. b_fraction = 0;
  600. }
  601. else
  602. {
  603. a_normal_exp = b_normal_exp;
  604. a_fraction = 0;
  605. }
  606. }
  607. }
  608. if (a->sign != b->sign)
  609. {
  610. if (a->sign)
  611. {
  612. tfraction = -a_fraction + b_fraction;
  613. }
  614. else
  615. {
  616. tfraction = a_fraction - b_fraction;
  617. }
  618. if (tfraction >= 0)
  619. {
  620. tmp->sign = 0;
  621. tmp->normal_exp = a_normal_exp;
  622. tmp->fraction.ll = tfraction;
  623. }
  624. else
  625. {
  626. tmp->sign = 1;
  627. tmp->normal_exp = a_normal_exp;
  628. tmp->fraction.ll = -tfraction;
  629. }
  630. /* and renormalize it */
  631. while (tmp->fraction.ll < IMPLICIT_1 && tmp->fraction.ll)
  632. {
  633. tmp->fraction.ll <<= 1;
  634. tmp->normal_exp--;
  635. }
  636. }
  637. else
  638. {
  639. tmp->sign = a->sign;
  640. tmp->normal_exp = a_normal_exp;
  641. tmp->fraction.ll = a_fraction + b_fraction;
  642. }
  643. tmp->class = CLASS_NUMBER;
  644. /* Now the fraction is added, we have to shift down to renormalize the
  645. number */
  646. if (tmp->fraction.ll >= IMPLICIT_2)
  647. {
  648. LSHIFT (tmp->fraction.ll, 1);
  649. tmp->normal_exp++;
  650. }
  651. return tmp;
  652. }
  653. FLO_type
  654. add (FLO_type arg_a, FLO_type arg_b)
  655. {
  656. fp_number_type a;
  657. fp_number_type b;
  658. fp_number_type tmp;
  659. const fp_number_type *res;
  660. FLO_union_type au, bu;
  661. au.value = arg_a;
  662. bu.value = arg_b;
  663. unpack_d (&au, &a);
  664. unpack_d (&bu, &b);
  665. res = _fpadd_parts (&a, &b, &tmp);
  666. return pack_d (res);
  667. }
  668. FLO_type
  669. sub (FLO_type arg_a, FLO_type arg_b)
  670. {
  671. fp_number_type a;
  672. fp_number_type b;
  673. fp_number_type tmp;
  674. const fp_number_type *res;
  675. FLO_union_type au, bu;
  676. au.value = arg_a;
  677. bu.value = arg_b;
  678. unpack_d (&au, &a);
  679. unpack_d (&bu, &b);
  680. b.sign ^= 1;
  681. res = _fpadd_parts (&a, &b, &tmp);
  682. return pack_d (res);
  683. }
  684. #endif /* L_addsub_sf || L_addsub_df */
  685. #if defined(L_mul_sf) || defined(L_mul_df) || defined(L_mul_tf)
  686. static inline __attribute__ ((__always_inline__)) const fp_number_type *
  687. _fpmul_parts ( fp_number_type * a,
  688. fp_number_type * b,
  689. fp_number_type * tmp)
  690. {
  691. fractype low = 0;
  692. fractype high = 0;
  693. if (isnan (a))
  694. {
  695. a->sign = a->sign != b->sign;
  696. return a;
  697. }
  698. if (isnan (b))
  699. {
  700. b->sign = a->sign != b->sign;
  701. return b;
  702. }
  703. if (isinf (a))
  704. {
  705. if (iszero (b))
  706. return makenan ();
  707. a->sign = a->sign != b->sign;
  708. return a;
  709. }
  710. if (isinf (b))
  711. {
  712. if (iszero (a))
  713. {
  714. return makenan ();
  715. }
  716. b->sign = a->sign != b->sign;
  717. return b;
  718. }
  719. if (iszero (a))
  720. {
  721. a->sign = a->sign != b->sign;
  722. return a;
  723. }
  724. if (iszero (b))
  725. {
  726. b->sign = a->sign != b->sign;
  727. return b;
  728. }
  729. /* Calculate the mantissa by multiplying both numbers to get a
  730. twice-as-wide number. */
  731. {
  732. #if defined(NO_DI_MODE) || defined(TFLOAT)
  733. {
  734. fractype x = a->fraction.ll;
  735. fractype ylow = b->fraction.ll;
  736. fractype yhigh = 0;
  737. int bit;
  738. /* ??? This does multiplies one bit at a time. Optimize. */
  739. for (bit = 0; bit < FRAC_NBITS; bit++)
  740. {
  741. int carry;
  742. if (x & 1)
  743. {
  744. carry = (low += ylow) < ylow;
  745. high += yhigh + carry;
  746. }
  747. yhigh <<= 1;
  748. if (ylow & FRACHIGH)
  749. {
  750. yhigh |= 1;
  751. }
  752. ylow <<= 1;
  753. x >>= 1;
  754. }
  755. }
  756. #elif defined(FLOAT)
  757. /* Multiplying two USIs to get a UDI, we're safe. */
  758. {
  759. UDItype answer = (UDItype)a->fraction.ll * (UDItype)b->fraction.ll;
  760. high = answer >> BITS_PER_SI;
  761. low = answer;
  762. }
  763. #else
  764. /* fractype is DImode, but we need the result to be twice as wide.
  765. Assuming a widening multiply from DImode to TImode is not
  766. available, build one by hand. */
  767. {
  768. USItype nl = a->fraction.ll;
  769. USItype nh = a->fraction.ll >> BITS_PER_SI;
  770. USItype ml = b->fraction.ll;
  771. USItype mh = b->fraction.ll >> BITS_PER_SI;
  772. UDItype pp_ll = (UDItype) ml * nl;
  773. UDItype pp_hl = (UDItype) mh * nl;
  774. UDItype pp_lh = (UDItype) ml * nh;
  775. UDItype pp_hh = (UDItype) mh * nh;
  776. UDItype res2 = 0;
  777. UDItype res0 = 0;
  778. UDItype ps_hh__ = pp_hl + pp_lh;
  779. if (ps_hh__ < pp_hl)
  780. res2 += (UDItype)1 << BITS_PER_SI;
  781. pp_hl = (UDItype)(USItype)ps_hh__ << BITS_PER_SI;
  782. res0 = pp_ll + pp_hl;
  783. if (res0 < pp_ll)
  784. res2++;
  785. res2 += (ps_hh__ >> BITS_PER_SI) + pp_hh;
  786. high = res2;
  787. low = res0;
  788. }
  789. #endif
  790. }
  791. tmp->normal_exp = a->normal_exp + b->normal_exp
  792. + FRAC_NBITS - (FRACBITS + NGARDS);
  793. tmp->sign = a->sign != b->sign;
  794. while (high >= IMPLICIT_2)
  795. {
  796. tmp->normal_exp++;
  797. if (high & 1)
  798. {
  799. low >>= 1;
  800. low |= FRACHIGH;
  801. }
  802. high >>= 1;
  803. }
  804. while (high < IMPLICIT_1)
  805. {
  806. tmp->normal_exp--;
  807. high <<= 1;
  808. if (low & FRACHIGH)
  809. high |= 1;
  810. low <<= 1;
  811. }
  812. if ((high & GARDMASK) == GARDMSB)
  813. {
  814. if (high & (1 << NGARDS))
  815. {
  816. /* Because we're half way, we would round to even by adding
  817. GARDROUND + 1, except that's also done in the packing
  818. function, and rounding twice will lose precision and cause
  819. the result to be too far off. Example: 32-bit floats with
  820. bit patterns 0xfff * 0x3f800400 ~= 0xfff (less than 0.5ulp
  821. off), not 0x1000 (more than 0.5ulp off). */
  822. }
  823. else if (low)
  824. {
  825. /* We're a further than half way by a small amount corresponding
  826. to the bits set in "low". Knowing that, we round here and
  827. not in pack_d, because there we don't have "low" available
  828. anymore. */
  829. high += GARDROUND + 1;
  830. /* Avoid further rounding in pack_d. */
  831. high &= ~(fractype) GARDMASK;
  832. }
  833. }
  834. tmp->fraction.ll = high;
  835. tmp->class = CLASS_NUMBER;
  836. return tmp;
  837. }
  838. FLO_type
  839. multiply (FLO_type arg_a, FLO_type arg_b)
  840. {
  841. fp_number_type a;
  842. fp_number_type b;
  843. fp_number_type tmp;
  844. const fp_number_type *res;
  845. FLO_union_type au, bu;
  846. au.value = arg_a;
  847. bu.value = arg_b;
  848. unpack_d (&au, &a);
  849. unpack_d (&bu, &b);
  850. res = _fpmul_parts (&a, &b, &tmp);
  851. return pack_d (res);
  852. }
  853. #endif /* L_mul_sf || L_mul_df || L_mul_tf */
  854. #if defined(L_div_sf) || defined(L_div_df) || defined(L_div_tf)
  855. static inline __attribute__ ((__always_inline__)) const fp_number_type *
  856. _fpdiv_parts (fp_number_type * a,
  857. fp_number_type * b)
  858. {
  859. fractype bit;
  860. fractype numerator;
  861. fractype denominator;
  862. fractype quotient;
  863. if (isnan (a))
  864. {
  865. return a;
  866. }
  867. if (isnan (b))
  868. {
  869. return b;
  870. }
  871. a->sign = a->sign ^ b->sign;
  872. if (isinf (a) || iszero (a))
  873. {
  874. if (a->class == b->class)
  875. return makenan ();
  876. return a;
  877. }
  878. if (isinf (b))
  879. {
  880. a->fraction.ll = 0;
  881. a->normal_exp = 0;
  882. return a;
  883. }
  884. if (iszero (b))
  885. {
  886. a->class = CLASS_INFINITY;
  887. return a;
  888. }
  889. /* Calculate the mantissa by multiplying both 64bit numbers to get a
  890. 128 bit number */
  891. {
  892. /* quotient =
  893. ( numerator / denominator) * 2^(numerator exponent - denominator exponent)
  894. */
  895. a->normal_exp = a->normal_exp - b->normal_exp;
  896. numerator = a->fraction.ll;
  897. denominator = b->fraction.ll;
  898. if (numerator < denominator)
  899. {
  900. /* Fraction will be less than 1.0 */
  901. numerator *= 2;
  902. a->normal_exp--;
  903. }
  904. bit = IMPLICIT_1;
  905. quotient = 0;
  906. /* ??? Does divide one bit at a time. Optimize. */
  907. while (bit)
  908. {
  909. if (numerator >= denominator)
  910. {
  911. quotient |= bit;
  912. numerator -= denominator;
  913. }
  914. bit >>= 1;
  915. numerator *= 2;
  916. }
  917. if ((quotient & GARDMASK) == GARDMSB)
  918. {
  919. if (quotient & (1 << NGARDS))
  920. {
  921. /* Because we're half way, we would round to even by adding
  922. GARDROUND + 1, except that's also done in the packing
  923. function, and rounding twice will lose precision and cause
  924. the result to be too far off. */
  925. }
  926. else if (numerator)
  927. {
  928. /* We're a further than half way by the small amount
  929. corresponding to the bits set in "numerator". Knowing
  930. that, we round here and not in pack_d, because there we
  931. don't have "numerator" available anymore. */
  932. quotient += GARDROUND + 1;
  933. /* Avoid further rounding in pack_d. */
  934. quotient &= ~(fractype) GARDMASK;
  935. }
  936. }
  937. a->fraction.ll = quotient;
  938. return (a);
  939. }
  940. }
  941. FLO_type
  942. divide (FLO_type arg_a, FLO_type arg_b)
  943. {
  944. fp_number_type a;
  945. fp_number_type b;
  946. const fp_number_type *res;
  947. FLO_union_type au, bu;
  948. au.value = arg_a;
  949. bu.value = arg_b;
  950. unpack_d (&au, &a);
  951. unpack_d (&bu, &b);
  952. res = _fpdiv_parts (&a, &b);
  953. return pack_d (res);
  954. }
  955. #endif /* L_div_sf || L_div_df */
  956. #if defined(L_fpcmp_parts_sf) || defined(L_fpcmp_parts_df) \
  957. || defined(L_fpcmp_parts_tf)
  958. /* according to the demo, fpcmp returns a comparison with 0... thus
  959. a<b -> -1
  960. a==b -> 0
  961. a>b -> +1
  962. */
  963. int
  964. __fpcmp_parts (fp_number_type * a, fp_number_type * b)
  965. {
  966. #if 0
  967. /* either nan -> unordered. Must be checked outside of this routine. */
  968. if (isnan (a) && isnan (b))
  969. {
  970. return 1; /* still unordered! */
  971. }
  972. #endif
  973. if (isnan (a) || isnan (b))
  974. {
  975. return 1; /* how to indicate unordered compare? */
  976. }
  977. if (isinf (a) && isinf (b))
  978. {
  979. /* +inf > -inf, but +inf != +inf */
  980. /* b \a| +inf(0)| -inf(1)
  981. ______\+--------+--------
  982. +inf(0)| a==b(0)| a<b(-1)
  983. -------+--------+--------
  984. -inf(1)| a>b(1) | a==b(0)
  985. -------+--------+--------
  986. So since unordered must be nonzero, just line up the columns...
  987. */
  988. return b->sign - a->sign;
  989. }
  990. /* but not both... */
  991. if (isinf (a))
  992. {
  993. return a->sign ? -1 : 1;
  994. }
  995. if (isinf (b))
  996. {
  997. return b->sign ? 1 : -1;
  998. }
  999. if (iszero (a) && iszero (b))
  1000. {
  1001. return 0;
  1002. }
  1003. if (iszero (a))
  1004. {
  1005. return b->sign ? 1 : -1;
  1006. }
  1007. if (iszero (b))
  1008. {
  1009. return a->sign ? -1 : 1;
  1010. }
  1011. /* now both are "normal". */
  1012. if (a->sign != b->sign)
  1013. {
  1014. /* opposite signs */
  1015. return a->sign ? -1 : 1;
  1016. }
  1017. /* same sign; exponents? */
  1018. if (a->normal_exp > b->normal_exp)
  1019. {
  1020. return a->sign ? -1 : 1;
  1021. }
  1022. if (a->normal_exp < b->normal_exp)
  1023. {
  1024. return a->sign ? 1 : -1;
  1025. }
  1026. /* same exponents; check size. */
  1027. if (a->fraction.ll > b->fraction.ll)
  1028. {
  1029. return a->sign ? -1 : 1;
  1030. }
  1031. if (a->fraction.ll < b->fraction.ll)
  1032. {
  1033. return a->sign ? 1 : -1;
  1034. }
  1035. /* after all that, they're equal. */
  1036. return 0;
  1037. }
  1038. #endif
  1039. #if defined(L_compare_sf) || defined(L_compare_df) || defined(L_compoare_tf)
  1040. CMPtype
  1041. compare (FLO_type arg_a, FLO_type arg_b)
  1042. {
  1043. fp_number_type a;
  1044. fp_number_type b;
  1045. FLO_union_type au, bu;
  1046. au.value = arg_a;
  1047. bu.value = arg_b;
  1048. unpack_d (&au, &a);
  1049. unpack_d (&bu, &b);
  1050. return __fpcmp_parts (&a, &b);
  1051. }
  1052. #endif /* L_compare_sf || L_compare_df */
  1053. /* These should be optimized for their specific tasks someday. */
  1054. #if defined(L_eq_sf) || defined(L_eq_df) || defined(L_eq_tf)
  1055. CMPtype
  1056. _eq_f2 (FLO_type arg_a, FLO_type arg_b)
  1057. {
  1058. fp_number_type a;
  1059. fp_number_type b;
  1060. FLO_union_type au, bu;
  1061. au.value = arg_a;
  1062. bu.value = arg_b;
  1063. unpack_d (&au, &a);
  1064. unpack_d (&bu, &b);
  1065. if (isnan (&a) || isnan (&b))
  1066. return 1; /* false, truth == 0 */
  1067. return __fpcmp_parts (&a, &b) ;
  1068. }
  1069. #endif /* L_eq_sf || L_eq_df */
  1070. #if defined(L_ne_sf) || defined(L_ne_df) || defined(L_ne_tf)
  1071. CMPtype
  1072. _ne_f2 (FLO_type arg_a, FLO_type arg_b)
  1073. {
  1074. fp_number_type a;
  1075. fp_number_type b;
  1076. FLO_union_type au, bu;
  1077. au.value = arg_a;
  1078. bu.value = arg_b;
  1079. unpack_d (&au, &a);
  1080. unpack_d (&bu, &b);
  1081. if (isnan (&a) || isnan (&b))
  1082. return 1; /* true, truth != 0 */
  1083. return __fpcmp_parts (&a, &b) ;
  1084. }
  1085. #endif /* L_ne_sf || L_ne_df */
  1086. #if defined(L_gt_sf) || defined(L_gt_df) || defined(L_gt_tf)
  1087. CMPtype
  1088. _gt_f2 (FLO_type arg_a, FLO_type arg_b)
  1089. {
  1090. fp_number_type a;
  1091. fp_number_type b;
  1092. FLO_union_type au, bu;
  1093. au.value = arg_a;
  1094. bu.value = arg_b;
  1095. unpack_d (&au, &a);
  1096. unpack_d (&bu, &b);
  1097. if (isnan (&a) || isnan (&b))
  1098. return -1; /* false, truth > 0 */
  1099. return __fpcmp_parts (&a, &b);
  1100. }
  1101. #endif /* L_gt_sf || L_gt_df */
  1102. #if defined(L_ge_sf) || defined(L_ge_df) || defined(L_ge_tf)
  1103. CMPtype
  1104. _ge_f2 (FLO_type arg_a, FLO_type arg_b)
  1105. {
  1106. fp_number_type a;
  1107. fp_number_type b;
  1108. FLO_union_type au, bu;
  1109. au.value = arg_a;
  1110. bu.value = arg_b;
  1111. unpack_d (&au, &a);
  1112. unpack_d (&bu, &b);
  1113. if (isnan (&a) || isnan (&b))
  1114. return -1; /* false, truth >= 0 */
  1115. return __fpcmp_parts (&a, &b) ;
  1116. }
  1117. #endif /* L_ge_sf || L_ge_df */
  1118. #if defined(L_lt_sf) || defined(L_lt_df) || defined(L_lt_tf)
  1119. CMPtype
  1120. _lt_f2 (FLO_type arg_a, FLO_type arg_b)
  1121. {
  1122. fp_number_type a;
  1123. fp_number_type b;
  1124. FLO_union_type au, bu;
  1125. au.value = arg_a;
  1126. bu.value = arg_b;
  1127. unpack_d (&au, &a);
  1128. unpack_d (&bu, &b);
  1129. if (isnan (&a) || isnan (&b))
  1130. return 1; /* false, truth < 0 */
  1131. return __fpcmp_parts (&a, &b);
  1132. }
  1133. #endif /* L_lt_sf || L_lt_df */
  1134. #if defined(L_le_sf) || defined(L_le_df) || defined(L_le_tf)
  1135. CMPtype
  1136. _le_f2 (FLO_type arg_a, FLO_type arg_b)
  1137. {
  1138. fp_number_type a;
  1139. fp_number_type b;
  1140. FLO_union_type au, bu;
  1141. au.value = arg_a;
  1142. bu.value = arg_b;
  1143. unpack_d (&au, &a);
  1144. unpack_d (&bu, &b);
  1145. if (isnan (&a) || isnan (&b))
  1146. return 1; /* false, truth <= 0 */
  1147. return __fpcmp_parts (&a, &b) ;
  1148. }
  1149. #endif /* L_le_sf || L_le_df */
  1150. #if defined(L_unord_sf) || defined(L_unord_df) || defined(L_unord_tf)
  1151. CMPtype
  1152. _unord_f2 (FLO_type arg_a, FLO_type arg_b)
  1153. {
  1154. fp_number_type a;
  1155. fp_number_type b;
  1156. FLO_union_type au, bu;
  1157. au.value = arg_a;
  1158. bu.value = arg_b;
  1159. unpack_d (&au, &a);
  1160. unpack_d (&bu, &b);
  1161. return (isnan (&a) || isnan (&b));
  1162. }
  1163. #endif /* L_unord_sf || L_unord_df */
  1164. #if defined(L_si_to_sf) || defined(L_si_to_df) || defined(L_si_to_tf)
  1165. FLO_type
  1166. si_to_float (SItype arg_a)
  1167. {
  1168. fp_number_type in;
  1169. in.class = CLASS_NUMBER;
  1170. in.sign = arg_a < 0;
  1171. if (!arg_a)
  1172. {
  1173. in.class = CLASS_ZERO;
  1174. }
  1175. else
  1176. {
  1177. USItype uarg;
  1178. int shift;
  1179. in.normal_exp = FRACBITS + NGARDS;
  1180. if (in.sign)
  1181. {
  1182. /* Special case for minint, since there is no +ve integer
  1183. representation for it */
  1184. if (arg_a == (- MAX_SI_INT - 1))
  1185. {
  1186. return (FLO_type)(- MAX_SI_INT - 1);
  1187. }
  1188. uarg = (-arg_a);
  1189. }
  1190. else
  1191. uarg = arg_a;
  1192. in.fraction.ll = uarg;
  1193. shift = clzusi (uarg) - (BITS_PER_SI - 1 - FRACBITS - NGARDS);
  1194. if (shift > 0)
  1195. {
  1196. in.fraction.ll <<= shift;
  1197. in.normal_exp -= shift;
  1198. }
  1199. }
  1200. return pack_d (&in);
  1201. }
  1202. #endif /* L_si_to_sf || L_si_to_df */
  1203. #if defined(L_usi_to_sf) || defined(L_usi_to_df) || defined(L_usi_to_tf)
  1204. FLO_type
  1205. usi_to_float (USItype arg_a)
  1206. {
  1207. fp_number_type in;
  1208. in.sign = 0;
  1209. if (!arg_a)
  1210. {
  1211. in.class = CLASS_ZERO;
  1212. }
  1213. else
  1214. {
  1215. int shift;
  1216. in.class = CLASS_NUMBER;
  1217. in.normal_exp = FRACBITS + NGARDS;
  1218. in.fraction.ll = arg_a;
  1219. shift = clzusi (arg_a) - (BITS_PER_SI - 1 - FRACBITS - NGARDS);
  1220. if (shift < 0)
  1221. {
  1222. fractype guard = in.fraction.ll & (((fractype)1 << -shift) - 1);
  1223. in.fraction.ll >>= -shift;
  1224. in.fraction.ll |= (guard != 0);
  1225. in.normal_exp -= shift;
  1226. }
  1227. else if (shift > 0)
  1228. {
  1229. in.fraction.ll <<= shift;
  1230. in.normal_exp -= shift;
  1231. }
  1232. }
  1233. return pack_d (&in);
  1234. }
  1235. #endif
  1236. #if defined(L_sf_to_si) || defined(L_df_to_si) || defined(L_tf_to_si)
  1237. SItype
  1238. float_to_si (FLO_type arg_a)
  1239. {
  1240. fp_number_type a;
  1241. SItype tmp;
  1242. FLO_union_type au;
  1243. au.value = arg_a;
  1244. unpack_d (&au, &a);
  1245. if (iszero (&a))
  1246. return 0;
  1247. if (isnan (&a))
  1248. return 0;
  1249. /* get reasonable MAX_SI_INT... */
  1250. if (isinf (&a))
  1251. return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
  1252. /* it is a number, but a small one */
  1253. if (a.normal_exp < 0)
  1254. return 0;
  1255. if (a.normal_exp > BITS_PER_SI - 2)
  1256. return a.sign ? (-MAX_SI_INT)-1 : MAX_SI_INT;
  1257. tmp = a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
  1258. return a.sign ? (-tmp) : (tmp);
  1259. }
  1260. #endif /* L_sf_to_si || L_df_to_si */
  1261. #if defined(L_tf_to_usi)
  1262. USItype
  1263. float_to_usi (FLO_type arg_a)
  1264. {
  1265. fp_number_type a;
  1266. FLO_union_type au;
  1267. au.value = arg_a;
  1268. unpack_d (&au, &a);
  1269. if (iszero (&a))
  1270. return 0;
  1271. if (isnan (&a))
  1272. return 0;
  1273. /* it is a negative number */
  1274. if (a.sign)
  1275. return 0;
  1276. /* get reasonable MAX_USI_INT... */
  1277. if (isinf (&a))
  1278. return MAX_USI_INT;
  1279. /* it is a number, but a small one */
  1280. if (a.normal_exp < 0)
  1281. return 0;
  1282. if (a.normal_exp > BITS_PER_SI - 1)
  1283. return MAX_USI_INT;
  1284. else if (a.normal_exp > (FRACBITS + NGARDS))
  1285. return a.fraction.ll << (a.normal_exp - (FRACBITS + NGARDS));
  1286. else
  1287. return a.fraction.ll >> ((FRACBITS + NGARDS) - a.normal_exp);
  1288. }
  1289. #endif /* L_tf_to_usi */
  1290. #if defined(L_negate_sf) || defined(L_negate_df) || defined(L_negate_tf)
  1291. FLO_type
  1292. negate (FLO_type arg_a)
  1293. {
  1294. fp_number_type a;
  1295. FLO_union_type au;
  1296. au.value = arg_a;
  1297. unpack_d (&au, &a);
  1298. flip_sign (&a);
  1299. return pack_d (&a);
  1300. }
  1301. #endif /* L_negate_sf || L_negate_df */
  1302. #ifdef FLOAT
  1303. #if defined(L_make_sf)
  1304. SFtype
  1305. __make_fp(fp_class_type class,
  1306. unsigned int sign,
  1307. int exp,
  1308. USItype frac)
  1309. {
  1310. fp_number_type in;
  1311. in.class = class;
  1312. in.sign = sign;
  1313. in.normal_exp = exp;
  1314. in.fraction.ll = frac;
  1315. return pack_d (&in);
  1316. }
  1317. #endif /* L_make_sf */
  1318. #ifndef FLOAT_ONLY
  1319. /* This enables one to build an fp library that supports float but not double.
  1320. Otherwise, we would get an undefined reference to __make_dp.
  1321. This is needed for some 8-bit ports that can't handle well values that
  1322. are 8-bytes in size, so we just don't support double for them at all. */
  1323. #if defined(L_sf_to_df)
  1324. DFtype
  1325. sf_to_df (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_dp (in.class, in.sign, in.normal_exp,
  1332. ((UDItype) in.fraction.ll) << F_D_BITOFF);
  1333. }
  1334. #endif /* L_sf_to_df */
  1335. #if defined(L_sf_to_tf) && defined(TMODES)
  1336. TFtype
  1337. sf_to_tf (SFtype arg_a)
  1338. {
  1339. fp_number_type in;
  1340. FLO_union_type au;
  1341. au.value = arg_a;
  1342. unpack_d (&au, &in);
  1343. return __make_tp (in.class, in.sign, in.normal_exp,
  1344. ((UTItype) in.fraction.ll) << F_T_BITOFF);
  1345. }
  1346. #endif /* L_sf_to_df */
  1347. #endif /* ! FLOAT_ONLY */
  1348. #endif /* FLOAT */
  1349. #ifndef FLOAT
  1350. extern SFtype __make_fp (fp_class_type, unsigned int, int, USItype);
  1351. #if defined(L_make_df)
  1352. DFtype
  1353. __make_dp (fp_class_type class, unsigned int sign, int exp, UDItype frac)
  1354. {
  1355. fp_number_type in;
  1356. in.class = class;
  1357. in.sign = sign;
  1358. in.normal_exp = exp;
  1359. in.fraction.ll = frac;
  1360. return pack_d (&in);
  1361. }
  1362. #endif /* L_make_df */
  1363. #if defined(L_df_to_sf)
  1364. SFtype
  1365. df_to_sf (DFtype arg_a)
  1366. {
  1367. fp_number_type in;
  1368. USItype sffrac;
  1369. FLO_union_type au;
  1370. au.value = arg_a;
  1371. unpack_d (&au, &in);
  1372. sffrac = in.fraction.ll >> F_D_BITOFF;
  1373. /* We set the lowest guard bit in SFFRAC if we discarded any non
  1374. zero bits. */
  1375. if ((in.fraction.ll & (((USItype) 1 << F_D_BITOFF) - 1)) != 0)
  1376. sffrac |= 1;
  1377. return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
  1378. }
  1379. #endif /* L_df_to_sf */
  1380. #if defined(L_df_to_tf) && defined(TMODES) \
  1381. && !defined(FLOAT) && !defined(TFLOAT)
  1382. TFtype
  1383. df_to_tf (DFtype arg_a)
  1384. {
  1385. fp_number_type in;
  1386. FLO_union_type au;
  1387. au.value = arg_a;
  1388. unpack_d (&au, &in);
  1389. return __make_tp (in.class, in.sign, in.normal_exp,
  1390. ((UTItype) in.fraction.ll) << D_T_BITOFF);
  1391. }
  1392. #endif /* L_sf_to_df */
  1393. #ifdef TFLOAT
  1394. #if defined(L_make_tf)
  1395. TFtype
  1396. __make_tp(fp_class_type class,
  1397. unsigned int sign,
  1398. int exp,
  1399. UTItype frac)
  1400. {
  1401. fp_number_type in;
  1402. in.class = class;
  1403. in.sign = sign;
  1404. in.normal_exp = exp;
  1405. in.fraction.ll = frac;
  1406. return pack_d (&in);
  1407. }
  1408. #endif /* L_make_tf */
  1409. #if defined(L_tf_to_df)
  1410. DFtype
  1411. tf_to_df (TFtype arg_a)
  1412. {
  1413. fp_number_type in;
  1414. UDItype sffrac;
  1415. FLO_union_type au;
  1416. au.value = arg_a;
  1417. unpack_d (&au, &in);
  1418. sffrac = in.fraction.ll >> D_T_BITOFF;
  1419. /* We set the lowest guard bit in SFFRAC if we discarded any non
  1420. zero bits. */
  1421. if ((in.fraction.ll & (((UTItype) 1 << D_T_BITOFF) - 1)) != 0)
  1422. sffrac |= 1;
  1423. return __make_dp (in.class, in.sign, in.normal_exp, sffrac);
  1424. }
  1425. #endif /* L_tf_to_df */
  1426. #if defined(L_tf_to_sf)
  1427. SFtype
  1428. tf_to_sf (TFtype arg_a)
  1429. {
  1430. fp_number_type in;
  1431. USItype sffrac;
  1432. FLO_union_type au;
  1433. au.value = arg_a;
  1434. unpack_d (&au, &in);
  1435. sffrac = in.fraction.ll >> F_T_BITOFF;
  1436. /* We set the lowest guard bit in SFFRAC if we discarded any non
  1437. zero bits. */
  1438. if ((in.fraction.ll & (((UTItype) 1 << F_T_BITOFF) - 1)) != 0)
  1439. sffrac |= 1;
  1440. return __make_fp (in.class, in.sign, in.normal_exp, sffrac);
  1441. }
  1442. #endif /* L_tf_to_sf */
  1443. #endif /* TFLOAT */
  1444. #endif /* ! FLOAT */
  1445. #endif /* !EXTENDED_FLOAT_STUBS */