📄 ieee.c
字号:
#include <stdio.h>
/* Include file for extended precision arithmetic programs.
*/
/* Number of 16 bit words in external x type format */
#define NE 10
/* Number of 16 bit words in internal format */
#define NI (NE+3)
/* Array offset to exponent */
#define E 1
/* Array offset to high guard word */
#define M 2
/* Number of bits of precision */
#define NBITS ((NI-4)*16)
/* Maximum number of decimal digits in ASCII conversion
* = NBITS*log10(2)
*/
#define NDEC (NBITS*8/27)
/* The exponent of 1.0 */
#define EXONE (0x3fff)
void eadd(), esub(), emul(), ediv();
int ecmp(), enormlz(), eshift();
void eshup1(), eshup8(), eshup6(), eshdn1(), eshdn8(), eshdn6();
void eabs(), eneg(), emov(), eclear(), einfin(), efloor();
void eldexp(), efrexp(), eifrac(), ltoe();
void esqrt(), elog(), eexp(), etanh(), epow();
void asctoe(), asctoe24(), asctoe53(), asctoe64();
void etoasc(), e24toasc(), e53toasc(), e64toasc();
void etoe64(), etoe53(), etoe24(), e64toe(), e53toe(), e24toe();
int mtherr();
extern unsigned short ezero[], ehalf[], eone[], etwo[];
extern unsigned short elog2[], esqrt2[];
/* by Stephen L. Moshier. */
/* mconf.h
*
* Common include file for math routines
*
*
*
* SYNOPSIS:
*
* #include "mconf.h"
*
*
*
* DESCRIPTION:
*
* This file contains definitions for error codes that are
* passed to the common error handling routine mtherr()
* (which see).
*
* The file also includes a conditional assembly definition
* for the type of computer arithmetic (IEEE, DEC, Motorola
* IEEE, or UNKnown).
*
* For Digital Equipment PDP-11 and VAX computers, certain
* IBM systems, and others that use numbers with a 56-bit
* significand, the symbol DEC should be defined. In this
* mode, most floating point constants are given as arrays
* of octal integers to eliminate decimal to binary conversion
* errors that might be introduced by the compiler.
*
* For little-endian computers, such as IBM PC, that follow the
* IEEE Standard for Binary Floating Point Arithmetic (ANSI/IEEE
* Std 754-1985), the symbol IBMPC should be defined. These
* numbers have 53-bit significands. In this mode, constants
* are provided as arrays of hexadecimal 16 bit integers.
*
* Big-endian IEEE format is denoted MIEEE. On some RISC
* systems such as Sun SPARC, double precision constants
* must be stored on 8-byte address boundaries. Since integer
* arrays may be aligned differently, the MIEEE configuration
* may fail on such machines.
*
* To accommodate other types of computer arithmetic, all
* constants are also provided in a normal decimal radix
* which one can hope are correctly converted to a suitable
* format by the available C language compiler. To invoke
* this mode, define the symbol UNK.
*
* An important difference among these modes is a predefined
* set of machine arithmetic constants for each. The numbers
* MACHEP (the machine roundoff error), MAXNUM (largest number
* represented), and several other parameters are preset by
* the configuration symbol. Check the file const.c to
* ensure that these values are correct for your computer.
*
* Configurations NANS, INFINITIES, MINUSZERO, and DENORMAL
* may fail on many systems. Verify that they are supposed
* to work on your computer.
*/
/*
Cephes Math Library Release 2.3: June, 1995
Copyright 1984, 1987, 1989, 1995 by Stephen L. Moshier
*/
/* Constant definitions for math error conditions
*/
#define DOMAIN 1 /* argument domain error */
#define SING 2 /* argument singularity */
#define OVERFLOW 3 /* overflow range error */
#define UNDERFLOW 4 /* underflow range error */
#define TLOSS 5 /* total loss of precision */
#define PLOSS 6 /* partial loss of precision */
#define EDOM 33
#define ERANGE 34
/* Complex numeral. */
typedef struct
{
double r;
double i;
} cmplx;
/* Long double complex numeral. */
typedef struct
{
double r;
double i;
} cmplxl;
/* Type of computer arithmetic */
/* PDP-11, Pro350, VAX:
*/
/* #define DEC 1 */
/* Intel IEEE, low order words come first:
*/
/* #define IBMPC 1 */
/* Motorola IEEE, high order words come first
* (Sun 680x0 workstation):
*/
/* #define MIEEE 1 */
/* UNKnown arithmetic, invokes coefficients given in
* normal decimal format. Beware of range boundary
* problems (MACHEP, MAXLOG, etc. in const.c) and
* roundoff problems in pow.c:
* (Sun SPARCstation)
*/
#define UNK 1
/* If you define UNK, then be sure to set BIGENDIAN properly. */
#define BIGENDIAN 0
/* Define this `volatile' if your compiler thinks
* that floating point arithmetic obeys the associative
* and distributive laws. It will defeat some optimizations
* (but probably not enough of them).
*
* #define VOLATILE volatile
*/
#define VOLATILE
/* For 12-byte long doubles on an i386, pad a 16-bit short 0
* to the end of real constants initialized by integer arrays.
*
* #define XPD 0,
*
* Otherwise, the type is 10 bytes long and XPD should be
* defined blank (e.g., Microsoft C).
*
* #define XPD
*/
#define XPD 0,
/* Define to support tiny denormal numbers, else undefine. */
#define DENORMAL 1
/* Define to ask for infinity support, else undefine. */
#define INFINITIES 1
/* Define to ask for support of numbers that are Not-a-Number,
else undefine. This may automatically define INFINITIES in some files. */
#define NANS 1
/* Define to distinguish between -0.0 and +0.0. */
#define MINUSZERO 1
/* Define 1 for ANSI C atan2() function
See atan.c and clog.c. */
#define ANSIC 1
int mtherr();
/* Variable for error reporting. See mtherr.c. */
extern int merror;
/* econst.c */
/* e type constants used by high precision check routines */
#if NE == 10
/* 0.0 */
unsigned short ezero[NE] =
{0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,};
/* 5.0E-1 */
unsigned short ehalf[NE] =
{0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3ffe,};
/* 1.0E0 */
unsigned short eone[NE] =
{0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x3fff,};
/* 2.0E0 */
unsigned short etwo[NE] =
{0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4000,};
/* 3.2E1 */
unsigned short e32[NE] =
{0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x8000, 0x4004,};
/* 6.93147180559945309417232121458176568075500134360255E-1 */
unsigned short elog2[NE] =
{0x40f3, 0xf6af, 0x03f2, 0xb398,
0xc9e3, 0x79ab, 0150717, 0013767, 0130562, 0x3ffe,};
/* 1.41421356237309504880168872420969807856967187537695E0 */
unsigned short esqrt2[NE] =
{0x1d6f, 0xbe9f, 0x754a, 0x89b3,
0x597d, 0x6484, 0174736, 0171463, 0132404, 0x3fff,};
/* 3.14159265358979323846264338327950288419716939937511E0 */
unsigned short epi[NE] =
{0x2902, 0x1cd1, 0x80dc, 0x628b,
0xc4c6, 0xc234, 0020550, 0155242, 0144417, 0040000,};
/* 5.7721566490153286060651209008240243104215933593992E-1 */
unsigned short eeul[NE] = {
0xd1be,0xc7a4,0076660,0063743,0111704,0x3ffe,};
#else
/* 0.0 */
unsigned short ezero[NE] = {
0, 0000000,0000000,0000000,0000000,0000000,};
/* 5.0E-1 */
unsigned short ehalf[NE] = {
0, 0000000,0000000,0000000,0100000,0x3ffe,};
/* 1.0E0 */
unsigned short eone[NE] = {
0, 0000000,0000000,0000000,0100000,0x3fff,};
/* 2.0E0 */
unsigned short etwo[NE] = {
0, 0000000,0000000,0000000,0100000,0040000,};
/* 3.2E1 */
unsigned short e32[NE] = {
0, 0000000,0000000,0000000,0100000,0040004,};
/* 6.93147180559945309417232121458176568075500134360255E-1 */
unsigned short elog2[NE] = {
0xc9e4,0x79ab,0150717,0013767,0130562,0x3ffe,};
/* 1.41421356237309504880168872420969807856967187537695E0 */
unsigned short esqrt2[NE] = {
0x597e,0x6484,0174736,0171463,0132404,0x3fff,};
/* 2/sqrt(PI) =
* 1.12837916709551257389615890312154517168810125865800E0 */
unsigned short eoneopi[NE] = {
0x71d5,0x688d,0012333,0135202,0110156,0x3fff,};
/* 3.14159265358979323846264338327950288419716939937511E0 */
unsigned short epi[NE] = {
0xc4c6,0xc234,0020550,0155242,0144417,0040000,};
/* 5.7721566490153286060651209008240243104215933593992E-1 */
unsigned short eeul[NE] = {
0xd1be,0xc7a4,0076660,0063743,0111704,0x3ffe,};
#endif
extern unsigned short ezero[];
extern unsigned short ehalf[];
extern unsigned short eone[];
extern unsigned short etwo[];
extern unsigned short e32[];
extern unsigned short elog2[];
extern unsigned short esqrt2[];
extern unsigned short eoneopi[];
extern unsigned short epi[];
extern unsigned short eeul[];
/* ieee.c
*
* Extended precision IEEE binary floating point arithmetic routines
*
* Numbers are stored in C language as arrays of 16-bit unsigned
* short integers. The arguments of the routines are pointers to
* the arrays.
*
*
* External e type data structure, simulates Intel 8087 chip
* temporary real format but possibly with a larger significand:
*
* NE-1 significand words (least significant word first,
* most significant bit is normally set)
* exponent (value = EXONE for 1.0,
* top bit is the sign)
*
*
* Internal data structure of a number (a "word" is 16 bits):
*
* ei[0] sign word (0 for positive, 0xffff for negative)
* ei[1] biased exponent (value = EXONE for the number 1.0)
* ei[2] high guard word (always zero after normalization)
* ei[3]
* to ei[NI-2] significand (NI-4 significand words,
* most significant word first,
* most significant bit is set)
* ei[NI-1] low guard word (0x8000 bit is rounding place)
*
*
*
* Routines for external format numbers
*
* asctoe( string, e ) ASCII string to extended double e type
* asctoe64( string, &d ) ASCII string to long double
* asctoe53( string, &d ) ASCII string to double
* asctoe24( string, &f ) ASCII string to single
* asctoeg( string, e, prec ) ASCII string to specified precision
* e24toe( &f, e ) IEEE single precision to e type
* e53toe( &d, e ) IEEE double precision to e type
* e64toe( &d, e ) IEEE long double precision to e type
* eabs(e) absolute value
* eadd( a, b, c ) c = b + a
* eclear(e) e = 0
* ecmp (a, b) Returns 1 if a > b, 0 if a == b,
* -1 if a < b, -2 if either a or b is a NaN.
* ediv( a, b, c ) c = b / a
* efloor( a, b ) truncate to integer, toward -infinity
* efrexp( a, exp, s ) extract exponent and significand
* eifrac( e, &l, frac ) e to long integer and e type fraction
* euifrac( e, &l, frac ) e to unsigned long integer and e type fraction
* einfin( e ) set e to infinity, leaving its sign alone
* eldexp( a, n, b ) multiply by 2**n
* emov( a, b ) b = a
* emul( a, b, c ) c = b * a
* eneg(e) e = -e
* eround( a, b ) b = nearest integer value to a
* esub( a, b, c ) c = b - a
* e24toasc( &f, str, n ) single to ASCII string, n digits after decimal
* e53toasc( &d, str, n ) double to ASCII string, n digits after decimal
* e64toasc( &d, str, n ) long double to ASCII string
* etoasc( e, str, n ) e to ASCII string, n digits after decimal
* etoe24( e, &f ) convert e type to IEEE single precision
* etoe53( e, &d ) convert e type to IEEE double precision
* etoe64( e, &d ) convert e type to IEEE long double precision
* ltoe( &l, e ) long (32 bit) integer to e type
* ultoe( &l, e ) unsigned long (32 bit) integer to e type
* eisneg( e ) 1 if sign bit of e != 0, else 0
* eisinf( e ) 1 if e has maximum exponent (non-IEEE)
* or is infinite (IEEE)
* eisnan( e ) 1 if e is a NaN
* esqrt( a, b ) b = square root of a
*
*
* Routines for internal format numbers
*
* eaddm( ai, bi ) add significands, bi = bi + ai
* ecleaz(ei) ei = 0
* ecleazs(ei) set ei = 0 but leave its sign alone
* ecmpm( ai, bi ) compare significands, return 1, 0, or -1
* edivm( ai, bi ) divide significands, bi = bi / ai
* emdnorm(ai,l,s,exp) normalize and round off
* emovi( a, ai ) convert external a to internal ai
* emovo( ai, a ) convert internal ai to external a
* emovz( ai, bi ) bi = ai, low guard word of bi = 0
* emulm( ai, bi ) multiply significands, bi = bi * ai
* enormlz(ei) left-justify the significand
* eshdn1( ai ) shift significand and guards down 1 bit
* eshdn8( ai ) shift down 8 bits
* eshdn6( ai ) shift down 16 bits
* eshift( ai, n ) shift ai n bits up (or down if n < 0)
* eshup1( ai ) shift significand and guards up 1 bit
* eshup8( ai ) shift up 8 bits
* eshup6( ai ) shift up 16 bits
* esubm( ai, bi ) subtract significands, bi = bi - ai
*
*
* The result is always normalized and rounded to NI-4 word precision
* after each arithmetic operation.
*
* Exception flags are NOT fully supported.
*
* Define INFINITY in mconf.h for support of infinity; otherwise a
* saturation arithmetic is implemented.
*
* Define NANS for support of Not-a-Number items; otherwise the
* arithmetic will never produce a NaN output, and might be confused
* by a NaN input.
* If NaN's are supported, the output of ecmp(a,b) is -2 if
* either a or b is a NaN. This means asking if(ecmp(a,b) < 0)
* may not be legitimate. Use if(ecmp(a,b) == -1) for less-than
* if in doubt.
* Signaling NaN's are NOT supported; they are treated the same
* as quiet NaN's.
*
* Denormals are always supported here where appropriate (e.g., not
* for conversion to DEC numbers).
*/
/*
* Revision history:
*
* 5 Jan 84 PDP-11 assembly language version
* 2 Mar 86 fixed bug in asctoq()
* 6 Dec 86 C language version
* 30 Aug 88 100 digit version, improved rounding
* 15 May 92 80-bit long double support
*
* Author: S. L. Moshier.
*/
/* Change UNK into something else. */
#ifdef UNK
#undef UNK
#if BIGENDIAN
#define MIEEE 1
#else
#define IBMPC 1
#endif
#endif
/* NaN's require infinity support. */
#ifdef NANS
#ifndef INFINITY
#define INFINITY
#endif
#endif
/* This handles 64-bit long ints. */
#define LONGBITS (8 * sizeof(long))
/* Control register for rounding precision.
* This can be set to 80 (if NE=6), 64, 56, 53, or 24 bits.
*/
int rndprc = NBITS;
extern int rndprc;
void eaddm(), esubm(), emdnorm(), asctoeg(), enan();
static void toe24(), toe53(), toe64(), toe113();
void eremain(), einit(), eiremain();
int ecmpm(), edivm(), emulm(), eisneg(), eisinf();
void emovi(), emovo(), emovz(), ecleaz(), eadd1();
void etodec(), todec(), dectoe();
int eisnan(), eiisnan();
void einit()
{
}
/*
; Clear out entire external format number.
;
; unsigned short x[];
; eclear( x );
*/
void eclear( x )
register unsigned short *x;
{
register int i;
for( i=0; i<NE; i++ )
*x++ = 0;
}
/* Move external format number from a to b.
*
* emov( a, b );
*/
void emov( a, b )
register unsigned short *a, *b;
{
register int i;
for( i=0; i<NE; i++ )
*b++ = *a++;
}
/*
; Absolute value of external format number
;
; short x[NE];
; eabs( x );
*/
void eabs(x)
unsigned short x[]; /* x is the memory address of a short */
{
x[NE-1] &= 0x7fff; /* sign is top bit of last word of external format */
}
/*
; Negate external format number
;
; unsigned short x[NE];
; eneg( x );
*/
void eneg(x)
unsigned short x[];
{
#ifdef NANS
if( eisnan(x) )
return;
#endif
x[NE-1] ^= 0x8000; /* Toggle the sign bit */
}
/* Return 1 if external format number is negative,
* else return zero.
*/
int eisneg(x)
unsigned short x[];
{
#ifdef NANS
if( eisnan(x) )
return( 0 );
#endif
if( x[NE-1] & 0x8000 )
return( 1 );
else
return( 0 );
}
/* Return 1 if external format number has maximum possible exponent,
* else return zero.
*/
int eisinf(x)
unsigned short x[];
{
if( (x[NE-1] & 0x7fff) == 0x7fff )
{
#ifdef NANS
if( eisnan(x) )
return( 0 );
#endif
return( 1 );
}
else
return( 0 );
}
/* Check if e-type number is not a number.
*/
int eisnan(x)
unsigned short x[];
{
#ifdef NANS
int i;
/* NaN has maximum exponent */
if( (x[NE-1] & 0x7fff) != 0x7fff )
return (0);
/* ... and non-zero significand field. */
for( i=0; i<NE-1; i++ )
{
if( *x++ != 0 )
return (1);
}
#endif
return (0);
}
/*
; Fill entire number, including exponent and significand, with
; largest possible number. These programs implement a saturation
; value that is an ordinary, legal number. A special value
; "infinity" may also be implemented; this would require tests
; for that value and implementation of special rules for arithmetic
; operations involving inifinity.
*/
void einfin(x)
register unsigned short *x;
{
register int i;
#ifdef INFINITY
for( i=0; i<NE-1; i++ )
*x++ = 0;
*x |= 32767;
#else
for( i=0; i<NE-1; i++ )
*x++ = 0xffff;
*x |= 32766;
if( rndprc < NBITS )
{
if (rndprc == 113)
{
*(x - 9) = 0;
*(x - 8) = 0;
}
if( rndprc == 64 )
{
*(x-5) = 0;
}
if( rndprc == 53 )
{
*(x-4) = 0xf800;
}
else
{
*(x-4) = 0;
*(x-3) = 0;
*(x-2) = 0xff00;
}
}
#endif
}
/* Move in external format number,
* converting it to internal format.
*/
void emovi( a, b )
unsigned short *a, *b;
{
register unsigned short *p, *q;
int i;
q = b;
p = a + (NE-1); /* point to last word of external number */
/* get the sign bit */
if( *p & 0x8000 )
*q++ = 0xffff;
else
*q++ = 0;
/* get the exponent */
*q = *p--;
*q++ &= 0x7fff; /* delete the sign bit */
#ifdef INFINITY
if( (*(q-1) & 0x7fff) == 0x7fff )
{
#ifdef NANS
if( eisnan(a) )
{
*q++ = 0;
for( i=3; i<NI; i++ )
*q++ = *p--;
return;
}
#endif
for( i=2; i<NI; i++ )
*q++ = 0;
return;
}
#endif
/* clear high guard word */
*q++ = 0;
/* move in the significand */
for( i=0; i<NE-1; i++ )
*q++ = *p--;
/* clear low guard word */
*q = 0;
}
/* Move internal format number out,
* converting it to external format.
*/
void emovo( a, b )
unsigned short *a, *b;
{
register unsigned short *p, *q;
unsigned short i;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -