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 if ((exp <= 0) && (rndprc != 64) && (rndprc != NBITS))
2373 lost |= s[NI - 1] & 1;
2376 /* Clear out all bits below the rounding bit,
2377 remembering in r if any were nonzero. */
2391 if ((r & rmbit) != 0)
2396 { /* round to even */
2397 if ((s[re] & rebit) == 0)
2409 if ((exp <= 0) && (rndprc != 64) && (rndprc != NBITS))
2414 { /* overflow on roundoff */
2427 for (i = 2; i < NI - 1; i++)
2430 warning ("floating point overflow");
2434 for (i = M + 1; i < NI - 1; i++)
2437 if ((rndprc < 64) || (rndprc == 113))
2452 s[1] = (unsigned EMUSHORT) exp;
2455 /* Subtract. C = B - A, all e type numbers. */
2457 static int subflg = 0;
2461 unsigned EMUSHORT *a, *b, *c;
2475 /* Infinity minus infinity is a NaN.
2476 Test for subtracting infinities of the same sign. */
2477 if (eisinf (a) && eisinf (b)
2478 && ((eisneg (a) ^ eisneg (b)) == 0))
2480 mtherr ("esub", INVALID);
2489 /* Add. C = A + B, all e type. */
2493 unsigned EMUSHORT *a, *b, *c;
2497 /* NaN plus anything is a NaN. */
2508 /* Infinity minus infinity is a NaN.
2509 Test for adding infinities of opposite signs. */
2510 if (eisinf (a) && eisinf (b)
2511 && ((eisneg (a) ^ eisneg (b)) != 0))
2513 mtherr ("esub", INVALID);
2522 /* Arithmetic common to both addition and subtraction. */
2526 unsigned EMUSHORT *a, *b, *c;
2528 unsigned EMUSHORT ai[NI], bi[NI], ci[NI];
2530 EMULONG lt, lta, ltb;
2551 /* compare exponents */
2556 { /* put the larger number in bi */
2566 if (lt < (EMULONG) (-NBITS - 1))
2567 goto done; /* answer same as larger addend */
2569 lost = eshift (ai, k); /* shift the smaller number down */
2573 /* exponents were the same, so must compare significands */
2576 { /* the numbers are identical in magnitude */
2577 /* if different signs, result is zero */
2583 /* if same sign, result is double */
2584 /* double denomalized tiny number */
2585 if ((bi[E] == 0) && ((bi[3] & 0x8000) == 0))
2590 /* add 1 to exponent unless both are zero! */
2591 for (j = 1; j < NI - 1; j++)
2595 /* This could overflow, but let emovo take care of that. */
2600 bi[E] = (unsigned EMUSHORT) ltb;
2604 { /* put the larger number in bi */
2620 emdnorm (bi, lost, subflg, ltb, 64);
2626 /* Divide: C = B/A, all e type. */
2630 unsigned EMUSHORT *a, *b, *c;
2632 unsigned EMUSHORT ai[NI], bi[NI];
2634 EMULONG lt, lta, ltb;
2637 /* Return any NaN input. */
2648 /* Zero over zero, or infinity over infinity, is a NaN. */
2649 if (((ecmp (a, ezero) == 0) && (ecmp (b, ezero) == 0))
2650 || (eisinf (a) && eisinf (b)))
2652 mtherr ("ediv", INVALID);
2653 enan (c, eisneg (a) ^ eisneg (b));
2657 /* Infinity over anything else is infinity. */
2661 if (eisneg (a) ^ eisneg (b))
2662 *(c + (NE - 1)) = 0x8000;
2664 *(c + (NE - 1)) = 0;
2668 /* Anything else over infinity is zero. */
2680 { /* See if numerator is zero. */
2681 for (i = 1; i < NI - 1; i++)
2685 ltb -= enormlz (bi);
2695 { /* possible divide by zero */
2696 for (i = 1; i < NI - 1; i++)
2700 lta -= enormlz (ai);
2705 *(c + (NE - 1)) = 0;
2707 *(c + (NE - 1)) = 0x8000;
2708 /* Divide by zero is not an invalid operation.
2709 It is a divide-by-zero operation! */
2711 mtherr ("ediv", SING);
2717 /* calculate exponent */
2718 lt = ltb - lta + EXONE;
2719 emdnorm (bi, i, 0, lt, 64);
2728 /* Multiply e-types A and B, return e-type product C. */
2732 unsigned EMUSHORT *a, *b, *c;
2734 unsigned EMUSHORT ai[NI], bi[NI];
2736 EMULONG lt, lta, ltb;
2739 /* NaN times anything is the same NaN. */
2750 /* Zero times infinity is a NaN. */
2751 if ((eisinf (a) && (ecmp (b, ezero) == 0))
2752 || (eisinf (b) && (ecmp (a, ezero) == 0)))
2754 mtherr ("emul", INVALID);
2755 enan (c, eisneg (a) ^ eisneg (b));
2759 /* Infinity times anything else is infinity. */
2761 if (eisinf (a) || eisinf (b))
2763 if (eisneg (a) ^ eisneg (b))
2764 *(c + (NE - 1)) = 0x8000;
2766 *(c + (NE - 1)) = 0;
2777 for (i = 1; i < NI - 1; i++)
2781 lta -= enormlz (ai);
2792 for (i = 1; i < NI - 1; i++)
2796 ltb -= enormlz (bi);
2805 /* Multiply significands */
2807 /* calculate exponent */
2808 lt = lta + ltb - (EXONE - 1);
2809 emdnorm (bi, j, 0, lt, 64);
2810 /* calculate sign of product */
2818 /* Convert double precision PE to e-type Y. */
2822 unsigned EMUSHORT *pe, *y;
2831 ibmtoe (pe, y, DFmode);
2834 register unsigned EMUSHORT r;
2835 register unsigned EMUSHORT *e, *p;
2836 unsigned EMUSHORT yy[NI];
2840 denorm = 0; /* flag if denormalized number */
2842 if (! REAL_WORDS_BIG_ENDIAN)
2848 yy[M] = (r & 0x0f) | 0x10;
2849 r &= ~0x800f; /* strip sign and 4 significand bits */
2854 if (! REAL_WORDS_BIG_ENDIAN)
2856 if (((pe[3] & 0xf) != 0) || (pe[2] != 0)
2857 || (pe[1] != 0) || (pe[0] != 0))
2859 enan (y, yy[0] != 0);
2865 if (((pe[0] & 0xf) != 0) || (pe[1] != 0)
2866 || (pe[2] != 0) || (pe[3] != 0))
2868 enan (y, yy[0] != 0);
2879 #endif /* INFINITY */
2881 /* If zero exponent, then the significand is denormalized.
2882 So take back the understood high significand bit. */
2893 if (! REAL_WORDS_BIG_ENDIAN)
2909 { /* if zero exponent, then normalize the significand */
2910 if ((k = enormlz (yy)) > NBITS)
2913 yy[E] -= (unsigned EMUSHORT) (k - 1);
2916 #endif /* not IBM */
2917 #endif /* not DEC */
2920 /* Convert double extended precision float PE to e type Y. */
2924 unsigned EMUSHORT *pe, *y;
2926 unsigned EMUSHORT yy[NI];
2927 unsigned EMUSHORT *e, *p, *q;
2932 for (i = 0; i < NE - 5; i++)
2934 /* This precision is not ordinarily supported on DEC or IBM. */
2936 for (i = 0; i < 5; i++)
2940 p = &yy[0] + (NE - 1);
2943 for (i = 0; i < 5; i++)
2947 if (! REAL_WORDS_BIG_ENDIAN)
2949 for (i = 0; i < 5; i++)
2954 p = &yy[0] + (NE - 1);
2957 for (i = 0; i < 4; i++)
2962 /* Point to the exponent field and check max exponent cases. */
2967 if (! REAL_WORDS_BIG_ENDIAN)
2969 for (i = 0; i < 4; i++)
2971 if ((i != 3 && pe[i] != 0)
2972 /* Anything but 0x8000 here, including 0, is a NaN. */
2973 || (i == 3 && pe[i] != 0x8000))
2975 enan (y, (*p & 0x8000) != 0);
2982 for (i = 1; i <= 4; i++)
2986 enan (y, (*p & 0x8000) != 0);
2998 #endif /* INFINITY */
3001 for (i = 0; i < NE; i++)
3005 /* Convert 128-bit long double precision float PE to e type Y. */
3009 unsigned EMUSHORT *pe, *y;
3011 register unsigned EMUSHORT r;
3012 unsigned EMUSHORT *e, *p;
3013 unsigned EMUSHORT yy[NI];
3020 if (! REAL_WORDS_BIG_ENDIAN)
3032 if (! REAL_WORDS_BIG_ENDIAN)
3034 for (i = 0; i < 7; i++)
3038 enan (y, yy[0] != 0);
3045 for (i = 1; i < 8; i++)
3049 enan (y, yy[0] != 0);
3061 #endif /* INFINITY */
3065 if (! REAL_WORDS_BIG_ENDIAN)
3067 for (i = 0; i < 7; i++)
3073 for (i = 0; i < 7; i++)
3077 /* If denormal, remove the implied bit; else shift down 1. */
3090 /* Convert single precision float PE to e type Y. */
3094 unsigned EMUSHORT *pe, *y;
3098 ibmtoe (pe, y, SFmode);
3101 register unsigned EMUSHORT r;
3102 register unsigned EMUSHORT *e, *p;
3103 unsigned EMUSHORT yy[NI];
3107 denorm = 0; /* flag if denormalized number */
3110 if (! REAL_WORDS_BIG_ENDIAN)
3120 yy[M] = (r & 0x7f) | 0200;
3121 r &= ~0x807f; /* strip sign and 7 significand bits */
3126 if (REAL_WORDS_BIG_ENDIAN)
3128 if (((pe[0] & 0x7f) != 0) || (pe[1] != 0))
3130 enan (y, yy[0] != 0);
3136 if (((pe[1] & 0x7f) != 0) || (pe[0] != 0))
3138 enan (y, yy[0] != 0);
3149 #endif /* INFINITY */
3151 /* If zero exponent, then the significand is denormalized.
3152 So take back the understood high significand bit. */
3165 if (! REAL_WORDS_BIG_ENDIAN)
3175 { /* if zero exponent, then normalize the significand */
3176 if ((k = enormlz (yy)) > NBITS)
3179 yy[E] -= (unsigned EMUSHORT) (k - 1);
3182 #endif /* not IBM */
3185 /* Convert e-type X to IEEE 128-bit long double format E. */
3189 unsigned EMUSHORT *x, *e;
3191 unsigned EMUSHORT xi[NI];
3198 make_nan (e, eisneg (x), TFmode);
3203 exp = (EMULONG) xi[E];
3208 /* round off to nearest or even */
3211 emdnorm (xi, 0, 0, exp, 64);
3217 /* Convert exploded e-type X, that has already been rounded to
3218 113-bit precision, to IEEE 128-bit long double format Y. */
3222 unsigned EMUSHORT *a, *b;
3224 register unsigned EMUSHORT *p, *q;
3225 unsigned EMUSHORT i;
3230 make_nan (b, eiisneg (a), TFmode);
3235 if (REAL_WORDS_BIG_ENDIAN)
3238 q = b + 7; /* point to output exponent */
3240 /* If not denormal, delete the implied bit. */
3245 /* combine sign and exponent */
3247 if (REAL_WORDS_BIG_ENDIAN)
3250 *q++ = *p++ | 0x8000;
3257 *q-- = *p++ | 0x8000;
3261 /* skip over guard word */
3263 /* move the significand */
3264 if (REAL_WORDS_BIG_ENDIAN)
3266 for (i = 0; i < 7; i++)
3271 for (i = 0; i < 7; i++)
3276 /* Convert e-type X to IEEE double extended format E. */
3280 unsigned EMUSHORT *x, *e;
3282 unsigned EMUSHORT xi[NI];
3289 make_nan (e, eisneg (x), XFmode);
3294 /* adjust exponent for offset */
3295 exp = (EMULONG) xi[E];
3300 /* round off to nearest or even */
3303 emdnorm (xi, 0, 0, exp, 64);
3309 /* Convert exploded e-type X, that has already been rounded to
3310 64-bit precision, to IEEE double extended format Y. */
3314 unsigned EMUSHORT *a, *b;
3316 register unsigned EMUSHORT *p, *q;
3317 unsigned EMUSHORT i;
3322 make_nan (b, eiisneg (a), XFmode);
3334 if (REAL_WORDS_BIG_ENDIAN)
3338 q = b + 4; /* point to output exponent */
3339 #if LONG_DOUBLE_TYPE_SIZE == 96
3340 /* Clear the last two bytes of 12-byte Intel format */
3346 /* combine sign and exponent */
3350 *q++ = *p++ | 0x8000;
3357 *q-- = *p++ | 0x8000;
3362 if (REAL_WORDS_BIG_ENDIAN)
3365 *q++ = *p++ | 0x8000;
3373 *q-- = *p++ | 0x8000;
3378 /* skip over guard word */
3380 /* move the significand */
3382 for (i = 0; i < 4; i++)
3386 for (i = 0; i < 4; i++)
3390 if (REAL_WORDS_BIG_ENDIAN)
3392 for (i = 0; i < 4; i++)
3400 /* Intel long double infinity significand. */
3408 for (i = 0; i < 4; i++)
3414 /* e type to double precision. */
3417 /* Convert e-type X to DEC-format double E. */
3421 unsigned EMUSHORT *x, *e;
3423 etodec (x, e); /* see etodec.c */
3426 /* Convert exploded e-type X, that has already been rounded to
3427 56-bit double precision, to DEC double Y. */
3431 unsigned EMUSHORT *x, *y;
3438 /* Convert e-type X to IBM 370-format double E. */
3442 unsigned EMUSHORT *x, *e;
3444 etoibm (x, e, DFmode);
3447 /* Convert exploded e-type X, that has already been rounded to
3448 56-bit precision, to IBM 370 double Y. */
3452 unsigned EMUSHORT *x, *y;
3454 toibm (x, y, DFmode);
3457 #else /* it's neither DEC nor IBM */
3459 /* Convert e-type X to IEEE double E. */
3463 unsigned EMUSHORT *x, *e;
3465 unsigned EMUSHORT xi[NI];
3472 make_nan (e, eisneg (x), DFmode);
3477 /* adjust exponent for offsets */
3478 exp = (EMULONG) xi[E] - (EXONE - 0x3ff);
3483 /* round off to nearest or even */
3486 emdnorm (xi, 0, 0, exp, 64);
3492 /* Convert exploded e-type X, that has already been rounded to
3493 53-bit precision, to IEEE double Y. */
3497 unsigned EMUSHORT *x, *y;
3499 unsigned EMUSHORT i;
3500 unsigned EMUSHORT *p;
3505 make_nan (y, eiisneg (x), DFmode);
3511 if (! REAL_WORDS_BIG_ENDIAN)
3514 *y = 0; /* output high order */
3516 *y = 0x8000; /* output sign bit */
3519 if (i >= (unsigned int) 2047)
3520 { /* Saturate at largest number less than infinity. */
3523 if (! REAL_WORDS_BIG_ENDIAN)
3537 *y |= (unsigned EMUSHORT) 0x7fef;
3538 if (! REAL_WORDS_BIG_ENDIAN)
3563 i |= *p++ & (unsigned EMUSHORT) 0x0f; /* *p = xi[M] */
3564 *y |= (unsigned EMUSHORT) i; /* high order output already has sign bit set */
3565 if (! REAL_WORDS_BIG_ENDIAN)
3580 #endif /* not IBM */
3581 #endif /* not DEC */
3585 /* e type to single precision. */
3588 /* Convert e-type X to IBM 370 float E. */
3592 unsigned EMUSHORT *x, *e;
3594 etoibm (x, e, SFmode);
3597 /* Convert exploded e-type X, that has already been rounded to
3598 float precision, to IBM 370 float Y. */
3602 unsigned EMUSHORT *x, *y;
3604 toibm (x, y, SFmode);
3608 /* Convert e-type X to IEEE float E. DEC float is the same as IEEE float. */
3612 unsigned EMUSHORT *x, *e;
3615 unsigned EMUSHORT xi[NI];
3621 make_nan (e, eisneg (x), SFmode);
3626 /* adjust exponent for offsets */
3627 exp = (EMULONG) xi[E] - (EXONE - 0177);
3632 /* round off to nearest or even */
3635 emdnorm (xi, 0, 0, exp, 64);
3641 /* Convert exploded e-type X, that has already been rounded to
3642 float precision, to IEEE float Y. */
3646 unsigned EMUSHORT *x, *y;
3648 unsigned EMUSHORT i;
3649 unsigned EMUSHORT *p;
3654 make_nan (y, eiisneg (x), SFmode);
3660 if (! REAL_WORDS_BIG_ENDIAN)
3666 *y = 0; /* output high order */
3668 *y = 0x8000; /* output sign bit */
3671 /* Handle overflow cases. */
3675 *y |= (unsigned EMUSHORT) 0x7f80;
3680 if (! REAL_WORDS_BIG_ENDIAN)
3688 #else /* no INFINITY */
3689 *y |= (unsigned EMUSHORT) 0x7f7f;
3694 if (! REAL_WORDS_BIG_ENDIAN)
3705 #endif /* no INFINITY */
3717 i |= *p++ & (unsigned EMUSHORT) 0x7f; /* *p = xi[M] */
3718 /* High order output already has sign bit set. */
3724 if (! REAL_WORDS_BIG_ENDIAN)
3733 #endif /* not IBM */
3735 /* Compare two e type numbers.
3739 -2 if either a or b is a NaN. */
3743 unsigned EMUSHORT *a, *b;
3745 unsigned EMUSHORT ai[NI], bi[NI];
3746 register unsigned EMUSHORT *p, *q;
3751 if (eisnan (a) || eisnan (b))
3760 { /* the signs are different */
3762 for (i = 1; i < NI - 1; i++)
3776 /* both are the same sign */
3791 return (0); /* equality */
3795 if (*(--p) > *(--q))
3796 return (msign); /* p is bigger */
3798 return (-msign); /* p is littler */
3801 /* Find e-type nearest integer to X, as floor (X + 0.5). */
3805 unsigned EMUSHORT *x, *y;
3811 /* Convert HOST_WIDE_INT LP to e type Y. */
3816 unsigned EMUSHORT *y;
3818 unsigned EMUSHORT yi[NI];
3819 unsigned HOST_WIDE_INT ll;
3825 /* make it positive */
3826 ll = (unsigned HOST_WIDE_INT) (-(*lp));
3827 yi[0] = 0xffff; /* put correct sign in the e type number */
3831 ll = (unsigned HOST_WIDE_INT) (*lp);
3833 /* move the long integer to yi significand area */
3834 #if HOST_BITS_PER_WIDE_INT == 64
3835 yi[M] = (unsigned EMUSHORT) (ll >> 48);
3836 yi[M + 1] = (unsigned EMUSHORT) (ll >> 32);
3837 yi[M + 2] = (unsigned EMUSHORT) (ll >> 16);
3838 yi[M + 3] = (unsigned EMUSHORT) ll;
3839 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
3841 yi[M] = (unsigned EMUSHORT) (ll >> 16);
3842 yi[M + 1] = (unsigned EMUSHORT) ll;
3843 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
3846 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
3847 ecleaz (yi); /* it was zero */
3849 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
3850 emovo (yi, y); /* output the answer */
3853 /* Convert unsigned HOST_WIDE_INT LP to e type Y. */
3857 unsigned HOST_WIDE_INT *lp;
3858 unsigned EMUSHORT *y;
3860 unsigned EMUSHORT yi[NI];
3861 unsigned HOST_WIDE_INT ll;
3867 /* move the long integer to ayi significand area */
3868 #if HOST_BITS_PER_WIDE_INT == 64
3869 yi[M] = (unsigned EMUSHORT) (ll >> 48);
3870 yi[M + 1] = (unsigned EMUSHORT) (ll >> 32);
3871 yi[M + 2] = (unsigned EMUSHORT) (ll >> 16);
3872 yi[M + 3] = (unsigned EMUSHORT) ll;
3873 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
3875 yi[M] = (unsigned EMUSHORT) (ll >> 16);
3876 yi[M + 1] = (unsigned EMUSHORT) ll;
3877 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
3880 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
3881 ecleaz (yi); /* it was zero */
3883 yi[E] -= (unsigned EMUSHORT) k; /* subtract shift count from exponent */
3884 emovo (yi, y); /* output the answer */
3888 /* Find signed HOST_WIDE_INT integer I and floating point fractional
3889 part FRAC of e-type (packed internal format) floating point input X.
3890 The integer output I has the sign of the input, except that
3891 positive overflow is permitted if FIXUNS_TRUNC_LIKE_FIX_TRUNC.
3892 The output e-type fraction FRAC is the positive fractional
3897 unsigned EMUSHORT *x;
3899 unsigned EMUSHORT *frac;
3901 unsigned EMUSHORT xi[NI];
3903 unsigned HOST_WIDE_INT ll;
3906 k = (int) xi[E] - (EXONE - 1);
3909 /* if exponent <= 0, integer = 0 and real output is fraction */
3914 if (k > (HOST_BITS_PER_WIDE_INT - 1))
3916 /* long integer overflow: output large integer
3917 and correct fraction */
3919 *i = ((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1);
3922 #ifdef FIXUNS_TRUNC_LIKE_FIX_TRUNC
3923 /* In this case, let it overflow and convert as if unsigned. */
3924 euifrac (x, &ll, frac);
3925 *i = (HOST_WIDE_INT) ll;
3928 /* In other cases, return the largest positive integer. */
3929 *i = (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1;
3934 warning ("overflow on truncation to integer");
3938 /* Shift more than 16 bits: first shift up k-16 mod 16,
3939 then shift up by 16's. */
3940 j = k - ((k >> 4) << 4);
3947 ll = (ll << 16) | xi[M];
3949 while ((k -= 16) > 0);
3956 /* shift not more than 16 bits */
3958 *i = (HOST_WIDE_INT) xi[M] & 0xffff;
3965 if ((k = enormlz (xi)) > NBITS)
3968 xi[E] -= (unsigned EMUSHORT) k;
3974 /* Find unsigned HOST_WIDE_INT integer I and floating point fractional part
3975 FRAC of e-type X. A negative input yields integer output = 0 but
3976 correct fraction. */
3979 euifrac (x, i, frac)
3980 unsigned EMUSHORT *x;
3981 unsigned HOST_WIDE_INT *i;
3982 unsigned EMUSHORT *frac;
3984 unsigned HOST_WIDE_INT ll;
3985 unsigned EMUSHORT xi[NI];
3989 k = (int) xi[E] - (EXONE - 1);
3992 /* if exponent <= 0, integer = 0 and argument is fraction */
3997 if (k > HOST_BITS_PER_WIDE_INT)
3999 /* Long integer overflow: output large integer
4000 and correct fraction.
4001 Note, the BSD microvax compiler says that ~(0UL)
4002 is a syntax error. */
4006 warning ("overflow on truncation to unsigned integer");
4010 /* Shift more than 16 bits: first shift up k-16 mod 16,
4011 then shift up by 16's. */
4012 j = k - ((k >> 4) << 4);
4019 ll = (ll << 16) | xi[M];
4021 while ((k -= 16) > 0);
4026 /* shift not more than 16 bits */
4028 *i = (HOST_WIDE_INT) xi[M] & 0xffff;
4031 if (xi[0]) /* A negative value yields unsigned integer 0. */
4037 if ((k = enormlz (xi)) > NBITS)
4040 xi[E] -= (unsigned EMUSHORT) k;
4045 /* Shift the significand of exploded e-type X up or down by SC bits. */
4049 unsigned EMUSHORT *x;
4052 unsigned EMUSHORT lost;
4053 unsigned EMUSHORT *p;
4066 lost |= *p; /* remember lost bits */
4107 return ((int) lost);
4110 /* Shift normalize the significand area of exploded e-type X.
4111 Return the shift count (up = positive). */
4115 unsigned EMUSHORT x[];
4117 register unsigned EMUSHORT *p;
4126 return (0); /* already normalized */
4132 /* With guard word, there are NBITS+16 bits available.
4133 Return true if all are zero. */
4137 /* see if high byte is zero */
4138 while ((*p & 0xff00) == 0)
4143 /* now shift 1 bit at a time */
4144 while ((*p & 0x8000) == 0)
4150 mtherr ("enormlz", UNDERFLOW);
4156 /* Normalize by shifting down out of the high guard word
4157 of the significand */
4172 mtherr ("enormlz", OVERFLOW);
4179 /* Powers of ten used in decimal <-> binary conversions. */
4184 #if LONG_DOUBLE_TYPE_SIZE == 128
4185 static unsigned EMUSHORT etens[NTEN + 1][NE] =
4187 {0x6576, 0x4a92, 0x804a, 0x153f,
4188 0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
4189 {0x6a32, 0xce52, 0x329a, 0x28ce,
4190 0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
4191 {0x526c, 0x50ce, 0xf18b, 0x3d28,
4192 0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
4193 {0x9c66, 0x58f8, 0xbc50, 0x5c54,
4194 0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
4195 {0x851e, 0xeab7, 0x98fe, 0x901b,
4196 0xddbb, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
4197 {0x0235, 0x0137, 0x36b1, 0x336c,
4198 0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
4199 {0x50f8, 0x25fb, 0xc76b, 0x6b71,
4200 0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
4201 {0x0000, 0x0000, 0x0000, 0x0000,
4202 0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
4203 {0x0000, 0x0000, 0x0000, 0x0000,
4204 0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
4205 {0x0000, 0x0000, 0x0000, 0x0000,
4206 0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
4207 {0x0000, 0x0000, 0x0000, 0x0000,
4208 0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
4209 {0x0000, 0x0000, 0x0000, 0x0000,
4210 0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
4211 {0x0000, 0x0000, 0x0000, 0x0000,
4212 0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
4215 static unsigned EMUSHORT emtens[NTEN + 1][NE] =
4217 {0x2030, 0xcffc, 0xa1c3, 0x8123,
4218 0x2de3, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
4219 {0x8264, 0xd2cb, 0xf2ea, 0x12d4,
4220 0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
4221 {0xf53f, 0xf698, 0x6bd3, 0x0158,
4222 0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
4223 {0xe731, 0x04d4, 0xe3f2, 0xd332,
4224 0x7132, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
4225 {0xa23e, 0x5308, 0xfefb, 0x1155,
4226 0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
4227 {0xe26d, 0xdbde, 0xd05d, 0xb3f6,
4228 0xac7c, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
4229 {0x2a20, 0x6224, 0x47b3, 0x98d7,
4230 0x3f23, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
4231 {0x0b5b, 0x4af2, 0xa581, 0x18ed,
4232 0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
4233 {0xbf71, 0xa9b3, 0x7989, 0xbe68,
4234 0x4c2e, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
4235 {0x3d4d, 0x7c3d, 0x36ba, 0x0d2b,
4236 0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
4237 {0xc155, 0xa4a8, 0x404e, 0x6113,
4238 0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
4239 {0xd70a, 0x70a3, 0x0a3d, 0xa3d7,
4240 0x3d70, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
4241 {0xcccd, 0xcccc, 0xcccc, 0xcccc,
4242 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
4245 /* LONG_DOUBLE_TYPE_SIZE is other than 128 */
4246 static unsigned EMUSHORT etens[NTEN + 1][NE] =
4248 {0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
4249 {0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
4250 {0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
4251 {0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
4252 {0xddbc, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
4253 {0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
4254 {0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
4255 {0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
4256 {0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
4257 {0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
4258 {0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
4259 {0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
4260 {0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
4263 static unsigned EMUSHORT emtens[NTEN + 1][NE] =
4265 {0x2de4, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
4266 {0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
4267 {0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
4268 {0x7133, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
4269 {0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
4270 {0xac7d, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
4271 {0x3f24, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
4272 {0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
4273 {0x4c2f, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
4274 {0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
4275 {0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
4276 {0x3d71, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
4277 {0xcccd, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
4281 /* Convert float value X to ASCII string STRING with NDIG digits after
4282 the decimal point. */
4285 e24toasc (x, string, ndigs)
4286 unsigned EMUSHORT x[];
4290 unsigned EMUSHORT w[NI];
4293 etoasc (w, string, ndigs);
4296 /* Convert double value X to ASCII string STRING with NDIG digits after
4297 the decimal point. */
4300 e53toasc (x, string, ndigs)
4301 unsigned EMUSHORT x[];
4305 unsigned EMUSHORT w[NI];
4308 etoasc (w, string, ndigs);
4311 /* Convert double extended value X to ASCII string STRING with NDIG digits
4312 after the decimal point. */
4315 e64toasc (x, string, ndigs)
4316 unsigned EMUSHORT x[];
4320 unsigned EMUSHORT w[NI];
4323 etoasc (w, string, ndigs);
4326 /* Convert 128-bit long double value X to ASCII string STRING with NDIG digits
4327 after the decimal point. */
4330 e113toasc (x, string, ndigs)
4331 unsigned EMUSHORT x[];
4335 unsigned EMUSHORT w[NI];
4338 etoasc (w, string, ndigs);
4341 /* Convert e-type X to ASCII string STRING with NDIGS digits after
4342 the decimal point. */
4344 static char wstring[80]; /* working storage for ASCII output */
4347 etoasc (x, string, ndigs)
4348 unsigned EMUSHORT x[];
4353 unsigned EMUSHORT y[NI], t[NI], u[NI], w[NI];
4354 unsigned EMUSHORT *p, *r, *ten;
4355 unsigned EMUSHORT sign;
4356 int i, j, k, expon, rndsav;
4358 unsigned EMUSHORT m;
4369 sprintf (wstring, " NaN ");
4373 rndprc = NBITS; /* set to full precision */
4374 emov (x, y); /* retain external format */
4375 if (y[NE - 1] & 0x8000)
4378 y[NE - 1] &= 0x7fff;
4385 ten = &etens[NTEN][0];
4387 /* Test for zero exponent */
4390 for (k = 0; k < NE - 1; k++)
4393 goto tnzro; /* denormalized number */
4395 goto isone; /* valid all zeros */
4399 /* Test for infinity. */
4400 if (y[NE - 1] == 0x7fff)
4403 sprintf (wstring, " -Infinity ");
4405 sprintf (wstring, " Infinity ");
4409 /* Test for exponent nonzero but significand denormalized.
4410 * This is an error condition.
4412 if ((y[NE - 1] != 0) && ((y[NE - 2] & 0x8000) == 0))
4414 mtherr ("etoasc", DOMAIN);
4415 sprintf (wstring, "NaN");
4419 /* Compare to 1.0 */
4428 { /* Number is greater than 1 */
4429 /* Convert significand to an integer and strip trailing decimal zeros. */
4431 u[NE - 1] = EXONE + NBITS - 1;
4433 p = &etens[NTEN - 4][0];
4439 for (j = 0; j < NE - 1; j++)
4452 /* Rescale from integer significand */
4453 u[NE - 1] += y[NE - 1] - (unsigned int) (EXONE + NBITS - 1);
4455 /* Find power of 10 */
4459 /* An unordered compare result shouldn't happen here. */
4460 while (ecmp (ten, u) <= 0)
4462 if (ecmp (p, u) <= 0)
4475 { /* Number is less than 1.0 */
4476 /* Pad significand with trailing decimal zeros. */
4479 while ((y[NE - 2] & 0x8000) == 0)
4488 for (i = 0; i < NDEC + 1; i++)
4490 if ((w[NI - 1] & 0x7) != 0)
4492 /* multiply by 10 */
4505 if (eone[NE - 1] <= u[1])
4517 while (ecmp (eone, w) > 0)
4519 if (ecmp (p, w) >= 0)
4534 /* Find the first (leading) digit. */
4540 digit = equot[NI - 1];
4541 while ((digit == 0) && (ecmp (y, ezero) != 0))
4549 digit = equot[NI - 1];
4557 /* Examine number of digits requested by caller. */
4575 *s++ = (char)digit + '0';
4578 /* Generate digits after the decimal point. */
4579 for (k = 0; k <= ndigs; k++)
4581 /* multiply current number by 10, without normalizing */
4588 *s++ = (char) equot[NI - 1] + '0';
4590 digit = equot[NI - 1];
4593 /* round off the ASCII string */
4596 /* Test for critical rounding case in ASCII output. */
4600 if (ecmp (t, ezero) != 0)
4601 goto roun; /* round to nearest */
4602 if ((*(s - 1) & 1) == 0)
4603 goto doexp; /* round to even */
4605 /* Round up and propagate carry-outs */
4609 /* Carry out to most significant digit? */
4616 /* Most significant digit carries to 10? */
4624 /* Round up and carry out from less significant digits */
4636 sprintf (ss, "e+%d", expon);
4638 sprintf (ss, "e%d", expon);
4640 sprintf (ss, "e%d", expon);
4643 /* copy out the working string */
4646 while (*ss == ' ') /* strip possible leading space */
4648 while ((*s++ = *ss++) != '\0')
4653 /* Convert ASCII string to floating point.
4655 Numeric input is a free format decimal number of any length, with
4656 or without decimal point. Entering E after the number followed by an
4657 integer number causes the second number to be interpreted as a power of
4658 10 to be multiplied by the first number (i.e., "scientific" notation). */
4660 /* Convert ASCII string S to single precision float value Y. */
4665 unsigned EMUSHORT *y;
4671 /* Convert ASCII string S to double precision value Y. */
4676 unsigned EMUSHORT *y;
4678 #if defined(DEC) || defined(IBM)
4686 /* Convert ASCII string S to double extended value Y. */
4691 unsigned EMUSHORT *y;
4696 /* Convert ASCII string S to 128-bit long double Y. */
4701 unsigned EMUSHORT *y;
4703 asctoeg (s, y, 113);
4706 /* Convert ASCII string S to e type Y. */
4711 unsigned EMUSHORT *y;
4713 asctoeg (s, y, NBITS);
4716 /* Convert ASCII string SS to e type Y, with a specified rounding precision
4720 asctoeg (ss, y, oprec)
4722 unsigned EMUSHORT *y;
4725 unsigned EMUSHORT yy[NI], xt[NI], tt[NI];
4726 int esign, decflg, sgnflg, nexp, exp, prec, lost;
4727 int k, trail, c, rndsav;
4729 unsigned EMUSHORT nsign, *p;
4730 char *sp, *s, *lstr;
4732 /* Copy the input string. */
4733 lstr = (char *) alloca (strlen (ss) + 1);
4735 while (*s == ' ') /* skip leading spaces */
4738 while ((*sp++ = *s++) != '\0')
4743 rndprc = NBITS; /* Set to full precision */
4756 if ((k >= 0) && (k <= 9))
4758 /* Ignore leading zeros */
4759 if ((prec == 0) && (decflg == 0) && (k == 0))
4761 /* Identify and strip trailing zeros after the decimal point. */
4762 if ((trail == 0) && (decflg != 0))
4765 while ((*sp >= '0') && (*sp <= '9'))
4767 /* Check for syntax error */
4769 if ((c != 'e') && (c != 'E') && (c != '\0')
4770 && (c != '\n') && (c != '\r') && (c != ' ')
4781 /* If enough digits were given to more than fill up the yy register,
4782 continuing until overflow into the high guard word yy[2]
4783 guarantees that there will be a roundoff bit at the top
4784 of the low guard word after normalization. */
4789 nexp += 1; /* count digits after decimal point */
4790 eshup1 (yy); /* multiply current number by 10 */
4796 xt[NI - 2] = (unsigned EMUSHORT) k;
4801 /* Mark any lost non-zero digit. */
4803 /* Count lost digits before the decimal point. */
4818 case '.': /* decimal point */
4848 mtherr ("asctoe", DOMAIN);
4857 /* Exponent interpretation */
4863 /* check for + or - */
4871 while ((*s >= '0') && (*s <= '9'))
4875 if (exp > -(MINDECEXP))
4885 if (exp > MAXDECEXP)
4889 yy[E] = 0x7fff; /* infinity */
4892 if (exp < MINDECEXP)
4901 /* Pad trailing zeros to minimize power of 10, per IEEE spec. */
4902 while ((nexp > 0) && (yy[2] == 0))
4914 if ((k = enormlz (yy)) > NBITS)
4919 lexp = (EXONE - 1 + NBITS) - k;
4920 emdnorm (yy, lost, 0, lexp, 64);
4922 /* Convert to external format:
4924 Multiply by 10**nexp. If precision is 64 bits,
4925 the maximum relative error incurred in forming 10**n
4926 for 0 <= n <= 324 is 8.2e-20, at 10**180.
4927 For 0 <= n <= 999, the peak relative error is 1.4e-19 at 10**947.
4928 For 0 >= n >= -999, it is -1.55e-19 at 10**-435. */
4943 /* Punt. Can't handle this without 2 divides. */
4944 emovi (etens[0], tt);
4951 p = &etens[NTEN][0];
4961 while (exp <= MAXP);
4979 /* Round and convert directly to the destination type */
4981 lexp -= EXONE - 0x3ff;
4983 else if (oprec == 24 || oprec == 56)
4984 lexp -= EXONE - (0x41 << 2);
4986 else if (oprec == 24)
4987 lexp -= EXONE - 0177;
4990 else if (oprec == 56)
4991 lexp -= EXONE - 0201;
4994 emdnorm (yy, k, 0, lexp, 64);
5004 todec (yy, y); /* see etodec.c */
5009 toibm (yy, y, DFmode);
5032 /* Return Y = largest integer not greater than X (truncated toward minus
5035 static unsigned EMUSHORT bmask[] =
5058 unsigned EMUSHORT x[], y[];
5060 register unsigned EMUSHORT *p;
5062 unsigned EMUSHORT f[NE];
5064 emov (x, f); /* leave in external format */
5065 expon = (int) f[NE - 1];
5066 e = (expon & 0x7fff) - (EXONE - 1);
5072 /* number of bits to clear out */
5084 /* clear the remaining bits */
5086 /* truncate negatives toward minus infinity */
5089 if ((unsigned EMUSHORT) expon & (unsigned EMUSHORT) 0x8000)
5091 for (i = 0; i < NE - 1; i++)
5103 /* Return S and EXP such that S * 2^EXP = X and .5 <= S < 1.
5104 For example, 1.1 = 0.55 * 2^1. */
5108 unsigned EMUSHORT x[];
5110 unsigned EMUSHORT s[];
5112 unsigned EMUSHORT xi[NI];
5116 /* Handle denormalized numbers properly using long integer exponent. */
5117 li = (EMULONG) ((EMUSHORT) xi[1]);
5125 *exp = (int) (li - 0x3ffe);
5128 /* Return e type Y = X * 2^PWR2. */
5132 unsigned EMUSHORT x[];
5134 unsigned EMUSHORT y[];
5136 unsigned EMUSHORT xi[NI];
5144 emdnorm (xi, i, i, li, 64);
5149 /* C = remainder after dividing B by A, all e type values.
5150 Least significant integer quotient bits left in EQUOT. */
5154 unsigned EMUSHORT a[], b[], c[];
5156 unsigned EMUSHORT den[NI], num[NI];
5160 || (ecmp (a, ezero) == 0)
5168 if (ecmp (a, ezero) == 0)
5170 mtherr ("eremain", SING);
5176 eiremain (den, num);
5177 /* Sign of remainder = sign of quotient */
5185 /* Return quotient of exploded e-types NUM / DEN in EQUOT,
5186 remainder in NUM. */
5190 unsigned EMUSHORT den[], num[];
5193 unsigned EMUSHORT j;
5196 ld -= enormlz (den);
5198 ln -= enormlz (num);
5202 if (ecmpm (den, num) <= 0)
5214 emdnorm (num, 0, 0, ln, 0);
5217 /* Report an error condition CODE encountered in function NAME.
5218 CODE is one of the following:
5220 Mnemonic Value Significance
5222 DOMAIN 1 argument domain error
5223 SING 2 function singularity
5224 OVERFLOW 3 overflow range error
5225 UNDERFLOW 4 underflow range error
5226 TLOSS 5 total loss of precision
5227 PLOSS 6 partial loss of precision
5228 INVALID 7 NaN - producing operation
5229 EDOM 33 Unix domain error code
5230 ERANGE 34 Unix range error code
5232 The order of appearance of the following messages is bound to the
5233 error codes defined above. */
5236 static char *ermsg[NMSGS] =
5238 "unknown", /* error code 0 */
5239 "domain", /* error code 1 */
5240 "singularity", /* et seq. */
5243 "total loss of precision",
5244 "partial loss of precision",
5258 /* The string passed by the calling program is supposed to be the
5259 name of the function in which the error occurred.
5260 The code argument selects which error message string will be printed. */
5262 if ((code <= 0) || (code >= NMSGS))
5264 sprintf (errstr, " %s %s error", name, ermsg[code]);
5267 /* Set global error message word */
5272 /* Convert DEC double precision D to e type E. */
5276 unsigned EMUSHORT *d;
5277 unsigned EMUSHORT *e;
5279 unsigned EMUSHORT y[NI];
5280 register unsigned EMUSHORT r, *p;
5282 ecleaz (y); /* start with a zero */
5283 p = y; /* point to our number */
5284 r = *d; /* get DEC exponent word */
5285 if (*d & (unsigned int) 0x8000)
5286 *p = 0xffff; /* fill in our sign */
5287 ++p; /* bump pointer to our exponent word */
5288 r &= 0x7fff; /* strip the sign bit */
5289 if (r == 0) /* answer = 0 if high order DEC word = 0 */
5293 r >>= 7; /* shift exponent word down 7 bits */
5294 r += EXONE - 0201; /* subtract DEC exponent offset */
5295 /* add our e type exponent offset */
5296 *p++ = r; /* to form our exponent */
5298 r = *d++; /* now do the high order mantissa */
5299 r &= 0177; /* strip off the DEC exponent and sign bits */
5300 r |= 0200; /* the DEC understood high order mantissa bit */
5301 *p++ = r; /* put result in our high guard word */
5303 *p++ = *d++; /* fill in the rest of our mantissa */
5307 eshdn8 (y); /* shift our mantissa down 8 bits */
5312 /* Convert e type X to DEC double precision D. */
5316 unsigned EMUSHORT *x, *d;
5318 unsigned EMUSHORT xi[NI];
5323 /* Adjust exponent for offsets. */
5324 exp = (EMULONG) xi[E] - (EXONE - 0201);
5325 /* Round off to nearest or even. */
5328 emdnorm (xi, 0, 0, exp, 64);
5333 /* Convert exploded e-type X, that has already been rounded to
5334 56-bit precision, to DEC format double Y. */
5338 unsigned EMUSHORT *x, *y;
5340 unsigned EMUSHORT i;
5341 unsigned EMUSHORT *p;
5380 /* Convert IBM single/double precision to e type. */
5384 unsigned EMUSHORT *d;
5385 unsigned EMUSHORT *e;
5386 enum machine_mode mode;
5388 unsigned EMUSHORT y[NI];
5389 register unsigned EMUSHORT r, *p;
5392 ecleaz (y); /* start with a zero */
5393 p = y; /* point to our number */
5394 r = *d; /* get IBM exponent word */
5395 if (*d & (unsigned int) 0x8000)
5396 *p = 0xffff; /* fill in our sign */
5397 ++p; /* bump pointer to our exponent word */
5398 r &= 0x7f00; /* strip the sign bit */
5399 r >>= 6; /* shift exponent word down 6 bits */
5400 /* in fact shift by 8 right and 2 left */
5401 r += EXONE - (0x41 << 2); /* subtract IBM exponent offset */
5402 /* add our e type exponent offset */
5403 *p++ = r; /* to form our exponent */
5405 *p++ = *d++ & 0xff; /* now do the high order mantissa */
5406 /* strip off the IBM exponent and sign bits */
5407 if (mode != SFmode) /* there are only 2 words in SFmode */
5409 *p++ = *d++; /* fill in the rest of our mantissa */
5414 if (y[M] == 0 && y[M+1] == 0 && y[M+2] == 0 && y[M+3] == 0)
5417 y[E] -= 5 + enormlz (y); /* now normalise the mantissa */
5418 /* handle change in RADIX */
5424 /* Convert e type to IBM single/double precision. */
5428 unsigned EMUSHORT *x, *d;
5429 enum machine_mode mode;
5431 unsigned EMUSHORT xi[NI];
5436 exp = (EMULONG) xi[E] - (EXONE - (0x41 << 2)); /* adjust exponent for offsets */
5437 /* round off to nearest or even */
5440 emdnorm (xi, 0, 0, exp, 64);
5442 toibm (xi, d, mode);
5447 unsigned EMUSHORT *x, *y;
5448 enum machine_mode mode;
5450 unsigned EMUSHORT i;
5451 unsigned EMUSHORT *p;
5499 /* Output a binary NaN bit pattern in the target machine's format. */
5501 /* If special NaN bit patterns are required, define them in tm.h
5502 as arrays of unsigned 16-bit shorts. Otherwise, use the default
5508 unsigned EMUSHORT TFbignan[8] =
5509 {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
5510 unsigned EMUSHORT TFlittlenan[8] = {0, 0, 0, 0, 0, 0, 0x8000, 0xffff};
5518 unsigned EMUSHORT XFbignan[6] =
5519 {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
5520 unsigned EMUSHORT XFlittlenan[6] = {0, 0, 0, 0xc000, 0xffff, 0};
5528 unsigned EMUSHORT DFbignan[4] = {0x7fff, 0xffff, 0xffff, 0xffff};
5529 unsigned EMUSHORT DFlittlenan[4] = {0, 0, 0, 0xfff8};
5537 unsigned EMUSHORT SFbignan[2] = {0x7fff, 0xffff};
5538 unsigned EMUSHORT SFlittlenan[2] = {0, 0xffc0};
5544 make_nan (nan, sign, mode)
5545 unsigned EMUSHORT *nan;
5547 enum machine_mode mode;
5550 unsigned EMUSHORT *p;
5554 /* Possibly the `reserved operand' patterns on a VAX can be
5555 used like NaN's, but probably not in the same way as IEEE. */
5556 #if !defined(DEC) && !defined(IBM)
5559 if (REAL_WORDS_BIG_ENDIAN)
5566 if (REAL_WORDS_BIG_ENDIAN)
5573 if (REAL_WORDS_BIG_ENDIAN)
5581 if (REAL_WORDS_BIG_ENDIAN)
5590 if (REAL_WORDS_BIG_ENDIAN)
5591 *nan++ = (sign << 15) | *p++;
5594 if (! REAL_WORDS_BIG_ENDIAN)
5595 *nan = (sign << 15) | *p;
5598 /* Convert an SFmode target `float' value to a REAL_VALUE_TYPE.
5599 This is the inverse of the function `etarsingle' invoked by
5600 REAL_VALUE_TO_TARGET_SINGLE. */
5603 ereal_from_float (f)
5607 unsigned EMUSHORT s[2];
5608 unsigned EMUSHORT e[NE];
5610 /* Convert 32 bit integer to array of 16 bit pieces in target machine order.
5611 This is the inverse operation to what the function `endian' does. */
5612 if (REAL_WORDS_BIG_ENDIAN)
5614 s[0] = (unsigned EMUSHORT) (f >> 16);
5615 s[1] = (unsigned EMUSHORT) f;
5619 s[0] = (unsigned EMUSHORT) f;
5620 s[1] = (unsigned EMUSHORT) (f >> 16);
5622 /* Convert and promote the target float to E-type. */
5624 /* Output E-type to REAL_VALUE_TYPE. */
5630 /* Convert a DFmode target `double' value to a REAL_VALUE_TYPE.
5631 This is the inverse of the function `etardouble' invoked by
5632 REAL_VALUE_TO_TARGET_DOUBLE.
5634 The DFmode is stored as an array of HOST_WIDE_INT in the target's
5635 data format, with no holes in the bit packing. The first element
5636 of the input array holds the bits that would come first in the
5637 target computer's memory. */
5640 ereal_from_double (d)
5644 unsigned EMUSHORT s[4];
5645 unsigned EMUSHORT e[NE];
5647 /* Convert array of HOST_WIDE_INT to equivalent array of 16-bit pieces. */
5648 if (REAL_WORDS_BIG_ENDIAN)
5650 s[0] = (unsigned EMUSHORT) (d[0] >> 16);
5651 s[1] = (unsigned EMUSHORT) d[0];
5652 #if HOST_BITS_PER_WIDE_INT == 32
5653 s[2] = (unsigned EMUSHORT) (d[1] >> 16);
5654 s[3] = (unsigned EMUSHORT) d[1];
5656 /* In this case the entire target double is contained in the
5657 first array element. The second element of the input is
5659 s[2] = (unsigned EMUSHORT) (d[0] >> 48);
5660 s[3] = (unsigned EMUSHORT) (d[0] >> 32);
5665 /* Target float words are little-endian. */
5666 s[0] = (unsigned EMUSHORT) d[0];
5667 s[1] = (unsigned EMUSHORT) (d[0] >> 16);
5668 #if HOST_BITS_PER_WIDE_INT == 32
5669 s[2] = (unsigned EMUSHORT) d[1];
5670 s[3] = (unsigned EMUSHORT) (d[1] >> 16);
5672 s[2] = (unsigned EMUSHORT) (d[0] >> 32);
5673 s[3] = (unsigned EMUSHORT) (d[0] >> 48);
5676 /* Convert target double to E-type. */
5678 /* Output E-type to REAL_VALUE_TYPE. */
5684 /* Convert target computer unsigned 64-bit integer to e-type.
5685 The endian-ness of DImode follows the convention for integers,
5686 so we use WORDS_BIG_ENDIAN here, not REAL_WORDS_BIG_ENDIAN. */
5690 unsigned EMUSHORT *di; /* Address of the 64-bit int. */
5691 unsigned EMUSHORT *e;
5693 unsigned EMUSHORT yi[NI];
5697 if (WORDS_BIG_ENDIAN)
5699 for (k = M; k < M + 4; k++)
5704 for (k = M + 3; k >= M; k--)
5707 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
5708 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
5709 ecleaz (yi); /* it was zero */
5711 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
5715 /* Convert target computer signed 64-bit integer to e-type. */
5719 unsigned EMUSHORT *di; /* Address of the 64-bit int. */
5720 unsigned EMUSHORT *e;
5722 unsigned EMULONG acc;
5723 unsigned EMUSHORT yi[NI];
5724 unsigned EMUSHORT carry;
5728 if (WORDS_BIG_ENDIAN)
5730 for (k = M; k < M + 4; k++)
5735 for (k = M + 3; k >= M; k--)
5738 /* Take absolute value */
5744 for (k = M + 3; k >= M; k--)
5746 acc = (unsigned EMULONG) (~yi[k] & 0xffff) + carry;
5753 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
5754 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
5755 ecleaz (yi); /* it was zero */
5757 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
5764 /* Convert e-type to unsigned 64-bit int. */
5768 unsigned EMUSHORT *x;
5769 unsigned EMUSHORT *i;
5771 unsigned EMUSHORT xi[NI];
5780 k = (int) xi[E] - (EXONE - 1);
5783 for (j = 0; j < 4; j++)
5789 for (j = 0; j < 4; j++)
5792 warning ("overflow on truncation to integer");
5797 /* Shift more than 16 bits: first shift up k-16 mod 16,
5798 then shift up by 16's. */
5799 j = k - ((k >> 4) << 4);
5803 if (WORDS_BIG_ENDIAN)
5814 if (WORDS_BIG_ENDIAN)
5819 while ((k -= 16) > 0);
5823 /* shift not more than 16 bits */
5828 if (WORDS_BIG_ENDIAN)
5847 /* Convert e-type to signed 64-bit int. */
5851 unsigned EMUSHORT *x;
5852 unsigned EMUSHORT *i;
5854 unsigned EMULONG acc;
5855 unsigned EMUSHORT xi[NI];
5856 unsigned EMUSHORT carry;
5857 unsigned EMUSHORT *isave;
5861 k = (int) xi[E] - (EXONE - 1);
5864 for (j = 0; j < 4; j++)
5870 for (j = 0; j < 4; j++)
5873 warning ("overflow on truncation to integer");
5879 /* Shift more than 16 bits: first shift up k-16 mod 16,
5880 then shift up by 16's. */
5881 j = k - ((k >> 4) << 4);
5885 if (WORDS_BIG_ENDIAN)
5896 if (WORDS_BIG_ENDIAN)
5901 while ((k -= 16) > 0);
5905 /* shift not more than 16 bits */
5908 if (WORDS_BIG_ENDIAN)
5924 /* Negate if negative */
5928 if (WORDS_BIG_ENDIAN)
5930 for (k = 0; k < 4; k++)
5932 acc = (unsigned EMULONG) (~(*isave) & 0xffff) + carry;
5933 if (WORDS_BIG_ENDIAN)
5945 /* Longhand square root routine. */
5948 static int esqinited = 0;
5949 static unsigned short sqrndbit[NI];
5953 unsigned EMUSHORT *x, *y;
5955 unsigned EMUSHORT temp[NI], num[NI], sq[NI], xx[NI];
5957 int i, j, k, n, nlups;
5962 sqrndbit[NI - 2] = 1;
5965 /* Check for arg <= 0 */
5966 i = ecmp (x, ezero);
5971 mtherr ("esqrt", DOMAIN);
5987 /* Bring in the arg and renormalize if it is denormal. */
5989 m = (EMULONG) xx[1]; /* local long word exponent */
5993 /* Divide exponent by 2 */
5995 exp = (unsigned short) ((m / 2) + 0x3ffe);
5997 /* Adjust if exponent odd */
6007 n = 8; /* get 8 bits of result per inner loop */
6013 /* bring in next word of arg */
6015 num[NI - 1] = xx[j + 3];
6016 /* Do additional bit on last outer loop, for roundoff. */
6019 for (i = 0; i < n; i++)
6021 /* Next 2 bits of arg */
6024 /* Shift up answer */
6026 /* Make trial divisor */
6027 for (k = 0; k < NI; k++)
6030 eaddm (sqrndbit, temp);
6031 /* Subtract and insert answer bit if it goes in */
6032 if (ecmpm (temp, num) <= 0)
6042 /* Adjust for extra, roundoff loop done. */
6043 exp += (NBITS - 1) - rndprc;
6045 /* Sticky bit = 1 if the remainder is nonzero. */
6047 for (i = 3; i < NI; i++)
6050 /* Renormalize and round off. */
6051 emdnorm (sq, k, 0, exp, 64);
6054 #endif /* EMU_NON_COMPILE not defined */
6056 /* Return the binary precision of the significand for a given
6057 floating point mode. The mode can hold an integer value
6058 that many bits wide, without losing any bits. */
6061 significand_size (mode)
6062 enum machine_mode mode;
6071 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
6074 #if TARGET_FLOAT_FORMAT == IBM_FLOAT_FORMAT
6077 #if TARGET_FLOAT_FORMAT == VAX_FLOAT_FORMAT