Home | History | Annotate | Line # | Download | only in noieee_src
      1 /*      $NetBSD: n_cabs.c,v 1.5 2003/08/07 16:44:50 agc Exp $ */
      2 /*
      3  * Copyright (c) 1985, 1993
      4  *	The Regents of the University of California.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. Neither the name of the University nor the names of its contributors
     15  *    may be used to endorse or promote products derived from this software
     16  *    without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #ifndef lint
     32 static char sccsid[] = "@(#)cabs.c	8.1 (Berkeley) 6/4/93";
     33 #endif /* not lint */
     34 
     35 /* HYPOT(X,Y)
     36  * RETURN THE SQUARE ROOT OF X^2 + Y^2  WHERE Z=X+iY
     37  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
     38  * CODED IN C BY K.C. NG, 11/28/84;
     39  * REVISED BY K.C. NG, 7/12/85.
     40  *
     41  * Required system supported functions :
     42  *	copysign(x,y)
     43  *	finite(x)
     44  *	scalb(x,N)
     45  *	sqrt(x)
     46  *
     47  * Method :
     48  *	1. replace x by |x| and y by |y|, and swap x and
     49  *	   y if y > x (hence x is never smaller than y).
     50  *	2. Hypot(x,y) is computed by:
     51  *	   Case I, x/y > 2
     52  *
     53  *				       y
     54  *		hypot = x + -----------------------------
     55  *			 		    2
     56  *			    sqrt ( 1 + [x/y]  )  +  x/y
     57  *
     58  *	   Case II, x/y <= 2
     59  *				                   y
     60  *		hypot = x + --------------------------------------------------
     61  *				          		     2
     62  *				     			[x/y]   -  2
     63  *			   (sqrt(2)+1) + (x-y)/y + -----------------------------
     64  *			 		    			  2
     65  *			    			  sqrt ( 1 + [x/y]  )  + sqrt(2)
     66  *
     67  *
     68  *
     69  * Special cases:
     70  *	hypot(x,y) is INF if x or y is +INF or -INF; else
     71  *	hypot(x,y) is NAN if x or y is NAN.
     72  *
     73  * Accuracy:
     74  * 	hypot(x,y) returns the sqrt(x^2+y^2) with error less than 1 ulps (units
     75  *	in the last place). See Kahan's "Interval Arithmetic Options in the
     76  *	Proposed IEEE Floating Point Arithmetic Standard", Interval Mathematics
     77  *      1980, Edited by Karl L.E. Nickel, pp 99-128. (A faster but less accurate
     78  *	code follows in	comments.) In a test run with 500,000 random arguments
     79  *	on a VAX, the maximum observed error was .959 ulps.
     80  *
     81  * Constants:
     82  * The hexadecimal values are the intended ones for the following constants.
     83  * The decimal values may be used, provided that the compiler will convert
     84  * from decimal to binary accurately enough to produce the hexadecimal values
     85  * shown.
     86  */
     87 #define _LIBM_STATIC
     88 #include "mathimpl.h"
     89 
     90 vc(r2p1hi, 2.4142135623730950345E0   ,8279,411a,ef32,99fc,   2, .9A827999FCEF32)
     91 vc(r2p1lo, 1.4349369327986523769E-17 ,597d,2484,754b,89b3, -55, .84597D89B3754B)
     92 vc(sqrt2,  1.4142135623730950622E0   ,04f3,40b5,de65,33f9,   1, .B504F333F9DE65)
     93 
     94 ic(r2p1hi, 2.4142135623730949234E0   ,   1, 1.3504F333F9DE6)
     95 ic(r2p1lo, 1.2537167179050217666E-16 , -53, 1.21165F626CDD5)
     96 ic(sqrt2,  1.4142135623730951455E0   ,   0, 1.6A09E667F3BCD)
     97 
     98 #ifdef vccast
     99 #define	r2p1hi	vccast(r2p1hi)
    100 #define	r2p1lo	vccast(r2p1lo)
    101 #define	sqrt2	vccast(sqrt2)
    102 #endif
    103 
    104 double
    105 hypot(double x, double y)
    106 {
    107 	static const double zero=0, one=1,
    108 		      small=1.0E-18;	/* fl(1+small)==1 */
    109 	static const ibig=30;	/* fl(1+2**(2*ibig))==1 */
    110 	double t,r;
    111 	int exp;
    112 
    113 	if(finite(x))
    114 	    if(finite(y))
    115 	    {
    116 		x=copysign(x,one);
    117 		y=copysign(y,one);
    118 		if(y > x)
    119 		    { t=x; x=y; y=t; }
    120 		if(x == zero) return(zero);
    121 		if(y == zero) return(x);
    122 		exp= logb(x);
    123 		if(exp-(int)logb(y) > ibig )
    124 			/* raise inexact flag and return |x| */
    125 		   { one+small; return(x); }
    126 
    127 	    /* start computing sqrt(x^2 + y^2) */
    128 		r=x-y;
    129 		if(r>y) { 	/* x/y > 2 */
    130 		    r=x/y;
    131 		    r=r+sqrt(one+r*r); }
    132 		else {		/* 1 <= x/y <= 2 */
    133 		    r/=y; t=r*(r+2.0);
    134 		    r+=t/(sqrt2+sqrt(2.0+t));
    135 		    r+=r2p1lo; r+=r2p1hi; }
    136 
    137 		r=y/r;
    138 		return(x+r);
    139 
    140 	    }
    141 
    142 	    else if(y==y)   	   /* y is +-INF */
    143 		     return(copysign(y,one));
    144 	    else
    145 		     return(y);	   /* y is NaN and x is finite */
    146 
    147 	else if(x==x) 		   /* x is +-INF */
    148 	         return (copysign(x,one));
    149 	else if(finite(y))
    150 	         return(x);		   /* x is NaN, y is finite */
    151 #if !defined(__vax__)&&!defined(tahoe)
    152 	else if(y!=y) return(y);  /* x and y is NaN */
    153 #endif	/* !defined(__vax__)&&!defined(tahoe) */
    154 	else return(copysign(y,one));   /* y is INF */
    155 }
    156 
    157 /* CABS(Z)
    158  * RETURN THE ABSOLUTE VALUE OF THE COMPLEX NUMBER  Z = X + iY
    159  * DOUBLE PRECISION (VAX D format 56 bits, IEEE DOUBLE 53 BITS)
    160  * CODED IN C BY K.C. NG, 11/28/84.
    161  * REVISED BY K.C. NG, 7/12/85.
    162  *
    163  * Required kernel function :
    164  *	hypot(x,y)
    165  *
    166  * Method :
    167  *	cabs(z) = hypot(x,y) .
    168  */
    169 
    170 struct complex { double x, y; };
    171 
    172 double
    173 cabs(z)
    174 struct complex z;
    175 {
    176 	return hypot(z.x,z.y);
    177 }
    178 
    179 double
    180 z_abs(z)
    181 struct complex *z;
    182 {
    183 	return hypot(z->x,z->y);
    184 }
    185 
    186 /* A faster but less accurate version of cabs(x,y) */
    187 #if 0
    188 double hypot(x,y)
    189 double x, y;
    190 {
    191 	static const double zero=0, one=1;
    192 		      small=1.0E-18;	/* fl(1+small)==1 */
    193 	static const ibig=30;	/* fl(1+2**(2*ibig))==1 */
    194 	double temp;
    195 	int exp;
    196 
    197 	if(finite(x))
    198 	    if(finite(y))
    199 	    {
    200 		x=copysign(x,one);
    201 		y=copysign(y,one);
    202 		if(y > x)
    203 		    { temp=x; x=y; y=temp; }
    204 		if(x == zero) return(zero);
    205 		if(y == zero) return(x);
    206 		exp= logb(x);
    207 		x=scalb(x,-exp);
    208 		if(exp-(int)logb(y) > ibig )
    209 			/* raise inexact flag and return |x| */
    210 		   { one+small; return(scalb(x,exp)); }
    211 		else y=scalb(y,-exp);
    212 		return(scalb(sqrt(x*x+y*y),exp));
    213 	    }
    214 
    215 	    else if(y==y)   	   /* y is +-INF */
    216 		     return(copysign(y,one));
    217 	    else
    218 		     return(y);	   /* y is NaN and x is finite */
    219 
    220 	else if(x==x) 		   /* x is +-INF */
    221 	         return (copysign(x,one));
    222 	else if(finite(y))
    223 	         return(x);		   /* x is NaN, y is finite */
    224 	else if(y!=y) return(y);  	/* x and y is NaN */
    225 	else return(copysign(y,one));   /* y is INF */
    226 }
    227 #endif
    228