1 /* real.c - implementation of REAL_ARITHMETIC, REAL_VALUE_ATOF,
2 and support for XFmode IEEE extended real floating point arithmetic.
3 Contributed by Stephen L. Moshier (moshier@world.std.com).
5 Copyright (C) 1993 Free Software Foundation, Inc.
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
32 /* To enable support of XFmode extended real floating point, define
33 LONG_DOUBLE_TYPE_SIZE 96 in the tm.h file (m68k.h or i386.h).
35 To support cross compilation between IEEE and VAX floating
36 point formats, define REAL_ARITHMETIC in the tm.h file.
38 In either case the machine files (tm.h) must not contain any code
39 that tries to use host floating point arithmetic to convert
40 REAL_VALUE_TYPEs from `double' to `float', pass them to fprintf,
41 etc. In cross-compile situations a REAL_VALUE_TYPE may not
42 be intelligible to the host computer's native arithmetic.
44 The emulator defaults to the host's floating point format so that
45 its decimal conversion functions can be used if desired (see
48 The first part of this file interfaces gcc to ieee.c, which is a
49 floating point arithmetic suite that was not written with gcc in
50 mind. The interface is followed by ieee.c itself and related
51 items. Avoid changing ieee.c unless you have suitable test
52 programs available. A special version of the PARANOIA floating
53 point arithmetic tester, modified for this purpose, can be found
54 on usc.edu : /pub/C-numanal/ieeetest.zoo. Some tutorial
55 information on ieee.c is given in my book: S. L. Moshier,
56 _Methods and Programs for Mathematical Functions_, Prentice-Hall
57 or Simon & Schuster Int'l, 1989. A library of XFmode elementary
58 transcendental functions can be obtained by ftp from
59 research.att.com: netlib/cephes/ldouble.shar.Z */
61 /* Type of computer arithmetic.
62 * Only one of DEC, MIEEE, IBMPC, or UNK should get defined.
65 /* `MIEEE' refers generically to big-endian IEEE floating-point data
66 structure. This definition should work in SFmode `float' type and
67 DFmode `double' type on virtually all big-endian IEEE machines.
68 If LONG_DOUBLE_TYPE_SIZE has been defined to be 96, then MIEEE
69 also invokes the particular XFmode (`long double' type) data
70 structure used by the Motorola 680x0 series processors.
72 `IBMPC' refers generally to little-endian IEEE machines. In this
73 case, if LONG_DOUBLE_TYPE_SIZE has been defined to be 96, then
74 IBMPC also invokes the particular XFmode `long double' data
75 structure used by the Intel 80x86 series processors.
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 If LONG_DOUBLE_TYPE_SIZE = 64 (the default, unless tm.h defines it)
82 then `long double' and `double' are both implemented, but they
83 both mean DFmode. In this case, the software floating-point
84 support available here is activated by writing
85 #define REAL_ARITHMETIC
88 The case LONG_DOUBLE_TYPE_SIZE = 128 activates TFmode support
89 (Not Yet Implemented) and may deactivate XFmode since
90 `long double' is used to refer to both modes. */
92 /* The following converts gcc macros into the ones used by this file. */
94 /* REAL_ARITHMETIC defined means that macros in real.h are
95 defined to call emulator functions. */
96 #ifdef REAL_ARITHMETIC
98 #if TARGET_FLOAT_FORMAT == VAX_FLOAT_FORMAT
99 /* PDP-11, Pro350, VAX: */
101 #else /* it's not VAX */
102 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
104 /* Motorola IEEE, high order words come first (Sun workstation): */
106 #else /* not big-endian */
107 /* Intel IEEE, low order words come first:
110 #endif /* big-endian */
111 #else /* it's not IEEE either */
112 /* UNKnown arithmetic. We don't support this and can't go on. */
113 unknown arithmetic type
115 #endif /* not IEEE */
119 /* REAL_ARITHMETIC not defined means that the *host's* data
120 structure will be used. It may differ by endian-ness from the
121 target machine's structure and will get its ends swapped
122 accordingly (but not here). Probably only the decimal <-> binary
123 functions in this file will actually be used in this case. */
124 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
126 #else /* it's not VAX */
127 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
128 #ifdef HOST_WORDS_BIG_ENDIAN
130 #else /* not big-endian */
132 #endif /* big-endian */
133 #else /* it's not IEEE either */
134 unknown arithmetic type
136 #endif /* not IEEE */
139 #endif /* REAL_ARITHMETIC not defined */
141 /* Define INFINITY for support of infinity.
142 Define NANS for support of Not-a-Number's (NaN's). */
148 /* Support of NaNs requires support of infinity. */
157 * Include file for extended precision arithmetic programs.
160 /* Number of 16 bit words in external e type format */
163 /* Number of 16 bit words in internal format */
166 /* Array offset to exponent */
169 /* Array offset to high guard word */
172 /* Number of bits of precision */
173 #define NBITS ((NI-4)*16)
175 /* Maximum number of decimal digits in ASCII conversion
178 #define NDEC (NBITS*8/27)
180 /* The exponent of 1.0 */
181 #define EXONE (0x3fff)
183 /* Find a host integer type that is at least 16 bits wide,
184 and another type at least twice whatever that size is. */
186 #if HOST_BITS_PER_CHAR >= 16
187 #define EMUSHORT char
188 #define EMUSHORT_SIZE HOST_BITS_PER_CHAR
189 #define EMULONG_SIZE (2 * HOST_BITS_PER_CHAR)
191 #if HOST_BITS_PER_SHORT >= 16
192 #define EMUSHORT short
193 #define EMUSHORT_SIZE HOST_BITS_PER_SHORT
194 #define EMULONG_SIZE (2 * HOST_BITS_PER_SHORT)
196 #if HOST_BITS_PER_INT >= 16
198 #define EMUSHORT_SIZE HOST_BITS_PER_INT
199 #define EMULONG_SIZE (2 * HOST_BITS_PER_INT)
201 #if HOST_BITS_PER_LONG >= 16
202 #define EMUSHORT long
203 #define EMUSHORT_SIZE HOST_BITS_PER_LONG
204 #define EMULONG_SIZE (2 * HOST_BITS_PER_LONG)
206 /* You will have to modify this program to have a smaller unit size. */
207 #define EMU_NON_COMPILE
213 #if HOST_BITS_PER_SHORT >= EMULONG_SIZE
214 #define EMULONG short
216 #if HOST_BITS_PER_INT >= EMULONG_SIZE
219 #if HOST_BITS_PER_LONG >= EMULONG_SIZE
222 #if HOST_BITS_PER_LONG_LONG >= EMULONG_SIZE
223 #define EMULONG long long int
225 /* You will have to modify this program to have a smaller unit size. */
226 #define EMU_NON_COMPILE
233 /* The host interface doesn't work if no 16-bit size exists. */
234 #if EMUSHORT_SIZE != 16
235 #define EMU_NON_COMPILE
238 /* OK to continue compilation. */
239 #ifndef EMU_NON_COMPILE
241 /* Construct macros to translate between REAL_VALUE_TYPE and e type.
242 In GET_REAL and PUT_REAL, r and e are pointers.
243 A REAL_VALUE_TYPE is guaranteed to occupy contiguous locations
244 in memory, with no holes. */
246 #if LONG_DOUBLE_TYPE_SIZE == 96
247 #define GET_REAL(r,e) bcopy (r, e, 2*NE)
248 #define PUT_REAL(e,r) bcopy (e, r, 2*NE)
249 #else /* no XFmode */
251 #ifdef REAL_ARITHMETIC
252 /* Emulator uses target format internally
253 but host stores it in host endian-ness. */
255 #if defined (HOST_WORDS_BIG_ENDIAN) == WORDS_BIG_ENDIAN
256 #define GET_REAL(r,e) e53toe ((r), (e))
257 #define PUT_REAL(e,r) etoe53 ((e), (r))
259 #else /* endian-ness differs */
260 /* emulator uses target endian-ness internally */
261 #define GET_REAL(r,e) \
262 do { EMUSHORT w[4]; \
263 w[3] = ((EMUSHORT *) r)[0]; \
264 w[2] = ((EMUSHORT *) r)[1]; \
265 w[1] = ((EMUSHORT *) r)[2]; \
266 w[0] = ((EMUSHORT *) r)[3]; \
267 e53toe (w, (e)); } while (0)
269 #define PUT_REAL(e,r) \
270 do { EMUSHORT w[4]; \
272 *((EMUSHORT *) r) = w[3]; \
273 *((EMUSHORT *) r + 1) = w[2]; \
274 *((EMUSHORT *) r + 2) = w[1]; \
275 *((EMUSHORT *) r + 3) = w[0]; } while (0)
277 #endif /* endian-ness differs */
279 #else /* not REAL_ARITHMETIC */
281 /* emulator uses host format */
282 #define GET_REAL(r,e) e53toe ((r), (e))
283 #define PUT_REAL(e,r) etoe53 ((e), (r))
285 #endif /* not REAL_ARITHMETIC */
286 #endif /* no XFmode */
289 extern int extra_warnings;
290 int ecmp (), enormlz (), eshift ();
291 int eisneg (), eisinf (), eisnan (), eiisinf (), eiisnan ();
292 void eadd (), esub (), emul (), ediv ();
293 void eshup1 (), eshup8 (), eshup6 (), eshdn1 (), eshdn8 (), eshdn6 ();
294 void eabs (), eneg (), emov (), eclear (), einfin (), efloor ();
295 void eldexp (), efrexp (), eifrac (), euifrac (), ltoe (), ultoe ();
296 void eround (), ereal_to_decimal (), eiinfin (), einan ();
297 void esqrt (), elog (), eexp (), etanh (), epow ();
298 void asctoe (), asctoe24 (), asctoe53 (), asctoe64 ();
299 void etoasc (), e24toasc (), e53toasc (), e64toasc ();
300 void etoe64 (), etoe53 (), etoe24 (), e64toe (), e53toe (), e24toe ();
301 void mtherr (), make_nan ();
302 extern unsigned EMUSHORT ezero[], ehalf[], eone[], etwo[];
303 extern unsigned EMUSHORT elog2[], esqrt2[];
305 /* Pack output array with 32-bit numbers obtained from
306 array containing 16-bit numbers, swapping ends if required. */
309 unsigned EMUSHORT e[];
311 enum machine_mode mode;
321 /* Swap halfwords in the third long. */
322 th = (unsigned long) e[4] & 0xffff;
323 t = (unsigned long) e[5] & 0xffff;
326 /* fall into the double case */
330 /* swap halfwords in the second word */
331 th = (unsigned long) e[2] & 0xffff;
332 t = (unsigned long) e[3] & 0xffff;
335 /* fall into the float case */
339 /* swap halfwords in the first word */
340 th = (unsigned long) e[0] & 0xffff;
341 t = (unsigned long) e[1] & 0xffff;
352 /* Pack the output array without swapping. */
359 /* Pack the third long.
360 Each element of the input REAL_VALUE_TYPE array has 16 bit useful bits
362 th = (unsigned long) e[5] & 0xffff;
363 t = (unsigned long) e[4] & 0xffff;
366 /* fall into the double case */
370 /* pack the second long */
371 th = (unsigned long) e[3] & 0xffff;
372 t = (unsigned long) e[2] & 0xffff;
375 /* fall into the float case */
379 /* pack the first long */
380 th = (unsigned long) e[1] & 0xffff;
381 t = (unsigned long) e[0] & 0xffff;
394 /* This is the implementation of the REAL_ARITHMETIC macro.
397 earith (value, icode, r1, r2)
398 REAL_VALUE_TYPE *value;
403 unsigned EMUSHORT d1[NE], d2[NE], v[NE];
409 /* Return NaN input back to the caller. */
412 PUT_REAL (d1, value);
417 PUT_REAL (d2, value);
421 code = (enum tree_code) icode;
429 esub (d2, d1, v); /* d1 - d2 */
437 #ifndef REAL_INFINITY
438 if (ecmp (d2, ezero) == 0)
448 ediv (d2, d1, v); /* d1/d2 */
451 case MIN_EXPR: /* min (d1,d2) */
452 if (ecmp (d1, d2) < 0)
458 case MAX_EXPR: /* max (d1,d2) */
459 if (ecmp (d1, d2) > 0)
472 /* Truncate REAL_VALUE_TYPE toward zero to signed HOST_WIDE_INT
473 * implements REAL_VALUE_RNDZINT (x) (etrunci (x))
479 unsigned EMUSHORT f[NE], g[NE];
495 /* Truncate REAL_VALUE_TYPE toward zero to unsigned HOST_WIDE_INT
496 * implements REAL_VALUE_UNSIGNED_RNDZINT (x) (etruncui (x))
502 unsigned EMUSHORT f[NE], g[NE];
518 /* This is the REAL_VALUE_ATOF function.
519 * It converts a decimal string to binary, rounding off
520 * as indicated by the machine_mode argument. Then it
521 * promotes the rounded value to REAL_VALUE_TYPE.
528 unsigned EMUSHORT tem[NE], e[NE];
553 /* Expansion of REAL_NEGATE.
559 unsigned EMUSHORT e[NE];
574 * implements REAL_VALUE_FIX (x) (eroundi (x))
575 * The type of rounding is left unspecified by real.h.
576 * It is implemented here as round to nearest (add .5 and chop).
582 unsigned EMUSHORT f[NE], g[NE];
589 warning ("conversion from NaN to int");
598 /* Round real to nearest unsigned int
599 * implements REAL_VALUE_UNSIGNED_FIX (x) ((unsigned int) eroundi (x))
600 * Negative input returns zero.
601 * The type of rounding is left unspecified by real.h.
602 * It is implemented here as round to nearest (add .5 and chop).
608 unsigned EMUSHORT f[NE], g[NE];
615 warning ("conversion from NaN to unsigned int");
621 return ((unsigned int)l);
625 /* REAL_VALUE_FROM_INT macro.
628 ereal_from_int (d, i, j)
632 unsigned EMUSHORT df[NE], dg[NE];
641 /* complement and add 1 */
648 eldexp (eone, HOST_BITS_PER_LONG, df);
659 /* REAL_VALUE_FROM_UNSIGNED_INT macro.
662 ereal_from_uint (d, i, j)
666 unsigned EMUSHORT df[NE], dg[NE];
667 unsigned long low, high;
671 eldexp (eone, HOST_BITS_PER_LONG, df);
680 /* REAL_VALUE_TO_INT macro
683 ereal_to_int (low, high, rr)
687 unsigned EMUSHORT d[NE], df[NE], dg[NE], dh[NE];
694 warning ("conversion from NaN to int");
700 /* convert positive value */
707 eldexp (eone, HOST_BITS_PER_LONG, df);
708 ediv (df, d, dg); /* dg = d / 2^32 is the high word */
709 euifrac (dg, high, dh);
710 emul (df, dh, dg); /* fractional part is the low word */
711 euifrac (dg, low, dh);
714 /* complement and add 1 */
724 /* REAL_VALUE_LDEXP macro.
731 unsigned EMUSHORT e[NE], y[NE];
744 /* These routines are conditionally compiled because functions
745 * of the same names may be defined in fold-const.c. */
746 #ifdef REAL_ARITHMETIC
748 /* Check for infinity in a REAL_VALUE_TYPE. */
753 unsigned EMUSHORT e[NE];
764 /* Check whether a REAL_VALUE_TYPE item is a NaN. */
771 return (eisnan (&x));
778 /* Check for a negative REAL_VALUE_TYPE number.
779 * this means strictly less than zero, not -0.
786 unsigned EMUSHORT e[NE];
789 if (ecmp (e, ezero) == -1)
794 /* Expansion of REAL_VALUE_TRUNCATE.
795 * The result is in floating point, rounded to nearest or even.
798 real_value_truncate (mode, arg)
799 enum machine_mode mode;
802 unsigned EMUSHORT e[NE], t[NE];
839 #endif /* REAL_ARITHMETIC defined */
841 /* Target values are arrays of host longs. A long is guaranteed
842 to be at least 32 bits wide. */
848 unsigned EMUSHORT e[NE];
852 endian (e, l, XFmode);
860 unsigned EMUSHORT e[NE];
864 endian (e, l, DFmode);
871 unsigned EMUSHORT e[NE];
876 endian (e, &l, SFmode);
881 ereal_to_decimal (x, s)
885 unsigned EMUSHORT e[NE];
893 REAL_VALUE_TYPE x, y;
895 unsigned EMUSHORT ex[NE], ey[NE];
899 return (ecmp (ex, ey));
906 unsigned EMUSHORT ex[NE];
909 return (eisneg (ex));
912 /* End of REAL_ARITHMETIC interface */
916 * Extended precision IEEE binary floating point arithmetic routines
918 * Numbers are stored in C language as arrays of 16-bit unsigned
919 * short integers. The arguments of the routines are pointers to
923 * External e type data structure, simulates Intel 8087 chip
924 * temporary real format but possibly with a larger significand:
926 * NE-1 significand words (least significant word first,
927 * most significant bit is normally set)
928 * exponent (value = EXONE for 1.0,
929 * top bit is the sign)
932 * Internal data structure of a number (a "word" is 16 bits):
934 * ei[0] sign word (0 for positive, 0xffff for negative)
935 * ei[1] biased exponent (value = EXONE for the number 1.0)
936 * ei[2] high guard word (always zero after normalization)
938 * to ei[NI-2] significand (NI-4 significand words,
939 * most significant word first,
940 * most significant bit is set)
941 * ei[NI-1] low guard word (0x8000 bit is rounding place)
945 * Routines for external format numbers
947 * asctoe (string, e) ASCII string to extended double e type
948 * asctoe64 (string, &d) ASCII string to long double
949 * asctoe53 (string, &d) ASCII string to double
950 * asctoe24 (string, &f) ASCII string to single
951 * asctoeg (string, e, prec) ASCII string to specified precision
952 * e24toe (&f, e) IEEE single precision to e type
953 * e53toe (&d, e) IEEE double precision to e type
954 * e64toe (&d, e) IEEE long double precision to e type
955 * eabs (e) absolute value
956 * eadd (a, b, c) c = b + a
958 * ecmp (a, b) Returns 1 if a > b, 0 if a == b,
959 * -1 if a < b, -2 if either a or b is a NaN.
960 * ediv (a, b, c) c = b / a
961 * efloor (a, b) truncate to integer, toward -infinity
962 * efrexp (a, exp, s) extract exponent and significand
963 * eifrac (e, &l, frac) e to long integer and e type fraction
964 * euifrac (e, &l, frac) e to unsigned long integer and e type fraction
965 * einfin (e) set e to infinity, leaving its sign alone
966 * eldexp (a, n, b) multiply by 2**n
968 * emul (a, b, c) c = b * a
970 * eround (a, b) b = nearest integer value to a
971 * esub (a, b, c) c = b - a
972 * e24toasc (&f, str, n) single to ASCII string, n digits after decimal
973 * e53toasc (&d, str, n) double to ASCII string, n digits after decimal
974 * e64toasc (&d, str, n) long double to ASCII string
975 * etoasc (e, str, n) e to ASCII string, n digits after decimal
976 * etoe24 (e, &f) convert e type to IEEE single precision
977 * etoe53 (e, &d) convert e type to IEEE double precision
978 * etoe64 (e, &d) convert e type to IEEE long double precision
979 * ltoe (&l, e) long (32 bit) integer to e type
980 * ultoe (&l, e) unsigned long (32 bit) integer to e type
981 * eisneg (e) 1 if sign bit of e != 0, else 0
982 * eisinf (e) 1 if e has maximum exponent (non-IEEE)
983 * or is infinite (IEEE)
984 * eisnan (e) 1 if e is a NaN
987 * Routines for internal format numbers
989 * eaddm (ai, bi) add significands, bi = bi + ai
991 * ecleazs (ei) set ei = 0 but leave its sign alone
992 * ecmpm (ai, bi) compare significands, return 1, 0, or -1
993 * edivm (ai, bi) divide significands, bi = bi / ai
994 * emdnorm (ai,l,s,exp) normalize and round off
995 * emovi (a, ai) convert external a to internal ai
996 * emovo (ai, a) convert internal ai to external a
997 * emovz (ai, bi) bi = ai, low guard word of bi = 0
998 * emulm (ai, bi) multiply significands, bi = bi * ai
999 * enormlz (ei) left-justify the significand
1000 * eshdn1 (ai) shift significand and guards down 1 bit
1001 * eshdn8 (ai) shift down 8 bits
1002 * eshdn6 (ai) shift down 16 bits
1003 * eshift (ai, n) shift ai n bits up (or down if n < 0)
1004 * eshup1 (ai) shift significand and guards up 1 bit
1005 * eshup8 (ai) shift up 8 bits
1006 * eshup6 (ai) shift up 16 bits
1007 * esubm (ai, bi) subtract significands, bi = bi - ai
1008 * eiisinf (ai) 1 if infinite
1009 * eiisnan (ai) 1 if a NaN
1010 * einan (ai) set ai = NaN
1011 * eiinfin (ai) set ai = infinity
1014 * The result is always normalized and rounded to NI-4 word precision
1015 * after each arithmetic operation.
1017 * Exception flags are NOT fully supported.
1019 * Signaling NaN's are NOT supported; they are treated the same
1022 * Define INFINITY for support of infinity; otherwise a
1023 * saturation arithmetic is implemented.
1025 * Define NANS for support of Not-a-Number items; otherwise the
1026 * arithmetic will never produce a NaN output, and might be confused
1028 * If NaN's are supported, the output of `ecmp (a,b)' is -2 if
1029 * either a or b is a NaN. This means asking `if (ecmp (a,b) < 0)'
1030 * may not be legitimate. Use `if (ecmp (a,b) == -1)' for `less than'
1033 * Denormals are always supported here where appropriate (e.g., not
1034 * for conversion to DEC numbers).
1041 * Common include file for math routines
1047 * #include "mconf.h"
1053 * This file contains definitions for error codes that are
1054 * passed to the common error handling routine mtherr
1057 * The file also includes a conditional assembly definition
1058 * for the type of computer arithmetic (Intel IEEE, DEC, Motorola
1059 * IEEE, or UNKnown).
1061 * For Digital Equipment PDP-11 and VAX computers, certain
1062 * IBM systems, and others that use numbers with a 56-bit
1063 * significand, the symbol DEC should be defined. In this
1064 * mode, most floating point constants are given as arrays
1065 * of octal integers to eliminate decimal to binary conversion
1066 * errors that might be introduced by the compiler.
1068 * For computers, such as IBM PC, that follow the IEEE
1069 * Standard for Binary Floating Point Arithmetic (ANSI/IEEE
1070 * Std 754-1985), the symbol IBMPC or MIEEE should be defined.
1071 * These numbers have 53-bit significands. In this mode, constants
1072 * are provided as arrays of hexadecimal 16 bit integers.
1074 * To accommodate other types of computer arithmetic, all
1075 * constants are also provided in a normal decimal radix
1076 * which one can hope are correctly converted to a suitable
1077 * format by the available C language compiler. To invoke
1078 * this mode, the symbol UNK is defined.
1080 * An important difference among these modes is a predefined
1081 * set of machine arithmetic constants for each. The numbers
1082 * MACHEP (the machine roundoff error), MAXNUM (largest number
1083 * represented), and several other parameters are preset by
1084 * the configuration symbol. Check the file const.c to
1085 * ensure that these values are correct for your computer.
1087 * For ANSI C compatibility, define ANSIC equal to 1. Currently
1088 * this affects only the atan2 function and others that use it.
1091 /* Constant definitions for math error conditions. */
1093 #define DOMAIN 1 /* argument domain error */
1094 #define SING 2 /* argument singularity */
1095 #define OVERFLOW 3 /* overflow range error */
1096 #define UNDERFLOW 4 /* underflow range error */
1097 #define TLOSS 5 /* total loss of precision */
1098 #define PLOSS 6 /* partial loss of precision */
1099 #define INVALID 7 /* NaN-producing operation */
1101 /* e type constants used by high precision check routines */
1103 /*include "ehead.h"*/
1105 unsigned EMUSHORT ezero[NE] =
1107 0, 0000000, 0000000, 0000000, 0000000, 0000000,};
1108 extern unsigned EMUSHORT ezero[];
1111 unsigned EMUSHORT ehalf[NE] =
1113 0, 0000000, 0000000, 0000000, 0100000, 0x3ffe,};
1114 extern unsigned EMUSHORT ehalf[];
1117 unsigned EMUSHORT eone[NE] =
1119 0, 0000000, 0000000, 0000000, 0100000, 0x3fff,};
1120 extern unsigned EMUSHORT eone[];
1123 unsigned EMUSHORT etwo[NE] =
1125 0, 0000000, 0000000, 0000000, 0100000, 0040000,};
1126 extern unsigned EMUSHORT etwo[];
1129 unsigned EMUSHORT e32[NE] =
1131 0, 0000000, 0000000, 0000000, 0100000, 0040004,};
1132 extern unsigned EMUSHORT e32[];
1134 /* 6.93147180559945309417232121458176568075500134360255E-1 */
1135 unsigned EMUSHORT elog2[NE] =
1137 0xc9e4, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1138 extern unsigned EMUSHORT elog2[];
1140 /* 1.41421356237309504880168872420969807856967187537695E0 */
1141 unsigned EMUSHORT esqrt2[NE] =
1143 0x597e, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1144 extern unsigned EMUSHORT esqrt2[];
1147 * 1.12837916709551257389615890312154517168810125865800E0 */
1148 unsigned EMUSHORT eoneopi[NE] =
1150 0x71d5, 0x688d, 0012333, 0135202, 0110156, 0x3fff,};
1151 extern unsigned EMUSHORT eoneopi[];
1153 /* 3.14159265358979323846264338327950288419716939937511E0 */
1154 unsigned EMUSHORT epi[NE] =
1156 0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1157 extern unsigned EMUSHORT epi[];
1159 /* 5.7721566490153286060651209008240243104215933593992E-1 */
1160 unsigned EMUSHORT eeul[NE] =
1162 0xd1be, 0xc7a4, 0076660, 0063743, 0111704, 0x3ffe,};
1163 extern unsigned EMUSHORT eeul[];
1172 /* Control register for rounding precision.
1173 * This can be set to 80 (if NE=6), 64, 56, 53, or 24 bits.
1178 void eaddm (), esubm (), emdnorm (), asctoeg ();
1179 static void toe24 (), toe53 (), toe64 ();
1180 void eremain (), einit (), eiremain ();
1181 int ecmpm (), edivm (), emulm ();
1182 void emovi (), emovo (), emovz (), ecleaz (), ecleazs (), eadd1 ();
1183 void etodec (), todec (), dectoe ();
1194 ; Clear out entire external format number.
1196 ; unsigned EMUSHORT x[];
1202 register unsigned EMUSHORT *x;
1206 for (i = 0; i < NE; i++)
1212 /* Move external format number from a to b.
1219 register unsigned EMUSHORT *a, *b;
1223 for (i = 0; i < NE; i++)
1229 ; Absolute value of external format number
1237 unsigned EMUSHORT x[]; /* x is the memory address of a short */
1240 x[NE - 1] &= 0x7fff; /* sign is top bit of last word of external format */
1247 ; Negate external format number
1249 ; unsigned EMUSHORT x[NE];
1255 unsigned EMUSHORT x[];
1262 x[NE - 1] ^= 0x8000; /* Toggle the sign bit */
1267 /* Return 1 if external format number is negative,
1268 * else return zero, including when it is a NaN.
1272 unsigned EMUSHORT x[];
1279 if (x[NE - 1] & 0x8000)
1286 /* Return 1 if external format number is infinity.
1291 unsigned EMUSHORT x[];
1298 if ((x[NE - 1] & 0x7fff) == 0x7fff)
1305 /* Check if e-type number is not a number.
1306 The bit pattern is one that we defined, so we know for sure how to
1311 unsigned EMUSHORT x[];
1316 /* NaN has maximum exponent */
1317 if ((x[NE - 1] & 0x7fff) != 0x7fff)
1319 /* ... and non-zero significand field. */
1320 for (i = 0; i < NE - 1; i++)
1329 /* Fill external format number with infinity pattern (IEEE)
1330 or largest possible number (non-IEEE). */
1334 register unsigned EMUSHORT *x;
1339 for (i = 0; i < NE - 1; i++)
1343 for (i = 0; i < NE - 1; i++)
1367 /* Output an e-type NaN.
1368 This generates Intel's quiet NaN pattern for extended real.
1369 The exponent is 7fff, the leading mantissa word is c000. */
1373 register unsigned EMUSHORT *x;
1377 for (i = 0; i < NE - 2; i++)
1384 /* Move in external format number,
1385 * converting it to internal format.
1389 unsigned EMUSHORT *a, *b;
1391 register unsigned EMUSHORT *p, *q;
1395 p = a + (NE - 1); /* point to last word of external number */
1396 /* get the sign bit */
1401 /* get the exponent */
1403 *q++ &= 0x7fff; /* delete the sign bit */
1405 if ((*(q - 1) & 0x7fff) == 0x7fff)
1411 for (i = 3; i < NI; i++)
1416 for (i = 2; i < NI; i++)
1421 /* clear high guard word */
1423 /* move in the significand */
1424 for (i = 0; i < NE - 1; i++)
1426 /* clear low guard word */
1431 /* Move internal format number out,
1432 * converting it to external format.
1436 unsigned EMUSHORT *a, *b;
1438 register unsigned EMUSHORT *p, *q;
1439 unsigned EMUSHORT i;
1442 q = b + (NE - 1); /* point to output exponent */
1443 /* combine sign and exponent */
1446 *q-- = *p++ | 0x8000;
1450 if (*(p - 1) == 0x7fff)
1463 /* skip over guard word */
1465 /* move the significand */
1466 for (i = 0; i < NE - 1; i++)
1473 /* Clear out internal format number.
1478 register unsigned EMUSHORT *xi;
1482 for (i = 0; i < NI; i++)
1487 /* same, but don't touch the sign. */
1491 register unsigned EMUSHORT *xi;
1496 for (i = 0; i < NI - 1; i++)
1502 /* Move internal format number from a to b.
1506 register unsigned EMUSHORT *a, *b;
1510 for (i = 0; i < NI - 1; i++)
1512 /* clear low guard word */
1516 /* Generate internal format NaN.
1517 The explicit pattern for this is maximum exponent and
1518 top two significand bits set. */
1522 unsigned EMUSHORT x[];
1530 /* Return nonzero if internal format number is a NaN. */
1534 unsigned EMUSHORT x[];
1538 if ((x[E] & 0x7fff) == 0x7fff)
1540 for (i = M + 1; i < NI; i++)
1549 /* Fill internal format number with infinity pattern.
1550 This has maximum exponent and significand all zeros. */
1554 unsigned EMUSHORT x[];
1561 /* Return nonzero if internal format number is infinite. */
1565 unsigned EMUSHORT x[];
1572 if ((x[E] & 0x7fff) == 0x7fff)
1579 ; Compare significands of numbers in internal format.
1580 ; Guard words are included in the comparison.
1582 ; unsigned EMUSHORT a[NI], b[NI];
1585 ; for the significands:
1586 ; returns +1 if a > b
1592 register unsigned EMUSHORT *a, *b;
1596 a += M; /* skip up to significand area */
1598 for (i = M; i < NI; i++)
1606 if (*(--a) > *(--b))
1614 ; Shift significand down by 1 bit
1619 register unsigned EMUSHORT *x;
1621 register unsigned EMUSHORT bits;
1624 x += M; /* point to significand area */
1627 for (i = M; i < NI; i++)
1642 ; Shift significand up by 1 bit
1647 register unsigned EMUSHORT *x;
1649 register unsigned EMUSHORT bits;
1655 for (i = M; i < NI; i++)
1670 ; Shift significand down by 8 bits
1675 register unsigned EMUSHORT *x;
1677 register unsigned EMUSHORT newbyt, oldbyt;
1682 for (i = M; i < NI; i++)
1693 ; Shift significand up by 8 bits
1698 register unsigned EMUSHORT *x;
1701 register unsigned EMUSHORT newbyt, oldbyt;
1706 for (i = M; i < NI; i++)
1717 ; Shift significand up by 16 bits
1722 register unsigned EMUSHORT *x;
1725 register unsigned EMUSHORT *p;
1730 for (i = M; i < NI - 1; i++)
1737 ; Shift significand down by 16 bits
1742 register unsigned EMUSHORT *x;
1745 register unsigned EMUSHORT *p;
1750 for (i = M; i < NI - 1; i++)
1763 unsigned EMUSHORT *x, *y;
1765 register unsigned EMULONG a;
1772 for (i = M; i < NI; i++)
1774 a = (unsigned EMULONG) (*x) + (unsigned EMULONG) (*y) + carry;
1779 *y = (unsigned EMUSHORT) a;
1786 ; Subtract significands
1792 unsigned EMUSHORT *x, *y;
1801 for (i = M; i < NI; i++)
1803 a = (unsigned EMULONG) (*y) - (unsigned EMULONG) (*x) - carry;
1808 *y = (unsigned EMUSHORT) a;
1815 /* Divide significands */
1817 static unsigned EMUSHORT equot[NI];
1821 unsigned EMUSHORT den[], num[];
1824 register unsigned EMUSHORT *p, *q;
1825 unsigned EMUSHORT j;
1831 for (i = M; i < NI; i++)
1836 /* Use faster compare and subtraction if denominator
1837 * has only 15 bits of significance.
1842 for (i = M + 3; i < NI; i++)
1847 if ((den[M + 1] & 1) != 0)
1855 for (i = 0; i < NBITS + 2; i++)
1873 /* The number of quotient bits to calculate is
1874 * NBITS + 1 scaling guard bit + 1 roundoff bit.
1879 for (i = 0; i < NBITS + 2; i++)
1881 if (ecmpm (den, num) <= 0)
1884 j = 1; /* quotient bit = 1 */
1898 /* test for nonzero remainder after roundoff bit */
1901 for (i = M; i < NI; i++)
1909 for (i = 0; i < NI; i++)
1915 /* Multiply significands */
1918 unsigned EMUSHORT a[], b[];
1920 unsigned EMUSHORT *p, *q;
1925 for (i = M; i < NI; i++)
1930 while (*p == 0) /* significand is not supposed to be all zero */
1935 if ((*p & 0xff) == 0)
1943 for (i = 0; i < k; i++)
1947 /* remember if there were any nonzero bits shifted out */
1954 for (i = 0; i < NI; i++)
1957 /* return flag for lost nonzero bits */
1964 * Normalize and round off.
1966 * The internal format number to be rounded is "s".
1967 * Input "lost" indicates whether or not the number is exact.
1968 * This is the so-called sticky bit.
1970 * Input "subflg" indicates whether the number was obtained
1971 * by a subtraction operation. In that case if lost is nonzero
1972 * then the number is slightly smaller than indicated.
1974 * Input "exp" is the biased exponent, which may be negative.
1975 * the exponent field of "s" is ignored but is replaced by
1976 * "exp" as adjusted by normalization and rounding.
1978 * Input "rcntrl" is the rounding control.
1981 static int rlast = -1;
1983 static unsigned EMUSHORT rmsk = 0;
1984 static unsigned EMUSHORT rmbit = 0;
1985 static unsigned EMUSHORT rebit = 0;
1987 static unsigned EMUSHORT rbit[NI];
1990 emdnorm (s, lost, subflg, exp, rcntrl)
1991 unsigned EMUSHORT s[];
1998 unsigned EMUSHORT r;
2003 /* a blank significand could mean either zero or infinity. */
2016 if ((j > NBITS) && (exp < 32767))
2024 if (exp > (EMULONG) (-NBITS - 1))
2037 /* Round off, unless told not to by rcntrl. */
2040 /* Set up rounding parameters if the control register changed. */
2041 if (rndprc != rlast)
2048 rw = NI - 1; /* low guard word */
2063 /* For DEC arithmetic */
2112 /* These tests assume NI = 8 */
2132 if ((r & rmbit) != 0)
2137 { /* round to even */
2138 if ((s[re] & rebit) == 0)
2150 if ((rndprc < 64) && (exp <= 0))
2155 { /* overflow on roundoff */
2168 for (i = 2; i < NI - 1; i++)
2171 warning ("floating point overflow");
2175 for (i = M + 1; i < NI - 1; i++)
2193 s[1] = (unsigned EMUSHORT) exp;
2199 ; Subtract external format numbers.
2201 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2202 ; esub (a, b, c); c = b - a
2205 static int subflg = 0;
2209 unsigned EMUSHORT *a, *b, *c;
2223 /* Infinity minus infinity is a NaN.
2224 Test for subtracting infinities of the same sign. */
2225 if (eisinf (a) && eisinf (b)
2226 && ((eisneg (a) ^ eisneg (b)) == 0))
2228 mtherr ("esub", INVALID);
2241 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2242 ; eadd (a, b, c); c = b + a
2246 unsigned EMUSHORT *a, *b, *c;
2250 /* NaN plus anything is a NaN. */
2261 /* Infinity minus infinity is a NaN.
2262 Test for adding infinities of opposite signs. */
2263 if (eisinf (a) && eisinf (b)
2264 && ((eisneg (a) ^ eisneg (b)) != 0))
2266 mtherr ("esub", INVALID);
2277 unsigned EMUSHORT *a, *b, *c;
2279 unsigned EMUSHORT ai[NI], bi[NI], ci[NI];
2281 EMULONG lt, lta, ltb;
2302 /* compare exponents */
2307 { /* put the larger number in bi */
2317 if (lt < (EMULONG) (-NBITS - 1))
2318 goto done; /* answer same as larger addend */
2320 lost = eshift (ai, k); /* shift the smaller number down */
2324 /* exponents were the same, so must compare significands */
2327 { /* the numbers are identical in magnitude */
2328 /* if different signs, result is zero */
2334 /* if same sign, result is double */
2335 /* double denomalized tiny number */
2336 if ((bi[E] == 0) && ((bi[3] & 0x8000) == 0))
2341 /* add 1 to exponent unless both are zero! */
2342 for (j = 1; j < NI - 1; j++)
2346 /* This could overflow, but let emovo take care of that. */
2351 bi[E] = (unsigned EMUSHORT) ltb;
2355 { /* put the larger number in bi */
2371 emdnorm (bi, lost, subflg, ltb, 64);
2382 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2383 ; ediv (a, b, c); c = b / a
2387 unsigned EMUSHORT *a, *b, *c;
2389 unsigned EMUSHORT ai[NI], bi[NI];
2391 EMULONG lt, lta, ltb;
2394 /* Return any NaN input. */
2405 /* Zero over zero, or infinity over infinity, is a NaN. */
2406 if (((ecmp (a, ezero) == 0) && (ecmp (b, ezero) == 0))
2407 || (eisinf (a) && eisinf (b)))
2409 mtherr ("ediv", INVALID);
2414 /* Infinity over anything else is infinity. */
2418 if (eisneg (a) ^ eisneg (b))
2419 *(c + (NE - 1)) = 0x8000;
2421 *(c + (NE - 1)) = 0;
2425 /* Anything else over infinity is zero. */
2437 { /* See if numerator is zero. */
2438 for (i = 1; i < NI - 1; i++)
2442 ltb -= enormlz (bi);
2452 { /* possible divide by zero */
2453 for (i = 1; i < NI - 1; i++)
2457 lta -= enormlz (ai);
2462 *(c + (NE - 1)) = 0;
2464 *(c + (NE - 1)) = 0x8000;
2465 /* Divide by zero is not an invalid operation.
2466 It is a divide-by-zero operation! */
2468 mtherr ("ediv", SING);
2474 /* calculate exponent */
2475 lt = ltb - lta + EXONE;
2476 emdnorm (bi, i, 0, lt, 64);
2490 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2491 ; emul (a, b, c); c = b * a
2495 unsigned EMUSHORT *a, *b, *c;
2497 unsigned EMUSHORT ai[NI], bi[NI];
2499 EMULONG lt, lta, ltb;
2502 /* NaN times anything is the same NaN. */
2513 /* Zero times infinity is a NaN. */
2514 if ((eisinf (a) && (ecmp (b, ezero) == 0))
2515 || (eisinf (b) && (ecmp (a, ezero) == 0)))
2517 mtherr ("emul", INVALID);
2522 /* Infinity times anything else is infinity. */
2524 if (eisinf (a) || eisinf (b))
2526 if (eisneg (a) ^ eisneg (b))
2527 *(c + (NE - 1)) = 0x8000;
2529 *(c + (NE - 1)) = 0;
2540 for (i = 1; i < NI - 1; i++)
2544 lta -= enormlz (ai);
2555 for (i = 1; i < NI - 1; i++)
2559 ltb -= enormlz (bi);
2568 /* Multiply significands */
2570 /* calculate exponent */
2571 lt = lta + ltb - (EXONE - 1);
2572 emdnorm (bi, j, 0, lt, 64);
2573 /* calculate sign of product */
2585 ; Convert IEEE double precision to e type
2587 ; unsigned EMUSHORT x[N+2];
2592 unsigned EMUSHORT *pe, *y;
2596 dectoe (pe, y); /* see etodec.c */
2600 register unsigned EMUSHORT r;
2601 register unsigned EMUSHORT *e, *p;
2602 unsigned EMUSHORT yy[NI];
2606 denorm = 0; /* flag if denormalized number */
2615 yy[M] = (r & 0x0f) | 0x10;
2616 r &= ~0x800f; /* strip sign and 4 significand bits */
2622 if (((pe[3] & 0xf) != 0) || (pe[2] != 0)
2623 || (pe[1] != 0) || (pe[0] != 0))
2629 if (((pe[0] & 0xf) != 0) || (pe[1] != 0)
2630 || (pe[2] != 0) || (pe[3] != 0))
2642 #endif /* INFINITY */
2644 /* If zero exponent, then the significand is denormalized.
2645 * So, take back the understood high significand bit. */
2667 { /* if zero exponent, then normalize the significand */
2668 if ((k = enormlz (yy)) > NBITS)
2671 yy[E] -= (unsigned EMUSHORT) (k - 1);
2674 #endif /* not DEC */
2679 unsigned EMUSHORT *pe, *y;
2681 unsigned EMUSHORT yy[NI];
2682 unsigned EMUSHORT *e, *p, *q;
2687 for (i = 0; i < NE - 5; i++)
2690 for (i = 0; i < 5; i++)
2694 for (i = 0; i < 5; i++)
2698 p = &yy[0] + (NE - 1);
2701 for (i = 0; i < 4; i++)
2711 for (i = 0; i < 4; i++)
2720 for (i = 1; i <= 4; i++)
2735 #endif /* INFINITY */
2736 for (i = 0; i < NE; i++)
2742 ; Convert IEEE single precision to e type
2744 ; unsigned EMUSHORT x[N+2];
2749 unsigned EMUSHORT *pe, *y;
2751 register unsigned EMUSHORT r;
2752 register unsigned EMUSHORT *e, *p;
2753 unsigned EMUSHORT yy[NI];
2757 denorm = 0; /* flag if denormalized number */
2769 yy[M] = (r & 0x7f) | 0200;
2770 r &= ~0x807f; /* strip sign and 7 significand bits */
2776 if (((pe[0] & 0x7f) != 0) || (pe[1] != 0))
2782 if (((pe[1] & 0x7f) != 0) || (pe[0] != 0))
2794 #endif /* INFINITY */
2796 /* If zero exponent, then the significand is denormalized.
2797 * So, take back the understood high significand bit. */
2818 { /* if zero exponent, then normalize the significand */
2819 if ((k = enormlz (yy)) > NBITS)
2822 yy[E] -= (unsigned EMUSHORT) (k - 1);
2830 unsigned EMUSHORT *x, *e;
2832 unsigned EMUSHORT xi[NI];
2839 make_nan (e, XFmode);
2844 /* adjust exponent for offset */
2845 exp = (EMULONG) xi[E];
2850 /* round off to nearest or even */
2853 emdnorm (xi, 0, 0, exp, 64);
2859 /* move out internal format to ieee long double */
2862 unsigned EMUSHORT *a, *b;
2864 register unsigned EMUSHORT *p, *q;
2865 unsigned EMUSHORT i;
2870 make_nan (b, XFmode);
2878 q = b + 4; /* point to output exponent */
2879 #if LONG_DOUBLE_TYPE_SIZE == 96
2880 /* Clear the last two bytes of 12-byte Intel format */
2885 /* combine sign and exponent */
2889 *q++ = *p++ | 0x8000;
2895 *q-- = *p++ | 0x8000;
2899 /* skip over guard word */
2901 /* move the significand */
2903 for (i = 0; i < 4; i++)
2906 for (i = 0; i < 4; i++)
2913 ; e type to IEEE double precision
2915 ; unsigned EMUSHORT x[NE];
2923 unsigned EMUSHORT *x, *e;
2925 etodec (x, e); /* see etodec.c */
2930 unsigned EMUSHORT *x, *y;
2939 unsigned EMUSHORT *x, *e;
2941 unsigned EMUSHORT xi[NI];
2948 make_nan (e, DFmode);
2953 /* adjust exponent for offsets */
2954 exp = (EMULONG) xi[E] - (EXONE - 0x3ff);
2959 /* round off to nearest or even */
2962 emdnorm (xi, 0, 0, exp, 64);
2971 unsigned EMUSHORT *x, *y;
2973 unsigned EMUSHORT i;
2974 unsigned EMUSHORT *p;
2979 make_nan (y, DFmode);
2987 *y = 0; /* output high order */
2989 *y = 0x8000; /* output sign bit */
2992 if (i >= (unsigned int) 2047)
2993 { /* Saturate at largest number less than infinity. */
3008 *y |= (unsigned EMUSHORT) 0x7fef;
3032 i |= *p++ & (unsigned EMUSHORT) 0x0f; /* *p = xi[M] */
3033 *y |= (unsigned EMUSHORT) i; /* high order output already has sign bit set */
3047 #endif /* not DEC */
3052 ; e type to IEEE single precision
3054 ; unsigned EMUSHORT x[N+2];
3059 unsigned EMUSHORT *x, *e;
3062 unsigned EMUSHORT xi[NI];
3068 make_nan (e, SFmode);
3073 /* adjust exponent for offsets */
3074 exp = (EMULONG) xi[E] - (EXONE - 0177);
3079 /* round off to nearest or even */
3082 emdnorm (xi, 0, 0, exp, 64);
3090 unsigned EMUSHORT *x, *y;
3092 unsigned EMUSHORT i;
3093 unsigned EMUSHORT *p;
3098 make_nan (y, SFmode);
3109 *y = 0; /* output high order */
3111 *y = 0x8000; /* output sign bit */
3114 /* Handle overflow cases. */
3118 *y |= (unsigned EMUSHORT) 0x7f80;
3129 #else /* no INFINITY */
3130 *y |= (unsigned EMUSHORT) 0x7f7f;
3144 #endif /* no INFINITY */
3156 i |= *p++ & (unsigned EMUSHORT) 0x7f; /* *p = xi[M] */
3157 *y |= i; /* high order output already has sign bit set */
3171 /* Compare two e type numbers.
3173 * unsigned EMUSHORT a[NE], b[NE];
3176 * returns +1 if a > b
3179 * -2 if either a or b is a NaN.
3183 unsigned EMUSHORT *a, *b;
3185 unsigned EMUSHORT ai[NI], bi[NI];
3186 register unsigned EMUSHORT *p, *q;
3191 if (eisnan (a) || eisnan (b))
3200 { /* the signs are different */
3202 for (i = 1; i < NI - 1; i++)
3216 /* both are the same sign */
3231 return (0); /* equality */
3237 if (*(--p) > *(--q))
3238 return (msign); /* p is bigger */
3240 return (-msign); /* p is littler */
3246 /* Find nearest integer to x = floor (x + 0.5)
3248 * unsigned EMUSHORT x[NE], y[NE]
3253 unsigned EMUSHORT *x, *y;
3263 ; convert long integer to e type
3266 ; unsigned EMUSHORT x[NE];
3268 ; note &l is the memory address of l
3272 long *lp; /* lp is the memory address of a long integer */
3273 unsigned EMUSHORT *y; /* y is the address of a short */
3275 unsigned EMUSHORT yi[NI];
3282 /* make it positive */
3283 ll = (unsigned long) (-(*lp));
3284 yi[0] = 0xffff; /* put correct sign in the e type number */
3288 ll = (unsigned long) (*lp);
3290 /* move the long integer to yi significand area */
3291 yi[M] = (unsigned EMUSHORT) (ll >> 16);
3292 yi[M + 1] = (unsigned EMUSHORT) ll;
3294 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
3295 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
3296 ecleaz (yi); /* it was zero */
3298 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
3299 emovo (yi, y); /* output the answer */
3303 ; convert unsigned long integer to e type
3306 ; unsigned EMUSHORT x[NE];
3308 ; note &l is the memory address of l
3312 unsigned long *lp; /* lp is the memory address of a long integer */
3313 unsigned EMUSHORT *y; /* y is the address of a short */
3315 unsigned EMUSHORT yi[NI];
3322 /* move the long integer to ayi significand area */
3323 yi[M] = (unsigned EMUSHORT) (ll >> 16);
3324 yi[M + 1] = (unsigned EMUSHORT) ll;
3326 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
3327 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
3328 ecleaz (yi); /* it was zero */
3330 yi[E] -= (unsigned EMUSHORT) k; /* subtract shift count from exponent */
3331 emovo (yi, y); /* output the answer */
3336 ; Find long integer and fractional parts
3339 ; unsigned EMUSHORT x[NE], frac[NE];
3340 ; xifrac (x, &i, frac);
3342 The integer output has the sign of the input. The fraction is
3343 the positive fractional part of abs (x).
3347 unsigned EMUSHORT *x;
3349 unsigned EMUSHORT *frac;
3351 unsigned EMUSHORT xi[NI];
3355 k = (int) xi[E] - (EXONE - 1);
3358 /* if exponent <= 0, integer = 0 and real output is fraction */
3363 if (k > (HOST_BITS_PER_LONG - 1))
3366 ; long integer overflow: output large integer
3367 ; and correct fraction
3370 *i = ((unsigned long) 1) << (HOST_BITS_PER_LONG - 1);
3372 *i = (((unsigned long) 1) << (HOST_BITS_PER_LONG - 1)) - 1;
3375 warning ("overflow on truncation to integer");
3382 ; shift more than 16 bits: shift up k-16, output the integer,
3383 ; then complete the shift to get the fraction.
3388 *i = (long) (((unsigned long) xi[M] << 16) | xi[M + 1]);
3393 /* shift not more than 16 bits */
3395 *i = (long) xi[M] & 0xffff;
3406 if ((k = enormlz (xi)) > NBITS)
3409 xi[E] -= (unsigned EMUSHORT) k;
3416 ; Find unsigned long integer and fractional parts
3419 ; unsigned EMUSHORT x[NE], frac[NE];
3420 ; xifrac (x, &i, frac);
3422 A negative e type input yields integer output = 0
3423 but correct fraction.
3426 euifrac (x, i, frac)
3427 unsigned EMUSHORT *x;
3429 unsigned EMUSHORT *frac;
3431 unsigned EMUSHORT xi[NI];
3435 k = (int) xi[E] - (EXONE - 1);
3438 /* if exponent <= 0, integer = 0 and argument is fraction */
3446 ; long integer overflow: output large integer
3447 ; and correct fraction
3452 warning ("overflow on truncation to unsigned integer");
3459 ; shift more than 16 bits: shift up k-16, output the integer,
3460 ; then complete the shift to get the fraction.
3465 *i = (long) (((unsigned long) xi[M] << 16) | xi[M + 1]);
3470 /* shift not more than 16 bits */
3472 *i = (long) xi[M] & 0xffff;
3482 if ((k = enormlz (xi)) > NBITS)
3485 xi[E] -= (unsigned EMUSHORT) k;
3495 ; Shifts significand area up or down by the number of bits
3496 ; given by the variable sc.
3500 unsigned EMUSHORT *x;
3503 unsigned EMUSHORT lost;
3504 unsigned EMUSHORT *p;
3517 lost |= *p; /* remember lost bits */
3558 return ((int) lost);
3566 ; Shift normalizes the significand area pointed to by argument
3567 ; shift count (up = positive) is returned.
3571 unsigned EMUSHORT x[];
3573 register unsigned EMUSHORT *p;
3582 return (0); /* already normalized */
3587 /* With guard word, there are NBITS+16 bits available.
3588 * return true if all are zero.
3593 /* see if high byte is zero */
3594 while ((*p & 0xff00) == 0)
3599 /* now shift 1 bit at a time */
3600 while ((*p & 0x8000) == 0)
3606 mtherr ("enormlz", UNDERFLOW);
3612 /* Normalize by shifting down out of the high guard word
3613 of the significand */
3628 mtherr ("enormlz", OVERFLOW);
3638 /* Convert e type number to decimal format ASCII string.
3639 * The constants are for 64 bit precision.
3645 static unsigned EMUSHORT etens[NTEN + 1][NE] =
3647 {0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
3648 {0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
3649 {0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
3650 {0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
3651 {0xddbc, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
3652 {0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
3653 {0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
3654 {0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
3655 {0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
3656 {0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
3657 {0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
3658 {0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
3659 {0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
3662 static unsigned EMUSHORT emtens[NTEN + 1][NE] =
3664 {0x2de4, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
3665 {0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
3666 {0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
3667 {0x7133, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
3668 {0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
3669 {0xac7d, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
3670 {0x3f24, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
3671 {0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
3672 {0x4c2f, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
3673 {0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
3674 {0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
3675 {0x3d71, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
3676 {0xcccd, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
3680 e24toasc (x, string, ndigs)
3681 unsigned EMUSHORT x[];
3685 unsigned EMUSHORT w[NI];
3688 etoasc (w, string, ndigs);
3693 e53toasc (x, string, ndigs)
3694 unsigned EMUSHORT x[];
3698 unsigned EMUSHORT w[NI];
3701 etoasc (w, string, ndigs);
3706 e64toasc (x, string, ndigs)
3707 unsigned EMUSHORT x[];
3711 unsigned EMUSHORT w[NI];
3714 etoasc (w, string, ndigs);
3718 static char wstring[80]; /* working storage for ASCII output */
3721 etoasc (x, string, ndigs)
3722 unsigned EMUSHORT x[];
3727 unsigned EMUSHORT y[NI], t[NI], u[NI], w[NI];
3728 unsigned EMUSHORT *p, *r, *ten;
3729 unsigned EMUSHORT sign;
3730 int i, j, k, expon, rndsav;
3732 unsigned EMUSHORT m;
3743 sprintf (wstring, " NaN ");
3747 rndprc = NBITS; /* set to full precision */
3748 emov (x, y); /* retain external format */
3749 if (y[NE - 1] & 0x8000)
3752 y[NE - 1] &= 0x7fff;
3759 ten = &etens[NTEN][0];
3761 /* Test for zero exponent */
3764 for (k = 0; k < NE - 1; k++)
3767 goto tnzro; /* denormalized number */
3769 goto isone; /* legal all zeros */
3773 /* Test for infinity. */
3774 if (y[NE - 1] == 0x7fff)
3777 sprintf (wstring, " -Infinity ");
3779 sprintf (wstring, " Infinity ");
3783 /* Test for exponent nonzero but significand denormalized.
3784 * This is an error condition.
3786 if ((y[NE - 1] != 0) && ((y[NE - 2] & 0x8000) == 0))
3788 mtherr ("etoasc", DOMAIN);
3789 sprintf (wstring, "NaN");
3793 /* Compare to 1.0 */
3802 { /* Number is greater than 1 */
3803 /* Convert significand to an integer and strip trailing decimal zeros. */
3805 u[NE - 1] = EXONE + NBITS - 1;
3807 p = &etens[NTEN - 4][0];
3813 for (j = 0; j < NE - 1; j++)
3826 /* Rescale from integer significand */
3827 u[NE - 1] += y[NE - 1] - (unsigned int) (EXONE + NBITS - 1);
3829 /* Find power of 10 */
3833 /* An unordered compare result shouldn't happen here. */
3834 while (ecmp (ten, u) <= 0)
3836 if (ecmp (p, u) <= 0)
3849 { /* Number is less than 1.0 */
3850 /* Pad significand with trailing decimal zeros. */
3853 while ((y[NE - 2] & 0x8000) == 0)
3862 for (i = 0; i < NDEC + 1; i++)
3864 if ((w[NI - 1] & 0x7) != 0)
3866 /* multiply by 10 */
3879 if (eone[NE - 1] <= u[1])
3891 while (ecmp (eone, w) > 0)
3893 if (ecmp (p, w) >= 0)
3908 /* Find the first (leading) digit. */
3914 digit = equot[NI - 1];
3915 while ((digit == 0) && (ecmp (y, ezero) != 0))
3923 digit = equot[NI - 1];
3931 /* Examine number of digits requested by caller. */
3949 *s++ = (char )digit + '0';
3952 /* Generate digits after the decimal point. */
3953 for (k = 0; k <= ndigs; k++)
3955 /* multiply current number by 10, without normalizing */
3962 *s++ = (char) equot[NI - 1] + '0';
3964 digit = equot[NI - 1];
3967 /* round off the ASCII string */
3970 /* Test for critical rounding case in ASCII output. */
3974 if (ecmp (t, ezero) != 0)
3975 goto roun; /* round to nearest */
3976 if ((*(s - 1) & 1) == 0)
3977 goto doexp; /* round to even */
3979 /* Round up and propagate carry-outs */
3983 /* Carry out to most significant digit? */
3990 /* Most significant digit carries to 10? */
3998 /* Round up and carry out from less significant digits */
4010 sprintf (ss, "e+%d", expon);
4012 sprintf (ss, "e%d", expon);
4014 sprintf (ss, "e%d", expon);
4017 /* copy out the working string */
4020 while (*ss == ' ') /* strip possible leading space */
4022 while ((*s++ = *ss++) != '\0')
4031 ; ASCTOQ.MAC LATEST REV: 11 JAN 84
4034 ; Convert ASCII string to quadruple precision floating point
4036 ; Numeric input is free field decimal number
4037 ; with max of 15 digits with or without
4038 ; decimal point entered as ASCII from teletype.
4039 ; Entering E after the number followed by a second
4040 ; number causes the second number to be interpreted
4041 ; as a power of 10 to be multiplied by the first number
4042 ; (i.e., "scientific" notation).
4045 ; asctoq (string, q);
4048 /* ASCII to single */
4052 unsigned EMUSHORT *y;
4058 /* ASCII to double */
4062 unsigned EMUSHORT *y;
4072 /* ASCII to long double */
4076 unsigned EMUSHORT *y;
4081 /* ASCII to super double */
4085 unsigned EMUSHORT *y;
4087 asctoeg (s, y, NBITS);
4090 /* Space to make a copy of the input string: */
4091 static char lstr[82];
4094 asctoeg (ss, y, oprec)
4096 unsigned EMUSHORT *y;
4099 unsigned EMUSHORT yy[NI], xt[NI], tt[NI];
4100 int esign, decflg, sgnflg, nexp, exp, prec, lost;
4101 int k, trail, c, rndsav;
4103 unsigned EMUSHORT nsign, *p;
4106 /* Copy the input string. */
4108 while (*s == ' ') /* skip leading spaces */
4111 for (k = 0; k < 79; k++)
4113 if ((*sp++ = *s++) == '\0')
4120 rndprc = NBITS; /* Set to full precision */
4133 if ((k >= 0) && (k <= 9))
4135 /* Ignore leading zeros */
4136 if ((prec == 0) && (decflg == 0) && (k == 0))
4138 /* Identify and strip trailing zeros after the decimal point. */
4139 if ((trail == 0) && (decflg != 0))
4142 while ((*sp >= '0') && (*sp <= '9'))
4144 /* Check for syntax error */
4146 if ((c != 'e') && (c != 'E') && (c != '\0')
4147 && (c != '\n') && (c != '\r') && (c != ' ')
4157 /* If enough digits were given to more than fill up the yy register,
4158 * continuing until overflow into the high guard word yy[2]
4159 * guarantees that there will be a roundoff bit at the top
4160 * of the low guard word after normalization.
4165 nexp += 1; /* count digits after decimal point */
4166 eshup1 (yy); /* multiply current number by 10 */
4172 xt[NI - 2] = (unsigned EMUSHORT) k;
4190 case '.': /* decimal point */
4220 mtherr ("asctoe", DOMAIN);
4229 /* Exponent interpretation */
4235 /* check for + or - */
4243 while ((*s >= '0') && (*s <= '9'))
4261 yy[E] = 0x7fff; /* infinity */
4273 /* Pad trailing zeros to minimize power of 10, per IEEE spec. */
4274 while ((nexp > 0) && (yy[2] == 0))
4286 if ((k = enormlz (yy)) > NBITS)
4291 lexp = (EXONE - 1 + NBITS) - k;
4292 emdnorm (yy, lost, 0, lexp, 64);
4293 /* convert to external format */
4296 /* Multiply by 10**nexp. If precision is 64 bits,
4297 * the maximum relative error incurred in forming 10**n
4298 * for 0 <= n <= 324 is 8.2e-20, at 10**180.
4299 * For 0 <= n <= 999, the peak relative error is 1.4e-19 at 10**947.
4300 * For 0 >= n >= -999, it is -1.55e-19 at 10**-435.
4314 { /* Punt. Can't handle this without 2 divides. */
4315 emovi (etens[0], tt);
4322 p = &etens[NTEN][0];
4332 while (exp <= MAXP);
4350 /* Round and convert directly to the destination type */
4352 lexp -= EXONE - 0x3ff;
4353 else if (oprec == 24)
4354 lexp -= EXONE - 0177;
4356 else if (oprec == 56)
4357 lexp -= EXONE - 0201;
4360 emdnorm (yy, k, 0, lexp, 64);
4370 todec (yy, y); /* see etodec.c */
4390 /* y = largest integer not greater than x
4391 * (truncated toward minus infinity)
4393 * unsigned EMUSHORT x[NE], y[NE]
4397 static unsigned EMUSHORT bmask[] =
4420 unsigned EMUSHORT x[], y[];
4422 register unsigned EMUSHORT *p;
4424 unsigned EMUSHORT f[NE];
4426 emov (x, f); /* leave in external format */
4427 expon = (int) f[NE - 1];
4428 e = (expon & 0x7fff) - (EXONE - 1);
4434 /* number of bits to clear out */
4446 /* clear the remaining bits */
4448 /* truncate negatives toward minus infinity */
4451 if ((unsigned EMUSHORT) expon & (unsigned EMUSHORT) 0x8000)
4453 for (i = 0; i < NE - 1; i++)
4465 /* unsigned EMUSHORT x[], s[];
4468 * efrexp (x, exp, s);
4470 * Returns s and exp such that s * 2**exp = x and .5 <= s < 1.
4471 * For example, 1.1 = 0.55 * 2**1
4472 * Handles denormalized numbers properly using long integer exp.
4476 unsigned EMUSHORT x[];
4478 unsigned EMUSHORT s[];
4480 unsigned EMUSHORT xi[NI];
4484 li = (EMULONG) ((EMUSHORT) xi[1]);
4492 *exp = (int) (li - 0x3ffe);
4497 /* unsigned EMUSHORT x[], y[];
4500 * eldexp (x, pwr2, y);
4502 * Returns y = x * 2**pwr2.
4506 unsigned EMUSHORT x[];
4508 unsigned EMUSHORT y[];
4510 unsigned EMUSHORT xi[NI];
4518 emdnorm (xi, i, i, li, 64);
4523 /* c = remainder after dividing b by a
4524 * Least significant integer quotient bits left in equot[].
4528 unsigned EMUSHORT a[], b[], c[];
4530 unsigned EMUSHORT den[NI], num[NI];
4534 || (ecmp (a, ezero) == 0)
4542 if (ecmp (a, ezero) == 0)
4544 mtherr ("eremain", SING);
4550 eiremain (den, num);
4551 /* Sign of remainder = sign of quotient */
4561 unsigned EMUSHORT den[], num[];
4564 unsigned EMUSHORT j;
4567 ld -= enormlz (den);
4569 ln -= enormlz (num);
4573 if (ecmpm (den, num) <= 0)
4587 emdnorm (num, 0, 0, ln, 0);
4592 * Library common error handling routine
4602 * mtherr (fctnam, code);
4608 * This routine may be called to report one of the following
4609 * error conditions (in the include file mconf.h).
4611 * Mnemonic Value Significance
4613 * DOMAIN 1 argument domain error
4614 * SING 2 function singularity
4615 * OVERFLOW 3 overflow range error
4616 * UNDERFLOW 4 underflow range error
4617 * TLOSS 5 total loss of precision
4618 * PLOSS 6 partial loss of precision
4619 * INVALID 7 NaN - producing operation
4620 * EDOM 33 Unix domain error code
4621 * ERANGE 34 Unix range error code
4623 * The default version of the file prints the function name,
4624 * passed to it by the pointer fctnam, followed by the
4625 * error condition. The display is directed to the standard
4626 * output device. The routine then returns to the calling
4627 * program. Users may wish to modify the program to abort by
4628 * calling exit under severe error conditions such as domain
4631 * Since all error conditions pass control to this function,
4632 * the display may be easily changed, eliminated, or directed
4633 * to an error logging device.
4642 Cephes Math Library Release 2.0: April, 1987
4643 Copyright 1984, 1987 by Stephen L. Moshier
4644 Direct inquiries to 30 Frost Street, Cambridge, MA 02140
4647 /* include "mconf.h" */
4649 /* Notice: the order of appearance of the following
4650 * messages is bound to the error codes defined
4654 static char *ermsg[NMSGS] =
4656 "unknown", /* error code 0 */
4657 "domain", /* error code 1 */
4658 "singularity", /* et seq. */
4661 "total loss of precision",
4662 "partial loss of precision",
4676 /* Display string passed by calling program,
4677 * which is supposed to be the name of the
4678 * function in which the error occurred.
4681 /* Display error message defined
4682 * by the code argument.
4684 if ((code <= 0) || (code >= NMSGS))
4686 sprintf (errstr, " %s %s error", name, ermsg[code]);
4689 /* Set global error message word */
4692 /* Return to calling
4697 /* Here is etodec.c .
4702 ; convert DEC double precision to e type
4709 unsigned EMUSHORT *d;
4710 unsigned EMUSHORT *e;
4712 unsigned EMUSHORT y[NI];
4713 register unsigned EMUSHORT r, *p;
4715 ecleaz (y); /* start with a zero */
4716 p = y; /* point to our number */
4717 r = *d; /* get DEC exponent word */
4718 if (*d & (unsigned int) 0x8000)
4719 *p = 0xffff; /* fill in our sign */
4720 ++p; /* bump pointer to our exponent word */
4721 r &= 0x7fff; /* strip the sign bit */
4722 if (r == 0) /* answer = 0 if high order DEC word = 0 */
4726 r >>= 7; /* shift exponent word down 7 bits */
4727 r += EXONE - 0201; /* subtract DEC exponent offset */
4728 /* add our e type exponent offset */
4729 *p++ = r; /* to form our exponent */
4731 r = *d++; /* now do the high order mantissa */
4732 r &= 0177; /* strip off the DEC exponent and sign bits */
4733 r |= 0200; /* the DEC understood high order mantissa bit */
4734 *p++ = r; /* put result in our high guard word */
4736 *p++ = *d++; /* fill in the rest of our mantissa */
4740 eshdn8 (y); /* shift our mantissa down 8 bits */
4748 ; convert e type to DEC double precision
4754 static unsigned EMUSHORT decbit[NI] = {0, 0, 0, 0, 0, 0, 0200, 0};
4758 unsigned EMUSHORT *x, *d;
4760 unsigned EMUSHORT xi[NI];
4761 register unsigned EMUSHORT r;
4769 if (r < (EXONE - 128))
4772 if ((i & 0200) != 0)
4774 if ((i & 0377) == 0200)
4776 if ((i & 0400) != 0)
4778 /* check all less significant bits */
4779 for (j = M + 5; j < NI; j++)
4828 unsigned EMUSHORT *x, *d;
4830 unsigned EMUSHORT xi[NI];
4835 exp = (EMULONG) xi[E] - (EXONE - 0201); /* adjust exponent for offsets */
4836 /* round off to nearest or even */
4839 emdnorm (xi, 0, 0, exp, 64);
4846 unsigned EMUSHORT *x, *y;
4848 unsigned EMUSHORT i;
4849 unsigned EMUSHORT *p;
4889 /* Output a binary NaN bit pattern in the target machine's format. */
4891 /* If special NaN bit patterns are required, define them in tm.h
4892 as arrays of unsigned 16-bit shorts. Otherwise, use the default
4896 unsigned EMUSHORT TFnan[8] =
4897 {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
4900 unsigned EMUSHORT TFnan[8] = {0, 0, 0, 0, 0, 0, 0x8000, 0xffff};
4906 unsigned EMUSHORT XFnan[6] = {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
4909 unsigned EMUSHORT XFnan[6] = {0, 0, 0, 0xc000, 0xffff, 0};
4915 unsigned EMUSHORT DFnan[4] = {0x7fff, 0xffff, 0xffff, 0xffff};
4918 unsigned EMUSHORT DFnan[4] = {0, 0, 0, 0xfff8};
4924 unsigned EMUSHORT SFnan[2] = {0x7fff, 0xffff};
4927 unsigned EMUSHORT SFnan[2] = {0, 0xffc0};
4933 make_nan (nan, mode)
4934 unsigned EMUSHORT *nan;
4935 enum machine_mode mode;
4938 unsigned EMUSHORT *p;
4942 /* Possibly the `reserved operand' patterns on a VAX can be
4943 used like NaN's, but probably not in the same way as IEEE. */
4965 for (i=0; i < n; i++)
4969 #endif /* EMU_NON_COMPILE not defined */