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 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
31 /* To enable support of XFmode extended real floating point, define
32 LONG_DOUBLE_TYPE_SIZE 96 in the tm.h file (m68k.h or i386.h).
34 To support cross compilation between IEEE, VAX and IBM floating
35 point formats, define REAL_ARITHMETIC in the tm.h file.
37 In either case the machine files (tm.h) must not contain any code
38 that tries to use host floating point arithmetic to convert
39 REAL_VALUE_TYPEs from `double' to `float', pass them to fprintf,
40 etc. In cross-compile situations a REAL_VALUE_TYPE may not
41 be intelligible to the host computer's native arithmetic.
43 The emulator defaults to the host's floating point format so that
44 its decimal conversion functions can be used if desired (see
47 The first part of this file interfaces gcc to a floating point
48 arithmetic suite that was not written with gcc in mind. Avoid
49 changing the low-level arithmetic routines unless you have suitable
50 test programs available. A special version of the PARANOIA floating
51 point arithmetic tester, modified for this purpose, can be found on
52 usc.edu: /pub/C-numanal/ieeetest.zoo. Other tests, and libraries of
53 XFmode and TFmode transcendental functions, can be obtained by ftp from
54 netlib.att.com: netlib/cephes. */
56 /* Type of computer arithmetic.
57 Only one of DEC, IBM, IEEE, or UNK should get defined.
59 `IEEE', when REAL_WORDS_BIG_ENDIAN is non-zero, refers generically
60 to big-endian IEEE floating-point data structure. This definition
61 should work in SFmode `float' type and DFmode `double' type on
62 virtually all big-endian IEEE machines. If LONG_DOUBLE_TYPE_SIZE
63 has been defined to be 96, then IEEE also invokes the particular
64 XFmode (`long double' type) data structure used by the Motorola
65 680x0 series processors.
67 `IEEE', when REAL_WORDS_BIG_ENDIAN is zero, refers generally to
68 little-endian IEEE machines. In this case, if LONG_DOUBLE_TYPE_SIZE
69 has been defined to be 96, then IEEE also invokes the particular
70 XFmode `long double' data structure used by the Intel 80x86 series
73 `DEC' refers specifically to the Digital Equipment Corp PDP-11
74 and VAX floating point data structure. This model currently
75 supports no type wider than DFmode.
77 `IBM' refers specifically to the IBM System/370 and compatible
78 floating point data structure. This model currently supports
79 no type wider than DFmode. The IBM conversions were contributed by
80 frank@atom.ansto.gov.au (Frank Crawford).
82 If LONG_DOUBLE_TYPE_SIZE = 64 (the default, unless tm.h defines it)
83 then `long double' and `double' are both implemented, but they
84 both mean DFmode. In this case, the software floating-point
85 support available here is activated by writing
86 #define REAL_ARITHMETIC
89 The case LONG_DOUBLE_TYPE_SIZE = 128 activates TFmode support
90 and may deactivate XFmode since `long double' is used to refer
93 The macros FLOAT_WORDS_BIG_ENDIAN, HOST_FLOAT_WORDS_BIG_ENDIAN,
94 contributed by Richard Earnshaw <Richard.Earnshaw@cl.cam.ac.uk>,
95 separate the floating point unit's endian-ness from that of
96 the integer addressing. This permits one to define a big-endian
97 FPU on a little-endian machine (e.g., ARM). An extension to
98 BYTES_BIG_ENDIAN may be required for some machines in the future.
99 These optional macros may be defined in tm.h. In real.h, they
100 default to WORDS_BIG_ENDIAN, etc., so there is no need to define
101 them for any normal host or target machine on which the floats
102 and the integers have the same endian-ness. */
105 /* The following converts gcc macros into the ones used by this file. */
107 /* REAL_ARITHMETIC defined means that macros in real.h are
108 defined to call emulator functions. */
109 #ifdef REAL_ARITHMETIC
111 #if TARGET_FLOAT_FORMAT == VAX_FLOAT_FORMAT
112 /* PDP-11, Pro350, VAX: */
114 #else /* it's not VAX */
115 #if TARGET_FLOAT_FORMAT == IBM_FLOAT_FORMAT
116 /* IBM System/370 style */
118 #else /* it's also not an IBM */
119 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
121 #else /* it's not IEEE either */
122 /* UNKnown arithmetic. We don't support this and can't go on. */
123 unknown arithmetic type
125 #endif /* not IEEE */
129 #define REAL_WORDS_BIG_ENDIAN FLOAT_WORDS_BIG_ENDIAN
132 /* REAL_ARITHMETIC not defined means that the *host's* data
133 structure will be used. It may differ by endian-ness from the
134 target machine's structure and will get its ends swapped
135 accordingly (but not here). Probably only the decimal <-> binary
136 functions in this file will actually be used in this case. */
138 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
140 #else /* it's not VAX */
141 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
142 /* IBM System/370 style */
144 #else /* it's also not an IBM */
145 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
147 #else /* it's not IEEE either */
148 unknown arithmetic type
150 #endif /* not IEEE */
154 #define REAL_WORDS_BIG_ENDIAN HOST_FLOAT_WORDS_BIG_ENDIAN
156 #endif /* REAL_ARITHMETIC not defined */
158 /* Define INFINITY for support of infinity.
159 Define NANS for support of Not-a-Number's (NaN's). */
160 #if !defined(DEC) && !defined(IBM)
165 /* Support of NaNs requires support of infinity. */
172 /* Find a host integer type that is at least 16 bits wide,
173 and another type at least twice whatever that size is. */
175 #if HOST_BITS_PER_CHAR >= 16
176 #define EMUSHORT char
177 #define EMUSHORT_SIZE HOST_BITS_PER_CHAR
178 #define EMULONG_SIZE (2 * HOST_BITS_PER_CHAR)
180 #if HOST_BITS_PER_SHORT >= 16
181 #define EMUSHORT short
182 #define EMUSHORT_SIZE HOST_BITS_PER_SHORT
183 #define EMULONG_SIZE (2 * HOST_BITS_PER_SHORT)
185 #if HOST_BITS_PER_INT >= 16
187 #define EMUSHORT_SIZE HOST_BITS_PER_INT
188 #define EMULONG_SIZE (2 * HOST_BITS_PER_INT)
190 #if HOST_BITS_PER_LONG >= 16
191 #define EMUSHORT long
192 #define EMUSHORT_SIZE HOST_BITS_PER_LONG
193 #define EMULONG_SIZE (2 * HOST_BITS_PER_LONG)
195 /* You will have to modify this program to have a smaller unit size. */
196 #define EMU_NON_COMPILE
202 #if HOST_BITS_PER_SHORT >= EMULONG_SIZE
203 #define EMULONG short
205 #if HOST_BITS_PER_INT >= EMULONG_SIZE
208 #if HOST_BITS_PER_LONG >= EMULONG_SIZE
211 #if HOST_BITS_PER_LONG_LONG >= EMULONG_SIZE
212 #define EMULONG long long int
214 /* You will have to modify this program to have a smaller unit size. */
215 #define EMU_NON_COMPILE
222 /* The host interface doesn't work if no 16-bit size exists. */
223 #if EMUSHORT_SIZE != 16
224 #define EMU_NON_COMPILE
227 /* OK to continue compilation. */
228 #ifndef EMU_NON_COMPILE
230 /* Construct macros to translate between REAL_VALUE_TYPE and e type.
231 In GET_REAL and PUT_REAL, r and e are pointers.
232 A REAL_VALUE_TYPE is guaranteed to occupy contiguous locations
233 in memory, with no holes. */
235 #if LONG_DOUBLE_TYPE_SIZE == 96
236 /* Number of 16 bit words in external e type format */
238 #define MAXDECEXP 4932
239 #define MINDECEXP -4956
240 #define GET_REAL(r,e) bcopy ((char *) r, (char *) e, 2*NE)
241 #define PUT_REAL(e,r) bcopy ((char *) e, (char *) r, 2*NE)
242 #else /* no XFmode */
243 #if LONG_DOUBLE_TYPE_SIZE == 128
245 #define MAXDECEXP 4932
246 #define MINDECEXP -4977
247 #define GET_REAL(r,e) bcopy ((char *) r, (char *) e, 2*NE)
248 #define PUT_REAL(e,r) bcopy ((char *) e, (char *) r, 2*NE)
251 #define MAXDECEXP 4932
252 #define MINDECEXP -4956
253 #ifdef REAL_ARITHMETIC
254 /* Emulator uses target format internally
255 but host stores it in host endian-ness. */
257 #define GET_REAL(r,e) \
259 if (HOST_FLOAT_WORDS_BIG_ENDIAN == REAL_WORDS_BIG_ENDIAN) \
260 e53toe ((unsigned EMUSHORT*) (r), (e)); \
263 unsigned EMUSHORT w[4]; \
264 w[3] = ((EMUSHORT *) r)[0]; \
265 w[2] = ((EMUSHORT *) r)[1]; \
266 w[1] = ((EMUSHORT *) r)[2]; \
267 w[0] = ((EMUSHORT *) r)[3]; \
272 #define PUT_REAL(e,r) \
274 if (HOST_FLOAT_WORDS_BIG_ENDIAN == REAL_WORDS_BIG_ENDIAN) \
275 etoe53 ((e), (unsigned EMUSHORT *) (r)); \
278 unsigned EMUSHORT w[4]; \
280 *((EMUSHORT *) r) = w[3]; \
281 *((EMUSHORT *) r + 1) = w[2]; \
282 *((EMUSHORT *) r + 2) = w[1]; \
283 *((EMUSHORT *) r + 3) = w[0]; \
287 #else /* not REAL_ARITHMETIC */
289 /* emulator uses host format */
290 #define GET_REAL(r,e) e53toe ((unsigned EMUSHORT *) (r), (e))
291 #define PUT_REAL(e,r) etoe53 ((e), (unsigned EMUSHORT *) (r))
293 #endif /* not REAL_ARITHMETIC */
294 #endif /* not TFmode */
295 #endif /* no XFmode */
298 /* Number of 16 bit words in internal format */
301 /* Array offset to exponent */
304 /* Array offset to high guard word */
307 /* Number of bits of precision */
308 #define NBITS ((NI-4)*16)
310 /* Maximum number of decimal digits in ASCII conversion
313 #define NDEC (NBITS*8/27)
315 /* The exponent of 1.0 */
316 #define EXONE (0x3fff)
318 extern int extra_warnings;
319 extern unsigned EMUSHORT ezero[], ehalf[], eone[], etwo[];
320 extern unsigned EMUSHORT elog2[], esqrt2[];
322 static void endian PROTO((unsigned EMUSHORT *, long *,
324 static void eclear PROTO((unsigned EMUSHORT *));
325 static void emov PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
326 static void eabs PROTO((unsigned EMUSHORT *));
327 static void eneg PROTO((unsigned EMUSHORT *));
328 static int eisneg PROTO((unsigned EMUSHORT *));
329 static int eisinf PROTO((unsigned EMUSHORT *));
330 static int eisnan PROTO((unsigned EMUSHORT *));
331 static void einfin PROTO((unsigned EMUSHORT *));
332 static void enan PROTO((unsigned EMUSHORT *, int));
333 static void emovi PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
334 static void emovo PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
335 static void ecleaz PROTO((unsigned EMUSHORT *));
336 static void ecleazs PROTO((unsigned EMUSHORT *));
337 static void emovz PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
338 static void einan PROTO((unsigned EMUSHORT *));
339 static int eiisnan PROTO((unsigned EMUSHORT *));
340 static int eiisneg PROTO((unsigned EMUSHORT *));
341 static void eiinfin PROTO((unsigned EMUSHORT *));
342 static int eiisinf PROTO((unsigned EMUSHORT *));
343 static int ecmpm PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
344 static void eshdn1 PROTO((unsigned EMUSHORT *));
345 static void eshup1 PROTO((unsigned EMUSHORT *));
346 static void eshdn8 PROTO((unsigned EMUSHORT *));
347 static void eshup8 PROTO((unsigned EMUSHORT *));
348 static void eshup6 PROTO((unsigned EMUSHORT *));
349 static void eshdn6 PROTO((unsigned EMUSHORT *));
350 static void eaddm PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
\f
351 static void esubm PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
352 static void m16m PROTO((unsigned int, unsigned short *,
354 static int edivm PROTO((unsigned short *, unsigned short *));
355 static int emulm PROTO((unsigned short *, unsigned short *));
356 static void emdnorm PROTO((unsigned EMUSHORT *, int, int, EMULONG, int));
357 static void esub PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
358 unsigned EMUSHORT *));
359 static void eadd PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
360 unsigned EMUSHORT *));
361 static void eadd1 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
362 unsigned EMUSHORT *));
363 static void ediv PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
364 unsigned EMUSHORT *));
365 static void emul PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
366 unsigned EMUSHORT *));
367 static void e53toe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
368 static void e64toe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
369 static void e113toe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
370 static void e24toe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
371 static void etoe113 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
372 static void toe113 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
373 static void etoe64 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
374 static void toe64 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
375 static void etoe53 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
376 static void toe53 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
377 static void etoe24 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
378 static void toe24 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
379 static int ecmp PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
380 static void eround PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
381 static void ltoe PROTO((HOST_WIDE_INT *, unsigned EMUSHORT *));
382 static void ultoe PROTO((unsigned HOST_WIDE_INT *, unsigned EMUSHORT *));
383 static void eifrac PROTO((unsigned EMUSHORT *, HOST_WIDE_INT *,
384 unsigned EMUSHORT *));
385 static void euifrac PROTO((unsigned EMUSHORT *, unsigned HOST_WIDE_INT *,
386 unsigned EMUSHORT *));
387 static int eshift PROTO((unsigned EMUSHORT *, int));
388 static int enormlz PROTO((unsigned EMUSHORT *));
389 static void e24toasc PROTO((unsigned EMUSHORT *, char *, int));
390 static void e53toasc PROTO((unsigned EMUSHORT *, char *, int));
391 static void e64toasc PROTO((unsigned EMUSHORT *, char *, int));
392 static void e113toasc PROTO((unsigned EMUSHORT *, char *, int));
393 static void etoasc PROTO((unsigned EMUSHORT *, char *, int));
394 static void asctoe24 PROTO((char *, unsigned EMUSHORT *));
395 static void asctoe53 PROTO((char *, unsigned EMUSHORT *));
396 static void asctoe64 PROTO((char *, unsigned EMUSHORT *));
397 static void asctoe113 PROTO((char *, unsigned EMUSHORT *));
398 static void asctoe PROTO((char *, unsigned EMUSHORT *));
399 static void asctoeg PROTO((char *, unsigned EMUSHORT *, int));
400 static void efloor PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
401 static void efrexp PROTO((unsigned EMUSHORT *, int *,
402 unsigned EMUSHORT *));
403 static void eldexp PROTO((unsigned EMUSHORT *, int, unsigned EMUSHORT *));
404 static void eremain PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
405 unsigned EMUSHORT *));
406 static void eiremain PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
407 static void mtherr PROTO((char *, int));
408 static void dectoe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
409 static void etodec PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
410 static void todec PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
411 static void ibmtoe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
413 static void etoibm PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
415 static void toibm PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
417 static void make_nan PROTO((unsigned EMUSHORT *, int, enum machine_mode));
418 static void uditoe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
419 static void ditoe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
420 static void etoudi PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
421 static void etodi PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
422 static void esqrt PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
424 /* Copy 32-bit numbers obtained from array containing 16-bit numbers,
425 swapping ends if required, into output array of longs. The
426 result is normally passed to fprintf by the ASM_OUTPUT_ macros. */
430 unsigned EMUSHORT e[];
432 enum machine_mode mode;
436 if (REAL_WORDS_BIG_ENDIAN)
442 /* Swap halfwords in the fourth long. */
443 th = (unsigned long) e[6] & 0xffff;
444 t = (unsigned long) e[7] & 0xffff;
450 /* Swap halfwords in the third long. */
451 th = (unsigned long) e[4] & 0xffff;
452 t = (unsigned long) e[5] & 0xffff;
455 /* fall into the double case */
459 /* swap halfwords in the second word */
460 th = (unsigned long) e[2] & 0xffff;
461 t = (unsigned long) e[3] & 0xffff;
464 /* fall into the float case */
469 /* swap halfwords in the first word */
470 th = (unsigned long) e[0] & 0xffff;
471 t = (unsigned long) e[1] & 0xffff;
482 /* Pack the output array without swapping. */
489 /* Pack the fourth long. */
490 th = (unsigned long) e[7] & 0xffff;
491 t = (unsigned long) e[6] & 0xffff;
497 /* Pack the third long.
498 Each element of the input REAL_VALUE_TYPE array has 16 useful bits
500 th = (unsigned long) e[5] & 0xffff;
501 t = (unsigned long) e[4] & 0xffff;
504 /* fall into the double case */
508 /* pack the second long */
509 th = (unsigned long) e[3] & 0xffff;
510 t = (unsigned long) e[2] & 0xffff;
513 /* fall into the float case */
518 /* pack the first long */
519 th = (unsigned long) e[1] & 0xffff;
520 t = (unsigned long) e[0] & 0xffff;
532 /* This is the implementation of the REAL_ARITHMETIC macro. */
535 earith (value, icode, r1, r2)
536 REAL_VALUE_TYPE *value;
541 unsigned EMUSHORT d1[NE], d2[NE], v[NE];
547 /* Return NaN input back to the caller. */
550 PUT_REAL (d1, value);
555 PUT_REAL (d2, value);
559 code = (enum tree_code) icode;
567 esub (d2, d1, v); /* d1 - d2 */
575 #ifndef REAL_INFINITY
576 if (ecmp (d2, ezero) == 0)
579 enan (v, eisneg (d1) ^ eisneg (d2));
586 ediv (d2, d1, v); /* d1/d2 */
589 case MIN_EXPR: /* min (d1,d2) */
590 if (ecmp (d1, d2) < 0)
596 case MAX_EXPR: /* max (d1,d2) */
597 if (ecmp (d1, d2) > 0)
610 /* Truncate REAL_VALUE_TYPE toward zero to signed HOST_WIDE_INT.
611 implements REAL_VALUE_RNDZINT (x) (etrunci (x)). */
617 unsigned EMUSHORT f[NE], g[NE];
633 /* Truncate REAL_VALUE_TYPE toward zero to unsigned HOST_WIDE_INT;
634 implements REAL_VALUE_UNSIGNED_RNDZINT (x) (etruncui (x)). */
640 unsigned EMUSHORT f[NE], g[NE];
642 unsigned HOST_WIDE_INT l;
656 /* This is the REAL_VALUE_ATOF function. It converts a decimal string to
657 binary, rounding off as indicated by the machine_mode argument. Then it
658 promotes the rounded value to REAL_VALUE_TYPE. */
665 unsigned EMUSHORT tem[NE], e[NE];
695 /* Expansion of REAL_NEGATE. */
701 unsigned EMUSHORT e[NE];
711 /* Round real toward zero to HOST_WIDE_INT;
712 implements REAL_VALUE_FIX (x). */
718 unsigned EMUSHORT f[NE], g[NE];
725 warning ("conversion from NaN to int");
733 /* Round real toward zero to unsigned HOST_WIDE_INT
734 implements REAL_VALUE_UNSIGNED_FIX (x).
735 Negative input returns zero. */
737 unsigned HOST_WIDE_INT
741 unsigned EMUSHORT f[NE], g[NE];
742 unsigned HOST_WIDE_INT l;
748 warning ("conversion from NaN to unsigned int");
757 /* REAL_VALUE_FROM_INT macro. */
760 ereal_from_int (d, i, j)
764 unsigned EMUSHORT df[NE], dg[NE];
765 HOST_WIDE_INT low, high;
773 /* complement and add 1 */
780 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
781 ultoe ((unsigned HOST_WIDE_INT *) &high, dg);
783 ultoe ((unsigned HOST_WIDE_INT *) &low, df);
791 /* REAL_VALUE_FROM_UNSIGNED_INT macro. */
794 ereal_from_uint (d, i, j)
796 unsigned HOST_WIDE_INT i, j;
798 unsigned EMUSHORT df[NE], dg[NE];
799 unsigned HOST_WIDE_INT low, high;
803 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
812 /* REAL_VALUE_TO_INT macro. */
815 ereal_to_int (low, high, rr)
816 HOST_WIDE_INT *low, *high;
819 unsigned EMUSHORT d[NE], df[NE], dg[NE], dh[NE];
826 warning ("conversion from NaN to int");
832 /* convert positive value */
839 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
840 ediv (df, d, dg); /* dg = d / 2^32 is the high word */
841 euifrac (dg, (unsigned HOST_WIDE_INT *) high, dh);
842 emul (df, dh, dg); /* fractional part is the low word */
843 euifrac (dg, (unsigned HOST_WIDE_INT *)low, dh);
846 /* complement and add 1 */
856 /* REAL_VALUE_LDEXP macro. */
863 unsigned EMUSHORT e[NE], y[NE];
876 /* These routines are conditionally compiled because functions
877 of the same names may be defined in fold-const.c. */
879 #ifdef REAL_ARITHMETIC
881 /* Check for infinity in a REAL_VALUE_TYPE. */
887 unsigned EMUSHORT e[NE];
897 /* Check whether a REAL_VALUE_TYPE item is a NaN. */
903 unsigned EMUSHORT e[NE];
914 /* Check for a negative REAL_VALUE_TYPE number.
915 This just checks the sign bit, so that -0 counts as negative. */
921 return ereal_isneg (x);
924 /* Expansion of REAL_VALUE_TRUNCATE.
925 The result is in floating point, rounded to nearest or even. */
928 real_value_truncate (mode, arg)
929 enum machine_mode mode;
932 unsigned EMUSHORT e[NE], t[NE];
968 /* If an unsupported type was requested, presume that
969 the machine files know something useful to do with
970 the unmodified value. */
979 #endif /* REAL_ARITHMETIC defined */
981 /* Used for debugging--print the value of R in human-readable format
990 REAL_VALUE_TO_DECIMAL (r, "%.20g", dstr);
991 fprintf (stderr, "%s", dstr);
995 /* The following routines convert REAL_VALUE_TYPE to the various floating
996 point formats that are meaningful to supported computers.
998 The results are returned in 32-bit pieces, each piece stored in a `long'.
999 This is so they can be printed by statements like
1001 fprintf (file, "%lx, %lx", L[0], L[1]);
1003 that will work on both narrow- and wide-word host computers. */
1005 /* Convert R to a 128-bit long double precision value. The output array L
1006 contains four 32-bit pieces of the result, in the order they would appear
1014 unsigned EMUSHORT e[NE];
1018 endian (e, l, TFmode);
1021 /* Convert R to a double extended precision value. The output array L
1022 contains three 32-bit pieces of the result, in the order they would
1023 appear in memory. */
1030 unsigned EMUSHORT e[NE];
1034 endian (e, l, XFmode);
1037 /* Convert R to a double precision value. The output array L contains two
1038 32-bit pieces of the result, in the order they would appear in memory. */
1045 unsigned EMUSHORT e[NE];
1049 endian (e, l, DFmode);
1052 /* Convert R to a single precision float value stored in the least-significant
1053 bits of a `long'. */
1059 unsigned EMUSHORT e[NE];
1064 endian (e, &l, SFmode);
1068 /* Convert X to a decimal ASCII string S for output to an assembly
1069 language file. Note, there is no standard way to spell infinity or
1070 a NaN, so these values may require special treatment in the tm.h
1074 ereal_to_decimal (x, s)
1078 unsigned EMUSHORT e[NE];
1084 /* Compare X and Y. Return 1 if X > Y, 0 if X == Y, -1 if X < Y,
1085 or -2 if either is a NaN. */
1089 REAL_VALUE_TYPE x, y;
1091 unsigned EMUSHORT ex[NE], ey[NE];
1095 return (ecmp (ex, ey));
1098 /* Return 1 if the sign bit of X is set, else return 0. */
1104 unsigned EMUSHORT ex[NE];
1107 return (eisneg (ex));
1110 /* End of REAL_ARITHMETIC interface */
1113 Extended precision IEEE binary floating point arithmetic routines
1115 Numbers are stored in C language as arrays of 16-bit unsigned
1116 short integers. The arguments of the routines are pointers to
1119 External e type data structure, similar to Intel 8087 chip
1120 temporary real format but possibly with a larger significand:
1122 NE-1 significand words (least significant word first,
1123 most significant bit is normally set)
1124 exponent (value = EXONE for 1.0,
1125 top bit is the sign)
1128 Internal exploded e-type data structure of a number (a "word" is 16 bits):
1130 ei[0] sign word (0 for positive, 0xffff for negative)
1131 ei[1] biased exponent (value = EXONE for the number 1.0)
1132 ei[2] high guard word (always zero after normalization)
1134 to ei[NI-2] significand (NI-4 significand words,
1135 most significant word first,
1136 most significant bit is set)
1137 ei[NI-1] low guard word (0x8000 bit is rounding place)
1141 Routines for external format e-type numbers
1143 asctoe (string, e) ASCII string to extended double e type
1144 asctoe64 (string, &d) ASCII string to long double
1145 asctoe53 (string, &d) ASCII string to double
1146 asctoe24 (string, &f) ASCII string to single
1147 asctoeg (string, e, prec) ASCII string to specified precision
1148 e24toe (&f, e) IEEE single precision to e type
1149 e53toe (&d, e) IEEE double precision to e type
1150 e64toe (&d, e) IEEE long double precision to e type
1151 e113toe (&d, e) 128-bit long double precision to e type
1152 eabs (e) absolute value
1153 eadd (a, b, c) c = b + a
1155 ecmp (a, b) Returns 1 if a > b, 0 if a == b,
1156 -1 if a < b, -2 if either a or b is a NaN.
1157 ediv (a, b, c) c = b / a
1158 efloor (a, b) truncate to integer, toward -infinity
1159 efrexp (a, exp, s) extract exponent and significand
1160 eifrac (e, &l, frac) e to HOST_WIDE_INT and e type fraction
1161 euifrac (e, &l, frac) e to unsigned HOST_WIDE_INT and e type fraction
1162 einfin (e) set e to infinity, leaving its sign alone
1163 eldexp (a, n, b) multiply by 2**n
1165 emul (a, b, c) c = b * a
1167 eround (a, b) b = nearest integer value to a
1168 esub (a, b, c) c = b - a
1169 e24toasc (&f, str, n) single to ASCII string, n digits after decimal
1170 e53toasc (&d, str, n) double to ASCII string, n digits after decimal
1171 e64toasc (&d, str, n) 80-bit long double to ASCII string
1172 e113toasc (&d, str, n) 128-bit long double to ASCII string
1173 etoasc (e, str, n) e to ASCII string, n digits after decimal
1174 etoe24 (e, &f) convert e type to IEEE single precision
1175 etoe53 (e, &d) convert e type to IEEE double precision
1176 etoe64 (e, &d) convert e type to IEEE long double precision
1177 ltoe (&l, e) HOST_WIDE_INT to e type
1178 ultoe (&l, e) unsigned HOST_WIDE_INT to e type
1179 eisneg (e) 1 if sign bit of e != 0, else 0
1180 eisinf (e) 1 if e has maximum exponent (non-IEEE)
1181 or is infinite (IEEE)
1182 eisnan (e) 1 if e is a NaN
1185 Routines for internal format exploded e-type numbers
1187 eaddm (ai, bi) add significands, bi = bi + ai
1189 ecleazs (ei) set ei = 0 but leave its sign alone
1190 ecmpm (ai, bi) compare significands, return 1, 0, or -1
1191 edivm (ai, bi) divide significands, bi = bi / ai
1192 emdnorm (ai,l,s,exp) normalize and round off
1193 emovi (a, ai) convert external a to internal ai
1194 emovo (ai, a) convert internal ai to external a
1195 emovz (ai, bi) bi = ai, low guard word of bi = 0
1196 emulm (ai, bi) multiply significands, bi = bi * ai
1197 enormlz (ei) left-justify the significand
1198 eshdn1 (ai) shift significand and guards down 1 bit
1199 eshdn8 (ai) shift down 8 bits
1200 eshdn6 (ai) shift down 16 bits
1201 eshift (ai, n) shift ai n bits up (or down if n < 0)
1202 eshup1 (ai) shift significand and guards up 1 bit
1203 eshup8 (ai) shift up 8 bits
1204 eshup6 (ai) shift up 16 bits
1205 esubm (ai, bi) subtract significands, bi = bi - ai
1206 eiisinf (ai) 1 if infinite
1207 eiisnan (ai) 1 if a NaN
1208 eiisneg (ai) 1 if sign bit of ai != 0, else 0
1209 einan (ai) set ai = NaN
1210 eiinfin (ai) set ai = infinity
1212 The result is always normalized and rounded to NI-4 word precision
1213 after each arithmetic operation.
1215 Exception flags are NOT fully supported.
1217 Signaling NaN's are NOT supported; they are treated the same
1220 Define INFINITY for support of infinity; otherwise a
1221 saturation arithmetic is implemented.
1223 Define NANS for support of Not-a-Number items; otherwise the
1224 arithmetic will never produce a NaN output, and might be confused
1226 If NaN's are supported, the output of `ecmp (a,b)' is -2 if
1227 either a or b is a NaN. This means asking `if (ecmp (a,b) < 0)'
1228 may not be legitimate. Use `if (ecmp (a,b) == -1)' for `less than'
1231 Denormals are always supported here where appropriate (e.g., not
1232 for conversion to DEC numbers). */
1234 /* Definitions for error codes that are passed to the common error handling
1237 For Digital Equipment PDP-11 and VAX computers, certain
1238 IBM systems, and others that use numbers with a 56-bit
1239 significand, the symbol DEC should be defined. In this
1240 mode, most floating point constants are given as arrays
1241 of octal integers to eliminate decimal to binary conversion
1242 errors that might be introduced by the compiler.
1244 For computers, such as IBM PC, that follow the IEEE
1245 Standard for Binary Floating Point Arithmetic (ANSI/IEEE
1246 Std 754-1985), the symbol IEEE should be defined.
1247 These numbers have 53-bit significands. In this mode, constants
1248 are provided as arrays of hexadecimal 16 bit integers.
1249 The endian-ness of generated values is controlled by
1250 REAL_WORDS_BIG_ENDIAN.
1252 To accommodate other types of computer arithmetic, all
1253 constants are also provided in a normal decimal radix
1254 which one can hope are correctly converted to a suitable
1255 format by the available C language compiler. To invoke
1256 this mode, the symbol UNK is defined.
1258 An important difference among these modes is a predefined
1259 set of machine arithmetic constants for each. The numbers
1260 MACHEP (the machine roundoff error), MAXNUM (largest number
1261 represented), and several other parameters are preset by
1262 the configuration symbol. Check the file const.c to
1263 ensure that these values are correct for your computer.
1265 For ANSI C compatibility, define ANSIC equal to 1. Currently
1266 this affects only the atan2 function and others that use it. */
1268 /* Constant definitions for math error conditions. */
1270 #define DOMAIN 1 /* argument domain error */
1271 #define SING 2 /* argument singularity */
1272 #define OVERFLOW 3 /* overflow range error */
1273 #define UNDERFLOW 4 /* underflow range error */
1274 #define TLOSS 5 /* total loss of precision */
1275 #define PLOSS 6 /* partial loss of precision */
1276 #define INVALID 7 /* NaN-producing operation */
1278 /* e type constants used by high precision check routines */
1280 #if LONG_DOUBLE_TYPE_SIZE == 128
1282 unsigned EMUSHORT ezero[NE] =
1283 {0x0000, 0x0000, 0x0000, 0x0000,
1284 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,};
1285 extern unsigned EMUSHORT ezero[];
1288 unsigned EMUSHORT ehalf[NE] =
1289 {0x0000, 0x0000, 0x0000, 0x0000,
1290 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3ffe,};
1291 extern unsigned EMUSHORT ehalf[];
1294 unsigned EMUSHORT eone[NE] =
1295 {0x0000, 0x0000, 0x0000, 0x0000,
1296 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3fff,};
1297 extern unsigned EMUSHORT eone[];
1300 unsigned EMUSHORT etwo[NE] =
1301 {0x0000, 0x0000, 0x0000, 0x0000,
1302 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4000,};
1303 extern unsigned EMUSHORT etwo[];
1306 unsigned EMUSHORT e32[NE] =
1307 {0x0000, 0x0000, 0x0000, 0x0000,
1308 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4004,};
1309 extern unsigned EMUSHORT e32[];
1311 /* 6.93147180559945309417232121458176568075500134360255E-1 */
1312 unsigned EMUSHORT elog2[NE] =
1313 {0x40f3, 0xf6af, 0x03f2, 0xb398,
1314 0xc9e3, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1315 extern unsigned EMUSHORT elog2[];
1317 /* 1.41421356237309504880168872420969807856967187537695E0 */
1318 unsigned EMUSHORT esqrt2[NE] =
1319 {0x1d6f, 0xbe9f, 0x754a, 0x89b3,
1320 0x597d, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1321 extern unsigned EMUSHORT esqrt2[];
1323 /* 3.14159265358979323846264338327950288419716939937511E0 */
1324 unsigned EMUSHORT epi[NE] =
1325 {0x2902, 0x1cd1, 0x80dc, 0x628b,
1326 0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1327 extern unsigned EMUSHORT epi[];
1330 /* LONG_DOUBLE_TYPE_SIZE is other than 128 */
1331 unsigned EMUSHORT ezero[NE] =
1332 {0, 0000000, 0000000, 0000000, 0000000, 0000000,};
1333 unsigned EMUSHORT ehalf[NE] =
1334 {0, 0000000, 0000000, 0000000, 0100000, 0x3ffe,};
1335 unsigned EMUSHORT eone[NE] =
1336 {0, 0000000, 0000000, 0000000, 0100000, 0x3fff,};
1337 unsigned EMUSHORT etwo[NE] =
1338 {0, 0000000, 0000000, 0000000, 0100000, 0040000,};
1339 unsigned EMUSHORT e32[NE] =
1340 {0, 0000000, 0000000, 0000000, 0100000, 0040004,};
1341 unsigned EMUSHORT elog2[NE] =
1342 {0xc9e4, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1343 unsigned EMUSHORT esqrt2[NE] =
1344 {0x597e, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1345 unsigned EMUSHORT epi[NE] =
1346 {0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1349 /* Control register for rounding precision.
1350 This can be set to 113 (if NE=10), 80 (if NE=6), 64, 56, 53, or 24 bits. */
1355 /* Clear out entire e-type number X. */
1359 register unsigned EMUSHORT *x;
1363 for (i = 0; i < NE; i++)
1367 /* Move e-type number from A to B. */
1371 register unsigned EMUSHORT *a, *b;
1375 for (i = 0; i < NE; i++)
1380 /* Absolute value of e-type X. */
1384 unsigned EMUSHORT x[];
1386 /* sign is top bit of last word of external format */
1387 x[NE - 1] &= 0x7fff;
1390 /* Negate the e-type number X. */
1394 unsigned EMUSHORT x[];
1397 x[NE - 1] ^= 0x8000; /* Toggle the sign bit */
1400 /* Return 1 if sign bit of e-type number X is nonzero, else zero. */
1404 unsigned EMUSHORT x[];
1407 if (x[NE - 1] & 0x8000)
1413 /* Return 1 if e-type number X is infinity, else return zero. */
1417 unsigned EMUSHORT x[];
1424 if ((x[NE - 1] & 0x7fff) == 0x7fff)
1430 /* Check if e-type number is not a number. The bit pattern is one that we
1431 defined, so we know for sure how to detect it. */
1435 unsigned EMUSHORT x[];
1440 /* NaN has maximum exponent */
1441 if ((x[NE - 1] & 0x7fff) != 0x7fff)
1443 /* ... and non-zero significand field. */
1444 for (i = 0; i < NE - 1; i++)
1454 /* Fill e-type number X with infinity pattern (IEEE)
1455 or largest possible number (non-IEEE). */
1459 register unsigned EMUSHORT *x;
1464 for (i = 0; i < NE - 1; i++)
1468 for (i = 0; i < NE - 1; i++)
1496 /* Output an e-type NaN.
1497 This generates Intel's quiet NaN pattern for extended real.
1498 The exponent is 7fff, the leading mantissa word is c000. */
1502 register unsigned EMUSHORT *x;
1507 for (i = 0; i < NE - 2; i++)
1510 *x = (sign << 15) | 0x7fff;
1513 /* Move in an e-type number A, converting it to exploded e-type B. */
1517 unsigned EMUSHORT *a, *b;
1519 register unsigned EMUSHORT *p, *q;
1523 p = a + (NE - 1); /* point to last word of external number */
1524 /* get the sign bit */
1529 /* get the exponent */
1531 *q++ &= 0x7fff; /* delete the sign bit */
1533 if ((*(q - 1) & 0x7fff) == 0x7fff)
1539 for (i = 3; i < NI; i++)
1545 for (i = 2; i < NI; i++)
1551 /* clear high guard word */
1553 /* move in the significand */
1554 for (i = 0; i < NE - 1; i++)
1556 /* clear low guard word */
1560 /* Move out exploded e-type number A, converting it to e type B. */
1564 unsigned EMUSHORT *a, *b;
1566 register unsigned EMUSHORT *p, *q;
1567 unsigned EMUSHORT i;
1571 q = b + (NE - 1); /* point to output exponent */
1572 /* combine sign and exponent */
1575 *q-- = *p++ | 0x8000;
1579 if (*(p - 1) == 0x7fff)
1584 enan (b, eiisneg (a));
1592 /* skip over guard word */
1594 /* move the significand */
1595 for (j = 0; j < NE - 1; j++)
1599 /* Clear out exploded e-type number XI. */
1603 register unsigned EMUSHORT *xi;
1607 for (i = 0; i < NI; i++)
1611 /* Clear out exploded e-type XI, but don't touch the sign. */
1615 register unsigned EMUSHORT *xi;
1620 for (i = 0; i < NI - 1; i++)
1624 /* Move exploded e-type number from A to B. */
1628 register unsigned EMUSHORT *a, *b;
1632 for (i = 0; i < NI - 1; i++)
1634 /* clear low guard word */
1638 /* Generate exploded e-type NaN.
1639 The explicit pattern for this is maximum exponent and
1640 top two significant bits set. */
1644 unsigned EMUSHORT x[];
1652 /* Return nonzero if exploded e-type X is a NaN. */
1656 unsigned EMUSHORT x[];
1660 if ((x[E] & 0x7fff) == 0x7fff)
1662 for (i = M + 1; i < NI; i++)
1671 /* Return nonzero if sign of exploded e-type X is nonzero. */
1675 unsigned EMUSHORT x[];
1681 /* Fill exploded e-type X with infinity pattern.
1682 This has maximum exponent and significand all zeros. */
1686 unsigned EMUSHORT x[];
1693 /* Return nonzero if exploded e-type X is infinite. */
1697 unsigned EMUSHORT x[];
1704 if ((x[E] & 0x7fff) == 0x7fff)
1710 /* Compare significands of numbers in internal exploded e-type format.
1711 Guard words are included in the comparison.
1719 register unsigned EMUSHORT *a, *b;
1723 a += M; /* skip up to significand area */
1725 for (i = M; i < NI; i++)
1733 if (*(--a) > *(--b))
1739 /* Shift significand of exploded e-type X down by 1 bit. */
1743 register unsigned EMUSHORT *x;
1745 register unsigned EMUSHORT bits;
1748 x += M; /* point to significand area */
1751 for (i = M; i < NI; i++)
1763 /* Shift significand of exploded e-type X up by 1 bit. */
1767 register unsigned EMUSHORT *x;
1769 register unsigned EMUSHORT bits;
1775 for (i = M; i < NI; i++)
1788 /* Shift significand of exploded e-type X down by 8 bits. */
1792 register unsigned EMUSHORT *x;
1794 register unsigned EMUSHORT newbyt, oldbyt;
1799 for (i = M; i < NI; i++)
1809 /* Shift significand of exploded e-type X up by 8 bits. */
1813 register unsigned EMUSHORT *x;
1816 register unsigned EMUSHORT newbyt, oldbyt;
1821 for (i = M; i < NI; i++)
1831 /* Shift significand of exploded e-type X up by 16 bits. */
1835 register unsigned EMUSHORT *x;
1838 register unsigned EMUSHORT *p;
1843 for (i = M; i < NI - 1; i++)
1849 /* Shift significand of exploded e-type X down by 16 bits. */
1853 register unsigned EMUSHORT *x;
1856 register unsigned EMUSHORT *p;
1861 for (i = M; i < NI - 1; i++)
1867 /* Add significands of exploded e-type X and Y. X + Y replaces Y. */
1871 unsigned EMUSHORT *x, *y;
1873 register unsigned EMULONG a;
1880 for (i = M; i < NI; i++)
1882 a = (unsigned EMULONG) (*x) + (unsigned EMULONG) (*y) + carry;
1887 *y = (unsigned EMUSHORT) a;
1893 /* Subtract significands of exploded e-type X and Y. Y - X replaces Y. */
1897 unsigned EMUSHORT *x, *y;
1906 for (i = M; i < NI; i++)
1908 a = (unsigned EMULONG) (*y) - (unsigned EMULONG) (*x) - carry;
1913 *y = (unsigned EMUSHORT) a;
1920 static unsigned EMUSHORT equot[NI];
1924 /* Radix 2 shift-and-add versions of multiply and divide */
1927 /* Divide significands */
1931 unsigned EMUSHORT den[], num[];
1934 register unsigned EMUSHORT *p, *q;
1935 unsigned EMUSHORT j;
1941 for (i = M; i < NI; i++)
1946 /* Use faster compare and subtraction if denominator has only 15 bits of
1952 for (i = M + 3; i < NI; i++)
1957 if ((den[M + 1] & 1) != 0)
1965 for (i = 0; i < NBITS + 2; i++)
1983 /* The number of quotient bits to calculate is NBITS + 1 scaling guard
1984 bit + 1 roundoff bit. */
1989 for (i = 0; i < NBITS + 2; i++)
1991 if (ecmpm (den, num) <= 0)
1994 j = 1; /* quotient bit = 1 */
2008 /* test for nonzero remainder after roundoff bit */
2011 for (i = M; i < NI; i++)
2019 for (i = 0; i < NI; i++)
2025 /* Multiply significands */
2028 unsigned EMUSHORT a[], b[];
2030 unsigned EMUSHORT *p, *q;
2035 for (i = M; i < NI; i++)
2040 while (*p == 0) /* significand is not supposed to be zero */
2045 if ((*p & 0xff) == 0)
2053 for (i = 0; i < k; i++)
2057 /* remember if there were any nonzero bits shifted out */
2064 for (i = 0; i < NI; i++)
2067 /* return flag for lost nonzero bits */
2073 /* Radix 65536 versions of multiply and divide. */
2075 /* Multiply significand of e-type number B
2076 by 16-bit quantity A, return e-type result to C. */
2081 unsigned EMUSHORT b[], c[];
2083 register unsigned EMUSHORT *pp;
2084 register unsigned EMULONG carry;
2085 unsigned EMUSHORT *ps;
2086 unsigned EMUSHORT p[NI];
2087 unsigned EMULONG aa, m;
2096 for (i=M+1; i<NI; i++)
2106 m = (unsigned EMULONG) aa * *ps--;
2107 carry = (m & 0xffff) + *pp;
2108 *pp-- = (unsigned EMUSHORT)carry;
2109 carry = (carry >> 16) + (m >> 16) + *pp;
2110 *pp = (unsigned EMUSHORT)carry;
2111 *(pp-1) = carry >> 16;
2114 for (i=M; i<NI; i++)
2118 /* Divide significands of exploded e-types NUM / DEN. Neither the
2119 numerator NUM nor the denominator DEN is permitted to have its high guard
2124 unsigned EMUSHORT den[], num[];
2127 register unsigned EMUSHORT *p;
2128 unsigned EMULONG tnum;
2129 unsigned EMUSHORT j, tdenm, tquot;
2130 unsigned EMUSHORT tprod[NI+1];
2136 for (i=M; i<NI; i++)
2142 for (i=M; i<NI; i++)
2144 /* Find trial quotient digit (the radix is 65536). */
2145 tnum = (((unsigned EMULONG) num[M]) << 16) + num[M+1];
2147 /* Do not execute the divide instruction if it will overflow. */
2148 if ((tdenm * 0xffffL) < tnum)
2151 tquot = tnum / tdenm;
2152 /* Multiply denominator by trial quotient digit. */
2153 m16m ((unsigned int)tquot, den, tprod);
2154 /* The quotient digit may have been overestimated. */
2155 if (ecmpm (tprod, num) > 0)
2159 if (ecmpm (tprod, num) > 0)
2169 /* test for nonzero remainder after roundoff bit */
2172 for (i=M; i<NI; i++)
2179 for (i=0; i<NI; i++)
2185 /* Multiply significands of exploded e-type A and B, result in B. */
2189 unsigned EMUSHORT a[], b[];
2191 unsigned EMUSHORT *p, *q;
2192 unsigned EMUSHORT pprod[NI];
2193 unsigned EMUSHORT j;
2198 for (i=M; i<NI; i++)
2204 for (i=M+1; i<NI; i++)
2212 m16m ((unsigned int) *p--, b, pprod);
2213 eaddm(pprod, equot);
2219 for (i=0; i<NI; i++)
2222 /* return flag for lost nonzero bits */
2228 /* Normalize and round off.
2230 The internal format number to be rounded is S.
2231 Input LOST is 0 if the value is exact. This is the so-called sticky bit.
2233 Input SUBFLG indicates whether the number was obtained
2234 by a subtraction operation. In that case if LOST is nonzero
2235 then the number is slightly smaller than indicated.
2237 Input EXP is the biased exponent, which may be negative.
2238 the exponent field of S is ignored but is replaced by
2239 EXP as adjusted by normalization and rounding.
2241 Input RCNTRL is the rounding control. If it is nonzero, the
2242 returned value will be rounded to RNDPRC bits.
2244 For future reference: In order for emdnorm to round off denormal
2245 significands at the right point, the input exponent must be
2246 adjusted to be the actual value it would have after conversion to
2247 the final floating point type. This adjustment has been
2248 implemented for all type conversions (etoe53, etc.) and decimal
2249 conversions, but not for the arithmetic functions (eadd, etc.).
2250 Data types having standard 15-bit exponents are not affected by
2251 this, but SFmode and DFmode are affected. For example, ediv with
2252 rndprc = 24 will not round correctly to 24-bit precision if the
2253 result is denormal. */
2255 static int rlast = -1;
2257 static unsigned EMUSHORT rmsk = 0;
2258 static unsigned EMUSHORT rmbit = 0;
2259 static unsigned EMUSHORT rebit = 0;
2261 static unsigned EMUSHORT rbit[NI];
2264 emdnorm (s, lost, subflg, exp, rcntrl)
2265 unsigned EMUSHORT s[];
2272 unsigned EMUSHORT r;
2277 /* a blank significand could mean either zero or infinity. */
2290 if ((j > NBITS) && (exp < 32767))
2298 if (exp > (EMULONG) (-NBITS - 1))
2311 /* Round off, unless told not to by rcntrl. */
2314 /* Set up rounding parameters if the control register changed. */
2315 if (rndprc != rlast)
2322 rw = NI - 1; /* low guard word */
2342 /* For DEC or IBM arithmetic */
2369 /* Shift down 1 temporarily if the data structure has an implied
2370 most significant bit and the number is denormal.
2371 Intel long double denormals also lose one bit of precision. */
2372 if ((exp <= 0) && (rndprc != NBITS)
2373 && ((rndprc != 64) || ((rndprc == 64) && ! REAL_WORDS_BIG_ENDIAN)))
2375 lost |= s[NI - 1] & 1;
2378 /* Clear out all bits below the rounding bit,
2379 remembering in r if any were nonzero. */
2393 if ((r & rmbit) != 0)
2398 { /* round to even */
2399 if ((s[re] & rebit) == 0)
2411 /* Undo the temporary shift for denormal values. */
2412 if ((exp <= 0) && (rndprc != NBITS)
2413 && ((rndprc != 64) || ((rndprc == 64) && ! REAL_WORDS_BIG_ENDIAN)))
2418 { /* overflow on roundoff */
2431 for (i = 2; i < NI - 1; i++)
2434 warning ("floating point overflow");
2438 for (i = M + 1; i < NI - 1; i++)
2441 if ((rndprc < 64) || (rndprc == 113))
2456 s[1] = (unsigned EMUSHORT) exp;
2459 /* Subtract. C = B - A, all e type numbers. */
2461 static int subflg = 0;
2465 unsigned EMUSHORT *a, *b, *c;
2479 /* Infinity minus infinity is a NaN.
2480 Test for subtracting infinities of the same sign. */
2481 if (eisinf (a) && eisinf (b)
2482 && ((eisneg (a) ^ eisneg (b)) == 0))
2484 mtherr ("esub", INVALID);
2493 /* Add. C = A + B, all e type. */
2497 unsigned EMUSHORT *a, *b, *c;
2501 /* NaN plus anything is a NaN. */
2512 /* Infinity minus infinity is a NaN.
2513 Test for adding infinities of opposite signs. */
2514 if (eisinf (a) && eisinf (b)
2515 && ((eisneg (a) ^ eisneg (b)) != 0))
2517 mtherr ("esub", INVALID);
2526 /* Arithmetic common to both addition and subtraction. */
2530 unsigned EMUSHORT *a, *b, *c;
2532 unsigned EMUSHORT ai[NI], bi[NI], ci[NI];
2534 EMULONG lt, lta, ltb;
2555 /* compare exponents */
2560 { /* put the larger number in bi */
2570 if (lt < (EMULONG) (-NBITS - 1))
2571 goto done; /* answer same as larger addend */
2573 lost = eshift (ai, k); /* shift the smaller number down */
2577 /* exponents were the same, so must compare significands */
2580 { /* the numbers are identical in magnitude */
2581 /* if different signs, result is zero */
2587 /* if same sign, result is double */
2588 /* double denormalized tiny number */
2589 if ((bi[E] == 0) && ((bi[3] & 0x8000) == 0))
2594 /* add 1 to exponent unless both are zero! */
2595 for (j = 1; j < NI - 1; j++)
2599 /* This could overflow, but let emovo take care of that. */
2604 bi[E] = (unsigned EMUSHORT) ltb;
2608 { /* put the larger number in bi */
2624 emdnorm (bi, lost, subflg, ltb, 64);
2630 /* Divide: C = B/A, all e type. */
2634 unsigned EMUSHORT *a, *b, *c;
2636 unsigned EMUSHORT ai[NI], bi[NI];
2638 EMULONG lt, lta, ltb;
2641 /* Return any NaN input. */
2652 /* Zero over zero, or infinity over infinity, is a NaN. */
2653 if (((ecmp (a, ezero) == 0) && (ecmp (b, ezero) == 0))
2654 || (eisinf (a) && eisinf (b)))
2656 mtherr ("ediv", INVALID);
2657 enan (c, eisneg (a) ^ eisneg (b));
2661 /* Infinity over anything else is infinity. */
2665 if (eisneg (a) ^ eisneg (b))
2666 *(c + (NE - 1)) = 0x8000;
2668 *(c + (NE - 1)) = 0;
2672 /* Anything else over infinity is zero. */
2684 { /* See if numerator is zero. */
2685 for (i = 1; i < NI - 1; i++)
2689 ltb -= enormlz (bi);
2699 { /* possible divide by zero */
2700 for (i = 1; i < NI - 1; i++)
2704 lta -= enormlz (ai);
2709 *(c + (NE - 1)) = 0;
2711 *(c + (NE - 1)) = 0x8000;
2712 /* Divide by zero is not an invalid operation.
2713 It is a divide-by-zero operation! */
2715 mtherr ("ediv", SING);
2721 /* calculate exponent */
2722 lt = ltb - lta + EXONE;
2723 emdnorm (bi, i, 0, lt, 64);
2732 /* Multiply e-types A and B, return e-type product C. */
2736 unsigned EMUSHORT *a, *b, *c;
2738 unsigned EMUSHORT ai[NI], bi[NI];
2740 EMULONG lt, lta, ltb;
2743 /* NaN times anything is the same NaN. */
2754 /* Zero times infinity is a NaN. */
2755 if ((eisinf (a) && (ecmp (b, ezero) == 0))
2756 || (eisinf (b) && (ecmp (a, ezero) == 0)))
2758 mtherr ("emul", INVALID);
2759 enan (c, eisneg (a) ^ eisneg (b));
2763 /* Infinity times anything else is infinity. */
2765 if (eisinf (a) || eisinf (b))
2767 if (eisneg (a) ^ eisneg (b))
2768 *(c + (NE - 1)) = 0x8000;
2770 *(c + (NE - 1)) = 0;
2781 for (i = 1; i < NI - 1; i++)
2785 lta -= enormlz (ai);
2796 for (i = 1; i < NI - 1; i++)
2800 ltb -= enormlz (bi);
2809 /* Multiply significands */
2811 /* calculate exponent */
2812 lt = lta + ltb - (EXONE - 1);
2813 emdnorm (bi, j, 0, lt, 64);
2814 /* calculate sign of product */
2822 /* Convert double precision PE to e-type Y. */
2826 unsigned EMUSHORT *pe, *y;
2835 ibmtoe (pe, y, DFmode);
2838 register unsigned EMUSHORT r;
2839 register unsigned EMUSHORT *e, *p;
2840 unsigned EMUSHORT yy[NI];
2844 denorm = 0; /* flag if denormalized number */
2846 if (! REAL_WORDS_BIG_ENDIAN)
2852 yy[M] = (r & 0x0f) | 0x10;
2853 r &= ~0x800f; /* strip sign and 4 significand bits */
2858 if (! REAL_WORDS_BIG_ENDIAN)
2860 if (((pe[3] & 0xf) != 0) || (pe[2] != 0)
2861 || (pe[1] != 0) || (pe[0] != 0))
2863 enan (y, yy[0] != 0);
2869 if (((pe[0] & 0xf) != 0) || (pe[1] != 0)
2870 || (pe[2] != 0) || (pe[3] != 0))
2872 enan (y, yy[0] != 0);
2883 #endif /* INFINITY */
2885 /* If zero exponent, then the significand is denormalized.
2886 So take back the understood high significand bit. */
2897 if (! REAL_WORDS_BIG_ENDIAN)
2913 { /* if zero exponent, then normalize the significand */
2914 if ((k = enormlz (yy)) > NBITS)
2917 yy[E] -= (unsigned EMUSHORT) (k - 1);
2920 #endif /* not IBM */
2921 #endif /* not DEC */
2924 /* Convert double extended precision float PE to e type Y. */
2928 unsigned EMUSHORT *pe, *y;
2930 unsigned EMUSHORT yy[NI];
2931 unsigned EMUSHORT *e, *p, *q;
2936 for (i = 0; i < NE - 5; i++)
2938 /* This precision is not ordinarily supported on DEC or IBM. */
2940 for (i = 0; i < 5; i++)
2944 p = &yy[0] + (NE - 1);
2947 for (i = 0; i < 5; i++)
2951 if (! REAL_WORDS_BIG_ENDIAN)
2953 for (i = 0; i < 5; i++)
2956 /* For denormal long double Intel format, shift significand up one
2957 -- but only if the top significand bit is zero. A top bit of 1
2958 is "pseudodenormal" when the exponent is zero. */
2959 if((yy[NE-1] & 0x7fff) == 0 && (yy[NE-2] & 0x8000) == 0)
2961 unsigned EMUSHORT temp[NI];
2971 p = &yy[0] + (NE - 1);
2974 for (i = 0; i < 4; i++)
2979 /* Point to the exponent field and check max exponent cases. */
2984 if (! REAL_WORDS_BIG_ENDIAN)
2986 for (i = 0; i < 4; i++)
2988 if ((i != 3 && pe[i] != 0)
2989 /* Anything but 0x8000 here, including 0, is a NaN. */
2990 || (i == 3 && pe[i] != 0x8000))
2992 enan (y, (*p & 0x8000) != 0);
2999 for (i = 1; i <= 4; i++)
3003 enan (y, (*p & 0x8000) != 0);
3015 #endif /* INFINITY */
3018 for (i = 0; i < NE; i++)
3022 /* Convert 128-bit long double precision float PE to e type Y. */
3026 unsigned EMUSHORT *pe, *y;
3028 register unsigned EMUSHORT r;
3029 unsigned EMUSHORT *e, *p;
3030 unsigned EMUSHORT yy[NI];
3037 if (! REAL_WORDS_BIG_ENDIAN)
3049 if (! REAL_WORDS_BIG_ENDIAN)
3051 for (i = 0; i < 7; i++)
3055 enan (y, yy[0] != 0);
3062 for (i = 1; i < 8; i++)
3066 enan (y, yy[0] != 0);
3078 #endif /* INFINITY */
3082 if (! REAL_WORDS_BIG_ENDIAN)
3084 for (i = 0; i < 7; i++)
3090 for (i = 0; i < 7; i++)
3094 /* If denormal, remove the implied bit; else shift down 1. */
3107 /* Convert single precision float PE to e type Y. */
3111 unsigned EMUSHORT *pe, *y;
3115 ibmtoe (pe, y, SFmode);
3118 register unsigned EMUSHORT r;
3119 register unsigned EMUSHORT *e, *p;
3120 unsigned EMUSHORT yy[NI];
3124 denorm = 0; /* flag if denormalized number */
3127 if (! REAL_WORDS_BIG_ENDIAN)
3137 yy[M] = (r & 0x7f) | 0200;
3138 r &= ~0x807f; /* strip sign and 7 significand bits */
3143 if (REAL_WORDS_BIG_ENDIAN)
3145 if (((pe[0] & 0x7f) != 0) || (pe[1] != 0))
3147 enan (y, yy[0] != 0);
3153 if (((pe[1] & 0x7f) != 0) || (pe[0] != 0))
3155 enan (y, yy[0] != 0);
3166 #endif /* INFINITY */
3168 /* If zero exponent, then the significand is denormalized.
3169 So take back the understood high significand bit. */
3182 if (! REAL_WORDS_BIG_ENDIAN)
3192 { /* if zero exponent, then normalize the significand */
3193 if ((k = enormlz (yy)) > NBITS)
3196 yy[E] -= (unsigned EMUSHORT) (k - 1);
3199 #endif /* not IBM */
3202 /* Convert e-type X to IEEE 128-bit long double format E. */
3206 unsigned EMUSHORT *x, *e;
3208 unsigned EMUSHORT xi[NI];
3215 make_nan (e, eisneg (x), TFmode);
3220 exp = (EMULONG) xi[E];
3225 /* round off to nearest or even */
3228 emdnorm (xi, 0, 0, exp, 64);
3234 /* Convert exploded e-type X, that has already been rounded to
3235 113-bit precision, to IEEE 128-bit long double format Y. */
3239 unsigned EMUSHORT *a, *b;
3241 register unsigned EMUSHORT *p, *q;
3242 unsigned EMUSHORT i;
3247 make_nan (b, eiisneg (a), TFmode);
3252 if (REAL_WORDS_BIG_ENDIAN)
3255 q = b + 7; /* point to output exponent */
3257 /* If not denormal, delete the implied bit. */
3262 /* combine sign and exponent */
3264 if (REAL_WORDS_BIG_ENDIAN)
3267 *q++ = *p++ | 0x8000;
3274 *q-- = *p++ | 0x8000;
3278 /* skip over guard word */
3280 /* move the significand */
3281 if (REAL_WORDS_BIG_ENDIAN)
3283 for (i = 0; i < 7; i++)
3288 for (i = 0; i < 7; i++)
3293 /* Convert e-type X to IEEE double extended format E. */
3297 unsigned EMUSHORT *x, *e;
3299 unsigned EMUSHORT xi[NI];
3306 make_nan (e, eisneg (x), XFmode);
3311 /* adjust exponent for offset */
3312 exp = (EMULONG) xi[E];
3317 /* round off to nearest or even */
3320 emdnorm (xi, 0, 0, exp, 64);
3326 /* Convert exploded e-type X, that has already been rounded to
3327 64-bit precision, to IEEE double extended format Y. */
3331 unsigned EMUSHORT *a, *b;
3333 register unsigned EMUSHORT *p, *q;
3334 unsigned EMUSHORT i;
3339 make_nan (b, eiisneg (a), XFmode);
3343 /* Shift denormal long double Intel format significand down one bit. */
3344 if ((a[E] == 0) && ! REAL_WORDS_BIG_ENDIAN)
3354 if (REAL_WORDS_BIG_ENDIAN)
3358 q = b + 4; /* point to output exponent */
3359 #if LONG_DOUBLE_TYPE_SIZE == 96
3360 /* Clear the last two bytes of 12-byte Intel format */
3366 /* combine sign and exponent */
3370 *q++ = *p++ | 0x8000;
3377 *q-- = *p++ | 0x8000;
3382 if (REAL_WORDS_BIG_ENDIAN)
3385 *q++ = *p++ | 0x8000;
3393 *q-- = *p++ | 0x8000;
3398 /* skip over guard word */
3400 /* move the significand */
3402 for (i = 0; i < 4; i++)
3406 for (i = 0; i < 4; i++)
3410 if (REAL_WORDS_BIG_ENDIAN)
3412 for (i = 0; i < 4; i++)
3420 /* Intel long double infinity significand. */
3428 for (i = 0; i < 4; i++)
3434 /* e type to double precision. */
3437 /* Convert e-type X to DEC-format double E. */
3441 unsigned EMUSHORT *x, *e;
3443 etodec (x, e); /* see etodec.c */
3446 /* Convert exploded e-type X, that has already been rounded to
3447 56-bit double precision, to DEC double Y. */
3451 unsigned EMUSHORT *x, *y;
3458 /* Convert e-type X to IBM 370-format double E. */
3462 unsigned EMUSHORT *x, *e;
3464 etoibm (x, e, DFmode);
3467 /* Convert exploded e-type X, that has already been rounded to
3468 56-bit precision, to IBM 370 double Y. */
3472 unsigned EMUSHORT *x, *y;
3474 toibm (x, y, DFmode);
3477 #else /* it's neither DEC nor IBM */
3479 /* Convert e-type X to IEEE double E. */
3483 unsigned EMUSHORT *x, *e;
3485 unsigned EMUSHORT xi[NI];
3492 make_nan (e, eisneg (x), DFmode);
3497 /* adjust exponent for offsets */
3498 exp = (EMULONG) xi[E] - (EXONE - 0x3ff);
3503 /* round off to nearest or even */
3506 emdnorm (xi, 0, 0, exp, 64);
3512 /* Convert exploded e-type X, that has already been rounded to
3513 53-bit precision, to IEEE double Y. */
3517 unsigned EMUSHORT *x, *y;
3519 unsigned EMUSHORT i;
3520 unsigned EMUSHORT *p;
3525 make_nan (y, eiisneg (x), DFmode);
3531 if (! REAL_WORDS_BIG_ENDIAN)
3534 *y = 0; /* output high order */
3536 *y = 0x8000; /* output sign bit */
3539 if (i >= (unsigned int) 2047)
3540 { /* Saturate at largest number less than infinity. */
3543 if (! REAL_WORDS_BIG_ENDIAN)
3557 *y |= (unsigned EMUSHORT) 0x7fef;
3558 if (! REAL_WORDS_BIG_ENDIAN)
3583 i |= *p++ & (unsigned EMUSHORT) 0x0f; /* *p = xi[M] */
3584 *y |= (unsigned EMUSHORT) i; /* high order output already has sign bit set */
3585 if (! REAL_WORDS_BIG_ENDIAN)
3600 #endif /* not IBM */
3601 #endif /* not DEC */
3605 /* e type to single precision. */
3608 /* Convert e-type X to IBM 370 float E. */
3612 unsigned EMUSHORT *x, *e;
3614 etoibm (x, e, SFmode);
3617 /* Convert exploded e-type X, that has already been rounded to
3618 float precision, to IBM 370 float Y. */
3622 unsigned EMUSHORT *x, *y;
3624 toibm (x, y, SFmode);
3628 /* Convert e-type X to IEEE float E. DEC float is the same as IEEE float. */
3632 unsigned EMUSHORT *x, *e;
3635 unsigned EMUSHORT xi[NI];
3641 make_nan (e, eisneg (x), SFmode);
3646 /* adjust exponent for offsets */
3647 exp = (EMULONG) xi[E] - (EXONE - 0177);
3652 /* round off to nearest or even */
3655 emdnorm (xi, 0, 0, exp, 64);
3661 /* Convert exploded e-type X, that has already been rounded to
3662 float precision, to IEEE float Y. */
3666 unsigned EMUSHORT *x, *y;
3668 unsigned EMUSHORT i;
3669 unsigned EMUSHORT *p;
3674 make_nan (y, eiisneg (x), SFmode);
3680 if (! REAL_WORDS_BIG_ENDIAN)
3686 *y = 0; /* output high order */
3688 *y = 0x8000; /* output sign bit */
3691 /* Handle overflow cases. */
3695 *y |= (unsigned EMUSHORT) 0x7f80;
3700 if (! REAL_WORDS_BIG_ENDIAN)
3708 #else /* no INFINITY */
3709 *y |= (unsigned EMUSHORT) 0x7f7f;
3714 if (! REAL_WORDS_BIG_ENDIAN)
3725 #endif /* no INFINITY */
3737 i |= *p++ & (unsigned EMUSHORT) 0x7f; /* *p = xi[M] */
3738 /* High order output already has sign bit set. */
3744 if (! REAL_WORDS_BIG_ENDIAN)
3753 #endif /* not IBM */
3755 /* Compare two e type numbers.
3759 -2 if either a or b is a NaN. */
3763 unsigned EMUSHORT *a, *b;
3765 unsigned EMUSHORT ai[NI], bi[NI];
3766 register unsigned EMUSHORT *p, *q;
3771 if (eisnan (a) || eisnan (b))
3780 { /* the signs are different */
3782 for (i = 1; i < NI - 1; i++)
3796 /* both are the same sign */
3811 return (0); /* equality */
3815 if (*(--p) > *(--q))
3816 return (msign); /* p is bigger */
3818 return (-msign); /* p is littler */
3821 /* Find e-type nearest integer to X, as floor (X + 0.5). */
3825 unsigned EMUSHORT *x, *y;
3831 /* Convert HOST_WIDE_INT LP to e type Y. */
3836 unsigned EMUSHORT *y;
3838 unsigned EMUSHORT yi[NI];
3839 unsigned HOST_WIDE_INT ll;
3845 /* make it positive */
3846 ll = (unsigned HOST_WIDE_INT) (-(*lp));
3847 yi[0] = 0xffff; /* put correct sign in the e type number */
3851 ll = (unsigned HOST_WIDE_INT) (*lp);
3853 /* move the long integer to yi significand area */
3854 #if HOST_BITS_PER_WIDE_INT == 64
3855 yi[M] = (unsigned EMUSHORT) (ll >> 48);
3856 yi[M + 1] = (unsigned EMUSHORT) (ll >> 32);
3857 yi[M + 2] = (unsigned EMUSHORT) (ll >> 16);
3858 yi[M + 3] = (unsigned EMUSHORT) ll;
3859 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
3861 yi[M] = (unsigned EMUSHORT) (ll >> 16);
3862 yi[M + 1] = (unsigned EMUSHORT) ll;
3863 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
3866 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
3867 ecleaz (yi); /* it was zero */
3869 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
3870 emovo (yi, y); /* output the answer */
3873 /* Convert unsigned HOST_WIDE_INT LP to e type Y. */
3877 unsigned HOST_WIDE_INT *lp;
3878 unsigned EMUSHORT *y;
3880 unsigned EMUSHORT yi[NI];
3881 unsigned HOST_WIDE_INT ll;
3887 /* move the long integer to ayi significand area */
3888 #if HOST_BITS_PER_WIDE_INT == 64
3889 yi[M] = (unsigned EMUSHORT) (ll >> 48);
3890 yi[M + 1] = (unsigned EMUSHORT) (ll >> 32);
3891 yi[M + 2] = (unsigned EMUSHORT) (ll >> 16);
3892 yi[M + 3] = (unsigned EMUSHORT) ll;
3893 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
3895 yi[M] = (unsigned EMUSHORT) (ll >> 16);
3896 yi[M + 1] = (unsigned EMUSHORT) ll;
3897 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
3900 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
3901 ecleaz (yi); /* it was zero */
3903 yi[E] -= (unsigned EMUSHORT) k; /* subtract shift count from exponent */
3904 emovo (yi, y); /* output the answer */
3908 /* Find signed HOST_WIDE_INT integer I and floating point fractional
3909 part FRAC of e-type (packed internal format) floating point input X.
3910 The integer output I has the sign of the input, except that
3911 positive overflow is permitted if FIXUNS_TRUNC_LIKE_FIX_TRUNC.
3912 The output e-type fraction FRAC is the positive fractional
3917 unsigned EMUSHORT *x;
3919 unsigned EMUSHORT *frac;
3921 unsigned EMUSHORT xi[NI];
3923 unsigned HOST_WIDE_INT ll;
3926 k = (int) xi[E] - (EXONE - 1);
3929 /* if exponent <= 0, integer = 0 and real output is fraction */
3934 if (k > (HOST_BITS_PER_WIDE_INT - 1))
3936 /* long integer overflow: output large integer
3937 and correct fraction */
3939 *i = ((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1);
3942 #ifdef FIXUNS_TRUNC_LIKE_FIX_TRUNC
3943 /* In this case, let it overflow and convert as if unsigned. */
3944 euifrac (x, &ll, frac);
3945 *i = (HOST_WIDE_INT) ll;
3948 /* In other cases, return the largest positive integer. */
3949 *i = (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1;
3954 warning ("overflow on truncation to integer");
3958 /* Shift more than 16 bits: first shift up k-16 mod 16,
3959 then shift up by 16's. */
3960 j = k - ((k >> 4) << 4);
3967 ll = (ll << 16) | xi[M];
3969 while ((k -= 16) > 0);
3976 /* shift not more than 16 bits */
3978 *i = (HOST_WIDE_INT) xi[M] & 0xffff;
3985 if ((k = enormlz (xi)) > NBITS)
3988 xi[E] -= (unsigned EMUSHORT) k;
3994 /* Find unsigned HOST_WIDE_INT integer I and floating point fractional part
3995 FRAC of e-type X. A negative input yields integer output = 0 but
3996 correct fraction. */
3999 euifrac (x, i, frac)
4000 unsigned EMUSHORT *x;
4001 unsigned HOST_WIDE_INT *i;
4002 unsigned EMUSHORT *frac;
4004 unsigned HOST_WIDE_INT ll;
4005 unsigned EMUSHORT xi[NI];
4009 k = (int) xi[E] - (EXONE - 1);
4012 /* if exponent <= 0, integer = 0 and argument is fraction */
4017 if (k > HOST_BITS_PER_WIDE_INT)
4019 /* Long integer overflow: output large integer
4020 and correct fraction.
4021 Note, the BSD microvax compiler says that ~(0UL)
4022 is a syntax error. */
4026 warning ("overflow on truncation to unsigned integer");
4030 /* Shift more than 16 bits: first shift up k-16 mod 16,
4031 then shift up by 16's. */
4032 j = k - ((k >> 4) << 4);
4039 ll = (ll << 16) | xi[M];
4041 while ((k -= 16) > 0);
4046 /* shift not more than 16 bits */
4048 *i = (HOST_WIDE_INT) xi[M] & 0xffff;
4051 if (xi[0]) /* A negative value yields unsigned integer 0. */
4057 if ((k = enormlz (xi)) > NBITS)
4060 xi[E] -= (unsigned EMUSHORT) k;
4065 /* Shift the significand of exploded e-type X up or down by SC bits. */
4069 unsigned EMUSHORT *x;
4072 unsigned EMUSHORT lost;
4073 unsigned EMUSHORT *p;
4086 lost |= *p; /* remember lost bits */
4127 return ((int) lost);
4130 /* Shift normalize the significand area of exploded e-type X.
4131 Return the shift count (up = positive). */
4135 unsigned EMUSHORT x[];
4137 register unsigned EMUSHORT *p;
4146 return (0); /* already normalized */
4152 /* With guard word, there are NBITS+16 bits available.
4153 Return true if all are zero. */
4157 /* see if high byte is zero */
4158 while ((*p & 0xff00) == 0)
4163 /* now shift 1 bit at a time */
4164 while ((*p & 0x8000) == 0)
4170 mtherr ("enormlz", UNDERFLOW);
4176 /* Normalize by shifting down out of the high guard word
4177 of the significand */
4192 mtherr ("enormlz", OVERFLOW);
4199 /* Powers of ten used in decimal <-> binary conversions. */
4204 #if LONG_DOUBLE_TYPE_SIZE == 128
4205 static unsigned EMUSHORT etens[NTEN + 1][NE] =
4207 {0x6576, 0x4a92, 0x804a, 0x153f,
4208 0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
4209 {0x6a32, 0xce52, 0x329a, 0x28ce,
4210 0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
4211 {0x526c, 0x50ce, 0xf18b, 0x3d28,
4212 0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
4213 {0x9c66, 0x58f8, 0xbc50, 0x5c54,
4214 0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
4215 {0x851e, 0xeab7, 0x98fe, 0x901b,
4216 0xddbb, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
4217 {0x0235, 0x0137, 0x36b1, 0x336c,
4218 0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
4219 {0x50f8, 0x25fb, 0xc76b, 0x6b71,
4220 0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
4221 {0x0000, 0x0000, 0x0000, 0x0000,
4222 0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
4223 {0x0000, 0x0000, 0x0000, 0x0000,
4224 0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
4225 {0x0000, 0x0000, 0x0000, 0x0000,
4226 0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
4227 {0x0000, 0x0000, 0x0000, 0x0000,
4228 0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
4229 {0x0000, 0x0000, 0x0000, 0x0000,
4230 0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
4231 {0x0000, 0x0000, 0x0000, 0x0000,
4232 0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
4235 static unsigned EMUSHORT emtens[NTEN + 1][NE] =
4237 {0x2030, 0xcffc, 0xa1c3, 0x8123,
4238 0x2de3, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
4239 {0x8264, 0xd2cb, 0xf2ea, 0x12d4,
4240 0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
4241 {0xf53f, 0xf698, 0x6bd3, 0x0158,
4242 0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
4243 {0xe731, 0x04d4, 0xe3f2, 0xd332,
4244 0x7132, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
4245 {0xa23e, 0x5308, 0xfefb, 0x1155,
4246 0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
4247 {0xe26d, 0xdbde, 0xd05d, 0xb3f6,
4248 0xac7c, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
4249 {0x2a20, 0x6224, 0x47b3, 0x98d7,
4250 0x3f23, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
4251 {0x0b5b, 0x4af2, 0xa581, 0x18ed,
4252 0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
4253 {0xbf71, 0xa9b3, 0x7989, 0xbe68,
4254 0x4c2e, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
4255 {0x3d4d, 0x7c3d, 0x36ba, 0x0d2b,
4256 0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
4257 {0xc155, 0xa4a8, 0x404e, 0x6113,
4258 0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
4259 {0xd70a, 0x70a3, 0x0a3d, 0xa3d7,
4260 0x3d70, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
4261 {0xcccd, 0xcccc, 0xcccc, 0xcccc,
4262 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
4265 /* LONG_DOUBLE_TYPE_SIZE is other than 128 */
4266 static unsigned EMUSHORT etens[NTEN + 1][NE] =
4268 {0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
4269 {0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
4270 {0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
4271 {0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
4272 {0xddbc, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
4273 {0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
4274 {0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
4275 {0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
4276 {0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
4277 {0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
4278 {0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
4279 {0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
4280 {0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
4283 static unsigned EMUSHORT emtens[NTEN + 1][NE] =
4285 {0x2de4, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
4286 {0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
4287 {0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
4288 {0x7133, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
4289 {0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
4290 {0xac7d, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
4291 {0x3f24, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
4292 {0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
4293 {0x4c2f, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
4294 {0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
4295 {0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
4296 {0x3d71, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
4297 {0xcccd, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
4301 /* Convert float value X to ASCII string STRING with NDIG digits after
4302 the decimal point. */
4305 e24toasc (x, string, ndigs)
4306 unsigned EMUSHORT x[];
4310 unsigned EMUSHORT w[NI];
4313 etoasc (w, string, ndigs);
4316 /* Convert double value X to ASCII string STRING with NDIG digits after
4317 the decimal point. */
4320 e53toasc (x, string, ndigs)
4321 unsigned EMUSHORT x[];
4325 unsigned EMUSHORT w[NI];
4328 etoasc (w, string, ndigs);
4331 /* Convert double extended value X to ASCII string STRING with NDIG digits
4332 after the decimal point. */
4335 e64toasc (x, string, ndigs)
4336 unsigned EMUSHORT x[];
4340 unsigned EMUSHORT w[NI];
4343 etoasc (w, string, ndigs);
4346 /* Convert 128-bit long double value X to ASCII string STRING with NDIG digits
4347 after the decimal point. */
4350 e113toasc (x, string, ndigs)
4351 unsigned EMUSHORT x[];
4355 unsigned EMUSHORT w[NI];
4358 etoasc (w, string, ndigs);
4361 /* Convert e-type X to ASCII string STRING with NDIGS digits after
4362 the decimal point. */
4364 static char wstring[80]; /* working storage for ASCII output */
4367 etoasc (x, string, ndigs)
4368 unsigned EMUSHORT x[];
4373 unsigned EMUSHORT y[NI], t[NI], u[NI], w[NI];
4374 unsigned EMUSHORT *p, *r, *ten;
4375 unsigned EMUSHORT sign;
4376 int i, j, k, expon, rndsav;
4378 unsigned EMUSHORT m;
4389 sprintf (wstring, " NaN ");
4393 rndprc = NBITS; /* set to full precision */
4394 emov (x, y); /* retain external format */
4395 if (y[NE - 1] & 0x8000)
4398 y[NE - 1] &= 0x7fff;
4405 ten = &etens[NTEN][0];
4407 /* Test for zero exponent */
4410 for (k = 0; k < NE - 1; k++)
4413 goto tnzro; /* denormalized number */
4415 goto isone; /* valid all zeros */
4419 /* Test for infinity. */
4420 if (y[NE - 1] == 0x7fff)
4423 sprintf (wstring, " -Infinity ");
4425 sprintf (wstring, " Infinity ");
4429 /* Test for exponent nonzero but significand denormalized.
4430 * This is an error condition.
4432 if ((y[NE - 1] != 0) && ((y[NE - 2] & 0x8000) == 0))
4434 mtherr ("etoasc", DOMAIN);
4435 sprintf (wstring, "NaN");
4439 /* Compare to 1.0 */
4448 { /* Number is greater than 1 */
4449 /* Convert significand to an integer and strip trailing decimal zeros. */
4451 u[NE - 1] = EXONE + NBITS - 1;
4453 p = &etens[NTEN - 4][0];
4459 for (j = 0; j < NE - 1; j++)
4472 /* Rescale from integer significand */
4473 u[NE - 1] += y[NE - 1] - (unsigned int) (EXONE + NBITS - 1);
4475 /* Find power of 10 */
4479 /* An unordered compare result shouldn't happen here. */
4480 while (ecmp (ten, u) <= 0)
4482 if (ecmp (p, u) <= 0)
4495 { /* Number is less than 1.0 */
4496 /* Pad significand with trailing decimal zeros. */
4499 while ((y[NE - 2] & 0x8000) == 0)
4508 for (i = 0; i < NDEC + 1; i++)
4510 if ((w[NI - 1] & 0x7) != 0)
4512 /* multiply by 10 */
4525 if (eone[NE - 1] <= u[1])
4537 while (ecmp (eone, w) > 0)
4539 if (ecmp (p, w) >= 0)
4554 /* Find the first (leading) digit. */
4560 digit = equot[NI - 1];
4561 while ((digit == 0) && (ecmp (y, ezero) != 0))
4569 digit = equot[NI - 1];
4577 /* Examine number of digits requested by caller. */
4595 *s++ = (char)digit + '0';
4598 /* Generate digits after the decimal point. */
4599 for (k = 0; k <= ndigs; k++)
4601 /* multiply current number by 10, without normalizing */
4608 *s++ = (char) equot[NI - 1] + '0';
4610 digit = equot[NI - 1];
4613 /* round off the ASCII string */
4616 /* Test for critical rounding case in ASCII output. */
4620 if (ecmp (t, ezero) != 0)
4621 goto roun; /* round to nearest */
4622 if ((*(s - 1) & 1) == 0)
4623 goto doexp; /* round to even */
4625 /* Round up and propagate carry-outs */
4629 /* Carry out to most significant digit? */
4636 /* Most significant digit carries to 10? */
4644 /* Round up and carry out from less significant digits */
4656 sprintf (ss, "e+%d", expon);
4658 sprintf (ss, "e%d", expon);
4660 sprintf (ss, "e%d", expon);
4663 /* copy out the working string */
4666 while (*ss == ' ') /* strip possible leading space */
4668 while ((*s++ = *ss++) != '\0')
4673 /* Convert ASCII string to floating point.
4675 Numeric input is a free format decimal number of any length, with
4676 or without decimal point. Entering E after the number followed by an
4677 integer number causes the second number to be interpreted as a power of
4678 10 to be multiplied by the first number (i.e., "scientific" notation). */
4680 /* Convert ASCII string S to single precision float value Y. */
4685 unsigned EMUSHORT *y;
4691 /* Convert ASCII string S to double precision value Y. */
4696 unsigned EMUSHORT *y;
4698 #if defined(DEC) || defined(IBM)
4706 /* Convert ASCII string S to double extended value Y. */
4711 unsigned EMUSHORT *y;
4716 /* Convert ASCII string S to 128-bit long double Y. */
4721 unsigned EMUSHORT *y;
4723 asctoeg (s, y, 113);
4726 /* Convert ASCII string S to e type Y. */
4731 unsigned EMUSHORT *y;
4733 asctoeg (s, y, NBITS);
4736 /* Convert ASCII string SS to e type Y, with a specified rounding precision
4740 asctoeg (ss, y, oprec)
4742 unsigned EMUSHORT *y;
4745 unsigned EMUSHORT yy[NI], xt[NI], tt[NI];
4746 int esign, decflg, sgnflg, nexp, exp, prec, lost;
4747 int k, trail, c, rndsav;
4749 unsigned EMUSHORT nsign, *p;
4750 char *sp, *s, *lstr;
4752 /* Copy the input string. */
4753 lstr = (char *) alloca (strlen (ss) + 1);
4755 while (*s == ' ') /* skip leading spaces */
4758 while ((*sp++ = *s++) != '\0')
4763 rndprc = NBITS; /* Set to full precision */
4776 if ((k >= 0) && (k <= 9))
4778 /* Ignore leading zeros */
4779 if ((prec == 0) && (decflg == 0) && (k == 0))
4781 /* Identify and strip trailing zeros after the decimal point. */
4782 if ((trail == 0) && (decflg != 0))
4785 while ((*sp >= '0') && (*sp <= '9'))
4787 /* Check for syntax error */
4789 if ((c != 'e') && (c != 'E') && (c != '\0')
4790 && (c != '\n') && (c != '\r') && (c != ' ')
4801 /* If enough digits were given to more than fill up the yy register,
4802 continuing until overflow into the high guard word yy[2]
4803 guarantees that there will be a roundoff bit at the top
4804 of the low guard word after normalization. */
4809 nexp += 1; /* count digits after decimal point */
4810 eshup1 (yy); /* multiply current number by 10 */
4816 xt[NI - 2] = (unsigned EMUSHORT) k;
4821 /* Mark any lost non-zero digit. */
4823 /* Count lost digits before the decimal point. */
4838 case '.': /* decimal point */
4868 mtherr ("asctoe", DOMAIN);
4877 /* Exponent interpretation */
4883 /* check for + or - */
4891 while ((*s >= '0') && (*s <= '9'))
4895 if (exp > -(MINDECEXP))
4905 if (exp > MAXDECEXP)
4909 yy[E] = 0x7fff; /* infinity */
4912 if (exp < MINDECEXP)
4921 /* Pad trailing zeros to minimize power of 10, per IEEE spec. */
4922 while ((nexp > 0) && (yy[2] == 0))
4934 if ((k = enormlz (yy)) > NBITS)
4939 lexp = (EXONE - 1 + NBITS) - k;
4940 emdnorm (yy, lost, 0, lexp, 64);
4942 /* Convert to external format:
4944 Multiply by 10**nexp. If precision is 64 bits,
4945 the maximum relative error incurred in forming 10**n
4946 for 0 <= n <= 324 is 8.2e-20, at 10**180.
4947 For 0 <= n <= 999, the peak relative error is 1.4e-19 at 10**947.
4948 For 0 >= n >= -999, it is -1.55e-19 at 10**-435. */
4963 /* Punt. Can't handle this without 2 divides. */
4964 emovi (etens[0], tt);
4971 p = &etens[NTEN][0];
4981 while (exp <= MAXP);
4999 /* Round and convert directly to the destination type */
5001 lexp -= EXONE - 0x3ff;
5003 else if (oprec == 24 || oprec == 56)
5004 lexp -= EXONE - (0x41 << 2);
5006 else if (oprec == 24)
5007 lexp -= EXONE - 0177;
5010 else if (oprec == 56)
5011 lexp -= EXONE - 0201;
5014 emdnorm (yy, k, 0, lexp, 64);
5024 todec (yy, y); /* see etodec.c */
5029 toibm (yy, y, DFmode);
5052 /* Return Y = largest integer not greater than X (truncated toward minus
5055 static unsigned EMUSHORT bmask[] =
5078 unsigned EMUSHORT x[], y[];
5080 register unsigned EMUSHORT *p;
5082 unsigned EMUSHORT f[NE];
5084 emov (x, f); /* leave in external format */
5085 expon = (int) f[NE - 1];
5086 e = (expon & 0x7fff) - (EXONE - 1);
5092 /* number of bits to clear out */
5104 /* clear the remaining bits */
5106 /* truncate negatives toward minus infinity */
5109 if ((unsigned EMUSHORT) expon & (unsigned EMUSHORT) 0x8000)
5111 for (i = 0; i < NE - 1; i++)
5123 /* Return S and EXP such that S * 2^EXP = X and .5 <= S < 1.
5124 For example, 1.1 = 0.55 * 2^1. */
5128 unsigned EMUSHORT x[];
5130 unsigned EMUSHORT s[];
5132 unsigned EMUSHORT xi[NI];
5136 /* Handle denormalized numbers properly using long integer exponent. */
5137 li = (EMULONG) ((EMUSHORT) xi[1]);
5145 *exp = (int) (li - 0x3ffe);
5148 /* Return e type Y = X * 2^PWR2. */
5152 unsigned EMUSHORT x[];
5154 unsigned EMUSHORT y[];
5156 unsigned EMUSHORT xi[NI];
5164 emdnorm (xi, i, i, li, 64);
5169 /* C = remainder after dividing B by A, all e type values.
5170 Least significant integer quotient bits left in EQUOT. */
5174 unsigned EMUSHORT a[], b[], c[];
5176 unsigned EMUSHORT den[NI], num[NI];
5180 || (ecmp (a, ezero) == 0)
5188 if (ecmp (a, ezero) == 0)
5190 mtherr ("eremain", SING);
5196 eiremain (den, num);
5197 /* Sign of remainder = sign of quotient */
5205 /* Return quotient of exploded e-types NUM / DEN in EQUOT,
5206 remainder in NUM. */
5210 unsigned EMUSHORT den[], num[];
5213 unsigned EMUSHORT j;
5216 ld -= enormlz (den);
5218 ln -= enormlz (num);
5222 if (ecmpm (den, num) <= 0)
5234 emdnorm (num, 0, 0, ln, 0);
5237 /* Report an error condition CODE encountered in function NAME.
5238 CODE is one of the following:
5240 Mnemonic Value Significance
5242 DOMAIN 1 argument domain error
5243 SING 2 function singularity
5244 OVERFLOW 3 overflow range error
5245 UNDERFLOW 4 underflow range error
5246 TLOSS 5 total loss of precision
5247 PLOSS 6 partial loss of precision
5248 INVALID 7 NaN - producing operation
5249 EDOM 33 Unix domain error code
5250 ERANGE 34 Unix range error code
5252 The order of appearance of the following messages is bound to the
5253 error codes defined above. */
5256 static char *ermsg[NMSGS] =
5258 "unknown", /* error code 0 */
5259 "domain", /* error code 1 */
5260 "singularity", /* et seq. */
5263 "total loss of precision",
5264 "partial loss of precision",
5278 /* The string passed by the calling program is supposed to be the
5279 name of the function in which the error occurred.
5280 The code argument selects which error message string will be printed. */
5282 if ((code <= 0) || (code >= NMSGS))
5284 sprintf (errstr, " %s %s error", name, ermsg[code]);
5287 /* Set global error message word */
5292 /* Convert DEC double precision D to e type E. */
5296 unsigned EMUSHORT *d;
5297 unsigned EMUSHORT *e;
5299 unsigned EMUSHORT y[NI];
5300 register unsigned EMUSHORT r, *p;
5302 ecleaz (y); /* start with a zero */
5303 p = y; /* point to our number */
5304 r = *d; /* get DEC exponent word */
5305 if (*d & (unsigned int) 0x8000)
5306 *p = 0xffff; /* fill in our sign */
5307 ++p; /* bump pointer to our exponent word */
5308 r &= 0x7fff; /* strip the sign bit */
5309 if (r == 0) /* answer = 0 if high order DEC word = 0 */
5313 r >>= 7; /* shift exponent word down 7 bits */
5314 r += EXONE - 0201; /* subtract DEC exponent offset */
5315 /* add our e type exponent offset */
5316 *p++ = r; /* to form our exponent */
5318 r = *d++; /* now do the high order mantissa */
5319 r &= 0177; /* strip off the DEC exponent and sign bits */
5320 r |= 0200; /* the DEC understood high order mantissa bit */
5321 *p++ = r; /* put result in our high guard word */
5323 *p++ = *d++; /* fill in the rest of our mantissa */
5327 eshdn8 (y); /* shift our mantissa down 8 bits */
5332 /* Convert e type X to DEC double precision D. */
5336 unsigned EMUSHORT *x, *d;
5338 unsigned EMUSHORT xi[NI];
5343 /* Adjust exponent for offsets. */
5344 exp = (EMULONG) xi[E] - (EXONE - 0201);
5345 /* Round off to nearest or even. */
5348 emdnorm (xi, 0, 0, exp, 64);
5353 /* Convert exploded e-type X, that has already been rounded to
5354 56-bit precision, to DEC format double Y. */
5358 unsigned EMUSHORT *x, *y;
5360 unsigned EMUSHORT i;
5361 unsigned EMUSHORT *p;
5400 /* Convert IBM single/double precision to e type. */
5404 unsigned EMUSHORT *d;
5405 unsigned EMUSHORT *e;
5406 enum machine_mode mode;
5408 unsigned EMUSHORT y[NI];
5409 register unsigned EMUSHORT r, *p;
5412 ecleaz (y); /* start with a zero */
5413 p = y; /* point to our number */
5414 r = *d; /* get IBM exponent word */
5415 if (*d & (unsigned int) 0x8000)
5416 *p = 0xffff; /* fill in our sign */
5417 ++p; /* bump pointer to our exponent word */
5418 r &= 0x7f00; /* strip the sign bit */
5419 r >>= 6; /* shift exponent word down 6 bits */
5420 /* in fact shift by 8 right and 2 left */
5421 r += EXONE - (0x41 << 2); /* subtract IBM exponent offset */
5422 /* add our e type exponent offset */
5423 *p++ = r; /* to form our exponent */
5425 *p++ = *d++ & 0xff; /* now do the high order mantissa */
5426 /* strip off the IBM exponent and sign bits */
5427 if (mode != SFmode) /* there are only 2 words in SFmode */
5429 *p++ = *d++; /* fill in the rest of our mantissa */
5434 if (y[M] == 0 && y[M+1] == 0 && y[M+2] == 0 && y[M+3] == 0)
5437 y[E] -= 5 + enormlz (y); /* now normalise the mantissa */
5438 /* handle change in RADIX */
5444 /* Convert e type to IBM single/double precision. */
5448 unsigned EMUSHORT *x, *d;
5449 enum machine_mode mode;
5451 unsigned EMUSHORT xi[NI];
5456 exp = (EMULONG) xi[E] - (EXONE - (0x41 << 2)); /* adjust exponent for offsets */
5457 /* round off to nearest or even */
5460 emdnorm (xi, 0, 0, exp, 64);
5462 toibm (xi, d, mode);
5467 unsigned EMUSHORT *x, *y;
5468 enum machine_mode mode;
5470 unsigned EMUSHORT i;
5471 unsigned EMUSHORT *p;
5519 /* Output a binary NaN bit pattern in the target machine's format. */
5521 /* If special NaN bit patterns are required, define them in tm.h
5522 as arrays of unsigned 16-bit shorts. Otherwise, use the default
5528 unsigned EMUSHORT TFbignan[8] =
5529 {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
5530 unsigned EMUSHORT TFlittlenan[8] = {0, 0, 0, 0, 0, 0, 0x8000, 0xffff};
5538 unsigned EMUSHORT XFbignan[6] =
5539 {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
5540 unsigned EMUSHORT XFlittlenan[6] = {0, 0, 0, 0xc000, 0xffff, 0};
5548 unsigned EMUSHORT DFbignan[4] = {0x7fff, 0xffff, 0xffff, 0xffff};
5549 unsigned EMUSHORT DFlittlenan[4] = {0, 0, 0, 0xfff8};
5557 unsigned EMUSHORT SFbignan[2] = {0x7fff, 0xffff};
5558 unsigned EMUSHORT SFlittlenan[2] = {0, 0xffc0};
5564 make_nan (nan, sign, mode)
5565 unsigned EMUSHORT *nan;
5567 enum machine_mode mode;
5570 unsigned EMUSHORT *p;
5574 /* Possibly the `reserved operand' patterns on a VAX can be
5575 used like NaN's, but probably not in the same way as IEEE. */
5576 #if !defined(DEC) && !defined(IBM)
5579 if (REAL_WORDS_BIG_ENDIAN)
5586 if (REAL_WORDS_BIG_ENDIAN)
5593 if (REAL_WORDS_BIG_ENDIAN)
5601 if (REAL_WORDS_BIG_ENDIAN)
5610 if (REAL_WORDS_BIG_ENDIAN)
5611 *nan++ = (sign << 15) | *p++;
5614 if (! REAL_WORDS_BIG_ENDIAN)
5615 *nan = (sign << 15) | *p;
5618 /* Convert an SFmode target `float' value to a REAL_VALUE_TYPE.
5619 This is the inverse of the function `etarsingle' invoked by
5620 REAL_VALUE_TO_TARGET_SINGLE. */
5623 ereal_from_float (f)
5627 unsigned EMUSHORT s[2];
5628 unsigned EMUSHORT e[NE];
5630 /* Convert 32 bit integer to array of 16 bit pieces in target machine order.
5631 This is the inverse operation to what the function `endian' does. */
5632 if (REAL_WORDS_BIG_ENDIAN)
5634 s[0] = (unsigned EMUSHORT) (f >> 16);
5635 s[1] = (unsigned EMUSHORT) f;
5639 s[0] = (unsigned EMUSHORT) f;
5640 s[1] = (unsigned EMUSHORT) (f >> 16);
5642 /* Convert and promote the target float to E-type. */
5644 /* Output E-type to REAL_VALUE_TYPE. */
5650 /* Convert a DFmode target `double' value to a REAL_VALUE_TYPE.
5651 This is the inverse of the function `etardouble' invoked by
5652 REAL_VALUE_TO_TARGET_DOUBLE.
5654 The DFmode is stored as an array of HOST_WIDE_INT in the target's
5655 data format, with no holes in the bit packing. The first element
5656 of the input array holds the bits that would come first in the
5657 target computer's memory. */
5660 ereal_from_double (d)
5664 unsigned EMUSHORT s[4];
5665 unsigned EMUSHORT e[NE];
5667 /* Convert array of HOST_WIDE_INT to equivalent array of 16-bit pieces. */
5668 if (REAL_WORDS_BIG_ENDIAN)
5670 s[0] = (unsigned EMUSHORT) (d[0] >> 16);
5671 s[1] = (unsigned EMUSHORT) d[0];
5672 #if HOST_BITS_PER_WIDE_INT == 32
5673 s[2] = (unsigned EMUSHORT) (d[1] >> 16);
5674 s[3] = (unsigned EMUSHORT) d[1];
5676 /* In this case the entire target double is contained in the
5677 first array element. The second element of the input is
5679 s[2] = (unsigned EMUSHORT) (d[0] >> 48);
5680 s[3] = (unsigned EMUSHORT) (d[0] >> 32);
5685 /* Target float words are little-endian. */
5686 s[0] = (unsigned EMUSHORT) d[0];
5687 s[1] = (unsigned EMUSHORT) (d[0] >> 16);
5688 #if HOST_BITS_PER_WIDE_INT == 32
5689 s[2] = (unsigned EMUSHORT) d[1];
5690 s[3] = (unsigned EMUSHORT) (d[1] >> 16);
5692 s[2] = (unsigned EMUSHORT) (d[0] >> 32);
5693 s[3] = (unsigned EMUSHORT) (d[0] >> 48);
5696 /* Convert target double to E-type. */
5698 /* Output E-type to REAL_VALUE_TYPE. */
5704 /* Convert target computer unsigned 64-bit integer to e-type.
5705 The endian-ness of DImode follows the convention for integers,
5706 so we use WORDS_BIG_ENDIAN here, not REAL_WORDS_BIG_ENDIAN. */
5710 unsigned EMUSHORT *di; /* Address of the 64-bit int. */
5711 unsigned EMUSHORT *e;
5713 unsigned EMUSHORT yi[NI];
5717 if (WORDS_BIG_ENDIAN)
5719 for (k = M; k < M + 4; k++)
5724 for (k = M + 3; k >= M; k--)
5727 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
5728 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
5729 ecleaz (yi); /* it was zero */
5731 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
5735 /* Convert target computer signed 64-bit integer to e-type. */
5739 unsigned EMUSHORT *di; /* Address of the 64-bit int. */
5740 unsigned EMUSHORT *e;
5742 unsigned EMULONG acc;
5743 unsigned EMUSHORT yi[NI];
5744 unsigned EMUSHORT carry;
5748 if (WORDS_BIG_ENDIAN)
5750 for (k = M; k < M + 4; k++)
5755 for (k = M + 3; k >= M; k--)
5758 /* Take absolute value */
5764 for (k = M + 3; k >= M; k--)
5766 acc = (unsigned EMULONG) (~yi[k] & 0xffff) + carry;
5773 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
5774 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
5775 ecleaz (yi); /* it was zero */
5777 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
5784 /* Convert e-type to unsigned 64-bit int. */
5788 unsigned EMUSHORT *x;
5789 unsigned EMUSHORT *i;
5791 unsigned EMUSHORT xi[NI];
5800 k = (int) xi[E] - (EXONE - 1);
5803 for (j = 0; j < 4; j++)
5809 for (j = 0; j < 4; j++)
5812 warning ("overflow on truncation to integer");
5817 /* Shift more than 16 bits: first shift up k-16 mod 16,
5818 then shift up by 16's. */
5819 j = k - ((k >> 4) << 4);
5823 if (WORDS_BIG_ENDIAN)
5834 if (WORDS_BIG_ENDIAN)
5839 while ((k -= 16) > 0);
5843 /* shift not more than 16 bits */
5848 if (WORDS_BIG_ENDIAN)
5867 /* Convert e-type to signed 64-bit int. */
5871 unsigned EMUSHORT *x;
5872 unsigned EMUSHORT *i;
5874 unsigned EMULONG acc;
5875 unsigned EMUSHORT xi[NI];
5876 unsigned EMUSHORT carry;
5877 unsigned EMUSHORT *isave;
5881 k = (int) xi[E] - (EXONE - 1);
5884 for (j = 0; j < 4; j++)
5890 for (j = 0; j < 4; j++)
5893 warning ("overflow on truncation to integer");
5899 /* Shift more than 16 bits: first shift up k-16 mod 16,
5900 then shift up by 16's. */
5901 j = k - ((k >> 4) << 4);
5905 if (WORDS_BIG_ENDIAN)
5916 if (WORDS_BIG_ENDIAN)
5921 while ((k -= 16) > 0);
5925 /* shift not more than 16 bits */
5928 if (WORDS_BIG_ENDIAN)
5944 /* Negate if negative */
5948 if (WORDS_BIG_ENDIAN)
5950 for (k = 0; k < 4; k++)
5952 acc = (unsigned EMULONG) (~(*isave) & 0xffff) + carry;
5953 if (WORDS_BIG_ENDIAN)
5965 /* Longhand square root routine. */
5968 static int esqinited = 0;
5969 static unsigned short sqrndbit[NI];
5973 unsigned EMUSHORT *x, *y;
5975 unsigned EMUSHORT temp[NI], num[NI], sq[NI], xx[NI];
5977 int i, j, k, n, nlups;
5982 sqrndbit[NI - 2] = 1;
5985 /* Check for arg <= 0 */
5986 i = ecmp (x, ezero);
5991 mtherr ("esqrt", DOMAIN);
6007 /* Bring in the arg and renormalize if it is denormal. */
6009 m = (EMULONG) xx[1]; /* local long word exponent */
6013 /* Divide exponent by 2 */
6015 exp = (unsigned short) ((m / 2) + 0x3ffe);
6017 /* Adjust if exponent odd */
6027 n = 8; /* get 8 bits of result per inner loop */
6033 /* bring in next word of arg */
6035 num[NI - 1] = xx[j + 3];
6036 /* Do additional bit on last outer loop, for roundoff. */
6039 for (i = 0; i < n; i++)
6041 /* Next 2 bits of arg */
6044 /* Shift up answer */
6046 /* Make trial divisor */
6047 for (k = 0; k < NI; k++)
6050 eaddm (sqrndbit, temp);
6051 /* Subtract and insert answer bit if it goes in */
6052 if (ecmpm (temp, num) <= 0)
6062 /* Adjust for extra, roundoff loop done. */
6063 exp += (NBITS - 1) - rndprc;
6065 /* Sticky bit = 1 if the remainder is nonzero. */
6067 for (i = 3; i < NI; i++)
6070 /* Renormalize and round off. */
6071 emdnorm (sq, k, 0, exp, 64);
6074 #endif /* EMU_NON_COMPILE not defined */
6076 /* Return the binary precision of the significand for a given
6077 floating point mode. The mode can hold an integer value
6078 that many bits wide, without losing any bits. */
6081 significand_size (mode)
6082 enum machine_mode mode;
6091 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
6094 #if TARGET_FLOAT_FORMAT == IBM_FLOAT_FORMAT
6097 #if TARGET_FLOAT_FORMAT == VAX_FLOAT_FORMAT