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 Free Software Foundation, Inc.
4 Contributed by Stephen L. Moshier (moshier@world.std.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
27 /* To enable support of XFmode extended real floating point, define
28 LONG_DOUBLE_TYPE_SIZE 96 in the tm.h file (m68k.h or i386.h).
30 To support cross compilation between IEEE, VAX and IBM floating
31 point formats, define REAL_ARITHMETIC in the tm.h file.
33 In either case the machine files (tm.h) must not contain any code
34 that tries to use host floating point arithmetic to convert
35 REAL_VALUE_TYPEs from `double' to `float', pass them to fprintf,
36 etc. In cross-compile situations a REAL_VALUE_TYPE may not
37 be intelligible to the host computer's native arithmetic.
39 The emulator defaults to the host's floating point format so that
40 its decimal conversion functions can be used if desired (see
43 The first part of this file interfaces gcc to a floating point
44 arithmetic suite that was not written with gcc in mind. Avoid
45 changing the low-level arithmetic routines unless you have suitable
46 test programs available. A special version of the PARANOIA floating
47 point arithmetic tester, modified for this purpose, can be found on
48 usc.edu: /pub/C-numanal/ieeetest.zoo. Other tests, and libraries of
49 XFmode and TFmode transcendental functions, can be obtained by ftp from
50 netlib.att.com: netlib/cephes. */
52 /* Type of computer arithmetic.
53 Only one of DEC, IBM, IEEE, or UNK should get defined.
55 `IEEE', when REAL_WORDS_BIG_ENDIAN is non-zero, refers generically
56 to big-endian IEEE floating-point data structure. This definition
57 should work in SFmode `float' type and DFmode `double' type on
58 virtually all big-endian IEEE machines. If LONG_DOUBLE_TYPE_SIZE
59 has been defined to be 96, then IEEE also invokes the particular
60 XFmode (`long double' type) data structure used by the Motorola
61 680x0 series processors.
63 `IEEE', when REAL_WORDS_BIG_ENDIAN is zero, refers generally to
64 little-endian IEEE machines. In this case, if LONG_DOUBLE_TYPE_SIZE
65 has been defined to be 96, then IEEE also invokes the particular
66 XFmode `long double' data structure used by the Intel 80x86 series
69 `DEC' refers specifically to the Digital Equipment Corp PDP-11
70 and VAX floating point data structure. This model currently
71 supports no type wider than DFmode.
73 `IBM' refers specifically to the IBM System/370 and compatible
74 floating point data structure. This model currently supports
75 no type wider than DFmode. The IBM conversions were contributed by
76 frank@atom.ansto.gov.au (Frank Crawford).
78 If LONG_DOUBLE_TYPE_SIZE = 64 (the default, unless tm.h defines it)
79 then `long double' and `double' are both implemented, but they
80 both mean DFmode. In this case, the software floating-point
81 support available here is activated by writing
82 #define REAL_ARITHMETIC
85 The case LONG_DOUBLE_TYPE_SIZE = 128 activates TFmode support
86 and may deactivate XFmode since `long double' is used to refer
89 The macros FLOAT_WORDS_BIG_ENDIAN, HOST_FLOAT_WORDS_BIG_ENDIAN,
90 contributed by Richard Earnshaw <Richard.Earnshaw@cl.cam.ac.uk>,
91 separate the floating point unit's endian-ness from that of
92 the integer addressing. This permits one to define a big-endian
93 FPU on a little-endian machine (e.g., ARM). An extension to
94 BYTES_BIG_ENDIAN may be required for some machines in the future.
95 These optional macros may be defined in tm.h. In real.h, they
96 default to WORDS_BIG_ENDIAN, etc., so there is no need to define
97 them for any normal host or target machine on which the floats
98 and the integers have the same endian-ness. */
101 /* The following converts gcc macros into the ones used by this file. */
103 /* REAL_ARITHMETIC defined means that macros in real.h are
104 defined to call emulator functions. */
105 #ifdef REAL_ARITHMETIC
107 #if TARGET_FLOAT_FORMAT == VAX_FLOAT_FORMAT
108 /* PDP-11, Pro350, VAX: */
110 #else /* it's not VAX */
111 #if TARGET_FLOAT_FORMAT == IBM_FLOAT_FORMAT
112 /* IBM System/370 style */
114 #else /* it's also not an IBM */
115 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
117 #else /* it's not IEEE either */
118 /* UNKnown arithmetic. We don't support this and can't go on. */
119 unknown arithmetic type
121 #endif /* not IEEE */
125 #define REAL_WORDS_BIG_ENDIAN FLOAT_WORDS_BIG_ENDIAN
128 /* REAL_ARITHMETIC not defined means that the *host's* data
129 structure will be used. It may differ by endian-ness from the
130 target machine's structure and will get its ends swapped
131 accordingly (but not here). Probably only the decimal <-> binary
132 functions in this file will actually be used in this case. */
134 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
136 #else /* it's not VAX */
137 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
138 /* IBM System/370 style */
140 #else /* it's also not an IBM */
141 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
143 #else /* it's not IEEE either */
144 unknown arithmetic type
146 #endif /* not IEEE */
150 #define REAL_WORDS_BIG_ENDIAN HOST_FLOAT_WORDS_BIG_ENDIAN
152 #endif /* REAL_ARITHMETIC not defined */
154 /* Define INFINITY for support of infinity.
155 Define NANS for support of Not-a-Number's (NaN's). */
156 #if !defined(DEC) && !defined(IBM)
161 /* Support of NaNs requires support of infinity. */
168 /* Find a host integer type that is at least 16 bits wide,
169 and another type at least twice whatever that size is. */
171 #if HOST_BITS_PER_CHAR >= 16
172 #define EMUSHORT char
173 #define EMUSHORT_SIZE HOST_BITS_PER_CHAR
174 #define EMULONG_SIZE (2 * HOST_BITS_PER_CHAR)
176 #if HOST_BITS_PER_SHORT >= 16
177 #define EMUSHORT short
178 #define EMUSHORT_SIZE HOST_BITS_PER_SHORT
179 #define EMULONG_SIZE (2 * HOST_BITS_PER_SHORT)
181 #if HOST_BITS_PER_INT >= 16
183 #define EMUSHORT_SIZE HOST_BITS_PER_INT
184 #define EMULONG_SIZE (2 * HOST_BITS_PER_INT)
186 #if HOST_BITS_PER_LONG >= 16
187 #define EMUSHORT long
188 #define EMUSHORT_SIZE HOST_BITS_PER_LONG
189 #define EMULONG_SIZE (2 * HOST_BITS_PER_LONG)
191 /* You will have to modify this program to have a smaller unit size. */
192 #define EMU_NON_COMPILE
198 #if HOST_BITS_PER_SHORT >= EMULONG_SIZE
199 #define EMULONG short
201 #if HOST_BITS_PER_INT >= EMULONG_SIZE
204 #if HOST_BITS_PER_LONG >= EMULONG_SIZE
207 #if HOST_BITS_PER_LONGLONG >= EMULONG_SIZE
208 #define EMULONG long long int
210 /* You will have to modify this program to have a smaller unit size. */
211 #define EMU_NON_COMPILE
218 /* The host interface doesn't work if no 16-bit size exists. */
219 #if EMUSHORT_SIZE != 16
220 #define EMU_NON_COMPILE
223 /* OK to continue compilation. */
224 #ifndef EMU_NON_COMPILE
226 /* Construct macros to translate between REAL_VALUE_TYPE and e type.
227 In GET_REAL and PUT_REAL, r and e are pointers.
228 A REAL_VALUE_TYPE is guaranteed to occupy contiguous locations
229 in memory, with no holes. */
231 #if LONG_DOUBLE_TYPE_SIZE == 96
232 /* Number of 16 bit words in external e type format */
234 #define MAXDECEXP 4932
235 #define MINDECEXP -4956
236 #define GET_REAL(r,e) bcopy ((char *) r, (char *) e, 2*NE)
237 #define PUT_REAL(e,r) bcopy ((char *) e, (char *) r, 2*NE)
238 #else /* no XFmode */
239 #if LONG_DOUBLE_TYPE_SIZE == 128
241 #define MAXDECEXP 4932
242 #define MINDECEXP -4977
243 #define GET_REAL(r,e) bcopy ((char *) r, (char *) e, 2*NE)
244 #define PUT_REAL(e,r) bcopy ((char *) e, (char *) r, 2*NE)
247 #define MAXDECEXP 4932
248 #define MINDECEXP -4956
249 #ifdef REAL_ARITHMETIC
250 /* Emulator uses target format internally
251 but host stores it in host endian-ness. */
253 #define GET_REAL(r,e) \
255 if (HOST_FLOAT_WORDS_BIG_ENDIAN == REAL_WORDS_BIG_ENDIAN) \
256 e53toe ((unsigned EMUSHORT *) (r), (e)); \
259 unsigned EMUSHORT w[4]; \
260 w[3] = ((EMUSHORT *) r)[0]; \
261 w[2] = ((EMUSHORT *) r)[1]; \
262 w[1] = ((EMUSHORT *) r)[2]; \
263 w[0] = ((EMUSHORT *) r)[3]; \
268 #define PUT_REAL(e,r) \
270 if (HOST_FLOAT_WORDS_BIG_ENDIAN == REAL_WORDS_BIG_ENDIAN) \
271 etoe53 ((e), (unsigned EMUSHORT *) (r)); \
274 unsigned EMUSHORT w[4]; \
276 *((EMUSHORT *) r) = w[3]; \
277 *((EMUSHORT *) r + 1) = w[2]; \
278 *((EMUSHORT *) r + 2) = w[1]; \
279 *((EMUSHORT *) r + 3) = w[0]; \
283 #else /* not REAL_ARITHMETIC */
285 /* emulator uses host format */
286 #define GET_REAL(r,e) e53toe ((unsigned EMUSHORT *) (r), (e))
287 #define PUT_REAL(e,r) etoe53 ((e), (unsigned EMUSHORT *) (r))
289 #endif /* not REAL_ARITHMETIC */
290 #endif /* not TFmode */
291 #endif /* no XFmode */
294 /* Number of 16 bit words in internal format */
297 /* Array offset to exponent */
300 /* Array offset to high guard word */
303 /* Number of bits of precision */
304 #define NBITS ((NI-4)*16)
306 /* Maximum number of decimal digits in ASCII conversion
309 #define NDEC (NBITS*8/27)
311 /* The exponent of 1.0 */
312 #define EXONE (0x3fff)
314 extern int extra_warnings;
315 extern unsigned EMUSHORT ezero[], ehalf[], eone[], etwo[];
316 extern unsigned EMUSHORT elog2[], esqrt2[];
318 static void endian PROTO((unsigned EMUSHORT *, long *,
320 static void eclear PROTO((unsigned EMUSHORT *));
321 static void emov PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
322 static void eabs PROTO((unsigned EMUSHORT *));
323 static void eneg PROTO((unsigned EMUSHORT *));
324 static int eisneg PROTO((unsigned EMUSHORT *));
325 static int eisinf PROTO((unsigned EMUSHORT *));
326 static int eisnan PROTO((unsigned EMUSHORT *));
327 static void einfin PROTO((unsigned EMUSHORT *));
328 static void enan PROTO((unsigned EMUSHORT *, int));
329 static void emovi PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
330 static void emovo PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
331 static void ecleaz PROTO((unsigned EMUSHORT *));
332 static void ecleazs PROTO((unsigned EMUSHORT *));
333 static void emovz PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
334 static void einan PROTO((unsigned EMUSHORT *));
335 static int eiisnan PROTO((unsigned EMUSHORT *));
336 static int eiisneg PROTO((unsigned EMUSHORT *));
337 static void eiinfin PROTO((unsigned EMUSHORT *));
338 static int eiisinf PROTO((unsigned EMUSHORT *));
339 static int ecmpm PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
340 static void eshdn1 PROTO((unsigned EMUSHORT *));
341 static void eshup1 PROTO((unsigned EMUSHORT *));
342 static void eshdn8 PROTO((unsigned EMUSHORT *));
343 static void eshup8 PROTO((unsigned EMUSHORT *));
344 static void eshup6 PROTO((unsigned EMUSHORT *));
345 static void eshdn6 PROTO((unsigned EMUSHORT *));
346 static void eaddm PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
\f
347 static void esubm PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
348 static void m16m PROTO((unsigned int, unsigned short *,
350 static int edivm PROTO((unsigned short *, unsigned short *));
351 static int emulm PROTO((unsigned short *, unsigned short *));
352 static void emdnorm PROTO((unsigned EMUSHORT *, int, int, EMULONG, int));
353 static void esub PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
354 unsigned EMUSHORT *));
355 static void eadd PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
356 unsigned EMUSHORT *));
357 static void eadd1 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
358 unsigned EMUSHORT *));
359 static void ediv PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
360 unsigned EMUSHORT *));
361 static void emul PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
362 unsigned EMUSHORT *));
363 static void e53toe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
364 static void e64toe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
365 static void e113toe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
366 static void e24toe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
367 static void etoe113 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
368 static void toe113 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
369 static void etoe64 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
370 static void toe64 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
371 static void etoe53 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
372 static void toe53 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
373 static void etoe24 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
374 static void toe24 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
375 static int ecmp PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
376 static void eround PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
377 static void ltoe PROTO((HOST_WIDE_INT *, unsigned EMUSHORT *));
378 static void ultoe PROTO((unsigned HOST_WIDE_INT *, unsigned EMUSHORT *));
379 static void eifrac PROTO((unsigned EMUSHORT *, HOST_WIDE_INT *,
380 unsigned EMUSHORT *));
381 static void euifrac PROTO((unsigned EMUSHORT *, unsigned HOST_WIDE_INT *,
382 unsigned EMUSHORT *));
383 static int eshift PROTO((unsigned EMUSHORT *, int));
384 static int enormlz PROTO((unsigned EMUSHORT *));
385 static void e24toasc PROTO((unsigned EMUSHORT *, char *, int));
386 static void e53toasc PROTO((unsigned EMUSHORT *, char *, int));
387 static void e64toasc PROTO((unsigned EMUSHORT *, char *, int));
388 static void e113toasc PROTO((unsigned EMUSHORT *, char *, int));
389 static void etoasc PROTO((unsigned EMUSHORT *, char *, int));
390 static void asctoe24 PROTO((char *, unsigned EMUSHORT *));
391 static void asctoe53 PROTO((char *, unsigned EMUSHORT *));
392 static void asctoe64 PROTO((char *, unsigned EMUSHORT *));
393 static void asctoe113 PROTO((char *, unsigned EMUSHORT *));
394 static void asctoe PROTO((char *, unsigned EMUSHORT *));
395 static void asctoeg PROTO((char *, unsigned EMUSHORT *, int));
396 static void efloor PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
398 static void efrexp PROTO((unsigned EMUSHORT *, int *,
399 unsigned EMUSHORT *));
401 static void eldexp PROTO((unsigned EMUSHORT *, int, unsigned EMUSHORT *));
403 static void eremain PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
404 unsigned EMUSHORT *));
406 static void eiremain PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
407 static void mtherr PROTO((char *, int));
409 static void dectoe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
410 static void etodec PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
411 static void todec PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
414 static void ibmtoe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
416 static void etoibm PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
418 static void toibm PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
421 static void make_nan PROTO((unsigned EMUSHORT *, int, enum machine_mode));
423 static void uditoe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
424 static void ditoe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
425 static void etoudi PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
426 static void etodi PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
427 static void esqrt PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
430 /* Copy 32-bit numbers obtained from array containing 16-bit numbers,
431 swapping ends if required, into output array of longs. The
432 result is normally passed to fprintf by the ASM_OUTPUT_ macros. */
436 unsigned EMUSHORT e[];
438 enum machine_mode mode;
442 if (REAL_WORDS_BIG_ENDIAN)
448 /* Swap halfwords in the fourth long. */
449 th = (unsigned long) e[6] & 0xffff;
450 t = (unsigned long) e[7] & 0xffff;
456 /* Swap halfwords in the third long. */
457 th = (unsigned long) e[4] & 0xffff;
458 t = (unsigned long) e[5] & 0xffff;
461 /* fall into the double case */
465 /* swap halfwords in the second word */
466 th = (unsigned long) e[2] & 0xffff;
467 t = (unsigned long) e[3] & 0xffff;
470 /* fall into the float case */
475 /* swap halfwords in the first word */
476 th = (unsigned long) e[0] & 0xffff;
477 t = (unsigned long) e[1] & 0xffff;
488 /* Pack the output array without swapping. */
495 /* Pack the fourth long. */
496 th = (unsigned long) e[7] & 0xffff;
497 t = (unsigned long) e[6] & 0xffff;
503 /* Pack the third long.
504 Each element of the input REAL_VALUE_TYPE array has 16 useful bits
506 th = (unsigned long) e[5] & 0xffff;
507 t = (unsigned long) e[4] & 0xffff;
510 /* fall into the double case */
514 /* pack the second long */
515 th = (unsigned long) e[3] & 0xffff;
516 t = (unsigned long) e[2] & 0xffff;
519 /* fall into the float case */
524 /* pack the first long */
525 th = (unsigned long) e[1] & 0xffff;
526 t = (unsigned long) e[0] & 0xffff;
538 /* This is the implementation of the REAL_ARITHMETIC macro. */
541 earith (value, icode, r1, r2)
542 REAL_VALUE_TYPE *value;
547 unsigned EMUSHORT d1[NE], d2[NE], v[NE];
553 /* Return NaN input back to the caller. */
556 PUT_REAL (d1, value);
561 PUT_REAL (d2, value);
565 code = (enum tree_code) icode;
573 esub (d2, d1, v); /* d1 - d2 */
581 #ifndef REAL_INFINITY
582 if (ecmp (d2, ezero) == 0)
585 enan (v, eisneg (d1) ^ eisneg (d2));
592 ediv (d2, d1, v); /* d1/d2 */
595 case MIN_EXPR: /* min (d1,d2) */
596 if (ecmp (d1, d2) < 0)
602 case MAX_EXPR: /* max (d1,d2) */
603 if (ecmp (d1, d2) > 0)
616 /* Truncate REAL_VALUE_TYPE toward zero to signed HOST_WIDE_INT.
617 implements REAL_VALUE_RNDZINT (x) (etrunci (x)). */
623 unsigned EMUSHORT f[NE], g[NE];
639 /* Truncate REAL_VALUE_TYPE toward zero to unsigned HOST_WIDE_INT;
640 implements REAL_VALUE_UNSIGNED_RNDZINT (x) (etruncui (x)). */
646 unsigned EMUSHORT f[NE], g[NE];
648 unsigned HOST_WIDE_INT l;
662 /* This is the REAL_VALUE_ATOF function. It converts a decimal string to
663 binary, rounding off as indicated by the machine_mode argument. Then it
664 promotes the rounded value to REAL_VALUE_TYPE. */
671 unsigned EMUSHORT tem[NE], e[NE];
701 /* Expansion of REAL_NEGATE. */
707 unsigned EMUSHORT e[NE];
717 /* Round real toward zero to HOST_WIDE_INT;
718 implements REAL_VALUE_FIX (x). */
724 unsigned EMUSHORT f[NE], g[NE];
731 warning ("conversion from NaN to int");
739 /* Round real toward zero to unsigned HOST_WIDE_INT
740 implements REAL_VALUE_UNSIGNED_FIX (x).
741 Negative input returns zero. */
743 unsigned HOST_WIDE_INT
747 unsigned EMUSHORT f[NE], g[NE];
748 unsigned HOST_WIDE_INT l;
754 warning ("conversion from NaN to unsigned int");
763 /* REAL_VALUE_FROM_INT macro. */
766 ereal_from_int (d, i, j, mode)
769 enum machine_mode mode;
771 unsigned EMUSHORT df[NE], dg[NE];
772 HOST_WIDE_INT low, high;
775 if (GET_MODE_CLASS (mode) != MODE_FLOAT)
782 /* complement and add 1 */
789 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
790 ultoe ((unsigned HOST_WIDE_INT *) &high, dg);
792 ultoe ((unsigned HOST_WIDE_INT *) &low, df);
797 /* A REAL_VALUE_TYPE may not be wide enough to hold the two HOST_WIDE_INTS.
798 Avoid double-rounding errors later by rounding off now from the
799 extra-wide internal format to the requested precision. */
800 switch (GET_MODE_BITSIZE (mode))
830 /* REAL_VALUE_FROM_UNSIGNED_INT macro. */
833 ereal_from_uint (d, i, j, mode)
835 unsigned HOST_WIDE_INT i, j;
836 enum machine_mode mode;
838 unsigned EMUSHORT df[NE], dg[NE];
839 unsigned HOST_WIDE_INT low, high;
841 if (GET_MODE_CLASS (mode) != MODE_FLOAT)
845 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
851 /* A REAL_VALUE_TYPE may not be wide enough to hold the two HOST_WIDE_INTS.
852 Avoid double-rounding errors later by rounding off now from the
853 extra-wide internal format to the requested precision. */
854 switch (GET_MODE_BITSIZE (mode))
884 /* REAL_VALUE_TO_INT macro. */
887 ereal_to_int (low, high, rr)
888 HOST_WIDE_INT *low, *high;
891 unsigned EMUSHORT d[NE], df[NE], dg[NE], dh[NE];
898 warning ("conversion from NaN to int");
904 /* convert positive value */
911 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
912 ediv (df, d, dg); /* dg = d / 2^32 is the high word */
913 euifrac (dg, (unsigned HOST_WIDE_INT *) high, dh);
914 emul (df, dh, dg); /* fractional part is the low word */
915 euifrac (dg, (unsigned HOST_WIDE_INT *)low, dh);
918 /* complement and add 1 */
928 /* REAL_VALUE_LDEXP macro. */
935 unsigned EMUSHORT e[NE], y[NE];
948 /* These routines are conditionally compiled because functions
949 of the same names may be defined in fold-const.c. */
951 #ifdef REAL_ARITHMETIC
953 /* Check for infinity in a REAL_VALUE_TYPE. */
959 unsigned EMUSHORT e[NE];
969 /* Check whether a REAL_VALUE_TYPE item is a NaN. */
975 unsigned EMUSHORT e[NE];
986 /* Check for a negative REAL_VALUE_TYPE number.
987 This just checks the sign bit, so that -0 counts as negative. */
993 return ereal_isneg (x);
996 /* Expansion of REAL_VALUE_TRUNCATE.
997 The result is in floating point, rounded to nearest or even. */
1000 real_value_truncate (mode, arg)
1001 enum machine_mode mode;
1002 REAL_VALUE_TYPE arg;
1004 unsigned EMUSHORT e[NE], t[NE];
1040 /* If an unsupported type was requested, presume that
1041 the machine files know something useful to do with
1042 the unmodified value. */
1051 /* Try to change R into its exact multiplicative inverse in machine mode
1052 MODE. Return nonzero function value if successful. */
1055 exact_real_inverse (mode, r)
1056 enum machine_mode mode;
1059 unsigned EMUSHORT e[NE], einv[NE];
1060 REAL_VALUE_TYPE rinv;
1065 /* Test for input in range. Don't transform IEEE special values. */
1066 if (eisinf (e) || eisnan (e) || (ecmp (e, ezero) == 0))
1069 /* Test for a power of 2: all significand bits zero except the MSB.
1070 We are assuming the target has binary (or hex) arithmetic. */
1071 if (e[NE - 2] != 0x8000)
1074 for (i = 0; i < NE - 2; i++)
1080 /* Compute the inverse and truncate it to the required mode. */
1081 ediv (e, eone, einv);
1082 PUT_REAL (einv, &rinv);
1083 rinv = real_value_truncate (mode, rinv);
1085 #ifdef CHECK_FLOAT_VALUE
1086 /* This check is not redundant. It may, for example, flush
1087 a supposedly IEEE denormal value to zero. */
1089 if (CHECK_FLOAT_VALUE (mode, rinv, i))
1092 GET_REAL (&rinv, einv);
1094 /* Check the bits again, because the truncation might have
1095 generated an arbitrary saturation value on overflow. */
1096 if (einv[NE - 2] != 0x8000)
1099 for (i = 0; i < NE - 2; i++)
1105 /* Fail if the computed inverse is out of range. */
1106 if (eisinf (einv) || eisnan (einv) || (ecmp (einv, ezero) == 0))
1109 /* Output the reciprocal and return success flag. */
1113 #endif /* REAL_ARITHMETIC defined */
1115 /* Used for debugging--print the value of R in human-readable format
1124 REAL_VALUE_TO_DECIMAL (r, "%.20g", dstr);
1125 fprintf (stderr, "%s", dstr);
1129 /* The following routines convert REAL_VALUE_TYPE to the various floating
1130 point formats that are meaningful to supported computers.
1132 The results are returned in 32-bit pieces, each piece stored in a `long'.
1133 This is so they can be printed by statements like
1135 fprintf (file, "%lx, %lx", L[0], L[1]);
1137 that will work on both narrow- and wide-word host computers. */
1139 /* Convert R to a 128-bit long double precision value. The output array L
1140 contains four 32-bit pieces of the result, in the order they would appear
1148 unsigned EMUSHORT e[NE];
1152 endian (e, l, TFmode);
1155 /* Convert R to a double extended precision value. The output array L
1156 contains three 32-bit pieces of the result, in the order they would
1157 appear in memory. */
1164 unsigned EMUSHORT e[NE];
1168 endian (e, l, XFmode);
1171 /* Convert R to a double precision value. The output array L contains two
1172 32-bit pieces of the result, in the order they would appear in memory. */
1179 unsigned EMUSHORT e[NE];
1183 endian (e, l, DFmode);
1186 /* Convert R to a single precision float value stored in the least-significant
1187 bits of a `long'. */
1193 unsigned EMUSHORT e[NE];
1198 endian (e, &l, SFmode);
1202 /* Convert X to a decimal ASCII string S for output to an assembly
1203 language file. Note, there is no standard way to spell infinity or
1204 a NaN, so these values may require special treatment in the tm.h
1208 ereal_to_decimal (x, s)
1212 unsigned EMUSHORT e[NE];
1218 /* Compare X and Y. Return 1 if X > Y, 0 if X == Y, -1 if X < Y,
1219 or -2 if either is a NaN. */
1223 REAL_VALUE_TYPE x, y;
1225 unsigned EMUSHORT ex[NE], ey[NE];
1229 return (ecmp (ex, ey));
1232 /* Return 1 if the sign bit of X is set, else return 0. */
1238 unsigned EMUSHORT ex[NE];
1241 return (eisneg (ex));
1244 /* End of REAL_ARITHMETIC interface */
1247 Extended precision IEEE binary floating point arithmetic routines
1249 Numbers are stored in C language as arrays of 16-bit unsigned
1250 short integers. The arguments of the routines are pointers to
1253 External e type data structure, similar to Intel 8087 chip
1254 temporary real format but possibly with a larger significand:
1256 NE-1 significand words (least significant word first,
1257 most significant bit is normally set)
1258 exponent (value = EXONE for 1.0,
1259 top bit is the sign)
1262 Internal exploded e-type data structure of a number (a "word" is 16 bits):
1264 ei[0] sign word (0 for positive, 0xffff for negative)
1265 ei[1] biased exponent (value = EXONE for the number 1.0)
1266 ei[2] high guard word (always zero after normalization)
1268 to ei[NI-2] significand (NI-4 significand words,
1269 most significant word first,
1270 most significant bit is set)
1271 ei[NI-1] low guard word (0x8000 bit is rounding place)
1275 Routines for external format e-type numbers
1277 asctoe (string, e) ASCII string to extended double e type
1278 asctoe64 (string, &d) ASCII string to long double
1279 asctoe53 (string, &d) ASCII string to double
1280 asctoe24 (string, &f) ASCII string to single
1281 asctoeg (string, e, prec) ASCII string to specified precision
1282 e24toe (&f, e) IEEE single precision to e type
1283 e53toe (&d, e) IEEE double precision to e type
1284 e64toe (&d, e) IEEE long double precision to e type
1285 e113toe (&d, e) 128-bit long double precision to e type
1286 eabs (e) absolute value
1287 eadd (a, b, c) c = b + a
1289 ecmp (a, b) Returns 1 if a > b, 0 if a == b,
1290 -1 if a < b, -2 if either a or b is a NaN.
1291 ediv (a, b, c) c = b / a
1292 efloor (a, b) truncate to integer, toward -infinity
1293 efrexp (a, exp, s) extract exponent and significand
1294 eifrac (e, &l, frac) e to HOST_WIDE_INT and e type fraction
1295 euifrac (e, &l, frac) e to unsigned HOST_WIDE_INT and e type fraction
1296 einfin (e) set e to infinity, leaving its sign alone
1297 eldexp (a, n, b) multiply by 2**n
1299 emul (a, b, c) c = b * a
1301 eround (a, b) b = nearest integer value to a
1302 esub (a, b, c) c = b - a
1303 e24toasc (&f, str, n) single to ASCII string, n digits after decimal
1304 e53toasc (&d, str, n) double to ASCII string, n digits after decimal
1305 e64toasc (&d, str, n) 80-bit long double to ASCII string
1306 e113toasc (&d, str, n) 128-bit long double to ASCII string
1307 etoasc (e, str, n) e to ASCII string, n digits after decimal
1308 etoe24 (e, &f) convert e type to IEEE single precision
1309 etoe53 (e, &d) convert e type to IEEE double precision
1310 etoe64 (e, &d) convert e type to IEEE long double precision
1311 ltoe (&l, e) HOST_WIDE_INT to e type
1312 ultoe (&l, e) unsigned HOST_WIDE_INT to e type
1313 eisneg (e) 1 if sign bit of e != 0, else 0
1314 eisinf (e) 1 if e has maximum exponent (non-IEEE)
1315 or is infinite (IEEE)
1316 eisnan (e) 1 if e is a NaN
1319 Routines for internal format exploded e-type numbers
1321 eaddm (ai, bi) add significands, bi = bi + ai
1323 ecleazs (ei) set ei = 0 but leave its sign alone
1324 ecmpm (ai, bi) compare significands, return 1, 0, or -1
1325 edivm (ai, bi) divide significands, bi = bi / ai
1326 emdnorm (ai,l,s,exp) normalize and round off
1327 emovi (a, ai) convert external a to internal ai
1328 emovo (ai, a) convert internal ai to external a
1329 emovz (ai, bi) bi = ai, low guard word of bi = 0
1330 emulm (ai, bi) multiply significands, bi = bi * ai
1331 enormlz (ei) left-justify the significand
1332 eshdn1 (ai) shift significand and guards down 1 bit
1333 eshdn8 (ai) shift down 8 bits
1334 eshdn6 (ai) shift down 16 bits
1335 eshift (ai, n) shift ai n bits up (or down if n < 0)
1336 eshup1 (ai) shift significand and guards up 1 bit
1337 eshup8 (ai) shift up 8 bits
1338 eshup6 (ai) shift up 16 bits
1339 esubm (ai, bi) subtract significands, bi = bi - ai
1340 eiisinf (ai) 1 if infinite
1341 eiisnan (ai) 1 if a NaN
1342 eiisneg (ai) 1 if sign bit of ai != 0, else 0
1343 einan (ai) set ai = NaN
1344 eiinfin (ai) set ai = infinity
1346 The result is always normalized and rounded to NI-4 word precision
1347 after each arithmetic operation.
1349 Exception flags are NOT fully supported.
1351 Signaling NaN's are NOT supported; they are treated the same
1354 Define INFINITY for support of infinity; otherwise a
1355 saturation arithmetic is implemented.
1357 Define NANS for support of Not-a-Number items; otherwise the
1358 arithmetic will never produce a NaN output, and might be confused
1360 If NaN's are supported, the output of `ecmp (a,b)' is -2 if
1361 either a or b is a NaN. This means asking `if (ecmp (a,b) < 0)'
1362 may not be legitimate. Use `if (ecmp (a,b) == -1)' for `less than'
1365 Denormals are always supported here where appropriate (e.g., not
1366 for conversion to DEC numbers). */
1368 /* Definitions for error codes that are passed to the common error handling
1371 For Digital Equipment PDP-11 and VAX computers, certain
1372 IBM systems, and others that use numbers with a 56-bit
1373 significand, the symbol DEC should be defined. In this
1374 mode, most floating point constants are given as arrays
1375 of octal integers to eliminate decimal to binary conversion
1376 errors that might be introduced by the compiler.
1378 For computers, such as IBM PC, that follow the IEEE
1379 Standard for Binary Floating Point Arithmetic (ANSI/IEEE
1380 Std 754-1985), the symbol IEEE should be defined.
1381 These numbers have 53-bit significands. In this mode, constants
1382 are provided as arrays of hexadecimal 16 bit integers.
1383 The endian-ness of generated values is controlled by
1384 REAL_WORDS_BIG_ENDIAN.
1386 To accommodate other types of computer arithmetic, all
1387 constants are also provided in a normal decimal radix
1388 which one can hope are correctly converted to a suitable
1389 format by the available C language compiler. To invoke
1390 this mode, the symbol UNK is defined.
1392 An important difference among these modes is a predefined
1393 set of machine arithmetic constants for each. The numbers
1394 MACHEP (the machine roundoff error), MAXNUM (largest number
1395 represented), and several other parameters are preset by
1396 the configuration symbol. Check the file const.c to
1397 ensure that these values are correct for your computer.
1399 For ANSI C compatibility, define ANSIC equal to 1. Currently
1400 this affects only the atan2 function and others that use it. */
1402 /* Constant definitions for math error conditions. */
1404 #define DOMAIN 1 /* argument domain error */
1405 #define SING 2 /* argument singularity */
1406 #define OVERFLOW 3 /* overflow range error */
1407 #define UNDERFLOW 4 /* underflow range error */
1408 #define TLOSS 5 /* total loss of precision */
1409 #define PLOSS 6 /* partial loss of precision */
1410 #define INVALID 7 /* NaN-producing operation */
1412 /* e type constants used by high precision check routines */
1414 #if LONG_DOUBLE_TYPE_SIZE == 128
1416 unsigned EMUSHORT ezero[NE] =
1417 {0x0000, 0x0000, 0x0000, 0x0000,
1418 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,};
1419 extern unsigned EMUSHORT ezero[];
1422 unsigned EMUSHORT ehalf[NE] =
1423 {0x0000, 0x0000, 0x0000, 0x0000,
1424 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3ffe,};
1425 extern unsigned EMUSHORT ehalf[];
1428 unsigned EMUSHORT eone[NE] =
1429 {0x0000, 0x0000, 0x0000, 0x0000,
1430 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3fff,};
1431 extern unsigned EMUSHORT eone[];
1434 unsigned EMUSHORT etwo[NE] =
1435 {0x0000, 0x0000, 0x0000, 0x0000,
1436 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4000,};
1437 extern unsigned EMUSHORT etwo[];
1440 unsigned EMUSHORT e32[NE] =
1441 {0x0000, 0x0000, 0x0000, 0x0000,
1442 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4004,};
1443 extern unsigned EMUSHORT e32[];
1445 /* 6.93147180559945309417232121458176568075500134360255E-1 */
1446 unsigned EMUSHORT elog2[NE] =
1447 {0x40f3, 0xf6af, 0x03f2, 0xb398,
1448 0xc9e3, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1449 extern unsigned EMUSHORT elog2[];
1451 /* 1.41421356237309504880168872420969807856967187537695E0 */
1452 unsigned EMUSHORT esqrt2[NE] =
1453 {0x1d6f, 0xbe9f, 0x754a, 0x89b3,
1454 0x597d, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1455 extern unsigned EMUSHORT esqrt2[];
1457 /* 3.14159265358979323846264338327950288419716939937511E0 */
1458 unsigned EMUSHORT epi[NE] =
1459 {0x2902, 0x1cd1, 0x80dc, 0x628b,
1460 0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1461 extern unsigned EMUSHORT epi[];
1464 /* LONG_DOUBLE_TYPE_SIZE is other than 128 */
1465 unsigned EMUSHORT ezero[NE] =
1466 {0, 0000000, 0000000, 0000000, 0000000, 0000000,};
1467 unsigned EMUSHORT ehalf[NE] =
1468 {0, 0000000, 0000000, 0000000, 0100000, 0x3ffe,};
1469 unsigned EMUSHORT eone[NE] =
1470 {0, 0000000, 0000000, 0000000, 0100000, 0x3fff,};
1471 unsigned EMUSHORT etwo[NE] =
1472 {0, 0000000, 0000000, 0000000, 0100000, 0040000,};
1473 unsigned EMUSHORT e32[NE] =
1474 {0, 0000000, 0000000, 0000000, 0100000, 0040004,};
1475 unsigned EMUSHORT elog2[NE] =
1476 {0xc9e4, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1477 unsigned EMUSHORT esqrt2[NE] =
1478 {0x597e, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1479 unsigned EMUSHORT epi[NE] =
1480 {0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1483 /* Control register for rounding precision.
1484 This can be set to 113 (if NE=10), 80 (if NE=6), 64, 56, 53, or 24 bits. */
1489 /* Clear out entire e-type number X. */
1493 register unsigned EMUSHORT *x;
1497 for (i = 0; i < NE; i++)
1501 /* Move e-type number from A to B. */
1505 register unsigned EMUSHORT *a, *b;
1509 for (i = 0; i < NE; i++)
1514 /* Absolute value of e-type X. */
1518 unsigned EMUSHORT x[];
1520 /* sign is top bit of last word of external format */
1521 x[NE - 1] &= 0x7fff;
1524 /* Negate the e-type number X. */
1528 unsigned EMUSHORT x[];
1531 x[NE - 1] ^= 0x8000; /* Toggle the sign bit */
1534 /* Return 1 if sign bit of e-type number X is nonzero, else zero. */
1538 unsigned EMUSHORT x[];
1541 if (x[NE - 1] & 0x8000)
1547 /* Return 1 if e-type number X is infinity, else return zero. */
1551 unsigned EMUSHORT x[];
1558 if ((x[NE - 1] & 0x7fff) == 0x7fff)
1564 /* Check if e-type number is not a number. The bit pattern is one that we
1565 defined, so we know for sure how to detect it. */
1569 unsigned EMUSHORT x[];
1574 /* NaN has maximum exponent */
1575 if ((x[NE - 1] & 0x7fff) != 0x7fff)
1577 /* ... and non-zero significand field. */
1578 for (i = 0; i < NE - 1; i++)
1588 /* Fill e-type number X with infinity pattern (IEEE)
1589 or largest possible number (non-IEEE). */
1593 register unsigned EMUSHORT *x;
1598 for (i = 0; i < NE - 1; i++)
1602 for (i = 0; i < NE - 1; i++)
1630 /* Output an e-type NaN.
1631 This generates Intel's quiet NaN pattern for extended real.
1632 The exponent is 7fff, the leading mantissa word is c000. */
1636 register unsigned EMUSHORT *x;
1641 for (i = 0; i < NE - 2; i++)
1644 *x = (sign << 15) | 0x7fff;
1647 /* Move in an e-type number A, converting it to exploded e-type B. */
1651 unsigned EMUSHORT *a, *b;
1653 register unsigned EMUSHORT *p, *q;
1657 p = a + (NE - 1); /* point to last word of external number */
1658 /* get the sign bit */
1663 /* get the exponent */
1665 *q++ &= 0x7fff; /* delete the sign bit */
1667 if ((*(q - 1) & 0x7fff) == 0x7fff)
1673 for (i = 3; i < NI; i++)
1679 for (i = 2; i < NI; i++)
1685 /* clear high guard word */
1687 /* move in the significand */
1688 for (i = 0; i < NE - 1; i++)
1690 /* clear low guard word */
1694 /* Move out exploded e-type number A, converting it to e type B. */
1698 unsigned EMUSHORT *a, *b;
1700 register unsigned EMUSHORT *p, *q;
1701 unsigned EMUSHORT i;
1705 q = b + (NE - 1); /* point to output exponent */
1706 /* combine sign and exponent */
1709 *q-- = *p++ | 0x8000;
1713 if (*(p - 1) == 0x7fff)
1718 enan (b, eiisneg (a));
1726 /* skip over guard word */
1728 /* move the significand */
1729 for (j = 0; j < NE - 1; j++)
1733 /* Clear out exploded e-type number XI. */
1737 register unsigned EMUSHORT *xi;
1741 for (i = 0; i < NI; i++)
1745 /* Clear out exploded e-type XI, but don't touch the sign. */
1749 register unsigned EMUSHORT *xi;
1754 for (i = 0; i < NI - 1; i++)
1758 /* Move exploded e-type number from A to B. */
1762 register unsigned EMUSHORT *a, *b;
1766 for (i = 0; i < NI - 1; i++)
1768 /* clear low guard word */
1772 /* Generate exploded e-type NaN.
1773 The explicit pattern for this is maximum exponent and
1774 top two significant bits set. */
1778 unsigned EMUSHORT x[];
1786 /* Return nonzero if exploded e-type X is a NaN. */
1790 unsigned EMUSHORT x[];
1794 if ((x[E] & 0x7fff) == 0x7fff)
1796 for (i = M + 1; i < NI; i++)
1805 /* Return nonzero if sign of exploded e-type X is nonzero. */
1809 unsigned EMUSHORT x[];
1815 /* Fill exploded e-type X with infinity pattern.
1816 This has maximum exponent and significand all zeros. */
1820 unsigned EMUSHORT x[];
1827 /* Return nonzero if exploded e-type X is infinite. */
1831 unsigned EMUSHORT x[];
1838 if ((x[E] & 0x7fff) == 0x7fff)
1844 /* Compare significands of numbers in internal exploded e-type format.
1845 Guard words are included in the comparison.
1853 register unsigned EMUSHORT *a, *b;
1857 a += M; /* skip up to significand area */
1859 for (i = M; i < NI; i++)
1867 if (*(--a) > *(--b))
1873 /* Shift significand of exploded e-type X down by 1 bit. */
1877 register unsigned EMUSHORT *x;
1879 register unsigned EMUSHORT bits;
1882 x += M; /* point to significand area */
1885 for (i = M; i < NI; i++)
1897 /* Shift significand of exploded e-type X up by 1 bit. */
1901 register unsigned EMUSHORT *x;
1903 register unsigned EMUSHORT bits;
1909 for (i = M; i < NI; i++)
1922 /* Shift significand of exploded e-type X down by 8 bits. */
1926 register unsigned EMUSHORT *x;
1928 register unsigned EMUSHORT newbyt, oldbyt;
1933 for (i = M; i < NI; i++)
1943 /* Shift significand of exploded e-type X up by 8 bits. */
1947 register unsigned EMUSHORT *x;
1950 register unsigned EMUSHORT newbyt, oldbyt;
1955 for (i = M; i < NI; i++)
1965 /* Shift significand of exploded e-type X up by 16 bits. */
1969 register unsigned EMUSHORT *x;
1972 register unsigned EMUSHORT *p;
1977 for (i = M; i < NI - 1; i++)
1983 /* Shift significand of exploded e-type X down by 16 bits. */
1987 register unsigned EMUSHORT *x;
1990 register unsigned EMUSHORT *p;
1995 for (i = M; i < NI - 1; i++)
2001 /* Add significands of exploded e-type X and Y. X + Y replaces Y. */
2005 unsigned EMUSHORT *x, *y;
2007 register unsigned EMULONG a;
2014 for (i = M; i < NI; i++)
2016 a = (unsigned EMULONG) (*x) + (unsigned EMULONG) (*y) + carry;
2021 *y = (unsigned EMUSHORT) a;
2027 /* Subtract significands of exploded e-type X and Y. Y - X replaces Y. */
2031 unsigned EMUSHORT *x, *y;
2040 for (i = M; i < NI; i++)
2042 a = (unsigned EMULONG) (*y) - (unsigned EMULONG) (*x) - carry;
2047 *y = (unsigned EMUSHORT) a;
2054 static unsigned EMUSHORT equot[NI];
2058 /* Radix 2 shift-and-add versions of multiply and divide */
2061 /* Divide significands */
2065 unsigned EMUSHORT den[], num[];
2068 register unsigned EMUSHORT *p, *q;
2069 unsigned EMUSHORT j;
2075 for (i = M; i < NI; i++)
2080 /* Use faster compare and subtraction if denominator has only 15 bits of
2086 for (i = M + 3; i < NI; i++)
2091 if ((den[M + 1] & 1) != 0)
2099 for (i = 0; i < NBITS + 2; i++)
2117 /* The number of quotient bits to calculate is NBITS + 1 scaling guard
2118 bit + 1 roundoff bit. */
2123 for (i = 0; i < NBITS + 2; i++)
2125 if (ecmpm (den, num) <= 0)
2128 j = 1; /* quotient bit = 1 */
2142 /* test for nonzero remainder after roundoff bit */
2145 for (i = M; i < NI; i++)
2153 for (i = 0; i < NI; i++)
2159 /* Multiply significands */
2163 unsigned EMUSHORT a[], b[];
2165 unsigned EMUSHORT *p, *q;
2170 for (i = M; i < NI; i++)
2175 while (*p == 0) /* significand is not supposed to be zero */
2180 if ((*p & 0xff) == 0)
2188 for (i = 0; i < k; i++)
2192 /* remember if there were any nonzero bits shifted out */
2199 for (i = 0; i < NI; i++)
2202 /* return flag for lost nonzero bits */
2208 /* Radix 65536 versions of multiply and divide. */
2210 /* Multiply significand of e-type number B
2211 by 16-bit quantity A, return e-type result to C. */
2216 unsigned EMUSHORT b[], c[];
2218 register unsigned EMUSHORT *pp;
2219 register unsigned EMULONG carry;
2220 unsigned EMUSHORT *ps;
2221 unsigned EMUSHORT p[NI];
2222 unsigned EMULONG aa, m;
2231 for (i=M+1; i<NI; i++)
2241 m = (unsigned EMULONG) aa * *ps--;
2242 carry = (m & 0xffff) + *pp;
2243 *pp-- = (unsigned EMUSHORT)carry;
2244 carry = (carry >> 16) + (m >> 16) + *pp;
2245 *pp = (unsigned EMUSHORT)carry;
2246 *(pp-1) = carry >> 16;
2249 for (i=M; i<NI; i++)
2253 /* Divide significands of exploded e-types NUM / DEN. Neither the
2254 numerator NUM nor the denominator DEN is permitted to have its high guard
2259 unsigned EMUSHORT den[], num[];
2262 register unsigned EMUSHORT *p;
2263 unsigned EMULONG tnum;
2264 unsigned EMUSHORT j, tdenm, tquot;
2265 unsigned EMUSHORT tprod[NI+1];
2271 for (i=M; i<NI; i++)
2277 for (i=M; i<NI; i++)
2279 /* Find trial quotient digit (the radix is 65536). */
2280 tnum = (((unsigned EMULONG) num[M]) << 16) + num[M+1];
2282 /* Do not execute the divide instruction if it will overflow. */
2283 if ((tdenm * 0xffffL) < tnum)
2286 tquot = tnum / tdenm;
2287 /* Multiply denominator by trial quotient digit. */
2288 m16m ((unsigned int)tquot, den, tprod);
2289 /* The quotient digit may have been overestimated. */
2290 if (ecmpm (tprod, num) > 0)
2294 if (ecmpm (tprod, num) > 0)
2304 /* test for nonzero remainder after roundoff bit */
2307 for (i=M; i<NI; i++)
2314 for (i=0; i<NI; i++)
2320 /* Multiply significands of exploded e-type A and B, result in B. */
2324 unsigned EMUSHORT a[], b[];
2326 unsigned EMUSHORT *p, *q;
2327 unsigned EMUSHORT pprod[NI];
2328 unsigned EMUSHORT j;
2333 for (i=M; i<NI; i++)
2339 for (i=M+1; i<NI; i++)
2347 m16m ((unsigned int) *p--, b, pprod);
2348 eaddm(pprod, equot);
2354 for (i=0; i<NI; i++)
2357 /* return flag for lost nonzero bits */
2363 /* Normalize and round off.
2365 The internal format number to be rounded is S.
2366 Input LOST is 0 if the value is exact. This is the so-called sticky bit.
2368 Input SUBFLG indicates whether the number was obtained
2369 by a subtraction operation. In that case if LOST is nonzero
2370 then the number is slightly smaller than indicated.
2372 Input EXP is the biased exponent, which may be negative.
2373 the exponent field of S is ignored but is replaced by
2374 EXP as adjusted by normalization and rounding.
2376 Input RCNTRL is the rounding control. If it is nonzero, the
2377 returned value will be rounded to RNDPRC bits.
2379 For future reference: In order for emdnorm to round off denormal
2380 significands at the right point, the input exponent must be
2381 adjusted to be the actual value it would have after conversion to
2382 the final floating point type. This adjustment has been
2383 implemented for all type conversions (etoe53, etc.) and decimal
2384 conversions, but not for the arithmetic functions (eadd, etc.).
2385 Data types having standard 15-bit exponents are not affected by
2386 this, but SFmode and DFmode are affected. For example, ediv with
2387 rndprc = 24 will not round correctly to 24-bit precision if the
2388 result is denormal. */
2390 static int rlast = -1;
2392 static unsigned EMUSHORT rmsk = 0;
2393 static unsigned EMUSHORT rmbit = 0;
2394 static unsigned EMUSHORT rebit = 0;
2396 static unsigned EMUSHORT rbit[NI];
2399 emdnorm (s, lost, subflg, exp, rcntrl)
2400 unsigned EMUSHORT s[];
2407 unsigned EMUSHORT r;
2412 /* a blank significand could mean either zero or infinity. */
2425 if ((j > NBITS) && (exp < 32767))
2433 if (exp > (EMULONG) (-NBITS - 1))
2446 /* Round off, unless told not to by rcntrl. */
2449 /* Set up rounding parameters if the control register changed. */
2450 if (rndprc != rlast)
2457 rw = NI - 1; /* low guard word */
2477 /* For DEC or IBM arithmetic */
2504 /* Shift down 1 temporarily if the data structure has an implied
2505 most significant bit and the number is denormal.
2506 Intel long double denormals also lose one bit of precision. */
2507 if ((exp <= 0) && (rndprc != NBITS)
2508 && ((rndprc != 64) || ((rndprc == 64) && ! REAL_WORDS_BIG_ENDIAN)))
2510 lost |= s[NI - 1] & 1;
2513 /* Clear out all bits below the rounding bit,
2514 remembering in r if any were nonzero. */
2528 if ((r & rmbit) != 0)
2533 { /* round to even */
2534 if ((s[re] & rebit) == 0)
2546 /* Undo the temporary shift for denormal values. */
2547 if ((exp <= 0) && (rndprc != NBITS)
2548 && ((rndprc != 64) || ((rndprc == 64) && ! REAL_WORDS_BIG_ENDIAN)))
2553 { /* overflow on roundoff */
2566 for (i = 2; i < NI - 1; i++)
2569 warning ("floating point overflow");
2573 for (i = M + 1; i < NI - 1; i++)
2576 if ((rndprc < 64) || (rndprc == 113))
2591 s[1] = (unsigned EMUSHORT) exp;
2594 /* Subtract. C = B - A, all e type numbers. */
2596 static int subflg = 0;
2600 unsigned EMUSHORT *a, *b, *c;
2614 /* Infinity minus infinity is a NaN.
2615 Test for subtracting infinities of the same sign. */
2616 if (eisinf (a) && eisinf (b)
2617 && ((eisneg (a) ^ eisneg (b)) == 0))
2619 mtherr ("esub", INVALID);
2628 /* Add. C = A + B, all e type. */
2632 unsigned EMUSHORT *a, *b, *c;
2636 /* NaN plus anything is a NaN. */
2647 /* Infinity minus infinity is a NaN.
2648 Test for adding infinities of opposite signs. */
2649 if (eisinf (a) && eisinf (b)
2650 && ((eisneg (a) ^ eisneg (b)) != 0))
2652 mtherr ("esub", INVALID);
2661 /* Arithmetic common to both addition and subtraction. */
2665 unsigned EMUSHORT *a, *b, *c;
2667 unsigned EMUSHORT ai[NI], bi[NI], ci[NI];
2669 EMULONG lt, lta, ltb;
2690 /* compare exponents */
2695 { /* put the larger number in bi */
2705 if (lt < (EMULONG) (-NBITS - 1))
2706 goto done; /* answer same as larger addend */
2708 lost = eshift (ai, k); /* shift the smaller number down */
2712 /* exponents were the same, so must compare significands */
2715 { /* the numbers are identical in magnitude */
2716 /* if different signs, result is zero */
2722 /* if same sign, result is double */
2723 /* double denormalized tiny number */
2724 if ((bi[E] == 0) && ((bi[3] & 0x8000) == 0))
2729 /* add 1 to exponent unless both are zero! */
2730 for (j = 1; j < NI - 1; j++)
2746 bi[E] = (unsigned EMUSHORT) ltb;
2750 { /* put the larger number in bi */
2766 emdnorm (bi, lost, subflg, ltb, 64);
2772 /* Divide: C = B/A, all e type. */
2776 unsigned EMUSHORT *a, *b, *c;
2778 unsigned EMUSHORT ai[NI], bi[NI];
2780 EMULONG lt, lta, ltb;
2782 /* IEEE says if result is not a NaN, the sign is "-" if and only if
2783 operands have opposite signs -- but flush -0 to 0 later if not IEEE. */
2784 sign = eisneg(a) ^ eisneg(b);
2787 /* Return any NaN input. */
2798 /* Zero over zero, or infinity over infinity, is a NaN. */
2799 if (((ecmp (a, ezero) == 0) && (ecmp (b, ezero) == 0))
2800 || (eisinf (a) && eisinf (b)))
2802 mtherr ("ediv", INVALID);
2807 /* Infinity over anything else is infinity. */
2814 /* Anything else over infinity is zero. */
2826 { /* See if numerator is zero. */
2827 for (i = 1; i < NI - 1; i++)
2831 ltb -= enormlz (bi);
2841 { /* possible divide by zero */
2842 for (i = 1; i < NI - 1; i++)
2846 lta -= enormlz (ai);
2850 /* Divide by zero is not an invalid operation.
2851 It is a divide-by-zero operation! */
2853 mtherr ("ediv", SING);
2859 /* calculate exponent */
2860 lt = ltb - lta + EXONE;
2861 emdnorm (bi, i, 0, lt, 64);
2868 && (ecmp (c, ezero) != 0)
2871 *(c+(NE-1)) |= 0x8000;
2873 *(c+(NE-1)) &= ~0x8000;
2876 /* Multiply e-types A and B, return e-type product C. */
2880 unsigned EMUSHORT *a, *b, *c;
2882 unsigned EMUSHORT ai[NI], bi[NI];
2884 EMULONG lt, lta, ltb;
2886 /* IEEE says if result is not a NaN, the sign is "-" if and only if
2887 operands have opposite signs -- but flush -0 to 0 later if not IEEE. */
2888 sign = eisneg(a) ^ eisneg(b);
2891 /* NaN times anything is the same NaN. */
2902 /* Zero times infinity is a NaN. */
2903 if ((eisinf (a) && (ecmp (b, ezero) == 0))
2904 || (eisinf (b) && (ecmp (a, ezero) == 0)))
2906 mtherr ("emul", INVALID);
2911 /* Infinity times anything else is infinity. */
2913 if (eisinf (a) || eisinf (b))
2925 for (i = 1; i < NI - 1; i++)
2929 lta -= enormlz (ai);
2940 for (i = 1; i < NI - 1; i++)
2944 ltb -= enormlz (bi);
2953 /* Multiply significands */
2955 /* calculate exponent */
2956 lt = lta + ltb - (EXONE - 1);
2957 emdnorm (bi, j, 0, lt, 64);
2964 && (ecmp (c, ezero) != 0)
2967 *(c+(NE-1)) |= 0x8000;
2969 *(c+(NE-1)) &= ~0x8000;
2972 /* Convert double precision PE to e-type Y. */
2976 unsigned EMUSHORT *pe, *y;
2985 ibmtoe (pe, y, DFmode);
2988 register unsigned EMUSHORT r;
2989 register unsigned EMUSHORT *e, *p;
2990 unsigned EMUSHORT yy[NI];
2994 denorm = 0; /* flag if denormalized number */
2996 if (! REAL_WORDS_BIG_ENDIAN)
3002 yy[M] = (r & 0x0f) | 0x10;
3003 r &= ~0x800f; /* strip sign and 4 significand bits */
3008 if (! REAL_WORDS_BIG_ENDIAN)
3010 if (((pe[3] & 0xf) != 0) || (pe[2] != 0)
3011 || (pe[1] != 0) || (pe[0] != 0))
3013 enan (y, yy[0] != 0);
3019 if (((pe[0] & 0xf) != 0) || (pe[1] != 0)
3020 || (pe[2] != 0) || (pe[3] != 0))
3022 enan (y, yy[0] != 0);
3033 #endif /* INFINITY */
3035 /* If zero exponent, then the significand is denormalized.
3036 So take back the understood high significand bit. */
3047 if (! REAL_WORDS_BIG_ENDIAN)
3063 { /* if zero exponent, then normalize the significand */
3064 if ((k = enormlz (yy)) > NBITS)
3067 yy[E] -= (unsigned EMUSHORT) (k - 1);
3070 #endif /* not IBM */
3071 #endif /* not DEC */
3074 /* Convert double extended precision float PE to e type Y. */
3078 unsigned EMUSHORT *pe, *y;
3080 unsigned EMUSHORT yy[NI];
3081 unsigned EMUSHORT *e, *p, *q;
3086 for (i = 0; i < NE - 5; i++)
3088 /* This precision is not ordinarily supported on DEC or IBM. */
3090 for (i = 0; i < 5; i++)
3094 p = &yy[0] + (NE - 1);
3097 for (i = 0; i < 5; i++)
3101 if (! REAL_WORDS_BIG_ENDIAN)
3103 for (i = 0; i < 5; i++)
3106 /* For denormal long double Intel format, shift significand up one
3107 -- but only if the top significand bit is zero. A top bit of 1
3108 is "pseudodenormal" when the exponent is zero. */
3109 if((yy[NE-1] & 0x7fff) == 0 && (yy[NE-2] & 0x8000) == 0)
3111 unsigned EMUSHORT temp[NI];
3121 p = &yy[0] + (NE - 1);
3122 #ifdef ARM_EXTENDED_IEEE_FORMAT
3123 /* For ARMs, the exponent is in the lowest 15 bits of the word. */
3124 *p-- = (e[0] & 0x8000) | (e[1] & 0x7ffff);
3130 for (i = 0; i < 4; i++)
3135 /* Point to the exponent field and check max exponent cases. */
3137 if ((*p & 0x7fff) == 0x7fff)
3140 if (! REAL_WORDS_BIG_ENDIAN)
3142 for (i = 0; i < 4; i++)
3144 if ((i != 3 && pe[i] != 0)
3145 /* Anything but 0x8000 here, including 0, is a NaN. */
3146 || (i == 3 && pe[i] != 0x8000))
3148 enan (y, (*p & 0x8000) != 0);
3155 #ifdef ARM_EXTENDED_IEEE_FORMAT
3156 for (i = 2; i <= 5; i++)
3160 enan (y, (*p & 0x8000) != 0);
3165 /* In Motorola extended precision format, the most significant
3166 bit of an infinity mantissa could be either 1 or 0. It is
3167 the lower order bits that tell whether the value is a NaN. */
3168 if ((pe[2] & 0x7fff) != 0)
3171 for (i = 3; i <= 5; i++)
3176 enan (y, (*p & 0x8000) != 0);
3180 #endif /* not ARM */
3189 #endif /* INFINITY */
3192 for (i = 0; i < NE; i++)
3196 /* Convert 128-bit long double precision float PE to e type Y. */
3200 unsigned EMUSHORT *pe, *y;
3202 register unsigned EMUSHORT r;
3203 unsigned EMUSHORT *e, *p;
3204 unsigned EMUSHORT yy[NI];
3211 if (! REAL_WORDS_BIG_ENDIAN)
3223 if (! REAL_WORDS_BIG_ENDIAN)
3225 for (i = 0; i < 7; i++)
3229 enan (y, yy[0] != 0);
3236 for (i = 1; i < 8; i++)
3240 enan (y, yy[0] != 0);
3252 #endif /* INFINITY */
3256 if (! REAL_WORDS_BIG_ENDIAN)
3258 for (i = 0; i < 7; i++)
3264 for (i = 0; i < 7; i++)
3268 /* If denormal, remove the implied bit; else shift down 1. */
3281 /* Convert single precision float PE to e type Y. */
3285 unsigned EMUSHORT *pe, *y;
3289 ibmtoe (pe, y, SFmode);
3292 register unsigned EMUSHORT r;
3293 register unsigned EMUSHORT *e, *p;
3294 unsigned EMUSHORT yy[NI];
3298 denorm = 0; /* flag if denormalized number */
3301 if (! REAL_WORDS_BIG_ENDIAN)
3311 yy[M] = (r & 0x7f) | 0200;
3312 r &= ~0x807f; /* strip sign and 7 significand bits */
3317 if (REAL_WORDS_BIG_ENDIAN)
3319 if (((pe[0] & 0x7f) != 0) || (pe[1] != 0))
3321 enan (y, yy[0] != 0);
3327 if (((pe[1] & 0x7f) != 0) || (pe[0] != 0))
3329 enan (y, yy[0] != 0);
3340 #endif /* INFINITY */
3342 /* If zero exponent, then the significand is denormalized.
3343 So take back the understood high significand bit. */
3356 if (! REAL_WORDS_BIG_ENDIAN)
3366 { /* if zero exponent, then normalize the significand */
3367 if ((k = enormlz (yy)) > NBITS)
3370 yy[E] -= (unsigned EMUSHORT) (k - 1);
3373 #endif /* not IBM */
3376 /* Convert e-type X to IEEE 128-bit long double format E. */
3380 unsigned EMUSHORT *x, *e;
3382 unsigned EMUSHORT xi[NI];
3389 make_nan (e, eisneg (x), TFmode);
3394 exp = (EMULONG) xi[E];
3399 /* round off to nearest or even */
3402 emdnorm (xi, 0, 0, exp, 64);
3408 /* Convert exploded e-type X, that has already been rounded to
3409 113-bit precision, to IEEE 128-bit long double format Y. */
3413 unsigned EMUSHORT *a, *b;
3415 register unsigned EMUSHORT *p, *q;
3416 unsigned EMUSHORT i;
3421 make_nan (b, eiisneg (a), TFmode);
3426 if (REAL_WORDS_BIG_ENDIAN)
3429 q = b + 7; /* point to output exponent */
3431 /* If not denormal, delete the implied bit. */
3436 /* combine sign and exponent */
3438 if (REAL_WORDS_BIG_ENDIAN)
3441 *q++ = *p++ | 0x8000;
3448 *q-- = *p++ | 0x8000;
3452 /* skip over guard word */
3454 /* move the significand */
3455 if (REAL_WORDS_BIG_ENDIAN)
3457 for (i = 0; i < 7; i++)
3462 for (i = 0; i < 7; i++)
3467 /* Convert e-type X to IEEE double extended format E. */
3471 unsigned EMUSHORT *x, *e;
3473 unsigned EMUSHORT xi[NI];
3480 make_nan (e, eisneg (x), XFmode);
3485 /* adjust exponent for offset */
3486 exp = (EMULONG) xi[E];
3491 /* round off to nearest or even */
3494 emdnorm (xi, 0, 0, exp, 64);
3500 /* Convert exploded e-type X, that has already been rounded to
3501 64-bit precision, to IEEE double extended format Y. */
3505 unsigned EMUSHORT *a, *b;
3507 register unsigned EMUSHORT *p, *q;
3508 unsigned EMUSHORT i;
3513 make_nan (b, eiisneg (a), XFmode);
3517 /* Shift denormal long double Intel format significand down one bit. */
3518 if ((a[E] == 0) && ! REAL_WORDS_BIG_ENDIAN)
3528 if (REAL_WORDS_BIG_ENDIAN)
3532 q = b + 4; /* point to output exponent */
3533 #if LONG_DOUBLE_TYPE_SIZE == 96
3534 /* Clear the last two bytes of 12-byte Intel format */
3540 /* combine sign and exponent */
3544 *q++ = *p++ | 0x8000;
3551 *q-- = *p++ | 0x8000;
3556 if (REAL_WORDS_BIG_ENDIAN)
3558 #ifdef ARM_EXTENDED_IEEE_FORMAT
3559 /* The exponent is in the lowest 15 bits of the first word. */
3560 *q++ = i ? 0x8000 : 0;
3564 *q++ = *p++ | 0x8000;
3573 *q-- = *p++ | 0x8000;
3578 /* skip over guard word */
3580 /* move the significand */
3582 for (i = 0; i < 4; i++)
3586 for (i = 0; i < 4; i++)
3590 if (REAL_WORDS_BIG_ENDIAN)
3592 for (i = 0; i < 4; i++)
3600 /* Intel long double infinity significand. */
3608 for (i = 0; i < 4; i++)
3614 /* e type to double precision. */
3617 /* Convert e-type X to DEC-format double E. */
3621 unsigned EMUSHORT *x, *e;
3623 etodec (x, e); /* see etodec.c */
3626 /* Convert exploded e-type X, that has already been rounded to
3627 56-bit double precision, to DEC double Y. */
3631 unsigned EMUSHORT *x, *y;
3638 /* Convert e-type X to IBM 370-format double E. */
3642 unsigned EMUSHORT *x, *e;
3644 etoibm (x, e, DFmode);
3647 /* Convert exploded e-type X, that has already been rounded to
3648 56-bit precision, to IBM 370 double Y. */
3652 unsigned EMUSHORT *x, *y;
3654 toibm (x, y, DFmode);
3657 #else /* it's neither DEC nor IBM */
3659 /* Convert e-type X to IEEE double E. */
3663 unsigned EMUSHORT *x, *e;
3665 unsigned EMUSHORT xi[NI];
3672 make_nan (e, eisneg (x), DFmode);
3677 /* adjust exponent for offsets */
3678 exp = (EMULONG) xi[E] - (EXONE - 0x3ff);
3683 /* round off to nearest or even */
3686 emdnorm (xi, 0, 0, exp, 64);
3692 /* Convert exploded e-type X, that has already been rounded to
3693 53-bit precision, to IEEE double Y. */
3697 unsigned EMUSHORT *x, *y;
3699 unsigned EMUSHORT i;
3700 unsigned EMUSHORT *p;
3705 make_nan (y, eiisneg (x), DFmode);
3711 if (! REAL_WORDS_BIG_ENDIAN)
3714 *y = 0; /* output high order */
3716 *y = 0x8000; /* output sign bit */
3719 if (i >= (unsigned int) 2047)
3721 /* Saturate at largest number less than infinity. */
3724 if (! REAL_WORDS_BIG_ENDIAN)
3738 *y |= (unsigned EMUSHORT) 0x7fef;
3739 if (! REAL_WORDS_BIG_ENDIAN)
3764 i |= *p++ & (unsigned EMUSHORT) 0x0f; /* *p = xi[M] */
3765 *y |= (unsigned EMUSHORT) i; /* high order output already has sign bit set */
3766 if (! REAL_WORDS_BIG_ENDIAN)
3781 #endif /* not IBM */
3782 #endif /* not DEC */
3786 /* e type to single precision. */
3789 /* Convert e-type X to IBM 370 float E. */
3793 unsigned EMUSHORT *x, *e;
3795 etoibm (x, e, SFmode);
3798 /* Convert exploded e-type X, that has already been rounded to
3799 float precision, to IBM 370 float Y. */
3803 unsigned EMUSHORT *x, *y;
3805 toibm (x, y, SFmode);
3809 /* Convert e-type X to IEEE float E. DEC float is the same as IEEE float. */
3813 unsigned EMUSHORT *x, *e;
3816 unsigned EMUSHORT xi[NI];
3822 make_nan (e, eisneg (x), SFmode);
3827 /* adjust exponent for offsets */
3828 exp = (EMULONG) xi[E] - (EXONE - 0177);
3833 /* round off to nearest or even */
3836 emdnorm (xi, 0, 0, exp, 64);
3842 /* Convert exploded e-type X, that has already been rounded to
3843 float precision, to IEEE float Y. */
3847 unsigned EMUSHORT *x, *y;
3849 unsigned EMUSHORT i;
3850 unsigned EMUSHORT *p;
3855 make_nan (y, eiisneg (x), SFmode);
3861 if (! REAL_WORDS_BIG_ENDIAN)
3867 *y = 0; /* output high order */
3869 *y = 0x8000; /* output sign bit */
3872 /* Handle overflow cases. */
3876 *y |= (unsigned EMUSHORT) 0x7f80;
3881 if (! REAL_WORDS_BIG_ENDIAN)
3889 #else /* no INFINITY */
3890 *y |= (unsigned EMUSHORT) 0x7f7f;
3895 if (! REAL_WORDS_BIG_ENDIAN)
3906 #endif /* no INFINITY */
3918 i |= *p++ & (unsigned EMUSHORT) 0x7f; /* *p = xi[M] */
3919 /* High order output already has sign bit set. */
3925 if (! REAL_WORDS_BIG_ENDIAN)
3934 #endif /* not IBM */
3936 /* Compare two e type numbers.
3940 -2 if either a or b is a NaN. */
3944 unsigned EMUSHORT *a, *b;
3946 unsigned EMUSHORT ai[NI], bi[NI];
3947 register unsigned EMUSHORT *p, *q;
3952 if (eisnan (a) || eisnan (b))
3961 { /* the signs are different */
3963 for (i = 1; i < NI - 1; i++)
3977 /* both are the same sign */
3992 return (0); /* equality */
3996 if (*(--p) > *(--q))
3997 return (msign); /* p is bigger */
3999 return (-msign); /* p is littler */
4002 /* Find e-type nearest integer to X, as floor (X + 0.5). */
4006 unsigned EMUSHORT *x, *y;
4012 /* Convert HOST_WIDE_INT LP to e type Y. */
4017 unsigned EMUSHORT *y;
4019 unsigned EMUSHORT yi[NI];
4020 unsigned HOST_WIDE_INT ll;
4026 /* make it positive */
4027 ll = (unsigned HOST_WIDE_INT) (-(*lp));
4028 yi[0] = 0xffff; /* put correct sign in the e type number */
4032 ll = (unsigned HOST_WIDE_INT) (*lp);
4034 /* move the long integer to yi significand area */
4035 #if HOST_BITS_PER_WIDE_INT == 64
4036 yi[M] = (unsigned EMUSHORT) (ll >> 48);
4037 yi[M + 1] = (unsigned EMUSHORT) (ll >> 32);
4038 yi[M + 2] = (unsigned EMUSHORT) (ll >> 16);
4039 yi[M + 3] = (unsigned EMUSHORT) ll;
4040 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
4042 yi[M] = (unsigned EMUSHORT) (ll >> 16);
4043 yi[M + 1] = (unsigned EMUSHORT) ll;
4044 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
4047 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
4048 ecleaz (yi); /* it was zero */
4050 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
4051 emovo (yi, y); /* output the answer */
4054 /* Convert unsigned HOST_WIDE_INT LP to e type Y. */
4058 unsigned HOST_WIDE_INT *lp;
4059 unsigned EMUSHORT *y;
4061 unsigned EMUSHORT yi[NI];
4062 unsigned HOST_WIDE_INT ll;
4068 /* move the long integer to ayi significand area */
4069 #if HOST_BITS_PER_WIDE_INT == 64
4070 yi[M] = (unsigned EMUSHORT) (ll >> 48);
4071 yi[M + 1] = (unsigned EMUSHORT) (ll >> 32);
4072 yi[M + 2] = (unsigned EMUSHORT) (ll >> 16);
4073 yi[M + 3] = (unsigned EMUSHORT) ll;
4074 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
4076 yi[M] = (unsigned EMUSHORT) (ll >> 16);
4077 yi[M + 1] = (unsigned EMUSHORT) ll;
4078 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
4081 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
4082 ecleaz (yi); /* it was zero */
4084 yi[E] -= (unsigned EMUSHORT) k; /* subtract shift count from exponent */
4085 emovo (yi, y); /* output the answer */
4089 /* Find signed HOST_WIDE_INT integer I and floating point fractional
4090 part FRAC of e-type (packed internal format) floating point input X.
4091 The integer output I has the sign of the input, except that
4092 positive overflow is permitted if FIXUNS_TRUNC_LIKE_FIX_TRUNC.
4093 The output e-type fraction FRAC is the positive fractional
4098 unsigned EMUSHORT *x;
4100 unsigned EMUSHORT *frac;
4102 unsigned EMUSHORT xi[NI];
4104 unsigned HOST_WIDE_INT ll;
4107 k = (int) xi[E] - (EXONE - 1);
4110 /* if exponent <= 0, integer = 0 and real output is fraction */
4115 if (k > (HOST_BITS_PER_WIDE_INT - 1))
4117 /* long integer overflow: output large integer
4118 and correct fraction */
4120 *i = ((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1);
4123 #ifdef FIXUNS_TRUNC_LIKE_FIX_TRUNC
4124 /* In this case, let it overflow and convert as if unsigned. */
4125 euifrac (x, &ll, frac);
4126 *i = (HOST_WIDE_INT) ll;
4129 /* In other cases, return the largest positive integer. */
4130 *i = (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1;
4135 warning ("overflow on truncation to integer");
4139 /* Shift more than 16 bits: first shift up k-16 mod 16,
4140 then shift up by 16's. */
4141 j = k - ((k >> 4) << 4);
4148 ll = (ll << 16) | xi[M];
4150 while ((k -= 16) > 0);
4157 /* shift not more than 16 bits */
4159 *i = (HOST_WIDE_INT) xi[M] & 0xffff;
4166 if ((k = enormlz (xi)) > NBITS)
4169 xi[E] -= (unsigned EMUSHORT) k;
4175 /* Find unsigned HOST_WIDE_INT integer I and floating point fractional part
4176 FRAC of e-type X. A negative input yields integer output = 0 but
4177 correct fraction. */
4180 euifrac (x, i, frac)
4181 unsigned EMUSHORT *x;
4182 unsigned HOST_WIDE_INT *i;
4183 unsigned EMUSHORT *frac;
4185 unsigned HOST_WIDE_INT ll;
4186 unsigned EMUSHORT xi[NI];
4190 k = (int) xi[E] - (EXONE - 1);
4193 /* if exponent <= 0, integer = 0 and argument is fraction */
4198 if (k > HOST_BITS_PER_WIDE_INT)
4200 /* Long integer overflow: output large integer
4201 and correct fraction.
4202 Note, the BSD microvax compiler says that ~(0UL)
4203 is a syntax error. */
4207 warning ("overflow on truncation to unsigned integer");
4211 /* Shift more than 16 bits: first shift up k-16 mod 16,
4212 then shift up by 16's. */
4213 j = k - ((k >> 4) << 4);
4220 ll = (ll << 16) | xi[M];
4222 while ((k -= 16) > 0);
4227 /* shift not more than 16 bits */
4229 *i = (HOST_WIDE_INT) xi[M] & 0xffff;
4232 if (xi[0]) /* A negative value yields unsigned integer 0. */
4238 if ((k = enormlz (xi)) > NBITS)
4241 xi[E] -= (unsigned EMUSHORT) k;
4246 /* Shift the significand of exploded e-type X up or down by SC bits. */
4250 unsigned EMUSHORT *x;
4253 unsigned EMUSHORT lost;
4254 unsigned EMUSHORT *p;
4267 lost |= *p; /* remember lost bits */
4308 return ((int) lost);
4311 /* Shift normalize the significand area of exploded e-type X.
4312 Return the shift count (up = positive). */
4316 unsigned EMUSHORT x[];
4318 register unsigned EMUSHORT *p;
4327 return (0); /* already normalized */
4333 /* With guard word, there are NBITS+16 bits available.
4334 Return true if all are zero. */
4338 /* see if high byte is zero */
4339 while ((*p & 0xff00) == 0)
4344 /* now shift 1 bit at a time */
4345 while ((*p & 0x8000) == 0)
4351 mtherr ("enormlz", UNDERFLOW);
4357 /* Normalize by shifting down out of the high guard word
4358 of the significand */
4373 mtherr ("enormlz", OVERFLOW);
4380 /* Powers of ten used in decimal <-> binary conversions. */
4385 #if LONG_DOUBLE_TYPE_SIZE == 128
4386 static unsigned EMUSHORT etens[NTEN + 1][NE] =
4388 {0x6576, 0x4a92, 0x804a, 0x153f,
4389 0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
4390 {0x6a32, 0xce52, 0x329a, 0x28ce,
4391 0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
4392 {0x526c, 0x50ce, 0xf18b, 0x3d28,
4393 0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
4394 {0x9c66, 0x58f8, 0xbc50, 0x5c54,
4395 0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
4396 {0x851e, 0xeab7, 0x98fe, 0x901b,
4397 0xddbb, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
4398 {0x0235, 0x0137, 0x36b1, 0x336c,
4399 0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
4400 {0x50f8, 0x25fb, 0xc76b, 0x6b71,
4401 0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
4402 {0x0000, 0x0000, 0x0000, 0x0000,
4403 0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
4404 {0x0000, 0x0000, 0x0000, 0x0000,
4405 0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
4406 {0x0000, 0x0000, 0x0000, 0x0000,
4407 0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
4408 {0x0000, 0x0000, 0x0000, 0x0000,
4409 0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
4410 {0x0000, 0x0000, 0x0000, 0x0000,
4411 0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
4412 {0x0000, 0x0000, 0x0000, 0x0000,
4413 0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
4416 static unsigned EMUSHORT emtens[NTEN + 1][NE] =
4418 {0x2030, 0xcffc, 0xa1c3, 0x8123,
4419 0x2de3, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
4420 {0x8264, 0xd2cb, 0xf2ea, 0x12d4,
4421 0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
4422 {0xf53f, 0xf698, 0x6bd3, 0x0158,
4423 0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
4424 {0xe731, 0x04d4, 0xe3f2, 0xd332,
4425 0x7132, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
4426 {0xa23e, 0x5308, 0xfefb, 0x1155,
4427 0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
4428 {0xe26d, 0xdbde, 0xd05d, 0xb3f6,
4429 0xac7c, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
4430 {0x2a20, 0x6224, 0x47b3, 0x98d7,
4431 0x3f23, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
4432 {0x0b5b, 0x4af2, 0xa581, 0x18ed,
4433 0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
4434 {0xbf71, 0xa9b3, 0x7989, 0xbe68,
4435 0x4c2e, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
4436 {0x3d4d, 0x7c3d, 0x36ba, 0x0d2b,
4437 0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
4438 {0xc155, 0xa4a8, 0x404e, 0x6113,
4439 0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
4440 {0xd70a, 0x70a3, 0x0a3d, 0xa3d7,
4441 0x3d70, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
4442 {0xcccd, 0xcccc, 0xcccc, 0xcccc,
4443 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
4446 /* LONG_DOUBLE_TYPE_SIZE is other than 128 */
4447 static unsigned EMUSHORT etens[NTEN + 1][NE] =
4449 {0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
4450 {0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
4451 {0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
4452 {0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
4453 {0xddbc, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
4454 {0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
4455 {0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
4456 {0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
4457 {0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
4458 {0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
4459 {0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
4460 {0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
4461 {0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
4464 static unsigned EMUSHORT emtens[NTEN + 1][NE] =
4466 {0x2de4, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
4467 {0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
4468 {0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
4469 {0x7133, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
4470 {0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
4471 {0xac7d, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
4472 {0x3f24, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
4473 {0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
4474 {0x4c2f, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
4475 {0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
4476 {0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
4477 {0x3d71, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
4478 {0xcccd, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
4482 /* Convert float value X to ASCII string STRING with NDIG digits after
4483 the decimal point. */
4486 e24toasc (x, string, ndigs)
4487 unsigned EMUSHORT x[];
4491 unsigned EMUSHORT w[NI];
4494 etoasc (w, string, ndigs);
4497 /* Convert double value X to ASCII string STRING with NDIG digits after
4498 the decimal point. */
4501 e53toasc (x, string, ndigs)
4502 unsigned EMUSHORT x[];
4506 unsigned EMUSHORT w[NI];
4509 etoasc (w, string, ndigs);
4512 /* Convert double extended value X to ASCII string STRING with NDIG digits
4513 after the decimal point. */
4516 e64toasc (x, string, ndigs)
4517 unsigned EMUSHORT x[];
4521 unsigned EMUSHORT w[NI];
4524 etoasc (w, string, ndigs);
4527 /* Convert 128-bit long double value X to ASCII string STRING with NDIG digits
4528 after the decimal point. */
4531 e113toasc (x, string, ndigs)
4532 unsigned EMUSHORT x[];
4536 unsigned EMUSHORT w[NI];
4539 etoasc (w, string, ndigs);
4542 /* Convert e-type X to ASCII string STRING with NDIGS digits after
4543 the decimal point. */
4545 static char wstring[80]; /* working storage for ASCII output */
4548 etoasc (x, string, ndigs)
4549 unsigned EMUSHORT x[];
4554 unsigned EMUSHORT y[NI], t[NI], u[NI], w[NI];
4555 unsigned EMUSHORT *p, *r, *ten;
4556 unsigned EMUSHORT sign;
4557 int i, j, k, expon, rndsav;
4559 unsigned EMUSHORT m;
4570 sprintf (wstring, " NaN ");
4574 rndprc = NBITS; /* set to full precision */
4575 emov (x, y); /* retain external format */
4576 if (y[NE - 1] & 0x8000)
4579 y[NE - 1] &= 0x7fff;
4586 ten = &etens[NTEN][0];
4588 /* Test for zero exponent */
4591 for (k = 0; k < NE - 1; k++)
4594 goto tnzro; /* denormalized number */
4596 goto isone; /* valid all zeros */
4600 /* Test for infinity. */
4601 if (y[NE - 1] == 0x7fff)
4604 sprintf (wstring, " -Infinity ");
4606 sprintf (wstring, " Infinity ");
4610 /* Test for exponent nonzero but significand denormalized.
4611 * This is an error condition.
4613 if ((y[NE - 1] != 0) && ((y[NE - 2] & 0x8000) == 0))
4615 mtherr ("etoasc", DOMAIN);
4616 sprintf (wstring, "NaN");
4620 /* Compare to 1.0 */
4629 { /* Number is greater than 1 */
4630 /* Convert significand to an integer and strip trailing decimal zeros. */
4632 u[NE - 1] = EXONE + NBITS - 1;
4634 p = &etens[NTEN - 4][0];
4640 for (j = 0; j < NE - 1; j++)
4653 /* Rescale from integer significand */
4654 u[NE - 1] += y[NE - 1] - (unsigned int) (EXONE + NBITS - 1);
4656 /* Find power of 10 */
4660 /* An unordered compare result shouldn't happen here. */
4661 while (ecmp (ten, u) <= 0)
4663 if (ecmp (p, u) <= 0)
4676 { /* Number is less than 1.0 */
4677 /* Pad significand with trailing decimal zeros. */
4680 while ((y[NE - 2] & 0x8000) == 0)
4689 for (i = 0; i < NDEC + 1; i++)
4691 if ((w[NI - 1] & 0x7) != 0)
4693 /* multiply by 10 */
4706 if (eone[NE - 1] <= u[1])
4718 while (ecmp (eone, w) > 0)
4720 if (ecmp (p, w) >= 0)
4735 /* Find the first (leading) digit. */
4741 digit = equot[NI - 1];
4742 while ((digit == 0) && (ecmp (y, ezero) != 0))
4750 digit = equot[NI - 1];
4758 /* Examine number of digits requested by caller. */
4776 *s++ = (char)digit + '0';
4779 /* Generate digits after the decimal point. */
4780 for (k = 0; k <= ndigs; k++)
4782 /* multiply current number by 10, without normalizing */
4789 *s++ = (char) equot[NI - 1] + '0';
4791 digit = equot[NI - 1];
4794 /* round off the ASCII string */
4797 /* Test for critical rounding case in ASCII output. */
4801 if (ecmp (t, ezero) != 0)
4802 goto roun; /* round to nearest */
4803 if ((*(s - 1) & 1) == 0)
4804 goto doexp; /* round to even */
4806 /* Round up and propagate carry-outs */
4810 /* Carry out to most significant digit? */
4817 /* Most significant digit carries to 10? */
4825 /* Round up and carry out from less significant digits */
4837 sprintf (ss, "e+%d", expon);
4839 sprintf (ss, "e%d", expon);
4841 sprintf (ss, "e%d", expon);
4844 /* copy out the working string */
4847 while (*ss == ' ') /* strip possible leading space */
4849 while ((*s++ = *ss++) != '\0')
4854 /* Convert ASCII string to floating point.
4856 Numeric input is a free format decimal number of any length, with
4857 or without decimal point. Entering E after the number followed by an
4858 integer number causes the second number to be interpreted as a power of
4859 10 to be multiplied by the first number (i.e., "scientific" notation). */
4861 /* Convert ASCII string S to single precision float value Y. */
4866 unsigned EMUSHORT *y;
4872 /* Convert ASCII string S to double precision value Y. */
4877 unsigned EMUSHORT *y;
4879 #if defined(DEC) || defined(IBM)
4887 /* Convert ASCII string S to double extended value Y. */
4892 unsigned EMUSHORT *y;
4897 /* Convert ASCII string S to 128-bit long double Y. */
4902 unsigned EMUSHORT *y;
4904 asctoeg (s, y, 113);
4907 /* Convert ASCII string S to e type Y. */
4912 unsigned EMUSHORT *y;
4914 asctoeg (s, y, NBITS);
4917 /* Convert ASCII string SS to e type Y, with a specified rounding precision
4921 asctoeg (ss, y, oprec)
4923 unsigned EMUSHORT *y;
4926 unsigned EMUSHORT yy[NI], xt[NI], tt[NI];
4927 int esign, decflg, sgnflg, nexp, exp, prec, lost;
4928 int k, trail, c, rndsav;
4930 unsigned EMUSHORT nsign, *p;
4931 char *sp, *s, *lstr;
4933 /* Copy the input string. */
4934 lstr = (char *) alloca (strlen (ss) + 1);
4936 while (*s == ' ') /* skip leading spaces */
4939 while ((*sp++ = *s++) != '\0')
4944 rndprc = NBITS; /* Set to full precision */
4957 if ((k >= 0) && (k <= 9))
4959 /* Ignore leading zeros */
4960 if ((prec == 0) && (decflg == 0) && (k == 0))
4962 /* Identify and strip trailing zeros after the decimal point. */
4963 if ((trail == 0) && (decflg != 0))
4966 while ((*sp >= '0') && (*sp <= '9'))
4968 /* Check for syntax error */
4970 if ((c != 'e') && (c != 'E') && (c != '\0')
4971 && (c != '\n') && (c != '\r') && (c != ' ')
4982 /* If enough digits were given to more than fill up the yy register,
4983 continuing until overflow into the high guard word yy[2]
4984 guarantees that there will be a roundoff bit at the top
4985 of the low guard word after normalization. */
4990 nexp += 1; /* count digits after decimal point */
4991 eshup1 (yy); /* multiply current number by 10 */
4997 xt[NI - 2] = (unsigned EMUSHORT) k;
5002 /* Mark any lost non-zero digit. */
5004 /* Count lost digits before the decimal point. */
5019 case '.': /* decimal point */
5049 mtherr ("asctoe", DOMAIN);
5058 /* Exponent interpretation */
5060 /* 0.0eXXX is zero, regardless of XXX. Check for the 0.0. */
5061 for (k = 0; k < NI; k++)
5072 /* check for + or - */
5080 while ((*s >= '0') && (*s <= '9'))
5084 if (exp > -(MINDECEXP))
5094 if (exp > MAXDECEXP)
5098 yy[E] = 0x7fff; /* infinity */
5101 if (exp < MINDECEXP)
5110 /* Pad trailing zeros to minimize power of 10, per IEEE spec. */
5111 while ((nexp > 0) && (yy[2] == 0))
5123 if ((k = enormlz (yy)) > NBITS)
5128 lexp = (EXONE - 1 + NBITS) - k;
5129 emdnorm (yy, lost, 0, lexp, 64);
5131 /* Convert to external format:
5133 Multiply by 10**nexp. If precision is 64 bits,
5134 the maximum relative error incurred in forming 10**n
5135 for 0 <= n <= 324 is 8.2e-20, at 10**180.
5136 For 0 <= n <= 999, the peak relative error is 1.4e-19 at 10**947.
5137 For 0 >= n >= -999, it is -1.55e-19 at 10**-435. */
5152 /* Punt. Can't handle this without 2 divides. */
5153 emovi (etens[0], tt);
5160 p = &etens[NTEN][0];
5170 while (exp <= MAXP);
5188 /* Round and convert directly to the destination type */
5190 lexp -= EXONE - 0x3ff;
5192 else if (oprec == 24 || oprec == 56)
5193 lexp -= EXONE - (0x41 << 2);
5195 else if (oprec == 24)
5196 lexp -= EXONE - 0177;
5199 else if (oprec == 56)
5200 lexp -= EXONE - 0201;
5203 emdnorm (yy, k, 0, lexp, 64);
5213 todec (yy, y); /* see etodec.c */
5218 toibm (yy, y, DFmode);
5241 /* Return Y = largest integer not greater than X (truncated toward minus
5244 static unsigned EMUSHORT bmask[] =
5267 unsigned EMUSHORT x[], y[];
5269 register unsigned EMUSHORT *p;
5271 unsigned EMUSHORT f[NE];
5273 emov (x, f); /* leave in external format */
5274 expon = (int) f[NE - 1];
5275 e = (expon & 0x7fff) - (EXONE - 1);
5281 /* number of bits to clear out */
5293 /* clear the remaining bits */
5295 /* truncate negatives toward minus infinity */
5298 if ((unsigned EMUSHORT) expon & (unsigned EMUSHORT) 0x8000)
5300 for (i = 0; i < NE - 1; i++)
5313 /* Return S and EXP such that S * 2^EXP = X and .5 <= S < 1.
5314 For example, 1.1 = 0.55 * 2^1. */
5318 unsigned EMUSHORT x[];
5320 unsigned EMUSHORT s[];
5322 unsigned EMUSHORT xi[NI];
5326 /* Handle denormalized numbers properly using long integer exponent. */
5327 li = (EMULONG) ((EMUSHORT) xi[1]);
5335 *exp = (int) (li - 0x3ffe);
5339 /* Return e type Y = X * 2^PWR2. */
5343 unsigned EMUSHORT x[];
5345 unsigned EMUSHORT y[];
5347 unsigned EMUSHORT xi[NI];
5355 emdnorm (xi, i, i, li, 64);
5361 /* C = remainder after dividing B by A, all e type values.
5362 Least significant integer quotient bits left in EQUOT. */
5366 unsigned EMUSHORT a[], b[], c[];
5368 unsigned EMUSHORT den[NI], num[NI];
5372 || (ecmp (a, ezero) == 0)
5380 if (ecmp (a, ezero) == 0)
5382 mtherr ("eremain", SING);
5388 eiremain (den, num);
5389 /* Sign of remainder = sign of quotient */
5398 /* Return quotient of exploded e-types NUM / DEN in EQUOT,
5399 remainder in NUM. */
5403 unsigned EMUSHORT den[], num[];
5406 unsigned EMUSHORT j;
5409 ld -= enormlz (den);
5411 ln -= enormlz (num);
5415 if (ecmpm (den, num) <= 0)
5427 emdnorm (num, 0, 0, ln, 0);
5430 /* Report an error condition CODE encountered in function NAME.
5431 CODE is one of the following:
5433 Mnemonic Value Significance
5435 DOMAIN 1 argument domain error
5436 SING 2 function singularity
5437 OVERFLOW 3 overflow range error
5438 UNDERFLOW 4 underflow range error
5439 TLOSS 5 total loss of precision
5440 PLOSS 6 partial loss of precision
5441 INVALID 7 NaN - producing operation
5442 EDOM 33 Unix domain error code
5443 ERANGE 34 Unix range error code
5445 The order of appearance of the following messages is bound to the
5446 error codes defined above. */
5449 static char *ermsg[NMSGS] =
5451 "unknown", /* error code 0 */
5452 "domain", /* error code 1 */
5453 "singularity", /* et seq. */
5456 "total loss of precision",
5457 "partial loss of precision",
5471 /* The string passed by the calling program is supposed to be the
5472 name of the function in which the error occurred.
5473 The code argument selects which error message string will be printed. */
5475 if ((code <= 0) || (code >= NMSGS))
5477 sprintf (errstr, " %s %s error", name, ermsg[code]);
5480 /* Set global error message word */
5485 /* Convert DEC double precision D to e type E. */
5489 unsigned EMUSHORT *d;
5490 unsigned EMUSHORT *e;
5492 unsigned EMUSHORT y[NI];
5493 register unsigned EMUSHORT r, *p;
5495 ecleaz (y); /* start with a zero */
5496 p = y; /* point to our number */
5497 r = *d; /* get DEC exponent word */
5498 if (*d & (unsigned int) 0x8000)
5499 *p = 0xffff; /* fill in our sign */
5500 ++p; /* bump pointer to our exponent word */
5501 r &= 0x7fff; /* strip the sign bit */
5502 if (r == 0) /* answer = 0 if high order DEC word = 0 */
5506 r >>= 7; /* shift exponent word down 7 bits */
5507 r += EXONE - 0201; /* subtract DEC exponent offset */
5508 /* add our e type exponent offset */
5509 *p++ = r; /* to form our exponent */
5511 r = *d++; /* now do the high order mantissa */
5512 r &= 0177; /* strip off the DEC exponent and sign bits */
5513 r |= 0200; /* the DEC understood high order mantissa bit */
5514 *p++ = r; /* put result in our high guard word */
5516 *p++ = *d++; /* fill in the rest of our mantissa */
5520 eshdn8 (y); /* shift our mantissa down 8 bits */
5525 /* Convert e type X to DEC double precision D. */
5529 unsigned EMUSHORT *x, *d;
5531 unsigned EMUSHORT xi[NI];
5536 /* Adjust exponent for offsets. */
5537 exp = (EMULONG) xi[E] - (EXONE - 0201);
5538 /* Round off to nearest or even. */
5541 emdnorm (xi, 0, 0, exp, 64);
5546 /* Convert exploded e-type X, that has already been rounded to
5547 56-bit precision, to DEC format double Y. */
5551 unsigned EMUSHORT *x, *y;
5553 unsigned EMUSHORT i;
5554 unsigned EMUSHORT *p;
5593 /* Convert IBM single/double precision to e type. */
5597 unsigned EMUSHORT *d;
5598 unsigned EMUSHORT *e;
5599 enum machine_mode mode;
5601 unsigned EMUSHORT y[NI];
5602 register unsigned EMUSHORT r, *p;
5605 ecleaz (y); /* start with a zero */
5606 p = y; /* point to our number */
5607 r = *d; /* get IBM exponent word */
5608 if (*d & (unsigned int) 0x8000)
5609 *p = 0xffff; /* fill in our sign */
5610 ++p; /* bump pointer to our exponent word */
5611 r &= 0x7f00; /* strip the sign bit */
5612 r >>= 6; /* shift exponent word down 6 bits */
5613 /* in fact shift by 8 right and 2 left */
5614 r += EXONE - (0x41 << 2); /* subtract IBM exponent offset */
5615 /* add our e type exponent offset */
5616 *p++ = r; /* to form our exponent */
5618 *p++ = *d++ & 0xff; /* now do the high order mantissa */
5619 /* strip off the IBM exponent and sign bits */
5620 if (mode != SFmode) /* there are only 2 words in SFmode */
5622 *p++ = *d++; /* fill in the rest of our mantissa */
5627 if (y[M] == 0 && y[M+1] == 0 && y[M+2] == 0 && y[M+3] == 0)
5630 y[E] -= 5 + enormlz (y); /* now normalise the mantissa */
5631 /* handle change in RADIX */
5637 /* Convert e type to IBM single/double precision. */
5641 unsigned EMUSHORT *x, *d;
5642 enum machine_mode mode;
5644 unsigned EMUSHORT xi[NI];
5649 exp = (EMULONG) xi[E] - (EXONE - (0x41 << 2)); /* adjust exponent for offsets */
5650 /* round off to nearest or even */
5653 emdnorm (xi, 0, 0, exp, 64);
5655 toibm (xi, d, mode);
5660 unsigned EMUSHORT *x, *y;
5661 enum machine_mode mode;
5663 unsigned EMUSHORT i;
5664 unsigned EMUSHORT *p;
5712 /* Output a binary NaN bit pattern in the target machine's format. */
5714 /* If special NaN bit patterns are required, define them in tm.h
5715 as arrays of unsigned 16-bit shorts. Otherwise, use the default
5721 unsigned EMUSHORT TFbignan[8] =
5722 {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
5723 unsigned EMUSHORT TFlittlenan[8] = {0, 0, 0, 0, 0, 0, 0x8000, 0xffff};
5731 unsigned EMUSHORT XFbignan[6] =
5732 {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
5733 unsigned EMUSHORT XFlittlenan[6] = {0, 0, 0, 0xc000, 0xffff, 0};
5741 unsigned EMUSHORT DFbignan[4] = {0x7fff, 0xffff, 0xffff, 0xffff};
5742 unsigned EMUSHORT DFlittlenan[4] = {0, 0, 0, 0xfff8};
5750 unsigned EMUSHORT SFbignan[2] = {0x7fff, 0xffff};
5751 unsigned EMUSHORT SFlittlenan[2] = {0, 0xffc0};
5757 make_nan (nan, sign, mode)
5758 unsigned EMUSHORT *nan;
5760 enum machine_mode mode;
5763 unsigned EMUSHORT *p;
5767 /* Possibly the `reserved operand' patterns on a VAX can be
5768 used like NaN's, but probably not in the same way as IEEE. */
5769 #if !defined(DEC) && !defined(IBM)
5772 if (REAL_WORDS_BIG_ENDIAN)
5779 if (REAL_WORDS_BIG_ENDIAN)
5786 if (REAL_WORDS_BIG_ENDIAN)
5794 if (REAL_WORDS_BIG_ENDIAN)
5803 if (REAL_WORDS_BIG_ENDIAN)
5804 *nan++ = (sign << 15) | *p++;
5807 if (! REAL_WORDS_BIG_ENDIAN)
5808 *nan = (sign << 15) | *p;
5811 /* This is the inverse of the function `etarsingle' invoked by
5812 REAL_VALUE_TO_TARGET_SINGLE. */
5815 ereal_unto_float (f)
5819 unsigned EMUSHORT s[2];
5820 unsigned EMUSHORT e[NE];
5822 /* Convert 32 bit integer to array of 16 bit pieces in target machine order.
5823 This is the inverse operation to what the function `endian' does. */
5824 if (REAL_WORDS_BIG_ENDIAN)
5826 s[0] = (unsigned EMUSHORT) (f >> 16);
5827 s[1] = (unsigned EMUSHORT) f;
5831 s[0] = (unsigned EMUSHORT) f;
5832 s[1] = (unsigned EMUSHORT) (f >> 16);
5834 /* Convert and promote the target float to E-type. */
5836 /* Output E-type to REAL_VALUE_TYPE. */
5842 /* This is the inverse of the function `etardouble' invoked by
5843 REAL_VALUE_TO_TARGET_DOUBLE. */
5846 ereal_unto_double (d)
5850 unsigned EMUSHORT s[4];
5851 unsigned EMUSHORT e[NE];
5853 /* Convert array of HOST_WIDE_INT to equivalent array of 16-bit pieces. */
5854 if (REAL_WORDS_BIG_ENDIAN)
5856 s[0] = (unsigned EMUSHORT) (d[0] >> 16);
5857 s[1] = (unsigned EMUSHORT) d[0];
5858 s[2] = (unsigned EMUSHORT) (d[1] >> 16);
5859 s[3] = (unsigned EMUSHORT) d[1];
5863 /* Target float words are little-endian. */
5864 s[0] = (unsigned EMUSHORT) d[0];
5865 s[1] = (unsigned EMUSHORT) (d[0] >> 16);
5866 s[2] = (unsigned EMUSHORT) d[1];
5867 s[3] = (unsigned EMUSHORT) (d[1] >> 16);
5869 /* Convert target double to E-type. */
5871 /* Output E-type to REAL_VALUE_TYPE. */
5877 /* Convert an SFmode target `float' value to a REAL_VALUE_TYPE.
5878 This is somewhat like ereal_unto_float, but the input types
5879 for these are different. */
5882 ereal_from_float (f)
5886 unsigned EMUSHORT s[2];
5887 unsigned EMUSHORT e[NE];
5889 /* Convert 32 bit integer to array of 16 bit pieces in target machine order.
5890 This is the inverse operation to what the function `endian' does. */
5891 if (REAL_WORDS_BIG_ENDIAN)
5893 s[0] = (unsigned EMUSHORT) (f >> 16);
5894 s[1] = (unsigned EMUSHORT) f;
5898 s[0] = (unsigned EMUSHORT) f;
5899 s[1] = (unsigned EMUSHORT) (f >> 16);
5901 /* Convert and promote the target float to E-type. */
5903 /* Output E-type to REAL_VALUE_TYPE. */
5909 /* Convert a DFmode target `double' value to a REAL_VALUE_TYPE.
5910 This is somewhat like ereal_unto_double, but the input types
5911 for these are different.
5913 The DFmode is stored as an array of HOST_WIDE_INT in the target's
5914 data format, with no holes in the bit packing. The first element
5915 of the input array holds the bits that would come first in the
5916 target computer's memory. */
5919 ereal_from_double (d)
5923 unsigned EMUSHORT s[4];
5924 unsigned EMUSHORT e[NE];
5926 /* Convert array of HOST_WIDE_INT to equivalent array of 16-bit pieces. */
5927 if (REAL_WORDS_BIG_ENDIAN)
5929 s[0] = (unsigned EMUSHORT) (d[0] >> 16);
5930 s[1] = (unsigned EMUSHORT) d[0];
5931 #if HOST_BITS_PER_WIDE_INT == 32
5932 s[2] = (unsigned EMUSHORT) (d[1] >> 16);
5933 s[3] = (unsigned EMUSHORT) d[1];
5935 /* In this case the entire target double is contained in the
5936 first array element. The second element of the input is
5938 s[2] = (unsigned EMUSHORT) (d[0] >> 48);
5939 s[3] = (unsigned EMUSHORT) (d[0] >> 32);
5944 /* Target float words are little-endian. */
5945 s[0] = (unsigned EMUSHORT) d[0];
5946 s[1] = (unsigned EMUSHORT) (d[0] >> 16);
5947 #if HOST_BITS_PER_WIDE_INT == 32
5948 s[2] = (unsigned EMUSHORT) d[1];
5949 s[3] = (unsigned EMUSHORT) (d[1] >> 16);
5951 s[2] = (unsigned EMUSHORT) (d[0] >> 32);
5952 s[3] = (unsigned EMUSHORT) (d[0] >> 48);
5955 /* Convert target double to E-type. */
5957 /* Output E-type to REAL_VALUE_TYPE. */
5964 /* Convert target computer unsigned 64-bit integer to e-type.
5965 The endian-ness of DImode follows the convention for integers,
5966 so we use WORDS_BIG_ENDIAN here, not REAL_WORDS_BIG_ENDIAN. */
5970 unsigned EMUSHORT *di; /* Address of the 64-bit int. */
5971 unsigned EMUSHORT *e;
5973 unsigned EMUSHORT yi[NI];
5977 if (WORDS_BIG_ENDIAN)
5979 for (k = M; k < M + 4; k++)
5984 for (k = M + 3; k >= M; k--)
5987 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
5988 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
5989 ecleaz (yi); /* it was zero */
5991 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
5995 /* Convert target computer signed 64-bit integer to e-type. */
5999 unsigned EMUSHORT *di; /* Address of the 64-bit int. */
6000 unsigned EMUSHORT *e;
6002 unsigned EMULONG acc;
6003 unsigned EMUSHORT yi[NI];
6004 unsigned EMUSHORT carry;
6008 if (WORDS_BIG_ENDIAN)
6010 for (k = M; k < M + 4; k++)
6015 for (k = M + 3; k >= M; k--)
6018 /* Take absolute value */
6024 for (k = M + 3; k >= M; k--)
6026 acc = (unsigned EMULONG) (~yi[k] & 0xffff) + carry;
6033 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
6034 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
6035 ecleaz (yi); /* it was zero */
6037 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
6044 /* Convert e-type to unsigned 64-bit int. */
6048 unsigned EMUSHORT *x;
6049 unsigned EMUSHORT *i;
6051 unsigned EMUSHORT xi[NI];
6060 k = (int) xi[E] - (EXONE - 1);
6063 for (j = 0; j < 4; j++)
6069 for (j = 0; j < 4; j++)
6072 warning ("overflow on truncation to integer");
6077 /* Shift more than 16 bits: first shift up k-16 mod 16,
6078 then shift up by 16's. */
6079 j = k - ((k >> 4) << 4);
6083 if (WORDS_BIG_ENDIAN)
6094 if (WORDS_BIG_ENDIAN)
6099 while ((k -= 16) > 0);
6103 /* shift not more than 16 bits */
6108 if (WORDS_BIG_ENDIAN)
6127 /* Convert e-type to signed 64-bit int. */
6131 unsigned EMUSHORT *x;
6132 unsigned EMUSHORT *i;
6134 unsigned EMULONG acc;
6135 unsigned EMUSHORT xi[NI];
6136 unsigned EMUSHORT carry;
6137 unsigned EMUSHORT *isave;
6141 k = (int) xi[E] - (EXONE - 1);
6144 for (j = 0; j < 4; j++)
6150 for (j = 0; j < 4; j++)
6153 warning ("overflow on truncation to integer");
6159 /* Shift more than 16 bits: first shift up k-16 mod 16,
6160 then shift up by 16's. */
6161 j = k - ((k >> 4) << 4);
6165 if (WORDS_BIG_ENDIAN)
6176 if (WORDS_BIG_ENDIAN)
6181 while ((k -= 16) > 0);
6185 /* shift not more than 16 bits */
6188 if (WORDS_BIG_ENDIAN)
6204 /* Negate if negative */
6208 if (WORDS_BIG_ENDIAN)
6210 for (k = 0; k < 4; k++)
6212 acc = (unsigned EMULONG) (~(*isave) & 0xffff) + carry;
6213 if (WORDS_BIG_ENDIAN)
6225 /* Longhand square root routine. */
6228 static int esqinited = 0;
6229 static unsigned short sqrndbit[NI];
6233 unsigned EMUSHORT *x, *y;
6235 unsigned EMUSHORT temp[NI], num[NI], sq[NI], xx[NI];
6237 int i, j, k, n, nlups;
6242 sqrndbit[NI - 2] = 1;
6245 /* Check for arg <= 0 */
6246 i = ecmp (x, ezero);
6251 mtherr ("esqrt", DOMAIN);
6267 /* Bring in the arg and renormalize if it is denormal. */
6269 m = (EMULONG) xx[1]; /* local long word exponent */
6273 /* Divide exponent by 2 */
6275 exp = (unsigned short) ((m / 2) + 0x3ffe);
6277 /* Adjust if exponent odd */
6287 n = 8; /* get 8 bits of result per inner loop */
6293 /* bring in next word of arg */
6295 num[NI - 1] = xx[j + 3];
6296 /* Do additional bit on last outer loop, for roundoff. */
6299 for (i = 0; i < n; i++)
6301 /* Next 2 bits of arg */
6304 /* Shift up answer */
6306 /* Make trial divisor */
6307 for (k = 0; k < NI; k++)
6310 eaddm (sqrndbit, temp);
6311 /* Subtract and insert answer bit if it goes in */
6312 if (ecmpm (temp, num) <= 0)
6322 /* Adjust for extra, roundoff loop done. */
6323 exp += (NBITS - 1) - rndprc;
6325 /* Sticky bit = 1 if the remainder is nonzero. */
6327 for (i = 3; i < NI; i++)
6330 /* Renormalize and round off. */
6331 emdnorm (sq, k, 0, exp, 64);
6335 #endif /* EMU_NON_COMPILE not defined */
6337 /* Return the binary precision of the significand for a given
6338 floating point mode. The mode can hold an integer value
6339 that many bits wide, without losing any bits. */
6342 significand_size (mode)
6343 enum machine_mode mode;
6346 /* Don't test the modes, but their sizes, lest this
6347 code won't work for BITS_PER_UNIT != 8 . */
6349 switch (GET_MODE_BITSIZE (mode))
6355 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
6358 #if TARGET_FLOAT_FORMAT == IBM_FLOAT_FORMAT
6361 #if TARGET_FLOAT_FORMAT == VAX_FLOAT_FORMAT