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