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 */
997 /* e type constants used by high precision check routines */
999 /*include "ehead.h"*/
1001 unsigned EMUSHORT ezero[NE] =
1003 0, 0000000, 0000000, 0000000, 0000000, 0000000,};
1004 extern unsigned EMUSHORT ezero[];
1007 unsigned EMUSHORT ehalf[NE] =
1009 0, 0000000, 0000000, 0000000, 0100000, 0x3ffe,};
1010 extern unsigned EMUSHORT ehalf[];
1013 unsigned EMUSHORT eone[NE] =
1015 0, 0000000, 0000000, 0000000, 0100000, 0x3fff,};
1016 extern unsigned EMUSHORT eone[];
1019 unsigned EMUSHORT etwo[NE] =
1021 0, 0000000, 0000000, 0000000, 0100000, 0040000,};
1022 extern unsigned EMUSHORT etwo[];
1025 unsigned EMUSHORT e32[NE] =
1027 0, 0000000, 0000000, 0000000, 0100000, 0040004,};
1028 extern unsigned EMUSHORT e32[];
1030 /* 6.93147180559945309417232121458176568075500134360255E-1 */
1031 unsigned EMUSHORT elog2[NE] =
1033 0xc9e4, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1034 extern unsigned EMUSHORT elog2[];
1036 /* 1.41421356237309504880168872420969807856967187537695E0 */
1037 unsigned EMUSHORT esqrt2[NE] =
1039 0x597e, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1040 extern unsigned EMUSHORT esqrt2[];
1043 * 1.12837916709551257389615890312154517168810125865800E0 */
1044 unsigned EMUSHORT eoneopi[NE] =
1046 0x71d5, 0x688d, 0012333, 0135202, 0110156, 0x3fff,};
1047 extern unsigned EMUSHORT eoneopi[];
1049 /* 3.14159265358979323846264338327950288419716939937511E0 */
1050 unsigned EMUSHORT epi[NE] =
1052 0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1053 extern unsigned EMUSHORT epi[];
1055 /* 5.7721566490153286060651209008240243104215933593992E-1 */
1056 unsigned EMUSHORT eeul[NE] =
1058 0xd1be, 0xc7a4, 0076660, 0063743, 0111704, 0x3ffe,};
1059 extern unsigned EMUSHORT eeul[];
1068 /* Control register for rounding precision.
1069 * This can be set to 80 (if NE=6), 64, 56, 53, or 24 bits.
1074 void eaddm (), esubm (), emdnorm (), asctoeg ();
1075 static void toe24 (), toe53 (), toe64 ();
1076 void eremain (), einit (), eiremain ();
1077 int ecmpm (), edivm (), emulm ();
1078 void emovi (), emovo (), emovz (), ecleaz (), ecleazs (), eadd1 ();
1079 void etodec (), todec (), dectoe ();
1090 ; Clear out entire external format number.
1092 ; unsigned EMUSHORT x[];
1098 register unsigned EMUSHORT *x;
1102 for (i = 0; i < NE; i++)
1108 /* Move external format number from a to b.
1115 register unsigned EMUSHORT *a, *b;
1119 for (i = 0; i < NE; i++)
1125 ; Absolute value of external format number
1133 unsigned EMUSHORT x[]; /* x is the memory address of a short */
1136 x[NE - 1] &= 0x7fff; /* sign is top bit of last word of external format */
1143 ; Negate external format number
1145 ; unsigned EMUSHORT x[NE];
1151 unsigned EMUSHORT x[];
1154 x[NE - 1] ^= 0x8000; /* Toggle the sign bit */
1159 /* Return 1 if external format number is negative,
1164 unsigned EMUSHORT x[];
1167 if (x[NE - 1] & 0x8000)
1174 /* Return 1 if external format number has maximum possible exponent,
1179 unsigned EMUSHORT x[];
1182 if ((x[NE - 1] & 0x7fff) == 0x7fff)
1190 ; Fill entire number, including exponent and significand, with
1191 ; largest possible number. These programs implement a saturation
1192 ; value that is an ordinary, legal number. A special value
1193 ; "infinity" may also be implemented; this would require tests
1194 ; for that value and implementation of special rules for arithmetic
1195 ; operations involving inifinity.
1200 register unsigned EMUSHORT *x;
1205 for (i = 0; i < NE - 1; i++)
1209 for (i = 0; i < NE - 1; i++)
1234 /* Move in external format number,
1235 * converting it to internal format.
1239 unsigned EMUSHORT *a, *b;
1241 register unsigned EMUSHORT *p, *q;
1245 p = a + (NE - 1); /* point to last word of external number */
1246 /* get the sign bit */
1251 /* get the exponent */
1253 *q++ &= 0x7fff; /* delete the sign bit */
1255 if ((*(q - 1) & 0x7fff) == 0x7fff)
1257 for (i = 2; i < NI; i++)
1262 /* clear high guard word */
1264 /* move in the significand */
1265 for (i = 0; i < NE - 1; i++)
1267 /* clear low guard word */
1272 /* Move internal format number out,
1273 * converting it to external format.
1277 unsigned EMUSHORT *a, *b;
1279 register unsigned EMUSHORT *p, *q;
1280 unsigned EMUSHORT i;
1283 q = b + (NE - 1); /* point to output exponent */
1284 /* combine sign and exponent */
1287 *q-- = *p++ | 0x8000;
1291 if (*(p - 1) == 0x7fff)
1297 /* skip over guard word */
1299 /* move the significand */
1300 for (i = 0; i < NE - 1; i++)
1307 /* Clear out internal format number.
1312 register unsigned EMUSHORT *xi;
1316 for (i = 0; i < NI; i++)
1321 /* same, but don't touch the sign. */
1325 register unsigned EMUSHORT *xi;
1330 for (i = 0; i < NI - 1; i++)
1336 /* Move internal format number from a to b.
1340 register unsigned EMUSHORT *a, *b;
1344 for (i = 0; i < NI - 1; i++)
1346 /* clear low guard word */
1352 ; Compare significands of numbers in internal format.
1353 ; Guard words are included in the comparison.
1355 ; unsigned EMUSHORT a[NI], b[NI];
1358 ; for the significands:
1359 ; returns +1 if a > b
1365 register unsigned EMUSHORT *a, *b;
1369 a += M; /* skip up to significand area */
1371 for (i = M; i < NI; i++)
1379 if (*(--a) > *(--b))
1387 ; Shift significand down by 1 bit
1392 register unsigned EMUSHORT *x;
1394 register unsigned EMUSHORT bits;
1397 x += M; /* point to significand area */
1400 for (i = M; i < NI; i++)
1415 ; Shift significand up by 1 bit
1420 register unsigned EMUSHORT *x;
1422 register unsigned EMUSHORT bits;
1428 for (i = M; i < NI; i++)
1443 ; Shift significand down by 8 bits
1448 register unsigned EMUSHORT *x;
1450 register unsigned EMUSHORT newbyt, oldbyt;
1455 for (i = M; i < NI; i++)
1466 ; Shift significand up by 8 bits
1471 register unsigned EMUSHORT *x;
1474 register unsigned EMUSHORT newbyt, oldbyt;
1479 for (i = M; i < NI; i++)
1490 ; Shift significand up by 16 bits
1495 register unsigned EMUSHORT *x;
1498 register unsigned EMUSHORT *p;
1503 for (i = M; i < NI - 1; i++)
1510 ; Shift significand down by 16 bits
1515 register unsigned EMUSHORT *x;
1518 register unsigned EMUSHORT *p;
1523 for (i = M; i < NI - 1; i++)
1536 unsigned EMUSHORT *x, *y;
1538 register unsigned EMULONG a;
1545 for (i = M; i < NI; i++)
1547 a = (unsigned EMULONG) (*x) + (unsigned EMULONG) (*y) + carry;
1552 *y = (unsigned EMUSHORT) a;
1559 ; Subtract significands
1565 unsigned EMUSHORT *x, *y;
1574 for (i = M; i < NI; i++)
1576 a = (unsigned EMULONG) (*y) - (unsigned EMULONG) (*x) - carry;
1581 *y = (unsigned EMUSHORT) a;
1588 /* Divide significands */
1590 static unsigned EMUSHORT equot[NI];
1594 unsigned EMUSHORT den[], num[];
1597 register unsigned EMUSHORT *p, *q;
1598 unsigned EMUSHORT j;
1604 for (i = M; i < NI; i++)
1609 /* Use faster compare and subtraction if denominator
1610 * has only 15 bits of significance.
1615 for (i = M + 3; i < NI; i++)
1620 if ((den[M + 1] & 1) != 0)
1628 for (i = 0; i < NBITS + 2; i++)
1646 /* The number of quotient bits to calculate is
1647 * NBITS + 1 scaling guard bit + 1 roundoff bit.
1652 for (i = 0; i < NBITS + 2; i++)
1654 if (ecmpm (den, num) <= 0)
1657 j = 1; /* quotient bit = 1 */
1671 /* test for nonzero remainder after roundoff bit */
1674 for (i = M; i < NI; i++)
1682 for (i = 0; i < NI; i++)
1688 /* Multiply significands */
1691 unsigned EMUSHORT a[], b[];
1693 unsigned EMUSHORT *p, *q;
1698 for (i = M; i < NI; i++)
1703 while (*p == 0) /* significand is not supposed to be all zero */
1708 if ((*p & 0xff) == 0)
1716 for (i = 0; i < k; i++)
1720 /* remember if there were any nonzero bits shifted out */
1727 for (i = 0; i < NI; i++)
1730 /* return flag for lost nonzero bits */
1737 * Normalize and round off.
1739 * The internal format number to be rounded is "s".
1740 * Input "lost" indicates whether or not the number is exact.
1741 * This is the so-called sticky bit.
1743 * Input "subflg" indicates whether the number was obtained
1744 * by a subtraction operation. In that case if lost is nonzero
1745 * then the number is slightly smaller than indicated.
1747 * Input "exp" is the biased exponent, which may be negative.
1748 * the exponent field of "s" is ignored but is replaced by
1749 * "exp" as adjusted by normalization and rounding.
1751 * Input "rcntrl" is the rounding control.
1754 static int rlast = -1;
1756 static unsigned EMUSHORT rmsk = 0;
1757 static unsigned EMUSHORT rmbit = 0;
1758 static unsigned EMUSHORT rebit = 0;
1760 static unsigned EMUSHORT rbit[NI];
1763 emdnorm (s, lost, subflg, exp, rcntrl)
1764 unsigned EMUSHORT s[];
1771 unsigned EMUSHORT r;
1776 /* a blank significand could mean either zero or infinity. */
1789 if ((j > NBITS) && (exp < 32767))
1797 if (exp > (EMULONG) (-NBITS - 1))
1810 /* Round off, unless told not to by rcntrl. */
1813 /* Set up rounding parameters if the control register changed. */
1814 if (rndprc != rlast)
1821 rw = NI - 1; /* low guard word */
1836 /* For DEC arithmetic */
1885 /* These tests assume NI = 8 */
1905 if ((r & rmbit) != 0)
1910 { /* round to even */
1911 if ((s[re] & rebit) == 0)
1923 if ((rndprc < 64) && (exp <= 0))
1928 { /* overflow on roundoff */
1941 for (i = 2; i < NI - 1; i++)
1944 warning ("floating point overflow");
1948 for (i = M + 1; i < NI - 1; i++)
1966 s[1] = (unsigned EMUSHORT) exp;
1972 ; Subtract external format numbers.
1974 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
1975 ; esub (a, b, c); c = b - a
1978 static int subflg = 0;
1982 unsigned EMUSHORT *a, *b, *c;
1993 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
1994 ; eadd (a, b, c); c = b + a
1998 unsigned EMUSHORT *a, *b, *c;
2007 unsigned EMUSHORT *a, *b, *c;
2009 unsigned EMUSHORT ai[NI], bi[NI], ci[NI];
2011 EMULONG lt, lta, ltb;
2032 /* compare exponents */
2037 { /* put the larger number in bi */
2047 if (lt < (EMULONG) (-NBITS - 1))
2048 goto done; /* answer same as larger addend */
2050 lost = eshift (ai, k); /* shift the smaller number down */
2054 /* exponents were the same, so must compare significands */
2057 { /* the numbers are identical in magnitude */
2058 /* if different signs, result is zero */
2064 /* if same sign, result is double */
2065 /* double denomalized tiny number */
2066 if ((bi[E] == 0) && ((bi[3] & 0x8000) == 0))
2071 /* add 1 to exponent unless both are zero! */
2072 for (j = 1; j < NI - 1; j++)
2076 /* This could overflow, but let emovo take care of that. */
2081 bi[E] = (unsigned EMUSHORT) ltb;
2085 { /* put the larger number in bi */
2101 emdnorm (bi, lost, subflg, ltb, 64);
2112 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2113 ; ediv (a, b, c); c = b / a
2117 unsigned EMUSHORT *a, *b, *c;
2119 unsigned EMUSHORT ai[NI], bi[NI];
2121 EMULONG lt, lta, ltb;
2126 if (eisneg (a) ^ eisneg (b))
2127 *(c + (NE - 1)) = 0x8000;
2129 *(c + (NE - 1)) = 0;
2144 { /* See if numerator is zero. */
2145 for (i = 1; i < NI - 1; i++)
2149 ltb -= enormlz (bi);
2159 { /* possible divide by zero */
2160 for (i = 1; i < NI - 1; i++)
2164 lta -= enormlz (ai);
2169 *(c + (NE - 1)) = 0;
2171 *(c + (NE - 1)) = 0x8000;
2173 mtherr ("ediv", SING);
2179 /* calculate exponent */
2180 lt = ltb - lta + EXONE;
2181 emdnorm (bi, i, 0, lt, 64);
2195 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2196 ; emul (a, b, c); c = b * a
2200 unsigned EMUSHORT *a, *b, *c;
2202 unsigned EMUSHORT ai[NI], bi[NI];
2204 EMULONG lt, lta, ltb;
2207 if (eisinf (a) || eisinf (b))
2209 if (eisneg (a) ^ eisneg (b))
2210 *(c + (NE - 1)) = 0x8000;
2212 *(c + (NE - 1)) = 0;
2223 for (i = 1; i < NI - 1; i++)
2227 lta -= enormlz (ai);
2238 for (i = 1; i < NI - 1; i++)
2242 ltb -= enormlz (bi);
2251 /* Multiply significands */
2253 /* calculate exponent */
2254 lt = lta + ltb - (EXONE - 1);
2255 emdnorm (bi, j, 0, lt, 64);
2256 /* calculate sign of product */
2268 ; Convert IEEE double precision to e type
2270 ; unsigned EMUSHORT x[N+2];
2275 unsigned EMUSHORT *e, *y;
2279 dectoe (e, y); /* see etodec.c */
2283 register unsigned EMUSHORT r;
2284 register unsigned EMUSHORT *p;
2285 unsigned EMUSHORT yy[NI];
2288 denorm = 0; /* flag if denormalized number */
2297 yy[M] = (r & 0x0f) | 0x10;
2298 r &= ~0x800f; /* strip sign and 4 significand bits */
2309 /* If zero exponent, then the significand is denormalized.
2310 * So, take back the understood high significand bit. */
2332 { /* if zero exponent, then normalize the significand */
2333 if ((k = enormlz (yy)) > NBITS)
2336 yy[E] -= (unsigned EMUSHORT) (k - 1);
2339 #endif /* not DEC */
2344 unsigned EMUSHORT *e, *y;
2346 unsigned EMUSHORT yy[NI];
2347 unsigned EMUSHORT *p, *q;
2351 for (i = 0; i < NE - 5; i++)
2354 for (i = 0; i < 5; i++)
2358 for (i = 0; i < 5; i++)
2362 p = &yy[0] + (NE - 1);
2365 for (i = 0; i < 4; i++)
2379 for (i = 0; i < NE; i++)
2385 ; Convert IEEE single precision to e type
2387 ; unsigned EMUSHORT x[N+2];
2392 unsigned EMUSHORT *e, *y;
2394 register unsigned EMUSHORT r;
2395 register unsigned EMUSHORT *p;
2396 unsigned EMUSHORT yy[NI];
2399 denorm = 0; /* flag if denormalized number */
2411 yy[M] = (r & 0x7f) | 0200;
2412 r &= ~0x807f; /* strip sign and 7 significand bits */
2423 /* If zero exponent, then the significand is denormalized.
2424 * So, take back the understood high significand bit. */
2445 { /* if zero exponent, then normalize the significand */
2446 if ((k = enormlz (yy)) > NBITS)
2449 yy[E] -= (unsigned EMUSHORT) (k - 1);
2457 unsigned EMUSHORT *x, *e;
2459 unsigned EMUSHORT xi[NI];
2464 /* adjust exponent for offset */
2465 exp = (EMULONG) xi[E];
2470 /* round off to nearest or even */
2473 emdnorm (xi, 0, 0, exp, 64);
2479 /* move out internal format to ieee long double */
2482 unsigned EMUSHORT *a, *b;
2484 register unsigned EMUSHORT *p, *q;
2485 unsigned EMUSHORT i;
2491 q = b + 4; /* point to output exponent */
2492 #if LONG_DOUBLE_TYPE_SIZE == 96
2493 /* Clear the last two bytes of 12-byte Intel format */
2498 /* combine sign and exponent */
2502 *q++ = *p++ | 0x8000;
2508 *q-- = *p++ | 0x8000;
2512 /* skip over guard word */
2514 /* move the significand */
2516 for (i = 0; i < 4; i++)
2519 for (i = 0; i < 4; i++)
2526 ; e type to IEEE double precision
2528 ; unsigned EMUSHORT x[NE];
2536 unsigned EMUSHORT *x, *e;
2538 etodec (x, e); /* see etodec.c */
2543 unsigned EMUSHORT *x, *y;
2552 unsigned EMUSHORT *x, *e;
2554 unsigned EMUSHORT xi[NI];
2559 /* adjust exponent for offsets */
2560 exp = (EMULONG) xi[E] - (EXONE - 0x3ff);
2565 /* round off to nearest or even */
2568 emdnorm (xi, 0, 0, exp, 64);
2577 unsigned EMUSHORT *x, *y;
2579 unsigned EMUSHORT i;
2580 unsigned EMUSHORT *p;
2587 *y = 0; /* output high order */
2589 *y = 0x8000; /* output sign bit */
2592 if (i >= (unsigned int) 2047)
2593 { /* Saturate at largest number less than infinity. */
2608 *y |= (unsigned EMUSHORT) 0x7fef;
2632 i |= *p++ & (unsigned EMUSHORT) 0x0f; /* *p = xi[M] */
2633 *y |= (unsigned EMUSHORT) i; /* high order output already has sign bit set */
2647 #endif /* not DEC */
2652 ; e type to IEEE single precision
2654 ; unsigned EMUSHORT x[N+2];
2659 unsigned EMUSHORT *x, *e;
2662 unsigned EMUSHORT xi[NI];
2666 /* adjust exponent for offsets */
2667 exp = (EMULONG) xi[E] - (EXONE - 0177);
2672 /* round off to nearest or even */
2675 emdnorm (xi, 0, 0, exp, 64);
2683 unsigned EMUSHORT *x, *y;
2685 unsigned EMUSHORT i;
2686 unsigned EMUSHORT *p;
2695 *y = 0; /* output high order */
2697 *y = 0x8000; /* output sign bit */
2700 /* Handle overflow cases. */
2704 *y |= (unsigned EMUSHORT) 0x7f80;
2715 #else /* no INFINITY */
2716 *y |= (unsigned EMUSHORT) 0x7f7f;
2730 #endif /* no INFINITY */
2742 i |= *p++ & (unsigned EMUSHORT) 0x7f; /* *p = xi[M] */
2743 *y |= i; /* high order output already has sign bit set */
2757 /* Compare two e type numbers.
2759 * unsigned EMUSHORT a[NE], b[NE];
2762 * returns +1 if a > b
2768 unsigned EMUSHORT *a, *b;
2770 unsigned EMUSHORT ai[NI], bi[NI];
2771 register unsigned EMUSHORT *p, *q;
2781 { /* the signs are different */
2783 for (i = 1; i < NI - 1; i++)
2797 /* both are the same sign */
2812 return (0); /* equality */
2818 if (*(--p) > *(--q))
2819 return (msign); /* p is bigger */
2821 return (-msign); /* p is littler */
2827 /* Find nearest integer to x = floor (x + 0.5)
2829 * unsigned EMUSHORT x[NE], y[NE]
2834 unsigned EMUSHORT *x, *y;
2844 ; convert long integer to e type
2847 ; unsigned EMUSHORT x[NE];
2849 ; note &l is the memory address of l
2853 long *lp; /* lp is the memory address of a long integer */
2854 unsigned EMUSHORT *y; /* y is the address of a short */
2856 unsigned EMUSHORT yi[NI];
2863 /* make it positive */
2864 ll = (unsigned long) (-(*lp));
2865 yi[0] = 0xffff; /* put correct sign in the e type number */
2869 ll = (unsigned long) (*lp);
2871 /* move the long integer to yi significand area */
2872 yi[M] = (unsigned EMUSHORT) (ll >> 16);
2873 yi[M + 1] = (unsigned EMUSHORT) ll;
2875 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
2876 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
2877 ecleaz (yi); /* it was zero */
2879 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
2880 emovo (yi, y); /* output the answer */
2884 ; convert unsigned long integer to e type
2887 ; unsigned EMUSHORT x[NE];
2889 ; note &l is the memory address of l
2893 unsigned long *lp; /* lp is the memory address of a long integer */
2894 unsigned EMUSHORT *y; /* y is the address of a short */
2896 unsigned EMUSHORT yi[NI];
2903 /* move the long integer to ayi significand area */
2904 yi[M] = (unsigned EMUSHORT) (ll >> 16);
2905 yi[M + 1] = (unsigned EMUSHORT) ll;
2907 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
2908 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
2909 ecleaz (yi); /* it was zero */
2911 yi[E] -= (unsigned EMUSHORT) k; /* subtract shift count from exponent */
2912 emovo (yi, y); /* output the answer */
2917 ; Find long integer and fractional parts
2920 ; unsigned EMUSHORT x[NE], frac[NE];
2921 ; xifrac (x, &i, frac);
2923 The integer output has the sign of the input. The fraction is
2924 the positive fractional part of abs (x).
2928 unsigned EMUSHORT *x;
2930 unsigned EMUSHORT *frac;
2932 unsigned EMUSHORT xi[NI];
2936 k = (int) xi[E] - (EXONE - 1);
2939 /* if exponent <= 0, integer = 0 and real output is fraction */
2944 if (k > (HOST_BITS_PER_LONG - 1))
2947 ; long integer overflow: output large integer
2948 ; and correct fraction
2951 *i = ((unsigned long) 1) << (HOST_BITS_PER_LONG - 1);
2953 *i = (((unsigned long) 1) << (HOST_BITS_PER_LONG - 1)) - 1;
2956 warning ("overflow on truncation to integer");
2963 ; shift more than 16 bits: shift up k-16, output the integer,
2964 ; then complete the shift to get the fraction.
2969 *i = (long) (((unsigned long) xi[M] << 16) | xi[M + 1]);
2974 /* shift not more than 16 bits */
2976 *i = (long) xi[M] & 0xffff;
2987 if ((k = enormlz (xi)) > NBITS)
2990 xi[E] -= (unsigned EMUSHORT) k;
2997 ; Find unsigned long integer and fractional parts
3000 ; unsigned EMUSHORT x[NE], frac[NE];
3001 ; xifrac (x, &i, frac);
3003 A negative e type input yields integer output = 0
3004 but correct fraction.
3007 euifrac (x, i, frac)
3008 unsigned EMUSHORT *x;
3010 unsigned EMUSHORT *frac;
3012 unsigned EMUSHORT xi[NI];
3016 k = (int) xi[E] - (EXONE - 1);
3019 /* if exponent <= 0, integer = 0 and argument is fraction */
3027 ; long integer overflow: output large integer
3028 ; and correct fraction
3033 warning ("overflow on truncation to unsigned integer");
3040 ; shift more than 16 bits: shift up k-16, output the integer,
3041 ; then complete the shift to get the fraction.
3046 *i = (long) (((unsigned long) xi[M] << 16) | xi[M + 1]);
3051 /* shift not more than 16 bits */
3053 *i = (long) xi[M] & 0xffff;
3063 if ((k = enormlz (xi)) > NBITS)
3066 xi[E] -= (unsigned EMUSHORT) k;
3076 ; Shifts significand area up or down by the number of bits
3077 ; given by the variable sc.
3081 unsigned EMUSHORT *x;
3084 unsigned EMUSHORT lost;
3085 unsigned EMUSHORT *p;
3098 lost |= *p; /* remember lost bits */
3139 return ((int) lost);
3147 ; Shift normalizes the significand area pointed to by argument
3148 ; shift count (up = positive) is returned.
3152 unsigned EMUSHORT x[];
3154 register unsigned EMUSHORT *p;
3163 return (0); /* already normalized */
3168 /* With guard word, there are NBITS+16 bits available.
3169 * return true if all are zero.
3174 /* see if high byte is zero */
3175 while ((*p & 0xff00) == 0)
3180 /* now shift 1 bit at a time */
3181 while ((*p & 0x8000) == 0)
3187 mtherr ("enormlz", UNDERFLOW);
3193 /* Normalize by shifting down out of the high guard word
3194 of the significand */
3209 mtherr ("enormlz", OVERFLOW);
3219 /* Convert e type number to decimal format ASCII string.
3220 * The constants are for 64 bit precision.
3226 static unsigned EMUSHORT etens[NTEN + 1][NE] =
3228 {0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
3229 {0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
3230 {0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
3231 {0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
3232 {0xddbc, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
3233 {0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
3234 {0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
3235 {0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
3236 {0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
3237 {0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
3238 {0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
3239 {0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
3240 {0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
3243 static unsigned EMUSHORT emtens[NTEN + 1][NE] =
3245 {0x2de4, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
3246 {0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
3247 {0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
3248 {0x7133, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
3249 {0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
3250 {0xac7d, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
3251 {0x3f24, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
3252 {0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
3253 {0x4c2f, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
3254 {0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
3255 {0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
3256 {0x3d71, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
3257 {0xcccd, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
3261 e24toasc (x, string, ndigs)
3262 unsigned EMUSHORT x[];
3266 unsigned EMUSHORT w[NI];
3270 if ((x[1] & 0x7f80) == 0x7f80)
3272 if ((x[0] & 0x7f80) == 0x7f80)
3280 sprintf (string, " -Infinity ");
3282 sprintf (string, " Infinity ");
3287 etoasc (w, string, ndigs);
3292 e53toasc (x, string, ndigs)
3293 unsigned EMUSHORT x[];
3297 unsigned EMUSHORT w[NI];
3301 if ((x[3] & 0x7ff0) == 0x7ff0)
3303 if ((x[0] & 0x7ff0) == 0x7ff0)
3311 sprintf (string, " -Infinity ");
3313 sprintf (string, " Infinity ");
3318 etoasc (w, string, ndigs);
3323 e64toasc (x, string, ndigs)
3324 unsigned EMUSHORT x[];
3328 unsigned EMUSHORT w[NI];
3332 if ((x[4] & 0x7fff) == 0x7fff)
3334 if ((x[0] & 0x7fff) == 0x7fff)
3342 sprintf (string, " -Infinity ");
3344 sprintf (string, " Infinity ");
3349 etoasc (w, string, ndigs);
3353 static char wstring[80]; /* working storage for ASCII output */
3356 etoasc (x, string, ndigs)
3357 unsigned EMUSHORT x[];
3362 unsigned EMUSHORT y[NI], t[NI], u[NI], w[NI];
3363 unsigned EMUSHORT *p, *r, *ten;
3364 unsigned EMUSHORT sign;
3365 int i, j, k, expon, rndsav;
3367 unsigned EMUSHORT m;
3371 while ((*s++ = *ss++) != '\0')
3374 rndprc = NBITS; /* set to full precision */
3375 emov (x, y); /* retain external format */
3376 if (y[NE - 1] & 0x8000)
3379 y[NE - 1] &= 0x7fff;
3386 ten = &etens[NTEN][0];
3388 /* Test for zero exponent */
3391 for (k = 0; k < NE - 1; k++)
3394 goto tnzro; /* denormalized number */
3396 goto isone; /* legal all zeros */
3400 /* Test for infinity. Don't bother with illegal infinities.
3402 if (y[NE - 1] == 0x7fff)
3405 sprintf (wstring, " -Infinity ");
3407 sprintf (wstring, " Infinity ");
3411 /* Test for exponent nonzero but significand denormalized.
3412 * This is an error condition.
3414 if ((y[NE - 1] != 0) && ((y[NE - 2] & 0x8000) == 0))
3416 mtherr ("etoasc", DOMAIN);
3417 sprintf (wstring, "NaN");
3421 /* Compare to 1.0 */
3427 { /* Number is greater than 1 */
3428 /* Convert significand to an integer and strip trailing decimal zeros. */
3430 u[NE - 1] = EXONE + NBITS - 1;
3432 p = &etens[NTEN - 4][0];
3438 for (j = 0; j < NE - 1; j++)
3451 /* Rescale from integer significand */
3452 u[NE - 1] += y[NE - 1] - (unsigned int) (EXONE + NBITS - 1);
3454 /* Find power of 10 */
3458 while (ecmp (ten, u) <= 0)
3460 if (ecmp (p, u) <= 0)
3473 { /* Number is less than 1.0 */
3474 /* Pad significand with trailing decimal zeros. */
3477 while ((y[NE - 2] & 0x8000) == 0)
3486 for (i = 0; i < NDEC + 1; i++)
3488 if ((w[NI - 1] & 0x7) != 0)
3490 /* multiply by 10 */
3503 if (eone[NE - 1] <= u[1])
3515 while (ecmp (eone, w) > 0)
3517 if (ecmp (p, w) >= 0)
3532 /* Find the first (leading) digit. */
3538 digit = equot[NI - 1];
3539 while ((digit == 0) && (ecmp (y, ezero) != 0))
3547 digit = equot[NI - 1];
3555 /* Examine number of digits requested by caller. */
3573 *s++ = (char )digit + '0';
3576 /* Generate digits after the decimal point. */
3577 for (k = 0; k <= ndigs; k++)
3579 /* multiply current number by 10, without normalizing */
3586 *s++ = (char) equot[NI - 1] + '0';
3588 digit = equot[NI - 1];
3591 /* round off the ASCII string */
3594 /* Test for critical rounding case in ASCII output. */
3598 if (ecmp (t, ezero) != 0)
3599 goto roun; /* round to nearest */
3600 if ((*(s - 1) & 1) == 0)
3601 goto doexp; /* round to even */
3603 /* Round up and propagate carry-outs */
3607 /* Carry out to most significant digit? */
3614 /* Most significant digit carries to 10? */
3622 /* Round up and carry out from less significant digits */
3634 sprintf (ss, "e+%d", expon);
3636 sprintf (ss, "e%d", expon);
3638 sprintf (ss, "e%d", expon);
3641 /* copy out the working string */
3644 while (*ss == ' ') /* strip possible leading space */
3646 while ((*s++ = *ss++) != '\0')
3655 ; ASCTOQ.MAC LATEST REV: 11 JAN 84
3658 ; Convert ASCII string to quadruple precision floating point
3660 ; Numeric input is free field decimal number
3661 ; with max of 15 digits with or without
3662 ; decimal point entered as ASCII from teletype.
3663 ; Entering E after the number followed by a second
3664 ; number causes the second number to be interpreted
3665 ; as a power of 10 to be multiplied by the first number
3666 ; (i.e., "scientific" notation).
3669 ; asctoq (string, q);
3672 /* ASCII to single */
3676 unsigned EMUSHORT *y;
3682 /* ASCII to double */
3686 unsigned EMUSHORT *y;
3696 /* ASCII to long double */
3700 unsigned EMUSHORT *y;
3705 /* ASCII to super double */
3709 unsigned EMUSHORT *y;
3711 asctoeg (s, y, NBITS);
3714 /* Space to make a copy of the input string: */
3715 static char lstr[82];
3718 asctoeg (ss, y, oprec)
3720 unsigned EMUSHORT *y;
3723 unsigned EMUSHORT yy[NI], xt[NI], tt[NI];
3724 int esign, decflg, sgnflg, nexp, exp, prec, lost;
3725 int k, trail, c, rndsav;
3727 unsigned EMUSHORT nsign, *p;
3730 /* Copy the input string. */
3732 while (*s == ' ') /* skip leading spaces */
3735 for (k = 0; k < 79; k++)
3737 if ((*sp++ = *s++) == '\0')
3744 rndprc = NBITS; /* Set to full precision */
3757 if ((k >= 0) && (k <= 9))
3759 /* Ignore leading zeros */
3760 if ((prec == 0) && (decflg == 0) && (k == 0))
3762 /* Identify and strip trailing zeros after the decimal point. */
3763 if ((trail == 0) && (decflg != 0))
3766 while ((*sp >= '0') && (*sp <= '9'))
3768 /* Check for syntax error */
3770 if ((c != 'e') && (c != 'E') && (c != '\0')
3771 && (c != '\n') && (c != '\r') && (c != ' ')
3781 /* If enough digits were given to more than fill up the yy register,
3782 * continuing until overflow into the high guard word yy[2]
3783 * guarantees that there will be a roundoff bit at the top
3784 * of the low guard word after normalization.
3789 nexp += 1; /* count digits after decimal point */
3790 eshup1 (yy); /* multiply current number by 10 */
3796 xt[NI - 2] = (unsigned EMUSHORT) k;
3814 case '.': /* decimal point */
3841 mtherr ("asctoe", DOMAIN);
3849 /* Exponent interpretation */
3855 /* check for + or - */
3863 while ((*s >= '0') && (*s <= '9'))
3881 yy[E] = 0x7fff; /* infinity */
3893 /* Pad trailing zeros to minimize power of 10, per IEEE spec. */
3894 while ((nexp > 0) && (yy[2] == 0))
3906 if ((k = enormlz (yy)) > NBITS)
3911 lexp = (EXONE - 1 + NBITS) - k;
3912 emdnorm (yy, lost, 0, lexp, 64);
3913 /* convert to external format */
3916 /* Multiply by 10**nexp. If precision is 64 bits,
3917 * the maximum relative error incurred in forming 10**n
3918 * for 0 <= n <= 324 is 8.2e-20, at 10**180.
3919 * For 0 <= n <= 999, the peak relative error is 1.4e-19 at 10**947.
3920 * For 0 >= n >= -999, it is -1.55e-19 at 10**-435.
3934 { /* Punt. Can't handle this without 2 divides. */
3935 emovi (etens[0], tt);
3942 p = &etens[NTEN][0];
3952 while (exp <= MAXP);
3970 /* Round and convert directly to the destination type */
3972 lexp -= EXONE - 0x3ff;
3973 else if (oprec == 24)
3974 lexp -= EXONE - 0177;
3976 else if (oprec == 56)
3977 lexp -= EXONE - 0201;
3980 emdnorm (yy, k, 0, lexp, 64);
3990 todec (yy, y); /* see etodec.c */
4010 /* y = largest integer not greater than x
4011 * (truncated toward minus infinity)
4013 * unsigned EMUSHORT x[NE], y[NE]
4017 static unsigned EMUSHORT bmask[] =
4040 unsigned EMUSHORT x[], y[];
4042 register unsigned EMUSHORT *p;
4044 unsigned EMUSHORT f[NE];
4046 emov (x, f); /* leave in external format */
4047 expon = (int) f[NE - 1];
4048 e = (expon & 0x7fff) - (EXONE - 1);
4054 /* number of bits to clear out */
4066 /* clear the remaining bits */
4068 /* truncate negatives toward minus infinity */
4071 if ((unsigned EMUSHORT) expon & (unsigned EMUSHORT) 0x8000)
4073 for (i = 0; i < NE - 1; i++)
4085 /* unsigned EMUSHORT x[], s[];
4088 * efrexp (x, exp, s);
4090 * Returns s and exp such that s * 2**exp = x and .5 <= s < 1.
4091 * For example, 1.1 = 0.55 * 2**1
4092 * Handles denormalized numbers properly using long integer exp.
4096 unsigned EMUSHORT x[];
4098 unsigned EMUSHORT s[];
4100 unsigned EMUSHORT xi[NI];
4104 li = (EMULONG) ((EMUSHORT) xi[1]);
4112 *exp = (int) (li - 0x3ffe);
4117 /* unsigned EMUSHORT x[], y[];
4120 * eldexp (x, pwr2, y);
4122 * Returns y = x * 2**pwr2.
4126 unsigned EMUSHORT x[];
4128 unsigned EMUSHORT y[];
4130 unsigned EMUSHORT xi[NI];
4138 emdnorm (xi, i, i, li, 64);
4143 /* c = remainder after dividing b by a
4144 * Least significant integer quotient bits left in equot[].
4148 unsigned EMUSHORT a[], b[], c[];
4150 unsigned EMUSHORT den[NI], num[NI];
4152 if (ecmp (a, ezero) == 0)
4154 mtherr ("eremain", SING);
4160 eiremain (den, num);
4161 /* Sign of remainder = sign of quotient */
4171 unsigned EMUSHORT den[], num[];
4174 unsigned EMUSHORT j;
4177 ld -= enormlz (den);
4179 ln -= enormlz (num);
4183 if (ecmpm (den, num) <= 0)
4197 emdnorm (num, 0, 0, ln, 0);
4202 * Library common error handling routine
4212 * mtherr (fctnam, code);
4218 * This routine may be called to report one of the following
4219 * error conditions (in the include file mconf.h).
4221 * Mnemonic Value Significance
4223 * DOMAIN 1 argument domain error
4224 * SING 2 function singularity
4225 * OVERFLOW 3 overflow range error
4226 * UNDERFLOW 4 underflow range error
4227 * TLOSS 5 total loss of precision
4228 * PLOSS 6 partial loss of precision
4229 * EDOM 33 Unix domain error code
4230 * ERANGE 34 Unix range error code
4232 * The default version of the file prints the function name,
4233 * passed to it by the pointer fctnam, followed by the
4234 * error condition. The display is directed to the standard
4235 * output device. The routine then returns to the calling
4236 * program. Users may wish to modify the program to abort by
4237 * calling exit under severe error conditions such as domain
4240 * Since all error conditions pass control to this function,
4241 * the display may be easily changed, eliminated, or directed
4242 * to an error logging device.
4251 Cephes Math Library Release 2.0: April, 1987
4252 Copyright 1984, 1987 by Stephen L. Moshier
4253 Direct inquiries to 30 Frost Street, Cambridge, MA 02140
4256 /* include "mconf.h" */
4258 /* Notice: the order of appearance of the following
4259 * messages is bound to the error codes defined
4262 static char *ermsg[7] =
4264 "unknown", /* error code 0 */
4265 "domain", /* error code 1 */
4266 "singularity", /* et seq. */
4269 "total loss of precision",
4270 "partial loss of precision"
4283 /* Display string passed by calling program,
4284 * which is supposed to be the name of the
4285 * function in which the error occurred.
4288 /* Display error message defined
4289 * by the code argument.
4291 if ((code <= 0) || (code >= 6))
4293 sprintf (errstr, "\n%s %s error\n", name, ermsg[code]);
4296 /* Set global error message word */
4299 /* Return to calling
4304 /* Here is etodec.c .
4309 ; convert DEC double precision to e type
4316 unsigned EMUSHORT *d;
4317 unsigned EMUSHORT *e;
4319 unsigned EMUSHORT y[NI];
4320 register unsigned EMUSHORT r, *p;
4322 ecleaz (y); /* start with a zero */
4323 p = y; /* point to our number */
4324 r = *d; /* get DEC exponent word */
4325 if (*d & (unsigned int) 0x8000)
4326 *p = 0xffff; /* fill in our sign */
4327 ++p; /* bump pointer to our exponent word */
4328 r &= 0x7fff; /* strip the sign bit */
4329 if (r == 0) /* answer = 0 if high order DEC word = 0 */
4333 r >>= 7; /* shift exponent word down 7 bits */
4334 r += EXONE - 0201; /* subtract DEC exponent offset */
4335 /* add our e type exponent offset */
4336 *p++ = r; /* to form our exponent */
4338 r = *d++; /* now do the high order mantissa */
4339 r &= 0177; /* strip off the DEC exponent and sign bits */
4340 r |= 0200; /* the DEC understood high order mantissa bit */
4341 *p++ = r; /* put result in our high guard word */
4343 *p++ = *d++; /* fill in the rest of our mantissa */
4347 eshdn8 (y); /* shift our mantissa down 8 bits */
4355 ; convert e type to DEC double precision
4361 static unsigned EMUSHORT decbit[NI] = {0, 0, 0, 0, 0, 0, 0200, 0};
4365 unsigned EMUSHORT *x, *d;
4367 unsigned EMUSHORT xi[NI];
4368 register unsigned EMUSHORT r;
4376 if (r < (EXONE - 128))
4379 if ((i & 0200) != 0)
4381 if ((i & 0377) == 0200)
4383 if ((i & 0400) != 0)
4385 /* check all less significant bits */
4386 for (j = M + 5; j < NI; j++)
4435 unsigned EMUSHORT *x, *d;
4437 unsigned EMUSHORT xi[NI];
4442 exp = (EMULONG) xi[E] - (EXONE - 0201); /* adjust exponent for offsets */
4443 /* round off to nearest or even */
4446 emdnorm (xi, 0, 0, exp, 64);
4453 unsigned EMUSHORT *x, *y;
4455 unsigned EMUSHORT i;
4456 unsigned EMUSHORT *p;
4495 #endif /* EMU_NON_COMPILE not defined */