1 /* real.c - implementation of REAL_ARITHMETIC, REAL_VALUE_ATOF,
2 and support for XFmode IEEE extended real floating point arithmetic.
3 Copyright (C) 1993, 1994 Free Software Foundation, Inc.
4 Contributed by Stephen L. Moshier (moshier@world.std.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
31 /* To enable support of XFmode extended real floating point, define
32 LONG_DOUBLE_TYPE_SIZE 96 in the tm.h file (m68k.h or i386.h).
34 To support cross compilation between IEEE, VAX and IBM floating
35 point formats, define REAL_ARITHMETIC in the tm.h file.
37 In either case the machine files (tm.h) must not contain any code
38 that tries to use host floating point arithmetic to convert
39 REAL_VALUE_TYPEs from `double' to `float', pass them to fprintf,
40 etc. In cross-compile situations a REAL_VALUE_TYPE may not
41 be intelligible to the host computer's native arithmetic.
43 The emulator defaults to the host's floating point format so that
44 its decimal conversion functions can be used if desired (see
47 The first part of this file interfaces gcc to ieee.c, which is a
48 floating point arithmetic suite that was not written with gcc in
49 mind. The interface is followed by ieee.c itself and related
50 items. Avoid changing ieee.c unless you have suitable test
51 programs available. A special version of the PARANOIA floating
52 point arithmetic tester, modified for this purpose, can be found
53 on usc.edu : /pub/C-numanal/ieeetest.zoo. Some tutorial
54 information on ieee.c is given in my book: S. L. Moshier,
55 _Methods and Programs for Mathematical Functions_, Prentice-Hall
56 or Simon & Schuster Int'l, 1989. A library of XFmode elementary
57 transcendental functions can be obtained by ftp from
58 research.att.com: netlib/cephes/ldouble.shar.Z */
60 /* Type of computer arithmetic.
61 * Only one of DEC, IBM, MIEEE, IBMPC, or UNK should get defined.
64 /* `MIEEE' refers generically to big-endian IEEE floating-point data
65 structure. This definition should work in SFmode `float' type and
66 DFmode `double' type on virtually all big-endian IEEE machines.
67 If LONG_DOUBLE_TYPE_SIZE has been defined to be 96, then MIEEE
68 also invokes the particular XFmode (`long double' type) data
69 structure used by the Motorola 680x0 series processors.
71 `IBMPC' refers generally to little-endian IEEE machines. In this
72 case, if LONG_DOUBLE_TYPE_SIZE has been defined to be 96, then
73 IBMPC also invokes the particular XFmode `long double' data
74 structure used by the Intel 80x86 series processors.
76 `DEC' refers specifically to the Digital Equipment Corp PDP-11
77 and VAX floating point data structure. This model currently
78 supports no type wider than DFmode.
80 `IBM' refers specifically to the IBM System/370 and compatible
81 floating point data structure. This model currently supports
82 no type wider than DFmode. The IBM conversions were contributed by
83 frank@atom.ansto.gov.au (Frank Crawford).
85 If LONG_DOUBLE_TYPE_SIZE = 64 (the default, unless tm.h defines it)
86 then `long double' and `double' are both implemented, but they
87 both mean DFmode. In this case, the software floating-point
88 support available here is activated by writing
89 #define REAL_ARITHMETIC
92 The case LONG_DOUBLE_TYPE_SIZE = 128 activates TFmode support
93 and may deactivate XFmode since `long double' is used to refer
96 The macros FLOAT_WORDS_BIG_ENDIAN, HOST_FLOAT_WORDS_BIG_ENDIAN,
97 contributed by Richard Earnshaw <Richard.Earnshaw@cl.cam.ac.uk>,
98 separate the floating point unit's endian-ness from that of
99 the integer addressing. This permits one to define a big-endian
100 FPU on a little-endian machine (e.g., ARM). An extension to
101 BYTES_BIG_ENDIAN may be required for some machines in the future.
102 These optional macros may be defined in tm.h. In real.h, they
103 default to WORDS_BIG_ENDIAN, etc., so there is no need to define
104 them for any normal host or target machine on which the floats
105 and the integers have the same endian-ness. */
108 /* The following converts gcc macros into the ones used by this file. */
110 /* REAL_ARITHMETIC defined means that macros in real.h are
111 defined to call emulator functions. */
112 #ifdef REAL_ARITHMETIC
114 #if TARGET_FLOAT_FORMAT == VAX_FLOAT_FORMAT
115 /* PDP-11, Pro350, VAX: */
117 #else /* it's not VAX */
118 #if TARGET_FLOAT_FORMAT == IBM_FLOAT_FORMAT
119 /* IBM System/370 style */
121 #else /* it's also not an IBM */
122 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
123 #if FLOAT_WORDS_BIG_ENDIAN
124 /* Motorola IEEE, high order words come first (Sun workstation): */
126 #else /* not big-endian */
127 /* Intel IEEE, low order words come first:
130 #endif /* big-endian */
131 #else /* it's not IEEE either */
132 /* UNKnown arithmetic. We don't support this and can't go on. */
133 unknown arithmetic type
135 #endif /* not IEEE */
140 /* REAL_ARITHMETIC not defined means that the *host's* data
141 structure will be used. It may differ by endian-ness from the
142 target machine's structure and will get its ends swapped
143 accordingly (but not here). Probably only the decimal <-> binary
144 functions in this file will actually be used in this case. */
145 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT
147 #else /* it's not VAX */
148 #if HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
149 /* IBM System/370 style */
151 #else /* it's also not an IBM */
152 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
153 #if HOST_FLOAT_WORDS_BIG_ENDIAN
155 #else /* not big-endian */
157 #endif /* big-endian */
158 #else /* it's not IEEE either */
159 unknown arithmetic type
161 #endif /* not IEEE */
165 #endif /* REAL_ARITHMETIC not defined */
167 /* Define INFINITY for support of infinity.
168 Define NANS for support of Not-a-Number's (NaN's). */
169 #if !defined(DEC) && !defined(IBM)
174 /* Support of NaNs requires support of infinity. */
181 /* Find a host integer type that is at least 16 bits wide,
182 and another type at least twice whatever that size is. */
184 #if HOST_BITS_PER_CHAR >= 16
185 #define EMUSHORT char
186 #define EMUSHORT_SIZE HOST_BITS_PER_CHAR
187 #define EMULONG_SIZE (2 * HOST_BITS_PER_CHAR)
189 #if HOST_BITS_PER_SHORT >= 16
190 #define EMUSHORT short
191 #define EMUSHORT_SIZE HOST_BITS_PER_SHORT
192 #define EMULONG_SIZE (2 * HOST_BITS_PER_SHORT)
194 #if HOST_BITS_PER_INT >= 16
196 #define EMUSHORT_SIZE HOST_BITS_PER_INT
197 #define EMULONG_SIZE (2 * HOST_BITS_PER_INT)
199 #if HOST_BITS_PER_LONG >= 16
200 #define EMUSHORT long
201 #define EMUSHORT_SIZE HOST_BITS_PER_LONG
202 #define EMULONG_SIZE (2 * HOST_BITS_PER_LONG)
204 /* You will have to modify this program to have a smaller unit size. */
205 #define EMU_NON_COMPILE
211 #if HOST_BITS_PER_SHORT >= EMULONG_SIZE
212 #define EMULONG short
214 #if HOST_BITS_PER_INT >= EMULONG_SIZE
217 #if HOST_BITS_PER_LONG >= EMULONG_SIZE
220 #if HOST_BITS_PER_LONG_LONG >= EMULONG_SIZE
221 #define EMULONG long long int
223 /* You will have to modify this program to have a smaller unit size. */
224 #define EMU_NON_COMPILE
231 /* The host interface doesn't work if no 16-bit size exists. */
232 #if EMUSHORT_SIZE != 16
233 #define EMU_NON_COMPILE
236 /* OK to continue compilation. */
237 #ifndef EMU_NON_COMPILE
239 /* Construct macros to translate between REAL_VALUE_TYPE and e type.
240 In GET_REAL and PUT_REAL, r and e are pointers.
241 A REAL_VALUE_TYPE is guaranteed to occupy contiguous locations
242 in memory, with no holes. */
244 #if LONG_DOUBLE_TYPE_SIZE == 96
245 /* Number of 16 bit words in external e type format */
247 #define MAXDECEXP 4932
248 #define MINDECEXP -4956
249 #define GET_REAL(r,e) bcopy (r, e, 2*NE)
250 #define PUT_REAL(e,r) bcopy (e, r, 2*NE)
251 #else /* no XFmode */
252 #if LONG_DOUBLE_TYPE_SIZE == 128
254 #define MAXDECEXP 4932
255 #define MINDECEXP -4977
256 #define GET_REAL(r,e) bcopy (r, e, 2*NE)
257 #define PUT_REAL(e,r) bcopy (e, r, 2*NE)
260 #define MAXDECEXP 4932
261 #define MINDECEXP -4956
262 #ifdef REAL_ARITHMETIC
263 /* Emulator uses target format internally
264 but host stores it in host endian-ness. */
266 #if HOST_FLOAT_WORDS_BIG_ENDIAN == FLOAT_WORDS_BIG_ENDIAN
267 #define GET_REAL(r,e) e53toe ((r), (e))
268 #define PUT_REAL(e,r) etoe53 ((e), (r))
270 #else /* endian-ness differs */
271 /* emulator uses target endian-ness internally */
272 #define GET_REAL(r,e) \
273 do { EMUSHORT w[4]; \
274 w[3] = ((EMUSHORT *) r)[0]; \
275 w[2] = ((EMUSHORT *) r)[1]; \
276 w[1] = ((EMUSHORT *) r)[2]; \
277 w[0] = ((EMUSHORT *) r)[3]; \
278 e53toe (w, (e)); } while (0)
280 #define PUT_REAL(e,r) \
281 do { EMUSHORT w[4]; \
283 *((EMUSHORT *) r) = w[3]; \
284 *((EMUSHORT *) r + 1) = w[2]; \
285 *((EMUSHORT *) r + 2) = w[1]; \
286 *((EMUSHORT *) r + 3) = w[0]; } while (0)
288 #endif /* endian-ness differs */
290 #else /* not REAL_ARITHMETIC */
292 /* emulator uses host format */
293 #define GET_REAL(r,e) e53toe ((r), (e))
294 #define PUT_REAL(e,r) etoe53 ((e), (r))
296 #endif /* not REAL_ARITHMETIC */
297 #endif /* not TFmode */
298 #endif /* no XFmode */
301 /* Number of 16 bit words in internal format */
304 /* Array offset to exponent */
307 /* Array offset to high guard word */
310 /* Number of bits of precision */
311 #define NBITS ((NI-4)*16)
313 /* Maximum number of decimal digits in ASCII conversion
316 #define NDEC (NBITS*8/27)
318 /* The exponent of 1.0 */
319 #define EXONE (0x3fff)
322 extern int extra_warnings;
323 int ecmp (), enormlz (), eshift ();
324 int eisneg (), eisinf (), eisnan (), eiisinf (), eiisnan (), eiisneg ();
325 void eadd (), esub (), emul (), ediv ();
326 void eshup1 (), eshup8 (), eshup6 (), eshdn1 (), eshdn8 (), eshdn6 ();
327 void eabs (), eneg (), emov (), eclear (), einfin (), efloor ();
328 void eldexp (), efrexp (), eifrac (), euifrac (), ltoe (), ultoe ();
329 void ereal_to_decimal (), eiinfin (), einan ();
330 void esqrt (), elog (), eexp (), etanh (), epow ();
331 void asctoe (), asctoe24 (), asctoe53 (), asctoe64 (), asctoe113 ();
332 void etoasc (), e24toasc (), e53toasc (), e64toasc (), e113toasc ();
333 void etoe64 (), etoe53 (), etoe24 (), e64toe (), e53toe (), e24toe ();
334 void etoe113 (), e113toe ();
335 void mtherr (), make_nan ();
337 extern unsigned EMUSHORT ezero[], ehalf[], eone[], etwo[];
338 extern unsigned EMUSHORT elog2[], esqrt2[];
340 /* Copy 32-bit numbers obtained from array containing 16-bit numbers,
341 swapping ends if required, into output array of longs. The
342 result is normally passed to fprintf by the ASM_OUTPUT_ macros. */
345 unsigned EMUSHORT e[];
347 enum machine_mode mode;
351 #if FLOAT_WORDS_BIG_ENDIAN
356 /* Swap halfwords in the fourth long. */
357 th = (unsigned long) e[6] & 0xffff;
358 t = (unsigned long) e[7] & 0xffff;
364 /* Swap halfwords in the third long. */
365 th = (unsigned long) e[4] & 0xffff;
366 t = (unsigned long) e[5] & 0xffff;
369 /* fall into the double case */
373 /* swap halfwords in the second word */
374 th = (unsigned long) e[2] & 0xffff;
375 t = (unsigned long) e[3] & 0xffff;
378 /* fall into the float case */
382 /* swap halfwords in the first word */
383 th = (unsigned long) e[0] & 0xffff;
384 t = (unsigned long) e[1] & 0xffff;
395 /* Pack the output array without swapping. */
402 /* Pack the fourth long. */
403 th = (unsigned long) e[7] & 0xffff;
404 t = (unsigned long) e[6] & 0xffff;
410 /* Pack the third long.
411 Each element of the input REAL_VALUE_TYPE array has 16 useful bits
413 th = (unsigned long) e[5] & 0xffff;
414 t = (unsigned long) e[4] & 0xffff;
417 /* fall into the double case */
421 /* pack the second long */
422 th = (unsigned long) e[3] & 0xffff;
423 t = (unsigned long) e[2] & 0xffff;
426 /* fall into the float case */
430 /* pack the first long */
431 th = (unsigned long) e[1] & 0xffff;
432 t = (unsigned long) e[0] & 0xffff;
445 /* This is the implementation of the REAL_ARITHMETIC macro.
448 earith (value, icode, r1, r2)
449 REAL_VALUE_TYPE *value;
454 unsigned EMUSHORT d1[NE], d2[NE], v[NE];
460 /* Return NaN input back to the caller. */
463 PUT_REAL (d1, value);
468 PUT_REAL (d2, value);
472 code = (enum tree_code) icode;
480 esub (d2, d1, v); /* d1 - d2 */
488 #ifndef REAL_INFINITY
489 if (ecmp (d2, ezero) == 0)
492 enan (v, eisneg (d1) ^ eisneg (d2));
499 ediv (d2, d1, v); /* d1/d2 */
502 case MIN_EXPR: /* min (d1,d2) */
503 if (ecmp (d1, d2) < 0)
509 case MAX_EXPR: /* max (d1,d2) */
510 if (ecmp (d1, d2) > 0)
523 /* Truncate REAL_VALUE_TYPE toward zero to signed HOST_WIDE_INT
524 * implements REAL_VALUE_RNDZINT (x) (etrunci (x))
530 unsigned EMUSHORT f[NE], g[NE];
546 /* Truncate REAL_VALUE_TYPE toward zero to unsigned HOST_WIDE_INT
547 * implements REAL_VALUE_UNSIGNED_RNDZINT (x) (etruncui (x))
553 unsigned EMUSHORT f[NE], g[NE];
555 unsigned HOST_WIDE_INT l;
569 /* This is the REAL_VALUE_ATOF function.
570 * It converts a decimal string to binary, rounding off
571 * as indicated by the machine_mode argument. Then it
572 * promotes the rounded value to REAL_VALUE_TYPE.
579 unsigned EMUSHORT tem[NE], e[NE];
608 /* Expansion of REAL_NEGATE.
614 unsigned EMUSHORT e[NE];
624 /* Round real toward zero to HOST_WIDE_INT
625 * implements REAL_VALUE_FIX (x).
631 unsigned EMUSHORT f[NE], g[NE];
638 warning ("conversion from NaN to int");
646 /* Round real toward zero to unsigned HOST_WIDE_INT
647 * implements REAL_VALUE_UNSIGNED_FIX (x).
648 * Negative input returns zero.
650 unsigned HOST_WIDE_INT
654 unsigned EMUSHORT f[NE], g[NE];
655 unsigned HOST_WIDE_INT l;
661 warning ("conversion from NaN to unsigned int");
670 /* REAL_VALUE_FROM_INT macro.
673 ereal_from_int (d, i, j)
677 unsigned EMUSHORT df[NE], dg[NE];
678 HOST_WIDE_INT low, high;
686 /* complement and add 1 */
693 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
704 /* REAL_VALUE_FROM_UNSIGNED_INT macro.
707 ereal_from_uint (d, i, j)
709 unsigned HOST_WIDE_INT i, j;
711 unsigned EMUSHORT df[NE], dg[NE];
712 unsigned HOST_WIDE_INT low, high;
716 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
725 /* REAL_VALUE_TO_INT macro
728 ereal_to_int (low, high, rr)
729 HOST_WIDE_INT *low, *high;
732 unsigned EMUSHORT d[NE], df[NE], dg[NE], dh[NE];
739 warning ("conversion from NaN to int");
745 /* convert positive value */
752 eldexp (eone, HOST_BITS_PER_WIDE_INT, df);
753 ediv (df, d, dg); /* dg = d / 2^32 is the high word */
754 euifrac (dg, high, dh);
755 emul (df, dh, dg); /* fractional part is the low word */
756 euifrac (dg, low, dh);
759 /* complement and add 1 */
769 /* REAL_VALUE_LDEXP macro.
776 unsigned EMUSHORT e[NE], y[NE];
789 /* These routines are conditionally compiled because functions
790 * of the same names may be defined in fold-const.c. */
791 #ifdef REAL_ARITHMETIC
793 /* Check for infinity in a REAL_VALUE_TYPE. */
798 unsigned EMUSHORT e[NE];
809 /* Check whether a REAL_VALUE_TYPE item is a NaN. */
815 unsigned EMUSHORT e[NE];
826 /* Check for a negative REAL_VALUE_TYPE number.
827 * this means strictly less than zero, not -0.
834 unsigned EMUSHORT e[NE];
837 if (ecmp (e, ezero) == -1)
842 /* Expansion of REAL_VALUE_TRUNCATE.
843 * The result is in floating point, rounded to nearest or even.
846 real_value_truncate (mode, arg)
847 enum machine_mode mode;
850 unsigned EMUSHORT e[NE], t[NE];
892 #endif /* REAL_ARITHMETIC defined */
894 /* Used for debugging--print the value of R in human-readable format
903 REAL_VALUE_TO_DECIMAL (r, "%.20g", dstr);
904 fprintf (stderr, "%s", dstr);
908 /* Target values are arrays of host longs. A long is guaranteed
909 to be at least 32 bits wide. */
911 /* 128-bit long double */
917 unsigned EMUSHORT e[NE];
921 endian (e, l, TFmode);
924 /* 80-bit long double */
930 unsigned EMUSHORT e[NE];
934 endian (e, l, XFmode);
942 unsigned EMUSHORT e[NE];
946 endian (e, l, DFmode);
953 unsigned EMUSHORT e[NE];
958 endian (e, &l, SFmode);
963 ereal_to_decimal (x, s)
967 unsigned EMUSHORT e[NE];
975 REAL_VALUE_TYPE x, y;
977 unsigned EMUSHORT ex[NE], ey[NE];
981 return (ecmp (ex, ey));
988 unsigned EMUSHORT ex[NE];
991 return (eisneg (ex));
994 /* End of REAL_ARITHMETIC interface */
998 * Extended precision IEEE binary floating point arithmetic routines
1000 * Numbers are stored in C language as arrays of 16-bit unsigned
1001 * short integers. The arguments of the routines are pointers to
1005 * External e type data structure, simulates Intel 8087 chip
1006 * temporary real format but possibly with a larger significand:
1008 * NE-1 significand words (least significant word first,
1009 * most significant bit is normally set)
1010 * exponent (value = EXONE for 1.0,
1011 * top bit is the sign)
1014 * Internal data structure of a number (a "word" is 16 bits):
1016 * ei[0] sign word (0 for positive, 0xffff for negative)
1017 * ei[1] biased exponent (value = EXONE for the number 1.0)
1018 * ei[2] high guard word (always zero after normalization)
1020 * to ei[NI-2] significand (NI-4 significand words,
1021 * most significant word first,
1022 * most significant bit is set)
1023 * ei[NI-1] low guard word (0x8000 bit is rounding place)
1027 * Routines for external format numbers
1029 * asctoe (string, e) ASCII string to extended double e type
1030 * asctoe64 (string, &d) ASCII string to long double
1031 * asctoe53 (string, &d) ASCII string to double
1032 * asctoe24 (string, &f) ASCII string to single
1033 * asctoeg (string, e, prec) ASCII string to specified precision
1034 * e24toe (&f, e) IEEE single precision to e type
1035 * e53toe (&d, e) IEEE double precision to e type
1036 * e64toe (&d, e) IEEE long double precision to e type
1037 * e113toe (&d, e) 128-bit long double precision to e type
1038 * eabs (e) absolute value
1039 * eadd (a, b, c) c = b + a
1041 * ecmp (a, b) Returns 1 if a > b, 0 if a == b,
1042 * -1 if a < b, -2 if either a or b is a NaN.
1043 * ediv (a, b, c) c = b / a
1044 * efloor (a, b) truncate to integer, toward -infinity
1045 * efrexp (a, exp, s) extract exponent and significand
1046 * eifrac (e, &l, frac) e to HOST_WIDE_INT and e type fraction
1047 * euifrac (e, &l, frac) e to unsigned HOST_WIDE_INT and e type fraction
1048 * einfin (e) set e to infinity, leaving its sign alone
1049 * eldexp (a, n, b) multiply by 2**n
1051 * emul (a, b, c) c = b * a
1053 * eround (a, b) b = nearest integer value to a
1054 * esub (a, b, c) c = b - a
1055 * e24toasc (&f, str, n) single to ASCII string, n digits after decimal
1056 * e53toasc (&d, str, n) double to ASCII string, n digits after decimal
1057 * e64toasc (&d, str, n) 80-bit long double to ASCII string
1058 * e113toasc (&d, str, n) 128-bit long double to ASCII string
1059 * etoasc (e, str, n) e to ASCII string, n digits after decimal
1060 * etoe24 (e, &f) convert e type to IEEE single precision
1061 * etoe53 (e, &d) convert e type to IEEE double precision
1062 * etoe64 (e, &d) convert e type to IEEE long double precision
1063 * ltoe (&l, e) HOST_WIDE_INT to e type
1064 * ultoe (&l, e) unsigned HOST_WIDE_INT to e type
1065 * eisneg (e) 1 if sign bit of e != 0, else 0
1066 * eisinf (e) 1 if e has maximum exponent (non-IEEE)
1067 * or is infinite (IEEE)
1068 * eisnan (e) 1 if e is a NaN
1071 * Routines for internal format numbers
1073 * eaddm (ai, bi) add significands, bi = bi + ai
1074 * ecleaz (ei) ei = 0
1075 * ecleazs (ei) set ei = 0 but leave its sign alone
1076 * ecmpm (ai, bi) compare significands, return 1, 0, or -1
1077 * edivm (ai, bi) divide significands, bi = bi / ai
1078 * emdnorm (ai,l,s,exp) normalize and round off
1079 * emovi (a, ai) convert external a to internal ai
1080 * emovo (ai, a) convert internal ai to external a
1081 * emovz (ai, bi) bi = ai, low guard word of bi = 0
1082 * emulm (ai, bi) multiply significands, bi = bi * ai
1083 * enormlz (ei) left-justify the significand
1084 * eshdn1 (ai) shift significand and guards down 1 bit
1085 * eshdn8 (ai) shift down 8 bits
1086 * eshdn6 (ai) shift down 16 bits
1087 * eshift (ai, n) shift ai n bits up (or down if n < 0)
1088 * eshup1 (ai) shift significand and guards up 1 bit
1089 * eshup8 (ai) shift up 8 bits
1090 * eshup6 (ai) shift up 16 bits
1091 * esubm (ai, bi) subtract significands, bi = bi - ai
1092 * eiisinf (ai) 1 if infinite
1093 * eiisnan (ai) 1 if a NaN
1094 * eiisneg (ai) 1 if sign bit of ai != 0, else 0
1095 * einan (ai) set ai = NaN
1096 * eiinfin (ai) set ai = infinity
1099 * The result is always normalized and rounded to NI-4 word precision
1100 * after each arithmetic operation.
1102 * Exception flags are NOT fully supported.
1104 * Signaling NaN's are NOT supported; they are treated the same
1107 * Define INFINITY for support of infinity; otherwise a
1108 * saturation arithmetic is implemented.
1110 * Define NANS for support of Not-a-Number items; otherwise the
1111 * arithmetic will never produce a NaN output, and might be confused
1113 * If NaN's are supported, the output of `ecmp (a,b)' is -2 if
1114 * either a or b is a NaN. This means asking `if (ecmp (a,b) < 0)'
1115 * may not be legitimate. Use `if (ecmp (a,b) == -1)' for `less than'
1118 * Denormals are always supported here where appropriate (e.g., not
1119 * for conversion to DEC numbers).
1126 * Common include file for math routines
1132 * #include "mconf.h"
1138 * This file contains definitions for error codes that are
1139 * passed to the common error handling routine mtherr
1142 * The file also includes a conditional assembly definition
1143 * for the type of computer arithmetic (Intel IEEE, DEC, Motorola
1144 * IEEE, or UNKnown).
1146 * For Digital Equipment PDP-11 and VAX computers, certain
1147 * IBM systems, and others that use numbers with a 56-bit
1148 * significand, the symbol DEC should be defined. In this
1149 * mode, most floating point constants are given as arrays
1150 * of octal integers to eliminate decimal to binary conversion
1151 * errors that might be introduced by the compiler.
1153 * For computers, such as IBM PC, that follow the IEEE
1154 * Standard for Binary Floating Point Arithmetic (ANSI/IEEE
1155 * Std 754-1985), the symbol IBMPC or MIEEE should be defined.
1156 * These numbers have 53-bit significands. In this mode, constants
1157 * are provided as arrays of hexadecimal 16 bit integers.
1159 * To accommodate other types of computer arithmetic, all
1160 * constants are also provided in a normal decimal radix
1161 * which one can hope are correctly converted to a suitable
1162 * format by the available C language compiler. To invoke
1163 * this mode, the symbol UNK is defined.
1165 * An important difference among these modes is a predefined
1166 * set of machine arithmetic constants for each. The numbers
1167 * MACHEP (the machine roundoff error), MAXNUM (largest number
1168 * represented), and several other parameters are preset by
1169 * the configuration symbol. Check the file const.c to
1170 * ensure that these values are correct for your computer.
1172 * For ANSI C compatibility, define ANSIC equal to 1. Currently
1173 * this affects only the atan2 function and others that use it.
1176 /* Constant definitions for math error conditions. */
1178 #define DOMAIN 1 /* argument domain error */
1179 #define SING 2 /* argument singularity */
1180 #define OVERFLOW 3 /* overflow range error */
1181 #define UNDERFLOW 4 /* underflow range error */
1182 #define TLOSS 5 /* total loss of precision */
1183 #define PLOSS 6 /* partial loss of precision */
1184 #define INVALID 7 /* NaN-producing operation */
1186 /* e type constants used by high precision check routines */
1188 #if LONG_DOUBLE_TYPE_SIZE == 128
1190 unsigned EMUSHORT ezero[NE] =
1191 {0x0000, 0x0000, 0x0000, 0x0000,
1192 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,};
1193 extern unsigned EMUSHORT ezero[];
1196 unsigned EMUSHORT ehalf[NE] =
1197 {0x0000, 0x0000, 0x0000, 0x0000,
1198 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3ffe,};
1199 extern unsigned EMUSHORT ehalf[];
1202 unsigned EMUSHORT eone[NE] =
1203 {0x0000, 0x0000, 0x0000, 0x0000,
1204 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3fff,};
1205 extern unsigned EMUSHORT eone[];
1208 unsigned EMUSHORT etwo[NE] =
1209 {0x0000, 0x0000, 0x0000, 0x0000,
1210 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4000,};
1211 extern unsigned EMUSHORT etwo[];
1214 unsigned EMUSHORT e32[NE] =
1215 {0x0000, 0x0000, 0x0000, 0x0000,
1216 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4004,};
1217 extern unsigned EMUSHORT e32[];
1219 /* 6.93147180559945309417232121458176568075500134360255E-1 */
1220 unsigned EMUSHORT elog2[NE] =
1221 {0x40f3, 0xf6af, 0x03f2, 0xb398,
1222 0xc9e3, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1223 extern unsigned EMUSHORT elog2[];
1225 /* 1.41421356237309504880168872420969807856967187537695E0 */
1226 unsigned EMUSHORT esqrt2[NE] =
1227 {0x1d6f, 0xbe9f, 0x754a, 0x89b3,
1228 0x597d, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1229 extern unsigned EMUSHORT esqrt2[];
1231 /* 3.14159265358979323846264338327950288419716939937511E0 */
1232 unsigned EMUSHORT epi[NE] =
1233 {0x2902, 0x1cd1, 0x80dc, 0x628b,
1234 0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1235 extern unsigned EMUSHORT epi[];
1238 /* LONG_DOUBLE_TYPE_SIZE is other than 128 */
1239 unsigned EMUSHORT ezero[NE] =
1240 {0, 0000000, 0000000, 0000000, 0000000, 0000000,};
1241 unsigned EMUSHORT ehalf[NE] =
1242 {0, 0000000, 0000000, 0000000, 0100000, 0x3ffe,};
1243 unsigned EMUSHORT eone[NE] =
1244 {0, 0000000, 0000000, 0000000, 0100000, 0x3fff,};
1245 unsigned EMUSHORT etwo[NE] =
1246 {0, 0000000, 0000000, 0000000, 0100000, 0040000,};
1247 unsigned EMUSHORT e32[NE] =
1248 {0, 0000000, 0000000, 0000000, 0100000, 0040004,};
1249 unsigned EMUSHORT elog2[NE] =
1250 {0xc9e4, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
1251 unsigned EMUSHORT esqrt2[NE] =
1252 {0x597e, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
1253 unsigned EMUSHORT epi[NE] =
1254 {0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
1259 /* Control register for rounding precision.
1260 * This can be set to 113 (if NE=10), 80 (if NE=6), 64, 56, 53, or 24 bits.
1265 void eaddm (), esubm (), emdnorm (), asctoeg ();
1266 static void toe24 (), toe53 (), toe64 (), toe113 ();
1267 void eremain (), einit (), eiremain ();
1268 int ecmpm (), edivm (), emulm ();
1269 void emovi (), emovo (), emovz (), ecleaz (), ecleazs (), eadd1 ();
1271 void etodec (), todec (), dectoe ();
1274 void etoibm (), toibm (), ibmtoe ();
1284 ; Clear out entire external format number.
1286 ; unsigned EMUSHORT x[];
1292 register unsigned EMUSHORT *x;
1296 for (i = 0; i < NE; i++)
1302 /* Move external format number from a to b.
1309 register unsigned EMUSHORT *a, *b;
1313 for (i = 0; i < NE; i++)
1319 ; Absolute value of external format number
1327 unsigned EMUSHORT x[]; /* x is the memory address of a short */
1330 x[NE - 1] &= 0x7fff; /* sign is top bit of last word of external format */
1337 ; Negate external format number
1339 ; unsigned EMUSHORT x[NE];
1345 unsigned EMUSHORT x[];
1348 x[NE - 1] ^= 0x8000; /* Toggle the sign bit */
1353 /* Return 1 if sign bit of external format number is nonzero,
1358 unsigned EMUSHORT x[];
1361 if (x[NE - 1] & 0x8000)
1368 /* Return 1 if external format number is infinity.
1373 unsigned EMUSHORT x[];
1380 if ((x[NE - 1] & 0x7fff) == 0x7fff)
1387 /* Check if e-type number is not a number.
1388 The bit pattern is one that we defined, so we know for sure how to
1393 unsigned EMUSHORT x[];
1398 /* NaN has maximum exponent */
1399 if ((x[NE - 1] & 0x7fff) != 0x7fff)
1401 /* ... and non-zero significand field. */
1402 for (i = 0; i < NE - 1; i++)
1411 /* Fill external format number with infinity pattern (IEEE)
1412 or largest possible number (non-IEEE). */
1416 register unsigned EMUSHORT *x;
1421 for (i = 0; i < NE - 1; i++)
1425 for (i = 0; i < NE - 1; i++)
1454 /* Output an e-type NaN.
1455 This generates Intel's quiet NaN pattern for extended real.
1456 The exponent is 7fff, the leading mantissa word is c000. */
1460 register unsigned EMUSHORT *x;
1465 for (i = 0; i < NE - 2; i++)
1468 *x = (sign << 15) | 0x7fff;
1472 /* Move in external format number,
1473 * converting it to internal format.
1477 unsigned EMUSHORT *a, *b;
1479 register unsigned EMUSHORT *p, *q;
1483 p = a + (NE - 1); /* point to last word of external number */
1484 /* get the sign bit */
1489 /* get the exponent */
1491 *q++ &= 0x7fff; /* delete the sign bit */
1493 if ((*(q - 1) & 0x7fff) == 0x7fff)
1499 for (i = 3; i < NI; i++)
1504 for (i = 2; i < NI; i++)
1509 /* clear high guard word */
1511 /* move in the significand */
1512 for (i = 0; i < NE - 1; i++)
1514 /* clear low guard word */
1519 /* Move internal format number out,
1520 * converting it to external format.
1524 unsigned EMUSHORT *a, *b;
1526 register unsigned EMUSHORT *p, *q;
1527 unsigned EMUSHORT i;
1531 q = b + (NE - 1); /* point to output exponent */
1532 /* combine sign and exponent */
1535 *q-- = *p++ | 0x8000;
1539 if (*(p - 1) == 0x7fff)
1544 enan (b, eiisneg (a));
1552 /* skip over guard word */
1554 /* move the significand */
1555 for (j = 0; j < NE - 1; j++)
1562 /* Clear out internal format number.
1567 register unsigned EMUSHORT *xi;
1571 for (i = 0; i < NI; i++)
1576 /* same, but don't touch the sign. */
1580 register unsigned EMUSHORT *xi;
1585 for (i = 0; i < NI - 1; i++)
1591 /* Move internal format number from a to b.
1595 register unsigned EMUSHORT *a, *b;
1599 for (i = 0; i < NI - 1; i++)
1601 /* clear low guard word */
1605 /* Generate internal format NaN.
1606 The explicit pattern for this is maximum exponent and
1607 top two significand bits set. */
1611 unsigned EMUSHORT x[];
1619 /* Return nonzero if internal format number is a NaN. */
1623 unsigned EMUSHORT x[];
1627 if ((x[E] & 0x7fff) == 0x7fff)
1629 for (i = M + 1; i < NI; i++)
1638 /* Return nonzero if sign of internal format number is nonzero. */
1642 unsigned EMUSHORT x[];
1648 /* Fill internal format number with infinity pattern.
1649 This has maximum exponent and significand all zeros. */
1653 unsigned EMUSHORT x[];
1660 /* Return nonzero if internal format number is infinite. */
1664 unsigned EMUSHORT x[];
1671 if ((x[E] & 0x7fff) == 0x7fff)
1678 ; Compare significands of numbers in internal format.
1679 ; Guard words are included in the comparison.
1681 ; unsigned EMUSHORT a[NI], b[NI];
1684 ; for the significands:
1685 ; returns +1 if a > b
1691 register unsigned EMUSHORT *a, *b;
1695 a += M; /* skip up to significand area */
1697 for (i = M; i < NI; i++)
1705 if (*(--a) > *(--b))
1713 ; Shift significand down by 1 bit
1718 register unsigned EMUSHORT *x;
1720 register unsigned EMUSHORT bits;
1723 x += M; /* point to significand area */
1726 for (i = M; i < NI; i++)
1741 ; Shift significand up by 1 bit
1746 register unsigned EMUSHORT *x;
1748 register unsigned EMUSHORT bits;
1754 for (i = M; i < NI; i++)
1769 ; Shift significand down by 8 bits
1774 register unsigned EMUSHORT *x;
1776 register unsigned EMUSHORT newbyt, oldbyt;
1781 for (i = M; i < NI; i++)
1792 ; Shift significand up by 8 bits
1797 register unsigned EMUSHORT *x;
1800 register unsigned EMUSHORT newbyt, oldbyt;
1805 for (i = M; i < NI; i++)
1816 ; Shift significand up by 16 bits
1821 register unsigned EMUSHORT *x;
1824 register unsigned EMUSHORT *p;
1829 for (i = M; i < NI - 1; i++)
1836 ; Shift significand down by 16 bits
1841 register unsigned EMUSHORT *x;
1844 register unsigned EMUSHORT *p;
1849 for (i = M; i < NI - 1; i++)
1862 unsigned EMUSHORT *x, *y;
1864 register unsigned EMULONG a;
1871 for (i = M; i < NI; i++)
1873 a = (unsigned EMULONG) (*x) + (unsigned EMULONG) (*y) + carry;
1878 *y = (unsigned EMUSHORT) a;
1885 ; Subtract significands
1891 unsigned EMUSHORT *x, *y;
1900 for (i = M; i < NI; i++)
1902 a = (unsigned EMULONG) (*y) - (unsigned EMULONG) (*x) - carry;
1907 *y = (unsigned EMUSHORT) a;
1914 static unsigned EMUSHORT equot[NI];
1918 /* Radix 2 shift-and-add versions of multiply and divide */
1921 /* Divide significands */
1925 unsigned EMUSHORT den[], num[];
1928 register unsigned EMUSHORT *p, *q;
1929 unsigned EMUSHORT j;
1935 for (i = M; i < NI; i++)
1940 /* Use faster compare and subtraction if denominator
1941 * has only 15 bits of significance.
1946 for (i = M + 3; i < NI; i++)
1951 if ((den[M + 1] & 1) != 0)
1959 for (i = 0; i < NBITS + 2; i++)
1977 /* The number of quotient bits to calculate is
1978 * NBITS + 1 scaling guard bit + 1 roundoff bit.
1983 for (i = 0; i < NBITS + 2; i++)
1985 if (ecmpm (den, num) <= 0)
1988 j = 1; /* quotient bit = 1 */
2002 /* test for nonzero remainder after roundoff bit */
2005 for (i = M; i < NI; i++)
2013 for (i = 0; i < NI; i++)
2019 /* Multiply significands */
2022 unsigned EMUSHORT a[], b[];
2024 unsigned EMUSHORT *p, *q;
2029 for (i = M; i < NI; i++)
2034 while (*p == 0) /* significand is not supposed to be all zero */
2039 if ((*p & 0xff) == 0)
2047 for (i = 0; i < k; i++)
2051 /* remember if there were any nonzero bits shifted out */
2058 for (i = 0; i < NI; i++)
2061 /* return flag for lost nonzero bits */
2067 /* Radix 65536 versions of multiply and divide */
2070 /* Multiply significand of e-type number b
2071 by 16-bit quantity a, e-type result to c. */
2076 unsigned short b[], c[];
2078 register unsigned short *pp;
2079 register unsigned long carry;
2081 unsigned short p[NI];
2082 unsigned long aa, m;
2091 for (i=M+1; i<NI; i++)
2101 m = (unsigned long) aa * *ps--;
2102 carry = (m & 0xffff) + *pp;
2103 *pp-- = (unsigned short)carry;
2104 carry = (carry >> 16) + (m >> 16) + *pp;
2105 *pp = (unsigned short)carry;
2106 *(pp-1) = carry >> 16;
2109 for (i=M; i<NI; i++)
2114 /* Divide significands. Neither the numerator nor the denominator
2115 is permitted to have its high guard word nonzero. */
2119 unsigned short den[], num[];
2122 register unsigned short *p;
2124 unsigned short j, tdenm, tquot;
2125 unsigned short tprod[NI+1];
2131 for (i=M; i<NI; i++)
2137 for (i=M; i<NI; i++)
2139 /* Find trial quotient digit (the radix is 65536). */
2140 tnum = (((unsigned long) num[M]) << 16) + num[M+1];
2142 /* Do not execute the divide instruction if it will overflow. */
2143 if ((tdenm * 0xffffL) < tnum)
2146 tquot = tnum / tdenm;
2147 /* Multiply denominator by trial quotient digit. */
2148 m16m (tquot, den, tprod);
2149 /* The quotient digit may have been overestimated. */
2150 if (ecmpm (tprod, num) > 0)
2154 if (ecmpm (tprod, num) > 0)
2164 /* test for nonzero remainder after roundoff bit */
2167 for (i=M; i<NI; i++)
2174 for (i=0; i<NI; i++)
2182 /* Multiply significands */
2185 unsigned short a[], b[];
2187 unsigned short *p, *q;
2188 unsigned short pprod[NI];
2194 for (i=M; i<NI; i++)
2200 for (i=M+1; i<NI; i++)
2208 m16m (*p--, b, pprod);
2209 eaddm(pprod, equot);
2215 for (i=0; i<NI; i++)
2218 /* return flag for lost nonzero bits */
2225 * Normalize and round off.
2227 * The internal format number to be rounded is "s".
2228 * Input "lost" indicates whether or not the number is exact.
2229 * This is the so-called sticky bit.
2231 * Input "subflg" indicates whether the number was obtained
2232 * by a subtraction operation. In that case if lost is nonzero
2233 * then the number is slightly smaller than indicated.
2235 * Input "exp" is the biased exponent, which may be negative.
2236 * the exponent field of "s" is ignored but is replaced by
2237 * "exp" as adjusted by normalization and rounding.
2239 * Input "rcntrl" is the rounding control.
2242 /* For future reference: In order for emdnorm to round off denormal
2243 significands at the right point, the input exponent must be
2244 adjusted to be the actual value it would have after conversion to
2245 the final floating point type. This adjustment has been
2246 implemented for all type conversions (etoe53, etc.) and decimal
2247 conversions, but not for the arithmetic functions (eadd, etc.).
2248 Data types having standard 15-bit exponents are not affected by
2249 this, but SFmode and DFmode are affected. For example, ediv with
2250 rndprc = 24 will not round correctly to 24-bit precision if the
2251 result is denormal. */
2253 static int rlast = -1;
2255 static unsigned EMUSHORT rmsk = 0;
2256 static unsigned EMUSHORT rmbit = 0;
2257 static unsigned EMUSHORT rebit = 0;
2259 static unsigned EMUSHORT rbit[NI];
2262 emdnorm (s, lost, subflg, exp, rcntrl)
2263 unsigned EMUSHORT s[];
2270 unsigned EMUSHORT r;
2275 /* a blank significand could mean either zero or infinity. */
2288 if ((j > NBITS) && (exp < 32767))
2296 if (exp > (EMULONG) (-NBITS - 1))
2309 /* Round off, unless told not to by rcntrl. */
2312 /* Set up rounding parameters if the control register changed. */
2313 if (rndprc != rlast)
2320 rw = NI - 1; /* low guard word */
2340 /* For DEC or IBM arithmetic */
2367 /* Shift down 1 temporarily if the data structure has an implied
2368 most significant bit and the number is denormal. */
2369 if ((exp <= 0) && (rndprc != 64) && (rndprc != NBITS))
2371 lost |= s[NI - 1] & 1;
2374 /* Clear out all bits below the rounding bit,
2375 remembering in r if any were nonzero. */
2389 if ((r & rmbit) != 0)
2394 { /* round to even */
2395 if ((s[re] & rebit) == 0)
2407 if ((exp <= 0) && (rndprc != 64) && (rndprc != NBITS))
2412 { /* overflow on roundoff */
2425 for (i = 2; i < NI - 1; i++)
2428 warning ("floating point overflow");
2432 for (i = M + 1; i < NI - 1; i++)
2435 if ((rndprc < 64) || (rndprc == 113))
2450 s[1] = (unsigned EMUSHORT) exp;
2456 ; Subtract external format numbers.
2458 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2459 ; esub (a, b, c); c = b - a
2462 static int subflg = 0;
2466 unsigned EMUSHORT *a, *b, *c;
2480 /* Infinity minus infinity is a NaN.
2481 Test for subtracting infinities of the same sign. */
2482 if (eisinf (a) && eisinf (b)
2483 && ((eisneg (a) ^ eisneg (b)) == 0))
2485 mtherr ("esub", INVALID);
2498 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2499 ; eadd (a, b, c); c = b + a
2503 unsigned EMUSHORT *a, *b, *c;
2507 /* NaN plus anything is a NaN. */
2518 /* Infinity minus infinity is a NaN.
2519 Test for adding infinities of opposite signs. */
2520 if (eisinf (a) && eisinf (b)
2521 && ((eisneg (a) ^ eisneg (b)) != 0))
2523 mtherr ("esub", INVALID);
2534 unsigned EMUSHORT *a, *b, *c;
2536 unsigned EMUSHORT ai[NI], bi[NI], ci[NI];
2538 EMULONG lt, lta, ltb;
2559 /* compare exponents */
2564 { /* put the larger number in bi */
2574 if (lt < (EMULONG) (-NBITS - 1))
2575 goto done; /* answer same as larger addend */
2577 lost = eshift (ai, k); /* shift the smaller number down */
2581 /* exponents were the same, so must compare significands */
2584 { /* the numbers are identical in magnitude */
2585 /* if different signs, result is zero */
2591 /* if same sign, result is double */
2592 /* double denomalized tiny number */
2593 if ((bi[E] == 0) && ((bi[3] & 0x8000) == 0))
2598 /* add 1 to exponent unless both are zero! */
2599 for (j = 1; j < NI - 1; j++)
2603 /* This could overflow, but let emovo take care of that. */
2608 bi[E] = (unsigned EMUSHORT) ltb;
2612 { /* put the larger number in bi */
2628 emdnorm (bi, lost, subflg, ltb, 64);
2639 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2640 ; ediv (a, b, c); c = b / a
2644 unsigned EMUSHORT *a, *b, *c;
2646 unsigned EMUSHORT ai[NI], bi[NI];
2648 EMULONG lt, lta, ltb;
2651 /* Return any NaN input. */
2662 /* Zero over zero, or infinity over infinity, is a NaN. */
2663 if (((ecmp (a, ezero) == 0) && (ecmp (b, ezero) == 0))
2664 || (eisinf (a) && eisinf (b)))
2666 mtherr ("ediv", INVALID);
2667 enan (c, eisneg (a) ^ eisneg (b));
2671 /* Infinity over anything else is infinity. */
2675 if (eisneg (a) ^ eisneg (b))
2676 *(c + (NE - 1)) = 0x8000;
2678 *(c + (NE - 1)) = 0;
2682 /* Anything else over infinity is zero. */
2694 { /* See if numerator is zero. */
2695 for (i = 1; i < NI - 1; i++)
2699 ltb -= enormlz (bi);
2709 { /* possible divide by zero */
2710 for (i = 1; i < NI - 1; i++)
2714 lta -= enormlz (ai);
2719 *(c + (NE - 1)) = 0;
2721 *(c + (NE - 1)) = 0x8000;
2722 /* Divide by zero is not an invalid operation.
2723 It is a divide-by-zero operation! */
2725 mtherr ("ediv", SING);
2731 /* calculate exponent */
2732 lt = ltb - lta + EXONE;
2733 emdnorm (bi, i, 0, lt, 64);
2747 ; unsigned EMUSHORT a[NE], b[NE], c[NE];
2748 ; emul (a, b, c); c = b * a
2752 unsigned EMUSHORT *a, *b, *c;
2754 unsigned EMUSHORT ai[NI], bi[NI];
2756 EMULONG lt, lta, ltb;
2759 /* NaN times anything is the same NaN. */
2770 /* Zero times infinity is a NaN. */
2771 if ((eisinf (a) && (ecmp (b, ezero) == 0))
2772 || (eisinf (b) && (ecmp (a, ezero) == 0)))
2774 mtherr ("emul", INVALID);
2775 enan (c, eisneg (a) ^ eisneg (b));
2779 /* Infinity times anything else is infinity. */
2781 if (eisinf (a) || eisinf (b))
2783 if (eisneg (a) ^ eisneg (b))
2784 *(c + (NE - 1)) = 0x8000;
2786 *(c + (NE - 1)) = 0;
2797 for (i = 1; i < NI - 1; i++)
2801 lta -= enormlz (ai);
2812 for (i = 1; i < NI - 1; i++)
2816 ltb -= enormlz (bi);
2825 /* Multiply significands */
2827 /* calculate exponent */
2828 lt = lta + ltb - (EXONE - 1);
2829 emdnorm (bi, j, 0, lt, 64);
2830 /* calculate sign of product */
2842 ; Convert IEEE double precision to e type
2844 ; unsigned EMUSHORT x[N+2];
2849 unsigned EMUSHORT *pe, *y;
2853 dectoe (pe, y); /* see etodec.c */
2858 ibmtoe (pe, y, DFmode);
2861 register unsigned EMUSHORT r;
2862 register unsigned EMUSHORT *e, *p;
2863 unsigned EMUSHORT yy[NI];
2867 denorm = 0; /* flag if denormalized number */
2876 yy[M] = (r & 0x0f) | 0x10;
2877 r &= ~0x800f; /* strip sign and 4 significand bits */
2883 if (((pe[3] & 0xf) != 0) || (pe[2] != 0)
2884 || (pe[1] != 0) || (pe[0] != 0))
2886 enan (y, yy[0] != 0);
2890 if (((pe[0] & 0xf) != 0) || (pe[1] != 0)
2891 || (pe[2] != 0) || (pe[3] != 0))
2893 enan (y, yy[0] != 0);
2904 #endif /* INFINITY */
2906 /* If zero exponent, then the significand is denormalized.
2907 * So, take back the understood high significand bit. */
2929 { /* if zero exponent, then normalize the significand */
2930 if ((k = enormlz (yy)) > NBITS)
2933 yy[E] -= (unsigned EMUSHORT) (k - 1);
2936 #endif /* not IBM */
2937 #endif /* not DEC */
2942 unsigned EMUSHORT *pe, *y;
2944 unsigned EMUSHORT yy[NI];
2945 unsigned EMUSHORT *e, *p, *q;
2950 for (i = 0; i < NE - 5; i++)
2953 for (i = 0; i < 5; i++)
2956 /* This precision is not ordinarily supported on DEC or IBM. */
2958 for (i = 0; i < 5; i++)
2962 p = &yy[0] + (NE - 1);
2965 for (i = 0; i < 5; i++)
2969 p = &yy[0] + (NE - 1);
2972 for (i = 0; i < 4; i++)
2982 for (i = 0; i < 4; i++)
2986 enan (y, (*p & 0x8000) != 0);
2991 for (i = 1; i <= 4; i++)
2995 enan (y, (*p & 0x8000) != 0);
3007 #endif /* INFINITY */
3008 for (i = 0; i < NE; i++)
3015 unsigned EMUSHORT *pe, *y;
3017 register unsigned EMUSHORT r;
3018 unsigned EMUSHORT *e, *p;
3019 unsigned EMUSHORT yy[NI];
3038 for (i = 0; i < 7; i++)
3042 enan (y, yy[0] != 0);
3047 for (i = 1; i < 8; i++)
3051 enan (y, yy[0] != 0);
3063 #endif /* INFINITY */
3067 for (i = 0; i < 7; i++)
3072 for (i = 0; i < 7; i++)
3075 /* If denormal, remove the implied bit; else shift down 1. */
3090 ; Convert IEEE single precision to e type
3092 ; unsigned EMUSHORT x[N+2];
3097 unsigned EMUSHORT *pe, *y;
3101 ibmtoe (pe, y, SFmode);
3104 register unsigned EMUSHORT r;
3105 register unsigned EMUSHORT *e, *p;
3106 unsigned EMUSHORT yy[NI];
3110 denorm = 0; /* flag if denormalized number */
3122 yy[M] = (r & 0x7f) | 0200;
3123 r &= ~0x807f; /* strip sign and 7 significand bits */
3129 if (((pe[0] & 0x7f) != 0) || (pe[1] != 0))
3131 enan (y, yy[0] != 0);
3135 if (((pe[1] & 0x7f) != 0) || (pe[0] != 0))
3137 enan (y, yy[0] != 0);
3148 #endif /* INFINITY */
3150 /* If zero exponent, then the significand is denormalized.
3151 * So, take back the understood high significand bit. */
3172 { /* if zero exponent, then normalize the significand */
3173 if ((k = enormlz (yy)) > NBITS)
3176 yy[E] -= (unsigned EMUSHORT) (k - 1);
3179 #endif /* not IBM */
3185 unsigned EMUSHORT *x, *e;
3187 unsigned EMUSHORT xi[NI];
3194 make_nan (e, eisneg (x), TFmode);
3199 exp = (EMULONG) xi[E];
3204 /* round off to nearest or even */
3207 emdnorm (xi, 0, 0, exp, 64);
3213 /* move out internal format to ieee long double */
3216 unsigned EMUSHORT *a, *b;
3218 register unsigned EMUSHORT *p, *q;
3219 unsigned EMUSHORT i;
3224 make_nan (b, eiisneg (a), TFmode);
3232 q = b + 7; /* point to output exponent */
3235 /* If not denormal, delete the implied bit. */
3240 /* combine sign and exponent */
3244 *q++ = *p++ | 0x8000;
3249 *q-- = *p++ | 0x8000;
3253 /* skip over guard word */
3255 /* move the significand */
3257 for (i = 0; i < 7; i++)
3260 for (i = 0; i < 7; i++)
3267 unsigned EMUSHORT *x, *e;
3269 unsigned EMUSHORT xi[NI];
3276 make_nan (e, eisneg (x), XFmode);
3281 /* adjust exponent for offset */
3282 exp = (EMULONG) xi[E];
3287 /* round off to nearest or even */
3290 emdnorm (xi, 0, 0, exp, 64);
3296 /* move out internal format to ieee long double */
3299 unsigned EMUSHORT *a, *b;
3301 register unsigned EMUSHORT *p, *q;
3302 unsigned EMUSHORT i;
3307 make_nan (b, eiisneg (a), XFmode);
3312 #if defined(MIEEE) || defined(IBM)
3315 q = b + 4; /* point to output exponent */
3316 #if LONG_DOUBLE_TYPE_SIZE == 96
3317 /* Clear the last two bytes of 12-byte Intel format */
3322 /* combine sign and exponent */
3324 #if defined(MIEEE) || defined(IBM)
3326 *q++ = *p++ | 0x8000;
3332 *q-- = *p++ | 0x8000;
3336 /* skip over guard word */
3338 /* move the significand */
3339 #if defined(MIEEE) || defined(IBM)
3340 for (i = 0; i < 4; i++)
3343 for (i = 0; i < 4; i++)
3350 ; e type to IEEE double precision
3352 ; unsigned EMUSHORT x[NE];
3360 unsigned EMUSHORT *x, *e;
3362 etodec (x, e); /* see etodec.c */
3367 unsigned EMUSHORT *x, *y;
3377 unsigned EMUSHORT *x, *e;
3379 etoibm (x, e, DFmode);
3384 unsigned EMUSHORT *x, *y;
3386 toibm (x, y, DFmode);
3389 #else /* it's neither DEC nor IBM */
3393 unsigned EMUSHORT *x, *e;
3395 unsigned EMUSHORT xi[NI];
3402 make_nan (e, eisneg (x), DFmode);
3407 /* adjust exponent for offsets */
3408 exp = (EMULONG) xi[E] - (EXONE - 0x3ff);
3413 /* round off to nearest or even */
3416 emdnorm (xi, 0, 0, exp, 64);
3425 unsigned EMUSHORT *x, *y;
3427 unsigned EMUSHORT i;
3428 unsigned EMUSHORT *p;
3433 make_nan (y, eiisneg (x), DFmode);
3441 *y = 0; /* output high order */
3443 *y = 0x8000; /* output sign bit */
3446 if (i >= (unsigned int) 2047)
3447 { /* Saturate at largest number less than infinity. */
3462 *y |= (unsigned EMUSHORT) 0x7fef;
3486 i |= *p++ & (unsigned EMUSHORT) 0x0f; /* *p = xi[M] */
3487 *y |= (unsigned EMUSHORT) i; /* high order output already has sign bit set */
3501 #endif /* not IBM */
3502 #endif /* not DEC */
3507 ; e type to IEEE single precision
3509 ; unsigned EMUSHORT x[N+2];
3516 unsigned EMUSHORT *x, *e;
3518 etoibm (x, e, SFmode);
3523 unsigned EMUSHORT *x, *y;
3525 toibm (x, y, SFmode);
3532 unsigned EMUSHORT *x, *e;
3535 unsigned EMUSHORT xi[NI];
3541 make_nan (e, eisneg (x), SFmode);
3546 /* adjust exponent for offsets */
3547 exp = (EMULONG) xi[E] - (EXONE - 0177);
3552 /* round off to nearest or even */
3555 emdnorm (xi, 0, 0, exp, 64);
3563 unsigned EMUSHORT *x, *y;
3565 unsigned EMUSHORT i;
3566 unsigned EMUSHORT *p;
3571 make_nan (y, eiisneg (x), SFmode);
3582 *y = 0; /* output high order */
3584 *y = 0x8000; /* output sign bit */
3587 /* Handle overflow cases. */
3591 *y |= (unsigned EMUSHORT) 0x7f80;
3602 #else /* no INFINITY */
3603 *y |= (unsigned EMUSHORT) 0x7f7f;
3617 #endif /* no INFINITY */
3629 i |= *p++ & (unsigned EMUSHORT) 0x7f; /* *p = xi[M] */
3630 *y |= i; /* high order output already has sign bit set */
3642 #endif /* not IBM */
3644 /* Compare two e type numbers.
3646 * unsigned EMUSHORT a[NE], b[NE];
3649 * returns +1 if a > b
3652 * -2 if either a or b is a NaN.
3656 unsigned EMUSHORT *a, *b;
3658 unsigned EMUSHORT ai[NI], bi[NI];
3659 register unsigned EMUSHORT *p, *q;
3664 if (eisnan (a) || eisnan (b))
3673 { /* the signs are different */
3675 for (i = 1; i < NI - 1; i++)
3689 /* both are the same sign */
3704 return (0); /* equality */
3710 if (*(--p) > *(--q))
3711 return (msign); /* p is bigger */
3713 return (-msign); /* p is littler */
3719 /* Find nearest integer to x = floor (x + 0.5)
3721 * unsigned EMUSHORT x[NE], y[NE]
3726 unsigned EMUSHORT *x, *y;
3736 ; convert HOST_WIDE_INT to e type
3739 ; unsigned EMUSHORT x[NE];
3741 ; note &l is the memory address of l
3746 unsigned EMUSHORT *y;
3748 unsigned EMUSHORT yi[NI];
3749 unsigned HOST_WIDE_INT ll;
3755 /* make it positive */
3756 ll = (unsigned HOST_WIDE_INT) (-(*lp));
3757 yi[0] = 0xffff; /* put correct sign in the e type number */
3761 ll = (unsigned HOST_WIDE_INT) (*lp);
3763 /* move the long integer to yi significand area */
3764 #if HOST_BITS_PER_WIDE_INT == 64
3765 yi[M] = (unsigned EMUSHORT) (ll >> 48);
3766 yi[M + 1] = (unsigned EMUSHORT) (ll >> 32);
3767 yi[M + 2] = (unsigned EMUSHORT) (ll >> 16);
3768 yi[M + 3] = (unsigned EMUSHORT) ll;
3769 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
3771 yi[M] = (unsigned EMUSHORT) (ll >> 16);
3772 yi[M + 1] = (unsigned EMUSHORT) ll;
3773 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
3776 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
3777 ecleaz (yi); /* it was zero */
3779 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
3780 emovo (yi, y); /* output the answer */
3784 ; convert unsigned HOST_WIDE_INT to e type
3786 ; unsigned HOST_WIDE_INT l;
3787 ; unsigned EMUSHORT x[NE];
3789 ; note &l is the memory address of l
3793 unsigned HOST_WIDE_INT *lp;
3794 unsigned EMUSHORT *y;
3796 unsigned EMUSHORT yi[NI];
3797 unsigned HOST_WIDE_INT ll;
3803 /* move the long integer to ayi significand area */
3804 #if HOST_BITS_PER_WIDE_INT == 64
3805 yi[M] = (unsigned EMUSHORT) (ll >> 48);
3806 yi[M + 1] = (unsigned EMUSHORT) (ll >> 32);
3807 yi[M + 2] = (unsigned EMUSHORT) (ll >> 16);
3808 yi[M + 3] = (unsigned EMUSHORT) ll;
3809 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
3811 yi[M] = (unsigned EMUSHORT) (ll >> 16);
3812 yi[M + 1] = (unsigned EMUSHORT) ll;
3813 yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */
3816 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
3817 ecleaz (yi); /* it was zero */
3819 yi[E] -= (unsigned EMUSHORT) k; /* subtract shift count from exponent */
3820 emovo (yi, y); /* output the answer */
3824 /* Find signed HOST_WIDE_INT integer and floating point fractional
3825 parts of e-type (packed internal format) floating point input X.
3826 The integer output I has the sign of the input, except that
3827 positive overflow is permitted if FIXUNS_TRUNC_LIKE_FIX_TRUNC.
3828 The output e-type fraction FRAC is the positive fractional
3833 unsigned EMUSHORT *x;
3835 unsigned EMUSHORT *frac;
3837 unsigned EMUSHORT xi[NI];
3839 unsigned HOST_WIDE_INT ll;
3842 k = (int) xi[E] - (EXONE - 1);
3845 /* if exponent <= 0, integer = 0 and real output is fraction */
3850 if (k > (HOST_BITS_PER_WIDE_INT - 1))
3852 /* long integer overflow: output large integer
3853 and correct fraction */
3855 *i = ((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1);
3858 #ifdef FIXUNS_TRUNC_LIKE_FIX_TRUNC
3859 /* In this case, let it overflow and convert as if unsigned. */
3860 euifrac (x, &ll, frac);
3861 *i = (HOST_WIDE_INT) ll;
3864 /* In other cases, return the largest positive integer. */
3865 *i = (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1;
3870 warning ("overflow on truncation to integer");
3874 /* Shift more than 16 bits: first shift up k-16 mod 16,
3875 then shift up by 16's. */
3876 j = k - ((k >> 4) << 4);
3883 ll = (ll << 16) | xi[M];
3885 while ((k -= 16) > 0);
3892 /* shift not more than 16 bits */
3894 *i = (HOST_WIDE_INT) xi[M] & 0xffff;
3901 if ((k = enormlz (xi)) > NBITS)
3904 xi[E] -= (unsigned EMUSHORT) k;
3910 /* Find unsigned HOST_WIDE_INT integer and floating point fractional parts.
3911 A negative e type input yields integer output = 0
3912 but correct fraction. */
3915 euifrac (x, i, frac)
3916 unsigned EMUSHORT *x;
3917 unsigned HOST_WIDE_INT *i;
3918 unsigned EMUSHORT *frac;
3920 unsigned HOST_WIDE_INT ll;
3921 unsigned EMUSHORT xi[NI];
3925 k = (int) xi[E] - (EXONE - 1);
3928 /* if exponent <= 0, integer = 0 and argument is fraction */
3933 if (k > HOST_BITS_PER_WIDE_INT)
3935 /* Long integer overflow: output large integer
3936 and correct fraction.
3937 Note, the BSD microvax compiler says that ~(0UL)
3938 is a syntax error. */
3942 warning ("overflow on truncation to unsigned integer");
3946 /* Shift more than 16 bits: first shift up k-16 mod 16,
3947 then shift up by 16's. */
3948 j = k - ((k >> 4) << 4);
3955 ll = (ll << 16) | xi[M];
3957 while ((k -= 16) > 0);
3962 /* shift not more than 16 bits */
3964 *i = (HOST_WIDE_INT) xi[M] & 0xffff;
3967 if (xi[0]) /* A negative value yields unsigned integer 0. */
3973 if ((k = enormlz (xi)) > NBITS)
3976 xi[E] -= (unsigned EMUSHORT) k;
3986 ; Shifts significand area up or down by the number of bits
3987 ; given by the variable sc.
3991 unsigned EMUSHORT *x;
3994 unsigned EMUSHORT lost;
3995 unsigned EMUSHORT *p;
4008 lost |= *p; /* remember lost bits */
4049 return ((int) lost);
4057 ; Shift normalizes the significand area pointed to by argument
4058 ; shift count (up = positive) is returned.
4062 unsigned EMUSHORT x[];
4064 register unsigned EMUSHORT *p;
4073 return (0); /* already normalized */
4078 /* With guard word, there are NBITS+16 bits available.
4079 * return true if all are zero.
4084 /* see if high byte is zero */
4085 while ((*p & 0xff00) == 0)
4090 /* now shift 1 bit at a time */
4091 while ((*p & 0x8000) == 0)
4097 mtherr ("enormlz", UNDERFLOW);
4103 /* Normalize by shifting down out of the high guard word
4104 of the significand */
4119 mtherr ("enormlz", OVERFLOW);
4129 /* Convert e type number to decimal format ASCII string.
4130 * The constants are for 64 bit precision.
4136 #if LONG_DOUBLE_TYPE_SIZE == 128
4137 static unsigned EMUSHORT etens[NTEN + 1][NE] =
4139 {0x6576, 0x4a92, 0x804a, 0x153f,
4140 0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
4141 {0x6a32, 0xce52, 0x329a, 0x28ce,
4142 0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
4143 {0x526c, 0x50ce, 0xf18b, 0x3d28,
4144 0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
4145 {0x9c66, 0x58f8, 0xbc50, 0x5c54,
4146 0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
4147 {0x851e, 0xeab7, 0x98fe, 0x901b,
4148 0xddbb, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
4149 {0x0235, 0x0137, 0x36b1, 0x336c,
4150 0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
4151 {0x50f8, 0x25fb, 0xc76b, 0x6b71,
4152 0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
4153 {0x0000, 0x0000, 0x0000, 0x0000,
4154 0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
4155 {0x0000, 0x0000, 0x0000, 0x0000,
4156 0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
4157 {0x0000, 0x0000, 0x0000, 0x0000,
4158 0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
4159 {0x0000, 0x0000, 0x0000, 0x0000,
4160 0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
4161 {0x0000, 0x0000, 0x0000, 0x0000,
4162 0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
4163 {0x0000, 0x0000, 0x0000, 0x0000,
4164 0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
4167 static unsigned EMUSHORT emtens[NTEN + 1][NE] =
4169 {0x2030, 0xcffc, 0xa1c3, 0x8123,
4170 0x2de3, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
4171 {0x8264, 0xd2cb, 0xf2ea, 0x12d4,
4172 0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
4173 {0xf53f, 0xf698, 0x6bd3, 0x0158,
4174 0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
4175 {0xe731, 0x04d4, 0xe3f2, 0xd332,
4176 0x7132, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
4177 {0xa23e, 0x5308, 0xfefb, 0x1155,
4178 0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
4179 {0xe26d, 0xdbde, 0xd05d, 0xb3f6,
4180 0xac7c, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
4181 {0x2a20, 0x6224, 0x47b3, 0x98d7,
4182 0x3f23, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
4183 {0x0b5b, 0x4af2, 0xa581, 0x18ed,
4184 0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
4185 {0xbf71, 0xa9b3, 0x7989, 0xbe68,
4186 0x4c2e, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
4187 {0x3d4d, 0x7c3d, 0x36ba, 0x0d2b,
4188 0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
4189 {0xc155, 0xa4a8, 0x404e, 0x6113,
4190 0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
4191 {0xd70a, 0x70a3, 0x0a3d, 0xa3d7,
4192 0x3d70, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
4193 {0xcccd, 0xcccc, 0xcccc, 0xcccc,
4194 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
4197 /* LONG_DOUBLE_TYPE_SIZE is other than 128 */
4198 static unsigned EMUSHORT etens[NTEN + 1][NE] =
4200 {0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */
4201 {0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */
4202 {0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},
4203 {0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},
4204 {0xddbc, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},
4205 {0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},
4206 {0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},
4207 {0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},
4208 {0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},
4209 {0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},
4210 {0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},
4211 {0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},
4212 {0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */
4215 static unsigned EMUSHORT emtens[NTEN + 1][NE] =
4217 {0x2de4, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */
4218 {0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */
4219 {0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},
4220 {0x7133, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},
4221 {0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},
4222 {0xac7d, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},
4223 {0x3f24, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},
4224 {0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},
4225 {0x4c2f, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},
4226 {0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},
4227 {0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},
4228 {0x3d71, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},
4229 {0xcccd, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */
4234 e24toasc (x, string, ndigs)
4235 unsigned EMUSHORT x[];
4239 unsigned EMUSHORT w[NI];
4242 etoasc (w, string, ndigs);
4247 e53toasc (x, string, ndigs)
4248 unsigned EMUSHORT x[];
4252 unsigned EMUSHORT w[NI];
4255 etoasc (w, string, ndigs);
4260 e64toasc (x, string, ndigs)
4261 unsigned EMUSHORT x[];
4265 unsigned EMUSHORT w[NI];
4268 etoasc (w, string, ndigs);
4272 e113toasc (x, string, ndigs)
4273 unsigned EMUSHORT x[];
4277 unsigned EMUSHORT w[NI];
4280 etoasc (w, string, ndigs);
4284 static char wstring[80]; /* working storage for ASCII output */
4287 etoasc (x, string, ndigs)
4288 unsigned EMUSHORT x[];
4293 unsigned EMUSHORT y[NI], t[NI], u[NI], w[NI];
4294 unsigned EMUSHORT *p, *r, *ten;
4295 unsigned EMUSHORT sign;
4296 int i, j, k, expon, rndsav;
4298 unsigned EMUSHORT m;
4309 sprintf (wstring, " NaN ");
4313 rndprc = NBITS; /* set to full precision */
4314 emov (x, y); /* retain external format */
4315 if (y[NE - 1] & 0x8000)
4318 y[NE - 1] &= 0x7fff;
4325 ten = &etens[NTEN][0];
4327 /* Test for zero exponent */
4330 for (k = 0; k < NE - 1; k++)
4333 goto tnzro; /* denormalized number */
4335 goto isone; /* legal all zeros */
4339 /* Test for infinity. */
4340 if (y[NE - 1] == 0x7fff)
4343 sprintf (wstring, " -Infinity ");
4345 sprintf (wstring, " Infinity ");
4349 /* Test for exponent nonzero but significand denormalized.
4350 * This is an error condition.
4352 if ((y[NE - 1] != 0) && ((y[NE - 2] & 0x8000) == 0))
4354 mtherr ("etoasc", DOMAIN);
4355 sprintf (wstring, "NaN");
4359 /* Compare to 1.0 */
4368 { /* Number is greater than 1 */
4369 /* Convert significand to an integer and strip trailing decimal zeros. */
4371 u[NE - 1] = EXONE + NBITS - 1;
4373 p = &etens[NTEN - 4][0];
4379 for (j = 0; j < NE - 1; j++)
4392 /* Rescale from integer significand */
4393 u[NE - 1] += y[NE - 1] - (unsigned int) (EXONE + NBITS - 1);
4395 /* Find power of 10 */
4399 /* An unordered compare result shouldn't happen here. */
4400 while (ecmp (ten, u) <= 0)
4402 if (ecmp (p, u) <= 0)
4415 { /* Number is less than 1.0 */
4416 /* Pad significand with trailing decimal zeros. */
4419 while ((y[NE - 2] & 0x8000) == 0)
4428 for (i = 0; i < NDEC + 1; i++)
4430 if ((w[NI - 1] & 0x7) != 0)
4432 /* multiply by 10 */
4445 if (eone[NE - 1] <= u[1])
4457 while (ecmp (eone, w) > 0)
4459 if (ecmp (p, w) >= 0)
4474 /* Find the first (leading) digit. */
4480 digit = equot[NI - 1];
4481 while ((digit == 0) && (ecmp (y, ezero) != 0))
4489 digit = equot[NI - 1];
4497 /* Examine number of digits requested by caller. */
4515 *s++ = (char)digit + '0';
4518 /* Generate digits after the decimal point. */
4519 for (k = 0; k <= ndigs; k++)
4521 /* multiply current number by 10, without normalizing */
4528 *s++ = (char) equot[NI - 1] + '0';
4530 digit = equot[NI - 1];
4533 /* round off the ASCII string */
4536 /* Test for critical rounding case in ASCII output. */
4540 if (ecmp (t, ezero) != 0)
4541 goto roun; /* round to nearest */
4542 if ((*(s - 1) & 1) == 0)
4543 goto doexp; /* round to even */
4545 /* Round up and propagate carry-outs */
4549 /* Carry out to most significant digit? */
4556 /* Most significant digit carries to 10? */
4564 /* Round up and carry out from less significant digits */
4576 sprintf (ss, "e+%d", expon);
4578 sprintf (ss, "e%d", expon);
4580 sprintf (ss, "e%d", expon);
4583 /* copy out the working string */
4586 while (*ss == ' ') /* strip possible leading space */
4588 while ((*s++ = *ss++) != '\0')
4597 ; ASCTOQ.MAC LATEST REV: 11 JAN 84
4600 ; Convert ASCII string to quadruple precision floating point
4602 ; Numeric input is free field decimal number
4603 ; with max of 15 digits with or without
4604 ; decimal point entered as ASCII from teletype.
4605 ; Entering E after the number followed by a second
4606 ; number causes the second number to be interpreted
4607 ; as a power of 10 to be multiplied by the first number
4608 ; (i.e., "scientific" notation).
4611 ; asctoq (string, q);
4614 /* ASCII to single */
4618 unsigned EMUSHORT *y;
4624 /* ASCII to double */
4628 unsigned EMUSHORT *y;
4630 #if defined(DEC) || defined(IBM)
4638 /* ASCII to long double */
4642 unsigned EMUSHORT *y;
4647 /* ASCII to 128-bit long double */
4651 unsigned EMUSHORT *y;
4653 asctoeg (s, y, 113);
4656 /* ASCII to super double */
4660 unsigned EMUSHORT *y;
4662 asctoeg (s, y, NBITS);
4666 /* ASCII to e type, with specified rounding precision = oprec. */
4668 asctoeg (ss, y, oprec)
4670 unsigned EMUSHORT *y;
4673 unsigned EMUSHORT yy[NI], xt[NI], tt[NI];
4674 int esign, decflg, sgnflg, nexp, exp, prec, lost;
4675 int k, trail, c, rndsav;
4677 unsigned EMUSHORT nsign, *p;
4678 char *sp, *s, *lstr;
4680 /* Copy the input string. */
4681 lstr = (char *) alloca (strlen (ss) + 1);
4683 while (*s == ' ') /* skip leading spaces */
4686 while ((*sp++ = *s++) != '\0')
4691 rndprc = NBITS; /* Set to full precision */
4704 if ((k >= 0) && (k <= 9))
4706 /* Ignore leading zeros */
4707 if ((prec == 0) && (decflg == 0) && (k == 0))
4709 /* Identify and strip trailing zeros after the decimal point. */
4710 if ((trail == 0) && (decflg != 0))
4713 while ((*sp >= '0') && (*sp <= '9'))
4715 /* Check for syntax error */
4717 if ((c != 'e') && (c != 'E') && (c != '\0')
4718 && (c != '\n') && (c != '\r') && (c != ' ')
4728 /* If enough digits were given to more than fill up the yy register,
4729 * continuing until overflow into the high guard word yy[2]
4730 * guarantees that there will be a roundoff bit at the top
4731 * of the low guard word after normalization.
4736 nexp += 1; /* count digits after decimal point */
4737 eshup1 (yy); /* multiply current number by 10 */
4743 xt[NI - 2] = (unsigned EMUSHORT) k;
4748 /* Mark any lost non-zero digit. */
4750 /* Count lost digits before the decimal point. */
4765 case '.': /* decimal point */
4795 mtherr ("asctoe", DOMAIN);
4804 /* Exponent interpretation */
4810 /* check for + or - */
4818 while ((*s >= '0') && (*s <= '9'))
4822 if (exp > -(MINDECEXP))
4832 if (exp > MAXDECEXP)
4836 yy[E] = 0x7fff; /* infinity */
4839 if (exp < MINDECEXP)
4848 /* Pad trailing zeros to minimize power of 10, per IEEE spec. */
4849 while ((nexp > 0) && (yy[2] == 0))
4861 if ((k = enormlz (yy)) > NBITS)
4866 lexp = (EXONE - 1 + NBITS) - k;
4867 emdnorm (yy, lost, 0, lexp, 64);
4868 /* convert to external format */
4871 /* Multiply by 10**nexp. If precision is 64 bits,
4872 * the maximum relative error incurred in forming 10**n
4873 * for 0 <= n <= 324 is 8.2e-20, at 10**180.
4874 * For 0 <= n <= 999, the peak relative error is 1.4e-19 at 10**947.
4875 * For 0 >= n >= -999, it is -1.55e-19 at 10**-435.
4889 { /* Punt. Can't handle this without 2 divides. */
4890 emovi (etens[0], tt);
4897 p = &etens[NTEN][0];
4907 while (exp <= MAXP);
4925 /* Round and convert directly to the destination type */
4927 lexp -= EXONE - 0x3ff;
4929 else if (oprec == 24 || oprec == 56)
4930 lexp -= EXONE - (0x41 << 2);
4932 else if (oprec == 24)
4933 lexp -= EXONE - 0177;
4936 else if (oprec == 56)
4937 lexp -= EXONE - 0201;
4940 emdnorm (yy, k, 0, lexp, 64);
4950 todec (yy, y); /* see etodec.c */
4955 toibm (yy, y, DFmode);
4978 /* y = largest integer not greater than x
4979 * (truncated toward minus infinity)
4981 * unsigned EMUSHORT x[NE], y[NE]
4985 static unsigned EMUSHORT bmask[] =
5008 unsigned EMUSHORT x[], y[];
5010 register unsigned EMUSHORT *p;
5012 unsigned EMUSHORT f[NE];
5014 emov (x, f); /* leave in external format */
5015 expon = (int) f[NE - 1];
5016 e = (expon & 0x7fff) - (EXONE - 1);
5022 /* number of bits to clear out */
5034 /* clear the remaining bits */
5036 /* truncate negatives toward minus infinity */
5039 if ((unsigned EMUSHORT) expon & (unsigned EMUSHORT) 0x8000)
5041 for (i = 0; i < NE - 1; i++)
5053 /* unsigned EMUSHORT x[], s[];
5056 * efrexp (x, exp, s);
5058 * Returns s and exp such that s * 2**exp = x and .5 <= s < 1.
5059 * For example, 1.1 = 0.55 * 2**1
5060 * Handles denormalized numbers properly using long integer exp.
5064 unsigned EMUSHORT x[];
5066 unsigned EMUSHORT s[];
5068 unsigned EMUSHORT xi[NI];
5072 li = (EMULONG) ((EMUSHORT) xi[1]);
5080 *exp = (int) (li - 0x3ffe);
5085 /* unsigned EMUSHORT x[], y[];
5088 * eldexp (x, pwr2, y);
5090 * Returns y = x * 2**pwr2.
5094 unsigned EMUSHORT x[];
5096 unsigned EMUSHORT y[];
5098 unsigned EMUSHORT xi[NI];
5106 emdnorm (xi, i, i, li, 64);
5111 /* c = remainder after dividing b by a
5112 * Least significant integer quotient bits left in equot[].
5116 unsigned EMUSHORT a[], b[], c[];
5118 unsigned EMUSHORT den[NI], num[NI];
5122 || (ecmp (a, ezero) == 0)
5130 if (ecmp (a, ezero) == 0)
5132 mtherr ("eremain", SING);
5138 eiremain (den, num);
5139 /* Sign of remainder = sign of quotient */
5149 unsigned EMUSHORT den[], num[];
5152 unsigned EMUSHORT j;
5155 ld -= enormlz (den);
5157 ln -= enormlz (num);
5161 if (ecmpm (den, num) <= 0)
5175 emdnorm (num, 0, 0, ln, 0);
5180 * Library common error handling routine
5190 * mtherr (fctnam, code);
5196 * This routine may be called to report one of the following
5197 * error conditions (in the include file mconf.h).
5199 * Mnemonic Value Significance
5201 * DOMAIN 1 argument domain error
5202 * SING 2 function singularity
5203 * OVERFLOW 3 overflow range error
5204 * UNDERFLOW 4 underflow range error
5205 * TLOSS 5 total loss of precision
5206 * PLOSS 6 partial loss of precision
5207 * INVALID 7 NaN - producing operation
5208 * EDOM 33 Unix domain error code
5209 * ERANGE 34 Unix range error code
5211 * The default version of the file prints the function name,
5212 * passed to it by the pointer fctnam, followed by the
5213 * error condition. The display is directed to the standard
5214 * output device. The routine then returns to the calling
5215 * program. Users may wish to modify the program to abort by
5216 * calling exit under severe error conditions such as domain
5219 * Since all error conditions pass control to this function,
5220 * the display may be easily changed, eliminated, or directed
5221 * to an error logging device.
5230 Cephes Math Library Release 2.0: April, 1987
5231 Copyright 1984, 1987 by Stephen L. Moshier
5232 Direct inquiries to 30 Frost Street, Cambridge, MA 02140
5235 /* include "mconf.h" */
5237 /* Notice: the order of appearance of the following
5238 * messages is bound to the error codes defined
5242 static char *ermsg[NMSGS] =
5244 "unknown", /* error code 0 */
5245 "domain", /* error code 1 */
5246 "singularity", /* et seq. */
5249 "total loss of precision",
5250 "partial loss of precision",
5264 /* Display string passed by calling program,
5265 * which is supposed to be the name of the
5266 * function in which the error occurred.
5269 /* Display error message defined
5270 * by the code argument.
5272 if ((code <= 0) || (code >= NMSGS))
5274 sprintf (errstr, " %s %s error", name, ermsg[code]);
5277 /* Set global error message word */
5280 /* Return to calling
5286 /* Here is etodec.c .
5291 ; convert DEC double precision to e type
5298 unsigned EMUSHORT *d;
5299 unsigned EMUSHORT *e;
5301 unsigned EMUSHORT y[NI];
5302 register unsigned EMUSHORT r, *p;
5304 ecleaz (y); /* start with a zero */
5305 p = y; /* point to our number */
5306 r = *d; /* get DEC exponent word */
5307 if (*d & (unsigned int) 0x8000)
5308 *p = 0xffff; /* fill in our sign */
5309 ++p; /* bump pointer to our exponent word */
5310 r &= 0x7fff; /* strip the sign bit */
5311 if (r == 0) /* answer = 0 if high order DEC word = 0 */
5315 r >>= 7; /* shift exponent word down 7 bits */
5316 r += EXONE - 0201; /* subtract DEC exponent offset */
5317 /* add our e type exponent offset */
5318 *p++ = r; /* to form our exponent */
5320 r = *d++; /* now do the high order mantissa */
5321 r &= 0177; /* strip off the DEC exponent and sign bits */
5322 r |= 0200; /* the DEC understood high order mantissa bit */
5323 *p++ = r; /* put result in our high guard word */
5325 *p++ = *d++; /* fill in the rest of our mantissa */
5329 eshdn8 (y); /* shift our mantissa down 8 bits */
5337 ; convert e type to DEC double precision
5345 unsigned EMUSHORT *x, *d;
5347 unsigned EMUSHORT xi[NI];
5352 exp = (EMULONG) xi[E] - (EXONE - 0201); /* adjust exponent for offsets */
5353 /* round off to nearest or even */
5356 emdnorm (xi, 0, 0, exp, 64);
5363 unsigned EMUSHORT *x, *y;
5365 unsigned EMUSHORT i;
5366 unsigned EMUSHORT *p;
5410 ; convert IBM single/double precision to e type
5413 ; enum machine_mode mode; SFmode/DFmode
5414 ; ibmtoe (&d, e, mode);
5418 unsigned EMUSHORT *d;
5419 unsigned EMUSHORT *e;
5420 enum machine_mode mode;
5422 unsigned EMUSHORT y[NI];
5423 register unsigned EMUSHORT r, *p;
5426 ecleaz (y); /* start with a zero */
5427 p = y; /* point to our number */
5428 r = *d; /* get IBM exponent word */
5429 if (*d & (unsigned int) 0x8000)
5430 *p = 0xffff; /* fill in our sign */
5431 ++p; /* bump pointer to our exponent word */
5432 r &= 0x7f00; /* strip the sign bit */
5433 r >>= 6; /* shift exponent word down 6 bits */
5434 /* in fact shift by 8 right and 2 left */
5435 r += EXONE - (0x41 << 2); /* subtract IBM exponent offset */
5436 /* add our e type exponent offset */
5437 *p++ = r; /* to form our exponent */
5439 *p++ = *d++ & 0xff; /* now do the high order mantissa */
5440 /* strip off the IBM exponent and sign bits */
5441 if (mode != SFmode) /* there are only 2 words in SFmode */
5443 *p++ = *d++; /* fill in the rest of our mantissa */
5448 if (y[M] == 0 && y[M+1] == 0 && y[M+2] == 0 && y[M+3] == 0)
5451 y[E] -= 5 + enormlz (y); /* now normalise the mantissa */
5452 /* handle change in RADIX */
5459 ; convert e type to IBM single/double precision
5462 ; enum machine_mode mode; SFmode/DFmode
5463 ; etoibm (e, &d, mode);
5468 unsigned EMUSHORT *x, *d;
5469 enum machine_mode mode;
5471 unsigned EMUSHORT xi[NI];
5476 exp = (EMULONG) xi[E] - (EXONE - (0x41 << 2)); /* adjust exponent for offsets */
5477 /* round off to nearest or even */
5480 emdnorm (xi, 0, 0, exp, 64);
5482 toibm (xi, d, mode);
5487 unsigned EMUSHORT *x, *y;
5488 enum machine_mode mode;
5490 unsigned EMUSHORT i;
5491 unsigned EMUSHORT *p;
5539 /* Output a binary NaN bit pattern in the target machine's format. */
5541 /* If special NaN bit patterns are required, define them in tm.h
5542 as arrays of unsigned 16-bit shorts. Otherwise, use the default
5548 unsigned EMUSHORT TFnan[8] =
5549 {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
5552 unsigned EMUSHORT TFnan[8] = {0, 0, 0, 0, 0, 0, 0x8000, 0xffff};
5560 unsigned EMUSHORT XFnan[6] = {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};
5563 unsigned EMUSHORT XFnan[6] = {0, 0, 0, 0xc000, 0xffff, 0};
5571 unsigned EMUSHORT DFnan[4] = {0x7fff, 0xffff, 0xffff, 0xffff};
5574 unsigned EMUSHORT DFnan[4] = {0, 0, 0, 0xfff8};
5582 unsigned EMUSHORT SFnan[2] = {0x7fff, 0xffff};
5585 unsigned EMUSHORT SFnan[2] = {0, 0xffc0};
5591 make_nan (nan, sign, mode)
5592 unsigned EMUSHORT *nan;
5594 enum machine_mode mode;
5597 unsigned EMUSHORT *p;
5601 /* Possibly the `reserved operand' patterns on a VAX can be
5602 used like NaN's, but probably not in the same way as IEEE. */
5603 #if !defined(DEC) && !defined(IBM)
5625 *nan++ = (sign << 15) | *p++;
5630 *nan = (sign << 15) | *p;
5634 /* Convert an SFmode target `float' value to a REAL_VALUE_TYPE.
5635 This is the inverse of the function `etarsingle' invoked by
5636 REAL_VALUE_TO_TARGET_SINGLE. */
5639 ereal_from_float (f)
5643 unsigned EMUSHORT s[2];
5644 unsigned EMUSHORT e[NE];
5646 /* Convert 32 bit integer to array of 16 bit pieces in target machine order.
5647 This is the inverse operation to what the function `endian' does. */
5648 #if FLOAT_WORDS_BIG_ENDIAN
5649 s[0] = (unsigned EMUSHORT) (f >> 16);
5650 s[1] = (unsigned EMUSHORT) f;
5652 s[0] = (unsigned EMUSHORT) f;
5653 s[1] = (unsigned EMUSHORT) (f >> 16);
5655 /* Convert and promote the target float to E-type. */
5657 /* Output E-type to REAL_VALUE_TYPE. */
5663 /* Convert a DFmode target `double' value to a REAL_VALUE_TYPE.
5664 This is the inverse of the function `etardouble' invoked by
5665 REAL_VALUE_TO_TARGET_DOUBLE.
5667 The DFmode is stored as an array of long ints
5668 with 32 bits of the value per each long. The first element
5669 of the input array holds the bits that would come first in the
5670 target computer's memory. */
5673 ereal_from_double (d)
5677 unsigned EMUSHORT s[4];
5678 unsigned EMUSHORT e[NE];
5680 /* Convert array of 32 bit pieces to equivalent array of 16 bit pieces.
5681 This is the inverse of `endian'. */
5682 #if FLOAT_WORDS_BIG_ENDIAN
5683 s[0] = (unsigned EMUSHORT) (d[0] >> 16);
5684 s[1] = (unsigned EMUSHORT) d[0];
5685 s[2] = (unsigned EMUSHORT) (d[1] >> 16);
5686 s[3] = (unsigned EMUSHORT) d[1];
5688 s[0] = (unsigned EMUSHORT) d[0];
5689 s[1] = (unsigned EMUSHORT) (d[0] >> 16);
5690 s[2] = (unsigned EMUSHORT) d[1];
5691 s[3] = (unsigned EMUSHORT) (d[1] >> 16);
5693 /* Convert target double to E-type. */
5695 /* Output E-type to REAL_VALUE_TYPE. */
5701 /* Convert target computer unsigned 64-bit integer to e-type.
5702 The endian-ness of DImode follows the convention for integers,
5703 so we use WORDS_BIG_ENDIAN here, not FLOAT_WORDS_BIG_ENDIAN. */
5707 unsigned EMUSHORT *di; /* Address of the 64-bit int. */
5708 unsigned EMUSHORT *e;
5710 unsigned EMUSHORT yi[NI];
5714 #if WORDS_BIG_ENDIAN
5715 for (k = M; k < M + 4; k++)
5718 for (k = M + 3; k >= M; k--)
5721 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
5722 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
5723 ecleaz (yi); /* it was zero */
5725 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
5729 /* Convert target computer signed 64-bit integer to e-type. */
5733 unsigned EMUSHORT *di; /* Address of the 64-bit int. */
5734 unsigned EMUSHORT *e;
5736 unsigned EMULONG acc;
5737 unsigned EMUSHORT yi[NI];
5738 unsigned EMUSHORT carry;
5742 #if WORDS_BIG_ENDIAN
5743 for (k = M; k < M + 4; k++)
5746 for (k = M + 3; k >= M; k--)
5749 /* Take absolute value */
5755 for (k = M + 3; k >= M; k--)
5757 acc = (unsigned EMULONG) (~yi[k] & 0xffff) + carry;
5764 yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */
5765 if ((k = enormlz (yi)) > NBITS)/* normalize the significand */
5766 ecleaz (yi); /* it was zero */
5768 yi[E] -= (unsigned EMUSHORT) k;/* subtract shift count from exponent */
5775 /* Convert e-type to unsigned 64-bit int. */
5779 unsigned EMUSHORT *x;
5780 unsigned EMUSHORT *i;
5782 unsigned EMUSHORT xi[NI];
5791 k = (int) xi[E] - (EXONE - 1);
5794 for (j = 0; j < 4; j++)
5800 for (j = 0; j < 4; j++)
5803 warning ("overflow on truncation to integer");
5808 /* Shift more than 16 bits: first shift up k-16 mod 16,
5809 then shift up by 16's. */
5810 j = k - ((k >> 4) << 4);
5814 #if WORDS_BIG_ENDIAN
5824 #if WORDS_BIG_ENDIAN
5830 while ((k -= 16) > 0);
5834 /* shift not more than 16 bits */
5839 #if WORDS_BIG_ENDIAN
5855 /* Convert e-type to signed 64-bit int. */
5859 unsigned EMUSHORT *x;
5860 unsigned EMUSHORT *i;
5862 unsigned EMULONG acc;
5863 unsigned EMUSHORT xi[NI];
5864 unsigned EMUSHORT carry;
5865 unsigned EMUSHORT *isave;
5869 k = (int) xi[E] - (EXONE - 1);
5872 for (j = 0; j < 4; j++)
5878 for (j = 0; j < 4; j++)
5881 warning ("overflow on truncation to integer");
5887 /* Shift more than 16 bits: first shift up k-16 mod 16,
5888 then shift up by 16's. */
5889 j = k - ((k >> 4) << 4);
5893 #if WORDS_BIG_ENDIAN
5903 #if WORDS_BIG_ENDIAN
5909 while ((k -= 16) > 0);
5913 /* shift not more than 16 bits */
5916 #if WORDS_BIG_ENDIAN
5929 /* Negate if negative */
5933 #if WORDS_BIG_ENDIAN
5936 for (k = 0; k < 4; k++)
5938 acc = (unsigned EMULONG) (~(*isave) & 0xffff) + carry;
5939 #if WORDS_BIG_ENDIAN
5952 /* Longhand square root routine. */
5955 static int esqinited = 0;
5956 static unsigned short sqrndbit[NI];
5960 unsigned EMUSHORT *x, *y;
5962 unsigned EMUSHORT temp[NI], num[NI], sq[NI], xx[NI];
5964 int i, j, k, n, nlups;
5969 sqrndbit[NI - 2] = 1;
5972 /* Check for arg <= 0 */
5973 i = ecmp (x, ezero);
5978 mtherr ("esqrt", DOMAIN);
5994 /* Bring in the arg and renormalize if it is denormal. */
5996 m = (EMULONG) xx[1]; /* local long word exponent */
6000 /* Divide exponent by 2 */
6002 exp = (unsigned short) ((m / 2) + 0x3ffe);
6004 /* Adjust if exponent odd */
6014 n = 8; /* get 8 bits of result per inner loop */
6020 /* bring in next word of arg */
6022 num[NI - 1] = xx[j + 3];
6023 /* Do additional bit on last outer loop, for roundoff. */
6026 for (i = 0; i < n; i++)
6028 /* Next 2 bits of arg */
6031 /* Shift up answer */
6033 /* Make trial divisor */
6034 for (k = 0; k < NI; k++)
6037 eaddm (sqrndbit, temp);
6038 /* Subtract and insert answer bit if it goes in */
6039 if (ecmpm (temp, num) <= 0)
6049 /* Adjust for extra, roundoff loop done. */
6050 exp += (NBITS - 1) - rndprc;
6052 /* Sticky bit = 1 if the remainder is nonzero. */
6054 for (i = 3; i < NI; i++)
6057 /* Renormalize and round off. */
6058 emdnorm (sq, k, 0, exp, 64);
6062 #endif /* EMU_NON_COMPILE not defined */