1 /* real.c - implementation of REAL_ARITHMETIC, REAL_VALUE_ATOF,
2 and support for XFmode IEEE extended real floating point arithmetic.
3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2002 Free Software Foundation, Inc.
5 Contributed by Stephen L. Moshier (moshier@world.std.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
31 /* To enable support of XFmode extended real floating point, define
32 LONG_DOUBLE_TYPE_SIZE 96 in the tm.h file (m68k.h or i386.h).
34 Machine files (tm.h etc) must not contain any code
35 that tries to use host floating point arithmetic to convert
36 REAL_VALUE_TYPEs from `double' to `float', pass them to fprintf,
37 etc. In cross-compile situations a REAL_VALUE_TYPE may not
38 be intelligible to the host computer's native arithmetic.
40 The first part of this file interfaces gcc to a floating point
41 arithmetic suite that was not written with gcc in mind. Avoid
42 changing the low-level arithmetic routines unless you have suitable
43 test programs available. A special version of the PARANOIA floating
44 point arithmetic tester, modified for this purpose, can be found on
45 usc.edu: /pub/C-numanal/ieeetest.zoo. Other tests, and libraries of
46 XFmode and TFmode transcendental functions, can be obtained by ftp from
47 netlib.att.com: netlib/cephes. */
49 /* Type of computer arithmetic.
50 Only one of DEC, IBM, IEEE, C4X, or UNK should get defined.
52 `IEEE', when REAL_WORDS_BIG_ENDIAN is non-zero, refers generically
53 to big-endian IEEE floating-point data structure. This definition
54 should work in SFmode `float' type and DFmode `double' type on
55 virtually all big-endian IEEE machines. If LONG_DOUBLE_TYPE_SIZE
56 has been defined to be 96, then IEEE also invokes the particular
57 XFmode (`long double' type) data structure used by the Motorola
58 680x0 series processors.
60 `IEEE', when REAL_WORDS_BIG_ENDIAN is zero, refers generally to
61 little-endian IEEE machines. In this case, if LONG_DOUBLE_TYPE_SIZE
62 has been defined to be 96, then IEEE also invokes the particular
63 XFmode `long double' data structure used by the Intel 80x86 series
66 `DEC' refers specifically to the Digital Equipment Corp PDP-11
67 and VAX floating point data structure. This model currently
68 supports no type wider than DFmode.
70 `IBM' refers specifically to the IBM System/370 and compatible
71 floating point data structure. This model currently supports
72 no type wider than DFmode. The IBM conversions were contributed by
73 frank@atom.ansto.gov.au (Frank Crawford).
75 `C4X' refers specifically to the floating point format used on
76 Texas Instruments TMS320C3x and TMS320C4x digital signal
77 processors. This supports QFmode (32-bit float, double) and HFmode
78 (40-bit long double) where BITS_PER_BYTE is 32. Unlike IEEE
79 floats, C4x floats are not rounded to be even. The C4x conversions
80 were contributed by m.hayes@elec.canterbury.ac.nz (Michael Hayes) and
81 Haj.Ten.Brugge@net.HCC.nl (Herman ten Brugge).
83 If LONG_DOUBLE_TYPE_SIZE = 64 (the default, unless tm.h defines it)
84 then `long double' and `double' are both implemented, but they
87 The case LONG_DOUBLE_TYPE_SIZE = 128 activates TFmode support
88 and may deactivate XFmode since `long double' is used to refer
89 to both modes. Defining INTEL_EXTENDED_IEEE_FORMAT to non-zero
90 at the same time enables 80387-style 80-bit floats in a 128-bit
91 padded image, as seen on IA-64.
93 The macros FLOAT_WORDS_BIG_ENDIAN, HOST_FLOAT_WORDS_BIG_ENDIAN,
94 contributed by Richard Earnshaw <Richard.Earnshaw@cl.cam.ac.uk>,
95 separate the floating point unit's endian-ness from that of
96 the integer addressing. This permits one to define a big-endian
97 FPU on a little-endian machine (e.g., ARM). An extension to
98 BYTES_BIG_ENDIAN may be required for some machines in the future.
99 These optional macros may be defined in tm.h. In real.h, they
100 default to WORDS_BIG_ENDIAN, etc., so there is no need to define
101 them for any normal host or target machine on which the floats
102 and the integers have the same endian-ness. */
105 /* The following converts gcc macros into the ones used by this file. */
107 #if TARGET_FLOAT_FORMAT == VAX_FLOAT_FORMAT
108 /* PDP-11, Pro350, VAX: */
110 #else /* it's not VAX */
111 #if TARGET_FLOAT_FORMAT == IBM_FLOAT_FORMAT
112 /* IBM System/370 style */
114 #else /* it's also not an IBM */
115 #if TARGET_FLOAT_FORMAT == C4X_FLOAT_FORMAT
116 /* TMS320C3x/C4x style */
118 #else /* it's also not a C4X */
119 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
121 #else /* it's not IEEE either */
122 /* UNKnown arithmetic. We don't support this and can't go on. */
123 unknown arithmetic type
125 #endif /* not IEEE */
130 #define REAL_WORDS_BIG_ENDIAN FLOAT_WORDS_BIG_ENDIAN
132 /* Make sure that the endianness is correct for IBM and DEC. */
134 #undef LARGEST_EXPONENT_IS_NORMAL
135 #define LARGEST_EXPONENT_IS_NORMAL(x) 1
136 #undef REAL_WORDS_BIG_ENDIAN
137 /* Strangely enough, DEC float most closely resembles big endian IEEE */
138 #define REAL_WORDS_BIG_ENDIAN 1
139 /* ... but the halfwords are reversed from IEEE big endian. */
140 #ifndef VAX_HALFWORD_ORDER
141 #define VAX_HALFWORD_ORDER 1
145 #if !REAL_WORDS_BIG_ENDIAN
146 #error "Little-endian representations are not supported for IBM."
151 #if defined(DEC) && !defined (TARGET_G_FLOAT)
152 #define TARGET_G_FLOAT 0
155 #ifndef VAX_HALFWORD_ORDER
156 #define VAX_HALFWORD_ORDER 0
159 /* Define INFINITY for support of infinity.
160 Define NANS for support of Not-a-Number's (NaN's). */
161 #if !defined(DEC) && !defined(IBM) && !defined(C4X)
166 /* Support of NaNs requires support of infinity. */
173 /* Find a host integer type that is at least 16 bits wide,
174 and another type at least twice whatever that size is. */
176 #if HOST_BITS_PER_CHAR >= 16
177 #define EMUSHORT char
178 #define EMUSHORT_SIZE HOST_BITS_PER_CHAR
179 #define EMULONG_SIZE (2 * HOST_BITS_PER_CHAR)
181 #if HOST_BITS_PER_SHORT >= 16
182 #define EMUSHORT short
183 #define EMUSHORT_SIZE HOST_BITS_PER_SHORT
184 #define EMULONG_SIZE (2 * HOST_BITS_PER_SHORT)
186 #if HOST_BITS_PER_INT >= 16
188 #define EMUSHORT_SIZE HOST_BITS_PER_INT
189 #define EMULONG_SIZE (2 * HOST_BITS_PER_INT)
191 #if HOST_BITS_PER_LONG >= 16
192 #define EMUSHORT long
193 #define EMUSHORT_SIZE HOST_BITS_PER_LONG
194 #define EMULONG_SIZE (2 * HOST_BITS_PER_LONG)
196 #error "You will have to modify this program to have a smaller unit size."
202 /* If no 16-bit type has been found and the compiler is GCC, try HImode. */
203 #if defined(__GNUC__) && EMUSHORT_SIZE != 16
204 typedef int HItype __attribute__ ((mode (HI)));
205 typedef unsigned int UHItype __attribute__ ((mode (HI)));
209 #define EMUSHORT HItype
210 #define UEMUSHORT UHItype
211 #define EMUSHORT_SIZE 16
212 #define EMULONG_SIZE 32
214 #define UEMUSHORT unsigned EMUSHORT
217 #if HOST_BITS_PER_SHORT >= EMULONG_SIZE
218 #define EMULONG short
220 #if HOST_BITS_PER_INT >= EMULONG_SIZE
223 #if HOST_BITS_PER_LONG >= EMULONG_SIZE
226 #if HOST_BITS_PER_LONGLONG >= EMULONG_SIZE
227 #define EMULONG long long int
229 #error "You will have to modify this program to have a smaller unit size."
235 #if EMUSHORT_SIZE != 16
236 #error "The host interface doesn't work if no 16-bit size exists."
239 /* Calculate the size of the generic "e" type. This always has
240 identical in-memory size to REAL_VALUE_TYPE. The sizes are supposed
241 to be the same as well, but when REAL_VALUE_TYPE_SIZE is not evenly
242 divisible by HOST_BITS_PER_WIDE_INT we have some padding in
244 There are only two supported sizes: ten and six 16-bit words (160
247 #if MAX_LONG_DOUBLE_TYPE_SIZE == 128 && !INTEL_EXTENDED_IEEE_FORMAT
250 # define MAXDECEXP 4932
251 # define MINDECEXP -4977
254 # define MAXDECEXP 4932
255 # define MINDECEXP -4956
258 /* Fail compilation if 2*NE is not the appropriate size.
259 If HOST_BITS_PER_WIDE_INT is 64, we're going to have padding
260 at the end of the array, because neither 96 nor 160 is
261 evenly divisible by 64. */
262 struct compile_test_dummy {
263 char twice_NE_must_equal_sizeof_REAL_VALUE_TYPE
264 [(sizeof (REAL_VALUE_TYPE) >= 2*NE) ? 1 : -1];
267 /* Construct macros to translate between REAL_VALUE_TYPE and e type.
268 In GET_REAL and PUT_REAL, r and e are pointers.
269 A REAL_VALUE_TYPE is guaranteed to occupy contiguous locations
270 in memory, with no holes. */
271 #define GET_REAL(r, e) memcpy ((e), (r), 2*NE)
272 #define PUT_REAL(e, r) \
274 memcpy (r, e, 2*NE); \
275 if (2*NE < sizeof (*r)) \
276 memset ((char *) (r) + 2*NE, 0, sizeof (*r) - 2*NE); \
279 /* Number of 16 bit words in internal format */
282 /* Array offset to exponent */
285 /* Array offset to high guard word */
288 /* Number of bits of precision */
289 #define NBITS ((NI-4)*16)
291 /* Maximum number of decimal digits in ASCII conversion
294 #define NDEC (NBITS*8/27)
296 /* The exponent of 1.0 */
297 #define EXONE (0x3fff)
299 #if defined(HOST_EBCDIC)
300 /* bit 8 is significant in EBCDIC */
301 #define CHARMASK 0xff
303 #define CHARMASK 0x7f
306 /* Information about the various IEEE precisions. At the moment, we only
307 support exponents of 15 bits or less. */
313 /* Size of the exponent in bits. */
316 /* Overall size of the value in bits. */
319 /* Mode used for representing the value. */
320 enum machine_mode mode;
322 /* Exponent adjustment for offsets. */
327 /* IEEE float (24 bits). */
328 static const struct ieee_format ieee_24 =
337 /* IEEE double (53 bits). */
338 static const struct ieee_format ieee_53 =
349 /* IEEE extended double (64 bits). */
350 static const struct ieee_format ieee_64 =
359 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
360 /* IEEE long double (113 bits). */
361 static const struct ieee_format ieee_113 =
369 #endif /* INTEL_EXTENDED_IEEE_FORMAT == 0 */
372 /* DEC F float (24 bits). */
373 static const struct ieee_format dec_f =
382 /* DEC D float (56 bits). */
383 static const struct ieee_format dec_d =
392 /* DEC G float (53 bits). */
393 static const struct ieee_format dec_g =
403 /* DEC H float (113 bits). (not yet used) */
404 static const struct ieee_format dec_h =
415 extern int extra_warnings;
416 extern const UEMUSHORT ezero[NE], ehalf[NE], eone[NE], etwo[NE];
417 extern const UEMUSHORT elog2[NE], esqrt2[NE];
419 static void endian PARAMS ((const UEMUSHORT *, long *,
421 static void eclear PARAMS ((UEMUSHORT *));
422 static void emov PARAMS ((const UEMUSHORT *, UEMUSHORT *));
424 static void eabs PARAMS ((UEMUSHORT *));
426 static void eneg PARAMS ((UEMUSHORT *));
427 static int eisneg PARAMS ((const UEMUSHORT *));
428 static int eisinf PARAMS ((const UEMUSHORT *));
429 static int eisnan PARAMS ((const UEMUSHORT *));
430 static void einfin PARAMS ((UEMUSHORT *));
432 static void enan PARAMS ((UEMUSHORT *, int));
433 static void einan PARAMS ((UEMUSHORT *));
434 static int eiisnan PARAMS ((const UEMUSHORT *));
435 static void make_nan PARAMS ((UEMUSHORT *, int, enum machine_mode));
437 static int eiisneg PARAMS ((const UEMUSHORT *));
438 static void saturate PARAMS ((UEMUSHORT *, int, int, int));
439 static void emovi PARAMS ((const UEMUSHORT *, UEMUSHORT *));
440 static void emovo PARAMS ((const UEMUSHORT *, UEMUSHORT *));
441 static void ecleaz PARAMS ((UEMUSHORT *));
442 static void ecleazs PARAMS ((UEMUSHORT *));
443 static void emovz PARAMS ((const UEMUSHORT *, UEMUSHORT *));
445 static void eiinfin PARAMS ((UEMUSHORT *));
448 static int eiisinf PARAMS ((const UEMUSHORT *));
450 static int ecmpm PARAMS ((const UEMUSHORT *, const UEMUSHORT *));
451 static void eshdn1 PARAMS ((UEMUSHORT *));
452 static void eshup1 PARAMS ((UEMUSHORT *));
453 static void eshdn8 PARAMS ((UEMUSHORT *));
454 static void eshup8 PARAMS ((UEMUSHORT *));
455 static void eshup6 PARAMS ((UEMUSHORT *));
456 static void eshdn6 PARAMS ((UEMUSHORT *));
457 static void eaddm PARAMS ((const UEMUSHORT *, UEMUSHORT *));
\f
458 static void esubm PARAMS ((const UEMUSHORT *, UEMUSHORT *));
459 static void m16m PARAMS ((unsigned int, const UEMUSHORT *, UEMUSHORT *));
460 static int edivm PARAMS ((const UEMUSHORT *, UEMUSHORT *));
461 static int emulm PARAMS ((const UEMUSHORT *, UEMUSHORT *));
462 static void emdnorm PARAMS ((UEMUSHORT *, int, int, EMULONG, int));
463 static void esub PARAMS ((const UEMUSHORT *, const UEMUSHORT *,
465 static void eadd PARAMS ((const UEMUSHORT *, const UEMUSHORT *,
467 static void eadd1 PARAMS ((const UEMUSHORT *, const UEMUSHORT *,
469 static void ediv PARAMS ((const UEMUSHORT *, const UEMUSHORT *,
471 static void emul PARAMS ((const UEMUSHORT *, const UEMUSHORT *,
473 static void e53toe PARAMS ((const UEMUSHORT *, UEMUSHORT *));
474 static void e64toe PARAMS ((const UEMUSHORT *, UEMUSHORT *));
475 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
476 static void e113toe PARAMS ((const UEMUSHORT *, UEMUSHORT *));
478 static void e24toe PARAMS ((const UEMUSHORT *, UEMUSHORT *));
479 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
480 static void etoe113 PARAMS ((const UEMUSHORT *, UEMUSHORT *));
481 static void toe113 PARAMS ((UEMUSHORT *, UEMUSHORT *));
483 static void etoe64 PARAMS ((const UEMUSHORT *, UEMUSHORT *));
484 static void toe64 PARAMS ((UEMUSHORT *, UEMUSHORT *));
485 static void etoe53 PARAMS ((const UEMUSHORT *, UEMUSHORT *));
486 static void toe53 PARAMS ((UEMUSHORT *, UEMUSHORT *));
487 static void etoe24 PARAMS ((const UEMUSHORT *, UEMUSHORT *));
488 static void toe24 PARAMS ((UEMUSHORT *, UEMUSHORT *));
489 static void ieeetoe PARAMS ((const UEMUSHORT *, UEMUSHORT *,
490 const struct ieee_format *));
491 static void etoieee PARAMS ((const UEMUSHORT *, UEMUSHORT *,
492 const struct ieee_format *));
493 static void toieee PARAMS ((UEMUSHORT *, UEMUSHORT *,
494 const struct ieee_format *));
495 static int ecmp PARAMS ((const UEMUSHORT *, const UEMUSHORT *));
497 static void eround PARAMS ((const UEMUSHORT *, UEMUSHORT *));
499 static void ltoe PARAMS ((const HOST_WIDE_INT *, UEMUSHORT *));
500 static void ultoe PARAMS ((const unsigned HOST_WIDE_INT *, UEMUSHORT *));
501 static void eifrac PARAMS ((const UEMUSHORT *, HOST_WIDE_INT *,
503 static void euifrac PARAMS ((const UEMUSHORT *, unsigned HOST_WIDE_INT *,
505 static int eshift PARAMS ((UEMUSHORT *, int));
506 static int enormlz PARAMS ((UEMUSHORT *));
508 static void e24toasc PARAMS ((const UEMUSHORT *, char *, int));
509 static void e53toasc PARAMS ((const UEMUSHORT *, char *, int));
510 static void e64toasc PARAMS ((const UEMUSHORT *, char *, int));
511 static void e113toasc PARAMS ((const UEMUSHORT *, char *, int));
513 static void etoasc PARAMS ((const UEMUSHORT *, char *, int));
514 static void asctoe24 PARAMS ((const char *, UEMUSHORT *));
515 static void asctoe53 PARAMS ((const char *, UEMUSHORT *));
516 static void asctoe64 PARAMS ((const char *, UEMUSHORT *));
517 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
518 static void asctoe113 PARAMS ((const char *, UEMUSHORT *));
520 static void asctoe PARAMS ((const char *, UEMUSHORT *));
521 static void asctoeg PARAMS ((const char *, UEMUSHORT *, int));
522 static void efloor PARAMS ((const UEMUSHORT *, UEMUSHORT *));
524 static void efrexp PARAMS ((const UEMUSHORT *, int *,
527 static void eldexp PARAMS ((const UEMUSHORT *, int, UEMUSHORT *));
529 static void eremain PARAMS ((const UEMUSHORT *, const UEMUSHORT *,
532 static void eiremain PARAMS ((UEMUSHORT *, UEMUSHORT *));
533 static void mtherr PARAMS ((const char *, int));
535 static void dectoe PARAMS ((const UEMUSHORT *, UEMUSHORT *));
536 static void etodec PARAMS ((const UEMUSHORT *, UEMUSHORT *));
537 static void todec PARAMS ((UEMUSHORT *, UEMUSHORT *));
540 static void ibmtoe PARAMS ((const UEMUSHORT *, UEMUSHORT *,
542 static void etoibm PARAMS ((const UEMUSHORT *, UEMUSHORT *,
544 static void toibm PARAMS ((UEMUSHORT *, UEMUSHORT *,
548 static void c4xtoe PARAMS ((const UEMUSHORT *, UEMUSHORT *,
550 static void etoc4x PARAMS ((const UEMUSHORT *, UEMUSHORT *,
552 static void toc4x PARAMS ((UEMUSHORT *, UEMUSHORT *,
556 static void uditoe PARAMS ((const UEMUSHORT *, UEMUSHORT *));
557 static void ditoe PARAMS ((const UEMUSHORT *, UEMUSHORT *));
558 static void etoudi PARAMS ((const UEMUSHORT *, UEMUSHORT *));
559 static void etodi PARAMS ((const UEMUSHORT *, UEMUSHORT *));
560 static void esqrt PARAMS ((const UEMUSHORT *, UEMUSHORT *));
563 /* Copy 32-bit numbers obtained from array containing 16-bit numbers,
564 swapping ends if required, into output array of longs. The
565 result is normally passed to fprintf by the ASM_OUTPUT_ macros. */
571 enum machine_mode mode;
575 if (REAL_WORDS_BIG_ENDIAN && !VAX_HALFWORD_ORDER)
580 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
581 /* Swap halfwords in the fourth long. */
582 th = (unsigned long) e[6] & 0xffff;
583 t = (unsigned long) e[7] & 0xffff;
592 /* Swap halfwords in the third long. */
593 th = (unsigned long) e[4] & 0xffff;
594 t = (unsigned long) e[5] & 0xffff;
600 /* Swap halfwords in the second word. */
601 th = (unsigned long) e[2] & 0xffff;
602 t = (unsigned long) e[3] & 0xffff;
609 /* Swap halfwords in the first word. */
610 th = (unsigned long) e[0] & 0xffff;
611 t = (unsigned long) e[1] & 0xffff;
622 /* Pack the output array without swapping. */
627 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
628 /* Pack the fourth long. */
629 th = (unsigned long) e[7] & 0xffff;
630 t = (unsigned long) e[6] & 0xffff;
639 /* Pack the third long.
640 Each element of the input REAL_VALUE_TYPE array has 16 useful bits
642 th = (unsigned long) e[5] & 0xffff;
643 t = (unsigned long) e[4] & 0xffff;
649 /* Pack the second long */
650 th = (unsigned long) e[3] & 0xffff;
651 t = (unsigned long) e[2] & 0xffff;
658 /* Pack the first long */
659 th = (unsigned long) e[1] & 0xffff;
660 t = (unsigned long) e[0] & 0xffff;
672 /* This is the implementation of the REAL_ARITHMETIC macro. */
675 earith (value, icode, r1, r2)
676 REAL_VALUE_TYPE *value;
681 UEMUSHORT d1[NE], d2[NE], v[NE];
687 /* Return NaN input back to the caller. */
690 PUT_REAL (d1, value);
695 PUT_REAL (d2, value);
699 code = (enum tree_code) icode;
707 esub (d2, d1, v); /* d1 - d2 */
716 if (ecmp (d2, ezero) == 0)
719 ediv (d2, d1, v); /* d1/d2 */
722 case MIN_EXPR: /* min (d1,d2) */
723 if (ecmp (d1, d2) < 0)
729 case MAX_EXPR: /* max (d1,d2) */
730 if (ecmp (d1, d2) > 0)
743 /* Truncate REAL_VALUE_TYPE toward zero to signed HOST_WIDE_INT.
744 implements REAL_VALUE_RNDZINT (x) (etrunci (x)). */
750 UEMUSHORT f[NE], g[NE];
766 /* Truncate REAL_VALUE_TYPE toward zero to unsigned HOST_WIDE_INT;
767 implements REAL_VALUE_UNSIGNED_RNDZINT (x) (etruncui (x)). */
773 UEMUSHORT f[NE], g[NE];
775 unsigned HOST_WIDE_INT l;
789 /* This is the REAL_VALUE_ATOF function. It converts a decimal or hexadecimal
790 string to binary, rounding off as indicated by the machine_mode argument.
791 Then it promotes the rounded value to REAL_VALUE_TYPE. */
798 UEMUSHORT tem[NE], e[NE];
824 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
844 /* Expansion of REAL_NEGATE. */
860 /* Round real toward zero to HOST_WIDE_INT;
861 implements REAL_VALUE_FIX (x). */
867 UEMUSHORT f[NE], g[NE];
874 warning ("conversion from NaN to int");
882 /* Round real toward zero to unsigned HOST_WIDE_INT
883 implements REAL_VALUE_UNSIGNED_FIX (x).
884 Negative input returns zero. */
886 unsigned HOST_WIDE_INT
890 UEMUSHORT f[NE], g[NE];
891 unsigned HOST_WIDE_INT l;
897 warning ("conversion from NaN to unsigned int");
906 /* REAL_VALUE_FROM_INT macro. */
909 ereal_from_int (d, i, j, mode)
912 enum machine_mode mode;
914 UEMUSHORT df[NE], dg[NE];
915 HOST_WIDE_INT low, high;
918 if (GET_MODE_CLASS (mode) != MODE_FLOAT)
925 /* complement and add 1 */
932 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
933 ultoe ((unsigned HOST_WIDE_INT *) &high, dg);
935 ultoe ((unsigned HOST_WIDE_INT *) &low, df);
940 /* A REAL_VALUE_TYPE may not be wide enough to hold the two HOST_WIDE_INTS.
941 Avoid double-rounding errors later by rounding off now from the
942 extra-wide internal format to the requested precision. */
943 switch (GET_MODE_BITSIZE (mode))
961 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
978 /* REAL_VALUE_FROM_UNSIGNED_INT macro. */
981 ereal_from_uint (d, i, j, mode)
983 unsigned HOST_WIDE_INT i, j;
984 enum machine_mode mode;
986 UEMUSHORT df[NE], dg[NE];
987 unsigned HOST_WIDE_INT low, high;
989 if (GET_MODE_CLASS (mode) != MODE_FLOAT)
993 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
999 /* A REAL_VALUE_TYPE may not be wide enough to hold the two HOST_WIDE_INTS.
1000 Avoid double-rounding errors later by rounding off now from the
1001 extra-wide internal format to the requested precision. */
1002 switch (GET_MODE_BITSIZE (mode))
1020 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
1037 /* REAL_VALUE_TO_INT macro. */
1040 ereal_to_int (low, high, rr)
1041 HOST_WIDE_INT *low, *high;
1044 UEMUSHORT d[NE], df[NE], dg[NE], dh[NE];
1051 warning ("conversion from NaN to int");
1057 /* convert positive value */
1064 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
1065 ediv (df, d, dg); /* dg = d / 2^32 is the high word */
1066 euifrac (dg, (unsigned HOST_WIDE_INT *) high, dh);
1067 emul (df, dh, dg); /* fractional part is the low word */
1068 euifrac (dg, (unsigned HOST_WIDE_INT *) low, dh);
1071 /* complement and add 1 */
1081 /* REAL_VALUE_LDEXP macro. */
1088 UEMUSHORT e[NE], y[NE];
1101 /* Check for infinity in a REAL_VALUE_TYPE. */
1105 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
1111 return (eisinf (e));
1117 /* Check whether a REAL_VALUE_TYPE item is a NaN. */
1121 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
1127 return (eisnan (e));
1134 /* Check for a negative REAL_VALUE_TYPE number.
1135 This just checks the sign bit, so that -0 counts as negative. */
1141 return ereal_isneg (x);
1144 /* Expansion of REAL_VALUE_TRUNCATE.
1145 The result is in floating point, rounded to nearest or even. */
1148 real_value_truncate (mode, arg)
1149 enum machine_mode mode;
1150 REAL_VALUE_TYPE arg;
1152 UEMUSHORT e[NE], t[NE];
1164 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
1201 /* If an unsupported type was requested, presume that
1202 the machine files know something useful to do with
1203 the unmodified value. */
1212 /* Return true if ARG can be represented exactly in MODE. */
1215 exact_real_truncate (mode, arg)
1216 enum machine_mode mode;
1217 REAL_VALUE_TYPE *arg;
1219 REAL_VALUE_TYPE trunc;
1221 if (target_isnan (*arg))
1224 trunc = real_value_truncate (mode, *arg);
1225 return ereal_cmp (*arg, trunc) == 0;
1228 /* Try to change R into its exact multiplicative inverse in machine mode
1229 MODE. Return nonzero function value if successful. */
1232 exact_real_inverse (mode, r)
1233 enum machine_mode mode;
1236 UEMUSHORT e[NE], einv[NE];
1237 REAL_VALUE_TYPE rinv;
1242 /* Test for input in range. Don't transform IEEE special values. */
1243 if (eisinf (e) || eisnan (e) || (ecmp (e, ezero) == 0))
1246 /* Test for a power of 2: all significand bits zero except the MSB.
1247 We are assuming the target has binary (or hex) arithmetic. */
1248 if (e[NE - 2] != 0x8000)
1251 for (i = 0; i < NE - 2; i++)
1257 /* Compute the inverse and truncate it to the required mode. */
1258 ediv (e, eone, einv);
1259 PUT_REAL (einv, &rinv);
1260 rinv = real_value_truncate (mode, rinv);
1262 #ifdef CHECK_FLOAT_VALUE
1263 /* This check is not redundant. It may, for example, flush
1264 a supposedly IEEE denormal value to zero. */
1266 if (CHECK_FLOAT_VALUE (mode, rinv, i))
1269 GET_REAL (&rinv, einv);
1271 /* Check the bits again, because the truncation might have
1272 generated an arbitrary saturation value on overflow. */
1273 if (einv[NE - 2] != 0x8000)
1276 for (i = 0; i < NE - 2; i++)
1282 /* Fail if the computed inverse is out of range. */
1283 if (eisinf (einv) || eisnan (einv) || (ecmp (einv, ezero) == 0))
1286 /* Output the reciprocal and return success flag. */
1291 /* Used for debugging--print the value of R in human-readable format
1300 REAL_VALUE_TO_DECIMAL (r, dstr, -1);
1301 fprintf (stderr, "%s", dstr);
1305 /* The following routines convert REAL_VALUE_TYPE to the various floating
1306 point formats that are meaningful to supported computers.
1308 The results are returned in 32-bit pieces, each piece stored in a `long'.
1309 This is so they can be printed by statements like
1311 fprintf (file, "%lx, %lx", L[0], L[1]);
1313 that will work on both narrow- and wide-word host computers. */
1315 /* Convert R to a 128-bit long double precision value. The output array L
1316 contains four 32-bit pieces of the result, in the order they would appear
1327 #if INTEL_EXTENDED_IEEE_FORMAT == 0
1332 endian (e, l, TFmode);
1335 /* Convert R to a double extended precision value. The output array L
1336 contains three 32-bit pieces of the result, in the order they would
1337 appear in memory. */
1348 endian (e, l, XFmode);
1351 /* Convert R to a double precision value. The output array L contains two
1352 32-bit pieces of the result, in the order they would appear in memory. */
1363 endian (e, l, DFmode);
1366 /* Convert R to a single precision float value stored in the least-significant
1367 bits of a `long'. */
1378 endian (e, &l, SFmode);
1382 /* Convert X to a decimal ASCII string S for output to an assembly
1383 language file. Note, there is no standard way to spell infinity or
1384 a NaN, so these values may require special treatment in the tm.h
1387 The argument DIGITS is the number of decimal digits to print,
1388 or -1 to indicate "enough", i.e. DECIMAL_DIG for for the target. */
1391 ereal_to_decimal (x, s, digits)
1399 /* Find DECIMAL_DIG for the target. */
1401 switch (TARGET_FLOAT_FORMAT)
1403 case IEEE_FLOAT_FORMAT:
1404 switch (LONG_DOUBLE_TYPE_SIZE)
1413 if (!INTEL_EXTENDED_IEEE_FORMAT)
1428 case VAX_FLOAT_FORMAT:
1429 digits = 18; /* D_FLOAT */
1432 case IBM_FLOAT_FORMAT:
1436 case C4X_FLOAT_FORMAT:
1444 /* etoasc interprets digits as places after the decimal point.
1445 We interpret digits as total decimal digits, which IMO is
1446 more useful. Since the output will have one digit before
1447 the point, subtract one. */
1448 etoasc (e, s, digits - 1);
1451 /* Compare X and Y. Return 1 if X > Y, 0 if X == Y, -1 if X < Y,
1452 or -2 if either is a NaN. */
1456 REAL_VALUE_TYPE x, y;
1458 UEMUSHORT ex[NE], ey[NE];
1462 return (ecmp (ex, ey));
1465 /* Return 1 if the sign bit of X is set, else return 0. */
1474 return (eisneg (ex));
1479 Extended precision IEEE binary floating point arithmetic routines
1481 Numbers are stored in C language as arrays of 16-bit unsigned
1482 short integers. The arguments of the routines are pointers to
1485 External e type data structure, similar to Intel 8087 chip
1486 temporary real format but possibly with a larger significand:
1488 NE-1 significand words (least significant word first,
1489 most significant bit is normally set)
1490 exponent (value = EXONE for 1.0,
1491 top bit is the sign)
1494 Internal exploded e-type data structure of a number (a "word" is 16 bits):
1496 ei[0] sign word (0 for positive, 0xffff for negative)
1497 ei[1] biased exponent (value = EXONE for the number 1.0)
1498 ei[2] high guard word (always zero after normalization)
1500 to ei[NI-2] significand (NI-4 significand words,
1501 most significant word first,
1502 most significant bit is set)
1503 ei[NI-1] low guard word (0x8000 bit is rounding place)
1507 Routines for external format e-type numbers
1509 asctoe (string, e) ASCII string to extended double e type
1510 asctoe64 (string, &d) ASCII string to long double
1511 asctoe53 (string, &d) ASCII string to double
1512 asctoe24 (string, &f) ASCII string to single
1513 asctoeg (string, e, prec) ASCII string to specified precision
1514 e24toe (&f, e) IEEE single precision to e type
1515 e53toe (&d, e) IEEE double precision to e type
1516 e64toe (&d, e) IEEE long double precision to e type
1517 e113toe (&d, e) 128-bit long double precision to e type
1519 eabs (e) absolute value
1521 eadd (a, b, c) c = b + a
1523 ecmp (a, b) Returns 1 if a > b, 0 if a == b,
1524 -1 if a < b, -2 if either a or b is a NaN.
1525 ediv (a, b, c) c = b / a
1526 efloor (a, b) truncate to integer, toward -infinity
1527 efrexp (a, exp, s) extract exponent and significand
1528 eifrac (e, &l, frac) e to HOST_WIDE_INT and e type fraction
1529 euifrac (e, &l, frac) e to unsigned HOST_WIDE_INT and e type fraction
1530 einfin (e) set e to infinity, leaving its sign alone
1531 eldexp (a, n, b) multiply by 2**n
1533 emul (a, b, c) c = b * a
1536 eround (a, b) b = nearest integer value to a
1538 esub (a, b, c) c = b - a
1540 e24toasc (&f, str, n) single to ASCII string, n digits after decimal
1541 e53toasc (&d, str, n) double to ASCII string, n digits after decimal
1542 e64toasc (&d, str, n) 80-bit long double to ASCII string
1543 e113toasc (&d, str, n) 128-bit long double to ASCII string
1545 etoasc (e, str, n) e to ASCII string, n digits after decimal
1546 etoe24 (e, &f) convert e type to IEEE single precision
1547 etoe53 (e, &d) convert e type to IEEE double precision
1548 etoe64 (e, &d) convert e type to IEEE long double precision
1549 ltoe (&l, e) HOST_WIDE_INT to e type
1550 ultoe (&l, e) unsigned HOST_WIDE_INT to e type
1551 eisneg (e) 1 if sign bit of e != 0, else 0
1552 eisinf (e) 1 if e has maximum exponent (non-IEEE)
1553 or is infinite (IEEE)
1554 eisnan (e) 1 if e is a NaN
1557 Routines for internal format exploded e-type numbers
1559 eaddm (ai, bi) add significands, bi = bi + ai
1561 ecleazs (ei) set ei = 0 but leave its sign alone
1562 ecmpm (ai, bi) compare significands, return 1, 0, or -1
1563 edivm (ai, bi) divide significands, bi = bi / ai
1564 emdnorm (ai,l,s,exp) normalize and round off
1565 emovi (a, ai) convert external a to internal ai
1566 emovo (ai, a) convert internal ai to external a
1567 emovz (ai, bi) bi = ai, low guard word of bi = 0
1568 emulm (ai, bi) multiply significands, bi = bi * ai
1569 enormlz (ei) left-justify the significand
1570 eshdn1 (ai) shift significand and guards down 1 bit
1571 eshdn8 (ai) shift down 8 bits
1572 eshdn6 (ai) shift down 16 bits
1573 eshift (ai, n) shift ai n bits up (or down if n < 0)
1574 eshup1 (ai) shift significand and guards up 1 bit
1575 eshup8 (ai) shift up 8 bits
1576 eshup6 (ai) shift up 16 bits
1577 esubm (ai, bi) subtract significands, bi = bi - ai
1578 eiisinf (ai) 1 if infinite
1579 eiisnan (ai) 1 if a NaN
1580 eiisneg (ai) 1 if sign bit of ai != 0, else 0
1581 einan (ai) set ai = NaN
1583 eiinfin (ai) set ai = infinity
1586 The result is always normalized and rounded to NI-4 word precision
1587 after each arithmetic operation.
1589 Exception flags are NOT fully supported.
1591 Signaling NaN's are NOT supported; they are treated the same
1594 Define INFINITY for support of infinity; otherwise a
1595 saturation arithmetic is implemented.
1597 Define NANS for support of Not-a-Number items; otherwise the
1598 arithmetic will never produce a NaN output, and might be confused
1600 If NaN's are supported, the output of `ecmp (a,b)' is -2 if
1601 either a or b is a NaN. This means asking `if (ecmp (a,b) < 0)'
1602 may not be legitimate. Use `if (ecmp (a,b) == -1)' for `less than'
1605 Denormals are always supported here where appropriate (e.g., not
1606 for conversion to DEC numbers). */
1608 /* Definitions for error codes that are passed to the common error handling
1611 For Digital Equipment PDP-11 and VAX computers, certain
1612 IBM systems, and others that use numbers with a 56-bit
1613 significand, the symbol DEC should be defined. In this
1614 mode, most floating point constants are given as arrays
1615 of octal integers to eliminate decimal to binary conversion
1616 errors that might be introduced by the compiler.
1618 For computers, such as IBM PC, that follow the IEEE
1619 Standard for Binary Floating Point Arithmetic (ANSI/IEEE
1620 Std 754-1985), the symbol IEEE should be defined.
1621 These numbers have 53-bit significands. In this mode, constants
1622 are provided as arrays of hexadecimal 16 bit integers.
1623 The endian-ness of generated values is controlled by
1624 REAL_WORDS_BIG_ENDIAN.
1626 To accommodate other types of computer arithmetic, all
1627 constants are also provided in a normal decimal radix
1628 which one can hope are correctly converted to a suitable
1629 format by the available C language compiler. To invoke
1630 this mode, the symbol UNK is defined.
1632 An important difference among these modes is a predefined
1633 set of machine arithmetic constants for each. The numbers
1634 MACHEP (the machine roundoff error), MAXNUM (largest number
1635 represented), and several other parameters are preset by
1636 the configuration symbol. Check the file const.c to
1637 ensure that these values are correct for your computer.
1639 For ANSI C compatibility, define ANSIC equal to 1. Currently
1640 this affects only the atan2 function and others that use it. */
1642 /* Constant definitions for math error conditions. */
1644 #define DOMAIN 1 /* argument domain error */
1645 #define SING 2 /* argument singularity */
1646 #define OVERFLOW 3 /* overflow range error */
1647 #define UNDERFLOW 4 /* underflow range error */
1648 #define TLOSS 5 /* total loss of precision */
1649 #define PLOSS 6 /* partial loss of precision */
1650 #define INVALID 7 /* NaN-producing operation */
1652 /* e type constants used by high precision check routines */
1654 #if MAX_LONG_DOUBLE_TYPE_SIZE == 128 && (INTEL_EXTENDED_IEEE_FORMAT == 0)
1656 const UEMUSHORT ezero[NE] =
1657 {0x0000, 0x0000, 0x0000, 0x0000,
1658 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,};
1661 const UEMUSHORT ehalf[NE] =
1662 {0x0000, 0x0000, 0x0000, 0x0000,
1663 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3ffe,};
1666 const UEMUSHORT eone[NE] =
1667 {0x0000, 0x0000, 0x0000, 0x0000,
1668 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3fff,};
1671 const UEMUSHORT etwo[NE] =
1672 {0x0000, 0x0000, 0x0000, 0x0000,
1673 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4000,};
1676 const UEMUSHORT e32[NE] =
1677 {0x0000, 0x0000, 0x0000, 0x0000,
1678 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4004,};
1680 /* 6.93147180559945309417232121458176568075500134360255E-1 */
1681 const UEMUSHORT elog2[NE] =
1682 {0x40f3, 0xf6af, 0x03f2, 0xb398,
1683 0xc9e3, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1685 /* 1.41421356237309504880168872420969807856967187537695E0 */
1686 const UEMUSHORT esqrt2[NE] =
1687 {0x1d6f, 0xbe9f, 0x754a, 0x89b3,
1688 0x597d, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1690 /* 3.14159265358979323846264338327950288419716939937511E0 */
1691 const UEMUSHORT epi[NE] =
1692 {0x2902, 0x1cd1, 0x80dc, 0x628b,
1693 0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1696 /* LONG_DOUBLE_TYPE_SIZE is other than 128 */
1697 const UEMUSHORT ezero[NE] =
1698 {0, 0000000, 0000000, 0000000, 0000000, 0000000,};
1699 const UEMUSHORT ehalf[NE] =
1700 {0, 0000000, 0000000, 0000000, 0100000, 0x3ffe,};
1701 const UEMUSHORT eone[NE] =
1702 {0, 0000000, 0000000, 0000000, 0100000, 0x3fff,};
1703 const UEMUSHORT etwo[NE] =
1704 {0, 0000000, 0000000, 0000000, 0100000, 0040000,};
1705 const UEMUSHORT e32[NE] =
1706 {0, 0000000, 0000000, 0000000, 0100000, 0040004,};
1707 const UEMUSHORT elog2[NE] =
1708 {0xc9e4, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1709 const UEMUSHORT esqrt2[NE] =
1710 {0x597e, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1711 const UEMUSHORT epi[NE] =
1712 {0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1715 /* Control register for rounding precision.
1716 This can be set to 113 (if NE=10), 80 (if NE=6), 64, 56, 53, or 24 bits. */
1721 /* Clear out entire e-type number X. */
1729 for (i = 0; i < NE; i++)
1733 /* Move e-type number from A to B. */
1742 for (i = 0; i < NE; i++)
1748 /* Absolute value of e-type X. */
1754 /* sign is top bit of last word of external format */
1755 x[NE - 1] &= 0x7fff;
1759 /* Negate the e-type number X. */
1766 x[NE - 1] ^= 0x8000; /* Toggle the sign bit */
1769 /* Return 1 if sign bit of e-type number X is nonzero, else zero. */
1773 const UEMUSHORT x[];
1776 if (x[NE - 1] & 0x8000)
1782 /* Return 1 if e-type number X is infinity, else return zero. */
1786 const UEMUSHORT x[];
1793 if ((x[NE - 1] & 0x7fff) == 0x7fff)
1799 /* Check if e-type number is not a number. The bit pattern is one that we
1800 defined, so we know for sure how to detect it. */
1804 const UEMUSHORT x[] ATTRIBUTE_UNUSED;
1809 /* NaN has maximum exponent */
1810 if ((x[NE - 1] & 0x7fff) != 0x7fff)
1812 /* ... and non-zero significand field. */
1813 for (i = 0; i < NE - 1; i++)
1823 /* Fill e-type number X with infinity pattern (IEEE)
1824 or largest possible number (non-IEEE). */
1833 for (i = 0; i < NE - 1; i++)
1837 for (i = 0; i < NE - 1; i++)
1865 /* Similar, except return it as a REAL_VALUE_TYPE. */
1869 enum machine_mode mode;
1886 if (!INTEL_EXTENDED_IEEE_FORMAT)
1908 /* Output an e-type NaN.
1909 This generates Intel's quiet NaN pattern for extended real.
1910 The exponent is 7fff, the leading mantissa word is c000. */
1920 for (i = 0; i < NE - 2; i++)
1923 *x = (sign << 15) | 0x7fff;
1927 /* Move in an e-type number A, converting it to exploded e-type B. */
1939 p = a + (NE - 1); /* point to last word of external number */
1940 /* get the sign bit */
1945 /* get the exponent */
1947 *q++ &= 0x7fff; /* delete the sign bit */
1949 if ((*(q - 1) & 0x7fff) == 0x7fff)
1955 for (i = 3; i < NI; i++)
1961 for (i = 2; i < NI; i++)
1967 /* clear high guard word */
1969 /* move in the significand */
1970 for (i = 0; i < NE - 1; i++)
1972 /* clear low guard word */
1976 /* Move out exploded e-type number A, converting it to e type B. */
1989 q = b + (NE - 1); /* point to output exponent */
1990 /* combine sign and exponent */
1993 *q-- = *p++ | 0x8000;
1997 if (*(p - 1) == 0x7fff)
2002 enan (b, eiisneg (a));
2010 /* skip over guard word */
2012 /* move the significand */
2013 for (j = 0; j < NE - 1; j++)
2017 /* Clear out exploded e-type number XI. */
2025 for (i = 0; i < NI; i++)
2029 /* Clear out exploded e-type XI, but don't touch the sign. */
2038 for (i = 0; i < NI - 1; i++)
2042 /* Move exploded e-type number from A to B. */
2051 for (i = 0; i < NI - 1; i++)
2053 /* clear low guard word */
2057 /* Generate exploded e-type NaN.
2058 The explicit pattern for this is maximum exponent and
2059 top two significant bits set. */
2073 /* Return nonzero if exploded e-type X is a NaN. */
2078 const UEMUSHORT x[];
2082 if ((x[E] & 0x7fff) == 0x7fff)
2084 for (i = M + 1; i < NI; i++)
2094 /* Return nonzero if sign of exploded e-type X is nonzero. */
2098 const UEMUSHORT x[];
2105 /* Fill exploded e-type X with infinity pattern.
2106 This has maximum exponent and significand all zeros. */
2118 /* Return nonzero if exploded e-type X is infinite. */
2123 const UEMUSHORT x[];
2130 if ((x[E] & 0x7fff) == 0x7fff)
2134 #endif /* INFINITY */
2136 /* Compare significands of numbers in internal exploded e-type format.
2137 Guard words are included in the comparison.
2145 const UEMUSHORT *a, *b;
2149 a += M; /* skip up to significand area */
2151 for (i = M; i < NI; i++)
2159 if (*(--a) > *(--b))
2165 /* Shift significand of exploded e-type X down by 1 bit. */
2174 x += M; /* point to significand area */
2177 for (i = M; i < NI; i++)
2189 /* Shift significand of exploded e-type X up by 1 bit. */
2201 for (i = M; i < NI; i++)
2214 /* Shift significand of exploded e-type X down by 8 bits. */
2220 UEMUSHORT newbyt, oldbyt;
2225 for (i = M; i < NI; i++)
2235 /* Shift significand of exploded e-type X up by 8 bits. */
2242 UEMUSHORT newbyt, oldbyt;
2247 for (i = M; i < NI; i++)
2257 /* Shift significand of exploded e-type X up by 16 bits. */
2269 for (i = M; i < NI - 1; i++)
2275 /* Shift significand of exploded e-type X down by 16 bits. */
2287 for (i = M; i < NI - 1; i++)
2293 /* Add significands of exploded e-type X and Y. X + Y replaces Y. */
2307 for (i = M; i < NI; i++)
2309 a = (unsigned EMULONG) (*x) + (unsigned EMULONG) (*y) + carry;
2320 /* Subtract significands of exploded e-type X and Y. Y - X replaces Y. */
2334 for (i = M; i < NI; i++)
2336 a = (unsigned EMULONG) (*y) - (unsigned EMULONG) (*x) - carry;
2348 static UEMUSHORT equot[NI];
2352 /* Radix 2 shift-and-add versions of multiply and divide */
2355 /* Divide significands */
2359 UEMUSHORT den[], num[];
2369 for (i = M; i < NI; i++)
2374 /* Use faster compare and subtraction if denominator has only 15 bits of
2380 for (i = M + 3; i < NI; i++)
2385 if ((den[M + 1] & 1) != 0)
2393 for (i = 0; i < NBITS + 2; i++)
2411 /* The number of quotient bits to calculate is NBITS + 1 scaling guard
2412 bit + 1 roundoff bit. */
2417 for (i = 0; i < NBITS + 2; i++)
2419 if (ecmpm (den, num) <= 0)
2422 j = 1; /* quotient bit = 1 */
2436 /* test for nonzero remainder after roundoff bit */
2439 for (i = M; i < NI; i++)
2447 for (i = 0; i < NI; i++)
2453 /* Multiply significands */
2464 for (i = M; i < NI; i++)
2469 while (*p == 0) /* significand is not supposed to be zero */
2474 if ((*p & 0xff) == 0)
2482 for (i = 0; i < k; i++)
2486 /* remember if there were any nonzero bits shifted out */
2493 for (i = 0; i < NI; i++)
2496 /* return flag for lost nonzero bits */
2502 /* Radix 65536 versions of multiply and divide. */
2504 /* Multiply significand of e-type number B
2505 by 16-bit quantity A, return e-type result to C. */
2510 const UEMUSHORT b[];
2514 unsigned EMULONG carry;
2515 const UEMUSHORT *ps;
2517 unsigned EMULONG aa, m;
2526 for (i=M+1; i<NI; i++)
2536 m = (unsigned EMULONG) aa * *ps--;
2537 carry = (m & 0xffff) + *pp;
2538 *pp-- = (UEMUSHORT) carry;
2539 carry = (carry >> 16) + (m >> 16) + *pp;
2540 *pp = (UEMUSHORT) carry;
2541 *(pp-1) = carry >> 16;
2544 for (i=M; i<NI; i++)
2548 /* Divide significands of exploded e-types NUM / DEN. Neither the
2549 numerator NUM nor the denominator DEN is permitted to have its high guard
2554 const UEMUSHORT den[];
2559 unsigned EMULONG tnum;
2560 UEMUSHORT j, tdenm, tquot;
2561 UEMUSHORT tprod[NI+1];
2567 for (i=M; i<NI; i++)
2573 for (i=M; i<NI; i++)
2575 /* Find trial quotient digit (the radix is 65536). */
2576 tnum = (((unsigned EMULONG) num[M]) << 16) + num[M+1];
2578 /* Do not execute the divide instruction if it will overflow. */
2579 if ((tdenm * (unsigned long) 0xffff) < tnum)
2582 tquot = tnum / tdenm;
2583 /* Multiply denominator by trial quotient digit. */
2584 m16m ((unsigned int) tquot, den, tprod);
2585 /* The quotient digit may have been overestimated. */
2586 if (ecmpm (tprod, num) > 0)
2590 if (ecmpm (tprod, num) > 0)
2600 /* test for nonzero remainder after roundoff bit */
2603 for (i=M; i<NI; i++)
2610 for (i=0; i<NI; i++)
2616 /* Multiply significands of exploded e-type A and B, result in B. */
2620 const UEMUSHORT a[];
2625 UEMUSHORT pprod[NI];
2631 for (i=M; i<NI; i++)
2637 for (i=M+1; i<NI; i++)
2645 m16m ((unsigned int) *p--, b, pprod);
2646 eaddm (pprod, equot);
2652 for (i=0; i<NI; i++)
2655 /* return flag for lost nonzero bits */
2661 /* Normalize and round off.
2663 The internal format number to be rounded is S.
2664 Input LOST is 0 if the value is exact. This is the so-called sticky bit.
2666 Input SUBFLG indicates whether the number was obtained
2667 by a subtraction operation. In that case if LOST is nonzero
2668 then the number is slightly smaller than indicated.
2670 Input EXP is the biased exponent, which may be negative.
2671 the exponent field of S is ignored but is replaced by
2672 EXP as adjusted by normalization and rounding.
2674 Input RCNTRL is the rounding control. If it is nonzero, the
2675 returned value will be rounded to RNDPRC bits.
2677 For future reference: In order for emdnorm to round off denormal
2678 significands at the right point, the input exponent must be
2679 adjusted to be the actual value it would have after conversion to
2680 the final floating point type. This adjustment has been
2681 implemented for all type conversions (etoe53, etc.) and decimal
2682 conversions, but not for the arithmetic functions (eadd, etc.).
2683 Data types having standard 15-bit exponents are not affected by
2684 this, but SFmode and DFmode are affected. For example, ediv with
2685 rndprc = 24 will not round correctly to 24-bit precision if the
2686 result is denormal. */
2688 static int rlast = -1;
2690 static UEMUSHORT rmsk = 0;
2691 static UEMUSHORT rmbit = 0;
2692 static UEMUSHORT rebit = 0;
2694 static UEMUSHORT rbit[NI];
2697 emdnorm (s, lost, subflg, exp, rcntrl)
2700 int subflg ATTRIBUTE_UNUSED;
2710 /* a blank significand could mean either zero or infinity. */
2723 if ((j > NBITS) && (exp < 32767))
2731 if (exp > (EMULONG) (-NBITS - 1))
2744 /* Round off, unless told not to by rcntrl. */
2747 /* Set up rounding parameters if the control register changed. */
2748 if (rndprc != rlast)
2755 rw = NI - 1; /* low guard word */
2778 /* For DEC or IBM arithmetic */
2795 /* For C4x arithmetic */
2816 /* Shift down 1 temporarily if the data structure has an implied
2817 most significant bit and the number is denormal.
2818 Intel long double denormals also lose one bit of precision. */
2819 if ((exp <= 0) && (rndprc != NBITS)
2820 && ((rndprc != 64) || ((rndprc == 64) && ! REAL_WORDS_BIG_ENDIAN)))
2822 lost |= s[NI - 1] & 1;
2825 /* Clear out all bits below the rounding bit,
2826 remembering in r if any were nonzero. */
2840 if ((r & rmbit) != 0)
2846 { /* round to even */
2847 if ((s[re] & rebit) == 0)
2862 /* Undo the temporary shift for denormal values. */
2863 if ((exp <= 0) && (rndprc != NBITS)
2864 && ((rndprc != 64) || ((rndprc == 64) && ! REAL_WORDS_BIG_ENDIAN)))
2869 { /* overflow on roundoff */
2882 for (i = 2; i < NI - 1; i++)
2885 warning ("floating point overflow");
2889 for (i = M + 1; i < NI - 1; i++)
2892 if ((rndprc < 64) || (rndprc == 113))
2907 s[1] = (UEMUSHORT) exp;
2910 /* Subtract. C = B - A, all e type numbers. */
2912 static int subflg = 0;
2916 const UEMUSHORT *a, *b;
2931 /* Infinity minus infinity is a NaN.
2932 Test for subtracting infinities of the same sign. */
2933 if (eisinf (a) && eisinf (b)
2934 && ((eisneg (a) ^ eisneg (b)) == 0))
2936 mtherr ("esub", INVALID);
2945 /* Add. C = A + B, all e type. */
2949 const UEMUSHORT *a, *b;
2954 /* NaN plus anything is a NaN. */
2965 /* Infinity minus infinity is a NaN.
2966 Test for adding infinities of opposite signs. */
2967 if (eisinf (a) && eisinf (b)
2968 && ((eisneg (a) ^ eisneg (b)) != 0))
2970 mtherr ("esub", INVALID);
2979 /* Arithmetic common to both addition and subtraction. */
2983 const UEMUSHORT *a, *b;
2986 UEMUSHORT ai[NI], bi[NI], ci[NI];
2988 EMULONG lt, lta, ltb;
3009 /* compare exponents */
3014 { /* put the larger number in bi */
3024 if (lt < (EMULONG) (-NBITS - 1))
3025 goto done; /* answer same as larger addend */
3027 lost = eshift (ai, k); /* shift the smaller number down */
3031 /* exponents were the same, so must compare significands */
3034 { /* the numbers are identical in magnitude */
3035 /* if different signs, result is zero */
3041 /* if same sign, result is double */
3042 /* double denormalized tiny number */
3043 if ((bi[E] == 0) && ((bi[3] & 0x8000) == 0))
3048 /* add 1 to exponent unless both are zero! */
3049 for (j = 1; j < NI - 1; j++)
3065 bi[E] = (UEMUSHORT) ltb;
3069 { /* put the larger number in bi */
3085 emdnorm (bi, lost, subflg, ltb, !ROUND_TOWARDS_ZERO);
3091 /* Divide: C = B/A, all e type. */
3095 const UEMUSHORT *a, *b;
3098 UEMUSHORT ai[NI], bi[NI];
3100 EMULONG lt, lta, ltb;
3102 /* IEEE says if result is not a NaN, the sign is "-" if and only if
3103 operands have opposite signs -- but flush -0 to 0 later if not IEEE. */
3104 sign = eisneg (a) ^ eisneg (b);
3107 /* Return any NaN input. */
3118 /* Zero over zero, or infinity over infinity, is a NaN. */
3119 if (((ecmp (a, ezero) == 0) && (ecmp (b, ezero) == 0))
3120 || (eisinf (a) && eisinf (b)))
3122 mtherr ("ediv", INVALID);
3127 /* Infinity over anything else is infinity. */
3134 /* Anything else over infinity is zero. */
3146 { /* See if numerator is zero. */
3147 for (i = 1; i < NI - 1; i++)
3151 ltb -= enormlz (bi);
3161 { /* possible divide by zero */
3162 for (i = 1; i < NI - 1; i++)
3166 lta -= enormlz (ai);
3170 /* Divide by zero is not an invalid operation.
3171 It is a divide-by-zero operation! */
3173 mtherr ("ediv", SING);
3179 /* calculate exponent */
3180 lt = ltb - lta + EXONE;
3181 emdnorm (bi, i, 0, lt, !ROUND_TOWARDS_ZERO);
3188 && (ecmp (c, ezero) != 0)
3191 *(c+(NE-1)) |= 0x8000;
3193 *(c+(NE-1)) &= ~0x8000;
3196 /* Multiply e-types A and B, return e-type product C. */
3200 const UEMUSHORT *a, *b;
3203 UEMUSHORT ai[NI], bi[NI];
3205 EMULONG lt, lta, ltb;
3207 /* IEEE says if result is not a NaN, the sign is "-" if and only if
3208 operands have opposite signs -- but flush -0 to 0 later if not IEEE. */
3209 sign = eisneg (a) ^ eisneg (b);
3212 /* NaN times anything is the same NaN. */
3223 /* Zero times infinity is a NaN. */
3224 if ((eisinf (a) && (ecmp (b, ezero) == 0))
3225 || (eisinf (b) && (ecmp (a, ezero) == 0)))
3227 mtherr ("emul", INVALID);
3232 /* Infinity times anything else is infinity. */
3234 if (eisinf (a) || eisinf (b))
3246 for (i = 1; i < NI - 1; i++)
3250 lta -= enormlz (ai);
3261 for (i = 1; i < NI - 1; i++)
3265 ltb -= enormlz (bi);
3274 /* Multiply significands */
3276 /* calculate exponent */
3277 lt = lta + ltb - (EXONE - 1);
3278 emdnorm (bi, j, 0, lt, !ROUND_TOWARDS_ZERO);
3285 && (ecmp (c, ezero) != 0)
3288 *(c+(NE-1)) |= 0x8000;
3290 *(c+(NE-1)) &= ~0x8000;
3293 /* Convert double precision PE to e-type Y. */
3297 const UEMUSHORT *pe;
3307 ibmtoe (pe, y, DFmode);
3312 c4xtoe (pe, y, HFmode);
3316 ieeetoe (pe, y, &ieee_53);
3318 #endif /* not C4X */
3319 #endif /* not IBM */
3320 #endif /* not DEC */
3323 /* Convert double extended precision float PE to e type Y. */
3327 const UEMUSHORT *pe;
3337 for (i = 0; i < NE - 5; i++)
3340 /* REAL_WORDS_BIG_ENDIAN is always 0 for DEC and 1 for IBM.
3341 This precision is not ordinarily supported on DEC or IBM. */
3342 if (! REAL_WORDS_BIG_ENDIAN)
3344 for (i = 0; i < 5; i++)
3348 /* For denormal long double Intel format, shift significand up one
3349 -- but only if the top significand bit is zero. A top bit of 1
3350 is "pseudodenormal" when the exponent is zero. */
3351 if ((yy[NE-1] & 0x7fff) == 0 && (yy[NE-2] & 0x8000) == 0)
3364 p = &yy[0] + (NE - 1);
3367 for (i = 0; i < 4; i++)
3370 #endif /* not C4X */
3372 /* Point to the exponent field and check max exponent cases. */
3374 if ((*p & 0x7fff) == 0x7fff)
3377 if (! REAL_WORDS_BIG_ENDIAN)
3379 for (i = 0; i < 4; i++)
3381 if ((i != 3 && pe[i] != 0)
3382 /* Anything but 0x8000 here, including 0, is a NaN. */
3383 || (i == 3 && pe[i] != 0x8000))
3385 enan (y, (*p & 0x8000) != 0);
3392 /* In Motorola extended precision format, the most significant
3393 bit of an infinity mantissa could be either 1 or 0. It is
3394 the lower order bits that tell whether the value is a NaN. */
3395 if ((pe[2] & 0x7fff) != 0)
3398 for (i = 3; i <= 5; i++)
3403 enan (y, (*p & 0x8000) != 0);
3415 #endif /* INFINITY */
3418 for (i = 0; i < NE; i++)
3422 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
3423 /* Convert 128-bit long double precision float PE to e type Y. */
3427 const UEMUSHORT *pe;
3430 ieeetoe (pe, y, &ieee_113);
3432 #endif /* INTEL_EXTENDED_IEEE_FORMAT == 0 */
3434 /* Convert single precision float PE to e type Y. */
3438 const UEMUSHORT *pe;
3443 ibmtoe (pe, y, SFmode);
3449 c4xtoe (pe, y, QFmode);
3454 ieeetoe (pe, y, &dec_f);
3458 ieeetoe (pe, y, &ieee_24);
3460 #endif /* not DEC */
3461 #endif /* not C4X */
3462 #endif /* not IBM */
3465 /* Convert machine format float of specified format PE to e type Y. */
3468 ieeetoe (pe, y, fmt)
3469 const UEMUSHORT *pe;
3471 const struct ieee_format *fmt;
3478 int shortsm1 = fmt->bits / 16 - 1;
3480 int expmask = (1 << fmt->expbits) - 1;
3482 int expshift = (fmt->precision - 1) & 0x0f;
3483 int highbit = 1 << expshift;
3488 if (! REAL_WORDS_BIG_ENDIAN)
3494 yy[M] = (r & (highbit - 1)) | highbit;
3495 r = (r & 0x7fff) >> expshift;
3497 if (!LARGEST_EXPONENT_IS_NORMAL (fmt->precision) && r == expmask)
3500 /* First check the word where high order mantissa and exponent live */
3501 if ((*e & (highbit - 1)) != 0)
3503 enan (y, yy[0] != 0);
3506 if (! REAL_WORDS_BIG_ENDIAN)
3508 for (i = 0; i < shortsm1; i++)
3512 enan (y, yy[0] != 0);
3519 for (i = 1; i < shortsm1 + 1; i++)
3523 enan (y, yy[0] != 0);
3535 #endif /* INFINITY */
3536 /* If zero exponent, then the significand is denormalized.
3537 So take back the understood high significand bit. */
3543 r += fmt->adjustment;
3546 if (! REAL_WORDS_BIG_ENDIAN)
3548 for (i = 0; i < shortsm1; i++)
3554 for (i = 0; i < shortsm1; i++)
3557 if (fmt->precision == 113)
3559 /* denorm is left alone in 113 bit format */
3565 eshift (yy, -(expshift + 1));
3567 { /* if zero exponent, then normalize the significand */
3568 if ((k = enormlz (yy)) > NBITS)
3571 yy[E] -= (UEMUSHORT) (k - 1);
3577 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
3578 /* Convert e-type X to IEEE 128-bit long double format E. */
3585 etoieee (x, e, &ieee_113);
3588 /* Convert exploded e-type X, that has already been rounded to
3589 113-bit precision, to IEEE 128-bit long double format Y. */
3595 toieee (x, y, &ieee_113);
3598 #endif /* INTEL_EXTENDED_IEEE_FORMAT == 0 */
3600 /* Convert e-type X to IEEE double extended format E. */
3607 etoieee (x, e, &ieee_64);
3610 /* Convert exploded e-type X, that has already been rounded to
3611 64-bit precision, to IEEE double extended format Y. */
3617 toieee (x, y, &ieee_64);
3620 /* e type to double precision. */
3623 /* Convert e-type X to DEC-format double E. */
3633 /* Convert exploded e-type X, that has already been rounded to
3634 56-bit double precision, to DEC double Y. */
3645 /* Convert e-type X to IBM 370-format double E. */
3652 etoibm (x, e, DFmode);
3655 /* Convert exploded e-type X, that has already been rounded to
3656 56-bit precision, to IBM 370 double Y. */
3662 toibm (x, y, DFmode);
3665 #else /* it's neither DEC nor IBM */
3667 /* Convert e-type X to C4X-format long double E. */
3674 etoc4x (x, e, HFmode);
3677 /* Convert exploded e-type X, that has already been rounded to
3678 56-bit precision, to IBM 370 double Y. */
3684 toc4x (x, y, HFmode);
3687 #else /* it's neither DEC nor IBM nor C4X */
3689 /* Convert e-type X to IEEE double E. */
3696 etoieee (x, e, &ieee_53);
3699 /* Convert exploded e-type X, that has already been rounded to
3700 53-bit precision, to IEEE double Y. */
3706 toieee (x, y, &ieee_53);
3709 #endif /* not C4X */
3710 #endif /* not IBM */
3711 #endif /* not DEC */
3715 /* e type to single precision. */
3718 /* Convert e-type X to IBM 370 float E. */
3725 etoibm (x, e, SFmode);
3728 /* Convert exploded e-type X, that has already been rounded to
3729 float precision, to IBM 370 float Y. */
3735 toibm (x, y, SFmode);
3738 #else /* it's not IBM */
3741 /* Convert e-type X to C4X float E. */
3748 etoc4x (x, e, QFmode);
3751 /* Convert exploded e-type X, that has already been rounded to
3752 float precision, to IBM 370 float Y. */
3758 toc4x (x, y, QFmode);
3761 #else /* it's neither IBM nor C4X */
3765 /* Convert e-type X to DEC F-float E. */
3772 etoieee (x, e, &dec_f);
3775 /* Convert exploded e-type X, that has already been rounded to
3776 float precision, to DEC F-float Y. */
3782 toieee (x, y, &dec_f);
3787 /* Convert e-type X to IEEE float E. */
3794 etoieee (x, e, &ieee_24);
3797 /* Convert exploded e-type X, that has already been rounded to
3798 float precision, to IEEE float Y. */
3804 toieee (x, y, &ieee_24);
3807 #endif /* not DEC */
3808 #endif /* not C4X */
3809 #endif /* not IBM */
3812 /* Convert e-type X to the IEEE format described by FMT. */
3818 const struct ieee_format *fmt;
3827 make_nan (e, eisneg (x), fmt->mode);
3838 /* Adjust exponent for offset. */
3839 exp = (EMULONG) xi[E] - fmt->adjustment;
3841 /* Round off to nearest or even. */
3843 rndprc = fmt->precision;
3844 emdnorm (xi, 0, 0, exp, !ROUND_TOWARDS_ZERO);
3849 toieee (xi, e, fmt);
3852 /* Convert exploded e-type X, that has already been rounded to
3853 the necessary precision, to the IEEE format described by FMT. */
3858 const struct ieee_format *fmt;
3865 maxexp = (1 << fmt->expbits) - 1;
3866 words = (fmt->bits - fmt->expbits) / EMUSHORT_SIZE;
3871 make_nan (y, eiisneg (x), fmt->mode);
3876 if (fmt->expbits < 15
3877 && LARGEST_EXPONENT_IS_NORMAL (fmt->bits)
3880 saturate (y, eiisneg (x), fmt->bits, 1);
3884 /* Point to the exponent. */
3885 if (REAL_WORDS_BIG_ENDIAN)
3890 /* Copy the sign. */
3896 if (fmt->expbits < 15
3897 && !LARGEST_EXPONENT_IS_NORMAL (fmt->bits)
3900 /* Saturate at largest number less that infinity. */
3903 *q |= maxexp << (15 - fmt->expbits);
3906 *q |= (maxexp << (15 - fmt->expbits)) - 1;
3910 if (!REAL_WORDS_BIG_ENDIAN)
3912 for (i = 0; i < words; i++)
3917 for (i = 0; i < words; i++)
3920 #if defined(INFINITY) && defined(ERANGE)
3926 /* If denormal and DEC float, return zero (DEC has no denormals) */
3930 for (i = 0; i < fmt->bits / EMUSHORT_SIZE ; i++)
3936 /* Delete the implied bit unless denormal, except for
3937 64-bit precision. */
3938 if (fmt->precision != 64 && x[E] != 0)
3943 /* Shift denormal double extended Intel format significand down
3945 if (fmt->precision == 64 && x[E] == 0 && ! REAL_WORDS_BIG_ENDIAN)
3948 if (fmt->expbits < 15)
3950 /* Shift the significand. */
3951 eshift (x, 15 - fmt->expbits);
3953 /* Combine the exponent and upper bits of the significand. */
3954 *q |= x[E] << (15 - fmt->expbits);
3955 *q |= x[M] & (UEMUSHORT) ~((maxexp << (15 - fmt->expbits)) | 0x8000);
3959 /* Copy the exponent. */
3963 /* Add padding after the exponent. At the moment, this is only necessary for
3964 64-bit precision; in this case, the padding is 16 bits. */
3965 if (fmt->precision == 64)
3970 if (REAL_WORDS_BIG_ENDIAN)
3974 /* Copy the significand. */
3975 if (REAL_WORDS_BIG_ENDIAN)
3977 for (i = 0; i < words; i++)
3978 *(++q) = x[i + M + 1];
3981 else if (fmt->precision == 64 && eiisinf (x))
3983 /* Intel double extended infinity significand. */
3992 for (i = 0; i < words; i++)
3993 *(--q) = x[i + M + 1];
3998 /* Compare two e type numbers.
4002 -2 if either a or b is a NaN. */
4006 const UEMUSHORT *a, *b;
4008 UEMUSHORT ai[NI], bi[NI];
4014 if (eisnan (a) || eisnan (b))
4023 { /* the signs are different */
4025 for (i = 1; i < NI - 1; i++)
4039 /* both are the same sign */
4054 return (0); /* equality */
4058 if (*(--p) > *(--q))
4059 return (msign); /* p is bigger */
4061 return (-msign); /* p is littler */
4065 /* Find e-type nearest integer to X, as floor (X + 0.5). */
4077 /* Convert HOST_WIDE_INT LP to e type Y. */
4081 const HOST_WIDE_INT *lp;
4085 unsigned HOST_WIDE_INT ll;
4091 /* make it positive */
4092 ll = (unsigned HOST_WIDE_INT) (-(*lp));
4093 yi[0] = 0xffff; /* put correct sign in the e type number */
4097 ll = (unsigned HOST_WIDE_INT) (*lp);
4099 /* move the long integer to yi significand area */
4100 #if HOST_BITS_PER_WIDE_INT == 64
4101 yi[M] = (UEMUSHORT) (ll >> 48);
4102 yi[M + 1] = (UEMUSHORT) (ll >> 32);
4103 yi[M + 2] = (UEMUSHORT) (ll >> 16);
4104 yi[M + 3] = (UEMUSHORT) ll;
4105 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
4107 yi[M] = (UEMUSHORT) (ll >> 16);
4108 yi[M + 1] = (UEMUSHORT) ll;
4109 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
4112 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
4113 ecleaz (yi); /* it was zero */
4115 yi[E] -= (UEMUSHORT) k;/* subtract shift count from exponent */
4116 emovo (yi, y); /* output the answer */
4119 /* Convert unsigned HOST_WIDE_INT LP to e type Y. */
4123 const unsigned HOST_WIDE_INT *lp;
4127 unsigned HOST_WIDE_INT ll;
4133 /* move the long integer to ayi significand area */
4134 #if HOST_BITS_PER_WIDE_INT == 64
4135 yi[M] = (UEMUSHORT) (ll >> 48);
4136 yi[M + 1] = (UEMUSHORT) (ll >> 32);
4137 yi[M + 2] = (UEMUSHORT) (ll >> 16);
4138 yi[M + 3] = (UEMUSHORT) ll;
4139 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
4141 yi[M] = (UEMUSHORT) (ll >> 16);
4142 yi[M + 1] = (UEMUSHORT) ll;
4143 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
4146 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
4147 ecleaz (yi); /* it was zero */
4149 yi[E] -= (UEMUSHORT) k; /* subtract shift count from exponent */
4150 emovo (yi, y); /* output the answer */
4154 /* Find signed HOST_WIDE_INT integer I and floating point fractional
4155 part FRAC of e-type (packed internal format) floating point input X.
4156 The integer output I has the sign of the input, except that
4157 positive overflow is permitted if FIXUNS_TRUNC_LIKE_FIX_TRUNC.
4158 The output e-type fraction FRAC is the positive fractional
4169 unsigned HOST_WIDE_INT ll;
4172 k = (int) xi[E] - (EXONE - 1);
4175 /* if exponent <= 0, integer = 0 and real output is fraction */
4180 if (k > (HOST_BITS_PER_WIDE_INT - 1))
4182 /* long integer overflow: output large integer
4183 and correct fraction */
4185 *i = ((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1);
4188 #ifdef FIXUNS_TRUNC_LIKE_FIX_TRUNC
4189 /* In this case, let it overflow and convert as if unsigned. */
4190 euifrac (x, &ll, frac);
4191 *i = (HOST_WIDE_INT) ll;
4194 /* In other cases, return the largest positive integer. */
4195 *i = (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1;
4200 warning ("overflow on truncation to integer");
4204 /* Shift more than 16 bits: first shift up k-16 mod 16,
4205 then shift up by 16's. */
4206 j = k - ((k >> 4) << 4);
4213 ll = (ll << 16) | xi[M];
4215 while ((k -= 16) > 0);
4222 /* shift not more than 16 bits */
4224 *i = (HOST_WIDE_INT) xi[M] & 0xffff;
4231 if ((k = enormlz (xi)) > NBITS)
4234 xi[E] -= (UEMUSHORT) k;
4240 /* Find unsigned HOST_WIDE_INT integer I and floating point fractional part
4241 FRAC of e-type X. A negative input yields integer output = 0 but
4242 correct fraction. */
4245 euifrac (x, i, frac)
4247 unsigned HOST_WIDE_INT *i;
4250 unsigned HOST_WIDE_INT ll;
4255 k = (int) xi[E] - (EXONE - 1);
4258 /* if exponent <= 0, integer = 0 and argument is fraction */
4263 if (k > HOST_BITS_PER_WIDE_INT)
4265 /* Long integer overflow: output large integer
4266 and correct fraction.
4267 Note, the BSD MicroVAX compiler says that ~(0UL)
4268 is a syntax error. */
4272 warning ("overflow on truncation to unsigned integer");
4276 /* Shift more than 16 bits: first shift up k-16 mod 16,
4277 then shift up by 16's. */
4278 j = k - ((k >> 4) << 4);
4285 ll = (ll << 16) | xi[M];
4287 while ((k -= 16) > 0);
4292 /* shift not more than 16 bits */
4294 *i = (HOST_WIDE_INT) xi[M] & 0xffff;
4297 if (xi[0]) /* A negative value yields unsigned integer 0. */
4303 if ((k = enormlz (xi)) > NBITS)
4306 xi[E] -= (UEMUSHORT) k;
4311 /* Shift the significand of exploded e-type X up or down by SC bits. */
4332 lost |= *p; /* remember lost bits */
4373 return ((int) lost);
4376 /* Shift normalize the significand area of exploded e-type X.
4377 Return the shift count (up = positive). */
4392 return (0); /* already normalized */
4398 /* With guard word, there are NBITS+16 bits available.
4399 Return true if all are zero. */
4403 /* see if high byte is zero */
4404 while ((*p & 0xff00) == 0)
4409 /* now shift 1 bit at a time */
4410 while ((*p & 0x8000) == 0)
4416 mtherr ("enormlz", UNDERFLOW);
4422 /* Normalize by shifting down out of the high guard word
4423 of the significand */
4438 mtherr ("enormlz", OVERFLOW);
4445 /* Powers of ten used in decimal <-> binary conversions. */
4450 #if MAX_LONG_DOUBLE_TYPE_SIZE == 128 && (INTEL_EXTENDED_IEEE_FORMAT == 0)
4451 static const UEMUSHORT etens[NTEN + 1][NE] =
4453 {0x6576, 0x4a92, 0x804a, 0x153f,
4454 0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
4455 {0x6a32, 0xce52, 0x329a, 0x28ce,
4456 0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
4457 {0x526c, 0x50ce, 0xf18b, 0x3d28,
4458 0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
4459 {0x9c66, 0x58f8, 0xbc50, 0x5c54,
4460 0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
4461 {0x851e, 0xeab7, 0x98fe, 0x901b,
4462 0xddbb, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
4463 {0x0235, 0x0137, 0x36b1, 0x336c,
4464 0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
4465 {0x50f8, 0x25fb, 0xc76b, 0x6b71,
4466 0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
4467 {0x0000, 0x0000, 0x0000, 0x0000,
4468 0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
4469 {0x0000, 0x0000, 0x0000, 0x0000,
4470 0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
4471 {0x0000, 0x0000, 0x0000, 0x0000,
4472 0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
4473 {0x0000, 0x0000, 0x0000, 0x0000,
4474 0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
4475 {0x0000, 0x0000, 0x0000, 0x0000,
4476 0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
4477 {0x0000, 0x0000, 0x0000, 0x0000,
4478 0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
4481 static const UEMUSHORT emtens[NTEN + 1][NE] =
4483 {0x2030, 0xcffc, 0xa1c3, 0x8123,
4484 0x2de3, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
4485 {0x8264, 0xd2cb, 0xf2ea, 0x12d4,
4486 0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
4487 {0xf53f, 0xf698, 0x6bd3, 0x0158,
4488 0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
4489 {0xe731, 0x04d4, 0xe3f2, 0xd332,
4490 0x7132, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
4491 {0xa23e, 0x5308, 0xfefb, 0x1155,
4492 0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
4493 {0xe26d, 0xdbde, 0xd05d, 0xb3f6,
4494 0xac7c, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
4495 {0x2a20, 0x6224, 0x47b3, 0x98d7,
4496 0x3f23, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
4497 {0x0b5b, 0x4af2, 0xa581, 0x18ed,
4498 0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
4499 {0xbf71, 0xa9b3, 0x7989, 0xbe68,
4500 0x4c2e, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
4501 {0x3d4d, 0x7c3d, 0x36ba, 0x0d2b,
4502 0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
4503 {0xc155, 0xa4a8, 0x404e, 0x6113,
4504 0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
4505 {0xd70a, 0x70a3, 0x0a3d, 0xa3d7,
4506 0x3d70, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
4507 {0xcccd, 0xcccc, 0xcccc, 0xcccc,
4508 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
4511 /* LONG_DOUBLE_TYPE_SIZE is other than 128 */
4512 static const UEMUSHORT etens[NTEN + 1][NE] =
4514 {0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
4515 {0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
4516 {0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
4517 {0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
4518 {0xddbc, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
4519 {0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
4520 {0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
4521 {0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
4522 {0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
4523 {0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
4524 {0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
4525 {0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
4526 {0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
4529 static const UEMUSHORT emtens[NTEN + 1][NE] =
4531 {0x2de4, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
4532 {0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
4533 {0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
4534 {0x7133, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
4535 {0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
4536 {0xac7d, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
4537 {0x3f24, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
4538 {0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
4539 {0x4c2f, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
4540 {0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
4541 {0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
4542 {0x3d71, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
4543 {0xcccd, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
4548 /* Convert float value X to ASCII string STRING with NDIG digits after
4549 the decimal point. */
4552 e24toasc (x, string, ndigs)
4553 const UEMUSHORT x[];
4560 etoasc (w, string, ndigs);
4563 /* Convert double value X to ASCII string STRING with NDIG digits after
4564 the decimal point. */
4567 e53toasc (x, string, ndigs)
4568 const UEMUSHORT x[];
4575 etoasc (w, string, ndigs);
4578 /* Convert double extended value X to ASCII string STRING with NDIG digits
4579 after the decimal point. */
4582 e64toasc (x, string, ndigs)
4583 const UEMUSHORT x[];
4590 etoasc (w, string, ndigs);
4593 /* Convert 128-bit long double value X to ASCII string STRING with NDIG digits
4594 after the decimal point. */
4597 e113toasc (x, string, ndigs)
4598 const UEMUSHORT x[];
4605 etoasc (w, string, ndigs);
4609 /* Convert e-type X to ASCII string STRING with NDIGS digits after
4610 the decimal point. */
4612 static char wstring[80]; /* working storage for ASCII output */
4615 etoasc (x, string, ndigs)
4616 const UEMUSHORT x[];
4621 UEMUSHORT y[NI], t[NI], u[NI], w[NI];
4622 const UEMUSHORT *p, *r, *ten;
4624 int i, j, k, expon, rndsav;
4637 sprintf (wstring, " NaN ");
4641 rndprc = NBITS; /* set to full precision */
4642 emov (x, y); /* retain external format */
4643 if (y[NE - 1] & 0x8000)
4646 y[NE - 1] &= 0x7fff;
4653 ten = &etens[NTEN][0];
4655 /* Test for zero exponent */
4658 for (k = 0; k < NE - 1; k++)
4661 goto tnzro; /* denormalized number */
4663 goto isone; /* valid all zeros */
4667 /* Test for infinity. */
4668 if (y[NE - 1] == 0x7fff)
4671 sprintf (wstring, " -Infinity ");
4673 sprintf (wstring, " Infinity ");
4677 /* Test for exponent nonzero but significand denormalized.
4678 * This is an error condition.
4680 if ((y[NE - 1] != 0) && ((y[NE - 2] & 0x8000) == 0))
4682 mtherr ("etoasc", DOMAIN);
4683 sprintf (wstring, "NaN");
4687 /* Compare to 1.0 */
4696 { /* Number is greater than 1 */
4697 /* Convert significand to an integer and strip trailing decimal zeros. */
4699 u[NE - 1] = EXONE + NBITS - 1;
4701 p = &etens[NTEN - 4][0];
4707 for (j = 0; j < NE - 1; j++)
4720 /* Rescale from integer significand */
4721 u[NE - 1] += y[NE - 1] - (unsigned int) (EXONE + NBITS - 1);
4723 /* Find power of 10 */
4727 /* An unordered compare result shouldn't happen here. */
4728 while (ecmp (ten, u) <= 0)
4730 if (ecmp (p, u) <= 0)
4743 { /* Number is less than 1.0 */
4744 /* Pad significand with trailing decimal zeros. */
4747 while ((y[NE - 2] & 0x8000) == 0)
4756 for (i = 0; i < NDEC + 1; i++)
4758 if ((w[NI - 1] & 0x7) != 0)
4760 /* multiply by 10 */
4773 if (eone[NE - 1] <= u[1])
4785 while (ecmp (eone, w) > 0)
4787 if (ecmp (p, w) >= 0)
4802 /* Find the first (leading) digit. */
4808 digit = equot[NI - 1];
4809 while ((digit == 0) && (ecmp (y, ezero) != 0))
4817 digit = equot[NI - 1];
4825 /* Examine number of digits requested by caller. */
4843 *s++ = (char) digit + '0';
4846 /* Generate digits after the decimal point. */
4847 for (k = 0; k <= ndigs; k++)
4849 /* multiply current number by 10, without normalizing */
4856 *s++ = (char) equot[NI - 1] + '0';
4858 digit = equot[NI - 1];
4861 /* round off the ASCII string */
4864 /* Test for critical rounding case in ASCII output. */
4868 if (ecmp (t, ezero) != 0)
4869 goto roun; /* round to nearest */
4871 if ((*(s - 1) & 1) == 0)
4872 goto doexp; /* round to even */
4875 /* Round up and propagate carry-outs */
4879 /* Carry out to most significant digit? */
4886 /* Most significant digit carries to 10? */
4894 /* Round up and carry out from less significant digits */
4904 /* Strip trailing zeros, but leave at least one. */
4905 while (ss[-1] == '0' && ss[-2] != '.')
4907 sprintf (ss, "e%d", expon);
4910 /* copy out the working string */
4913 while (*ss == ' ') /* strip possible leading space */
4915 while ((*s++ = *ss++) != '\0')
4920 /* Convert ASCII string to floating point.
4922 Numeric input is a free format decimal number of any length, with
4923 or without decimal point. Entering E after the number followed by an
4924 integer number causes the second number to be interpreted as a power of
4925 10 to be multiplied by the first number (i.e., "scientific" notation). */
4927 /* Convert ASCII string S to single precision float value Y. */
4938 /* Convert ASCII string S to double precision value Y. */
4945 #if defined(DEC) || defined(IBM)
4957 /* Convert ASCII string S to double extended value Y. */
4967 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
4968 /* Convert ASCII string S to 128-bit long double Y. */
4975 asctoeg (s, y, 113);
4979 /* Convert ASCII string S to e type Y. */
4986 asctoeg (s, y, NBITS);
4989 /* Convert ASCII string SS to e type Y, with a specified rounding precision
4990 of OPREC bits. BASE is 16 for C99 hexadecimal floating constants. */
4993 asctoeg (ss, y, oprec)
4998 UEMUSHORT yy[NI], xt[NI], tt[NI];
4999 int esign, decflg, sgnflg, nexp, exp, prec, lost;
5000 int i, k, trail, c, rndsav;
5003 char *sp, *s, *lstr;
5006 /* Copy the input string. */
5007 lstr = (char *) alloca (strlen (ss) + 1);
5009 while (*ss == ' ') /* skip leading spaces */
5013 while ((*sp++ = *ss++) != '\0')
5017 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
5024 rndprc = NBITS; /* Set to full precision */
5037 if ((k >= 0) && (k < base))
5039 /* Ignore leading zeros */
5040 if ((prec == 0) && (decflg == 0) && (k == 0))
5042 /* Identify and strip trailing zeros after the decimal point. */
5043 if ((trail == 0) && (decflg != 0))
5046 while (ISDIGIT (*sp) || (base == 16 && ISXDIGIT (*sp)))
5048 /* Check for syntax error */
5050 if ((base != 10 || ((c != 'e') && (c != 'E')))
5051 && (base != 16 || ((c != 'p') && (c != 'P')))
5053 && (c != '\n') && (c != '\r') && (c != ' ')
5055 goto unexpected_char_error;
5064 /* If enough digits were given to more than fill up the yy register,
5065 continuing until overflow into the high guard word yy[2]
5066 guarantees that there will be a roundoff bit at the top
5067 of the low guard word after normalization. */
5074 nexp += 4; /* count digits after decimal point */
5076 eshup1 (yy); /* multiply current number by 16 */
5084 nexp += 1; /* count digits after decimal point */
5086 eshup1 (yy); /* multiply current number by 10 */
5092 /* Insert the current digit. */
5094 xt[NI - 2] = (UEMUSHORT) k;
5099 /* Mark any lost non-zero digit. */
5101 /* Count lost digits before the decimal point. */
5123 case '.': /* decimal point */
5125 goto unexpected_char_error;
5131 goto unexpected_char_error;
5136 goto unexpected_char_error;
5149 unexpected_char_error:
5153 mtherr ("asctoe", DOMAIN);
5162 /* Exponent interpretation */
5164 /* 0.0eXXX is zero, regardless of XXX. Check for the 0.0. */
5165 for (k = 0; k < NI; k++)
5176 /* check for + or - */
5184 while (ISDIGIT (*s))
5193 if ((exp > MAXDECEXP) && (base == 10))
5197 yy[E] = 0x7fff; /* infinity */
5200 if ((exp < MINDECEXP) && (base == 10))
5210 /* Base 16 hexadecimal floating constant. */
5211 if ((k = enormlz (yy)) > NBITS)
5216 /* Adjust the exponent. NEXP is the number of hex digits,
5217 EXP is a power of 2. */
5218 lexp = (EXONE - 1 + NBITS) - k + yy[E] + exp - nexp;
5228 /* Pad trailing zeros to minimize power of 10, per IEEE spec. */
5229 while ((nexp > 0) && (yy[2] == 0))
5241 if ((k = enormlz (yy)) > NBITS)
5246 lexp = (EXONE - 1 + NBITS) - k;
5247 emdnorm (yy, lost, 0, lexp, 64);
5250 /* Convert to external format:
5252 Multiply by 10**nexp. If precision is 64 bits,
5253 the maximum relative error incurred in forming 10**n
5254 for 0 <= n <= 324 is 8.2e-20, at 10**180.
5255 For 0 <= n <= 999, the peak relative error is 1.4e-19 at 10**947.
5256 For 0 >= n >= -999, it is -1.55e-19 at 10**-435. */
5271 /* Punt. Can't handle this without 2 divides. */
5272 emovi (etens[0], tt);
5285 emul (etens[i], xt, xt);
5289 while (exp <= MAXP);
5308 /* Round and convert directly to the destination type */
5310 lexp -= EXONE - 0x3ff;
5312 else if (oprec == 24 || oprec == 32)
5313 lexp -= (EXONE - 0x7f);
5316 else if (oprec == 24 || oprec == 56)
5317 lexp -= EXONE - (0x41 << 2);
5320 else if (oprec == 24)
5321 lexp -= dec_f.adjustment;
5322 else if (oprec == 56)
5325 lexp -= dec_g.adjustment;
5327 lexp -= dec_d.adjustment;
5330 else if (oprec == 24)
5331 lexp -= EXONE - 0177;
5336 emdnorm (yy, lost, 0, lexp, 64);
5351 toibm (yy, y, DFmode);
5356 toc4x (yy, y, HFmode);
5369 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
5382 /* Return Y = largest integer not greater than X (truncated toward minus
5385 static const UEMUSHORT bmask[] =
5408 const UEMUSHORT x[];
5415 emov (x, f); /* leave in external format */
5416 expon = (int) f[NE - 1];
5417 e = (expon & 0x7fff) - (EXONE - 1);
5423 /* number of bits to clear out */
5435 /* clear the remaining bits */
5437 /* truncate negatives toward minus infinity */
5440 if ((UEMUSHORT) expon & (UEMUSHORT) 0x8000)
5442 for (i = 0; i < NE - 1; i++)
5455 /* Return S and EXP such that S * 2^EXP = X and .5 <= S < 1.
5456 For example, 1.1 = 0.55 * 2^1. */
5460 const UEMUSHORT x[];
5468 /* Handle denormalized numbers properly using long integer exponent. */
5469 li = (EMULONG) ((EMUSHORT) xi[1]);
5477 *exp = (int) (li - 0x3ffe);
5481 /* Return e type Y = X * 2^PWR2. */
5485 const UEMUSHORT x[];
5497 emdnorm (xi, i, i, li, !ROUND_TOWARDS_ZERO);
5503 /* C = remainder after dividing B by A, all e type values.
5504 Least significant integer quotient bits left in EQUOT. */
5508 const UEMUSHORT a[], b[];
5511 UEMUSHORT den[NI], num[NI];
5515 || (ecmp (a, ezero) == 0)
5523 if (ecmp (a, ezero) == 0)
5525 mtherr ("eremain", SING);
5531 eiremain (den, num);
5532 /* Sign of remainder = sign of quotient */
5541 /* Return quotient of exploded e-types NUM / DEN in EQUOT,
5542 remainder in NUM. */
5546 UEMUSHORT den[], num[];
5552 ld -= enormlz (den);
5554 ln -= enormlz (num);
5558 if (ecmpm (den, num) <= 0)
5570 emdnorm (num, 0, 0, ln, 0);
5573 /* Report an error condition CODE encountered in function NAME.
5575 Mnemonic Value Significance
5577 DOMAIN 1 argument domain error
5578 SING 2 function singularity
5579 OVERFLOW 3 overflow range error
5580 UNDERFLOW 4 underflow range error
5581 TLOSS 5 total loss of precision
5582 PLOSS 6 partial loss of precision
5583 INVALID 7 NaN - producing operation
5584 EDOM 33 Unix domain error code
5585 ERANGE 34 Unix range error code
5587 The order of appearance of the following messages is bound to the
5588 error codes defined above. */
5598 /* The string passed by the calling program is supposed to be the
5599 name of the function in which the error occurred.
5600 The code argument selects which error message string will be printed. */
5602 if (strcmp (name, "esub") == 0)
5603 name = "subtraction";
5604 else if (strcmp (name, "ediv") == 0)
5606 else if (strcmp (name, "emul") == 0)
5607 name = "multiplication";
5608 else if (strcmp (name, "enormlz") == 0)
5609 name = "normalization";
5610 else if (strcmp (name, "etoasc") == 0)
5611 name = "conversion to text";
5612 else if (strcmp (name, "asctoe") == 0)
5614 else if (strcmp (name, "eremain") == 0)
5616 else if (strcmp (name, "esqrt") == 0)
5617 name = "square root";
5622 case DOMAIN: warning ("%s: argument domain error" , name); break;
5623 case SING: warning ("%s: function singularity" , name); break;
5624 case OVERFLOW: warning ("%s: overflow range error" , name); break;
5625 case UNDERFLOW: warning ("%s: underflow range error" , name); break;
5626 case TLOSS: warning ("%s: total loss of precision" , name); break;
5627 case PLOSS: warning ("%s: partial loss of precision", name); break;
5628 case INVALID: warning ("%s: NaN - producing operation", name); break;
5633 /* Set global error message word */
5638 /* Convert DEC double precision D to e type E. */
5646 ieeetoe (d, e, &dec_g);
5648 ieeetoe (d, e, &dec_d);
5651 /* Convert e type X to DEC double precision D. */
5661 const struct ieee_format *fmt;
5669 /* Adjust exponent for offsets. */
5670 exp = (EMULONG) xi[E] - fmt->adjustment;
5671 /* Round off to nearest or even. */
5673 rndprc = fmt->precision;
5674 emdnorm (xi, 0, 0, exp, !ROUND_TOWARDS_ZERO);
5679 /* Convert exploded e-type X, that has already been rounded to
5680 56-bit precision, to DEC format double Y. */
5687 toieee (x, y, &dec_g);
5689 toieee (x, y, &dec_d);
5694 /* Convert IBM single/double precision to e type. */
5700 enum machine_mode mode;
5705 ecleaz (y); /* start with a zero */
5706 p = y; /* point to our number */
5707 r = *d; /* get IBM exponent word */
5708 if (*d & (unsigned int) 0x8000)
5709 *p = 0xffff; /* fill in our sign */
5710 ++p; /* bump pointer to our exponent word */
5711 r &= 0x7f00; /* strip the sign bit */
5712 r >>= 6; /* shift exponent word down 6 bits */
5713 /* in fact shift by 8 right and 2 left */
5714 r += EXONE - (0x41 << 2); /* subtract IBM exponent offset */
5715 /* add our e type exponent offset */
5716 *p++ = r; /* to form our exponent */
5718 *p++ = *d++ & 0xff; /* now do the high order mantissa */
5719 /* strip off the IBM exponent and sign bits */
5720 if (mode != SFmode) /* there are only 2 words in SFmode */
5722 *p++ = *d++; /* fill in the rest of our mantissa */
5727 if (y[M] == 0 && y[M+1] == 0 && y[M+2] == 0 && y[M+3] == 0)
5730 y[E] -= 5 + enormlz (y); /* now normalise the mantissa */
5731 /* handle change in RADIX */
5737 /* Convert e type to IBM single/double precision. */
5743 enum machine_mode mode;
5750 exp = (EMULONG) xi[E] - (EXONE - (0x41 << 2)); /* adjust exponent for offsets */
5751 /* round off to nearest or even */
5754 emdnorm (xi, 0, 0, exp, !ROUND_TOWARDS_ZERO);
5756 toibm (xi, d, mode);
5762 enum machine_mode mode;
5815 /* Convert C4X single/double precision to e type. */
5821 enum machine_mode mode;
5839 /* Short-circuit the zero case. */
5840 if ((dn[0] == 0x8000)
5841 && (dn[1] == 0x0000)
5842 && ((mode == QFmode) || ((dn[2] == 0x0000) && (dn[3] == 0x0000))))
5853 ecleaz (y); /* start with a zero */
5854 r = dn[0]; /* get sign/exponent part */
5855 if (r & (unsigned int) 0x0080)
5857 y[0] = 0xffff; /* fill in our sign */
5863 r >>= 8; /* Shift exponent word down 8 bits. */
5864 if (r & 0x80) /* Make the exponent negative if it is. */
5865 r = r | (~0 & ~0xff);
5869 /* Now do the high order mantissa. We don't "or" on the high bit
5870 because it is 2 (not 1) and is handled a little differently
5872 y[M] = dn[0] & 0x7f;
5875 if (mode != QFmode) /* There are only 2 words in QFmode. */
5877 y[M+2] = dn[2]; /* Fill in the rest of our mantissa. */
5885 /* Now do the two's complement on the data. */
5887 carry = 1; /* Initially add 1 for the two's complement. */
5888 for (i=size + M; i > M; i--)
5890 if (carry && (y[i] == 0x0000))
5891 /* We overflowed into the next word, carry is the same. */
5892 y[i] = carry ? 0x0000 : 0xffff;
5895 /* No overflow, just invert and add carry. */
5896 y[i] = ((~y[i]) + carry) & 0xffff;
5911 /* Add our e type exponent offset to form our exponent. */
5915 /* Now do the high order mantissa strip off the exponent and sign
5916 bits and add the high 1 bit. */
5917 y[M] = (dn[0] & 0x7f) | 0x80;
5920 if (mode != QFmode) /* There are only 2 words in QFmode. */
5922 y[M+2] = dn[2]; /* Fill in the rest of our mantissa. */
5932 /* Convert e type to C4X single/double precision. */
5938 enum machine_mode mode;
5946 /* Adjust exponent for offsets. */
5947 exp = (EMULONG) xi[E] - (EXONE - 0x7f);
5949 /* Round off to nearest or even. */
5951 rndprc = mode == QFmode ? 24 : 32;
5952 emdnorm (xi, 0, 0, exp, !ROUND_TOWARDS_ZERO);
5954 toc4x (xi, d, mode);
5960 enum machine_mode mode;
5966 /* Short-circuit the zero case */
5967 if ((x[0] == 0) /* Zero exponent and sign */
5969 && (x[M] == 0) /* The rest is for zero mantissa */
5971 /* Only check for double if necessary */
5972 && ((mode == QFmode) || ((x[M+2] == 0) && (x[M+3] == 0))))
5974 /* We have a zero. Put it into the output and return. */
5987 /* Negative number require a two's complement conversion of the
5993 i = ((int) x[1]) - 0x7f;
5995 /* Now add 1 to the inverted data to do the two's complement. */
6004 x[v] = carry ? 0x0000 : 0xffff;
6007 x[v] = ((~x[v]) + carry) & 0xffff;
6013 /* The following is a special case. The C4X negative float requires
6014 a zero in the high bit (because the format is (2 - x) x 2^m), so
6015 if a one is in that bit, we have to shift left one to get rid
6016 of it. This only occurs if the number is -1 x 2^m. */
6017 if (x[M+1] & 0x8000)
6019 /* This is the case of -1 x 2^m, we have to rid ourselves of the
6020 high sign bit and shift the exponent. */
6026 i = ((int) x[1]) - 0x7f;
6028 if ((i < -128) || (i > 127))
6036 y[3] = (y[1] << 8) | ((y[2] >> 8) & 0xff);
6037 y[2] = (y[0] << 8) | ((y[1] >> 8) & 0xff);
6045 y[0] |= ((i & 0xff) << 8);
6049 y[0] |= x[M] & 0x7f;
6055 y[3] = (y[1] << 8) | ((y[2] >> 8) & 0xff);
6056 y[2] = (y[0] << 8) | ((y[1] >> 8) & 0xff);
6061 /* Output a binary NaN bit pattern in the target machine's format. */
6063 /* If special NaN bit patterns are required, define them in tm.h
6064 as arrays of unsigned 16-bit shorts. Otherwise, use the default
6069 #if defined (IEEE) && (INTEL_EXTENDED_IEEE_FORMAT == 0)
6070 static const UEMUSHORT TFbignan[8] =
6071 {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
6072 static const UEMUSHORT TFlittlenan[8] = {0, 0, 0, 0, 0, 0, 0x8000, 0xffff};
6080 static const UEMUSHORT XFbignan[6] =
6081 {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
6082 static const UEMUSHORT XFlittlenan[6] = {0, 0, 0, 0xc000, 0xffff, 0};
6090 static const UEMUSHORT DFbignan[4] = {0x7fff, 0xffff, 0xffff, 0xffff};
6091 static const UEMUSHORT DFlittlenan[4] = {0, 0, 0, 0xfff8};
6099 static const UEMUSHORT SFbignan[2] = {0x7fff, 0xffff};
6100 static const UEMUSHORT SFlittlenan[2] = {0, 0xffc0};
6107 make_nan (nan, sign, mode)
6110 enum machine_mode mode;
6116 size = GET_MODE_BITSIZE (mode);
6117 if (LARGEST_EXPONENT_IS_NORMAL (size))
6119 warning ("%d-bit floats cannot hold NaNs", size);
6120 saturate (nan, sign, size, 0);
6125 /* Possibly the `reserved operand' patterns on a VAX can be
6126 used like NaN's, but probably not in the same way as IEEE. */
6127 #if !defined(DEC) && !defined(IBM) && !defined(C4X)
6129 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
6131 if (REAL_WORDS_BIG_ENDIAN)
6141 if (REAL_WORDS_BIG_ENDIAN)
6149 if (REAL_WORDS_BIG_ENDIAN)
6158 if (REAL_WORDS_BIG_ENDIAN)
6168 if (REAL_WORDS_BIG_ENDIAN)
6169 *nan++ = (sign << 15) | (*p++ & 0x7fff);
6172 if (! REAL_WORDS_BIG_ENDIAN)
6173 *nan = (sign << 15) | (*p & 0x7fff);
6178 /* Create a saturation value for a SIZE-bit float, assuming that
6179 LARGEST_EXPONENT_IS_NORMAL (SIZE).
6181 If SIGN is true, fill X with the most negative value, otherwise fill
6182 it with the most positive value. WARN is true if the function should
6183 warn about overflow. */
6186 saturate (x, sign, size, warn)
6188 int sign, size, warn;
6192 if (warn && extra_warnings)
6193 warning ("value exceeds the range of a %d-bit float", size);
6195 /* Create the most negative value. */
6196 for (i = 0; i < size / EMUSHORT_SIZE; i++)
6199 /* Make it positive, if necessary. */
6201 x[REAL_WORDS_BIG_ENDIAN? 0 : i - 1] = 0x7fff;
6205 /* This is the inverse of the function `etarsingle' invoked by
6206 REAL_VALUE_TO_TARGET_SINGLE. */
6209 ereal_unto_float (f)
6216 /* Convert 32 bit integer to array of 16 bit pieces in target machine order.
6217 This is the inverse operation to what the function `endian' does. */
6218 if (REAL_WORDS_BIG_ENDIAN)
6220 s[0] = (UEMUSHORT) (f >> 16);
6221 s[1] = (UEMUSHORT) f;
6225 s[0] = (UEMUSHORT) f;
6226 s[1] = (UEMUSHORT) (f >> 16);
6228 /* Convert and promote the target float to E-type. */
6230 /* Output E-type to REAL_VALUE_TYPE. */
6236 /* This is the inverse of the function `etardouble' invoked by
6237 REAL_VALUE_TO_TARGET_DOUBLE. */
6240 ereal_unto_double (d)
6247 /* Convert array of HOST_WIDE_INT to equivalent array of 16-bit pieces. */
6248 if (REAL_WORDS_BIG_ENDIAN)
6250 s[0] = (UEMUSHORT) (d[0] >> 16);
6251 s[1] = (UEMUSHORT) d[0];
6252 s[2] = (UEMUSHORT) (d[1] >> 16);
6253 s[3] = (UEMUSHORT) d[1];
6257 /* Target float words are little-endian. */
6258 s[0] = (UEMUSHORT) d[0];
6259 s[1] = (UEMUSHORT) (d[0] >> 16);
6260 s[2] = (UEMUSHORT) d[1];
6261 s[3] = (UEMUSHORT) (d[1] >> 16);
6263 /* Convert target double to E-type. */
6265 /* Output E-type to REAL_VALUE_TYPE. */
6271 /* Convert an SFmode target `float' value to a REAL_VALUE_TYPE.
6272 This is somewhat like ereal_unto_float, but the input types
6273 for these are different. */
6276 ereal_from_float (f)
6283 /* Convert 32 bit integer to array of 16 bit pieces in target machine order.
6284 This is the inverse operation to what the function `endian' does. */
6285 if (REAL_WORDS_BIG_ENDIAN)
6287 s[0] = (UEMUSHORT) (f >> 16);
6288 s[1] = (UEMUSHORT) f;
6292 s[0] = (UEMUSHORT) f;
6293 s[1] = (UEMUSHORT) (f >> 16);
6295 /* Convert and promote the target float to E-type. */
6297 /* Output E-type to REAL_VALUE_TYPE. */
6303 /* Convert a DFmode target `double' value to a REAL_VALUE_TYPE.
6304 This is somewhat like ereal_unto_double, but the input types
6305 for these are different.
6307 The DFmode is stored as an array of HOST_WIDE_INT in the target's
6308 data format, with no holes in the bit packing. The first element
6309 of the input array holds the bits that would come first in the
6310 target computer's memory. */
6313 ereal_from_double (d)
6320 /* Convert array of HOST_WIDE_INT to equivalent array of 16-bit pieces. */
6321 if (REAL_WORDS_BIG_ENDIAN)
6323 #if HOST_BITS_PER_WIDE_INT == 32
6324 s[0] = (UEMUSHORT) (d[0] >> 16);
6325 s[1] = (UEMUSHORT) d[0];
6326 s[2] = (UEMUSHORT) (d[1] >> 16);
6327 s[3] = (UEMUSHORT) d[1];
6329 /* In this case the entire target double is contained in the
6330 first array element. The second element of the input is
6332 s[0] = (UEMUSHORT) (d[0] >> 48);
6333 s[1] = (UEMUSHORT) (d[0] >> 32);
6334 s[2] = (UEMUSHORT) (d[0] >> 16);
6335 s[3] = (UEMUSHORT) d[0];
6340 /* Target float words are little-endian. */
6341 s[0] = (UEMUSHORT) d[0];
6342 s[1] = (UEMUSHORT) (d[0] >> 16);
6343 #if HOST_BITS_PER_WIDE_INT == 32
6344 s[2] = (UEMUSHORT) d[1];
6345 s[3] = (UEMUSHORT) (d[1] >> 16);
6347 s[2] = (UEMUSHORT) (d[0] >> 32);
6348 s[3] = (UEMUSHORT) (d[0] >> 48);
6351 /* Convert target double to E-type. */
6353 /* Output E-type to REAL_VALUE_TYPE. */
6360 /* Convert target computer unsigned 64-bit integer to e-type.
6361 The endian-ness of DImode follows the convention for integers,
6362 so we use WORDS_BIG_ENDIAN here, not REAL_WORDS_BIG_ENDIAN. */
6366 const UEMUSHORT *di; /* Address of the 64-bit int. */
6373 if (WORDS_BIG_ENDIAN)
6375 for (k = M; k < M + 4; k++)
6380 for (k = M + 3; k >= M; k--)
6383 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
6384 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
6385 ecleaz (yi); /* it was zero */
6387 yi[E] -= (UEMUSHORT) k;/* subtract shift count from exponent */
6391 /* Convert target computer signed 64-bit integer to e-type. */
6395 const UEMUSHORT *di; /* Address of the 64-bit int. */
6398 unsigned EMULONG acc;
6404 if (WORDS_BIG_ENDIAN)
6406 for (k = M; k < M + 4; k++)
6411 for (k = M + 3; k >= M; k--)
6414 /* Take absolute value */
6420 for (k = M + 3; k >= M; k--)
6422 acc = (unsigned EMULONG) (~yi[k] & 0xffff) + carry;
6429 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
6430 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
6431 ecleaz (yi); /* it was zero */
6433 yi[E] -= (UEMUSHORT) k;/* subtract shift count from exponent */
6440 /* Convert e-type to unsigned 64-bit int. */
6456 k = (int) xi[E] - (EXONE - 1);
6459 for (j = 0; j < 4; j++)
6465 for (j = 0; j < 4; j++)
6468 warning ("overflow on truncation to integer");
6473 /* Shift more than 16 bits: first shift up k-16 mod 16,
6474 then shift up by 16's. */
6475 j = k - ((k >> 4) << 4);
6479 if (WORDS_BIG_ENDIAN)
6490 if (WORDS_BIG_ENDIAN)
6495 while ((k -= 16) > 0);
6499 /* shift not more than 16 bits */
6504 if (WORDS_BIG_ENDIAN)
6523 /* Convert e-type to signed 64-bit int. */
6530 unsigned EMULONG acc;
6537 k = (int) xi[E] - (EXONE - 1);
6540 for (j = 0; j < 4; j++)
6546 for (j = 0; j < 4; j++)
6549 warning ("overflow on truncation to integer");
6555 /* Shift more than 16 bits: first shift up k-16 mod 16,
6556 then shift up by 16's. */
6557 j = k - ((k >> 4) << 4);
6561 if (WORDS_BIG_ENDIAN)
6572 if (WORDS_BIG_ENDIAN)
6577 while ((k -= 16) > 0);
6581 /* shift not more than 16 bits */
6584 if (WORDS_BIG_ENDIAN)
6600 /* Negate if negative */
6604 if (WORDS_BIG_ENDIAN)
6606 for (k = 0; k < 4; k++)
6608 acc = (unsigned EMULONG) (~(*isave) & 0xffff) + carry;
6609 if (WORDS_BIG_ENDIAN)
6621 /* Longhand square root routine. */
6624 static int esqinited = 0;
6625 static unsigned short sqrndbit[NI];
6632 UEMUSHORT temp[NI], num[NI], sq[NI], xx[NI];
6634 int i, j, k, n, nlups;
6639 sqrndbit[NI - 2] = 1;
6642 /* Check for arg <= 0 */
6643 i = ecmp (x, ezero);
6648 mtherr ("esqrt", DOMAIN);
6664 /* Bring in the arg and renormalize if it is denormal. */
6666 m = (EMULONG) xx[1]; /* local long word exponent */
6670 /* Divide exponent by 2 */
6672 exp = (unsigned short) ((m / 2) + 0x3ffe);
6674 /* Adjust if exponent odd */
6684 n = 8; /* get 8 bits of result per inner loop */
6690 /* bring in next word of arg */
6692 num[NI - 1] = xx[j + 3];
6693 /* Do additional bit on last outer loop, for roundoff. */
6696 for (i = 0; i < n; i++)
6698 /* Next 2 bits of arg */
6701 /* Shift up answer */
6703 /* Make trial divisor */
6704 for (k = 0; k < NI; k++)
6707 eaddm (sqrndbit, temp);
6708 /* Subtract and insert answer bit if it goes in */
6709 if (ecmpm (temp, num) <= 0)
6719 /* Adjust for extra, roundoff loop done. */
6720 exp += (NBITS - 1) - rndprc;
6722 /* Sticky bit = 1 if the remainder is nonzero. */
6724 for (i = 3; i < NI; i++)
6727 /* Renormalize and round off. */
6728 emdnorm (sq, k, 0, exp, !ROUND_TOWARDS_ZERO);
6733 /* Return the binary precision of the significand for a given
6734 floating point mode. The mode can hold an integer value
6735 that many bits wide, without losing any bits. */
6738 significand_size (mode)
6739 enum machine_mode mode;
6742 /* Don't test the modes, but their sizes, lest this
6743 code won't work for BITS_PER_UNIT != 8 . */
6745 switch (GET_MODE_BITSIZE (mode))
6766 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)