blob: e45486ee912f73e239a7c3a336a2e266c7136270 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* e_hypotf.c -- float version of e_hypot.c.
2 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3 */
4
5/*
6 * ====================================================
7 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8 *
9 * Developed at SunPro, a Sun Microsystems, Inc. business.
10 * Permission to use, copy, modify, and distribute this
11 * software is freely granted, provided that this notice
12 * is preserved.
13 * ====================================================
14 */
15
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080016#include "math.h"
17#include "math_private.h"
18
19float
Elliott Hughes4088e3a2023-08-03 13:33:56 -070020hypotf(float x, float y)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080021{
Elliott Hughesa0ee0782013-01-30 19:06:37 -080022 float a,b,t1,t2,y1,y2,w;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080023 int32_t j,k,ha,hb;
24
25 GET_FLOAT_WORD(ha,x);
26 ha &= 0x7fffffff;
27 GET_FLOAT_WORD(hb,y);
28 hb &= 0x7fffffff;
29 if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
Elliott Hughesa0ee0782013-01-30 19:06:37 -080030 a = fabsf(a);
31 b = fabsf(b);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080032 if((ha-hb)>0xf000000) {return a+b;} /* x/y > 2**30 */
33 k=0;
34 if(ha > 0x58800000) { /* a>2**50 */
35 if(ha >= 0x7f800000) { /* Inf or NaN */
Elliott Hughesa0ee0782013-01-30 19:06:37 -080036 /* Use original arg order iff result is NaN; quieten sNaNs. */
Elliott Hughesab528072018-07-24 00:01:52 +000037 w = fabsl(x+0.0L)-fabsf(y+0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038 if(ha == 0x7f800000) w = a;
39 if(hb == 0x7f800000) w = b;
40 return w;
41 }
42 /* scale a and b by 2**-68 */
43 ha -= 0x22000000; hb -= 0x22000000; k += 68;
44 SET_FLOAT_WORD(a,ha);
45 SET_FLOAT_WORD(b,hb);
46 }
47 if(hb < 0x26800000) { /* b < 2**-50 */
48 if(hb <= 0x007fffff) { /* subnormal b or 0 */
49 if(hb==0) return a;
50 SET_FLOAT_WORD(t1,0x7e800000); /* t1=2^126 */
51 b *= t1;
52 a *= t1;
53 k -= 126;
54 } else { /* scale a and b by 2^68 */
55 ha += 0x22000000; /* a *= 2^68 */
56 hb += 0x22000000; /* b *= 2^68 */
57 k -= 68;
58 SET_FLOAT_WORD(a,ha);
59 SET_FLOAT_WORD(b,hb);
60 }
61 }
62 /* medium size a and b */
63 w = a-b;
64 if (w>b) {
65 SET_FLOAT_WORD(t1,ha&0xfffff000);
66 t2 = a-t1;
Elliott Hughes4088e3a2023-08-03 13:33:56 -070067 w = sqrtf(t1*t1-(b*(-b)-t2*(a+t1)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068 } else {
69 a = a+a;
70 SET_FLOAT_WORD(y1,hb&0xfffff000);
71 y2 = b - y1;
Elliott Hughesa0ee0782013-01-30 19:06:37 -080072 SET_FLOAT_WORD(t1,(ha+0x00800000)&0xfffff000);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073 t2 = a - t1;
Elliott Hughes4088e3a2023-08-03 13:33:56 -070074 w = sqrtf(t1*y1-(w*(-w)-(t1*y2+t2*b)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075 }
76 if(k!=0) {
Elliott Hughesbac0ebb2021-01-26 14:17:20 -080077 SET_FLOAT_WORD(t1,(127+k)<<23);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078 return t1*w;
79 } else return w;
80}