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