2 * Copyright 2023 Siemens
4 * The authors hereby grant permission to use, copy, modify, distribute,
5 * and license this software and its documentation for any purpose, provided
6 * that existing copyright notices are retained in all copies and that this
7 * notice is included verbatim in any distributions. No written agreement,
8 * license, or royalty fee is required for any of the authorized uses.
9 * Modifications to this software may be copyrighted by their authors
10 * and need not follow the licensing terms described here, provided that
11 * the new terms are clearly indicated on the first page of each file where
16 * ====================================================
17 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
19 * Developed at SunPro, a Sun Microsystems, Inc. business.
20 * Permission to use, copy, modify, and distribute this
21 * software is freely granted, provided that this notice
23 * ====================================================
26 /* Based on newlib/libm/mathfp/e_hypot.c in Newlib. */
28 #include "amdgcnmach.h"
30 v64df
v64df_sqrt_aux (v64df
, v64di
);
32 DEF_VD_MATH_FUNC (v64df
, hypot
, v64df x
, v64df y
)
34 FUNCTION_INIT (v64df
);
40 GET_HIGH_WORD (ha
, x
, NO_COND
);
43 GET_HIGH_WORD (hb
, y
, NO_COND
);
46 VECTOR_IF (hb
> ha
, cond
)
47 VECTOR_COND_MOVE (a
, y
, cond
);
48 VECTOR_COND_MOVE (b
, x
, cond
);
50 VECTOR_COND_MOVE (ha
, hb
, cond
);
51 VECTOR_COND_MOVE (hb
, j
, cond
);
53 SET_HIGH_WORD (a
, ha
, NO_COND
); /* a <- |a| */
54 SET_HIGH_WORD (b
, hb
, NO_COND
); /* b <- |b| */
55 VECTOR_IF((ha
- hb
) > 0x3c00000L
, cond
) // x/y > 2**60 */
56 VECTOR_RETURN (a
+ b
, cond
);
59 v64si k
= VECTOR_INIT (0);
61 VECTOR_IF (ha
> 0x5f300000L
, cond
) /* a>2**500 */
62 VECTOR_IF2 (ha
>= 0x7ff00000L
, cond2
, cond
) /* Inf or NaN */
63 v64df w
= a
+ b
; // for sNaN */
65 GET_LOW_WORD (low
, a
, cond2
);
66 VECTOR_COND_MOVE (w
, a
, cond2
& (((ha
& 0xfffff) | low
) == 0));
67 GET_LOW_WORD (low
, b
, cond2
);
68 VECTOR_COND_MOVE (w
, b
, cond2
& (((hb
& 0xfffff) | low
) == 0));
69 VECTOR_RETURN (w
, cond
);
71 /* scale a and b by 2**-600 */
72 VECTOR_COND_MOVE (ha
, ha
- 0x25800000, cond
);
73 VECTOR_COND_MOVE (hb
, hb
- 0x25800000, cond
);
74 VECTOR_COND_MOVE (k
, k
+ 600, cond
);
75 SET_HIGH_WORD (a
, ha
, cond
);
76 SET_HIGH_WORD (b
, hb
, cond
);
78 VECTOR_IF (hb
< 0x20b00000, cond
) /* b < 2**-500 */
79 VECTOR_IF2 (hb
<= 0x000fffff, cond2
, cond
) /* subnormal b or 0 */
81 GET_LOW_WORD (low
, b
, cond
);
82 VECTOR_RETURN (a
, cond2
& ((hb
| low
) == 0));
84 v64df t1
= VECTOR_INIT (0.0);
85 SET_HIGH_WORD (t1
, VECTOR_INIT (0x7fd00000), cond2
);
86 VECTOR_COND_MOVE (b
, b
* t1
, cond2
);
87 VECTOR_COND_MOVE (a
, a
* t1
, cond2
);
88 VECTOR_COND_MOVE (k
, k
- 1022, cond2
);
89 VECTOR_ELSE2 (cond2
, cond
) /* scale a and b by 2^600 */
90 VECTOR_COND_MOVE (ha
, ha
+ 0x25800000, cond2
); /* a *= 2^600 */
91 VECTOR_COND_MOVE (hb
, hb
+ 0x25800000, cond2
); /* b *= 2^600 */
92 VECTOR_COND_MOVE (k
, k
- 600, cond2
);
93 SET_HIGH_WORD (a
, ha
, cond2
);
94 SET_HIGH_WORD (b
, hb
, cond2
);
97 /* medium size a and b */
99 VECTOR_IF (w
> b
, cond
)
100 v64df t1
= VECTOR_INIT (0.0);
101 SET_HIGH_WORD (t1
, ha
, cond
);
103 VECTOR_COND_MOVE (w
, v64df_sqrt_aux (t1
*t1
- (b
*(-b
) - t2
* (a
+ t1
)), __mask
), cond
);
105 VECTOR_COND_MOVE (a
, a
+a
, cond
);
106 v64df y1
= VECTOR_INIT (0.0);
107 SET_HIGH_WORD (y1
, hb
, cond
);
110 SET_HIGH_WORD (t1
, ha
+ 0x00100000, cond
);
112 VECTOR_COND_MOVE (w
, v64df_sqrt_aux (t1
*y1
- (w
*(-w
) - (t1
*y2
+ t2
*b
)), __mask
), cond
);
114 VECTOR_IF (k
!= 0, cond
)
116 v64df t1
= VECTOR_INIT (1.0);
117 GET_HIGH_WORD (high
, t1
, cond
);
118 SET_HIGH_WORD (t1
, high
+ (k
<< 20), cond
);
119 VECTOR_RETURN (t1
* w
, cond
);
121 VECTOR_RETURN (w
, cond
);
127 DEF_VARIANTS2 (hypot
, df
, df
)