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.
63 * The following modification converts gcc macros into the ones
66 * Note: long double formats differ between IBMPC and MIEEE
67 * by more than just endian-ness.
70 /* REAL_ARITHMETIC defined means that macros in real.h are
71 defined to call emulator functions. */
72 #ifdef REAL_ARITHMETIC
74 #if TARGET_FLOAT_FORMAT == VAX_FLOAT_FORMAT
75 /* PDP-11, Pro350, VAX: */
77 #else /* it's not VAX */
78 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
80 /* Motorola IEEE, high order words come first (Sun workstation): */
82 #else /* not big-endian */
83 /* Intel IEEE, low order words come first:
86 #endif /* big-endian */
87 #else /* it's not IEEE either */
88 /* UNKnown arithmetic. We don't support this and can't go on. */
89 unknown arithmetic type
95 /* REAL_ARITHMETIC not defined means that the *host's* data
96 structure will be used. It may differ by endian-ness from the
97 target machine's structure and will get its ends swapped
98 accordingly (but not here). Probably only the decimal <-> binary
99 functions in this file will actually be used in this case. */
100 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
102 #else /* it's not VAX */
103 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
104 #ifdef HOST_WORDS_BIG_ENDIAN
106 #else /* not big-endian */
108 #endif /* big-endian */
109 #else /* it's not IEEE either */
110 unknown arithmetic type
112 #endif /* not IEEE */
115 #endif /* REAL_ARITHMETIC not defined */
117 /* Define for support of infinity. */
125 * Include file for extended precision arithmetic programs.
128 /* Number of 16 bit words in external e type format */
131 /* Number of 16 bit words in internal format */
134 /* Array offset to exponent */
137 /* Array offset to high guard word */
140 /* Number of bits of precision */
141 #define NBITS ((NI-4)*16)
143 /* Maximum number of decimal digits in ASCII conversion
146 #define NDEC (NBITS*8/27)
148 /* The exponent of 1.0 */
149 #define EXONE (0x3fff)
151 /* Find a host integer type that is at least 16 bits wide,
152 and another type at least twice whatever that size is. */
154 #if HOST_BITS_PER_CHAR >= 16
155 #define EMUSHORT char
156 #define EMUSHORT_SIZE HOST_BITS_PER_CHAR
157 #define EMULONG_SIZE (2 * HOST_BITS_PER_CHAR)
159 #if HOST_BITS_PER_SHORT >= 16
160 #define EMUSHORT short
161 #define EMUSHORT_SIZE HOST_BITS_PER_SHORT
162 #define EMULONG_SIZE (2 * HOST_BITS_PER_SHORT)
164 #if HOST_BITS_PER_INT >= 16
166 #define EMUSHORT_SIZE HOST_BITS_PER_INT
167 #define EMULONG_SIZE (2 * HOST_BITS_PER_INT)
169 #if HOST_BITS_PER_LONG >= 16
170 #define EMUSHORT long
171 #define EMUSHORT_SIZE HOST_BITS_PER_LONG
172 #define EMULONG_SIZE (2 * HOST_BITS_PER_LONG)
174 /* You will have to modify this program to have a smaller unit size. */
175 #define EMU_NON_COMPILE
181 #if HOST_BITS_PER_SHORT >= EMULONG_SIZE
182 #define EMULONG short
184 #if HOST_BITS_PER_INT >= EMULONG_SIZE
187 #if HOST_BITS_PER_LONG >= EMULONG_SIZE
190 #if HOST_BITS_PER_LONG_LONG >= EMULONG_SIZE
191 #define EMULONG long long int
193 /* You will have to modify this program to have a smaller unit size. */
194 #define EMU_NON_COMPILE
201 /* The host interface doesn't work if no 16-bit size exists. */
202 #if EMUSHORT_SIZE != 16
203 #define EMU_NON_COMPILE
206 /* OK to continue compilation. */
207 #ifndef EMU_NON_COMPILE
209 /* Construct macros to translate between REAL_VALUE_TYPE and e type.
210 In GET_REAL and PUT_REAL, r and e are pointers.
211 A REAL_VALUE_TYPE is guaranteed to occupy contiguous locations
212 in memory, with no holes. */
214 #if LONG_DOUBLE_TYPE_SIZE == 96
215 #define GET_REAL(r,e) bcopy (r, e, 2*NE)
216 #define PUT_REAL(e,r) bcopy (e, r, 2*NE)
217 #else /* no XFmode */
219 #ifdef REAL_ARITHMETIC
220 /* Emulator uses target format internally
221 but host stores it in host endian-ness. */
223 #if defined (HOST_WORDS_BIG_ENDIAN) == WORDS_BIG_ENDIAN
224 #define GET_REAL(r,e) e53toe ((r), (e))
225 #define PUT_REAL(e,r) etoe53 ((e), (r))
227 #else /* endian-ness differs */
228 /* emulator uses target endian-ness internally */
229 #define GET_REAL(r,e) \
230 do { EMUSHORT w[4]; \
231 w[3] = ((EMUSHORT *) r)[0]; \
232 w[2] = ((EMUSHORT *) r)[1]; \
233 w[1] = ((EMUSHORT *) r)[2]; \
234 w[0] = ((EMUSHORT *) r)[3]; \
235 e53toe (w, (e)); } while (0)
237 #define PUT_REAL(e,r) \
238 do { EMUSHORT w[4]; \
240 *((EMUSHORT *) r) = w[3]; \
241 *((EMUSHORT *) r + 1) = w[2]; \
242 *((EMUSHORT *) r + 2) = w[1]; \
243 *((EMUSHORT *) r + 3) = w[0]; } while (0)
245 #endif /* endian-ness differs */
247 #else /* not REAL_ARITHMETIC */
249 /* emulator uses host format */
250 #define GET_REAL(r,e) e53toe ((r), (e))
251 #define PUT_REAL(e,r) etoe53 ((e), (r))
253 #endif /* not REAL_ARITHMETIC */
254 #endif /* no XFmode */
257 extern int extra_warnings;
258 int ecmp (), enormlz (), eshift (), eisneg (), eisinf ();
259 void eadd (), esub (), emul (), ediv ();
260 void eshup1 (), eshup8 (), eshup6 (), eshdn1 (), eshdn8 (), eshdn6 ();
261 void eabs (), eneg (), emov (), eclear (), einfin (), efloor ();
262 void eldexp (), efrexp (), eifrac (), euifrac (), ltoe (), ultoe ();
263 void eround (), ereal_to_decimal ();
264 void esqrt (), elog (), eexp (), etanh (), epow ();
265 void asctoe (), asctoe24 (), asctoe53 (), asctoe64 ();
266 void etoasc (), e24toasc (), e53toasc (), e64toasc ();
267 void etoe64 (), etoe53 (), etoe24 (), e64toe (), e53toe (), e24toe ();
269 extern unsigned EMUSHORT ezero[], ehalf[], eone[], etwo[];
270 extern unsigned EMUSHORT elog2[], esqrt2[];
272 /* Pack output array with 32-bit numbers obtained from
273 array containing 16-bit numbers, swapping ends if required. */
276 unsigned EMUSHORT e[];
278 enum machine_mode mode;
288 /* Swap halfwords in the third long. */
289 th = (unsigned long) e[4] & 0xffff;
290 t = (unsigned long) e[5] & 0xffff;
293 /* fall into the double case */
297 /* swap halfwords in the second word */
298 th = (unsigned long) e[2] & 0xffff;
299 t = (unsigned long) e[3] & 0xffff;
302 /* fall into the float case */
306 /* swap halfwords in the first word */
307 th = (unsigned long) e[0] & 0xffff;
308 t = (unsigned long) e[1] & 0xffff;
319 /* Pack the output array without swapping. */
326 /* Pack the third long.
327 Each element of the input REAL_VALUE_TYPE array has 16 bit useful bits
329 th = (unsigned long) e[5] & 0xffff;
330 t = (unsigned long) e[4] & 0xffff;
333 /* fall into the double case */
337 /* pack the second long */
338 th = (unsigned long) e[3] & 0xffff;
339 t = (unsigned long) e[2] & 0xffff;
342 /* fall into the float case */
346 /* pack the first long */
347 th = (unsigned long) e[1] & 0xffff;
348 t = (unsigned long) e[0] & 0xffff;
361 /* This is the implementation of the REAL_ARITHMETIC macro.
364 earith (value, icode, r1, r2)
365 REAL_VALUE_TYPE *value;
370 unsigned EMUSHORT d1[NE], d2[NE], v[NE];
375 code = (enum tree_code) icode;
383 esub (d2, d1, v); /* d1 - d2 */
391 #ifndef REAL_INFINITY
392 if (ecmp (d2, ezero) == 0)
395 ediv (d2, d1, v); /* d1/d2 */
398 case MIN_EXPR: /* min (d1,d2) */
399 if (ecmp (d1, d2) < 0)
405 case MAX_EXPR: /* max (d1,d2) */
406 if (ecmp (d1, d2) > 0)
419 /* Truncate REAL_VALUE_TYPE toward zero to signed HOST_WIDE_INT
420 * implements REAL_VALUE_FIX_TRUNCATE (x) (etrunci (x))
426 unsigned EMUSHORT f[NE], g[NE];
438 /* Truncate REAL_VALUE_TYPE toward zero to unsigned HOST_WIDE_INT
439 * implements REAL_VALUE_UNSIGNED_FIX_TRUNCATE (x) (etruncui (x))
445 unsigned EMUSHORT f[NE], g[NE];
457 /* This is the REAL_VALUE_ATOF function.
458 * It converts a decimal string to binary, rounding off
459 * as indicated by the machine_mode argument. Then it
460 * promotes the rounded value to REAL_VALUE_TYPE.
467 unsigned EMUSHORT tem[NE], e[NE];
492 /* Expansion of REAL_NEGATE.
498 unsigned EMUSHORT e[NE];
509 * implements REAL_VALUE_FIX (x) (eroundi (x))
510 * The type of rounding is left unspecified by real.h.
511 * It is implemented here as round to nearest (add .5 and chop).
517 unsigned EMUSHORT f[NE], g[NE];
526 /* Round real to nearest unsigned int
527 * implements REAL_VALUE_UNSIGNED_FIX (x) ((unsigned int) eroundi (x))
528 * Negative input returns zero.
529 * The type of rounding is left unspecified by real.h.
530 * It is implemented here as round to nearest (add .5 and chop).
536 unsigned EMUSHORT f[NE], g[NE];
542 return ((unsigned int)l);
546 /* REAL_VALUE_FROM_INT macro.
549 ereal_from_int (d, i, j)
553 unsigned EMUSHORT df[NE], dg[NE];
562 /* complement and add 1 */
569 eldexp (eone, HOST_BITS_PER_LONG, df);
580 /* REAL_VALUE_FROM_UNSIGNED_INT macro.
583 ereal_from_uint (d, i, j)
587 unsigned EMUSHORT df[NE], dg[NE];
588 unsigned long low, high;
592 eldexp (eone, HOST_BITS_PER_LONG, df);
601 /* REAL_VALUE_TO_INT macro
604 ereal_to_int (low, high, rr)
608 unsigned EMUSHORT d[NE], df[NE], dg[NE], dh[NE];
612 /* convert positive value */
619 eldexp (eone, HOST_BITS_PER_LONG, df);
620 ediv (df, d, dg); /* dg = d / 2^32 is the high word */
621 euifrac (dg, high, dh);
622 emul (df, dh, dg); /* fractional part is the low word */
623 euifrac (dg, low, dh);
626 /* complement and add 1 */
636 /* REAL_VALUE_LDEXP macro.
643 unsigned EMUSHORT e[NE], y[NE];
652 /* These routines are conditionally compiled because functions
653 * of the same names may be defined in fold-const.c. */
654 #ifdef REAL_ARITHMETIC
656 /* Check for infinity in a REAL_VALUE_TYPE. */
661 unsigned EMUSHORT e[NE];
672 /* Check whether an IEEE double precision number is a NaN. */
682 /* Check for a negative IEEE double precision number.
683 * this means strictly less than zero, not -0.
690 unsigned EMUSHORT e[NE];
693 if (ecmp (e, ezero) < 0)
698 /* Expansion of REAL_VALUE_TRUNCATE.
699 * The result is in floating point, rounded to nearest or even.
702 real_value_truncate (mode, arg)
703 enum machine_mode mode;
706 unsigned EMUSHORT e[NE], t[NE];
739 #endif /* REAL_ARITHMETIC defined */
741 /* Target values are arrays of host longs. A long is guaranteed
742 to be at least 32 bits wide. */
748 unsigned EMUSHORT e[NE];
752 endian (e, l, XFmode);
760 unsigned EMUSHORT e[NE];
764 endian (e, l, DFmode);
771 unsigned EMUSHORT e[NE];
776 endian (e, &l, SFmode);
781 ereal_to_decimal (x, s)
785 unsigned EMUSHORT e[NE];
793 REAL_VALUE_TYPE x, y;
795 unsigned EMUSHORT ex[NE], ey[NE];
799 return (ecmp (ex, ey));
806 unsigned EMUSHORT ex[NE];
809 return (eisneg (ex));
812 /* End of REAL_ARITHMETIC interface */
816 * Extended precision IEEE binary floating point arithmetic routines
818 * Numbers are stored in C language as arrays of 16-bit unsigned
819 * short integers. The arguments of the routines are pointers to
823 * External e type data structure, simulates Intel 8087 chip
824 * temporary real format but possibly with a larger significand:
826 * NE-1 significand words (least significant word first,
827 * most significant bit is normally set)
828 * exponent (value = EXONE for 1.0,
829 * top bit is the sign)
832 * Internal data structure of a number (a "word" is 16 bits):
834 * ei[0] sign word (0 for positive, 0xffff for negative)
835 * ei[1] biased exponent (value = EXONE for the number 1.0)
836 * ei[2] high guard word (always zero after normalization)
838 * to ei[NI-2] significand (NI-4 significand words,
839 * most significant word first,
840 * most significant bit is set)
841 * ei[NI-1] low guard word (0x8000 bit is rounding place)
845 * Routines for external format numbers
847 * asctoe (string, e) ASCII string to extended double e type
848 * asctoe64 (string, &d) ASCII string to long double
849 * asctoe53 (string, &d) ASCII string to double
850 * asctoe24 (string, &f) ASCII string to single
851 * asctoeg (string, e, prec) ASCII string to specified precision
852 * e24toe (&f, e) IEEE single precision to e type
853 * e53toe (&d, e) IEEE double precision to e type
854 * e64toe (&d, e) IEEE long double precision to e type
855 * eabs (e) absolute value
856 * eadd (a, b, c) c = b + a
858 * ecmp (a, b) compare a to b, return 1, 0, or -1
859 * ediv (a, b, c) c = b / a
860 * efloor (a, b) truncate to integer, toward -infinity
861 * efrexp (a, exp, s) extract exponent and significand
862 * eifrac (e, &l, frac) e to long integer and e type fraction
863 * euifrac (e, &l, frac) e to unsigned long integer and e type fraction
864 * einfin (e) set e to infinity, leaving its sign alone
865 * eldexp (a, n, b) multiply by 2**n
867 * emul (a, b, c) c = b * a
869 * eround (a, b) b = nearest integer value to a
870 * esub (a, b, c) c = b - a
871 * e24toasc (&f, str, n) single to ASCII string, n digits after decimal
872 * e53toasc (&d, str, n) double to ASCII string, n digits after decimal
873 * e64toasc (&d, str, n) long double to ASCII string
874 * etoasc (e, str, n) e to ASCII string, n digits after decimal
875 * etoe24 (e, &f) convert e type to IEEE single precision
876 * etoe53 (e, &d) convert e type to IEEE double precision
877 * etoe64 (e, &d) convert e type to IEEE long double precision
878 * ltoe (&l, e) long (32 bit) integer to e type
879 * ultoe (&l, e) unsigned long (32 bit) integer to e type
880 * eisneg (e) 1 if sign bit of e != 0, else 0
881 * eisinf (e) 1 if e has maximum exponent
884 * Routines for internal format numbers
886 * eaddm (ai, bi) add significands, bi = bi + ai
888 * ecleazs (ei) set ei = 0 but leave its sign alone
889 * ecmpm (ai, bi) compare significands, return 1, 0, or -1
890 * edivm (ai, bi) divide significands, bi = bi / ai
891 * emdnorm (ai,l,s,exp) normalize and round off
892 * emovi (a, ai) convert external a to internal ai
893 * emovo (ai, a) convert internal ai to external a
894 * emovz (ai, bi) bi = ai, low guard word of bi = 0
895 * emulm (ai, bi) multiply significands, bi = bi * ai
896 * enormlz (ei) left-justify the significand
897 * eshdn1 (ai) shift significand and guards down 1 bit
898 * eshdn8 (ai) shift down 8 bits
899 * eshdn6 (ai) shift down 16 bits
900 * eshift (ai, n) shift ai n bits up (or down if n < 0)
901 * eshup1 (ai) shift significand and guards up 1 bit
902 * eshup8 (ai) shift up 8 bits
903 * eshup6 (ai) shift up 16 bits
904 * esubm (ai, bi) subtract significands, bi = bi - ai
907 * The result is always normalized and rounded to NI-4 word precision
908 * after each arithmetic operation.
910 * Exception flags and NaNs are NOT fully supported.
911 * This arithmetic should never produce a NaN output, but it might
912 * be confused by a NaN input.
913 * Define INFINITY in mconf.h for support of infinity; otherwise a
914 * saturation arithmetic is implemented.
915 * Denormals are always supported here where appropriate (e.g., not
916 * for conversion to DEC numbers).
923 * 5 Jan 84 PDP-11 assembly language version
924 * 2 Mar 86 fixed bug in asctoq
925 * 6 Dec 86 C language version
926 * 30 Aug 88 100 digit version, improved rounding
927 * 15 May 92 80-bit long double support
929 * Author: S. L. Moshier.
935 * Common include file for math routines
947 * This file contains definitions for error codes that are
948 * passed to the common error handling routine mtherr
951 * The file also includes a conditional assembly definition
952 * for the type of computer arithmetic (Intel IEEE, DEC, Motorola
955 * For Digital Equipment PDP-11 and VAX computers, certain
956 * IBM systems, and others that use numbers with a 56-bit
957 * significand, the symbol DEC should be defined. In this
958 * mode, most floating point constants are given as arrays
959 * of octal integers to eliminate decimal to binary conversion
960 * errors that might be introduced by the compiler.
962 * For computers, such as IBM PC, that follow the IEEE
963 * Standard for Binary Floating Point Arithmetic (ANSI/IEEE
964 * Std 754-1985), the symbol IBMPC or MIEEE should be defined.
965 * These numbers have 53-bit significands. In this mode, constants
966 * are provided as arrays of hexadecimal 16 bit integers.
968 * To accommodate other types of computer arithmetic, all
969 * constants are also provided in a normal decimal radix
970 * which one can hope are correctly converted to a suitable
971 * format by the available C language compiler. To invoke
972 * this mode, the symbol UNK is defined.
974 * An important difference among these modes is a predefined
975 * set of machine arithmetic constants for each. The numbers
976 * MACHEP (the machine roundoff error), MAXNUM (largest number
977 * represented), and several other parameters are preset by
978 * the configuration symbol. Check the file const.c to
979 * ensure that these values are correct for your computer.
981 * For ANSI C compatibility, define ANSIC equal to 1. Currently
982 * this affects only the atan2 function and others that use it.
985 /* Constant definitions for math error conditions. */
987 #define DOMAIN 1 /* argument domain error */
988 #define SING 2 /* argument singularity */
989 #define OVERFLOW 3 /* overflow range error */
990 #define UNDERFLOW 4 /* underflow range error */
991 #define TLOSS 5 /* total loss of precision */
992 #define PLOSS 6 /* partial loss of precision */
994 /* e type constants used by high precision check routines */
996 /*include "ehead.h"*/
998 unsigned EMUSHORT ezero[NE] =
1000 0, 0000000, 0000000, 0000000, 0000000, 0000000,};
1001 extern unsigned EMUSHORT ezero[];
1004 unsigned EMUSHORT ehalf[NE] =
1006 0, 0000000, 0000000, 0000000, 0100000, 0x3ffe,};
1007 extern unsigned EMUSHORT ehalf[];
1010 unsigned EMUSHORT eone[NE] =
1012 0, 0000000, 0000000, 0000000, 0100000, 0x3fff,};
1013 extern unsigned EMUSHORT eone[];
1016 unsigned EMUSHORT etwo[NE] =
1018 0, 0000000, 0000000, 0000000, 0100000, 0040000,};
1019 extern unsigned EMUSHORT etwo[];
1022 unsigned EMUSHORT e32[NE] =
1024 0, 0000000, 0000000, 0000000, 0100000, 0040004,};
1025 extern unsigned EMUSHORT e32[];
1027 /* 6.93147180559945309417232121458176568075500134360255E-1 */
1028 unsigned EMUSHORT elog2[NE] =
1030 0xc9e4, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1031 extern unsigned EMUSHORT elog2[];
1033 /* 1.41421356237309504880168872420969807856967187537695E0 */
1034 unsigned EMUSHORT esqrt2[NE] =
1036 0x597e, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1037 extern unsigned EMUSHORT esqrt2[];
1040 * 1.12837916709551257389615890312154517168810125865800E0 */
1041 unsigned EMUSHORT eoneopi[NE] =
1043 0x71d5, 0x688d, 0012333, 0135202, 0110156, 0x3fff,};
1044 extern unsigned EMUSHORT eoneopi[];
1046 /* 3.14159265358979323846264338327950288419716939937511E0 */
1047 unsigned EMUSHORT epi[NE] =
1049 0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1050 extern unsigned EMUSHORT epi[];
1052 /* 5.7721566490153286060651209008240243104215933593992E-1 */
1053 unsigned EMUSHORT eeul[NE] =
1055 0xd1be, 0xc7a4, 0076660, 0063743, 0111704, 0x3ffe,};
1056 extern unsigned EMUSHORT eeul[];
1065 /* Control register for rounding precision.
1066 * This can be set to 80 (if NE=6), 64, 56, 53, or 24 bits.
1071 void eaddm (), esubm (), emdnorm (), asctoeg ();
1072 static void toe24 (), toe53 (), toe64 ();
1073 void eremain (), einit (), eiremain ();
1074 int ecmpm (), edivm (), emulm ();
1075 void emovi (), emovo (), emovz (), ecleaz (), ecleazs (), eadd1 ();
1076 void etodec (), todec (), dectoe ();
1087 ; Clear out entire external format number.
1089 ; unsigned EMUSHORT x[];
1095 register unsigned EMUSHORT *x;
1099 for (i = 0; i < NE; i++)
1105 /* Move external format number from a to b.
1112 register unsigned EMUSHORT *a, *b;
1116 for (i = 0; i < NE; i++)
1122 ; Absolute value of external format number
1130 unsigned EMUSHORT x[]; /* x is the memory address of a short */
1133 x[NE - 1] &= 0x7fff; /* sign is top bit of last word of external format */
1140 ; Negate external format number
1142 ; unsigned EMUSHORT x[NE];
1148 unsigned EMUSHORT x[];
1151 x[NE - 1] ^= 0x8000; /* Toggle the sign bit */
1156 /* Return 1 if external format number is negative,
1161 unsigned EMUSHORT x[];
1164 if (x[NE - 1] & 0x8000)
1171 /* Return 1 if external format number has maximum possible exponent,
1176 unsigned EMUSHORT x[];
1179 if ((x[NE - 1] & 0x7fff) == 0x7fff)
1187 ; Fill entire number, including exponent and significand, with
1188 ; largest possible number. These programs implement a saturation
1189 ; value that is an ordinary, legal number. A special value
1190 ; "infinity" may also be implemented; this would require tests
1191 ; for that value and implementation of special rules for arithmetic
1192 ; operations involving inifinity.
1197 register unsigned EMUSHORT *x;
1202 for (i = 0; i < NE - 1; i++)
1206 for (i = 0; i < NE - 1; i++)
1231 /* Move in external format number,
1232 * converting it to internal format.
1236 unsigned EMUSHORT *a, *b;
1238 register unsigned EMUSHORT *p, *q;
1242 p = a + (NE - 1); /* point to last word of external number */
1243 /* get the sign bit */
1248 /* get the exponent */
1250 *q++ &= 0x7fff; /* delete the sign bit */
1252 if ((*(q - 1) & 0x7fff) == 0x7fff)
1254 for (i = 2; i < NI; i++)
1259 /* clear high guard word */
1261 /* move in the significand */
1262 for (i = 0; i < NE - 1; i++)
1264 /* clear low guard word */
1269 /* Move internal format number out,
1270 * converting it to external format.
1274 unsigned EMUSHORT *a, *b;
1276 register unsigned EMUSHORT *p, *q;
1277 unsigned EMUSHORT i;
1280 q = b + (NE - 1); /* point to output exponent */
1281 /* combine sign and exponent */
1284 *q-- = *p++ | 0x8000;
1288 if (*(p - 1) == 0x7fff)
1294 /* skip over guard word */
1296 /* move the significand */
1297 for (i = 0; i < NE - 1; i++)
1304 /* Clear out internal format number.
1309 register unsigned EMUSHORT *xi;
1313 for (i = 0; i < NI; i++)
1318 /* same, but don't touch the sign. */
1322 register unsigned EMUSHORT *xi;
1327 for (i = 0; i < NI - 1; i++)
1333 /* Move internal format number from a to b.
1337 register unsigned EMUSHORT *a, *b;
1341 for (i = 0; i < NI - 1; i++)
1343 /* clear low guard word */
1349 ; Compare significands of numbers in internal format.
1350 ; Guard words are included in the comparison.
1352 ; unsigned EMUSHORT a[NI], b[NI];
1355 ; for the significands:
1356 ; returns +1 if a > b
1362 register unsigned EMUSHORT *a, *b;
1366 a += M; /* skip up to significand area */
1368 for (i = M; i < NI; i++)
1376 if (*(--a) > *(--b))
1384 ; Shift significand down by 1 bit
1389 register unsigned EMUSHORT *x;
1391 register unsigned EMUSHORT bits;
1394 x += M; /* point to significand area */
1397 for (i = M; i < NI; i++)
1412 ; Shift significand up by 1 bit
1417 register unsigned EMUSHORT *x;
1419 register unsigned EMUSHORT bits;
1425 for (i = M; i < NI; i++)
1440 ; Shift significand down by 8 bits
1445 register unsigned EMUSHORT *x;
1447 register unsigned EMUSHORT newbyt, oldbyt;
1452 for (i = M; i < NI; i++)
1463 ; Shift significand up by 8 bits
1468 register unsigned EMUSHORT *x;
1471 register unsigned EMUSHORT newbyt, oldbyt;
1476 for (i = M; i < NI; i++)
1487 ; Shift significand up by 16 bits
1492 register unsigned EMUSHORT *x;
1495 register unsigned EMUSHORT *p;
1500 for (i = M; i < NI - 1; i++)
1507 ; Shift significand down by 16 bits
1512 register unsigned EMUSHORT *x;
1515 register unsigned EMUSHORT *p;
1520 for (i = M; i < NI - 1; i++)
1533 unsigned EMUSHORT *x, *y;
1535 register unsigned EMULONG a;
1542 for (i = M; i < NI; i++)
1544 a = (unsigned EMULONG) (*x) + (unsigned EMULONG) (*y) + carry;
1549 *y = (unsigned EMUSHORT) a;
1556 ; Subtract significands
1562 unsigned EMUSHORT *x, *y;
1571 for (i = M; i < NI; i++)
1573 a = (unsigned EMULONG) (*y) - (unsigned EMULONG) (*x) - carry;
1578 *y = (unsigned EMUSHORT) a;
1585 /* Divide significands */
1587 static unsigned EMUSHORT equot[NI];
1591 unsigned EMUSHORT den[], num[];
1594 register unsigned EMUSHORT *p, *q;
1595 unsigned EMUSHORT j;
1601 for (i = M; i < NI; i++)
1606 /* Use faster compare and subtraction if denominator
1607 * has only 15 bits of significance.
1612 for (i = M + 3; i < NI; i++)
1617 if ((den[M + 1] & 1) != 0)
1625 for (i = 0; i < NBITS + 2; i++)
1643 /* The number of quotient bits to calculate is
1644 * NBITS + 1 scaling guard bit + 1 roundoff bit.
1649 for (i = 0; i < NBITS + 2; i++)
1651 if (ecmpm (den, num) <= 0)
1654 j = 1; /* quotient bit = 1 */
1668 /* test for nonzero remainder after roundoff bit */
1671 for (i = M; i < NI; i++)
1679 for (i = 0; i < NI; i++)
1685 /* Multiply significands */
1688 unsigned EMUSHORT a[], b[];
1690 unsigned EMUSHORT *p, *q;
1695 for (i = M; i < NI; i++)
1700 while (*p == 0) /* significand is not supposed to be all zero */
1705 if ((*p & 0xff) == 0)
1713 for (i = 0; i < k; i++)
1717 /* remember if there were any nonzero bits shifted out */
1724 for (i = 0; i < NI; i++)
1727 /* return flag for lost nonzero bits */
1734 * Normalize and round off.
1736 * The internal format number to be rounded is "s".
1737 * Input "lost" indicates whether or not the number is exact.
1738 * This is the so-called sticky bit.
1740 * Input "subflg" indicates whether the number was obtained
1741 * by a subtraction operation. In that case if lost is nonzero
1742 * then the number is slightly smaller than indicated.
1744 * Input "exp" is the biased exponent, which may be negative.
1745 * the exponent field of "s" is ignored but is replaced by
1746 * "exp" as adjusted by normalization and rounding.
1748 * Input "rcntrl" is the rounding control.
1751 static int rlast = -1;
1753 static unsigned EMUSHORT rmsk = 0;
1754 static unsigned EMUSHORT rmbit = 0;
1755 static unsigned EMUSHORT rebit = 0;
1757 static unsigned EMUSHORT rbit[NI];
1760 emdnorm (s, lost, subflg, exp, rcntrl)
1761 unsigned EMUSHORT s[];
1768 unsigned EMUSHORT r;
1773 /* a blank significand could mean either zero or infinity. */
1786 if ((j > NBITS) && (exp < 32767))
1794 if (exp > (EMULONG) (-NBITS - 1))
1807 /* Round off, unless told not to by rcntrl. */
1810 /* Set up rounding parameters if the control register changed. */
1811 if (rndprc != rlast)
1818 rw = NI - 1; /* low guard word */
1833 /* For DEC arithmetic */
1882 /* These tests assume NI = 8 */
1902 if ((r & rmbit) != 0)
1907 { /* round to even */
1908 if ((s[re] & rebit) == 0)
1920 if ((rndprc < 64) && (exp <= 0))
1925 { /* overflow on roundoff */
1938 for (i = 2; i < NI - 1; i++)
1941 warning ("floating point overflow");
1945 for (i = M + 1; i < NI - 1; i++)
1963 s[1] = (unsigned EMUSHORT) exp;
1969 ; Subtract external format numbers.
1971 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
1972 ; esub (a, b, c); c = b - a
1975 static int subflg = 0;
1979 unsigned EMUSHORT *a, *b, *c;
1990 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
1991 ; eadd (a, b, c); c = b + a
1995 unsigned EMUSHORT *a, *b, *c;
2004 unsigned EMUSHORT *a, *b, *c;
2006 unsigned EMUSHORT ai[NI], bi[NI], ci[NI];
2008 EMULONG lt, lta, ltb;
2029 /* compare exponents */
2034 { /* put the larger number in bi */
2044 if (lt < (EMULONG) (-NBITS - 1))
2045 goto done; /* answer same as larger addend */
2047 lost = eshift (ai, k); /* shift the smaller number down */
2051 /* exponents were the same, so must compare significands */
2054 { /* the numbers are identical in magnitude */
2055 /* if different signs, result is zero */
2061 /* if same sign, result is double */
2062 /* double denomalized tiny number */
2063 if ((bi[E] == 0) && ((bi[3] & 0x8000) == 0))
2068 /* add 1 to exponent unless both are zero! */
2069 for (j = 1; j < NI - 1; j++)
2073 /* This could overflow, but let emovo take care of that. */
2078 bi[E] = (unsigned EMUSHORT) ltb;
2082 { /* put the larger number in bi */
2098 emdnorm (bi, lost, subflg, ltb, 64);
2109 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2110 ; ediv (a, b, c); c = b / a
2114 unsigned EMUSHORT *a, *b, *c;
2116 unsigned EMUSHORT ai[NI], bi[NI];
2118 EMULONG lt, lta, ltb;
2123 if (eisneg (a) ^ eisneg (b))
2124 *(c + (NE - 1)) = 0x8000;
2126 *(c + (NE - 1)) = 0;
2141 { /* See if numerator is zero. */
2142 for (i = 1; i < NI - 1; i++)
2146 ltb -= enormlz (bi);
2156 { /* possible divide by zero */
2157 for (i = 1; i < NI - 1; i++)
2161 lta -= enormlz (ai);
2166 *(c + (NE - 1)) = 0;
2168 *(c + (NE - 1)) = 0x8000;
2170 mtherr ("ediv", SING);
2176 /* calculate exponent */
2177 lt = ltb - lta + EXONE;
2178 emdnorm (bi, i, 0, lt, 64);
2192 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2193 ; emul (a, b, c); c = b * a
2197 unsigned EMUSHORT *a, *b, *c;
2199 unsigned EMUSHORT ai[NI], bi[NI];
2201 EMULONG lt, lta, ltb;
2204 if (eisinf (a) || eisinf (b))
2206 if (eisneg (a) ^ eisneg (b))
2207 *(c + (NE - 1)) = 0x8000;
2209 *(c + (NE - 1)) = 0;
2220 for (i = 1; i < NI - 1; i++)
2224 lta -= enormlz (ai);
2235 for (i = 1; i < NI - 1; i++)
2239 ltb -= enormlz (bi);
2248 /* Multiply significands */
2250 /* calculate exponent */
2251 lt = lta + ltb - (EXONE - 1);
2252 emdnorm (bi, j, 0, lt, 64);
2253 /* calculate sign of product */
2265 ; Convert IEEE double precision to e type
2267 ; unsigned EMUSHORT x[N+2];
2272 unsigned EMUSHORT *e, *y;
2276 dectoe (e, y); /* see etodec.c */
2280 register unsigned EMUSHORT r;
2281 register unsigned EMUSHORT *p;
2282 unsigned EMUSHORT yy[NI];
2285 denorm = 0; /* flag if denormalized number */
2294 yy[M] = (r & 0x0f) | 0x10;
2295 r &= ~0x800f; /* strip sign and 4 significand bits */
2306 /* If zero exponent, then the significand is denormalized.
2307 * So, take back the understood high significand bit. */
2329 { /* if zero exponent, then normalize the significand */
2330 if ((k = enormlz (yy)) > NBITS)
2333 yy[E] -= (unsigned EMUSHORT) (k - 1);
2336 #endif /* not DEC */
2341 unsigned EMUSHORT *e, *y;
2343 unsigned EMUSHORT yy[NI];
2344 unsigned EMUSHORT *p, *q;
2348 for (i = 0; i < NE - 5; i++)
2351 for (i = 0; i < 5; i++)
2355 for (i = 0; i < 5; i++)
2359 p = &yy[0] + (NE - 1);
2362 for (i = 0; i < 4; i++)
2376 for (i = 0; i < NE; i++)
2382 ; Convert IEEE single precision to e type
2384 ; unsigned EMUSHORT x[N+2];
2389 unsigned EMUSHORT *e, *y;
2391 register unsigned EMUSHORT r;
2392 register unsigned EMUSHORT *p;
2393 unsigned EMUSHORT yy[NI];
2396 denorm = 0; /* flag if denormalized number */
2408 yy[M] = (r & 0x7f) | 0200;
2409 r &= ~0x807f; /* strip sign and 7 significand bits */
2420 /* If zero exponent, then the significand is denormalized.
2421 * So, take back the understood high significand bit. */
2442 { /* if zero exponent, then normalize the significand */
2443 if ((k = enormlz (yy)) > NBITS)
2446 yy[E] -= (unsigned EMUSHORT) (k - 1);
2454 unsigned EMUSHORT *x, *e;
2456 unsigned EMUSHORT xi[NI];
2461 /* adjust exponent for offset */
2462 exp = (EMULONG) xi[E];
2467 /* round off to nearest or even */
2470 emdnorm (xi, 0, 0, exp, 64);
2476 /* move out internal format to ieee long double */
2479 unsigned EMUSHORT *a, *b;
2481 register unsigned EMUSHORT *p, *q;
2482 unsigned EMUSHORT i;
2488 q = b + 4; /* point to output exponent */
2489 #if LONG_DOUBLE_TYPE_SIZE == 96
2490 /* Clear the last two bytes of 12-byte Intel format */
2495 /* combine sign and exponent */
2499 *q++ = *p++ | 0x8000;
2505 *q-- = *p++ | 0x8000;
2509 /* skip over guard word */
2511 /* move the significand */
2513 for (i = 0; i < 4; i++)
2516 for (i = 0; i < 4; i++)
2523 ; e type to IEEE double precision
2525 ; unsigned EMUSHORT x[NE];
2533 unsigned EMUSHORT *x, *e;
2535 etodec (x, e); /* see etodec.c */
2540 unsigned EMUSHORT *x, *y;
2549 unsigned EMUSHORT *x, *e;
2551 unsigned EMUSHORT xi[NI];
2556 /* adjust exponent for offsets */
2557 exp = (EMULONG) xi[E] - (EXONE - 0x3ff);
2562 /* round off to nearest or even */
2565 emdnorm (xi, 0, 0, exp, 64);
2574 unsigned EMUSHORT *x, *y;
2576 unsigned EMUSHORT i;
2577 unsigned EMUSHORT *p;
2584 *y = 0; /* output high order */
2586 *y = 0x8000; /* output sign bit */
2589 if (i >= (unsigned int) 2047)
2590 { /* Saturate at largest number less than infinity. */
2605 *y |= (unsigned EMUSHORT) 0x7fef;
2629 i |= *p++ & (unsigned EMUSHORT) 0x0f; /* *p = xi[M] */
2630 *y |= (unsigned EMUSHORT) i; /* high order output already has sign bit set */
2644 #endif /* not DEC */
2649 ; e type to IEEE single precision
2651 ; unsigned EMUSHORT x[N+2];
2656 unsigned EMUSHORT *x, *e;
2659 unsigned EMUSHORT xi[NI];
2663 /* adjust exponent for offsets */
2664 exp = (EMULONG) xi[E] - (EXONE - 0177);
2669 /* round off to nearest or even */
2672 emdnorm (xi, 0, 0, exp, 64);
2680 unsigned EMUSHORT *x, *y;
2682 unsigned EMUSHORT i;
2683 unsigned EMUSHORT *p;
2692 *y = 0; /* output high order */
2694 *y = 0x8000; /* output sign bit */
2697 /* Handle overflow cases. */
2701 *y |= (unsigned EMUSHORT) 0x7f80;
2712 #else /* no INFINITY */
2713 *y |= (unsigned EMUSHORT) 0x7f7f;
2727 #endif /* no INFINITY */
2739 i |= *p++ & (unsigned EMUSHORT) 0x7f; /* *p = xi[M] */
2740 *y |= i; /* high order output already has sign bit set */
2754 /* Compare two e type numbers.
2756 * unsigned EMUSHORT a[NE], b[NE];
2759 * returns +1 if a > b
2765 unsigned EMUSHORT *a, *b;
2767 unsigned EMUSHORT ai[NI], bi[NI];
2768 register unsigned EMUSHORT *p, *q;
2778 { /* the signs are different */
2780 for (i = 1; i < NI - 1; i++)
2794 /* both are the same sign */
2809 return (0); /* equality */
2815 if (*(--p) > *(--q))
2816 return (msign); /* p is bigger */
2818 return (-msign); /* p is littler */
2824 /* Find nearest integer to x = floor (x + 0.5)
2826 * unsigned EMUSHORT x[NE], y[NE]
2831 unsigned EMUSHORT *x, *y;
2841 ; convert long integer to e type
2844 ; unsigned EMUSHORT x[NE];
2846 ; note &l is the memory address of l
2850 long *lp; /* lp is the memory address of a long integer */
2851 unsigned EMUSHORT *y; /* y is the address of a short */
2853 unsigned EMUSHORT yi[NI];
2860 /* make it positive */
2861 ll = (unsigned long) (-(*lp));
2862 yi[0] = 0xffff; /* put correct sign in the e type number */
2866 ll = (unsigned long) (*lp);
2868 /* move the long integer to yi significand area */
2869 yi[M] = (unsigned EMUSHORT) (ll >> 16);
2870 yi[M + 1] = (unsigned EMUSHORT) ll;
2872 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
2873 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
2874 ecleaz (yi); /* it was zero */
2876 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
2877 emovo (yi, y); /* output the answer */
2881 ; convert unsigned long integer to e type
2884 ; unsigned EMUSHORT x[NE];
2886 ; note &l is the memory address of l
2890 unsigned long *lp; /* lp is the memory address of a long integer */
2891 unsigned EMUSHORT *y; /* y is the address of a short */
2893 unsigned EMUSHORT yi[NI];
2900 /* move the long integer to ayi significand area */
2901 yi[M] = (unsigned EMUSHORT) (ll >> 16);
2902 yi[M + 1] = (unsigned EMUSHORT) ll;
2904 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
2905 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
2906 ecleaz (yi); /* it was zero */
2908 yi[E] -= (unsigned EMUSHORT) k; /* subtract shift count from exponent */
2909 emovo (yi, y); /* output the answer */
2914 ; Find long integer and fractional parts
2917 ; unsigned EMUSHORT x[NE], frac[NE];
2918 ; xifrac (x, &i, frac);
2920 The integer output has the sign of the input. The fraction is
2921 the positive fractional part of abs (x).
2925 unsigned EMUSHORT *x;
2927 unsigned EMUSHORT *frac;
2929 unsigned EMUSHORT xi[NI];
2933 k = (int) xi[E] - (EXONE - 1);
2936 /* if exponent <= 0, integer = 0 and real output is fraction */
2941 if (k > (HOST_BITS_PER_LONG - 1))
2944 ; long integer overflow: output large integer
2945 ; and correct fraction
2948 *i = ((unsigned long) 1) << (HOST_BITS_PER_LONG - 1);
2950 *i = (((unsigned long) 1) << (HOST_BITS_PER_LONG - 1)) - 1;
2953 warning ("overflow on truncation to integer");
2960 ; shift more than 16 bits: shift up k-16, output the integer,
2961 ; then complete the shift to get the fraction.
2966 *i = (long) (((unsigned long) xi[M] << 16) | xi[M + 1]);
2971 /* shift not more than 16 bits */
2973 *i = (long) xi[M] & 0xffff;
2984 if ((k = enormlz (xi)) > NBITS)
2987 xi[E] -= (unsigned EMUSHORT) k;
2994 ; Find unsigned long integer and fractional parts
2997 ; unsigned EMUSHORT x[NE], frac[NE];
2998 ; xifrac (x, &i, frac);
3000 A negative e type input yields integer output = 0
3001 but correct fraction.
3004 euifrac (x, i, frac)
3005 unsigned EMUSHORT *x;
3007 unsigned EMUSHORT *frac;
3009 unsigned EMUSHORT xi[NI];
3013 k = (int) xi[E] - (EXONE - 1);
3016 /* if exponent <= 0, integer = 0 and argument is fraction */
3024 ; long integer overflow: output large integer
3025 ; and correct fraction
3030 warning ("overflow on truncation to unsigned integer");
3037 ; shift more than 16 bits: shift up k-16, output the integer,
3038 ; then complete the shift to get the fraction.
3043 *i = (long) (((unsigned long) xi[M] << 16) | xi[M + 1]);
3048 /* shift not more than 16 bits */
3050 *i = (long) xi[M] & 0xffff;
3060 if ((k = enormlz (xi)) > NBITS)
3063 xi[E] -= (unsigned EMUSHORT) k;
3073 ; Shifts significand area up or down by the number of bits
3074 ; given by the variable sc.
3078 unsigned EMUSHORT *x;
3081 unsigned EMUSHORT lost;
3082 unsigned EMUSHORT *p;
3095 lost |= *p; /* remember lost bits */
3136 return ((int) lost);
3144 ; Shift normalizes the significand area pointed to by argument
3145 ; shift count (up = positive) is returned.
3149 unsigned EMUSHORT x[];
3151 register unsigned EMUSHORT *p;
3160 return (0); /* already normalized */
3165 /* With guard word, there are NBITS+16 bits available.
3166 * return true if all are zero.
3171 /* see if high byte is zero */
3172 while ((*p & 0xff00) == 0)
3177 /* now shift 1 bit at a time */
3178 while ((*p & 0x8000) == 0)
3184 mtherr ("enormlz", UNDERFLOW);
3190 /* Normalize by shifting down out of the high guard word
3191 of the significand */
3206 mtherr ("enormlz", OVERFLOW);
3216 /* Convert e type number to decimal format ASCII string.
3217 * The constants are for 64 bit precision.
3223 static unsigned EMUSHORT etens[NTEN + 1][NE] =
3225 {0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
3226 {0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
3227 {0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
3228 {0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
3229 {0xddbc, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
3230 {0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
3231 {0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
3232 {0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
3233 {0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
3234 {0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
3235 {0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
3236 {0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
3237 {0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
3240 static unsigned EMUSHORT emtens[NTEN + 1][NE] =
3242 {0x2de4, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
3243 {0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
3244 {0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
3245 {0x7133, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
3246 {0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
3247 {0xac7d, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
3248 {0x3f24, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
3249 {0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
3250 {0x4c2f, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
3251 {0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
3252 {0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
3253 {0x3d71, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
3254 {0xcccd, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
3258 e24toasc (x, string, ndigs)
3259 unsigned EMUSHORT x[];
3263 unsigned EMUSHORT w[NI];
3267 if ((x[1] & 0x7f80) == 0x7f80)
3269 if ((x[0] & 0x7f80) == 0x7f80)
3277 sprintf (string, " -Infinity ");
3279 sprintf (string, " Infinity ");
3284 etoasc (w, string, ndigs);
3289 e53toasc (x, string, ndigs)
3290 unsigned EMUSHORT x[];
3294 unsigned EMUSHORT w[NI];
3298 if ((x[3] & 0x7ff0) == 0x7ff0)
3300 if ((x[0] & 0x7ff0) == 0x7ff0)
3308 sprintf (string, " -Infinity ");
3310 sprintf (string, " Infinity ");
3315 etoasc (w, string, ndigs);
3320 e64toasc (x, string, ndigs)
3321 unsigned EMUSHORT x[];
3325 unsigned EMUSHORT w[NI];
3329 if ((x[4] & 0x7fff) == 0x7fff)
3331 if ((x[0] & 0x7fff) == 0x7fff)
3339 sprintf (string, " -Infinity ");
3341 sprintf (string, " Infinity ");
3346 etoasc (w, string, ndigs);
3350 static char wstring[80]; /* working storage for ASCII output */
3353 etoasc (x, string, ndigs)
3354 unsigned EMUSHORT x[];
3359 unsigned EMUSHORT y[NI], t[NI], u[NI], w[NI];
3360 unsigned EMUSHORT *p, *r, *ten;
3361 unsigned EMUSHORT sign;
3362 int i, j, k, expon, rndsav;
3364 unsigned EMUSHORT m;
3368 while ((*s++ = *ss++) != '\0')
3371 rndprc = NBITS; /* set to full precision */
3372 emov (x, y); /* retain external format */
3373 if (y[NE - 1] & 0x8000)
3376 y[NE - 1] &= 0x7fff;
3383 ten = &etens[NTEN][0];
3385 /* Test for zero exponent */
3388 for (k = 0; k < NE - 1; k++)
3391 goto tnzro; /* denormalized number */
3393 goto isone; /* legal all zeros */
3397 /* Test for infinity. Don't bother with illegal infinities.
3399 if (y[NE - 1] == 0x7fff)
3402 sprintf (wstring, " -Infinity ");
3404 sprintf (wstring, " Infinity ");
3408 /* Test for exponent nonzero but significand denormalized.
3409 * This is an error condition.
3411 if ((y[NE - 1] != 0) && ((y[NE - 2] & 0x8000) == 0))
3413 mtherr ("etoasc", DOMAIN);
3414 sprintf (wstring, "NaN");
3418 /* Compare to 1.0 */
3424 { /* Number is greater than 1 */
3425 /* Convert significand to an integer and strip trailing decimal zeros. */
3427 u[NE - 1] = EXONE + NBITS - 1;
3429 p = &etens[NTEN - 4][0];
3435 for (j = 0; j < NE - 1; j++)
3448 /* Rescale from integer significand */
3449 u[NE - 1] += y[NE - 1] - (unsigned int) (EXONE + NBITS - 1);
3451 /* Find power of 10 */
3455 while (ecmp (ten, u) <= 0)
3457 if (ecmp (p, u) <= 0)
3470 { /* Number is less than 1.0 */
3471 /* Pad significand with trailing decimal zeros. */
3474 while ((y[NE - 2] & 0x8000) == 0)
3483 for (i = 0; i < NDEC + 1; i++)
3485 if ((w[NI - 1] & 0x7) != 0)
3487 /* multiply by 10 */
3500 if (eone[NE - 1] <= u[1])
3512 while (ecmp (eone, w) > 0)
3514 if (ecmp (p, w) >= 0)
3529 /* Find the first (leading) digit. */
3535 digit = equot[NI - 1];
3536 while ((digit == 0) && (ecmp (y, ezero) != 0))
3544 digit = equot[NI - 1];
3552 /* Examine number of digits requested by caller. */
3570 *s++ = (char )digit + '0';
3573 /* Generate digits after the decimal point. */
3574 for (k = 0; k <= ndigs; k++)
3576 /* multiply current number by 10, without normalizing */
3583 *s++ = (char) equot[NI - 1] + '0';
3585 digit = equot[NI - 1];
3588 /* round off the ASCII string */
3591 /* Test for critical rounding case in ASCII output. */
3595 if (ecmp (t, ezero) != 0)
3596 goto roun; /* round to nearest */
3597 if ((*(s - 1) & 1) == 0)
3598 goto doexp; /* round to even */
3600 /* Round up and propagate carry-outs */
3604 /* Carry out to most significant digit? */
3611 /* Most significant digit carries to 10? */
3619 /* Round up and carry out from less significant digits */
3631 sprintf (ss, "e+%d", expon);
3633 sprintf (ss, "e%d", expon);
3635 sprintf (ss, "e%d", expon);
3638 /* copy out the working string */
3641 while (*ss == ' ') /* strip possible leading space */
3643 while ((*s++ = *ss++) != '\0')
3652 ; ASCTOQ.MAC LATEST REV: 11 JAN 84
3655 ; Convert ASCII string to quadruple precision floating point
3657 ; Numeric input is free field decimal number
3658 ; with max of 15 digits with or without
3659 ; decimal point entered as ASCII from teletype.
3660 ; Entering E after the number followed by a second
3661 ; number causes the second number to be interpreted
3662 ; as a power of 10 to be multiplied by the first number
3663 ; (i.e., "scientific" notation).
3666 ; asctoq (string, q);
3669 /* ASCII to single */
3673 unsigned EMUSHORT *y;
3679 /* ASCII to double */
3683 unsigned EMUSHORT *y;
3693 /* ASCII to long double */
3697 unsigned EMUSHORT *y;
3702 /* ASCII to super double */
3706 unsigned EMUSHORT *y;
3708 asctoeg (s, y, NBITS);
3711 /* Space to make a copy of the input string: */
3712 static char lstr[82];
3715 asctoeg (ss, y, oprec)
3717 unsigned EMUSHORT *y;
3720 unsigned EMUSHORT yy[NI], xt[NI], tt[NI];
3721 int esign, decflg, sgnflg, nexp, exp, prec, lost;
3722 int k, trail, c, rndsav;
3724 unsigned EMUSHORT nsign, *p;
3727 /* Copy the input string. */
3729 while (*s == ' ') /* skip leading spaces */
3732 for (k = 0; k < 79; k++)
3734 if ((*sp++ = *s++) == '\0')
3741 rndprc = NBITS; /* Set to full precision */
3754 if ((k >= 0) && (k <= 9))
3756 /* Ignore leading zeros */
3757 if ((prec == 0) && (decflg == 0) && (k == 0))
3759 /* Identify and strip trailing zeros after the decimal point. */
3760 if ((trail == 0) && (decflg != 0))
3763 while ((*sp >= '0') && (*sp <= '9'))
3765 /* Check for syntax error */
3767 if ((c != 'e') && (c != 'E') && (c != '\0')
3768 && (c != '\n') && (c != '\r') && (c != ' ')
3778 /* If enough digits were given to more than fill up the yy register,
3779 * continuing until overflow into the high guard word yy[2]
3780 * guarantees that there will be a roundoff bit at the top
3781 * of the low guard word after normalization.
3786 nexp += 1; /* count digits after decimal point */
3787 eshup1 (yy); /* multiply current number by 10 */
3793 xt[NI - 2] = (unsigned EMUSHORT) k;
3811 case '.': /* decimal point */
3838 mtherr ("asctoe", DOMAIN);
3846 /* Exponent interpretation */
3852 /* check for + or - */
3860 while ((*s >= '0') && (*s <= '9'))
3878 yy[E] = 0x7fff; /* infinity */
3890 /* Pad trailing zeros to minimize power of 10, per IEEE spec. */
3891 while ((nexp > 0) && (yy[2] == 0))
3903 if ((k = enormlz (yy)) > NBITS)
3908 lexp = (EXONE - 1 + NBITS) - k;
3909 emdnorm (yy, lost, 0, lexp, 64);
3910 /* convert to external format */
3913 /* Multiply by 10**nexp. If precision is 64 bits,
3914 * the maximum relative error incurred in forming 10**n
3915 * for 0 <= n <= 324 is 8.2e-20, at 10**180.
3916 * For 0 <= n <= 999, the peak relative error is 1.4e-19 at 10**947.
3917 * For 0 >= n >= -999, it is -1.55e-19 at 10**-435.
3931 { /* Punt. Can't handle this without 2 divides. */
3932 emovi (etens[0], tt);
3939 p = &etens[NTEN][0];
3949 while (exp <= MAXP);
3967 /* Round and convert directly to the destination type */
3969 lexp -= EXONE - 0x3ff;
3970 else if (oprec == 24)
3971 lexp -= EXONE - 0177;
3973 else if (oprec == 56)
3974 lexp -= EXONE - 0201;
3977 emdnorm (yy, k, 0, lexp, 64);
3987 todec (yy, y); /* see etodec.c */
4007 /* y = largest integer not greater than x
4008 * (truncated toward minus infinity)
4010 * unsigned EMUSHORT x[NE], y[NE]
4014 static unsigned EMUSHORT bmask[] =
4037 unsigned EMUSHORT x[], y[];
4039 register unsigned EMUSHORT *p;
4041 unsigned EMUSHORT f[NE];
4043 emov (x, f); /* leave in external format */
4044 expon = (int) f[NE - 1];
4045 e = (expon & 0x7fff) - (EXONE - 1);
4051 /* number of bits to clear out */
4063 /* clear the remaining bits */
4065 /* truncate negatives toward minus infinity */
4068 if ((unsigned EMUSHORT) expon & (unsigned EMUSHORT) 0x8000)
4070 for (i = 0; i < NE - 1; i++)
4082 /* unsigned EMUSHORT x[], s[];
4085 * efrexp (x, exp, s);
4087 * Returns s and exp such that s * 2**exp = x and .5 <= s < 1.
4088 * For example, 1.1 = 0.55 * 2**1
4089 * Handles denormalized numbers properly using long integer exp.
4093 unsigned EMUSHORT x[];
4095 unsigned EMUSHORT s[];
4097 unsigned EMUSHORT xi[NI];
4101 li = (EMULONG) ((EMUSHORT) xi[1]);
4109 *exp = (int) (li - 0x3ffe);
4114 /* unsigned EMUSHORT x[], y[];
4117 * eldexp (x, pwr2, y);
4119 * Returns y = x * 2**pwr2.
4123 unsigned EMUSHORT x[];
4125 unsigned EMUSHORT y[];
4127 unsigned EMUSHORT xi[NI];
4135 emdnorm (xi, i, i, li, 64);
4140 /* c = remainder after dividing b by a
4141 * Least significant integer quotient bits left in equot[].
4145 unsigned EMUSHORT a[], b[], c[];
4147 unsigned EMUSHORT den[NI], num[NI];
4149 if (ecmp (a, ezero) == 0)
4151 mtherr ("eremain", SING);
4157 eiremain (den, num);
4158 /* Sign of remainder = sign of quotient */
4168 unsigned EMUSHORT den[], num[];
4171 unsigned EMUSHORT j;
4174 ld -= enormlz (den);
4176 ln -= enormlz (num);
4180 if (ecmpm (den, num) <= 0)
4194 emdnorm (num, 0, 0, ln, 0);
4199 * Library common error handling routine
4209 * mtherr (fctnam, code);
4215 * This routine may be called to report one of the following
4216 * error conditions (in the include file mconf.h).
4218 * Mnemonic Value Significance
4220 * DOMAIN 1 argument domain error
4221 * SING 2 function singularity
4222 * OVERFLOW 3 overflow range error
4223 * UNDERFLOW 4 underflow range error
4224 * TLOSS 5 total loss of precision
4225 * PLOSS 6 partial loss of precision
4226 * EDOM 33 Unix domain error code
4227 * ERANGE 34 Unix range error code
4229 * The default version of the file prints the function name,
4230 * passed to it by the pointer fctnam, followed by the
4231 * error condition. The display is directed to the standard
4232 * output device. The routine then returns to the calling
4233 * program. Users may wish to modify the program to abort by
4234 * calling exit under severe error conditions such as domain
4237 * Since all error conditions pass control to this function,
4238 * the display may be easily changed, eliminated, or directed
4239 * to an error logging device.
4248 Cephes Math Library Release 2.0: April, 1987
4249 Copyright 1984, 1987 by Stephen L. Moshier
4250 Direct inquiries to 30 Frost Street, Cambridge, MA 02140
4253 /* include "mconf.h" */
4255 /* Notice: the order of appearance of the following
4256 * messages is bound to the error codes defined
4259 static char *ermsg[7] =
4261 "unknown", /* error code 0 */
4262 "domain", /* error code 1 */
4263 "singularity", /* et seq. */
4266 "total loss of precision",
4267 "partial loss of precision"
4280 /* Display string passed by calling program,
4281 * which is supposed to be the name of the
4282 * function in which the error occurred.
4285 /* Display error message defined
4286 * by the code argument.
4288 if ((code <= 0) || (code >= 6))
4290 sprintf (errstr, "\n%s %s error\n", name, ermsg[code]);
4293 /* Set global error message word */
4296 /* Return to calling
4301 /* Here is etodec.c .
4306 ; convert DEC double precision to e type
4313 unsigned EMUSHORT *d;
4314 unsigned EMUSHORT *e;
4316 unsigned EMUSHORT y[NI];
4317 register unsigned EMUSHORT r, *p;
4319 ecleaz (y); /* start with a zero */
4320 p = y; /* point to our number */
4321 r = *d; /* get DEC exponent word */
4322 if (*d & (unsigned int) 0x8000)
4323 *p = 0xffff; /* fill in our sign */
4324 ++p; /* bump pointer to our exponent word */
4325 r &= 0x7fff; /* strip the sign bit */
4326 if (r == 0) /* answer = 0 if high order DEC word = 0 */
4330 r >>= 7; /* shift exponent word down 7 bits */
4331 r += EXONE - 0201; /* subtract DEC exponent offset */
4332 /* add our e type exponent offset */
4333 *p++ = r; /* to form our exponent */
4335 r = *d++; /* now do the high order mantissa */
4336 r &= 0177; /* strip off the DEC exponent and sign bits */
4337 r |= 0200; /* the DEC understood high order mantissa bit */
4338 *p++ = r; /* put result in our high guard word */
4340 *p++ = *d++; /* fill in the rest of our mantissa */
4344 eshdn8 (y); /* shift our mantissa down 8 bits */
4352 ; convert e type to DEC double precision
4358 static unsigned EMUSHORT decbit[NI] = {0, 0, 0, 0, 0, 0, 0200, 0};
4362 unsigned EMUSHORT *x, *d;
4364 unsigned EMUSHORT xi[NI];
4365 register unsigned EMUSHORT r;
4373 if (r < (EXONE - 128))
4376 if ((i & 0200) != 0)
4378 if ((i & 0377) == 0200)
4380 if ((i & 0400) != 0)
4382 /* check all less significant bits */
4383 for (j = M + 5; j < NI; j++)
4432 unsigned EMUSHORT *x, *d;
4434 unsigned EMUSHORT xi[NI];
4439 exp = (EMULONG) xi[E] - (EXONE - 0201); /* adjust exponent for offsets */
4440 /* round off to nearest or even */
4443 emdnorm (xi, 0, 0, exp, 64);
4450 unsigned EMUSHORT *x, *y;
4452 unsigned EMUSHORT i;
4453 unsigned EMUSHORT *p;
4492 #endif /* EMU_NON_COMPILE not defined */