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 ieee.c, which is a
48 floating point arithmetic suite that was not written with gcc in
49 mind. The interface is followed by ieee.c itself and related
50 items. Avoid changing ieee.c unless you have suitable test
51 programs available. A special version of the PARANOIA floating
52 point arithmetic tester, modified for this purpose, can be found
53 on usc.edu : /pub/C-numanal/ieeetest.zoo. Some tutorial
54 information on ieee.c is given in my book: S. L. Moshier,
55 _Methods and Programs for Mathematical Functions_, Prentice-Hall
56 or Simon & Schuster Int'l, 1989. A library of XFmode elementary
57 transcendental functions can be obtained by ftp from
58 research.att.com: netlib/cephes/ldouble.shar.Z */
60 /* Type of computer arithmetic.
61 Only one of DEC, IBM, IEEE, or UNK should get defined.
63 `IEEE', when FLOAT_WORDS_BIG_ENDIAN is non-zero, refers generically
64 to big-endian IEEE floating-point data structure. This definition
65 should work in SFmode `float' type and DFmode `double' type on
66 virtually all big-endian IEEE machines. If LONG_DOUBLE_TYPE_SIZE
67 has been defined to be 96, then IEEE also invokes the particular
68 XFmode (`long double' type) data structure used by the Motorola
69 680x0 series processors.
71 `IEEE', when FLOAT_WORDS_BIG_ENDIAN is zero, refers generally to
72 little-endian IEEE machines. In this case, if LONG_DOUBLE_TYPE_SIZE
73 has been defined to be 96, then IEEE also invokes the particular
74 XFmode `long double' data structure used by the Intel 80x86 series
77 `DEC' refers specifically to the Digital Equipment Corp PDP-11
78 and VAX floating point data structure. This model currently
79 supports no type wider than DFmode.
81 `IBM' refers specifically to the IBM System/370 and compatible
82 floating point data structure. This model currently supports
83 no type wider than DFmode. The IBM conversions were contributed by
84 frank@atom.ansto.gov.au (Frank Crawford).
86 If LONG_DOUBLE_TYPE_SIZE = 64 (the default, unless tm.h defines it)
87 then `long double' and `double' are both implemented, but they
88 both mean DFmode. In this case, the software floating-point
89 support available here is activated by writing
90 #define REAL_ARITHMETIC
93 The case LONG_DOUBLE_TYPE_SIZE = 128 activates TFmode support
94 and may deactivate XFmode since `long double' is used to refer
97 The macros FLOAT_WORDS_BIG_ENDIAN, HOST_FLOAT_WORDS_BIG_ENDIAN,
98 contributed by Richard Earnshaw <Richard.Earnshaw@cl.cam.ac.uk>,
99 separate the floating point unit's endian-ness from that of
100 the integer addressing. This permits one to define a big-endian
101 FPU on a little-endian machine (e.g., ARM). An extension to
102 BYTES_BIG_ENDIAN may be required for some machines in the future.
103 These optional macros may be defined in tm.h. In real.h, they
104 default to WORDS_BIG_ENDIAN, etc., so there is no need to define
105 them for any normal host or target machine on which the floats
106 and the integers have the same endian-ness. */
109 /* The following converts gcc macros into the ones used by this file. */
111 /* REAL_ARITHMETIC defined means that macros in real.h are
112 defined to call emulator functions. */
113 #ifdef REAL_ARITHMETIC
115 #if TARGET_FLOAT_FORMAT == VAX_FLOAT_FORMAT
116 /* PDP-11, Pro350, VAX: */
118 #else /* it's not VAX */
119 #if TARGET_FLOAT_FORMAT == IBM_FLOAT_FORMAT
120 /* IBM System/370 style */
122 #else /* it's also not an IBM */
123 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
125 #else /* it's not IEEE either */
126 /* UNKnown arithmetic. We don't support this and can't go on. */
127 unknown arithmetic type
129 #endif /* not IEEE */
134 /* REAL_ARITHMETIC not defined means that the *host's* data
135 structure will be used. It may differ by endian-ness from the
136 target machine's structure and will get its ends swapped
137 accordingly (but not here). Probably only the decimal <-> binary
138 functions in this file will actually be used in this case. */
140 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
142 #else /* it's not VAX */
143 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
144 /* IBM System/370 style */
146 #else /* it's also not an IBM */
147 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
149 #else /* it's not IEEE either */
150 unknown arithmetic type
152 #endif /* not IEEE */
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 == FLOAT_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 == FLOAT_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 (FLOAT_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
];
898 /* Check whether a REAL_VALUE_TYPE item is a NaN. */
904 unsigned EMUSHORT e
[NE
];
915 /* Check for a negative REAL_VALUE_TYPE number.
916 This just checks the sign bit, so that -0 counts as negative. */
922 return ereal_isneg (x
);
925 /* Expansion of REAL_VALUE_TRUNCATE.
926 The result is in floating point, rounded to nearest or even. */
929 real_value_truncate (mode
, arg
)
930 enum machine_mode mode
;
933 unsigned EMUSHORT e
[NE
], t
[NE
];
969 /* If an unsupported type was requested, presume that
970 the machine files know something useful to do with
971 the unmodified value. */
980 #endif /* REAL_ARITHMETIC defined */
982 /* Used for debugging--print the value of R in human-readable format
991 REAL_VALUE_TO_DECIMAL (r
, "%.20g", dstr
);
992 fprintf (stderr
, "%s", dstr
);
996 /* Target values are arrays of host longs. A long is guaranteed
997 to be at least 32 bits wide. */
999 /* 128-bit long double */
1006 unsigned EMUSHORT e
[NE
];
1010 endian (e
, l
, TFmode
);
1013 /* 80-bit long double */
1020 unsigned EMUSHORT e
[NE
];
1024 endian (e
, l
, XFmode
);
1032 unsigned EMUSHORT e
[NE
];
1036 endian (e
, l
, DFmode
);
1043 unsigned EMUSHORT e
[NE
];
1048 endian (e
, &l
, SFmode
);
1053 ereal_to_decimal (x
, s
)
1057 unsigned EMUSHORT e
[NE
];
1065 REAL_VALUE_TYPE x
, y
;
1067 unsigned EMUSHORT ex
[NE
], ey
[NE
];
1071 return (ecmp (ex
, ey
));
1078 unsigned EMUSHORT ex
[NE
];
1081 return (eisneg (ex
));
1084 /* End of REAL_ARITHMETIC interface */
1087 Extended precision IEEE binary floating point arithmetic routines
1089 Numbers are stored in C language as arrays of 16-bit unsigned
1090 short integers. The arguments of the routines are pointers to
1093 External e type data structure, simulates Intel 8087 chip
1094 temporary real format but possibly with a larger significand:
1096 NE-1 significand words (least significant word first,
1097 most significant bit is normally set)
1098 exponent (value = EXONE for 1.0,
1099 top bit is the sign)
1102 Internal data structure of a number (a "word" is 16 bits):
1104 ei[0] sign word (0 for positive, 0xffff for negative)
1105 ei[1] biased exponent (value = EXONE for the number 1.0)
1106 ei[2] high guard word (always zero after normalization)
1108 to ei[NI-2] significand (NI-4 significand words,
1109 most significant word first,
1110 most significant bit is set)
1111 ei[NI-1] low guard word (0x8000 bit is rounding place)
1115 Routines for external format numbers
1117 asctoe (string, e) ASCII string to extended double e type
1118 asctoe64 (string, &d) ASCII string to long double
1119 asctoe53 (string, &d) ASCII string to double
1120 asctoe24 (string, &f) ASCII string to single
1121 asctoeg (string, e, prec) ASCII string to specified precision
1122 e24toe (&f, e) IEEE single precision to e type
1123 e53toe (&d, e) IEEE double precision to e type
1124 e64toe (&d, e) IEEE long double precision to e type
1125 e113toe (&d, e) 128-bit long double precision to e type
1126 eabs (e) absolute value
1127 eadd (a, b, c) c = b + a
1129 ecmp (a, b) Returns 1 if a > b, 0 if a == b,
1130 -1 if a < b, -2 if either a or b is a NaN.
1131 ediv (a, b, c) c = b / a
1132 efloor (a, b) truncate to integer, toward -infinity
1133 efrexp (a, exp, s) extract exponent and significand
1134 eifrac (e, &l, frac) e to HOST_WIDE_INT and e type fraction
1135 euifrac (e, &l, frac) e to unsigned HOST_WIDE_INT and e type fraction
1136 einfin (e) set e to infinity, leaving its sign alone
1137 eldexp (a, n, b) multiply by 2**n
1139 emul (a, b, c) c = b * a
1141 eround (a, b) b = nearest integer value to a
1142 esub (a, b, c) c = b - a
1143 e24toasc (&f, str, n) single to ASCII string, n digits after decimal
1144 e53toasc (&d, str, n) double to ASCII string, n digits after decimal
1145 e64toasc (&d, str, n) 80-bit long double to ASCII string
1146 e113toasc (&d, str, n) 128-bit long double to ASCII string
1147 etoasc (e, str, n) e to ASCII string, n digits after decimal
1148 etoe24 (e, &f) convert e type to IEEE single precision
1149 etoe53 (e, &d) convert e type to IEEE double precision
1150 etoe64 (e, &d) convert e type to IEEE long double precision
1151 ltoe (&l, e) HOST_WIDE_INT to e type
1152 ultoe (&l, e) unsigned HOST_WIDE_INT to e type
1153 eisneg (e) 1 if sign bit of e != 0, else 0
1154 eisinf (e) 1 if e has maximum exponent (non-IEEE)
1155 or is infinite (IEEE)
1156 eisnan (e) 1 if e is a NaN
1159 Routines for internal format numbers
1161 eaddm (ai, bi) add significands, bi = bi + ai
1163 ecleazs (ei) set ei = 0 but leave its sign alone
1164 ecmpm (ai, bi) compare significands, return 1, 0, or -1
1165 edivm (ai, bi) divide significands, bi = bi / ai
1166 emdnorm (ai,l,s,exp) normalize and round off
1167 emovi (a, ai) convert external a to internal ai
1168 emovo (ai, a) convert internal ai to external a
1169 emovz (ai, bi) bi = ai, low guard word of bi = 0
1170 emulm (ai, bi) multiply significands, bi = bi * ai
1171 enormlz (ei) left-justify the significand
1172 eshdn1 (ai) shift significand and guards down 1 bit
1173 eshdn8 (ai) shift down 8 bits
1174 eshdn6 (ai) shift down 16 bits
1175 eshift (ai, n) shift ai n bits up (or down if n < 0)
1176 eshup1 (ai) shift significand and guards up 1 bit
1177 eshup8 (ai) shift up 8 bits
1178 eshup6 (ai) shift up 16 bits
1179 esubm (ai, bi) subtract significands, bi = bi - ai
1180 eiisinf (ai) 1 if infinite
1181 eiisnan (ai) 1 if a NaN
1182 eiisneg (ai) 1 if sign bit of ai != 0, else 0
1183 einan (ai) set ai = NaN
1184 eiinfin (ai) set ai = infinity
1186 The result is always normalized and rounded to NI-4 word precision
1187 after each arithmetic operation.
1189 Exception flags are NOT fully supported.
1191 Signaling NaN's are NOT supported; they are treated the same
1194 Define INFINITY for support of infinity; otherwise a
1195 saturation arithmetic is implemented.
1197 Define NANS for support of Not-a-Number items; otherwise the
1198 arithmetic will never produce a NaN output, and might be confused
1200 If NaN's are supported, the output of `ecmp (a,b)' is -2 if
1201 either a or b is a NaN. This means asking `if (ecmp (a,b) < 0)'
1202 may not be legitimate. Use `if (ecmp (a,b) == -1)' for `less than'
1205 Denormals are always supported here where appropriate (e.g., not
1206 for conversion to DEC numbers). */
1208 /* Definitions for error codes that are passed to the common error handling
1211 For Digital Equipment PDP-11 and VAX computers, certain
1212 IBM systems, and others that use numbers with a 56-bit
1213 significand, the symbol DEC should be defined. In this
1214 mode, most floating point constants are given as arrays
1215 of octal integers to eliminate decimal to binary conversion
1216 errors that might be introduced by the compiler.
1218 For computers, such as IBM PC, that follow the IEEE
1219 Standard for Binary Floating Point Arithmetic (ANSI/IEEE
1220 Std 754-1985), the symbol IBMPC or MIEEE should be defined.
1221 These numbers have 53-bit significands. In this mode, constants
1222 are provided as arrays of hexadecimal 16 bit integers.
1223 [This has been changed to instead check the preprocessor macros IEEE
1224 and FLOAT_WORDS_BIG_ENDIAN].
1226 To accommodate other types of computer arithmetic, all
1227 constants are also provided in a normal decimal radix
1228 which one can hope are correctly converted to a suitable
1229 format by the available C language compiler. To invoke
1230 this mode, the symbol UNK is defined.
1232 An important difference among these modes is a predefined
1233 set of machine arithmetic constants for each. The numbers
1234 MACHEP (the machine roundoff error), MAXNUM (largest number
1235 represented), and several other parameters are preset by
1236 the configuration symbol. Check the file const.c to
1237 ensure that these values are correct for your computer.
1239 For ANSI C compatibility, define ANSIC equal to 1. Currently
1240 this affects only the atan2 function and others that use it. */
1242 /* Constant definitions for math error conditions. */
1244 #define DOMAIN 1 /* argument domain error */
1245 #define SING 2 /* argument singularity */
1246 #define OVERFLOW 3 /* overflow range error */
1247 #define UNDERFLOW 4 /* underflow range error */
1248 #define TLOSS 5 /* total loss of precision */
1249 #define PLOSS 6 /* partial loss of precision */
1250 #define INVALID 7 /* NaN-producing operation */
1252 /* e type constants used by high precision check routines */
1254 #if LONG_DOUBLE_TYPE_SIZE == 128
1256 unsigned EMUSHORT ezero
[NE
] =
1257 {0x0000, 0x0000, 0x0000, 0x0000,
1258 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,};
1259 extern unsigned EMUSHORT ezero
[];
1262 unsigned EMUSHORT ehalf
[NE
] =
1263 {0x0000, 0x0000, 0x0000, 0x0000,
1264 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3ffe,};
1265 extern unsigned EMUSHORT ehalf
[];
1268 unsigned EMUSHORT eone
[NE
] =
1269 {0x0000, 0x0000, 0x0000, 0x0000,
1270 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3fff,};
1271 extern unsigned EMUSHORT eone
[];
1274 unsigned EMUSHORT etwo
[NE
] =
1275 {0x0000, 0x0000, 0x0000, 0x0000,
1276 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4000,};
1277 extern unsigned EMUSHORT etwo
[];
1280 unsigned EMUSHORT e32
[NE
] =
1281 {0x0000, 0x0000, 0x0000, 0x0000,
1282 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4004,};
1283 extern unsigned EMUSHORT e32
[];
1285 /* 6.93147180559945309417232121458176568075500134360255E-1 */
1286 unsigned EMUSHORT elog2
[NE
] =
1287 {0x40f3, 0xf6af, 0x03f2, 0xb398,
1288 0xc9e3, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1289 extern unsigned EMUSHORT elog2
[];
1291 /* 1.41421356237309504880168872420969807856967187537695E0 */
1292 unsigned EMUSHORT esqrt2
[NE
] =
1293 {0x1d6f, 0xbe9f, 0x754a, 0x89b3,
1294 0x597d, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1295 extern unsigned EMUSHORT esqrt2
[];
1297 /* 3.14159265358979323846264338327950288419716939937511E0 */
1298 unsigned EMUSHORT epi
[NE
] =
1299 {0x2902, 0x1cd1, 0x80dc, 0x628b,
1300 0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1301 extern unsigned EMUSHORT epi
[];
1304 /* LONG_DOUBLE_TYPE_SIZE is other than 128 */
1305 unsigned EMUSHORT ezero
[NE
] =
1306 {0, 0000000, 0000000, 0000000, 0000000, 0000000,};
1307 unsigned EMUSHORT ehalf
[NE
] =
1308 {0, 0000000, 0000000, 0000000, 0100000, 0x3ffe,};
1309 unsigned EMUSHORT eone
[NE
] =
1310 {0, 0000000, 0000000, 0000000, 0100000, 0x3fff,};
1311 unsigned EMUSHORT etwo
[NE
] =
1312 {0, 0000000, 0000000, 0000000, 0100000, 0040000,};
1313 unsigned EMUSHORT e32
[NE
] =
1314 {0, 0000000, 0000000, 0000000, 0100000, 0040004,};
1315 unsigned EMUSHORT elog2
[NE
] =
1316 {0xc9e4, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1317 unsigned EMUSHORT esqrt2
[NE
] =
1318 {0x597e, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1319 unsigned EMUSHORT epi
[NE
] =
1320 {0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1325 /* Control register for rounding precision.
1326 This can be set to 113 (if NE=10), 80 (if NE=6), 64, 56, 53, or 24 bits. */
1331 /* Clear out entire external format number. */
1335 register unsigned EMUSHORT
*x
;
1339 for (i
= 0; i
< NE
; i
++)
1345 /* Move external format number from a to b. */
1349 register unsigned EMUSHORT
*a
, *b
;
1353 for (i
= 0; i
< NE
; i
++)
1358 /* Absolute value of external format number. */
1362 unsigned EMUSHORT x
[];
1364 /* sign is top bit of last word of external format */
1365 x
[NE
- 1] &= 0x7fff;
1368 /* Negate external format number. */
1372 unsigned EMUSHORT x
[];
1375 x
[NE
- 1] ^= 0x8000; /* Toggle the sign bit */
1380 /* Return 1 if sign bit of external format number is nonzero, else zero. */
1384 unsigned EMUSHORT x
[];
1387 if (x
[NE
- 1] & 0x8000)
1394 /* Return 1 if external format number is infinity, else return zero. */
1398 unsigned EMUSHORT x
[];
1405 if ((x
[NE
- 1] & 0x7fff) == 0x7fff)
1412 /* Check if e-type number is not a number. The bit pattern is one that we
1413 defined, so we know for sure how to detect it. */
1417 unsigned EMUSHORT x
[];
1422 /* NaN has maximum exponent */
1423 if ((x
[NE
- 1] & 0x7fff) != 0x7fff)
1425 /* ... and non-zero significand field. */
1426 for (i
= 0; i
< NE
- 1; i
++)
1436 /* Fill external format number with infinity pattern (IEEE)
1437 or largest possible number (non-IEEE). */
1441 register unsigned EMUSHORT
*x
;
1446 for (i
= 0; i
< NE
- 1; i
++)
1450 for (i
= 0; i
< NE
- 1; i
++)
1479 /* Output an e-type NaN.
1480 This generates Intel's quiet NaN pattern for extended real.
1481 The exponent is 7fff, the leading mantissa word is c000. */
1485 register unsigned EMUSHORT
*x
;
1490 for (i
= 0; i
< NE
- 2; i
++)
1493 *x
= (sign
<< 15) | 0x7fff;
1497 /* Move in external format number, converting it to internal format. */
1501 unsigned EMUSHORT
*a
, *b
;
1503 register unsigned EMUSHORT
*p
, *q
;
1507 p
= a
+ (NE
- 1); /* point to last word of external number */
1508 /* get the sign bit */
1513 /* get the exponent */
1515 *q
++ &= 0x7fff; /* delete the sign bit */
1517 if ((*(q
- 1) & 0x7fff) == 0x7fff)
1523 for (i
= 3; i
< NI
; i
++)
1529 for (i
= 2; i
< NI
; i
++)
1535 /* clear high guard word */
1537 /* move in the significand */
1538 for (i
= 0; i
< NE
- 1; i
++)
1540 /* clear low guard word */
1545 /* Move internal format number out, converting it to external format. */
1549 unsigned EMUSHORT
*a
, *b
;
1551 register unsigned EMUSHORT
*p
, *q
;
1552 unsigned EMUSHORT i
;
1556 q
= b
+ (NE
- 1); /* point to output exponent */
1557 /* combine sign and exponent */
1560 *q
-- = *p
++ | 0x8000;
1564 if (*(p
- 1) == 0x7fff)
1569 enan (b
, eiisneg (a
));
1577 /* skip over guard word */
1579 /* move the significand */
1580 for (j
= 0; j
< NE
- 1; j
++)
1584 /* Clear out internal format number. */
1588 register unsigned EMUSHORT
*xi
;
1592 for (i
= 0; i
< NI
; i
++)
1597 /* Same, but don't touch the sign. */
1601 register unsigned EMUSHORT
*xi
;
1606 for (i
= 0; i
< NI
- 1; i
++)
1612 /* Move internal format number from a to b. */
1616 register unsigned EMUSHORT
*a
, *b
;
1620 for (i
= 0; i
< NI
- 1; i
++)
1622 /* clear low guard word */
1626 /* Generate internal format NaN.
1627 The explicit pattern for this is maximum exponent and
1628 top two significant bits set. */
1632 unsigned EMUSHORT x
[];
1640 /* Return nonzero if internal format number is a NaN. */
1644 unsigned EMUSHORT x
[];
1648 if ((x
[E
] & 0x7fff) == 0x7fff)
1650 for (i
= M
+ 1; i
< NI
; i
++)
1659 /* Return nonzero if sign of internal format number is nonzero. */
1663 unsigned EMUSHORT x
[];
1669 /* Fill internal format number with infinity pattern.
1670 This has maximum exponent and significand all zeros. */
1674 unsigned EMUSHORT x
[];
1681 /* Return nonzero if internal format number is infinite. */
1685 unsigned EMUSHORT x
[];
1692 if ((x
[E
] & 0x7fff) == 0x7fff)
1698 /* Compare significands of numbers in internal format.
1699 Guard words are included in the comparison.
1707 register unsigned EMUSHORT
*a
, *b
;
1711 a
+= M
; /* skip up to significand area */
1713 for (i
= M
; i
< NI
; i
++)
1721 if (*(--a
) > *(--b
))
1728 /* Shift significand down by 1 bit. */
1732 register unsigned EMUSHORT
*x
;
1734 register unsigned EMUSHORT bits
;
1737 x
+= M
; /* point to significand area */
1740 for (i
= M
; i
< NI
; i
++)
1754 /* Shift significand up by 1 bit. */
1758 register unsigned EMUSHORT
*x
;
1760 register unsigned EMUSHORT bits
;
1766 for (i
= M
; i
< NI
; i
++)
1779 /* Shift significand down by 8 bits. */
1783 register unsigned EMUSHORT
*x
;
1785 register unsigned EMUSHORT newbyt
, oldbyt
;
1790 for (i
= M
; i
< NI
; i
++)
1800 /* Shift significand up by 8 bits. */
1804 register unsigned EMUSHORT
*x
;
1807 register unsigned EMUSHORT newbyt
, oldbyt
;
1812 for (i
= M
; i
< NI
; i
++)
1822 /* Shift significand up by 16 bits. */
1826 register unsigned EMUSHORT
*x
;
1829 register unsigned EMUSHORT
*p
;
1834 for (i
= M
; i
< NI
- 1; i
++)
1840 /* Shift significand down by 16 bits. */
1844 register unsigned EMUSHORT
*x
;
1847 register unsigned EMUSHORT
*p
;
1852 for (i
= M
; i
< NI
- 1; i
++)
1858 /* Add significands. x + y replaces y. */
1862 unsigned EMUSHORT
*x
, *y
;
1864 register unsigned EMULONG a
;
1871 for (i
= M
; i
< NI
; i
++)
1873 a
= (unsigned EMULONG
) (*x
) + (unsigned EMULONG
) (*y
) + carry
;
1878 *y
= (unsigned EMUSHORT
) a
;
1884 /* Subtract significands. y - x replaces y. */
1888 unsigned EMUSHORT
*x
, *y
;
1897 for (i
= M
; i
< NI
; i
++)
1899 a
= (unsigned EMULONG
) (*y
) - (unsigned EMULONG
) (*x
) - carry
;
1904 *y
= (unsigned EMUSHORT
) a
;
1911 static unsigned EMUSHORT equot
[NI
];
1915 /* Radix 2 shift-and-add versions of multiply and divide */
1918 /* Divide significands */
1922 unsigned EMUSHORT den
[], num
[];
1925 register unsigned EMUSHORT
*p
, *q
;
1926 unsigned EMUSHORT j
;
1932 for (i
= M
; i
< NI
; i
++)
1937 /* Use faster compare and subtraction if denominator has only 15 bits of
1943 for (i
= M
+ 3; i
< NI
; i
++)
1948 if ((den
[M
+ 1] & 1) != 0)
1956 for (i
= 0; i
< NBITS
+ 2; i
++)
1974 /* The number of quotient bits to calculate is NBITS + 1 scaling guard
1975 bit + 1 roundoff bit. */
1980 for (i
= 0; i
< NBITS
+ 2; i
++)
1982 if (ecmpm (den
, num
) <= 0)
1985 j
= 1; /* quotient bit = 1 */
1999 /* test for nonzero remainder after roundoff bit */
2002 for (i
= M
; i
< NI
; i
++)
2010 for (i
= 0; i
< NI
; i
++)
2016 /* Multiply significands */
2019 unsigned EMUSHORT a
[], b
[];
2021 unsigned EMUSHORT
*p
, *q
;
2026 for (i
= M
; i
< NI
; i
++)
2031 while (*p
== 0) /* significand is not supposed to be zero */
2036 if ((*p
& 0xff) == 0)
2044 for (i
= 0; i
< k
; i
++)
2048 /* remember if there were any nonzero bits shifted out */
2055 for (i
= 0; i
< NI
; i
++)
2058 /* return flag for lost nonzero bits */
2064 /* Radix 65536 versions of multiply and divide */
2067 /* Multiply significand of e-type number b
2068 by 16-bit quantity a, e-type result to c. */
2073 unsigned short b
[], c
[];
2075 register unsigned short *pp
;
2076 register unsigned long carry
;
2078 unsigned short p
[NI
];
2079 unsigned long aa
, m
;
2088 for (i
=M
+1; i
<NI
; i
++)
2098 m
= (unsigned long) aa
* *ps
--;
2099 carry
= (m
& 0xffff) + *pp
;
2100 *pp
-- = (unsigned short)carry
;
2101 carry
= (carry
>> 16) + (m
>> 16) + *pp
;
2102 *pp
= (unsigned short)carry
;
2103 *(pp
-1) = carry
>> 16;
2106 for (i
=M
; i
<NI
; i
++)
2111 /* Divide significands. Neither the numerator nor the denominator
2112 is permitted to have its high guard word nonzero. */
2116 unsigned short den
[], num
[];
2119 register unsigned short *p
;
2121 unsigned short j
, tdenm
, tquot
;
2122 unsigned short tprod
[NI
+1];
2128 for (i
=M
; i
<NI
; i
++)
2134 for (i
=M
; i
<NI
; i
++)
2136 /* Find trial quotient digit (the radix is 65536). */
2137 tnum
= (((unsigned long) num
[M
]) << 16) + num
[M
+1];
2139 /* Do not execute the divide instruction if it will overflow. */
2140 if ((tdenm
* 0xffffL
) < tnum
)
2143 tquot
= tnum
/ tdenm
;
2144 /* Multiply denominator by trial quotient digit. */
2145 m16m ((unsigned int)tquot
, den
, tprod
);
2146 /* The quotient digit may have been overestimated. */
2147 if (ecmpm (tprod
, num
) > 0)
2151 if (ecmpm (tprod
, num
) > 0)
2161 /* test for nonzero remainder after roundoff bit */
2164 for (i
=M
; i
<NI
; i
++)
2171 for (i
=0; i
<NI
; i
++)
2179 /* Multiply significands */
2182 unsigned short a
[], b
[];
2184 unsigned short *p
, *q
;
2185 unsigned short pprod
[NI
];
2191 for (i
=M
; i
<NI
; i
++)
2197 for (i
=M
+1; i
<NI
; i
++)
2205 m16m ((unsigned int) *p
--, b
, pprod
);
2206 eaddm(pprod
, equot
);
2212 for (i
=0; i
<NI
; i
++)
2215 /* return flag for lost nonzero bits */
2221 /* Normalize and round off.
2223 The internal format number to be rounded is "s".
2224 Input "lost" indicates whether or not the number is exact.
2225 This is the so-called sticky bit.
2227 Input "subflg" indicates whether the number was obtained
2228 by a subtraction operation. In that case if lost is nonzero
2229 then the number is slightly smaller than indicated.
2231 Input "exp" is the biased exponent, which may be negative.
2232 the exponent field of "s" is ignored but is replaced by
2233 "exp" as adjusted by normalization and rounding.
2235 Input "rcntrl" is the rounding control.
2237 For future reference: In order for emdnorm to round off denormal
2238 significands at the right point, the input exponent must be
2239 adjusted to be the actual value it would have after conversion to
2240 the final floating point type. This adjustment has been
2241 implemented for all type conversions (etoe53, etc.) and decimal
2242 conversions, but not for the arithmetic functions (eadd, etc.).
2243 Data types having standard 15-bit exponents are not affected by
2244 this, but SFmode and DFmode are affected. For example, ediv with
2245 rndprc = 24 will not round correctly to 24-bit precision if the
2246 result is denormal. */
2248 static int rlast
= -1;
2250 static unsigned EMUSHORT rmsk
= 0;
2251 static unsigned EMUSHORT rmbit
= 0;
2252 static unsigned EMUSHORT rebit
= 0;
2254 static unsigned EMUSHORT rbit
[NI
];
2257 emdnorm (s
, lost
, subflg
, exp
, rcntrl
)
2258 unsigned EMUSHORT s
[];
2265 unsigned EMUSHORT r
;
2270 /* a blank significand could mean either zero or infinity. */
2283 if ((j
> NBITS
) && (exp
< 32767))
2291 if (exp
> (EMULONG
) (-NBITS
- 1))
2304 /* Round off, unless told not to by rcntrl. */
2307 /* Set up rounding parameters if the control register changed. */
2308 if (rndprc
!= rlast
)
2315 rw
= NI
- 1; /* low guard word */
2335 /* For DEC or IBM arithmetic */
2362 /* Shift down 1 temporarily if the data structure has an implied
2363 most significant bit and the number is denormal. */
2364 if ((exp
<= 0) && (rndprc
!= 64) && (rndprc
!= NBITS
))
2366 lost
|= s
[NI
- 1] & 1;
2369 /* Clear out all bits below the rounding bit,
2370 remembering in r if any were nonzero. */
2384 if ((r
& rmbit
) != 0)
2389 { /* round to even */
2390 if ((s
[re
] & rebit
) == 0)
2402 if ((exp
<= 0) && (rndprc
!= 64) && (rndprc
!= NBITS
))
2407 { /* overflow on roundoff */
2420 for (i
= 2; i
< NI
- 1; i
++)
2423 warning ("floating point overflow");
2427 for (i
= M
+ 1; i
< NI
- 1; i
++)
2430 if ((rndprc
< 64) || (rndprc
== 113))
2445 s
[1] = (unsigned EMUSHORT
) exp
;
2450 /* Subtract external format numbers. */
2452 static int subflg
= 0;
2456 unsigned EMUSHORT
*a
, *b
, *c
;
2470 /* Infinity minus infinity is a NaN.
2471 Test for subtracting infinities of the same sign. */
2472 if (eisinf (a
) && eisinf (b
)
2473 && ((eisneg (a
) ^ eisneg (b
)) == 0))
2475 mtherr ("esub", INVALID
);
2489 unsigned EMUSHORT
*a
, *b
, *c
;
2493 /* NaN plus anything is a NaN. */
2504 /* Infinity minus infinity is a NaN.
2505 Test for adding infinities of opposite signs. */
2506 if (eisinf (a
) && eisinf (b
)
2507 && ((eisneg (a
) ^ eisneg (b
)) != 0))
2509 mtherr ("esub", INVALID
);
2520 unsigned EMUSHORT
*a
, *b
, *c
;
2522 unsigned EMUSHORT ai
[NI
], bi
[NI
], ci
[NI
];
2524 EMULONG lt
, lta
, ltb
;
2545 /* compare exponents */
2550 { /* put the larger number in bi */
2560 if (lt
< (EMULONG
) (-NBITS
- 1))
2561 goto done
; /* answer same as larger addend */
2563 lost
= eshift (ai
, k
); /* shift the smaller number down */
2567 /* exponents were the same, so must compare significands */
2570 { /* the numbers are identical in magnitude */
2571 /* if different signs, result is zero */
2577 /* if same sign, result is double */
2578 /* double denomalized tiny number */
2579 if ((bi
[E
] == 0) && ((bi
[3] & 0x8000) == 0))
2584 /* add 1 to exponent unless both are zero! */
2585 for (j
= 1; j
< NI
- 1; j
++)
2589 /* This could overflow, but let emovo take care of that. */
2594 bi
[E
] = (unsigned EMUSHORT
) ltb
;
2598 { /* put the larger number in bi */
2614 emdnorm (bi
, lost
, subflg
, ltb
, 64);
2626 unsigned EMUSHORT
*a
, *b
, *c
;
2628 unsigned EMUSHORT ai
[NI
], bi
[NI
];
2630 EMULONG lt
, lta
, ltb
;
2633 /* Return any NaN input. */
2644 /* Zero over zero, or infinity over infinity, is a NaN. */
2645 if (((ecmp (a
, ezero
) == 0) && (ecmp (b
, ezero
) == 0))
2646 || (eisinf (a
) && eisinf (b
)))
2648 mtherr ("ediv", INVALID
);
2649 enan (c
, eisneg (a
) ^ eisneg (b
));
2653 /* Infinity over anything else is infinity. */
2657 if (eisneg (a
) ^ eisneg (b
))
2658 *(c
+ (NE
- 1)) = 0x8000;
2660 *(c
+ (NE
- 1)) = 0;
2664 /* Anything else over infinity is zero. */
2676 { /* See if numerator is zero. */
2677 for (i
= 1; i
< NI
- 1; i
++)
2681 ltb
-= enormlz (bi
);
2691 { /* possible divide by zero */
2692 for (i
= 1; i
< NI
- 1; i
++)
2696 lta
-= enormlz (ai
);
2701 *(c
+ (NE
- 1)) = 0;
2703 *(c
+ (NE
- 1)) = 0x8000;
2704 /* Divide by zero is not an invalid operation.
2705 It is a divide-by-zero operation! */
2707 mtherr ("ediv", SING
);
2713 /* calculate exponent */
2714 lt
= ltb
- lta
+ EXONE
;
2715 emdnorm (bi
, i
, 0, lt
, 64);
2730 unsigned EMUSHORT
*a
, *b
, *c
;
2732 unsigned EMUSHORT ai
[NI
], bi
[NI
];
2734 EMULONG lt
, lta
, ltb
;
2737 /* NaN times anything is the same NaN. */
2748 /* Zero times infinity is a NaN. */
2749 if ((eisinf (a
) && (ecmp (b
, ezero
) == 0))
2750 || (eisinf (b
) && (ecmp (a
, ezero
) == 0)))
2752 mtherr ("emul", INVALID
);
2753 enan (c
, eisneg (a
) ^ eisneg (b
));
2757 /* Infinity times anything else is infinity. */
2759 if (eisinf (a
) || eisinf (b
))
2761 if (eisneg (a
) ^ eisneg (b
))
2762 *(c
+ (NE
- 1)) = 0x8000;
2764 *(c
+ (NE
- 1)) = 0;
2775 for (i
= 1; i
< NI
- 1; i
++)
2779 lta
-= enormlz (ai
);
2790 for (i
= 1; i
< NI
- 1; i
++)
2794 ltb
-= enormlz (bi
);
2803 /* Multiply significands */
2805 /* calculate exponent */
2806 lt
= lta
+ ltb
- (EXONE
- 1);
2807 emdnorm (bi
, j
, 0, lt
, 64);
2808 /* calculate sign of product */
2819 /* Convert IEEE double precision to e type. */
2823 unsigned EMUSHORT
*pe
, *y
;
2827 dectoe (pe
, y
); /* see etodec.c */
2832 ibmtoe (pe
, y
, DFmode
);
2835 register unsigned EMUSHORT r
;
2836 register unsigned EMUSHORT
*e
, *p
;
2837 unsigned EMUSHORT yy
[NI
];
2841 denorm
= 0; /* flag if denormalized number */
2843 if (! FLOAT_WORDS_BIG_ENDIAN
)
2849 yy
[M
] = (r
& 0x0f) | 0x10;
2850 r
&= ~0x800f; /* strip sign and 4 significand bits */
2855 if (! FLOAT_WORDS_BIG_ENDIAN
)
2857 if (((pe
[3] & 0xf) != 0) || (pe
[2] != 0)
2858 || (pe
[1] != 0) || (pe
[0] != 0))
2860 enan (y
, yy
[0] != 0);
2866 if (((pe
[0] & 0xf) != 0) || (pe
[1] != 0)
2867 || (pe
[2] != 0) || (pe
[3] != 0))
2869 enan (y
, yy
[0] != 0);
2880 #endif /* INFINITY */
2882 /* If zero exponent, then the significand is denormalized.
2883 So take back the understood high significand bit. */
2894 if (! FLOAT_WORDS_BIG_ENDIAN
)
2910 { /* if zero exponent, then normalize the significand */
2911 if ((k
= enormlz (yy
)) > NBITS
)
2914 yy
[E
] -= (unsigned EMUSHORT
) (k
- 1);
2917 #endif /* not IBM */
2918 #endif /* not DEC */
2923 unsigned EMUSHORT
*pe
, *y
;
2925 unsigned EMUSHORT yy
[NI
];
2926 unsigned EMUSHORT
*e
, *p
, *q
;
2931 for (i
= 0; i
< NE
- 5; i
++)
2933 /* This precision is not ordinarily supported on DEC or IBM. */
2935 for (i
= 0; i
< 5; i
++)
2939 p
= &yy
[0] + (NE
- 1);
2942 for (i
= 0; i
< 5; i
++)
2946 if (! FLOAT_WORDS_BIG_ENDIAN
)
2948 for (i
= 0; i
< 5; i
++)
2953 p
= &yy
[0] + (NE
- 1);
2956 for (i
= 0; i
< 4; i
++)
2961 /* Point to the exponent field and check max exponent cases. */
2966 if (! FLOAT_WORDS_BIG_ENDIAN
)
2968 for (i
= 0; i
< 4; i
++)
2970 if ((i
!= 3 && pe
[i
] != 0)
2971 /* Anything but 0x8000 here, including 0, is a NaN. */
2972 || (i
== 3 && pe
[i
] != 0x8000))
2974 enan (y
, (*p
& 0x8000) != 0);
2981 for (i
= 1; i
<= 4; i
++)
2985 enan (y
, (*p
& 0x8000) != 0);
2997 #endif /* INFINITY */
3000 for (i
= 0; i
< NE
; i
++)
3007 unsigned EMUSHORT
*pe
, *y
;
3009 register unsigned EMUSHORT r
;
3010 unsigned EMUSHORT
*e
, *p
;
3011 unsigned EMUSHORT yy
[NI
];
3018 if (! FLOAT_WORDS_BIG_ENDIAN
)
3030 if (! FLOAT_WORDS_BIG_ENDIAN
)
3032 for (i
= 0; i
< 7; i
++)
3036 enan (y
, yy
[0] != 0);
3043 for (i
= 1; i
< 8; i
++)
3047 enan (y
, yy
[0] != 0);
3059 #endif /* INFINITY */
3063 if (! FLOAT_WORDS_BIG_ENDIAN
)
3065 for (i
= 0; i
< 7; i
++)
3071 for (i
= 0; i
< 7; i
++)
3075 /* If denormal, remove the implied bit; else shift down 1. */
3089 /* Convert IEEE single precision to e type. */
3093 unsigned EMUSHORT
*pe
, *y
;
3097 ibmtoe (pe
, y
, SFmode
);
3100 register unsigned EMUSHORT r
;
3101 register unsigned EMUSHORT
*e
, *p
;
3102 unsigned EMUSHORT yy
[NI
];
3106 denorm
= 0; /* flag if denormalized number */
3109 if (! FLOAT_WORDS_BIG_ENDIAN
)
3119 yy
[M
] = (r
& 0x7f) | 0200;
3120 r
&= ~0x807f; /* strip sign and 7 significand bits */
3125 if (FLOAT_WORDS_BIG_ENDIAN
)
3127 if (((pe
[0] & 0x7f) != 0) || (pe
[1] != 0))
3129 enan (y
, yy
[0] != 0);
3135 if (((pe
[1] & 0x7f) != 0) || (pe
[0] != 0))
3137 enan (y
, yy
[0] != 0);
3148 #endif /* INFINITY */
3150 /* If zero exponent, then the significand is denormalized.
3151 So take back the understood high significand bit. */
3164 if (! FLOAT_WORDS_BIG_ENDIAN
)
3174 { /* if zero exponent, then normalize the significand */
3175 if ((k
= enormlz (yy
)) > NBITS
)
3178 yy
[E
] -= (unsigned EMUSHORT
) (k
- 1);
3181 #endif /* not IBM */
3187 unsigned EMUSHORT
*x
, *e
;
3189 unsigned EMUSHORT xi
[NI
];
3196 make_nan (e
, eisneg (x
), TFmode
);
3201 exp
= (EMULONG
) xi
[E
];
3206 /* round off to nearest or even */
3209 emdnorm (xi
, 0, 0, exp
, 64);
3215 /* Move out internal format to ieee long double */
3219 unsigned EMUSHORT
*a
, *b
;
3221 register unsigned EMUSHORT
*p
, *q
;
3222 unsigned EMUSHORT i
;
3227 make_nan (b
, eiisneg (a
), TFmode
);
3232 if (FLOAT_WORDS_BIG_ENDIAN
)
3235 q
= b
+ 7; /* point to output exponent */
3237 /* If not denormal, delete the implied bit. */
3242 /* combine sign and exponent */
3244 if (FLOAT_WORDS_BIG_ENDIAN
)
3247 *q
++ = *p
++ | 0x8000;
3254 *q
-- = *p
++ | 0x8000;
3258 /* skip over guard word */
3260 /* move the significand */
3261 if (FLOAT_WORDS_BIG_ENDIAN
)
3263 for (i
= 0; i
< 7; i
++)
3268 for (i
= 0; i
< 7; i
++)
3275 unsigned EMUSHORT
*x
, *e
;
3277 unsigned EMUSHORT xi
[NI
];
3284 make_nan (e
, eisneg (x
), XFmode
);
3289 /* adjust exponent for offset */
3290 exp
= (EMULONG
) xi
[E
];
3295 /* round off to nearest or even */
3298 emdnorm (xi
, 0, 0, exp
, 64);
3305 /* Move out internal format to ieee long double. */
3309 unsigned EMUSHORT
*a
, *b
;
3311 register unsigned EMUSHORT
*p
, *q
;
3312 unsigned EMUSHORT i
;
3317 make_nan (b
, eiisneg (a
), XFmode
);
3329 if (FLOAT_WORDS_BIG_ENDIAN
)
3333 q
= b
+ 4; /* point to output exponent */
3334 #if LONG_DOUBLE_TYPE_SIZE == 96
3335 /* Clear the last two bytes of 12-byte Intel format */
3341 /* combine sign and exponent */
3345 *q
++ = *p
++ | 0x8000;
3352 *q
-- = *p
++ | 0x8000;
3357 if (FLOAT_WORDS_BIG_ENDIAN
)
3360 *q
++ = *p
++ | 0x8000;
3368 *q
-- = *p
++ | 0x8000;
3373 /* skip over guard word */
3375 /* move the significand */
3377 for (i
= 0; i
< 4; i
++)
3381 for (i
= 0; i
< 4; i
++)
3385 if (FLOAT_WORDS_BIG_ENDIAN
)
3387 for (i
= 0; i
< 4; i
++)
3395 /* Intel long double infinity significand. */
3403 for (i
= 0; i
< 4; i
++)
3410 /* e type to IEEE double precision. */
3416 unsigned EMUSHORT
*x
, *e
;
3418 etodec (x
, e
); /* see etodec.c */
3423 unsigned EMUSHORT
*x
, *y
;
3433 unsigned EMUSHORT
*x
, *e
;
3435 etoibm (x
, e
, DFmode
);
3440 unsigned EMUSHORT
*x
, *y
;
3442 toibm (x
, y
, DFmode
);
3445 #else /* it's neither DEC nor IBM */
3449 unsigned EMUSHORT
*x
, *e
;
3451 unsigned EMUSHORT xi
[NI
];
3458 make_nan (e
, eisneg (x
), DFmode
);
3463 /* adjust exponent for offsets */
3464 exp
= (EMULONG
) xi
[E
] - (EXONE
- 0x3ff);
3469 /* round off to nearest or even */
3472 emdnorm (xi
, 0, 0, exp
, 64);
3481 unsigned EMUSHORT
*x
, *y
;
3483 unsigned EMUSHORT i
;
3484 unsigned EMUSHORT
*p
;
3489 make_nan (y
, eiisneg (x
), DFmode
);
3495 if (! FLOAT_WORDS_BIG_ENDIAN
)
3498 *y
= 0; /* output high order */
3500 *y
= 0x8000; /* output sign bit */
3503 if (i
>= (unsigned int) 2047)
3504 { /* Saturate at largest number less than infinity. */
3507 if (! FLOAT_WORDS_BIG_ENDIAN
)
3521 *y
|= (unsigned EMUSHORT
) 0x7fef;
3522 if (! FLOAT_WORDS_BIG_ENDIAN
)
3547 i
|= *p
++ & (unsigned EMUSHORT
) 0x0f; /* *p = xi[M] */
3548 *y
|= (unsigned EMUSHORT
) i
; /* high order output already has sign bit set */
3549 if (! FLOAT_WORDS_BIG_ENDIAN
)
3564 #endif /* not IBM */
3565 #endif /* not DEC */
3569 /* e type to IEEE single precision. */
3575 unsigned EMUSHORT
*x
, *e
;
3577 etoibm (x
, e
, SFmode
);
3582 unsigned EMUSHORT
*x
, *y
;
3584 toibm (x
, y
, SFmode
);
3591 unsigned EMUSHORT
*x
, *e
;
3594 unsigned EMUSHORT xi
[NI
];
3600 make_nan (e
, eisneg (x
), SFmode
);
3605 /* adjust exponent for offsets */
3606 exp
= (EMULONG
) xi
[E
] - (EXONE
- 0177);
3611 /* round off to nearest or even */
3614 emdnorm (xi
, 0, 0, exp
, 64);
3622 unsigned EMUSHORT
*x
, *y
;
3624 unsigned EMUSHORT i
;
3625 unsigned EMUSHORT
*p
;
3630 make_nan (y
, eiisneg (x
), SFmode
);
3636 if (! FLOAT_WORDS_BIG_ENDIAN
)
3642 *y
= 0; /* output high order */
3644 *y
= 0x8000; /* output sign bit */
3647 /* Handle overflow cases. */
3651 *y
|= (unsigned EMUSHORT
) 0x7f80;
3656 if (! FLOAT_WORDS_BIG_ENDIAN
)
3664 #else /* no INFINITY */
3665 *y
|= (unsigned EMUSHORT
) 0x7f7f;
3670 if (! FLOAT_WORDS_BIG_ENDIAN
)
3681 #endif /* no INFINITY */
3693 i
|= *p
++ & (unsigned EMUSHORT
) 0x7f; /* *p = xi[M] */
3694 *y
|= i
; /* high order output already has sign bit set */
3699 if (! FLOAT_WORDS_BIG_ENDIAN
)
3708 #endif /* not IBM */
3710 /* Compare two e type numbers.
3714 -2 if either a or b is a NaN. */
3718 unsigned EMUSHORT
*a
, *b
;
3720 unsigned EMUSHORT ai
[NI
], bi
[NI
];
3721 register unsigned EMUSHORT
*p
, *q
;
3726 if (eisnan (a
) || eisnan (b
))
3735 { /* the signs are different */
3737 for (i
= 1; i
< NI
- 1; i
++)
3751 /* both are the same sign */
3766 return (0); /* equality */
3772 if (*(--p
) > *(--q
))
3773 return (msign
); /* p is bigger */
3775 return (-msign
); /* p is littler */
3781 /* Find nearest integer to x = floor (x + 0.5). */
3785 unsigned EMUSHORT
*x
, *y
;
3794 /* Convert HOST_WIDE_INT to e type. */
3799 unsigned EMUSHORT
*y
;
3801 unsigned EMUSHORT yi
[NI
];
3802 unsigned HOST_WIDE_INT ll
;
3808 /* make it positive */
3809 ll
= (unsigned HOST_WIDE_INT
) (-(*lp
));
3810 yi
[0] = 0xffff; /* put correct sign in the e type number */
3814 ll
= (unsigned HOST_WIDE_INT
) (*lp
);
3816 /* move the long integer to yi significand area */
3817 #if HOST_BITS_PER_WIDE_INT == 64
3818 yi
[M
] = (unsigned EMUSHORT
) (ll
>> 48);
3819 yi
[M
+ 1] = (unsigned EMUSHORT
) (ll
>> 32);
3820 yi
[M
+ 2] = (unsigned EMUSHORT
) (ll
>> 16);
3821 yi
[M
+ 3] = (unsigned EMUSHORT
) ll
;
3822 yi
[E
] = EXONE
+ 47; /* exponent if normalize shift count were 0 */
3824 yi
[M
] = (unsigned EMUSHORT
) (ll
>> 16);
3825 yi
[M
+ 1] = (unsigned EMUSHORT
) ll
;
3826 yi
[E
] = EXONE
+ 15; /* exponent if normalize shift count were 0 */
3829 if ((k
= enormlz (yi
)) > NBITS
)/* normalize the significand */
3830 ecleaz (yi
); /* it was zero */
3832 yi
[E
] -= (unsigned EMUSHORT
) k
;/* subtract shift count from exponent */
3833 emovo (yi
, y
); /* output the answer */
3836 /* Convert unsigned HOST_WIDE_INT to e type. */
3840 unsigned HOST_WIDE_INT
*lp
;
3841 unsigned EMUSHORT
*y
;
3843 unsigned EMUSHORT yi
[NI
];
3844 unsigned HOST_WIDE_INT ll
;
3850 /* move the long integer to ayi significand area */
3851 #if HOST_BITS_PER_WIDE_INT == 64
3852 yi
[M
] = (unsigned EMUSHORT
) (ll
>> 48);
3853 yi
[M
+ 1] = (unsigned EMUSHORT
) (ll
>> 32);
3854 yi
[M
+ 2] = (unsigned EMUSHORT
) (ll
>> 16);
3855 yi
[M
+ 3] = (unsigned EMUSHORT
) ll
;
3856 yi
[E
] = EXONE
+ 47; /* exponent if normalize shift count were 0 */
3858 yi
[M
] = (unsigned EMUSHORT
) (ll
>> 16);
3859 yi
[M
+ 1] = (unsigned EMUSHORT
) ll
;
3860 yi
[E
] = EXONE
+ 15; /* exponent if normalize shift count were 0 */
3863 if ((k
= enormlz (yi
)) > NBITS
)/* normalize the significand */
3864 ecleaz (yi
); /* it was zero */
3866 yi
[E
] -= (unsigned EMUSHORT
) k
; /* subtract shift count from exponent */
3867 emovo (yi
, y
); /* output the answer */
3871 /* Find signed HOST_WIDE_INT integer and floating point fractional
3872 parts of e-type (packed internal format) floating point input X.
3873 The integer output I has the sign of the input, except that
3874 positive overflow is permitted if FIXUNS_TRUNC_LIKE_FIX_TRUNC.
3875 The output e-type fraction FRAC is the positive fractional
3880 unsigned EMUSHORT
*x
;
3882 unsigned EMUSHORT
*frac
;
3884 unsigned EMUSHORT xi
[NI
];
3886 unsigned HOST_WIDE_INT ll
;
3889 k
= (int) xi
[E
] - (EXONE
- 1);
3892 /* if exponent <= 0, integer = 0 and real output is fraction */
3897 if (k
> (HOST_BITS_PER_WIDE_INT
- 1))
3899 /* long integer overflow: output large integer
3900 and correct fraction */
3902 *i
= ((unsigned HOST_WIDE_INT
) 1) << (HOST_BITS_PER_WIDE_INT
- 1);
3905 #ifdef FIXUNS_TRUNC_LIKE_FIX_TRUNC
3906 /* In this case, let it overflow and convert as if unsigned. */
3907 euifrac (x
, &ll
, frac
);
3908 *i
= (HOST_WIDE_INT
) ll
;
3911 /* In other cases, return the largest positive integer. */
3912 *i
= (((unsigned HOST_WIDE_INT
) 1) << (HOST_BITS_PER_WIDE_INT
- 1)) - 1;
3917 warning ("overflow on truncation to integer");
3921 /* Shift more than 16 bits: first shift up k-16 mod 16,
3922 then shift up by 16's. */
3923 j
= k
- ((k
>> 4) << 4);
3930 ll
= (ll
<< 16) | xi
[M
];
3932 while ((k
-= 16) > 0);
3939 /* shift not more than 16 bits */
3941 *i
= (HOST_WIDE_INT
) xi
[M
] & 0xffff;
3948 if ((k
= enormlz (xi
)) > NBITS
)
3951 xi
[E
] -= (unsigned EMUSHORT
) k
;
3957 /* Find unsigned HOST_WIDE_INT integer and floating point fractional parts.
3958 A negative e type input yields integer output = 0
3959 but correct fraction. */
3962 euifrac (x
, i
, frac
)
3963 unsigned EMUSHORT
*x
;
3964 unsigned HOST_WIDE_INT
*i
;
3965 unsigned EMUSHORT
*frac
;
3967 unsigned HOST_WIDE_INT ll
;
3968 unsigned EMUSHORT xi
[NI
];
3972 k
= (int) xi
[E
] - (EXONE
- 1);
3975 /* if exponent <= 0, integer = 0 and argument is fraction */
3980 if (k
> HOST_BITS_PER_WIDE_INT
)
3982 /* Long integer overflow: output large integer
3983 and correct fraction.
3984 Note, the BSD microvax compiler says that ~(0UL)
3985 is a syntax error. */
3989 warning ("overflow on truncation to unsigned integer");
3993 /* Shift more than 16 bits: first shift up k-16 mod 16,
3994 then shift up by 16's. */
3995 j
= k
- ((k
>> 4) << 4);
4002 ll
= (ll
<< 16) | xi
[M
];
4004 while ((k
-= 16) > 0);
4009 /* shift not more than 16 bits */
4011 *i
= (HOST_WIDE_INT
) xi
[M
] & 0xffff;
4014 if (xi
[0]) /* A negative value yields unsigned integer 0. */
4020 if ((k
= enormlz (xi
)) > NBITS
)
4023 xi
[E
] -= (unsigned EMUSHORT
) k
;
4030 /* Shift significand area up or down by the number of bits given by SC. */
4034 unsigned EMUSHORT
*x
;
4037 unsigned EMUSHORT lost
;
4038 unsigned EMUSHORT
*p
;
4051 lost
|= *p
; /* remember lost bits */
4092 return ((int) lost
);
4097 /* Shift normalize the significand area pointed to by argument.
4098 Shift count (up = positive) is returned. */
4102 unsigned EMUSHORT x
[];
4104 register unsigned EMUSHORT
*p
;
4113 return (0); /* already normalized */
4119 /* With guard word, there are NBITS+16 bits available.
4120 Return true if all are zero. */
4124 /* see if high byte is zero */
4125 while ((*p
& 0xff00) == 0)
4130 /* now shift 1 bit at a time */
4131 while ((*p
& 0x8000) == 0)
4137 mtherr ("enormlz", UNDERFLOW
);
4143 /* Normalize by shifting down out of the high guard word
4144 of the significand */
4159 mtherr ("enormlz", OVERFLOW
);
4169 /* Convert e type number to decimal format ASCII string.
4170 The constants are for 64 bit precision. */
4175 #if LONG_DOUBLE_TYPE_SIZE == 128
4176 static unsigned EMUSHORT etens
[NTEN
+ 1][NE
] =
4178 {0x6576, 0x4a92, 0x804a, 0x153f,
4179 0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
4180 {0x6a32, 0xce52, 0x329a, 0x28ce,
4181 0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
4182 {0x526c, 0x50ce, 0xf18b, 0x3d28,
4183 0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
4184 {0x9c66, 0x58f8, 0xbc50, 0x5c54,
4185 0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
4186 {0x851e, 0xeab7, 0x98fe, 0x901b,
4187 0xddbb, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
4188 {0x0235, 0x0137, 0x36b1, 0x336c,
4189 0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
4190 {0x50f8, 0x25fb, 0xc76b, 0x6b71,
4191 0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
4192 {0x0000, 0x0000, 0x0000, 0x0000,
4193 0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
4194 {0x0000, 0x0000, 0x0000, 0x0000,
4195 0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
4196 {0x0000, 0x0000, 0x0000, 0x0000,
4197 0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
4198 {0x0000, 0x0000, 0x0000, 0x0000,
4199 0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
4200 {0x0000, 0x0000, 0x0000, 0x0000,
4201 0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
4202 {0x0000, 0x0000, 0x0000, 0x0000,
4203 0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
4206 static unsigned EMUSHORT emtens
[NTEN
+ 1][NE
] =
4208 {0x2030, 0xcffc, 0xa1c3, 0x8123,
4209 0x2de3, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
4210 {0x8264, 0xd2cb, 0xf2ea, 0x12d4,
4211 0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
4212 {0xf53f, 0xf698, 0x6bd3, 0x0158,
4213 0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
4214 {0xe731, 0x04d4, 0xe3f2, 0xd332,
4215 0x7132, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
4216 {0xa23e, 0x5308, 0xfefb, 0x1155,
4217 0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
4218 {0xe26d, 0xdbde, 0xd05d, 0xb3f6,
4219 0xac7c, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
4220 {0x2a20, 0x6224, 0x47b3, 0x98d7,
4221 0x3f23, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
4222 {0x0b5b, 0x4af2, 0xa581, 0x18ed,
4223 0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
4224 {0xbf71, 0xa9b3, 0x7989, 0xbe68,
4225 0x4c2e, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
4226 {0x3d4d, 0x7c3d, 0x36ba, 0x0d2b,
4227 0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
4228 {0xc155, 0xa4a8, 0x404e, 0x6113,
4229 0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
4230 {0xd70a, 0x70a3, 0x0a3d, 0xa3d7,
4231 0x3d70, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
4232 {0xcccd, 0xcccc, 0xcccc, 0xcccc,
4233 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
4236 /* LONG_DOUBLE_TYPE_SIZE is other than 128 */
4237 static unsigned EMUSHORT etens
[NTEN
+ 1][NE
] =
4239 {0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
4240 {0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
4241 {0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
4242 {0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
4243 {0xddbc, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
4244 {0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
4245 {0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
4246 {0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
4247 {0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
4248 {0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
4249 {0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
4250 {0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
4251 {0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
4254 static unsigned EMUSHORT emtens
[NTEN
+ 1][NE
] =
4256 {0x2de4, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
4257 {0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
4258 {0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
4259 {0x7133, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
4260 {0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
4261 {0xac7d, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
4262 {0x3f24, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
4263 {0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
4264 {0x4c2f, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
4265 {0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
4266 {0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
4267 {0x3d71, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
4268 {0xcccd, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
4273 e24toasc (x
, string
, ndigs
)
4274 unsigned EMUSHORT x
[];
4278 unsigned EMUSHORT w
[NI
];
4281 etoasc (w
, string
, ndigs
);
4286 e53toasc (x
, string
, ndigs
)
4287 unsigned EMUSHORT x
[];
4291 unsigned EMUSHORT w
[NI
];
4294 etoasc (w
, string
, ndigs
);
4299 e64toasc (x
, string
, ndigs
)
4300 unsigned EMUSHORT x
[];
4304 unsigned EMUSHORT w
[NI
];
4307 etoasc (w
, string
, ndigs
);
4311 e113toasc (x
, string
, ndigs
)
4312 unsigned EMUSHORT x
[];
4316 unsigned EMUSHORT w
[NI
];
4319 etoasc (w
, string
, ndigs
);
4323 static char wstring
[80]; /* working storage for ASCII output */
4326 etoasc (x
, string
, ndigs
)
4327 unsigned EMUSHORT x
[];
4332 unsigned EMUSHORT y
[NI
], t
[NI
], u
[NI
], w
[NI
];
4333 unsigned EMUSHORT
*p
, *r
, *ten
;
4334 unsigned EMUSHORT sign
;
4335 int i
, j
, k
, expon
, rndsav
;
4337 unsigned EMUSHORT m
;
4348 sprintf (wstring
, " NaN ");
4352 rndprc
= NBITS
; /* set to full precision */
4353 emov (x
, y
); /* retain external format */
4354 if (y
[NE
- 1] & 0x8000)
4357 y
[NE
- 1] &= 0x7fff;
4364 ten
= &etens
[NTEN
][0];
4366 /* Test for zero exponent */
4369 for (k
= 0; k
< NE
- 1; k
++)
4372 goto tnzro
; /* denormalized number */
4374 goto isone
; /* valid all zeros */
4378 /* Test for infinity. */
4379 if (y
[NE
- 1] == 0x7fff)
4382 sprintf (wstring
, " -Infinity ");
4384 sprintf (wstring
, " Infinity ");
4388 /* Test for exponent nonzero but significand denormalized.
4389 * This is an error condition.
4391 if ((y
[NE
- 1] != 0) && ((y
[NE
- 2] & 0x8000) == 0))
4393 mtherr ("etoasc", DOMAIN
);
4394 sprintf (wstring
, "NaN");
4398 /* Compare to 1.0 */
4407 { /* Number is greater than 1 */
4408 /* Convert significand to an integer and strip trailing decimal zeros. */
4410 u
[NE
- 1] = EXONE
+ NBITS
- 1;
4412 p
= &etens
[NTEN
- 4][0];
4418 for (j
= 0; j
< NE
- 1; j
++)
4431 /* Rescale from integer significand */
4432 u
[NE
- 1] += y
[NE
- 1] - (unsigned int) (EXONE
+ NBITS
- 1);
4434 /* Find power of 10 */
4438 /* An unordered compare result shouldn't happen here. */
4439 while (ecmp (ten
, u
) <= 0)
4441 if (ecmp (p
, u
) <= 0)
4454 { /* Number is less than 1.0 */
4455 /* Pad significand with trailing decimal zeros. */
4458 while ((y
[NE
- 2] & 0x8000) == 0)
4467 for (i
= 0; i
< NDEC
+ 1; i
++)
4469 if ((w
[NI
- 1] & 0x7) != 0)
4471 /* multiply by 10 */
4484 if (eone
[NE
- 1] <= u
[1])
4496 while (ecmp (eone
, w
) > 0)
4498 if (ecmp (p
, w
) >= 0)
4513 /* Find the first (leading) digit. */
4519 digit
= equot
[NI
- 1];
4520 while ((digit
== 0) && (ecmp (y
, ezero
) != 0))
4528 digit
= equot
[NI
- 1];
4536 /* Examine number of digits requested by caller. */
4554 *s
++ = (char)digit
+ '0';
4557 /* Generate digits after the decimal point. */
4558 for (k
= 0; k
<= ndigs
; k
++)
4560 /* multiply current number by 10, without normalizing */
4567 *s
++ = (char) equot
[NI
- 1] + '0';
4569 digit
= equot
[NI
- 1];
4572 /* round off the ASCII string */
4575 /* Test for critical rounding case in ASCII output. */
4579 if (ecmp (t
, ezero
) != 0)
4580 goto roun
; /* round to nearest */
4581 if ((*(s
- 1) & 1) == 0)
4582 goto doexp
; /* round to even */
4584 /* Round up and propagate carry-outs */
4588 /* Carry out to most significant digit? */
4595 /* Most significant digit carries to 10? */
4603 /* Round up and carry out from less significant digits */
4615 sprintf (ss, "e+%d", expon);
4617 sprintf (ss, "e%d", expon);
4619 sprintf (ss
, "e%d", expon
);
4622 /* copy out the working string */
4625 while (*ss
== ' ') /* strip possible leading space */
4627 while ((*s
++ = *ss
++) != '\0')
4632 /* Convert ASCII string to quadruple precision floating point
4634 Numeric input is free field decimal number with max of 15 digits with or
4635 without decimal point entered as ASCII from teletype. Entering E after
4636 the number followed by a second number causes the second number to be
4637 interpreted as a power of 10 to be multiplied by the first number
4638 (i.e., "scientific" notation). */
4640 /* ASCII to single */
4645 unsigned EMUSHORT
*y
;
4651 /* ASCII to double */
4656 unsigned EMUSHORT
*y
;
4658 #if defined(DEC) || defined(IBM)
4666 /* ASCII to long double */
4671 unsigned EMUSHORT
*y
;
4676 /* ASCII to 128-bit long double */
4681 unsigned EMUSHORT
*y
;
4683 asctoeg (s
, y
, 113);
4686 /* ASCII to super double */
4691 unsigned EMUSHORT
*y
;
4693 asctoeg (s
, y
, NBITS
);
4697 /* ASCII to e type, with specified rounding precision = oprec. */
4700 asctoeg (ss
, y
, oprec
)
4702 unsigned EMUSHORT
*y
;
4705 unsigned EMUSHORT yy
[NI
], xt
[NI
], tt
[NI
];
4706 int esign
, decflg
, sgnflg
, nexp
, exp
, prec
, lost
;
4707 int k
, trail
, c
, rndsav
;
4709 unsigned EMUSHORT nsign
, *p
;
4710 char *sp
, *s
, *lstr
;
4712 /* Copy the input string. */
4713 lstr
= (char *) alloca (strlen (ss
) + 1);
4715 while (*s
== ' ') /* skip leading spaces */
4718 while ((*sp
++ = *s
++) != '\0')
4723 rndprc
= NBITS
; /* Set to full precision */
4736 if ((k
>= 0) && (k
<= 9))
4738 /* Ignore leading zeros */
4739 if ((prec
== 0) && (decflg
== 0) && (k
== 0))
4741 /* Identify and strip trailing zeros after the decimal point. */
4742 if ((trail
== 0) && (decflg
!= 0))
4745 while ((*sp
>= '0') && (*sp
<= '9'))
4747 /* Check for syntax error */
4749 if ((c
!= 'e') && (c
!= 'E') && (c
!= '\0')
4750 && (c
!= '\n') && (c
!= '\r') && (c
!= ' ')
4761 /* If enough digits were given to more than fill up the yy register,
4762 continuing until overflow into the high guard word yy[2]
4763 guarantees that there will be a roundoff bit at the top
4764 of the low guard word after normalization. */
4769 nexp
+= 1; /* count digits after decimal point */
4770 eshup1 (yy
); /* multiply current number by 10 */
4776 xt
[NI
- 2] = (unsigned EMUSHORT
) k
;
4781 /* Mark any lost non-zero digit. */
4783 /* Count lost digits before the decimal point. */
4798 case '.': /* decimal point */
4828 mtherr ("asctoe", DOMAIN
);
4837 /* Exponent interpretation */
4843 /* check for + or - */
4851 while ((*s
>= '0') && (*s
<= '9'))
4855 if (exp
> -(MINDECEXP
))
4865 if (exp
> MAXDECEXP
)
4869 yy
[E
] = 0x7fff; /* infinity */
4872 if (exp
< MINDECEXP
)
4881 /* Pad trailing zeros to minimize power of 10, per IEEE spec. */
4882 while ((nexp
> 0) && (yy
[2] == 0))
4894 if ((k
= enormlz (yy
)) > NBITS
)
4899 lexp
= (EXONE
- 1 + NBITS
) - k
;
4900 emdnorm (yy
, lost
, 0, lexp
, 64);
4902 /* Convert to external format:
4904 Multiply by 10**nexp. If precision is 64 bits,
4905 the maximum relative error incurred in forming 10**n
4906 for 0 <= n <= 324 is 8.2e-20, at 10**180.
4907 For 0 <= n <= 999, the peak relative error is 1.4e-19 at 10**947.
4908 For 0 >= n >= -999, it is -1.55e-19 at 10**-435. */
4923 /* Punt. Can't handle this without 2 divides. */
4924 emovi (etens
[0], tt
);
4931 p
= &etens
[NTEN
][0];
4941 while (exp
<= MAXP
);
4959 /* Round and convert directly to the destination type */
4961 lexp
-= EXONE
- 0x3ff;
4963 else if (oprec
== 24 || oprec
== 56)
4964 lexp
-= EXONE
- (0x41 << 2);
4966 else if (oprec
== 24)
4967 lexp
-= EXONE
- 0177;
4970 else if (oprec
== 56)
4971 lexp
-= EXONE
- 0201;
4974 emdnorm (yy
, k
, 0, lexp
, 64);
4984 todec (yy
, y
); /* see etodec.c */
4989 toibm (yy
, y
, DFmode
);
5012 /* y = largest integer not greater than x (truncated toward minus infinity) */
5014 static unsigned EMUSHORT bmask
[] =
5037 unsigned EMUSHORT x
[], y
[];
5039 register unsigned EMUSHORT
*p
;
5041 unsigned EMUSHORT f
[NE
];
5043 emov (x
, f
); /* leave in external format */
5044 expon
= (int) f
[NE
- 1];
5045 e
= (expon
& 0x7fff) - (EXONE
- 1);
5051 /* number of bits to clear out */
5063 /* clear the remaining bits */
5065 /* truncate negatives toward minus infinity */
5068 if ((unsigned EMUSHORT
) expon
& (unsigned EMUSHORT
) 0x8000)
5070 for (i
= 0; i
< NE
- 1; i
++)
5082 /* Returns s and exp such that s * 2**exp = x and .5 <= s < 1.
5083 For example, 1.1 = 0.55 * 2**1
5084 Handles denormalized numbers properly using long integer exp. */
5088 unsigned EMUSHORT x
[];
5090 unsigned EMUSHORT s
[];
5092 unsigned EMUSHORT xi
[NI
];
5096 li
= (EMULONG
) ((EMUSHORT
) xi
[1]);
5104 *exp
= (int) (li
- 0x3ffe);
5109 /* Return y = x * 2**pwr2. */
5113 unsigned EMUSHORT x
[];
5115 unsigned EMUSHORT y
[];
5117 unsigned EMUSHORT xi
[NI
];
5125 emdnorm (xi
, i
, i
, li
, 64);
5130 /* c = remainder after dividing b by a
5131 Least significant integer quotient bits left in equot[]. */
5135 unsigned EMUSHORT a
[], b
[], c
[];
5137 unsigned EMUSHORT den
[NI
], num
[NI
];
5141 || (ecmp (a
, ezero
) == 0)
5149 if (ecmp (a
, ezero
) == 0)
5151 mtherr ("eremain", SING
);
5157 eiremain (den
, num
);
5158 /* Sign of remainder = sign of quotient */
5168 unsigned EMUSHORT den
[], num
[];
5171 unsigned EMUSHORT j
;
5174 ld
-= enormlz (den
);
5176 ln
-= enormlz (num
);
5180 if (ecmpm (den
, num
) <= 0)
5194 emdnorm (num
, 0, 0, ln
, 0);
5197 /* This routine may be called to report one of the following
5198 error conditions (in the include file mconf.h).
5200 Mnemonic Value Significance
5202 DOMAIN 1 argument domain error
5203 SING 2 function singularity
5204 OVERFLOW 3 overflow range error
5205 UNDERFLOW 4 underflow range error
5206 TLOSS 5 total loss of precision
5207 PLOSS 6 partial loss of precision
5208 INVALID 7 NaN - producing operation
5209 EDOM 33 Unix domain error code
5210 ERANGE 34 Unix range error code
5212 The default version of the file prints the function name,
5213 passed to it by the pointer fctnam, followed by the
5214 error condition. The display is directed to the standard
5215 output device. The routine then returns to the calling
5216 program. Users may wish to modify the program to abort by
5217 calling exit under severe error conditions such as domain
5220 Since all error conditions pass control to this function,
5221 the display may be easily changed, eliminated, or directed
5222 to an error logging device. */
5224 /* Note: the order of appearance of the following messages is bound to the
5225 error codes defined above. */
5228 static char *ermsg
[NMSGS
] =
5230 "unknown", /* error code 0 */
5231 "domain", /* error code 1 */
5232 "singularity", /* et seq. */
5235 "total loss of precision",
5236 "partial loss of precision",
5250 /* Display string passed by calling program, which is supposed to be the
5251 name of the function in which the error occurred.
5253 Display error message defined by the code argument. */
5255 if ((code
<= 0) || (code
>= NMSGS
))
5257 sprintf (errstr
, " %s %s error", name
, ermsg
[code
]);
5260 /* Set global error message word */
5265 /* Convert DEC double precision to e type. */
5269 unsigned EMUSHORT
*d
;
5270 unsigned EMUSHORT
*e
;
5272 unsigned EMUSHORT y
[NI
];
5273 register unsigned EMUSHORT r
, *p
;
5275 ecleaz (y
); /* start with a zero */
5276 p
= y
; /* point to our number */
5277 r
= *d
; /* get DEC exponent word */
5278 if (*d
& (unsigned int) 0x8000)
5279 *p
= 0xffff; /* fill in our sign */
5280 ++p
; /* bump pointer to our exponent word */
5281 r
&= 0x7fff; /* strip the sign bit */
5282 if (r
== 0) /* answer = 0 if high order DEC word = 0 */
5286 r
>>= 7; /* shift exponent word down 7 bits */
5287 r
+= EXONE
- 0201; /* subtract DEC exponent offset */
5288 /* add our e type exponent offset */
5289 *p
++ = r
; /* to form our exponent */
5291 r
= *d
++; /* now do the high order mantissa */
5292 r
&= 0177; /* strip off the DEC exponent and sign bits */
5293 r
|= 0200; /* the DEC understood high order mantissa bit */
5294 *p
++ = r
; /* put result in our high guard word */
5296 *p
++ = *d
++; /* fill in the rest of our mantissa */
5300 eshdn8 (y
); /* shift our mantissa down 8 bits */
5308 ; convert e type to DEC double precision
5316 unsigned EMUSHORT
*x
, *d
;
5318 unsigned EMUSHORT xi
[NI
];
5323 exp
= (EMULONG
) xi
[E
] - (EXONE
- 0201); /* adjust exponent for offsets */
5324 /* round off to nearest or even */
5327 emdnorm (xi
, 0, 0, exp
, 64);
5334 unsigned EMUSHORT
*x
, *y
;
5336 unsigned EMUSHORT i
;
5337 unsigned EMUSHORT
*p
;
5376 /* Convert IBM single/double precision to e type. */
5380 unsigned EMUSHORT
*d
;
5381 unsigned EMUSHORT
*e
;
5382 enum machine_mode mode
;
5384 unsigned EMUSHORT y
[NI
];
5385 register unsigned EMUSHORT r
, *p
;
5388 ecleaz (y
); /* start with a zero */
5389 p
= y
; /* point to our number */
5390 r
= *d
; /* get IBM exponent word */
5391 if (*d
& (unsigned int) 0x8000)
5392 *p
= 0xffff; /* fill in our sign */
5393 ++p
; /* bump pointer to our exponent word */
5394 r
&= 0x7f00; /* strip the sign bit */
5395 r
>>= 6; /* shift exponent word down 6 bits */
5396 /* in fact shift by 8 right and 2 left */
5397 r
+= EXONE
- (0x41 << 2); /* subtract IBM exponent offset */
5398 /* add our e type exponent offset */
5399 *p
++ = r
; /* to form our exponent */
5401 *p
++ = *d
++ & 0xff; /* now do the high order mantissa */
5402 /* strip off the IBM exponent and sign bits */
5403 if (mode
!= SFmode
) /* there are only 2 words in SFmode */
5405 *p
++ = *d
++; /* fill in the rest of our mantissa */
5410 if (y
[M
] == 0 && y
[M
+1] == 0 && y
[M
+2] == 0 && y
[M
+3] == 0)
5413 y
[E
] -= 5 + enormlz (y
); /* now normalise the mantissa */
5414 /* handle change in RADIX */
5420 /* Convert e type to IBM single/double precision. */
5424 unsigned EMUSHORT
*x
, *d
;
5425 enum machine_mode mode
;
5427 unsigned EMUSHORT xi
[NI
];
5432 exp
= (EMULONG
) xi
[E
] - (EXONE
- (0x41 << 2)); /* adjust exponent for offsets */
5433 /* round off to nearest or even */
5436 emdnorm (xi
, 0, 0, exp
, 64);
5438 toibm (xi
, d
, mode
);
5443 unsigned EMUSHORT
*x
, *y
;
5444 enum machine_mode mode
;
5446 unsigned EMUSHORT i
;
5447 unsigned EMUSHORT
*p
;
5495 /* Output a binary NaN bit pattern in the target machine's format. */
5497 /* If special NaN bit patterns are required, define them in tm.h
5498 as arrays of unsigned 16-bit shorts. Otherwise, use the default
5504 unsigned EMUSHORT TFbignan
[8] =
5505 {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
5506 unsigned EMUSHORT TFlittlenan
[8] = {0, 0, 0, 0, 0, 0, 0x8000, 0xffff};
5514 unsigned EMUSHORT XFbignan
[6] =
5515 {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
5516 unsigned EMUSHORT XFlittlenan
[6] = {0, 0, 0, 0xc000, 0xffff, 0};
5524 unsigned EMUSHORT DFbignan
[4] = {0x7fff, 0xffff, 0xffff, 0xffff};
5525 unsigned EMUSHORT DFlittlenan
[4] = {0, 0, 0, 0xfff8};
5533 unsigned EMUSHORT SFbignan
[2] = {0x7fff, 0xffff};
5534 unsigned EMUSHORT SFlittlenan
[2] = {0, 0xffc0};
5540 make_nan (nan
, sign
, mode
)
5541 unsigned EMUSHORT
*nan
;
5543 enum machine_mode mode
;
5546 unsigned EMUSHORT
*p
;
5550 /* Possibly the `reserved operand' patterns on a VAX can be
5551 used like NaN's, but probably not in the same way as IEEE. */
5552 #if !defined(DEC) && !defined(IBM)
5555 if (FLOAT_WORDS_BIG_ENDIAN
)
5562 if (FLOAT_WORDS_BIG_ENDIAN
)
5569 if (FLOAT_WORDS_BIG_ENDIAN
)
5577 if (FLOAT_WORDS_BIG_ENDIAN
)
5586 if (FLOAT_WORDS_BIG_ENDIAN
)
5587 *nan
++ = (sign
<< 15) | *p
++;
5590 if (! FLOAT_WORDS_BIG_ENDIAN
)
5591 *nan
= (sign
<< 15) | *p
;
5594 /* Convert an SFmode target `float' value to a REAL_VALUE_TYPE.
5595 This is the inverse of the function `etarsingle' invoked by
5596 REAL_VALUE_TO_TARGET_SINGLE. */
5599 ereal_from_float (f
)
5603 unsigned EMUSHORT s
[2];
5604 unsigned EMUSHORT e
[NE
];
5606 /* Convert 32 bit integer to array of 16 bit pieces in target machine order.
5607 This is the inverse operation to what the function `endian' does. */
5608 if (FLOAT_WORDS_BIG_ENDIAN
)
5610 s
[0] = (unsigned EMUSHORT
) (f
>> 16);
5611 s
[1] = (unsigned EMUSHORT
) f
;
5615 s
[0] = (unsigned EMUSHORT
) f
;
5616 s
[1] = (unsigned EMUSHORT
) (f
>> 16);
5618 /* Convert and promote the target float to E-type. */
5620 /* Output E-type to REAL_VALUE_TYPE. */
5626 /* Convert a DFmode target `double' value to a REAL_VALUE_TYPE.
5627 This is the inverse of the function `etardouble' invoked by
5628 REAL_VALUE_TO_TARGET_DOUBLE.
5630 The DFmode is stored as an array of HOST_WIDE_INT in the target's
5631 data format, with no holes in the bit packing. The first element
5632 of the input array holds the bits that would come first in the
5633 target computer's memory. */
5636 ereal_from_double (d
)
5640 unsigned EMUSHORT s
[4];
5641 unsigned EMUSHORT e
[NE
];
5643 /* Convert array of HOST_WIDE_INT to equivalent array of 16-bit pieces. */
5644 if (FLOAT_WORDS_BIG_ENDIAN
)
5646 s
[0] = (unsigned EMUSHORT
) (d
[0] >> 16);
5647 s
[1] = (unsigned EMUSHORT
) d
[0];
5648 #if HOST_BITS_PER_WIDE_INT == 32
5649 s
[2] = (unsigned EMUSHORT
) (d
[1] >> 16);
5650 s
[3] = (unsigned EMUSHORT
) d
[1];
5652 /* In this case the entire target double is contained in the
5653 first array element. The second element of the input is
5655 s
[2] = (unsigned EMUSHORT
) (d
[0] >> 48);
5656 s
[3] = (unsigned EMUSHORT
) (d
[0] >> 32);
5661 /* Target float words are little-endian. */
5662 s
[0] = (unsigned EMUSHORT
) d
[0];
5663 s
[1] = (unsigned EMUSHORT
) (d
[0] >> 16);
5664 #if HOST_BITS_PER_WIDE_INT == 32
5665 s
[2] = (unsigned EMUSHORT
) d
[1];
5666 s
[3] = (unsigned EMUSHORT
) (d
[1] >> 16);
5668 s
[2] = (unsigned EMUSHORT
) (d
[0] >> 32);
5669 s
[3] = (unsigned EMUSHORT
) (d
[0] >> 48);
5672 /* Convert target double to E-type. */
5674 /* Output E-type to REAL_VALUE_TYPE. */
5680 /* Convert target computer unsigned 64-bit integer to e-type.
5681 The endian-ness of DImode follows the convention for integers,
5682 so we use WORDS_BIG_ENDIAN here, not FLOAT_WORDS_BIG_ENDIAN. */
5686 unsigned EMUSHORT
*di
; /* Address of the 64-bit int. */
5687 unsigned EMUSHORT
*e
;
5689 unsigned EMUSHORT yi
[NI
];
5693 if (WORDS_BIG_ENDIAN
)
5695 for (k
= M
; k
< M
+ 4; k
++)
5700 for (k
= M
+ 3; k
>= M
; k
--)
5703 yi
[E
] = EXONE
+ 47; /* exponent if normalize shift count were 0 */
5704 if ((k
= enormlz (yi
)) > NBITS
)/* normalize the significand */
5705 ecleaz (yi
); /* it was zero */
5707 yi
[E
] -= (unsigned EMUSHORT
) k
;/* subtract shift count from exponent */
5711 /* Convert target computer signed 64-bit integer to e-type. */
5715 unsigned EMUSHORT
*di
; /* Address of the 64-bit int. */
5716 unsigned EMUSHORT
*e
;
5718 unsigned EMULONG acc
;
5719 unsigned EMUSHORT yi
[NI
];
5720 unsigned EMUSHORT carry
;
5724 if (WORDS_BIG_ENDIAN
)
5726 for (k
= M
; k
< M
+ 4; k
++)
5731 for (k
= M
+ 3; k
>= M
; k
--)
5734 /* Take absolute value */
5740 for (k
= M
+ 3; k
>= M
; k
--)
5742 acc
= (unsigned EMULONG
) (~yi
[k
] & 0xffff) + carry
;
5749 yi
[E
] = EXONE
+ 47; /* exponent if normalize shift count were 0 */
5750 if ((k
= enormlz (yi
)) > NBITS
)/* normalize the significand */
5751 ecleaz (yi
); /* it was zero */
5753 yi
[E
] -= (unsigned EMUSHORT
) k
;/* subtract shift count from exponent */
5760 /* Convert e-type to unsigned 64-bit int. */
5764 unsigned EMUSHORT
*x
;
5765 unsigned EMUSHORT
*i
;
5767 unsigned EMUSHORT xi
[NI
];
5776 k
= (int) xi
[E
] - (EXONE
- 1);
5779 for (j
= 0; j
< 4; j
++)
5785 for (j
= 0; j
< 4; j
++)
5788 warning ("overflow on truncation to integer");
5793 /* Shift more than 16 bits: first shift up k-16 mod 16,
5794 then shift up by 16's. */
5795 j
= k
- ((k
>> 4) << 4);
5799 if (WORDS_BIG_ENDIAN
)
5810 if (WORDS_BIG_ENDIAN
)
5815 while ((k
-= 16) > 0);
5819 /* shift not more than 16 bits */
5824 if (WORDS_BIG_ENDIAN
)
5843 /* Convert e-type to signed 64-bit int. */
5847 unsigned EMUSHORT
*x
;
5848 unsigned EMUSHORT
*i
;
5850 unsigned EMULONG acc
;
5851 unsigned EMUSHORT xi
[NI
];
5852 unsigned EMUSHORT carry
;
5853 unsigned EMUSHORT
*isave
;
5857 k
= (int) xi
[E
] - (EXONE
- 1);
5860 for (j
= 0; j
< 4; j
++)
5866 for (j
= 0; j
< 4; j
++)
5869 warning ("overflow on truncation to integer");
5875 /* Shift more than 16 bits: first shift up k-16 mod 16,
5876 then shift up by 16's. */
5877 j
= k
- ((k
>> 4) << 4);
5881 if (WORDS_BIG_ENDIAN
)
5892 if (WORDS_BIG_ENDIAN
)
5897 while ((k
-= 16) > 0);
5901 /* shift not more than 16 bits */
5904 if (WORDS_BIG_ENDIAN
)
5920 /* Negate if negative */
5924 if (WORDS_BIG_ENDIAN
)
5926 for (k
= 0; k
< 4; k
++)
5928 acc
= (unsigned EMULONG
) (~(*isave
) & 0xffff) + carry
;
5929 if (WORDS_BIG_ENDIAN
)
5941 /* Longhand square root routine. */
5944 static int esqinited
= 0;
5945 static unsigned short sqrndbit
[NI
];
5949 unsigned EMUSHORT
*x
, *y
;
5951 unsigned EMUSHORT temp
[NI
], num
[NI
], sq
[NI
], xx
[NI
];
5953 int i
, j
, k
, n
, nlups
;
5958 sqrndbit
[NI
- 2] = 1;
5961 /* Check for arg <= 0 */
5962 i
= ecmp (x
, ezero
);
5967 mtherr ("esqrt", DOMAIN
);
5983 /* Bring in the arg and renormalize if it is denormal. */
5985 m
= (EMULONG
) xx
[1]; /* local long word exponent */
5989 /* Divide exponent by 2 */
5991 exp
= (unsigned short) ((m
/ 2) + 0x3ffe);
5993 /* Adjust if exponent odd */
6003 n
= 8; /* get 8 bits of result per inner loop */
6009 /* bring in next word of arg */
6011 num
[NI
- 1] = xx
[j
+ 3];
6012 /* Do additional bit on last outer loop, for roundoff. */
6015 for (i
= 0; i
< n
; i
++)
6017 /* Next 2 bits of arg */
6020 /* Shift up answer */
6022 /* Make trial divisor */
6023 for (k
= 0; k
< NI
; k
++)
6026 eaddm (sqrndbit
, temp
);
6027 /* Subtract and insert answer bit if it goes in */
6028 if (ecmpm (temp
, num
) <= 0)
6038 /* Adjust for extra, roundoff loop done. */
6039 exp
+= (NBITS
- 1) - rndprc
;
6041 /* Sticky bit = 1 if the remainder is nonzero. */
6043 for (i
= 3; i
< NI
; i
++)
6046 /* Renormalize and round off. */
6047 emdnorm (sq
, k
, 0, exp
, 64);
6050 #endif /* EMU_NON_COMPILE not defined */
6052 /* Return the binary precision of the significand for a given
6053 floating point mode. The mode can hold an integer value
6054 that many bits wide, without losing any bits. */
6057 significand_size (mode
)
6058 enum machine_mode mode
;
6067 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
6070 #if TARGET_FLOAT_FORMAT == IBM_FLOAT_FORMAT
6073 #if TARGET_FLOAT_FORMAT == VAX_FLOAT_FORMAT