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 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 To support cross compilation between IEEE, VAX and IBM floating
34 point formats, define REAL_ARITHMETIC in the tm.h file.
36 In either case the machine files (tm.h) must not contain any code
37 that tries to use host floating point arithmetic to convert
38 REAL_VALUE_TYPEs from `double' to `float', pass them to fprintf,
39 etc. In cross-compile situations a REAL_VALUE_TYPE may not
40 be intelligible to the host computer's native arithmetic.
42 The emulator defaults to the host's floating point format so that
43 its decimal conversion functions can be used if desired (see
46 The first part of this file interfaces gcc to a floating point
47 arithmetic suite that was not written with gcc in mind. Avoid
48 changing the low-level arithmetic routines unless you have suitable
49 test programs available. A special version of the PARANOIA floating
50 point arithmetic tester, modified for this purpose, can be found on
51 usc.edu: /pub/C-numanal/ieeetest.zoo. Other tests, and libraries of
52 XFmode and TFmode transcendental functions, can be obtained by ftp from
53 netlib.att.com: netlib/cephes. */
55 /* Type of computer arithmetic.
56 Only one of DEC, IBM, IEEE, C4X, or UNK should get defined.
58 `IEEE', when REAL_WORDS_BIG_ENDIAN is non-zero, refers generically
59 to big-endian IEEE floating-point data structure. This definition
60 should work in SFmode `float' type and DFmode `double' type on
61 virtually all big-endian IEEE machines. If LONG_DOUBLE_TYPE_SIZE
62 has been defined to be 96, then IEEE also invokes the particular
63 XFmode (`long double' type) data structure used by the Motorola
64 680x0 series processors.
66 `IEEE', when REAL_WORDS_BIG_ENDIAN is zero, refers generally to
67 little-endian IEEE machines. In this case, if LONG_DOUBLE_TYPE_SIZE
68 has been defined to be 96, then IEEE also invokes the particular
69 XFmode `long double' data structure used by the Intel 80x86 series
72 `DEC' refers specifically to the Digital Equipment Corp PDP-11
73 and VAX floating point data structure. This model currently
74 supports no type wider than DFmode.
76 `IBM' refers specifically to the IBM System/370 and compatible
77 floating point data structure. This model currently supports
78 no type wider than DFmode. The IBM conversions were contributed by
79 frank@atom.ansto.gov.au (Frank Crawford).
81 `C4X' refers specifically to the floating point format used on
82 Texas Instruments TMS320C3x and TMS320C4x digital signal
83 processors. This supports QFmode (32-bit float, double) and HFmode
84 (40-bit long double) where BITS_PER_BYTE is 32. Unlike IEEE
85 floats, C4x floats are not rounded to be even. The C4x conversions
86 were contributed by m.hayes@elec.canterbury.ac.nz (Michael Hayes) and
87 Haj.Ten.Brugge@net.HCC.nl (Herman ten Brugge).
89 If LONG_DOUBLE_TYPE_SIZE = 64 (the default, unless tm.h defines it)
90 then `long double' and `double' are both implemented, but they
91 both mean DFmode. In this case, the software floating-point
92 support available here is activated by writing
93 #define REAL_ARITHMETIC
96 The case LONG_DOUBLE_TYPE_SIZE = 128 activates TFmode support
97 and may deactivate XFmode since `long double' is used to refer
98 to both modes. Defining INTEL_EXTENDED_IEEE_FORMAT to non-zero
99 at the same time enables 80387-style 80-bit floats in a 128-bit
100 padded image, as seen on IA-64.
102 The macros FLOAT_WORDS_BIG_ENDIAN, HOST_FLOAT_WORDS_BIG_ENDIAN,
103 contributed by Richard Earnshaw <Richard.Earnshaw@cl.cam.ac.uk>,
104 separate the floating point unit's endian-ness from that of
105 the integer addressing. This permits one to define a big-endian
106 FPU on a little-endian machine (e.g., ARM). An extension to
107 BYTES_BIG_ENDIAN may be required for some machines in the future.
108 These optional macros may be defined in tm.h. In real.h, they
109 default to WORDS_BIG_ENDIAN, etc., so there is no need to define
110 them for any normal host or target machine on which the floats
111 and the integers have the same endian-ness. */
114 /* The following converts gcc macros into the ones used by this file. */
116 /* REAL_ARITHMETIC defined means that macros in real.h are
117 defined to call emulator functions. */
118 #ifdef REAL_ARITHMETIC
120 #if TARGET_FLOAT_FORMAT == VAX_FLOAT_FORMAT
121 /* PDP-11, Pro350, VAX: */
123 #else /* it's not VAX */
124 #if TARGET_FLOAT_FORMAT == IBM_FLOAT_FORMAT
125 /* IBM System/370 style */
127 #else /* it's also not an IBM */
128 #if TARGET_FLOAT_FORMAT == C4X_FLOAT_FORMAT
129 /* TMS320C3x/C4x style */
131 #else /* it's also not a C4X */
132 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
134 #else /* it's not IEEE either */
135 /* UNKnown arithmetic. We don't support this and can't go on. */
136 unknown arithmetic type
138 #endif /* not IEEE */
143 #define REAL_WORDS_BIG_ENDIAN FLOAT_WORDS_BIG_ENDIAN
146 /* REAL_ARITHMETIC not defined means that the *host's* data
147 structure will be used. It may differ by endian-ness from the
148 target machine's structure and will get its ends swapped
149 accordingly (but not here). Probably only the decimal <-> binary
150 functions in this file will actually be used in this case. */
152 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
154 #else /* it's not VAX */
155 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
156 /* IBM System/370 style */
158 #else /* it's also not an IBM */
159 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
161 #else /* it's not IEEE either */
162 unknown arithmetic type
164 #endif /* not IEEE */
168 #define REAL_WORDS_BIG_ENDIAN HOST_FLOAT_WORDS_BIG_ENDIAN
170 #endif /* REAL_ARITHMETIC not defined */
172 /* Define INFINITY for support of infinity.
173 Define NANS for support of Not-a-Number's (NaN's). */
174 #if !defined(DEC) && !defined(IBM) && !defined(C4X)
179 /* Support of NaNs requires support of infinity. */
186 /* Find a host integer type that is at least 16 bits wide,
187 and another type at least twice whatever that size is. */
189 #if HOST_BITS_PER_CHAR >= 16
190 #define EMUSHORT char
191 #define EMUSHORT_SIZE HOST_BITS_PER_CHAR
192 #define EMULONG_SIZE (2 * HOST_BITS_PER_CHAR)
194 #if HOST_BITS_PER_SHORT >= 16
195 #define EMUSHORT short
196 #define EMUSHORT_SIZE HOST_BITS_PER_SHORT
197 #define EMULONG_SIZE (2 * HOST_BITS_PER_SHORT)
199 #if HOST_BITS_PER_INT >= 16
201 #define EMUSHORT_SIZE HOST_BITS_PER_INT
202 #define EMULONG_SIZE (2 * HOST_BITS_PER_INT)
204 #if HOST_BITS_PER_LONG >= 16
205 #define EMUSHORT long
206 #define EMUSHORT_SIZE HOST_BITS_PER_LONG
207 #define EMULONG_SIZE (2 * HOST_BITS_PER_LONG)
209 /* You will have to modify this program to have a smaller unit size. */
210 #define EMU_NON_COMPILE
216 /* If no 16-bit type has been found and the compiler is GCC, try HImode. */
217 #if defined(__GNUC__) && EMUSHORT_SIZE != 16
218 typedef int HItype __attribute__ ((mode (HI)));
219 typedef unsigned int UHItype __attribute__ ((mode (HI)));
223 #define EMUSHORT HItype
224 #define UEMUSHORT UHItype
225 #define EMUSHORT_SIZE 16
226 #define EMULONG_SIZE 32
228 #define UEMUSHORT unsigned EMUSHORT
231 #if HOST_BITS_PER_SHORT >= EMULONG_SIZE
232 #define EMULONG short
234 #if HOST_BITS_PER_INT >= EMULONG_SIZE
237 #if HOST_BITS_PER_LONG >= EMULONG_SIZE
240 #if HOST_BITS_PER_LONGLONG >= EMULONG_SIZE
241 #define EMULONG long long int
243 /* You will have to modify this program to have a smaller unit size. */
244 #define EMU_NON_COMPILE
251 /* The host interface doesn't work if no 16-bit size exists. */
252 #if EMUSHORT_SIZE != 16
253 #define EMU_NON_COMPILE
256 /* OK to continue compilation. */
257 #ifndef EMU_NON_COMPILE
259 /* Construct macros to translate between REAL_VALUE_TYPE and e type.
260 In GET_REAL and PUT_REAL, r and e are pointers.
261 A REAL_VALUE_TYPE is guaranteed to occupy contiguous locations
262 in memory, with no holes. */
264 #if MAX_LONG_DOUBLE_TYPE_SIZE == 96 || \
265 ((INTEL_EXTENDED_IEEE_FORMAT != 0) && MAX_LONG_DOUBLE_TYPE_SIZE == 128)
266 /* Number of 16 bit words in external e type format */
268 # define MAXDECEXP 4932
269 # define MINDECEXP -4956
270 # define GET_REAL(r,e) memcpy ((char *)(e), (char *)(r), 2*NE)
271 # define PUT_REAL(e,r) \
273 memcpy ((char *)(r), (char *)(e), 2*NE); \
274 if (2*NE < sizeof(*r)) \
275 memset ((char *)(r) + 2*NE, 0, sizeof(*r) - 2*NE); \
277 # else /* no XFmode */
278 # if MAX_LONG_DOUBLE_TYPE_SIZE == 128
280 # define MAXDECEXP 4932
281 # define MINDECEXP -4977
282 # define GET_REAL(r,e) memcpy ((char *)(e), (char *)(r), 2*NE)
283 # define PUT_REAL(e,r) \
285 memcpy ((char *)(r), (char *)(e), 2*NE); \
286 if (2*NE < sizeof(*r)) \
287 memset ((char *)(r) + 2*NE, 0, sizeof(*r) - 2*NE); \
291 #define MAXDECEXP 4932
292 #define MINDECEXP -4956
293 #ifdef REAL_ARITHMETIC
294 /* Emulator uses target format internally
295 but host stores it in host endian-ness. */
297 #define GET_REAL(r,e) \
299 if (HOST_FLOAT_WORDS_BIG_ENDIAN == REAL_WORDS_BIG_ENDIAN) \
300 e53toe ((UEMUSHORT *) (r), (e)); \
304 memcpy (&w[3], ((EMUSHORT *) r), sizeof (EMUSHORT)); \
305 memcpy (&w[2], ((EMUSHORT *) r) + 1, sizeof (EMUSHORT)); \
306 memcpy (&w[1], ((EMUSHORT *) r) + 2, sizeof (EMUSHORT)); \
307 memcpy (&w[0], ((EMUSHORT *) r) + 3, sizeof (EMUSHORT)); \
312 #define PUT_REAL(e,r) \
314 if (HOST_FLOAT_WORDS_BIG_ENDIAN == REAL_WORDS_BIG_ENDIAN) \
315 etoe53 ((e), (UEMUSHORT *) (r)); \
320 memcpy (((EMUSHORT *) r), &w[3], sizeof (EMUSHORT)); \
321 memcpy (((EMUSHORT *) r) + 1, &w[2], sizeof (EMUSHORT)); \
322 memcpy (((EMUSHORT *) r) + 2, &w[1], sizeof (EMUSHORT)); \
323 memcpy (((EMUSHORT *) r) + 3, &w[0], sizeof (EMUSHORT)); \
327 #else /* not REAL_ARITHMETIC */
329 /* emulator uses host format */
330 #define GET_REAL(r,e) e53toe ((UEMUSHORT *) (r), (e))
331 #define PUT_REAL(e,r) etoe53 ((e), (UEMUSHORT *) (r))
333 #endif /* not REAL_ARITHMETIC */
334 #endif /* not TFmode */
335 #endif /* not XFmode */
338 /* Number of 16 bit words in internal format */
341 /* Array offset to exponent */
344 /* Array offset to high guard word */
347 /* Number of bits of precision */
348 #define NBITS ((NI-4)*16)
350 /* Maximum number of decimal digits in ASCII conversion
353 #define NDEC (NBITS*8/27)
355 /* The exponent of 1.0 */
356 #define EXONE (0x3fff)
358 #if defined(HOST_EBCDIC)
359 /* bit 8 is significant in EBCDIC */
360 #define CHARMASK 0xff
362 #define CHARMASK 0x7f
365 extern int extra_warnings;
366 extern UEMUSHORT ezero[], ehalf[], eone[], etwo[];
367 extern UEMUSHORT elog2[], esqrt2[];
369 static void endian PARAMS ((UEMUSHORT *, long *,
371 static void eclear PARAMS ((UEMUSHORT *));
372 static void emov PARAMS ((UEMUSHORT *, UEMUSHORT *));
374 static void eabs PARAMS ((UEMUSHORT *));
376 static void eneg PARAMS ((UEMUSHORT *));
377 static int eisneg PARAMS ((UEMUSHORT *));
378 static int eisinf PARAMS ((UEMUSHORT *));
379 static int eisnan PARAMS ((UEMUSHORT *));
380 static void einfin PARAMS ((UEMUSHORT *));
382 static void enan PARAMS ((UEMUSHORT *, int));
383 static void einan PARAMS ((UEMUSHORT *));
384 static int eiisnan PARAMS ((UEMUSHORT *));
385 static int eiisneg PARAMS ((UEMUSHORT *));
386 static void make_nan PARAMS ((UEMUSHORT *, int, enum machine_mode));
388 static void emovi PARAMS ((UEMUSHORT *, UEMUSHORT *));
389 static void emovo PARAMS ((UEMUSHORT *, UEMUSHORT *));
390 static void ecleaz PARAMS ((UEMUSHORT *));
391 static void ecleazs PARAMS ((UEMUSHORT *));
392 static void emovz PARAMS ((UEMUSHORT *, UEMUSHORT *));
394 static void eiinfin PARAMS ((UEMUSHORT *));
397 static int eiisinf PARAMS ((UEMUSHORT *));
399 static int ecmpm PARAMS ((UEMUSHORT *, UEMUSHORT *));
400 static void eshdn1 PARAMS ((UEMUSHORT *));
401 static void eshup1 PARAMS ((UEMUSHORT *));
402 static void eshdn8 PARAMS ((UEMUSHORT *));
403 static void eshup8 PARAMS ((UEMUSHORT *));
404 static void eshup6 PARAMS ((UEMUSHORT *));
405 static void eshdn6 PARAMS ((UEMUSHORT *));
406 static void eaddm PARAMS ((UEMUSHORT *, UEMUSHORT *));
\f
407 static void esubm PARAMS ((UEMUSHORT *, UEMUSHORT *));
408 static void m16m PARAMS ((unsigned int, UEMUSHORT *, UEMUSHORT *));
409 static int edivm PARAMS ((UEMUSHORT *, UEMUSHORT *));
410 static int emulm PARAMS ((UEMUSHORT *, UEMUSHORT *));
411 static void emdnorm PARAMS ((UEMUSHORT *, int, int, EMULONG, int));
412 static void esub PARAMS ((UEMUSHORT *, UEMUSHORT *,
414 static void eadd PARAMS ((UEMUSHORT *, UEMUSHORT *,
416 static void eadd1 PARAMS ((UEMUSHORT *, UEMUSHORT *,
418 static void ediv PARAMS ((UEMUSHORT *, UEMUSHORT *,
420 static void emul PARAMS ((UEMUSHORT *, UEMUSHORT *,
422 static void e53toe PARAMS ((UEMUSHORT *, UEMUSHORT *));
423 static void e64toe PARAMS ((UEMUSHORT *, UEMUSHORT *));
424 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
425 static void e113toe PARAMS ((UEMUSHORT *, UEMUSHORT *));
427 static void e24toe PARAMS ((UEMUSHORT *, UEMUSHORT *));
428 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
429 static void etoe113 PARAMS ((UEMUSHORT *, UEMUSHORT *));
430 static void toe113 PARAMS ((UEMUSHORT *, UEMUSHORT *));
432 static void etoe64 PARAMS ((UEMUSHORT *, UEMUSHORT *));
433 static void toe64 PARAMS ((UEMUSHORT *, UEMUSHORT *));
434 static void etoe53 PARAMS ((UEMUSHORT *, UEMUSHORT *));
435 static void toe53 PARAMS ((UEMUSHORT *, UEMUSHORT *));
436 static void etoe24 PARAMS ((UEMUSHORT *, UEMUSHORT *));
437 static void toe24 PARAMS ((UEMUSHORT *, UEMUSHORT *));
438 static int ecmp PARAMS ((UEMUSHORT *, UEMUSHORT *));
440 static void eround PARAMS ((UEMUSHORT *, UEMUSHORT *));
442 static void ltoe PARAMS ((HOST_WIDE_INT *, UEMUSHORT *));
443 static void ultoe PARAMS ((unsigned HOST_WIDE_INT *, UEMUSHORT *));
444 static void eifrac PARAMS ((UEMUSHORT *, HOST_WIDE_INT *,
446 static void euifrac PARAMS ((UEMUSHORT *, unsigned HOST_WIDE_INT *,
448 static int eshift PARAMS ((UEMUSHORT *, int));
449 static int enormlz PARAMS ((UEMUSHORT *));
451 static void e24toasc PARAMS ((UEMUSHORT *, char *, int));
452 static void e53toasc PARAMS ((UEMUSHORT *, char *, int));
453 static void e64toasc PARAMS ((UEMUSHORT *, char *, int));
454 static void e113toasc PARAMS ((UEMUSHORT *, char *, int));
456 static void etoasc PARAMS ((UEMUSHORT *, char *, int));
457 static void asctoe24 PARAMS ((const char *, UEMUSHORT *));
458 static void asctoe53 PARAMS ((const char *, UEMUSHORT *));
459 static void asctoe64 PARAMS ((const char *, UEMUSHORT *));
460 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
461 static void asctoe113 PARAMS ((const char *, UEMUSHORT *));
463 static void asctoe PARAMS ((const char *, UEMUSHORT *));
464 static void asctoeg PARAMS ((const char *, UEMUSHORT *, int));
465 static void efloor PARAMS ((UEMUSHORT *, UEMUSHORT *));
467 static void efrexp PARAMS ((UEMUSHORT *, int *,
470 static void eldexp PARAMS ((UEMUSHORT *, int, UEMUSHORT *));
472 static void eremain PARAMS ((UEMUSHORT *, UEMUSHORT *,
475 static void eiremain PARAMS ((UEMUSHORT *, UEMUSHORT *));
476 static void mtherr PARAMS ((const char *, int));
478 static void dectoe PARAMS ((UEMUSHORT *, UEMUSHORT *));
479 static void etodec PARAMS ((UEMUSHORT *, UEMUSHORT *));
480 static void todec PARAMS ((UEMUSHORT *, UEMUSHORT *));
483 static void ibmtoe PARAMS ((UEMUSHORT *, UEMUSHORT *,
485 static void etoibm PARAMS ((UEMUSHORT *, UEMUSHORT *,
487 static void toibm PARAMS ((UEMUSHORT *, UEMUSHORT *,
491 static void c4xtoe PARAMS ((UEMUSHORT *, UEMUSHORT *,
493 static void etoc4x PARAMS ((UEMUSHORT *, UEMUSHORT *,
495 static void toc4x PARAMS ((UEMUSHORT *, UEMUSHORT *,
499 static void uditoe PARAMS ((UEMUSHORT *, UEMUSHORT *));
500 static void ditoe PARAMS ((UEMUSHORT *, UEMUSHORT *));
501 static void etoudi PARAMS ((UEMUSHORT *, UEMUSHORT *));
502 static void etodi PARAMS ((UEMUSHORT *, UEMUSHORT *));
503 static void esqrt PARAMS ((UEMUSHORT *, UEMUSHORT *));
506 /* Copy 32-bit numbers obtained from array containing 16-bit numbers,
507 swapping ends if required, into output array of longs. The
508 result is normally passed to fprintf by the ASM_OUTPUT_ macros. */
514 enum machine_mode mode;
518 if (REAL_WORDS_BIG_ENDIAN)
523 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
524 /* Swap halfwords in the fourth long. */
525 th = (unsigned long) e[6] & 0xffff;
526 t = (unsigned long) e[7] & 0xffff;
535 /* Swap halfwords in the third long. */
536 th = (unsigned long) e[4] & 0xffff;
537 t = (unsigned long) e[5] & 0xffff;
543 /* Swap halfwords in the second word. */
544 th = (unsigned long) e[2] & 0xffff;
545 t = (unsigned long) e[3] & 0xffff;
552 /* Swap halfwords in the first word. */
553 th = (unsigned long) e[0] & 0xffff;
554 t = (unsigned long) e[1] & 0xffff;
565 /* Pack the output array without swapping. */
570 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
571 /* Pack the fourth long. */
572 th = (unsigned long) e[7] & 0xffff;
573 t = (unsigned long) e[6] & 0xffff;
582 /* Pack the third long.
583 Each element of the input REAL_VALUE_TYPE array has 16 useful bits
585 th = (unsigned long) e[5] & 0xffff;
586 t = (unsigned long) e[4] & 0xffff;
592 /* Pack the second long */
593 th = (unsigned long) e[3] & 0xffff;
594 t = (unsigned long) e[2] & 0xffff;
601 /* Pack the first long */
602 th = (unsigned long) e[1] & 0xffff;
603 t = (unsigned long) e[0] & 0xffff;
615 /* This is the implementation of the REAL_ARITHMETIC macro. */
618 earith (value, icode, r1, r2)
619 REAL_VALUE_TYPE *value;
624 UEMUSHORT d1[NE], d2[NE], v[NE];
630 /* Return NaN input back to the caller. */
633 PUT_REAL (d1, value);
638 PUT_REAL (d2, value);
642 code = (enum tree_code) icode;
650 esub (d2, d1, v); /* d1 - d2 */
658 #ifndef REAL_INFINITY
659 if (ecmp (d2, ezero) == 0)
662 enan (v, eisneg (d1) ^ eisneg (d2));
669 ediv (d2, d1, v); /* d1/d2 */
672 case MIN_EXPR: /* min (d1,d2) */
673 if (ecmp (d1, d2) < 0)
679 case MAX_EXPR: /* max (d1,d2) */
680 if (ecmp (d1, d2) > 0)
693 /* Truncate REAL_VALUE_TYPE toward zero to signed HOST_WIDE_INT.
694 implements REAL_VALUE_RNDZINT (x) (etrunci (x)). */
700 UEMUSHORT f[NE], g[NE];
716 /* Truncate REAL_VALUE_TYPE toward zero to unsigned HOST_WIDE_INT;
717 implements REAL_VALUE_UNSIGNED_RNDZINT (x) (etruncui (x)). */
723 UEMUSHORT f[NE], g[NE];
725 unsigned HOST_WIDE_INT l;
739 /* This is the REAL_VALUE_ATOF function. It converts a decimal or hexadecimal
740 string to binary, rounding off as indicated by the machine_mode argument.
741 Then it promotes the rounded value to REAL_VALUE_TYPE. */
748 UEMUSHORT tem[NE], e[NE];
774 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
794 /* Expansion of REAL_NEGATE. */
810 /* Round real toward zero to HOST_WIDE_INT;
811 implements REAL_VALUE_FIX (x). */
817 UEMUSHORT f[NE], g[NE];
824 warning ("conversion from NaN to int");
832 /* Round real toward zero to unsigned HOST_WIDE_INT
833 implements REAL_VALUE_UNSIGNED_FIX (x).
834 Negative input returns zero. */
836 unsigned HOST_WIDE_INT
840 UEMUSHORT f[NE], g[NE];
841 unsigned HOST_WIDE_INT l;
847 warning ("conversion from NaN to unsigned int");
856 /* REAL_VALUE_FROM_INT macro. */
859 ereal_from_int (d, i, j, mode)
862 enum machine_mode mode;
864 UEMUSHORT df[NE], dg[NE];
865 HOST_WIDE_INT low, high;
868 if (GET_MODE_CLASS (mode) != MODE_FLOAT)
875 /* complement and add 1 */
882 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
883 ultoe ((unsigned HOST_WIDE_INT *) &high, dg);
885 ultoe ((unsigned HOST_WIDE_INT *) &low, df);
890 /* A REAL_VALUE_TYPE may not be wide enough to hold the two HOST_WIDE_INTS.
891 Avoid double-rounding errors later by rounding off now from the
892 extra-wide internal format to the requested precision. */
893 switch (GET_MODE_BITSIZE (mode))
911 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
928 /* REAL_VALUE_FROM_UNSIGNED_INT macro. */
931 ereal_from_uint (d, i, j, mode)
933 unsigned HOST_WIDE_INT i, j;
934 enum machine_mode mode;
936 UEMUSHORT df[NE], dg[NE];
937 unsigned HOST_WIDE_INT low, high;
939 if (GET_MODE_CLASS (mode) != MODE_FLOAT)
943 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
949 /* A REAL_VALUE_TYPE may not be wide enough to hold the two HOST_WIDE_INTS.
950 Avoid double-rounding errors later by rounding off now from the
951 extra-wide internal format to the requested precision. */
952 switch (GET_MODE_BITSIZE (mode))
970 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
987 /* REAL_VALUE_TO_INT macro. */
990 ereal_to_int (low, high, rr)
991 HOST_WIDE_INT *low, *high;
994 UEMUSHORT d[NE], df[NE], dg[NE], dh[NE];
1001 warning ("conversion from NaN to int");
1007 /* convert positive value */
1014 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
1015 ediv (df, d, dg); /* dg = d / 2^32 is the high word */
1016 euifrac (dg, (unsigned HOST_WIDE_INT *) high, dh);
1017 emul (df, dh, dg); /* fractional part is the low word */
1018 euifrac (dg, (unsigned HOST_WIDE_INT *)low, dh);
1021 /* complement and add 1 */
1031 /* REAL_VALUE_LDEXP macro. */
1038 UEMUSHORT e[NE], y[NE];
1051 /* These routines are conditionally compiled because functions
1052 of the same names may be defined in fold-const.c. */
1054 #ifdef REAL_ARITHMETIC
1056 /* Check for infinity in a REAL_VALUE_TYPE. */
1060 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
1066 return (eisinf (e));
1072 /* Check whether a REAL_VALUE_TYPE item is a NaN. */
1076 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
1082 return (eisnan (e));
1089 /* Check for a negative REAL_VALUE_TYPE number.
1090 This just checks the sign bit, so that -0 counts as negative. */
1096 return ereal_isneg (x);
1099 /* Expansion of REAL_VALUE_TRUNCATE.
1100 The result is in floating point, rounded to nearest or even. */
1103 real_value_truncate (mode, arg)
1104 enum machine_mode mode;
1105 REAL_VALUE_TYPE arg;
1107 UEMUSHORT e[NE], t[NE];
1119 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
1156 /* If an unsupported type was requested, presume that
1157 the machine files know something useful to do with
1158 the unmodified value. */
1167 /* Try to change R into its exact multiplicative inverse in machine mode
1168 MODE. Return nonzero function value if successful. */
1171 exact_real_inverse (mode, r)
1172 enum machine_mode mode;
1175 UEMUSHORT e[NE], einv[NE];
1176 REAL_VALUE_TYPE rinv;
1181 /* Test for input in range. Don't transform IEEE special values. */
1182 if (eisinf (e) || eisnan (e) || (ecmp (e, ezero) == 0))
1185 /* Test for a power of 2: all significand bits zero except the MSB.
1186 We are assuming the target has binary (or hex) arithmetic. */
1187 if (e[NE - 2] != 0x8000)
1190 for (i = 0; i < NE - 2; i++)
1196 /* Compute the inverse and truncate it to the required mode. */
1197 ediv (e, eone, einv);
1198 PUT_REAL (einv, &rinv);
1199 rinv = real_value_truncate (mode, rinv);
1201 #ifdef CHECK_FLOAT_VALUE
1202 /* This check is not redundant. It may, for example, flush
1203 a supposedly IEEE denormal value to zero. */
1205 if (CHECK_FLOAT_VALUE (mode, rinv, i))
1208 GET_REAL (&rinv, einv);
1210 /* Check the bits again, because the truncation might have
1211 generated an arbitrary saturation value on overflow. */
1212 if (einv[NE - 2] != 0x8000)
1215 for (i = 0; i < NE - 2; i++)
1221 /* Fail if the computed inverse is out of range. */
1222 if (eisinf (einv) || eisnan (einv) || (ecmp (einv, ezero) == 0))
1225 /* Output the reciprocal and return success flag. */
1229 #endif /* REAL_ARITHMETIC defined */
1231 /* Used for debugging--print the value of R in human-readable format
1240 REAL_VALUE_TO_DECIMAL (r, "%.20g", dstr);
1241 fprintf (stderr, "%s", dstr);
1245 /* The following routines convert REAL_VALUE_TYPE to the various floating
1246 point formats that are meaningful to supported computers.
1248 The results are returned in 32-bit pieces, each piece stored in a `long'.
1249 This is so they can be printed by statements like
1251 fprintf (file, "%lx, %lx", L[0], L[1]);
1253 that will work on both narrow- and wide-word host computers. */
1255 /* Convert R to a 128-bit long double precision value. The output array L
1256 contains four 32-bit pieces of the result, in the order they would appear
1267 #if INTEL_EXTENDED_IEEE_FORMAT == 0
1272 endian (e, l, TFmode);
1275 /* Convert R to a double extended precision value. The output array L
1276 contains three 32-bit pieces of the result, in the order they would
1277 appear in memory. */
1288 endian (e, l, XFmode);
1291 /* Convert R to a double precision value. The output array L contains two
1292 32-bit pieces of the result, in the order they would appear in memory. */
1303 endian (e, l, DFmode);
1306 /* Convert R to a single precision float value stored in the least-significant
1307 bits of a `long'. */
1318 endian (e, &l, SFmode);
1322 /* Convert X to a decimal ASCII string S for output to an assembly
1323 language file. Note, there is no standard way to spell infinity or
1324 a NaN, so these values may require special treatment in the tm.h
1328 ereal_to_decimal (x, s)
1338 /* Compare X and Y. Return 1 if X > Y, 0 if X == Y, -1 if X < Y,
1339 or -2 if either is a NaN. */
1343 REAL_VALUE_TYPE x, y;
1345 UEMUSHORT ex[NE], ey[NE];
1349 return (ecmp (ex, ey));
1352 /* Return 1 if the sign bit of X is set, else return 0. */
1361 return (eisneg (ex));
1364 /* End of REAL_ARITHMETIC interface */
1367 Extended precision IEEE binary floating point arithmetic routines
1369 Numbers are stored in C language as arrays of 16-bit unsigned
1370 short integers. The arguments of the routines are pointers to
1373 External e type data structure, similar to Intel 8087 chip
1374 temporary real format but possibly with a larger significand:
1376 NE-1 significand words (least significant word first,
1377 most significant bit is normally set)
1378 exponent (value = EXONE for 1.0,
1379 top bit is the sign)
1382 Internal exploded e-type data structure of a number (a "word" is 16 bits):
1384 ei[0] sign word (0 for positive, 0xffff for negative)
1385 ei[1] biased exponent (value = EXONE for the number 1.0)
1386 ei[2] high guard word (always zero after normalization)
1388 to ei[NI-2] significand (NI-4 significand words,
1389 most significant word first,
1390 most significant bit is set)
1391 ei[NI-1] low guard word (0x8000 bit is rounding place)
1395 Routines for external format e-type numbers
1397 asctoe (string, e) ASCII string to extended double e type
1398 asctoe64 (string, &d) ASCII string to long double
1399 asctoe53 (string, &d) ASCII string to double
1400 asctoe24 (string, &f) ASCII string to single
1401 asctoeg (string, e, prec) ASCII string to specified precision
1402 e24toe (&f, e) IEEE single precision to e type
1403 e53toe (&d, e) IEEE double precision to e type
1404 e64toe (&d, e) IEEE long double precision to e type
1405 e113toe (&d, e) 128-bit long double precision to e type
1407 eabs (e) absolute value
1409 eadd (a, b, c) c = b + a
1411 ecmp (a, b) Returns 1 if a > b, 0 if a == b,
1412 -1 if a < b, -2 if either a or b is a NaN.
1413 ediv (a, b, c) c = b / a
1414 efloor (a, b) truncate to integer, toward -infinity
1415 efrexp (a, exp, s) extract exponent and significand
1416 eifrac (e, &l, frac) e to HOST_WIDE_INT and e type fraction
1417 euifrac (e, &l, frac) e to unsigned HOST_WIDE_INT and e type fraction
1418 einfin (e) set e to infinity, leaving its sign alone
1419 eldexp (a, n, b) multiply by 2**n
1421 emul (a, b, c) c = b * a
1424 eround (a, b) b = nearest integer value to a
1426 esub (a, b, c) c = b - a
1428 e24toasc (&f, str, n) single to ASCII string, n digits after decimal
1429 e53toasc (&d, str, n) double to ASCII string, n digits after decimal
1430 e64toasc (&d, str, n) 80-bit long double to ASCII string
1431 e113toasc (&d, str, n) 128-bit long double to ASCII string
1433 etoasc (e, str, n) e to ASCII string, n digits after decimal
1434 etoe24 (e, &f) convert e type to IEEE single precision
1435 etoe53 (e, &d) convert e type to IEEE double precision
1436 etoe64 (e, &d) convert e type to IEEE long double precision
1437 ltoe (&l, e) HOST_WIDE_INT to e type
1438 ultoe (&l, e) unsigned HOST_WIDE_INT to e type
1439 eisneg (e) 1 if sign bit of e != 0, else 0
1440 eisinf (e) 1 if e has maximum exponent (non-IEEE)
1441 or is infinite (IEEE)
1442 eisnan (e) 1 if e is a NaN
1445 Routines for internal format exploded e-type numbers
1447 eaddm (ai, bi) add significands, bi = bi + ai
1449 ecleazs (ei) set ei = 0 but leave its sign alone
1450 ecmpm (ai, bi) compare significands, return 1, 0, or -1
1451 edivm (ai, bi) divide significands, bi = bi / ai
1452 emdnorm (ai,l,s,exp) normalize and round off
1453 emovi (a, ai) convert external a to internal ai
1454 emovo (ai, a) convert internal ai to external a
1455 emovz (ai, bi) bi = ai, low guard word of bi = 0
1456 emulm (ai, bi) multiply significands, bi = bi * ai
1457 enormlz (ei) left-justify the significand
1458 eshdn1 (ai) shift significand and guards down 1 bit
1459 eshdn8 (ai) shift down 8 bits
1460 eshdn6 (ai) shift down 16 bits
1461 eshift (ai, n) shift ai n bits up (or down if n < 0)
1462 eshup1 (ai) shift significand and guards up 1 bit
1463 eshup8 (ai) shift up 8 bits
1464 eshup6 (ai) shift up 16 bits
1465 esubm (ai, bi) subtract significands, bi = bi - ai
1466 eiisinf (ai) 1 if infinite
1467 eiisnan (ai) 1 if a NaN
1468 eiisneg (ai) 1 if sign bit of ai != 0, else 0
1469 einan (ai) set ai = NaN
1471 eiinfin (ai) set ai = infinity
1474 The result is always normalized and rounded to NI-4 word precision
1475 after each arithmetic operation.
1477 Exception flags are NOT fully supported.
1479 Signaling NaN's are NOT supported; they are treated the same
1482 Define INFINITY for support of infinity; otherwise a
1483 saturation arithmetic is implemented.
1485 Define NANS for support of Not-a-Number items; otherwise the
1486 arithmetic will never produce a NaN output, and might be confused
1488 If NaN's are supported, the output of `ecmp (a,b)' is -2 if
1489 either a or b is a NaN. This means asking `if (ecmp (a,b) < 0)'
1490 may not be legitimate. Use `if (ecmp (a,b) == -1)' for `less than'
1493 Denormals are always supported here where appropriate (e.g., not
1494 for conversion to DEC numbers). */
1496 /* Definitions for error codes that are passed to the common error handling
1499 For Digital Equipment PDP-11 and VAX computers, certain
1500 IBM systems, and others that use numbers with a 56-bit
1501 significand, the symbol DEC should be defined. In this
1502 mode, most floating point constants are given as arrays
1503 of octal integers to eliminate decimal to binary conversion
1504 errors that might be introduced by the compiler.
1506 For computers, such as IBM PC, that follow the IEEE
1507 Standard for Binary Floating Point Arithmetic (ANSI/IEEE
1508 Std 754-1985), the symbol IEEE should be defined.
1509 These numbers have 53-bit significands. In this mode, constants
1510 are provided as arrays of hexadecimal 16 bit integers.
1511 The endian-ness of generated values is controlled by
1512 REAL_WORDS_BIG_ENDIAN.
1514 To accommodate other types of computer arithmetic, all
1515 constants are also provided in a normal decimal radix
1516 which one can hope are correctly converted to a suitable
1517 format by the available C language compiler. To invoke
1518 this mode, the symbol UNK is defined.
1520 An important difference among these modes is a predefined
1521 set of machine arithmetic constants for each. The numbers
1522 MACHEP (the machine roundoff error), MAXNUM (largest number
1523 represented), and several other parameters are preset by
1524 the configuration symbol. Check the file const.c to
1525 ensure that these values are correct for your computer.
1527 For ANSI C compatibility, define ANSIC equal to 1. Currently
1528 this affects only the atan2 function and others that use it. */
1530 /* Constant definitions for math error conditions. */
1532 #define DOMAIN 1 /* argument domain error */
1533 #define SING 2 /* argument singularity */
1534 #define OVERFLOW 3 /* overflow range error */
1535 #define UNDERFLOW 4 /* underflow range error */
1536 #define TLOSS 5 /* total loss of precision */
1537 #define PLOSS 6 /* partial loss of precision */
1538 #define INVALID 7 /* NaN-producing operation */
1540 /* e type constants used by high precision check routines */
1542 #if MAX_LONG_DOUBLE_TYPE_SIZE == 128 && (INTEL_EXTENDED_IEEE_FORMAT == 0)
1544 UEMUSHORT ezero[NE] =
1545 {0x0000, 0x0000, 0x0000, 0x0000,
1546 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,};
1547 extern UEMUSHORT ezero[];
1550 UEMUSHORT ehalf[NE] =
1551 {0x0000, 0x0000, 0x0000, 0x0000,
1552 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3ffe,};
1553 extern UEMUSHORT ehalf[];
1556 UEMUSHORT eone[NE] =
1557 {0x0000, 0x0000, 0x0000, 0x0000,
1558 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3fff,};
1559 extern UEMUSHORT eone[];
1562 UEMUSHORT etwo[NE] =
1563 {0x0000, 0x0000, 0x0000, 0x0000,
1564 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4000,};
1565 extern UEMUSHORT etwo[];
1569 {0x0000, 0x0000, 0x0000, 0x0000,
1570 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4004,};
1571 extern UEMUSHORT e32[];
1573 /* 6.93147180559945309417232121458176568075500134360255E-1 */
1574 UEMUSHORT elog2[NE] =
1575 {0x40f3, 0xf6af, 0x03f2, 0xb398,
1576 0xc9e3, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1577 extern UEMUSHORT elog2[];
1579 /* 1.41421356237309504880168872420969807856967187537695E0 */
1580 UEMUSHORT esqrt2[NE] =
1581 {0x1d6f, 0xbe9f, 0x754a, 0x89b3,
1582 0x597d, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1583 extern UEMUSHORT esqrt2[];
1585 /* 3.14159265358979323846264338327950288419716939937511E0 */
1587 {0x2902, 0x1cd1, 0x80dc, 0x628b,
1588 0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1589 extern UEMUSHORT epi[];
1592 /* LONG_DOUBLE_TYPE_SIZE is other than 128 */
1593 UEMUSHORT ezero[NE] =
1594 {0, 0000000, 0000000, 0000000, 0000000, 0000000,};
1595 UEMUSHORT ehalf[NE] =
1596 {0, 0000000, 0000000, 0000000, 0100000, 0x3ffe,};
1597 UEMUSHORT eone[NE] =
1598 {0, 0000000, 0000000, 0000000, 0100000, 0x3fff,};
1599 UEMUSHORT etwo[NE] =
1600 {0, 0000000, 0000000, 0000000, 0100000, 0040000,};
1602 {0, 0000000, 0000000, 0000000, 0100000, 0040004,};
1603 UEMUSHORT elog2[NE] =
1604 {0xc9e4, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1605 UEMUSHORT esqrt2[NE] =
1606 {0x597e, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1608 {0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1611 /* Control register for rounding precision.
1612 This can be set to 113 (if NE=10), 80 (if NE=6), 64, 56, 53, or 24 bits. */
1617 /* Clear out entire e-type number X. */
1625 for (i = 0; i < NE; i++)
1629 /* Move e-type number from A to B. */
1637 for (i = 0; i < NE; i++)
1643 /* Absolute value of e-type X. */
1649 /* sign is top bit of last word of external format */
1650 x[NE - 1] &= 0x7fff;
1654 /* Negate the e-type number X. */
1661 x[NE - 1] ^= 0x8000; /* Toggle the sign bit */
1664 /* Return 1 if sign bit of e-type number X is nonzero, else zero. */
1671 if (x[NE - 1] & 0x8000)
1677 /* Return 1 if e-type number X is infinity, else return zero. */
1688 if ((x[NE - 1] & 0x7fff) == 0x7fff)
1694 /* Check if e-type number is not a number. The bit pattern is one that we
1695 defined, so we know for sure how to detect it. */
1699 UEMUSHORT x[] ATTRIBUTE_UNUSED;
1704 /* NaN has maximum exponent */
1705 if ((x[NE - 1] & 0x7fff) != 0x7fff)
1707 /* ... and non-zero significand field. */
1708 for (i = 0; i < NE - 1; i++)
1718 /* Fill e-type number X with infinity pattern (IEEE)
1719 or largest possible number (non-IEEE). */
1728 for (i = 0; i < NE - 1; i++)
1732 for (i = 0; i < NE - 1; i++)
1760 /* Output an e-type NaN.
1761 This generates Intel's quiet NaN pattern for extended real.
1762 The exponent is 7fff, the leading mantissa word is c000. */
1772 for (i = 0; i < NE - 2; i++)
1775 *x = (sign << 15) | 0x7fff;
1779 /* Move in an e-type number A, converting it to exploded e-type B. */
1789 p = a + (NE - 1); /* point to last word of external number */
1790 /* get the sign bit */
1795 /* get the exponent */
1797 *q++ &= 0x7fff; /* delete the sign bit */
1799 if ((*(q - 1) & 0x7fff) == 0x7fff)
1805 for (i = 3; i < NI; i++)
1811 for (i = 2; i < NI; i++)
1817 /* clear high guard word */
1819 /* move in the significand */
1820 for (i = 0; i < NE - 1; i++)
1822 /* clear low guard word */
1826 /* Move out exploded e-type number A, converting it to e type B. */
1837 q = b + (NE - 1); /* point to output exponent */
1838 /* combine sign and exponent */
1841 *q-- = *p++ | 0x8000;
1845 if (*(p - 1) == 0x7fff)
1850 enan (b, eiisneg (a));
1858 /* skip over guard word */
1860 /* move the significand */
1861 for (j = 0; j < NE - 1; j++)
1865 /* Clear out exploded e-type number XI. */
1873 for (i = 0; i < NI; i++)
1877 /* Clear out exploded e-type XI, but don't touch the sign. */
1886 for (i = 0; i < NI - 1; i++)
1890 /* Move exploded e-type number from A to B. */
1898 for (i = 0; i < NI - 1; i++)
1900 /* clear low guard word */
1904 /* Generate exploded e-type NaN.
1905 The explicit pattern for this is maximum exponent and
1906 top two significant bits set. */
1920 /* Return nonzero if exploded e-type X is a NaN. */
1929 if ((x[E] & 0x7fff) == 0x7fff)
1931 for (i = M + 1; i < NI; i++)
1941 /* Return nonzero if sign of exploded e-type X is nonzero. */
1954 /* Fill exploded e-type X with infinity pattern.
1955 This has maximum exponent and significand all zeros. */
1967 /* Return nonzero if exploded e-type X is infinite. */
1979 if ((x[E] & 0x7fff) == 0x7fff)
1983 #endif /* INFINITY */
1985 /* Compare significands of numbers in internal exploded e-type format.
1986 Guard words are included in the comparison.
1998 a += M; /* skip up to significand area */
2000 for (i = M; i < NI; i++)
2008 if (*(--a) > *(--b))
2014 /* Shift significand of exploded e-type X down by 1 bit. */
2023 x += M; /* point to significand area */
2026 for (i = M; i < NI; i++)
2038 /* Shift significand of exploded e-type X up by 1 bit. */
2050 for (i = M; i < NI; i++)
2063 /* Shift significand of exploded e-type X down by 8 bits. */
2069 UEMUSHORT newbyt, oldbyt;
2074 for (i = M; i < NI; i++)
2084 /* Shift significand of exploded e-type X up by 8 bits. */
2091 UEMUSHORT newbyt, oldbyt;
2096 for (i = M; i < NI; i++)
2106 /* Shift significand of exploded e-type X up by 16 bits. */
2118 for (i = M; i < NI - 1; i++)
2124 /* Shift significand of exploded e-type X down by 16 bits. */
2136 for (i = M; i < NI - 1; i++)
2142 /* Add significands of exploded e-type X and Y. X + Y replaces Y. */
2155 for (i = M; i < NI; i++)
2157 a = (unsigned EMULONG) (*x) + (unsigned EMULONG) (*y) + carry;
2168 /* Subtract significands of exploded e-type X and Y. Y - X replaces Y. */
2181 for (i = M; i < NI; i++)
2183 a = (unsigned EMULONG) (*y) - (unsigned EMULONG) (*x) - carry;
2195 static UEMUSHORT equot[NI];
2199 /* Radix 2 shift-and-add versions of multiply and divide */
2202 /* Divide significands */
2206 UEMUSHORT den[], num[];
2216 for (i = M; i < NI; i++)
2221 /* Use faster compare and subtraction if denominator has only 15 bits of
2227 for (i = M + 3; i < NI; i++)
2232 if ((den[M + 1] & 1) != 0)
2240 for (i = 0; i < NBITS + 2; i++)
2258 /* The number of quotient bits to calculate is NBITS + 1 scaling guard
2259 bit + 1 roundoff bit. */
2264 for (i = 0; i < NBITS + 2; i++)
2266 if (ecmpm (den, num) <= 0)
2269 j = 1; /* quotient bit = 1 */
2283 /* test for nonzero remainder after roundoff bit */
2286 for (i = M; i < NI; i++)
2294 for (i = 0; i < NI; i++)
2300 /* Multiply significands */
2311 for (i = M; i < NI; i++)
2316 while (*p == 0) /* significand is not supposed to be zero */
2321 if ((*p & 0xff) == 0)
2329 for (i = 0; i < k; i++)
2333 /* remember if there were any nonzero bits shifted out */
2340 for (i = 0; i < NI; i++)
2343 /* return flag for lost nonzero bits */
2349 /* Radix 65536 versions of multiply and divide. */
2351 /* Multiply significand of e-type number B
2352 by 16-bit quantity A, return e-type result to C. */
2360 unsigned EMULONG carry;
2363 unsigned EMULONG aa, m;
2372 for (i=M+1; i<NI; i++)
2382 m = (unsigned EMULONG) aa * *ps--;
2383 carry = (m & 0xffff) + *pp;
2384 *pp-- = (UEMUSHORT)carry;
2385 carry = (carry >> 16) + (m >> 16) + *pp;
2386 *pp = (UEMUSHORT)carry;
2387 *(pp-1) = carry >> 16;
2390 for (i=M; i<NI; i++)
2394 /* Divide significands of exploded e-types NUM / DEN. Neither the
2395 numerator NUM nor the denominator DEN is permitted to have its high guard
2400 UEMUSHORT den[], num[];
2404 unsigned EMULONG tnum;
2405 UEMUSHORT j, tdenm, tquot;
2406 UEMUSHORT tprod[NI+1];
2412 for (i=M; i<NI; i++)
2418 for (i=M; i<NI; i++)
2420 /* Find trial quotient digit (the radix is 65536). */
2421 tnum = (((unsigned EMULONG) num[M]) << 16) + num[M+1];
2423 /* Do not execute the divide instruction if it will overflow. */
2424 if ((tdenm * (unsigned long)0xffff) < tnum)
2427 tquot = tnum / tdenm;
2428 /* Multiply denominator by trial quotient digit. */
2429 m16m ((unsigned int)tquot, den, tprod);
2430 /* The quotient digit may have been overestimated. */
2431 if (ecmpm (tprod, num) > 0)
2435 if (ecmpm (tprod, num) > 0)
2445 /* test for nonzero remainder after roundoff bit */
2448 for (i=M; i<NI; i++)
2455 for (i=0; i<NI; i++)
2461 /* Multiply significands of exploded e-type A and B, result in B. */
2468 UEMUSHORT pprod[NI];
2474 for (i=M; i<NI; i++)
2480 for (i=M+1; i<NI; i++)
2488 m16m ((unsigned int) *p--, b, pprod);
2489 eaddm(pprod, equot);
2495 for (i=0; i<NI; i++)
2498 /* return flag for lost nonzero bits */
2504 /* Normalize and round off.
2506 The internal format number to be rounded is S.
2507 Input LOST is 0 if the value is exact. This is the so-called sticky bit.
2509 Input SUBFLG indicates whether the number was obtained
2510 by a subtraction operation. In that case if LOST is nonzero
2511 then the number is slightly smaller than indicated.
2513 Input EXP is the biased exponent, which may be negative.
2514 the exponent field of S is ignored but is replaced by
2515 EXP as adjusted by normalization and rounding.
2517 Input RCNTRL is the rounding control. If it is nonzero, the
2518 returned value will be rounded to RNDPRC bits.
2520 For future reference: In order for emdnorm to round off denormal
2521 significands at the right point, the input exponent must be
2522 adjusted to be the actual value it would have after conversion to
2523 the final floating point type. This adjustment has been
2524 implemented for all type conversions (etoe53, etc.) and decimal
2525 conversions, but not for the arithmetic functions (eadd, etc.).
2526 Data types having standard 15-bit exponents are not affected by
2527 this, but SFmode and DFmode are affected. For example, ediv with
2528 rndprc = 24 will not round correctly to 24-bit precision if the
2529 result is denormal. */
2531 static int rlast = -1;
2533 static UEMUSHORT rmsk = 0;
2534 static UEMUSHORT rmbit = 0;
2535 static UEMUSHORT rebit = 0;
2537 static UEMUSHORT rbit[NI];
2540 emdnorm (s, lost, subflg, exp, rcntrl)
2553 /* a blank significand could mean either zero or infinity. */
2566 if ((j > NBITS) && (exp < 32767))
2574 if (exp > (EMULONG) (-NBITS - 1))
2587 /* Round off, unless told not to by rcntrl. */
2590 /* Set up rounding parameters if the control register changed. */
2591 if (rndprc != rlast)
2598 rw = NI - 1; /* low guard word */
2621 /* For DEC or IBM arithmetic */
2638 /* For C4x arithmetic */
2659 /* Shift down 1 temporarily if the data structure has an implied
2660 most significant bit and the number is denormal.
2661 Intel long double denormals also lose one bit of precision. */
2662 if ((exp <= 0) && (rndprc != NBITS)
2663 && ((rndprc != 64) || ((rndprc == 64) && ! REAL_WORDS_BIG_ENDIAN)))
2665 lost |= s[NI - 1] & 1;
2668 /* Clear out all bits below the rounding bit,
2669 remembering in r if any were nonzero. */
2683 if ((r & rmbit) != 0)
2689 { /* round to even */
2690 if ((s[re] & rebit) == 0)
2703 /* Undo the temporary shift for denormal values. */
2704 if ((exp <= 0) && (rndprc != NBITS)
2705 && ((rndprc != 64) || ((rndprc == 64) && ! REAL_WORDS_BIG_ENDIAN)))
2710 { /* overflow on roundoff */
2723 for (i = 2; i < NI - 1; i++)
2726 warning ("floating point overflow");
2730 for (i = M + 1; i < NI - 1; i++)
2733 if ((rndprc < 64) || (rndprc == 113))
2748 s[1] = (UEMUSHORT) exp;
2751 /* Subtract. C = B - A, all e type numbers. */
2753 static int subflg = 0;
2757 UEMUSHORT *a, *b, *c;
2771 /* Infinity minus infinity is a NaN.
2772 Test for subtracting infinities of the same sign. */
2773 if (eisinf (a) && eisinf (b)
2774 && ((eisneg (a) ^ eisneg (b)) == 0))
2776 mtherr ("esub", INVALID);
2785 /* Add. C = A + B, all e type. */
2789 UEMUSHORT *a, *b, *c;
2793 /* NaN plus anything is a NaN. */
2804 /* Infinity minus infinity is a NaN.
2805 Test for adding infinities of opposite signs. */
2806 if (eisinf (a) && eisinf (b)
2807 && ((eisneg (a) ^ eisneg (b)) != 0))
2809 mtherr ("esub", INVALID);
2818 /* Arithmetic common to both addition and subtraction. */
2822 UEMUSHORT *a, *b, *c;
2824 UEMUSHORT ai[NI], bi[NI], ci[NI];
2826 EMULONG lt, lta, ltb;
2847 /* compare exponents */
2852 { /* put the larger number in bi */
2862 if (lt < (EMULONG) (-NBITS - 1))
2863 goto done; /* answer same as larger addend */
2865 lost = eshift (ai, k); /* shift the smaller number down */
2869 /* exponents were the same, so must compare significands */
2872 { /* the numbers are identical in magnitude */
2873 /* if different signs, result is zero */
2879 /* if same sign, result is double */
2880 /* double denormalized tiny number */
2881 if ((bi[E] == 0) && ((bi[3] & 0x8000) == 0))
2886 /* add 1 to exponent unless both are zero! */
2887 for (j = 1; j < NI - 1; j++)
2903 bi[E] = (UEMUSHORT) ltb;
2907 { /* put the larger number in bi */
2923 emdnorm (bi, lost, subflg, ltb, 64);
2929 /* Divide: C = B/A, all e type. */
2933 UEMUSHORT *a, *b, *c;
2935 UEMUSHORT ai[NI], bi[NI];
2937 EMULONG lt, lta, ltb;
2939 /* IEEE says if result is not a NaN, the sign is "-" if and only if
2940 operands have opposite signs -- but flush -0 to 0 later if not IEEE. */
2941 sign = eisneg(a) ^ eisneg(b);
2944 /* Return any NaN input. */
2955 /* Zero over zero, or infinity over infinity, is a NaN. */
2956 if (((ecmp (a, ezero) == 0) && (ecmp (b, ezero) == 0))
2957 || (eisinf (a) && eisinf (b)))
2959 mtherr ("ediv", INVALID);
2964 /* Infinity over anything else is infinity. */
2971 /* Anything else over infinity is zero. */
2983 { /* See if numerator is zero. */
2984 for (i = 1; i < NI - 1; i++)
2988 ltb -= enormlz (bi);
2998 { /* possible divide by zero */
2999 for (i = 1; i < NI - 1; i++)
3003 lta -= enormlz (ai);
3007 /* Divide by zero is not an invalid operation.
3008 It is a divide-by-zero operation! */
3010 mtherr ("ediv", SING);
3016 /* calculate exponent */
3017 lt = ltb - lta + EXONE;
3018 emdnorm (bi, i, 0, lt, 64);
3025 && (ecmp (c, ezero) != 0)
3028 *(c+(NE-1)) |= 0x8000;
3030 *(c+(NE-1)) &= ~0x8000;
3033 /* Multiply e-types A and B, return e-type product C. */
3037 UEMUSHORT *a, *b, *c;
3039 UEMUSHORT ai[NI], bi[NI];
3041 EMULONG lt, lta, ltb;
3043 /* IEEE says if result is not a NaN, the sign is "-" if and only if
3044 operands have opposite signs -- but flush -0 to 0 later if not IEEE. */
3045 sign = eisneg(a) ^ eisneg(b);
3048 /* NaN times anything is the same NaN. */
3059 /* Zero times infinity is a NaN. */
3060 if ((eisinf (a) && (ecmp (b, ezero) == 0))
3061 || (eisinf (b) && (ecmp (a, ezero) == 0)))
3063 mtherr ("emul", INVALID);
3068 /* Infinity times anything else is infinity. */
3070 if (eisinf (a) || eisinf (b))
3082 for (i = 1; i < NI - 1; i++)
3086 lta -= enormlz (ai);
3097 for (i = 1; i < NI - 1; i++)
3101 ltb -= enormlz (bi);
3110 /* Multiply significands */
3112 /* calculate exponent */
3113 lt = lta + ltb - (EXONE - 1);
3114 emdnorm (bi, j, 0, lt, 64);
3121 && (ecmp (c, ezero) != 0)
3124 *(c+(NE-1)) |= 0x8000;
3126 *(c+(NE-1)) &= ~0x8000;
3129 /* Convert double precision PE to e-type Y. */
3142 ibmtoe (pe, y, DFmode);
3147 c4xtoe (pe, y, HFmode);
3156 denorm = 0; /* flag if denormalized number */
3158 if (! REAL_WORDS_BIG_ENDIAN)
3164 yy[M] = (r & 0x0f) | 0x10;
3165 r &= ~0x800f; /* strip sign and 4 significand bits */
3170 if (! REAL_WORDS_BIG_ENDIAN)
3172 if (((pe[3] & 0xf) != 0) || (pe[2] != 0)
3173 || (pe[1] != 0) || (pe[0] != 0))
3175 enan (y, yy[0] != 0);
3181 if (((pe[0] & 0xf) != 0) || (pe[1] != 0)
3182 || (pe[2] != 0) || (pe[3] != 0))
3184 enan (y, yy[0] != 0);
3195 #endif /* INFINITY */
3197 /* If zero exponent, then the significand is denormalized.
3198 So take back the understood high significand bit. */
3209 if (! REAL_WORDS_BIG_ENDIAN)
3226 /* If zero exponent, then normalize the significand. */
3227 if ((k = enormlz (yy)) > NBITS)
3230 yy[E] -= (UEMUSHORT) (k - 1);
3233 #endif /* not C4X */
3234 #endif /* not IBM */
3235 #endif /* not DEC */
3238 /* Convert double extended precision float PE to e type Y. */
3245 UEMUSHORT *e, *p, *q;
3250 for (i = 0; i < NE - 5; i++)
3252 /* This precision is not ordinarily supported on DEC or IBM. */
3254 for (i = 0; i < 5; i++)
3258 p = &yy[0] + (NE - 1);
3261 for (i = 0; i < 5; i++)
3265 if (! REAL_WORDS_BIG_ENDIAN)
3267 for (i = 0; i < 5; i++)
3270 /* For denormal long double Intel format, shift significand up one
3271 -- but only if the top significand bit is zero. A top bit of 1
3272 is "pseudodenormal" when the exponent is zero. */
3273 if((yy[NE-1] & 0x7fff) == 0 && (yy[NE-2] & 0x8000) == 0)
3285 p = &yy[0] + (NE - 1);
3286 #ifdef ARM_EXTENDED_IEEE_FORMAT
3287 /* For ARMs, the exponent is in the lowest 15 bits of the word. */
3288 *p-- = (e[0] & 0x8000) | (e[1] & 0x7ffff);
3294 for (i = 0; i < 4; i++)
3299 /* Point to the exponent field and check max exponent cases. */
3301 if ((*p & 0x7fff) == 0x7fff)
3304 if (! REAL_WORDS_BIG_ENDIAN)
3306 for (i = 0; i < 4; i++)
3308 if ((i != 3 && pe[i] != 0)
3309 /* Anything but 0x8000 here, including 0, is a NaN. */
3310 || (i == 3 && pe[i] != 0x8000))
3312 enan (y, (*p & 0x8000) != 0);
3319 #ifdef ARM_EXTENDED_IEEE_FORMAT
3320 for (i = 2; i <= 5; i++)
3324 enan (y, (*p & 0x8000) != 0);
3329 /* In Motorola extended precision format, the most significant
3330 bit of an infinity mantissa could be either 1 or 0. It is
3331 the lower order bits that tell whether the value is a NaN. */
3332 if ((pe[2] & 0x7fff) != 0)
3335 for (i = 3; i <= 5; i++)
3340 enan (y, (*p & 0x8000) != 0);
3344 #endif /* not ARM */
3353 #endif /* INFINITY */
3356 for (i = 0; i < NE; i++)
3360 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
3361 /* Convert 128-bit long double precision float PE to e type Y. */
3376 if (! REAL_WORDS_BIG_ENDIAN)
3388 if (! REAL_WORDS_BIG_ENDIAN)
3390 for (i = 0; i < 7; i++)
3394 enan (y, yy[0] != 0);
3401 for (i = 1; i < 8; i++)
3405 enan (y, yy[0] != 0);
3417 #endif /* INFINITY */
3421 if (! REAL_WORDS_BIG_ENDIAN)
3423 for (i = 0; i < 7; i++)
3429 for (i = 0; i < 7; i++)
3433 /* If denormal, remove the implied bit; else shift down 1. */
3447 /* Convert single precision float PE to e type Y. */
3455 ibmtoe (pe, y, SFmode);
3461 c4xtoe (pe, y, QFmode);
3471 denorm = 0; /* flag if denormalized number */
3474 if (! REAL_WORDS_BIG_ENDIAN)
3484 yy[M] = (r & 0x7f) | 0200;
3485 r &= ~0x807f; /* strip sign and 7 significand bits */
3490 if (REAL_WORDS_BIG_ENDIAN)
3492 if (((pe[0] & 0x7f) != 0) || (pe[1] != 0))
3494 enan (y, yy[0] != 0);
3500 if (((pe[1] & 0x7f) != 0) || (pe[0] != 0))
3502 enan (y, yy[0] != 0);
3513 #endif /* INFINITY */
3515 /* If zero exponent, then the significand is denormalized.
3516 So take back the understood high significand bit. */
3529 if (! REAL_WORDS_BIG_ENDIAN)
3539 { /* if zero exponent, then normalize the significand */
3540 if ((k = enormlz (yy)) > NBITS)
3543 yy[E] -= (UEMUSHORT) (k - 1);
3546 #endif /* not C4X */
3547 #endif /* not IBM */
3550 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
3551 /* Convert e-type X to IEEE 128-bit long double format E. */
3564 make_nan (e, eisneg (x), TFmode);
3569 exp = (EMULONG) xi[E];
3574 /* round off to nearest or even */
3577 emdnorm (xi, 0, 0, exp, 64);
3585 /* Convert exploded e-type X, that has already been rounded to
3586 113-bit precision, to IEEE 128-bit long double format Y. */
3598 make_nan (b, eiisneg (a), TFmode);
3603 if (REAL_WORDS_BIG_ENDIAN)
3606 q = b + 7; /* point to output exponent */
3608 /* If not denormal, delete the implied bit. */
3613 /* combine sign and exponent */
3615 if (REAL_WORDS_BIG_ENDIAN)
3618 *q++ = *p++ | 0x8000;
3625 *q-- = *p++ | 0x8000;
3629 /* skip over guard word */
3631 /* move the significand */
3632 if (REAL_WORDS_BIG_ENDIAN)
3634 for (i = 0; i < 7; i++)
3639 for (i = 0; i < 7; i++)
3645 /* Convert e-type X to IEEE double extended format E. */
3658 make_nan (e, eisneg (x), XFmode);
3663 /* adjust exponent for offset */
3664 exp = (EMULONG) xi[E];
3669 /* round off to nearest or even */
3672 emdnorm (xi, 0, 0, exp, 64);
3680 /* Convert exploded e-type X, that has already been rounded to
3681 64-bit precision, to IEEE double extended format Y. */
3693 make_nan (b, eiisneg (a), XFmode);
3697 /* Shift denormal long double Intel format significand down one bit. */
3698 if ((a[E] == 0) && ! REAL_WORDS_BIG_ENDIAN)
3708 if (REAL_WORDS_BIG_ENDIAN)
3712 q = b + 4; /* point to output exponent */
3713 /* Clear the last two bytes of 12-byte Intel format. q is pointing
3714 into an array of size 6 (e.g. x[NE]), so the last two bytes are
3715 always there, and there are never more bytes, even when we are using
3716 INTEL_EXTENDED_IEEE_FORMAT. */
3721 /* combine sign and exponent */
3725 *q++ = *p++ | 0x8000;
3732 *q-- = *p++ | 0x8000;
3737 if (REAL_WORDS_BIG_ENDIAN)
3739 #ifdef ARM_EXTENDED_IEEE_FORMAT
3740 /* The exponent is in the lowest 15 bits of the first word. */
3741 *q++ = i ? 0x8000 : 0;
3745 *q++ = *p++ | 0x8000;
3754 *q-- = *p++ | 0x8000;
3759 /* skip over guard word */
3761 /* move the significand */
3763 for (i = 0; i < 4; i++)
3767 for (i = 0; i < 4; i++)
3771 if (REAL_WORDS_BIG_ENDIAN)
3773 for (i = 0; i < 4; i++)
3781 /* Intel long double infinity significand. */
3789 for (i = 0; i < 4; i++)
3795 /* e type to double precision. */
3798 /* Convert e-type X to DEC-format double E. */
3804 etodec (x, e); /* see etodec.c */
3807 /* Convert exploded e-type X, that has already been rounded to
3808 56-bit double precision, to DEC double Y. */
3819 /* Convert e-type X to IBM 370-format double E. */
3825 etoibm (x, e, DFmode);
3828 /* Convert exploded e-type X, that has already been rounded to
3829 56-bit precision, to IBM 370 double Y. */
3835 toibm (x, y, DFmode);
3838 #else /* it's neither DEC nor IBM */
3840 /* Convert e-type X to C4X-format long double E. */
3846 etoc4x (x, e, HFmode);
3849 /* Convert exploded e-type X, that has already been rounded to
3850 56-bit precision, to IBM 370 double Y. */
3856 toc4x (x, y, HFmode);
3859 #else /* it's neither DEC nor IBM nor C4X */
3861 /* Convert e-type X to IEEE double E. */
3874 make_nan (e, eisneg (x), DFmode);
3879 /* adjust exponent for offsets */
3880 exp = (EMULONG) xi[E] - (EXONE - 0x3ff);
3885 /* round off to nearest or even */
3888 emdnorm (xi, 0, 0, exp, 64);
3896 /* Convert exploded e-type X, that has already been rounded to
3897 53-bit precision, to IEEE double Y. */
3909 make_nan (y, eiisneg (x), DFmode);
3915 if (! REAL_WORDS_BIG_ENDIAN)
3918 *y = 0; /* output high order */
3920 *y = 0x8000; /* output sign bit */
3923 if (i >= (unsigned int) 2047)
3925 /* Saturate at largest number less than infinity. */
3928 if (! REAL_WORDS_BIG_ENDIAN)
3942 *y |= (UEMUSHORT) 0x7fef;
3943 if (! REAL_WORDS_BIG_ENDIAN)
3968 i |= *p++ & (UEMUSHORT) 0x0f; /* *p = xi[M] */
3969 *y |= (UEMUSHORT) i; /* high order output already has sign bit set */
3970 if (! REAL_WORDS_BIG_ENDIAN)
3985 #endif /* not C4X */
3986 #endif /* not IBM */
3987 #endif /* not DEC */
3991 /* e type to single precision. */
3994 /* Convert e-type X to IBM 370 float E. */
4000 etoibm (x, e, SFmode);
4003 /* Convert exploded e-type X, that has already been rounded to
4004 float precision, to IBM 370 float Y. */
4010 toibm (x, y, SFmode);
4016 /* Convert e-type X to C4X float E. */
4022 etoc4x (x, e, QFmode);
4025 /* Convert exploded e-type X, that has already been rounded to
4026 float precision, to IBM 370 float Y. */
4032 toc4x (x, y, QFmode);
4037 /* Convert e-type X to IEEE float E. DEC float is the same as IEEE float. */
4050 make_nan (e, eisneg (x), SFmode);
4055 /* adjust exponent for offsets */
4056 exp = (EMULONG) xi[E] - (EXONE - 0177);
4061 /* round off to nearest or even */
4064 emdnorm (xi, 0, 0, exp, 64);
4072 /* Convert exploded e-type X, that has already been rounded to
4073 float precision, to IEEE float Y. */
4085 make_nan (y, eiisneg (x), SFmode);
4091 if (! REAL_WORDS_BIG_ENDIAN)
4097 *y = 0; /* output high order */
4099 *y = 0x8000; /* output sign bit */
4102 /* Handle overflow cases. */
4106 *y |= (UEMUSHORT) 0x7f80;
4111 if (! REAL_WORDS_BIG_ENDIAN)
4119 #else /* no INFINITY */
4120 *y |= (UEMUSHORT) 0x7f7f;
4125 if (! REAL_WORDS_BIG_ENDIAN)
4136 #endif /* no INFINITY */
4148 i |= *p++ & (UEMUSHORT) 0x7f; /* *p = xi[M] */
4149 /* High order output already has sign bit set. */
4155 if (! REAL_WORDS_BIG_ENDIAN)
4164 #endif /* not C4X */
4165 #endif /* not IBM */
4167 /* Compare two e type numbers.
4171 -2 if either a or b is a NaN. */
4177 UEMUSHORT ai[NI], bi[NI];
4183 if (eisnan (a) || eisnan (b))
4192 { /* the signs are different */
4194 for (i = 1; i < NI - 1; i++)
4208 /* both are the same sign */
4223 return (0); /* equality */
4227 if (*(--p) > *(--q))
4228 return (msign); /* p is bigger */
4230 return (-msign); /* p is littler */
4234 /* Find e-type nearest integer to X, as floor (X + 0.5). */
4245 /* Convert HOST_WIDE_INT LP to e type Y. */
4253 unsigned HOST_WIDE_INT ll;
4259 /* make it positive */
4260 ll = (unsigned HOST_WIDE_INT) (-(*lp));
4261 yi[0] = 0xffff; /* put correct sign in the e type number */
4265 ll = (unsigned HOST_WIDE_INT) (*lp);
4267 /* move the long integer to yi significand area */
4268 #if HOST_BITS_PER_WIDE_INT == 64
4269 yi[M] = (UEMUSHORT) (ll >> 48);
4270 yi[M + 1] = (UEMUSHORT) (ll >> 32);
4271 yi[M + 2] = (UEMUSHORT) (ll >> 16);
4272 yi[M + 3] = (UEMUSHORT) ll;
4273 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
4275 yi[M] = (UEMUSHORT) (ll >> 16);
4276 yi[M + 1] = (UEMUSHORT) ll;
4277 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
4280 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
4281 ecleaz (yi); /* it was zero */
4283 yi[E] -= (UEMUSHORT) k;/* subtract shift count from exponent */
4284 emovo (yi, y); /* output the answer */
4287 /* Convert unsigned HOST_WIDE_INT LP to e type Y. */
4291 unsigned HOST_WIDE_INT *lp;
4295 unsigned HOST_WIDE_INT ll;
4301 /* move the long integer to ayi significand area */
4302 #if HOST_BITS_PER_WIDE_INT == 64
4303 yi[M] = (UEMUSHORT) (ll >> 48);
4304 yi[M + 1] = (UEMUSHORT) (ll >> 32);
4305 yi[M + 2] = (UEMUSHORT) (ll >> 16);
4306 yi[M + 3] = (UEMUSHORT) ll;
4307 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
4309 yi[M] = (UEMUSHORT) (ll >> 16);
4310 yi[M + 1] = (UEMUSHORT) ll;
4311 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
4314 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
4315 ecleaz (yi); /* it was zero */
4317 yi[E] -= (UEMUSHORT) k; /* subtract shift count from exponent */
4318 emovo (yi, y); /* output the answer */
4322 /* Find signed HOST_WIDE_INT integer I and floating point fractional
4323 part FRAC of e-type (packed internal format) floating point input X.
4324 The integer output I has the sign of the input, except that
4325 positive overflow is permitted if FIXUNS_TRUNC_LIKE_FIX_TRUNC.
4326 The output e-type fraction FRAC is the positive fractional
4337 unsigned HOST_WIDE_INT ll;
4340 k = (int) xi[E] - (EXONE - 1);
4343 /* if exponent <= 0, integer = 0 and real output is fraction */
4348 if (k > (HOST_BITS_PER_WIDE_INT - 1))
4350 /* long integer overflow: output large integer
4351 and correct fraction */
4353 *i = ((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1);
4356 #ifdef FIXUNS_TRUNC_LIKE_FIX_TRUNC
4357 /* In this case, let it overflow and convert as if unsigned. */
4358 euifrac (x, &ll, frac);
4359 *i = (HOST_WIDE_INT) ll;
4362 /* In other cases, return the largest positive integer. */
4363 *i = (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1;
4368 warning ("overflow on truncation to integer");
4372 /* Shift more than 16 bits: first shift up k-16 mod 16,
4373 then shift up by 16's. */
4374 j = k - ((k >> 4) << 4);
4381 ll = (ll << 16) | xi[M];
4383 while ((k -= 16) > 0);
4390 /* shift not more than 16 bits */
4392 *i = (HOST_WIDE_INT) xi[M] & 0xffff;
4399 if ((k = enormlz (xi)) > NBITS)
4402 xi[E] -= (UEMUSHORT) k;
4408 /* Find unsigned HOST_WIDE_INT integer I and floating point fractional part
4409 FRAC of e-type X. A negative input yields integer output = 0 but
4410 correct fraction. */
4413 euifrac (x, i, frac)
4415 unsigned HOST_WIDE_INT *i;
4418 unsigned HOST_WIDE_INT ll;
4423 k = (int) xi[E] - (EXONE - 1);
4426 /* if exponent <= 0, integer = 0 and argument is fraction */
4431 if (k > HOST_BITS_PER_WIDE_INT)
4433 /* Long integer overflow: output large integer
4434 and correct fraction.
4435 Note, the BSD MicroVAX compiler says that ~(0UL)
4436 is a syntax error. */
4440 warning ("overflow on truncation to unsigned integer");
4444 /* Shift more than 16 bits: first shift up k-16 mod 16,
4445 then shift up by 16's. */
4446 j = k - ((k >> 4) << 4);
4453 ll = (ll << 16) | xi[M];
4455 while ((k -= 16) > 0);
4460 /* shift not more than 16 bits */
4462 *i = (HOST_WIDE_INT) xi[M] & 0xffff;
4465 if (xi[0]) /* A negative value yields unsigned integer 0. */
4471 if ((k = enormlz (xi)) > NBITS)
4474 xi[E] -= (UEMUSHORT) k;
4479 /* Shift the significand of exploded e-type X up or down by SC bits. */
4500 lost |= *p; /* remember lost bits */
4541 return ((int) lost);
4544 /* Shift normalize the significand area of exploded e-type X.
4545 Return the shift count (up = positive). */
4560 return (0); /* already normalized */
4566 /* With guard word, there are NBITS+16 bits available.
4567 Return true if all are zero. */
4571 /* see if high byte is zero */
4572 while ((*p & 0xff00) == 0)
4577 /* now shift 1 bit at a time */
4578 while ((*p & 0x8000) == 0)
4584 mtherr ("enormlz", UNDERFLOW);
4590 /* Normalize by shifting down out of the high guard word
4591 of the significand */
4606 mtherr ("enormlz", OVERFLOW);
4613 /* Powers of ten used in decimal <-> binary conversions. */
4618 #if MAX_LONG_DOUBLE_TYPE_SIZE == 128 && (INTEL_EXTENDED_IEEE_FORMAT == 0)
4619 static UEMUSHORT etens[NTEN + 1][NE] =
4621 {0x6576, 0x4a92, 0x804a, 0x153f,
4622 0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
4623 {0x6a32, 0xce52, 0x329a, 0x28ce,
4624 0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
4625 {0x526c, 0x50ce, 0xf18b, 0x3d28,
4626 0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
4627 {0x9c66, 0x58f8, 0xbc50, 0x5c54,
4628 0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
4629 {0x851e, 0xeab7, 0x98fe, 0x901b,
4630 0xddbb, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
4631 {0x0235, 0x0137, 0x36b1, 0x336c,
4632 0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
4633 {0x50f8, 0x25fb, 0xc76b, 0x6b71,
4634 0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
4635 {0x0000, 0x0000, 0x0000, 0x0000,
4636 0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
4637 {0x0000, 0x0000, 0x0000, 0x0000,
4638 0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
4639 {0x0000, 0x0000, 0x0000, 0x0000,
4640 0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
4641 {0x0000, 0x0000, 0x0000, 0x0000,
4642 0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
4643 {0x0000, 0x0000, 0x0000, 0x0000,
4644 0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
4645 {0x0000, 0x0000, 0x0000, 0x0000,
4646 0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
4649 static UEMUSHORT emtens[NTEN + 1][NE] =
4651 {0x2030, 0xcffc, 0xa1c3, 0x8123,
4652 0x2de3, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
4653 {0x8264, 0xd2cb, 0xf2ea, 0x12d4,
4654 0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
4655 {0xf53f, 0xf698, 0x6bd3, 0x0158,
4656 0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
4657 {0xe731, 0x04d4, 0xe3f2, 0xd332,
4658 0x7132, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
4659 {0xa23e, 0x5308, 0xfefb, 0x1155,
4660 0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
4661 {0xe26d, 0xdbde, 0xd05d, 0xb3f6,
4662 0xac7c, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
4663 {0x2a20, 0x6224, 0x47b3, 0x98d7,
4664 0x3f23, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
4665 {0x0b5b, 0x4af2, 0xa581, 0x18ed,
4666 0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
4667 {0xbf71, 0xa9b3, 0x7989, 0xbe68,
4668 0x4c2e, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
4669 {0x3d4d, 0x7c3d, 0x36ba, 0x0d2b,
4670 0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
4671 {0xc155, 0xa4a8, 0x404e, 0x6113,
4672 0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
4673 {0xd70a, 0x70a3, 0x0a3d, 0xa3d7,
4674 0x3d70, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
4675 {0xcccd, 0xcccc, 0xcccc, 0xcccc,
4676 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
4679 /* LONG_DOUBLE_TYPE_SIZE is other than 128 */
4680 static UEMUSHORT etens[NTEN + 1][NE] =
4682 {0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
4683 {0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
4684 {0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
4685 {0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
4686 {0xddbc, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
4687 {0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
4688 {0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
4689 {0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
4690 {0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
4691 {0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
4692 {0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
4693 {0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
4694 {0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
4697 static UEMUSHORT emtens[NTEN + 1][NE] =
4699 {0x2de4, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
4700 {0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
4701 {0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
4702 {0x7133, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
4703 {0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
4704 {0xac7d, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
4705 {0x3f24, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
4706 {0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
4707 {0x4c2f, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
4708 {0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
4709 {0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
4710 {0x3d71, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
4711 {0xcccd, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
4716 /* Convert float value X to ASCII string STRING with NDIG digits after
4717 the decimal point. */
4720 e24toasc (x, string, ndigs)
4728 etoasc (w, string, ndigs);
4731 /* Convert double value X to ASCII string STRING with NDIG digits after
4732 the decimal point. */
4735 e53toasc (x, string, ndigs)
4743 etoasc (w, string, ndigs);
4746 /* Convert double extended value X to ASCII string STRING with NDIG digits
4747 after the decimal point. */
4750 e64toasc (x, string, ndigs)
4758 etoasc (w, string, ndigs);
4761 /* Convert 128-bit long double value X to ASCII string STRING with NDIG digits
4762 after the decimal point. */
4765 e113toasc (x, string, ndigs)
4773 etoasc (w, string, ndigs);
4777 /* Convert e-type X to ASCII string STRING with NDIGS digits after
4778 the decimal point. */
4780 static char wstring[80]; /* working storage for ASCII output */
4783 etoasc (x, string, ndigs)
4789 UEMUSHORT y[NI], t[NI], u[NI], w[NI];
4790 UEMUSHORT *p, *r, *ten;
4792 int i, j, k, expon, rndsav;
4805 sprintf (wstring, " NaN ");
4809 rndprc = NBITS; /* set to full precision */
4810 emov (x, y); /* retain external format */
4811 if (y[NE - 1] & 0x8000)
4814 y[NE - 1] &= 0x7fff;
4821 ten = &etens[NTEN][0];
4823 /* Test for zero exponent */
4826 for (k = 0; k < NE - 1; k++)
4829 goto tnzro; /* denormalized number */
4831 goto isone; /* valid all zeros */
4835 /* Test for infinity. */
4836 if (y[NE - 1] == 0x7fff)
4839 sprintf (wstring, " -Infinity ");
4841 sprintf (wstring, " Infinity ");
4845 /* Test for exponent nonzero but significand denormalized.
4846 * This is an error condition.
4848 if ((y[NE - 1] != 0) && ((y[NE - 2] & 0x8000) == 0))
4850 mtherr ("etoasc", DOMAIN);
4851 sprintf (wstring, "NaN");
4855 /* Compare to 1.0 */
4864 { /* Number is greater than 1 */
4865 /* Convert significand to an integer and strip trailing decimal zeros. */
4867 u[NE - 1] = EXONE + NBITS - 1;
4869 p = &etens[NTEN - 4][0];
4875 for (j = 0; j < NE - 1; j++)
4888 /* Rescale from integer significand */
4889 u[NE - 1] += y[NE - 1] - (unsigned int) (EXONE + NBITS - 1);
4891 /* Find power of 10 */
4895 /* An unordered compare result shouldn't happen here. */
4896 while (ecmp (ten, u) <= 0)
4898 if (ecmp (p, u) <= 0)
4911 { /* Number is less than 1.0 */
4912 /* Pad significand with trailing decimal zeros. */
4915 while ((y[NE - 2] & 0x8000) == 0)
4924 for (i = 0; i < NDEC + 1; i++)
4926 if ((w[NI - 1] & 0x7) != 0)
4928 /* multiply by 10 */
4941 if (eone[NE - 1] <= u[1])
4953 while (ecmp (eone, w) > 0)
4955 if (ecmp (p, w) >= 0)
4970 /* Find the first (leading) digit. */
4976 digit = equot[NI - 1];
4977 while ((digit == 0) && (ecmp (y, ezero) != 0))
4985 digit = equot[NI - 1];
4993 /* Examine number of digits requested by caller. */
5011 *s++ = (char)digit + '0';
5014 /* Generate digits after the decimal point. */
5015 for (k = 0; k <= ndigs; k++)
5017 /* multiply current number by 10, without normalizing */
5024 *s++ = (char) equot[NI - 1] + '0';
5026 digit = equot[NI - 1];
5029 /* round off the ASCII string */
5032 /* Test for critical rounding case in ASCII output. */
5036 if (ecmp (t, ezero) != 0)
5037 goto roun; /* round to nearest */
5039 if ((*(s - 1) & 1) == 0)
5040 goto doexp; /* round to even */
5043 /* Round up and propagate carry-outs */
5047 /* Carry out to most significant digit? */
5054 /* Most significant digit carries to 10? */
5062 /* Round up and carry out from less significant digits */
5074 sprintf (ss, "e+%d", expon);
5076 sprintf (ss, "e%d", expon);
5078 sprintf (ss, "e%d", expon);
5081 /* copy out the working string */
5084 while (*ss == ' ') /* strip possible leading space */
5086 while ((*s++ = *ss++) != '\0')
5091 /* Convert ASCII string to floating point.
5093 Numeric input is a free format decimal number of any length, with
5094 or without decimal point. Entering E after the number followed by an
5095 integer number causes the second number to be interpreted as a power of
5096 10 to be multiplied by the first number (i.e., "scientific" notation). */
5098 /* Convert ASCII string S to single precision float value Y. */
5109 /* Convert ASCII string S to double precision value Y. */
5116 #if defined(DEC) || defined(IBM)
5128 /* Convert ASCII string S to double extended value Y. */
5138 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
5139 /* Convert ASCII string S to 128-bit long double Y. */
5146 asctoeg (s, y, 113);
5150 /* Convert ASCII string S to e type Y. */
5157 asctoeg (s, y, NBITS);
5160 /* Convert ASCII string SS to e type Y, with a specified rounding precision
5161 of OPREC bits. BASE is 16 for C99 hexadecimal floating constants. */
5164 asctoeg (ss, y, oprec)
5169 UEMUSHORT yy[NI], xt[NI], tt[NI];
5170 int esign, decflg, sgnflg, nexp, exp, prec, lost;
5171 int i, k, trail, c, rndsav;
5174 char *sp, *s, *lstr;
5177 /* Copy the input string. */
5178 lstr = (char *) alloca (strlen (ss) + 1);
5180 while (*ss == ' ') /* skip leading spaces */
5184 while ((*sp++ = *ss++) != '\0')
5188 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
5195 rndprc = NBITS; /* Set to full precision */
5208 if ((k >= 0) && (k < base))
5210 /* Ignore leading zeros */
5211 if ((prec == 0) && (decflg == 0) && (k == 0))
5213 /* Identify and strip trailing zeros after the decimal point. */
5214 if ((trail == 0) && (decflg != 0))
5217 while (ISDIGIT (*sp) || (base == 16 && ISXDIGIT (*sp)))
5219 /* Check for syntax error */
5221 if ((base != 10 || ((c != 'e') && (c != 'E')))
5222 && (base != 16 || ((c != 'p') && (c != 'P')))
5224 && (c != '\n') && (c != '\r') && (c != ' ')
5226 goto unexpected_char_error;
5235 /* If enough digits were given to more than fill up the yy register,
5236 continuing until overflow into the high guard word yy[2]
5237 guarantees that there will be a roundoff bit at the top
5238 of the low guard word after normalization. */
5245 nexp += 4; /* count digits after decimal point */
5247 eshup1 (yy); /* multiply current number by 16 */
5255 nexp += 1; /* count digits after decimal point */
5257 eshup1 (yy); /* multiply current number by 10 */
5263 /* Insert the current digit. */
5265 xt[NI - 2] = (UEMUSHORT) k;
5270 /* Mark any lost non-zero digit. */
5272 /* Count lost digits before the decimal point. */
5294 case '.': /* decimal point */
5296 goto unexpected_char_error;
5302 goto unexpected_char_error;
5307 goto unexpected_char_error;
5320 unexpected_char_error:
5324 mtherr ("asctoe", DOMAIN);
5333 /* Exponent interpretation */
5335 /* 0.0eXXX is zero, regardless of XXX. Check for the 0.0. */
5336 for (k = 0; k < NI; k++)
5347 /* check for + or - */
5355 while (ISDIGIT (*s))
5364 if ((exp > MAXDECEXP) && (base == 10))
5368 yy[E] = 0x7fff; /* infinity */
5371 if ((exp < MINDECEXP) && (base == 10))
5381 /* Base 16 hexadecimal floating constant. */
5382 if ((k = enormlz (yy)) > NBITS)
5387 /* Adjust the exponent. NEXP is the number of hex digits,
5388 EXP is a power of 2. */
5389 lexp = (EXONE - 1 + NBITS) - k + yy[E] + exp - nexp;
5399 /* Pad trailing zeros to minimize power of 10, per IEEE spec. */
5400 while ((nexp > 0) && (yy[2] == 0))
5412 if ((k = enormlz (yy)) > NBITS)
5417 lexp = (EXONE - 1 + NBITS) - k;
5418 emdnorm (yy, lost, 0, lexp, 64);
5421 /* Convert to external format:
5423 Multiply by 10**nexp. If precision is 64 bits,
5424 the maximum relative error incurred in forming 10**n
5425 for 0 <= n <= 324 is 8.2e-20, at 10**180.
5426 For 0 <= n <= 999, the peak relative error is 1.4e-19 at 10**947.
5427 For 0 >= n >= -999, it is -1.55e-19 at 10**-435. */
5442 /* Punt. Can't handle this without 2 divides. */
5443 emovi (etens[0], tt);
5456 emul (etens[i], xt, xt);
5460 while (exp <= MAXP);
5479 /* Round and convert directly to the destination type */
5481 lexp -= EXONE - 0x3ff;
5483 else if (oprec == 24 || oprec == 32)
5484 lexp -= (EXONE - 0x7f);
5487 else if (oprec == 24 || oprec == 56)
5488 lexp -= EXONE - (0x41 << 2);
5490 else if (oprec == 24)
5491 lexp -= EXONE - 0177;
5495 else if (oprec == 56)
5496 lexp -= EXONE - 0201;
5499 emdnorm (yy, lost, 0, lexp, 64);
5509 todec (yy, y); /* see etodec.c */
5514 toibm (yy, y, DFmode);
5519 toc4x (yy, y, HFmode);
5532 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
5545 /* Return Y = largest integer not greater than X (truncated toward minus
5548 static const UEMUSHORT bmask[] =
5577 emov (x, f); /* leave in external format */
5578 expon = (int) f[NE - 1];
5579 e = (expon & 0x7fff) - (EXONE - 1);
5585 /* number of bits to clear out */
5597 /* clear the remaining bits */
5599 /* truncate negatives toward minus infinity */
5602 if ((UEMUSHORT) expon & (UEMUSHORT) 0x8000)
5604 for (i = 0; i < NE - 1; i++)
5617 /* Return S and EXP such that S * 2^EXP = X and .5 <= S < 1.
5618 For example, 1.1 = 0.55 * 2^1. */
5630 /* Handle denormalized numbers properly using long integer exponent. */
5631 li = (EMULONG) ((EMUSHORT) xi[1]);
5639 *exp = (int) (li - 0x3ffe);
5643 /* Return e type Y = X * 2^PWR2. */
5659 emdnorm (xi, i, i, li, 64);
5665 /* C = remainder after dividing B by A, all e type values.
5666 Least significant integer quotient bits left in EQUOT. */
5670 UEMUSHORT a[], b[], c[];
5672 UEMUSHORT den[NI], num[NI];
5676 || (ecmp (a, ezero) == 0)
5684 if (ecmp (a, ezero) == 0)
5686 mtherr ("eremain", SING);
5692 eiremain (den, num);
5693 /* Sign of remainder = sign of quotient */
5702 /* Return quotient of exploded e-types NUM / DEN in EQUOT,
5703 remainder in NUM. */
5707 UEMUSHORT den[], num[];
5713 ld -= enormlz (den);
5715 ln -= enormlz (num);
5719 if (ecmpm (den, num) <= 0)
5731 emdnorm (num, 0, 0, ln, 0);
5734 /* Report an error condition CODE encountered in function NAME.
5736 Mnemonic Value Significance
5738 DOMAIN 1 argument domain error
5739 SING 2 function singularity
5740 OVERFLOW 3 overflow range error
5741 UNDERFLOW 4 underflow range error
5742 TLOSS 5 total loss of precision
5743 PLOSS 6 partial loss of precision
5744 INVALID 7 NaN - producing operation
5745 EDOM 33 Unix domain error code
5746 ERANGE 34 Unix range error code
5748 The order of appearance of the following messages is bound to the
5749 error codes defined above. */
5759 /* The string passed by the calling program is supposed to be the
5760 name of the function in which the error occurred.
5761 The code argument selects which error message string will be printed. */
5763 if (strcmp (name, "esub") == 0)
5764 name = "subtraction";
5765 else if (strcmp (name, "ediv") == 0)
5767 else if (strcmp (name, "emul") == 0)
5768 name = "multiplication";
5769 else if (strcmp (name, "enormlz") == 0)
5770 name = "normalization";
5771 else if (strcmp (name, "etoasc") == 0)
5772 name = "conversion to text";
5773 else if (strcmp (name, "asctoe") == 0)
5775 else if (strcmp (name, "eremain") == 0)
5777 else if (strcmp (name, "esqrt") == 0)
5778 name = "square root";
5783 case DOMAIN: warning ("%s: argument domain error" , name); break;
5784 case SING: warning ("%s: function singularity" , name); break;
5785 case OVERFLOW: warning ("%s: overflow range error" , name); break;
5786 case UNDERFLOW: warning ("%s: underflow range error" , name); break;
5787 case TLOSS: warning ("%s: total loss of precision" , name); break;
5788 case PLOSS: warning ("%s: partial loss of precision", name); break;
5789 case INVALID: warning ("%s: NaN - producing operation", name); break;
5794 /* Set global error message word */
5799 /* Convert DEC double precision D to e type E. */
5809 ecleaz (y); /* start with a zero */
5810 p = y; /* point to our number */
5811 r = *d; /* get DEC exponent word */
5812 if (*d & (unsigned int) 0x8000)
5813 *p = 0xffff; /* fill in our sign */
5814 ++p; /* bump pointer to our exponent word */
5815 r &= 0x7fff; /* strip the sign bit */
5816 if (r == 0) /* answer = 0 if high order DEC word = 0 */
5820 r >>= 7; /* shift exponent word down 7 bits */
5821 r += EXONE - 0201; /* subtract DEC exponent offset */
5822 /* add our e type exponent offset */
5823 *p++ = r; /* to form our exponent */
5825 r = *d++; /* now do the high order mantissa */
5826 r &= 0177; /* strip off the DEC exponent and sign bits */
5827 r |= 0200; /* the DEC understood high order mantissa bit */
5828 *p++ = r; /* put result in our high guard word */
5830 *p++ = *d++; /* fill in the rest of our mantissa */
5834 eshdn8 (y); /* shift our mantissa down 8 bits */
5839 /* Convert e type X to DEC double precision D. */
5850 /* Adjust exponent for offsets. */
5851 exp = (EMULONG) xi[E] - (EXONE - 0201);
5852 /* Round off to nearest or even. */
5855 emdnorm (xi, 0, 0, exp, 64);
5860 /* Convert exploded e-type X, that has already been rounded to
5861 56-bit precision, to DEC format double Y. */
5907 /* Convert IBM single/double precision to e type. */
5913 enum machine_mode mode;
5918 ecleaz (y); /* start with a zero */
5919 p = y; /* point to our number */
5920 r = *d; /* get IBM exponent word */
5921 if (*d & (unsigned int) 0x8000)
5922 *p = 0xffff; /* fill in our sign */
5923 ++p; /* bump pointer to our exponent word */
5924 r &= 0x7f00; /* strip the sign bit */
5925 r >>= 6; /* shift exponent word down 6 bits */
5926 /* in fact shift by 8 right and 2 left */
5927 r += EXONE - (0x41 << 2); /* subtract IBM exponent offset */
5928 /* add our e type exponent offset */
5929 *p++ = r; /* to form our exponent */
5931 *p++ = *d++ & 0xff; /* now do the high order mantissa */
5932 /* strip off the IBM exponent and sign bits */
5933 if (mode != SFmode) /* there are only 2 words in SFmode */
5935 *p++ = *d++; /* fill in the rest of our mantissa */
5940 if (y[M] == 0 && y[M+1] == 0 && y[M+2] == 0 && y[M+3] == 0)
5943 y[E] -= 5 + enormlz (y); /* now normalise the mantissa */
5944 /* handle change in RADIX */
5950 /* Convert e type to IBM single/double precision. */
5955 enum machine_mode mode;
5962 exp = (EMULONG) xi[E] - (EXONE - (0x41 << 2)); /* adjust exponent for offsets */
5963 /* round off to nearest or even */
5966 emdnorm (xi, 0, 0, exp, 64);
5968 toibm (xi, d, mode);
5974 enum machine_mode mode;
6027 /* Convert C4X single/double precision to e type. */
6033 enum machine_mode mode;
6042 /* Short-circuit the zero case. */
6043 if ((d[0] == 0x8000)
6045 && ((mode == QFmode) || ((d[2] == 0x0000) && (d[3] == 0x0000))))
6056 ecleaz (y); /* start with a zero */
6057 r = d[0]; /* get sign/exponent part */
6058 if (r & (unsigned int) 0x0080)
6060 y[0] = 0xffff; /* fill in our sign */
6068 r >>= 8; /* Shift exponent word down 8 bits. */
6069 if (r & 0x80) /* Make the exponent negative if it is. */
6071 r = r | (~0 & ~0xff);
6076 /* Now do the high order mantissa. We don't "or" on the high bit
6077 because it is 2 (not 1) and is handled a little differently
6082 if (mode != QFmode) /* There are only 2 words in QFmode. */
6084 y[M+2] = d[2]; /* Fill in the rest of our mantissa. */
6094 /* Now do the two's complement on the data. */
6096 carry = 1; /* Initially add 1 for the two's complement. */
6097 for (i=size + M; i > M; i--)
6099 if (carry && (y[i] == 0x0000))
6101 /* We overflowed into the next word, carry is the same. */
6102 y[i] = carry ? 0x0000 : 0xffff;
6106 /* No overflow, just invert and add carry. */
6107 y[i] = ((~y[i]) + carry) & 0xffff;
6122 /* Add our e type exponent offset to form our exponent. */
6126 /* Now do the high order mantissa strip off the exponent and sign
6127 bits and add the high 1 bit. */
6128 y[M] = (d[0] & 0x7f) | 0x80;
6131 if (mode != QFmode) /* There are only 2 words in QFmode. */
6133 y[M+2] = d[2]; /* Fill in the rest of our mantissa. */
6143 /* Convert e type to C4X single/double precision. */
6148 enum machine_mode mode;
6156 /* Adjust exponent for offsets. */
6157 exp = (EMULONG) xi[E] - (EXONE - 0x7f);
6159 /* Round off to nearest or even. */
6161 rndprc = mode == QFmode ? 24 : 32;
6162 emdnorm (xi, 0, 0, exp, 64);
6164 toc4x (xi, d, mode);
6170 enum machine_mode mode;
6176 /* Short-circuit the zero case */
6177 if ((x[0] == 0) /* Zero exponent and sign */
6179 && (x[M] == 0) /* The rest is for zero mantissa */
6181 /* Only check for double if necessary */
6182 && ((mode == QFmode) || ((x[M+2] == 0) && (x[M+3] == 0))))
6184 /* We have a zero. Put it into the output and return. */
6197 /* Negative number require a two's complement conversion of the
6203 i = ((int) x[1]) - 0x7f;
6205 /* Now add 1 to the inverted data to do the two's complement. */
6215 x[v] = carry ? 0x0000 : 0xffff;
6219 x[v] = ((~x[v]) + carry) & 0xffff;
6225 /* The following is a special case. The C4X negative float requires
6226 a zero in the high bit (because the format is (2 - x) x 2^m), so
6227 if a one is in that bit, we have to shift left one to get rid
6228 of it. This only occurs if the number is -1 x 2^m. */
6229 if (x[M+1] & 0x8000)
6231 /* This is the case of -1 x 2^m, we have to rid ourselves of the
6232 high sign bit and shift the exponent. */
6239 i = ((int) x[1]) - 0x7f;
6242 if ((i < -128) || (i > 127))
6257 y[0] |= ((i & 0xff) << 8);
6261 y[0] |= x[M] & 0x7f;
6271 /* Output a binary NaN bit pattern in the target machine's format. */
6273 /* If special NaN bit patterns are required, define them in tm.h
6274 as arrays of unsigned 16-bit shorts. Otherwise, use the default
6280 UEMUSHORT TFbignan[8] =
6281 {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
6282 UEMUSHORT TFlittlenan[8] = {0, 0, 0, 0, 0, 0, 0x8000, 0xffff};
6290 UEMUSHORT XFbignan[6] =
6291 {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
6292 UEMUSHORT XFlittlenan[6] = {0, 0, 0, 0xc000, 0xffff, 0};
6300 UEMUSHORT DFbignan[4] = {0x7fff, 0xffff, 0xffff, 0xffff};
6301 UEMUSHORT DFlittlenan[4] = {0, 0, 0, 0xfff8};
6309 UEMUSHORT SFbignan[2] = {0x7fff, 0xffff};
6310 UEMUSHORT SFlittlenan[2] = {0, 0xffc0};
6317 make_nan (nan, sign, mode)
6320 enum machine_mode mode;
6327 /* Possibly the `reserved operand' patterns on a VAX can be
6328 used like NaN's, but probably not in the same way as IEEE. */
6329 #if !defined(DEC) && !defined(IBM) && !defined(C4X)
6331 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)
6333 if (REAL_WORDS_BIG_ENDIAN)
6343 if (REAL_WORDS_BIG_ENDIAN)
6351 if (REAL_WORDS_BIG_ENDIAN)
6360 if (REAL_WORDS_BIG_ENDIAN)
6370 if (REAL_WORDS_BIG_ENDIAN)
6371 *nan++ = (sign << 15) | (*p++ & 0x7fff);
6374 if (! REAL_WORDS_BIG_ENDIAN)
6375 *nan = (sign << 15) | (*p & 0x7fff);
6379 /* This is the inverse of the function `etarsingle' invoked by
6380 REAL_VALUE_TO_TARGET_SINGLE. */
6383 ereal_unto_float (f)
6390 /* Convert 32 bit integer to array of 16 bit pieces in target machine order.
6391 This is the inverse operation to what the function `endian' does. */
6392 if (REAL_WORDS_BIG_ENDIAN)
6394 s[0] = (UEMUSHORT) (f >> 16);
6395 s[1] = (UEMUSHORT) f;
6399 s[0] = (UEMUSHORT) f;
6400 s[1] = (UEMUSHORT) (f >> 16);
6402 /* Convert and promote the target float to E-type. */
6404 /* Output E-type to REAL_VALUE_TYPE. */
6410 /* This is the inverse of the function `etardouble' invoked by
6411 REAL_VALUE_TO_TARGET_DOUBLE. */
6414 ereal_unto_double (d)
6421 /* Convert array of HOST_WIDE_INT to equivalent array of 16-bit pieces. */
6422 if (REAL_WORDS_BIG_ENDIAN)
6424 s[0] = (UEMUSHORT) (d[0] >> 16);
6425 s[1] = (UEMUSHORT) d[0];
6426 s[2] = (UEMUSHORT) (d[1] >> 16);
6427 s[3] = (UEMUSHORT) d[1];
6431 /* Target float words are little-endian. */
6432 s[0] = (UEMUSHORT) d[0];
6433 s[1] = (UEMUSHORT) (d[0] >> 16);
6434 s[2] = (UEMUSHORT) d[1];
6435 s[3] = (UEMUSHORT) (d[1] >> 16);
6437 /* Convert target double to E-type. */
6439 /* Output E-type to REAL_VALUE_TYPE. */
6445 /* Convert an SFmode target `float' value to a REAL_VALUE_TYPE.
6446 This is somewhat like ereal_unto_float, but the input types
6447 for these are different. */
6450 ereal_from_float (f)
6457 /* Convert 32 bit integer to array of 16 bit pieces in target machine order.
6458 This is the inverse operation to what the function `endian' does. */
6459 if (REAL_WORDS_BIG_ENDIAN)
6461 s[0] = (UEMUSHORT) (f >> 16);
6462 s[1] = (UEMUSHORT) f;
6466 s[0] = (UEMUSHORT) f;
6467 s[1] = (UEMUSHORT) (f >> 16);
6469 /* Convert and promote the target float to E-type. */
6471 /* Output E-type to REAL_VALUE_TYPE. */
6477 /* Convert a DFmode target `double' value to a REAL_VALUE_TYPE.
6478 This is somewhat like ereal_unto_double, but the input types
6479 for these are different.
6481 The DFmode is stored as an array of HOST_WIDE_INT in the target's
6482 data format, with no holes in the bit packing. The first element
6483 of the input array holds the bits that would come first in the
6484 target computer's memory. */
6487 ereal_from_double (d)
6494 /* Convert array of HOST_WIDE_INT to equivalent array of 16-bit pieces. */
6495 if (REAL_WORDS_BIG_ENDIAN)
6497 #if HOST_BITS_PER_WIDE_INT == 32
6498 s[0] = (UEMUSHORT) (d[0] >> 16);
6499 s[1] = (UEMUSHORT) d[0];
6500 s[2] = (UEMUSHORT) (d[1] >> 16);
6501 s[3] = (UEMUSHORT) d[1];
6503 /* In this case the entire target double is contained in the
6504 first array element. The second element of the input is
6506 s[0] = (UEMUSHORT) (d[0] >> 48);
6507 s[1] = (UEMUSHORT) (d[0] >> 32);
6508 s[2] = (UEMUSHORT) (d[0] >> 16);
6509 s[3] = (UEMUSHORT) d[0];
6514 /* Target float words are little-endian. */
6515 s[0] = (UEMUSHORT) d[0];
6516 s[1] = (UEMUSHORT) (d[0] >> 16);
6517 #if HOST_BITS_PER_WIDE_INT == 32
6518 s[2] = (UEMUSHORT) d[1];
6519 s[3] = (UEMUSHORT) (d[1] >> 16);
6521 s[2] = (UEMUSHORT) (d[0] >> 32);
6522 s[3] = (UEMUSHORT) (d[0] >> 48);
6525 /* Convert target double to E-type. */
6527 /* Output E-type to REAL_VALUE_TYPE. */
6534 /* Convert target computer unsigned 64-bit integer to e-type.
6535 The endian-ness of DImode follows the convention for integers,
6536 so we use WORDS_BIG_ENDIAN here, not REAL_WORDS_BIG_ENDIAN. */
6540 UEMUSHORT *di; /* Address of the 64-bit int. */
6547 if (WORDS_BIG_ENDIAN)
6549 for (k = M; k < M + 4; k++)
6554 for (k = M + 3; k >= M; k--)
6557 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
6558 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
6559 ecleaz (yi); /* it was zero */
6561 yi[E] -= (UEMUSHORT) k;/* subtract shift count from exponent */
6565 /* Convert target computer signed 64-bit integer to e-type. */
6569 UEMUSHORT *di; /* Address of the 64-bit int. */
6572 unsigned EMULONG acc;
6578 if (WORDS_BIG_ENDIAN)
6580 for (k = M; k < M + 4; k++)
6585 for (k = M + 3; k >= M; k--)
6588 /* Take absolute value */
6594 for (k = M + 3; k >= M; k--)
6596 acc = (unsigned EMULONG) (~yi[k] & 0xffff) + carry;
6603 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
6604 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
6605 ecleaz (yi); /* it was zero */
6607 yi[E] -= (UEMUSHORT) k;/* subtract shift count from exponent */
6614 /* Convert e-type to unsigned 64-bit int. */
6630 k = (int) xi[E] - (EXONE - 1);
6633 for (j = 0; j < 4; j++)
6639 for (j = 0; j < 4; j++)
6642 warning ("overflow on truncation to integer");
6647 /* Shift more than 16 bits: first shift up k-16 mod 16,
6648 then shift up by 16's. */
6649 j = k - ((k >> 4) << 4);
6653 if (WORDS_BIG_ENDIAN)
6664 if (WORDS_BIG_ENDIAN)
6669 while ((k -= 16) > 0);
6673 /* shift not more than 16 bits */
6678 if (WORDS_BIG_ENDIAN)
6697 /* Convert e-type to signed 64-bit int. */
6704 unsigned EMULONG acc;
6711 k = (int) xi[E] - (EXONE - 1);
6714 for (j = 0; j < 4; j++)
6720 for (j = 0; j < 4; j++)
6723 warning ("overflow on truncation to integer");
6729 /* Shift more than 16 bits: first shift up k-16 mod 16,
6730 then shift up by 16's. */
6731 j = k - ((k >> 4) << 4);
6735 if (WORDS_BIG_ENDIAN)
6746 if (WORDS_BIG_ENDIAN)
6751 while ((k -= 16) > 0);
6755 /* shift not more than 16 bits */
6758 if (WORDS_BIG_ENDIAN)
6774 /* Negate if negative */
6778 if (WORDS_BIG_ENDIAN)
6780 for (k = 0; k < 4; k++)
6782 acc = (unsigned EMULONG) (~(*isave) & 0xffff) + carry;
6783 if (WORDS_BIG_ENDIAN)
6795 /* Longhand square root routine. */
6798 static int esqinited = 0;
6799 static unsigned short sqrndbit[NI];
6805 UEMUSHORT temp[NI], num[NI], sq[NI], xx[NI];
6807 int i, j, k, n, nlups;
6812 sqrndbit[NI - 2] = 1;
6815 /* Check for arg <= 0 */
6816 i = ecmp (x, ezero);
6821 mtherr ("esqrt", DOMAIN);
6837 /* Bring in the arg and renormalize if it is denormal. */
6839 m = (EMULONG) xx[1]; /* local long word exponent */
6843 /* Divide exponent by 2 */
6845 exp = (unsigned short) ((m / 2) + 0x3ffe);
6847 /* Adjust if exponent odd */
6857 n = 8; /* get 8 bits of result per inner loop */
6863 /* bring in next word of arg */
6865 num[NI - 1] = xx[j + 3];
6866 /* Do additional bit on last outer loop, for roundoff. */
6869 for (i = 0; i < n; i++)
6871 /* Next 2 bits of arg */
6874 /* Shift up answer */
6876 /* Make trial divisor */
6877 for (k = 0; k < NI; k++)
6880 eaddm (sqrndbit, temp);
6881 /* Subtract and insert answer bit if it goes in */
6882 if (ecmpm (temp, num) <= 0)
6892 /* Adjust for extra, roundoff loop done. */
6893 exp += (NBITS - 1) - rndprc;
6895 /* Sticky bit = 1 if the remainder is nonzero. */
6897 for (i = 3; i < NI; i++)
6900 /* Renormalize and round off. */
6901 emdnorm (sq, k, 0, exp, 64);
6905 #endif /* EMU_NON_COMPILE not defined */
6907 /* Return the binary precision of the significand for a given
6908 floating point mode. The mode can hold an integer value
6909 that many bits wide, without losing any bits. */
6912 significand_size (mode)
6913 enum machine_mode mode;
6916 /* Don't test the modes, but their sizes, lest this
6917 code won't work for BITS_PER_UNIT != 8 . */
6919 switch (GET_MODE_BITSIZE (mode))
6923 #if TARGET_FLOAT_FORMAT == C4X_FLOAT_FORMAT
6930 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
6933 #if TARGET_FLOAT_FORMAT == IBM_FLOAT_FORMAT
6936 #if TARGET_FLOAT_FORMAT == VAX_FLOAT_FORMAT
6939 #if TARGET_FLOAT_FORMAT == C4X_FLOAT_FORMAT
6952 #if (INTEL_EXTENDED_IEEE_FORMAT == 0)