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 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, MIEEE, IBMPC, or UNK should get defined.
64 /* `MIEEE' refers generically to big-endian IEEE floating-point data
65 structure. This definition should work in SFmode `float' type and
66 DFmode `double' type on virtually all big-endian IEEE machines.
67 If LONG_DOUBLE_TYPE_SIZE has been defined to be 96, then MIEEE
68 also invokes the particular XFmode (`long double' type) data
69 structure used by the Motorola 680x0 series processors.
71 `IBMPC' refers generally to little-endian IEEE machines. In this
72 case, if LONG_DOUBLE_TYPE_SIZE has been defined to be 96, then
73 IBMPC also invokes the particular XFmode `long double' data
74 structure used by the Intel 80x86 series processors.
76 `DEC' refers specifically to the Digital Equipment Corp PDP-11
77 and VAX floating point data structure. This model currently
78 supports no type wider than DFmode.
80 `IBM' refers specifically to the IBM System/370 and compatible
81 floating point data structure. This model currently supports
82 no type wider than DFmode. The IBM conversions were contributed by
83 frank@atom.ansto.gov.au (Frank Crawford).
85 If LONG_DOUBLE_TYPE_SIZE = 64 (the default, unless tm.h defines it)
86 then `long double' and `double' are both implemented, but they
87 both mean DFmode. In this case, the software floating-point
88 support available here is activated by writing
89 #define REAL_ARITHMETIC
92 The case LONG_DOUBLE_TYPE_SIZE = 128 activates TFmode support
93 and may deactivate XFmode since `long double' is used to refer
96 The macros FLOAT_WORDS_BIG_ENDIAN, HOST_FLOAT_WORDS_BIG_ENDIAN,
97 contributed by Richard Earnshaw <Richard.Earnshaw@cl.cam.ac.uk>,
98 separate the floating point unit's endian-ness from that of
99 the integer addressing. This permits one to define a big-endian
100 FPU on a little-endian machine (e.g., ARM). An extension to
101 BYTES_BIG_ENDIAN may be required for some machines in the future.
102 These optional macros may be defined in tm.h. In real.h, they
103 default to WORDS_BIG_ENDIAN, etc., so there is no need to define
104 them for any normal host or target machine on which the floats
105 and the integers have the same endian-ness. */
108 /* The following converts gcc macros into the ones used by this file. */
110 /* REAL_ARITHMETIC defined means that macros in real.h are
111 defined to call emulator functions. */
112 #ifdef REAL_ARITHMETIC
114 #if TARGET_FLOAT_FORMAT == VAX_FLOAT_FORMAT
115 /* PDP-11, Pro350, VAX: */
117 #else /* it's not VAX */
118 #if TARGET_FLOAT_FORMAT == IBM_FLOAT_FORMAT
119 /* IBM System/370 style */
121 #else /* it's also not an IBM */
122 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
123 #if FLOAT_WORDS_BIG_ENDIAN
124 /* Motorola IEEE, high order words come first (Sun workstation): */
126 #else /* not big-endian */
127 /* Intel IEEE, low order words come first:
130 #endif /* big-endian */
131 #else /* it's not IEEE either */
132 /* UNKnown arithmetic. We don't support this and can't go on. */
133 unknown arithmetic type
135 #endif /* not IEEE */
140 /* REAL_ARITHMETIC not defined means that the *host's* data
141 structure will be used. It may differ by endian-ness from the
142 target machine's structure and will get its ends swapped
143 accordingly (but not here). Probably only the decimal <-> binary
144 functions in this file will actually be used in this case. */
145 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
147 #else /* it's not VAX */
148 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
149 /* IBM System/370 style */
151 #else /* it's also not an IBM */
152 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
153 #if HOST_FLOAT_WORDS_BIG_ENDIAN
155 #else /* not big-endian */
157 #endif /* big-endian */
158 #else /* it's not IEEE either */
159 unknown arithmetic type
161 #endif /* not IEEE */
165 #endif /* REAL_ARITHMETIC not defined */
167 /* Define INFINITY for support of infinity.
168 Define NANS for support of Not-a-Number's (NaN's). */
169 #if !defined(DEC) && !defined(IBM)
174 /* Support of NaNs requires support of infinity. */
181 /* Find a host integer type that is at least 16 bits wide,
182 and another type at least twice whatever that size is. */
184 #if HOST_BITS_PER_CHAR >= 16
185 #define EMUSHORT char
186 #define EMUSHORT_SIZE HOST_BITS_PER_CHAR
187 #define EMULONG_SIZE (2 * HOST_BITS_PER_CHAR)
189 #if HOST_BITS_PER_SHORT >= 16
190 #define EMUSHORT short
191 #define EMUSHORT_SIZE HOST_BITS_PER_SHORT
192 #define EMULONG_SIZE (2 * HOST_BITS_PER_SHORT)
194 #if HOST_BITS_PER_INT >= 16
196 #define EMUSHORT_SIZE HOST_BITS_PER_INT
197 #define EMULONG_SIZE (2 * HOST_BITS_PER_INT)
199 #if HOST_BITS_PER_LONG >= 16
200 #define EMUSHORT long
201 #define EMUSHORT_SIZE HOST_BITS_PER_LONG
202 #define EMULONG_SIZE (2 * HOST_BITS_PER_LONG)
204 /* You will have to modify this program to have a smaller unit size. */
205 #define EMU_NON_COMPILE
211 #if HOST_BITS_PER_SHORT >= EMULONG_SIZE
212 #define EMULONG short
214 #if HOST_BITS_PER_INT >= EMULONG_SIZE
217 #if HOST_BITS_PER_LONG >= EMULONG_SIZE
220 #if HOST_BITS_PER_LONG_LONG >= EMULONG_SIZE
221 #define EMULONG long long int
223 /* You will have to modify this program to have a smaller unit size. */
224 #define EMU_NON_COMPILE
231 /* The host interface doesn't work if no 16-bit size exists. */
232 #if EMUSHORT_SIZE != 16
233 #define EMU_NON_COMPILE
236 /* OK to continue compilation. */
237 #ifndef EMU_NON_COMPILE
239 /* Construct macros to translate between REAL_VALUE_TYPE and e type.
240 In GET_REAL and PUT_REAL, r and e are pointers.
241 A REAL_VALUE_TYPE is guaranteed to occupy contiguous locations
242 in memory, with no holes. */
244 #if LONG_DOUBLE_TYPE_SIZE == 96
245 /* Number of 16 bit words in external e type format */
247 #define MAXDECEXP 4932
248 #define MINDECEXP -4956
249 #define GET_REAL(r,e) bcopy (r, e, 2*NE)
250 #define PUT_REAL(e,r) bcopy (e, r, 2*NE)
251 #else /* no XFmode */
252 #if LONG_DOUBLE_TYPE_SIZE == 128
254 #define MAXDECEXP 4932
255 #define MINDECEXP -4977
256 #define GET_REAL(r,e) bcopy (r, e, 2*NE)
257 #define PUT_REAL(e,r) bcopy (e, r, 2*NE)
260 #define MAXDECEXP 4932
261 #define MINDECEXP -4956
262 #ifdef REAL_ARITHMETIC
263 /* Emulator uses target format internally
264 but host stores it in host endian-ness. */
266 #if HOST_FLOAT_WORDS_BIG_ENDIAN == FLOAT_WORDS_BIG_ENDIAN
267 #define GET_REAL(r,e) e53toe ((unsigned EMUSHORT*) (r), (e))
268 #define PUT_REAL(e,r) etoe53 ((e), (unsigned EMUSHORT *) (r))
270 #else /* endian-ness differs */
271 /* emulator uses target endian-ness internally */
272 #define GET_REAL(r,e) \
273 do { unsigned EMUSHORT w[4]; \
274 w[3] = ((EMUSHORT *) r)[0]; \
275 w[2] = ((EMUSHORT *) r)[1]; \
276 w[1] = ((EMUSHORT *) r)[2]; \
277 w[0] = ((EMUSHORT *) r)[3]; \
278 e53toe (w, (e)); } while (0)
280 #define PUT_REAL(e,r) \
281 do { unsigned EMUSHORT w[4]; \
283 *((EMUSHORT *) r) = w[3]; \
284 *((EMUSHORT *) r + 1) = w[2]; \
285 *((EMUSHORT *) r + 2) = w[1]; \
286 *((EMUSHORT *) r + 3) = w[0]; } while (0)
288 #endif /* endian-ness differs */
290 #else /* not REAL_ARITHMETIC */
292 /* emulator uses host format */
293 #define GET_REAL(r,e) e53toe ((unsigned EMUSHORT *) (r), (e))
294 #define PUT_REAL(e,r) etoe53 ((e), (unsigned EMUSHORT *) (r))
296 #endif /* not REAL_ARITHMETIC */
297 #endif /* not TFmode */
298 #endif /* no XFmode */
301 /* Number of 16 bit words in internal format */
304 /* Array offset to exponent */
307 /* Array offset to high guard word */
310 /* Number of bits of precision */
311 #define NBITS ((NI-4)*16)
313 /* Maximum number of decimal digits in ASCII conversion
316 #define NDEC (NBITS*8/27)
318 /* The exponent of 1.0 */
319 #define EXONE (0x3fff)
321 extern int extra_warnings;
322 extern unsigned EMUSHORT ezero[], ehalf[], eone[], etwo[];
323 extern unsigned EMUSHORT elog2[], esqrt2[];
325 static void endian PROTO((unsigned EMUSHORT *, long *,
327 static void eclear PROTO((unsigned EMUSHORT *));
328 static void emov PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
329 static void eabs PROTO((unsigned EMUSHORT *));
330 static void eneg PROTO((unsigned EMUSHORT *));
331 static int eisneg PROTO((unsigned EMUSHORT *));
332 static int eisinf PROTO((unsigned EMUSHORT *));
333 static int eisnan PROTO((unsigned EMUSHORT *));
334 static void einfin PROTO((unsigned EMUSHORT *));
335 static void enan PROTO((unsigned EMUSHORT *, int));
336 static void emovi PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
337 static void emovo PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
338 static void ecleaz PROTO((unsigned EMUSHORT *));
339 static void ecleazs PROTO((unsigned EMUSHORT *));
340 static void emovz PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
341 static void einan PROTO((unsigned EMUSHORT *));
342 static int eiisnan PROTO((unsigned EMUSHORT *));
343 static int eiisneg PROTO((unsigned EMUSHORT *));
344 static void eiinfin PROTO((unsigned EMUSHORT *));
345 static int eiisinf PROTO((unsigned EMUSHORT *));
346 static int ecmpm PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
347 static void eshdn1 PROTO((unsigned EMUSHORT *));
348 static void eshup1 PROTO((unsigned EMUSHORT *));
349 static void eshdn8 PROTO((unsigned EMUSHORT *));
350 static void eshup8 PROTO((unsigned EMUSHORT *));
351 static void eshup6 PROTO((unsigned EMUSHORT *));
352 static void eshdn6 PROTO((unsigned EMUSHORT *));
353 static void eaddm PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
\f
354 static void esubm PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
355 static void m16m PROTO((unsigned short, unsigned short *,
357 static int edivm PROTO((unsigned short *, unsigned short *));
358 static int emulm PROTO((unsigned short *, unsigned short *));
359 static void emdnorm PROTO((unsigned EMUSHORT *, int, int, EMULONG, int));
360 static void esub PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
361 unsigned EMUSHORT *));
362 static void eadd PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
363 unsigned EMUSHORT *));
364 static void eadd1 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
365 unsigned EMUSHORT *));
366 static void ediv PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
367 unsigned EMUSHORT *));
368 static void emul PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
369 unsigned EMUSHORT *));
370 static void e53toe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
371 static void e64toe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
372 static void e113toe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
373 static void e24toe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
374 static void etoe113 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
375 static void toe113 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
376 static void etoe64 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
377 static void toe64 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
378 static void etoe53 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
379 static void toe53 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
380 static void etoe24 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
381 static void toe24 PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
382 static int ecmp PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
383 static void eround PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
384 static void ltoe PROTO((HOST_WIDE_INT *, unsigned EMUSHORT *));
385 static void ultoe PROTO((unsigned HOST_WIDE_INT *, unsigned EMUSHORT *));
386 static void eifrac PROTO((unsigned EMUSHORT *, HOST_WIDE_INT *,
387 unsigned EMUSHORT *));
388 static void euifrac PROTO((unsigned EMUSHORT *, unsigned HOST_WIDE_INT *,
389 unsigned EMUSHORT *));
390 static int eshift PROTO((unsigned EMUSHORT *, int));
391 static int enormlz PROTO((unsigned EMUSHORT *));
392 static void e24toasc PROTO((unsigned EMUSHORT *, char *, int));
393 static void e53toasc PROTO((unsigned EMUSHORT *, char *, int));
394 static void e64toasc PROTO((unsigned EMUSHORT *, char *, int));
395 static void e113toasc PROTO((unsigned EMUSHORT *, char *, int));
396 static void etoasc PROTO((unsigned EMUSHORT *, char *, int));
397 static void asctoe24 PROTO((char *, unsigned EMUSHORT *));
398 static void asctoe53 PROTO((char *, unsigned EMUSHORT *));
399 static void asctoe64 PROTO((char *, unsigned EMUSHORT *));
400 static void asctoe113 PROTO((char *, unsigned EMUSHORT *));
401 static void asctoe PROTO((char *, unsigned EMUSHORT *));
402 static void asctoeg PROTO((char *, unsigned EMUSHORT *, int));
403 static void efloor PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
404 static void efrexp PROTO((unsigned EMUSHORT *, int *,
405 unsigned EMUSHORT *));
406 static void eldexp PROTO((unsigned EMUSHORT *, int, unsigned EMUSHORT *));
407 static void eremain PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
408 unsigned EMUSHORT *));
409 static void eiremain PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
410 static void mtherr PROTO((char *, int));
411 static void dectoe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
412 static void etodec PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
413 static void todec PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
414 static void ibmtoe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
416 static void etoibm PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
418 static void toibm PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *,
420 static void make_nan PROTO((unsigned EMUSHORT *, int, enum machine_mode));
421 static void uditoe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
422 static void ditoe PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
423 static void etoudi PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
424 static void etodi PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
425 static void esqrt PROTO((unsigned EMUSHORT *, unsigned EMUSHORT *));
427 /* Copy 32-bit numbers obtained from array containing 16-bit numbers,
428 swapping ends if required, into output array of longs. The
429 result is normally passed to fprintf by the ASM_OUTPUT_ macros. */
433 unsigned EMUSHORT e[];
435 enum machine_mode mode;
439 #if FLOAT_WORDS_BIG_ENDIAN
444 /* Swap halfwords in the fourth long. */
445 th = (unsigned long) e[6] & 0xffff;
446 t = (unsigned long) e[7] & 0xffff;
452 /* Swap halfwords in the third long. */
453 th = (unsigned long) e[4] & 0xffff;
454 t = (unsigned long) e[5] & 0xffff;
457 /* fall into the double case */
461 /* swap halfwords in the second word */
462 th = (unsigned long) e[2] & 0xffff;
463 t = (unsigned long) e[3] & 0xffff;
466 /* fall into the float case */
470 /* swap halfwords in the first word */
471 th = (unsigned long) e[0] & 0xffff;
472 t = (unsigned long) e[1] & 0xffff;
483 /* Pack the output array without swapping. */
490 /* Pack the fourth long. */
491 th = (unsigned long) e[7] & 0xffff;
492 t = (unsigned long) e[6] & 0xffff;
498 /* Pack the third long.
499 Each element of the input REAL_VALUE_TYPE array has 16 useful bits
501 th = (unsigned long) e[5] & 0xffff;
502 t = (unsigned long) e[4] & 0xffff;
505 /* fall into the double case */
509 /* pack the second long */
510 th = (unsigned long) e[3] & 0xffff;
511 t = (unsigned long) e[2] & 0xffff;
514 /* fall into the float case */
518 /* pack the first long */
519 th = (unsigned long) e[1] & 0xffff;
520 t = (unsigned long) e[0] & 0xffff;
533 /* This is the implementation of the REAL_ARITHMETIC macro.
537 earith (value, icode, r1, r2)
538 REAL_VALUE_TYPE *value;
543 unsigned EMUSHORT d1[NE], d2[NE], v[NE];
549 /* Return NaN input back to the caller. */
552 PUT_REAL (d1, value);
557 PUT_REAL (d2, value);
561 code = (enum tree_code) icode;
569 esub (d2, d1, v); /* d1 - d2 */
577 #ifndef REAL_INFINITY
578 if (ecmp (d2, ezero) == 0)
581 enan (v, eisneg (d1) ^ eisneg (d2));
588 ediv (d2, d1, v); /* d1/d2 */
591 case MIN_EXPR: /* min (d1,d2) */
592 if (ecmp (d1, d2) < 0)
598 case MAX_EXPR: /* max (d1,d2) */
599 if (ecmp (d1, d2) > 0)
612 /* Truncate REAL_VALUE_TYPE toward zero to signed HOST_WIDE_INT
613 * implements REAL_VALUE_RNDZINT (x) (etrunci (x))
619 unsigned EMUSHORT f[NE], g[NE];
635 /* Truncate REAL_VALUE_TYPE toward zero to unsigned HOST_WIDE_INT
636 * implements REAL_VALUE_UNSIGNED_RNDZINT (x) (etruncui (x))
642 unsigned EMUSHORT f[NE], g[NE];
644 unsigned HOST_WIDE_INT l;
658 /* This is the REAL_VALUE_ATOF function.
659 * It converts a decimal string to binary, rounding off
660 * as indicated by the machine_mode argument. Then it
661 * promotes the rounded value to REAL_VALUE_TYPE.
668 unsigned EMUSHORT tem[NE], e[NE];
697 /* Expansion of REAL_NEGATE.
703 unsigned EMUSHORT e[NE];
713 /* Round real toward zero to HOST_WIDE_INT
714 * implements REAL_VALUE_FIX (x).
720 unsigned EMUSHORT f[NE], g[NE];
727 warning ("conversion from NaN to int");
735 /* Round real toward zero to unsigned HOST_WIDE_INT
736 * implements REAL_VALUE_UNSIGNED_FIX (x).
737 * Negative input returns zero.
739 unsigned HOST_WIDE_INT
743 unsigned EMUSHORT f[NE], g[NE];
744 unsigned HOST_WIDE_INT l;
750 warning ("conversion from NaN to unsigned int");
759 /* REAL_VALUE_FROM_INT macro.
762 ereal_from_int (d, i, j)
766 unsigned EMUSHORT df[NE], dg[NE];
767 HOST_WIDE_INT low, high;
775 /* complement and add 1 */
782 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
793 /* REAL_VALUE_FROM_UNSIGNED_INT macro.
797 ereal_from_uint (d, i, j)
799 unsigned HOST_WIDE_INT i, j;
801 unsigned EMUSHORT df[NE], dg[NE];
802 unsigned HOST_WIDE_INT low, high;
806 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
815 /* REAL_VALUE_TO_INT macro
818 ereal_to_int (low, high, rr)
819 HOST_WIDE_INT *low, *high;
822 unsigned EMUSHORT d[NE], df[NE], dg[NE], dh[NE];
829 warning ("conversion from NaN to int");
835 /* convert positive value */
842 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
843 ediv (df, d, dg); /* dg = d / 2^32 is the high word */
844 euifrac (dg, high, dh);
845 emul (df, dh, dg); /* fractional part is the low word */
846 euifrac (dg, low, dh);
849 /* complement and add 1 */
859 /* REAL_VALUE_LDEXP macro.
866 unsigned EMUSHORT e[NE], y[NE];
879 /* These routines are conditionally compiled because functions
880 * of the same names may be defined in fold-const.c. */
881 #ifdef REAL_ARITHMETIC
883 /* Check for infinity in a REAL_VALUE_TYPE. */
888 unsigned EMUSHORT e[NE];
899 /* Check whether a REAL_VALUE_TYPE item is a NaN. */
905 unsigned EMUSHORT e[NE];
916 /* Check for a negative REAL_VALUE_TYPE number.
917 * This just checks the sign bit, so that -0 counts as negative.
924 return ereal_isneg (x);
927 /* Expansion of REAL_VALUE_TRUNCATE.
928 * The result is in floating point, rounded to nearest or even.
931 real_value_truncate (mode, arg)
932 enum machine_mode mode;
935 unsigned EMUSHORT e[NE], t[NE];
970 /* If an unsupported type was requested, presume that
971 the machine files know something useful to do with
972 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 */
1005 unsigned EMUSHORT e[NE];
1009 endian (e, l, TFmode);
1012 /* 80-bit long double */
1018 unsigned EMUSHORT e[NE];
1022 endian (e, l, XFmode);
1030 unsigned EMUSHORT e[NE];
1034 endian (e, l, DFmode);
1041 unsigned EMUSHORT e[NE];
1046 endian (e, &l, SFmode);
1051 ereal_to_decimal (x, s)
1055 unsigned EMUSHORT e[NE];
1063 REAL_VALUE_TYPE x, y;
1065 unsigned EMUSHORT ex[NE], ey[NE];
1069 return (ecmp (ex, ey));
1076 unsigned EMUSHORT ex[NE];
1079 return (eisneg (ex));
1082 /* End of REAL_ARITHMETIC interface */
1086 * Extended precision IEEE binary floating point arithmetic routines
1088 * Numbers are stored in C language as arrays of 16-bit unsigned
1089 * 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
1162 * ecleaz (ei) ei = 0
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
1187 * The result is always normalized and rounded to NI-4 word precision
1188 * after each arithmetic operation.
1190 * Exception flags are NOT fully supported.
1192 * Signaling NaN's are NOT supported; they are treated the same
1195 * Define INFINITY for support of infinity; otherwise a
1196 * saturation arithmetic is implemented.
1198 * Define NANS for support of Not-a-Number items; otherwise the
1199 * arithmetic will never produce a NaN output, and might be confused
1201 * If NaN's are supported, the output of `ecmp (a,b)' is -2 if
1202 * either a or b is a NaN. This means asking `if (ecmp (a,b) < 0)'
1203 * may not be legitimate. Use `if (ecmp (a,b) == -1)' for `less than'
1206 * Denormals are always supported here where appropriate (e.g., not
1207 * for conversion to DEC numbers).
1214 * Common include file for math routines
1220 * #include "mconf.h"
1226 * This file contains definitions for error codes that are
1227 * passed to the common error handling routine mtherr
1230 * The file also includes a conditional assembly definition
1231 * for the type of computer arithmetic (Intel IEEE, DEC, Motorola
1232 * IEEE, or UNKnown).
1234 * For Digital Equipment PDP-11 and VAX computers, certain
1235 * IBM systems, and others that use numbers with a 56-bit
1236 * significand, the symbol DEC should be defined. In this
1237 * mode, most floating point constants are given as arrays
1238 * of octal integers to eliminate decimal to binary conversion
1239 * errors that might be introduced by the compiler.
1241 * For computers, such as IBM PC, that follow the IEEE
1242 * Standard for Binary Floating Point Arithmetic (ANSI/IEEE
1243 * Std 754-1985), the symbol IBMPC or MIEEE should be defined.
1244 * These numbers have 53-bit significands. In this mode, constants
1245 * are provided as arrays of hexadecimal 16 bit integers.
1247 * To accommodate other types of computer arithmetic, all
1248 * constants are also provided in a normal decimal radix
1249 * which one can hope are correctly converted to a suitable
1250 * format by the available C language compiler. To invoke
1251 * this mode, the symbol UNK is defined.
1253 * An important difference among these modes is a predefined
1254 * set of machine arithmetic constants for each. The numbers
1255 * MACHEP (the machine roundoff error), MAXNUM (largest number
1256 * represented), and several other parameters are preset by
1257 * the configuration symbol. Check the file const.c to
1258 * ensure that these values are correct for your computer.
1260 * For ANSI C compatibility, define ANSIC equal to 1. Currently
1261 * this affects only the atan2 function and others that use it.
1264 /* Constant definitions for math error conditions. */
1266 #define DOMAIN 1 /* argument domain error */
1267 #define SING 2 /* argument singularity */
1268 #define OVERFLOW 3 /* overflow range error */
1269 #define UNDERFLOW 4 /* underflow range error */
1270 #define TLOSS 5 /* total loss of precision */
1271 #define PLOSS 6 /* partial loss of precision */
1272 #define INVALID 7 /* NaN-producing operation */
1274 /* e type constants used by high precision check routines */
1276 #if LONG_DOUBLE_TYPE_SIZE == 128
1278 unsigned EMUSHORT ezero[NE] =
1279 {0x0000, 0x0000, 0x0000, 0x0000,
1280 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,};
1281 extern unsigned EMUSHORT ezero[];
1284 unsigned EMUSHORT ehalf[NE] =
1285 {0x0000, 0x0000, 0x0000, 0x0000,
1286 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3ffe,};
1287 extern unsigned EMUSHORT ehalf[];
1290 unsigned EMUSHORT eone[NE] =
1291 {0x0000, 0x0000, 0x0000, 0x0000,
1292 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3fff,};
1293 extern unsigned EMUSHORT eone[];
1296 unsigned EMUSHORT etwo[NE] =
1297 {0x0000, 0x0000, 0x0000, 0x0000,
1298 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4000,};
1299 extern unsigned EMUSHORT etwo[];
1302 unsigned EMUSHORT e32[NE] =
1303 {0x0000, 0x0000, 0x0000, 0x0000,
1304 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4004,};
1305 extern unsigned EMUSHORT e32[];
1307 /* 6.93147180559945309417232121458176568075500134360255E-1 */
1308 unsigned EMUSHORT elog2[NE] =
1309 {0x40f3, 0xf6af, 0x03f2, 0xb398,
1310 0xc9e3, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1311 extern unsigned EMUSHORT elog2[];
1313 /* 1.41421356237309504880168872420969807856967187537695E0 */
1314 unsigned EMUSHORT esqrt2[NE] =
1315 {0x1d6f, 0xbe9f, 0x754a, 0x89b3,
1316 0x597d, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1317 extern unsigned EMUSHORT esqrt2[];
1319 /* 3.14159265358979323846264338327950288419716939937511E0 */
1320 unsigned EMUSHORT epi[NE] =
1321 {0x2902, 0x1cd1, 0x80dc, 0x628b,
1322 0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1323 extern unsigned EMUSHORT epi[];
1326 /* LONG_DOUBLE_TYPE_SIZE is other than 128 */
1327 unsigned EMUSHORT ezero[NE] =
1328 {0, 0000000, 0000000, 0000000, 0000000, 0000000,};
1329 unsigned EMUSHORT ehalf[NE] =
1330 {0, 0000000, 0000000, 0000000, 0100000, 0x3ffe,};
1331 unsigned EMUSHORT eone[NE] =
1332 {0, 0000000, 0000000, 0000000, 0100000, 0x3fff,};
1333 unsigned EMUSHORT etwo[NE] =
1334 {0, 0000000, 0000000, 0000000, 0100000, 0040000,};
1335 unsigned EMUSHORT e32[NE] =
1336 {0, 0000000, 0000000, 0000000, 0100000, 0040004,};
1337 unsigned EMUSHORT elog2[NE] =
1338 {0xc9e4, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1339 unsigned EMUSHORT esqrt2[NE] =
1340 {0x597e, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1341 unsigned EMUSHORT epi[NE] =
1342 {0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1347 /* Control register for rounding precision.
1348 * This can be set to 113 (if NE=10), 80 (if NE=6), 64, 56, 53, or 24 bits.
1353 static void toe24 (), toe53 (), toe64 (), toe113 ();
1357 ; Clear out entire external format number.
1359 ; unsigned EMUSHORT x[];
1365 register unsigned EMUSHORT *x;
1369 for (i = 0; i < NE; i++)
1375 /* Move external format number from a to b.
1382 register unsigned EMUSHORT *a, *b;
1386 for (i = 0; i < NE; i++)
1392 ; Absolute value of external format number
1400 unsigned EMUSHORT x[];
1402 /* sign is top bit of last word of external format */
1403 x[NE - 1] &= 0x7fff;
1410 ; Negate external format number
1412 ; unsigned EMUSHORT x[NE];
1418 unsigned EMUSHORT x[];
1421 x[NE - 1] ^= 0x8000; /* Toggle the sign bit */
1426 /* Return 1 if sign bit of external format number is nonzero,
1431 unsigned EMUSHORT x[];
1434 if (x[NE - 1] & 0x8000)
1441 /* Return 1 if external format number is infinity.
1447 unsigned EMUSHORT x[];
1454 if ((x[NE - 1] & 0x7fff) == 0x7fff)
1461 /* Check if e-type number is not a number.
1462 The bit pattern is one that we defined, so we know for sure how to
1467 unsigned EMUSHORT x[];
1472 /* NaN has maximum exponent */
1473 if ((x[NE - 1] & 0x7fff) != 0x7fff)
1475 /* ... and non-zero significand field. */
1476 for (i = 0; i < NE - 1; i++)
1485 /* Fill external format number with infinity pattern (IEEE)
1486 or largest possible number (non-IEEE). */
1490 register unsigned EMUSHORT *x;
1495 for (i = 0; i < NE - 1; i++)
1499 for (i = 0; i < NE - 1; i++)
1528 /* Output an e-type NaN.
1529 This generates Intel's quiet NaN pattern for extended real.
1530 The exponent is 7fff, the leading mantissa word is c000. */
1534 register unsigned EMUSHORT *x;
1539 for (i = 0; i < NE - 2; i++)
1542 *x = (sign << 15) | 0x7fff;
1546 /* Move in external format number,
1547 * converting it to internal format.
1551 unsigned EMUSHORT *a, *b;
1553 register unsigned EMUSHORT *p, *q;
1557 p = a + (NE - 1); /* point to last word of external number */
1558 /* get the sign bit */
1563 /* get the exponent */
1565 *q++ &= 0x7fff; /* delete the sign bit */
1567 if ((*(q - 1) & 0x7fff) == 0x7fff)
1573 for (i = 3; i < NI; i++)
1578 for (i = 2; i < NI; i++)
1583 /* clear high guard word */
1585 /* move in the significand */
1586 for (i = 0; i < NE - 1; i++)
1588 /* clear low guard word */
1593 /* Move internal format number out,
1594 * converting it to external format.
1598 unsigned EMUSHORT *a, *b;
1600 register unsigned EMUSHORT *p, *q;
1601 unsigned EMUSHORT i;
1605 q = b + (NE - 1); /* point to output exponent */
1606 /* combine sign and exponent */
1609 *q-- = *p++ | 0x8000;
1613 if (*(p - 1) == 0x7fff)
1618 enan (b, eiisneg (a));
1626 /* skip over guard word */
1628 /* move the significand */
1629 for (j = 0; j < NE - 1; j++)
1636 /* Clear out internal format number.
1641 register unsigned EMUSHORT *xi;
1645 for (i = 0; i < NI; i++)
1650 /* same, but don't touch the sign. */
1654 register unsigned EMUSHORT *xi;
1659 for (i = 0; i < NI - 1; i++)
1665 /* Move internal format number from a to b.
1670 register unsigned EMUSHORT *a, *b;
1674 for (i = 0; i < NI - 1; i++)
1676 /* clear low guard word */
1680 /* Generate internal format NaN.
1681 The explicit pattern for this is maximum exponent and
1682 top two significand bits set. */
1686 unsigned EMUSHORT x[];
1694 /* Return nonzero if internal format number is a NaN. */
1698 unsigned EMUSHORT x[];
1702 if ((x[E] & 0x7fff) == 0x7fff)
1704 for (i = M + 1; i < NI; i++)
1713 /* Return nonzero if sign of internal format number is nonzero. */
1717 unsigned EMUSHORT x[];
1723 /* Fill internal format number with infinity pattern.
1724 This has maximum exponent and significand all zeros. */
1728 unsigned EMUSHORT x[];
1735 /* Return nonzero if internal format number is infinite. */
1739 unsigned EMUSHORT x[];
1746 if ((x[E] & 0x7fff) == 0x7fff)
1753 ; Compare significands of numbers in internal format.
1754 ; Guard words are included in the comparison.
1756 ; unsigned EMUSHORT a[NI], b[NI];
1759 ; for the significands:
1760 ; returns +1 if a > b
1767 register unsigned EMUSHORT *a, *b;
1771 a += M; /* skip up to significand area */
1773 for (i = M; i < NI; i++)
1781 if (*(--a) > *(--b))
1789 ; Shift significand down by 1 bit
1794 register unsigned EMUSHORT *x;
1796 register unsigned EMUSHORT bits;
1799 x += M; /* point to significand area */
1802 for (i = M; i < NI; i++)
1817 ; Shift significand up by 1 bit
1822 register unsigned EMUSHORT *x;
1824 register unsigned EMUSHORT bits;
1830 for (i = M; i < NI; i++)
1845 ; Shift significand down by 8 bits
1850 register unsigned EMUSHORT *x;
1852 register unsigned EMUSHORT newbyt, oldbyt;
1857 for (i = M; i < NI; i++)
1868 ; Shift significand up by 8 bits
1873 register unsigned EMUSHORT *x;
1876 register unsigned EMUSHORT newbyt, oldbyt;
1881 for (i = M; i < NI; i++)
1892 ; Shift significand up by 16 bits
1897 register unsigned EMUSHORT *x;
1900 register unsigned EMUSHORT *p;
1905 for (i = M; i < NI - 1; i++)
1912 ; Shift significand down by 16 bits
1917 register unsigned EMUSHORT *x;
1920 register unsigned EMUSHORT *p;
1925 for (i = M; i < NI - 1; i++)
1938 unsigned EMUSHORT *x, *y;
1940 register unsigned EMULONG a;
1947 for (i = M; i < NI; i++)
1949 a = (unsigned EMULONG) (*x) + (unsigned EMULONG) (*y) + carry;
1954 *y = (unsigned EMUSHORT) a;
1961 ; Subtract significands
1967 unsigned EMUSHORT *x, *y;
1976 for (i = M; i < NI; i++)
1978 a = (unsigned EMULONG) (*y) - (unsigned EMULONG) (*x) - carry;
1983 *y = (unsigned EMUSHORT) a;
1990 static unsigned EMUSHORT equot[NI];
1994 /* Radix 2 shift-and-add versions of multiply and divide */
1997 /* Divide significands */
2001 unsigned EMUSHORT den[], num[];
2004 register unsigned EMUSHORT *p, *q;
2005 unsigned EMUSHORT j;
2011 for (i = M; i < NI; i++)
2016 /* Use faster compare and subtraction if denominator
2017 * has only 15 bits of significance.
2022 for (i = M + 3; i < NI; i++)
2027 if ((den[M + 1] & 1) != 0)
2035 for (i = 0; i < NBITS + 2; i++)
2053 /* The number of quotient bits to calculate is
2054 * NBITS + 1 scaling guard bit + 1 roundoff bit.
2059 for (i = 0; i < NBITS + 2; i++)
2061 if (ecmpm (den, num) <= 0)
2064 j = 1; /* quotient bit = 1 */
2078 /* test for nonzero remainder after roundoff bit */
2081 for (i = M; i < NI; i++)
2089 for (i = 0; i < NI; i++)
2095 /* Multiply significands */
2098 unsigned EMUSHORT a[], b[];
2100 unsigned EMUSHORT *p, *q;
2105 for (i = M; i < NI; i++)
2110 while (*p == 0) /* significand is not supposed to be all zero */
2115 if ((*p & 0xff) == 0)
2123 for (i = 0; i < k; i++)
2127 /* remember if there were any nonzero bits shifted out */
2134 for (i = 0; i < NI; i++)
2137 /* return flag for lost nonzero bits */
2143 /* Radix 65536 versions of multiply and divide */
2146 /* Multiply significand of e-type number b
2147 by 16-bit quantity a, e-type result to c. */
2152 unsigned short b[], c[];
2154 register unsigned short *pp;
2155 register unsigned long carry;
2157 unsigned short p[NI];
2158 unsigned long aa, m;
2167 for (i=M+1; i<NI; i++)
2177 m = (unsigned long) aa * *ps--;
2178 carry = (m & 0xffff) + *pp;
2179 *pp-- = (unsigned short)carry;
2180 carry = (carry >> 16) + (m >> 16) + *pp;
2181 *pp = (unsigned short)carry;
2182 *(pp-1) = carry >> 16;
2185 for (i=M; i<NI; i++)
2190 /* Divide significands. Neither the numerator nor the denominator
2191 is permitted to have its high guard word nonzero. */
2195 unsigned short den[], num[];
2198 register unsigned short *p;
2200 unsigned short j, tdenm, tquot;
2201 unsigned short tprod[NI+1];
2207 for (i=M; i<NI; i++)
2213 for (i=M; i<NI; i++)
2215 /* Find trial quotient digit (the radix is 65536). */
2216 tnum = (((unsigned long) num[M]) << 16) + num[M+1];
2218 /* Do not execute the divide instruction if it will overflow. */
2219 if ((tdenm * 0xffffL) < tnum)
2222 tquot = tnum / tdenm;
2223 /* Multiply denominator by trial quotient digit. */
2224 m16m (tquot, den, tprod);
2225 /* The quotient digit may have been overestimated. */
2226 if (ecmpm (tprod, num) > 0)
2230 if (ecmpm (tprod, num) > 0)
2240 /* test for nonzero remainder after roundoff bit */
2243 for (i=M; i<NI; i++)
2250 for (i=0; i<NI; i++)
2258 /* Multiply significands */
2261 unsigned short a[], b[];
2263 unsigned short *p, *q;
2264 unsigned short pprod[NI];
2270 for (i=M; i<NI; i++)
2276 for (i=M+1; i<NI; i++)
2284 m16m (*p--, b, pprod);
2285 eaddm(pprod, equot);
2291 for (i=0; i<NI; i++)
2294 /* return flag for lost nonzero bits */
2301 * Normalize and round off.
2303 * The internal format number to be rounded is "s".
2304 * Input "lost" indicates whether or not the number is exact.
2305 * This is the so-called sticky bit.
2307 * Input "subflg" indicates whether the number was obtained
2308 * by a subtraction operation. In that case if lost is nonzero
2309 * then the number is slightly smaller than indicated.
2311 * Input "exp" is the biased exponent, which may be negative.
2312 * the exponent field of "s" is ignored but is replaced by
2313 * "exp" as adjusted by normalization and rounding.
2315 * Input "rcntrl" is the rounding control.
2318 /* For future reference: In order for emdnorm to round off denormal
2319 significands at the right point, the input exponent must be
2320 adjusted to be the actual value it would have after conversion to
2321 the final floating point type. This adjustment has been
2322 implemented for all type conversions (etoe53, etc.) and decimal
2323 conversions, but not for the arithmetic functions (eadd, etc.).
2324 Data types having standard 15-bit exponents are not affected by
2325 this, but SFmode and DFmode are affected. For example, ediv with
2326 rndprc = 24 will not round correctly to 24-bit precision if the
2327 result is denormal. */
2329 static int rlast = -1;
2331 static unsigned EMUSHORT rmsk = 0;
2332 static unsigned EMUSHORT rmbit = 0;
2333 static unsigned EMUSHORT rebit = 0;
2335 static unsigned EMUSHORT rbit[NI];
2338 emdnorm (s, lost, subflg, exp, rcntrl)
2339 unsigned EMUSHORT s[];
2346 unsigned EMUSHORT r;
2351 /* a blank significand could mean either zero or infinity. */
2364 if ((j > NBITS) && (exp < 32767))
2372 if (exp > (EMULONG) (-NBITS - 1))
2385 /* Round off, unless told not to by rcntrl. */
2388 /* Set up rounding parameters if the control register changed. */
2389 if (rndprc != rlast)
2396 rw = NI - 1; /* low guard word */
2416 /* For DEC or IBM arithmetic */
2443 /* Shift down 1 temporarily if the data structure has an implied
2444 most significant bit and the number is denormal. */
2445 if ((exp <= 0) && (rndprc != 64) && (rndprc != NBITS))
2447 lost |= s[NI - 1] & 1;
2450 /* Clear out all bits below the rounding bit,
2451 remembering in r if any were nonzero. */
2465 if ((r & rmbit) != 0)
2470 { /* round to even */
2471 if ((s[re] & rebit) == 0)
2483 if ((exp <= 0) && (rndprc != 64) && (rndprc != NBITS))
2488 { /* overflow on roundoff */
2501 for (i = 2; i < NI - 1; i++)
2504 warning ("floating point overflow");
2508 for (i = M + 1; i < NI - 1; i++)
2511 if ((rndprc < 64) || (rndprc == 113))
2526 s[1] = (unsigned EMUSHORT) exp;
2532 ; Subtract external format numbers.
2534 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2535 ; esub (a, b, c); c = b - a
2538 static int subflg = 0;
2542 unsigned EMUSHORT *a, *b, *c;
2556 /* Infinity minus infinity is a NaN.
2557 Test for subtracting infinities of the same sign. */
2558 if (eisinf (a) && eisinf (b)
2559 && ((eisneg (a) ^ eisneg (b)) == 0))
2561 mtherr ("esub", INVALID);
2574 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2575 ; eadd (a, b, c); c = b + a
2580 unsigned EMUSHORT *a, *b, *c;
2584 /* NaN plus anything is a NaN. */
2595 /* Infinity minus infinity is a NaN.
2596 Test for adding infinities of opposite signs. */
2597 if (eisinf (a) && eisinf (b)
2598 && ((eisneg (a) ^ eisneg (b)) != 0))
2600 mtherr ("esub", INVALID);
2611 unsigned EMUSHORT *a, *b, *c;
2613 unsigned EMUSHORT ai[NI], bi[NI], ci[NI];
2615 EMULONG lt, lta, ltb;
2636 /* compare exponents */
2641 { /* put the larger number in bi */
2651 if (lt < (EMULONG) (-NBITS - 1))
2652 goto done; /* answer same as larger addend */
2654 lost = eshift (ai, k); /* shift the smaller number down */
2658 /* exponents were the same, so must compare significands */
2661 { /* the numbers are identical in magnitude */
2662 /* if different signs, result is zero */
2668 /* if same sign, result is double */
2669 /* double denomalized tiny number */
2670 if ((bi[E] == 0) && ((bi[3] & 0x8000) == 0))
2675 /* add 1 to exponent unless both are zero! */
2676 for (j = 1; j < NI - 1; j++)
2680 /* This could overflow, but let emovo take care of that. */
2685 bi[E] = (unsigned EMUSHORT) ltb;
2689 { /* put the larger number in bi */
2705 emdnorm (bi, lost, subflg, ltb, 64);
2716 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2717 ; ediv (a, b, c); c = b / a
2722 unsigned EMUSHORT *a, *b, *c;
2724 unsigned EMUSHORT ai[NI], bi[NI];
2726 EMULONG lt, lta, ltb;
2729 /* Return any NaN input. */
2740 /* Zero over zero, or infinity over infinity, is a NaN. */
2741 if (((ecmp (a, ezero) == 0) && (ecmp (b, ezero) == 0))
2742 || (eisinf (a) && eisinf (b)))
2744 mtherr ("ediv", INVALID);
2745 enan (c, eisneg (a) ^ eisneg (b));
2749 /* Infinity over anything else is infinity. */
2753 if (eisneg (a) ^ eisneg (b))
2754 *(c + (NE - 1)) = 0x8000;
2756 *(c + (NE - 1)) = 0;
2760 /* Anything else over infinity is zero. */
2772 { /* See if numerator is zero. */
2773 for (i = 1; i < NI - 1; i++)
2777 ltb -= enormlz (bi);
2787 { /* possible divide by zero */
2788 for (i = 1; i < NI - 1; i++)
2792 lta -= enormlz (ai);
2797 *(c + (NE - 1)) = 0;
2799 *(c + (NE - 1)) = 0x8000;
2800 /* Divide by zero is not an invalid operation.
2801 It is a divide-by-zero operation! */
2803 mtherr ("ediv", SING);
2809 /* calculate exponent */
2810 lt = ltb - lta + EXONE;
2811 emdnorm (bi, i, 0, lt, 64);
2825 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2826 ; emul (a, b, c); c = b * a
2831 unsigned EMUSHORT *a, *b, *c;
2833 unsigned EMUSHORT ai[NI], bi[NI];
2835 EMULONG lt, lta, ltb;
2838 /* NaN times anything is the same NaN. */
2849 /* Zero times infinity is a NaN. */
2850 if ((eisinf (a) && (ecmp (b, ezero) == 0))
2851 || (eisinf (b) && (ecmp (a, ezero) == 0)))
2853 mtherr ("emul", INVALID);
2854 enan (c, eisneg (a) ^ eisneg (b));
2858 /* Infinity times anything else is infinity. */
2860 if (eisinf (a) || eisinf (b))
2862 if (eisneg (a) ^ eisneg (b))
2863 *(c + (NE - 1)) = 0x8000;
2865 *(c + (NE - 1)) = 0;
2876 for (i = 1; i < NI - 1; i++)
2880 lta -= enormlz (ai);
2891 for (i = 1; i < NI - 1; i++)
2895 ltb -= enormlz (bi);
2904 /* Multiply significands */
2906 /* calculate exponent */
2907 lt = lta + ltb - (EXONE - 1);
2908 emdnorm (bi, j, 0, lt, 64);
2909 /* calculate sign of product */
2921 ; Convert IEEE double precision to e type
2923 ; unsigned EMUSHORT x[N+2];
2929 unsigned EMUSHORT *pe, *y;
2933 dectoe (pe, y); /* see etodec.c */
2938 ibmtoe (pe, y, DFmode);
2941 register unsigned EMUSHORT r;
2942 register unsigned EMUSHORT *e, *p;
2943 unsigned EMUSHORT yy[NI];
2947 denorm = 0; /* flag if denormalized number */
2956 yy[M] = (r & 0x0f) | 0x10;
2957 r &= ~0x800f; /* strip sign and 4 significand bits */
2963 if (((pe[3] & 0xf) != 0) || (pe[2] != 0)
2964 || (pe[1] != 0) || (pe[0] != 0))
2966 enan (y, yy[0] != 0);
2970 if (((pe[0] & 0xf) != 0) || (pe[1] != 0)
2971 || (pe[2] != 0) || (pe[3] != 0))
2973 enan (y, yy[0] != 0);
2984 #endif /* INFINITY */
2986 /* If zero exponent, then the significand is denormalized.
2987 * So, take back the understood high significand bit. */
3009 { /* if zero exponent, then normalize the significand */
3010 if ((k = enormlz (yy)) > NBITS)
3013 yy[E] -= (unsigned EMUSHORT) (k - 1);
3016 #endif /* not IBM */
3017 #endif /* not DEC */
3022 unsigned EMUSHORT *pe, *y;
3024 unsigned EMUSHORT yy[NI];
3025 unsigned EMUSHORT *e, *p, *q;
3030 for (i = 0; i < NE - 5; i++)
3033 for (i = 0; i < 5; i++)
3036 /* This precision is not ordinarily supported on DEC or IBM. */
3038 for (i = 0; i < 5; i++)
3042 p = &yy[0] + (NE - 1);
3045 for (i = 0; i < 5; i++)
3049 p = &yy[0] + (NE - 1);
3052 for (i = 0; i < 4; i++)
3062 for (i = 0; i < 4; i++)
3066 enan (y, (*p & 0x8000) != 0);
3071 for (i = 1; i <= 4; i++)
3075 enan (y, (*p & 0x8000) != 0);
3087 #endif /* INFINITY */
3088 for (i = 0; i < NE; i++)
3095 unsigned EMUSHORT *pe, *y;
3097 register unsigned EMUSHORT r;
3098 unsigned EMUSHORT *e, *p;
3099 unsigned EMUSHORT yy[NI];
3118 for (i = 0; i < 7; i++)
3122 enan (y, yy[0] != 0);
3127 for (i = 1; i < 8; i++)
3131 enan (y, yy[0] != 0);
3143 #endif /* INFINITY */
3147 for (i = 0; i < 7; i++)
3152 for (i = 0; i < 7; i++)
3155 /* If denormal, remove the implied bit; else shift down 1. */
3170 ; Convert IEEE single precision to e type
3172 ; unsigned EMUSHORT x[N+2];
3178 unsigned EMUSHORT *pe, *y;
3182 ibmtoe (pe, y, SFmode);
3185 register unsigned EMUSHORT r;
3186 register unsigned EMUSHORT *e, *p;
3187 unsigned EMUSHORT yy[NI];
3191 denorm = 0; /* flag if denormalized number */
3203 yy[M] = (r & 0x7f) | 0200;
3204 r &= ~0x807f; /* strip sign and 7 significand bits */
3210 if (((pe[0] & 0x7f) != 0) || (pe[1] != 0))
3212 enan (y, yy[0] != 0);
3216 if (((pe[1] & 0x7f) != 0) || (pe[0] != 0))
3218 enan (y, yy[0] != 0);
3229 #endif /* INFINITY */
3231 /* If zero exponent, then the significand is denormalized.
3232 * So, take back the understood high significand bit. */
3253 { /* if zero exponent, then normalize the significand */
3254 if ((k = enormlz (yy)) > NBITS)
3257 yy[E] -= (unsigned EMUSHORT) (k - 1);
3260 #endif /* not IBM */
3266 unsigned EMUSHORT *x, *e;
3268 unsigned EMUSHORT xi[NI];
3275 make_nan (e, eisneg (x), TFmode);
3280 exp = (EMULONG) xi[E];
3285 /* round off to nearest or even */
3288 emdnorm (xi, 0, 0, exp, 64);
3294 /* move out internal format to ieee long double */
3298 unsigned EMUSHORT *a, *b;
3300 register unsigned EMUSHORT *p, *q;
3301 unsigned EMUSHORT i;
3306 make_nan (b, eiisneg (a), TFmode);
3314 q = b + 7; /* point to output exponent */
3317 /* If not denormal, delete the implied bit. */
3322 /* combine sign and exponent */
3326 *q++ = *p++ | 0x8000;
3331 *q-- = *p++ | 0x8000;
3335 /* skip over guard word */
3337 /* move the significand */
3339 for (i = 0; i < 7; i++)
3342 for (i = 0; i < 7; i++)
3349 unsigned EMUSHORT *x, *e;
3351 unsigned EMUSHORT xi[NI];
3358 make_nan (e, eisneg (x), XFmode);
3363 /* adjust exponent for offset */
3364 exp = (EMULONG) xi[E];
3369 /* round off to nearest or even */
3372 emdnorm (xi, 0, 0, exp, 64);
3379 /* move out internal format to ieee long double */
3382 unsigned EMUSHORT *a, *b;
3384 register unsigned EMUSHORT *p, *q;
3385 unsigned EMUSHORT i;
3390 make_nan (b, eiisneg (a), XFmode);
3395 #if defined(MIEEE) || defined(IBM)
3398 q = b + 4; /* point to output exponent */
3399 #if LONG_DOUBLE_TYPE_SIZE == 96
3400 /* Clear the last two bytes of 12-byte Intel format */
3405 /* combine sign and exponent */
3407 #if defined(MIEEE) || defined(IBM)
3409 *q++ = *p++ | 0x8000;
3415 *q-- = *p++ | 0x8000;
3419 /* skip over guard word */
3421 /* move the significand */
3422 #if defined(MIEEE) || defined(IBM)
3423 for (i = 0; i < 4; i++)
3426 for (i = 0; i < 4; i++)
3433 ; e type to IEEE double precision
3435 ; unsigned EMUSHORT x[NE];
3443 unsigned EMUSHORT *x, *e;
3445 etodec (x, e); /* see etodec.c */
3450 unsigned EMUSHORT *x, *y;
3460 unsigned EMUSHORT *x, *e;
3462 etoibm (x, e, DFmode);
3467 unsigned EMUSHORT *x, *y;
3469 toibm (x, y, DFmode);
3472 #else /* it's neither DEC nor IBM */
3476 unsigned EMUSHORT *x, *e;
3478 unsigned EMUSHORT xi[NI];
3485 make_nan (e, eisneg (x), DFmode);
3490 /* adjust exponent for offsets */
3491 exp = (EMULONG) xi[E] - (EXONE - 0x3ff);
3496 /* round off to nearest or even */
3499 emdnorm (xi, 0, 0, exp, 64);
3508 unsigned EMUSHORT *x, *y;
3510 unsigned EMUSHORT i;
3511 unsigned EMUSHORT *p;
3516 make_nan (y, eiisneg (x), DFmode);
3524 *y = 0; /* output high order */
3526 *y = 0x8000; /* output sign bit */
3529 if (i >= (unsigned int) 2047)
3530 { /* Saturate at largest number less than infinity. */
3545 *y |= (unsigned EMUSHORT) 0x7fef;
3569 i |= *p++ & (unsigned EMUSHORT) 0x0f; /* *p = xi[M] */
3570 *y |= (unsigned EMUSHORT) i; /* high order output already has sign bit set */
3584 #endif /* not IBM */
3585 #endif /* not DEC */
3590 ; e type to IEEE single precision
3592 ; unsigned EMUSHORT x[N+2];
3599 unsigned EMUSHORT *x, *e;
3601 etoibm (x, e, SFmode);
3606 unsigned EMUSHORT *x, *y;
3608 toibm (x, y, SFmode);
3615 unsigned EMUSHORT *x, *e;
3618 unsigned EMUSHORT xi[NI];
3624 make_nan (e, eisneg (x), SFmode);
3629 /* adjust exponent for offsets */
3630 exp = (EMULONG) xi[E] - (EXONE - 0177);
3635 /* round off to nearest or even */
3638 emdnorm (xi, 0, 0, exp, 64);
3646 unsigned EMUSHORT *x, *y;
3648 unsigned EMUSHORT i;
3649 unsigned EMUSHORT *p;
3654 make_nan (y, eiisneg (x), SFmode);
3665 *y = 0; /* output high order */
3667 *y = 0x8000; /* output sign bit */
3670 /* Handle overflow cases. */
3674 *y |= (unsigned EMUSHORT) 0x7f80;
3685 #else /* no INFINITY */
3686 *y |= (unsigned EMUSHORT) 0x7f7f;
3700 #endif /* no INFINITY */
3712 i |= *p++ & (unsigned EMUSHORT) 0x7f; /* *p = xi[M] */
3713 *y |= i; /* high order output already has sign bit set */
3725 #endif /* not IBM */
3727 /* Compare two e type numbers.
3729 * unsigned EMUSHORT a[NE], b[NE];
3732 * returns +1 if a > b
3735 * -2 if either a or b is a NaN.
3740 unsigned EMUSHORT *a, *b;
3742 unsigned EMUSHORT ai[NI], bi[NI];
3743 register unsigned EMUSHORT *p, *q;
3748 if (eisnan (a) || eisnan (b))
3757 { /* the signs are different */
3759 for (i = 1; i < NI - 1; i++)
3773 /* both are the same sign */
3788 return (0); /* equality */
3794 if (*(--p) > *(--q))
3795 return (msign); /* p is bigger */
3797 return (-msign); /* p is littler */
3803 /* Find nearest integer to x = floor (x + 0.5)
3805 * unsigned EMUSHORT x[NE], y[NE]
3811 unsigned EMUSHORT *x, *y;
3821 ; convert HOST_WIDE_INT to e type
3824 ; unsigned EMUSHORT x[NE];
3826 ; note &l is the memory address of l
3832 unsigned EMUSHORT *y;
3834 unsigned EMUSHORT yi[NI];
3835 unsigned HOST_WIDE_INT ll;
3841 /* make it positive */
3842 ll = (unsigned HOST_WIDE_INT) (-(*lp));
3843 yi[0] = 0xffff; /* put correct sign in the e type number */
3847 ll = (unsigned HOST_WIDE_INT) (*lp);
3849 /* move the long integer to yi significand area */
3850 #if HOST_BITS_PER_WIDE_INT == 64
3851 yi[M] = (unsigned EMUSHORT) (ll >> 48);
3852 yi[M + 1] = (unsigned EMUSHORT) (ll >> 32);
3853 yi[M + 2] = (unsigned EMUSHORT) (ll >> 16);
3854 yi[M + 3] = (unsigned EMUSHORT) ll;
3855 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
3857 yi[M] = (unsigned EMUSHORT) (ll >> 16);
3858 yi[M + 1] = (unsigned EMUSHORT) ll;
3859 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
3862 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
3863 ecleaz (yi); /* it was zero */
3865 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
3866 emovo (yi, y); /* output the answer */
3870 ; convert unsigned HOST_WIDE_INT to e type
3872 ; unsigned HOST_WIDE_INT l;
3873 ; unsigned EMUSHORT x[NE];
3875 ; note &l is the memory address of l
3880 unsigned HOST_WIDE_INT *lp;
3881 unsigned EMUSHORT *y;
3883 unsigned EMUSHORT yi[NI];
3884 unsigned HOST_WIDE_INT ll;
3890 /* move the long integer to ayi significand area */
3891 #if HOST_BITS_PER_WIDE_INT == 64
3892 yi[M] = (unsigned EMUSHORT) (ll >> 48);
3893 yi[M + 1] = (unsigned EMUSHORT) (ll >> 32);
3894 yi[M + 2] = (unsigned EMUSHORT) (ll >> 16);
3895 yi[M + 3] = (unsigned EMUSHORT) ll;
3896 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
3898 yi[M] = (unsigned EMUSHORT) (ll >> 16);
3899 yi[M + 1] = (unsigned EMUSHORT) ll;
3900 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
3903 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
3904 ecleaz (yi); /* it was zero */
3906 yi[E] -= (unsigned EMUSHORT) k; /* subtract shift count from exponent */
3907 emovo (yi, y); /* output the answer */
3911 /* Find signed HOST_WIDE_INT integer and floating point fractional
3912 parts of e-type (packed internal format) floating point input X.
3913 The integer output I has the sign of the input, except that
3914 positive overflow is permitted if FIXUNS_TRUNC_LIKE_FIX_TRUNC.
3915 The output e-type fraction FRAC is the positive fractional
3920 unsigned EMUSHORT *x;
3922 unsigned EMUSHORT *frac;
3924 unsigned EMUSHORT xi[NI];
3926 unsigned HOST_WIDE_INT ll;
3929 k = (int) xi[E] - (EXONE - 1);
3932 /* if exponent <= 0, integer = 0 and real output is fraction */
3937 if (k > (HOST_BITS_PER_WIDE_INT - 1))
3939 /* long integer overflow: output large integer
3940 and correct fraction */
3942 *i = ((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1);
3945 #ifdef FIXUNS_TRUNC_LIKE_FIX_TRUNC
3946 /* In this case, let it overflow and convert as if unsigned. */
3947 euifrac (x, &ll, frac);
3948 *i = (HOST_WIDE_INT) ll;
3951 /* In other cases, return the largest positive integer. */
3952 *i = (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1;
3957 warning ("overflow on truncation to integer");
3961 /* Shift more than 16 bits: first shift up k-16 mod 16,
3962 then shift up by 16's. */
3963 j = k - ((k >> 4) << 4);
3970 ll = (ll << 16) | xi[M];
3972 while ((k -= 16) > 0);
3979 /* shift not more than 16 bits */
3981 *i = (HOST_WIDE_INT) xi[M] & 0xffff;
3988 if ((k = enormlz (xi)) > NBITS)
3991 xi[E] -= (unsigned EMUSHORT) k;
3997 /* Find unsigned HOST_WIDE_INT integer and floating point fractional parts.
3998 A negative e type input yields integer output = 0
3999 but correct fraction. */
4002 euifrac (x, i, frac)
4003 unsigned EMUSHORT *x;
4004 unsigned HOST_WIDE_INT *i;
4005 unsigned EMUSHORT *frac;
4007 unsigned HOST_WIDE_INT ll;
4008 unsigned EMUSHORT xi[NI];
4012 k = (int) xi[E] - (EXONE - 1);
4015 /* if exponent <= 0, integer = 0 and argument is fraction */
4020 if (k > HOST_BITS_PER_WIDE_INT)
4022 /* Long integer overflow: output large integer
4023 and correct fraction.
4024 Note, the BSD microvax compiler says that ~(0UL)
4025 is a syntax error. */
4029 warning ("overflow on truncation to unsigned integer");
4033 /* Shift more than 16 bits: first shift up k-16 mod 16,
4034 then shift up by 16's. */
4035 j = k - ((k >> 4) << 4);
4042 ll = (ll << 16) | xi[M];
4044 while ((k -= 16) > 0);
4049 /* shift not more than 16 bits */
4051 *i = (HOST_WIDE_INT) xi[M] & 0xffff;
4054 if (xi[0]) /* A negative value yields unsigned integer 0. */
4060 if ((k = enormlz (xi)) > NBITS)
4063 xi[E] -= (unsigned EMUSHORT) k;
4073 ; Shifts significand area up or down by the number of bits
4074 ; given by the variable sc.
4079 unsigned EMUSHORT *x;
4082 unsigned EMUSHORT lost;
4083 unsigned EMUSHORT *p;
4096 lost |= *p; /* remember lost bits */
4137 return ((int) lost);
4145 ; Shift normalizes the significand area pointed to by argument
4146 ; shift count (up = positive) is returned.
4151 unsigned EMUSHORT x[];
4153 register unsigned EMUSHORT *p;
4162 return (0); /* already normalized */
4167 /* With guard word, there are NBITS+16 bits available.
4168 * return true if all are zero.
4173 /* see if high byte is zero */
4174 while ((*p & 0xff00) == 0)
4179 /* now shift 1 bit at a time */
4180 while ((*p & 0x8000) == 0)
4186 mtherr ("enormlz", UNDERFLOW);
4192 /* Normalize by shifting down out of the high guard word
4193 of the significand */
4208 mtherr ("enormlz", OVERFLOW);
4218 /* Convert e type number to decimal format ASCII string.
4219 * The constants are for 64 bit precision.
4225 #if LONG_DOUBLE_TYPE_SIZE == 128
4226 static unsigned EMUSHORT etens[NTEN + 1][NE] =
4228 {0x6576, 0x4a92, 0x804a, 0x153f,
4229 0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
4230 {0x6a32, 0xce52, 0x329a, 0x28ce,
4231 0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
4232 {0x526c, 0x50ce, 0xf18b, 0x3d28,
4233 0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
4234 {0x9c66, 0x58f8, 0xbc50, 0x5c54,
4235 0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
4236 {0x851e, 0xeab7, 0x98fe, 0x901b,
4237 0xddbb, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
4238 {0x0235, 0x0137, 0x36b1, 0x336c,
4239 0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
4240 {0x50f8, 0x25fb, 0xc76b, 0x6b71,
4241 0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
4242 {0x0000, 0x0000, 0x0000, 0x0000,
4243 0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
4244 {0x0000, 0x0000, 0x0000, 0x0000,
4245 0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
4246 {0x0000, 0x0000, 0x0000, 0x0000,
4247 0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
4248 {0x0000, 0x0000, 0x0000, 0x0000,
4249 0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
4250 {0x0000, 0x0000, 0x0000, 0x0000,
4251 0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
4252 {0x0000, 0x0000, 0x0000, 0x0000,
4253 0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
4256 static unsigned EMUSHORT emtens[NTEN + 1][NE] =
4258 {0x2030, 0xcffc, 0xa1c3, 0x8123,
4259 0x2de3, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
4260 {0x8264, 0xd2cb, 0xf2ea, 0x12d4,
4261 0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
4262 {0xf53f, 0xf698, 0x6bd3, 0x0158,
4263 0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
4264 {0xe731, 0x04d4, 0xe3f2, 0xd332,
4265 0x7132, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
4266 {0xa23e, 0x5308, 0xfefb, 0x1155,
4267 0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
4268 {0xe26d, 0xdbde, 0xd05d, 0xb3f6,
4269 0xac7c, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
4270 {0x2a20, 0x6224, 0x47b3, 0x98d7,
4271 0x3f23, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
4272 {0x0b5b, 0x4af2, 0xa581, 0x18ed,
4273 0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
4274 {0xbf71, 0xa9b3, 0x7989, 0xbe68,
4275 0x4c2e, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
4276 {0x3d4d, 0x7c3d, 0x36ba, 0x0d2b,
4277 0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
4278 {0xc155, 0xa4a8, 0x404e, 0x6113,
4279 0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
4280 {0xd70a, 0x70a3, 0x0a3d, 0xa3d7,
4281 0x3d70, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
4282 {0xcccd, 0xcccc, 0xcccc, 0xcccc,
4283 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
4286 /* LONG_DOUBLE_TYPE_SIZE is other than 128 */
4287 static unsigned EMUSHORT etens[NTEN + 1][NE] =
4289 {0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
4290 {0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
4291 {0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
4292 {0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
4293 {0xddbc, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
4294 {0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
4295 {0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
4296 {0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
4297 {0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
4298 {0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
4299 {0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
4300 {0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
4301 {0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
4304 static unsigned EMUSHORT emtens[NTEN + 1][NE] =
4306 {0x2de4, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
4307 {0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
4308 {0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
4309 {0x7133, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
4310 {0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
4311 {0xac7d, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
4312 {0x3f24, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
4313 {0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
4314 {0x4c2f, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
4315 {0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
4316 {0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
4317 {0x3d71, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
4318 {0xcccd, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
4323 e24toasc (x, string, ndigs)
4324 unsigned EMUSHORT x[];
4328 unsigned EMUSHORT w[NI];
4331 etoasc (w, string, ndigs);
4336 e53toasc (x, string, ndigs)
4337 unsigned EMUSHORT x[];
4341 unsigned EMUSHORT w[NI];
4344 etoasc (w, string, ndigs);
4349 e64toasc (x, string, ndigs)
4350 unsigned EMUSHORT x[];
4354 unsigned EMUSHORT w[NI];
4357 etoasc (w, string, ndigs);
4361 e113toasc (x, string, ndigs)
4362 unsigned EMUSHORT x[];
4366 unsigned EMUSHORT w[NI];
4369 etoasc (w, string, ndigs);
4373 static char wstring[80]; /* working storage for ASCII output */
4376 etoasc (x, string, ndigs)
4377 unsigned EMUSHORT x[];
4382 unsigned EMUSHORT y[NI], t[NI], u[NI], w[NI];
4383 unsigned EMUSHORT *p, *r, *ten;
4384 unsigned EMUSHORT sign;
4385 int i, j, k, expon, rndsav;
4387 unsigned EMUSHORT m;
4398 sprintf (wstring, " NaN ");
4402 rndprc = NBITS; /* set to full precision */
4403 emov (x, y); /* retain external format */
4404 if (y[NE - 1] & 0x8000)
4407 y[NE - 1] &= 0x7fff;
4414 ten = &etens[NTEN][0];
4416 /* Test for zero exponent */
4419 for (k = 0; k < NE - 1; k++)
4422 goto tnzro; /* denormalized number */
4424 goto isone; /* legal all zeros */
4428 /* Test for infinity. */
4429 if (y[NE - 1] == 0x7fff)
4432 sprintf (wstring, " -Infinity ");
4434 sprintf (wstring, " Infinity ");
4438 /* Test for exponent nonzero but significand denormalized.
4439 * This is an error condition.
4441 if ((y[NE - 1] != 0) && ((y[NE - 2] & 0x8000) == 0))
4443 mtherr ("etoasc", DOMAIN);
4444 sprintf (wstring, "NaN");
4448 /* Compare to 1.0 */
4457 { /* Number is greater than 1 */
4458 /* Convert significand to an integer and strip trailing decimal zeros. */
4460 u[NE - 1] = EXONE + NBITS - 1;
4462 p = &etens[NTEN - 4][0];
4468 for (j = 0; j < NE - 1; j++)
4481 /* Rescale from integer significand */
4482 u[NE - 1] += y[NE - 1] - (unsigned int) (EXONE + NBITS - 1);
4484 /* Find power of 10 */
4488 /* An unordered compare result shouldn't happen here. */
4489 while (ecmp (ten, u) <= 0)
4491 if (ecmp (p, u) <= 0)
4504 { /* Number is less than 1.0 */
4505 /* Pad significand with trailing decimal zeros. */
4508 while ((y[NE - 2] & 0x8000) == 0)
4517 for (i = 0; i < NDEC + 1; i++)
4519 if ((w[NI - 1] & 0x7) != 0)
4521 /* multiply by 10 */
4534 if (eone[NE - 1] <= u[1])
4546 while (ecmp (eone, w) > 0)
4548 if (ecmp (p, w) >= 0)
4563 /* Find the first (leading) digit. */
4569 digit = equot[NI - 1];
4570 while ((digit == 0) && (ecmp (y, ezero) != 0))
4578 digit = equot[NI - 1];
4586 /* Examine number of digits requested by caller. */
4604 *s++ = (char)digit + '0';
4607 /* Generate digits after the decimal point. */
4608 for (k = 0; k <= ndigs; k++)
4610 /* multiply current number by 10, without normalizing */
4617 *s++ = (char) equot[NI - 1] + '0';
4619 digit = equot[NI - 1];
4622 /* round off the ASCII string */
4625 /* Test for critical rounding case in ASCII output. */
4629 if (ecmp (t, ezero) != 0)
4630 goto roun; /* round to nearest */
4631 if ((*(s - 1) & 1) == 0)
4632 goto doexp; /* round to even */
4634 /* Round up and propagate carry-outs */
4638 /* Carry out to most significant digit? */
4645 /* Most significant digit carries to 10? */
4653 /* Round up and carry out from less significant digits */
4665 sprintf (ss, "e+%d", expon);
4667 sprintf (ss, "e%d", expon);
4669 sprintf (ss, "e%d", expon);
4672 /* copy out the working string */
4675 while (*ss == ' ') /* strip possible leading space */
4677 while ((*s++ = *ss++) != '\0')
4686 ; ASCTOQ.MAC LATEST REV: 11 JAN 84
4689 ; Convert ASCII string to quadruple precision floating point
4691 ; Numeric input is free field decimal number
4692 ; with max of 15 digits with or without
4693 ; decimal point entered as ASCII from teletype.
4694 ; Entering E after the number followed by a second
4695 ; number causes the second number to be interpreted
4696 ; as a power of 10 to be multiplied by the first number
4697 ; (i.e., "scientific" notation).
4700 ; asctoq (string, q);
4703 /* ASCII to single */
4708 unsigned EMUSHORT *y;
4714 /* ASCII to double */
4719 unsigned EMUSHORT *y;
4721 #if defined(DEC) || defined(IBM)
4729 /* ASCII to long double */
4734 unsigned EMUSHORT *y;
4739 /* ASCII to 128-bit long double */
4744 unsigned EMUSHORT *y;
4746 asctoeg (s, y, 113);
4749 /* ASCII to super double */
4753 unsigned EMUSHORT *y;
4755 asctoeg (s, y, NBITS);
4759 /* ASCII to e type, with specified rounding precision = oprec. */
4761 asctoeg (ss, y, oprec)
4763 unsigned EMUSHORT *y;
4766 unsigned EMUSHORT yy[NI], xt[NI], tt[NI];
4767 int esign, decflg, sgnflg, nexp, exp, prec, lost;
4768 int k, trail, c, rndsav;
4770 unsigned EMUSHORT nsign, *p;
4771 char *sp, *s, *lstr;
4773 /* Copy the input string. */
4774 lstr = (char *) alloca (strlen (ss) + 1);
4776 while (*s == ' ') /* skip leading spaces */
4779 while ((*sp++ = *s++) != '\0')
4784 rndprc = NBITS; /* Set to full precision */
4797 if ((k >= 0) && (k <= 9))
4799 /* Ignore leading zeros */
4800 if ((prec == 0) && (decflg == 0) && (k == 0))
4802 /* Identify and strip trailing zeros after the decimal point. */
4803 if ((trail == 0) && (decflg != 0))
4806 while ((*sp >= '0') && (*sp <= '9'))
4808 /* Check for syntax error */
4810 if ((c != 'e') && (c != 'E') && (c != '\0')
4811 && (c != '\n') && (c != '\r') && (c != ' ')
4821 /* If enough digits were given to more than fill up the yy register,
4822 * continuing until overflow into the high guard word yy[2]
4823 * guarantees that there will be a roundoff bit at the top
4824 * of the low guard word after normalization.
4829 nexp += 1; /* count digits after decimal point */
4830 eshup1 (yy); /* multiply current number by 10 */
4836 xt[NI - 2] = (unsigned EMUSHORT) k;
4841 /* Mark any lost non-zero digit. */
4843 /* Count lost digits before the decimal point. */
4858 case '.': /* decimal point */
4888 mtherr ("asctoe", DOMAIN);
4897 /* Exponent interpretation */
4903 /* check for + or - */
4911 while ((*s >= '0') && (*s <= '9'))
4915 if (exp > -(MINDECEXP))
4925 if (exp > MAXDECEXP)
4929 yy[E] = 0x7fff; /* infinity */
4932 if (exp < MINDECEXP)
4941 /* Pad trailing zeros to minimize power of 10, per IEEE spec. */
4942 while ((nexp > 0) && (yy[2] == 0))
4954 if ((k = enormlz (yy)) > NBITS)
4959 lexp = (EXONE - 1 + NBITS) - k;
4960 emdnorm (yy, lost, 0, lexp, 64);
4961 /* convert to external format */
4964 /* Multiply by 10**nexp. If precision is 64 bits,
4965 * the maximum relative error incurred in forming 10**n
4966 * for 0 <= n <= 324 is 8.2e-20, at 10**180.
4967 * For 0 <= n <= 999, the peak relative error is 1.4e-19 at 10**947.
4968 * For 0 >= n >= -999, it is -1.55e-19 at 10**-435.
4982 { /* Punt. Can't handle this without 2 divides. */
4983 emovi (etens[0], tt);
4990 p = &etens[NTEN][0];
5000 while (exp <= MAXP);
5018 /* Round and convert directly to the destination type */
5020 lexp -= EXONE - 0x3ff;
5022 else if (oprec == 24 || oprec == 56)
5023 lexp -= EXONE - (0x41 << 2);
5025 else if (oprec == 24)
5026 lexp -= EXONE - 0177;
5029 else if (oprec == 56)
5030 lexp -= EXONE - 0201;
5033 emdnorm (yy, k, 0, lexp, 64);
5043 todec (yy, y); /* see etodec.c */
5048 toibm (yy, y, DFmode);
5071 /* y = largest integer not greater than x
5072 * (truncated toward minus infinity)
5074 * unsigned EMUSHORT x[NE], y[NE]
5078 static unsigned EMUSHORT bmask[] =
5101 unsigned EMUSHORT x[], y[];
5103 register unsigned EMUSHORT *p;
5105 unsigned EMUSHORT f[NE];
5107 emov (x, f); /* leave in external format */
5108 expon = (int) f[NE - 1];
5109 e = (expon & 0x7fff) - (EXONE - 1);
5115 /* number of bits to clear out */
5127 /* clear the remaining bits */
5129 /* truncate negatives toward minus infinity */
5132 if ((unsigned EMUSHORT) expon & (unsigned EMUSHORT) 0x8000)
5134 for (i = 0; i < NE - 1; i++)
5146 /* unsigned EMUSHORT x[], s[];
5149 * efrexp (x, exp, s);
5151 * Returns s and exp such that s * 2**exp = x and .5 <= s < 1.
5152 * For example, 1.1 = 0.55 * 2**1
5153 * Handles denormalized numbers properly using long integer exp.
5158 unsigned EMUSHORT x[];
5160 unsigned EMUSHORT s[];
5162 unsigned EMUSHORT xi[NI];
5166 li = (EMULONG) ((EMUSHORT) xi[1]);
5174 *exp = (int) (li - 0x3ffe);
5179 /* unsigned EMUSHORT x[], y[];
5182 * eldexp (x, pwr2, y);
5184 * Returns y = x * 2**pwr2.
5189 unsigned EMUSHORT x[];
5191 unsigned EMUSHORT y[];
5193 unsigned EMUSHORT xi[NI];
5201 emdnorm (xi, i, i, li, 64);
5206 /* c = remainder after dividing b by a
5207 * Least significant integer quotient bits left in equot[].
5212 unsigned EMUSHORT a[], b[], c[];
5214 unsigned EMUSHORT den[NI], num[NI];
5218 || (ecmp (a, ezero) == 0)
5226 if (ecmp (a, ezero) == 0)
5228 mtherr ("eremain", SING);
5234 eiremain (den, num);
5235 /* Sign of remainder = sign of quotient */
5245 unsigned EMUSHORT den[], num[];
5248 unsigned EMUSHORT j;
5251 ld -= enormlz (den);
5253 ln -= enormlz (num);
5257 if (ecmpm (den, num) <= 0)
5271 emdnorm (num, 0, 0, ln, 0);
5276 * Library common error handling routine
5286 * mtherr (fctnam, code);
5292 * This routine may be called to report one of the following
5293 * error conditions (in the include file mconf.h).
5295 * Mnemonic Value Significance
5297 * DOMAIN 1 argument domain error
5298 * SING 2 function singularity
5299 * OVERFLOW 3 overflow range error
5300 * UNDERFLOW 4 underflow range error
5301 * TLOSS 5 total loss of precision
5302 * PLOSS 6 partial loss of precision
5303 * INVALID 7 NaN - producing operation
5304 * EDOM 33 Unix domain error code
5305 * ERANGE 34 Unix range error code
5307 * The default version of the file prints the function name,
5308 * passed to it by the pointer fctnam, followed by the
5309 * error condition. The display is directed to the standard
5310 * output device. The routine then returns to the calling
5311 * program. Users may wish to modify the program to abort by
5312 * calling exit under severe error conditions such as domain
5315 * Since all error conditions pass control to this function,
5316 * the display may be easily changed, eliminated, or directed
5317 * to an error logging device.
5326 Cephes Math Library Release 2.0: April, 1987
5327 Copyright 1984, 1987 by Stephen L. Moshier
5328 Direct inquiries to 30 Frost Street, Cambridge, MA 02140
5331 /* include "mconf.h" */
5333 /* Notice: the order of appearance of the following
5334 * messages is bound to the error codes defined
5338 static char *ermsg[NMSGS] =
5340 "unknown", /* error code 0 */
5341 "domain", /* error code 1 */
5342 "singularity", /* et seq. */
5345 "total loss of precision",
5346 "partial loss of precision",
5360 /* Display string passed by calling program,
5361 * which is supposed to be the name of the
5362 * function in which the error occurred.
5365 /* Display error message defined
5366 * by the code argument.
5368 if ((code <= 0) || (code >= NMSGS))
5370 sprintf (errstr, " %s %s error", name, ermsg[code]);
5373 /* Set global error message word */
5376 /* Return to calling
5382 /* Here is etodec.c .
5387 ; convert DEC double precision to e type
5395 unsigned EMUSHORT *d;
5396 unsigned EMUSHORT *e;
5398 unsigned EMUSHORT y[NI];
5399 register unsigned EMUSHORT r, *p;
5401 ecleaz (y); /* start with a zero */
5402 p = y; /* point to our number */
5403 r = *d; /* get DEC exponent word */
5404 if (*d & (unsigned int) 0x8000)
5405 *p = 0xffff; /* fill in our sign */
5406 ++p; /* bump pointer to our exponent word */
5407 r &= 0x7fff; /* strip the sign bit */
5408 if (r == 0) /* answer = 0 if high order DEC word = 0 */
5412 r >>= 7; /* shift exponent word down 7 bits */
5413 r += EXONE - 0201; /* subtract DEC exponent offset */
5414 /* add our e type exponent offset */
5415 *p++ = r; /* to form our exponent */
5417 r = *d++; /* now do the high order mantissa */
5418 r &= 0177; /* strip off the DEC exponent and sign bits */
5419 r |= 0200; /* the DEC understood high order mantissa bit */
5420 *p++ = r; /* put result in our high guard word */
5422 *p++ = *d++; /* fill in the rest of our mantissa */
5426 eshdn8 (y); /* shift our mantissa down 8 bits */
5434 ; convert e type to DEC double precision
5442 unsigned EMUSHORT *x, *d;
5444 unsigned EMUSHORT xi[NI];
5449 exp = (EMULONG) xi[E] - (EXONE - 0201); /* adjust exponent for offsets */
5450 /* round off to nearest or even */
5453 emdnorm (xi, 0, 0, exp, 64);
5460 unsigned EMUSHORT *x, *y;
5462 unsigned EMUSHORT i;
5463 unsigned EMUSHORT *p;
5507 ; convert IBM single/double precision to e type
5510 ; enum machine_mode mode; SFmode/DFmode
5511 ; ibmtoe (&d, e, mode);
5516 unsigned EMUSHORT *d;
5517 unsigned EMUSHORT *e;
5518 enum machine_mode mode;
5520 unsigned EMUSHORT y[NI];
5521 register unsigned EMUSHORT r, *p;
5524 ecleaz (y); /* start with a zero */
5525 p = y; /* point to our number */
5526 r = *d; /* get IBM exponent word */
5527 if (*d & (unsigned int) 0x8000)
5528 *p = 0xffff; /* fill in our sign */
5529 ++p; /* bump pointer to our exponent word */
5530 r &= 0x7f00; /* strip the sign bit */
5531 r >>= 6; /* shift exponent word down 6 bits */
5532 /* in fact shift by 8 right and 2 left */
5533 r += EXONE - (0x41 << 2); /* subtract IBM exponent offset */
5534 /* add our e type exponent offset */
5535 *p++ = r; /* to form our exponent */
5537 *p++ = *d++ & 0xff; /* now do the high order mantissa */
5538 /* strip off the IBM exponent and sign bits */
5539 if (mode != SFmode) /* there are only 2 words in SFmode */
5541 *p++ = *d++; /* fill in the rest of our mantissa */
5546 if (y[M] == 0 && y[M+1] == 0 && y[M+2] == 0 && y[M+3] == 0)
5549 y[E] -= 5 + enormlz (y); /* now normalise the mantissa */
5550 /* handle change in RADIX */
5557 ; convert e type to IBM single/double precision
5560 ; enum machine_mode mode; SFmode/DFmode
5561 ; etoibm (e, &d, mode);
5566 unsigned EMUSHORT *x, *d;
5567 enum machine_mode mode;
5569 unsigned EMUSHORT xi[NI];
5574 exp = (EMULONG) xi[E] - (EXONE - (0x41 << 2)); /* adjust exponent for offsets */
5575 /* round off to nearest or even */
5578 emdnorm (xi, 0, 0, exp, 64);
5580 toibm (xi, d, mode);
5585 unsigned EMUSHORT *x, *y;
5586 enum machine_mode mode;
5588 unsigned EMUSHORT i;
5589 unsigned EMUSHORT *p;
5637 /* Output a binary NaN bit pattern in the target machine's format. */
5639 /* If special NaN bit patterns are required, define them in tm.h
5640 as arrays of unsigned 16-bit shorts. Otherwise, use the default
5646 unsigned EMUSHORT TFnan[8] =
5647 {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
5650 unsigned EMUSHORT TFnan[8] = {0, 0, 0, 0, 0, 0, 0x8000, 0xffff};
5658 unsigned EMUSHORT XFnan[6] = {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
5661 unsigned EMUSHORT XFnan[6] = {0, 0, 0, 0xc000, 0xffff, 0};
5669 unsigned EMUSHORT DFnan[4] = {0x7fff, 0xffff, 0xffff, 0xffff};
5672 unsigned EMUSHORT DFnan[4] = {0, 0, 0, 0xfff8};
5680 unsigned EMUSHORT SFnan[2] = {0x7fff, 0xffff};
5683 unsigned EMUSHORT SFnan[2] = {0, 0xffc0};
5689 make_nan (nan, sign, mode)
5690 unsigned EMUSHORT *nan;
5692 enum machine_mode mode;
5695 unsigned EMUSHORT *p;
5699 /* Possibly the `reserved operand' patterns on a VAX can be
5700 used like NaN's, but probably not in the same way as IEEE. */
5701 #if !defined(DEC) && !defined(IBM)
5723 *nan++ = (sign << 15) | *p++;
5728 *nan = (sign << 15) | *p;
5732 /* Convert an SFmode target `float' value to a REAL_VALUE_TYPE.
5733 This is the inverse of the function `etarsingle' invoked by
5734 REAL_VALUE_TO_TARGET_SINGLE. */
5737 ereal_from_float (f)
5741 unsigned EMUSHORT s[2];
5742 unsigned EMUSHORT e[NE];
5744 /* Convert 32 bit integer to array of 16 bit pieces in target machine order.
5745 This is the inverse operation to what the function `endian' does. */
5746 #if FLOAT_WORDS_BIG_ENDIAN
5747 s[0] = (unsigned EMUSHORT) (f >> 16);
5748 s[1] = (unsigned EMUSHORT) f;
5750 s[0] = (unsigned EMUSHORT) f;
5751 s[1] = (unsigned EMUSHORT) (f >> 16);
5753 /* Convert and promote the target float to E-type. */
5755 /* Output E-type to REAL_VALUE_TYPE. */
5761 /* Convert a DFmode target `double' value to a REAL_VALUE_TYPE.
5762 This is the inverse of the function `etardouble' invoked by
5763 REAL_VALUE_TO_TARGET_DOUBLE.
5765 The DFmode is stored as an array of long ints
5766 with 32 bits of the value per each long. The first element
5767 of the input array holds the bits that would come first in the
5768 target computer's memory. */
5771 ereal_from_double (d)
5775 unsigned EMUSHORT s[4];
5776 unsigned EMUSHORT e[NE];
5778 /* Convert array of 32 bit pieces to equivalent array of 16 bit pieces.
5779 This is the inverse of `endian'. */
5780 #if FLOAT_WORDS_BIG_ENDIAN
5781 s[0] = (unsigned EMUSHORT) (d[0] >> 16);
5782 s[1] = (unsigned EMUSHORT) d[0];
5783 s[2] = (unsigned EMUSHORT) (d[1] >> 16);
5784 s[3] = (unsigned EMUSHORT) d[1];
5786 s[0] = (unsigned EMUSHORT) d[0];
5787 s[1] = (unsigned EMUSHORT) (d[0] >> 16);
5788 s[2] = (unsigned EMUSHORT) d[1];
5789 s[3] = (unsigned EMUSHORT) (d[1] >> 16);
5791 /* Convert target double to E-type. */
5793 /* Output E-type to REAL_VALUE_TYPE. */
5799 /* Convert target computer unsigned 64-bit integer to e-type.
5800 The endian-ness of DImode follows the convention for integers,
5801 so we use WORDS_BIG_ENDIAN here, not FLOAT_WORDS_BIG_ENDIAN. */
5805 unsigned EMUSHORT *di; /* Address of the 64-bit int. */
5806 unsigned EMUSHORT *e;
5808 unsigned EMUSHORT yi[NI];
5812 #if WORDS_BIG_ENDIAN
5813 for (k = M; k < M + 4; k++)
5816 for (k = M + 3; k >= M; k--)
5819 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
5820 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
5821 ecleaz (yi); /* it was zero */
5823 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
5827 /* Convert target computer signed 64-bit integer to e-type. */
5831 unsigned EMUSHORT *di; /* Address of the 64-bit int. */
5832 unsigned EMUSHORT *e;
5834 unsigned EMULONG acc;
5835 unsigned EMUSHORT yi[NI];
5836 unsigned EMUSHORT carry;
5840 #if WORDS_BIG_ENDIAN
5841 for (k = M; k < M + 4; k++)
5844 for (k = M + 3; k >= M; k--)
5847 /* Take absolute value */
5853 for (k = M + 3; k >= M; k--)
5855 acc = (unsigned EMULONG) (~yi[k] & 0xffff) + carry;
5862 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
5863 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
5864 ecleaz (yi); /* it was zero */
5866 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
5873 /* Convert e-type to unsigned 64-bit int. */
5876 static etoudi (x, i)
5877 unsigned EMUSHORT *x;
5878 unsigned EMUSHORT *i;
5880 unsigned EMUSHORT xi[NI];
5889 k = (int) xi[E] - (EXONE - 1);
5892 for (j = 0; j < 4; j++)
5898 for (j = 0; j < 4; j++)
5901 warning ("overflow on truncation to integer");
5906 /* Shift more than 16 bits: first shift up k-16 mod 16,
5907 then shift up by 16's. */
5908 j = k - ((k >> 4) << 4);
5912 #if WORDS_BIG_ENDIAN
5922 #if WORDS_BIG_ENDIAN
5928 while ((k -= 16) > 0);
5932 /* shift not more than 16 bits */
5937 #if WORDS_BIG_ENDIAN
5953 /* Convert e-type to signed 64-bit int. */
5957 unsigned EMUSHORT *x;
5958 unsigned EMUSHORT *i;
5960 unsigned EMULONG acc;
5961 unsigned EMUSHORT xi[NI];
5962 unsigned EMUSHORT carry;
5963 unsigned EMUSHORT *isave;
5967 k = (int) xi[E] - (EXONE - 1);
5970 for (j = 0; j < 4; j++)
5976 for (j = 0; j < 4; j++)
5979 warning ("overflow on truncation to integer");
5985 /* Shift more than 16 bits: first shift up k-16 mod 16,
5986 then shift up by 16's. */
5987 j = k - ((k >> 4) << 4);
5991 #if WORDS_BIG_ENDIAN
6001 #if WORDS_BIG_ENDIAN
6007 while ((k -= 16) > 0);
6011 /* shift not more than 16 bits */
6014 #if WORDS_BIG_ENDIAN
6027 /* Negate if negative */
6031 #if WORDS_BIG_ENDIAN
6034 for (k = 0; k < 4; k++)
6036 acc = (unsigned EMULONG) (~(*isave) & 0xffff) + carry;
6037 #if WORDS_BIG_ENDIAN
6050 /* Longhand square root routine. */
6053 static int esqinited = 0;
6054 static unsigned short sqrndbit[NI];
6058 unsigned EMUSHORT *x, *y;
6060 unsigned EMUSHORT temp[NI], num[NI], sq[NI], xx[NI];
6062 int i, j, k, n, nlups;
6067 sqrndbit[NI - 2] = 1;
6070 /* Check for arg <= 0 */
6071 i = ecmp (x, ezero);
6076 mtherr ("esqrt", DOMAIN);
6092 /* Bring in the arg and renormalize if it is denormal. */
6094 m = (EMULONG) xx[1]; /* local long word exponent */
6098 /* Divide exponent by 2 */
6100 exp = (unsigned short) ((m / 2) + 0x3ffe);
6102 /* Adjust if exponent odd */
6112 n = 8; /* get 8 bits of result per inner loop */
6118 /* bring in next word of arg */
6120 num[NI - 1] = xx[j + 3];
6121 /* Do additional bit on last outer loop, for roundoff. */
6124 for (i = 0; i < n; i++)
6126 /* Next 2 bits of arg */
6129 /* Shift up answer */
6131 /* Make trial divisor */
6132 for (k = 0; k < NI; k++)
6135 eaddm (sqrndbit, temp);
6136 /* Subtract and insert answer bit if it goes in */
6137 if (ecmpm (temp, num) <= 0)
6147 /* Adjust for extra, roundoff loop done. */
6148 exp += (NBITS - 1) - rndprc;
6150 /* Sticky bit = 1 if the remainder is nonzero. */
6152 for (i = 3; i < NI; i++)
6155 /* Renormalize and round off. */
6156 emdnorm (sq, k, 0, exp, 64);
6160 #endif /* EMU_NON_COMPILE not defined */