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 ();
303 extern unsigned EMUSHORT ezero[], ehalf[], eone[], etwo[];
304 extern unsigned EMUSHORT elog2[], esqrt2[];
306 /* Pack output array with 32-bit numbers obtained from
307 array containing 16-bit numbers, swapping ends if required. */
310 unsigned EMUSHORT e[];
312 enum machine_mode mode;
322 /* Swap halfwords in the third long. */
323 th = (unsigned long) e[4] & 0xffff;
324 t = (unsigned long) e[5] & 0xffff;
327 /* fall into the double case */
331 /* swap halfwords in the second word */
332 th = (unsigned long) e[2] & 0xffff;
333 t = (unsigned long) e[3] & 0xffff;
336 /* fall into the float case */
340 /* swap halfwords in the first word */
341 th = (unsigned long) e[0] & 0xffff;
342 t = (unsigned long) e[1] & 0xffff;
353 /* Pack the output array without swapping. */
360 /* Pack the third long.
361 Each element of the input REAL_VALUE_TYPE array has 16 bit useful bits
363 th = (unsigned long) e[5] & 0xffff;
364 t = (unsigned long) e[4] & 0xffff;
367 /* fall into the double case */
371 /* pack the second long */
372 th = (unsigned long) e[3] & 0xffff;
373 t = (unsigned long) e[2] & 0xffff;
376 /* fall into the float case */
380 /* pack the first long */
381 th = (unsigned long) e[1] & 0xffff;
382 t = (unsigned long) e[0] & 0xffff;
395 /* This is the implementation of the REAL_ARITHMETIC macro.
398 earith (value, icode, r1, r2)
399 REAL_VALUE_TYPE *value;
404 unsigned EMUSHORT d1[NE], d2[NE], v[NE];
410 /* Return NaN input back to the caller. */
413 PUT_REAL (d1, value);
418 PUT_REAL (d2, value);
422 code = (enum tree_code) icode;
430 esub (d2, d1, v); /* d1 - d2 */
438 #ifndef REAL_INFINITY
439 if (ecmp (d2, ezero) == 0)
449 ediv (d2, d1, v); /* d1/d2 */
452 case MIN_EXPR: /* min (d1,d2) */
453 if (ecmp (d1, d2) < 0)
459 case MAX_EXPR: /* max (d1,d2) */
460 if (ecmp (d1, d2) > 0)
473 /* Truncate REAL_VALUE_TYPE toward zero to signed HOST_WIDE_INT
474 * implements REAL_VALUE_RNDZINT (x) (etrunci (x))
480 unsigned EMUSHORT f[NE], g[NE];
496 /* Truncate REAL_VALUE_TYPE toward zero to unsigned HOST_WIDE_INT
497 * implements REAL_VALUE_UNSIGNED_RNDZINT (x) (etruncui (x))
503 unsigned EMUSHORT f[NE], g[NE];
519 /* This is the REAL_VALUE_ATOF function.
520 * It converts a decimal string to binary, rounding off
521 * as indicated by the machine_mode argument. Then it
522 * promotes the rounded value to REAL_VALUE_TYPE.
529 unsigned EMUSHORT tem[NE], e[NE];
554 /* Expansion of REAL_NEGATE.
560 unsigned EMUSHORT e[NE];
575 * implements REAL_VALUE_FIX (x) (eroundi (x))
576 * The type of rounding is left unspecified by real.h.
577 * It is implemented here as round to nearest (add .5 and chop).
583 unsigned EMUSHORT f[NE], g[NE];
590 warning ("conversion from NaN to int");
599 /* Round real to nearest unsigned int
600 * implements REAL_VALUE_UNSIGNED_FIX (x) ((unsigned int) eroundi (x))
601 * Negative input returns zero.
602 * The type of rounding is left unspecified by real.h.
603 * It is implemented here as round to nearest (add .5 and chop).
609 unsigned EMUSHORT f[NE], g[NE];
616 warning ("conversion from NaN to unsigned int");
622 return ((unsigned int)l);
626 /* REAL_VALUE_FROM_INT macro.
629 ereal_from_int (d, i, j)
633 unsigned EMUSHORT df[NE], dg[NE];
642 /* complement and add 1 */
649 eldexp (eone, HOST_BITS_PER_LONG, df);
660 /* REAL_VALUE_FROM_UNSIGNED_INT macro.
663 ereal_from_uint (d, i, j)
667 unsigned EMUSHORT df[NE], dg[NE];
668 unsigned long low, high;
672 eldexp (eone, HOST_BITS_PER_LONG, df);
681 /* REAL_VALUE_TO_INT macro
684 ereal_to_int (low, high, rr)
688 unsigned EMUSHORT d[NE], df[NE], dg[NE], dh[NE];
695 warning ("conversion from NaN to int");
701 /* convert positive value */
708 eldexp (eone, HOST_BITS_PER_LONG, df);
709 ediv (df, d, dg); /* dg = d / 2^32 is the high word */
710 euifrac (dg, high, dh);
711 emul (df, dh, dg); /* fractional part is the low word */
712 euifrac (dg, low, dh);
715 /* complement and add 1 */
725 /* REAL_VALUE_LDEXP macro.
732 unsigned EMUSHORT e[NE], y[NE];
745 /* These routines are conditionally compiled because functions
746 * of the same names may be defined in fold-const.c. */
747 #ifdef REAL_ARITHMETIC
749 /* Check for infinity in a REAL_VALUE_TYPE. */
754 unsigned EMUSHORT e[NE];
765 /* Check whether a REAL_VALUE_TYPE item is a NaN. */
772 return (eisnan (&x));
779 /* Check for a negative REAL_VALUE_TYPE number.
780 * this means strictly less than zero, not -0.
787 unsigned EMUSHORT e[NE];
790 if (ecmp (e, ezero) == -1)
795 /* Expansion of REAL_VALUE_TRUNCATE.
796 * The result is in floating point, rounded to nearest or even.
799 real_value_truncate (mode, arg)
800 enum machine_mode mode;
803 unsigned EMUSHORT e[NE], t[NE];
840 #endif /* REAL_ARITHMETIC defined */
842 /* Target values are arrays of host longs. A long is guaranteed
843 to be at least 32 bits wide. */
849 unsigned EMUSHORT e[NE];
853 endian (e, l, XFmode);
861 unsigned EMUSHORT e[NE];
865 endian (e, l, DFmode);
872 unsigned EMUSHORT e[NE];
877 endian (e, &l, SFmode);
882 ereal_to_decimal (x, s)
886 unsigned EMUSHORT e[NE];
894 REAL_VALUE_TYPE x, y;
896 unsigned EMUSHORT ex[NE], ey[NE];
900 return (ecmp (ex, ey));
907 unsigned EMUSHORT ex[NE];
910 return (eisneg (ex));
913 /* End of REAL_ARITHMETIC interface */
917 * Extended precision IEEE binary floating point arithmetic routines
919 * Numbers are stored in C language as arrays of 16-bit unsigned
920 * short integers. The arguments of the routines are pointers to
924 * External e type data structure, simulates Intel 8087 chip
925 * temporary real format but possibly with a larger significand:
927 * NE-1 significand words (least significant word first,
928 * most significant bit is normally set)
929 * exponent (value = EXONE for 1.0,
930 * top bit is the sign)
933 * Internal data structure of a number (a "word" is 16 bits):
935 * ei[0] sign word (0 for positive, 0xffff for negative)
936 * ei[1] biased exponent (value = EXONE for the number 1.0)
937 * ei[2] high guard word (always zero after normalization)
939 * to ei[NI-2] significand (NI-4 significand words,
940 * most significant word first,
941 * most significant bit is set)
942 * ei[NI-1] low guard word (0x8000 bit is rounding place)
946 * Routines for external format numbers
948 * asctoe (string, e) ASCII string to extended double e type
949 * asctoe64 (string, &d) ASCII string to long double
950 * asctoe53 (string, &d) ASCII string to double
951 * asctoe24 (string, &f) ASCII string to single
952 * asctoeg (string, e, prec) ASCII string to specified precision
953 * e24toe (&f, e) IEEE single precision to e type
954 * e53toe (&d, e) IEEE double precision to e type
955 * e64toe (&d, e) IEEE long double precision to e type
956 * eabs (e) absolute value
957 * eadd (a, b, c) c = b + a
959 * ecmp (a, b) Returns 1 if a > b, 0 if a == b,
960 * -1 if a < b, -2 if either a or b is a NaN.
961 * ediv (a, b, c) c = b / a
962 * efloor (a, b) truncate to integer, toward -infinity
963 * efrexp (a, exp, s) extract exponent and significand
964 * eifrac (e, &l, frac) e to long integer and e type fraction
965 * euifrac (e, &l, frac) e to unsigned long integer and e type fraction
966 * einfin (e) set e to infinity, leaving its sign alone
967 * eldexp (a, n, b) multiply by 2**n
969 * emul (a, b, c) c = b * a
971 * eround (a, b) b = nearest integer value to a
972 * esub (a, b, c) c = b - a
973 * e24toasc (&f, str, n) single to ASCII string, n digits after decimal
974 * e53toasc (&d, str, n) double to ASCII string, n digits after decimal
975 * e64toasc (&d, str, n) long double to ASCII string
976 * etoasc (e, str, n) e to ASCII string, n digits after decimal
977 * etoe24 (e, &f) convert e type to IEEE single precision
978 * etoe53 (e, &d) convert e type to IEEE double precision
979 * etoe64 (e, &d) convert e type to IEEE long double precision
980 * ltoe (&l, e) long (32 bit) integer to e type
981 * ultoe (&l, e) unsigned long (32 bit) integer to e type
982 * eisneg (e) 1 if sign bit of e != 0, else 0
983 * eisinf (e) 1 if e has maximum exponent (non-IEEE)
984 * or is infinite (IEEE)
985 * eisnan (e) 1 if e is a NaN
988 * Routines for internal format numbers
990 * eaddm (ai, bi) add significands, bi = bi + ai
992 * ecleazs (ei) set ei = 0 but leave its sign alone
993 * ecmpm (ai, bi) compare significands, return 1, 0, or -1
994 * edivm (ai, bi) divide significands, bi = bi / ai
995 * emdnorm (ai,l,s,exp) normalize and round off
996 * emovi (a, ai) convert external a to internal ai
997 * emovo (ai, a) convert internal ai to external a
998 * emovz (ai, bi) bi = ai, low guard word of bi = 0
999 * emulm (ai, bi) multiply significands, bi = bi * ai
1000 * enormlz (ei) left-justify the significand
1001 * eshdn1 (ai) shift significand and guards down 1 bit
1002 * eshdn8 (ai) shift down 8 bits
1003 * eshdn6 (ai) shift down 16 bits
1004 * eshift (ai, n) shift ai n bits up (or down if n < 0)
1005 * eshup1 (ai) shift significand and guards up 1 bit
1006 * eshup8 (ai) shift up 8 bits
1007 * eshup6 (ai) shift up 16 bits
1008 * esubm (ai, bi) subtract significands, bi = bi - ai
1009 * eiisinf (ai) 1 if infinite
1010 * eiisnan (ai) 1 if a NaN
1011 * einan (ai) set ai = NaN
1012 * eiinfin (ai) set ai = infinity
1015 * The result is always normalized and rounded to NI-4 word precision
1016 * after each arithmetic operation.
1018 * Exception flags are NOT fully supported.
1020 * Signaling NaN's are NOT supported; they are treated the same
1023 * Define INFINITY for support of infinity; otherwise a
1024 * saturation arithmetic is implemented.
1026 * Define NANS for support of Not-a-Number items; otherwise the
1027 * arithmetic will never produce a NaN output, and might be confused
1029 * If NaN's are supported, the output of `ecmp (a,b)' is -2 if
1030 * either a or b is a NaN. This means asking `if (ecmp (a,b) < 0)'
1031 * may not be legitimate. Use `if (ecmp (a,b) == -1)' for `less than'
1034 * Denormals are always supported here where appropriate (e.g., not
1035 * for conversion to DEC numbers).
1042 * Common include file for math routines
1048 * #include "mconf.h"
1054 * This file contains definitions for error codes that are
1055 * passed to the common error handling routine mtherr
1058 * The file also includes a conditional assembly definition
1059 * for the type of computer arithmetic (Intel IEEE, DEC, Motorola
1060 * IEEE, or UNKnown).
1062 * For Digital Equipment PDP-11 and VAX computers, certain
1063 * IBM systems, and others that use numbers with a 56-bit
1064 * significand, the symbol DEC should be defined. In this
1065 * mode, most floating point constants are given as arrays
1066 * of octal integers to eliminate decimal to binary conversion
1067 * errors that might be introduced by the compiler.
1069 * For computers, such as IBM PC, that follow the IEEE
1070 * Standard for Binary Floating Point Arithmetic (ANSI/IEEE
1071 * Std 754-1985), the symbol IBMPC or MIEEE should be defined.
1072 * These numbers have 53-bit significands. In this mode, constants
1073 * are provided as arrays of hexadecimal 16 bit integers.
1075 * To accommodate other types of computer arithmetic, all
1076 * constants are also provided in a normal decimal radix
1077 * which one can hope are correctly converted to a suitable
1078 * format by the available C language compiler. To invoke
1079 * this mode, the symbol UNK is defined.
1081 * An important difference among these modes is a predefined
1082 * set of machine arithmetic constants for each. The numbers
1083 * MACHEP (the machine roundoff error), MAXNUM (largest number
1084 * represented), and several other parameters are preset by
1085 * the configuration symbol. Check the file const.c to
1086 * ensure that these values are correct for your computer.
1088 * For ANSI C compatibility, define ANSIC equal to 1. Currently
1089 * this affects only the atan2 function and others that use it.
1092 /* Constant definitions for math error conditions. */
1094 #define DOMAIN 1 /* argument domain error */
1095 #define SING 2 /* argument singularity */
1096 #define OVERFLOW 3 /* overflow range error */
1097 #define UNDERFLOW 4 /* underflow range error */
1098 #define TLOSS 5 /* total loss of precision */
1099 #define PLOSS 6 /* partial loss of precision */
1100 #define INVALID 7 /* NaN-producing operation */
1102 /* e type constants used by high precision check routines */
1104 /*include "ehead.h"*/
1106 unsigned EMUSHORT ezero[NE] =
1108 0, 0000000, 0000000, 0000000, 0000000, 0000000,};
1109 extern unsigned EMUSHORT ezero[];
1112 unsigned EMUSHORT ehalf[NE] =
1114 0, 0000000, 0000000, 0000000, 0100000, 0x3ffe,};
1115 extern unsigned EMUSHORT ehalf[];
1118 unsigned EMUSHORT eone[NE] =
1120 0, 0000000, 0000000, 0000000, 0100000, 0x3fff,};
1121 extern unsigned EMUSHORT eone[];
1124 unsigned EMUSHORT etwo[NE] =
1126 0, 0000000, 0000000, 0000000, 0100000, 0040000,};
1127 extern unsigned EMUSHORT etwo[];
1130 unsigned EMUSHORT e32[NE] =
1132 0, 0000000, 0000000, 0000000, 0100000, 0040004,};
1133 extern unsigned EMUSHORT e32[];
1135 /* 6.93147180559945309417232121458176568075500134360255E-1 */
1136 unsigned EMUSHORT elog2[NE] =
1138 0xc9e4, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1139 extern unsigned EMUSHORT elog2[];
1141 /* 1.41421356237309504880168872420969807856967187537695E0 */
1142 unsigned EMUSHORT esqrt2[NE] =
1144 0x597e, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1145 extern unsigned EMUSHORT esqrt2[];
1148 * 1.12837916709551257389615890312154517168810125865800E0 */
1149 unsigned EMUSHORT eoneopi[NE] =
1151 0x71d5, 0x688d, 0012333, 0135202, 0110156, 0x3fff,};
1152 extern unsigned EMUSHORT eoneopi[];
1154 /* 3.14159265358979323846264338327950288419716939937511E0 */
1155 unsigned EMUSHORT epi[NE] =
1157 0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1158 extern unsigned EMUSHORT epi[];
1160 /* 5.7721566490153286060651209008240243104215933593992E-1 */
1161 unsigned EMUSHORT eeul[NE] =
1163 0xd1be, 0xc7a4, 0076660, 0063743, 0111704, 0x3ffe,};
1164 extern unsigned EMUSHORT eeul[];
1173 /* Control register for rounding precision.
1174 * This can be set to 80 (if NE=6), 64, 56, 53, or 24 bits.
1179 void eaddm (), esubm (), emdnorm (), asctoeg ();
1180 static void toe24 (), toe53 (), toe64 ();
1181 void eremain (), einit (), eiremain ();
1182 int ecmpm (), edivm (), emulm ();
1183 void emovi (), emovo (), emovz (), ecleaz (), ecleazs (), eadd1 ();
1184 void etodec (), todec (), dectoe ();
1195 ; Clear out entire external format number.
1197 ; unsigned EMUSHORT x[];
1203 register unsigned EMUSHORT *x;
1207 for (i = 0; i < NE; i++)
1213 /* Move external format number from a to b.
1220 register unsigned EMUSHORT *a, *b;
1224 for (i = 0; i < NE; i++)
1230 ; Absolute value of external format number
1238 unsigned EMUSHORT x[]; /* x is the memory address of a short */
1241 x[NE - 1] &= 0x7fff; /* sign is top bit of last word of external format */
1248 ; Negate external format number
1250 ; unsigned EMUSHORT x[NE];
1256 unsigned EMUSHORT x[];
1263 x[NE - 1] ^= 0x8000; /* Toggle the sign bit */
1268 /* Return 1 if external format number is negative,
1269 * else return zero, including when it is a NaN.
1273 unsigned EMUSHORT x[];
1280 if (x[NE - 1] & 0x8000)
1287 /* Return 1 if external format number is infinity.
1292 unsigned EMUSHORT x[];
1299 if ((x[NE - 1] & 0x7fff) == 0x7fff)
1306 /* Check if e-type number is not a number.
1307 The bit pattern is one that we defined, so we know for sure how to
1312 unsigned EMUSHORT x[];
1317 /* NaN has maximum exponent */
1318 if ((x[NE - 1] & 0x7fff) != 0x7fff)
1320 /* ... and non-zero significand field. */
1321 for (i = 0; i < NE - 1; i++)
1330 /* Fill external format number with infinity pattern (IEEE)
1331 or largest possible number (non-IEEE).
1332 Before calling einfin, you should either call eclear
1333 or set up the sign bit by hand. */
1337 register unsigned EMUSHORT *x;
1342 for (i = 0; i < NE - 1; i++)
1346 for (i = 0; i < NE - 1; i++)
1370 /* Output an e-type NaN.
1371 This generates Intel's quiet NaN pattern for extended real.
1372 The exponent is 7fff, the leading mantissa word is c000. */
1376 register unsigned EMUSHORT *x;
1380 for (i = 0; i < NE - 2; i++)
1387 /* Move in external format number,
1388 * converting it to internal format.
1392 unsigned EMUSHORT *a, *b;
1394 register unsigned EMUSHORT *p, *q;
1398 p = a + (NE - 1); /* point to last word of external number */
1399 /* get the sign bit */
1404 /* get the exponent */
1406 *q++ &= 0x7fff; /* delete the sign bit */
1408 if ((*(q - 1) & 0x7fff) == 0x7fff)
1414 for (i = 3; i < NI; i++)
1419 for (i = 2; i < NI; i++)
1424 /* clear high guard word */
1426 /* move in the significand */
1427 for (i = 0; i < NE - 1; i++)
1429 /* clear low guard word */
1434 /* Move internal format number out,
1435 * converting it to external format.
1439 unsigned EMUSHORT *a, *b;
1441 register unsigned EMUSHORT *p, *q;
1442 unsigned EMUSHORT i;
1445 q = b + (NE - 1); /* point to output exponent */
1446 /* combine sign and exponent */
1449 *q-- = *p++ | 0x8000;
1453 if (*(p - 1) == 0x7fff)
1466 /* skip over guard word */
1468 /* move the significand */
1469 for (i = 0; i < NE - 1; i++)
1476 /* Clear out internal format number.
1481 register unsigned EMUSHORT *xi;
1485 for (i = 0; i < NI; i++)
1490 /* same, but don't touch the sign. */
1494 register unsigned EMUSHORT *xi;
1499 for (i = 0; i < NI - 1; i++)
1505 /* Move internal format number from a to b.
1509 register unsigned EMUSHORT *a, *b;
1513 for (i = 0; i < NI - 1; i++)
1515 /* clear low guard word */
1519 /* Generate internal format NaN.
1520 The explicit pattern for this is maximum exponent and
1521 top two significand bits set. */
1525 unsigned EMUSHORT x[];
1533 /* Return nonzero if internal format number is a NaN. */
1537 unsigned EMUSHORT x[];
1541 if ((x[E] & 0x7fff) == 0x7fff)
1543 for (i = M + 1; i < NI; i++)
1552 /* Fill internal format number with infinity pattern.
1553 This has maximum exponent and significand all zeros. */
1557 unsigned EMUSHORT x[];
1564 /* Return nonzero if internal format number is infinite. */
1568 unsigned EMUSHORT x[];
1575 if ((x[E] & 0x7fff) == 0x7fff)
1582 ; Compare significands of numbers in internal format.
1583 ; Guard words are included in the comparison.
1585 ; unsigned EMUSHORT a[NI], b[NI];
1588 ; for the significands:
1589 ; returns +1 if a > b
1595 register unsigned EMUSHORT *a, *b;
1599 a += M; /* skip up to significand area */
1601 for (i = M; i < NI; i++)
1609 if (*(--a) > *(--b))
1617 ; Shift significand down by 1 bit
1622 register unsigned EMUSHORT *x;
1624 register unsigned EMUSHORT bits;
1627 x += M; /* point to significand area */
1630 for (i = M; i < NI; i++)
1645 ; Shift significand up by 1 bit
1650 register unsigned EMUSHORT *x;
1652 register unsigned EMUSHORT bits;
1658 for (i = M; i < NI; i++)
1673 ; Shift significand down by 8 bits
1678 register unsigned EMUSHORT *x;
1680 register unsigned EMUSHORT newbyt, oldbyt;
1685 for (i = M; i < NI; i++)
1696 ; Shift significand up by 8 bits
1701 register unsigned EMUSHORT *x;
1704 register unsigned EMUSHORT newbyt, oldbyt;
1709 for (i = M; i < NI; i++)
1720 ; Shift significand up by 16 bits
1725 register unsigned EMUSHORT *x;
1728 register unsigned EMUSHORT *p;
1733 for (i = M; i < NI - 1; i++)
1740 ; Shift significand down by 16 bits
1745 register unsigned EMUSHORT *x;
1748 register unsigned EMUSHORT *p;
1753 for (i = M; i < NI - 1; i++)
1766 unsigned EMUSHORT *x, *y;
1768 register unsigned EMULONG a;
1775 for (i = M; i < NI; i++)
1777 a = (unsigned EMULONG) (*x) + (unsigned EMULONG) (*y) + carry;
1782 *y = (unsigned EMUSHORT) a;
1789 ; Subtract significands
1795 unsigned EMUSHORT *x, *y;
1804 for (i = M; i < NI; i++)
1806 a = (unsigned EMULONG) (*y) - (unsigned EMULONG) (*x) - carry;
1811 *y = (unsigned EMUSHORT) a;
1818 /* Divide significands */
1820 static unsigned EMUSHORT equot[NI];
1824 unsigned EMUSHORT den[], num[];
1827 register unsigned EMUSHORT *p, *q;
1828 unsigned EMUSHORT j;
1834 for (i = M; i < NI; i++)
1839 /* Use faster compare and subtraction if denominator
1840 * has only 15 bits of significance.
1845 for (i = M + 3; i < NI; i++)
1850 if ((den[M + 1] & 1) != 0)
1858 for (i = 0; i < NBITS + 2; i++)
1876 /* The number of quotient bits to calculate is
1877 * NBITS + 1 scaling guard bit + 1 roundoff bit.
1882 for (i = 0; i < NBITS + 2; i++)
1884 if (ecmpm (den, num) <= 0)
1887 j = 1; /* quotient bit = 1 */
1901 /* test for nonzero remainder after roundoff bit */
1904 for (i = M; i < NI; i++)
1912 for (i = 0; i < NI; i++)
1918 /* Multiply significands */
1921 unsigned EMUSHORT a[], b[];
1923 unsigned EMUSHORT *p, *q;
1928 for (i = M; i < NI; i++)
1933 while (*p == 0) /* significand is not supposed to be all zero */
1938 if ((*p & 0xff) == 0)
1946 for (i = 0; i < k; i++)
1950 /* remember if there were any nonzero bits shifted out */
1957 for (i = 0; i < NI; i++)
1960 /* return flag for lost nonzero bits */
1967 * Normalize and round off.
1969 * The internal format number to be rounded is "s".
1970 * Input "lost" indicates whether or not the number is exact.
1971 * This is the so-called sticky bit.
1973 * Input "subflg" indicates whether the number was obtained
1974 * by a subtraction operation. In that case if lost is nonzero
1975 * then the number is slightly smaller than indicated.
1977 * Input "exp" is the biased exponent, which may be negative.
1978 * the exponent field of "s" is ignored but is replaced by
1979 * "exp" as adjusted by normalization and rounding.
1981 * Input "rcntrl" is the rounding control.
1984 static int rlast = -1;
1986 static unsigned EMUSHORT rmsk = 0;
1987 static unsigned EMUSHORT rmbit = 0;
1988 static unsigned EMUSHORT rebit = 0;
1990 static unsigned EMUSHORT rbit[NI];
1993 emdnorm (s, lost, subflg, exp, rcntrl)
1994 unsigned EMUSHORT s[];
2001 unsigned EMUSHORT r;
2006 /* a blank significand could mean either zero or infinity. */
2019 if ((j > NBITS) && (exp < 32767))
2027 if (exp > (EMULONG) (-NBITS - 1))
2040 /* Round off, unless told not to by rcntrl. */
2043 /* Set up rounding parameters if the control register changed. */
2044 if (rndprc != rlast)
2051 rw = NI - 1; /* low guard word */
2066 /* For DEC arithmetic */
2115 /* These tests assume NI = 8 */
2135 if ((r & rmbit) != 0)
2140 { /* round to even */
2141 if ((s[re] & rebit) == 0)
2153 if ((rndprc < 64) && (exp <= 0))
2158 { /* overflow on roundoff */
2171 for (i = 2; i < NI - 1; i++)
2174 warning ("floating point overflow");
2178 for (i = M + 1; i < NI - 1; i++)
2196 s[1] = (unsigned EMUSHORT) exp;
2202 ; Subtract external format numbers.
2204 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2205 ; esub (a, b, c); c = b - a
2208 static int subflg = 0;
2212 unsigned EMUSHORT *a, *b, *c;
2226 /* Infinity minus infinity is a NaN.
2227 Test for subtracting infinities of the same sign. */
2228 if (eisinf (a) && eisinf (b)
2229 && ((eisneg (a) ^ eisneg (b)) == 0))
2231 mtherr ("esub", INVALID);
2244 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2245 ; eadd (a, b, c); c = b + a
2249 unsigned EMUSHORT *a, *b, *c;
2253 /* NaN plus anything is a NaN. */
2264 /* Infinity minus infinity is a NaN.
2265 Test for adding infinities of opposite signs. */
2266 if (eisinf (a) && eisinf (b)
2267 && ((eisneg (a) ^ eisneg (b)) != 0))
2269 mtherr ("esub", INVALID);
2280 unsigned EMUSHORT *a, *b, *c;
2282 unsigned EMUSHORT ai[NI], bi[NI], ci[NI];
2284 EMULONG lt, lta, ltb;
2305 /* compare exponents */
2310 { /* put the larger number in bi */
2320 if (lt < (EMULONG) (-NBITS - 1))
2321 goto done; /* answer same as larger addend */
2323 lost = eshift (ai, k); /* shift the smaller number down */
2327 /* exponents were the same, so must compare significands */
2330 { /* the numbers are identical in magnitude */
2331 /* if different signs, result is zero */
2337 /* if same sign, result is double */
2338 /* double denomalized tiny number */
2339 if ((bi[E] == 0) && ((bi[3] & 0x8000) == 0))
2344 /* add 1 to exponent unless both are zero! */
2345 for (j = 1; j < NI - 1; j++)
2349 /* This could overflow, but let emovo take care of that. */
2354 bi[E] = (unsigned EMUSHORT) ltb;
2358 { /* put the larger number in bi */
2374 emdnorm (bi, lost, subflg, ltb, 64);
2385 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2386 ; ediv (a, b, c); c = b / a
2390 unsigned EMUSHORT *a, *b, *c;
2392 unsigned EMUSHORT ai[NI], bi[NI];
2394 EMULONG lt, lta, ltb;
2397 /* Return any NaN input. */
2408 /* Zero over zero, or infinity over infinity, is a NaN. */
2409 if (((ecmp (a, ezero) == 0) && (ecmp (b, ezero) == 0))
2410 || (eisinf (a) && eisinf (b)))
2412 mtherr ("ediv", INVALID);
2417 /* Infinity over anything else is infinity. */
2421 if (eisneg (a) ^ eisneg (b))
2422 *(c + (NE - 1)) = 0x8000;
2424 *(c + (NE - 1)) = 0;
2428 /* Anything else over infinity is zero. */
2440 { /* See if numerator is zero. */
2441 for (i = 1; i < NI - 1; i++)
2445 ltb -= enormlz (bi);
2455 { /* possible divide by zero */
2456 for (i = 1; i < NI - 1; i++)
2460 lta -= enormlz (ai);
2465 *(c + (NE - 1)) = 0;
2467 *(c + (NE - 1)) = 0x8000;
2468 /* Divide by zero is not an invalid operation.
2469 It is a divide-by-zero operation! */
2471 mtherr ("ediv", SING);
2477 /* calculate exponent */
2478 lt = ltb - lta + EXONE;
2479 emdnorm (bi, i, 0, lt, 64);
2493 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2494 ; emul (a, b, c); c = b * a
2498 unsigned EMUSHORT *a, *b, *c;
2500 unsigned EMUSHORT ai[NI], bi[NI];
2502 EMULONG lt, lta, ltb;
2505 /* NaN times anything is the same NaN. */
2516 /* Zero times infinity is a NaN. */
2517 if ((eisinf (a) && (ecmp (b, ezero) == 0))
2518 || (eisinf (b) && (ecmp (a, ezero) == 0)))
2520 mtherr ("emul", INVALID);
2525 /* Infinity times anything else is infinity. */
2527 if (eisinf (a) || eisinf (b))
2529 if (eisneg (a) ^ eisneg (b))
2530 *(c + (NE - 1)) = 0x8000;
2532 *(c + (NE - 1)) = 0;
2543 for (i = 1; i < NI - 1; i++)
2547 lta -= enormlz (ai);
2558 for (i = 1; i < NI - 1; i++)
2562 ltb -= enormlz (bi);
2571 /* Multiply significands */
2573 /* calculate exponent */
2574 lt = lta + ltb - (EXONE - 1);
2575 emdnorm (bi, j, 0, lt, 64);
2576 /* calculate sign of product */
2588 ; Convert IEEE double precision to e type
2590 ; unsigned EMUSHORT x[N+2];
2595 unsigned EMUSHORT *pe, *y;
2599 dectoe (pe, y); /* see etodec.c */
2603 register unsigned EMUSHORT r;
2604 register unsigned EMUSHORT *e, *p;
2605 unsigned EMUSHORT yy[NI];
2609 denorm = 0; /* flag if denormalized number */
2618 yy[M] = (r & 0x0f) | 0x10;
2619 r &= ~0x800f; /* strip sign and 4 significand bits */
2625 if (((pe[3] & 0xf) != 0) || (pe[2] != 0)
2626 || (pe[1] != 0) || (pe[0] != 0))
2632 if (((pe[0] & 0xf) != 0) || (pe[1] != 0)
2633 || (pe[2] != 0) || (pe[3] != 0))
2646 #endif /* INFINITY */
2648 /* If zero exponent, then the significand is denormalized.
2649 * So, take back the understood high significand bit. */
2671 { /* if zero exponent, then normalize the significand */
2672 if ((k = enormlz (yy)) > NBITS)
2675 yy[E] -= (unsigned EMUSHORT) (k - 1);
2678 #endif /* not DEC */
2683 unsigned EMUSHORT *pe, *y;
2685 unsigned EMUSHORT yy[NI];
2686 unsigned EMUSHORT *e, *p, *q;
2691 for (i = 0; i < NE - 5; i++)
2694 for (i = 0; i < 5; i++)
2698 for (i = 0; i < 5; i++)
2702 p = &yy[0] + (NE - 1);
2705 for (i = 0; i < 4; i++)
2715 for (i = 0; i < 4; i++)
2724 for (i = 1; i <= 4; i++)
2740 #endif /* INFINITY */
2741 for (i = 0; i < NE; i++)
2747 ; Convert IEEE single precision to e type
2749 ; unsigned EMUSHORT x[N+2];
2754 unsigned EMUSHORT *pe, *y;
2756 register unsigned EMUSHORT r;
2757 register unsigned EMUSHORT *e, *p;
2758 unsigned EMUSHORT yy[NI];
2762 denorm = 0; /* flag if denormalized number */
2774 yy[M] = (r & 0x7f) | 0200;
2775 r &= ~0x807f; /* strip sign and 7 significand bits */
2781 if (((pe[0] & 0x7f) != 0) || (pe[1] != 0))
2787 if (((pe[1] & 0x7f) != 0) || (pe[0] != 0))
2800 #endif /* INFINITY */
2802 /* If zero exponent, then the significand is denormalized.
2803 * So, take back the understood high significand bit. */
2824 { /* if zero exponent, then normalize the significand */
2825 if ((k = enormlz (yy)) > NBITS)
2828 yy[E] -= (unsigned EMUSHORT) (k - 1);
2836 unsigned EMUSHORT *x, *e;
2838 unsigned EMUSHORT xi[NI];
2845 make_nan (e, XFmode);
2850 /* adjust exponent for offset */
2851 exp = (EMULONG) xi[E];
2856 /* round off to nearest or even */
2859 emdnorm (xi, 0, 0, exp, 64);
2865 /* move out internal format to ieee long double */
2868 unsigned EMUSHORT *a, *b;
2870 register unsigned EMUSHORT *p, *q;
2871 unsigned EMUSHORT i;
2876 make_nan (b, XFmode);
2884 q = b + 4; /* point to output exponent */
2885 #if LONG_DOUBLE_TYPE_SIZE == 96
2886 /* Clear the last two bytes of 12-byte Intel format */
2891 /* combine sign and exponent */
2895 *q++ = *p++ | 0x8000;
2901 *q-- = *p++ | 0x8000;
2905 /* skip over guard word */
2907 /* move the significand */
2909 for (i = 0; i < 4; i++)
2912 for (i = 0; i < 4; i++)
2919 ; e type to IEEE double precision
2921 ; unsigned EMUSHORT x[NE];
2929 unsigned EMUSHORT *x, *e;
2931 etodec (x, e); /* see etodec.c */
2936 unsigned EMUSHORT *x, *y;
2945 unsigned EMUSHORT *x, *e;
2947 unsigned EMUSHORT xi[NI];
2954 make_nan (e, DFmode);
2959 /* adjust exponent for offsets */
2960 exp = (EMULONG) xi[E] - (EXONE - 0x3ff);
2965 /* round off to nearest or even */
2968 emdnorm (xi, 0, 0, exp, 64);
2977 unsigned EMUSHORT *x, *y;
2979 unsigned EMUSHORT i;
2980 unsigned EMUSHORT *p;
2985 make_nan (y, DFmode);
2993 *y = 0; /* output high order */
2995 *y = 0x8000; /* output sign bit */
2998 if (i >= (unsigned int) 2047)
2999 { /* Saturate at largest number less than infinity. */
3014 *y |= (unsigned EMUSHORT) 0x7fef;
3038 i |= *p++ & (unsigned EMUSHORT) 0x0f; /* *p = xi[M] */
3039 *y |= (unsigned EMUSHORT) i; /* high order output already has sign bit set */
3053 #endif /* not DEC */
3058 ; e type to IEEE single precision
3060 ; unsigned EMUSHORT x[N+2];
3065 unsigned EMUSHORT *x, *e;
3068 unsigned EMUSHORT xi[NI];
3074 make_nan (e, SFmode);
3079 /* adjust exponent for offsets */
3080 exp = (EMULONG) xi[E] - (EXONE - 0177);
3085 /* round off to nearest or even */
3088 emdnorm (xi, 0, 0, exp, 64);
3096 unsigned EMUSHORT *x, *y;
3098 unsigned EMUSHORT i;
3099 unsigned EMUSHORT *p;
3104 make_nan (y, SFmode);
3115 *y = 0; /* output high order */
3117 *y = 0x8000; /* output sign bit */
3120 /* Handle overflow cases. */
3124 *y |= (unsigned EMUSHORT) 0x7f80;
3135 #else /* no INFINITY */
3136 *y |= (unsigned EMUSHORT) 0x7f7f;
3150 #endif /* no INFINITY */
3162 i |= *p++ & (unsigned EMUSHORT) 0x7f; /* *p = xi[M] */
3163 *y |= i; /* high order output already has sign bit set */
3177 /* Compare two e type numbers.
3179 * unsigned EMUSHORT a[NE], b[NE];
3182 * returns +1 if a > b
3185 * -2 if either a or b is a NaN.
3189 unsigned EMUSHORT *a, *b;
3191 unsigned EMUSHORT ai[NI], bi[NI];
3192 register unsigned EMUSHORT *p, *q;
3197 if (eisnan (a) || eisnan (b))
3206 { /* the signs are different */
3208 for (i = 1; i < NI - 1; i++)
3222 /* both are the same sign */
3237 return (0); /* equality */
3243 if (*(--p) > *(--q))
3244 return (msign); /* p is bigger */
3246 return (-msign); /* p is littler */
3252 /* Find nearest integer to x = floor (x + 0.5)
3254 * unsigned EMUSHORT x[NE], y[NE]
3259 unsigned EMUSHORT *x, *y;
3269 ; convert long integer to e type
3272 ; unsigned EMUSHORT x[NE];
3274 ; note &l is the memory address of l
3278 long *lp; /* lp is the memory address of a long integer */
3279 unsigned EMUSHORT *y; /* y is the address of a short */
3281 unsigned EMUSHORT yi[NI];
3288 /* make it positive */
3289 ll = (unsigned long) (-(*lp));
3290 yi[0] = 0xffff; /* put correct sign in the e type number */
3294 ll = (unsigned long) (*lp);
3296 /* move the long integer to yi significand area */
3297 yi[M] = (unsigned EMUSHORT) (ll >> 16);
3298 yi[M + 1] = (unsigned EMUSHORT) ll;
3300 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
3301 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
3302 ecleaz (yi); /* it was zero */
3304 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
3305 emovo (yi, y); /* output the answer */
3309 ; convert unsigned long integer to e type
3312 ; unsigned EMUSHORT x[NE];
3314 ; note &l is the memory address of l
3318 unsigned long *lp; /* lp is the memory address of a long integer */
3319 unsigned EMUSHORT *y; /* y is the address of a short */
3321 unsigned EMUSHORT yi[NI];
3328 /* move the long integer to ayi significand area */
3329 yi[M] = (unsigned EMUSHORT) (ll >> 16);
3330 yi[M + 1] = (unsigned EMUSHORT) ll;
3332 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
3333 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
3334 ecleaz (yi); /* it was zero */
3336 yi[E] -= (unsigned EMUSHORT) k; /* subtract shift count from exponent */
3337 emovo (yi, y); /* output the answer */
3342 ; Find long integer and fractional parts
3345 ; unsigned EMUSHORT x[NE], frac[NE];
3346 ; xifrac (x, &i, frac);
3348 The integer output has the sign of the input. The fraction is
3349 the positive fractional part of abs (x).
3353 unsigned EMUSHORT *x;
3355 unsigned EMUSHORT *frac;
3357 unsigned EMUSHORT xi[NI];
3361 k = (int) xi[E] - (EXONE - 1);
3364 /* if exponent <= 0, integer = 0 and real output is fraction */
3369 if (k > (HOST_BITS_PER_LONG - 1))
3372 ; long integer overflow: output large integer
3373 ; and correct fraction
3376 *i = ((unsigned long) 1) << (HOST_BITS_PER_LONG - 1);
3378 *i = (((unsigned long) 1) << (HOST_BITS_PER_LONG - 1)) - 1;
3381 warning ("overflow on truncation to integer");
3388 ; shift more than 16 bits: shift up k-16, output the integer,
3389 ; then complete the shift to get the fraction.
3394 *i = (long) (((unsigned long) xi[M] << 16) | xi[M + 1]);
3399 /* shift not more than 16 bits */
3401 *i = (long) xi[M] & 0xffff;
3412 if ((k = enormlz (xi)) > NBITS)
3415 xi[E] -= (unsigned EMUSHORT) k;
3422 ; Find unsigned long integer and fractional parts
3425 ; unsigned EMUSHORT x[NE], frac[NE];
3426 ; xifrac (x, &i, frac);
3428 A negative e type input yields integer output = 0
3429 but correct fraction.
3432 euifrac (x, i, frac)
3433 unsigned EMUSHORT *x;
3435 unsigned EMUSHORT *frac;
3437 unsigned EMUSHORT xi[NI];
3441 k = (int) xi[E] - (EXONE - 1);
3444 /* if exponent <= 0, integer = 0 and argument is fraction */
3452 ; long integer overflow: output large integer
3453 ; and correct fraction
3458 warning ("overflow on truncation to unsigned integer");
3465 ; shift more than 16 bits: shift up k-16, output the integer,
3466 ; then complete the shift to get the fraction.
3471 *i = (long) (((unsigned long) xi[M] << 16) | xi[M + 1]);
3476 /* shift not more than 16 bits */
3478 *i = (long) xi[M] & 0xffff;
3488 if ((k = enormlz (xi)) > NBITS)
3491 xi[E] -= (unsigned EMUSHORT) k;
3501 ; Shifts significand area up or down by the number of bits
3502 ; given by the variable sc.
3506 unsigned EMUSHORT *x;
3509 unsigned EMUSHORT lost;
3510 unsigned EMUSHORT *p;
3523 lost |= *p; /* remember lost bits */
3564 return ((int) lost);
3572 ; Shift normalizes the significand area pointed to by argument
3573 ; shift count (up = positive) is returned.
3577 unsigned EMUSHORT x[];
3579 register unsigned EMUSHORT *p;
3588 return (0); /* already normalized */
3593 /* With guard word, there are NBITS+16 bits available.
3594 * return true if all are zero.
3599 /* see if high byte is zero */
3600 while ((*p & 0xff00) == 0)
3605 /* now shift 1 bit at a time */
3606 while ((*p & 0x8000) == 0)
3612 mtherr ("enormlz", UNDERFLOW);
3618 /* Normalize by shifting down out of the high guard word
3619 of the significand */
3634 mtherr ("enormlz", OVERFLOW);
3644 /* Convert e type number to decimal format ASCII string.
3645 * The constants are for 64 bit precision.
3651 static unsigned EMUSHORT etens[NTEN + 1][NE] =
3653 {0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
3654 {0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
3655 {0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
3656 {0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
3657 {0xddbc, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
3658 {0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
3659 {0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
3660 {0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
3661 {0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
3662 {0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
3663 {0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
3664 {0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
3665 {0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
3668 static unsigned EMUSHORT emtens[NTEN + 1][NE] =
3670 {0x2de4, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
3671 {0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
3672 {0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
3673 {0x7133, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
3674 {0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
3675 {0xac7d, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
3676 {0x3f24, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
3677 {0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
3678 {0x4c2f, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
3679 {0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
3680 {0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
3681 {0x3d71, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
3682 {0xcccd, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
3686 e24toasc (x, string, ndigs)
3687 unsigned EMUSHORT x[];
3691 unsigned EMUSHORT w[NI];
3694 etoasc (w, string, ndigs);
3699 e53toasc (x, string, ndigs)
3700 unsigned EMUSHORT x[];
3704 unsigned EMUSHORT w[NI];
3707 etoasc (w, string, ndigs);
3712 e64toasc (x, string, ndigs)
3713 unsigned EMUSHORT x[];
3717 unsigned EMUSHORT w[NI];
3720 etoasc (w, string, ndigs);
3724 static char wstring[80]; /* working storage for ASCII output */
3727 etoasc (x, string, ndigs)
3728 unsigned EMUSHORT x[];
3733 unsigned EMUSHORT y[NI], t[NI], u[NI], w[NI];
3734 unsigned EMUSHORT *p, *r, *ten;
3735 unsigned EMUSHORT sign;
3736 int i, j, k, expon, rndsav;
3738 unsigned EMUSHORT m;
3749 sprintf (wstring, " NaN ");
3753 rndprc = NBITS; /* set to full precision */
3754 emov (x, y); /* retain external format */
3755 if (y[NE - 1] & 0x8000)
3758 y[NE - 1] &= 0x7fff;
3765 ten = &etens[NTEN][0];
3767 /* Test for zero exponent */
3770 for (k = 0; k < NE - 1; k++)
3773 goto tnzro; /* denormalized number */
3775 goto isone; /* legal all zeros */
3779 /* Test for infinity. */
3780 if (y[NE - 1] == 0x7fff)
3783 sprintf (wstring, " -Infinity ");
3785 sprintf (wstring, " Infinity ");
3789 /* Test for exponent nonzero but significand denormalized.
3790 * This is an error condition.
3792 if ((y[NE - 1] != 0) && ((y[NE - 2] & 0x8000) == 0))
3794 mtherr ("etoasc", DOMAIN);
3795 sprintf (wstring, "NaN");
3799 /* Compare to 1.0 */
3808 { /* Number is greater than 1 */
3809 /* Convert significand to an integer and strip trailing decimal zeros. */
3811 u[NE - 1] = EXONE + NBITS - 1;
3813 p = &etens[NTEN - 4][0];
3819 for (j = 0; j < NE - 1; j++)
3832 /* Rescale from integer significand */
3833 u[NE - 1] += y[NE - 1] - (unsigned int) (EXONE + NBITS - 1);
3835 /* Find power of 10 */
3839 /* An unordered compare result shouldn't happen here. */
3840 while (ecmp (ten, u) <= 0)
3842 if (ecmp (p, u) <= 0)
3855 { /* Number is less than 1.0 */
3856 /* Pad significand with trailing decimal zeros. */
3859 while ((y[NE - 2] & 0x8000) == 0)
3868 for (i = 0; i < NDEC + 1; i++)
3870 if ((w[NI - 1] & 0x7) != 0)
3872 /* multiply by 10 */
3885 if (eone[NE - 1] <= u[1])
3897 while (ecmp (eone, w) > 0)
3899 if (ecmp (p, w) >= 0)
3914 /* Find the first (leading) digit. */
3920 digit = equot[NI - 1];
3921 while ((digit == 0) && (ecmp (y, ezero) != 0))
3929 digit = equot[NI - 1];
3937 /* Examine number of digits requested by caller. */
3955 *s++ = (char )digit + '0';
3958 /* Generate digits after the decimal point. */
3959 for (k = 0; k <= ndigs; k++)
3961 /* multiply current number by 10, without normalizing */
3968 *s++ = (char) equot[NI - 1] + '0';
3970 digit = equot[NI - 1];
3973 /* round off the ASCII string */
3976 /* Test for critical rounding case in ASCII output. */
3980 if (ecmp (t, ezero) != 0)
3981 goto roun; /* round to nearest */
3982 if ((*(s - 1) & 1) == 0)
3983 goto doexp; /* round to even */
3985 /* Round up and propagate carry-outs */
3989 /* Carry out to most significant digit? */
3996 /* Most significant digit carries to 10? */
4004 /* Round up and carry out from less significant digits */
4016 sprintf (ss, "e+%d", expon);
4018 sprintf (ss, "e%d", expon);
4020 sprintf (ss, "e%d", expon);
4023 /* copy out the working string */
4026 while (*ss == ' ') /* strip possible leading space */
4028 while ((*s++ = *ss++) != '\0')
4037 ; ASCTOQ.MAC LATEST REV: 11 JAN 84
4040 ; Convert ASCII string to quadruple precision floating point
4042 ; Numeric input is free field decimal number
4043 ; with max of 15 digits with or without
4044 ; decimal point entered as ASCII from teletype.
4045 ; Entering E after the number followed by a second
4046 ; number causes the second number to be interpreted
4047 ; as a power of 10 to be multiplied by the first number
4048 ; (i.e., "scientific" notation).
4051 ; asctoq (string, q);
4054 /* ASCII to single */
4058 unsigned EMUSHORT *y;
4064 /* ASCII to double */
4068 unsigned EMUSHORT *y;
4078 /* ASCII to long double */
4082 unsigned EMUSHORT *y;
4087 /* ASCII to super double */
4091 unsigned EMUSHORT *y;
4093 asctoeg (s, y, NBITS);
4096 /* Space to make a copy of the input string: */
4097 static char lstr[82];
4100 asctoeg (ss, y, oprec)
4102 unsigned EMUSHORT *y;
4105 unsigned EMUSHORT yy[NI], xt[NI], tt[NI];
4106 int esign, decflg, sgnflg, nexp, exp, prec, lost;
4107 int k, trail, c, rndsav;
4109 unsigned EMUSHORT nsign, *p;
4112 /* Copy the input string. */
4114 while (*s == ' ') /* skip leading spaces */
4117 for (k = 0; k < 79; k++)
4119 if ((*sp++ = *s++) == '\0')
4126 rndprc = NBITS; /* Set to full precision */
4139 if ((k >= 0) && (k <= 9))
4141 /* Ignore leading zeros */
4142 if ((prec == 0) && (decflg == 0) && (k == 0))
4144 /* Identify and strip trailing zeros after the decimal point. */
4145 if ((trail == 0) && (decflg != 0))
4148 while ((*sp >= '0') && (*sp <= '9'))
4150 /* Check for syntax error */
4152 if ((c != 'e') && (c != 'E') && (c != '\0')
4153 && (c != '\n') && (c != '\r') && (c != ' ')
4163 /* If enough digits were given to more than fill up the yy register,
4164 * continuing until overflow into the high guard word yy[2]
4165 * guarantees that there will be a roundoff bit at the top
4166 * of the low guard word after normalization.
4171 nexp += 1; /* count digits after decimal point */
4172 eshup1 (yy); /* multiply current number by 10 */
4178 xt[NI - 2] = (unsigned EMUSHORT) k;
4196 case '.': /* decimal point */
4226 mtherr ("asctoe", DOMAIN);
4235 /* Exponent interpretation */
4241 /* check for + or - */
4249 while ((*s >= '0') && (*s <= '9'))
4267 yy[E] = 0x7fff; /* infinity */
4279 /* Pad trailing zeros to minimize power of 10, per IEEE spec. */
4280 while ((nexp > 0) && (yy[2] == 0))
4292 if ((k = enormlz (yy)) > NBITS)
4297 lexp = (EXONE - 1 + NBITS) - k;
4298 emdnorm (yy, lost, 0, lexp, 64);
4299 /* convert to external format */
4302 /* Multiply by 10**nexp. If precision is 64 bits,
4303 * the maximum relative error incurred in forming 10**n
4304 * for 0 <= n <= 324 is 8.2e-20, at 10**180.
4305 * For 0 <= n <= 999, the peak relative error is 1.4e-19 at 10**947.
4306 * For 0 >= n >= -999, it is -1.55e-19 at 10**-435.
4320 { /* Punt. Can't handle this without 2 divides. */
4321 emovi (etens[0], tt);
4328 p = &etens[NTEN][0];
4338 while (exp <= MAXP);
4356 /* Round and convert directly to the destination type */
4358 lexp -= EXONE - 0x3ff;
4359 else if (oprec == 24)
4360 lexp -= EXONE - 0177;
4362 else if (oprec == 56)
4363 lexp -= EXONE - 0201;
4366 emdnorm (yy, k, 0, lexp, 64);
4376 todec (yy, y); /* see etodec.c */
4396 /* y = largest integer not greater than x
4397 * (truncated toward minus infinity)
4399 * unsigned EMUSHORT x[NE], y[NE]
4403 static unsigned EMUSHORT bmask[] =
4426 unsigned EMUSHORT x[], y[];
4428 register unsigned EMUSHORT *p;
4430 unsigned EMUSHORT f[NE];
4432 emov (x, f); /* leave in external format */
4433 expon = (int) f[NE - 1];
4434 e = (expon & 0x7fff) - (EXONE - 1);
4440 /* number of bits to clear out */
4452 /* clear the remaining bits */
4454 /* truncate negatives toward minus infinity */
4457 if ((unsigned EMUSHORT) expon & (unsigned EMUSHORT) 0x8000)
4459 for (i = 0; i < NE - 1; i++)
4471 /* unsigned EMUSHORT x[], s[];
4474 * efrexp (x, exp, s);
4476 * Returns s and exp such that s * 2**exp = x and .5 <= s < 1.
4477 * For example, 1.1 = 0.55 * 2**1
4478 * Handles denormalized numbers properly using long integer exp.
4482 unsigned EMUSHORT x[];
4484 unsigned EMUSHORT s[];
4486 unsigned EMUSHORT xi[NI];
4490 li = (EMULONG) ((EMUSHORT) xi[1]);
4498 *exp = (int) (li - 0x3ffe);
4503 /* unsigned EMUSHORT x[], y[];
4506 * eldexp (x, pwr2, y);
4508 * Returns y = x * 2**pwr2.
4512 unsigned EMUSHORT x[];
4514 unsigned EMUSHORT y[];
4516 unsigned EMUSHORT xi[NI];
4524 emdnorm (xi, i, i, li, 64);
4529 /* c = remainder after dividing b by a
4530 * Least significant integer quotient bits left in equot[].
4534 unsigned EMUSHORT a[], b[], c[];
4536 unsigned EMUSHORT den[NI], num[NI];
4540 || (ecmp (a, ezero) == 0)
4548 if (ecmp (a, ezero) == 0)
4550 mtherr ("eremain", SING);
4556 eiremain (den, num);
4557 /* Sign of remainder = sign of quotient */
4567 unsigned EMUSHORT den[], num[];
4570 unsigned EMUSHORT j;
4573 ld -= enormlz (den);
4575 ln -= enormlz (num);
4579 if (ecmpm (den, num) <= 0)
4593 emdnorm (num, 0, 0, ln, 0);
4598 * Library common error handling routine
4608 * mtherr (fctnam, code);
4614 * This routine may be called to report one of the following
4615 * error conditions (in the include file mconf.h).
4617 * Mnemonic Value Significance
4619 * DOMAIN 1 argument domain error
4620 * SING 2 function singularity
4621 * OVERFLOW 3 overflow range error
4622 * UNDERFLOW 4 underflow range error
4623 * TLOSS 5 total loss of precision
4624 * PLOSS 6 partial loss of precision
4625 * INVALID 7 NaN - producing operation
4626 * EDOM 33 Unix domain error code
4627 * ERANGE 34 Unix range error code
4629 * The default version of the file prints the function name,
4630 * passed to it by the pointer fctnam, followed by the
4631 * error condition. The display is directed to the standard
4632 * output device. The routine then returns to the calling
4633 * program. Users may wish to modify the program to abort by
4634 * calling exit under severe error conditions such as domain
4637 * Since all error conditions pass control to this function,
4638 * the display may be easily changed, eliminated, or directed
4639 * to an error logging device.
4648 Cephes Math Library Release 2.0: April, 1987
4649 Copyright 1984, 1987 by Stephen L. Moshier
4650 Direct inquiries to 30 Frost Street, Cambridge, MA 02140
4653 /* include "mconf.h" */
4655 /* Notice: the order of appearance of the following
4656 * messages is bound to the error codes defined
4660 static char *ermsg[NMSGS] =
4662 "unknown", /* error code 0 */
4663 "domain", /* error code 1 */
4664 "singularity", /* et seq. */
4667 "total loss of precision",
4668 "partial loss of precision",
4682 /* Display string passed by calling program,
4683 * which is supposed to be the name of the
4684 * function in which the error occurred.
4687 /* Display error message defined
4688 * by the code argument.
4690 if ((code <= 0) || (code >= NMSGS))
4692 sprintf (errstr, " %s %s error", name, ermsg[code]);
4695 /* Set global error message word */
4698 /* Return to calling
4703 /* Here is etodec.c .
4708 ; convert DEC double precision to e type
4715 unsigned EMUSHORT *d;
4716 unsigned EMUSHORT *e;
4718 unsigned EMUSHORT y[NI];
4719 register unsigned EMUSHORT r, *p;
4721 ecleaz (y); /* start with a zero */
4722 p = y; /* point to our number */
4723 r = *d; /* get DEC exponent word */
4724 if (*d & (unsigned int) 0x8000)
4725 *p = 0xffff; /* fill in our sign */
4726 ++p; /* bump pointer to our exponent word */
4727 r &= 0x7fff; /* strip the sign bit */
4728 if (r == 0) /* answer = 0 if high order DEC word = 0 */
4732 r >>= 7; /* shift exponent word down 7 bits */
4733 r += EXONE - 0201; /* subtract DEC exponent offset */
4734 /* add our e type exponent offset */
4735 *p++ = r; /* to form our exponent */
4737 r = *d++; /* now do the high order mantissa */
4738 r &= 0177; /* strip off the DEC exponent and sign bits */
4739 r |= 0200; /* the DEC understood high order mantissa bit */
4740 *p++ = r; /* put result in our high guard word */
4742 *p++ = *d++; /* fill in the rest of our mantissa */
4746 eshdn8 (y); /* shift our mantissa down 8 bits */
4754 ; convert e type to DEC double precision
4760 static unsigned EMUSHORT decbit[NI] = {0, 0, 0, 0, 0, 0, 0200, 0};
4764 unsigned EMUSHORT *x, *d;
4766 unsigned EMUSHORT xi[NI];
4767 register unsigned EMUSHORT r;
4775 if (r < (EXONE - 128))
4778 if ((i & 0200) != 0)
4780 if ((i & 0377) == 0200)
4782 if ((i & 0400) != 0)
4784 /* check all less significant bits */
4785 for (j = M + 5; j < NI; j++)
4834 unsigned EMUSHORT *x, *d;
4836 unsigned EMUSHORT xi[NI];
4841 exp = (EMULONG) xi[E] - (EXONE - 0201); /* adjust exponent for offsets */
4842 /* round off to nearest or even */
4845 emdnorm (xi, 0, 0, exp, 64);
4852 unsigned EMUSHORT *x, *y;
4854 unsigned EMUSHORT i;
4855 unsigned EMUSHORT *p;
4895 /* Output a binary NaN bit pattern in the target machine's format. */
4897 /* If special NaN bit patterns are required, define them in tm.h
4898 as arrays of unsigned 16-bit shorts. Otherwise, use the default
4902 unsigned EMUSHORT TFnan[8] =
4903 {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
4906 unsigned EMUSHORT TFnan[8] = {0, 0, 0, 0, 0, 0, 0x8000, 0xffff};
4912 unsigned EMUSHORT XFnan[6] = {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
4915 unsigned EMUSHORT XFnan[6] = {0, 0, 0, 0xc000, 0xffff, 0};
4921 unsigned EMUSHORT DFnan[4] = {0x7fff, 0xffff, 0xffff, 0xffff};
4924 unsigned EMUSHORT DFnan[4] = {0, 0, 0, 0xfff8};
4930 unsigned EMUSHORT SFnan[2] = {0x7fff, 0xffff};
4933 unsigned EMUSHORT SFnan[2] = {0, 0xffc0};
4939 make_nan (nan, mode)
4940 unsigned EMUSHORT *nan;
4941 enum machine_mode mode;
4944 unsigned EMUSHORT *p;
4948 /* Possibly the `reserved operand' patterns on a VAX can be
4949 used like NaN's, but probably not in the same way as IEEE. */
4971 for (i=0; i < n; i++)
4975 #endif /* EMU_NON_COMPILE not defined */