--- /dev/null
+# Unix makefile for ieetst, eparanoi.\r
+# Set LARGEMEM 1 in qcalc.h for 32-bit memory addresses.\r
+# Define computer type and/or endianness in mconf.h.\r
+#\r
+# Configure eparanoi.c for desired arithmetic test;\r
+# also define appropriate version of setprec.o, or use a stub that\r
+# does no FPU setup. To test native arithmetic, eparanoi uses\r
+# the system libraries only; compile simply by `cc eparanoi.c -lm'.\r
+#\r
+\r
+TESTDIR=../\r
+include $(TESTDIR)/Rules.mak\r
+\r
+\r
+#CC = gcc\r
+#CFLAGS= -O\r
+INCS= mconf.h ehead.h\r
+OBJS = ieee.o econst.o eexp.o elog.o epow.o etanh.o etodec.o mtherr.o #setprec.o\r
+TARGETS=ieetst eparanoi\r
+\r
+all: $(TARGETS)\r
+\r
+ieetst: ieetst.o $(OBJS) drand.o $(INCS)\r
+ $(CC) -o ieetst ieetst.o $(OBJS) drand.o -lc -lm\r
+\r
+eparanoi: eparanoi.o $(OBJS) $(INCS)\r
+ $(CC) -o eparanoi eparanoi.o $(OBJS) -lc -lm\r
+\r
+#setprec.o: setprec.387\r
+# as -o setprec.o setprec.387\r
+\r
+#setprec.o: setprec.688\r
+# as -o setprec.o setprec.688\r
+\r
+ieee.o: ieee.c $(INCS)\r
+ $(CC) $(CFLAGS) -c ieee.c\r
+\r
+econst.o: econst.c $(INCS)\r
+ $(CC) $(CFLAGS) -c econst.c\r
+\r
+elog.o: elog.c $(INCS)\r
+ $(CC) $(CFLAGS) -c elog.c\r
+\r
+eexp.o: eexp.c $(INCS)\r
+ $(CC) $(CFLAGS) -c eexp.c\r
+\r
+etanh.o: etanh.c $(INCS)\r
+ $(CC) $(CFLAGS) -c etanh.c\r
+\r
+epow.o: epow.c $(INCS)\r
+ $(CC) $(CFLAGS) -c epow.c\r
+\r
+mtherr.o: mtherr.c $(INCS)\r
+ $(CC) $(CFLAGS) -c mtherr.c\r
+\r
+ieetst.o: ieetst.c $(INCS)\r
+ $(CC) $(CFLAGS) -c ieetst.c\r
+\r
+drand.o: drand.c $(INCS)\r
+ $(CC) $(CFLAGS) -c drand.c\r
+\r
+etodec.o: etodec.c $(INCS)\r
+ $(CC) $(CFLAGS) -c etodec.c\r
+\r
+eparanoi.o: eparanoi.c $(INCS)\r
+ $(CC) $(CFLAGS) -c eparanoi.c\r
+\r
+clean:\r
+ rm -f *.[oa] *~ core $(TARGETS)\r
+\r
+\r
--- /dev/null
+/* drand.c
+ *
+ * Pseudorandom number generator
+ *
+ *
+ *
+ * SYNOPSIS:
+ *
+ * double y, drand();
+ *
+ * drand( &y );
+ *
+ *
+ *
+ * DESCRIPTION:
+ *
+ * Yields a random number 1.0 <= y < 2.0.
+ *
+ * The three-generator congruential algorithm by Brian
+ * Wichmann and David Hill (BYTE magazine, March, 1987,
+ * pp 127-8) is used. The period, given by them, is
+ * 6953607871644.
+ *
+ * Versions invoked by the different arithmetic compile
+ * time options DEC, IBMPC, and MIEEE, produce
+ * approximately the same sequences, differing only in the
+ * least significant bits of the numbers. The UNK option
+ * implements the algorithm as recommended in the BYTE
+ * article. It may be used on all computers. However,
+ * the low order bits of a double precision number may
+ * not be adequately random, and may vary due to arithmetic
+ * implementation details on different computers.
+ *
+ * The other compile options generate an additional random
+ * integer that overwrites the low order bits of the double
+ * precision number. This reduces the period by a factor of
+ * two but tends to overcome the problems mentioned.
+ *
+ */
+\f
+
+
+#include "mconf.h"
+
+
+/* Three-generator random number algorithm
+ * of Brian Wichmann and David Hill
+ * BYTE magazine, March, 1987 pp 127-8
+ *
+ * The period, given by them, is (p-1)(q-1)(r-1)/4 = 6.95e12.
+ */
+
+static int sx = 1;
+static int sy = 10000;
+static int sz = 3000;
+
+static union {
+ double d;
+ unsigned short s[4];
+} unkans;
+
+/* This function implements the three
+ * congruential generators.
+ */
+
+int ranwh()
+{
+int r, s;
+
+/* sx = sx * 171 mod 30269 */
+r = sx/177;
+s = sx - 177 * r;
+sx = 171 * s - 2 * r;
+if( sx < 0 )
+ sx += 30269;
+
+
+/* sy = sy * 172 mod 30307 */
+r = sy/176;
+s = sy - 176 * r;
+sy = 172 * s - 35 * r;
+if( sy < 0 )
+ sy += 30307;
+
+/* sz = 170 * sz mod 30323 */
+r = sz/178;
+s = sz - 178 * r;
+sz = 170 * s - 63 * r;
+if( sz < 0 )
+ sz += 30323;
+/* The results are in static sx, sy, sz. */
+return 0;
+}
+\f
+/* drand.c
+ *
+ * Random double precision floating point number between 1 and 2.
+ *
+ * C callable:
+ * drand( &x );
+ */
+
+int drand( a )
+double *a;
+{
+unsigned short r;
+#ifdef DEC
+unsigned short s, t;
+#endif
+
+/* This algorithm of Wichmann and Hill computes a floating point
+ * result:
+ */
+ranwh();
+unkans.d = sx/30269.0 + sy/30307.0 + sz/30323.0;
+r = unkans.d;
+unkans.d -= r;
+unkans.d += 1.0;
+
+/* if UNK option, do nothing further.
+ * Otherwise, make a random 16 bit integer
+ * to overwrite the least significant word
+ * of unkans.
+ */
+#ifdef UNK
+/* do nothing */
+#else
+ranwh();
+r = sx * sy + sz;
+#endif
+
+#ifdef DEC
+/* To make the numbers as similar as possible
+ * in all arithmetics, the random integer has
+ * to be inserted 3 bits higher up in a DEC number.
+ * An alternative would be put it 3 bits lower down
+ * in all the other number types.
+ */
+s = unkans.s[2];
+t = s & 07; /* save these bits to put in at the bottom */
+s &= 0177770;
+s |= (r >> 13) & 07;
+unkans.s[2] = s;
+t |= r << 3;
+unkans.s[3] = t;
+#endif
+
+#ifdef IBMPC
+unkans.s[0] = r;
+#endif
+
+#ifdef MIEEE
+unkans.s[3] = r;
+#endif
+
+*a = unkans.d;
+return 0;
+}
--- /dev/null
+/* econst.c */\r
+/* e type constants used by high precision check routines */\r
+\r
+#include "ehead.h"\r
+\r
+\r
+#if NE == 10\r
+/* 0.0 */\r
+unsigned short ezero[NE] =\r
+ {0x0000, 0x0000, 0x0000, 0x0000,\r
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,};\r
+\r
+/* 5.0E-1 */\r
+unsigned short ehalf[NE] =\r
+ {0x0000, 0x0000, 0x0000, 0x0000,\r
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3ffe,};\r
+\r
+/* 1.0E0 */\r
+unsigned short eone[NE] =\r
+ {0x0000, 0x0000, 0x0000, 0x0000,\r
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3fff,};\r
+\r
+/* 2.0E0 */\r
+unsigned short etwo[NE] =\r
+ {0x0000, 0x0000, 0x0000, 0x0000,\r
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4000,};\r
+\r
+/* 3.2E1 */\r
+unsigned short e32[NE] =\r
+ {0x0000, 0x0000, 0x0000, 0x0000,\r
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4004,};\r
+\r
+/* 6.93147180559945309417232121458176568075500134360255E-1 */\r
+unsigned short elog2[NE] =\r
+ {0x40f3, 0xf6af, 0x03f2, 0xb398,\r
+ 0xc9e3, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};\r
+\r
+/* 1.41421356237309504880168872420969807856967187537695E0 */\r
+unsigned short esqrt2[NE] =\r
+ {0x1d6f, 0xbe9f, 0x754a, 0x89b3,\r
+ 0x597d, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};\r
+\r
+/* 3.14159265358979323846264338327950288419716939937511E0 */\r
+unsigned short epi[NE] =\r
+ {0x2902, 0x1cd1, 0x80dc, 0x628b,\r
+ 0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};\r
+ \r
+/* 5.7721566490153286060651209008240243104215933593992E-1 */\r
+unsigned short eeul[NE] = {\r
+0xd1be,0xc7a4,0076660,0063743,0111704,0x3ffe,};\r
+\r
+#else\r
+\r
+/* 0.0 */\r
+unsigned short ezero[NE] = {\r
+0, 0000000,0000000,0000000,0000000,0000000,};\r
+/* 5.0E-1 */\r
+unsigned short ehalf[NE] = {\r
+0, 0000000,0000000,0000000,0100000,0x3ffe,};\r
+/* 1.0E0 */\r
+unsigned short eone[NE] = {\r
+0, 0000000,0000000,0000000,0100000,0x3fff,};\r
+/* 2.0E0 */\r
+unsigned short etwo[NE] = {\r
+0, 0000000,0000000,0000000,0100000,0040000,};\r
+/* 3.2E1 */\r
+unsigned short e32[NE] = {\r
+0, 0000000,0000000,0000000,0100000,0040004,};\r
+/* 6.93147180559945309417232121458176568075500134360255E-1 */\r
+unsigned short elog2[NE] = {\r
+0xc9e4,0x79ab,0150717,0013767,0130562,0x3ffe,};\r
+/* 1.41421356237309504880168872420969807856967187537695E0 */\r
+unsigned short esqrt2[NE] = {\r
+0x597e,0x6484,0174736,0171463,0132404,0x3fff,};\r
+/* 2/sqrt(PI) =\r
+ * 1.12837916709551257389615890312154517168810125865800E0 */\r
+unsigned short eoneopi[NE] = {\r
+0x71d5,0x688d,0012333,0135202,0110156,0x3fff,};\r
+/* 3.14159265358979323846264338327950288419716939937511E0 */\r
+unsigned short epi[NE] = {\r
+0xc4c6,0xc234,0020550,0155242,0144417,0040000,};\r
+/* 5.7721566490153286060651209008240243104215933593992E-1 */\r
+unsigned short eeul[NE] = {\r
+0xd1be,0xc7a4,0076660,0063743,0111704,0x3ffe,};\r
+#endif\r
+extern unsigned short ezero[];\r
+extern unsigned short ehalf[];\r
+extern unsigned short eone[];\r
+extern unsigned short etwo[];\r
+extern unsigned short e32[];\r
+extern unsigned short elog2[];\r
+extern unsigned short esqrt2[];\r
+extern unsigned short eoneopi[];\r
+extern unsigned short epi[];\r
+extern unsigned short eeul[];\r
+\r
--- /dev/null
+/* xexp.c */\r
+/* exponential function check routine */\r
+/* by Stephen L. Moshier. */\r
+\r
+\r
+#include "ehead.h"\r
+\r
+/*\r
+extern int powinited;\r
+extern short maxposint[], maxnegint[];\r
+*/\r
+\r
+void eexp( x, y )\r
+unsigned short *x, *y;\r
+{\r
+unsigned short num[NE], den[NE], x2[NE];\r
+long i;\r
+unsigned short sign, expchk;\r
+\r
+/* range reduction theory: x = i + f, 0<=f<1;\r
+ * e**x = e**i * e**f \r
+ * e**i = 2**(i/log 2).\r
+ * Let i/log2 = i1 + f1, 0<=f1<1.\r
+ * Then e**i = 2**i1 * 2**f1, so\r
+ * e**x = 2**i1 * e**(log 2 * f1) * e**f.\r
+ */\r
+/*\r
+if( powinited == 0 )\r
+ initpow();\r
+*/\r
+if( ecmp(x, ezero) == 0 )\r
+ {\r
+ emov( eone, y );\r
+ return;\r
+ }\r
+emov(x, x2);\r
+expchk = x2[NE-1];\r
+sign = expchk & 0x8000;\r
+x2[NE-1] &= 0x7fff;\r
+\r
+/* Test for excessively large argument */\r
+expchk &= 0x7fff;\r
+if( expchk > (EXONE + 15) )\r
+ {\r
+ eclear( y );\r
+ if( sign == 0 )\r
+ einfin( y );\r
+ return;\r
+ }\r
+\r
+eifrac( x2, &i, num ); /* x = i + f */\r
+\r
+if( i != 0 )\r
+ {\r
+ ltoe( &i, den ); /* floating point i */\r
+ ediv( elog2, den, den ); /* i/log 2 */\r
+ eifrac( den, &i, den ); /* i/log 2 = i1 + f1 */\r
+ emul( elog2, den, den ); /* log 2 * f1 */\r
+ eadd( den, num, x2 ); /* log 2 * f1 + f */\r
+ }\r
+\r
+/*x2[NE-1] -= 1;*/\r
+eldexp( x2, -1L, x2 ); /* divide by 2 */\r
+etanh( x2, x2 ); /* tanh( x/2 ) */\r
+eadd( x2, eone, num ); /* 1 + tanh */\r
+eneg( x2 );\r
+eadd( x2, eone, den ); /* 1 - tanh */\r
+ediv( den, num, y ); /* (1 + tanh)/(1 - tanh) */\r
+\r
+/*y[NE-1] += i;*/\r
+if( sign )\r
+ {\r
+ ediv( y, eone, y );\r
+ i = -i;\r
+ }\r
+eldexp( y, i, y ); /* multiply by 2**i */\r
+}\r
--- /dev/null
+\r
+/* Include file for extended precision arithmetic programs.\r
+ */\r
+\r
+/* Number of 16 bit words in external x type format */\r
+#define NE 6\r
+\r
+/* Number of 16 bit words in internal format */\r
+#define NI (NE+3)\r
+\r
+/* Array offset to exponent */\r
+#define E 1\r
+\r
+/* Array offset to high guard word */\r
+#define M 2\r
+\r
+/* Number of bits of precision */\r
+#define NBITS ((NI-4)*16)\r
+\r
+/* Maximum number of decimal digits in ASCII conversion\r
+ * = NBITS*log10(2)\r
+ */\r
+#define NDEC (NBITS*8/27)\r
+\r
+/* The exponent of 1.0 */\r
+#define EXONE (0x3fff)\r
+\r
+void eadd(), esub(), emul(), ediv();\r
+int ecmp(), enormlz(), eshift();\r
+void eshup1(), eshup8(), eshup6(), eshdn1(), eshdn8(), eshdn6();\r
+void eabs(), eneg(), emov(), eclear(), einfin(), efloor();\r
+void eldexp(), efrexp(), eifrac(), ltoe();\r
+void esqrt(), elog(), eexp(), etanh(), epow();\r
+void asctoe(), asctoe24(), asctoe53(), asctoe64();\r
+void etoasc(), e24toasc(), e53toasc(), e64toasc();\r
+void etoe64(), etoe53(), etoe24(), e64toe(), e53toe(), e24toe();\r
+void mtherr();\r
+extern unsigned short ezero[], ehalf[], eone[], etwo[];\r
+extern unsigned short elog2[], esqrt2[];\r
+\r
+\r
+/* by Stephen L. Moshier. */\r
--- /dev/null
+/* xlog.c */\r
+/* natural logarithm */\r
+/* by Stephen L. Moshier. */\r
+\r
+#include "mconf.h"\r
+#include "ehead.h"\r
+\r
+\r
+\r
+void elog( x, y )\r
+unsigned short *x, *y;\r
+{\r
+unsigned short xx[NE], z[NE], a[NE], b[NE], t[NE], qj[NE];\r
+long ex;\r
+int fex;\r
+\r
+\r
+if( x[NE-1] & (unsigned short )0x8000 )\r
+ {\r
+ eclear(y);\r
+ mtherr( "elog", DOMAIN );\r
+ return;\r
+ }\r
+if( ecmp( x, ezero ) == 0 )\r
+ {\r
+ einfin( y );\r
+ eneg(y);\r
+ mtherr( "elog", SING );\r
+ return;\r
+ }\r
+if( ecmp( x, eone ) == 0 )\r
+ {\r
+ eclear( y );\r
+ return;\r
+ }\r
+\r
+/* range reduction: log x = log( 2**ex * m ) = ex * log2 + log m */\r
+efrexp( x, &fex, xx );\r
+/*\r
+emov(x, xx );\r
+ex = xx[NX-1] & 0x7fff;\r
+ex -= 0x3ffe;\r
+xx[NX-1] = 0x3ffe;\r
+*/\r
+\r
+/* Adjust range to 1/sqrt(2), sqrt(2) */\r
+esqrt2[NE-1] -= 1;\r
+if( ecmp( xx, esqrt2 ) < 0 )\r
+ {\r
+ fex -= 1;\r
+ emul( xx, etwo, xx );\r
+ }\r
+esqrt2[NE-1] += 1;\r
+\r
+esub( eone, xx, a );\r
+if( a[NE-1] == 0 )\r
+ {\r
+ eclear( y );\r
+ goto logdon;\r
+ }\r
+eadd( eone, xx, b );\r
+ediv( b, a, y ); /* store (x-1)/(x+1) in y */\r
+\r
+emul( y, y, z );\r
+\r
+emov( eone, a );\r
+emov( eone, b );\r
+emov( eone, qj );\r
+do\r
+ {\r
+ eadd( etwo, qj, qj ); /* 2 * i + 1 */\r
+ emul( z, a, a );\r
+ ediv( qj, a, t );\r
+ eadd( t, b, b );\r
+ }\r
+while( ((b[NE-1] & 0x7fff) - (t[NE-1] & 0x7fff)) < NBITS );\r
+\r
+\r
+emul( b, y, y );\r
+emul( y, etwo, y );\r
+\r
+logdon:\r
+\r
+/* now add log of 2**ex */\r
+if( fex != 0 )\r
+ {\r
+ ex = fex;\r
+ ltoe( &ex, b );\r
+ emul( elog2, b, b );\r
+ eadd( b, y, y );\r
+ }\r
+}\r
--- /dev/null
+/* paranoia.c arithmetic tester\r
+ *\r
+ * This is an implementation of the PARANOIA program. It substitutes\r
+ * subroutine calls for ALL floating point arithmetic operations.\r
+ * This permits you to substitute your own experimental versions of\r
+ * arithmetic routines. It also defeats compiler optimizations,\r
+ * so for native arithmetic you can be pretty sure you are testing\r
+ * the arithmetic and not the compiler.\r
+ *\r
+ * This version of PARANOIA omits the display of division by zero.\r
+ * It also omits the test for extra precise subexpressions, since\r
+ * they cannot occur in this context. Otherwise it includes all the\r
+ * tests of the 27 Jan 86 distribution, plus a few additional tests.\r
+ * Commentary has been reduced to a minimum in order to make the program\r
+ * smaller.\r
+ *\r
+ * The original PARANOIA program, written by W. Kahan, C version\r
+ * by Thos Sumner and David Gay, can be downloaded free from the\r
+ * Internet NETLIB. An MSDOS disk can be obtained for $15 from:\r
+ * Richard Karpinski\r
+ * 6521 Raymond Street\r
+ * Oakland, CA 94609\r
+ *\r
+ * Steve Moshier, 28 Oct 88\r
+ * last rev: 23 May 92\r
+ */\r
+\r
+#define DEBUG 0\r
+\r
+/* To use the native arithmetic of the computer, define NATIVE\r
+ * to be 1. To use your own supplied arithmetic routines, NATIVE is 0.\r
+ */\r
+#define NATIVE 0\r
+\r
+/* gcc real.c interface */\r
+#define L128DOUBLE 0\r
+\r
+#include <stdio.h>\r
+\r
+\r
+\r
+\r
+/* Data structure of floating point number. If NATIVE was\r
+ * selected above, you can define LDOUBLE 1 to test 80-bit long double\r
+ * precision or define it 0 to test 64-bit double precision.\r
+*/\r
+#define LDOUBLE 0\r
+#if NATIVE\r
+\r
+#define NE 1\r
+#if LDOUBLE\r
+#define FSIZE long double\r
+#define FLOAT(x) FSIZE x[NE]\r
+static FSIZE eone[NE] = {1.0L}; /* The constant 1.0 */\r
+#define ZSQRT sqrtl\r
+#define ZLOG logl\r
+#define ZFLOOR floorl\r
+#define ZPOW powl\r
+long double sqrtl(), logl(), floorl(), powl();\r
+#define FSETUP einit\r
+#else /* not LDOUBLE */\r
+#define FSIZE double\r
+#define FLOAT(x) FSIZE x[NE]\r
+static FSIZE eone[NE] = {1.0}; /* The constant 1.0 */\r
+#define ZSQRT sqrt\r
+#define ZLOG log\r
+#define ZFLOOR floor\r
+#define ZPOW pow\r
+double sqrt(), log(), floor(), pow();\r
+/* Coprocessor initialization,\r
+ * defeat underflow trap or what have you.\r
+ * This is required mainly on i386 and 68K processors.\r
+ */\r
+#define FSETUP dprec\r
+#endif /* double, not LDOUBLE */\r
+\r
+#else /* not NATIVE */\r
+\r
+/* Setup for extended double type.\r
+ * Put NE = 10 for real.c operating with TFmode support (16-byte reals)\r
+ * Put NE = 6 for real.c operating with XFmode support (10- or 12-byte reals)\r
+ * The value of NE must agree with that in ehead.h, if ieee.c is used.\r
+ */\r
+#define NE 6\r
+#define FSIZE unsigned short\r
+#define FLOAT(x) unsigned short x[NE]\r
+extern unsigned short eone[];\r
+#define FSETUP einit\r
+\r
+/* default for FSETUP */\r
+/*\r
+einit()\r
+{}\r
+*/\r
+\r
+error(s)\r
+char *s;\r
+{\r
+printf( "error: %s\n", s );\r
+}\r
+\r
+#endif /* not NATIVE */\r
+\r
+\r
+\r
+#if L128DOUBLE\r
+/* real.c interface */\r
+\r
+#undef FSETUP\r
+#define FSETUP efsetup\r
+\r
+FLOAT(enone);\r
+\r
+#define ONE enone\r
+\r
+/* Use emov to convert from widest type to widest type, ... */\r
+/*\r
+#define ENTOE emov\r
+#define ETOEN emov\r
+*/\r
+\r
+/* ... else choose e24toe, e53toe, etc. */\r
+#define ENTOE e64toe\r
+#define ETOEN etoe64\r
+#define NNBITS 64\r
+\r
+#define NIBITS ((NE-1)*16)\r
+extern int rndprc;\r
+\r
+efsetup()\r
+{\r
+rndprc = NNBITS;\r
+ETOEN(eone, enone);\r
+}\r
+\r
+add(a,b,c)\r
+FLOAT(a);\r
+FLOAT(b);\r
+FLOAT(c);\r
+{\r
+unsigned short aa[10], bb[10], cc[10];\r
+\r
+ENTOE(a,aa);\r
+ENTOE(b,bb);\r
+eadd(aa,bb,cc);\r
+ETOEN(cc,c);\r
+}\r
+\r
+sub(a,b,c)\r
+FLOAT(a);\r
+FLOAT(b);\r
+FLOAT(c);\r
+{\r
+unsigned short aa[10], bb[10], cc[10];\r
+\r
+ENTOE(a,aa);\r
+ENTOE(b,bb);\r
+esub(aa,bb,cc);\r
+ETOEN(cc,c);\r
+}\r
+\r
+mul(a,b,c)\r
+FLOAT(a);\r
+FLOAT(b);\r
+FLOAT(c);\r
+{\r
+unsigned short aa[10], bb[10], cc[10];\r
+\r
+ENTOE(a,aa);\r
+ENTOE(b,bb);\r
+emul(aa,bb,cc);\r
+ETOEN(cc,c);\r
+}\r
+\r
+div(a,b,c)\r
+FLOAT(a);\r
+FLOAT(b);\r
+FLOAT(c);\r
+{\r
+unsigned short aa[10], bb[10], cc[10];\r
+\r
+ENTOE(a,aa);\r
+ENTOE(b,bb);\r
+ediv(aa,bb,cc);\r
+ETOEN(cc,c);\r
+}\r
+\r
+int cmp(a,b)\r
+FLOAT(a);\r
+FLOAT(b);\r
+{\r
+unsigned short aa[10], bb[10];\r
+int c;\r
+int ecmp();\r
+\r
+ENTOE(a,aa);\r
+ENTOE(b,bb);\r
+c = ecmp(aa,bb);\r
+return(c);\r
+}\r
+\r
+mov(a,b)\r
+FLOAT(a);\r
+FLOAT(b);\r
+{\r
+int i;\r
+\r
+for( i=0; i<NE; i++ )\r
+ b[i] = a[i];\r
+}\r
+\r
+\r
+neg(a)\r
+FLOAT(a);\r
+{\r
+unsigned short aa[10];\r
+\r
+ENTOE(a,aa);\r
+eneg(aa);\r
+ETOEN(aa,a);\r
+}\r
+\r
+clear(a)\r
+FLOAT(a);\r
+{\r
+int i;\r
+\r
+for( i=0; i<NE; i++ )\r
+ a[i] = 0;\r
+}\r
+\r
+FABS(a)\r
+FLOAT(a);\r
+{\r
+unsigned short aa[10];\r
+\r
+ENTOE(a,aa);\r
+eabs(aa);\r
+ETOEN(aa,a);\r
+}\r
+\r
+FLOOR(a,b)\r
+FLOAT(a);\r
+FLOAT(b);\r
+{\r
+unsigned short aa[10], bb[10];\r
+\r
+ENTOE(a,aa);\r
+efloor(aa,bb);\r
+ETOEN(bb,b);\r
+}\r
+\r
+LOG(a,b)\r
+FLOAT(a);\r
+FLOAT(b);\r
+{\r
+unsigned short aa[10], bb[10];\r
+int rndsav;\r
+\r
+ENTOE(a,aa);\r
+rndsav = rndprc;\r
+rndprc = NIBITS;\r
+elog(aa,bb);\r
+rndprc = rndsav;\r
+ETOEN(bb,b);\r
+}\r
+\r
+POW(a,b,c)\r
+FLOAT(a);\r
+FLOAT(b);\r
+FLOAT(c);\r
+{\r
+unsigned short aa[10], bb[10], cc[10];\r
+int rndsav;\r
+\r
+ENTOE(a,aa);\r
+ENTOE(b,bb);\r
+rndsav = rndprc;\r
+rndprc = NIBITS;\r
+epow(aa,bb,cc);\r
+rndprc = rndsav;\r
+ETOEN(cc,c);\r
+}\r
+\r
+SQRT(a,b)\r
+FLOAT(a);\r
+FLOAT(b);\r
+{\r
+unsigned short aa[10], bb[10];\r
+\r
+ENTOE(a,aa);\r
+esqrt(aa,bb);\r
+ETOEN(bb,b);\r
+}\r
+\r
+FTOL(x,ip,f)\r
+FLOAT(x);\r
+long *ip;\r
+FLOAT(f);\r
+{\r
+unsigned short xx[10], ff[10];\r
+\r
+ENTOE(x,xx);\r
+eifrac(xx,ip,ff);\r
+ETOEN(ff,f);\r
+}\r
+\r
+LTOF(ip,x)\r
+long *ip;\r
+FLOAT(x);\r
+{\r
+unsigned short xx[10];\r
+ltoe(ip,xx);\r
+ETOEN(xx,x);\r
+}\r
+\r
+TOASC(a,b,c)\r
+FLOAT(a);\r
+int b;\r
+char *c;\r
+{\r
+unsigned short xx[10];\r
+\r
+ENTOE(a,xx);\r
+etoasc(xx,b,c);\r
+}\r
+\r
+#else /* not L128DOUBLE */\r
+\r
+#define ONE eone\r
+\r
+/* Note all arguments of operation subroutines are pointers. */\r
+/* c = b + a */\r
+#define add(a,b,c) eadd(a,b,c)\r
+/* c = b - a */\r
+#define sub(a,b,c) esub(a,b,c)\r
+/* c = b * a */\r
+#define mul(a,b,c) emul(a,b,c)\r
+/* c = b / a */\r
+#define div(a,b,c) ediv(a,b,c)\r
+/* 1 if a>b, 0 if a==b, -1 if a<b */\r
+#define cmp(a,b) ecmp(a,b)\r
+/* b = a */\r
+#define mov(a,b) emov(a,b)\r
+/* a = -a */\r
+#define neg(a) eneg(a)\r
+/* a = 0 */\r
+#define clear(a) eclear(a)\r
+\r
+#define FABS(x) eabs(x)\r
+#define FLOOR(x,y) efloor(x,y)\r
+#define LOG(x,y) elog(x,y)\r
+#define POW(x,y,z) epow(x,y,z)\r
+#define SQRT(x,y) esqrt(x,y)\r
+\r
+/* x = &FLOAT input, i = &long integer part, f = &FLOAT fractional part */\r
+#define FTOL(x,i,f) eifrac(x,i,f)\r
+\r
+/* i = &long integer input, x = &FLOAT output */\r
+#define LTOF(i,x) ltoe(i,x)\r
+\r
+/* Convert FLOAT a to decimal ASCII string with b digits */\r
+#define TOASC(a,b,c) etoasc(a,b,c)\r
+#endif /* not L128DOUBLE */\r
+\r
+\r
+\r
+/* The following subroutines are implementations of the above\r
+ * named functions, using the native or default arithmetic.\r
+ */\r
+#if NATIVE\r
+eadd(a,b,c)\r
+FSIZE *a, *b, *c;\r
+{\r
+*c = *b + *a;\r
+}\r
+\r
+esub(a,b,c)\r
+FSIZE *a, *b, *c;\r
+{\r
+*c = *b - *a;\r
+}\r
+\r
+emul(a,b,c)\r
+FSIZE *a, *b, *c;\r
+{\r
+*c = (*b) * (*a);\r
+}\r
+\r
+ediv(a,b,c)\r
+FSIZE *a, *b, *c;\r
+{\r
+*c = (*b) / (*a);\r
+}\r
+\r
+\r
+/* Important note: comparison can be done by subracting\r
+ * or by a compare instruction that may or may not be\r
+ * equivalent to subtracting.\r
+ */\r
+ecmp(a,b)\r
+FSIZE *a, *b;\r
+{\r
+if( (*a) > (*b) )\r
+ return( 1 );\r
+if( (*a) < (*b) )\r
+ return( -1 );\r
+if( (*a) != (*b) )\r
+ goto cmpf;\r
+if( (*a) == (*b) )\r
+ return( 0 );\r
+cmpf:\r
+printf( "Compare fails\n" );\r
+return(0);\r
+}\r
+\r
+\r
+emov( a, b )\r
+FSIZE *a, *b;\r
+{\r
+*b = *a;\r
+}\r
+\r
+eneg( a )\r
+FSIZE *a;\r
+{\r
+*a = -(*a);\r
+}\r
+\r
+eclear(a)\r
+FSIZE *a;\r
+{\r
+*a = 0.0;\r
+}\r
+\r
+eabs(x)\r
+FSIZE *x;\r
+{\r
+if( (*x) < 0.0 )\r
+ *x = -(*x);\r
+}\r
+\r
+efloor(x,y)\r
+FSIZE *x, *y;\r
+{\r
+\r
+*y = (FSIZE )ZFLOOR( *x );\r
+}\r
+\r
+elog(x,y)\r
+FSIZE *x, *y;\r
+{\r
+\r
+*y = (FSIZE )ZLOG( *x );\r
+}\r
+\r
+epow(x,y,z)\r
+FSIZE *x, *y, *z;\r
+{\r
+\r
+*z = (FSIZE )ZPOW( *x, *y );\r
+}\r
+\r
+esqrt(x,y)\r
+FSIZE *x, *y;\r
+{\r
+\r
+*y = (FSIZE )ZSQRT( *x );\r
+}\r
+\r
+\r
+eifrac(x,i,f)\r
+FSIZE *x;\r
+long *i;\r
+FSIZE *f;\r
+{\r
+FSIZE y;\r
+\r
+y = (FSIZE )ZFLOOR( *x );\r
+if( y < 0.0 )\r
+ {\r
+ *f = y - *x;\r
+ *i = -y;\r
+ }\r
+else\r
+ {\r
+ *f = *x - y;\r
+ *i = y;\r
+ }\r
+}\r
+\r
+\r
+ltoe(i,x)\r
+long *i;\r
+FSIZE *x;\r
+{\r
+*x = *i;\r
+}\r
+\r
+\r
+etoasc(a,str,n)\r
+FSIZE *a;\r
+char *str;\r
+int n;\r
+{\r
+double x;\r
+\r
+x = (double )(*a);\r
+sprintf( str, " %.17e ", x );\r
+}\r
+\r
+/* default for FSETUP */\r
+einit()\r
+{}\r
+\r
+#endif /* NATIVE */\r
+\r
+\r
+\r
+\r
+FLOAT(Radix);\r
+FLOAT(BInvrse);\r
+FLOAT(RadixD2);\r
+FLOAT(BMinusU2);\r
+/*Small floating point constants.*/\r
+FLOAT(Zero);\r
+FLOAT(Half);\r
+FLOAT(One);\r
+FLOAT(Two);\r
+FLOAT(Three);\r
+FLOAT(Four);\r
+FLOAT(Five);\r
+FLOAT(Six);\r
+FLOAT(Eight);\r
+FLOAT(Nine);\r
+FLOAT(Ten);\r
+FLOAT(TwentySeven);\r
+FLOAT(ThirtyTwo);\r
+FLOAT(TwoForty);\r
+FLOAT(MinusOne );\r
+FLOAT(OneAndHalf);\r
+\r
+/*Integer constants*/\r
+int NoTrials = 20; /*Number of tests for commutativity. */\r
+#define False 0\r
+#define True 1\r
+\r
+/* Definitions for declared types \r
+ Guard == (Yes, No);\r
+ Rounding == (Chopped, Rounded, Other);\r
+ Message == packed array [1..40] of char;\r
+ Class == (Flaw, Defect, Serious, Failure);\r
+ */\r
+#define Yes 1\r
+#define No 0\r
+#define Chopped 2\r
+#define Rounded 1\r
+#define Other 0\r
+#define Flaw 3\r
+#define Defect 2\r
+#define Serious 1\r
+#define Failure 0\r
+\r
+typedef int Guard, Rounding, Class;\r
+typedef char Message;\r
+\r
+/* Declarations of Variables */\r
+FLOAT(AInvrse);\r
+FLOAT(A1);\r
+FLOAT(C);\r
+FLOAT(CInvrse);\r
+FLOAT(D);\r
+FLOAT(FourD);\r
+FLOAT(E0);\r
+FLOAT(E1);\r
+FLOAT(Exp2);\r
+FLOAT(E3);\r
+FLOAT(MinSqEr);\r
+FLOAT(SqEr);\r
+FLOAT(MaxSqEr);\r
+FLOAT(E9);\r
+FLOAT(Third);\r
+FLOAT(F6);\r
+FLOAT(F9);\r
+FLOAT(H);\r
+FLOAT(HInvrse);\r
+FLOAT(StickyBit);\r
+FLOAT(J);\r
+FLOAT(MyZero);\r
+FLOAT(Precision);\r
+FLOAT(Q);\r
+FLOAT(Q9);\r
+FLOAT(R);\r
+FLOAT(Random9);\r
+FLOAT(T);\r
+FLOAT(Underflow);\r
+FLOAT(S);\r
+FLOAT(OneUlp);\r
+FLOAT(UfThold);\r
+FLOAT(U1);\r
+FLOAT(U2);\r
+FLOAT(V);\r
+FLOAT(V0);\r
+FLOAT(V9);\r
+FLOAT(W);\r
+FLOAT(X);\r
+FLOAT(X1);\r
+FLOAT(X2);\r
+FLOAT(X8);\r
+FLOAT(Random1);\r
+FLOAT(Y);\r
+FLOAT(YY1);\r
+FLOAT(Y2);\r
+FLOAT(Random2);\r
+FLOAT(Z);\r
+FLOAT(PseudoZero);\r
+FLOAT(Z1);\r
+FLOAT(Z2);\r
+FLOAT(Z9);\r
+static FLOAT(t);\r
+FLOAT(t2);\r
+FLOAT(Sqarg);\r
+int ErrCnt[4];\r
+int fpecount;\r
+int Milestone;\r
+int PageNo;\r
+int I, M, N, N1, stkflg;\r
+Guard GMult, GDiv, GAddSub;\r
+Rounding RMult, RDiv, RAddSub, RSqrt;\r
+int Break, Done, NotMonot, Monot, Anomaly, IEEE;\r
+int SqRWrng, UfNGrad;\r
+int k, k2;\r
+int Indx;\r
+char ch[8];\r
+\r
+long lngint, lng2; /* intermediate for conversion between int and FLOAT */\r
+\r
+/* Computed constants. */\r
+/*U1 gap below 1.0, i.e, 1.0-U1 is next number below 1.0 */\r
+/*U2 gap above 1.0, i.e, 1.0+U2 is next number above 1.0 */\r
+\r
+\r
+show( x )\r
+short x[];\r
+{\r
+int i;\r
+char s[80];\r
+\r
+/* Number of 16-bit groups to display */\r
+#if NATIVE\r
+#if LDOUBLE\r
+#define NPRT (sizeof( long double )/2)\r
+#else\r
+#define NPRT (sizeof( double )/2)\r
+#endif\r
+#else\r
+#define NPRT NE\r
+#endif\r
+\r
+TOASC( x, s, 70 );\r
+printf( "%s\n", s );\r
+for( i=0; i<NPRT; i++ )\r
+ printf( "%04x ", x[i] & 0xffff );\r
+printf( "\n" );\r
+}\r
+\r
+/* define NOSIGNAL */\r
+#ifndef NOSIGNAL\r
+#include <signal.h>\r
+#endif\r
+#include <setjmp.h>\r
+jmp_buf ovfl_buf;\r
+/*typedef int (*Sig_type)();*/\r
+typedef void (*Sig_type)();\r
+Sig_type sigsave;\r
+\r
+/* Floating point exception receiver */\r
+void sigfpe()\r
+{\r
+fpecount++;\r
+printf( "\n* * * FLOATING-POINT ERROR * * *\n" );\r
+/* reinitialize the floating point unit */\r
+FSETUP();\r
+fflush(stdout);\r
+if( sigsave )\r
+ {\r
+#ifndef NOSIGNAL\r
+ signal( SIGFPE, sigsave );\r
+#endif\r
+ sigsave = 0;\r
+ longjmp( ovfl_buf, 1 );\r
+ }\r
+abort();\r
+}\r
+\r
+\r
+main()\r
+{\r
+\r
+/* Do coprocessor or other initializations */\r
+FSETUP();\r
+\r
+printf(\r
+ "This version of paranoia omits test for extra precise subexpressions\n" );\r
+printf( "and includes a few additional tests.\n" );\r
+\r
+clear(Zero);\r
+printf( "0 = " );\r
+show( Zero );\r
+mov( ONE, One);\r
+printf( "1 = " );\r
+show( One );\r
+add( One, One, Two );\r
+printf( "1+1 = " );\r
+show( Two );\r
+add( Two, One, Three );\r
+add( Three, One, Four );\r
+add( Four, One, Five );\r
+add( Five, One, Six );\r
+add( Four, Four, Eight );\r
+mul( Three, Three, Nine );\r
+add( Nine, One, Ten );\r
+mul( Nine, Three, TwentySeven );\r
+mul( Four, Eight, ThirtyTwo );\r
+mul( Four, Five, t );\r
+mul( t, Three, t );\r
+mul( t, Four, TwoForty );\r
+mov( One, MinusOne );\r
+neg( MinusOne );\r
+div( Two, One, Half );\r
+add( One, Half, OneAndHalf );\r
+ErrCnt[Failure] = 0;\r
+ErrCnt[Serious] = 0;\r
+ErrCnt[Defect] = 0;\r
+ErrCnt[Flaw] = 0;\r
+PageNo = 1;\r
+#ifndef NOSIGNAL\r
+signal( SIGFPE, sigfpe );\r
+#endif\r
+printf("Program is now RUNNING tests on small integers:\n");\r
+\r
+add( Zero, Zero, t );\r
+if( cmp( t, Zero ) != 0)\r
+ {\r
+ printf( "0+0 != 0\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+sub( One, One, t );\r
+if( cmp( t, Zero ) != 0 )\r
+ {\r
+ printf( "1-1 != 0\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+if( cmp( One, Zero ) <= 0 )\r
+ {\r
+ printf( "1 <= 0\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+add( One, One, t );\r
+if( cmp( t, Two ) != 0 )\r
+ {\r
+ printf( "1+1 != 2\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+mov( Zero, Z );\r
+neg( Z );\r
+FLOOR( Z, t );\r
+if( cmp(t,Zero) != 0 )\r
+ {\r
+ ErrCnt[Serious] += 1;\r
+ printf( "FLOOR(-0) should equal 0, is = " );\r
+ show( t );\r
+ }\r
+if( cmp(Z, Zero) != 0)\r
+ {\r
+ ErrCnt[Failure] += 1;\r
+ printf("Comparison alleges that -0.0 is Non-zero!\n");\r
+ }\r
+else\r
+ {\r
+ div( TwoForty, One, U1 ); /* U1 = 0.001 */\r
+ mov( One, Radix );\r
+ TstPtUf();\r
+ }\r
+add( Two, One, t );\r
+if( cmp( t, Three ) != 0 )\r
+ {\r
+ printf( "2+1 != 3\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+add( Three, One, t );\r
+if( cmp( t, Four ) != 0 )\r
+ {\r
+ printf( "3+1 != 4\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+mov( Two, t );\r
+neg( t );\r
+mul( Two, t, t );\r
+add( Four, t, t );\r
+if( cmp( t, Zero ) != 0 )\r
+ {\r
+ printf( "4+2*(-2) != 0\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+sub( Three, Four, t );\r
+sub( One, t, t );\r
+if( cmp( t, Zero ) != 0 )\r
+ {\r
+ printf( "4-3-1 != 0\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+ sub( One, Zero, t );\r
+if( cmp( t, MinusOne ) != 0 )\r
+ {\r
+ printf( "-1 != 0-1\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+add( One, MinusOne, t );\r
+if( cmp( t, Zero ) != 0 )\r
+ {\r
+ printf( "1+(-1) != 0\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+mov( One, t );\r
+FABS( t );\r
+add( MinusOne, t, t );\r
+if( cmp( t, Zero ) != 0 )\r
+ {\r
+ printf( "-1+abs(1) != 0\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+mul( MinusOne, MinusOne, t );\r
+add( MinusOne, t, t );\r
+if( cmp( t, Zero ) != 0 )\r
+ {\r
+ printf( "-1+(-1)*(-1) != 0\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+add( Half, MinusOne, t );\r
+add( Half, t, t );\r
+if( cmp( t, Zero ) != 0 )\r
+ {\r
+ printf( "1/2 + (-1) + 1/2 != 0\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+Milestone = 10;\r
+mul( Three, Three, t );\r
+if( cmp( t, Nine ) != 0 )\r
+ {\r
+ printf( "3*3 != 9\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+mul( Nine, Three, t );\r
+if( cmp( t, TwentySeven ) != 0 )\r
+ {\r
+ printf( "3*9 != 27\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+add( Four, Four, t );\r
+if( cmp( t, Eight ) != 0 )\r
+ {\r
+ printf( "4+4 != 8\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+mul( Eight, Four, t );\r
+if( cmp( t, ThirtyTwo ) != 0 )\r
+ {\r
+ printf( "8*4 != 32\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+sub( TwentySeven, ThirtyTwo, t );\r
+sub( Four, t, t );\r
+sub( One, t, t );\r
+if( cmp( t, Zero ) != 0 )\r
+ {\r
+ printf( "32-27-4-1 != 0\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+add( Four, One, t );\r
+if( cmp( t, Five ) != 0 )\r
+ {\r
+ printf( "4+1 != 5\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+mul( Four, Five, t );\r
+mul( Three, t, t );\r
+mul( Four, t, t );\r
+if( cmp( t, TwoForty ) != 0 )\r
+ {\r
+ printf( "4*5*3*4 != 240\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+div( Three, TwoForty, t );\r
+mul( Four, Four, t2 );\r
+mul( Five, t2, t2 );\r
+sub( t2, t2, t );\r
+if( cmp( t, Zero ) != 0 )\r
+ {\r
+ printf( "240/3 - 4*4*5 != 0\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+div( Four, TwoForty, t );\r
+mul( Five, Three, t2 );\r
+mul( Four, t2, t2 );\r
+sub( t2, t, t );\r
+if( cmp( t, Zero ) != 0 )\r
+ {\r
+ printf( "240/4 - 5*3*4 != 0\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+div( Five, TwoForty, t );\r
+mul( Four, Three, t2 );\r
+mul( Four, t2, t2 );\r
+sub( t2, t, t );\r
+if( cmp( t, Zero ) != 0 )\r
+ {\r
+ printf( "240/5 - 4*3*4 != 0\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+if(ErrCnt[Failure] == 0)\r
+ {\r
+printf("-1, 0, 1/2, 1, 2, 3, 4, 5, 9, 27, 32 & 240 are O.K.\n\n");\r
+ }\r
+printf("Searching for Radix and Precision.\n");\r
+mov( One, W );\r
+do\r
+ {\r
+ add( W, W, W );\r
+ add( W, One, Y );\r
+ sub( W, Y, Z );\r
+ sub( One, Z, Y );\r
+ mov( Y, t );\r
+ FABS(t);\r
+ add( MinusOne, t, t );\r
+ k = cmp( t, Zero );\r
+ }\r
+while( k < 0 );\r
+/*.. now W is just big enough that |((W+1)-W)-1| >= 1 ...*/\r
+mov( Zero, Precision );\r
+mov( One, Y );\r
+do\r
+ {\r
+ add( W, Y, Radix );\r
+ add( Y, Y, Y );\r
+ sub( W, Radix, Radix );\r
+ k = cmp( Radix, Zero );\r
+ }\r
+while( k == 0);\r
+\r
+if( cmp(Radix, Two) < 0 )\r
+ mov( One, Radix );\r
+printf("Radix = " );\r
+show( Radix );\r
+if( cmp(Radix, One) != 0)\r
+ {\r
+ mov( One, W );\r
+ do\r
+ {\r
+ add( One, Precision, Precision );\r
+ mul( W, Radix, W );\r
+ add( W, One, Y );\r
+ sub( W, Y, t );\r
+ k = cmp( t, One );\r
+ }\r
+ while( k == 0 );\r
+ }\r
+/* now W == Radix^Precision is barely too big to satisfy (W+1)-W == 1 */\r
+div( W, One, U1 );\r
+mul( Radix, U1, U2 );\r
+printf( "Closest relative separation found is U 1 = " );\r
+show( U1 );\r
+printf( "Recalculating radix and precision." );\r
+ \r
+/*save old values*/\r
+mov( Radix, E0 );\r
+mov( U1, E1 );\r
+mov( U2, E9 );\r
+mov( Precision, E3 );\r
+ \r
+div( Three, Four, X );\r
+sub( One, X, Third );\r
+sub( Third, Half, F6 );\r
+add( F6, F6, X );\r
+sub( Third, X, X );\r
+FABS( X );\r
+if( cmp(X, U2) < 0 )\r
+ mov( U2, X );\r
+ \r
+/*... now X = (unknown no.) ulps of 1+...*/\r
+do\r
+ {\r
+ mov( X, U2 );\r
+/* Y = Half * U2 + ThirtyTwo * U2 * U2; */\r
+ mul( ThirtyTwo, U2, t );\r
+ mul( t, U2, t );\r
+ mul( Half, U2, Y );\r
+ add( t, Y, Y );\r
+ add( One, Y, Y );\r
+ sub( One, Y, X );\r
+ k = cmp( U2, X );\r
+ k2 = cmp( X, Zero );\r
+ }\r
+while ( ! ((k <= 0) || (k2 <= 0)));\r
+ \r
+/*... now U2 == 1 ulp of 1 + ... */\r
+div( Three, Two, X );\r
+sub( Half, X, F6 );\r
+add( F6, F6, Third );\r
+sub( Half, Third, X );\r
+add( F6, X, X );\r
+FABS( X );\r
+if( cmp(X, U1) < 0 )\r
+ mov( U1, X );\r
+ \r
+/*... now X == (unknown no.) ulps of 1 -... */\r
+do\r
+ {\r
+ mov( X, U1 );\r
+ /* Y = Half * U1 + ThirtyTwo * U1 * U1;*/\r
+ mul( ThirtyTwo, U1, t );\r
+ mul( U1, t, t );\r
+ mul( Half, U1, Y );\r
+ add( t, Y, Y );\r
+ sub( Y, Half, Y );\r
+ add( Half, Y, X );\r
+ sub( X, Half, Y );\r
+ add( Half, Y, X );\r
+ k = cmp( U1, X );\r
+ k2 = cmp( X, Zero );\r
+ } while ( ! ((k <= 0) || (k2 <= 0)));\r
+/*... now U1 == 1 ulp of 1 - ... */\r
+if( cmp( U1, E1 ) == 0 )\r
+ printf("confirms closest relative separation U1 .\n");\r
+else\r
+ {\r
+ printf("gets better closest relative separation U1 = " );\r
+ show( U1 );\r
+ }\r
+div( U1, One, W );\r
+sub( U1, Half, F9 );\r
+add( F9, Half, F9 );\r
+div( U1, U2, t );\r
+div( TwoForty, One, t2 );\r
+add( t2, t, t );\r
+FLOOR( t, Radix );\r
+if( cmp(Radix, E0) == 0 )\r
+ printf("Radix confirmed.\n");\r
+else\r
+ {\r
+ printf("MYSTERY: recalculated Radix = " );\r
+ show( Radix );\r
+ mov( E0, Radix );\r
+ }\r
+add( Eight, Eight, t );\r
+if( cmp( Radix, t ) > 0 )\r
+ {\r
+ printf( "Radix is too big: roundoff problems\n" );\r
+ ErrCnt[Defect] += 1;\r
+ }\r
+k = 1;\r
+if( cmp( Radix, Two ) == 0 )\r
+ k = 0;\r
+if( cmp( Radix, Ten ) == 0 )\r
+ k = 0;\r
+if( cmp( Radix, One ) == 0 )\r
+ k = 0;\r
+if( k != 0 )\r
+ {\r
+ printf( "Radix is not as good as 2 or 10\n" );\r
+ ErrCnt[Flaw] += 1;\r
+ }\r
+/*=============================================*/\r
+Milestone = 20;\r
+/*=============================================*/\r
+sub( Half, F9, t );\r
+if( cmp( t, Half ) >= 0 )\r
+ {\r
+ printf( "(1-U1)-1/2 < 1/2 is FALSE, prog. fails?\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+mov( F9, X );\r
+I = 1;\r
+sub( Half, X, Y );\r
+sub( Half, Y, Z );\r
+if( (cmp( X, One ) == 0) && (cmp( Z, Zero) != 0) )\r
+ {\r
+ printf( "Comparison is fuzzy ,X=1 but X-1/2-1/2 != 0\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+add( One, U2, X );\r
+I = 0;\r
+/*=============================================*/\r
+Milestone = 25;\r
+/*=============================================*/\r
+/*... BMinusU2 = nextafter(Radix, 0) */\r
+\r
+sub( One, Radix, BMinusU2 );\r
+sub( U2, BMinusU2, t );\r
+add( One, t, BMinusU2 );\r
+/* Purify Integers */\r
+if( cmp(Radix,One) != 0 )\r
+ {\r
+/*X = - TwoForty * LOG(U1) / LOG(Radix);*/\r
+ LOG( U1, X );\r
+ LOG( Radix, t );\r
+ div( t, X, X );\r
+ mul( TwoForty, X, X );\r
+ neg( X ); \r
+\r
+ add( Half, X, Y );\r
+ FLOOR( Y, Y );\r
+ sub( Y, X, t );\r
+ FABS( t );\r
+ mul( Four, t, t );\r
+ if( cmp( t, One ) < 0 )\r
+ mov( Y, X );\r
+ div( TwoForty, X, Precision );\r
+ add( Half, Precision, Y );\r
+ FLOOR( Y, Y );\r
+ sub( Y, Precision, t );\r
+ FABS( t );\r
+ mul( TwoForty, t, t );\r
+ if( cmp( t, Half ) < 0 )\r
+ mov( Y, Precision );\r
+ }\r
+FLOOR( Precision, t );\r
+if( (cmp( Precision, t ) != 0) || (cmp( Radix, One ) == 0) )\r
+ {\r
+ printf("Precision cannot be characterized by an Integer number\n");\r
+ printf("of significant digits but, by itself, this is a minor flaw.\n");\r
+ }\r
+if( cmp(Radix, One) == 0 ) \r
+ printf("logarithmic encoding has precision characterized solely by U1.\n");\r
+else\r
+ {\r
+ printf("The number of significant digits of the Radix is " );\r
+ show( Precision );\r
+ }\r
+mul( U2, Nine, t );\r
+mul( Nine, t, t );\r
+mul( TwoForty, t, t );\r
+if( cmp( t, One ) >= 0 )\r
+ {\r
+ printf( "Precision worse than 5 decimal figures\n" );\r
+ ErrCnt[Serious] += 1;\r
+ }\r
+/*=============================================*/\r
+Milestone = 30;\r
+/*=============================================*/\r
+/* Test for extra-precise subepressions has been deleted. */\r
+Milestone = 35;\r
+/*=============================================*/\r
+if( cmp(Radix,Two) >= 0 )\r
+ {\r
+ mul( Radix, Radix, t );\r
+ div( t, W, X );\r
+ add( X, One, Y );\r
+ sub( X, Y, Z );\r
+ add( Z, U2, T );\r
+ sub( Z, T, X );\r
+ if( cmp( X, U2 ) != 0 )\r
+ {\r
+ printf( "Subtraction is not normalized X=Y,X+Z != Y+Z!\n" );\r
+ ErrCnt[Failure] += 1;\r
+ }\r
+ if( cmp(X,U2) == 0 )\r
+ printf("Subtraction appears to be normalized, as it should be.");\r
+ }\r
+\r
+printf("\nChecking for guard digit in *, /, and -.\n");\r
+mul( F9, One, Y );\r
+mul( One, F9, Z );\r
+sub( Half, F9, X );\r
+sub( Half, Y, Y );\r
+sub( X, Y, Y );\r
+sub( Half, Z, Z );\r
+sub( X, Z, Z );\r
+add( One, U2, X );\r
+mul( X, Radix, T );\r
+mul( Radix, X, R );\r
+sub( Radix, T, X );\r
+mul( Radix, U2, t );\r
+sub( t, X, X );\r
+sub( Radix, R, T );\r
+mul( Radix, U2, t );\r
+sub( t, T, T );\r
+sub( One, Radix, t );\r
+mul( t, X, X );\r
+sub( One, Radix, t );\r
+mul( t, T, T );\r
+\r
+k = cmp(X,Zero);\r
+k |= cmp(Y,Zero);\r
+k |= cmp(Z,Zero);\r
+k |= cmp(T,Zero);\r
+if( k == 0 )\r
+ GMult = Yes;\r
+else\r
+ {\r
+ GMult = No;\r
+ ErrCnt[Serious] += 1;\r
+ printf( "* lacks a Guard Digit, so 1*X != X\n" );\r
+ }\r
+mul( Radix, U2, Z );\r
+add( One, Z, X );\r
+add( X, Z, Y );\r
+mul( X, X, t );\r
+sub( t, Y, Y );\r
+FABS( Y );\r
+sub( U2, Y, Y );\r
+sub( U2, One, X );\r
+sub( U2, X, Z );\r
+mul( X, X, t );\r
+sub( t, Z, Z );\r
+FABS( Z );\r
+sub( U1, Z, Z );\r
+if( (cmp(Y,Zero) > 0) || (cmp(Z,Zero) > 0) )\r
+ {\r
+ ErrCnt[Failure] += 1;\r
+ printf( "* gets too many final digits wrong.\n" );\r
+ }\r
+sub( U2, One, Y );\r
+add( One, U2, X );\r
+div( Y, One, Z );\r
+sub( X, Z, Y );\r
+div( Three, One, X );\r
+div( Nine, Three, Z );\r
+sub( Z, X, X );\r
+div( TwentySeven, Nine, T );\r
+sub( T, Z, Z );\r
+k = cmp( X, Zero );\r
+k |= cmp( Y, Zero );\r
+k |= cmp( Z, Zero );\r
+if( k )\r
+ {\r
+ ErrCnt[Defect] += 1;\r
+printf( "Division lacks a Guard Digit, so error can exceed 1 ulp\n" );\r
+printf( "or 1/3 and 3/9 and 9/27 may disagree\n" );\r
+ }\r
+div( One, F9, Y );\r
+sub( Half, F9, X );\r
+sub( Half, Y, Y );\r
+sub( X, Y, Y );\r
+add( One, U2, X );\r
+div( One, X, T );\r
+sub( X, T, X );\r
+k = cmp( X, Zero );\r
+k |= cmp( Y, Zero );\r
+k |= cmp( Z, Zero );\r
+if( k == 0 )\r
+ GDiv = Yes;\r
+else\r
+ {\r
+ GDiv = No;\r
+ ErrCnt[Serious] += 1;\r
+ printf( "Division lacks a Guard Digit, so X/1 != X\n" );\r
+ }\r
+add( One, U2, X );\r
+div( X, One, X );\r
+sub( Half, X, Y );\r
+sub( Half, Y, Y );\r
+if( cmp(Y,Zero) >= 0 )\r
+ {\r
+ ErrCnt[Serious] += 1;\r
+ printf( "Computed value of 1/1.000..1 >= 1\n" );\r
+ }\r
+sub( U2, One, X );\r
+mul( Radix, U2, Y );\r
+add( One, Y, Y );\r
+mul( X, Radix, Z );\r
+mul( Y, Radix, T );\r
+div( Radix, Z, R );\r
+div( Radix, T, StickyBit );\r
+sub( X, R, X );\r
+sub( Y, StickyBit, Y );\r
+k = cmp( X, Zero );\r
+k |= cmp( Y, Zero );\r
+if( k )\r
+ {\r
+ ErrCnt[Failure] += 1;\r
+ printf( "* and/or / gets too many last digits wrong\n" );\r
+ }\r
+sub( U1, One, Y );\r
+sub( F9, One, X );\r
+sub( Y, One, Y );\r
+sub( U2, Radix, T );\r
+sub( BMinusU2, Radix, Z );\r
+sub( T, Radix, T );\r
+k = cmp( X, U1 );\r
+k |= cmp( Y, U1 );\r
+k |= cmp( Z, U2 );\r
+k |= cmp( T, U2 );\r
+if( k == 0 )\r
+ GAddSub = Yes;\r
+else\r
+ {\r
+ GAddSub = No;\r
+ ErrCnt[Serious] += 1;\r
+ printf( "- lacks Guard Digit, so cancellation is obscured\n" );\r
+ }\r
+sub( One, F9, t );\r
+if( (cmp(F9,One) != 0) && (cmp(t,Zero) >= 0) )\r
+ {\r
+ ErrCnt[Serious] += 1;\r
+ printf("comparison alleges (1-U1) < 1 although\n");\r
+ printf(" subtration yields (1-U1) - 1 = 0 , thereby vitiating\n");\r
+ printf(" such precautions against division by zero as\n");\r
+ printf(" ... if (X == 1.0) {.....} else {.../(X-1.0)...}\n");\r
+ }\r
+if (GMult == Yes && GDiv == Yes && GAddSub == Yes)\r
+ printf(" *, /, and - appear to have guard digits, as they should.\n");\r
+/*=============================================*/\r
+Milestone = 40;\r
+/*=============================================*/\r
+printf("Checking rounding on multiply, divide and add/subtract.\n");\r
+RMult = Other;\r
+RDiv = Other;\r
+RAddSub = Other;\r
+div( Two, Radix, RadixD2 );\r
+mov( Two, A1 );\r
+Done = False;\r
+do\r
+ {\r
+ mov( Radix, AInvrse );\r
+ do\r
+ {\r
+ mov( AInvrse, X );\r
+ div( A1, AInvrse, AInvrse );\r
+ FLOOR( AInvrse, t );\r
+ k = cmp( t, AInvrse );\r
+ }\r
+ while( ! (k != 0 ) );\r
+ k = cmp( X, One );\r
+ k2 = cmp( A1, Three );\r
+ Done = (k == 0) || (k2 > 0);\r
+ if(! Done)\r
+ add( Nine, One, A1 );\r
+ }\r
+while( ! (Done));\r
+if( cmp(X, One) == 0 )\r
+ mov( Radix, A1 );\r
+div( A1, One, AInvrse );\r
+mov( A1, X );\r
+mov( AInvrse, Y );\r
+Done = False;\r
+do\r
+ {\r
+ mul( X, Y, Z );\r
+ sub( Half, Z, Z );\r
+ if( cmp( Z, Half ) != 0 )\r
+ {\r
+ ErrCnt[Failure] += 1;\r
+ printf( "X * (1/X) differs from 1\n" );\r
+ }\r
+ k = cmp( X, Radix );\r
+ Done = (k == 0);\r
+ mov( Radix, X );\r
+ div( X, One, Y );\r
+ }\r
+while( ! (Done));\r
+\r
+add( One, U2, Y2 );\r
+sub( U2, One, YY1 );\r
+sub( U2, OneAndHalf, X );\r
+add( OneAndHalf, U2, Y );\r
+sub( U2, X, Z );\r
+mul( Z, Y2, Z );\r
+mul( Y, YY1, T );\r
+sub( X, Z, Z );\r
+sub( X, T, T );\r
+mul( X, Y2, X );\r
+add( Y, U2, Y );\r
+mul( Y, YY1, Y );\r
+sub( OneAndHalf, X, X );\r
+sub( OneAndHalf, Y, Y );\r
+k = cmp( X, Zero );\r
+k |= cmp( Y, Zero );\r
+k |= cmp( Z, Zero );\r
+if( cmp( T, Zero ) > 0 )\r
+ k = 1;\r
+if( k == 0 )\r
+ {\r
+ add( OneAndHalf, U2, X );\r
+ mul( X, Y2, X );\r
+ sub( U2, OneAndHalf, Y );\r
+ sub( U2, Y, Y );\r
+ add( OneAndHalf, U2, Z );\r
+ add( U2, Z, Z );\r
+ sub( U2, OneAndHalf, T );\r
+ mul( T, YY1, T );\r
+ add( Z, U2, t );\r
+ sub( t, X, X );\r
+ mul( Y, YY1, StickyBit );\r
+ mul( Z, Y2, S );\r
+ sub( Y, T, T );\r
+ sub( Y, U2, Y );\r
+ add( StickyBit, Y, Y );\r
+/* Z = S - (Z + U2 + U2); */\r
+ add( Z, U2, t );\r
+ add( t, U2, t );\r
+ sub( t, S, Z );\r
+ add( Y2, U2, t );\r
+ mul( t, YY1, StickyBit );\r
+ mul( Y2, YY1, YY1 );\r
+ sub( Y2, StickyBit, StickyBit );\r
+ sub( Half, YY1, YY1 );\r
+ k = cmp( X, Zero );\r
+ k |= cmp( Y, Zero );\r
+ k |= cmp( Z, Zero );\r
+ k |= cmp( T, Zero );\r
+ k |= cmp( StickyBit, Zero );\r
+ k |= cmp( YY1, Half );\r
+ if( k == 0 )\r
+ {\r
+ RMult = Rounded;\r
+ printf("Multiplication appears to round correctly.\n");\r
+ }\r
+ else\r
+ {\r
+ add( X, U2, t );\r
+ k = cmp( t, Zero );\r
+ if( cmp( Y, Zero ) >= 0 )\r
+ k |= 1;\r
+ add( Z, U2, t );\r
+ k |= cmp( t, Zero );\r
+ if( cmp( T, Zero ) >= 0 )\r
+ k |= 1;\r
+ add( StickyBit, U2, t );\r
+ k |= cmp( t, Zero );\r
+ if( cmp(YY1, Half) >= 0 )\r
+ k |= 1;\r
+ if( k == 0 )\r
+ {\r
+ printf("Multiplication appears to chop.\n");\r
+ }\r
+ else\r
+ {\r
+ printf("* is neither chopped nor correctly rounded.\n");\r
+ }\r
+ if( (RMult == Rounded) && (GMult == No) )\r
+ printf("Multiplication has inconsistent result");\r
+ }\r
+ }\r
+else\r
+ printf("* is neither chopped nor correctly rounded.\n");\r
+\r
+/*=============================================*/\r
+Milestone = 45;\r
+/*=============================================*/\r
+add( One, U2, Y2 );\r
+sub( U2, One, YY1 );\r
+add( OneAndHalf, U2, Z );\r
+add( Z, U2, Z );\r
+div( Y2, Z, X );\r
+sub( U2, OneAndHalf, T );\r
+sub( U2, T, T );\r
+sub( U2, T, Y );\r
+div( YY1, Y, Y );\r
+add( Z, U2, Z );\r
+div( Y2, Z, Z );\r
+sub( OneAndHalf, X, X );\r
+sub( T, Y, Y );\r
+div( YY1, T, T );\r
+add( OneAndHalf, U2, t );\r
+sub( t, Z, Z );\r
+sub( OneAndHalf, U2, t );\r
+add( t, T, T );\r
+k = 0;\r
+if( cmp( X, Zero ) > 0 )\r
+ k = 1;\r
+if( cmp( Y, Zero ) > 0 )\r
+ k = 1;\r
+if( cmp( Z, Zero ) > 0 )\r
+ k = 1;\r
+if( cmp( T, Zero ) > 0 )\r
+ k = 1;\r
+if( k == 0 )\r
+ {\r
+ div( Y2, OneAndHalf, X );\r
+ sub( U2, OneAndHalf, Y );\r
+ add( U2, OneAndHalf, Z );\r
+ sub( Y, X, X );\r
+ div( YY1, OneAndHalf, T );\r
+ div( YY1, Y, Y );\r
+ add( Z, U2, t );\r
+ sub( t, T, T );\r
+ sub( Z, Y, Y );\r
+ div( Y2, Z, Z );\r
+ add( Y2, U2, YY1 );\r
+ div( Y2, YY1, YY1 );\r
+ sub( OneAndHalf, Z, Z );\r
+ sub( Y2, YY1, Y2 );\r
+ sub( U1, F9, YY1 );\r
+ div( F9, YY1, YY1 );\r
+ k = cmp( X, Zero );\r
+ k |= cmp( Y, Zero );\r
+ k |= cmp( Z, Zero );\r
+ k |= cmp( T, Zero );\r
+ k |= cmp( Y2, Zero );\r
+ sub( Half, YY1, t );\r
+ sub( Half, F9, t2 );\r
+ k |= cmp( t, t2 );\r
+ if( k == 0 )\r
+ {\r
+ RDiv = Rounded;\r
+ printf("Division appears to round correctly.\n");\r
+ if(GDiv == No)\r
+ printf("Division test inconsistent\n");\r
+ }\r
+ else\r
+ {\r
+ k = 0;\r
+ if( cmp( X, Zero ) >= 0 )\r
+ k = 1;\r
+ if( cmp( Y, Zero ) >= 0 )\r
+ k = 1;\r
+ if( cmp( Z, Zero ) >= 0 )\r
+ k = 1;\r
+ if( cmp( T, Zero ) >= 0 )\r
+ k = 1;\r
+ if( cmp( Y, Zero ) >= 0 )\r
+ k = 1;\r
+ sub( Half, YY1, t );\r
+ sub( Half, F9, t2 );\r
+ if( cmp( t, t2 ) >= 0 )\r
+ k = 1;\r
+ if( k == 0 )\r
+ {\r
+ RDiv = Chopped;\r
+ printf("Division appears to chop.\n");\r
+ }\r
+ }\r
+ }\r
+if(RDiv == Other)\r
+ printf("/ is neither chopped nor correctly rounded.\n");\r
+div( Radix, One, BInvrse );\r
+mul( BInvrse, Radix, t );\r
+sub( Half, t, t );\r
+if( cmp( t, Half ) != 0 )\r
+ {\r
+ ErrCnt[Failure] += 1;\r
+ printf( "Radix * ( 1 / Radix ) differs from 1\n" );\r
+ }\r
+\r
+Milestone = 50;\r
+/*=============================================*/\r
+add( F9, U1, t );\r
+sub( Half, t, t );\r
+k = cmp( t, Half );\r
+add( BMinusU2, U2, t );\r
+sub( One, t, t );\r
+sub( One, Radix, t2 );\r
+k |= cmp( t, t2 );\r
+if( k != 0 )\r
+ {\r
+ ErrCnt[Failure] += 1;\r
+ printf( "Incomplete carry-propagation in Addition\n" );\r
+ }\r
+mul( U1, U1, X );\r
+sub( X, One, X );\r
+sub( U2, One, Y );\r
+mul( U2, Y, Y );\r
+add( One, Y, Y );\r
+sub( Half, F9, Z );\r
+sub( Half, X, X );\r
+sub( Z, X, X );\r
+sub( One, Y, Y );\r
+if( (cmp(X,Zero) == 0) && (cmp(Y,Zero) == 0) )\r
+ {\r
+ RAddSub = Chopped;\r
+ printf("Add/Subtract appears to be chopped.\n");\r
+ }\r
+if(GAddSub == Yes)\r
+ {\r
+ add( Half, U2, X );\r
+ mul( X, U2, X );\r
+ sub( U2, Half, Y );\r
+ mul( Y, U2, Y );\r
+ add( One, X, X );\r
+ add( One, Y, Y );\r
+ add( One, U2, t );\r
+ sub( X, t, X );\r
+ sub( Y, One, Y );\r
+ k = cmp(X,Zero);\r
+ if( k )\r
+ printf( "1+U2-[u2(1/2+U2)+1] != 0\n" );\r
+ k2 = cmp(Y,Zero);\r
+ if( k2 )\r
+ printf( "1-[U2(1/2-U2)+1] != 0\n" );\r
+ k |= k2;\r
+ if( k == 0 )\r
+ {\r
+ add( Half, U2, X );\r
+ mul( X, U1, X );\r
+ sub( U2, Half, Y );\r
+ mul( Y, U1, Y );\r
+ sub( X, One, X );\r
+ sub( Y, One, Y );\r
+ sub( X, F9, X );\r
+ sub( Y, One, Y );\r
+ k = cmp(X,Zero);\r
+ if( k )\r
+ printf( "F9-[1-U1(1/2+U2)] != 0\n" );\r
+ k2 = cmp(Y,Zero);\r
+ if( k2 )\r
+ printf( "1-[1-U1(1/2-U2)] != 0\n" );\r
+ k |= k2;\r
+ if( k == 0 )\r
+ {\r
+ RAddSub = Rounded;\r
+ printf("Addition/Subtraction appears to round correctly.\n");\r
+ if(GAddSub == No)\r
+ printf( "Add/Subtract test inconsistent\n");\r
+ }\r
+ else\r
+ {\r
+ printf("Addition/Subtraction neither rounds nor chops.\n");\r
+ }\r
+ }\r
+ else\r
+ printf("Addition/Subtraction neither rounds nor chops.\n");\r
+ }\r
+else\r
+ printf("Addition/Subtraction neither rounds nor chops.\n");\r
+\r
+mov( One, S );\r
+add( One, Half, X );\r
+mul( Half, X, X );\r
+add( One, X, X );\r
+add( One, U2, Y );\r
+mul( Y, Half, Y );\r
+sub( Y, X, Z );\r
+sub( X, Y, T );\r
+add( Z, T, StickyBit );\r
+if( cmp(StickyBit, Zero) != 0 )\r
+ {\r
+ mov( Zero, S );\r
+ ErrCnt[Flaw] += 1;\r
+ printf( "(X - Y) + (Y - X) is non zero!\n" );\r
+ }\r
+mov( Zero, StickyBit );\r
+FLOOR( RadixD2, t );\r
+k2 = cmp( t, RadixD2 );\r
+k = 1;\r
+if( (GMult == Yes) && (GDiv == Yes) && (GAddSub == Yes)\r
+ && (RMult == Rounded) && (RDiv == Rounded)\r
+ && (RAddSub == Rounded) && (k2 == 0) )\r
+ {\r
+ printf("Checking for sticky bit.\n");\r
+ k = 0;\r
+ add( Half, U1, X );\r
+ mul( X, U2, X );\r
+ mul( Half, U2, Y );\r
+ add( One, Y, Z );\r
+ add( One, X, T );\r
+ sub( One, Z, t );\r
+ sub( One, T, t2 );\r
+ if( cmp(t,Zero) > 0 )\r
+ {\r
+ k = 1;\r
+ printf( "[1+(1/2)U2]-1 > 0\n" );\r
+ }\r
+ if( cmp(t2,U2) < 0 )\r
+ {\r
+ k = 1;\r
+ printf( "[1+U2(1/2+U1)]-1 < U2\n" );\r
+ }\r
+ add( T, Y, Z );\r
+ sub( X, Z, Y );\r
+ sub( T, Z, t );\r
+ sub( T, Y, t2 );\r
+ if( cmp(t,U2) < 0 )\r
+ {\r
+ k = 1;\r
+ printf( "[[1+U2(1/2+U1)]+(1/2)U2]-[1+U2(1/2+U1)] < U2\n" );\r
+ }\r
+ if( cmp(t2,Zero) != 0 )\r
+ {\r
+ k = 1;\r
+ printf( "(1/2)U2-[1+U2(1/2+U1)] != 0\n" );\r
+ }\r
+ add( Half, U1, X );\r
+ mul( X, U1, X );\r
+ mul( Half, U1, Y );\r
+ sub( Y, One, Z );\r
+ sub( X, One, T );\r
+ sub( One, Z, t );\r
+ sub( F9, T, t2 );\r
+ if( cmp(t,Zero) != 0 )\r
+ {\r
+ k = 1;\r
+ printf( "(1-(1/2)U1)-1 != 0\n" );\r
+ }\r
+ if( cmp(t2,Zero) != 0 )\r
+ {\r
+ k = 1;\r
+ printf( "[1-U1(1/2+U1)]-F9 != 0\n" );\r
+ }\r
+ sub( U1, Half, Z );\r
+ mul( Z, U1, Z );\r
+ sub( Z, F9, T );\r
+ sub( Y, F9, Q );\r
+ sub( F9, T, t );\r
+ if( cmp( t, Zero ) != 0 )\r
+ {\r
+ k = 1;\r
+ printf( "[F9-U1(1/2-U1)]-F9 != 0\n" );\r
+ }\r
+ sub( U1, F9, t );\r
+ sub( Q, t, t );\r
+ if( cmp( t, Zero ) != 0 )\r
+ {\r
+ k = 1;\r
+ printf( "(F9-U1)-(F9-(1/2)U1) != 0\n" );\r
+ }\r
+ add( One, U2, Z );\r
+ mul( Z, OneAndHalf, Z );\r
+ add( OneAndHalf, U2, T );\r
+ sub( Z, T, T );\r
+ add( U2, T, T );\r
+ div( Radix, Half, X );\r
+ add( One, X, X );\r
+ mul( Radix, U2, Y );\r
+ add( One, Y, Y );\r
+ mul( X, Y, Z );\r
+ if( cmp( T, Zero ) != 0 )\r
+ {\r
+ k = 1;\r
+ printf( "(3/2+U2)-3/2(1+U2)+U2 != 0\n" );\r
+ }\r
+ mul( Radix, U2, t );\r
+ add( X, t, t );\r
+ sub( Z, t, t );\r
+ if( cmp( t, Zero ) != 0 )\r
+ {\r
+ k = 1;\r
+ printf( "(1+1/2Radix)+Radix*U2-[1+1/(2Radix)][1+Radix*U2] != 0\n" );\r
+ }\r
+ if( cmp(Radix, Two) != 0 )\r
+ {\r
+ add( Two, U2, X );\r
+ div( Two, X, Y );\r
+ sub( One, Y, t );\r
+ if( cmp( t, Zero) != 0 )\r
+ k = 1;\r
+ }\r
+ }\r
+if( k == 0 )\r
+ {\r
+ printf("Sticky bit apparently used correctly.\n");\r
+ mov( One, StickyBit );\r
+ }\r
+else\r
+ {\r
+ printf("Sticky bit used incorrectly or not at all.\n");\r
+ }\r
+\r
+if( GMult == No || GDiv == No || GAddSub == No ||\r
+ RMult == Other || RDiv == Other || RAddSub == Other)\r
+ {\r
+ ErrCnt[Flaw] += 1;\r
+ printf("lack(s) of guard digits or failure(s) to correctly round or chop\n");\r
+printf( "(noted above) count as one flaw in the final tally below\n" );\r
+ }\r
+/*=============================================*/\r
+Milestone = 60;\r
+/*=============================================*/\r
+printf("\n");\r
+printf("Does Multiplication commute? ");\r
+printf("Testing on %d random pairs.\n", NoTrials);\r
+SQRT( Three, Random9 );\r
+mov( Third, Random1 );\r
+I = 1;\r
+do\r
+ {\r
+ Random();\r
+ mov( Random1, X );\r
+ Random();\r
+ mov( Random1, Y );\r
+ mul( Y, X, Z9 );\r
+ mul( X, Y, Z );\r
+ sub( Z9, Z, Z9 );\r
+ I = I + 1;\r
+ }\r
+while ( ! ((I > NoTrials) || (cmp(Z9,Zero) != 0)));\r
+if(I == NoTrials)\r
+ {\r
+ div( Three, Half, t );\r
+ add( One, t, Random1 );\r
+ add( U2, U1, t );\r
+ add( t, One, Random2 );\r
+ mul( Random1, Random2, Z );\r
+ mul( Random2, Random1, Y );\r
+/* Z9 = (One + Half / Three) * ((U2 + U1) + One) - (One + Half /\r
+ * Three) * ((U2 + U1) + One);\r
+ */\r
+ div( Three, Half, t2 );\r
+ add( One, t2, t2 );\r
+ add( U2, U1, t );\r
+ add( t, One, t );\r
+ mul( t2, t, Z9 );\r
+ mul( t2, t, t );\r
+ sub( t, Z9, Z9 );\r
+ }\r
+if(! ((I == NoTrials) || (cmp(Z9,Zero) == 0)))\r
+ {\r
+ ErrCnt[Defect] += 1;\r
+ printf( "X * Y == Y * X trial fails.\n");\r
+ }\r
+else\r
+ {\r
+ printf(" No failures found in %d integer pairs.\n", NoTrials);\r
+ }\r
+/*=============================================*/\r
+Milestone = 70;\r
+/*=============================================*/\r
+sqtest();\r
+Milestone = 90;\r
+pow1test();\r
+\r
+Milestone = 110;\r
+\r
+printf("Seeking Underflow thresholds UfThold and E0.\n");\r
+mov( U1, D );\r
+FLOOR( Precision, t );\r
+if( cmp(Precision, t) != 0 )\r
+ {\r
+ mov( BInvrse, D );\r
+ mov( Precision, X );\r
+ do\r
+ {\r
+ mul( D, BInvrse, D );\r
+ sub( One, X, X );\r
+ }\r
+ while( cmp(X, Zero) > 0 );\r
+ }\r
+mov( One, Y );\r
+mov( D, Z );\r
+/* ... D is power of 1/Radix < 1. */\r
+sigsave = sigfpe;\r
+if( setjmp(ovfl_buf) )\r
+ goto under0;\r
+do\r
+ {\r
+ mov( Y, C );\r
+ mov( Z, Y );\r
+ mul( Y, Y, Z );\r
+ add( Z, Z, t );\r
+ }\r
+while( (cmp(Y,Z) > 0) && (cmp(t,Z) > 0) );\r
+\r
+under0:\r
+sigsave = 0;\r
+\r
+mov( C, Y );\r
+mul( Y, D, Z );\r
+sigsave = sigfpe;\r
+if( setjmp(ovfl_buf) )\r
+ goto under1;\r
+do\r
+ {\r
+ mov( Y, C );\r
+ mov( Z, Y );\r
+ mul( Y, D, Z );\r
+ add( Z, Z, t );\r
+ }\r
+while( (cmp(Y,Z) > 0) && (cmp(t,Z) > 0) );\r
+\r
+under1:\r
+sigsave = 0;\r
+\r
+if( cmp(Radix,Two) < 0 )\r
+ mov( Two, HInvrse );\r
+else\r
+ mov( Radix, HInvrse );\r
+div( HInvrse, One, H );\r
+/* ... 1/HInvrse == H == Min(1/Radix, 1/2) */\r
+div( C, One, CInvrse );\r
+mov( C, E0 );\r
+mul( E0, H, Z );\r
+/* ...1/Radix^(BIG Integer) << 1 << CInvrse == 1/C */\r
+sigsave = sigfpe;\r
+if( setjmp(ovfl_buf) )\r
+ goto under2;\r
+do\r
+ {\r
+ mov( E0, Y );\r
+ mov( Z, E0 );\r
+ mul( E0, H, Z );\r
+ add( Z, Z, t );\r
+ }\r
+while( (cmp(E0,Z) > 0) && (cmp(t,Z) > 0) );\r
+\r
+under2:\r
+sigsave = 0;\r
+\r
+mov( E0, UfThold );\r
+mov( Zero, E1 );\r
+mov( Zero, Q );\r
+mov( U2, E9 );\r
+add( One, E9, S );\r
+mul( C, S, D );\r
+if( cmp(D,C) <= 0 )\r
+ {\r
+ mul( Radix, U2, E9 );\r
+ add( One, E9, S );\r
+ mul( C, S, D );\r
+ if( cmp(D, C) <= 0 )\r
+ {\r
+ ErrCnt[Failure] += 1;\r
+ printf( "multiplication gets too many last digits wrong.\n" );\r
+ mov( E0, Underflow );\r
+ mov( Zero, YY1 );\r
+ mov( Z, PseudoZero );\r
+ }\r
+ }\r
+else\r
+ {\r
+ mov( D, Underflow );\r
+ mul( Underflow, H, PseudoZero );\r
+ mov( Zero, UfThold );\r
+ do\r
+ {\r
+ mov( Underflow, YY1 );\r
+ mov( PseudoZero, Underflow );\r
+ add( E1, E1, t );\r
+ if( cmp(t, E1) <= 0)\r
+ {\r
+ mul( Underflow, HInvrse, Y2 );\r
+ sub( Y2, YY1, E1 );\r
+ FABS( E1 );\r
+ mov( YY1, Q );\r
+ if( (cmp( UfThold, Zero ) == 0)\r
+ && (cmp(YY1, Y2) != 0) )\r
+ mov( YY1, UfThold );\r
+ }\r
+ mul( PseudoZero, H, PseudoZero );\r
+ add( PseudoZero, PseudoZero, t );\r
+ }\r
+ while( (cmp(Underflow, PseudoZero) > 0)\r
+ && (cmp(t, PseudoZero) > 0) );\r
+ }\r
+/* Comment line 4530 .. 4560 */\r
+if( cmp(PseudoZero, Zero) != 0 )\r
+ {\r
+ printf("\n");\r
+ mov(PseudoZero, Z );\r
+/* ... Test PseudoZero for "phoney- zero" violates */\r
+/* ... PseudoZero < Underflow or PseudoZero < PseudoZero + PseudoZero\r
+ ... */\r
+ if( cmp(PseudoZero, Zero) <= 0 )\r
+ {\r
+ ErrCnt[Failure] += 1;\r
+ printf("Positive expressions can underflow to an\n");\r
+ printf("allegedly negative value\n");\r
+ printf("PseudoZero that prints out as: " );\r
+ show( PseudoZero );\r
+ mov( PseudoZero, X );\r
+ neg( X );\r
+ if( cmp(X, Zero) <= 0 )\r
+ {\r
+ printf("But -PseudoZero, which should be\n");\r
+ printf("positive, isn't; it prints out as " );\r
+ show( X );\r
+ }\r
+ }\r
+ else\r
+ {\r
+ ErrCnt[Flaw] += 1;\r
+ printf( "Underflow can stick at an allegedly positive\n");\r
+ printf("value PseudoZero that prints out as " );\r
+ show( PseudoZero );\r
+ }\r
+/* TstPtUf();*/\r
+ }\r
+\r
+/*=============================================*/\r
+Milestone = 120;\r
+/*=============================================*/\r
+mul( CInvrse, Y, t );\r
+mul( CInvrse, YY1, t2 );\r
+if( cmp(t,t2) > 0 )\r
+ {\r
+ mul( H, S, S );\r
+ mov( Underflow, E0 );\r
+ }\r
+if(! ((cmp(E1,Zero) == 0) || (cmp(E1,E0) == 0)) )\r
+ {\r
+ ErrCnt[Defect] += 1;\r
+ if( cmp(E1,E0) < 0 )\r
+ {\r
+ printf("Products underflow at a higher");\r
+ printf(" threshold than differences.\n");\r
+ if( cmp(PseudoZero,Zero) == 0 ) \r
+ mov( E1, E0 );\r
+ }\r
+ else\r
+ {\r
+ printf("Difference underflows at a higher");\r
+ printf(" threshold than products.\n");\r
+ }\r
+ }\r
+printf("Smallest strictly positive number found is E0 = " );\r
+show( E0 );\r
+mov( E0, Z );\r
+TstPtUf();\r
+mov( E0, Underflow );\r
+if(N == 1)\r
+ mov( Y, Underflow );\r
+I = 4;\r
+if( cmp(E1,Zero) == 0 )\r
+ I = 3;\r
+if( cmp( UfThold,Zero) == 0 )\r
+ I = I - 2;\r
+UfNGrad = True;\r
+switch(I)\r
+ {\r
+ case 1:\r
+ mov( Underflow, UfThold );\r
+ mul( CInvrse, Q, t );\r
+ mul( CInvrse, Y, t2 );\r
+ mul( t2, S, t2 );\r
+ if( cmp( t, t2 ) != 0 )\r
+ {\r
+ mov( Y, UfThold );\r
+ ErrCnt[Failure] += 1;\r
+ printf( "Either accuracy deteriorates as numbers\n");\r
+ printf("approach a threshold = " );\r
+ show( UfThold );\r
+ printf(" coming down from " );\r
+ show( C );\r
+ printf(" or else multiplication gets too many last digits wrong.\n");\r
+ }\r
+ break;\r
+ \r
+ case 2:\r
+ ErrCnt[Failure] += 1;\r
+ printf( "Underflow confuses Comparison which alleges that\n");\r
+ printf("Q == Y while denying that |Q - Y| == 0; these values\n");\r
+ printf("print out as Q = " );\r
+ show( Q );\r
+ printf( ", Y = " );\r
+ show( Y );\r
+ sub( Y2, Q, t );\r
+ FABS(t);\r
+ printf ("|Q - Y| = " );\r
+ show( t );\r
+ mov( Q, UfThold );\r
+ break;\r
+ \r
+ case 3:\r
+ mov( X, X );\r
+ break;\r
+ \r
+ case 4:\r
+ div( E9, E1, t );\r
+ sub( t, UfThold, t );\r
+ FABS(t);\r
+ if( (cmp(Q,UfThold) == 0) && (cmp(E1,E0) == 0)\r
+ && (cmp(t,E1) <= 0) )\r
+ {\r
+ UfNGrad = False;\r
+ printf("Underflow is gradual; it incurs Absolute Error =\n");\r
+ printf("(roundoff in UfThold) < E0.\n");\r
+ mul( E0, CInvrse, Y );\r
+ add( OneAndHalf, U2, t );\r
+ mul( Y, t, Y );\r
+ add( One, U2, X );\r
+ mul( CInvrse, X, X );\r
+ div( X, Y, t );\r
+ IEEE = (cmp(t,E0) == 0);\r
+ if( IEEE == 0 )\r
+ {\r
+ printf( "((CInvrse E0) (1.5+U2)) / (CInvrse (1+U2)) != E0\n" );\r
+ printf( "CInvrse = " );\r
+ show( CInvrse );\r
+ printf( "E0 = " );\r
+ show( E0 );\r
+ printf( "U2 = " );\r
+ show( U2 );\r
+ printf( "X = " );\r
+ show(X);\r
+ printf( "Y = " );\r
+ show(Y);\r
+ printf( "Y/X = " );\r
+ show(t);\r
+ }\r
+ }\r
+ }\r
+if(UfNGrad)\r
+ {\r
+ printf("\n");\r
+ div( UfThold, Underflow, R );\r
+ SQRT( R, R );\r
+ if( cmp(R,H) <= 0)\r
+ {\r
+ mul( R, UfThold, Z );\r
+/* X = Z * (One + R * H * (One + H));*/\r
+ add( One, H, X );\r
+ mul( H, X, X );\r
+ mul( R, X, X );\r
+ add( One, X, X );\r
+ mul( Z, X, X );\r
+ }\r
+ else\r
+ {\r
+ mov( UfThold, Z );\r
+/*X = Z * (One + H * H * (One + H));*/\r
+ add( One, H, X );\r
+ mul( H, X, X );\r
+ mul( H, X, X );\r
+ add( One, X, X );\r
+ mul( Z, X, X );\r
+ }\r
+ sub( Z, X, t );\r
+/* if(! ((cmp(X,Z) == 0) || (cmp(t,Zero) != 0)) )*/\r
+ if( (cmp(X,Z) != 0) && (cmp(t,Zero) == 0) )\r
+ {\r
+/* ErrCnt[Flaw] += 1;*/\r
+ ErrCnt[Serious] += 1;\r
+ printf("X = " );\r
+ show( X );\r
+ printf( "\tis not equal to Z = " );\r
+ show( Z );\r
+/* sub( Z, X, Z9 );*/\r
+ printf("yet X - Z yields " );\r
+ show( t );\r
+ printf("which compares equal to " );\r
+ show( Zero );\r
+ printf(" Should this NOT signal Underflow, ");\r
+ printf("this is a SERIOUS DEFECT\nthat causes ");\r
+ printf("confusion when innocent statements like\n");;\r
+ printf(" if (X == Z) ... else");\r
+ printf(" ... (f(X) - f(Z)) / (X - Z) ...\n");\r
+ printf("encounter Division by Zero although actually\n");\r
+ printf("X / Z = 1 + " );\r
+ div( Z, X, t );\r
+ sub( Half, t, t );\r
+ sub( Half, t, t );\r
+ show(t);\r
+ }\r
+ }\r
+printf("The Underflow threshold is " );\r
+show( UfThold );\r
+printf( "below which calculation may suffer larger Relative error than" );\r
+printf( " merely roundoff.\n");\r
+mul( U1, U1, Y2 );\r
+mul( Y2, Y2, Y );\r
+mul( Y, U1, Y2 );\r
+if( cmp( Y2,UfThold) <= 0 )\r
+ {\r
+ if( cmp(Y,E0) > 0 )\r
+ {\r
+ ErrCnt[Defect] += 1;\r
+ I = 5;\r
+ }\r
+ else\r
+ {\r
+ ErrCnt[Serious] += 1;\r
+ I = 4;\r
+ }\r
+ printf("Range is too narrow; U1^%d Underflows.\n", I);\r
+ }\r
+Milestone = 130;\r
+\r
+/*Y = - FLOOR(Half - TwoForty * LOG(UfThold) / LOG(HInvrse)) / TwoForty;*/\r
+LOG( UfThold, Y );\r
+LOG( HInvrse, t );\r
+div( t, Y, Y );\r
+mul( TwoForty, Y, Y );\r
+sub( Y, Half, Y );\r
+FLOOR( Y, Y );\r
+div( TwoForty, Y, Y );\r
+neg(Y);\r
+sub( One, Y, Y2 ); /* ***** changed from Y2 = Y + Y */\r
+printf("Since underflow occurs below the threshold\n");\r
+printf("UfThold = " ); \r
+show( HInvrse );\r
+printf( "\tto the power " );\r
+show( Y );\r
+printf( "only underflow should afflict the expression " );\r
+show( HInvrse );\r
+printf( "\tto the power " );\r
+show( Y2 );\r
+POW( HInvrse, Y2, V9 );\r
+printf("Actually calculating yields: " );\r
+show( V9 );\r
+add( Radix, Radix, t );\r
+add( t, E9, t );\r
+mul( t, UfThold, t );\r
+if( (cmp(V9,Zero) < 0) || (cmp(V9,t) > 0) )\r
+ {\r
+ ErrCnt[Serious] += 1;\r
+ printf( "this is not between 0 and underflow\n");\r
+ printf(" threshold = " );\r
+ show( UfThold );\r
+ }\r
+else\r
+ {\r
+ add( One, E9, t );\r
+ mul( UfThold, t, t );\r
+ if( cmp(V9,t) <= 0 )\r
+ printf("This computed value is O.K.\n");\r
+ else\r
+ {\r
+ ErrCnt[Defect] += 1;\r
+ printf( "this is not between 0 and underflow\n");\r
+ printf(" threshold = " );\r
+ show( UfThold );\r
+ }\r
+ }\r
+\r
+Milestone = 140;\r
+\r
+pow2test();\r
+ \r
+/*=============================================*/\r
+Milestone = 160;\r
+/*=============================================*/\r
+Pause();\r
+printf("Searching for Overflow threshold:\n");\r
+printf("This may generate an error.\n");\r
+sigsave = sigfpe;\r
+I = 0;\r
+mov( CInvrse, Y ); /* a large power of 2 */\r
+neg(Y);\r
+mul( HInvrse, Y, V9 ); /* HInvrse = 2 */\r
+if (setjmp(ovfl_buf))\r
+ goto overflow;\r
+do\r
+ {\r
+ mov( Y, V );\r
+ mov( V9, Y );\r
+ mul( HInvrse, Y, V9 );\r
+ }\r
+while( cmp(V9,Y) < 0 ); /* V9 = 2 * Y */\r
+I = 1;\r
+\r
+overflow:\r
+\r
+show( HInvrse );\r
+printf( "\ttimes " );\r
+show( Y );\r
+printf( "\tequals " );\r
+show( V9 );\r
+\r
+mov( V9, Z );\r
+printf("Can `Z = -Y' overflow?\n");\r
+printf("Trying it on Y = " );\r
+show(Y);\r
+mov( Y, V9 );\r
+neg( V9 );\r
+mov( V9, V0 );\r
+sub( Y, V, t );\r
+add( V, V0, t2 );\r
+if( cmp(t,t2) == 0 )\r
+ printf("Seems O.K.\n");\r
+else\r
+ {\r
+ printf("finds a Flaw, -(-Y) differs from Y.\n");\r
+ printf( "V-Y=t:" );\r
+ show(V);\r
+ show(Y);\r
+ show(t);\r
+ printf( "V+V0=t2:" );\r
+ show(V);\r
+ show(V0);\r
+ show(t2);\r
+ ErrCnt[Flaw] += 1;\r
+ }\r
+if( (cmp(Z, Y) != 0) && (I != 0) )\r
+ {\r
+ ErrCnt[Serious] += 1;\r
+ printf("overflow past " );\r
+ show( Y );\r
+ printf( "\tshrinks to " );\r
+ show( Z );\r
+ printf( "= Y * " );\r
+ show( HInvrse );\r
+ }\r
+/*Y = V * (HInvrse * U2 - HInvrse);*/\r
+mul( HInvrse, U2, Y );\r
+sub( HInvrse, Y, Y );\r
+mul( V, Y, Y );\r
+/*Z = Y + ((One - HInvrse) * U2) * V;*/\r
+sub( HInvrse, One, Z );\r
+mul( Z, U2, Z );\r
+mul( Z, V, Z );\r
+add( Y, Z, Z );\r
+if( cmp(Z,V0) < 0 )\r
+ mov( Z, Y );\r
+if( cmp(Y,V0) < 0)\r
+ mov( Y, V );\r
+sub( V, V0, t );\r
+if( cmp(t,V0) < 0 )\r
+ mov( V0, V );\r
+printf("Overflow threshold is V = " );\r
+show( V );\r
+if(I)\r
+ {\r
+ printf("Overflow saturates at V0 = " );\r
+ show( V0 );\r
+ }\r
+else\r
+printf("There is no saturation value because the system traps on overflow.\n");\r
+\r
+mul( V, One, V9 );\r
+printf("No Overflow should be signaled for V * 1 = " );\r
+show( V9 );\r
+div( One, V, V9 );\r
+ printf(" nor for V / 1 = " );\r
+ show( V9 );\r
+ printf("Any overflow signal separating this * from the one\n");\r
+ printf("above is a DEFECT.\n");\r
+/*=============================================*/\r
+Milestone = 170;\r
+/*=============================================*/\r
+mov( V, t );\r
+neg( t );\r
+k = 0;\r
+if( cmp(t,V) >= 0 )\r
+ k = 1;\r
+mov( V0, t );\r
+neg( t );\r
+if( cmp(t,V0) >= 0 )\r
+ k = 1;\r
+mov( UfThold, t );\r
+neg(t);\r
+if( cmp(t,V) >= 0 )\r
+ k = 1;\r
+if( cmp(UfThold,V) >= 0 )\r
+ k = 1;\r
+if( k != 0 )\r
+ {\r
+ ErrCnt[Failure] += 1;\r
+ printf( "Comparisons involving +-");\r
+ show( V );\r
+ show( V0 );\r
+ show( UfThold );\r
+ printf("are confused by Overflow." );\r
+ }\r
+/*=============================================*/\r
+Milestone = 175;\r
+/*=============================================*/\r
+printf("\n");\r
+for(Indx = 1; Indx <= 3; ++Indx) {\r
+ switch(Indx)\r
+ {\r
+ case 1: mov(UfThold, Z); break;\r
+ case 2: mov( E0, Z); break;\r
+ case 3: mov(PseudoZero, Z); break;\r
+ }\r
+if( cmp(Z, Zero) != 0 )\r
+ {\r
+ SQRT( Z, V9 );\r
+ mul( V9, V9, Y );\r
+ mul( Radix, E9, t );\r
+ sub( t, One, t );\r
+ div( t, Y, t );\r
+ add( One, Radix, t2 );\r
+ add( t2, E9, t2 );\r
+ mul( t2, Z, t2 );\r
+ if( (cmp(t,Z) < 0) || (cmp(Y,t2) > 0) )\r
+ {\r
+ if( cmp(V9,U1) > 0 )\r
+ ErrCnt[Serious] += 1;\r
+ else\r
+ ErrCnt[Defect] += 1;\r
+ printf("Comparison alleges that what prints as Z = " );\r
+ show( Z );\r
+ printf(" is too far from sqrt(Z) ^ 2 = " );\r
+ show( Y );\r
+ }\r
+ }\r
+}\r
+\r
+Milestone = 180;\r
+\r
+for(Indx = 1; Indx <= 2; ++Indx)\r
+ {\r
+ if(Indx == 1)\r
+ mov( V, Z );\r
+ else\r
+ mov( V0, Z );\r
+ SQRT( Z, V9 );\r
+ mul( Radix, E9, X );\r
+ sub( X, One, X );\r
+ mul( X, V9, X );\r
+ mul( V9, X, V9 );\r
+ mul( Two, Radix, t );\r
+ mul( t, E9, t );\r
+ sub( t, One, t );\r
+ mul( t, Z, t );\r
+ if( (cmp(V9,t) < 0) || (cmp(V9,Z) > 0) )\r
+ {\r
+ mov( V9, Y );\r
+ if( cmp(X,W) < 0 )\r
+ ErrCnt[Serious] += 1;\r
+ else\r
+ ErrCnt[Defect] += 1;\r
+ printf("Comparison alleges that Z = " );\r
+ show( Z );\r
+ printf(" is too far from sqrt(Z) ^ 2 :" );\r
+ show( Y );\r
+ }\r
+ }\r
+\r
+Milestone = 190;\r
+\r
+Pause();\r
+mul( UfThold, V, X ); \r
+mul( Radix, Radix, Y );\r
+mul( X, Y, t );\r
+if( (cmp(t,One) < 0) || (cmp(X,Y) > 0) )\r
+ {\r
+ mul( X, Y, t );\r
+ div( U1, Y, t2 );\r
+ if( (cmp(t,U1) < 0) || (cmp(X,t2) > 0) )\r
+ {\r
+ ErrCnt[Defect] += 1;\r
+ printf( "Badly " );\r
+ }\r
+ else\r
+ {\r
+ ErrCnt[Flaw] += 1;\r
+ }\r
+ printf(" unbalanced range; UfThold * V = " );\r
+ show( X );\r
+ printf( "\tis too far from 1.\n");\r
+ }\r
+Milestone = 200;\r
+\r
+for(Indx = 1; Indx <= 5; ++Indx)\r
+ {\r
+ mov( F9, X );\r
+ switch(Indx)\r
+ {\r
+ case 2: add( One, U2, X ); break;\r
+ case 3: mov( V, X ); break;\r
+ case 4: mov(UfThold,X); break;\r
+ case 5: mov(Radix,X);\r
+ }\r
+ mov( X, Y );\r
+\r
+ sigsave = sigfpe;\r
+ if (setjmp(ovfl_buf))\r
+ {\r
+ printf(" X / X traps when X = " );\r
+ show( X );\r
+ }\r
+ else\r
+ {\r
+/*V9 = (Y / X - Half) - Half;*/\r
+ div( X, Y, t );\r
+ sub( Half, t, t );\r
+ sub( Half, t, V9 );\r
+ if( cmp(V9,Zero) == 0 )\r
+ continue;\r
+ mov( U1, t );\r
+ neg(t);\r
+ if( (cmp(V9,t) == 0) && (Indx < 5) )\r
+ {\r
+ ErrCnt[Flaw] += 1;\r
+ }\r
+ else\r
+ {\r
+ ErrCnt[Serious] += 1;\r
+ }\r
+ printf(" X / X differs from 1 when X = " );\r
+ show( X );\r
+ printf(" instead, X / X - 1/2 - 1/2 = " );\r
+ show( V9 );\r
+ }\r
+ }\r
+\r
+ Pause();\r
+ printf("\n");\r
+ {\r
+ static char *msg[] = {\r
+ "FAILUREs encountered =",\r
+ "SERIOUS DEFECTs discovered =",\r
+ "DEFECTs discovered =",\r
+ "FLAWs discovered =" };\r
+ int i;\r
+ for(i = 0; i < 4; i++) if (ErrCnt[i])\r
+ printf("The number of %-29s %d.\n",\r
+ msg[i], ErrCnt[i]);\r
+ }\r
+ printf("\n");\r
+ if ((ErrCnt[Failure] + ErrCnt[Serious] + ErrCnt[Defect]\r
+ + ErrCnt[Flaw]) > 0) {\r
+ if ((ErrCnt[Failure] + ErrCnt[Serious] + ErrCnt[\r
+ Defect] == 0) && (ErrCnt[Flaw] > 0)) {\r
+ printf("The arithmetic diagnosed seems ");\r
+ printf("satisfactory though flawed.\n");\r
+ }\r
+ if ((ErrCnt[Failure] + ErrCnt[Serious] == 0)\r
+ && ( ErrCnt[Defect] > 0)) {\r
+ printf("The arithmetic diagnosed may be acceptable\n");\r
+ printf("despite inconvenient Defects.\n");\r
+ }\r
+ if ((ErrCnt[Failure] + ErrCnt[Serious]) > 0) {\r
+ printf("The arithmetic diagnosed has ");\r
+ printf("unacceptable serious defects.\n");\r
+ }\r
+ if (ErrCnt[Failure] > 0) {\r
+ printf("Fatal FAILURE may have spoiled this");\r
+ printf(" program's subsequent diagnoses.\n");\r
+ }\r
+ }\r
+ else {\r
+ printf("No failures, defects nor flaws have been discovered.\n");\r
+ if (! ((RMult == Rounded) && (RDiv == Rounded)\r
+ && (RAddSub == Rounded) && (RSqrt == Rounded))) \r
+ printf("The arithmetic diagnosed seems satisfactory.\n");\r
+ else {\r
+ k = 0;\r
+ if( cmp( Radix, Two ) == 0 )\r
+ k = 1;\r
+ if( cmp( Radix, Ten ) == 0 )\r
+ k = 1;\r
+ if( (cmp(StickyBit,One) >= 0) && (k == 1) )\r
+ {\r
+ printf("Rounding appears to conform to ");\r
+ printf("the proposed IEEE standard P");\r
+ k = 0;\r
+ k |= cmp( Radix, Two );\r
+ mul( Four, Three, t );\r
+ mul( t, Two, t );\r
+ sub( t, Precision, t );\r
+ sub( TwentySeven, Precision, t2 );\r
+ sub( TwentySeven, t2, t2 );\r
+ add( t2, One, t2 );\r
+ mul( t2, t, t );\r
+ if( (cmp(Radix,Two) == 0)\r
+ && (cmp(t,Zero) == 0) )\r
+ printf("754");\r
+ else\r
+ printf("854");\r
+ if(IEEE)\r
+ printf(".\n");\r
+ else\r
+ {\r
+ printf(",\nexcept for possibly Double Rounding");\r
+ printf(" during Gradual Underflow.\n");\r
+ }\r
+ }\r
+ printf("The arithmetic diagnosed appears to be excellent!\n");\r
+ }\r
+ }\r
+ if (fpecount)\r
+ printf("\nA total of %d floating point exceptions were registered.\n",\r
+ fpecount);\r
+ printf("END OF TEST.\n");\r
+ }\r
+\r
+\r
+/* Random */\r
+/* Random computes\r
+ X = (Random1 + Random9)^5\r
+ Random1 = X - FLOOR(X) + 0.000005 * X;\r
+ and returns the new value of Random1\r
+*/\r
+\r
+\r
+static int randflg = 0;\r
+FLOAT(C5em6);\r
+\r
+Random()\r
+{\r
+\r
+if( randflg == 0 )\r
+ {\r
+ mov( Six, t );\r
+ neg(t);\r
+ POW( Ten, t, t );\r
+ mul( Five, t, C5em6 );\r
+ randflg = 1;\r
+ }\r
+add( Random1, Random9, t );\r
+mul( t, t, t2 );\r
+mul( t2, t2, t2 );\r
+mul( t, t2, t );\r
+FLOOR(t, t2 );\r
+sub( t2, t, t2 );\r
+mul( t, C5em6, t );\r
+add( t, t2, Random1 );\r
+/*return(Random1);*/\r
+}\r
+\r
+/* SqXMinX */\r
+\r
+SqXMinX( ErrKind )\r
+int ErrKind;\r
+{\r
+mul( X, BInvrse, t2 );\r
+sub( t2, X, t );\r
+/*SqEr = ((SQRT(X * X) - XB) - XA) / OneUlp;*/\r
+mul( X, X, Sqarg );\r
+SQRT( Sqarg, SqEr );\r
+sub( t2, SqEr, SqEr );\r
+sub( t, SqEr, SqEr );\r
+div( OneUlp, SqEr, SqEr );\r
+if( cmp(SqEr,Zero) != 0)\r
+ {\r
+ Showsq( 0 );\r
+ add( J, One, J );\r
+ ErrCnt[ErrKind] += 1;\r
+ printf("sqrt of " );\r
+ mul( X, X, t );\r
+ show( t );\r
+ printf( "minus " );\r
+ show( X );\r
+ printf( "equals " );\r
+ mul( OneUlp, SqEr, t );\r
+ show( t );\r
+ printf("\tinstead of correct value 0 .\n");\r
+ }\r
+}\r
+\r
+/* NewD */\r
+\r
+NewD()\r
+{\r
+mul( Z1, Q, X );\r
+/*X = FLOOR(Half - X / Radix) * Radix + X;*/\r
+div( Radix, X, t );\r
+sub( t, Half, t );\r
+FLOOR( t, t );\r
+mul( t, Radix, t );\r
+add( t, X, X );\r
+/*Q = (Q - X * Z) / Radix + X * X * (D / Radix);*/\r
+mul( X, Z, t );\r
+sub( t, Q, t );\r
+div( Radix, t, t );\r
+div( Radix, D, t2 );\r
+mul( X, t2, t2 );\r
+mul( X, t2, t2 );\r
+add( t, t2, Q );\r
+/*Z = Z - Two * X * D;*/\r
+mul( Two, X, t );\r
+mul( t, D, t );\r
+sub( t, Z, Z );\r
+\r
+if( cmp(Z,Zero) <= 0)\r
+ {\r
+ neg(Z);\r
+ neg(Z1);\r
+ }\r
+mul( Radix, D, D );\r
+}\r
+\r
+/* SR3750 */\r
+\r
+SR3750()\r
+{\r
+sub( Radix, X, t );\r
+sub( Radix, Z2, t2 );\r
+k = 0;\r
+if( cmp(t,t2) < 0 )\r
+ k = 1;\r
+sub( Z2, X, t );\r
+sub( Z2, W, t2 );\r
+if( cmp(t,t2) > 0 )\r
+ k = 1;\r
+/*if (! ((X - Radix < Z2 - Radix) || (X - Z2 > W - Z2))) {*/\r
+if( k == 0 )\r
+ {\r
+ I = I + 1;\r
+ mul( X, D, X2 );\r
+ mov( X2, Sqarg );\r
+ SQRT( X2, X2 );\r
+/*Y2 = (X2 - Z2) - (Y - Z2);*/\r
+ sub( Z2, X2, Y2 );\r
+ sub( Z2, Y, t );\r
+ sub( t, Y2, Y2 );\r
+ sub( Half, Y, X2 );\r
+ div( X2, X8, X2 );\r
+ mul( Half, X2, t );\r
+ mul( t, X2, t );\r
+ sub( t, X2, X2 );\r
+/*SqEr = (Y2 + Half) + (Half - X2);*/\r
+ add( Y2, Half, SqEr );\r
+ sub( X2, Half, t );\r
+ add( t, SqEr, SqEr );\r
+ Showsq( -1 );\r
+ sub( X2, Y2, SqEr );\r
+ Showsq( 1 );\r
+ }\r
+}\r
+\r
+/* IsYeqX */\r
+\r
+IsYeqX()\r
+{\r
+if( cmp(Y,X) != 0 )\r
+ {\r
+ if (N <= 0)\r
+ {\r
+ if( (cmp(Z,Zero) == 0) && (cmp(Q,Zero) <= 0) )\r
+ printf("WARNING: computing\n");\r
+ else\r
+ {\r
+ ErrCnt[Defect] += 1;\r
+ printf( "computing\n");\r
+ }\r
+ show( Z );\r
+ printf( "\tto the power " );\r
+ show( Q );\r
+ printf("\tyielded " );\r
+ show( Y );\r
+ printf("\twhich compared unequal to correct " );\r
+ show( X );\r
+ sub( X, Y, t );\r
+ printf("\t\tthey differ by " );\r
+ show( t );\r
+ }\r
+ N = N + 1; /* ... count discrepancies. */\r
+ }\r
+}\r
+\r
+/* SR3980 */\r
+\r
+SR3980()\r
+{\r
+long li;\r
+\r
+do\r
+ {\r
+/*Q = (FLOAT) I;*/\r
+ li = I;\r
+ LTOF( &li, Q );\r
+ POW( Z, Q, Y );\r
+ IsYeqX();\r
+ if(++I > M)\r
+ break;\r
+ mul( Z, X, X );\r
+ }\r
+while( cmp(X,W) < 0 );\r
+}\r
+\r
+/* PrintIfNPositive */\r
+\r
+PrintIfNPositive()\r
+{\r
+if(N > 0)\r
+ printf("Similar discrepancies have occurred %d times.\n", N);\r
+}\r
+\r
+\r
+/* TstPtUf */\r
+\r
+TstPtUf()\r
+{\r
+N = 0;\r
+if( cmp(Z,Zero) != 0)\r
+ {\r
+ printf( "Z = " );\r
+ show(Z);\r
+ printf("Since comparison denies Z = 0, evaluating ");\r
+ printf("(Z + Z) / Z should be safe.\n");\r
+ sigsave = sigfpe;\r
+ if (setjmp(ovfl_buf))\r
+ goto very_serious;\r
+ add( Z, Z, Q9 );\r
+ div( Z, Q9, Q9 );\r
+ printf("What the machine gets for (Z + Z) / Z is " );\r
+ show( Q9 );\r
+ sub( Two, Q9, t );\r
+ FABS(t);\r
+ mul( Radix, U2, t2 );\r
+ if( cmp(t,t2) < 0 )\r
+ {\r
+ printf("This is O.K., provided Over/Underflow");\r
+ printf(" has NOT just been signaled.\n");\r
+ }\r
+ else\r
+ {\r
+ if( (cmp(Q9,One) < 0) || (cmp(Q9,Two) > 0) )\r
+ {\r
+very_serious:\r
+ N = 1;\r
+ ErrCnt [Serious] = ErrCnt [Serious] + 1;\r
+ printf("This is a VERY SERIOUS DEFECT!\n");\r
+ }\r
+ else\r
+ {\r
+ N = 1;\r
+ ErrCnt[Defect] += 1;\r
+ printf("This is a DEFECT!\n");\r
+ }\r
+ }\r
+ mul( Z, One, V9 );\r
+ mov( V9, Random1 );\r
+ mul( One, Z, V9 );\r
+ mov( V9, Random2 );\r
+ div( One, Z, V9 );\r
+ if( (cmp(Z,Random1) == 0) && (cmp(Z,Random2) == 0)\r
+ && (cmp(Z,V9) == 0) )\r
+ {\r
+ if (N > 0)\r
+ Pause();\r
+ }\r
+ else\r
+ {\r
+ N = 1;\r
+ ErrCnt[Defect] += 1;\r
+ printf( "What prints as Z = ");\r
+ show( Z );\r
+ printf( "\tcompares different from " );\r
+ if( cmp(Z,Random1) != 0)\r
+ {\r
+ printf("Z * 1 = " );\r
+ show( Random1 );\r
+ }\r
+ if( (cmp(Z,Random2) != 0)\r
+ || (cmp(Random2,Random1) != 0) )\r
+ {\r
+ printf("1 * Z == " );\r
+ show( Random2 );\r
+ }\r
+ if( cmp(Z,V9) != 0 )\r
+ {\r
+ printf("Z / 1 = " );\r
+ show( V9 );\r
+ }\r
+ if( cmp(Random2,Random1) != 0 )\r
+ {\r
+ ErrCnt[Defect] += 1;\r
+ printf( "Multiplication does not commute!\n");\r
+ printf("\tComparison alleges that 1 * Z = " );\r
+ show(Random2);\r
+ printf("\tdiffers from Z * 1 = " );\r
+ show(Random1);\r
+ }\r
+ Pause();\r
+ }\r
+ }\r
+}\r
+\r
+Pause()\r
+{\r
+}\r
+\r
+Sign( x, y )\r
+FSIZE *x, *y;\r
+{\r
+\r
+if( cmp( x, Zero ) < 0 )\r
+ {\r
+ mov( One, y );\r
+ neg( y );\r
+ }\r
+else\r
+ {\r
+ mov( One, y );\r
+ }\r
+}\r
+\r
+sqtest()\r
+{\r
+printf("\nRunning test of square root(x).\n");\r
+\r
+RSqrt = Other;\r
+k = 0;\r
+SQRT( Zero, t );\r
+k |= cmp( Zero, t );\r
+mov( Zero, t );\r
+neg(t);\r
+SQRT( t, t2 );\r
+k |= cmp( t, t2 );\r
+SQRT( One, t );\r
+k |= cmp( One, t );\r
+if( k != 0 )\r
+ {\r
+ ErrCnt[Failure] += 1;\r
+ printf( "Square root of 0.0, -0.0 or 1.0 wrong\n");\r
+ }\r
+mov( Zero, MinSqEr );\r
+mov( Zero, MaxSqEr );\r
+mov( Zero, J );\r
+mov( Radix, X );\r
+mov( U2, OneUlp );\r
+SqXMinX( Serious );\r
+mov( BInvrse, X );\r
+mul( BInvrse, U1, OneUlp );\r
+SqXMinX( Serious );\r
+mov( U1, X );\r
+mul( U1, U1, OneUlp );\r
+SqXMinX( Serious );\r
+if( cmp(J,Zero) != 0)\r
+ Pause();\r
+printf("Testing if sqrt(X * X) == X for %d Integers X.\n", NoTrials);\r
+mov( Zero, J );\r
+mov( Two, X );\r
+mov( Radix, Y );\r
+if( cmp(Radix,One) != 0 )\r
+ {\r
+ lngint = NoTrials;\r
+ LTOF( &lngint, t );\r
+ FTOL( t, &lng2, X );\r
+ if( lngint != lng2 )\r
+ {\r
+ printf( "Integer conversion error\n" );\r
+ exit(1);\r
+ }\r
+ do\r
+ {\r
+ mov( Y, X );\r
+ mul( Radix, Y, Y );\r
+ sub( X, Y, t2 );\r
+ }\r
+ while( ! (cmp(t2,t) >= 0) );\r
+ }\r
+mul( X, U2, OneUlp );\r
+I = 1;\r
+while(I < 10)\r
+ {\r
+ add( X, One, X );\r
+ SqXMinX( Defect );\r
+ if( cmp(J,Zero) > 0 )\r
+ break;\r
+ I = I + 1;\r
+ }\r
+printf("Test for sqrt monotonicity.\n");\r
+I = - 1;\r
+mov( BMinusU2, X );\r
+mov( Radix, Y );\r
+mul( Radix, U2, Z );\r
+add( Radix, Z, Z );\r
+NotMonot = False;\r
+Monot = False;\r
+while( ! (NotMonot || Monot))\r
+ {\r
+ I = I + 1;\r
+ SQRT(X, X);\r
+ SQRT(Y,Q);\r
+ SQRT(Z,Z);\r
+ if( (cmp(X,Q) > 0) || (cmp(Q,Z) > 0) )\r
+ NotMonot = True;\r
+ else\r
+ {\r
+ add( Q, Half, Q );\r
+ FLOOR( Q, Q );\r
+ mul( Q, Q, t );\r
+ if( (I > 0) || (cmp(Radix,t) == 0) )\r
+ Monot = True;\r
+ else if (I > 0)\r
+ {\r
+ if(I > 1)\r
+ Monot = True;\r
+ else\r
+ {\r
+ mul( Y, BInvrse, Y );\r
+ sub( U1, Y, X );\r
+ add( Y, U1, Z );\r
+ }\r
+ }\r
+ else\r
+ {\r
+ mov( Q, Y );\r
+ sub( U2, Y, X );\r
+ add( Y, U2, Z );\r
+ }\r
+ }\r
+ }\r
+if( Monot )\r
+ printf("sqrt has passed a test for Monotonicity.\n");\r
+else\r
+ {\r
+ ErrCnt[Defect] += 1;\r
+ printf("sqrt(X) is non-monotonic for X near " );\r
+ show(Y);\r
+ }\r
+/*=============================================*/\r
+Milestone = 80;\r
+/*=============================================*/\r
+add( MinSqEr, Half, MinSqEr );\r
+sub( Half, MaxSqEr, MaxSqEr);\r
+/*Y = (SQRT(One + U2) - One) / U2;*/\r
+add( One, U2, Sqarg );\r
+SQRT( Sqarg, Y );\r
+sub( One, Y, Y );\r
+div( U2, Y, Y );\r
+/*SqEr = (Y - One) + U2 / Eight;*/\r
+sub( One, Y, t );\r
+div( Eight, U2, SqEr );\r
+add( t, SqEr, SqEr );\r
+Showsq( 1 );\r
+div( Eight, U2, SqEr );\r
+add( Y, SqEr, SqEr );\r
+Showsq( -1 );\r
+/*Y = ((SQRT(F9) - U2) - (One - U2)) / U1;*/\r
+mov( F9, Sqarg );\r
+SQRT( Sqarg, Y );\r
+sub( U2, Y, Y );\r
+sub( U2, One, t );\r
+sub( t, Y, Y );\r
+div( U1, Y, Y );\r
+div( Eight, U1, SqEr );\r
+add( Y, SqEr, SqEr );\r
+Showsq( 1 );\r
+/*SqEr = (Y + One) + U1 / Eight;*/\r
+div( Eight, U1, t );\r
+add( Y, One, SqEr );\r
+add( SqEr, t, SqEr );\r
+Showsq( -1 );\r
+mov( U2, OneUlp );\r
+mov( OneUlp, X );\r
+for( Indx = 1; Indx <= 3; ++Indx)\r
+ {\r
+/*Y = SQRT((X + U1 + X) + F9);*/\r
+ add( X, U1, Y );\r
+ add( Y, X, Y );\r
+ add( Y, F9, Y );\r
+ mov( Y, Sqarg );\r
+ SQRT( Sqarg, Y );\r
+/*Y = ((Y - U2) - ((One - U2) + X)) / OneUlp;*/\r
+ sub( U2, One, t );\r
+ add( t, X, t );\r
+ sub( U2, Y, Y );\r
+ sub( t, Y, Y );\r
+ div( OneUlp, Y, Y );\r
+/*Z = ((U1 - X) + F9) * Half * X * X / OneUlp;*/\r
+ sub( X, U1, t );\r
+ add( t, F9, t );\r
+ mul( t, Half, t );\r
+ mul( t, X, t );\r
+ mul( t, X, t );\r
+ div( OneUlp, t, Z );\r
+ add( Y, Half, SqEr );\r
+ add( SqEr, Z, SqEr );\r
+ Showsq( -1 );\r
+ sub( Half, Y, SqEr );\r
+ add( SqEr, Z, SqEr );\r
+ Showsq( 1 );\r
+ if(((Indx == 1) || (Indx == 3))) \r
+ {\r
+/*X = OneUlp * Sign (X) * FLOOR(Eight / (Nine * SQRT(OneUlp)));*/\r
+ mov( OneUlp, Sqarg );\r
+ SQRT( Sqarg, t );\r
+ mul( Nine, t, t );\r
+ div( t, Eight, t );\r
+ FLOOR( t, t );\r
+ Sign( X, t2 );\r
+ mul( t2, t, t );\r
+ mul( OneUlp, t, X );\r
+ }\r
+ else\r
+ {\r
+ mov( U1, OneUlp );\r
+ mov( OneUlp, X );\r
+ neg( X );\r
+ }\r
+ }\r
+/*=============================================*/\r
+Milestone = 85;\r
+/*=============================================*/\r
+SqRWrng = False;\r
+Anomaly = False;\r
+if( cmp(Radix,One) != 0 )\r
+ {\r
+ printf("Testing whether sqrt is rounded or chopped.\n");\r
+/*D = FLOOR(Half + POW(Radix, One + Precision - FLOOR(Precision)));*/\r
+ FLOOR( Precision, t2 );\r
+ add( One, Precision, t );\r
+ sub( t2, t, t );\r
+ POW( Radix, t, D );\r
+ add( Half, D, D );\r
+ FLOOR( D, D );\r
+/* ... == Radix^(1 + fract) if (Precision == Integer + fract. */\r
+ div( Radix, D, X );\r
+ div( A1, D, Y );\r
+ FLOOR( X, t );\r
+ FLOOR( Y, t2 );\r
+ if( (cmp(X,t) != 0) || (cmp(Y,t2) != 0) )\r
+ {\r
+ Anomaly = True;\r
+ printf( "Anomaly 1\n" );\r
+ }\r
+ else\r
+ {\r
+ mov( Zero, X );\r
+ mov( X, Z2 );\r
+ mov( One, Y );\r
+ mov( Y, Y2 );\r
+ sub( One, Radix, Z1 );\r
+ mul( Four, D, FourD );\r
+ do\r
+ {\r
+ if( cmp(Y2,Z2) >0 )\r
+ {\r
+ mov( Radix, Q );\r
+ mov( Y, YY1 );\r
+ do\r
+ {\r
+/*X1 = FABS(Q + FLOOR(Half - Q / YY1) * YY1);*/\r
+ div( YY1, Q, t );\r
+ sub( t, Half, t );\r
+ FLOOR( t, t );\r
+ mul( t, YY1, t );\r
+ add( Q, t, X1 );\r
+ FABS( X1 );\r
+ mov( YY1, Q );\r
+ mov( X1, YY1 );\r
+ }\r
+ while( ! (cmp(X1,Zero) <= 0) );\r
+ if( cmp(Q,One) <= 0 )\r
+ {\r
+ mov( Y2, Z2 );\r
+ mov( Y, Z );\r
+ }\r
+ }\r
+ add( Y, Two, Y );\r
+ add( X, Eight, X );\r
+ add( Y2, X, Y2 );\r
+ if( cmp(Y2,FourD) >= 0 )\r
+ sub( FourD, Y2, Y2 );\r
+ }\r
+ while( ! (cmp(Y,D) >= 0) );\r
+ sub( Z2, FourD, X8 );\r
+ mul( Z, Z, Q );\r
+ add( X8, Q, Q );\r
+ div( FourD, Q, Q );\r
+ div( Eight, X8, X8 );\r
+ FLOOR( Q, t );\r
+ if( cmp(Q,t) != 0 )\r
+ {\r
+ Anomaly = True;\r
+ printf( "Anomaly 2\n" );\r
+ }\r
+ else\r
+ {\r
+ Break = False;\r
+ do\r
+ {\r
+ mul( Z1, Z, X );\r
+/*X = X - FLOOR(X / Radix) * Radix;*/\r
+ div( Radix, X, t );\r
+ FLOOR( t, t );\r
+ mul( t, Radix, t );\r
+ sub( t, X, X );\r
+ if( cmp(X,One) == 0 ) \r
+ Break = True;\r
+ else\r
+ sub( One, Z1, Z1 );\r
+ }\r
+ while( ! (Break || (cmp(Z1,Zero) <= 0)) );\r
+ if( (cmp(Z1,Zero) <= 0) && (! Break))\r
+ {\r
+ printf( "Anomaly 3\n" );\r
+ Anomaly = True;\r
+ }\r
+ else\r
+ {\r
+ if( cmp(Z1,RadixD2) > 0)\r
+ sub( Radix, Z1, Z1 );\r
+ do\r
+ {\r
+ NewD();\r
+ mul( U2, D, t );\r
+ }\r
+ while( ! (cmp(t,F9) >= 0) );\r
+ mul( D, Radix, t );\r
+ sub( D, t, t );\r
+ sub( D, W, t2 );\r
+ if (cmp(t,t2) != 0 )\r
+ {\r
+ printf( "Anomaly 4\n" );\r
+ Anomaly = True;\r
+ }\r
+ else\r
+ {\r
+ mov( D, Z2 );\r
+ I = 0;\r
+ add( One, Z, t );\r
+ mul( t, Half, t );\r
+ add( D, t, Y );\r
+ add( D, Z, X );\r
+ add( X, Q, X );\r
+ SR3750();\r
+ sub( Z, One, t );\r
+ mul( t, Half, t );\r
+ add( D, t, Y );\r
+ add( Y, D, Y );\r
+ sub( Z, D, X );\r
+ add( X, D, X );\r
+ add( X, Q, t );\r
+ add( t, X, X );\r
+ SR3750();\r
+ NewD();\r
+ sub( Z2, D, t );\r
+ sub( Z2, W, t2 );\r
+ if(cmp(t,t2) != 0 )\r
+ {\r
+ printf( "Anomaly 5\n" );\r
+ Anomaly = True;\r
+ }\r
+ else\r
+ {\r
+/*Y = (D - Z2) + (Z2 + (One - Z) * Half);*/\r
+ sub( Z, One, t );\r
+ mul( t, Half, t );\r
+ add( Z2, t, t );\r
+ sub( Z2, D, Y );\r
+ add( Y, t, Y );\r
+/*X = (D - Z2) + (Z2 - Z + Q);*/\r
+ sub( Z, Z2, t );\r
+ add( t, Q, t );\r
+ sub( Z2, D, X );\r
+ add( X, t, X );\r
+ SR3750();\r
+ add( One, Z, Y );\r
+ mul( Y, Half, Y );\r
+ mov( Q, X );\r
+ SR3750();\r
+ if(I == 0)\r
+ {\r
+ printf( "Anomaly 6\n" );\r
+ Anomaly = True;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ if ((I == 0) || Anomaly)\r
+ {\r
+ ErrCnt[Failure] += 1;\r
+ printf( "Anomalous arithmetic with Integer < \n");\r
+ printf("Radix^Precision = " );\r
+ show( W );\r
+ printf(" fails test whether sqrt rounds or chops.\n");\r
+ SqRWrng = True;\r
+ }\r
+ }\r
+if(! Anomaly)\r
+ {\r
+ if(! ((cmp(MinSqEr,Zero) < 0) || (cmp(MaxSqEr,Zero) > 0))) {\r
+ RSqrt = Rounded;\r
+ printf("Square root appears to be correctly rounded.\n");\r
+ }\r
+ else\r
+ {\r
+ k = 0;\r
+ add( MaxSqEr, U2, t );\r
+ sub( Half, U2, t2 );\r
+ if( cmp(t,t2) > 0 )\r
+ k = 1;\r
+ if( cmp( MinSqEr, Half ) > 0 )\r
+ k = 1;\r
+ add( MinSqEr, Radix, t );\r
+ if( cmp( t, Half ) < 0 )\r
+ k = 1;\r
+ if( k == 1 )\r
+ SqRWrng = True;\r
+ else\r
+ {\r
+ RSqrt = Chopped;\r
+ printf("Square root appears to be chopped.\n");\r
+ }\r
+ }\r
+ }\r
+if( SqRWrng )\r
+ {\r
+ printf("Square root is neither chopped nor correctly rounded.\n");\r
+ printf("Observed errors run from " );\r
+ sub( Half, MinSqEr, t );\r
+ show( t );\r
+ printf("\tto " );\r
+ add( Half, MaxSqEr, t );\r
+ show( t );\r
+ printf( "ulps.\n" );\r
+ sub( MinSqEr, MaxSqEr, t );\r
+ mul( Radix, Radix, t2 );\r
+ if( cmp( t, t2 ) >= 0 )\r
+ {\r
+ ErrCnt[Serious] += 1;\r
+ printf( "sqrt gets too many last digits wrong\n");\r
+ }\r
+ }\r
+}\r
+\r
+Showsq( arg )\r
+int arg;\r
+{\r
+\r
+k = 0;\r
+if( arg <= 0 )\r
+ {\r
+ if( cmp(SqEr,MinSqEr) < 0 )\r
+ {\r
+ k = 1;\r
+ mov( SqEr, MinSqEr );\r
+ }\r
+ }\r
+if( arg >= 0 )\r
+ {\r
+ if( cmp(SqEr,MaxSqEr) > 0 )\r
+ {\r
+ k = 2;\r
+ mov( SqEr, MaxSqEr );\r
+ }\r
+ }\r
+#if DEBUG\r
+if( k != 0 )\r
+ {\r
+ printf( "Square root of " );\r
+ show( arg );\r
+ printf( "\tis in error by " );\r
+ show( SqEr );\r
+ }\r
+#endif\r
+}\r
+\r
+\r
+pow1test()\r
+{\r
+\r
+/*=============================================*/\r
+Milestone = 90;\r
+/*=============================================*/\r
+Pause();\r
+printf("Testing powers Z^i for small Integers Z and i.\n");\r
+N = 0;\r
+/* ... test powers of zero. */\r
+I = 0;\r
+mov( Zero, Z );\r
+neg(Z);\r
+M = 3;\r
+Break = False;\r
+do\r
+ {\r
+ mov( One, X );\r
+ SR3980();\r
+ if(I <= 10)\r
+ {\r
+ I = 1023;\r
+ SR3980();\r
+ }\r
+ if( cmp(Z,MinusOne) == 0 )\r
+ Break = True;\r
+ else\r
+ {\r
+ mov( MinusOne, Z );\r
+ PrintIfNPositive();\r
+ N = 0;\r
+/* .. if(-1)^N is invalid, replace MinusOne by One. */\r
+ I = - 4;\r
+ }\r
+ }\r
+while( ! Break );\r
+PrintIfNPositive();\r
+N1 = N;\r
+N = 0;\r
+mov( A1, Z );\r
+/*M = FLOOR(Two * LOG(W) / LOG(A1));*/\r
+LOG( W, t );\r
+mul( Two, t, t );\r
+FLOOR( t, t );\r
+LOG( A1, t2 );\r
+div( t2, t, t );\r
+FTOL( t, &lngint, t2 );\r
+M = lngint;\r
+Break = False;\r
+do\r
+ {\r
+ mov( Z, X );\r
+ I = 1;\r
+ SR3980();\r
+ if( cmp(Z,AInvrse) == 0 )\r
+ Break = True;\r
+ else\r
+ mov( AInvrse, Z );\r
+ }\r
+while( ! (Break) );\r
+/*=============================================*/\r
+Milestone = 100;\r
+/*=============================================*/\r
+/* Powers of Radix have been tested, */\r
+/* next try a few primes */\r
+\r
+M = NoTrials;\r
+\r
+mov( Three, Z );\r
+do\r
+ {\r
+ mov( Z, X );\r
+ I = 1;\r
+ SR3980();\r
+ do\r
+ {\r
+ add( Z, Two, Z );\r
+ div( Three, Z, t );\r
+ FLOOR( t, t );\r
+ mul( Three, t, t );\r
+ }\r
+ while( cmp(t,Z) == 0 );\r
+ mul( Eight, Three, t );\r
+ }\r
+while( cmp(Z,t) < 0 );\r
+\r
+if(N > 0)\r
+ {\r
+ printf("Errors like this may invalidate financial calculations\n");\r
+ printf("\tinvolving interest rates.\n");\r
+ }\r
+PrintIfNPositive();\r
+N += N1;\r
+if(N == 0)\r
+ printf("... no discrepancies found.\n");\r
+if(N > 0)\r
+ Pause();\r
+else printf("\n");\r
+}\r
+\r
+\r
+\r
+pow2test()\r
+{\r
+printf("\n");\r
+/* ...calculate Exp2 == exp(2) == 7.38905 60989 30650 22723 04275-... */\r
+mov( Zero, X );\r
+mov( Two, t2 ); /*I = 2;*/\r
+\r
+mul( Two, Three, Y );\r
+mov( Zero, Q );\r
+N = 0;\r
+do\r
+ {\r
+ mov( X, Z );\r
+ add( t2, One, t2 ); /*I = I + 1;*/\r
+ add( t2, t2, t );\r
+ div( t, Y, Y ); /*Y = Y / (I + I);*/\r
+ add( Y, Q, R );\r
+ add( Z, R, X );\r
+ sub( X, Z, Q );\r
+ add( Q, R, Q );\r
+ }\r
+while( cmp(X,Z) > 0 );\r
+\r
+/*Z = (OneAndHalf + One / Eight) + X / (OneAndHalf * ThirtyTwo);*/\r
+div( Eight, One, t );\r
+add( OneAndHalf, t, Z );\r
+mul( OneAndHalf, ThirtyTwo, t );\r
+div( t, X, t );\r
+add( Z, t, Z );\r
+mul( Z, Z, X );\r
+mul( X, X, Exp2 );\r
+mov( F9, X );\r
+sub( U1, X, Y );\r
+printf("Testing X^((X + 1) / (X - 1)) vs. exp(2) = " );\r
+show( Exp2 );\r
+printf( "\tas X -> 1.\n" );\r
+for(I = 1;;)\r
+ {\r
+ sub( BInvrse, X, Z );\r
+/*Z = (X + One) / (Z - (One - BInvrse));*/\r
+ add( X, One, t2 );\r
+ sub( BInvrse, One, t );\r
+ sub( t, Z, t );\r
+ div( t, t2, Z );\r
+ POW( X, Z, Sqarg );\r
+ sub( Exp2, Sqarg, Q );\r
+ mov( Q, t );\r
+ FABS( t );\r
+ mul( TwoForty, U2, t2 );\r
+ if( cmp( t, t2 ) > 0 )\r
+ {\r
+ N = 1;\r
+ sub( BInvrse, X, V9 );\r
+ sub( BInvrse, One, t );\r
+ sub( t, V9, V9 );\r
+ ErrCnt[Defect] += 1;\r
+ printf( "Calculated " );\r
+ show( Sqarg );\r
+ printf(" for \t(1 + " );\r
+ show( V9 );\r
+ printf( "\tto the power " );\r
+ show( Z );\r
+ printf("\tdiffers from correct value by " );\r
+ show( Q );\r
+ printf("\tThis much error may spoil financial\n");\r
+ printf("\tcalculations involving tiny interest rates.\n");\r
+ break;\r
+ }\r
+ else\r
+ {\r
+ sub( X, Y, Z );\r
+ mul( Z, Two, Z );\r
+ add( Z, Y, Z );\r
+ mov( Y, X );\r
+ mov( Z, Y );\r
+ sub( F9, X, Z );\r
+ mul( Z, Z, Z );\r
+ add( Z, One, Z );\r
+ if( (cmp(Z,One) > 0) && (I < NoTrials) )\r
+ I++;\r
+ else\r
+ {\r
+ if( cmp(X,One) > 0 )\r
+ {\r
+ if(N == 0)\r
+ printf("Accuracy seems adequate.\n");\r
+ break;\r
+ }\r
+ else\r
+ {\r
+ add( One, U2, X );\r
+ add( U2, U2, Y );\r
+ add( X, Y, Y );\r
+ I = 1;\r
+ }\r
+ }\r
+ }\r
+ }\r
+/*=============================================*/\r
+Milestone = 150;\r
+/*=============================================*/\r
+printf("Testing powers Z^Q at four nearly extreme values.\n");\r
+N = 0;\r
+mov( A1, Z );\r
+/*Q = FLOOR(Half - LOG(C) / LOG(A1));*/\r
+LOG( C, t );\r
+LOG( A1, t2 );\r
+div( t2, t, t );\r
+sub( t, Half, t );\r
+FLOOR( t, Q );\r
+Break = False;\r
+do\r
+ {\r
+ mov( CInvrse, X );\r
+ POW( Z, Q, Y );\r
+ IsYeqX();\r
+ neg(Q);\r
+ mov( C, X );\r
+ POW( Z, Q, Y );\r
+ IsYeqX();\r
+ if( cmp(Z,One) < 0 )\r
+ Break = True;\r
+ else\r
+ mov( AInvrse, Z );\r
+ }\r
+while( ! (Break));\r
+PrintIfNPositive();\r
+if(N == 0)\r
+ printf(" ... no discrepancies found.\n");\r
+printf("\n");\r
+}\r
--- /dev/null
+/* epow.c */\r
+/* power function: z = x**y */\r
+/* by Stephen L. Moshier. */\r
+\r
+\r
+#include "ehead.h"\r
+#define MAXPOS ((long) (((unsigned long) ~(0L)) >> 1))\r
+#define MAXNEG (-MAXPOS)\r
+/* #define MAXNEG (-MAXPOS - 1L) */\r
+\r
+extern int rndprc;\r
+void epowi();\r
+static void epowr();\r
+\r
+\r
+/* Run-time determination of largest integers */\r
+\r
+int powinited = 0;\r
+unsigned short maxposint[NE], maxnegint[NE];\r
+\r
+void initpow()\r
+{\r
+long li;\r
+\r
+li = MAXPOS;\r
+ltoe( &li, maxposint );\r
+li = MAXNEG;\r
+ltoe( &li, maxnegint );\r
+powinited = 1;\r
+}\r
+\r
+\r
+\r
+\r
+void epow( x, y, z )\r
+unsigned short *x, *y, *z;\r
+{\r
+unsigned short w[NE];\r
+int rndsav;\r
+long li;\r
+\r
+if( powinited == 0 )\r
+ initpow();\r
+\r
+/* Check for integer power. */\r
+\r
+efloor( y, w );\r
+if( (ecmp(y,w) == 0)\r
+ && (ecmp(maxposint,w) >= 0)\r
+ && (ecmp(w,maxnegint) >= 0) )\r
+ {\r
+ eifrac( y, &li, w );\r
+ epowi( x, y, z );\r
+ return;\r
+ }\r
+epowr( x, y, z );\r
+}\r
+\r
+\r
+\r
+\r
+/* y is integer valued. */\r
+\r
+void epowi( x, y, z )\r
+unsigned short x[], y[], z[];\r
+{\r
+unsigned short w[NE];\r
+long li, lx;\r
+unsigned long lu;\r
+int rndsav;\r
+unsigned short signx;\r
+/* unsigned short signy; */\r
+\r
+if( powinited == 0 )\r
+ initpow();\r
+\r
+rndsav = rndprc;\r
+\r
+if( (ecmp(y,maxposint) > 0) || (ecmp(maxnegint,y) > 0) )\r
+ {\r
+ epowr( x, y, z );\r
+ return;\r
+ }\r
+\r
+eifrac( y, &li, w );\r
+if( li < 0 )\r
+ lx = -li;\r
+else\r
+ lx = li;\r
+\r
+/*\r
+if( (x[NE-1] & (unsigned short )0x7fff) == 0 )\r
+*/\r
+\r
+if( ecmp( x, ezero) == 0 )\r
+ {\r
+ if( li == 0 )\r
+ {\r
+ emov( eone, z );\r
+ return;\r
+ }\r
+ else if( li < 0 )\r
+ {\r
+ einfin( z );\r
+ return;\r
+ }\r
+ else\r
+ {\r
+ eclear( z );\r
+ return;\r
+ }\r
+ }\r
+\r
+if( li == 0L )\r
+ {\r
+ emov( eone, z );\r
+ return;\r
+ }\r
+\r
+emov( x, w );\r
+signx = w[NE-1] & (unsigned short )0x8000;\r
+w[NE-1] &= (unsigned short )0x7fff;\r
+\r
+/* Overflow detection */\r
+/*\r
+lx = li * (w[NE-1] - 0x3fff);\r
+if( lx > 16385L )\r
+ {\r
+ einfin( z );\r
+ mtherr( "epowi", OVERFLOW );\r
+ goto done;\r
+ }\r
+if( lx < -16450L )\r
+ {\r
+ eclear( z );\r
+ return;\r
+ }\r
+*/\r
+rndprc = NBITS;\r
+\r
+if( li < 0 )\r
+ {\r
+ lu = (unsigned int )( -li );\r
+/* signy = 0xffff;*/\r
+ ediv( w, eone, w );\r
+ }\r
+else\r
+ {\r
+ lu = (unsigned int )li;\r
+/* signy = 0;*/\r
+ }\r
+\r
+/* First bit of the power */\r
+if( lu & 1 )\r
+ {\r
+ emov( w, z );\r
+ } \r
+else\r
+ {\r
+ emov( eone, z );\r
+ signx = 0;\r
+ }\r
+\r
+\r
+lu >>= 1;\r
+while( lu != 0L )\r
+ {\r
+ emul( w, w, w ); /* arg to the 2-to-the-kth power */\r
+ if( lu & 1L ) /* if that bit is set, then include in product */\r
+ emul( w, z, z );\r
+ lu >>= 1;\r
+ }\r
+\r
+\r
+done:\r
+\r
+if( signx )\r
+ eneg( z ); /* odd power of negative number */\r
+\r
+/*\r
+if( signy )\r
+ {\r
+ if( ecmp( z, ezero ) != 0 )\r
+ {\r
+ ediv( z, eone, z );\r
+ }\r
+ else\r
+ {\r
+ einfin( z );\r
+ printf( "epowi OVERFLOW\n" );\r
+ }\r
+ }\r
+*/\r
+rndprc = rndsav;\r
+emul( eone, z, z );\r
+}\r
+\r
+\r
+\r
+/* z = exp( y * log(x) ) */\r
+\r
+static void epowr( x, y, z )\r
+unsigned short *x, *y, *z;\r
+{\r
+unsigned short w[NE];\r
+int rndsav;\r
+\r
+rndsav = rndprc;\r
+rndprc = NBITS;\r
+elog( x, w );\r
+emul( y, w, w );\r
+eexp( w, z );\r
+rndprc = rndsav;\r
+emul( eone, z, z );\r
+}\r
--- /dev/null
+/* xtanh.c */\r
+/* hyperbolic tangent check routine */\r
+/* this subroutine is used by the exponential function routine */\r
+/* by Stephen L. Moshier. */\r
+\r
+\r
+\r
+#include "ehead.h"\r
+\r
+\r
+void etanh( x, y )\r
+unsigned short *x, *y;\r
+{\r
+unsigned short e[NE], r[NE], j[NE], xx[NE], m2[NE];\r
+short i, n;\r
+long lj;\r
+\r
+emov( x, r );\r
+r[NE-1] &= (unsigned short )0x7fff;\r
+if( ecmp(r, eone) >= 0 )\r
+ {\r
+/* tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x))\r
+ * Note eexp() calls xtanh, but with an argument less than (1 + log 2)/2.\r
+ */\r
+ eexp( r, e );\r
+ ediv( e, eone, r );\r
+ esub( r, e, xx );\r
+ eadd( r, e, j );\r
+ ediv( j, xx, y );\r
+ return;\r
+ }\r
+\r
+emov( etwo, m2 );\r
+eneg( m2 );\r
+\r
+n = NBITS/8; /* Number of terms to do in the continued fraction */\r
+lj = 2 * n + 1;\r
+ltoe( &lj, j );\r
+\r
+emov( j, e );\r
+emul( x, x, xx );\r
+\r
+/* continued fraction */\r
+for( i=0; i<n; i++)\r
+ {\r
+ ediv( e, xx, r );\r
+ eadd( m2, j, j );\r
+ eadd( r, j, e );\r
+ }\r
+\r
+ediv( e, x, y );\r
+}\r
--- /dev/null
+#include "ehead.h"\r
+void emovi(), emovo(), ecleaz(), eshdn8(), emdnorm();\r
+void todec();\r
+/*\r
+; convert DEC double precision to e type\r
+; double d;\r
+; short e[NE];\r
+; dectoe( &d, e );\r
+*/\r
+void dectoe( d, e )\r
+unsigned short *d;\r
+unsigned short *e;\r
+{\r
+unsigned short y[NI];\r
+register unsigned short r, *p;\r
+\r
+ecleaz(y); /* start with a zero */\r
+p = y; /* point to our number */\r
+r = *d; /* get DEC exponent word */\r
+if( *d & (unsigned int )0x8000 )\r
+ *p = 0xffff; /* fill in our sign */\r
+++p; /* bump pointer to our exponent word */\r
+r &= 0x7fff; /* strip the sign bit */\r
+if( r == 0 ) /* answer = 0 if high order DEC word = 0 */\r
+ goto done;\r
+\r
+\r
+r >>= 7; /* shift exponent word down 7 bits */\r
+r += EXONE - 0201; /* subtract DEC exponent offset */\r
+ /* add our e type exponent offset */\r
+*p++ = r; /* to form our exponent */\r
+\r
+r = *d++; /* now do the high order mantissa */\r
+r &= 0177; /* strip off the DEC exponent and sign bits */\r
+r |= 0200; /* the DEC understood high order mantissa bit */\r
+*p++ = r; /* put result in our high guard word */\r
+\r
+*p++ = *d++; /* fill in the rest of our mantissa */\r
+*p++ = *d++;\r
+*p = *d;\r
+\r
+eshdn8(y); /* shift our mantissa down 8 bits */\r
+done:\r
+emovo( y, e );\r
+}\r
+\r
+\r
+\r
+/*\r
+; convert e type to DEC double precision\r
+; double d;\r
+; short e[NE];\r
+; etodec( e, &d );\r
+*/\r
+#if 0\r
+static unsigned short decbit[NI] = {0,0,0,0,0,0,0200,0};\r
+void etodec( x, d )\r
+unsigned short *x, *d;\r
+{\r
+unsigned short xi[NI];\r
+register unsigned short r;\r
+int i, j;\r
+\r
+emovi( x, xi );\r
+*d = 0;\r
+if( xi[0] != 0 )\r
+ *d = 0100000;\r
+r = xi[E];\r
+if( r < (EXONE - 128) )\r
+ goto zout;\r
+i = xi[M+4];\r
+if( (i & 0200) != 0 )\r
+ {\r
+ if( (i & 0377) == 0200 )\r
+ {\r
+ if( (i & 0400) != 0 )\r
+ {\r
+ /* check all less significant bits */\r
+ for( j=M+5; j<NI; j++ )\r
+ {\r
+ if( xi[j] != 0 )\r
+ goto yesrnd;\r
+ }\r
+ }\r
+ goto nornd;\r
+ }\r
+yesrnd:\r
+ eaddm( decbit, xi );\r
+ r -= enormlz(xi);\r
+ }\r
+\r
+nornd:\r
+\r
+r -= EXONE;\r
+r += 0201;\r
+if( r < 0 )\r
+ {\r
+zout:\r
+ *d++ = 0;\r
+ *d++ = 0;\r
+ *d++ = 0;\r
+ *d++ = 0;\r
+ return;\r
+ }\r
+if( r >= 0377 )\r
+ {\r
+ *d++ = 077777;\r
+ *d++ = -1;\r
+ *d++ = -1;\r
+ *d++ = -1;\r
+ return;\r
+ }\r
+r &= 0377;\r
+r <<= 7;\r
+eshup8( xi );\r
+xi[M] &= 0177;\r
+r |= xi[M];\r
+*d++ |= r;\r
+*d++ = xi[M+1];\r
+*d++ = xi[M+2];\r
+*d++ = xi[M+3];\r
+}\r
+#else\r
+\r
+extern int rndprc;\r
+\r
+void etodec( x, d )\r
+unsigned short *x, *d;\r
+{\r
+unsigned short xi[NI];\r
+long exp;\r
+int rndsav;\r
+\r
+emovi( x, xi );\r
+exp = (long )xi[E] - (EXONE - 0201); /* adjust exponent for offsets */\r
+/* round off to nearest or even */\r
+rndsav = rndprc;\r
+rndprc = 56;\r
+emdnorm( xi, 0, 0, exp, 64 );\r
+rndprc = rndsav;\r
+todec( xi, d );\r
+}\r
+\r
+void todec( x, y )\r
+unsigned short *x, *y;\r
+{\r
+unsigned short i;\r
+unsigned short *p;\r
+\r
+p = x;\r
+*y = 0;\r
+if( *p++ )\r
+ *y = 0100000;\r
+i = *p++;\r
+if( i == 0 )\r
+ {\r
+ *y++ = 0;\r
+ *y++ = 0;\r
+ *y++ = 0;\r
+ *y++ = 0;\r
+ return;\r
+ }\r
+if( i > 0377 )\r
+ {\r
+ *y++ |= 077777;\r
+ *y++ = 0xffff;\r
+ *y++ = 0xffff;\r
+ *y++ = 0xffff;\r
+ return;\r
+ }\r
+i &= 0377;\r
+i <<= 7;\r
+eshup8( x );\r
+x[M] &= 0177;\r
+i |= x[M];\r
+*y++ |= i;\r
+*y++ = x[M+1];\r
+*y++ = x[M+2];\r
+*y++ = x[M+3];\r
+}\r
+#endif\r
--- /dev/null
+/* ieee.c\r
+ *\r
+ * Extended precision IEEE binary floating point arithmetic routines\r
+ *\r
+ * Numbers are stored in C language as arrays of 16-bit unsigned\r
+ * short integers. The arguments of the routines are pointers to\r
+ * the arrays.\r
+ *\r
+ *\r
+ * External e type data structure, simulates Intel 8087 chip\r
+ * temporary real format but possibly with a larger significand:\r
+ *\r
+ * NE-1 significand words (least significant word first,\r
+ * most significant bit is normally set)\r
+ * exponent (value = EXONE for 1.0,\r
+ * top bit is the sign)\r
+ *\r
+ *\r
+ * Internal data structure of a number (a "word" is 16 bits):\r
+ *\r
+ * ei[0] sign word (0 for positive, 0xffff for negative)\r
+ * ei[1] biased exponent (value = EXONE for the number 1.0)\r
+ * ei[2] high guard word (always zero after normalization)\r
+ * ei[3]\r
+ * to ei[NI-2] significand (NI-4 significand words,\r
+ * most significant word first,\r
+ * most significant bit is set)\r
+ * ei[NI-1] low guard word (0x8000 bit is rounding place)\r
+ *\r
+ *\r
+ *\r
+ * Routines for external format numbers\r
+ *\r
+ * asctoe( string, e ) ASCII string to extended double e type\r
+ * asctoe64( string, &d ) ASCII string to long double\r
+ * asctoe53( string, &d ) ASCII string to double\r
+ * asctoe24( string, &f ) ASCII string to single\r
+ * asctoeg( string, e, prec ) ASCII string to specified precision\r
+ * e24toe( &f, e ) IEEE single precision to e type\r
+ * e53toe( &d, e ) IEEE double precision to e type\r
+ * e64toe( &d, e ) IEEE long double precision to e type\r
+ * eabs(e) absolute value\r
+ * eadd( a, b, c ) c = b + a\r
+ * eclear(e) e = 0\r
+ * ecmp (a, b) Returns 1 if a > b, 0 if a == b,\r
+ * -1 if a < b, -2 if either a or b is a NaN.\r
+ * ediv( a, b, c ) c = b / a\r
+ * efloor( a, b ) truncate to integer, toward -infinity\r
+ * efrexp( a, exp, s ) extract exponent and significand\r
+ * eifrac( e, &l, frac ) e to long integer and e type fraction\r
+ * euifrac( e, &l, frac ) e to unsigned long integer and e type fraction\r
+ * einfin( e ) set e to infinity, leaving its sign alone\r
+ * eldexp( a, n, b ) multiply by 2**n\r
+ * emov( a, b ) b = a\r
+ * emul( a, b, c ) c = b * a\r
+ * eneg(e) e = -e\r
+ * eround( a, b ) b = nearest integer value to a\r
+ * esub( a, b, c ) c = b - a\r
+ * e24toasc( &f, str, n ) single to ASCII string, n digits after decimal\r
+ * e53toasc( &d, str, n ) double to ASCII string, n digits after decimal\r
+ * e64toasc( &d, str, n ) long double to ASCII string\r
+ * etoasc( e, str, n ) e to ASCII string, n digits after decimal\r
+ * etoe24( e, &f ) convert e type to IEEE single precision\r
+ * etoe53( e, &d ) convert e type to IEEE double precision\r
+ * etoe64( e, &d ) convert e type to IEEE long double precision\r
+ * ltoe( &l, e ) long (32 bit) integer to e type\r
+ * ultoe( &l, e ) unsigned long (32 bit) integer to e type\r
+ * eisneg( e ) 1 if sign bit of e != 0, else 0\r
+ * eisinf( e ) 1 if e has maximum exponent (non-IEEE)\r
+ * or is infinite (IEEE)\r
+ * eisnan( e ) 1 if e is a NaN\r
+ * esqrt( a, b ) b = square root of a\r
+ *\r
+ *\r
+ * Routines for internal format numbers\r
+ *\r
+ * eaddm( ai, bi ) add significands, bi = bi + ai\r
+ * ecleaz(ei) ei = 0\r
+ * ecleazs(ei) set ei = 0 but leave its sign alone\r
+ * ecmpm( ai, bi ) compare significands, return 1, 0, or -1\r
+ * edivm( ai, bi ) divide significands, bi = bi / ai\r
+ * emdnorm(ai,l,s,exp) normalize and round off\r
+ * emovi( a, ai ) convert external a to internal ai\r
+ * emovo( ai, a ) convert internal ai to external a\r
+ * emovz( ai, bi ) bi = ai, low guard word of bi = 0\r
+ * emulm( ai, bi ) multiply significands, bi = bi * ai\r
+ * enormlz(ei) left-justify the significand\r
+ * eshdn1( ai ) shift significand and guards down 1 bit\r
+ * eshdn8( ai ) shift down 8 bits\r
+ * eshdn6( ai ) shift down 16 bits\r
+ * eshift( ai, n ) shift ai n bits up (or down if n < 0)\r
+ * eshup1( ai ) shift significand and guards up 1 bit\r
+ * eshup8( ai ) shift up 8 bits\r
+ * eshup6( ai ) shift up 16 bits\r
+ * esubm( ai, bi ) subtract significands, bi = bi - ai\r
+ *\r
+ *\r
+ * The result is always normalized and rounded to NI-4 word precision\r
+ * after each arithmetic operation.\r
+ *\r
+ * Exception flags are NOT fully supported.\r
+ *\r
+ * Define INFINITY in mconf.h for support of infinity; otherwise a\r
+ * saturation arithmetic is implemented.\r
+ *\r
+ * Define NANS for support of Not-a-Number items; otherwise the\r
+ * arithmetic will never produce a NaN output, and might be confused\r
+ * by a NaN input.\r
+ * If NaN's are supported, the output of ecmp(a,b) is -2 if\r
+ * either a or b is a NaN. This means asking if(ecmp(a,b) < 0)\r
+ * may not be legitimate. Use if(ecmp(a,b) == -1) for less-than\r
+ * if in doubt.\r
+ * Signaling NaN's are NOT supported; they are treated the same\r
+ * as quiet NaN's.\r
+ *\r
+ * Denormals are always supported here where appropriate (e.g., not\r
+ * for conversion to DEC numbers).\r
+ */\r
+\r
+/*\r
+ * Revision history:\r
+ *\r
+ * 5 Jan 84 PDP-11 assembly language version\r
+ * 2 Mar 86 fixed bug in asctoq()\r
+ * 6 Dec 86 C language version\r
+ * 30 Aug 88 100 digit version, improved rounding\r
+ * 15 May 92 80-bit long double support\r
+ *\r
+ * Author: S. L. Moshier.\r
+ */\r
+\r
+#include <stdio.h>\r
+/* #include "\usr\include\stdio.h" */\r
+#include "ehead.h"\r
+#include "mconf.h"\r
+\r
+/* Change UNK into something else. */\r
+#ifdef UNK\r
+#undef UNK\r
+#define IBMPC 1\r
+#endif\r
+\r
+/* NaN's require infinity support. */\r
+#ifdef NANS\r
+#ifndef INFINITY\r
+#define INFINITY\r
+#endif\r
+#endif\r
+\r
+/* This handles 64-bit long ints. */\r
+#define LONGBITS (8 * sizeof(long))\r
+\r
+/* Control register for rounding precision.\r
+ * This can be set to 80 (if NE=6), 64, 56, 53, or 24 bits.\r
+ */\r
+int rndprc = NBITS;\r
+extern int rndprc;\r
+\r
+void eaddm(), esubm(), emdnorm(), asctoeg(), enan();\r
+static void toe24(), toe53(), toe64(), toe113();\r
+void eremain(), einit(), eiremain();\r
+int ecmpm(), edivm(), emulm(), eisneg(), eisinf();\r
+void emovi(), emovo(), emovz(), ecleaz(), eadd1();\r
+void etodec(), todec(), dectoe();\r
+int eisnan(), eiisnan();\r
+\r
+\r
+\r
+void einit()\r
+{\r
+}\r
+\r
+/*\r
+; Clear out entire external format number.\r
+;\r
+; unsigned short x[];\r
+; eclear( x );\r
+*/\r
+\r
+void eclear( x )\r
+register unsigned short *x;\r
+{\r
+register int i;\r
+\r
+for( i=0; i<NE; i++ )\r
+ *x++ = 0;\r
+}\r
+\r
+\r
+\r
+/* Move external format number from a to b.\r
+ *\r
+ * emov( a, b );\r
+ */\r
+\r
+void emov( a, b )\r
+register unsigned short *a, *b;\r
+{\r
+register int i;\r
+\r
+for( i=0; i<NE; i++ )\r
+ *b++ = *a++;\r
+}\r
+\r
+\r
+/*\r
+; Absolute value of external format number\r
+;\r
+; short x[NE];\r
+; eabs( x );\r
+*/\r
+\r
+void eabs(x)\r
+unsigned short x[]; /* x is the memory address of a short */\r
+{\r
+\r
+x[NE-1] &= 0x7fff; /* sign is top bit of last word of external format */\r
+}\r
+\r
+\r
+\r
+\r
+/*\r
+; Negate external format number\r
+;\r
+; unsigned short x[NE];\r
+; eneg( x );\r
+*/\r
+\r
+void eneg(x)\r
+unsigned short x[];\r
+{\r
+\r
+#ifdef NANS\r
+if( eisnan(x) )\r
+ return;\r
+#endif\r
+x[NE-1] ^= 0x8000; /* Toggle the sign bit */\r
+}\r
+\r
+\r
+\r
+/* Return 1 if external format number is negative,\r
+ * else return zero.\r
+ */\r
+int eisneg(x)\r
+unsigned short x[];\r
+{\r
+\r
+#ifdef NANS\r
+if( eisnan(x) )\r
+ return( 0 );\r
+#endif\r
+if( x[NE-1] & 0x8000 )\r
+ return( 1 );\r
+else\r
+ return( 0 );\r
+}\r
+\r
+\r
+/* Return 1 if external format number has maximum possible exponent,\r
+ * else return zero.\r
+ */\r
+int eisinf(x)\r
+unsigned short x[];\r
+{\r
+\r
+if( (x[NE-1] & 0x7fff) == 0x7fff )\r
+ {\r
+#ifdef NANS\r
+ if( eisnan(x) )\r
+ return( 0 );\r
+#endif\r
+ return( 1 );\r
+ }\r
+else\r
+ return( 0 );\r
+}\r
+\r
+/* Check if e-type number is not a number.\r
+ */\r
+int eisnan(x)\r
+unsigned short x[];\r
+{\r
+\r
+#ifdef NANS\r
+int i;\r
+/* NaN has maximum exponent */\r
+if( (x[NE-1] & 0x7fff) != 0x7fff )\r
+ return (0);\r
+/* ... and non-zero significand field. */\r
+for( i=0; i<NE-1; i++ )\r
+ {\r
+ if( *x++ != 0 )\r
+ return (1);\r
+ }\r
+#endif\r
+return (0);\r
+}\r
+\r
+/*\r
+; Fill entire number, including exponent and significand, with\r
+; largest possible number. These programs implement a saturation\r
+; value that is an ordinary, legal number. A special value\r
+; "infinity" may also be implemented; this would require tests\r
+; for that value and implementation of special rules for arithmetic\r
+; operations involving inifinity.\r
+*/\r
+\r
+void einfin(x)\r
+register unsigned short *x;\r
+{\r
+register int i;\r
+\r
+#ifdef INFINITY\r
+for( i=0; i<NE-1; i++ )\r
+ *x++ = 0;\r
+*x |= 32767;\r
+#else\r
+for( i=0; i<NE-1; i++ )\r
+ *x++ = 0xffff;\r
+*x |= 32766;\r
+if( rndprc < NBITS )\r
+ {\r
+ if (rndprc == 113)\r
+ {\r
+ *(x - 9) = 0;\r
+ *(x - 8) = 0;\r
+ }\r
+ if( rndprc == 64 )\r
+ {\r
+ *(x-5) = 0;\r
+ }\r
+ if( rndprc == 53 )\r
+ {\r
+ *(x-4) = 0xf800;\r
+ }\r
+ else\r
+ {\r
+ *(x-4) = 0;\r
+ *(x-3) = 0;\r
+ *(x-2) = 0xff00;\r
+ }\r
+ }\r
+#endif\r
+}\r
+\r
+\r
+\r
+/* Move in external format number,\r
+ * converting it to internal format.\r
+ */\r
+void emovi( a, b )\r
+unsigned short *a, *b;\r
+{\r
+register unsigned short *p, *q;\r
+int i;\r
+\r
+q = b;\r
+p = a + (NE-1); /* point to last word of external number */\r
+/* get the sign bit */\r
+if( *p & 0x8000 )\r
+ *q++ = 0xffff;\r
+else\r
+ *q++ = 0;\r
+/* get the exponent */\r
+*q = *p--;\r
+*q++ &= 0x7fff; /* delete the sign bit */\r
+#ifdef INFINITY\r
+if( (*(q-1) & 0x7fff) == 0x7fff )\r
+ {\r
+#ifdef NANS\r
+ if( eisnan(a) )\r
+ {\r
+ *q++ = 0;\r
+ for( i=3; i<NI; i++ )\r
+ *q++ = *p--;\r
+ return;\r
+ }\r
+#endif\r
+ for( i=2; i<NI; i++ )\r
+ *q++ = 0;\r
+ return;\r
+ }\r
+#endif\r
+/* clear high guard word */\r
+*q++ = 0;\r
+/* move in the significand */\r
+for( i=0; i<NE-1; i++ )\r
+ *q++ = *p--;\r
+/* clear low guard word */\r
+*q = 0;\r
+}\r
+\r
+\r
+/* Move internal format number out,\r
+ * converting it to external format.\r
+ */\r
+void emovo( a, b )\r
+unsigned short *a, *b;\r
+{\r
+register unsigned short *p, *q;\r
+unsigned short i;\r
+\r
+p = a;\r
+q = b + (NE-1); /* point to output exponent */\r
+/* combine sign and exponent */\r
+i = *p++;\r
+if( i )\r
+ *q-- = *p++ | 0x8000;\r
+else\r
+ *q-- = *p++;\r
+#ifdef INFINITY\r
+if( *(p-1) == 0x7fff )\r
+ {\r
+#ifdef NANS\r
+ if( eiisnan(a) )\r
+ {\r
+ enan( b, NBITS );\r
+ return;\r
+ }\r
+#endif\r
+ einfin(b);\r
+ return;\r
+ }\r
+#endif\r
+/* skip over guard word */\r
+++p;\r
+/* move the significand */\r
+for( i=0; i<NE-1; i++ )\r
+ *q-- = *p++;\r
+}\r
+\r
+\r
+\r
+\r
+/* Clear out internal format number.\r
+ */\r
+\r
+void ecleaz( xi )\r
+register unsigned short *xi;\r
+{\r
+register int i;\r
+\r
+for( i=0; i<NI; i++ )\r
+ *xi++ = 0;\r
+}\r
+\r
+/* same, but don't touch the sign. */\r
+\r
+void ecleazs( xi )\r
+register unsigned short *xi;\r
+{\r
+register int i;\r
+\r
+++xi;\r
+for(i=0; i<NI-1; i++)\r
+ *xi++ = 0;\r
+}\r
+\r
+\r
+\r
+\r
+/* Move internal format number from a to b.\r
+ */\r
+void emovz( a, b )\r
+register unsigned short *a, *b;\r
+{\r
+register int i;\r
+\r
+for( i=0; i<NI-1; i++ )\r
+ *b++ = *a++;\r
+/* clear low guard word */\r
+*b = 0;\r
+}\r
+\r
+/* Return nonzero if internal format number is a NaN.\r
+ */\r
+\r
+int eiisnan (x)\r
+unsigned short x[];\r
+{\r
+int i;\r
+\r
+if( (x[E] & 0x7fff) == 0x7fff )\r
+ {\r
+ for( i=M+1; i<NI; i++ )\r
+ {\r
+ if( x[i] != 0 )\r
+ return(1);\r
+ }\r
+ }\r
+return(0);\r
+}\r
+\r
+#ifdef INFINITY\r
+/* Return nonzero if internal format number is infinite. */\r
+\r
+static int \r
+eiisinf (x)\r
+ unsigned short x[];\r
+{\r
+\r
+#ifdef NANS\r
+ if (eiisnan (x))\r
+ return (0);\r
+#endif\r
+ if ((x[E] & 0x7fff) == 0x7fff)\r
+ return (1);\r
+ return (0);\r
+}\r
+#endif\r
+\r
+/*\r
+; Compare significands of numbers in internal format.\r
+; Guard words are included in the comparison.\r
+;\r
+; unsigned short a[NI], b[NI];\r
+; cmpm( a, b );\r
+;\r
+; for the significands:\r
+; returns +1 if a > b\r
+; 0 if a == b\r
+; -1 if a < b\r
+*/\r
+int ecmpm( a, b )\r
+register unsigned short *a, *b;\r
+{\r
+int i;\r
+\r
+a += M; /* skip up to significand area */\r
+b += M;\r
+for( i=M; i<NI; i++ )\r
+ {\r
+ if( *a++ != *b++ )\r
+ goto difrnt;\r
+ }\r
+return(0);\r
+\r
+difrnt:\r
+if( *(--a) > *(--b) )\r
+ return(1);\r
+else\r
+ return(-1);\r
+}\r
+\r
+\r
+/*\r
+; Shift significand down by 1 bit\r
+*/\r
+\r
+void eshdn1(x)\r
+register unsigned short *x;\r
+{\r
+register unsigned short bits;\r
+int i;\r
+\r
+x += M; /* point to significand area */\r
+\r
+bits = 0;\r
+for( i=M; i<NI; i++ )\r
+ {\r
+ if( *x & 1 )\r
+ bits |= 1;\r
+ *x >>= 1;\r
+ if( bits & 2 )\r
+ *x |= 0x8000;\r
+ bits <<= 1;\r
+ ++x;\r
+ } \r
+}\r
+\r
+\r
+\r
+/*\r
+; Shift significand up by 1 bit\r
+*/\r
+\r
+void eshup1(x)\r
+register unsigned short *x;\r
+{\r
+register unsigned short bits;\r
+int i;\r
+\r
+x += NI-1;\r
+bits = 0;\r
+\r
+for( i=M; i<NI; i++ )\r
+ {\r
+ if( *x & 0x8000 )\r
+ bits |= 1;\r
+ *x <<= 1;\r
+ if( bits & 2 )\r
+ *x |= 1;\r
+ bits <<= 1;\r
+ --x;\r
+ }\r
+}\r
+\r
+\r
+\r
+/*\r
+; Shift significand down by 8 bits\r
+*/\r
+\r
+void eshdn8(x)\r
+register unsigned short *x;\r
+{\r
+register unsigned short newbyt, oldbyt;\r
+int i;\r
+\r
+x += M;\r
+oldbyt = 0;\r
+for( i=M; i<NI; i++ )\r
+ {\r
+ newbyt = *x << 8;\r
+ *x >>= 8;\r
+ *x |= oldbyt;\r
+ oldbyt = newbyt;\r
+ ++x;\r
+ }\r
+}\r
+\r
+/*\r
+; Shift significand up by 8 bits\r
+*/\r
+\r
+void eshup8(x)\r
+register unsigned short *x;\r
+{\r
+int i;\r
+register unsigned short newbyt, oldbyt;\r
+\r
+x += NI-1;\r
+oldbyt = 0;\r
+\r
+for( i=M; i<NI; i++ )\r
+ {\r
+ newbyt = *x >> 8;\r
+ *x <<= 8;\r
+ *x |= oldbyt;\r
+ oldbyt = newbyt;\r
+ --x;\r
+ }\r
+}\r
+\r
+/*\r
+; Shift significand up by 16 bits\r
+*/\r
+\r
+void eshup6(x)\r
+register unsigned short *x;\r
+{\r
+int i;\r
+register unsigned short *p;\r
+\r
+p = x + M;\r
+x += M + 1;\r
+\r
+for( i=M; i<NI-1; i++ )\r
+ *p++ = *x++;\r
+\r
+*p = 0;\r
+}\r
+\r
+/*\r
+; Shift significand down by 16 bits\r
+*/\r
+\r
+void eshdn6(x)\r
+register unsigned short *x;\r
+{\r
+int i;\r
+register unsigned short *p;\r
+\r
+x += NI-1;\r
+p = x + 1;\r
+\r
+for( i=M; i<NI-1; i++ )\r
+ *(--p) = *(--x);\r
+\r
+*(--p) = 0;\r
+}\r
+\f\r
+/*\r
+; Add significands\r
+; x + y replaces y\r
+*/\r
+\r
+void eaddm( x, y )\r
+unsigned short *x, *y;\r
+{\r
+register unsigned long a;\r
+int i;\r
+unsigned int carry;\r
+\r
+x += NI-1;\r
+y += NI-1;\r
+carry = 0;\r
+for( i=M; i<NI; i++ )\r
+ {\r
+ a = (unsigned long )(*x) + (unsigned long )(*y) + carry;\r
+ if( a & 0x10000 )\r
+ carry = 1;\r
+ else\r
+ carry = 0;\r
+ *y = (unsigned short )a;\r
+ --x;\r
+ --y;\r
+ }\r
+}\r
+\r
+/*\r
+; Subtract significands\r
+; y - x replaces y\r
+*/\r
+\r
+void esubm( x, y )\r
+unsigned short *x, *y;\r
+{\r
+unsigned long a;\r
+int i;\r
+unsigned int carry;\r
+\r
+x += NI-1;\r
+y += NI-1;\r
+carry = 0;\r
+for( i=M; i<NI; i++ )\r
+ {\r
+ a = (unsigned long )(*y) - (unsigned long )(*x) - carry;\r
+ if( a & 0x10000 )\r
+ carry = 1;\r
+ else\r
+ carry = 0;\r
+ *y = (unsigned short )a;\r
+ --x;\r
+ --y;\r
+ }\r
+}\r
+\r
+\r
+/* Divide significands */\r
+\r
+static unsigned short equot[NI] = {0}; /* was static */\r
+\r
+#if 0\r
+int edivm( den, num )\r
+unsigned short den[], num[];\r
+{\r
+int i;\r
+register unsigned short *p, *q;\r
+unsigned short j;\r
+\r
+p = &equot[0];\r
+*p++ = num[0];\r
+*p++ = num[1];\r
+\r
+for( i=M; i<NI; i++ )\r
+ {\r
+ *p++ = 0;\r
+ }\r
+\r
+/* Use faster compare and subtraction if denominator\r
+ * has only 15 bits of significance.\r
+ */\r
+p = &den[M+2];\r
+if( *p++ == 0 )\r
+ {\r
+ for( i=M+3; i<NI; i++ )\r
+ {\r
+ if( *p++ != 0 )\r
+ goto fulldiv;\r
+ }\r
+ if( (den[M+1] & 1) != 0 )\r
+ goto fulldiv;\r
+ eshdn1(num);\r
+ eshdn1(den);\r
+\r
+ p = &den[M+1];\r
+ q = &num[M+1];\r
+\r
+ for( i=0; i<NBITS+2; i++ )\r
+ {\r
+ if( *p <= *q )\r
+ {\r
+ *q -= *p;\r
+ j = 1;\r
+ }\r
+ else\r
+ {\r
+ j = 0;\r
+ }\r
+ eshup1(equot);\r
+ equot[NI-2] |= j;\r
+ eshup1(num);\r
+ }\r
+ goto divdon;\r
+ }\r
+\r
+/* The number of quotient bits to calculate is\r
+ * NBITS + 1 scaling guard bit + 1 roundoff bit.\r
+ */\r
+fulldiv:\r
+\r
+p = &equot[NI-2];\r
+for( i=0; i<NBITS+2; i++ )\r
+ {\r
+ if( ecmpm(den,num) <= 0 )\r
+ {\r
+ esubm(den, num);\r
+ j = 1; /* quotient bit = 1 */\r
+ }\r
+ else\r
+ j = 0;\r
+ eshup1(equot);\r
+ *p |= j;\r
+ eshup1(num);\r
+ }\r
+\r
+divdon:\r
+\r
+eshdn1( equot );\r
+eshdn1( equot );\r
+\r
+/* test for nonzero remainder after roundoff bit */\r
+p = &num[M];\r
+j = 0;\r
+for( i=M; i<NI; i++ )\r
+ {\r
+ j |= *p++;\r
+ }\r
+if( j )\r
+ j = 1;\r
+\r
+\r
+for( i=0; i<NI; i++ )\r
+ num[i] = equot[i];\r
+return( (int )j );\r
+}\r
+\r
+/* Multiply significands */\r
+int emulm( a, b )\r
+unsigned short a[], b[];\r
+{\r
+unsigned short *p, *q;\r
+int i, j, k;\r
+\r
+equot[0] = b[0];\r
+equot[1] = b[1];\r
+for( i=M; i<NI; i++ )\r
+ equot[i] = 0;\r
+\r
+p = &a[NI-2];\r
+k = NBITS;\r
+while( *p == 0 ) /* significand is not supposed to be all zero */\r
+ {\r
+ eshdn6(a);\r
+ k -= 16;\r
+ }\r
+if( (*p & 0xff) == 0 )\r
+ {\r
+ eshdn8(a);\r
+ k -= 8;\r
+ }\r
+\r
+q = &equot[NI-1];\r
+j = 0;\r
+for( i=0; i<k; i++ )\r
+ {\r
+ if( *p & 1 )\r
+ eaddm(b, equot);\r
+/* remember if there were any nonzero bits shifted out */\r
+ if( *q & 1 )\r
+ j |= 1;\r
+ eshdn1(a);\r
+ eshdn1(equot);\r
+ }\r
+\r
+for( i=0; i<NI; i++ )\r
+ b[i] = equot[i];\r
+\r
+/* return flag for lost nonzero bits */\r
+return(j);\r
+}\r
+\r
+#else\r
+\r
+/* Multiply significand of e-type number b\r
+by 16-bit quantity a, e-type result to c. */\r
+\r
+void m16m( a, b, c )\r
+unsigned short a;\r
+unsigned short b[], c[];\r
+{\r
+register unsigned short *pp;\r
+register unsigned long carry;\r
+unsigned short *ps;\r
+unsigned short p[NI];\r
+unsigned long aa, m;\r
+int i;\r
+\r
+aa = a;\r
+pp = &p[NI-2];\r
+*pp++ = 0;\r
+*pp = 0;\r
+ps = &b[NI-1];\r
+\r
+for( i=M+1; i<NI; i++ )\r
+ {\r
+ if( *ps == 0 )\r
+ {\r
+ --ps;\r
+ --pp;\r
+ *(pp-1) = 0;\r
+ }\r
+ else\r
+ {\r
+ m = (unsigned long) aa * *ps--;\r
+ carry = (m & 0xffff) + *pp;\r
+ *pp-- = (unsigned short )carry;\r
+ carry = (carry >> 16) + (m >> 16) + *pp;\r
+ *pp = (unsigned short )carry;\r
+ *(pp-1) = carry >> 16;\r
+ }\r
+ }\r
+for( i=M; i<NI; i++ )\r
+ c[i] = p[i];\r
+}\r
+\r
+\r
+/* Divide significands. Neither the numerator nor the denominator\r
+is permitted to have its high guard word nonzero. */\r
+\r
+\r
+int edivm( den, num )\r
+unsigned short den[], num[];\r
+{\r
+int i;\r
+register unsigned short *p;\r
+unsigned long tnum;\r
+unsigned short j, tdenm, tquot;\r
+unsigned short tprod[NI+1];\r
+\r
+p = &equot[0];\r
+*p++ = num[0];\r
+*p++ = num[1];\r
+\r
+for( i=M; i<NI; i++ )\r
+ {\r
+ *p++ = 0;\r
+ }\r
+eshdn1( num );\r
+tdenm = den[M+1];\r
+for( i=M; i<NI; i++ )\r
+ {\r
+ /* Find trial quotient digit (the radix is 65536). */\r
+ tnum = (((unsigned long) num[M]) << 16) + num[M+1];\r
+\r
+ /* Do not execute the divide instruction if it will overflow. */\r
+ if( (tdenm * 0xffffL) < tnum )\r
+ tquot = 0xffff;\r
+ else\r
+ tquot = tnum / tdenm;\r
+\r
+ /* Prove that the divide worked. */\r
+/*\r
+ tcheck = (unsigned long )tquot * tdenm;\r
+ if( tnum - tcheck > tdenm )\r
+ tquot = 0xffff;\r
+*/\r
+ /* Multiply denominator by trial quotient digit. */\r
+ m16m( tquot, den, tprod );\r
+ /* The quotient digit may have been overestimated. */\r
+ if( ecmpm( tprod, num ) > 0 )\r
+ {\r
+ tquot -= 1;\r
+ esubm( den, tprod );\r
+ if( ecmpm( tprod, num ) > 0 )\r
+ {\r
+ tquot -= 1;\r
+ esubm( den, tprod );\r
+ }\r
+ }\r
+/*\r
+ if( ecmpm( tprod, num ) > 0 )\r
+ {\r
+ eshow( "tprod", tprod );\r
+ eshow( "num ", num );\r
+ printf( "tnum = %08lx, tden = %04x, tquot = %04x\n",\r
+ tnum, den[M+1], tquot );\r
+ }\r
+*/\r
+ esubm( tprod, num );\r
+/*\r
+ if( ecmpm( num, den ) >= 0 )\r
+ {\r
+ eshow( "num ", num );\r
+ eshow( "den ", den );\r
+ printf( "tnum = %08lx, tden = %04x, tquot = %04x\n",\r
+ tnum, den[M+1], tquot );\r
+ }\r
+*/\r
+ equot[i] = tquot;\r
+ eshup6(num);\r
+ }\r
+/* test for nonzero remainder after roundoff bit */\r
+p = &num[M];\r
+j = 0;\r
+for( i=M; i<NI; i++ )\r
+ {\r
+ j |= *p++;\r
+ }\r
+if( j )\r
+ j = 1;\r
+\r
+for( i=0; i<NI; i++ )\r
+ num[i] = equot[i];\r
+\r
+return( (int )j );\r
+}\r
+\r
+\r
+\r
+/* Multiply significands */\r
+int emulm( a, b )\r
+unsigned short a[], b[];\r
+{\r
+unsigned short *p, *q;\r
+unsigned short pprod[NI];\r
+unsigned short j;\r
+int i;\r
+\r
+equot[0] = b[0];\r
+equot[1] = b[1];\r
+for( i=M; i<NI; i++ )\r
+ equot[i] = 0;\r
+\r
+j = 0;\r
+p = &a[NI-1];\r
+q = &equot[NI-1];\r
+for( i=M+1; i<NI; i++ )\r
+ {\r
+ if( *p == 0 )\r
+ {\r
+ --p;\r
+ }\r
+ else\r
+ {\r
+ m16m( *p--, b, pprod );\r
+ eaddm(pprod, equot);\r
+ }\r
+ j |= *q;\r
+ eshdn6(equot);\r
+ }\r
+\r
+for( i=0; i<NI; i++ )\r
+ b[i] = equot[i];\r
+\r
+/* return flag for lost nonzero bits */\r
+return( (int)j );\r
+}\r
+\r
+\r
+/*\r
+eshow(str, x)\r
+char *str;\r
+unsigned short *x;\r
+{\r
+int i;\r
+\r
+printf( "%s ", str );\r
+for( i=0; i<NI; i++ )\r
+ printf( "%04x ", *x++ );\r
+printf( "\n" );\r
+}\r
+*/\r
+#endif\r
+\r
+\r
+\r
+/*\r
+ * Normalize and round off.\r
+ *\r
+ * The internal format number to be rounded is "s".\r
+ * Input "lost" indicates whether the number is exact.\r
+ * This is the so-called sticky bit.\r
+ *\r
+ * Input "subflg" indicates whether the number was obtained\r
+ * by a subtraction operation. In that case if lost is nonzero\r
+ * then the number is slightly smaller than indicated.\r
+ *\r
+ * Input "exp" is the biased exponent, which may be negative.\r
+ * the exponent field of "s" is ignored but is replaced by\r
+ * "exp" as adjusted by normalization and rounding.\r
+ *\r
+ * Input "rcntrl" is the rounding control.\r
+ */\r
+\r
+static int rlast = -1;\r
+static int rw = 0;\r
+static unsigned short rmsk = 0;\r
+static unsigned short rmbit = 0;\r
+static unsigned short rebit = 0;\r
+static int re = 0;\r
+static unsigned short rbit[NI] = {0,0,0,0,0,0,0,0};\r
+\r
+void emdnorm( s, lost, subflg, exp, rcntrl )\r
+unsigned short s[];\r
+int lost;\r
+int subflg;\r
+long exp;\r
+int rcntrl;\r
+{\r
+int i, j;\r
+unsigned short r;\r
+\r
+/* Normalize */\r
+j = enormlz( s );\r
+\r
+/* a blank significand could mean either zero or infinity. */\r
+#ifndef INFINITY\r
+if( j > NBITS )\r
+ {\r
+ ecleazs( s );\r
+ return;\r
+ }\r
+#endif\r
+exp -= j;\r
+#ifndef INFINITY\r
+if( exp >= 32767L )\r
+ goto overf;\r
+#else\r
+if( (j > NBITS) && (exp < 32767L) )\r
+ {\r
+ ecleazs( s );\r
+ return;\r
+ }\r
+#endif\r
+if( exp < 0L )\r
+ {\r
+ if( exp > (long )(-NBITS-1) )\r
+ {\r
+ j = (int )exp;\r
+ i = eshift( s, j );\r
+ if( i )\r
+ lost = 1;\r
+ }\r
+ else\r
+ {\r
+ ecleazs( s );\r
+ return;\r
+ }\r
+ }\r
+/* Round off, unless told not to by rcntrl. */\r
+if( rcntrl == 0 )\r
+ goto mdfin;\r
+/* Set up rounding parameters if the control register changed. */\r
+if( rndprc != rlast )\r
+ {\r
+ ecleaz( rbit );\r
+ switch( rndprc )\r
+ {\r
+ default:\r
+ case NBITS:\r
+ rw = NI-1; /* low guard word */\r
+ rmsk = 0xffff;\r
+ rmbit = 0x8000;\r
+ rebit = 1;\r
+ re = rw - 1;\r
+ break;\r
+ case 113:\r
+ rw = 10;\r
+ rmsk = 0x7fff;\r
+ rmbit = 0x4000;\r
+ rebit = 0x8000;\r
+ re = rw;\r
+ break;\r
+ case 64:\r
+ rw = 7;\r
+ rmsk = 0xffff;\r
+ rmbit = 0x8000;\r
+ rebit = 1;\r
+ re = rw-1;\r
+ break;\r
+/* For DEC arithmetic */\r
+ case 56:\r
+ rw = 6;\r
+ rmsk = 0xff;\r
+ rmbit = 0x80;\r
+ rebit = 0x100;\r
+ re = rw;\r
+ break;\r
+ case 53:\r
+ rw = 6;\r
+ rmsk = 0x7ff;\r
+ rmbit = 0x0400;\r
+ rebit = 0x800;\r
+ re = rw;\r
+ break;\r
+ case 24:\r
+ rw = 4;\r
+ rmsk = 0xff;\r
+ rmbit = 0x80;\r
+ rebit = 0x100;\r
+ re = rw;\r
+ break;\r
+ }\r
+ rbit[re] = rebit;\r
+ rlast = rndprc;\r
+ }\r
+\r
+/* Shift down 1 temporarily if the data structure has an implied\r
+ * most significant bit and the number is denormal.\r
+ * For rndprc = 64 or NBITS, there is no implied bit.\r
+ * But Intel long double denormals lose one bit of significance even so.\r
+ */\r
+#if IBMPC\r
+if( (exp <= 0) && (rndprc != NBITS) )\r
+#else\r
+if( (exp <= 0) && (rndprc != 64) && (rndprc != NBITS) )\r
+#endif\r
+ {\r
+ lost |= s[NI-1] & 1;\r
+ eshdn1(s);\r
+ }\r
+/* Clear out all bits below the rounding bit,\r
+ * remembering in r if any were nonzero.\r
+ */\r
+r = s[rw] & rmsk;\r
+if( rndprc < NBITS )\r
+ {\r
+ i = rw + 1;\r
+ while( i < NI )\r
+ {\r
+ if( s[i] )\r
+ r |= 1;\r
+ s[i] = 0;\r
+ ++i;\r
+ }\r
+ }\r
+s[rw] &= ~rmsk;\r
+if( (r & rmbit) != 0 )\r
+ {\r
+ if( r == rmbit )\r
+ {\r
+ if( lost == 0 )\r
+ { /* round to even */\r
+ if( (s[re] & rebit) == 0 )\r
+ goto mddone;\r
+ }\r
+ else\r
+ {\r
+ if( subflg != 0 )\r
+ goto mddone;\r
+ }\r
+ }\r
+ eaddm( rbit, s );\r
+ }\r
+mddone:\r
+#if IBMPC\r
+if( (exp <= 0) && (rndprc != NBITS) )\r
+#else\r
+if( (exp <= 0) && (rndprc != 64) && (rndprc != NBITS) )\r
+#endif\r
+ {\r
+ eshup1(s);\r
+ }\r
+if( s[2] != 0 )\r
+ { /* overflow on roundoff */\r
+ eshdn1(s);\r
+ exp += 1;\r
+ }\r
+mdfin:\r
+s[NI-1] = 0;\r
+if( exp >= 32767L )\r
+ {\r
+#ifndef INFINITY\r
+overf:\r
+#endif\r
+#ifdef INFINITY\r
+ s[1] = 32767;\r
+ for( i=2; i<NI-1; i++ )\r
+ s[i] = 0;\r
+#else\r
+ s[1] = 32766;\r
+ s[2] = 0;\r
+ for( i=M+1; i<NI-1; i++ )\r
+ s[i] = 0xffff;\r
+ s[NI-1] = 0;\r
+ if( (rndprc < 64) || (rndprc == 113) )\r
+ {\r
+ s[rw] &= ~rmsk;\r
+ if( rndprc == 24 )\r
+ {\r
+ s[5] = 0;\r
+ s[6] = 0;\r
+ }\r
+ }\r
+#endif\r
+ return;\r
+ }\r
+if( exp < 0 )\r
+ s[1] = 0;\r
+else\r
+ s[1] = (unsigned short )exp;\r
+}\r
+\r
+\r
+\r
+/*\r
+; Subtract external format numbers.\r
+;\r
+; unsigned short a[NE], b[NE], c[NE];\r
+; esub( a, b, c ); c = b - a\r
+*/\r
+\r
+static int subflg = 0;\r
+\r
+void esub( a, b, c )\r
+unsigned short *a, *b, *c;\r
+{\r
+\r
+#ifdef NANS\r
+if( eisnan(a) )\r
+ {\r
+ emov (a, c);\r
+ return;\r
+ }\r
+if( eisnan(b) )\r
+ {\r
+ emov(b,c);\r
+ return;\r
+ }\r
+/* Infinity minus infinity is a NaN.\r
+ * Test for subtracting infinities of the same sign.\r
+ */\r
+if( eisinf(a) && eisinf(b) && ((eisneg (a) ^ eisneg (b)) == 0))\r
+ {\r
+ mtherr( "esub", DOMAIN );\r
+ enan( c, NBITS );\r
+ return;\r
+ }\r
+#endif\r
+subflg = 1;\r
+eadd1( a, b, c );\r
+}\r
+\r
+\r
+/*\r
+; Add.\r
+;\r
+; unsigned short a[NE], b[NE], c[NE];\r
+; eadd( a, b, c ); c = b + a\r
+*/\r
+void eadd( a, b, c )\r
+unsigned short *a, *b, *c;\r
+{\r
+\r
+#ifdef NANS\r
+/* NaN plus anything is a NaN. */\r
+if( eisnan(a) )\r
+ {\r
+ emov(a,c);\r
+ return;\r
+ }\r
+if( eisnan(b) )\r
+ {\r
+ emov(b,c);\r
+ return;\r
+ }\r
+/* Infinity minus infinity is a NaN.\r
+ * Test for adding infinities of opposite signs.\r
+ */\r
+if( eisinf(a) && eisinf(b)\r
+ && ((eisneg(a) ^ eisneg(b)) != 0) )\r
+ {\r
+ mtherr( "eadd", DOMAIN );\r
+ enan( c, NBITS );\r
+ return;\r
+ }\r
+#endif\r
+subflg = 0;\r
+eadd1( a, b, c );\r
+}\r
+\r
+void eadd1( a, b, c )\r
+unsigned short *a, *b, *c;\r
+{\r
+unsigned short ai[NI], bi[NI], ci[NI];\r
+int i, lost, j, k;\r
+long lt, lta, ltb;\r
+\r
+#ifdef INFINITY\r
+if( eisinf(a) )\r
+ {\r
+ emov(a,c);\r
+ if( subflg )\r
+ eneg(c);\r
+ return;\r
+ }\r
+if( eisinf(b) )\r
+ {\r
+ emov(b,c);\r
+ return;\r
+ }\r
+#endif\r
+emovi( a, ai );\r
+emovi( b, bi );\r
+if( subflg )\r
+ ai[0] = ~ai[0];\r
+\r
+/* compare exponents */\r
+lta = ai[E];\r
+ltb = bi[E];\r
+lt = lta - ltb;\r
+if( lt > 0L )\r
+ { /* put the larger number in bi */\r
+ emovz( bi, ci );\r
+ emovz( ai, bi );\r
+ emovz( ci, ai );\r
+ ltb = bi[E];\r
+ lt = -lt;\r
+ }\r
+lost = 0;\r
+if( lt != 0L )\r
+ {\r
+ if( lt < (long )(-NBITS-1) )\r
+ goto done; /* answer same as larger addend */\r
+ k = (int )lt;\r
+ lost = eshift( ai, k ); /* shift the smaller number down */\r
+ }\r
+else\r
+ {\r
+/* exponents were the same, so must compare significands */\r
+ i = ecmpm( ai, bi );\r
+ if( i == 0 )\r
+ { /* the numbers are identical in magnitude */\r
+ /* if different signs, result is zero */\r
+ if( ai[0] != bi[0] )\r
+ {\r
+ eclear(c);\r
+ return;\r
+ }\r
+ /* if same sign, result is double */\r
+ /* double denomalized tiny number */\r
+ if( (bi[E] == 0) && ((bi[3] & 0x8000) == 0) )\r
+ {\r
+ eshup1( bi );\r
+ goto done;\r
+ }\r
+ /* add 1 to exponent unless both are zero! */\r
+ for( j=1; j<NI-1; j++ )\r
+ {\r
+ if( bi[j] != 0 )\r
+ {\r
+/* This could overflow, but let emovo take care of that. */\r
+ ltb += 1;\r
+ break;\r
+ }\r
+ }\r
+ bi[E] = (unsigned short )ltb;\r
+ goto done;\r
+ }\r
+ if( i > 0 )\r
+ { /* put the larger number in bi */\r
+ emovz( bi, ci );\r
+ emovz( ai, bi );\r
+ emovz( ci, ai );\r
+ }\r
+ }\r
+if( ai[0] == bi[0] )\r
+ {\r
+ eaddm( ai, bi );\r
+ subflg = 0;\r
+ }\r
+else\r
+ {\r
+ esubm( ai, bi );\r
+ subflg = 1;\r
+ }\r
+emdnorm( bi, lost, subflg, ltb, 64 );\r
+\r
+done:\r
+emovo( bi, c );\r
+}\r
+\r
+\r
+\r
+/*\r
+; Divide.\r
+;\r
+; unsigned short a[NE], b[NE], c[NE];\r
+; ediv( a, b, c ); c = b / a\r
+*/\r
+void ediv( a, b, c )\r
+unsigned short *a, *b, *c;\r
+{\r
+unsigned short ai[NI], bi[NI];\r
+int i;\r
+long lt, lta, ltb;\r
+\r
+#ifdef NANS\r
+/* Return any NaN input. */\r
+if( eisnan(a) )\r
+ {\r
+ emov(a,c);\r
+ return;\r
+ }\r
+if( eisnan(b) )\r
+ {\r
+ emov(b,c);\r
+ return;\r
+ }\r
+/* Zero over zero, or infinity over infinity, is a NaN. */\r
+if( ((ecmp(a,ezero) == 0) && (ecmp(b,ezero) == 0))\r
+ || (eisinf (a) && eisinf (b)) )\r
+ {\r
+ mtherr( "ediv", DOMAIN );\r
+ enan( c, NBITS );\r
+ return;\r
+ }\r
+#endif\r
+/* Infinity over anything else is infinity. */\r
+#ifdef INFINITY\r
+if( eisinf(b) )\r
+ {\r
+ if( eisneg(a) ^ eisneg(b) )\r
+ *(c+(NE-1)) = 0x8000;\r
+ else\r
+ *(c+(NE-1)) = 0;\r
+ einfin(c);\r
+ return;\r
+ }\r
+if( eisinf(a) )\r
+ {\r
+ eclear(c);\r
+ return;\r
+ }\r
+#endif\r
+emovi( a, ai );\r
+emovi( b, bi );\r
+lta = ai[E];\r
+ltb = bi[E];\r
+if( bi[E] == 0 )\r
+ { /* See if numerator is zero. */\r
+ for( i=1; i<NI-1; i++ )\r
+ {\r
+ if( bi[i] != 0 )\r
+ {\r
+ ltb -= enormlz( bi );\r
+ goto dnzro1;\r
+ }\r
+ }\r
+ eclear(c);\r
+ return;\r
+ }\r
+dnzro1:\r
+\r
+if( ai[E] == 0 )\r
+ { /* possible divide by zero */\r
+ for( i=1; i<NI-1; i++ )\r
+ {\r
+ if( ai[i] != 0 )\r
+ {\r
+ lta -= enormlz( ai );\r
+ goto dnzro2;\r
+ }\r
+ }\r
+ if( ai[0] == bi[0] )\r
+ *(c+(NE-1)) = 0;\r
+ else\r
+ *(c+(NE-1)) = 0x8000;\r
+ einfin(c);\r
+ mtherr( "ediv", SING );\r
+ return;\r
+ }\r
+dnzro2:\r
+\r
+i = edivm( ai, bi );\r
+/* calculate exponent */\r
+lt = ltb - lta + EXONE;\r
+emdnorm( bi, i, 0, lt, 64 );\r
+/* set the sign */\r
+if( ai[0] == bi[0] )\r
+ bi[0] = 0;\r
+else\r
+ bi[0] = 0Xffff;\r
+emovo( bi, c );\r
+}\r
+\r
+\r
+\r
+/*\r
+; Multiply.\r
+;\r
+; unsigned short a[NE], b[NE], c[NE];\r
+; emul( a, b, c ); c = b * a\r
+*/\r
+void emul( a, b, c )\r
+unsigned short *a, *b, *c;\r
+{\r
+unsigned short ai[NI], bi[NI];\r
+int i, j;\r
+long lt, lta, ltb;\r
+\r
+#ifdef NANS\r
+/* NaN times anything is the same NaN. */\r
+if( eisnan(a) )\r
+ {\r
+ emov(a,c);\r
+ return;\r
+ }\r
+if( eisnan(b) )\r
+ {\r
+ emov(b,c);\r
+ return;\r
+ }\r
+/* Zero times infinity is a NaN. */\r
+if( (eisinf(a) && (ecmp(b,ezero) == 0))\r
+ || (eisinf(b) && (ecmp(a,ezero) == 0)) )\r
+ {\r
+ mtherr( "emul", DOMAIN );\r
+ enan( c, NBITS );\r
+ return;\r
+ }\r
+#endif\r
+/* Infinity times anything else is infinity. */\r
+#ifdef INFINITY\r
+if( eisinf(a) || eisinf(b) )\r
+ {\r
+ if( eisneg(a) ^ eisneg(b) )\r
+ *(c+(NE-1)) = 0x8000;\r
+ else\r
+ *(c+(NE-1)) = 0;\r
+ einfin(c);\r
+ return;\r
+ }\r
+#endif\r
+emovi( a, ai );\r
+emovi( b, bi );\r
+lta = ai[E];\r
+ltb = bi[E];\r
+if( ai[E] == 0 )\r
+ {\r
+ for( i=1; i<NI-1; i++ )\r
+ {\r
+ if( ai[i] != 0 )\r
+ {\r
+ lta -= enormlz( ai );\r
+ goto mnzer1;\r
+ }\r
+ }\r
+ eclear(c);\r
+ return;\r
+ }\r
+mnzer1:\r
+\r
+if( bi[E] == 0 )\r
+ {\r
+ for( i=1; i<NI-1; i++ )\r
+ {\r
+ if( bi[i] != 0 )\r
+ {\r
+ ltb -= enormlz( bi );\r
+ goto mnzer2;\r
+ }\r
+ }\r
+ eclear(c);\r
+ return;\r
+ }\r
+mnzer2:\r
+\r
+/* Multiply significands */\r
+j = emulm( ai, bi );\r
+/* calculate exponent */\r
+lt = lta + ltb - (EXONE - 1);\r
+emdnorm( bi, j, 0, lt, 64 );\r
+/* calculate sign of product */\r
+if( ai[0] == bi[0] )\r
+ bi[0] = 0;\r
+else\r
+ bi[0] = 0xffff;\r
+emovo( bi, c );\r
+}\r
+\r
+\r
+\r
+\r
+/*\r
+; Convert IEEE double precision to e type\r
+; double d;\r
+; unsigned short x[N+2];\r
+; e53toe( &d, x );\r
+*/\r
+void e53toe( pe, y )\r
+unsigned short *pe, *y;\r
+{\r
+#ifdef DEC\r
+\r
+dectoe( pe, y ); /* see etodec.c */\r
+\r
+#else\r
+\r
+register unsigned short r;\r
+register unsigned short *p, *e;\r
+unsigned short yy[NI];\r
+int denorm, k;\r
+\r
+e = pe;\r
+denorm = 0; /* flag if denormalized number */\r
+ecleaz(yy);\r
+#ifdef IBMPC\r
+e += 3;\r
+#endif\r
+r = *e;\r
+yy[0] = 0;\r
+if( r & 0x8000 )\r
+ yy[0] = 0xffff;\r
+yy[M] = (r & 0x0f) | 0x10;\r
+r &= ~0x800f; /* strip sign and 4 significand bits */\r
+#ifdef INFINITY\r
+if( r == 0x7ff0 )\r
+ {\r
+#ifdef NANS\r
+#ifdef IBMPC\r
+ if( ((pe[3] & 0xf) != 0) || (pe[2] != 0)\r
+ || (pe[1] != 0) || (pe[0] != 0) )\r
+ {\r
+ enan( y, NBITS );\r
+ return;\r
+ }\r
+#else\r
+ if( ((pe[0] & 0xf) != 0) || (pe[1] != 0)\r
+ || (pe[2] != 0) || (pe[3] != 0) )\r
+ {\r
+ enan( y, NBITS );\r
+ return;\r
+ }\r
+#endif\r
+#endif /* NANS */\r
+ eclear( y );\r
+ einfin( y );\r
+ if( yy[0] )\r
+ eneg(y);\r
+ return;\r
+ }\r
+#endif\r
+r >>= 4;\r
+/* If zero exponent, then the significand is denormalized.\r
+ * So, take back the understood high significand bit. */ \r
+if( r == 0 )\r
+ {\r
+ denorm = 1;\r
+ yy[M] &= ~0x10;\r
+ }\r
+r += EXONE - 01777;\r
+yy[E] = r;\r
+p = &yy[M+1];\r
+#ifdef IBMPC\r
+*p++ = *(--e);\r
+*p++ = *(--e);\r
+*p++ = *(--e);\r
+#endif\r
+#ifdef MIEEE\r
+++e;\r
+*p++ = *e++;\r
+*p++ = *e++;\r
+*p++ = *e++;\r
+#endif\r
+(void )eshift( yy, -5 );\r
+if( denorm )\r
+ { /* if zero exponent, then normalize the significand */\r
+ if( (k = enormlz(yy)) > NBITS )\r
+ ecleazs(yy);\r
+ else\r
+ yy[E] -= (unsigned short )(k-1);\r
+ }\r
+emovo( yy, y );\r
+#endif /* not DEC */\r
+}\r
+\r
+void e64toe( pe, y )\r
+unsigned short *pe, *y;\r
+{\r
+unsigned short yy[NI];\r
+unsigned short *p, *q, *e;\r
+int i;\r
+\r
+e = pe;\r
+p = yy;\r
+for( i=0; i<NE-5; i++ )\r
+ *p++ = 0;\r
+#ifdef IBMPC\r
+for( i=0; i<5; i++ )\r
+ *p++ = *e++;\r
+#endif\r
+#ifdef DEC\r
+for( i=0; i<5; i++ )\r
+ *p++ = *e++;\r
+#endif\r
+#ifdef MIEEE\r
+p = &yy[0] + (NE-1);\r
+*p-- = *e++;\r
+++e;\r
+for( i=0; i<4; i++ )\r
+ *p-- = *e++;\r
+#endif\r
+\r
+#ifdef IBMPC\r
+/* For Intel long double, shift denormal significand up 1\r
+ -- but only if the top significand bit is zero. */\r
+if((yy[NE-1] & 0x7fff) == 0 && (yy[NE-2] & 0x8000) == 0)\r
+ {\r
+ unsigned short temp[NI+1];\r
+ emovi(yy, temp);\r
+ eshup1(temp);\r
+ emovo(temp,y);\r
+ return;\r
+ }\r
+#endif\r
+#ifdef INFINITY\r
+/* Point to the exponent field. */\r
+p = &yy[NE-1];\r
+if( *p == 0x7fff )\r
+ {\r
+#ifdef NANS\r
+#ifdef IBMPC\r
+ for( i=0; i<4; i++ )\r
+ {\r
+ if((i != 3 && pe[i] != 0)\r
+ /* Check for Intel long double infinity pattern. */\r
+ || (i == 3 && pe[i] != 0x8000))\r
+ {\r
+ enan( y, NBITS );\r
+ return;\r
+ }\r
+ }\r
+#else\r
+ for( i=1; i<=4; i++ )\r
+ {\r
+ if( pe[i] != 0 )\r
+ {\r
+ enan( y, NBITS );\r
+ return;\r
+ }\r
+ }\r
+#endif\r
+#endif /* NANS */\r
+ eclear( y );\r
+ einfin( y );\r
+ if( *p & 0x8000 )\r
+ eneg(y);\r
+ return;\r
+ }\r
+#endif\r
+p = yy;\r
+q = y;\r
+for( i=0; i<NE; i++ )\r
+ *q++ = *p++;\r
+}\r
+\r
+void e113toe(pe,y)\r
+unsigned short *pe, *y;\r
+{\r
+register unsigned short r;\r
+unsigned short *e, *p;\r
+unsigned short yy[NI];\r
+int denorm, i;\r
+\r
+e = pe;\r
+denorm = 0;\r
+ecleaz(yy);\r
+#ifdef IBMPC\r
+e += 7;\r
+#endif\r
+r = *e;\r
+yy[0] = 0;\r
+if( r & 0x8000 )\r
+ yy[0] = 0xffff;\r
+r &= 0x7fff;\r
+#ifdef INFINITY\r
+if( r == 0x7fff )\r
+ {\r
+#ifdef NANS\r
+#ifdef IBMPC\r
+ for( i=0; i<7; i++ )\r
+ {\r
+ if( pe[i] != 0 )\r
+ {\r
+ enan( y, NBITS );\r
+ return;\r
+ }\r
+ }\r
+#else\r
+ for( i=1; i<8; i++ )\r
+ {\r
+ if( pe[i] != 0 )\r
+ {\r
+ enan( y, NBITS );\r
+ return;\r
+ }\r
+ }\r
+#endif\r
+#endif /* NANS */\r
+ eclear( y );\r
+ einfin( y );\r
+ if( *e & 0x8000 )\r
+ eneg(y);\r
+ return;\r
+ }\r
+#endif /* INFINITY */\r
+yy[E] = r;\r
+p = &yy[M + 1];\r
+#ifdef IBMPC\r
+for( i=0; i<7; i++ )\r
+ *p++ = *(--e);\r
+#endif\r
+#ifdef MIEEE\r
+++e;\r
+for( i=0; i<7; i++ )\r
+ *p++ = *e++;\r
+#endif\r
+/* If denormal, remove the implied bit; else shift down 1. */\r
+if( r == 0 )\r
+ {\r
+ yy[M] = 0;\r
+ }\r
+else\r
+ {\r
+ yy[M] = 1;\r
+ eshift( yy, -1 );\r
+ }\r
+emovo(yy,y);\r
+}\r
+\r
+\r
+/*\r
+; Convert IEEE single precision to e type\r
+; float d;\r
+; unsigned short x[N+2];\r
+; dtox( &d, x );\r
+*/\r
+void e24toe( pe, y )\r
+unsigned short *pe, *y;\r
+{\r
+register unsigned short r;\r
+register unsigned short *p, *e;\r
+unsigned short yy[NI];\r
+int denorm, k;\r
+\r
+e = pe;\r
+denorm = 0; /* flag if denormalized number */\r
+ecleaz(yy);\r
+#ifdef IBMPC\r
+e += 1;\r
+#endif\r
+#ifdef DEC\r
+e += 1;\r
+#endif\r
+r = *e;\r
+yy[0] = 0;\r
+if( r & 0x8000 )\r
+ yy[0] = 0xffff;\r
+yy[M] = (r & 0x7f) | 0200;\r
+r &= ~0x807f; /* strip sign and 7 significand bits */\r
+#ifdef INFINITY\r
+if( r == 0x7f80 )\r
+ {\r
+#ifdef NANS\r
+#ifdef MIEEE\r
+ if( ((pe[0] & 0x7f) != 0) || (pe[1] != 0) )\r
+ {\r
+ enan( y, NBITS );\r
+ return;\r
+ }\r
+#else\r
+ if( ((pe[1] & 0x7f) != 0) || (pe[0] != 0) )\r
+ {\r
+ enan( y, NBITS );\r
+ return;\r
+ }\r
+#endif\r
+#endif /* NANS */\r
+ eclear( y );\r
+ einfin( y );\r
+ if( yy[0] )\r
+ eneg(y);\r
+ return;\r
+ }\r
+#endif\r
+r >>= 7;\r
+/* If zero exponent, then the significand is denormalized.\r
+ * So, take back the understood high significand bit. */ \r
+if( r == 0 )\r
+ {\r
+ denorm = 1;\r
+ yy[M] &= ~0200;\r
+ }\r
+r += EXONE - 0177;\r
+yy[E] = r;\r
+p = &yy[M+1];\r
+#ifdef IBMPC\r
+*p++ = *(--e);\r
+#endif\r
+#ifdef DEC\r
+*p++ = *(--e);\r
+#endif\r
+#ifdef MIEEE\r
+++e;\r
+*p++ = *e++;\r
+#endif\r
+(void )eshift( yy, -8 );\r
+if( denorm )\r
+ { /* if zero exponent, then normalize the significand */\r
+ if( (k = enormlz(yy)) > NBITS )\r
+ ecleazs(yy);\r
+ else\r
+ yy[E] -= (unsigned short )(k-1);\r
+ }\r
+emovo( yy, y );\r
+}\r
+\r
+void etoe113(x,e)\r
+unsigned short *x, *e;\r
+{\r
+unsigned short xi[NI];\r
+long exp;\r
+int rndsav;\r
+\r
+#ifdef NANS\r
+if( eisnan(x) )\r
+ {\r
+ enan( e, 113 );\r
+ return;\r
+ }\r
+#endif\r
+emovi( x, xi );\r
+exp = (long )xi[E];\r
+#ifdef INFINITY\r
+if( eisinf(x) )\r
+ goto nonorm;\r
+#endif\r
+/* round off to nearest or even */\r
+rndsav = rndprc;\r
+rndprc = 113;\r
+emdnorm( xi, 0, 0, exp, 64 );\r
+rndprc = rndsav;\r
+nonorm:\r
+toe113 (xi, e);\r
+}\r
+\r
+/* move out internal format to ieee long double */\r
+static void toe113(a,b)\r
+unsigned short *a, *b;\r
+{\r
+register unsigned short *p, *q;\r
+unsigned short i;\r
+\r
+#ifdef NANS\r
+if( eiisnan(a) )\r
+ {\r
+ enan( b, 113 );\r
+ return;\r
+ }\r
+#endif\r
+p = a;\r
+#ifdef MIEEE\r
+q = b;\r
+#else\r
+q = b + 7; /* point to output exponent */\r
+#endif\r
+\r
+/* If not denormal, delete the implied bit. */\r
+if( a[E] != 0 )\r
+ {\r
+ eshup1 (a);\r
+ }\r
+/* combine sign and exponent */\r
+i = *p++;\r
+#ifdef MIEEE\r
+if( i )\r
+ *q++ = *p++ | 0x8000;\r
+else\r
+ *q++ = *p++;\r
+#else\r
+if( i )\r
+ *q-- = *p++ | 0x8000;\r
+else\r
+ *q-- = *p++;\r
+#endif\r
+/* skip over guard word */\r
+++p;\r
+/* move the significand */\r
+#ifdef MIEEE\r
+for (i = 0; i < 7; i++)\r
+ *q++ = *p++;\r
+#else\r
+for (i = 0; i < 7; i++)\r
+ *q-- = *p++;\r
+#endif\r
+}\r
+\r
+\r
+void etoe64( x, e )\r
+unsigned short *x, *e;\r
+{\r
+unsigned short xi[NI];\r
+long exp;\r
+int rndsav;\r
+\r
+#ifdef NANS\r
+if( eisnan(x) )\r
+ {\r
+ enan( e, 64 );\r
+ return;\r
+ }\r
+#endif\r
+emovi( x, xi );\r
+exp = (long )xi[E]; /* adjust exponent for offset */\r
+#ifdef INFINITY\r
+if( eisinf(x) )\r
+ goto nonorm;\r
+#endif\r
+/* round off to nearest or even */\r
+rndsav = rndprc;\r
+rndprc = 64;\r
+emdnorm( xi, 0, 0, exp, 64 );\r
+rndprc = rndsav;\r
+nonorm:\r
+toe64( xi, e );\r
+}\r
+\r
+/* move out internal format to ieee long double */\r
+static void toe64( a, b )\r
+unsigned short *a, *b;\r
+{\r
+register unsigned short *p, *q;\r
+unsigned short i;\r
+\r
+#ifdef NANS\r
+if( eiisnan(a) )\r
+ {\r
+ enan( b, 64 );\r
+ return;\r
+ }\r
+#endif\r
+#ifdef IBMPC\r
+/* Shift Intel denormal significand down 1. */\r
+if( a[E] == 0 )\r
+ eshdn1(a);\r
+#endif\r
+p = a;\r
+#ifdef MIEEE\r
+q = b;\r
+#else\r
+q = b + 4; /* point to output exponent */\r
+#if 1\r
+/* NOTE: if data type is 96 bits wide, clear the last word here. */\r
+*(q+1)= 0;\r
+#endif\r
+#endif\r
+\r
+/* combine sign and exponent */\r
+i = *p++;\r
+#ifdef MIEEE\r
+if( i )\r
+ *q++ = *p++ | 0x8000;\r
+else\r
+ *q++ = *p++;\r
+*q++ = 0;\r
+#else\r
+if( i )\r
+ *q-- = *p++ | 0x8000;\r
+else\r
+ *q-- = *p++;\r
+#endif\r
+/* skip over guard word */\r
+++p;\r
+/* move the significand */\r
+#ifdef MIEEE\r
+for( i=0; i<4; i++ )\r
+ *q++ = *p++;\r
+#else\r
+#ifdef INFINITY\r
+if (eiisinf (a))\r
+ {\r
+ /* Intel long double infinity. */\r
+ *q-- = 0x8000;\r
+ *q-- = 0;\r
+ *q-- = 0;\r
+ *q = 0;\r
+ return;\r
+ }\r
+#endif\r
+for( i=0; i<4; i++ )\r
+ *q-- = *p++;\r
+#endif\r
+}\r
+\r
+\r
+/*\r
+; e type to IEEE double precision\r
+; double d;\r
+; unsigned short x[NE];\r
+; etoe53( x, &d );\r
+*/\r
+\r
+#ifdef DEC\r
+\r
+void etoe53( x, e )\r
+unsigned short *x, *e;\r
+{\r
+etodec( x, e ); /* see etodec.c */\r
+}\r
+\r
+static void toe53( x, y )\r
+unsigned short *x, *y;\r
+{\r
+todec( x, y );\r
+}\r
+\r
+#else\r
+\r
+void etoe53( x, e )\r
+unsigned short *x, *e;\r
+{\r
+unsigned short xi[NI];\r
+long exp;\r
+int rndsav;\r
+\r
+#ifdef NANS\r
+if( eisnan(x) )\r
+ {\r
+ enan( e, 53 );\r
+ return;\r
+ }\r
+#endif\r
+emovi( x, xi );\r
+exp = (long )xi[E] - (EXONE - 0x3ff); /* adjust exponent for offsets */\r
+#ifdef INFINITY\r
+if( eisinf(x) )\r
+ goto nonorm;\r
+#endif\r
+/* round off to nearest or even */\r
+rndsav = rndprc;\r
+rndprc = 53;\r
+emdnorm( xi, 0, 0, exp, 64 );\r
+rndprc = rndsav;\r
+nonorm:\r
+toe53( xi, e );\r
+}\r
+\r
+\r
+static void toe53( x, y )\r
+unsigned short *x, *y;\r
+{\r
+unsigned short i;\r
+unsigned short *p;\r
+\r
+\r
+#ifdef NANS\r
+if( eiisnan(x) )\r
+ {\r
+ enan( y, 53 );\r
+ return;\r
+ }\r
+#endif\r
+p = &x[0];\r
+#ifdef IBMPC\r
+y += 3;\r
+#endif\r
+*y = 0; /* output high order */\r
+if( *p++ )\r
+ *y = 0x8000; /* output sign bit */\r
+\r
+i = *p++;\r
+if( i >= (unsigned int )2047 )\r
+ { /* Saturate at largest number less than infinity. */\r
+#ifdef INFINITY\r
+ *y |= 0x7ff0;\r
+#ifdef IBMPC\r
+ *(--y) = 0;\r
+ *(--y) = 0;\r
+ *(--y) = 0;\r
+#endif\r
+#ifdef MIEEE\r
+ ++y;\r
+ *y++ = 0;\r
+ *y++ = 0;\r
+ *y++ = 0;\r
+#endif\r
+#else\r
+ *y |= (unsigned short )0x7fef;\r
+#ifdef IBMPC\r
+ *(--y) = 0xffff;\r
+ *(--y) = 0xffff;\r
+ *(--y) = 0xffff;\r
+#endif\r
+#ifdef MIEEE\r
+ ++y;\r
+ *y++ = 0xffff;\r
+ *y++ = 0xffff;\r
+ *y++ = 0xffff;\r
+#endif\r
+#endif\r
+ return;\r
+ }\r
+if( i == 0 )\r
+ {\r
+ (void )eshift( x, 4 );\r
+ }\r
+else\r
+ {\r
+ i <<= 4;\r
+ (void )eshift( x, 5 );\r
+ }\r
+i |= *p++ & (unsigned short )0x0f; /* *p = xi[M] */\r
+*y |= (unsigned short )i; /* high order output already has sign bit set */\r
+#ifdef IBMPC\r
+*(--y) = *p++;\r
+*(--y) = *p++;\r
+*(--y) = *p;\r
+#endif\r
+#ifdef MIEEE\r
+++y;\r
+*y++ = *p++;\r
+*y++ = *p++;\r
+*y++ = *p++;\r
+#endif\r
+}\r
+\r
+#endif /* not DEC */\r
+\r
+\r
+\r
+/*\r
+; e type to IEEE single precision\r
+; float d;\r
+; unsigned short x[N+2];\r
+; xtod( x, &d );\r
+*/\r
+void etoe24( x, e )\r
+unsigned short *x, *e;\r
+{\r
+long exp;\r
+unsigned short xi[NI];\r
+int rndsav;\r
+\r
+#ifdef NANS\r
+if( eisnan(x) )\r
+ {\r
+ enan( e, 24 );\r
+ return;\r
+ }\r
+#endif\r
+emovi( x, xi );\r
+exp = (long )xi[E] - (EXONE - 0177); /* adjust exponent for offsets */\r
+#ifdef INFINITY\r
+if( eisinf(x) )\r
+ goto nonorm;\r
+#endif\r
+/* round off to nearest or even */\r
+rndsav = rndprc;\r
+rndprc = 24;\r
+emdnorm( xi, 0, 0, exp, 64 );\r
+rndprc = rndsav;\r
+nonorm:\r
+toe24( xi, e );\r
+}\r
+\r
+static void toe24( x, y )\r
+unsigned short *x, *y;\r
+{\r
+unsigned short i;\r
+unsigned short *p;\r
+\r
+#ifdef NANS\r
+if( eiisnan(x) )\r
+ {\r
+ enan( y, 24 );\r
+ return;\r
+ }\r
+#endif\r
+p = &x[0];\r
+#ifdef IBMPC\r
+y += 1;\r
+#endif\r
+#ifdef DEC\r
+y += 1;\r
+#endif\r
+*y = 0; /* output high order */\r
+if( *p++ )\r
+ *y = 0x8000; /* output sign bit */\r
+\r
+i = *p++;\r
+if( i >= 255 )\r
+ { /* Saturate at largest number less than infinity. */\r
+#ifdef INFINITY\r
+ *y |= (unsigned short )0x7f80;\r
+#ifdef IBMPC\r
+ *(--y) = 0;\r
+#endif\r
+#ifdef DEC\r
+ *(--y) = 0;\r
+#endif\r
+#ifdef MIEEE\r
+ ++y;\r
+ *y = 0;\r
+#endif\r
+#else\r
+ *y |= (unsigned short )0x7f7f;\r
+#ifdef IBMPC\r
+ *(--y) = 0xffff;\r
+#endif\r
+#ifdef DEC\r
+ *(--y) = 0xffff;\r
+#endif\r
+#ifdef MIEEE\r
+ ++y;\r
+ *y = 0xffff;\r
+#endif\r
+#endif\r
+ return;\r
+ }\r
+if( i == 0 )\r
+ {\r
+ (void )eshift( x, 7 );\r
+ }\r
+else\r
+ {\r
+ i <<= 7;\r
+ (void )eshift( x, 8 );\r
+ }\r
+i |= *p++ & (unsigned short )0x7f; /* *p = xi[M] */\r
+*y |= i; /* high order output already has sign bit set */\r
+#ifdef IBMPC\r
+*(--y) = *p;\r
+#endif\r
+#ifdef DEC\r
+*(--y) = *p;\r
+#endif\r
+#ifdef MIEEE\r
+++y;\r
+*y = *p;\r
+#endif\r
+}\r
+\r
+\r
+/* Compare two e type numbers.\r
+ *\r
+ * unsigned short a[NE], b[NE];\r
+ * ecmp( a, b );\r
+ *\r
+ * returns +1 if a > b\r
+ * 0 if a == b\r
+ * -1 if a < b\r
+ * -2 if either a or b is a NaN.\r
+ */\r
+int ecmp( a, b )\r
+unsigned short *a, *b;\r
+{\r
+unsigned short ai[NI], bi[NI];\r
+register unsigned short *p, *q;\r
+register int i;\r
+int msign;\r
+\r
+#ifdef NANS\r
+if (eisnan (a) || eisnan (b))\r
+ return( -2 );\r
+#endif\r
+emovi( a, ai );\r
+p = ai;\r
+emovi( b, bi );\r
+q = bi;\r
+\r
+if( *p != *q )\r
+ { /* the signs are different */\r
+/* -0 equals + 0 */\r
+ for( i=1; i<NI-1; i++ )\r
+ {\r
+ if( ai[i] != 0 )\r
+ goto nzro;\r
+ if( bi[i] != 0 )\r
+ goto nzro;\r
+ }\r
+ return(0);\r
+nzro:\r
+ if( *p == 0 )\r
+ return( 1 );\r
+ else\r
+ return( -1 );\r
+ }\r
+/* both are the same sign */\r
+if( *p == 0 )\r
+ msign = 1;\r
+else\r
+ msign = -1;\r
+i = NI-1;\r
+do\r
+ {\r
+ if( *p++ != *q++ )\r
+ {\r
+ goto diff;\r
+ }\r
+ }\r
+while( --i > 0 );\r
+\r
+return(0); /* equality */\r
+\r
+\r
+\r
+diff:\r
+\r
+if( *(--p) > *(--q) )\r
+ return( msign ); /* p is bigger */\r
+else\r
+ return( -msign ); /* p is littler */\r
+}\r
+\r
+\r
+\r
+\r
+/* Find nearest integer to x = floor( x + 0.5 )\r
+ *\r
+ * unsigned short x[NE], y[NE]\r
+ * eround( x, y );\r
+ */\r
+void eround( x, y )\r
+unsigned short *x, *y;\r
+{\r
+\r
+eadd( ehalf, x, y );\r
+efloor( y, y );\r
+}\r
+\r
+\r
+\r
+\r
+/*\r
+; convert long (32-bit) integer to e type\r
+;\r
+; long l;\r
+; unsigned short x[NE];\r
+; ltoe( &l, x );\r
+; note &l is the memory address of l\r
+*/\r
+void ltoe( lp, y )\r
+long *lp; /* lp is the memory address of a long integer */\r
+unsigned short *y; /* y is the address of a short */\r
+{\r
+unsigned short yi[NI];\r
+unsigned long ll;\r
+int k;\r
+\r
+ecleaz( yi );\r
+if( *lp < 0 )\r
+ {\r
+ ll = (unsigned long )( -(*lp) ); /* make it positive */\r
+ yi[0] = 0xffff; /* put correct sign in the e type number */\r
+ }\r
+else\r
+ {\r
+ ll = (unsigned long )( *lp );\r
+ }\r
+/* move the long integer to yi significand area */\r
+if( sizeof(long) == 8 )\r
+ {\r
+ yi[M] = (unsigned short) (ll >> (LONGBITS - 16));\r
+ yi[M + 1] = (unsigned short) (ll >> (LONGBITS - 32));\r
+ yi[M + 2] = (unsigned short) (ll >> 16);\r
+ yi[M + 3] = (unsigned short) ll;\r
+ yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */\r
+ }\r
+else\r
+ {\r
+ yi[M] = (unsigned short )(ll >> 16); \r
+ yi[M+1] = (unsigned short )ll;\r
+ yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */\r
+ }\r
+if( (k = enormlz( yi )) > NBITS ) /* normalize the significand */\r
+ ecleaz( yi ); /* it was zero */\r
+else\r
+ yi[E] -= (unsigned short )k; /* subtract shift count from exponent */\r
+emovo( yi, y ); /* output the answer */\r
+}\r
+\r
+/*\r
+; convert unsigned long (32-bit) integer to e type\r
+;\r
+; unsigned long l;\r
+; unsigned short x[NE];\r
+; ltox( &l, x );\r
+; note &l is the memory address of l\r
+*/\r
+void ultoe( lp, y )\r
+unsigned long *lp; /* lp is the memory address of a long integer */\r
+unsigned short *y; /* y is the address of a short */\r
+{\r
+unsigned short yi[NI];\r
+unsigned long ll;\r
+int k;\r
+\r
+ecleaz( yi );\r
+ll = *lp;\r
+\r
+/* move the long integer to ayi significand area */\r
+if( sizeof(long) == 8 )\r
+ {\r
+ yi[M] = (unsigned short) (ll >> (LONGBITS - 16));\r
+ yi[M + 1] = (unsigned short) (ll >> (LONGBITS - 32));\r
+ yi[M + 2] = (unsigned short) (ll >> 16);\r
+ yi[M + 3] = (unsigned short) ll;\r
+ yi[E] = EXONE + 47; /* exponent if normalize shift count were 0 */\r
+ }\r
+else\r
+ {\r
+ yi[M] = (unsigned short )(ll >> 16); \r
+ yi[M+1] = (unsigned short )ll;\r
+ yi[E] = EXONE + 15; /* exponent if normalize shift count were 0 */\r
+ }\r
+if( (k = enormlz( yi )) > NBITS ) /* normalize the significand */\r
+ ecleaz( yi ); /* it was zero */\r
+else\r
+ yi[E] -= (unsigned short )k; /* subtract shift count from exponent */\r
+emovo( yi, y ); /* output the answer */\r
+}\r
+\r
+\r
+/*\r
+; Find long integer and fractional parts\r
+\r
+; long i;\r
+; unsigned short x[NE], frac[NE];\r
+; xifrac( x, &i, frac );\r
+ \r
+ The integer output has the sign of the input. The fraction is\r
+ the positive fractional part of abs(x).\r
+*/\r
+void eifrac( x, i, frac )\r
+unsigned short *x;\r
+long *i;\r
+unsigned short *frac;\r
+{\r
+unsigned short xi[NI];\r
+int j, k;\r
+unsigned long ll;\r
+\r
+emovi( x, xi );\r
+k = (int )xi[E] - (EXONE - 1);\r
+if( k <= 0 )\r
+ {\r
+/* if exponent <= 0, integer = 0 and real output is fraction */\r
+ *i = 0L;\r
+ emovo( xi, frac );\r
+ return;\r
+ }\r
+if( k > (8 * sizeof(long) - 1) )\r
+ {\r
+/*\r
+; long integer overflow: output large integer\r
+; and correct fraction\r
+*/\r
+ j = 8 * sizeof(long) - 1;\r
+ if( xi[0] )\r
+ *i = (long) ((unsigned long) 1) << j;\r
+ else\r
+ *i = (long) (((unsigned long) (~(0L))) >> 1);\r
+ (void )eshift( xi, k );\r
+ }\r
+if( k > 16 )\r
+ {\r
+/*\r
+ Shift more than 16 bits: shift up k-16 mod 16\r
+ then shift by 16's.\r
+*/\r
+ j = k - ((k >> 4) << 4);\r
+ eshift (xi, j);\r
+ ll = xi[M];\r
+ k -= j;\r
+ do\r
+ {\r
+ eshup6 (xi);\r
+ ll = (ll << 16) | xi[M];\r
+ }\r
+ while ((k -= 16) > 0);\r
+ *i = ll;\r
+ if (xi[0])\r
+ *i = -(*i);\r
+ }\r
+else\r
+ {\r
+/* shift not more than 16 bits */\r
+ eshift( xi, k );\r
+ *i = (long )xi[M] & 0xffff;\r
+ if( xi[0] )\r
+ *i = -(*i);\r
+ }\r
+xi[0] = 0;\r
+xi[E] = EXONE - 1;\r
+xi[M] = 0;\r
+if( (k = enormlz( xi )) > NBITS )\r
+ ecleaz( xi );\r
+else\r
+ xi[E] -= (unsigned short )k;\r
+\r
+emovo( xi, frac );\r
+}\r
+\r
+\r
+/*\r
+; Find unsigned long integer and fractional parts\r
+\r
+; unsigned long i;\r
+; unsigned short x[NE], frac[NE];\r
+; xifrac( x, &i, frac );\r
+\r
+ A negative e type input yields integer output = 0\r
+ but correct fraction.\r
+*/\r
+void euifrac( x, i, frac )\r
+unsigned short *x;\r
+unsigned long *i;\r
+unsigned short *frac;\r
+{\r
+unsigned short xi[NI];\r
+int j, k;\r
+unsigned long ll;\r
+\r
+emovi( x, xi );\r
+k = (int )xi[E] - (EXONE - 1);\r
+if( k <= 0 )\r
+ {\r
+/* if exponent <= 0, integer = 0 and argument is fraction */\r
+ *i = 0L;\r
+ emovo( xi, frac );\r
+ return;\r
+ }\r
+if( k > (8 * sizeof(long)) )\r
+ {\r
+/*\r
+; long integer overflow: output large integer\r
+; and correct fraction\r
+*/\r
+ *i = ~(0L);\r
+ (void )eshift( xi, k );\r
+ }\r
+else if( k > 16 )\r
+ {\r
+/*\r
+ Shift more than 16 bits: shift up k-16 mod 16\r
+ then shift up by 16's.\r
+*/\r
+ j = k - ((k >> 4) << 4);\r
+ eshift (xi, j);\r
+ ll = xi[M];\r
+ k -= j;\r
+ do\r
+ {\r
+ eshup6 (xi);\r
+ ll = (ll << 16) | xi[M];\r
+ }\r
+ while ((k -= 16) > 0);\r
+ *i = ll;\r
+ }\r
+else\r
+ {\r
+/* shift not more than 16 bits */\r
+ eshift( xi, k );\r
+ *i = (long )xi[M] & 0xffff;\r
+ }\r
+\r
+if( xi[0] ) /* A negative value yields unsigned integer 0. */\r
+ *i = 0L;\r
+\r
+xi[0] = 0;\r
+xi[E] = EXONE - 1;\r
+xi[M] = 0;\r
+if( (k = enormlz( xi )) > NBITS )\r
+ ecleaz( xi );\r
+else\r
+ xi[E] -= (unsigned short )k;\r
+\r
+emovo( xi, frac );\r
+}\r
+\r
+\r
+\r
+/*\r
+; Shift significand\r
+;\r
+; Shifts significand area up or down by the number of bits\r
+; given by the variable sc.\r
+*/\r
+int eshift( x, sc )\r
+unsigned short *x;\r
+int sc;\r
+{\r
+unsigned short lost;\r
+unsigned short *p;\r
+\r
+if( sc == 0 )\r
+ return( 0 );\r
+\r
+lost = 0;\r
+p = x + NI-1;\r
+\r
+if( sc < 0 )\r
+ {\r
+ sc = -sc;\r
+ while( sc >= 16 )\r
+ {\r
+ lost |= *p; /* remember lost bits */\r
+ eshdn6(x);\r
+ sc -= 16;\r
+ }\r
+\r
+ while( sc >= 8 )\r
+ {\r
+ lost |= *p & 0xff;\r
+ eshdn8(x);\r
+ sc -= 8;\r
+ }\r
+\r
+ while( sc > 0 )\r
+ {\r
+ lost |= *p & 1;\r
+ eshdn1(x);\r
+ sc -= 1;\r
+ }\r
+ }\r
+else\r
+ {\r
+ while( sc >= 16 )\r
+ {\r
+ eshup6(x);\r
+ sc -= 16;\r
+ }\r
+\r
+ while( sc >= 8 )\r
+ {\r
+ eshup8(x);\r
+ sc -= 8;\r
+ }\r
+\r
+ while( sc > 0 )\r
+ {\r
+ eshup1(x);\r
+ sc -= 1;\r
+ }\r
+ }\r
+if( lost )\r
+ lost = 1;\r
+return( (int )lost );\r
+}\r
+\r
+\r
+\r
+/*\r
+; normalize\r
+;\r
+; Shift normalizes the significand area pointed to by argument\r
+; shift count (up = positive) is returned.\r
+*/\r
+int enormlz(x)\r
+unsigned short x[];\r
+{\r
+register unsigned short *p;\r
+int sc;\r
+\r
+sc = 0;\r
+p = &x[M];\r
+if( *p != 0 )\r
+ goto normdn;\r
+++p;\r
+if( *p & 0x8000 )\r
+ return( 0 ); /* already normalized */\r
+while( *p == 0 )\r
+ {\r
+ eshup6(x);\r
+ sc += 16;\r
+/* With guard word, there are NBITS+16 bits available.\r
+ * return true if all are zero.\r
+ */\r
+ if( sc > NBITS )\r
+ return( sc );\r
+ }\r
+/* see if high byte is zero */\r
+while( (*p & 0xff00) == 0 )\r
+ {\r
+ eshup8(x);\r
+ sc += 8;\r
+ }\r
+/* now shift 1 bit at a time */\r
+while( (*p & 0x8000) == 0)\r
+ {\r
+ eshup1(x);\r
+ sc += 1;\r
+ if( sc > (NBITS+16) )\r
+ {\r
+ mtherr( "enormlz", UNDERFLOW );\r
+ return( sc );\r
+ }\r
+ }\r
+return( sc );\r
+\r
+/* Normalize by shifting down out of the high guard word\r
+ of the significand */\r
+normdn:\r
+\r
+if( *p & 0xff00 )\r
+ {\r
+ eshdn8(x);\r
+ sc -= 8;\r
+ }\r
+while( *p != 0 )\r
+ {\r
+ eshdn1(x);\r
+ sc -= 1;\r
+\r
+ if( sc < -NBITS )\r
+ {\r
+ mtherr( "enormlz", OVERFLOW );\r
+ return( sc );\r
+ }\r
+ }\r
+return( sc );\r
+}\r
+\r
+\r
+\r
+\r
+/* Convert e type number to decimal format ASCII string.\r
+ * The constants are for 64 bit precision.\r
+ */\r
+\r
+#define NTEN 12\r
+#define MAXP 4096\r
+\r
+#if NE == 10\r
+static unsigned short etens[NTEN + 1][NE] =\r
+{\r
+ {0x6576, 0x4a92, 0x804a, 0x153f,\r
+ 0xc94c, 0x979a, 0x8a20, 0x5202, 0xc460, 0x7525,}, /* 10**4096 */\r
+ {0x6a32, 0xce52, 0x329a, 0x28ce,\r
+ 0xa74d, 0x5de4, 0xc53d, 0x3b5d, 0x9e8b, 0x5a92,}, /* 10**2048 */\r
+ {0x526c, 0x50ce, 0xf18b, 0x3d28,\r
+ 0x650d, 0x0c17, 0x8175, 0x7586, 0xc976, 0x4d48,},\r
+ {0x9c66, 0x58f8, 0xbc50, 0x5c54,\r
+ 0xcc65, 0x91c6, 0xa60e, 0xa0ae, 0xe319, 0x46a3,},\r
+ {0x851e, 0xeab7, 0x98fe, 0x901b,\r
+ 0xddbb, 0xde8d, 0x9df9, 0xebfb, 0xaa7e, 0x4351,},\r
+ {0x0235, 0x0137, 0x36b1, 0x336c,\r
+ 0xc66f, 0x8cdf, 0x80e9, 0x47c9, 0x93ba, 0x41a8,},\r
+ {0x50f8, 0x25fb, 0xc76b, 0x6b71,\r
+ 0x3cbf, 0xa6d5, 0xffcf, 0x1f49, 0xc278, 0x40d3,},\r
+ {0x0000, 0x0000, 0x0000, 0x0000,\r
+ 0xf020, 0xb59d, 0x2b70, 0xada8, 0x9dc5, 0x4069,},\r
+ {0x0000, 0x0000, 0x0000, 0x0000,\r
+ 0x0000, 0x0000, 0x0400, 0xc9bf, 0x8e1b, 0x4034,},\r
+ {0x0000, 0x0000, 0x0000, 0x0000,\r
+ 0x0000, 0x0000, 0x0000, 0x2000, 0xbebc, 0x4019,},\r
+ {0x0000, 0x0000, 0x0000, 0x0000,\r
+ 0x0000, 0x0000, 0x0000, 0x0000, 0x9c40, 0x400c,},\r
+ {0x0000, 0x0000, 0x0000, 0x0000,\r
+ 0x0000, 0x0000, 0x0000, 0x0000, 0xc800, 0x4005,},\r
+ {0x0000, 0x0000, 0x0000, 0x0000,\r
+ 0x0000, 0x0000, 0x0000, 0x0000, 0xa000, 0x4002,}, /* 10**1 */\r
+};\r
+\r
+static unsigned short emtens[NTEN + 1][NE] =\r
+{\r
+ {0x2030, 0xcffc, 0xa1c3, 0x8123,\r
+ 0x2de3, 0x9fde, 0xd2ce, 0x04c8, 0xa6dd, 0x0ad8,}, /* 10**-4096 */\r
+ {0x8264, 0xd2cb, 0xf2ea, 0x12d4,\r
+ 0x4925, 0x2de4, 0x3436, 0x534f, 0xceae, 0x256b,}, /* 10**-2048 */\r
+ {0xf53f, 0xf698, 0x6bd3, 0x0158,\r
+ 0x87a6, 0xc0bd, 0xda57, 0x82a5, 0xa2a6, 0x32b5,},\r
+ {0xe731, 0x04d4, 0xe3f2, 0xd332,\r
+ 0x7132, 0xd21c, 0xdb23, 0xee32, 0x9049, 0x395a,},\r
+ {0xa23e, 0x5308, 0xfefb, 0x1155,\r
+ 0xfa91, 0x1939, 0x637a, 0x4325, 0xc031, 0x3cac,},\r
+ {0xe26d, 0xdbde, 0xd05d, 0xb3f6,\r
+ 0xac7c, 0xe4a0, 0x64bc, 0x467c, 0xddd0, 0x3e55,},\r
+ {0x2a20, 0x6224, 0x47b3, 0x98d7,\r
+ 0x3f23, 0xe9a5, 0xa539, 0xea27, 0xa87f, 0x3f2a,},\r
+ {0x0b5b, 0x4af2, 0xa581, 0x18ed,\r
+ 0x67de, 0x94ba, 0x4539, 0x1ead, 0xcfb1, 0x3f94,},\r
+ {0xbf71, 0xa9b3, 0x7989, 0xbe68,\r
+ 0x4c2e, 0xe15b, 0xc44d, 0x94be, 0xe695, 0x3fc9,},\r
+ {0x3d4d, 0x7c3d, 0x36ba, 0x0d2b,\r
+ 0xfdc2, 0xcefc, 0x8461, 0x7711, 0xabcc, 0x3fe4,},\r
+ {0xc155, 0xa4a8, 0x404e, 0x6113,\r
+ 0xd3c3, 0x652b, 0xe219, 0x1758, 0xd1b7, 0x3ff1,},\r
+ {0xd70a, 0x70a3, 0x0a3d, 0xa3d7,\r
+ 0x3d70, 0xd70a, 0x70a3, 0x0a3d, 0xa3d7, 0x3ff8,},\r
+ {0xcccd, 0xcccc, 0xcccc, 0xcccc,\r
+ 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0xcccc, 0x3ffb,}, /* 10**-1 */\r
+};\r
+#else\r
+static unsigned short etens[NTEN+1][NE] = {\r
+{0xc94c,0x979a,0x8a20,0x5202,0xc460,0x7525,},/* 10**4096 */\r
+{0xa74d,0x5de4,0xc53d,0x3b5d,0x9e8b,0x5a92,},/* 10**2048 */\r
+{0x650d,0x0c17,0x8175,0x7586,0xc976,0x4d48,},\r
+{0xcc65,0x91c6,0xa60e,0xa0ae,0xe319,0x46a3,},\r
+{0xddbc,0xde8d,0x9df9,0xebfb,0xaa7e,0x4351,},\r
+{0xc66f,0x8cdf,0x80e9,0x47c9,0x93ba,0x41a8,},\r
+{0x3cbf,0xa6d5,0xffcf,0x1f49,0xc278,0x40d3,},\r
+{0xf020,0xb59d,0x2b70,0xada8,0x9dc5,0x4069,},\r
+{0x0000,0x0000,0x0400,0xc9bf,0x8e1b,0x4034,},\r
+{0x0000,0x0000,0x0000,0x2000,0xbebc,0x4019,},\r
+{0x0000,0x0000,0x0000,0x0000,0x9c40,0x400c,},\r
+{0x0000,0x0000,0x0000,0x0000,0xc800,0x4005,},\r
+{0x0000,0x0000,0x0000,0x0000,0xa000,0x4002,}, /* 10**1 */\r
+};\r
+\r
+static unsigned short emtens[NTEN+1][NE] = {\r
+{0x2de4,0x9fde,0xd2ce,0x04c8,0xa6dd,0x0ad8,}, /* 10**-4096 */\r
+{0x4925,0x2de4,0x3436,0x534f,0xceae,0x256b,}, /* 10**-2048 */\r
+{0x87a6,0xc0bd,0xda57,0x82a5,0xa2a6,0x32b5,},\r
+{0x7133,0xd21c,0xdb23,0xee32,0x9049,0x395a,},\r
+{0xfa91,0x1939,0x637a,0x4325,0xc031,0x3cac,},\r
+{0xac7d,0xe4a0,0x64bc,0x467c,0xddd0,0x3e55,},\r
+{0x3f24,0xe9a5,0xa539,0xea27,0xa87f,0x3f2a,},\r
+{0x67de,0x94ba,0x4539,0x1ead,0xcfb1,0x3f94,},\r
+{0x4c2f,0xe15b,0xc44d,0x94be,0xe695,0x3fc9,},\r
+{0xfdc2,0xcefc,0x8461,0x7711,0xabcc,0x3fe4,},\r
+{0xd3c3,0x652b,0xe219,0x1758,0xd1b7,0x3ff1,},\r
+{0x3d71,0xd70a,0x70a3,0x0a3d,0xa3d7,0x3ff8,},\r
+{0xcccd,0xcccc,0xcccc,0xcccc,0xcccc,0x3ffb,}, /* 10**-1 */\r
+};\r
+#endif\r
+\r
+void e24toasc( x, string, ndigs )\r
+unsigned short x[];\r
+char *string;\r
+int ndigs;\r
+{\r
+unsigned short w[NI];\r
+\r
+e24toe( x, w );\r
+etoasc( w, string, ndigs );\r
+}\r
+\r
+\r
+void e53toasc( x, string, ndigs )\r
+unsigned short x[];\r
+char *string;\r
+int ndigs;\r
+{\r
+unsigned short w[NI];\r
+\r
+e53toe( x, w );\r
+etoasc( w, string, ndigs );\r
+}\r
+\r
+\r
+void e64toasc( x, string, ndigs )\r
+unsigned short x[];\r
+char *string;\r
+int ndigs;\r
+{\r
+unsigned short w[NI];\r
+\r
+e64toe( x, w );\r
+etoasc( w, string, ndigs );\r
+}\r
+\r
+void e113toasc (x, string, ndigs)\r
+unsigned short x[];\r
+char *string;\r
+int ndigs;\r
+{\r
+unsigned short w[NI];\r
+\r
+e113toe (x, w);\r
+etoasc (w, string, ndigs);\r
+}\r
+\r
+\r
+void etoasc( x, string, ndigs )\r
+unsigned short x[];\r
+char *string;\r
+int ndigs;\r
+{\r
+long digit;\r
+unsigned short y[NI], t[NI], u[NI], w[NI];\r
+unsigned short *p, *r, *ten;\r
+unsigned short sign;\r
+int i, j, k, expon, rndsav;\r
+char *s, *ss;\r
+unsigned short m;\r
+\r
+rndsav = rndprc;\r
+#ifdef NANS\r
+if( eisnan(x) )\r
+ {\r
+ sprintf( string, " NaN " );\r
+ goto bxit;\r
+ }\r
+#endif\r
+rndprc = NBITS; /* set to full precision */\r
+emov( x, y ); /* retain external format */\r
+if( y[NE-1] & 0x8000 )\r
+ {\r
+ sign = 0xffff;\r
+ y[NE-1] &= 0x7fff;\r
+ }\r
+else\r
+ {\r
+ sign = 0;\r
+ }\r
+expon = 0;\r
+ten = &etens[NTEN][0];\r
+emov( eone, t );\r
+/* Test for zero exponent */\r
+if( y[NE-1] == 0 )\r
+ {\r
+ for( k=0; k<NE-1; k++ )\r
+ {\r
+ if( y[k] != 0 )\r
+ goto tnzro; /* denormalized number */\r
+ }\r
+ goto isone; /* legal all zeros */\r
+ }\r
+tnzro:\r
+\r
+/* Test for infinity.\r
+ */\r
+if( y[NE-1] == 0x7fff )\r
+ {\r
+ if( sign )\r
+ sprintf( string, " -Infinity " );\r
+ else\r
+ sprintf( string, " Infinity " );\r
+ goto bxit;\r
+ }\r
+\r
+/* Test for exponent nonzero but significand denormalized.\r
+ * This is an error condition.\r
+ */\r
+if( (y[NE-1] != 0) && ((y[NE-2] & 0x8000) == 0) )\r
+ {\r
+ mtherr( "etoasc", DOMAIN );\r
+ sprintf( string, "NaN" );\r
+ goto bxit;\r
+ }\r
+\r
+/* Compare to 1.0 */\r
+i = ecmp( eone, y );\r
+if( i == 0 )\r
+ goto isone;\r
+\r
+if( i < 0 )\r
+ { /* Number is greater than 1 */\r
+/* Convert significand to an integer and strip trailing decimal zeros. */\r
+ emov( y, u );\r
+ u[NE-1] = EXONE + NBITS - 1;\r
+\r
+ p = &etens[NTEN-4][0];\r
+ m = 16;\r
+do\r
+ {\r
+ ediv( p, u, t );\r
+ efloor( t, w );\r
+ for( j=0; j<NE-1; j++ )\r
+ {\r
+ if( t[j] != w[j] )\r
+ goto noint;\r
+ }\r
+ emov( t, u );\r
+ expon += (int )m;\r
+noint:\r
+ p += NE;\r
+ m >>= 1;\r
+ }\r
+while( m != 0 );\r
+\r
+/* Rescale from integer significand */\r
+ u[NE-1] += y[NE-1] - (unsigned int )(EXONE + NBITS - 1);\r
+ emov( u, y );\r
+/* Find power of 10 */\r
+ emov( eone, t );\r
+ m = MAXP;\r
+ p = &etens[0][0];\r
+ while( ecmp( ten, u ) <= 0 )\r
+ {\r
+ if( ecmp( p, u ) <= 0 )\r
+ {\r
+ ediv( p, u, u );\r
+ emul( p, t, t );\r
+ expon += (int )m;\r
+ }\r
+ m >>= 1;\r
+ if( m == 0 )\r
+ break;\r
+ p += NE;\r
+ }\r
+ }\r
+else\r
+ { /* Number is less than 1.0 */\r
+/* Pad significand with trailing decimal zeros. */\r
+ if( y[NE-1] == 0 )\r
+ {\r
+ while( (y[NE-2] & 0x8000) == 0 )\r
+ {\r
+ emul( ten, y, y );\r
+ expon -= 1;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ emovi( y, w );\r
+ for( i=0; i<NDEC+1; i++ )\r
+ {\r
+ if( (w[NI-1] & 0x7) != 0 )\r
+ break;\r
+/* multiply by 10 */\r
+ emovz( w, u );\r
+ eshdn1( u );\r
+ eshdn1( u );\r
+ eaddm( w, u );\r
+ u[1] += 3;\r
+ while( u[2] != 0 )\r
+ {\r
+ eshdn1(u);\r
+ u[1] += 1;\r
+ }\r
+ if( u[NI-1] != 0 )\r
+ break;\r
+ if( eone[NE-1] <= u[1] )\r
+ break;\r
+ emovz( u, w );\r
+ expon -= 1;\r
+ }\r
+ emovo( w, y );\r
+ }\r
+ k = -MAXP;\r
+ p = &emtens[0][0];\r
+ r = &etens[0][0];\r
+ emov( y, w );\r
+ emov( eone, t );\r
+ while( ecmp( eone, w ) > 0 )\r
+ {\r
+ if( ecmp( p, w ) >= 0 )\r
+ {\r
+ emul( r, w, w );\r
+ emul( r, t, t );\r
+ expon += k;\r
+ }\r
+ k /= 2;\r
+ if( k == 0 )\r
+ break;\r
+ p += NE;\r
+ r += NE;\r
+ }\r
+ ediv( t, eone, t );\r
+ }\r
+isone:\r
+/* Find the first (leading) digit. */\r
+emovi( t, w );\r
+emovz( w, t );\r
+emovi( y, w );\r
+emovz( w, y );\r
+eiremain( t, y );\r
+digit = equot[NI-1];\r
+while( (digit == 0) && (ecmp(y,ezero) != 0) )\r
+ {\r
+ eshup1( y );\r
+ emovz( y, u );\r
+ eshup1( u );\r
+ eshup1( u );\r
+ eaddm( u, y );\r
+ eiremain( t, y );\r
+ digit = equot[NI-1];\r
+ expon -= 1;\r
+ }\r
+s = string;\r
+if( sign )\r
+ *s++ = '-';\r
+else\r
+ *s++ = ' ';\r
+/* Examine number of digits requested by caller. */\r
+if( ndigs < 0 )\r
+ ndigs = 0;\r
+if( ndigs > NDEC )\r
+ ndigs = NDEC;\r
+if( digit == 10 )\r
+ {\r
+ *s++ = '1';\r
+ *s++ = '.';\r
+ if( ndigs > 0 )\r
+ {\r
+ *s++ = '0';\r
+ ndigs -= 1;\r
+ }\r
+ expon += 1;\r
+ }\r
+else\r
+ {\r
+ *s++ = (char )digit + '0';\r
+ *s++ = '.';\r
+ }\r
+/* Generate digits after the decimal point. */\r
+for( k=0; k<=ndigs; k++ )\r
+ {\r
+/* multiply current number by 10, without normalizing */\r
+ eshup1( y );\r
+ emovz( y, u );\r
+ eshup1( u );\r
+ eshup1( u );\r
+ eaddm( u, y );\r
+ eiremain( t, y );\r
+ *s++ = (char )equot[NI-1] + '0';\r
+ }\r
+digit = equot[NI-1];\r
+--s;\r
+ss = s;\r
+/* round off the ASCII string */\r
+if( digit > 4 )\r
+ {\r
+/* Test for critical rounding case in ASCII output. */\r
+ if( digit == 5 )\r
+ {\r
+ emovo( y, t );\r
+ if( ecmp(t,ezero) != 0 )\r
+ goto roun; /* round to nearest */\r
+ if( (*(s-1) & 1) == 0 )\r
+ goto doexp; /* round to even */\r
+ }\r
+/* Round up and propagate carry-outs */\r
+roun:\r
+ --s;\r
+ k = *s & 0x7f;\r
+/* Carry out to most significant digit? */\r
+ if( k == '.' )\r
+ {\r
+ --s;\r
+ k = *s;\r
+ k += 1;\r
+ *s = (char )k;\r
+/* Most significant digit carries to 10? */\r
+ if( k > '9' )\r
+ {\r
+ expon += 1;\r
+ *s = '1';\r
+ }\r
+ goto doexp;\r
+ }\r
+/* Round up and carry out from less significant digits */\r
+ k += 1;\r
+ *s = (char )k;\r
+ if( k > '9' )\r
+ {\r
+ *s = '0';\r
+ goto roun;\r
+ }\r
+ }\r
+doexp:\r
+/*\r
+if( expon >= 0 )\r
+ sprintf( ss, "e+%d", expon );\r
+else\r
+ sprintf( ss, "e%d", expon );\r
+*/\r
+ sprintf( ss, "E%d", expon );\r
+bxit:\r
+rndprc = rndsav;\r
+}\r
+\r
+\r
+\r
+\r
+/*\r
+; ASCTOQ\r
+; ASCTOQ.MAC LATEST REV: 11 JAN 84\r
+; SLM, 3 JAN 78\r
+;\r
+; Convert ASCII string to quadruple precision floating point\r
+;\r
+; Numeric input is free field decimal number\r
+; with max of 15 digits with or without \r
+; decimal point entered as ASCII from teletype.\r
+; Entering E after the number followed by a second\r
+; number causes the second number to be interpreted\r
+; as a power of 10 to be multiplied by the first number\r
+; (i.e., "scientific" notation).\r
+;\r
+; Usage:\r
+; asctoq( string, q );\r
+*/\r
+\r
+/* ASCII to single */\r
+void asctoe24( s, y )\r
+char *s;\r
+unsigned short *y;\r
+{\r
+asctoeg( s, y, 24 );\r
+}\r
+\r
+\r
+/* ASCII to double */\r
+void asctoe53( s, y )\r
+char *s;\r
+unsigned short *y;\r
+{\r
+#ifdef DEC\r
+asctoeg( s, y, 56 );\r
+#else\r
+asctoeg( s, y, 53 );\r
+#endif\r
+}\r
+\r
+\r
+/* ASCII to long double */\r
+void asctoe64( s, y )\r
+char *s;\r
+unsigned short *y;\r
+{\r
+asctoeg( s, y, 64 );\r
+}\r
+\r
+/* ASCII to 128-bit long double */\r
+void asctoe113 (s, y)\r
+char *s;\r
+unsigned short *y;\r
+{\r
+asctoeg( s, y, 113 );\r
+}\r
+\r
+/* ASCII to super double */\r
+void asctoe( s, y )\r
+char *s;\r
+unsigned short *y;\r
+{\r
+asctoeg( s, y, NBITS );\r
+}\r
+\r
+/* Space to make a copy of the input string: */\r
+static char lstr[82] = {0};\r
+\r
+void asctoeg( ss, y, oprec )\r
+char *ss;\r
+unsigned short *y;\r
+int oprec;\r
+{\r
+unsigned short yy[NI], xt[NI], tt[NI];\r
+int esign, decflg, sgnflg, nexp, exp, prec, lost;\r
+int k, trail, c, rndsav;\r
+long lexp;\r
+unsigned short nsign, *p;\r
+char *sp, *s;\r
+\r
+/* Copy the input string. */\r
+s = ss;\r
+while( *s == ' ' ) /* skip leading spaces */\r
+ ++s;\r
+sp = lstr;\r
+for( k=0; k<79; k++ )\r
+ {\r
+ if( (*sp++ = *s++) == '\0' )\r
+ break;\r
+ }\r
+*sp = '\0';\r
+s = lstr;\r
+\r
+rndsav = rndprc;\r
+rndprc = NBITS; /* Set to full precision */\r
+lost = 0;\r
+nsign = 0;\r
+decflg = 0;\r
+sgnflg = 0;\r
+nexp = 0;\r
+exp = 0;\r
+prec = 0;\r
+ecleaz( yy );\r
+trail = 0;\r
+\r
+nxtcom:\r
+k = *s - '0';\r
+if( (k >= 0) && (k <= 9) )\r
+ {\r
+/* Ignore leading zeros */\r
+ if( (prec == 0) && (decflg == 0) && (k == 0) )\r
+ goto donchr;\r
+/* Identify and strip trailing zeros after the decimal point. */\r
+ if( (trail == 0) && (decflg != 0) )\r
+ {\r
+ sp = s;\r
+ while( (*sp >= '0') && (*sp <= '9') )\r
+ ++sp;\r
+/* Check for syntax error */\r
+ c = *sp & 0x7f;\r
+ if( (c != 'e') && (c != 'E') && (c != '\0')\r
+ && (c != '\n') && (c != '\r') && (c != ' ')\r
+ && (c != ',') )\r
+ goto error;\r
+ --sp;\r
+ while( *sp == '0' )\r
+ *sp-- = 'z';\r
+ trail = 1;\r
+ if( *s == 'z' )\r
+ goto donchr;\r
+ }\r
+/* If enough digits were given to more than fill up the yy register,\r
+ * continuing until overflow into the high guard word yy[2]\r
+ * guarantees that there will be a roundoff bit at the top\r
+ * of the low guard word after normalization.\r
+ */\r
+ if( yy[2] == 0 )\r
+ {\r
+ if( decflg )\r
+ nexp += 1; /* count digits after decimal point */\r
+ eshup1( yy ); /* multiply current number by 10 */\r
+ emovz( yy, xt );\r
+ eshup1( xt );\r
+ eshup1( xt );\r
+ eaddm( xt, yy );\r
+ ecleaz( xt );\r
+ xt[NI-2] = (unsigned short )k;\r
+ eaddm( xt, yy );\r
+ }\r
+ else\r
+ {\r
+ /* Mark any lost non-zero digit. */\r
+ lost |= k;\r
+ /* Count lost digits before the decimal point. */\r
+ if (decflg == 0)\r
+ nexp -= 1;\r
+ }\r
+ prec += 1;\r
+ goto donchr;\r
+ }\r
+\r
+switch( *s )\r
+ {\r
+ case 'z':\r
+ break;\r
+ case 'E':\r
+ case 'e':\r
+ goto expnt;\r
+ case '.': /* decimal point */\r
+ if( decflg )\r
+ goto error;\r
+ ++decflg;\r
+ break;\r
+ case '-':\r
+ nsign = 0xffff;\r
+ if( sgnflg )\r
+ goto error;\r
+ ++sgnflg;\r
+ break;\r
+ case '+':\r
+ if( sgnflg )\r
+ goto error;\r
+ ++sgnflg;\r
+ break;\r
+ case ',':\r
+ case ' ':\r
+ case '\0':\r
+ case '\n':\r
+ case '\r':\r
+ goto daldone;\r
+ case 'i':\r
+ case 'I':\r
+ goto infinite;\r
+ default:\r
+ error:\r
+#ifdef NANS\r
+ enan( yy, NI*16 );\r
+#else\r
+ mtherr( "asctoe", DOMAIN );\r
+ ecleaz(yy);\r
+#endif\r
+ goto aexit;\r
+ }\r
+donchr:\r
+++s;\r
+goto nxtcom;\r
+\r
+/* Exponent interpretation */\r
+expnt:\r
+\r
+esign = 1;\r
+exp = 0;\r
+++s;\r
+/* check for + or - */\r
+if( *s == '-' )\r
+ {\r
+ esign = -1;\r
+ ++s;\r
+ }\r
+if( *s == '+' )\r
+ ++s;\r
+while( (*s >= '0') && (*s <= '9') )\r
+ {\r
+ exp *= 10;\r
+ exp += *s++ - '0';\r
+ if (exp > 4977)\r
+ {\r
+ if (esign < 0)\r
+ goto zero;\r
+ else\r
+ goto infinite;\r
+ }\r
+ }\r
+if( esign < 0 )\r
+ exp = -exp;\r
+if( exp > 4932 )\r
+ {\r
+infinite:\r
+ ecleaz(yy);\r
+ yy[E] = 0x7fff; /* infinity */\r
+ goto aexit;\r
+ }\r
+if( exp < -4977 )\r
+ {\r
+zero:\r
+ ecleaz(yy);\r
+ goto aexit;\r
+ }\r
+\r
+daldone:\r
+nexp = exp - nexp;\r
+/* Pad trailing zeros to minimize power of 10, per IEEE spec. */\r
+while( (nexp > 0) && (yy[2] == 0) )\r
+ {\r
+ emovz( yy, xt );\r
+ eshup1( xt );\r
+ eshup1( xt );\r
+ eaddm( yy, xt );\r
+ eshup1( xt );\r
+ if( xt[2] != 0 )\r
+ break;\r
+ nexp -= 1;\r
+ emovz( xt, yy );\r
+ }\r
+if( (k = enormlz(yy)) > NBITS )\r
+ {\r
+ ecleaz(yy);\r
+ goto aexit;\r
+ }\r
+lexp = (EXONE - 1 + NBITS) - k;\r
+emdnorm( yy, lost, 0, lexp, 64 );\r
+/* convert to external format */\r
+\r
+\r
+/* Multiply by 10**nexp. If precision is 64 bits,\r
+ * the maximum relative error incurred in forming 10**n\r
+ * for 0 <= n <= 324 is 8.2e-20, at 10**180.\r
+ * For 0 <= n <= 999, the peak relative error is 1.4e-19 at 10**947.\r
+ * For 0 >= n >= -999, it is -1.55e-19 at 10**-435.\r
+ */\r
+lexp = yy[E];\r
+if( nexp == 0 )\r
+ {\r
+ k = 0;\r
+ goto expdon;\r
+ }\r
+esign = 1;\r
+if( nexp < 0 )\r
+ {\r
+ nexp = -nexp;\r
+ esign = -1;\r
+ if( nexp > 4096 )\r
+ { /* Punt. Can't handle this without 2 divides. */\r
+ emovi( etens[0], tt );\r
+ lexp -= tt[E];\r
+ k = edivm( tt, yy );\r
+ lexp += EXONE;\r
+ nexp -= 4096;\r
+ }\r
+ }\r
+p = &etens[NTEN][0];\r
+emov( eone, xt );\r
+exp = 1;\r
+do\r
+ {\r
+ if( exp & nexp )\r
+ emul( p, xt, xt );\r
+ p -= NE;\r
+ exp = exp + exp;\r
+ }\r
+while( exp <= MAXP );\r
+\r
+emovi( xt, tt );\r
+if( esign < 0 )\r
+ {\r
+ lexp -= tt[E];\r
+ k = edivm( tt, yy );\r
+ lexp += EXONE;\r
+ }\r
+else\r
+ {\r
+ lexp += tt[E];\r
+ k = emulm( tt, yy );\r
+ lexp -= EXONE - 1;\r
+ }\r
+\r
+expdon:\r
+\r
+/* Round and convert directly to the destination type */\r
+if( oprec == 53 )\r
+ lexp -= EXONE - 0x3ff;\r
+else if( oprec == 24 )\r
+ lexp -= EXONE - 0177;\r
+#ifdef DEC\r
+else if( oprec == 56 )\r
+ lexp -= EXONE - 0201;\r
+#endif\r
+rndprc = oprec;\r
+emdnorm( yy, k, 0, lexp, 64 );\r
+\r
+aexit:\r
+\r
+rndprc = rndsav;\r
+yy[0] = nsign;\r
+switch( oprec )\r
+ {\r
+#ifdef DEC\r
+ case 56:\r
+ todec( yy, y ); /* see etodec.c */\r
+ break;\r
+#endif\r
+ case 53:\r
+ toe53( yy, y );\r
+ break;\r
+ case 24:\r
+ toe24( yy, y );\r
+ break;\r
+ case 64:\r
+ toe64( yy, y );\r
+ break;\r
+ case 113:\r
+ toe113( yy, y );\r
+ break;\r
+ case NBITS:\r
+ emovo( yy, y );\r
+ break;\r
+ }\r
+}\r
+\r
+\r
+ \r
+/* y = largest integer not greater than x\r
+ * (truncated toward minus infinity)\r
+ *\r
+ * unsigned short x[NE], y[NE]\r
+ *\r
+ * efloor( x, y );\r
+ */\r
+static unsigned short bmask[] = {\r
+0xffff,\r
+0xfffe,\r
+0xfffc,\r
+0xfff8,\r
+0xfff0,\r
+0xffe0,\r
+0xffc0,\r
+0xff80,\r
+0xff00,\r
+0xfe00,\r
+0xfc00,\r
+0xf800,\r
+0xf000,\r
+0xe000,\r
+0xc000,\r
+0x8000,\r
+0x0000,\r
+};\r
+\r
+void efloor( x, y )\r
+unsigned short x[], y[];\r
+{\r
+register unsigned short *p;\r
+int e, expon, i;\r
+unsigned short f[NE];\r
+\r
+emov( x, f ); /* leave in external format */\r
+expon = (int )f[NE-1];\r
+e = (expon & 0x7fff) - (EXONE - 1);\r
+if( e <= 0 )\r
+ {\r
+ eclear(y);\r
+ goto isitneg;\r
+ }\r
+/* number of bits to clear out */\r
+e = NBITS - e;\r
+emov( f, y );\r
+if( e <= 0 )\r
+ return;\r
+\r
+p = &y[0];\r
+while( e >= 16 )\r
+ {\r
+ *p++ = 0;\r
+ e -= 16;\r
+ }\r
+/* clear the remaining bits */\r
+*p &= bmask[e];\r
+/* truncate negatives toward minus infinity */\r
+isitneg:\r
+\r
+if( (unsigned short )expon & (unsigned short )0x8000 )\r
+ {\r
+ for( i=0; i<NE-1; i++ )\r
+ {\r
+ if( f[i] != y[i] )\r
+ {\r
+ esub( eone, y, y );\r
+ break;\r
+ }\r
+ }\r
+ }\r
+}\r
+\r
+\r
+/* unsigned short x[], s[];\r
+ * long *exp;\r
+ *\r
+ * efrexp( x, exp, s );\r
+ *\r
+ * Returns s and exp such that s * 2**exp = x and .5 <= s < 1.\r
+ * For example, 1.1 = 0.55 * 2**1\r
+ * Handles denormalized numbers properly using long integer exp.\r
+ */\r
+void efrexp( x, exp, s )\r
+unsigned short x[];\r
+long *exp;\r
+unsigned short s[];\r
+{\r
+unsigned short xi[NI];\r
+long li;\r
+\r
+emovi( x, xi );\r
+li = (long )((short )xi[1]);\r
+\r
+if( li == 0 )\r
+ {\r
+ li -= enormlz( xi );\r
+ }\r
+xi[1] = 0x3ffe;\r
+emovo( xi, s );\r
+*exp = li - 0x3ffe;\r
+}\r
+\r
+\r
+\r
+/* unsigned short x[], y[];\r
+ * long pwr2;\r
+ *\r
+ * eldexp( x, pwr2, y );\r
+ *\r
+ * Returns y = x * 2**pwr2.\r
+ */\r
+void eldexp( x, pwr2, y )\r
+unsigned short x[];\r
+long pwr2;\r
+unsigned short y[];\r
+{\r
+unsigned short xi[NI];\r
+long li;\r
+int i;\r
+\r
+emovi( x, xi );\r
+li = xi[1];\r
+li += pwr2;\r
+i = 0;\r
+emdnorm( xi, i, i, li, 64 );\r
+emovo( xi, y );\r
+}\r
+\r
+\r
+/* c = remainder after dividing b by a\r
+ * Least significant integer quotient bits left in equot[].\r
+ */\r
+void eremain( a, b, c )\r
+unsigned short a[], b[], c[];\r
+{\r
+unsigned short den[NI], num[NI];\r
+\r
+#ifdef NANS\r
+if( eisinf(b) || (ecmp(a,ezero) == 0) || eisnan(a) || eisnan(b))\r
+ {\r
+ enan( c, NBITS );\r
+ return;\r
+ }\r
+#endif\r
+if( ecmp(a,ezero) == 0 )\r
+ {\r
+ mtherr( "eremain", SING );\r
+ eclear( c );\r
+ return;\r
+ }\r
+emovi( a, den );\r
+emovi( b, num );\r
+eiremain( den, num );\r
+/* Sign of remainder = sign of quotient */\r
+if( a[0] == b[0] )\r
+ num[0] = 0;\r
+else\r
+ num[0] = 0xffff;\r
+emovo( num, c );\r
+}\r
+\r
+\r
+void eiremain( den, num )\r
+unsigned short den[], num[];\r
+{\r
+long ld, ln;\r
+unsigned short j;\r
+\r
+ld = den[E];\r
+ld -= enormlz( den );\r
+ln = num[E];\r
+ln -= enormlz( num );\r
+ecleaz( equot );\r
+while( ln >= ld )\r
+ {\r
+ if( ecmpm(den,num) <= 0 )\r
+ {\r
+ esubm(den, num);\r
+ j = 1;\r
+ }\r
+ else\r
+ {\r
+ j = 0;\r
+ }\r
+ eshup1(equot);\r
+ equot[NI-1] |= j;\r
+ eshup1(num);\r
+ ln -= 1;\r
+ }\r
+emdnorm( num, 0, 0, ln, 0 );\r
+}\r
+\r
+/* NaN bit patterns\r
+ */\r
+#ifdef MIEEE\r
+unsigned short nan113[8] = {\r
+ 0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};\r
+unsigned short nan64[6] = {0x7fff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff};\r
+unsigned short nan53[4] = {0x7fff, 0xffff, 0xffff, 0xffff};\r
+unsigned short nan24[2] = {0x7fff, 0xffff};\r
+#endif\r
+\r
+#ifdef IBMPC\r
+unsigned short nan113[8] = {0, 0, 0, 0, 0, 0, 0xc000, 0xffff};\r
+unsigned short nan64[6] = {0, 0, 0, 0xc000, 0xffff, 0};\r
+unsigned short nan53[4] = {0, 0, 0, 0xfff8};\r
+unsigned short nan24[2] = {0, 0xffc0};\r
+#endif\r
+\r
+\r
+void enan (nan, size)\r
+unsigned short *nan;\r
+int size;\r
+{\r
+int i, n;\r
+unsigned short *p;\r
+\r
+switch( size )\r
+ {\r
+#ifndef DEC\r
+ case 113:\r
+ n = 8;\r
+ p = nan113;\r
+ break;\r
+\r
+ case 64:\r
+ n = 6;\r
+ p = nan64;\r
+ break;\r
+\r
+ case 53:\r
+ n = 4;\r
+ p = nan53;\r
+ break;\r
+\r
+ case 24:\r
+ n = 2;\r
+ p = nan24;\r
+ break;\r
+\r
+ case NBITS:\r
+ for( i=0; i<NE-2; i++ )\r
+ *nan++ = 0;\r
+ *nan++ = 0xc000;\r
+ *nan++ = 0x7fff;\r
+ return;\r
+\r
+ case NI*16:\r
+ *nan++ = 0;\r
+ *nan++ = 0x7fff;\r
+ *nan++ = 0;\r
+ *nan++ = 0xc000;\r
+ for( i=4; i<NI; i++ )\r
+ *nan++ = 0;\r
+ return;\r
+#endif\r
+ default:\r
+ mtherr( "enan", DOMAIN );\r
+ return;\r
+ }\r
+for (i=0; i < n; i++)\r
+ *nan++ = *p++;\r
+}\r
+\r
+\r
+\r
+/* Longhand square root. */\r
+\r
+static int esqinited = 0;\r
+static unsigned short sqrndbit[NI];\r
+\r
+void esqrt( x, y )\r
+short *x, *y;\r
+{\r
+unsigned short temp[NI], num[NI], sq[NI], xx[NI];\r
+int i, j, k, n, nlups;\r
+long m, exp;\r
+\r
+if( esqinited == 0 )\r
+ {\r
+ ecleaz( sqrndbit );\r
+ sqrndbit[NI-2] = 1;\r
+ esqinited = 1;\r
+ }\r
+/* Check for arg <= 0 */\r
+i = ecmp( x, ezero );\r
+if( i <= 0 )\r
+ {\r
+#ifdef NANS\r
+ if (i == -2)\r
+ {\r
+ enan (y, NBITS);\r
+ return;\r
+ }\r
+#endif\r
+ eclear(y);\r
+ if( i < 0 )\r
+ mtherr( "esqrt", DOMAIN );\r
+ return;\r
+ }\r
+\r
+#ifdef INFINITY\r
+if( eisinf(x) )\r
+ {\r
+ eclear(y);\r
+ einfin(y);\r
+ return;\r
+ }\r
+#endif\r
+/* Bring in the arg and renormalize if it is denormal. */\r
+emovi( x, xx );\r
+m = (long )xx[1]; /* local long word exponent */\r
+if( m == 0 )\r
+ m -= enormlz( xx );\r
+\r
+/* Divide exponent by 2 */\r
+m -= 0x3ffe;\r
+exp = (unsigned short )( (m / 2) + 0x3ffe );\r
+\r
+/* Adjust if exponent odd */\r
+if( (m & 1) != 0 )\r
+ {\r
+ if( m > 0 )\r
+ exp += 1;\r
+ eshdn1( xx );\r
+ }\r
+\r
+ecleaz( sq );\r
+ecleaz( num );\r
+n = 8; /* get 8 bits of result per inner loop */\r
+nlups = rndprc;\r
+j = 0;\r
+\r
+while( nlups > 0 )\r
+ {\r
+/* bring in next word of arg */\r
+ if( j < NE )\r
+ num[NI-1] = xx[j+3];\r
+/* Do additional bit on last outer loop, for roundoff. */\r
+ if( nlups <= 8 )\r
+ n = nlups + 1;\r
+ for( i=0; i<n; i++ )\r
+ {\r
+/* Next 2 bits of arg */\r
+ eshup1( num );\r
+ eshup1( num );\r
+/* Shift up answer */\r
+ eshup1( sq );\r
+/* Make trial divisor */\r
+ for( k=0; k<NI; k++ )\r
+ temp[k] = sq[k];\r
+ eshup1( temp );\r
+ eaddm( sqrndbit, temp );\r
+/* Subtract and insert answer bit if it goes in */\r
+ if( ecmpm( temp, num ) <= 0 )\r
+ {\r
+ esubm( temp, num );\r
+ sq[NI-2] |= 1;\r
+ }\r
+ }\r
+ nlups -= n;\r
+ j += 1;\r
+ }\r
+\r
+/* Adjust for extra, roundoff loop done. */\r
+exp += (NBITS - 1) - rndprc;\r
+\r
+/* Sticky bit = 1 if the remainder is nonzero. */\r
+k = 0;\r
+for( i=3; i<NI; i++ )\r
+ k |= (int )num[i];\r
+\r
+/* Renormalize and round off. */\r
+emdnorm( sq, k, 0, exp, 64 );\r
+emovo( sq, y );\r
+}\r
--- /dev/null
+/* Floating point to ASCII input and output string test program.\r
+ *\r
+ * Numbers in the native machine data structure are converted\r
+ * to e type, then to and from decimal ASCII strings. Native\r
+ * printf() and scanf() functions are also used to produce\r
+ * and read strings. The resulting e type binary values\r
+ * are compared, with diagnostic printouts of any discrepancies.\r
+ *\r
+ * Steve Moshier, 16 Dec 88\r
+ * last revision: 16 May 92\r
+ */\r
+\r
+#include "ehead.h"\r
+#include "mconf.h"\r
+\r
+/* Include tests of 80-bit long double precision: */\r
+#define LDOUBLE 0\r
+/* Abort subtest after getting this many errors: */\r
+#define MAXERR 5\r
+/* Number of random arguments to try (set as large as you have\r
+ * patience for): */\r
+#define NRAND 100\r
+/* Perform internal consistency test: */\r
+#define CHKINTERNAL 0\r
+\r
+static unsigned short fullp[NE], rounded[NE];\r
+float prec24, sprec24, ssprec24;\r
+double prec53, sprec53, ssprec53;\r
+#if LDOUBLE\r
+long double prec64, sprec64, ssprec64;\r
+#endif\r
+\r
+static unsigned short rprint[NE], rscan[NE];\r
+static unsigned short q1[NE], q2[NE], q5[NE];\r
+static unsigned short e1[NE], e2[NE], e3[NE];\r
+static double d1, d2;\r
+static int errprint = 0;\r
+static int errscan = 0;\r
+static int identerr = 0;\r
+static int errtot = 0;\r
+static int count = 0;\r
+static char str0[80], str1[80], str2[80], str3[80];\r
+static unsigned short eten[NE], maxm[NE];\r
+\r
+int m, n, k2, mprec, SPREC;\r
+\r
+char *Ten = "10.0";\r
+char tformat[10];\r
+char *format24 = "%.8e";\r
+#ifdef DEC\r
+char *format53 = "%.17e";\r
+#else\r
+char *format53 = "%.16e";\r
+#endif\r
+char *fformat24 = "%e";\r
+char *fformat53 = "%le";\r
+char *pct = "%";\r
+char *quo = "\042";\r
+#if LDOUBLE\r
+char *format64 = "%.20Le";\r
+char *fformat64 = "%Le";\r
+#endif\r
+char *format;\r
+char *fformat;\r
+char *toomany = "Too many errors; aborting this test.\n";\r
+\r
+static int mnrflag;\r
+static int etrflag;\r
+void chkit(), printerr(), mnrand(), etrand(), shownoncrit();\r
+void chkid(), pvec();\r
+\r
+main()\r
+{\r
+int i, iprec;\r
+\r
+printf( "Steve Moshier's printf/scanf tester, version 0.2.\n\n" );\r
+#ifdef DEC\r
+ /* DEC PDP-11/VAX single precision not yet implemented */\r
+for( iprec = 1; iprec<2; iprec++ )\r
+#else\r
+for( iprec = 0; iprec<3; iprec++ )\r
+#endif\r
+ {\r
+ errscan = 0;\r
+ identerr = 0;\r
+ errprint = 0;\r
+ eclear( rprint );\r
+ eclear( rscan );\r
+\r
+switch( iprec )\r
+ {\r
+ case 0:\r
+ SPREC = 8; /* # digits after the decimal point */\r
+ mprec = 24; /* # bits in the significand */\r
+ m = 9; /* max # decimal digits for correct rounding */\r
+ n = 13; /* max power of ten for correct rounding */\r
+ k2 = -125; /* underflow beyond 2^-k2 */\r
+ format = format24; /* printf format string */\r
+ fformat = fformat24; /* scanf format string */\r
+ mnrflag = 1; /* sets interval for random numbers */\r
+ etrflag = 1;\r
+ printf( "Testing FLOAT precision.\n" );\r
+ break;\r
+\r
+ case 1:\r
+#ifdef DEC\r
+ SPREC = 17;\r
+ mprec = 56;\r
+ m = 17;\r
+ n = 27;\r
+ k2 = -125;\r
+ format = format53;\r
+ fformat = fformat53;\r
+ mnrflag = 2;\r
+ etrflag = 1;\r
+ printf( "Testing DEC DOUBLE precision.\n" );\r
+ break;\r
+#else\r
+ SPREC = 16;\r
+ mprec = 53;\r
+ m = 17;\r
+ n = 27;\r
+ k2 = -1021;\r
+ format = format53;\r
+ fformat = fformat53;\r
+ mnrflag = 2;\r
+ etrflag = 2;\r
+ printf( "Testing DOUBLE precision.\n" );\r
+ break;\r
+#endif\r
+ case 2:\r
+#if LDOUBLE\r
+ SPREC = 20;\r
+ mprec = 64;\r
+ m = 20;\r
+ n = 34;\r
+ k2 = -16382;\r
+ format = format64;\r
+ fformat = fformat64;\r
+ mnrflag = 3;\r
+ etrflag = 3;\r
+ printf( "Testing LONG DOUBLE precision.\n" );\r
+ break;\r
+#else\r
+ goto nodenorm;\r
+#endif\r
+ }\r
+\r
+ asctoe( Ten, eten );\r
+/* 10^m - 1 */\r
+ d2 = m;\r
+ e53toe( &d2, e1 );\r
+ epow( eten, e1, maxm );\r
+ esub( eone, maxm, maxm );\r
+\r
+/* test 1 */\r
+ printf( "1. Checking 10^n - 1 for n = %d to %d.\n", -m, m );\r
+ emov( eone, q5 );\r
+ for( count=0; count<=m; count++ )\r
+ {\r
+ esub( eone, q5, fullp );\r
+ chkit( 1 );\r
+ ediv( q5, eone, q2 );\r
+ esub( eone, q2, fullp );\r
+ chkit( 1 );\r
+ emul( eten, q5, q5 );\r
+ if( errtot >= MAXERR )\r
+ {\r
+ printf( "%s", toomany );\r
+ goto end1;\r
+ }\r
+ }\r
+end1:\r
+ printerr();\r
+\r
+\r
+/* test 2 */\r
+ printf( "2. Checking powers of 10 from 10^-%d to 10^%d.\n", n, n );\r
+ emov( eone, q5 );\r
+ for( count=0; count<=n; count++ )\r
+ {\r
+ emov( q5, fullp );\r
+ chkit( 2 );\r
+ ediv( q5, eone, fullp );\r
+ chkit( 2 );\r
+ emul( eten, q5, q5 );\r
+ if( errtot >= MAXERR )\r
+ {\r
+ printf( "%s", toomany );\r
+ goto end2;\r
+ }\r
+ }\r
+end2:\r
+ printerr();\r
+\r
+/* test 3 */\r
+ printf( "3. Checking (10^%d-1)*10^n from n = -%d to %d.\n", m, n, n );\r
+ emov( eone, q5 );\r
+ for( count= -n; count<=n; count++ )\r
+ {\r
+ emul( maxm, q5, fullp );\r
+ chkit( 3 );\r
+ emov( q5, fullp );\r
+ ediv( fullp, eone, fullp );\r
+ emul( maxm, fullp, fullp );\r
+ chkit( 3 );\r
+ emul( eten, q5, q5 );\r
+ if( errtot >= MAXERR )\r
+ {\r
+ printf( "%s", toomany );\r
+ goto end3;\r
+ }\r
+ }\r
+end3:\r
+ printerr();\r
+\r
+\r
+\r
+/* test 4 */\r
+ printf( "4. Checking powers of 2 from 2^-24 to 2^+56.\n" );\r
+ d1 = -24.0;\r
+ e53toe( &d1, q1 );\r
+ epow( etwo, q1, q5 );\r
+\r
+ for( count = -24; count <= 56; count++ )\r
+ {\r
+ emov( q5, fullp );\r
+ chkit( 4 );\r
+ emul( etwo, q5, q5 );\r
+ if( errtot >= MAXERR )\r
+ {\r
+ printf( "%s", toomany );\r
+ goto end4;\r
+ }\r
+ }\r
+end4:\r
+ printerr();\r
+\r
+\r
+/* test 5 */\r
+ printf( "5. Checking 2^n - 1 for n = 0 to %d.\n", mprec );\r
+ emov( eone, q5 );\r
+ for( count=0; count<=mprec; count++ )\r
+ {\r
+ esub( eone, q5, fullp );\r
+ chkit( 5 );\r
+ emul( etwo, q5, q5 );\r
+ if( errtot >= MAXERR )\r
+ {\r
+ printf( "%s", toomany );\r
+ goto end5;\r
+ }\r
+ }\r
+end5:\r
+ printerr();\r
+\r
+/* test 6 */\r
+ printf( "6. Checking 2^n + 1 for n = 0 to %d.\n", mprec );\r
+ emov( eone, q5 );\r
+ for( count=0; count<=mprec; count++ )\r
+ {\r
+ eadd( eone, q5, fullp );\r
+ chkit( 6 );\r
+ emul( etwo, q5, q5 );\r
+ if( errtot >= MAXERR )\r
+ {\r
+ printf( "%s", toomany );\r
+ goto end6;\r
+ }\r
+ }\r
+end6:\r
+ printerr();\r
+\r
+/* test 7 */\r
+ printf(\r
+ "7. Checking %d values M * 10^N with random integer M and N,\n",\r
+ NRAND );\r
+ printf(" 1 <= M <= 10^%d - 1 and -%d <= N <= +%d.\n", m, n, n );\r
+ for( i=0; i<NRAND; i++ )\r
+ {\r
+ mnrand( fullp );\r
+ chkit( 7 );\r
+ if( errtot >= MAXERR )\r
+ {\r
+ printf( "%s", toomany );\r
+ goto end7;\r
+ }\r
+ }\r
+end7:\r
+ printerr();\r
+\r
+/* test 8 */\r
+ printf("8. Checking critical rounding cases.\n" );\r
+ for( i=0; i<20; i++ )\r
+ {\r
+ mnrand( fullp );\r
+ eabs( fullp );\r
+ if( ecmp( fullp, eone ) < 0 )\r
+ ediv( fullp, eone, fullp );\r
+ efloor( fullp, fullp );\r
+ eadd( ehalf, fullp, fullp );\r
+ chkit( 8 );\r
+ if( errtot >= MAXERR )\r
+ {\r
+ printf( "%s", toomany );\r
+ goto end8;\r
+ }\r
+ }\r
+end8:\r
+ printerr();\r
+\r
+\r
+\r
+/* test 9 */\r
+ printf("9. Testing on %d random non-denormal values.\n", NRAND );\r
+ for( i=0; i<NRAND; i++ )\r
+ {\r
+ etrand( fullp );\r
+ chkit( 9 );\r
+ }\r
+ printerr();\r
+ shownoncrit();\r
+\r
+/* test 10 */\r
+ printf(\r
+ "Do you want to check denormal numbers in this precision ? (y/n) " );\r
+ gets( str0 );\r
+ if( str0[0] != 'y' )\r
+ goto nodenorm;\r
+\r
+ printf( "10. Checking denormal numbers.\n" );\r
+\r
+/* Form 2^-starting power */\r
+ d1 = k2;\r
+ e53toe( &d1, q1 );\r
+ epow( etwo, q1, e1 );\r
+\r
+/* Find 2^-mprec less than starting power */\r
+ d1 = -mprec + 4;\r
+ e53toe( &d1, q1 );\r
+ epow( etwo, q1, e3 );\r
+ emul( e1, e3, e3 );\r
+ emov( e3, e2 );\r
+ ediv( etwo, e2, e2 );\r
+\r
+ while( ecmp(e1,e2) != 0 )\r
+ {\r
+ eadd( e1, e2, fullp );\r
+ switch( mprec )\r
+ {\r
+#if LDOUBLE\r
+ case 64:\r
+ etoe64( e1, &sprec64 );\r
+ e64toe( &sprec64, q1 );\r
+ etoe64( fullp, &prec64 );\r
+ e64toe( &prec64, q2 );\r
+ break;\r
+#endif\r
+#ifdef DEC\r
+ case 56:\r
+#endif\r
+ case 53:\r
+ etoe53( e1, &sprec53 );\r
+ e53toe( &sprec53, q1 );\r
+ etoe53( fullp, &prec53 );\r
+ e53toe( &prec53, q2 );\r
+ break;\r
+\r
+ case 24:\r
+ etoe24( e1, &sprec24 );\r
+ e24toe( &sprec24, q1 );\r
+ etoe24( fullp, &prec24 );\r
+ e24toe( &prec24, q2 );\r
+ break;\r
+ }\r
+ if( ecmp( q2, ezero ) == 0 )\r
+ goto maxden;\r
+ chkit(10);\r
+ if( ecmp(q1,q2) == 0 )\r
+ {\r
+ ediv( etwo, e1, e1 );\r
+ emov( e3, e2 );\r
+ }\r
+ if( errtot >= MAXERR )\r
+ {\r
+ printf( "%s", toomany );\r
+ goto maxden;\r
+ }\r
+ ediv( etwo, e2, e2 );\r
+ }\r
+maxden:\r
+ printerr();\r
+nodenorm:\r
+ printf( "\n" );\r
+ } /* loop on precision */\r
+printf( "End of test.\n" );\r
+}\r
+\r
+#if CHKINTERNAL\r
+long double xprec64;\r
+double xprec53;\r
+float xprec24;\r
+\r
+/* Check binary -> printf -> scanf -> binary identity\r
+ * of internal routines\r
+ */\r
+void chkinternal( ref, tst, string )\r
+unsigned short ref[], tst[];\r
+char *string;\r
+{\r
+\r
+if( ecmp(ref,tst) != 0 )\r
+ {\r
+ printf( "internal identity compare error!\n" );\r
+ chkid( ref, tst, string );\r
+ }\r
+}\r
+#endif\r
+\r
+\r
+/* Check binary -> printf -> scanf -> binary identity\r
+ */\r
+void chkid( print, scan, string )\r
+unsigned short print[], scan[];\r
+char *string;\r
+{\r
+/* Test printf-scanf identity */\r
+if( ecmp( print, scan ) != 0 )\r
+ {\r
+ pvec( print, NE );\r
+ printf( " ->printf-> %s ->scanf->\n", string );\r
+ pvec( scan, NE );\r
+ printf( " is not an identity.\n" );\r
+ ++identerr;\r
+ }\r
+}\r
+\r
+\r
+/* Check scanf result\r
+ */\r
+void chkscan( ref, tst, string )\r
+unsigned short ref[], tst[];\r
+char *string;\r
+{\r
+/* Test scanf() */\r
+if( ecmp( ref, tst ) != 0 )\r
+ {\r
+ printf( "scanf(%s) -> ", string );\r
+ pvec( tst, NE );\r
+ printf( "\n should be " );\r
+ pvec( ref, NE );\r
+ printf( ".\n" );\r
+ ++errscan;\r
+ ++errtot;\r
+ }\r
+}\r
+\r
+\r
+/* Test printf() result\r
+ */\r
+void chkprint( ref, tst, string ) \r
+unsigned short ref[], tst[];\r
+char *string;\r
+{\r
+if( ecmp(ref, tst) != 0 )\r
+ {\r
+ printf( "printf( ");\r
+ pvec( ref, NE );\r
+ printf( ") -> %s\n", string );\r
+ printf( " = " );\r
+ pvec( tst, NE );\r
+ printf( ".\n" );\r
+ ++errprint;\r
+ ++errtot;\r
+ }\r
+}\r
+\r
+\r
+/* Print array of n 16-bit shorts\r
+ */\r
+void pvec( x, n )\r
+unsigned short x[];\r
+int n;\r
+{\r
+int i;\r
+\r
+for( i=0; i<n; i++ )\r
+ {\r
+ printf( "%04x ", x[i] );\r
+ }\r
+}\r
+\r
+/* Measure worst case printf rounding error\r
+ */\r
+void cmpprint( ref, tst )\r
+unsigned short ref[], tst[];\r
+{\r
+unsigned short e[NE];\r
+\r
+if( ecmp( ref, ezero ) != 0 )\r
+ {\r
+ esub( ref, tst, e );\r
+ ediv( ref, e, e );\r
+ eabs( e );\r
+ if( ecmp( e, rprint ) > 0 )\r
+ emov( e, rprint );\r
+ }\r
+}\r
+\r
+/* Measure worst case scanf rounding error\r
+ */\r
+void cmpscan( ref, tst )\r
+unsigned short ref[], tst[];\r
+{\r
+unsigned short er[NE];\r
+\r
+if( ecmp( ref, ezero ) != 0 )\r
+ {\r
+ esub( ref, tst, er );\r
+ ediv( ref, er, er );\r
+ eabs( er );\r
+ if( ecmp( er, rscan ) > 0 )\r
+ emov( er, rscan );\r
+ if( ecmp( er, ehalf ) > 0 )\r
+ {\r
+ etoasc( tst, str1, 21 );\r
+ printf( "Bad error: scanf(%s) = %s !\n", str0, str1 );\r
+ }\r
+ }\r
+}\r
+\r
+/* Check rounded-down decimal string output of printf\r
+ */\r
+void cmptrunc( ref, tst )\r
+unsigned short ref[], tst[];\r
+{\r
+if( ecmp( ref, tst ) != 0 )\r
+ {\r
+ printf( "printf(%s%s%s, %s) -> %s\n", quo, tformat, quo, str1, str2 );\r
+ printf( "should be %s .\n", str3 );\r
+ errprint += 1;\r
+ }\r
+}\r
+\r
+\r
+void shownoncrit()\r
+{\r
+\r
+etoasc( rprint, str0, 3 );\r
+printf( "Maximum relative printf error found = %s .\n", str0 );\r
+etoasc( rscan, str0, 3 );\r
+printf( "Maximum relative scanf error found = %s .\n", str0 );\r
+}\r
+\r
+\r
+\r
+/* Produce arguments and call comparison subroutines.\r
+ */\r
+void chkit( testno )\r
+int testno;\r
+{\r
+unsigned short t[NE], u[NE], v[NE];\r
+int j;\r
+\r
+switch( mprec )\r
+ {\r
+#if LDOUBLE\r
+ case 64:\r
+ etoe64( fullp, &prec64 );\r
+ e64toe( &prec64, rounded );\r
+#if CHKINTERNAL\r
+ e64toasc( &prec64, str1, SPREC );\r
+ asctoe64( str1, &xprec64 );\r
+ e64toe( &xprec64, t );\r
+ chkinternal( rounded, t, str1 );\r
+#endif\r
+/* check printf and scanf */\r
+ sprintf( str2, format, prec64 );\r
+ sscanf( str2, fformat, &sprec64 );\r
+ e64toe( &sprec64, u );\r
+ chkid( rounded, u, str2 );\r
+ asctoe64( str2, &ssprec64 );\r
+ e64toe( &ssprec64, v );\r
+ chkscan( v, u, str2 );\r
+ chkprint( rounded, v, str2 );\r
+ if( testno < 8 )\r
+ break;\r
+/* rounding error measurement */\r
+ etoasc( fullp, str0, 24 );\r
+ etoe64( fullp, &ssprec64 );\r
+ e64toe( &ssprec64, u );\r
+ sprintf( str2, format, ssprec64 );\r
+ asctoe( str2, t );\r
+ cmpprint( u, t );\r
+ sscanf( str0, fformat, &sprec64 );\r
+ e64toe( &sprec64, t );\r
+ cmpscan( fullp, t );\r
+ if( testno < 8 )\r
+ break;\r
+/* strings rounded to less than maximum precision */\r
+ e64toasc( &ssprec64, str1, 24 );\r
+ for( j=SPREC-1; j>0; j-- ) \r
+ {\r
+ e64toasc( &ssprec64, str3, j );\r
+ asctoe( str3, v );\r
+ sprintf( tformat, "%s.%dLe", pct, j );\r
+ sprintf( str2, tformat, ssprec64 );\r
+ asctoe( str2, t );\r
+ cmptrunc( v, t );\r
+ }\r
+ break;\r
+#endif\r
+#ifdef DEC\r
+ case 56:\r
+#endif\r
+ case 53:\r
+ etoe53( fullp, &prec53 );\r
+ e53toe( &prec53, rounded );\r
+#if CHKINTERNAL\r
+ e53toasc( &prec53, str1, SPREC );\r
+ asctoe53( str1, &xprec53 );\r
+ e53toe( &xprec53, t );\r
+ chkinternal( rounded, t, str1 );\r
+#endif\r
+ sprintf( str2, format, prec53 );\r
+ sscanf( str2, fformat, &sprec53 );\r
+ e53toe( &sprec53, u );\r
+ chkid( rounded, u, str2 );\r
+ asctoe53( str2, &ssprec53 );\r
+ e53toe( &ssprec53, v );\r
+ chkscan( v, u, str2 );\r
+ chkprint( rounded, v, str2 );\r
+ if( testno < 8 )\r
+ break;\r
+/* rounding error measurement */\r
+ etoasc( fullp, str0, 24 );\r
+ etoe53( fullp, &ssprec53 );\r
+ e53toe( &ssprec53, u );\r
+ sprintf( str2, format, ssprec53 );\r
+ asctoe( str2, t );\r
+ cmpprint( u, t );\r
+ sscanf( str0, fformat, &sprec53 );\r
+ e53toe( &sprec53, t );\r
+ cmpscan( fullp, t );\r
+ if( testno < 8 )\r
+ break;\r
+ e53toasc( &ssprec53, str1, 24 );\r
+ for( j=SPREC-1; j>0; j-- ) \r
+ {\r
+ e53toasc( &ssprec53, str3, j );\r
+ asctoe( str3, v );\r
+ sprintf( tformat, "%s.%de", pct, j );\r
+ sprintf( str2, tformat, ssprec53 );\r
+ asctoe( str2, t );\r
+ cmptrunc( v, t );\r
+ }\r
+ break;\r
+\r
+ case 24:\r
+ etoe24( fullp, &prec24 );\r
+ e24toe( &prec24, rounded );\r
+#if CHKINTERNAL\r
+ e24toasc( &prec24, str1, SPREC );\r
+ asctoe24( str1, &xprec24 );\r
+ e24toe( &xprec24, t );\r
+ chkinternal( rounded, t, str1 );\r
+#endif\r
+ sprintf( str2, format, prec24 );\r
+ sscanf( str2, fformat, &sprec24 );\r
+ e24toe( &sprec24, u );\r
+ chkid( rounded, u, str2 );\r
+ asctoe24( str2, &ssprec24 );\r
+ e24toe( &ssprec24, v );\r
+ chkscan( v, u, str2 );\r
+ chkprint( rounded, v, str2 );\r
+ if( testno < 8 )\r
+ break;\r
+/* rounding error measurement */\r
+ etoasc( fullp, str0, 24 );\r
+ etoe24( fullp, &ssprec24 );\r
+ e24toe( &ssprec24, u );\r
+ sprintf( str2, format, ssprec24 );\r
+ asctoe( str2, t );\r
+ cmpprint( u, t );\r
+ sscanf( str0, fformat, &sprec24 );\r
+ e24toe( &sprec24, t );\r
+ cmpscan( fullp, t );\r
+/*\r
+ if( testno < 8 )\r
+ break;\r
+*/\r
+ e24toasc( &ssprec24, str1, 24 );\r
+ for( j=SPREC-1; j>0; j-- ) \r
+ {\r
+ e24toasc( &ssprec24, str3, j );\r
+ asctoe( str3, v );\r
+ sprintf( tformat, "%s.%de", pct, j );\r
+ sprintf( str2, tformat, ssprec24 );\r
+ asctoe( str2, t );\r
+ cmptrunc( v, t );\r
+ }\r
+ break;\r
+ }\r
+}\r
+\r
+\r
+void printerr()\r
+{\r
+if( (errscan == 0) && (identerr == 0) && (errprint == 0) )\r
+ printf( "No errors found.\n" );\r
+else\r
+ {\r
+ printf( "%d binary -> decimal errors found.\n", errprint );\r
+ printf( "%d decimal -> binary errors found.\n", errscan );\r
+ }\r
+errscan = 0; /* reset for next test */\r
+identerr = 0;\r
+errprint = 0;\r
+errtot = 0;\r
+}\r
+\r
+\r
+/* Random number generator\r
+ * in the range M * 10^N, where 1 <= M <= 10^17 - 1\r
+ * and -27 <= N <= +27. Test values of M are logarithmically distributed\r
+ * random integers; test values of N are uniformly distributed random integers.\r
+ */\r
+\r
+static char *fwidth = "1.036163291797320557783096e1"; /* log(sqrt(10^9-1)) */\r
+static char *dwidth = "1.957197329044938830915E1"; /* log(sqrt(10^17-1)) */\r
+static char *ldwidth = "2.302585092994045684017491e1"; /* log(sqrt(10^20-1)) */\r
+\r
+static char *a13 = "13.0";\r
+static char *a27 = "27.0";\r
+static char *a34 = "34.0";\r
+static char *a10m13 = "1.0e-13";\r
+static unsigned short LOW[ NE ], WIDTH[NE], e27[NE], e10m13[NE];\r
+\r
+\r
+void mnrand( erand )\r
+unsigned short erand[];\r
+{\r
+unsigned short ea[NE], em[NE], en[NE], ex[NE];\r
+double x, a;\r
+\r
+if( mnrflag )\r
+ {\r
+ if( mnrflag == 3 )\r
+ {\r
+ asctoe( ldwidth, WIDTH );\r
+ asctoe( a34, e27 );\r
+ }\r
+ if( mnrflag == 2 )\r
+ {\r
+ asctoe( dwidth, WIDTH );\r
+ asctoe( a27, e27 );\r
+ }\r
+ if( mnrflag == 1 )\r
+ {\r
+ asctoe( fwidth, WIDTH );\r
+ asctoe( a13, e27 );\r
+ }\r
+ asctoe( a10m13, e10m13 );\r
+ mnrflag = 0;\r
+ }\r
+drand( &x );\r
+e53toe( &x, ex ); /* x = WIDTH * ( x - 1.0 ) + LOW; */\r
+esub( eone, ex, ex );\r
+emul( WIDTH, ex, ex );\r
+eexp( ex, ex ); /* x = exp(x); */\r
+\r
+drand( &a );\r
+e53toe( &a, ea );\r
+emul( ea, ex, ea ); /* a = 1.0e-13 * x * a; */\r
+emul( e10m13, ea, ea );\r
+eabs( ea );\r
+eadd( ea, ex, ex ); /* add fuzz */\r
+emul( ex, ex, ex ); /* square it, to get range to 10^17 - 1 */\r
+efloor( ex, em ); /* this is M */\r
+\r
+/* Random power of 10 */\r
+drand( &a );\r
+e53toe( &a, ex );\r
+esub( eone, ex, ex ); /* y3 = 54.0 * ( y3 - 1.0 ) + 0.5; */\r
+emul( e27, ex, ex );\r
+eadd( ex, ex, ex );\r
+eadd( ehalf, ex, ex );\r
+efloor( ex, ex ); /* y3 = floor( y3 ) - 27.0; */\r
+esub( e27, ex, en ); /* this is N */\r
+epow( eten, en, ex );\r
+emul( ex, em, erand );\r
+}\r
+\r
+/* -ln 2^16382 */\r
+char *ldemin = "-1.1355137111933024058873097E4";\r
+char *ldewid = "2.2710274223866048117746193E4";\r
+/* -ln 2^1022 */\r
+char *demin = "-7.0839641853226410622441123E2";\r
+char *dewid = "1.4167928370645282124488225E3";\r
+/* -ln 2^125 */\r
+char *femin = "-8.6643397569993163677154015E1";\r
+char *fewid = "1.7328679513998632735430803E2";\r
+\r
+void etrand( erand )\r
+unsigned short erand[];\r
+{\r
+unsigned short ea[NE], ex[NE];\r
+double x, a;\r
+\r
+if( etrflag )\r
+ {\r
+ if( etrflag == 3 )\r
+ {\r
+ asctoe( ldemin, LOW );\r
+ asctoe( ldewid, WIDTH );\r
+ asctoe( a34, e27 );\r
+ }\r
+ if( etrflag == 2 )\r
+ {\r
+ asctoe( demin, LOW );\r
+ asctoe( dewid, WIDTH );\r
+ asctoe( a27, e27 );\r
+ }\r
+ if( etrflag == 1 )\r
+ {\r
+ asctoe( femin, LOW );\r
+ asctoe( fewid, WIDTH );\r
+ asctoe( a13, e27 );\r
+ }\r
+ asctoe( a10m13, e10m13 );\r
+ etrflag = 0;\r
+ }\r
+drand( &x );\r
+e53toe( &x, ex ); /* x = WIDTH * ( x - 1.0 ) + LOW; */\r
+esub( eone, ex, ex );\r
+emul( WIDTH, ex, ex );\r
+eadd( LOW, ex, ex );\r
+eexp( ex, ex ); /* x = exp(x); */\r
+\r
+/* add fuzz\r
+ */\r
+drand( &a );\r
+e53toe( &a, ea );\r
+emul( ea, ex, ea ); /* a = 1.0e-13 * x * a; */\r
+emul( e10m13, ea, ea );\r
+if( ecmp( ex, ezero ) > 0 )\r
+ eneg( ea );\r
+eadd( ea, ex, erand );\r
+}\r
+\r
--- /dev/null
+\r
+ ieetst, version 0.2\r
+\r
+ This software tests the numerical accuracy of floating point\r
+binary <-> decimal string conversion, as done by your C language\r
+library functions printf() and scanf(), for compliance with the\r
+IEEE arithmetic standards ANSI/IEEE Std 754-1985 and ANSI/IEEE\r
+Std 854-1987. The test covers 32-bit float, 64-bit double, and\r
+80-bit long double precision conversions to and from decimal\r
+ASCII strings.\r
+\r
+ The test program checks for proper implementation of the\r
+following specifications of the standards:\r
+\r
+ (1) correctly rounded conversions of numbers of the form M *\r
+ 10^N, where M and N are integers such that, in double precision,\r
+ for example, |M| < 10^17, |N| <= 27.\r
+\r
+ (2) binary -> decimal -> binary conversions to be an identity\r
+ if a sufficiently large number of decimal digits is requested.\r
+\r
+ (3) correctly rounded decimal outputs of less than the maximum\r
+ number of digits\r
+\r
+ (4) The maximum observed conversion error of numbers outside the\r
+ domain covered by (1) is reported by the test program; it is\r
+ not supposed to exceed 0.97 ulp.\r
+\r
+There are 10 separate tests. Tests 1 through 6 use values near\r
+2^n and 10^n. Test 7 addresses item (1) above. Test 8 checks\r
+the rounding of exact half-integer numbers. Test 9 is for item\r
+(4) above. Test 10 checks denormal numbers. Tests 8 through 10\r
+address item (3) using printf formats that produce outputs of 1,\r
+2, 3, ... digits after the decimal point. All tests check, when\r
+appropriate, that the binary output of scanf is the same as the\r
+binary input to printf, item (2).\r
+\r
+Example error messages:\r
+\r
+ 0000 0000 0000 1000 8000 3f80 ->printf-> 5.87748296e-39 ->scanf->\r
+ 0000 0000 0000 0000 8000 3f6e is not an identity.\r
+\r
+ scanf(-9.9999900000000003e-01) -> 0000 4800 085f ef39 ffff bffe \r
+ should be 0000 5000 085f ef39 ffff bffe .\r
+\r
+ printf("%.14e", 6.13592315154256467968352E-3) -> 6.13592315154257e-03\r
+ should be 6.13592315154256E-3 .\r
+\r
+Binary values are displayed as four-digit hex groups in the\r
+little-endian format of the internal reference arithmetic. The\r
+least significant 16-bit word is first, the exponent is last.\r
+\r
+ The design of the test program requires knowing the binary\r
+data structure of the floating point format under test. For\r
+configuration, check the .h files carefully. All the programs\r
+need to be told via mconf.h if the numeric format is\r
+little-endian (IBMPC) or big-endian (MIEEE). If your system\r
+supports an 80-bit long double precision data type, define\r
+LDOUBLE 1 in ieetst.c; otherwise define LDOUBLE 0. A provision\r
+for DEC PDP-11/VAX numbers is implemented (double precision\r
+only). Conversions for other data structures can be added by\r
+analogy to the ifdefs for DEC.\r
+\r
+ Most of the tests rely on comparison with the results of a\r
+portable reference arithmetic, contained in the file ieee.c. \r
+This is configured for an 80-bit significand, to have enough\r
+precision to satisfy the conversion requirements of IEEE 854 for\r
+the extended double format of IEEE 754. The reference arithmetic\r
+includes binary <--> ASCII conversion routines and single <-->\r
+double <--> extended double conversions. A strictly rounded\r
+square root function is given in esqrt.c. Additional functions\r
+are provided by elog.c, eexp.c, etanh.c, epow.c, all of which\r
+call on ieee.c for their arithmetic. Some of the ANSI C\r
+functions are supplied in ieee.c; for example, efloor(),\r
+efrexp(), eldexp(). The functions and the reference arithmetic\r
+are described further in the book _Methods and Programs for\r
+Mathematical Functions_ (Prentice-Hall or Simon & Schuster\r
+International, 1989), by S. L. Moshier.\r
+\r
+ As an aid in diagnosis, a calculator program, ecalc.c, is\r
+supplied. It uses ieee.c for its arithmetic. Documentation for\r
+the calculator's user interface is in the file calc100.doc\r
+(calc100 is a fuller featured 100-digit version of ecalc). The\r
+calculator needs to be told by qcalc.h if addresses are 32 bits\r
+long (define LARGEMEM 1) or 16 bits long (define LARGEMEM 0).\r
+\r
+ Because the source code of ieee.c is included here, a version\r
+of W. Kahan's PARANOIA is also provided; this has been heavily\r
+modified by substituting subroutine calls to ieee.c in place of\r
+all arithmetic operators. It is important that you use PARANOIA\r
+to check the arithmetic after any modifications you may make to\r
+ieee.c.\r
+\r
+ Several systems have been tested with the initial version of\r
+ieetst. Sun 4 (SPARC) passes; DEC VMS C has only a small flaw;\r
+Microsoft flunks; ATT SysVR2 (Motorola) flunks even worse.\r
+\r
+\r
+ Files:\r
+\r
+calc100.doc calculator documentaton\r
+descrip.mms part of VAX VMS makefile\r
+drand.c random number generator\r
+ecalc.c calculator\r
+ecalc.opt part of VAX VMS makefile\r
+econst.c constants for reference arithmetic\r
+eexp.c reference exponential function\r
+ehead.h declarations for reference arithmetic routines\r
+elog.c reference logarithm\r
+eparanoi.c floating point arithmetic tester\r
+eparanoi.opt part of VAX VMS makefile\r
+epow.c reference exponentiation\r
+esqrt.c reference square root\r
+etanh.c reference hyperbolic tangent\r
+etodec.c conversions to and from DEC double precision format\r
+ieee.c the reference arithmetic\r
+ieetst.c printf/scanf tester\r
+ieetst.doc this file\r
+ieetst.mak Microsoft make file\r
+ieetst.opt part of VAX VMS makefile\r
+makefile Unix make file\r
+mconf.h definitions for arithmetic format\r
+mtherr.c common error reporter\r
+qcalc.h definitions for calculator\r
+\r
+\r
+This software may be copied freely.\r
+\r
+-- Steve Moshier\r
+\r
+v0.1 July, 1992\r
+v0.2 January, 1993\r
--- /dev/null
+/* mconf.h\r
+ *\r
+ * Common include file for math routines\r
+ *\r
+ *\r
+ *\r
+ * SYNOPSIS:\r
+ *\r
+ * #include "mconf.h"\r
+ *\r
+ *\r
+ *\r
+ * DESCRIPTION:\r
+ *\r
+ * This file contains definitions for error codes that are\r
+ * passed to the common error handling routine mtherr()\r
+ * (which see).\r
+ *\r
+ * The file also includes a conditional assembly definition\r
+ * for the type of computer arithmetic (IEEE, DEC, Motorola\r
+ * IEEE, or UNKnown).\r
+ *\r
+ * For Digital Equipment PDP-11 and VAX computers, certain\r
+ * IBM systems, and others that use numbers with a 56-bit\r
+ * significand, the symbol DEC should be defined. In this\r
+ * mode, most floating point constants are given as arrays\r
+ * of octal integers to eliminate decimal to binary conversion\r
+ * errors that might be introduced by the compiler.\r
+ *\r
+ * For computers, such as IBM PC, that follow the IEEE \r
+ * Standard for Binary Floating Point Arithmetic (ANSI/IEEE\r
+ * Std 754-1985), the symbol IBMPC should be defined. These\r
+ * numbers have 53-bit significands. In this mode, constants\r
+ * are provided as arrays of hexadecimal 16 bit integers.\r
+ *\r
+ * To accommodate other types of computer arithmetic, all\r
+ * constants are also provided in a normal decimal radix\r
+ * which one can hope are correctly converted to a suitable\r
+ * format by the available C language compiler. To invoke\r
+ * this mode, the symbol UNK is defined.\r
+ *\r
+ * An important difference among these modes is a predefined\r
+ * set of machine arithmetic constants for each. The numbers\r
+ * MACHEP (the machine roundoff error), MAXNUM (largest number\r
+ * represented), and several other parameters are preset by\r
+ * the configuration symbol. Check the file const.c to\r
+ * ensure that these values are correct for your computer.\r
+ *\r
+ */\r
+\f\r
+/*\r
+Cephes Math Library Release 2.0: April, 1987\r
+by Stephen L. Moshier\r
+Direct inquiries to 30 Frost Street, Cambridge, MA 02140\r
+*/\r
+\r
+\r
+/* Constant definitions for math error conditions\r
+ */\r
+\r
+#define DOMAIN 1 /* argument domain error */\r
+#define SING 2 /* argument singularity */\r
+#define OVERFLOW 3 /* overflow range error */\r
+#define UNDERFLOW 4 /* underflow range error */\r
+#define TLOSS 5 /* total loss of precision */\r
+#define PLOSS 6 /* partial loss of precision */\r
+\r
+#define EDOM 33\r
+#define ERANGE 34\r
+\r
+/*\r
+typedef struct\r
+ {\r
+ double r;\r
+ double i;\r
+ }cmplx;\r
+*/\r
+\r
+/* Type of computer arithmetic */\r
+\r
+/* PDP-11, Pro350, VAX:\r
+ */\r
+/*define DEC 1*/\r
+\r
+/* Intel IEEE, low order words come first:\r
+ */\r
+#define IBMPC 1\r
+\r
+/* Motorola IEEE, high order words come first\r
+ * (Sun workstation):\r
+ */\r
+/*define MIEEE 1*/\r
+\r
+/* UNKnown arithmetic, invokes coefficients given in\r
+ * normal decimal format. Beware of range boundary\r
+ * problems (MACHEP, MAXLOG, etc. in const.c) and\r
+ * roundoff problems in pow.c:\r
+ */\r
+ /*define UNK 1*/\r
+\r
+/* Define to ask for infinity support, else undefine. */\r
+#define INFINITY\r
+\r
+/* Define to ask for Not-a-Number support, else undefine. */\r
+#define NANS\r
+\r
+/* Define to support denormal numbers, else undefine. */\r
+#define DENORMAL\r
--- /dev/null
+/* mtherr.c\r
+ *\r
+ * Library common error handling routine\r
+ *\r
+ *\r
+ *\r
+ * SYNOPSIS:\r
+ *\r
+ * char *fctnam;\r
+ * int code;\r
+ * void mtherr();\r
+ *\r
+ * mtherr( fctnam, code );\r
+ *\r
+ *\r
+ *\r
+ * DESCRIPTION:\r
+ *\r
+ * This routine may be called to report one of the following\r
+ * error conditions (in the include file mconf.h).\r
+ * \r
+ * Mnemonic Value Significance\r
+ *\r
+ * DOMAIN 1 argument domain error\r
+ * SING 2 function singularity\r
+ * OVERFLOW 3 overflow range error\r
+ * UNDERFLOW 4 underflow range error\r
+ * TLOSS 5 total loss of precision\r
+ * PLOSS 6 partial loss of precision\r
+ * EDOM 33 Unix domain error code\r
+ * ERANGE 34 Unix range error code\r
+ *\r
+ * The default version of the file prints the function name,\r
+ * passed to it by the pointer fctnam, followed by the\r
+ * error condition. The display is directed to the standard\r
+ * output device. The routine then returns to the calling\r
+ * program. Users may wish to modify the program to abort by\r
+ * calling exit() under severe error conditions such as domain\r
+ * errors.\r
+ *\r
+ * Since all error conditions pass control to this function,\r
+ * the display may be easily changed, eliminated, or directed\r
+ * to an error logging device.\r
+ *\r
+ * SEE ALSO:\r
+ *\r
+ * mconf.h\r
+ *\r
+ */\r
+\f\r
+/*\r
+Cephes Math Library Release 2.0: April, 1987\r
+by Stephen L. Moshier\r
+Direct inquiries to 30 Frost Street, Cambridge, MA 02140\r
+*/\r
+\r
+#include "mconf.h"\r
+\r
+/* Notice: the order of appearance of the following\r
+ * messages is bound to the error codes defined\r
+ * in mconf.h.\r
+ */\r
+static char *ermsg[7] = {\r
+"unknown", /* error code 0 */\r
+"domain", /* error code 1 */\r
+"singularity", /* et seq. */\r
+"overflow",\r
+"underflow",\r
+"total loss of precision",\r
+"partial loss of precision"\r
+};\r
+\r
+\r
+\r
+void mtherr( name, code )\r
+char *name;\r
+int code;\r
+{\r
+\r
+/* Display string passed by calling program,\r
+ * which is supposed to be the name of the\r
+ * function in which the error occurred:\r
+ */\r
+printf( "\n%s ", name );\r
+\r
+/* Display error message defined\r
+ * by the code argument.\r
+ */\r
+if( (code <= 0) || (code >= 6) )\r
+ code = 0;\r
+printf( "%s error\n", ermsg[code] );\r
+\r
+/* Return to calling\r
+ * program\r
+ */\r
+}\r