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. */
28 /* To enable support of XFmode extended real floating point, define
29 LONG_DOUBLE_TYPE_SIZE 96 in the tm.h file (m68k.h or i386.h).
31 To support cross compilation between IEEE and VAX floating
32 point formats, define REAL_ARITHMETIC in the tm.h file.
34 In either case the machine files (tm.h) must not contain any code
35 that tries to use host floating point arithmetic to convert
36 REAL_VALUE_TYPEs from `double' to `float', pass them to fprintf,
37 etc. In cross-compile situations a REAL_VALUE_TYPE may not
38 be intelligible to the host computer's native arithmetic.
40 The emulator defaults to the host's floating point format so that
41 its decimal conversion functions can be used if desired (see
44 The first part of this file interfaces gcc to ieee.c, which is a
45 floating point arithmetic suite that was not written with gcc in
46 mind. The interface is followed by ieee.c itself and related
47 items. Avoid changing ieee.c unless you have suitable test
48 programs available. A special version of the PARANOIA floating
49 point arithmetic tester, modified for this purpose, can be found
50 on usc.edu : /pub/C-numanal/ieeetest.zoo. Some tutorial
51 information on ieee.c is given in my book: S. L. Moshier,
52 _Methods and Programs for Mathematical Functions_, Prentice-Hall
53 or Simon & Schuster Int'l, 1989. A library of XFmode elementary
54 transcendental functions can be obtained by ftp from
55 research.att.com: netlib/cephes/ldouble.shar.Z */
57 /* Type of computer arithmetic.
58 * Only one of DEC, MIEEE, IBMPC, or UNK should get defined.
59 * The following modification converts gcc macros into the ones
62 * Note: long double formats differ between IBMPC and MIEEE
63 * by more than just endian-ness.
66 /* REAL_ARITHMETIC defined means that macros in real.h are
67 defined to call emulator functions. */
68 #ifdef REAL_ARITHMETIC
70 #if TARGET_FLOAT_FORMAT == VAX_FLOAT_FORMAT
71 /* PDP-11, Pro350, VAX: */
73 #else /* it's not VAX */
74 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
76 /* Motorola IEEE, high order words come first (Sun workstation): */
78 #else /* not big-endian */
79 /* Intel IEEE, low order words come first:
82 #endif /* big-endian */
83 #else /* it's not IEEE either */
84 /* UNKnown arithmetic. We don't support this and can't go on. */
85 unknown arithmetic type
91 /* REAL_ARITHMETIC not defined means that the *host's* data
92 structure will be used. It may differ by endian-ness from the
93 target machine's structure and will get its ends swapped
94 accordingly (but not here). Probably only the decimal <-> binary
95 functions in this file will actually be used in this case. */
96 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
98 #else /* it's not VAX */
99 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
100 #ifdef HOST_WORDS_BIG_ENDIAN
102 #else /* not big-endian */
104 #endif /* big-endian */
105 #else /* it's not IEEE either */
106 unknown arithmetic type
108 #endif /* not IEEE */
111 #endif /* REAL_ARITHMETIC not defined */
113 /* Define for support of infinity. */
121 * Include file for extended precision arithmetic programs.
124 /* Number of 16 bit words in external e type format */
127 /* Number of 16 bit words in internal format */
130 /* Array offset to exponent */
133 /* Array offset to high guard word */
136 /* Number of bits of precision */
137 #define NBITS ((NI-4)*16)
139 /* Maximum number of decimal digits in ASCII conversion
142 #define NDEC (NBITS*8/27)
144 /* The exponent of 1.0 */
145 #define EXONE (0x3fff)
147 /* Find a host integer type that is at least 16 bits wide,
148 and another type at least twice whatever that size is. */
150 #if HOST_BITS_PER_CHAR >= 16
151 #define EMUSHORT char
152 #define EMUSHORT_SIZE HOST_BITS_PER_CHAR
153 #define EMULONG_SIZE (2 * HOST_BITS_PER_CHAR)
155 #if HOST_BITS_PER_SHORT >= 16
156 #define EMUSHORT short
157 #define EMUSHORT_SIZE HOST_BITS_PER_SHORT
158 #define EMULONG_SIZE (2 * HOST_BITS_PER_SHORT)
160 #if HOST_BITS_PER_INT >= 16
162 #define EMUSHORT_SIZE HOST_BITS_PER_INT
163 #define EMULONG_SIZE (2 * HOST_BITS_PER_INT)
165 #if HOST_BITS_PER_LONG >= 16
166 #define EMUSHORT long
167 #define EMUSHORT_SIZE HOST_BITS_PER_LONG
168 #define EMULONG_SIZE (2 * HOST_BITS_PER_LONG)
170 /* You will have to modify this program to have a smaller unit size. */
171 #define EMU_NON_COMPILE
177 #if HOST_BITS_PER_SHORT >= EMULONG_SIZE
178 #define EMULONG short
180 #if HOST_BITS_PER_INT >= EMULONG_SIZE
183 #if HOST_BITS_PER_LONG >= EMULONG_SIZE
186 #if HOST_BITS_PER_LONG_LONG >= EMULONG_SIZE
187 #define EMULONG long long int
189 /* You will have to modify this program to have a smaller unit size. */
190 #define EMU_NON_COMPILE
197 /* The host interface doesn't work if no 16-bit size exists. */
198 #if EMUSHORT_SIZE != 16
199 #define EMU_NON_COMPILE
202 /* OK to continue compilation. */
203 #ifndef EMU_NON_COMPILE
205 /* Construct macros to translate between REAL_VALUE_TYPE and e type.
206 In GET_REAL and PUT_REAL, r and e are pointers.
207 A REAL_VALUE_TYPE is guaranteed to occupy contiguous locations
208 in memory, with no holes. */
210 #if LONG_DOUBLE_TYPE_SIZE == 96
211 #define GET_REAL(r,e) bcopy (r, e, 2*NE)
212 #define PUT_REAL(e,r) bcopy (e, r, 2*NE)
213 #else /* no XFmode */
215 #ifdef REAL_ARITHMETIC
216 /* Emulator uses target format internally
217 but host stores it in host endian-ness. */
219 #if defined (HOST_WORDS_BIG_ENDIAN) == WORDS_BIG_ENDIAN
220 #define GET_REAL(r,e) e53toe ((r), (e))
221 #define PUT_REAL(e,r) etoe53 ((e), (r))
223 #else /* endian-ness differs */
224 /* emulator uses target endian-ness internally */
225 #define GET_REAL(r,e) \
226 do { EMUSHORT w[4]; \
227 w[3] = ((EMUSHORT *) r)[0]; \
228 w[2] = ((EMUSHORT *) r)[1]; \
229 w[1] = ((EMUSHORT *) r)[2]; \
230 w[0] = ((EMUSHORT *) r)[3]; \
231 e53toe (w, (e)); } while (0)
233 #define PUT_REAL(e,r) \
234 do { EMUSHORT w[4]; \
236 *((EMUSHORT *) r) = w[3]; \
237 *((EMUSHORT *) r + 1) = w[2]; \
238 *((EMUSHORT *) r + 2) = w[1]; \
239 *((EMUSHORT *) r + 3) = w[0]; } while (0)
241 #endif /* endian-ness differs */
243 #else /* not REAL_ARITHMETIC */
245 /* emulator uses host format */
246 #define GET_REAL(r,e) e53toe ((r), (e))
247 #define PUT_REAL(e,r) etoe53 ((e), (r))
249 #endif /* not REAL_ARITHMETIC */
250 #endif /* no XFmode */
253 int ecmp (), enormlz (), eshift (), eisneg (), eisinf ();
254 void eadd (), esub (), emul (), ediv ();
255 void eshup1 (), eshup8 (), eshup6 (), eshdn1 (), eshdn8 (), eshdn6 ();
256 void eabs (), eneg (), emov (), eclear (), einfin (), efloor ();
257 void eldexp (), efrexp (), eifrac (), euifrac (), ltoe (), ultoe ();
258 void eround (), ereal_to_decimal ();
259 void esqrt (), elog (), eexp (), etanh (), epow ();
260 void asctoe (), asctoe24 (), asctoe53 (), asctoe64 ();
261 void etoasc (), e24toasc (), e53toasc (), e64toasc ();
262 void etoe64 (), etoe53 (), etoe24 (), e64toe (), e53toe (), e24toe ();
264 unsigned EMUSHORT ezero[], ehalf[], eone[], etwo[];
265 unsigned EMUSHORT elog2[], esqrt2[];
267 /* Pack output array with 32-bit numbers obtained from
268 array containing 16-bit numbers, swapping ends if required. */
271 unsigned EMUSHORT e[];
273 enum machine_mode mode;
283 /* Swap halfwords in the third long. */
284 th = (unsigned long) e[4] & 0xffff;
285 t = (unsigned long) e[5] & 0xffff;
288 /* fall into the double case */
292 /* swap halfwords in the second word */
293 th = (unsigned long) e[2] & 0xffff;
294 t = (unsigned long) e[3] & 0xffff;
297 /* fall into the float case */
301 /* swap halfwords in the first word */
302 th = (unsigned long) e[0] & 0xffff;
303 t = (unsigned long) e[1] & 0xffff;
314 /* Pack the output array without swapping. */
321 /* Pack the third long.
322 Each element of the input REAL_VALUE_TYPE array has 16 bit useful bits
324 th = (unsigned long) e[5] & 0xffff;
325 t = (unsigned long) e[4] & 0xffff;
328 /* fall into the double case */
332 /* pack the second long */
333 th = (unsigned long) e[3] & 0xffff;
334 t = (unsigned long) e[2] & 0xffff;
337 /* fall into the float case */
341 /* pack the first long */
342 th = (unsigned long) e[1] & 0xffff;
343 t = (unsigned long) e[0] & 0xffff;
356 /* This is the implementation of the REAL_ARITHMETIC macro.
359 earith (value, icode, r1, r2)
360 REAL_VALUE_TYPE *value;
365 unsigned EMUSHORT d1[NE], d2[NE], v[NE];
370 code = (enum tree_code) icode;
378 esub (d2, d1, v); /* d1 - d2 */
386 #ifndef REAL_INFINITY
387 if (ecmp (d2, ezero) == 0)
390 ediv (d2, d1, v); /* d1/d2 */
393 case MIN_EXPR: /* min (d1,d2) */
394 if (ecmp (d1, d2) < 0)
400 case MAX_EXPR: /* max (d1,d2) */
401 if (ecmp (d1, d2) > 0)
414 /* Truncate REAL_VALUE_TYPE toward zero to signed HOST_WIDE_INT
415 * implements REAL_VALUE_FIX_TRUNCATE (x) (etrunci (x))
421 unsigned EMUSHORT f[NE], g[NE];
433 /* Truncate REAL_VALUE_TYPE toward zero to unsigned HOST_WIDE_INT
434 * implements REAL_VALUE_UNSIGNED_FIX_TRUNCATE (x) (etruncui (x))
440 unsigned EMUSHORT f[NE], g[NE];
452 /* This is the REAL_VALUE_ATOF function.
453 * It converts a decimal string to binary, rounding off
454 * as indicated by the machine_mode argument. Then it
455 * promotes the rounded value to REAL_VALUE_TYPE.
462 unsigned EMUSHORT tem[NE], e[NE];
487 /* Expansion of REAL_NEGATE.
493 unsigned EMUSHORT e[NE];
504 * implements REAL_VALUE_FIX (x) (eroundi (x))
505 * The type of rounding is left unspecified by real.h.
506 * It is implemented here as round to nearest (add .5 and chop).
512 unsigned EMUSHORT f[NE], g[NE];
521 /* Round real to nearest unsigned int
522 * implements REAL_VALUE_UNSIGNED_FIX (x) ((unsigned int) eroundi (x))
523 * Negative input returns zero.
524 * The type of rounding is left unspecified by real.h.
525 * It is implemented here as round to nearest (add .5 and chop).
531 unsigned EMUSHORT f[NE], g[NE];
537 return ((unsigned int)l);
541 /* REAL_VALUE_FROM_INT macro.
544 ereal_from_int (d, i, j)
548 unsigned EMUSHORT df[NE], dg[NE];
557 /* complement and add 1 */
564 eldexp (eone, HOST_BITS_PER_LONG, df);
575 /* REAL_VALUE_FROM_UNSIGNED_INT macro.
578 ereal_from_uint (d, i, j)
582 unsigned EMUSHORT df[NE], dg[NE];
583 unsigned long low, high;
587 eldexp (eone, HOST_BITS_PER_LONG, df);
596 /* REAL_VALUE_TO_INT macro
599 ereal_to_int (low, high, rr)
603 unsigned EMUSHORT d[NE], df[NE], dg[NE], dh[NE];
607 /* convert positive value */
614 eldexp (eone, HOST_BITS_PER_LONG, df);
615 ediv (df, d, dg); /* dg = d / 2^32 is the high word */
616 euifrac (dg, high, dh);
617 emul (df, dh, dg); /* fractional part is the low word */
618 euifrac (dg, low, dh);
621 /* complement and add 1 */
631 /* REAL_VALUE_LDEXP macro.
638 unsigned EMUSHORT e[NE], y[NE];
647 /* These routines are conditionally compiled because functions
648 * of the same names may be defined in fold-const.c. */
649 #ifdef REAL_ARITHMETIC
651 /* Check for infinity in a REAL_VALUE_TYPE. */
656 unsigned EMUSHORT e[NE];
667 /* Check whether an IEEE double precision number is a NaN. */
677 /* Check for a negative IEEE double precision number.
678 * this means strictly less than zero, not -0.
685 unsigned EMUSHORT e[NE];
688 if (ecmp (e, ezero) < 0)
693 /* Expansion of REAL_VALUE_TRUNCATE.
694 * The result is in floating point, rounded to nearest or even.
697 real_value_truncate (mode, arg)
698 enum machine_mode mode;
701 unsigned EMUSHORT e[NE], t[NE];
734 #endif /* REAL_ARITHMETIC defined */
736 /* Target values are arrays of host longs. A long is guaranteed
737 to be at least 32 bits wide. */
743 unsigned EMUSHORT e[NE];
747 endian (e, l, XFmode);
755 unsigned EMUSHORT e[NE];
759 endian (e, l, DFmode);
766 unsigned EMUSHORT e[NE];
771 endian (e, &l, SFmode);
776 ereal_to_decimal (x, s)
780 unsigned EMUSHORT e[NE];
788 REAL_VALUE_TYPE x, y;
790 unsigned EMUSHORT ex[NE], ey[NE];
794 return (ecmp (ex, ey));
801 unsigned EMUSHORT ex[NE];
804 return (eisneg (ex));
807 /* End of REAL_ARITHMETIC interface */
811 * Extended precision IEEE binary floating point arithmetic routines
813 * Numbers are stored in C language as arrays of 16-bit unsigned
814 * short integers. The arguments of the routines are pointers to
818 * External e type data structure, simulates Intel 8087 chip
819 * temporary real format but possibly with a larger significand:
821 * NE-1 significand words (least significant word first,
822 * most significant bit is normally set)
823 * exponent (value = EXONE for 1.0,
824 * top bit is the sign)
827 * Internal data structure of a number (a "word" is 16 bits):
829 * ei[0] sign word (0 for positive, 0xffff for negative)
830 * ei[1] biased exponent (value = EXONE for the number 1.0)
831 * ei[2] high guard word (always zero after normalization)
833 * to ei[NI-2] significand (NI-4 significand words,
834 * most significant word first,
835 * most significant bit is set)
836 * ei[NI-1] low guard word (0x8000 bit is rounding place)
840 * Routines for external format numbers
842 * asctoe (string, e) ASCII string to extended double e type
843 * asctoe64 (string, &d) ASCII string to long double
844 * asctoe53 (string, &d) ASCII string to double
845 * asctoe24 (string, &f) ASCII string to single
846 * asctoeg (string, e, prec) ASCII string to specified precision
847 * e24toe (&f, e) IEEE single precision to e type
848 * e53toe (&d, e) IEEE double precision to e type
849 * e64toe (&d, e) IEEE long double precision to e type
850 * eabs (e) absolute value
851 * eadd (a, b, c) c = b + a
853 * ecmp (a, b) compare a to b, return 1, 0, or -1
854 * ediv (a, b, c) c = b / a
855 * efloor (a, b) truncate to integer, toward -infinity
856 * efrexp (a, exp, s) extract exponent and significand
857 * eifrac (e, &l, frac) e to long integer and e type fraction
858 * euifrac (e, &l, frac) e to unsigned long integer and e type fraction
859 * einfin (e) set e to infinity, leaving its sign alone
860 * eldexp (a, n, b) multiply by 2**n
862 * emul (a, b, c) c = b * a
864 * eround (a, b) b = nearest integer value to a
865 * esub (a, b, c) c = b - a
866 * e24toasc (&f, str, n) single to ASCII string, n digits after decimal
867 * e53toasc (&d, str, n) double to ASCII string, n digits after decimal
868 * e64toasc (&d, str, n) long double to ASCII string
869 * etoasc (e, str, n) e to ASCII string, n digits after decimal
870 * etoe24 (e, &f) convert e type to IEEE single precision
871 * etoe53 (e, &d) convert e type to IEEE double precision
872 * etoe64 (e, &d) convert e type to IEEE long double precision
873 * ltoe (&l, e) long (32 bit) integer to e type
874 * ultoe (&l, e) unsigned long (32 bit) integer to e type
875 * eisneg (e) 1 if sign bit of e != 0, else 0
876 * eisinf (e) 1 if e has maximum exponent
879 * Routines for internal format numbers
881 * eaddm (ai, bi) add significands, bi = bi + ai
883 * ecleazs (ei) set ei = 0 but leave its sign alone
884 * ecmpm (ai, bi) compare significands, return 1, 0, or -1
885 * edivm (ai, bi) divide significands, bi = bi / ai
886 * emdnorm (ai,l,s,exp) normalize and round off
887 * emovi (a, ai) convert external a to internal ai
888 * emovo (ai, a) convert internal ai to external a
889 * emovz (ai, bi) bi = ai, low guard word of bi = 0
890 * emulm (ai, bi) multiply significands, bi = bi * ai
891 * enormlz (ei) left-justify the significand
892 * eshdn1 (ai) shift significand and guards down 1 bit
893 * eshdn8 (ai) shift down 8 bits
894 * eshdn6 (ai) shift down 16 bits
895 * eshift (ai, n) shift ai n bits up (or down if n < 0)
896 * eshup1 (ai) shift significand and guards up 1 bit
897 * eshup8 (ai) shift up 8 bits
898 * eshup6 (ai) shift up 16 bits
899 * esubm (ai, bi) subtract significands, bi = bi - ai
902 * The result is always normalized and rounded to NI-4 word precision
903 * after each arithmetic operation.
905 * Exception flags and NaNs are NOT fully supported.
906 * This arithmetic should never produce a NaN output, but it might
907 * be confused by a NaN input.
908 * Define INFINITY in mconf.h for support of infinity; otherwise a
909 * saturation arithmetic is implemented.
910 * Denormals are always supported here where appropriate (e.g., not
911 * for conversion to DEC numbers).
918 * 5 Jan 84 PDP-11 assembly language version
919 * 2 Mar 86 fixed bug in asctoq
920 * 6 Dec 86 C language version
921 * 30 Aug 88 100 digit version, improved rounding
922 * 15 May 92 80-bit long double support
924 * Author: S. L. Moshier.
930 * Common include file for math routines
942 * This file contains definitions for error codes that are
943 * passed to the common error handling routine mtherr
946 * The file also includes a conditional assembly definition
947 * for the type of computer arithmetic (Intel IEEE, DEC, Motorola
950 * For Digital Equipment PDP-11 and VAX computers, certain
951 * IBM systems, and others that use numbers with a 56-bit
952 * significand, the symbol DEC should be defined. In this
953 * mode, most floating point constants are given as arrays
954 * of octal integers to eliminate decimal to binary conversion
955 * errors that might be introduced by the compiler.
957 * For computers, such as IBM PC, that follow the IEEE
958 * Standard for Binary Floating Point Arithmetic (ANSI/IEEE
959 * Std 754-1985), the symbol IBMPC or MIEEE should be defined.
960 * These numbers have 53-bit significands. In this mode, constants
961 * are provided as arrays of hexadecimal 16 bit integers.
963 * To accommodate other types of computer arithmetic, all
964 * constants are also provided in a normal decimal radix
965 * which one can hope are correctly converted to a suitable
966 * format by the available C language compiler. To invoke
967 * this mode, the symbol UNK is defined.
969 * An important difference among these modes is a predefined
970 * set of machine arithmetic constants for each. The numbers
971 * MACHEP (the machine roundoff error), MAXNUM (largest number
972 * represented), and several other parameters are preset by
973 * the configuration symbol. Check the file const.c to
974 * ensure that these values are correct for your computer.
976 * For ANSI C compatibility, define ANSIC equal to 1. Currently
977 * this affects only the atan2 function and others that use it.
981 Cephes Math Library Release 2.1: January, 1989
982 Copyright 1984, 1987, 1989 by Stephen L. Moshier
983 Direct inquiries to 30 Frost Street, Cambridge, MA 02140
987 /* Constant definitions for math error conditions
990 #define DOMAIN 1 /* argument domain error */
991 #define SING 2 /* argument singularity */
992 #define OVERFLOW 3 /* overflow range error */
993 #define UNDERFLOW 4 /* underflow range error */
994 #define TLOSS 5 /* total loss of precision */
995 #define PLOSS 6 /* partial loss of precision */
1000 /* e type constants used by high precision check routines */
1002 /*include "ehead.h"*/
1004 unsigned EMUSHORT ezero[NE] =
1006 0, 0000000, 0000000, 0000000, 0000000, 0000000,};
1007 extern unsigned EMUSHORT ezero[];
1010 unsigned EMUSHORT ehalf[NE] =
1012 0, 0000000, 0000000, 0000000, 0100000, 0x3ffe,};
1013 extern unsigned EMUSHORT ehalf[];
1016 unsigned EMUSHORT eone[NE] =
1018 0, 0000000, 0000000, 0000000, 0100000, 0x3fff,};
1019 extern unsigned EMUSHORT eone[];
1022 unsigned EMUSHORT etwo[NE] =
1024 0, 0000000, 0000000, 0000000, 0100000, 0040000,};
1025 extern unsigned EMUSHORT etwo[];
1028 unsigned EMUSHORT e32[NE] =
1030 0, 0000000, 0000000, 0000000, 0100000, 0040004,};
1031 extern unsigned EMUSHORT e32[];
1033 /* 6.93147180559945309417232121458176568075500134360255E-1 */
1034 unsigned EMUSHORT elog2[NE] =
1036 0xc9e4, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1037 extern unsigned EMUSHORT elog2[];
1039 /* 1.41421356237309504880168872420969807856967187537695E0 */
1040 unsigned EMUSHORT esqrt2[NE] =
1042 0x597e, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1043 extern unsigned EMUSHORT esqrt2[];
1046 * 1.12837916709551257389615890312154517168810125865800E0 */
1047 unsigned EMUSHORT eoneopi[NE] =
1049 0x71d5, 0x688d, 0012333, 0135202, 0110156, 0x3fff,};
1050 extern unsigned EMUSHORT eoneopi[];
1052 /* 3.14159265358979323846264338327950288419716939937511E0 */
1053 unsigned EMUSHORT epi[NE] =
1055 0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1056 extern unsigned EMUSHORT epi[];
1058 /* 5.7721566490153286060651209008240243104215933593992E-1 */
1059 unsigned EMUSHORT eeul[NE] =
1061 0xd1be, 0xc7a4, 0076660, 0063743, 0111704, 0x3ffe,};
1062 extern unsigned EMUSHORT eeul[];
1071 /* Control register for rounding precision.
1072 * This can be set to 80 (if NE=6), 64, 56, 53, or 24 bits.
1077 void eaddm (), esubm (), emdnorm (), asctoeg ();
1078 static void toe24 (), toe53 (), toe64 ();
1079 void eremain (), einit (), eiremain ();
1080 int ecmpm (), edivm (), emulm ();
1081 void emovi (), emovo (), emovz (), ecleaz (), ecleazs (), eadd1 ();
1082 void etodec (), todec (), dectoe ();
1093 ; Clear out entire external format number.
1095 ; unsigned EMUSHORT x[];
1101 register unsigned EMUSHORT *x;
1105 for (i = 0; i < NE; i++)
1111 /* Move external format number from a to b.
1118 register unsigned EMUSHORT *a, *b;
1122 for (i = 0; i < NE; i++)
1128 ; Absolute value of external format number
1136 unsigned EMUSHORT x[]; /* x is the memory address of a short */
1139 x[NE - 1] &= 0x7fff; /* sign is top bit of last word of external format */
1146 ; Negate external format number
1148 ; unsigned EMUSHORT x[NE];
1154 unsigned EMUSHORT x[];
1157 x[NE - 1] ^= 0x8000; /* Toggle the sign bit */
1162 /* Return 1 if external format number is negative,
1167 unsigned EMUSHORT x[];
1170 if (x[NE - 1] & 0x8000)
1177 /* Return 1 if external format number has maximum possible exponent,
1182 unsigned EMUSHORT x[];
1185 if ((x[NE - 1] & 0x7fff) == 0x7fff)
1193 ; Fill entire number, including exponent and significand, with
1194 ; largest possible number. These programs implement a saturation
1195 ; value that is an ordinary, legal number. A special value
1196 ; "infinity" may also be implemented; this would require tests
1197 ; for that value and implementation of special rules for arithmetic
1198 ; operations involving inifinity.
1203 register unsigned EMUSHORT *x;
1208 for (i = 0; i < NE - 1; i++)
1212 for (i = 0; i < NE - 1; i++)
1237 /* Move in external format number,
1238 * converting it to internal format.
1242 unsigned EMUSHORT *a, *b;
1244 register unsigned EMUSHORT *p, *q;
1248 p = a + (NE - 1); /* point to last word of external number */
1249 /* get the sign bit */
1254 /* get the exponent */
1256 *q++ &= 0x7fff; /* delete the sign bit */
1258 if ((*(q - 1) & 0x7fff) == 0x7fff)
1260 for (i = 2; i < NI; i++)
1265 /* clear high guard word */
1267 /* move in the significand */
1268 for (i = 0; i < NE - 1; i++)
1270 /* clear low guard word */
1275 /* Move internal format number out,
1276 * converting it to external format.
1280 unsigned EMUSHORT *a, *b;
1282 register unsigned EMUSHORT *p, *q;
1283 unsigned EMUSHORT i;
1286 q = b + (NE - 1); /* point to output exponent */
1287 /* combine sign and exponent */
1290 *q-- = *p++ | 0x8000;
1294 if (*(p - 1) == 0x7fff)
1300 /* skip over guard word */
1302 /* move the significand */
1303 for (i = 0; i < NE - 1; i++)
1310 /* Clear out internal format number.
1315 register unsigned EMUSHORT *xi;
1319 for (i = 0; i < NI; i++)
1324 /* same, but don't touch the sign. */
1328 register unsigned EMUSHORT *xi;
1333 for (i = 0; i < NI - 1; i++)
1339 /* Move internal format number from a to b.
1343 register unsigned EMUSHORT *a, *b;
1347 for (i = 0; i < NI - 1; i++)
1349 /* clear low guard word */
1355 ; Compare significands of numbers in internal format.
1356 ; Guard words are included in the comparison.
1358 ; unsigned EMUSHORT a[NI], b[NI];
1361 ; for the significands:
1362 ; returns +1 if a > b
1368 register unsigned EMUSHORT *a, *b;
1372 a += M; /* skip up to significand area */
1374 for (i = M; i < NI; i++)
1382 if (*(--a) > *(--b))
1390 ; Shift significand down by 1 bit
1395 register unsigned EMUSHORT *x;
1397 register unsigned EMUSHORT bits;
1400 x += M; /* point to significand area */
1403 for (i = M; i < NI; i++)
1418 ; Shift significand up by 1 bit
1423 register unsigned EMUSHORT *x;
1425 register unsigned EMUSHORT bits;
1431 for (i = M; i < NI; i++)
1446 ; Shift significand down by 8 bits
1451 register unsigned EMUSHORT *x;
1453 register unsigned EMUSHORT newbyt, oldbyt;
1458 for (i = M; i < NI; i++)
1469 ; Shift significand up by 8 bits
1474 register unsigned EMUSHORT *x;
1477 register unsigned EMUSHORT newbyt, oldbyt;
1482 for (i = M; i < NI; i++)
1493 ; Shift significand up by 16 bits
1498 register unsigned EMUSHORT *x;
1501 register unsigned EMUSHORT *p;
1506 for (i = M; i < NI - 1; i++)
1513 ; Shift significand down by 16 bits
1518 register unsigned EMUSHORT *x;
1521 register unsigned EMUSHORT *p;
1526 for (i = M; i < NI - 1; i++)
1539 unsigned EMUSHORT *x, *y;
1541 register unsigned EMULONG a;
1548 for (i = M; i < NI; i++)
1550 a = (unsigned EMULONG) (*x) + (unsigned EMULONG) (*y) + carry;
1555 *y = (unsigned EMUSHORT) a;
1562 ; Subtract significands
1568 unsigned EMUSHORT *x, *y;
1577 for (i = M; i < NI; i++)
1579 a = (unsigned EMULONG) (*y) - (unsigned EMULONG) (*x) - carry;
1584 *y = (unsigned EMUSHORT) a;
1591 /* Divide significands */
1593 static unsigned EMUSHORT equot[NI];
1597 unsigned EMUSHORT den[], num[];
1600 register unsigned EMUSHORT *p, *q;
1601 unsigned EMUSHORT j;
1607 for (i = M; i < NI; i++)
1612 /* Use faster compare and subtraction if denominator
1613 * has only 15 bits of significance.
1618 for (i = M + 3; i < NI; i++)
1623 if ((den[M + 1] & 1) != 0)
1631 for (i = 0; i < NBITS + 2; i++)
1649 /* The number of quotient bits to calculate is
1650 * NBITS + 1 scaling guard bit + 1 roundoff bit.
1655 for (i = 0; i < NBITS + 2; i++)
1657 if (ecmpm (den, num) <= 0)
1660 j = 1; /* quotient bit = 1 */
1674 /* test for nonzero remainder after roundoff bit */
1677 for (i = M; i < NI; i++)
1685 for (i = 0; i < NI; i++)
1691 /* Multiply significands */
1694 unsigned EMUSHORT a[], b[];
1696 unsigned EMUSHORT *p, *q;
1701 for (i = M; i < NI; i++)
1706 while (*p == 0) /* significand is not supposed to be all zero */
1711 if ((*p & 0xff) == 0)
1719 for (i = 0; i < k; i++)
1723 /* remember if there were any nonzero bits shifted out */
1730 for (i = 0; i < NI; i++)
1733 /* return flag for lost nonzero bits */
1740 * Normalize and round off.
1742 * The internal format number to be rounded is "s".
1743 * Input "lost" indicates whether or not the number is exact.
1744 * This is the so-called sticky bit.
1746 * Input "subflg" indicates whether the number was obtained
1747 * by a subtraction operation. In that case if lost is nonzero
1748 * then the number is slightly smaller than indicated.
1750 * Input "exp" is the biased exponent, which may be negative.
1751 * the exponent field of "s" is ignored but is replaced by
1752 * "exp" as adjusted by normalization and rounding.
1754 * Input "rcntrl" is the rounding control.
1757 static int rlast = -1;
1759 static unsigned EMUSHORT rmsk = 0;
1760 static unsigned EMUSHORT rmbit = 0;
1761 static unsigned EMUSHORT rebit = 0;
1763 static unsigned EMUSHORT rbit[NI];
1766 emdnorm (s, lost, subflg, exp, rcntrl)
1767 unsigned EMUSHORT s[];
1774 unsigned EMUSHORT r;
1779 /* a blank significand could mean either zero or infinity. */
1792 if ((j > NBITS) && (exp < 32767))
1800 if (exp > (EMULONG) (-NBITS - 1))
1813 /* Round off, unless told not to by rcntrl. */
1816 /* Set up rounding parameters if the control register changed. */
1817 if (rndprc != rlast)
1824 rw = NI - 1; /* low guard word */
1839 /* For DEC arithmetic */
1888 /* These tests assume NI = 8 */
1908 if ((r & rmbit) != 0)
1913 { /* round to even */
1914 if ((s[re] & rebit) == 0)
1926 if ((rndprc < 64) && (exp <= 0))
1931 { /* overflow on roundoff */
1944 for (i = 2; i < NI - 1; i++)
1946 pedwarn ("floating point overflow");
1950 for (i = M + 1; i < NI - 1; i++)
1968 s[1] = (unsigned EMUSHORT) exp;
1974 ; Subtract external format numbers.
1976 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
1977 ; esub (a, b, c); c = b - a
1980 static int subflg = 0;
1984 unsigned EMUSHORT *a, *b, *c;
1995 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
1996 ; eadd (a, b, c); c = b + a
2000 unsigned EMUSHORT *a, *b, *c;
2009 unsigned EMUSHORT *a, *b, *c;
2011 unsigned EMUSHORT ai[NI], bi[NI], ci[NI];
2013 EMULONG lt, lta, ltb;
2034 /* compare exponents */
2039 { /* put the larger number in bi */
2049 if (lt < (EMULONG) (-NBITS - 1))
2050 goto done; /* answer same as larger addend */
2052 lost = eshift (ai, k); /* shift the smaller number down */
2056 /* exponents were the same, so must compare significands */
2059 { /* the numbers are identical in magnitude */
2060 /* if different signs, result is zero */
2066 /* if same sign, result is double */
2067 /* double denomalized tiny number */
2068 if ((bi[E] == 0) && ((bi[3] & 0x8000) == 0))
2073 /* add 1 to exponent unless both are zero! */
2074 for (j = 1; j < NI - 1; j++)
2078 /* This could overflow, but let emovo take care of that. */
2083 bi[E] = (unsigned EMUSHORT) ltb;
2087 { /* put the larger number in bi */
2103 emdnorm (bi, lost, subflg, ltb, 64);
2114 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2115 ; ediv (a, b, c); c = b / a
2119 unsigned EMUSHORT *a, *b, *c;
2121 unsigned EMUSHORT ai[NI], bi[NI];
2123 EMULONG lt, lta, ltb;
2128 if (eisneg (a) ^ eisneg (b))
2129 *(c + (NE - 1)) = 0x8000;
2131 *(c + (NE - 1)) = 0;
2146 { /* See if numerator is zero. */
2147 for (i = 1; i < NI - 1; i++)
2151 ltb -= enormlz (bi);
2161 { /* possible divide by zero */
2162 for (i = 1; i < NI - 1; i++)
2166 lta -= enormlz (ai);
2171 *(c + (NE - 1)) = 0;
2173 *(c + (NE - 1)) = 0x8000;
2175 mtherr ("ediv", SING);
2181 /* calculate exponent */
2182 lt = ltb - lta + EXONE;
2183 emdnorm (bi, i, 0, lt, 64);
2197 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2198 ; emul (a, b, c); c = b * a
2202 unsigned EMUSHORT *a, *b, *c;
2204 unsigned EMUSHORT ai[NI], bi[NI];
2206 EMULONG lt, lta, ltb;
2209 if (eisinf (a) || eisinf (b))
2211 if (eisneg (a) ^ eisneg (b))
2212 *(c + (NE - 1)) = 0x8000;
2214 *(c + (NE - 1)) = 0;
2225 for (i = 1; i < NI - 1; i++)
2229 lta -= enormlz (ai);
2240 for (i = 1; i < NI - 1; i++)
2244 ltb -= enormlz (bi);
2253 /* Multiply significands */
2255 /* calculate exponent */
2256 lt = lta + ltb - (EXONE - 1);
2257 emdnorm (bi, j, 0, lt, 64);
2258 /* calculate sign of product */
2270 ; Convert IEEE double precision to e type
2272 ; unsigned EMUSHORT x[N+2];
2277 unsigned EMUSHORT *e, *y;
2281 dectoe (e, y); /* see etodec.c */
2285 register unsigned EMUSHORT r;
2286 register unsigned EMUSHORT *p;
2287 unsigned EMUSHORT yy[NI];
2290 denorm = 0; /* flag if denormalized number */
2299 yy[M] = (r & 0x0f) | 0x10;
2300 r &= ~0x800f; /* strip sign and 4 significand bits */
2311 /* If zero exponent, then the significand is denormalized.
2312 * So, take back the understood high significand bit. */
2332 (void) eshift (yy, -5);
2334 { /* if zero exponent, then normalize the significand */
2335 if ((k = enormlz (yy)) > NBITS)
2338 yy[E] -= (unsigned EMUSHORT) (k - 1);
2341 #endif /* not DEC */
2346 unsigned EMUSHORT *e, *y;
2348 unsigned EMUSHORT yy[NI];
2349 unsigned EMUSHORT *p, *q;
2353 for (i = 0; i < NE - 5; i++)
2356 for (i = 0; i < 5; i++)
2360 for (i = 0; i < 5; i++)
2364 p = &yy[0] + (NE - 1);
2367 for (i = 0; i < 4; i++)
2381 for (i = 0; i < NE; i++)
2387 ; Convert IEEE single precision to e type
2389 ; unsigned EMUSHORT x[N+2];
2394 unsigned EMUSHORT *e, *y;
2396 register unsigned EMUSHORT r;
2397 register unsigned EMUSHORT *p;
2398 unsigned EMUSHORT yy[NI];
2401 denorm = 0; /* flag if denormalized number */
2413 yy[M] = (r & 0x7f) | 0200;
2414 r &= ~0x807f; /* strip sign and 7 significand bits */
2425 /* If zero exponent, then the significand is denormalized.
2426 * So, take back the understood high significand bit. */
2445 (void) eshift (yy, -8);
2447 { /* if zero exponent, then normalize the significand */
2448 if ((k = enormlz (yy)) > NBITS)
2451 yy[E] -= (unsigned EMUSHORT) (k - 1);
2459 unsigned EMUSHORT *x, *e;
2461 unsigned EMUSHORT xi[NI];
2466 /* adjust exponent for offset */
2467 exp = (EMULONG) xi[E];
2472 /* round off to nearest or even */
2475 emdnorm (xi, 0, 0, exp, 64);
2481 /* move out internal format to ieee long double */
2484 unsigned EMUSHORT *a, *b;
2486 register unsigned EMUSHORT *p, *q;
2487 unsigned EMUSHORT i;
2493 q = b + 4; /* point to output exponent */
2494 #if LONG_DOUBLE_TYPE_SIZE == 96
2495 /* Clear the last two bytes of 12-byte Intel format */
2500 /* combine sign and exponent */
2504 *q++ = *p++ | 0x8000;
2510 *q-- = *p++ | 0x8000;
2514 /* skip over guard word */
2516 /* move the significand */
2518 for (i = 0; i < 4; i++)
2521 for (i = 0; i < 4; i++)
2528 ; e type to IEEE double precision
2530 ; unsigned EMUSHORT x[NE];
2538 unsigned EMUSHORT *x, *e;
2540 etodec (x, e); /* see etodec.c */
2545 unsigned EMUSHORT *x, *y;
2554 unsigned EMUSHORT *x, *e;
2556 unsigned EMUSHORT xi[NI];
2561 /* adjust exponent for offsets */
2562 exp = (EMULONG) xi[E] - (EXONE - 0x3ff);
2567 /* round off to nearest or even */
2570 emdnorm (xi, 0, 0, exp, 64);
2579 unsigned EMUSHORT *x, *y;
2581 unsigned EMUSHORT i;
2582 unsigned EMUSHORT *p;
2589 *y = 0; /* output high order */
2591 *y = 0x8000; /* output sign bit */
2594 if (i >= (unsigned int) 2047)
2595 { /* Saturate at largest number less than infinity. */
2610 *y |= (unsigned EMUSHORT) 0x7fef;
2627 (void) eshift (x, 4);
2632 (void) eshift (x, 5);
2634 i |= *p++ & (unsigned EMUSHORT) 0x0f; /* *p = xi[M] */
2635 *y |= (unsigned EMUSHORT) i; /* high order output already has sign bit set */
2649 #endif /* not DEC */
2654 ; e type to IEEE single precision
2656 ; unsigned EMUSHORT x[N+2];
2661 unsigned EMUSHORT *x, *e;
2664 unsigned EMUSHORT xi[NI];
2668 /* adjust exponent for offsets */
2669 exp = (EMULONG) xi[E] - (EXONE - 0177);
2674 /* round off to nearest or even */
2677 emdnorm (xi, 0, 0, exp, 64);
2685 unsigned EMUSHORT *x, *y;
2687 unsigned EMUSHORT i;
2688 unsigned EMUSHORT *p;
2697 *y = 0; /* output high order */
2699 *y = 0x8000; /* output sign bit */
2703 { /* Saturate at largest number less than infinity. */
2705 *y |= (unsigned EMUSHORT) 0x7f80;
2717 *y |= (unsigned EMUSHORT) 0x7f7f;
2733 (void) eshift (x, 7);
2738 (void) eshift (x, 8);
2740 i |= *p++ & (unsigned EMUSHORT) 0x7f; /* *p = xi[M] */
2741 *y |= i; /* high order output already has sign bit set */
2755 /* Compare two e type numbers.
2757 * unsigned EMUSHORT a[NE], b[NE];
2760 * returns +1 if a > b
2766 unsigned EMUSHORT *a, *b;
2768 unsigned EMUSHORT ai[NI], bi[NI];
2769 register unsigned EMUSHORT *p, *q;
2779 { /* the signs are different */
2781 for (i = 1; i < NI - 1; i++)
2795 /* both are the same sign */
2810 return (0); /* equality */
2816 if (*(--p) > *(--q))
2817 return (msign); /* p is bigger */
2819 return (-msign); /* p is littler */
2825 /* Find nearest integer to x = floor (x + 0.5)
2827 * unsigned EMUSHORT x[NE], y[NE]
2832 unsigned EMUSHORT *x, *y;
2842 ; convert long integer to e type
2845 ; unsigned EMUSHORT x[NE];
2847 ; note &l is the memory address of l
2851 long *lp; /* lp is the memory address of a long integer */
2852 unsigned EMUSHORT *y; /* y is the address of a short */
2854 unsigned EMUSHORT yi[NI];
2861 /* make it positive */
2862 ll = (unsigned long) (-(*lp));
2863 yi[0] = 0xffff; /* put correct sign in the e type number */
2867 ll = (unsigned long) (*lp);
2869 /* move the long integer to yi significand area */
2870 yi[M] = (unsigned EMUSHORT) (ll >> 16);
2871 yi[M + 1] = (unsigned EMUSHORT) ll;
2873 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
2874 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
2875 ecleaz (yi); /* it was zero */
2877 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
2878 emovo (yi, y); /* output the answer */
2882 ; convert unsigned long integer to e type
2885 ; unsigned EMUSHORT x[NE];
2887 ; note &l is the memory address of l
2891 unsigned long *lp; /* lp is the memory address of a long integer */
2892 unsigned EMUSHORT *y; /* y is the address of a short */
2894 unsigned EMUSHORT yi[NI];
2901 /* move the long integer to ayi significand area */
2902 yi[M] = (unsigned EMUSHORT) (ll >> 16);
2903 yi[M + 1] = (unsigned EMUSHORT) ll;
2905 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
2906 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
2907 ecleaz (yi); /* it was zero */
2909 yi[E] -= (unsigned EMUSHORT) k; /* subtract shift count from exponent */
2910 emovo (yi, y); /* output the answer */
2915 ; Find long integer and fractional parts
2918 ; unsigned EMUSHORT x[NE], frac[NE];
2919 ; xifrac (x, &i, frac);
2921 The integer output has the sign of the input. The fraction is
2922 the positive fractional part of abs (x).
2926 unsigned EMUSHORT *x;
2928 unsigned EMUSHORT *frac;
2930 unsigned EMUSHORT xi[NI];
2934 k = (int) xi[E] - (EXONE - 1);
2937 /* if exponent <= 0, integer = 0 and real output is fraction */
2942 if (k > (HOST_BITS_PER_LONG - 1))
2945 ; long integer overflow: output large integer
2946 ; and correct fraction
2949 *i = ((unsigned long) 1) << (HOST_BITS_PER_LONG - 1);
2951 *i = (((unsigned long) 1) << (HOST_BITS_PER_LONG - 1)) - 1;
2952 (void) eshift (xi, k);
2953 pedwarn ("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.
2964 (void) eshift (xi, k);
2966 *i = (long) (((unsigned long) xi[M] << 16) | xi[M + 1]);
2971 /* shift not more than 16 bits */
2972 (void) eshift (xi, k);
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
3028 (void) eshift (xi, k);
3029 pedwarn ("Overflow on truncation to unsigned integer");
3036 ; shift more than 16 bits: shift up k-16, output the integer,
3037 ; then complete the shift to get the fraction.
3040 (void) eshift (xi, k);
3042 *i = (long) (((unsigned long) xi[M] << 16) | xi[M + 1]);
3047 /* shift not more than 16 bits */
3048 (void) eshift (xi, k);
3049 *i = (long) xi[M] & 0xffff;
3059 if ((k = enormlz (xi)) > NBITS)
3062 xi[E] -= (unsigned EMUSHORT) k;
3072 ; Shifts significand area up or down by the number of bits
3073 ; given by the variable sc.
3077 unsigned EMUSHORT *x;
3080 unsigned EMUSHORT lost;
3081 unsigned EMUSHORT *p;
3094 lost |= *p; /* remember lost bits */
3135 return ((int) lost);
3143 ; Shift normalizes the significand area pointed to by argument
3144 ; shift count (up = positive) is returned.
3148 unsigned EMUSHORT x[];
3150 register unsigned EMUSHORT *p;
3159 return (0); /* already normalized */
3164 /* With guard word, there are NBITS+16 bits available.
3165 * return true if all are zero.
3170 /* see if high byte is zero */
3171 while ((*p & 0xff00) == 0)
3176 /* now shift 1 bit at a time */
3177 while ((*p & 0x8000) == 0)
3183 mtherr ("enormlz", UNDERFLOW);
3189 /* Normalize by shifting down out of the high guard word
3190 of the significand */
3205 mtherr ("enormlz", OVERFLOW);
3215 /* Convert e type number to decimal format ASCII string.
3216 * The constants are for 64 bit precision.
3222 static unsigned EMUSHORT etens[NTEN + 1][NE] =
3224 {0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
3225 {0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
3226 {0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
3227 {0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
3228 {0xddbc, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
3229 {0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
3230 {0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
3231 {0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
3232 {0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
3233 {0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
3234 {0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
3235 {0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
3236 {0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
3239 static unsigned EMUSHORT emtens[NTEN + 1][NE] =
3241 {0x2de4, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
3242 {0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
3243 {0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
3244 {0x7133, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
3245 {0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
3246 {0xac7d, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
3247 {0x3f24, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
3248 {0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
3249 {0x4c2f, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
3250 {0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
3251 {0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
3252 {0x3d71, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
3253 {0xcccd, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
3257 e24toasc (x, string, ndigs)
3258 unsigned EMUSHORT x[];
3262 unsigned EMUSHORT w[NI];
3266 if ((x[1] & 0x7f80) == 0x7f80)
3268 if ((x[0] & 0x7f80) == 0x7f80)
3276 sprintf (string, " -Infinity ");
3278 sprintf (string, " Infinity ");
3283 etoasc (w, string, ndigs);
3288 e53toasc (x, string, ndigs)
3289 unsigned EMUSHORT x[];
3293 unsigned EMUSHORT w[NI];
3297 if ((x[3] & 0x7ff0) == 0x7ff0)
3299 if ((x[0] & 0x7ff0) == 0x7ff0)
3307 sprintf (string, " -Infinity ");
3309 sprintf (string, " Infinity ");
3314 etoasc (w, string, ndigs);
3319 e64toasc (x, string, ndigs)
3320 unsigned EMUSHORT x[];
3324 unsigned EMUSHORT w[NI];
3328 if ((x[4] & 0x7fff) == 0x7fff)
3330 if ((x[0] & 0x7fff) == 0x7fff)
3338 sprintf (string, " -Infinity ");
3340 sprintf (string, " Infinity ");
3345 etoasc (w, string, ndigs);
3349 static char wstring[80]; /* working storage for ASCII output */
3352 etoasc (x, string, ndigs)
3353 unsigned EMUSHORT x[];
3358 unsigned EMUSHORT y[NI], t[NI], u[NI], w[NI];
3359 unsigned EMUSHORT *p, *r, *ten;
3360 unsigned EMUSHORT sign;
3361 int i, j, k, expon, rndsav;
3363 unsigned EMUSHORT m;
3367 while ((*s++ = *ss++) != '\0')
3370 rndprc = NBITS; /* set to full precision */
3371 emov (x, y); /* retain external format */
3372 if (y[NE - 1] & 0x8000)
3375 y[NE - 1] &= 0x7fff;
3382 ten = &etens[NTEN][0];
3384 /* Test for zero exponent */
3387 for (k = 0; k < NE - 1; k++)
3390 goto tnzro; /* denormalized number */
3392 goto isone; /* legal all zeros */
3396 /* Test for infinity. Don't bother with illegal infinities.
3398 if (y[NE - 1] == 0x7fff)
3401 sprintf (wstring, " -Infinity ");
3403 sprintf (wstring, " Infinity ");
3407 /* Test for exponent nonzero but significand denormalized.
3408 * This is an error condition.
3410 if ((y[NE - 1] != 0) && ((y[NE - 2] & 0x8000) == 0))
3412 mtherr ("etoasc", DOMAIN);
3413 sprintf (wstring, "NaN");
3417 /* Compare to 1.0 */
3423 { /* Number is greater than 1 */
3424 /* Convert significand to an integer and strip trailing decimal zeros. */
3426 u[NE - 1] = EXONE + NBITS - 1;
3428 p = &etens[NTEN - 4][0];
3434 for (j = 0; j < NE - 1; j++)
3447 /* Rescale from integer significand */
3448 u[NE - 1] += y[NE - 1] - (unsigned int) (EXONE + NBITS - 1);
3450 /* Find power of 10 */
3454 while (ecmp (ten, u) <= 0)
3456 if (ecmp (p, u) <= 0)
3469 { /* Number is less than 1.0 */
3470 /* Pad significand with trailing decimal zeros. */
3473 while ((y[NE - 2] & 0x8000) == 0)
3482 for (i = 0; i < NDEC + 1; i++)
3484 if ((w[NI - 1] & 0x7) != 0)
3486 /* multiply by 10 */
3499 if (eone[NE - 1] <= u[1])
3511 while (ecmp (eone, w) > 0)
3513 if (ecmp (p, w) >= 0)
3528 /* Find the first (leading) digit. */
3534 digit = equot[NI - 1];
3535 while ((digit == 0) && (ecmp (y, ezero) != 0))
3543 digit = equot[NI - 1];
3551 *s++ = (char) digit + '0';
3553 /* Examine number of digits requested by caller. */
3558 /* Generate digits after the decimal point. */
3559 for (k = 0; k <= ndigs; k++)
3561 /* multiply current number by 10, without normalizing */
3568 *s++ = (char) equot[NI - 1] + '0';
3570 digit = equot[NI - 1];
3573 /* round off the ASCII string */
3576 /* Test for critical rounding case in ASCII output. */
3580 if (ecmp (t, ezero) != 0)
3581 goto roun; /* round to nearest */
3582 if ((*(s - 1) & 1) == 0)
3583 goto doexp; /* round to even */
3585 /* Round up and propagate carry-outs */
3589 /* Carry out to most significant digit? */
3596 /* Most significant digit carries to 10? */
3604 /* Round up and carry out from less significant digits */
3616 sprintf (ss, "e+%d", expon);
3618 sprintf (ss, "e%d", expon);
3620 sprintf (ss, "e%d", expon);
3623 /* copy out the working string */
3626 while (*ss == ' ') /* strip possible leading space */
3628 while ((*s++ = *ss++) != '\0')
3637 ; ASCTOQ.MAC LATEST REV: 11 JAN 84
3640 ; Convert ASCII string to quadruple precision floating point
3642 ; Numeric input is free field decimal number
3643 ; with max of 15 digits with or without
3644 ; decimal point entered as ASCII from teletype.
3645 ; Entering E after the number followed by a second
3646 ; number causes the second number to be interpreted
3647 ; as a power of 10 to be multiplied by the first number
3648 ; (i.e., "scientific" notation).
3651 ; asctoq (string, q);
3654 /* ASCII to single */
3658 unsigned EMUSHORT *y;
3664 /* ASCII to double */
3668 unsigned EMUSHORT *y;
3678 /* ASCII to long double */
3682 unsigned EMUSHORT *y;
3687 /* ASCII to super double */
3691 unsigned EMUSHORT *y;
3693 asctoeg (s, y, NBITS);
3696 /* Space to make a copy of the input string: */
3697 static char lstr[82];
3700 asctoeg (ss, y, oprec)
3702 unsigned EMUSHORT *y;
3705 unsigned EMUSHORT yy[NI], xt[NI], tt[NI];
3706 int esign, decflg, sgnflg, nexp, exp, prec, lost;
3707 int k, trail, c, rndsav;
3709 unsigned EMUSHORT nsign, *p;
3712 /* Copy the input string. */
3714 while (*s == ' ') /* skip leading spaces */
3717 for (k = 0; k < 79; k++)
3719 if ((*sp++ = *s++) == '\0')
3726 rndprc = NBITS; /* Set to full precision */
3739 if ((k >= 0) && (k <= 9))
3741 /* Ignore leading zeros */
3742 if ((prec == 0) && (decflg == 0) && (k == 0))
3744 /* Identify and strip trailing zeros after the decimal point. */
3745 if ((trail == 0) && (decflg != 0))
3748 while ((*sp >= '0') && (*sp <= '9'))
3750 /* Check for syntax error */
3752 if ((c != 'e') && (c != 'E') && (c != '\0')
3753 && (c != '\n') && (c != '\r') && (c != ' ')
3763 /* If enough digits were given to more than fill up the yy register,
3764 * continuing until overflow into the high guard word yy[2]
3765 * guarantees that there will be a roundoff bit at the top
3766 * of the low guard word after normalization.
3771 nexp += 1; /* count digits after decimal point */
3772 eshup1 (yy); /* multiply current number by 10 */
3778 xt[NI - 2] = (unsigned EMUSHORT) k;
3796 case '.': /* decimal point */
3821 yy[E] = 0x7fff; /* infinity */
3825 mtherr ("asctoe", DOMAIN);
3833 /* Exponent interpretation */
3839 /* check for + or - */
3847 while ((*s >= '0') && (*s <= '9'))
3857 /* Pad trailing zeros to minimize power of 10, per IEEE spec. */
3858 while ((nexp > 0) && (yy[2] == 0))
3870 if ((k = enormlz (yy)) > NBITS)
3875 lexp = (EXONE - 1 + NBITS) - k;
3876 emdnorm (yy, lost, 0, lexp, 64);
3877 /* convert to external format */
3880 /* Multiply by 10**nexp. If precision is 64 bits,
3881 * the maximum relative error incurred in forming 10**n
3882 * for 0 <= n <= 324 is 8.2e-20, at 10**180.
3883 * For 0 <= n <= 999, the peak relative error is 1.4e-19 at 10**947.
3884 * For 0 >= n >= -999, it is -1.55e-19 at 10**-435.
3898 { /* Punt. Can't handle this without 2 divides. */
3899 emovi (etens[0], tt);
3906 p = &etens[NTEN][0];
3916 while (exp <= MAXP);
3934 /* Round and convert directly to the destination type */
3936 lexp -= EXONE - 0x3ff;
3937 else if (oprec == 24)
3938 lexp -= EXONE - 0177;
3940 else if (oprec == 56)
3941 lexp -= EXONE - 0201;
3944 emdnorm (yy, k, 0, lexp, 64);
3954 todec (yy, y); /* see etodec.c */
3974 /* y = largest integer not greater than x
3975 * (truncated toward minus infinity)
3977 * unsigned EMUSHORT x[NE], y[NE]
3981 static unsigned EMUSHORT bmask[] =
4004 unsigned EMUSHORT x[], y[];
4006 register unsigned EMUSHORT *p;
4008 unsigned EMUSHORT f[NE];
4010 emov (x, f); /* leave in external format */
4011 expon = (int) f[NE - 1];
4012 e = (expon & 0x7fff) - (EXONE - 1);
4018 /* number of bits to clear out */
4030 /* clear the remaining bits */
4032 /* truncate negatives toward minus infinity */
4035 if ((unsigned EMUSHORT) expon & (unsigned EMUSHORT) 0x8000)
4037 for (i = 0; i < NE - 1; i++)
4049 /* unsigned EMUSHORT x[], s[];
4052 * efrexp (x, exp, s);
4054 * Returns s and exp such that s * 2**exp = x and .5 <= s < 1.
4055 * For example, 1.1 = 0.55 * 2**1
4056 * Handles denormalized numbers properly using long integer exp.
4060 unsigned EMUSHORT x[];
4062 unsigned EMUSHORT s[];
4064 unsigned EMUSHORT xi[NI];
4068 li = (EMULONG) ((EMUSHORT) xi[1]);
4076 *exp = (int) (li - 0x3ffe);
4081 /* unsigned EMUSHORT x[], y[];
4084 * eldexp (x, pwr2, y);
4086 * Returns y = x * 2**pwr2.
4090 unsigned EMUSHORT x[];
4092 unsigned EMUSHORT y[];
4094 unsigned EMUSHORT xi[NI];
4102 emdnorm (xi, i, i, li, 64);
4107 /* c = remainder after dividing b by a
4108 * Least significant integer quotient bits left in equot[].
4112 unsigned EMUSHORT a[], b[], c[];
4114 unsigned EMUSHORT den[NI], num[NI];
4116 if (ecmp (a, ezero) == 0)
4118 mtherr ("eremain", SING);
4124 eiremain (den, num);
4125 /* Sign of remainder = sign of quotient */
4135 unsigned EMUSHORT den[], num[];
4138 unsigned EMUSHORT j;
4141 ld -= enormlz (den);
4143 ln -= enormlz (num);
4147 if (ecmpm (den, num) <= 0)
4161 emdnorm (num, 0, 0, ln, 0);
4166 * Library common error handling routine
4176 * mtherr (fctnam, code);
4182 * This routine may be called to report one of the following
4183 * error conditions (in the include file mconf.h).
4185 * Mnemonic Value Significance
4187 * DOMAIN 1 argument domain error
4188 * SING 2 function singularity
4189 * OVERFLOW 3 overflow range error
4190 * UNDERFLOW 4 underflow range error
4191 * TLOSS 5 total loss of precision
4192 * PLOSS 6 partial loss of precision
4193 * EDOM 33 Unix domain error code
4194 * ERANGE 34 Unix range error code
4196 * The default version of the file prints the function name,
4197 * passed to it by the pointer fctnam, followed by the
4198 * error condition. The display is directed to the standard
4199 * output device. The routine then returns to the calling
4200 * program. Users may wish to modify the program to abort by
4201 * calling exit under severe error conditions such as domain
4204 * Since all error conditions pass control to this function,
4205 * the display may be easily changed, eliminated, or directed
4206 * to an error logging device.
4215 Cephes Math Library Release 2.0: April, 1987
4216 Copyright 1984, 1987 by Stephen L. Moshier
4217 Direct inquiries to 30 Frost Street, Cambridge, MA 02140
4220 /* include "mconf.h" */
4222 /* Notice: the order of appearance of the following
4223 * messages is bound to the error codes defined
4226 static char *ermsg[7] =
4228 "unknown", /* error code 0 */
4229 "domain", /* error code 1 */
4230 "singularity", /* et seq. */
4233 "total loss of precision",
4234 "partial loss of precision"
4247 /* Display string passed by calling program,
4248 * which is supposed to be the name of the
4249 * function in which the error occurred.
4252 /* Display error message defined
4253 * by the code argument.
4255 if ((code <= 0) || (code >= 6))
4257 sprintf (errstr, "\n%s %s error\n", name, ermsg[code]);
4259 /* Set global error message word */
4262 /* Return to calling
4267 /* Here is etodec.c .
4272 ; convert DEC double precision to e type
4279 unsigned EMUSHORT *d;
4280 unsigned EMUSHORT *e;
4282 unsigned EMUSHORT y[NI];
4283 register unsigned EMUSHORT r, *p;
4285 ecleaz (y); /* start with a zero */
4286 p = y; /* point to our number */
4287 r = *d; /* get DEC exponent word */
4288 if (*d & (unsigned int) 0x8000)
4289 *p = 0xffff; /* fill in our sign */
4290 ++p; /* bump pointer to our exponent word */
4291 r &= 0x7fff; /* strip the sign bit */
4292 if (r == 0) /* answer = 0 if high order DEC word = 0 */
4296 r >>= 7; /* shift exponent word down 7 bits */
4297 r += EXONE - 0201; /* subtract DEC exponent offset */
4298 /* add our e type exponent offset */
4299 *p++ = r; /* to form our exponent */
4301 r = *d++; /* now do the high order mantissa */
4302 r &= 0177; /* strip off the DEC exponent and sign bits */
4303 r |= 0200; /* the DEC understood high order mantissa bit */
4304 *p++ = r; /* put result in our high guard word */
4306 *p++ = *d++; /* fill in the rest of our mantissa */
4310 eshdn8 (y); /* shift our mantissa down 8 bits */
4318 ; convert e type to DEC double precision
4324 static unsigned EMUSHORT decbit[NI] = {0, 0, 0, 0, 0, 0, 0200, 0};
4328 unsigned EMUSHORT *x, *d;
4330 unsigned EMUSHORT xi[NI];
4331 register unsigned EMUSHORT r;
4339 if (r < (EXONE - 128))
4342 if ((i & 0200) != 0)
4344 if ((i & 0377) == 0200)
4346 if ((i & 0400) != 0)
4348 /* check all less significant bits */
4349 for (j = M + 5; j < NI; j++)
4398 unsigned EMUSHORT *x, *d;
4400 unsigned EMUSHORT xi[NI];
4405 exp = (EMULONG) xi[E] - (EXONE - 0201); /* adjust exponent for offsets */
4406 /* round off to nearest or even */
4409 emdnorm (xi, 0, 0, exp, 64);
4416 unsigned EMUSHORT *x, *y;
4418 unsigned EMUSHORT i;
4419 unsigned EMUSHORT *p;
4455 #endif /* EMU_NON_COMPILE not defined */