source: trunk/libs/newlib/src/newlib/libm/mathfp/e_hypot.c @ 452

Last change on this file since 452 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 4.1 KB
Line 
1
2/* @(#)e_hypot.c 5.1 93/09/24 */
3/*
4 * ====================================================
5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 *
7 * Developed at SunPro, a Sun Microsystems, Inc. business.
8 * Permission to use, copy, modify, and distribute this
9 * software is freely granted, provided that this notice
10 * is preserved.
11 * ====================================================
12 */
13
14/*
15FUNCTION
16        <<hypot>>, <<hypotf>>---distance from origin
17INDEX
18        hypot
19INDEX
20        hypotf
21
22SYNOPSIS
23        #include <math.h>
24        double hypot(double <[x]>, double <[y]>);
25        float hypotf(float <[x]>, float <[y]>);
26
27DESCRIPTION
28        <<hypot>> calculates the Euclidean distance
29        @tex
30        $\sqrt{x^2+y^2}$
31        @end tex
32        @ifnottex
33        <<sqrt(<[x]>*<[x]> + <[y]>*<[y]>)>>
34        @end ifnottex
35        between the origin (0,0) and a point represented by the
36        Cartesian coordinates (<[x]>,<[y]>).  <<hypotf>> differs only
37        in the type of its arguments and result.
38
39RETURNS
40        Normally, the distance value is returned.  On overflow,
41        <<hypot>> returns <<HUGE_VAL>> and sets <<errno>> to
42        <<ERANGE>>.
43
44        You can change the error treatment with <<matherr>>.
45
46PORTABILITY
47        <<hypot>> and <<hypotf>> are not ANSI C.  */
48
49/* hypot(x,y)
50 *
51 * Method :                 
52 *      If (assume round-to-nearest) z=x*x+y*y
53 *      has error less than sqrt(2)/2 ulp, than
54 *      sqrt(z) has error less than 1 ulp (exercise).
55 *
56 *      So, compute sqrt(x*x+y*y) with some care as
57 *      follows to get the error below 1 ulp:
58 *
59 *      Assume x>y>0;
60 *      (if possible, set rounding to round-to-nearest)
61 *      1. if x > 2y  use
62 *              x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y
63 *      where x1 = x with lower 32 bits cleared, x2 = x-x1; else
64 *      2. if x <= 2y use
65 *              t1*y1+((x-y)*(x-y)+(t1*y2+t2*y))
66 *      where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1,
67 *      y1= y with lower 32 bits chopped, y2 = y-y1.
68 *             
69 *      NOTE: scaling may be necessary if some argument is too
70 *            large or too tiny
71 *
72 * Special cases:
73 *      hypot(x,y) is INF if x or y is +INF or -INF; else
74 *      hypot(x,y) is NAN if x or y is NAN.
75 *
76 * Accuracy:
77 *      hypot(x,y) returns sqrt(x^2+y^2) with error less
78 *      than 1 ulps (units in the last place)
79 */
80
81#include "fdlibm.h"
82
83#ifndef _DOUBLE_IS_32BITS
84
85#ifdef __STDC__
86        double hypot(double x, double y)
87#else
88        double hypot(x,y)
89        double x, y;
90#endif
91{
92        double a=x,b=y,t1,t2,y1,y2,w;
93        __int32_t j,k,ha,hb;
94
95        GET_HIGH_WORD(ha,x);
96        ha &= 0x7fffffff;
97        GET_HIGH_WORD(hb,y);
98        hb &= 0x7fffffff;
99        if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
100        SET_HIGH_WORD(a,ha);    /* a <- |a| */
101        SET_HIGH_WORD(b,hb);    /* b <- |b| */
102        if((ha-hb)>0x3c00000) {return a+b;} /* x/y > 2**60 */
103        k=0;
104        if(ha > 0x5f300000) {   /* a>2**500 */
105           if(ha >= 0x7ff00000) {       /* Inf or NaN */
106               __uint32_t low;
107               w = a+b;                 /* for sNaN */
108               GET_LOW_WORD(low,a);
109               if(((ha&0xfffff)|low)==0) w = a;
110               GET_LOW_WORD(low,b);
111               if(((hb^0x7ff00000)|low)==0) w = b;
112               return w;
113           }
114           /* scale a and b by 2**-600 */
115           ha -= 0x25800000; hb -= 0x25800000;  k += 600;
116           SET_HIGH_WORD(a,ha);
117           SET_HIGH_WORD(b,hb);
118        }
119        if(hb < 0x20b00000) {   /* b < 2**-500 */
120            if(hb <= 0x000fffff) {      /* subnormal b or 0 */ 
121                __uint32_t low;
122                GET_LOW_WORD(low,b);
123                if((hb|low)==0) return a;
124                t1=0;
125                SET_HIGH_WORD(t1,0x7fd00000);   /* t1=2^1022 */
126                b *= t1;
127                a *= t1;
128                k -= 1022;
129            } else {            /* scale a and b by 2^600 */
130                ha += 0x25800000;       /* a *= 2^600 */
131                hb += 0x25800000;       /* b *= 2^600 */
132                k -= 600;
133                SET_HIGH_WORD(a,ha);
134                SET_HIGH_WORD(b,hb);
135            }
136        }
137    /* medium size a and b */
138        w = a-b;
139        if (w>b) {
140            t1 = 0;
141            SET_HIGH_WORD(t1,ha);
142            t2 = a-t1;
143            w  = sqrt(t1*t1-(b*(-b)-t2*(a+t1)));
144        } else {
145            a  = a+a;
146            y1 = 0;
147            SET_HIGH_WORD(y1,hb);
148            y2 = b - y1;
149            t1 = 0;
150            SET_HIGH_WORD(t1,ha+0x00100000);
151            t2 = a - t1;
152            w  = sqrt(t1*y1-(w*(-w)-(t1*y2+t2*b)));
153        }
154        if(k!=0) {
155            __uint32_t high;
156            t1 = 1.0;
157            GET_HIGH_WORD(high,t1);
158            SET_HIGH_WORD(t1,high+(k<<20));
159            return t1*w;
160        } else return w;
161}
162
163#endif /* defined(_DOUBLE_IS_32BITS) */
Note: See TracBrowser for help on using the repository browser.