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
30 /* To enable support of XFmode extended real floating point, define
31 LONG_DOUBLE_TYPE_SIZE 96 in the tm.h file (m68k.h or i386.h).
33 Machine files (tm.h etc) must not contain any code
34 that tries to use host floating point arithmetic to convert
35 REAL_VALUE_TYPEs from `double' to `float', pass them to fprintf,
36 etc. In cross-compile situations a REAL_VALUE_TYPE may not
37 be intelligible to the host computer's native arithmetic.
39 The first part of this file interfaces gcc to a floating point
40 arithmetic suite that was not written with gcc in mind. Avoid
41 changing the low-level arithmetic routines unless you have suitable
42 test programs available. A special version of the PARANOIA floating
43 point arithmetic tester, modified for this purpose, can be found on
44 usc.edu: /pub/C-numanal/ieeetest.zoo. Other tests, and libraries of
45 XFmode and TFmode transcendental functions, can be obtained by ftp from
46 netlib.att.com: netlib/cephes. */
48 /* Type of computer arithmetic.
49 Only one of DEC, IBM, IEEE, C4X, or UNK should get defined.
51 `IEEE', when REAL_WORDS_BIG_ENDIAN is non-zero, refers generically
52 to big-endian IEEE floating-point data structure. This definition
53 should work in SFmode `float' type and DFmode `double' type on
54 virtually all big-endian IEEE machines. If LONG_DOUBLE_TYPE_SIZE
55 has been defined to be 96, then IEEE also invokes the particular
56 XFmode (`long double' type) data structure used by the Motorola
57 680x0 series processors.
59 `IEEE', when REAL_WORDS_BIG_ENDIAN is zero, refers generally to
60 little-endian IEEE machines. In this case, if LONG_DOUBLE_TYPE_SIZE
61 has been defined to be 96, then IEEE also invokes the particular
62 XFmode `long double' data structure used by the Intel 80x86 series
65 `DEC' refers specifically to the Digital Equipment Corp PDP-11
66 and VAX floating point data structure. This model currently
67 supports no type wider than DFmode.
69 `IBM' refers specifically to the IBM System/370 and compatible
70 floating point data structure. This model currently supports
71 no type wider than DFmode. The IBM conversions were contributed by
72 frank@atom.ansto.gov.au (Frank Crawford).
74 `C4X' refers specifically to the floating point format used on
75 Texas Instruments TMS320C3x and TMS320C4x digital signal
76 processors. This supports QFmode (32-bit float, double) and HFmode
77 (40-bit long double) where BITS_PER_BYTE is 32. Unlike IEEE
78 floats, C4x floats are not rounded to be even. The C4x conversions
79 were contributed by m.hayes@elec.canterbury.ac.nz (Michael Hayes) and
80 Haj.Ten.Brugge@net.HCC.nl (Herman ten Brugge).
82 If LONG_DOUBLE_TYPE_SIZE = 64 (the default, unless tm.h defines it)
83 then `long double' and `double' are both implemented, but they
86 The case LONG_DOUBLE_TYPE_SIZE = 128 activates TFmode support
87 and may deactivate XFmode since `long double' is used to refer
88 to both modes. Defining INTEL_EXTENDED_IEEE_FORMAT to non-zero
89 at the same time enables 80387-style 80-bit floats in a 128-bit
90 padded image, as seen on IA-64.
92 The macros FLOAT_WORDS_BIG_ENDIAN, HOST_FLOAT_WORDS_BIG_ENDIAN,
93 contributed by Richard Earnshaw <Richard.Earnshaw@cl.cam.ac.uk>,
94 separate the floating point unit's endian-ness from that of
95 the integer addressing. This permits one to define a big-endian
96 FPU on a little-endian machine (e.g., ARM). An extension to
97 BYTES_BIG_ENDIAN may be required for some machines in the future.
98 These optional macros may be defined in tm.h. In real.h, they
99 default to WORDS_BIG_ENDIAN, etc., so there is no need to define
100 them for any normal host or target machine on which the floats
101 and the integers have the same endian-ness. */
104 /* The following converts gcc macros into the ones used by this file. */
106 #if TARGET_FLOAT_FORMAT == VAX_FLOAT_FORMAT
107 /* PDP-11, Pro350, VAX: */
109 #else /* it's not VAX */
110 #if TARGET_FLOAT_FORMAT == IBM_FLOAT_FORMAT
111 /* IBM System/370 style */
113 #else /* it's also not an IBM */
114 #if TARGET_FLOAT_FORMAT == C4X_FLOAT_FORMAT
115 /* TMS320C3x/C4x style */
117 #else /* it's also not a C4X */
118 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
120 #else /* it's not IEEE either */
121 /* UNKnown arithmetic. We don't support this and can't go on. */
122 unknown arithmetic type
124 #endif /* not IEEE */
129 #define REAL_WORDS_BIG_ENDIAN FLOAT_WORDS_BIG_ENDIAN
131 /* Define INFINITY for support of infinity.
132 Define NANS for support of Not-a-Number's (NaN's). */
133 #if !defined(DEC) && !defined(IBM) && !defined(C4X)
138 /* Support of NaNs requires support of infinity. */
145 /* Find a host integer type that is at least 16 bits wide,
146 and another type at least twice whatever that size is. */
148 #if HOST_BITS_PER_CHAR >= 16
149 #define EMUSHORT char
150 #define EMUSHORT_SIZE HOST_BITS_PER_CHAR
151 #define EMULONG_SIZE (2 * HOST_BITS_PER_CHAR)
153 #if HOST_BITS_PER_SHORT >= 16
154 #define EMUSHORT short
155 #define EMUSHORT_SIZE HOST_BITS_PER_SHORT
156 #define EMULONG_SIZE (2 * HOST_BITS_PER_SHORT)
158 #if HOST_BITS_PER_INT >= 16
160 #define EMUSHORT_SIZE HOST_BITS_PER_INT
161 #define EMULONG_SIZE (2 * HOST_BITS_PER_INT)
163 #if HOST_BITS_PER_LONG >= 16
164 #define EMUSHORT long
165 #define EMUSHORT_SIZE HOST_BITS_PER_LONG
166 #define EMULONG_SIZE (2 * HOST_BITS_PER_LONG)
168 /* You will have to modify this program to have a smaller unit size. */
169 #define EMU_NON_COMPILE
175 /* If no 16-bit type has been found and the compiler is GCC, try HImode. */
176 #if defined(__GNUC__) && EMUSHORT_SIZE != 16
177 typedef int HItype __attribute__ ((mode (HI)));
178 typedef unsigned int UHItype __attribute__ ((mode (HI)));
182 #define EMUSHORT HItype
183 #define UEMUSHORT UHItype
184 #define EMUSHORT_SIZE 16
185 #define EMULONG_SIZE 32
187 #define UEMUSHORT unsigned EMUSHORT
190 #if HOST_BITS_PER_SHORT >= EMULONG_SIZE
191 #define EMULONG short
193 #if HOST_BITS_PER_INT >= EMULONG_SIZE
196 #if HOST_BITS_PER_LONG >= EMULONG_SIZE
199 #if HOST_BITS_PER_LONGLONG >= EMULONG_SIZE
200 #define EMULONG long long int
202 /* You will have to modify this program to have a smaller unit size. */
203 #define EMU_NON_COMPILE
210 /* The host interface doesn't work if no 16-bit size exists. */
211 #if EMUSHORT_SIZE != 16
212 #define EMU_NON_COMPILE
215 /* OK to continue compilation. */
216 #ifndef EMU_NON_COMPILE
218 /* Construct macros to translate between REAL_VALUE_TYPE and e type.
219 In GET_REAL and PUT_REAL, r and e are pointers.
220 A REAL_VALUE_TYPE is guaranteed to occupy contiguous locations
221 in memory, with no holes. */
223 #if MAX_LONG_DOUBLE_TYPE_SIZE == 96 || \
224 ((INTEL_EXTENDED_IEEE_FORMAT != 0) && MAX_LONG_DOUBLE_TYPE_SIZE == 128)
225 /* Number of 16 bit words in external e type format */
227 # define MAXDECEXP 4932
228 # define MINDECEXP -4956
229 # define GET_REAL(r,e) memcpy ((e), (r), 2*NE)
230 # define PUT_REAL(e,r) \
232 memcpy ((r), (e), 2*NE); \
233 if (2*NE < sizeof (*r)) \
234 memset ((char *) (r) + 2*NE, 0, sizeof (*r) - 2*NE); \
236 # else /* no XFmode */
237 # if MAX_LONG_DOUBLE_TYPE_SIZE == 128
239 # define MAXDECEXP 4932
240 # define MINDECEXP -4977
241 # define GET_REAL(r,e) memcpy ((e), (r), 2*NE)
242 # define PUT_REAL(e,r) \
244 memcpy ((r), (e), 2*NE); \
245 if (2*NE < sizeof (*r)) \
246 memset ((char *) (r) + 2*NE, 0, sizeof (*r) - 2*NE); \
250 #define MAXDECEXP 4932
251 #define MINDECEXP -4956
252 /* Emulator uses target format internally
253 but host stores it in host endian-ness. */
255 #define GET_REAL(r,e) \
257 if (HOST_FLOAT_WORDS_BIG_ENDIAN == REAL_WORDS_BIG_ENDIAN) \
258 e53toe ((const UEMUSHORT *) (r), (e)); \
262 memcpy (&w[3], ((const EMUSHORT *) r), sizeof (EMUSHORT)); \
263 memcpy (&w[2], ((const EMUSHORT *) r) + 1, sizeof (EMUSHORT)); \
264 memcpy (&w[1], ((const EMUSHORT *) r) + 2, sizeof (EMUSHORT)); \
265 memcpy (&w[0], ((const EMUSHORT *) r) + 3, sizeof (EMUSHORT)); \
270 #define PUT_REAL(e,r) \
272 if (HOST_FLOAT_WORDS_BIG_ENDIAN == REAL_WORDS_BIG_ENDIAN) \
273 etoe53 ((e), (UEMUSHORT *) (r)); \
278 memcpy (((EMUSHORT *) r), &w[3], sizeof (EMUSHORT)); \
279 memcpy (((EMUSHORT *) r) + 1, &w[2], sizeof (EMUSHORT)); \
280 memcpy (((EMUSHORT *) r) + 2, &w[1], sizeof (EMUSHORT)); \
281 memcpy (((EMUSHORT *) r) + 3, &w[0], sizeof (EMUSHORT)); \
285 #endif /* not TFmode */
286 #endif /* not XFmode */
289 /* Number of 16 bit words in internal format */
292 /* Array offset to exponent */
295 /* Array offset to high guard word */
298 /* Number of bits of precision */
299 #define NBITS ((NI-4)*16)
301 /* Maximum number of decimal digits in ASCII conversion
304 #define NDEC (NBITS*8/27)
306 /* The exponent of 1.0 */
307 #define EXONE (0x3fff)
309 #if defined(HOST_EBCDIC)
310 /* bit 8 is significant in EBCDIC */
311 #define CHARMASK 0xff
313 #define CHARMASK 0x7f
316 extern int extra_warnings;
317 extern const UEMUSHORT ezero[NE], ehalf[NE], eone[NE], etwo[NE];
318 extern const UEMUSHORT elog2[NE], esqrt2[NE];
320 static void endian PARAMS ((const UEMUSHORT *, long *,
322 static void eclear PARAMS ((UEMUSHORT *));
323 static void emov PARAMS ((const UEMUSHORT *, UEMUSHORT *));
325 static void eabs PARAMS ((UEMUSHORT *));
327 static void eneg PARAMS ((UEMUSHORT *));
328 static int eisneg PARAMS ((const UEMUSHORT *));
329 static int eisinf PARAMS ((const UEMUSHORT *));
330 static int eisnan PARAMS ((const UEMUSHORT *));
331 static void einfin PARAMS ((UEMUSHORT *));
333 static void enan PARAMS ((UEMUSHORT *, int));
334 static void einan PARAMS ((UEMUSHORT *));
335 static int eiisnan PARAMS ((const UEMUSHORT *));
336 static void make_nan PARAMS ((UEMUSHORT *, int, enum machine_mode));
338 static int eiisneg PARAMS ((const UEMUSHORT *));
339 static void saturate PARAMS ((UEMUSHORT *, int, int, int));
340 static void emovi PARAMS ((const UEMUSHORT *, UEMUSHORT *));
341 static void emovo PARAMS ((const UEMUSHORT *, UEMUSHORT *));
342 static void ecleaz PARAMS ((UEMUSHORT *));
343 static void ecleazs PARAMS ((UEMUSHORT *));
344 static void emovz PARAMS ((const UEMUSHORT *, UEMUSHORT *));
346 static void eiinfin PARAMS ((UEMUSHORT *));
349 static int eiisinf PARAMS ((const UEMUSHORT *));
351 static int ecmpm PARAMS ((const UEMUSHORT *, const UEMUSHORT *));
352 static void eshdn1 PARAMS ((UEMUSHORT *));
353 static void eshup1 PARAMS ((UEMUSHORT *));
354 static void eshdn8 PARAMS ((UEMUSHORT *));
355 static void eshup8 PARAMS ((UEMUSHORT *));
356 static void eshup6 PARAMS ((UEMUSHORT *));
357 static void eshdn6 PARAMS ((UEMUSHORT *));
358 static void eaddm PARAMS ((const UEMUSHORT *, UEMUSHORT *));
\f
359 static void esubm PARAMS ((const UEMUSHORT *, UEMUSHORT *));
360 static void m16m PARAMS ((unsigned int, const UEMUSHORT *, UEMUSHORT *));
361 static int edivm PARAMS ((const UEMUSHORT *, UEMUSHORT *));
362 static int emulm PARAMS ((const UEMUSHORT *, UEMUSHORT *));
363 static void emdnorm PARAMS ((UEMUSHORT *, int, int, EMULONG, int));
364 static void esub PARAMS ((const UEMUSHORT *, const UEMUSHORT *,
366 static void eadd PARAMS ((const UEMUSHORT *, const UEMUSHORT *,
368 static void eadd1 PARAMS ((const UEMUSHORT *, const UEMUSHORT *,
370 static void ediv PARAMS ((const UEMUSHORT *, const UEMUSHORT *,
372 static void emul PARAMS ((const UEMUSHORT *, const UEMUSHORT *,
374 static void e53toe PARAMS ((const UEMUSHORT *, UEMUSHORT *));
375 static void e64toe PARAMS ((const UEMUSHORT *, UEMUSHORT *));
376 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
377 static void e113toe PARAMS ((const UEMUSHORT *, UEMUSHORT *));
379 static void e24toe PARAMS ((const UEMUSHORT *, UEMUSHORT *));
380 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
381 static void etoe113 PARAMS ((const UEMUSHORT *, UEMUSHORT *));
382 static void toe113 PARAMS ((UEMUSHORT *, UEMUSHORT *));
384 static void etoe64 PARAMS ((const UEMUSHORT *, UEMUSHORT *));
385 static void toe64 PARAMS ((UEMUSHORT *, UEMUSHORT *));
386 static void etoe53 PARAMS ((const UEMUSHORT *, UEMUSHORT *));
387 static void toe53 PARAMS ((UEMUSHORT *, UEMUSHORT *));
388 static void etoe24 PARAMS ((const UEMUSHORT *, UEMUSHORT *));
389 static void toe24 PARAMS ((UEMUSHORT *, UEMUSHORT *));
390 static int ecmp PARAMS ((const UEMUSHORT *, const UEMUSHORT *));
392 static void eround PARAMS ((const UEMUSHORT *, UEMUSHORT *));
394 static void ltoe PARAMS ((const HOST_WIDE_INT *, UEMUSHORT *));
395 static void ultoe PARAMS ((const unsigned HOST_WIDE_INT *, UEMUSHORT *));
396 static void eifrac PARAMS ((const UEMUSHORT *, HOST_WIDE_INT *,
398 static void euifrac PARAMS ((const UEMUSHORT *, unsigned HOST_WIDE_INT *,
400 static int eshift PARAMS ((UEMUSHORT *, int));
401 static int enormlz PARAMS ((UEMUSHORT *));
403 static void e24toasc PARAMS ((const UEMUSHORT *, char *, int));
404 static void e53toasc PARAMS ((const UEMUSHORT *, char *, int));
405 static void e64toasc PARAMS ((const UEMUSHORT *, char *, int));
406 static void e113toasc PARAMS ((const UEMUSHORT *, char *, int));
408 static void etoasc PARAMS ((const UEMUSHORT *, char *, int));
409 static void asctoe24 PARAMS ((const char *, UEMUSHORT *));
410 static void asctoe53 PARAMS ((const char *, UEMUSHORT *));
411 static void asctoe64 PARAMS ((const char *, UEMUSHORT *));
412 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
413 static void asctoe113 PARAMS ((const char *, UEMUSHORT *));
415 static void asctoe PARAMS ((const char *, UEMUSHORT *));
416 static void asctoeg PARAMS ((const char *, UEMUSHORT *, int));
417 static void efloor PARAMS ((const UEMUSHORT *, UEMUSHORT *));
419 static void efrexp PARAMS ((const UEMUSHORT *, int *,
422 static void eldexp PARAMS ((const UEMUSHORT *, int, UEMUSHORT *));
424 static void eremain PARAMS ((const UEMUSHORT *, const UEMUSHORT *,
427 static void eiremain PARAMS ((UEMUSHORT *, UEMUSHORT *));
428 static void mtherr PARAMS ((const char *, int));
430 static void dectoe PARAMS ((const UEMUSHORT *, UEMUSHORT *));
431 static void etodec PARAMS ((const UEMUSHORT *, UEMUSHORT *));
432 static void todec PARAMS ((UEMUSHORT *, UEMUSHORT *));
435 static void ibmtoe PARAMS ((const UEMUSHORT *, UEMUSHORT *,
437 static void etoibm PARAMS ((const UEMUSHORT *, UEMUSHORT *,
439 static void toibm PARAMS ((UEMUSHORT *, UEMUSHORT *,
443 static void c4xtoe PARAMS ((const UEMUSHORT *, UEMUSHORT *,
445 static void etoc4x PARAMS ((const UEMUSHORT *, UEMUSHORT *,
447 static void toc4x PARAMS ((UEMUSHORT *, UEMUSHORT *,
451 static void uditoe PARAMS ((const UEMUSHORT *, UEMUSHORT *));
452 static void ditoe PARAMS ((const UEMUSHORT *, UEMUSHORT *));
453 static void etoudi PARAMS ((const UEMUSHORT *, UEMUSHORT *));
454 static void etodi PARAMS ((const UEMUSHORT *, UEMUSHORT *));
455 static void esqrt PARAMS ((const UEMUSHORT *, UEMUSHORT *));
458 /* Copy 32-bit numbers obtained from array containing 16-bit numbers,
459 swapping ends if required, into output array of longs. The
460 result is normally passed to fprintf by the ASM_OUTPUT_ macros. */
466 enum machine_mode mode;
470 if (REAL_WORDS_BIG_ENDIAN)
475 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
476 /* Swap halfwords in the fourth long. */
477 th = (unsigned long) e[6] & 0xffff;
478 t = (unsigned long) e[7] & 0xffff;
487 /* Swap halfwords in the third long. */
488 th = (unsigned long) e[4] & 0xffff;
489 t = (unsigned long) e[5] & 0xffff;
495 /* Swap halfwords in the second word. */
496 th = (unsigned long) e[2] & 0xffff;
497 t = (unsigned long) e[3] & 0xffff;
504 /* Swap halfwords in the first word. */
505 th = (unsigned long) e[0] & 0xffff;
506 t = (unsigned long) e[1] & 0xffff;
517 /* Pack the output array without swapping. */
522 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
523 /* Pack the fourth long. */
524 th = (unsigned long) e[7] & 0xffff;
525 t = (unsigned long) e[6] & 0xffff;
534 /* Pack the third long.
535 Each element of the input REAL_VALUE_TYPE array has 16 useful bits
537 th = (unsigned long) e[5] & 0xffff;
538 t = (unsigned long) e[4] & 0xffff;
544 /* Pack the second long */
545 th = (unsigned long) e[3] & 0xffff;
546 t = (unsigned long) e[2] & 0xffff;
553 /* Pack the first long */
554 th = (unsigned long) e[1] & 0xffff;
555 t = (unsigned long) e[0] & 0xffff;
567 /* This is the implementation of the REAL_ARITHMETIC macro. */
570 earith (value, icode, r1, r2)
571 REAL_VALUE_TYPE *value;
576 UEMUSHORT d1[NE], d2[NE], v[NE];
582 /* Return NaN input back to the caller. */
585 PUT_REAL (d1, value);
590 PUT_REAL (d2, value);
594 code = (enum tree_code) icode;
602 esub (d2, d1, v); /* d1 - d2 */
611 if (ecmp (d2, ezero) == 0)
614 ediv (d2, d1, v); /* d1/d2 */
617 case MIN_EXPR: /* min (d1,d2) */
618 if (ecmp (d1, d2) < 0)
624 case MAX_EXPR: /* max (d1,d2) */
625 if (ecmp (d1, d2) > 0)
638 /* Truncate REAL_VALUE_TYPE toward zero to signed HOST_WIDE_INT.
639 implements REAL_VALUE_RNDZINT (x) (etrunci (x)). */
645 UEMUSHORT f[NE], g[NE];
661 /* Truncate REAL_VALUE_TYPE toward zero to unsigned HOST_WIDE_INT;
662 implements REAL_VALUE_UNSIGNED_RNDZINT (x) (etruncui (x)). */
668 UEMUSHORT f[NE], g[NE];
670 unsigned HOST_WIDE_INT l;
684 /* This is the REAL_VALUE_ATOF function. It converts a decimal or hexadecimal
685 string to binary, rounding off as indicated by the machine_mode argument.
686 Then it promotes the rounded value to REAL_VALUE_TYPE. */
693 UEMUSHORT tem[NE], e[NE];
719 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
739 /* Expansion of REAL_NEGATE. */
755 /* Round real toward zero to HOST_WIDE_INT;
756 implements REAL_VALUE_FIX (x). */
762 UEMUSHORT f[NE], g[NE];
769 warning ("conversion from NaN to int");
777 /* Round real toward zero to unsigned HOST_WIDE_INT
778 implements REAL_VALUE_UNSIGNED_FIX (x).
779 Negative input returns zero. */
781 unsigned HOST_WIDE_INT
785 UEMUSHORT f[NE], g[NE];
786 unsigned HOST_WIDE_INT l;
792 warning ("conversion from NaN to unsigned int");
801 /* REAL_VALUE_FROM_INT macro. */
804 ereal_from_int (d, i, j, mode)
807 enum machine_mode mode;
809 UEMUSHORT df[NE], dg[NE];
810 HOST_WIDE_INT low, high;
813 if (GET_MODE_CLASS (mode) != MODE_FLOAT)
820 /* complement and add 1 */
827 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
828 ultoe ((unsigned HOST_WIDE_INT *) &high, dg);
830 ultoe ((unsigned HOST_WIDE_INT *) &low, df);
835 /* A REAL_VALUE_TYPE may not be wide enough to hold the two HOST_WIDE_INTS.
836 Avoid double-rounding errors later by rounding off now from the
837 extra-wide internal format to the requested precision. */
838 switch (GET_MODE_BITSIZE (mode))
856 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
873 /* REAL_VALUE_FROM_UNSIGNED_INT macro. */
876 ereal_from_uint (d, i, j, mode)
878 unsigned HOST_WIDE_INT i, j;
879 enum machine_mode mode;
881 UEMUSHORT df[NE], dg[NE];
882 unsigned HOST_WIDE_INT low, high;
884 if (GET_MODE_CLASS (mode) != MODE_FLOAT)
888 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
894 /* A REAL_VALUE_TYPE may not be wide enough to hold the two HOST_WIDE_INTS.
895 Avoid double-rounding errors later by rounding off now from the
896 extra-wide internal format to the requested precision. */
897 switch (GET_MODE_BITSIZE (mode))
915 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
932 /* REAL_VALUE_TO_INT macro. */
935 ereal_to_int (low, high, rr)
936 HOST_WIDE_INT *low, *high;
939 UEMUSHORT d[NE], df[NE], dg[NE], dh[NE];
946 warning ("conversion from NaN to int");
952 /* convert positive value */
959 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
960 ediv (df, d, dg); /* dg = d / 2^32 is the high word */
961 euifrac (dg, (unsigned HOST_WIDE_INT *) high, dh);
962 emul (df, dh, dg); /* fractional part is the low word */
963 euifrac (dg, (unsigned HOST_WIDE_INT *) low, dh);
966 /* complement and add 1 */
976 /* REAL_VALUE_LDEXP macro. */
983 UEMUSHORT e[NE], y[NE];
996 /* Check for infinity in a REAL_VALUE_TYPE. */
1000 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
1006 return (eisinf (e));
1012 /* Check whether a REAL_VALUE_TYPE item is a NaN. */
1016 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
1022 return (eisnan (e));
1029 /* Check for a negative REAL_VALUE_TYPE number.
1030 This just checks the sign bit, so that -0 counts as negative. */
1036 return ereal_isneg (x);
1039 /* Expansion of REAL_VALUE_TRUNCATE.
1040 The result is in floating point, rounded to nearest or even. */
1043 real_value_truncate (mode, arg)
1044 enum machine_mode mode;
1045 REAL_VALUE_TYPE arg;
1047 UEMUSHORT e[NE], t[NE];
1059 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
1096 /* If an unsupported type was requested, presume that
1097 the machine files know something useful to do with
1098 the unmodified value. */
1107 /* Try to change R into its exact multiplicative inverse in machine mode
1108 MODE. Return nonzero function value if successful. */
1111 exact_real_inverse (mode, r)
1112 enum machine_mode mode;
1115 UEMUSHORT e[NE], einv[NE];
1116 REAL_VALUE_TYPE rinv;
1121 /* Test for input in range. Don't transform IEEE special values. */
1122 if (eisinf (e) || eisnan (e) || (ecmp (e, ezero) == 0))
1125 /* Test for a power of 2: all significand bits zero except the MSB.
1126 We are assuming the target has binary (or hex) arithmetic. */
1127 if (e[NE - 2] != 0x8000)
1130 for (i = 0; i < NE - 2; i++)
1136 /* Compute the inverse and truncate it to the required mode. */
1137 ediv (e, eone, einv);
1138 PUT_REAL (einv, &rinv);
1139 rinv = real_value_truncate (mode, rinv);
1141 #ifdef CHECK_FLOAT_VALUE
1142 /* This check is not redundant. It may, for example, flush
1143 a supposedly IEEE denormal value to zero. */
1145 if (CHECK_FLOAT_VALUE (mode, rinv, i))
1148 GET_REAL (&rinv, einv);
1150 /* Check the bits again, because the truncation might have
1151 generated an arbitrary saturation value on overflow. */
1152 if (einv[NE - 2] != 0x8000)
1155 for (i = 0; i < NE - 2; i++)
1161 /* Fail if the computed inverse is out of range. */
1162 if (eisinf (einv) || eisnan (einv) || (ecmp (einv, ezero) == 0))
1165 /* Output the reciprocal and return success flag. */
1170 /* Used for debugging--print the value of R in human-readable format
1179 REAL_VALUE_TO_DECIMAL (r, "%.20g", dstr);
1180 fprintf (stderr, "%s", dstr);
1184 /* The following routines convert REAL_VALUE_TYPE to the various floating
1185 point formats that are meaningful to supported computers.
1187 The results are returned in 32-bit pieces, each piece stored in a `long'.
1188 This is so they can be printed by statements like
1190 fprintf (file, "%lx, %lx", L[0], L[1]);
1192 that will work on both narrow- and wide-word host computers. */
1194 /* Convert R to a 128-bit long double precision value. The output array L
1195 contains four 32-bit pieces of the result, in the order they would appear
1206 #if INTEL_EXTENDED_IEEE_FORMAT == 0
1211 endian (e, l, TFmode);
1214 /* Convert R to a double extended precision value. The output array L
1215 contains three 32-bit pieces of the result, in the order they would
1216 appear in memory. */
1227 endian (e, l, XFmode);
1230 /* Convert R to a double precision value. The output array L contains two
1231 32-bit pieces of the result, in the order they would appear in memory. */
1242 endian (e, l, DFmode);
1245 /* Convert R to a single precision float value stored in the least-significant
1246 bits of a `long'. */
1257 endian (e, &l, SFmode);
1261 /* Convert X to a decimal ASCII string S for output to an assembly
1262 language file. Note, there is no standard way to spell infinity or
1263 a NaN, so these values may require special treatment in the tm.h
1267 ereal_to_decimal (x, s)
1277 /* Compare X and Y. Return 1 if X > Y, 0 if X == Y, -1 if X < Y,
1278 or -2 if either is a NaN. */
1282 REAL_VALUE_TYPE x, y;
1284 UEMUSHORT ex[NE], ey[NE];
1288 return (ecmp (ex, ey));
1291 /* Return 1 if the sign bit of X is set, else return 0. */
1300 return (eisneg (ex));
1305 Extended precision IEEE binary floating point arithmetic routines
1307 Numbers are stored in C language as arrays of 16-bit unsigned
1308 short integers. The arguments of the routines are pointers to
1311 External e type data structure, similar to Intel 8087 chip
1312 temporary real format but possibly with a larger significand:
1314 NE-1 significand words (least significant word first,
1315 most significant bit is normally set)
1316 exponent (value = EXONE for 1.0,
1317 top bit is the sign)
1320 Internal exploded e-type data structure of a number (a "word" is 16 bits):
1322 ei[0] sign word (0 for positive, 0xffff for negative)
1323 ei[1] biased exponent (value = EXONE for the number 1.0)
1324 ei[2] high guard word (always zero after normalization)
1326 to ei[NI-2] significand (NI-4 significand words,
1327 most significant word first,
1328 most significant bit is set)
1329 ei[NI-1] low guard word (0x8000 bit is rounding place)
1333 Routines for external format e-type numbers
1335 asctoe (string, e) ASCII string to extended double e type
1336 asctoe64 (string, &d) ASCII string to long double
1337 asctoe53 (string, &d) ASCII string to double
1338 asctoe24 (string, &f) ASCII string to single
1339 asctoeg (string, e, prec) ASCII string to specified precision
1340 e24toe (&f, e) IEEE single precision to e type
1341 e53toe (&d, e) IEEE double precision to e type
1342 e64toe (&d, e) IEEE long double precision to e type
1343 e113toe (&d, e) 128-bit long double precision to e type
1345 eabs (e) absolute value
1347 eadd (a, b, c) c = b + a
1349 ecmp (a, b) Returns 1 if a > b, 0 if a == b,
1350 -1 if a < b, -2 if either a or b is a NaN.
1351 ediv (a, b, c) c = b / a
1352 efloor (a, b) truncate to integer, toward -infinity
1353 efrexp (a, exp, s) extract exponent and significand
1354 eifrac (e, &l, frac) e to HOST_WIDE_INT and e type fraction
1355 euifrac (e, &l, frac) e to unsigned HOST_WIDE_INT and e type fraction
1356 einfin (e) set e to infinity, leaving its sign alone
1357 eldexp (a, n, b) multiply by 2**n
1359 emul (a, b, c) c = b * a
1362 eround (a, b) b = nearest integer value to a
1364 esub (a, b, c) c = b - a
1366 e24toasc (&f, str, n) single to ASCII string, n digits after decimal
1367 e53toasc (&d, str, n) double to ASCII string, n digits after decimal
1368 e64toasc (&d, str, n) 80-bit long double to ASCII string
1369 e113toasc (&d, str, n) 128-bit long double to ASCII string
1371 etoasc (e, str, n) e to ASCII string, n digits after decimal
1372 etoe24 (e, &f) convert e type to IEEE single precision
1373 etoe53 (e, &d) convert e type to IEEE double precision
1374 etoe64 (e, &d) convert e type to IEEE long double precision
1375 ltoe (&l, e) HOST_WIDE_INT to e type
1376 ultoe (&l, e) unsigned HOST_WIDE_INT to e type
1377 eisneg (e) 1 if sign bit of e != 0, else 0
1378 eisinf (e) 1 if e has maximum exponent (non-IEEE)
1379 or is infinite (IEEE)
1380 eisnan (e) 1 if e is a NaN
1383 Routines for internal format exploded e-type numbers
1385 eaddm (ai, bi) add significands, bi = bi + ai
1387 ecleazs (ei) set ei = 0 but leave its sign alone
1388 ecmpm (ai, bi) compare significands, return 1, 0, or -1
1389 edivm (ai, bi) divide significands, bi = bi / ai
1390 emdnorm (ai,l,s,exp) normalize and round off
1391 emovi (a, ai) convert external a to internal ai
1392 emovo (ai, a) convert internal ai to external a
1393 emovz (ai, bi) bi = ai, low guard word of bi = 0
1394 emulm (ai, bi) multiply significands, bi = bi * ai
1395 enormlz (ei) left-justify the significand
1396 eshdn1 (ai) shift significand and guards down 1 bit
1397 eshdn8 (ai) shift down 8 bits
1398 eshdn6 (ai) shift down 16 bits
1399 eshift (ai, n) shift ai n bits up (or down if n < 0)
1400 eshup1 (ai) shift significand and guards up 1 bit
1401 eshup8 (ai) shift up 8 bits
1402 eshup6 (ai) shift up 16 bits
1403 esubm (ai, bi) subtract significands, bi = bi - ai
1404 eiisinf (ai) 1 if infinite
1405 eiisnan (ai) 1 if a NaN
1406 eiisneg (ai) 1 if sign bit of ai != 0, else 0
1407 einan (ai) set ai = NaN
1409 eiinfin (ai) set ai = infinity
1412 The result is always normalized and rounded to NI-4 word precision
1413 after each arithmetic operation.
1415 Exception flags are NOT fully supported.
1417 Signaling NaN's are NOT supported; they are treated the same
1420 Define INFINITY for support of infinity; otherwise a
1421 saturation arithmetic is implemented.
1423 Define NANS for support of Not-a-Number items; otherwise the
1424 arithmetic will never produce a NaN output, and might be confused
1426 If NaN's are supported, the output of `ecmp (a,b)' is -2 if
1427 either a or b is a NaN. This means asking `if (ecmp (a,b) < 0)'
1428 may not be legitimate. Use `if (ecmp (a,b) == -1)' for `less than'
1431 Denormals are always supported here where appropriate (e.g., not
1432 for conversion to DEC numbers). */
1434 /* Definitions for error codes that are passed to the common error handling
1437 For Digital Equipment PDP-11 and VAX computers, certain
1438 IBM systems, and others that use numbers with a 56-bit
1439 significand, the symbol DEC should be defined. In this
1440 mode, most floating point constants are given as arrays
1441 of octal integers to eliminate decimal to binary conversion
1442 errors that might be introduced by the compiler.
1444 For computers, such as IBM PC, that follow the IEEE
1445 Standard for Binary Floating Point Arithmetic (ANSI/IEEE
1446 Std 754-1985), the symbol IEEE should be defined.
1447 These numbers have 53-bit significands. In this mode, constants
1448 are provided as arrays of hexadecimal 16 bit integers.
1449 The endian-ness of generated values is controlled by
1450 REAL_WORDS_BIG_ENDIAN.
1452 To accommodate other types of computer arithmetic, all
1453 constants are also provided in a normal decimal radix
1454 which one can hope are correctly converted to a suitable
1455 format by the available C language compiler. To invoke
1456 this mode, the symbol UNK is defined.
1458 An important difference among these modes is a predefined
1459 set of machine arithmetic constants for each. The numbers
1460 MACHEP (the machine roundoff error), MAXNUM (largest number
1461 represented), and several other parameters are preset by
1462 the configuration symbol. Check the file const.c to
1463 ensure that these values are correct for your computer.
1465 For ANSI C compatibility, define ANSIC equal to 1. Currently
1466 this affects only the atan2 function and others that use it. */
1468 /* Constant definitions for math error conditions. */
1470 #define DOMAIN 1 /* argument domain error */
1471 #define SING 2 /* argument singularity */
1472 #define OVERFLOW 3 /* overflow range error */
1473 #define UNDERFLOW 4 /* underflow range error */
1474 #define TLOSS 5 /* total loss of precision */
1475 #define PLOSS 6 /* partial loss of precision */
1476 #define INVALID 7 /* NaN-producing operation */
1478 /* e type constants used by high precision check routines */
1480 #if MAX_LONG_DOUBLE_TYPE_SIZE == 128 && (INTEL_EXTENDED_IEEE_FORMAT == 0)
1482 const UEMUSHORT ezero[NE] =
1483 {0x0000, 0x0000, 0x0000, 0x0000,
1484 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,};
1487 const UEMUSHORT ehalf[NE] =
1488 {0x0000, 0x0000, 0x0000, 0x0000,
1489 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3ffe,};
1492 const UEMUSHORT eone[NE] =
1493 {0x0000, 0x0000, 0x0000, 0x0000,
1494 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3fff,};
1497 const UEMUSHORT etwo[NE] =
1498 {0x0000, 0x0000, 0x0000, 0x0000,
1499 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4000,};
1502 const UEMUSHORT e32[NE] =
1503 {0x0000, 0x0000, 0x0000, 0x0000,
1504 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4004,};
1506 /* 6.93147180559945309417232121458176568075500134360255E-1 */
1507 const UEMUSHORT elog2[NE] =
1508 {0x40f3, 0xf6af, 0x03f2, 0xb398,
1509 0xc9e3, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1511 /* 1.41421356237309504880168872420969807856967187537695E0 */
1512 const UEMUSHORT esqrt2[NE] =
1513 {0x1d6f, 0xbe9f, 0x754a, 0x89b3,
1514 0x597d, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1516 /* 3.14159265358979323846264338327950288419716939937511E0 */
1517 const UEMUSHORT epi[NE] =
1518 {0x2902, 0x1cd1, 0x80dc, 0x628b,
1519 0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1522 /* LONG_DOUBLE_TYPE_SIZE is other than 128 */
1523 const UEMUSHORT ezero[NE] =
1524 {0, 0000000, 0000000, 0000000, 0000000, 0000000,};
1525 const UEMUSHORT ehalf[NE] =
1526 {0, 0000000, 0000000, 0000000, 0100000, 0x3ffe,};
1527 const UEMUSHORT eone[NE] =
1528 {0, 0000000, 0000000, 0000000, 0100000, 0x3fff,};
1529 const UEMUSHORT etwo[NE] =
1530 {0, 0000000, 0000000, 0000000, 0100000, 0040000,};
1531 const UEMUSHORT e32[NE] =
1532 {0, 0000000, 0000000, 0000000, 0100000, 0040004,};
1533 const UEMUSHORT elog2[NE] =
1534 {0xc9e4, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1535 const UEMUSHORT esqrt2[NE] =
1536 {0x597e, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1537 const UEMUSHORT epi[NE] =
1538 {0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1541 /* Control register for rounding precision.
1542 This can be set to 113 (if NE=10), 80 (if NE=6), 64, 56, 53, or 24 bits. */
1547 /* Clear out entire e-type number X. */
1555 for (i = 0; i < NE; i++)
1559 /* Move e-type number from A to B. */
1568 for (i = 0; i < NE; i++)
1574 /* Absolute value of e-type X. */
1580 /* sign is top bit of last word of external format */
1581 x[NE - 1] &= 0x7fff;
1585 /* Negate the e-type number X. */
1592 x[NE - 1] ^= 0x8000; /* Toggle the sign bit */
1595 /* Return 1 if sign bit of e-type number X is nonzero, else zero. */
1599 const UEMUSHORT x[];
1602 if (x[NE - 1] & 0x8000)
1608 /* Return 1 if e-type number X is infinity, else return zero. */
1612 const UEMUSHORT x[];
1619 if ((x[NE - 1] & 0x7fff) == 0x7fff)
1625 /* Check if e-type number is not a number. The bit pattern is one that we
1626 defined, so we know for sure how to detect it. */
1630 const UEMUSHORT x[] ATTRIBUTE_UNUSED;
1635 /* NaN has maximum exponent */
1636 if ((x[NE - 1] & 0x7fff) != 0x7fff)
1638 /* ... and non-zero significand field. */
1639 for (i = 0; i < NE - 1; i++)
1649 /* Fill e-type number X with infinity pattern (IEEE)
1650 or largest possible number (non-IEEE). */
1659 for (i = 0; i < NE - 1; i++)
1663 for (i = 0; i < NE - 1; i++)
1691 /* Output an e-type NaN.
1692 This generates Intel's quiet NaN pattern for extended real.
1693 The exponent is 7fff, the leading mantissa word is c000. */
1703 for (i = 0; i < NE - 2; i++)
1706 *x = (sign << 15) | 0x7fff;
1710 /* Move in an e-type number A, converting it to exploded e-type B. */
1722 p = a + (NE - 1); /* point to last word of external number */
1723 /* get the sign bit */
1728 /* get the exponent */
1730 *q++ &= 0x7fff; /* delete the sign bit */
1732 if ((*(q - 1) & 0x7fff) == 0x7fff)
1738 for (i = 3; i < NI; i++)
1744 for (i = 2; i < NI; i++)
1750 /* clear high guard word */
1752 /* move in the significand */
1753 for (i = 0; i < NE - 1; i++)
1755 /* clear low guard word */
1759 /* Move out exploded e-type number A, converting it to e type B. */
1772 q = b + (NE - 1); /* point to output exponent */
1773 /* combine sign and exponent */
1776 *q-- = *p++ | 0x8000;
1780 if (*(p - 1) == 0x7fff)
1785 enan (b, eiisneg (a));
1793 /* skip over guard word */
1795 /* move the significand */
1796 for (j = 0; j < NE - 1; j++)
1800 /* Clear out exploded e-type number XI. */
1808 for (i = 0; i < NI; i++)
1812 /* Clear out exploded e-type XI, but don't touch the sign. */
1821 for (i = 0; i < NI - 1; i++)
1825 /* Move exploded e-type number from A to B. */
1834 for (i = 0; i < NI - 1; i++)
1836 /* clear low guard word */
1840 /* Generate exploded e-type NaN.
1841 The explicit pattern for this is maximum exponent and
1842 top two significant bits set. */
1856 /* Return nonzero if exploded e-type X is a NaN. */
1861 const UEMUSHORT x[];
1865 if ((x[E] & 0x7fff) == 0x7fff)
1867 for (i = M + 1; i < NI; i++)
1877 /* Return nonzero if sign of exploded e-type X is nonzero. */
1881 const UEMUSHORT x[];
1888 /* Fill exploded e-type X with infinity pattern.
1889 This has maximum exponent and significand all zeros. */
1901 /* Return nonzero if exploded e-type X is infinite. */
1906 const UEMUSHORT x[];
1913 if ((x[E] & 0x7fff) == 0x7fff)
1917 #endif /* INFINITY */
1919 /* Compare significands of numbers in internal exploded e-type format.
1920 Guard words are included in the comparison.
1928 const UEMUSHORT *a, *b;
1932 a += M; /* skip up to significand area */
1934 for (i = M; i < NI; i++)
1942 if (*(--a) > *(--b))
1948 /* Shift significand of exploded e-type X down by 1 bit. */
1957 x += M; /* point to significand area */
1960 for (i = M; i < NI; i++)
1972 /* Shift significand of exploded e-type X up by 1 bit. */
1984 for (i = M; i < NI; i++)
1997 /* Shift significand of exploded e-type X down by 8 bits. */
2003 UEMUSHORT newbyt, oldbyt;
2008 for (i = M; i < NI; i++)
2018 /* Shift significand of exploded e-type X up by 8 bits. */
2025 UEMUSHORT newbyt, oldbyt;
2030 for (i = M; i < NI; i++)
2040 /* Shift significand of exploded e-type X up by 16 bits. */
2052 for (i = M; i < NI - 1; i++)
2058 /* Shift significand of exploded e-type X down by 16 bits. */
2070 for (i = M; i < NI - 1; i++)
2076 /* Add significands of exploded e-type X and Y. X + Y replaces Y. */
2090 for (i = M; i < NI; i++)
2092 a = (unsigned EMULONG) (*x) + (unsigned EMULONG) (*y) + carry;
2103 /* Subtract significands of exploded e-type X and Y. Y - X replaces Y. */
2117 for (i = M; i < NI; i++)
2119 a = (unsigned EMULONG) (*y) - (unsigned EMULONG) (*x) - carry;
2131 static UEMUSHORT equot[NI];
2135 /* Radix 2 shift-and-add versions of multiply and divide */
2138 /* Divide significands */
2142 UEMUSHORT den[], num[];
2152 for (i = M; i < NI; i++)
2157 /* Use faster compare and subtraction if denominator has only 15 bits of
2163 for (i = M + 3; i < NI; i++)
2168 if ((den[M + 1] & 1) != 0)
2176 for (i = 0; i < NBITS + 2; i++)
2194 /* The number of quotient bits to calculate is NBITS + 1 scaling guard
2195 bit + 1 roundoff bit. */
2200 for (i = 0; i < NBITS + 2; i++)
2202 if (ecmpm (den, num) <= 0)
2205 j = 1; /* quotient bit = 1 */
2219 /* test for nonzero remainder after roundoff bit */
2222 for (i = M; i < NI; i++)
2230 for (i = 0; i < NI; i++)
2236 /* Multiply significands */
2247 for (i = M; i < NI; i++)
2252 while (*p == 0) /* significand is not supposed to be zero */
2257 if ((*p & 0xff) == 0)
2265 for (i = 0; i < k; i++)
2269 /* remember if there were any nonzero bits shifted out */
2276 for (i = 0; i < NI; i++)
2279 /* return flag for lost nonzero bits */
2285 /* Radix 65536 versions of multiply and divide. */
2287 /* Multiply significand of e-type number B
2288 by 16-bit quantity A, return e-type result to C. */
2293 const UEMUSHORT b[];
2297 unsigned EMULONG carry;
2298 const UEMUSHORT *ps;
2300 unsigned EMULONG aa, m;
2309 for (i=M+1; i<NI; i++)
2319 m = (unsigned EMULONG) aa * *ps--;
2320 carry = (m & 0xffff) + *pp;
2321 *pp-- = (UEMUSHORT) carry;
2322 carry = (carry >> 16) + (m >> 16) + *pp;
2323 *pp = (UEMUSHORT) carry;
2324 *(pp-1) = carry >> 16;
2327 for (i=M; i<NI; i++)
2331 /* Divide significands of exploded e-types NUM / DEN. Neither the
2332 numerator NUM nor the denominator DEN is permitted to have its high guard
2337 const UEMUSHORT den[];
2342 unsigned EMULONG tnum;
2343 UEMUSHORT j, tdenm, tquot;
2344 UEMUSHORT tprod[NI+1];
2350 for (i=M; i<NI; i++)
2356 for (i=M; i<NI; i++)
2358 /* Find trial quotient digit (the radix is 65536). */
2359 tnum = (((unsigned EMULONG) num[M]) << 16) + num[M+1];
2361 /* Do not execute the divide instruction if it will overflow. */
2362 if ((tdenm * (unsigned long) 0xffff) < tnum)
2365 tquot = tnum / tdenm;
2366 /* Multiply denominator by trial quotient digit. */
2367 m16m ((unsigned int) tquot, den, tprod);
2368 /* The quotient digit may have been overestimated. */
2369 if (ecmpm (tprod, num) > 0)
2373 if (ecmpm (tprod, num) > 0)
2383 /* test for nonzero remainder after roundoff bit */
2386 for (i=M; i<NI; i++)
2393 for (i=0; i<NI; i++)
2399 /* Multiply significands of exploded e-type A and B, result in B. */
2403 const UEMUSHORT a[];
2408 UEMUSHORT pprod[NI];
2414 for (i=M; i<NI; i++)
2420 for (i=M+1; i<NI; i++)
2428 m16m ((unsigned int) *p--, b, pprod);
2429 eaddm (pprod, equot);
2435 for (i=0; i<NI; i++)
2438 /* return flag for lost nonzero bits */
2444 /* Normalize and round off.
2446 The internal format number to be rounded is S.
2447 Input LOST is 0 if the value is exact. This is the so-called sticky bit.
2449 Input SUBFLG indicates whether the number was obtained
2450 by a subtraction operation. In that case if LOST is nonzero
2451 then the number is slightly smaller than indicated.
2453 Input EXP is the biased exponent, which may be negative.
2454 the exponent field of S is ignored but is replaced by
2455 EXP as adjusted by normalization and rounding.
2457 Input RCNTRL is the rounding control. If it is nonzero, the
2458 returned value will be rounded to RNDPRC bits.
2460 For future reference: In order for emdnorm to round off denormal
2461 significands at the right point, the input exponent must be
2462 adjusted to be the actual value it would have after conversion to
2463 the final floating point type. This adjustment has been
2464 implemented for all type conversions (etoe53, etc.) and decimal
2465 conversions, but not for the arithmetic functions (eadd, etc.).
2466 Data types having standard 15-bit exponents are not affected by
2467 this, but SFmode and DFmode are affected. For example, ediv with
2468 rndprc = 24 will not round correctly to 24-bit precision if the
2469 result is denormal. */
2471 static int rlast = -1;
2473 static UEMUSHORT rmsk = 0;
2474 static UEMUSHORT rmbit = 0;
2475 static UEMUSHORT rebit = 0;
2477 static UEMUSHORT rbit[NI];
2480 emdnorm (s, lost, subflg, exp, rcntrl)
2493 /* a blank significand could mean either zero or infinity. */
2506 if ((j > NBITS) && (exp < 32767))
2514 if (exp > (EMULONG) (-NBITS - 1))
2527 /* Round off, unless told not to by rcntrl. */
2530 /* Set up rounding parameters if the control register changed. */
2531 if (rndprc != rlast)
2538 rw = NI - 1; /* low guard word */
2561 /* For DEC or IBM arithmetic */
2578 /* For C4x arithmetic */
2599 /* Shift down 1 temporarily if the data structure has an implied
2600 most significant bit and the number is denormal.
2601 Intel long double denormals also lose one bit of precision. */
2602 if ((exp <= 0) && (rndprc != NBITS)
2603 && ((rndprc != 64) || ((rndprc == 64) && ! REAL_WORDS_BIG_ENDIAN)))
2605 lost |= s[NI - 1] & 1;
2608 /* Clear out all bits below the rounding bit,
2609 remembering in r if any were nonzero. */
2623 if ((r & rmbit) != 0)
2629 { /* round to even */
2630 if ((s[re] & rebit) == 0)
2643 /* Undo the temporary shift for denormal values. */
2644 if ((exp <= 0) && (rndprc != NBITS)
2645 && ((rndprc != 64) || ((rndprc == 64) && ! REAL_WORDS_BIG_ENDIAN)))
2650 { /* overflow on roundoff */
2663 for (i = 2; i < NI - 1; i++)
2666 warning ("floating point overflow");
2670 for (i = M + 1; i < NI - 1; i++)
2673 if ((rndprc < 64) || (rndprc == 113))
2688 s[1] = (UEMUSHORT) exp;
2691 /* Subtract. C = B - A, all e type numbers. */
2693 static int subflg = 0;
2697 const UEMUSHORT *a, *b;
2712 /* Infinity minus infinity is a NaN.
2713 Test for subtracting infinities of the same sign. */
2714 if (eisinf (a) && eisinf (b)
2715 && ((eisneg (a) ^ eisneg (b)) == 0))
2717 mtherr ("esub", INVALID);
2726 /* Add. C = A + B, all e type. */
2730 const UEMUSHORT *a, *b;
2735 /* NaN plus anything is a NaN. */
2746 /* Infinity minus infinity is a NaN.
2747 Test for adding infinities of opposite signs. */
2748 if (eisinf (a) && eisinf (b)
2749 && ((eisneg (a) ^ eisneg (b)) != 0))
2751 mtherr ("esub", INVALID);
2760 /* Arithmetic common to both addition and subtraction. */
2764 const UEMUSHORT *a, *b;
2767 UEMUSHORT ai[NI], bi[NI], ci[NI];
2769 EMULONG lt, lta, ltb;
2790 /* compare exponents */
2795 { /* put the larger number in bi */
2805 if (lt < (EMULONG) (-NBITS - 1))
2806 goto done; /* answer same as larger addend */
2808 lost = eshift (ai, k); /* shift the smaller number down */
2812 /* exponents were the same, so must compare significands */
2815 { /* the numbers are identical in magnitude */
2816 /* if different signs, result is zero */
2822 /* if same sign, result is double */
2823 /* double denormalized tiny number */
2824 if ((bi[E] == 0) && ((bi[3] & 0x8000) == 0))
2829 /* add 1 to exponent unless both are zero! */
2830 for (j = 1; j < NI - 1; j++)
2846 bi[E] = (UEMUSHORT) ltb;
2850 { /* put the larger number in bi */
2866 emdnorm (bi, lost, subflg, ltb, !ROUND_TOWARDS_ZERO);
2872 /* Divide: C = B/A, all e type. */
2876 const UEMUSHORT *a, *b;
2879 UEMUSHORT ai[NI], bi[NI];
2881 EMULONG lt, lta, ltb;
2883 /* IEEE says if result is not a NaN, the sign is "-" if and only if
2884 operands have opposite signs -- but flush -0 to 0 later if not IEEE. */
2885 sign = eisneg (a) ^ eisneg (b);
2888 /* Return any NaN input. */
2899 /* Zero over zero, or infinity over infinity, is a NaN. */
2900 if (((ecmp (a, ezero) == 0) && (ecmp (b, ezero) == 0))
2901 || (eisinf (a) && eisinf (b)))
2903 mtherr ("ediv", INVALID);
2908 /* Infinity over anything else is infinity. */
2915 /* Anything else over infinity is zero. */
2927 { /* See if numerator is zero. */
2928 for (i = 1; i < NI - 1; i++)
2932 ltb -= enormlz (bi);
2942 { /* possible divide by zero */
2943 for (i = 1; i < NI - 1; i++)
2947 lta -= enormlz (ai);
2951 /* Divide by zero is not an invalid operation.
2952 It is a divide-by-zero operation! */
2954 mtherr ("ediv", SING);
2960 /* calculate exponent */
2961 lt = ltb - lta + EXONE;
2962 emdnorm (bi, i, 0, lt, !ROUND_TOWARDS_ZERO);
2969 && (ecmp (c, ezero) != 0)
2972 *(c+(NE-1)) |= 0x8000;
2974 *(c+(NE-1)) &= ~0x8000;
2977 /* Multiply e-types A and B, return e-type product C. */
2981 const UEMUSHORT *a, *b;
2984 UEMUSHORT ai[NI], bi[NI];
2986 EMULONG lt, lta, ltb;
2988 /* IEEE says if result is not a NaN, the sign is "-" if and only if
2989 operands have opposite signs -- but flush -0 to 0 later if not IEEE. */
2990 sign = eisneg (a) ^ eisneg (b);
2993 /* NaN times anything is the same NaN. */
3004 /* Zero times infinity is a NaN. */
3005 if ((eisinf (a) && (ecmp (b, ezero) == 0))
3006 || (eisinf (b) && (ecmp (a, ezero) == 0)))
3008 mtherr ("emul", INVALID);
3013 /* Infinity times anything else is infinity. */
3015 if (eisinf (a) || eisinf (b))
3027 for (i = 1; i < NI - 1; i++)
3031 lta -= enormlz (ai);
3042 for (i = 1; i < NI - 1; i++)
3046 ltb -= enormlz (bi);
3055 /* Multiply significands */
3057 /* calculate exponent */
3058 lt = lta + ltb - (EXONE - 1);
3059 emdnorm (bi, j, 0, lt, !ROUND_TOWARDS_ZERO);
3066 && (ecmp (c, ezero) != 0)
3069 *(c+(NE-1)) |= 0x8000;
3071 *(c+(NE-1)) &= ~0x8000;
3074 /* Convert double precision PE to e-type Y. */
3078 const UEMUSHORT *pe;
3088 ibmtoe (pe, y, DFmode);
3093 c4xtoe (pe, y, HFmode);
3103 denorm = 0; /* flag if denormalized number */
3105 if (! REAL_WORDS_BIG_ENDIAN)
3111 yy[M] = (r & 0x0f) | 0x10;
3112 r &= ~0x800f; /* strip sign and 4 significand bits */
3117 if (! REAL_WORDS_BIG_ENDIAN)
3119 if (((pe[3] & 0xf) != 0) || (pe[2] != 0)
3120 || (pe[1] != 0) || (pe[0] != 0))
3122 enan (y, yy[0] != 0);
3128 if (((pe[0] & 0xf) != 0) || (pe[1] != 0)
3129 || (pe[2] != 0) || (pe[3] != 0))
3131 enan (y, yy[0] != 0);
3142 #endif /* INFINITY */
3144 /* If zero exponent, then the significand is denormalized.
3145 So take back the understood high significand bit. */
3156 if (! REAL_WORDS_BIG_ENDIAN)
3173 /* If zero exponent, then normalize the significand. */
3174 if ((k = enormlz (yy)) > NBITS)
3177 yy[E] -= (UEMUSHORT) (k - 1);
3180 #endif /* not C4X */
3181 #endif /* not IBM */
3182 #endif /* not DEC */
3185 /* Convert double extended precision float PE to e type Y. */
3189 const UEMUSHORT *pe;
3199 for (i = 0; i < NE - 5; i++)
3201 /* This precision is not ordinarily supported on DEC or IBM. */
3203 for (i = 0; i < 5; i++)
3207 p = &yy[0] + (NE - 1);
3210 for (i = 0; i < 5; i++)
3214 if (! REAL_WORDS_BIG_ENDIAN)
3216 for (i = 0; i < 5; i++)
3219 /* For denormal long double Intel format, shift significand up one
3220 -- but only if the top significand bit is zero. A top bit of 1
3221 is "pseudodenormal" when the exponent is zero. */
3222 if ((yy[NE-1] & 0x7fff) == 0 && (yy[NE-2] & 0x8000) == 0)
3234 p = &yy[0] + (NE - 1);
3235 #ifdef ARM_EXTENDED_IEEE_FORMAT
3236 /* For ARMs, the exponent is in the lowest 15 bits of the word. */
3237 *p-- = (e[0] & 0x8000) | (e[1] & 0x7ffff);
3243 for (i = 0; i < 4; i++)
3248 /* Point to the exponent field and check max exponent cases. */
3250 if ((*p & 0x7fff) == 0x7fff)
3253 if (! REAL_WORDS_BIG_ENDIAN)
3255 for (i = 0; i < 4; i++)
3257 if ((i != 3 && pe[i] != 0)
3258 /* Anything but 0x8000 here, including 0, is a NaN. */
3259 || (i == 3 && pe[i] != 0x8000))
3261 enan (y, (*p & 0x8000) != 0);
3268 #ifdef ARM_EXTENDED_IEEE_FORMAT
3269 for (i = 2; i <= 5; i++)
3273 enan (y, (*p & 0x8000) != 0);
3278 /* In Motorola extended precision format, the most significant
3279 bit of an infinity mantissa could be either 1 or 0. It is
3280 the lower order bits that tell whether the value is a NaN. */
3281 if ((pe[2] & 0x7fff) != 0)
3284 for (i = 3; i <= 5; i++)
3289 enan (y, (*p & 0x8000) != 0);
3293 #endif /* not ARM */
3302 #endif /* INFINITY */
3305 for (i = 0; i < NE; i++)
3309 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
3310 /* Convert 128-bit long double precision float PE to e type Y. */
3314 const UEMUSHORT *pe;
3327 if (! REAL_WORDS_BIG_ENDIAN)
3339 if (! REAL_WORDS_BIG_ENDIAN)
3341 for (i = 0; i < 7; i++)
3345 enan (y, yy[0] != 0);
3352 for (i = 1; i < 8; i++)
3356 enan (y, yy[0] != 0);
3368 #endif /* INFINITY */
3372 if (! REAL_WORDS_BIG_ENDIAN)
3374 for (i = 0; i < 7; i++)
3380 for (i = 0; i < 7; i++)
3384 /* If denormal, remove the implied bit; else shift down 1. */
3398 /* Convert single precision float PE to e type Y. */
3402 const UEMUSHORT *pe;
3407 ibmtoe (pe, y, SFmode);
3413 c4xtoe (pe, y, QFmode);
3424 denorm = 0; /* flag if denormalized number */
3427 if (! REAL_WORDS_BIG_ENDIAN)
3437 yy[M] = (r & 0x7f) | 0200;
3438 r &= ~0x807f; /* strip sign and 7 significand bits */
3440 if (!LARGEST_EXPONENT_IS_NORMAL (32) && r == 0x7f80)
3443 if (REAL_WORDS_BIG_ENDIAN)
3445 if (((pe[0] & 0x7f) != 0) || (pe[1] != 0))
3447 enan (y, yy[0] != 0);
3453 if (((pe[1] & 0x7f) != 0) || (pe[0] != 0))
3455 enan (y, yy[0] != 0);
3466 #endif /* INFINITY */
3468 /* If zero exponent, then the significand is denormalized.
3469 So take back the understood high significand bit. */
3482 if (! REAL_WORDS_BIG_ENDIAN)
3492 { /* if zero exponent, then normalize the significand */
3493 if ((k = enormlz (yy)) > NBITS)
3496 yy[E] -= (UEMUSHORT) (k - 1);
3499 #endif /* not C4X */
3500 #endif /* not IBM */
3503 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
3504 /* Convert e-type X to IEEE 128-bit long double format E. */
3518 make_nan (e, eisneg (x), TFmode);
3523 exp = (EMULONG) xi[E];
3528 /* round off to nearest or even */
3531 emdnorm (xi, 0, 0, exp, !ROUND_TOWARDS_ZERO);
3539 /* Convert exploded e-type X, that has already been rounded to
3540 113-bit precision, to IEEE 128-bit long double format Y. */
3552 make_nan (b, eiisneg (a), TFmode);
3557 if (REAL_WORDS_BIG_ENDIAN)
3560 q = b + 7; /* point to output exponent */
3562 /* If not denormal, delete the implied bit. */
3567 /* combine sign and exponent */
3569 if (REAL_WORDS_BIG_ENDIAN)
3572 *q++ = *p++ | 0x8000;
3579 *q-- = *p++ | 0x8000;
3583 /* skip over guard word */
3585 /* move the significand */
3586 if (REAL_WORDS_BIG_ENDIAN)
3588 for (i = 0; i < 7; i++)
3593 for (i = 0; i < 7; i++)
3599 /* Convert e-type X to IEEE double extended format E. */
3613 make_nan (e, eisneg (x), XFmode);
3618 /* adjust exponent for offset */
3619 exp = (EMULONG) xi[E];
3624 /* round off to nearest or even */
3627 emdnorm (xi, 0, 0, exp, !ROUND_TOWARDS_ZERO);
3635 /* Convert exploded e-type X, that has already been rounded to
3636 64-bit precision, to IEEE double extended format Y. */
3648 make_nan (b, eiisneg (a), XFmode);
3652 /* Shift denormal long double Intel format significand down one bit. */
3653 if ((a[E] == 0) && ! REAL_WORDS_BIG_ENDIAN)
3663 if (REAL_WORDS_BIG_ENDIAN)
3667 q = b + 4; /* point to output exponent */
3668 /* Clear the last two bytes of 12-byte Intel format. q is pointing
3669 into an array of size 6 (e.g. x[NE]), so the last two bytes are
3670 always there, and there are never more bytes, even when we are using
3671 INTEL_EXTENDED_IEEE_FORMAT. */
3676 /* combine sign and exponent */
3680 *q++ = *p++ | 0x8000;
3687 *q-- = *p++ | 0x8000;
3692 if (REAL_WORDS_BIG_ENDIAN)
3694 #ifdef ARM_EXTENDED_IEEE_FORMAT
3695 /* The exponent is in the lowest 15 bits of the first word. */
3696 *q++ = i ? 0x8000 : 0;
3700 *q++ = *p++ | 0x8000;
3709 *q-- = *p++ | 0x8000;
3714 /* skip over guard word */
3716 /* move the significand */
3718 for (i = 0; i < 4; i++)
3722 for (i = 0; i < 4; i++)
3726 if (REAL_WORDS_BIG_ENDIAN)
3728 for (i = 0; i < 4; i++)
3736 /* Intel long double infinity significand. */
3744 for (i = 0; i < 4; i++)
3750 /* e type to double precision. */
3753 /* Convert e-type X to DEC-format double E. */
3760 etodec (x, e); /* see etodec.c */
3763 /* Convert exploded e-type X, that has already been rounded to
3764 56-bit double precision, to DEC double Y. */
3775 /* Convert e-type X to IBM 370-format double E. */
3782 etoibm (x, e, DFmode);
3785 /* Convert exploded e-type X, that has already been rounded to
3786 56-bit precision, to IBM 370 double Y. */
3792 toibm (x, y, DFmode);
3795 #else /* it's neither DEC nor IBM */
3797 /* Convert e-type X to C4X-format long double E. */
3804 etoc4x (x, e, HFmode);
3807 /* Convert exploded e-type X, that has already been rounded to
3808 56-bit precision, to IBM 370 double Y. */
3814 toc4x (x, y, HFmode);
3817 #else /* it's neither DEC nor IBM nor C4X */
3819 /* Convert e-type X to IEEE double E. */
3833 make_nan (e, eisneg (x), DFmode);
3838 /* adjust exponent for offsets */
3839 exp = (EMULONG) xi[E] - (EXONE - 0x3ff);
3844 /* round off to nearest or even */
3847 emdnorm (xi, 0, 0, exp, !ROUND_TOWARDS_ZERO);
3855 /* Convert exploded e-type X, that has already been rounded to
3856 53-bit precision, to IEEE double Y. */
3868 make_nan (y, eiisneg (x), DFmode);
3872 if (LARGEST_EXPONENT_IS_NORMAL (64) && x[1] > 2047)
3874 saturate (y, eiisneg (x), 64, 1);
3879 if (! REAL_WORDS_BIG_ENDIAN)
3882 *y = 0; /* output high order */
3884 *y = 0x8000; /* output sign bit */
3887 if (i >= (unsigned int) 2047)
3889 /* Saturate at largest number less than infinity. */
3892 if (! REAL_WORDS_BIG_ENDIAN)
3906 *y |= (UEMUSHORT) 0x7fef;
3907 if (! REAL_WORDS_BIG_ENDIAN)
3932 i |= *p++ & (UEMUSHORT) 0x0f; /* *p = xi[M] */
3933 *y |= (UEMUSHORT) i; /* high order output already has sign bit set */
3934 if (! REAL_WORDS_BIG_ENDIAN)
3949 #endif /* not C4X */
3950 #endif /* not IBM */
3951 #endif /* not DEC */
3955 /* e type to single precision. */
3958 /* Convert e-type X to IBM 370 float E. */
3965 etoibm (x, e, SFmode);
3968 /* Convert exploded e-type X, that has already been rounded to
3969 float precision, to IBM 370 float Y. */
3975 toibm (x, y, SFmode);
3981 /* Convert e-type X to C4X float E. */
3988 etoc4x (x, e, QFmode);
3991 /* Convert exploded e-type X, that has already been rounded to
3992 float precision, to IBM 370 float Y. */
3998 toc4x (x, y, QFmode);
4003 /* Convert e-type X to IEEE float E. DEC float is the same as IEEE float. */
4017 make_nan (e, eisneg (x), SFmode);
4022 /* adjust exponent for offsets */
4023 exp = (EMULONG) xi[E] - (EXONE - 0177);
4028 /* round off to nearest or even */
4031 emdnorm (xi, 0, 0, exp, !ROUND_TOWARDS_ZERO);
4039 /* Convert exploded e-type X, that has already been rounded to
4040 float precision, to IEEE float Y. */
4052 make_nan (y, eiisneg (x), SFmode);
4056 if (LARGEST_EXPONENT_IS_NORMAL (32) && x[1] > 255)
4058 saturate (y, eiisneg (x), 32, 1);
4063 if (! REAL_WORDS_BIG_ENDIAN)
4069 *y = 0; /* output high order */
4071 *y = 0x8000; /* output sign bit */
4074 /* Handle overflow cases. */
4075 if (!LARGEST_EXPONENT_IS_NORMAL (32) && i >= 255)
4078 *y |= (UEMUSHORT) 0x7f80;
4083 if (! REAL_WORDS_BIG_ENDIAN)
4091 #else /* no INFINITY */
4092 *y |= (UEMUSHORT) 0x7f7f;
4097 if (! REAL_WORDS_BIG_ENDIAN)
4108 #endif /* no INFINITY */
4120 i |= *p++ & (UEMUSHORT) 0x7f; /* *p = xi[M] */
4121 /* High order output already has sign bit set. */
4127 if (! REAL_WORDS_BIG_ENDIAN)
4136 #endif /* not C4X */
4137 #endif /* not IBM */
4139 /* Compare two e type numbers.
4143 -2 if either a or b is a NaN. */
4147 const UEMUSHORT *a, *b;
4149 UEMUSHORT ai[NI], bi[NI];
4155 if (eisnan (a) || eisnan (b))
4164 { /* the signs are different */
4166 for (i = 1; i < NI - 1; i++)
4180 /* both are the same sign */
4195 return (0); /* equality */
4199 if (*(--p) > *(--q))
4200 return (msign); /* p is bigger */
4202 return (-msign); /* p is littler */
4206 /* Find e-type nearest integer to X, as floor (X + 0.5). */
4218 /* Convert HOST_WIDE_INT LP to e type Y. */
4222 const HOST_WIDE_INT *lp;
4226 unsigned HOST_WIDE_INT ll;
4232 /* make it positive */
4233 ll = (unsigned HOST_WIDE_INT) (-(*lp));
4234 yi[0] = 0xffff; /* put correct sign in the e type number */
4238 ll = (unsigned HOST_WIDE_INT) (*lp);
4240 /* move the long integer to yi significand area */
4241 #if HOST_BITS_PER_WIDE_INT == 64
4242 yi[M] = (UEMUSHORT) (ll >> 48);
4243 yi[M + 1] = (UEMUSHORT) (ll >> 32);
4244 yi[M + 2] = (UEMUSHORT) (ll >> 16);
4245 yi[M + 3] = (UEMUSHORT) ll;
4246 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
4248 yi[M] = (UEMUSHORT) (ll >> 16);
4249 yi[M + 1] = (UEMUSHORT) ll;
4250 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
4253 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
4254 ecleaz (yi); /* it was zero */
4256 yi[E] -= (UEMUSHORT) k;/* subtract shift count from exponent */
4257 emovo (yi, y); /* output the answer */
4260 /* Convert unsigned HOST_WIDE_INT LP to e type Y. */
4264 const unsigned HOST_WIDE_INT *lp;
4268 unsigned HOST_WIDE_INT ll;
4274 /* move the long integer to ayi significand area */
4275 #if HOST_BITS_PER_WIDE_INT == 64
4276 yi[M] = (UEMUSHORT) (ll >> 48);
4277 yi[M + 1] = (UEMUSHORT) (ll >> 32);
4278 yi[M + 2] = (UEMUSHORT) (ll >> 16);
4279 yi[M + 3] = (UEMUSHORT) ll;
4280 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
4282 yi[M] = (UEMUSHORT) (ll >> 16);
4283 yi[M + 1] = (UEMUSHORT) ll;
4284 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
4287 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
4288 ecleaz (yi); /* it was zero */
4290 yi[E] -= (UEMUSHORT) k; /* subtract shift count from exponent */
4291 emovo (yi, y); /* output the answer */
4295 /* Find signed HOST_WIDE_INT integer I and floating point fractional
4296 part FRAC of e-type (packed internal format) floating point input X.
4297 The integer output I has the sign of the input, except that
4298 positive overflow is permitted if FIXUNS_TRUNC_LIKE_FIX_TRUNC.
4299 The output e-type fraction FRAC is the positive fractional
4310 unsigned HOST_WIDE_INT ll;
4313 k = (int) xi[E] - (EXONE - 1);
4316 /* if exponent <= 0, integer = 0 and real output is fraction */
4321 if (k > (HOST_BITS_PER_WIDE_INT - 1))
4323 /* long integer overflow: output large integer
4324 and correct fraction */
4326 *i = ((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1);
4329 #ifdef FIXUNS_TRUNC_LIKE_FIX_TRUNC
4330 /* In this case, let it overflow and convert as if unsigned. */
4331 euifrac (x, &ll, frac);
4332 *i = (HOST_WIDE_INT) ll;
4335 /* In other cases, return the largest positive integer. */
4336 *i = (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1;
4341 warning ("overflow on truncation to integer");
4345 /* Shift more than 16 bits: first shift up k-16 mod 16,
4346 then shift up by 16's. */
4347 j = k - ((k >> 4) << 4);
4354 ll = (ll << 16) | xi[M];
4356 while ((k -= 16) > 0);
4363 /* shift not more than 16 bits */
4365 *i = (HOST_WIDE_INT) xi[M] & 0xffff;
4372 if ((k = enormlz (xi)) > NBITS)
4375 xi[E] -= (UEMUSHORT) k;
4381 /* Find unsigned HOST_WIDE_INT integer I and floating point fractional part
4382 FRAC of e-type X. A negative input yields integer output = 0 but
4383 correct fraction. */
4386 euifrac (x, i, frac)
4388 unsigned HOST_WIDE_INT *i;
4391 unsigned HOST_WIDE_INT ll;
4396 k = (int) xi[E] - (EXONE - 1);
4399 /* if exponent <= 0, integer = 0 and argument is fraction */
4404 if (k > HOST_BITS_PER_WIDE_INT)
4406 /* Long integer overflow: output large integer
4407 and correct fraction.
4408 Note, the BSD MicroVAX compiler says that ~(0UL)
4409 is a syntax error. */
4413 warning ("overflow on truncation to unsigned integer");
4417 /* Shift more than 16 bits: first shift up k-16 mod 16,
4418 then shift up by 16's. */
4419 j = k - ((k >> 4) << 4);
4426 ll = (ll << 16) | xi[M];
4428 while ((k -= 16) > 0);
4433 /* shift not more than 16 bits */
4435 *i = (HOST_WIDE_INT) xi[M] & 0xffff;
4438 if (xi[0]) /* A negative value yields unsigned integer 0. */
4444 if ((k = enormlz (xi)) > NBITS)
4447 xi[E] -= (UEMUSHORT) k;
4452 /* Shift the significand of exploded e-type X up or down by SC bits. */
4473 lost |= *p; /* remember lost bits */
4514 return ((int) lost);
4517 /* Shift normalize the significand area of exploded e-type X.
4518 Return the shift count (up = positive). */
4533 return (0); /* already normalized */
4539 /* With guard word, there are NBITS+16 bits available.
4540 Return true if all are zero. */
4544 /* see if high byte is zero */
4545 while ((*p & 0xff00) == 0)
4550 /* now shift 1 bit at a time */
4551 while ((*p & 0x8000) == 0)
4557 mtherr ("enormlz", UNDERFLOW);
4563 /* Normalize by shifting down out of the high guard word
4564 of the significand */
4579 mtherr ("enormlz", OVERFLOW);
4586 /* Powers of ten used in decimal <-> binary conversions. */
4591 #if MAX_LONG_DOUBLE_TYPE_SIZE == 128 && (INTEL_EXTENDED_IEEE_FORMAT == 0)
4592 static const UEMUSHORT etens[NTEN + 1][NE] =
4594 {0x6576, 0x4a92, 0x804a, 0x153f,
4595 0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
4596 {0x6a32, 0xce52, 0x329a, 0x28ce,
4597 0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
4598 {0x526c, 0x50ce, 0xf18b, 0x3d28,
4599 0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
4600 {0x9c66, 0x58f8, 0xbc50, 0x5c54,
4601 0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
4602 {0x851e, 0xeab7, 0x98fe, 0x901b,
4603 0xddbb, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
4604 {0x0235, 0x0137, 0x36b1, 0x336c,
4605 0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
4606 {0x50f8, 0x25fb, 0xc76b, 0x6b71,
4607 0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
4608 {0x0000, 0x0000, 0x0000, 0x0000,
4609 0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
4610 {0x0000, 0x0000, 0x0000, 0x0000,
4611 0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
4612 {0x0000, 0x0000, 0x0000, 0x0000,
4613 0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
4614 {0x0000, 0x0000, 0x0000, 0x0000,
4615 0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
4616 {0x0000, 0x0000, 0x0000, 0x0000,
4617 0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
4618 {0x0000, 0x0000, 0x0000, 0x0000,
4619 0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
4622 static const UEMUSHORT emtens[NTEN + 1][NE] =
4624 {0x2030, 0xcffc, 0xa1c3, 0x8123,
4625 0x2de3, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
4626 {0x8264, 0xd2cb, 0xf2ea, 0x12d4,
4627 0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
4628 {0xf53f, 0xf698, 0x6bd3, 0x0158,
4629 0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
4630 {0xe731, 0x04d4, 0xe3f2, 0xd332,
4631 0x7132, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
4632 {0xa23e, 0x5308, 0xfefb, 0x1155,
4633 0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
4634 {0xe26d, 0xdbde, 0xd05d, 0xb3f6,
4635 0xac7c, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
4636 {0x2a20, 0x6224, 0x47b3, 0x98d7,
4637 0x3f23, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
4638 {0x0b5b, 0x4af2, 0xa581, 0x18ed,
4639 0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
4640 {0xbf71, 0xa9b3, 0x7989, 0xbe68,
4641 0x4c2e, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
4642 {0x3d4d, 0x7c3d, 0x36ba, 0x0d2b,
4643 0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
4644 {0xc155, 0xa4a8, 0x404e, 0x6113,
4645 0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
4646 {0xd70a, 0x70a3, 0x0a3d, 0xa3d7,
4647 0x3d70, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
4648 {0xcccd, 0xcccc, 0xcccc, 0xcccc,
4649 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
4652 /* LONG_DOUBLE_TYPE_SIZE is other than 128 */
4653 static const UEMUSHORT etens[NTEN + 1][NE] =
4655 {0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
4656 {0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
4657 {0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
4658 {0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
4659 {0xddbc, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
4660 {0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
4661 {0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
4662 {0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
4663 {0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
4664 {0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
4665 {0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
4666 {0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
4667 {0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
4670 static const UEMUSHORT emtens[NTEN + 1][NE] =
4672 {0x2de4, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
4673 {0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
4674 {0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
4675 {0x7133, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
4676 {0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
4677 {0xac7d, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
4678 {0x3f24, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
4679 {0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
4680 {0x4c2f, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
4681 {0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
4682 {0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
4683 {0x3d71, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
4684 {0xcccd, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
4689 /* Convert float value X to ASCII string STRING with NDIG digits after
4690 the decimal point. */
4693 e24toasc (x, string, ndigs)
4694 const UEMUSHORT x[];
4701 etoasc (w, string, ndigs);
4704 /* Convert double value X to ASCII string STRING with NDIG digits after
4705 the decimal point. */
4708 e53toasc (x, string, ndigs)
4709 const UEMUSHORT x[];
4716 etoasc (w, string, ndigs);
4719 /* Convert double extended value X to ASCII string STRING with NDIG digits
4720 after the decimal point. */
4723 e64toasc (x, string, ndigs)
4724 const UEMUSHORT x[];
4731 etoasc (w, string, ndigs);
4734 /* Convert 128-bit long double value X to ASCII string STRING with NDIG digits
4735 after the decimal point. */
4738 e113toasc (x, string, ndigs)
4739 const UEMUSHORT x[];
4746 etoasc (w, string, ndigs);
4750 /* Convert e-type X to ASCII string STRING with NDIGS digits after
4751 the decimal point. */
4753 static char wstring[80]; /* working storage for ASCII output */
4756 etoasc (x, string, ndigs)
4757 const UEMUSHORT x[];
4762 UEMUSHORT y[NI], t[NI], u[NI], w[NI];
4763 const UEMUSHORT *p, *r, *ten;
4765 int i, j, k, expon, rndsav;
4778 sprintf (wstring, " NaN ");
4782 rndprc = NBITS; /* set to full precision */
4783 emov (x, y); /* retain external format */
4784 if (y[NE - 1] & 0x8000)
4787 y[NE - 1] &= 0x7fff;
4794 ten = &etens[NTEN][0];
4796 /* Test for zero exponent */
4799 for (k = 0; k < NE - 1; k++)
4802 goto tnzro; /* denormalized number */
4804 goto isone; /* valid all zeros */
4808 /* Test for infinity. */
4809 if (y[NE - 1] == 0x7fff)
4812 sprintf (wstring, " -Infinity ");
4814 sprintf (wstring, " Infinity ");
4818 /* Test for exponent nonzero but significand denormalized.
4819 * This is an error condition.
4821 if ((y[NE - 1] != 0) && ((y[NE - 2] & 0x8000) == 0))
4823 mtherr ("etoasc", DOMAIN);
4824 sprintf (wstring, "NaN");
4828 /* Compare to 1.0 */
4837 { /* Number is greater than 1 */
4838 /* Convert significand to an integer and strip trailing decimal zeros. */
4840 u[NE - 1] = EXONE + NBITS - 1;
4842 p = &etens[NTEN - 4][0];
4848 for (j = 0; j < NE - 1; j++)
4861 /* Rescale from integer significand */
4862 u[NE - 1] += y[NE - 1] - (unsigned int) (EXONE + NBITS - 1);
4864 /* Find power of 10 */
4868 /* An unordered compare result shouldn't happen here. */
4869 while (ecmp (ten, u) <= 0)
4871 if (ecmp (p, u) <= 0)
4884 { /* Number is less than 1.0 */
4885 /* Pad significand with trailing decimal zeros. */
4888 while ((y[NE - 2] & 0x8000) == 0)
4897 for (i = 0; i < NDEC + 1; i++)
4899 if ((w[NI - 1] & 0x7) != 0)
4901 /* multiply by 10 */
4914 if (eone[NE - 1] <= u[1])
4926 while (ecmp (eone, w) > 0)
4928 if (ecmp (p, w) >= 0)
4943 /* Find the first (leading) digit. */
4949 digit = equot[NI - 1];
4950 while ((digit == 0) && (ecmp (y, ezero) != 0))
4958 digit = equot[NI - 1];
4966 /* Examine number of digits requested by caller. */
4984 *s++ = (char) digit + '0';
4987 /* Generate digits after the decimal point. */
4988 for (k = 0; k <= ndigs; k++)
4990 /* multiply current number by 10, without normalizing */
4997 *s++ = (char) equot[NI - 1] + '0';
4999 digit = equot[NI - 1];
5002 /* round off the ASCII string */
5005 /* Test for critical rounding case in ASCII output. */
5009 if (ecmp (t, ezero) != 0)
5010 goto roun; /* round to nearest */
5012 if ((*(s - 1) & 1) == 0)
5013 goto doexp; /* round to even */
5016 /* Round up and propagate carry-outs */
5020 /* Carry out to most significant digit? */
5027 /* Most significant digit carries to 10? */
5035 /* Round up and carry out from less significant digits */
5047 sprintf (ss, "e+%d", expon);
5049 sprintf (ss, "e%d", expon);
5051 sprintf (ss, "e%d", expon);
5054 /* copy out the working string */
5057 while (*ss == ' ') /* strip possible leading space */
5059 while ((*s++ = *ss++) != '\0')
5064 /* Convert ASCII string to floating point.
5066 Numeric input is a free format decimal number of any length, with
5067 or without decimal point. Entering E after the number followed by an
5068 integer number causes the second number to be interpreted as a power of
5069 10 to be multiplied by the first number (i.e., "scientific" notation). */
5071 /* Convert ASCII string S to single precision float value Y. */
5082 /* Convert ASCII string S to double precision value Y. */
5089 #if defined(DEC) || defined(IBM)
5101 /* Convert ASCII string S to double extended value Y. */
5111 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
5112 /* Convert ASCII string S to 128-bit long double Y. */
5119 asctoeg (s, y, 113);
5123 /* Convert ASCII string S to e type Y. */
5130 asctoeg (s, y, NBITS);
5133 /* Convert ASCII string SS to e type Y, with a specified rounding precision
5134 of OPREC bits. BASE is 16 for C99 hexadecimal floating constants. */
5137 asctoeg (ss, y, oprec)
5142 UEMUSHORT yy[NI], xt[NI], tt[NI];
5143 int esign, decflg, sgnflg, nexp, exp, prec, lost;
5144 int i, k, trail, c, rndsav;
5147 char *sp, *s, *lstr;
5150 /* Copy the input string. */
5151 lstr = (char *) alloca (strlen (ss) + 1);
5153 while (*ss == ' ') /* skip leading spaces */
5157 while ((*sp++ = *ss++) != '\0')
5161 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
5168 rndprc = NBITS; /* Set to full precision */
5181 if ((k >= 0) && (k < base))
5183 /* Ignore leading zeros */
5184 if ((prec == 0) && (decflg == 0) && (k == 0))
5186 /* Identify and strip trailing zeros after the decimal point. */
5187 if ((trail == 0) && (decflg != 0))
5190 while (ISDIGIT (*sp) || (base == 16 && ISXDIGIT (*sp)))
5192 /* Check for syntax error */
5194 if ((base != 10 || ((c != 'e') && (c != 'E')))
5195 && (base != 16 || ((c != 'p') && (c != 'P')))
5197 && (c != '\n') && (c != '\r') && (c != ' ')
5199 goto unexpected_char_error;
5208 /* If enough digits were given to more than fill up the yy register,
5209 continuing until overflow into the high guard word yy[2]
5210 guarantees that there will be a roundoff bit at the top
5211 of the low guard word after normalization. */
5218 nexp += 4; /* count digits after decimal point */
5220 eshup1 (yy); /* multiply current number by 16 */
5228 nexp += 1; /* count digits after decimal point */
5230 eshup1 (yy); /* multiply current number by 10 */
5236 /* Insert the current digit. */
5238 xt[NI - 2] = (UEMUSHORT) k;
5243 /* Mark any lost non-zero digit. */
5245 /* Count lost digits before the decimal point. */
5267 case '.': /* decimal point */
5269 goto unexpected_char_error;
5275 goto unexpected_char_error;
5280 goto unexpected_char_error;
5293 unexpected_char_error:
5297 mtherr ("asctoe", DOMAIN);
5306 /* Exponent interpretation */
5308 /* 0.0eXXX is zero, regardless of XXX. Check for the 0.0. */
5309 for (k = 0; k < NI; k++)
5320 /* check for + or - */
5328 while (ISDIGIT (*s))
5337 if ((exp > MAXDECEXP) && (base == 10))
5341 yy[E] = 0x7fff; /* infinity */
5344 if ((exp < MINDECEXP) && (base == 10))
5354 /* Base 16 hexadecimal floating constant. */
5355 if ((k = enormlz (yy)) > NBITS)
5360 /* Adjust the exponent. NEXP is the number of hex digits,
5361 EXP is a power of 2. */
5362 lexp = (EXONE - 1 + NBITS) - k + yy[E] + exp - nexp;
5372 /* Pad trailing zeros to minimize power of 10, per IEEE spec. */
5373 while ((nexp > 0) && (yy[2] == 0))
5385 if ((k = enormlz (yy)) > NBITS)
5390 lexp = (EXONE - 1 + NBITS) - k;
5391 emdnorm (yy, lost, 0, lexp, 64);
5394 /* Convert to external format:
5396 Multiply by 10**nexp. If precision is 64 bits,
5397 the maximum relative error incurred in forming 10**n
5398 for 0 <= n <= 324 is 8.2e-20, at 10**180.
5399 For 0 <= n <= 999, the peak relative error is 1.4e-19 at 10**947.
5400 For 0 >= n >= -999, it is -1.55e-19 at 10**-435. */
5415 /* Punt. Can't handle this without 2 divides. */
5416 emovi (etens[0], tt);
5429 emul (etens[i], xt, xt);
5433 while (exp <= MAXP);
5452 /* Round and convert directly to the destination type */
5454 lexp -= EXONE - 0x3ff;
5456 else if (oprec == 24 || oprec == 32)
5457 lexp -= (EXONE - 0x7f);
5460 else if (oprec == 24 || oprec == 56)
5461 lexp -= EXONE - (0x41 << 2);
5463 else if (oprec == 24)
5464 lexp -= EXONE - 0177;
5468 else if (oprec == 56)
5469 lexp -= EXONE - 0201;
5472 emdnorm (yy, lost, 0, lexp, 64);
5482 todec (yy, y); /* see etodec.c */
5487 toibm (yy, y, DFmode);
5492 toc4x (yy, y, HFmode);
5505 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
5518 /* Return Y = largest integer not greater than X (truncated toward minus
5521 static const UEMUSHORT bmask[] =
5544 const UEMUSHORT x[];
5551 emov (x, f); /* leave in external format */
5552 expon = (int) f[NE - 1];
5553 e = (expon & 0x7fff) - (EXONE - 1);
5559 /* number of bits to clear out */
5571 /* clear the remaining bits */
5573 /* truncate negatives toward minus infinity */
5576 if ((UEMUSHORT) expon & (UEMUSHORT) 0x8000)
5578 for (i = 0; i < NE - 1; i++)
5591 /* Return S and EXP such that S * 2^EXP = X and .5 <= S < 1.
5592 For example, 1.1 = 0.55 * 2^1. */
5596 const UEMUSHORT x[];
5604 /* Handle denormalized numbers properly using long integer exponent. */
5605 li = (EMULONG) ((EMUSHORT) xi[1]);
5613 *exp = (int) (li - 0x3ffe);
5617 /* Return e type Y = X * 2^PWR2. */
5621 const UEMUSHORT x[];
5633 emdnorm (xi, i, i, li, !ROUND_TOWARDS_ZERO);
5639 /* C = remainder after dividing B by A, all e type values.
5640 Least significant integer quotient bits left in EQUOT. */
5644 const UEMUSHORT a[], b[];
5647 UEMUSHORT den[NI], num[NI];
5651 || (ecmp (a, ezero) == 0)
5659 if (ecmp (a, ezero) == 0)
5661 mtherr ("eremain", SING);
5667 eiremain (den, num);
5668 /* Sign of remainder = sign of quotient */
5677 /* Return quotient of exploded e-types NUM / DEN in EQUOT,
5678 remainder in NUM. */
5682 UEMUSHORT den[], num[];
5688 ld -= enormlz (den);
5690 ln -= enormlz (num);
5694 if (ecmpm (den, num) <= 0)
5706 emdnorm (num, 0, 0, ln, 0);
5709 /* Report an error condition CODE encountered in function NAME.
5711 Mnemonic Value Significance
5713 DOMAIN 1 argument domain error
5714 SING 2 function singularity
5715 OVERFLOW 3 overflow range error
5716 UNDERFLOW 4 underflow range error
5717 TLOSS 5 total loss of precision
5718 PLOSS 6 partial loss of precision
5719 INVALID 7 NaN - producing operation
5720 EDOM 33 Unix domain error code
5721 ERANGE 34 Unix range error code
5723 The order of appearance of the following messages is bound to the
5724 error codes defined above. */
5734 /* The string passed by the calling program is supposed to be the
5735 name of the function in which the error occurred.
5736 The code argument selects which error message string will be printed. */
5738 if (strcmp (name, "esub") == 0)
5739 name = "subtraction";
5740 else if (strcmp (name, "ediv") == 0)
5742 else if (strcmp (name, "emul") == 0)
5743 name = "multiplication";
5744 else if (strcmp (name, "enormlz") == 0)
5745 name = "normalization";
5746 else if (strcmp (name, "etoasc") == 0)
5747 name = "conversion to text";
5748 else if (strcmp (name, "asctoe") == 0)
5750 else if (strcmp (name, "eremain") == 0)
5752 else if (strcmp (name, "esqrt") == 0)
5753 name = "square root";
5758 case DOMAIN: warning ("%s: argument domain error" , name); break;
5759 case SING: warning ("%s: function singularity" , name); break;
5760 case OVERFLOW: warning ("%s: overflow range error" , name); break;
5761 case UNDERFLOW: warning ("%s: underflow range error" , name); break;
5762 case TLOSS: warning ("%s: total loss of precision" , name); break;
5763 case PLOSS: warning ("%s: partial loss of precision", name); break;
5764 case INVALID: warning ("%s: NaN - producing operation", name); break;
5769 /* Set global error message word */
5774 /* Convert DEC double precision D to e type E. */
5784 ecleaz (y); /* start with a zero */
5785 p = y; /* point to our number */
5786 r = *d; /* get DEC exponent word */
5787 if (*d & (unsigned int) 0x8000)
5788 *p = 0xffff; /* fill in our sign */
5789 ++p; /* bump pointer to our exponent word */
5790 r &= 0x7fff; /* strip the sign bit */
5791 if (r == 0) /* answer = 0 if high order DEC word = 0 */
5795 r >>= 7; /* shift exponent word down 7 bits */
5796 r += EXONE - 0201; /* subtract DEC exponent offset */
5797 /* add our e type exponent offset */
5798 *p++ = r; /* to form our exponent */
5800 r = *d++; /* now do the high order mantissa */
5801 r &= 0177; /* strip off the DEC exponent and sign bits */
5802 r |= 0200; /* the DEC understood high order mantissa bit */
5803 *p++ = r; /* put result in our high guard word */
5805 *p++ = *d++; /* fill in the rest of our mantissa */
5809 eshdn8 (y); /* shift our mantissa down 8 bits */
5814 /* Convert e type X to DEC double precision D. */
5826 /* Adjust exponent for offsets. */
5827 exp = (EMULONG) xi[E] - (EXONE - 0201);
5828 /* Round off to nearest or even. */
5831 emdnorm (xi, 0, 0, exp, !ROUND_TOWARDS_ZERO);
5836 /* Convert exploded e-type X, that has already been rounded to
5837 56-bit precision, to DEC format double Y. */
5883 /* Convert IBM single/double precision to e type. */
5889 enum machine_mode mode;
5894 ecleaz (y); /* start with a zero */
5895 p = y; /* point to our number */
5896 r = *d; /* get IBM exponent word */
5897 if (*d & (unsigned int) 0x8000)
5898 *p = 0xffff; /* fill in our sign */
5899 ++p; /* bump pointer to our exponent word */
5900 r &= 0x7f00; /* strip the sign bit */
5901 r >>= 6; /* shift exponent word down 6 bits */
5902 /* in fact shift by 8 right and 2 left */
5903 r += EXONE - (0x41 << 2); /* subtract IBM exponent offset */
5904 /* add our e type exponent offset */
5905 *p++ = r; /* to form our exponent */
5907 *p++ = *d++ & 0xff; /* now do the high order mantissa */
5908 /* strip off the IBM exponent and sign bits */
5909 if (mode != SFmode) /* there are only 2 words in SFmode */
5911 *p++ = *d++; /* fill in the rest of our mantissa */
5916 if (y[M] == 0 && y[M+1] == 0 && y[M+2] == 0 && y[M+3] == 0)
5919 y[E] -= 5 + enormlz (y); /* now normalise the mantissa */
5920 /* handle change in RADIX */
5926 /* Convert e type to IBM single/double precision. */
5932 enum machine_mode mode;
5939 exp = (EMULONG) xi[E] - (EXONE - (0x41 << 2)); /* adjust exponent for offsets */
5940 /* round off to nearest or even */
5943 emdnorm (xi, 0, 0, exp, !ROUND_TOWARDS_ZERO);
5945 toibm (xi, d, mode);
5951 enum machine_mode mode;
6004 /* Convert C4X single/double precision to e type. */
6010 enum machine_mode mode;
6028 /* Short-circuit the zero case. */
6029 if ((dn[0] == 0x8000)
6030 && (dn[1] == 0x0000)
6031 && ((mode == QFmode) || ((dn[2] == 0x0000) && (dn[3] == 0x0000))))
6042 ecleaz (y); /* start with a zero */
6043 r = dn[0]; /* get sign/exponent part */
6044 if (r & (unsigned int) 0x0080)
6046 y[0] = 0xffff; /* fill in our sign */
6052 r >>= 8; /* Shift exponent word down 8 bits. */
6053 if (r & 0x80) /* Make the exponent negative if it is. */
6054 r = r | (~0 & ~0xff);
6058 /* Now do the high order mantissa. We don't "or" on the high bit
6059 because it is 2 (not 1) and is handled a little differently
6061 y[M] = dn[0] & 0x7f;
6064 if (mode != QFmode) /* There are only 2 words in QFmode. */
6066 y[M+2] = dn[2]; /* Fill in the rest of our mantissa. */
6074 /* Now do the two's complement on the data. */
6076 carry = 1; /* Initially add 1 for the two's complement. */
6077 for (i=size + M; i > M; i--)
6079 if (carry && (y[i] == 0x0000))
6080 /* We overflowed into the next word, carry is the same. */
6081 y[i] = carry ? 0x0000 : 0xffff;
6084 /* No overflow, just invert and add carry. */
6085 y[i] = ((~y[i]) + carry) & 0xffff;
6100 /* Add our e type exponent offset to form our exponent. */
6104 /* Now do the high order mantissa strip off the exponent and sign
6105 bits and add the high 1 bit. */
6106 y[M] = (dn[0] & 0x7f) | 0x80;
6109 if (mode != QFmode) /* There are only 2 words in QFmode. */
6111 y[M+2] = dn[2]; /* Fill in the rest of our mantissa. */
6121 /* Convert e type to C4X single/double precision. */
6127 enum machine_mode mode;
6135 /* Adjust exponent for offsets. */
6136 exp = (EMULONG) xi[E] - (EXONE - 0x7f);
6138 /* Round off to nearest or even. */
6140 rndprc = mode == QFmode ? 24 : 32;
6141 emdnorm (xi, 0, 0, exp, !ROUND_TOWARDS_ZERO);
6143 toc4x (xi, d, mode);
6149 enum machine_mode mode;
6155 /* Short-circuit the zero case */
6156 if ((x[0] == 0) /* Zero exponent and sign */
6158 && (x[M] == 0) /* The rest is for zero mantissa */
6160 /* Only check for double if necessary */
6161 && ((mode == QFmode) || ((x[M+2] == 0) && (x[M+3] == 0))))
6163 /* We have a zero. Put it into the output and return. */
6176 /* Negative number require a two's complement conversion of the
6182 i = ((int) x[1]) - 0x7f;
6184 /* Now add 1 to the inverted data to do the two's complement. */
6193 x[v] = carry ? 0x0000 : 0xffff;
6196 x[v] = ((~x[v]) + carry) & 0xffff;
6202 /* The following is a special case. The C4X negative float requires
6203 a zero in the high bit (because the format is (2 - x) x 2^m), so
6204 if a one is in that bit, we have to shift left one to get rid
6205 of it. This only occurs if the number is -1 x 2^m. */
6206 if (x[M+1] & 0x8000)
6208 /* This is the case of -1 x 2^m, we have to rid ourselves of the
6209 high sign bit and shift the exponent. */
6215 i = ((int) x[1]) - 0x7f;
6217 if ((i < -128) || (i > 127))
6225 y[3] = (y[1] << 8) | ((y[2] >> 8) & 0xff);
6226 y[2] = (y[0] << 8) | ((y[1] >> 8) & 0xff);
6234 y[0] |= ((i & 0xff) << 8);
6238 y[0] |= x[M] & 0x7f;
6244 y[3] = (y[1] << 8) | ((y[2] >> 8) & 0xff);
6245 y[2] = (y[0] << 8) | ((y[1] >> 8) & 0xff);
6250 /* Output a binary NaN bit pattern in the target machine's format. */
6252 /* If special NaN bit patterns are required, define them in tm.h
6253 as arrays of unsigned 16-bit shorts. Otherwise, use the default
6259 static const UEMUSHORT TFbignan[8] =
6260 {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
6261 static const UEMUSHORT TFlittlenan[8] = {0, 0, 0, 0, 0, 0, 0x8000, 0xffff};
6269 static const UEMUSHORT XFbignan[6] =
6270 {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
6271 static const UEMUSHORT XFlittlenan[6] = {0, 0, 0, 0xc000, 0xffff, 0};
6279 static const UEMUSHORT DFbignan[4] = {0x7fff, 0xffff, 0xffff, 0xffff};
6280 static const UEMUSHORT DFlittlenan[4] = {0, 0, 0, 0xfff8};
6288 static const UEMUSHORT SFbignan[2] = {0x7fff, 0xffff};
6289 static const UEMUSHORT SFlittlenan[2] = {0, 0xffc0};
6296 make_nan (nan, sign, mode)
6299 enum machine_mode mode;
6305 size = GET_MODE_BITSIZE (mode);
6306 if (LARGEST_EXPONENT_IS_NORMAL (size))
6308 warning ("%d-bit floats cannot hold NaNs", size);
6309 saturate (nan, sign, size, 0);
6314 /* Possibly the `reserved operand' patterns on a VAX can be
6315 used like NaN's, but probably not in the same way as IEEE. */
6316 #if !defined(DEC) && !defined(IBM) && !defined(C4X)
6318 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
6320 if (REAL_WORDS_BIG_ENDIAN)
6330 if (REAL_WORDS_BIG_ENDIAN)
6338 if (REAL_WORDS_BIG_ENDIAN)
6347 if (REAL_WORDS_BIG_ENDIAN)
6357 if (REAL_WORDS_BIG_ENDIAN)
6358 *nan++ = (sign << 15) | (*p++ & 0x7fff);
6361 if (! REAL_WORDS_BIG_ENDIAN)
6362 *nan = (sign << 15) | (*p & 0x7fff);
6367 /* Create a saturation value for a SIZE-bit float, assuming that
6368 LARGEST_EXPONENT_IS_NORMAL (SIZE).
6370 If SIGN is true, fill X with the most negative value, otherwise fill
6371 it with the most positive value. WARN is true if the function should
6372 warn about overflow. */
6375 saturate (x, sign, size, warn)
6377 int sign, size, warn;
6381 if (warn && extra_warnings)
6382 warning ("value exceeds the range of a %d-bit float", size);
6384 /* Create the most negative value. */
6385 for (i = 0; i < size / EMUSHORT_SIZE; i++)
6388 /* Make it positive, if necessary. */
6390 x[REAL_WORDS_BIG_ENDIAN? 0 : i - 1] = 0x7fff;
6394 /* This is the inverse of the function `etarsingle' invoked by
6395 REAL_VALUE_TO_TARGET_SINGLE. */
6398 ereal_unto_float (f)
6405 /* Convert 32 bit integer to array of 16 bit pieces in target machine order.
6406 This is the inverse operation to what the function `endian' does. */
6407 if (REAL_WORDS_BIG_ENDIAN)
6409 s[0] = (UEMUSHORT) (f >> 16);
6410 s[1] = (UEMUSHORT) f;
6414 s[0] = (UEMUSHORT) f;
6415 s[1] = (UEMUSHORT) (f >> 16);
6417 /* Convert and promote the target float to E-type. */
6419 /* Output E-type to REAL_VALUE_TYPE. */
6425 /* This is the inverse of the function `etardouble' invoked by
6426 REAL_VALUE_TO_TARGET_DOUBLE. */
6429 ereal_unto_double (d)
6436 /* Convert array of HOST_WIDE_INT to equivalent array of 16-bit pieces. */
6437 if (REAL_WORDS_BIG_ENDIAN)
6439 s[0] = (UEMUSHORT) (d[0] >> 16);
6440 s[1] = (UEMUSHORT) d[0];
6441 s[2] = (UEMUSHORT) (d[1] >> 16);
6442 s[3] = (UEMUSHORT) d[1];
6446 /* Target float words are little-endian. */
6447 s[0] = (UEMUSHORT) d[0];
6448 s[1] = (UEMUSHORT) (d[0] >> 16);
6449 s[2] = (UEMUSHORT) d[1];
6450 s[3] = (UEMUSHORT) (d[1] >> 16);
6452 /* Convert target double to E-type. */
6454 /* Output E-type to REAL_VALUE_TYPE. */
6460 /* Convert an SFmode target `float' value to a REAL_VALUE_TYPE.
6461 This is somewhat like ereal_unto_float, but the input types
6462 for these are different. */
6465 ereal_from_float (f)
6472 /* Convert 32 bit integer to array of 16 bit pieces in target machine order.
6473 This is the inverse operation to what the function `endian' does. */
6474 if (REAL_WORDS_BIG_ENDIAN)
6476 s[0] = (UEMUSHORT) (f >> 16);
6477 s[1] = (UEMUSHORT) f;
6481 s[0] = (UEMUSHORT) f;
6482 s[1] = (UEMUSHORT) (f >> 16);
6484 /* Convert and promote the target float to E-type. */
6486 /* Output E-type to REAL_VALUE_TYPE. */
6492 /* Convert a DFmode target `double' value to a REAL_VALUE_TYPE.
6493 This is somewhat like ereal_unto_double, but the input types
6494 for these are different.
6496 The DFmode is stored as an array of HOST_WIDE_INT in the target's
6497 data format, with no holes in the bit packing. The first element
6498 of the input array holds the bits that would come first in the
6499 target computer's memory. */
6502 ereal_from_double (d)
6509 /* Convert array of HOST_WIDE_INT to equivalent array of 16-bit pieces. */
6510 if (REAL_WORDS_BIG_ENDIAN)
6512 #if HOST_BITS_PER_WIDE_INT == 32
6513 s[0] = (UEMUSHORT) (d[0] >> 16);
6514 s[1] = (UEMUSHORT) d[0];
6515 s[2] = (UEMUSHORT) (d[1] >> 16);
6516 s[3] = (UEMUSHORT) d[1];
6518 /* In this case the entire target double is contained in the
6519 first array element. The second element of the input is
6521 s[0] = (UEMUSHORT) (d[0] >> 48);
6522 s[1] = (UEMUSHORT) (d[0] >> 32);
6523 s[2] = (UEMUSHORT) (d[0] >> 16);
6524 s[3] = (UEMUSHORT) d[0];
6529 /* Target float words are little-endian. */
6530 s[0] = (UEMUSHORT) d[0];
6531 s[1] = (UEMUSHORT) (d[0] >> 16);
6532 #if HOST_BITS_PER_WIDE_INT == 32
6533 s[2] = (UEMUSHORT) d[1];
6534 s[3] = (UEMUSHORT) (d[1] >> 16);
6536 s[2] = (UEMUSHORT) (d[0] >> 32);
6537 s[3] = (UEMUSHORT) (d[0] >> 48);
6540 /* Convert target double to E-type. */
6542 /* Output E-type to REAL_VALUE_TYPE. */
6549 /* Convert target computer unsigned 64-bit integer to e-type.
6550 The endian-ness of DImode follows the convention for integers,
6551 so we use WORDS_BIG_ENDIAN here, not REAL_WORDS_BIG_ENDIAN. */
6555 const UEMUSHORT *di; /* Address of the 64-bit int. */
6562 if (WORDS_BIG_ENDIAN)
6564 for (k = M; k < M + 4; k++)
6569 for (k = M + 3; k >= M; k--)
6572 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
6573 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
6574 ecleaz (yi); /* it was zero */
6576 yi[E] -= (UEMUSHORT) k;/* subtract shift count from exponent */
6580 /* Convert target computer signed 64-bit integer to e-type. */
6584 const UEMUSHORT *di; /* Address of the 64-bit int. */
6587 unsigned EMULONG acc;
6593 if (WORDS_BIG_ENDIAN)
6595 for (k = M; k < M + 4; k++)
6600 for (k = M + 3; k >= M; k--)
6603 /* Take absolute value */
6609 for (k = M + 3; k >= M; k--)
6611 acc = (unsigned EMULONG) (~yi[k] & 0xffff) + carry;
6618 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
6619 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
6620 ecleaz (yi); /* it was zero */
6622 yi[E] -= (UEMUSHORT) k;/* subtract shift count from exponent */
6629 /* Convert e-type to unsigned 64-bit int. */
6645 k = (int) xi[E] - (EXONE - 1);
6648 for (j = 0; j < 4; j++)
6654 for (j = 0; j < 4; j++)
6657 warning ("overflow on truncation to integer");
6662 /* Shift more than 16 bits: first shift up k-16 mod 16,
6663 then shift up by 16's. */
6664 j = k - ((k >> 4) << 4);
6668 if (WORDS_BIG_ENDIAN)
6679 if (WORDS_BIG_ENDIAN)
6684 while ((k -= 16) > 0);
6688 /* shift not more than 16 bits */
6693 if (WORDS_BIG_ENDIAN)
6712 /* Convert e-type to signed 64-bit int. */
6719 unsigned EMULONG acc;
6726 k = (int) xi[E] - (EXONE - 1);
6729 for (j = 0; j < 4; j++)
6735 for (j = 0; j < 4; j++)
6738 warning ("overflow on truncation to integer");
6744 /* Shift more than 16 bits: first shift up k-16 mod 16,
6745 then shift up by 16's. */
6746 j = k - ((k >> 4) << 4);
6750 if (WORDS_BIG_ENDIAN)
6761 if (WORDS_BIG_ENDIAN)
6766 while ((k -= 16) > 0);
6770 /* shift not more than 16 bits */
6773 if (WORDS_BIG_ENDIAN)
6789 /* Negate if negative */
6793 if (WORDS_BIG_ENDIAN)
6795 for (k = 0; k < 4; k++)
6797 acc = (unsigned EMULONG) (~(*isave) & 0xffff) + carry;
6798 if (WORDS_BIG_ENDIAN)
6810 /* Longhand square root routine. */
6813 static int esqinited = 0;
6814 static unsigned short sqrndbit[NI];
6821 UEMUSHORT temp[NI], num[NI], sq[NI], xx[NI];
6823 int i, j, k, n, nlups;
6828 sqrndbit[NI - 2] = 1;
6831 /* Check for arg <= 0 */
6832 i = ecmp (x, ezero);
6837 mtherr ("esqrt", DOMAIN);
6853 /* Bring in the arg and renormalize if it is denormal. */
6855 m = (EMULONG) xx[1]; /* local long word exponent */
6859 /* Divide exponent by 2 */
6861 exp = (unsigned short) ((m / 2) + 0x3ffe);
6863 /* Adjust if exponent odd */
6873 n = 8; /* get 8 bits of result per inner loop */
6879 /* bring in next word of arg */
6881 num[NI - 1] = xx[j + 3];
6882 /* Do additional bit on last outer loop, for roundoff. */
6885 for (i = 0; i < n; i++)
6887 /* Next 2 bits of arg */
6890 /* Shift up answer */
6892 /* Make trial divisor */
6893 for (k = 0; k < NI; k++)
6896 eaddm (sqrndbit, temp);
6897 /* Subtract and insert answer bit if it goes in */
6898 if (ecmpm (temp, num) <= 0)
6908 /* Adjust for extra, roundoff loop done. */
6909 exp += (NBITS - 1) - rndprc;
6911 /* Sticky bit = 1 if the remainder is nonzero. */
6913 for (i = 3; i < NI; i++)
6916 /* Renormalize and round off. */
6917 emdnorm (sq, k, 0, exp, !ROUND_TOWARDS_ZERO);
6921 #endif /* EMU_NON_COMPILE not defined */
6923 /* Return the binary precision of the significand for a given
6924 floating point mode. The mode can hold an integer value
6925 that many bits wide, without losing any bits. */
6928 significand_size (mode)
6929 enum machine_mode mode;
6932 /* Don't test the modes, but their sizes, lest this
6933 code won't work for BITS_PER_UNIT != 8 . */
6935 switch (GET_MODE_BITSIZE (mode))
6939 #if TARGET_FLOAT_FORMAT == C4X_FLOAT_FORMAT
6946 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
6949 #if TARGET_FLOAT_FORMAT == IBM_FLOAT_FORMAT
6952 #if TARGET_FLOAT_FORMAT == VAX_FLOAT_FORMAT
6955 #if TARGET_FLOAT_FORMAT == C4X_FLOAT_FORMAT
6968 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)