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/ef_hypot.c in Newlib. */
28 #include "amdgcnmach.h"
30 v64sf
v64sf_sqrtf_aux (v64sf
, v64si
);
32 DEF_VS_MATH_FUNC (v64sf
, hypotf
, v64sf x
, v64sf y
)
34 FUNCTION_INIT (v64sf
);
40 GET_FLOAT_WORD (ha
, x
, NO_COND
);
43 GET_FLOAT_WORD (hb
, y
, NO_COND
);
46 VECTOR_IF (hb
> ha
, cond
)
48 VECTOR_COND_MOVE (ha
, hb
, cond
);
49 VECTOR_COND_MOVE (hb
, j
, cond
);
51 SET_FLOAT_WORD (a
, ha
, NO_COND
); /* a <- |a| */
52 SET_FLOAT_WORD (b
, hb
, NO_COND
); /* b <- |b| */
53 VECTOR_IF((ha
- hb
) > 0xf000000L
, cond
) // x/y > 2**30 */
54 VECTOR_RETURN (a
+ b
, cond
);
57 v64si k
= VECTOR_INIT (0);
59 VECTOR_IF (ha
> 0x58800000L
, cond
) /* a>2**50 */
60 VECTOR_IF2 (ha
>= 0x7f800000L
, cond2
, cond
) /* Inf or NaN */
61 v64sf w
= a
+ b
; // for sNaN */
62 VECTOR_COND_MOVE (w
, a
, cond2
& (ha
== 0x7f800000));
63 VECTOR_COND_MOVE (w
, b
, cond2
& (hb
== 0x7f800000));
64 VECTOR_RETURN (w
, cond
);
66 /* scale a and b by 2**-60 */
67 VECTOR_COND_MOVE (ha
, ha
- 0x5d800000, cond
);
68 VECTOR_COND_MOVE (hb
, hb
- 0x5d800000, cond
);
69 VECTOR_COND_MOVE (k
, k
+ 60, cond
);
70 SET_FLOAT_WORD (a
, ha
, cond
);
71 SET_FLOAT_WORD (b
, hb
, cond
);
73 VECTOR_IF (hb
< 0x26800000, cond
) /* b < 2**-50 */
74 VECTOR_IF2 (hb
<= 0x007fffff, cond2
, cond
) /* subnormal b or 0 */
75 VECTOR_RETURN (a
, cond2
& (hb
== 0));
78 SET_FLOAT_WORD (t1
, VECTOR_INIT (0x3f000000), cond2
);
79 VECTOR_COND_MOVE (b
, b
* t1
, cond2
);
80 VECTOR_COND_MOVE (a
, a
* t1
, cond2
);
81 VECTOR_COND_MOVE (k
, k
- 126, cond2
);
82 VECTOR_ELSE2 (cond2
, cond
) /* scale a and b by 2^60 */
83 VECTOR_COND_MOVE (ha
, ha
+ 0x5d800000, cond2
); /* a *= 2^60 */
84 VECTOR_COND_MOVE (hb
, hb
+ 0x5d800000, cond2
); /* b *= 2^60 */
85 VECTOR_COND_MOVE (k
, k
- 60, cond2
);
86 SET_FLOAT_WORD (a
, ha
, cond2
);
87 SET_FLOAT_WORD (b
, hb
, cond2
);
90 /* medium size a and b */
92 VECTOR_IF (w
> b
, cond
)
94 SET_FLOAT_WORD (t1
, ha
& 0xfffff000, cond
);
96 VECTOR_COND_MOVE (w
, v64sf_sqrtf_aux (t1
*t1
- (b
*(-b
) - t2
* (a
+ t1
)), __mask
), cond
);
98 VECTOR_COND_MOVE (a
, a
+a
, cond
);
100 SET_FLOAT_WORD (y1
, hb
& 0xfffff000, cond
);
103 SET_FLOAT_WORD (t1
, ha
+ 0x00800000, cond
);
105 VECTOR_COND_MOVE (w
, v64sf_sqrtf_aux (t1
*y1
- (w
*(-w
) - (t1
*y2
+ t2
*b
)), __mask
), cond
);
107 VECTOR_IF (k
!= 0, cond
)
109 SET_FLOAT_WORD (t1
, 0x3f800000 + (k
<< 23), cond
);
110 VECTOR_RETURN (t1
* w
, cond
);
112 VECTOR_RETURN (w
, cond
);
118 DEF_VARIANTS2 (hypotf
, sf
, sf
)