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/math/ef_pow.c in Newlib. */
28 #include "amdgcnmach.h"
32 dp_h
[] = { 0.0, 5.84960938e-01,}, /* 0x3f15c000 */
33 dp_l
[] = { 0.0, 1.56322085e-06,}, /* 0x35d1cfdc */
37 two24
= 16777216.0, /* 0x4b800000 */
38 /* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */
39 L1
= 6.0000002384e-01, /* 0x3f19999a */
40 L2
= 4.2857143283e-01, /* 0x3edb6db7 */
41 L3
= 3.3333334327e-01, /* 0x3eaaaaab */
42 L4
= 2.7272811532e-01, /* 0x3e8ba305 */
43 L5
= 2.3066075146e-01, /* 0x3e6c3255 */
44 L6
= 2.0697501302e-01, /* 0x3e53f142 */
45 P1
= 1.6666667163e-01, /* 0x3e2aaaab */
46 P2
= -2.7777778450e-03, /* 0xbb360b61 */
47 P3
= 6.6137559770e-05, /* 0x388ab355 */
48 P4
= -1.6533901999e-06, /* 0xb5ddea0e */
49 P5
= 4.1381369442e-08, /* 0x3331bb4c */
50 lg2
= 6.9314718246e-01, /* 0x3f317218 */
51 lg2_h
= 6.93145752e-01, /* 0x3f317200 */
52 lg2_l
= 1.42860654e-06, /* 0x35bfbe8c */
53 ovt
= 4.2995665694e-08, /* -(128-log2(ovfl+.5ulp)) */
54 cp
= 9.6179670095e-01, /* 0x3f76384f =2/(3ln2) */
55 cp_h
= 9.6179199219e-01, /* 0x3f763800 =head of cp */
56 cp_l
= 4.7017383622e-06, /* 0x369dc3a0 =tail of cp_h */
57 ivln2
= 1.4426950216e+00, /* 0x3fb8aa3b =1/ln2 */
58 ivln2_h
= 1.4426879883e+00, /* 0x3fb8aa00 =16b 1/ln2*/
59 ivln2_l
= 7.0526075433e-06; /* 0x36eca570 =1/ln2 tail*/
61 v64sf
v64sf_sqrtf_aux (v64sf
, v64si
);
62 v64sf
v64sf_scalbnf_aux (v64sf
, v64si
, v64si
);
64 #if defined (__has_builtin) && __has_builtin (__builtin_gcn_fabsvf)
66 DEF_VS_MATH_FUNC (v64sf
, powf
, v64sf x
, v64sf y
)
68 FUNCTION_INIT (v64sf
);
71 GET_FLOAT_WORD (hx
,x
, NO_COND
);
72 GET_FLOAT_WORD (hy
,y
, NO_COND
);
73 v64si ix
= hx
&0x7fffffff;
74 v64si iy
= hy
&0x7fffffff;
76 /* y==zero: x**0 = 1 */
77 VECTOR_IF (FLT_UWORD_IS_ZERO(iy
), cond
)
78 VECTOR_RETURN (x
+ y
, cond
& v64sf_issignalingf_inline(x
));
79 VECTOR_RETURN (VECTOR_INIT (1.0f
), cond
);
82 /* x|y==NaN return NaN unless x==1 then return 1 */
83 VECTOR_IF (FLT_UWORD_IS_NAN(ix
) | FLT_UWORD_IS_NAN(iy
), cond
)
84 VECTOR_IF2 (hx
==0x3f800000 & ~v64sf_issignalingf_inline(y
), cond2
, cond
)
85 VECTOR_RETURN (VECTOR_INIT (1.0f
), cond2
);
86 VECTOR_ELSE2 (cond2
, cond
)
87 VECTOR_RETURN (x
+ y
, cond2
);
91 /* determine if y is an odd int when x < 0
92 * yisint = 0 ... y is not an integer
93 * yisint = 1 ... y is an odd int
94 * yisint = 2 ... y is an even int
96 v64si yisint
= VECTOR_INIT (0);
98 VECTOR_IF (hx
< 0, cond
)
99 VECTOR_IF2 (iy
>= 0x4b800000, cond2
, cond
)
100 VECTOR_COND_MOVE (yisint
, VECTOR_INIT (2), cond2
); /* even integer y */
101 VECTOR_ELSEIF2 (iy
>= 0x3f800000, cond2
, cond
)
102 v64si k
= (iy
>>23)-0x7f; /* exponent */
103 v64si j
= iy
>>(23-k
);
104 VECTOR_COND_MOVE (yisint
, 2-(j
&1), cond2
& (j
<<(23-k
))==iy
);
108 /* special value of y */
109 VECTOR_IF (FLT_UWORD_IS_INFINITE(iy
), cond
) /* y is +-inf */
110 VECTOR_IF2 (ix
==0x3f800000, cond2
, cond
)
111 VECTOR_RETURN (VECTOR_INIT (1.0f
), cond2
); /* +-1**+-inf = 1 */
112 VECTOR_ELSEIF2 (ix
> 0x3f800000, cond2
, cond
) /* (|x|>1)**+-inf = inf,0 */
113 VECTOR_RETURN (y
, cond2
& (hy
>= 0));
114 VECTOR_RETURN (VECTOR_INIT (0.0f
), cond2
);
115 VECTOR_ELSE2 (cond2
, cond
) /* (|x|<1)**-,+inf = inf,0 */
116 VECTOR_RETURN (-y
, cond2
& (hy
<0));
117 VECTOR_RETURN (VECTOR_INIT (0.0f
), cond2
);
120 VECTOR_IF (iy
==0x3f800000, cond
) /* y is +-1 */
121 VECTOR_RETURN (VECTOR_INIT (1.0f
) / x
, cond
& (hy
<0));
122 VECTOR_RETURN (x
, cond
);
125 VECTOR_RETURN (x
*x
, hy
==0x40000000); /* y is 2 */
128 VECTOR_RETURN (v64sf_sqrtf_aux (x
, __mask
), (hy
==0x3f000000) & (hx
>= 0));
130 v64sf ax
= __builtin_gcn_fabsvf(x
);
131 /* special value of x */
132 VECTOR_IF (FLT_UWORD_IS_INFINITE(ix
)|FLT_UWORD_IS_ZERO(ix
)|ix
==0x3f800000, cond
)
133 v64sf z
= ax
; /*x is +-0,+-inf,+-1*/
134 VECTOR_COND_MOVE (z
, VECTOR_INIT (1.0f
) / z
, cond
& (hy
< 0)); /* z = (1/|x|) */
135 VECTOR_IF2 (hx
<0, cond2
, cond
)
136 VECTOR_IF2 (((ix
-0x3f800000)|yisint
)==0, cond3
, cond2
)
137 /* (-1)**non-int is NaN */
138 VECTOR_COND_MOVE (z
, (z
-z
)/(z
-z
), cond3
);
139 VECTOR_ELSEIF2 (yisint
==1, cond3
, cond2
)
140 /* (x<0)**odd = -(|x|**odd) */
141 VECTOR_COND_MOVE (z
, -z
, cond3
);
144 VECTOR_RETURN (z
, cond
);
147 /* (x<0)**(non-int) is NaN */
148 VECTOR_RETURN ((x
-x
)/(x
-x
), ((((hx
>> 31) & 1) - 1)|yisint
)==0);
153 VECTOR_IF (iy
>0x4d000000, cond
) /* if |y| > 2**27 */
154 /* over/underflow if x is not close to one */
155 VECTOR_IF2(ix
<0x3f7ffff4, cond2
, cond
)
156 VECTOR_RETURN (v64sf_math_oflowf(VECTOR_INIT (0)), cond2
& (hy
< 0));
157 VECTOR_RETURN (v64sf_math_uflowf(VECTOR_INIT (0)), cond2
);
159 VECTOR_IF2(ix
>0x3f800007, cond2
, cond
)
160 VECTOR_RETURN (v64sf_math_oflowf(VECTOR_INIT (0)), cond2
& (hy
> 0));
161 VECTOR_RETURN (v64sf_math_uflowf(VECTOR_INIT (0)), cond2
);
163 /* now |1-x| is tiny <= 2**-20, suffice to compute
164 log(x) by x-x^2/2+x^3/3-x^4/4 */
165 v64sf t
= ax
-1; /* t has 20 trailing zeros */
166 v64sf w
= (t
*t
)*(0.5f
-t
*(0.333333333333f
-t
*0.25f
));
167 v64sf u
= ivln2_h
*t
; /* ivln2_h has 16 sig. bits */
168 v64sf v
= t
*ivln2_l
-w
*ivln2
;
169 VECTOR_COND_MOVE (t1
, u
+v
, cond
);
171 GET_FLOAT_WORD(is
,t1
, cond
);
172 SET_FLOAT_WORD(t1
,is
&0xfffff000, cond
);
173 VECTOR_COND_MOVE (t2
, v
-(t1
-u
), cond
);
174 VECTOR_RETURN (VECTOR_INIT (0.123456f
), cond
);
176 v64si n
= VECTOR_INIT (0);
177 /* take care subnormal number */
178 VECTOR_IF2 (FLT_UWORD_IS_SUBNORMAL(ix
), cond2
, cond
)
179 VECTOR_COND_MOVE (ax
, ax
* two24
, cond
);
180 VECTOR_COND_MOVE (n
, n
- 24, cond2
);
181 GET_FLOAT_WORD(ix
,ax
, cond2
);
184 v64si j
= ix
&0x007fffff;
185 /* determine interval */
186 v64si ix
= j
|0x3f800000; /* normalize ix */
188 VECTOR_IF2 (j
<=0x1cc471, cond2
, cond
)
189 VECTOR_COND_MOVE (k
, VECTOR_INIT (0), cond2
); /* |x|<sqrt(3/2) */
190 VECTOR_ELSEIF2 (j
<0x5db3d7, cond2
, cond
)
191 VECTOR_COND_MOVE (k
, VECTOR_INIT (1), cond2
); /* |x|<sqrt(3) */
192 VECTOR_ELSE2 (cond2
, cond
)
193 VECTOR_COND_MOVE (k
, VECTOR_INIT (0), cond2
);
194 VECTOR_COND_MOVE (n
, n
+ 1, cond2
);
195 VECTOR_COND_MOVE (ix
, ix
- 0x00800000, cond2
);
197 SET_FLOAT_WORD(ax
,ix
, cond
);
199 /* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
200 /* bp[0]=1.0, bp[1]=1.5 */
201 v64sf bp_k
= VECTOR_MERGE (VECTOR_INIT (bp
[1]), VECTOR_INIT (bp
[0]), k
== 1);
203 v64sf v
= 1.0f
/(ax
+bp_k
);
207 GET_FLOAT_WORD(is
,s_h
, cond
);
208 SET_FLOAT_WORD(s_h
,is
&0xfffff000, cond
);
209 /* t_h=ax+bp[k] High */
211 SET_FLOAT_WORD(t_h
,((ix
>>1)|0x20000000)+0x0040000+(k
<<21), cond
);
212 v64sf t_l
= ax
- (t_h
-bp_k
);
213 v64sf s_l
= v
*((u
-s_h
*t_h
)-s_h
*t_l
);
214 /* compute log(ax) */
216 v64sf r
= s2
*s2
*(L1
+s2
*(L2
+s2
*(L3
+s2
*(L4
+s2
*(L5
+s2
*L6
)))));
219 t_h
= __builtin_convertvector(3.0f
+s2
+r
, v64sf
);
220 GET_FLOAT_WORD(is
,t_h
, cond
);
221 SET_FLOAT_WORD(t_h
,is
&0xfffff000, cond
);
222 t_l
= r
-((t_h
-3.0f
)-s2
);
223 /* u+v = s*(1+...) */
226 /* 2/(3log2)*(s+...) */
228 GET_FLOAT_WORD(is
,p_h
, cond
);
229 SET_FLOAT_WORD(p_h
,is
&0xfffff000, cond
);
230 v64sf p_l
= v
-(p_h
-u
);
231 v64sf z_h
= cp_h
*p_h
; /* cp_h+cp_l = 2/(3*log2) */
232 v64sf dp_l_k
= VECTOR_MERGE (VECTOR_INIT (dp_l
[1]), VECTOR_INIT (dp_l
[0]), k
== 1);
233 v64sf z_l
= cp_l
*p_h
+p_l
*cp
+dp_l_k
;
234 /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */
235 v64sf t
= __builtin_convertvector (n
, v64sf
);
236 v64sf dp_h_k
= VECTOR_MERGE (VECTOR_INIT (dp_h
[1]), VECTOR_INIT (dp_h
[0]), k
== 1);
237 VECTOR_COND_MOVE (t1
, (((z_h
+z_l
)+dp_h_k
)+t
), cond
);
238 GET_FLOAT_WORD(is
,t1
, cond
);
239 SET_FLOAT_WORD(t1
,is
&0xfffff000, cond
);
240 VECTOR_COND_MOVE (t2
, z_l
-(((t1
-t
)-dp_h_k
)-z_h
), cond
);
243 v64sf s
= VECTOR_INIT (1.0f
); /* s (sign of result -ve**odd) = -1 else = 1 */
244 VECTOR_COND_MOVE (s
, VECTOR_INIT (-1.0f
), /* (-ve)**(odd int) */
245 ((hx
>>31) != 0)&(yisint
== 1));
247 /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
249 GET_FLOAT_WORD(is
,y
, NO_COND
);
251 SET_FLOAT_WORD(y1
,is
&0xfffff000, NO_COND
);
252 v64sf p_l
= (y
-y1
)*t1
+y
*t2
;
256 GET_FLOAT_WORD(j
,z
, NO_COND
);
257 v64si i
= j
&0x7fffffff;
258 VECTOR_IF (j
>0, cond
)
259 VECTOR_RETURN (v64sf_math_oflowf(s
<0), cond
& i
>FLT_UWORD_EXP_MAX
); /* overflow */
260 VECTOR_RETURN (v64sf_math_oflowf(s
<0), cond
& (i
==FLT_UWORD_EXP_MAX
)
261 & (p_l
+ovt
>z
-p_h
)); /* overflow */
263 VECTOR_RETURN (v64sf_math_uflowf(s
<0), cond
& (i
>FLT_UWORD_EXP_MIN
)); /* underflow */
264 VECTOR_RETURN (v64sf_math_uflowf(s
<0), cond
& (i
==FLT_UWORD_EXP_MIN
)
265 & (p_l
<=z
-p_h
)); /* underflow */
268 * compute 2**(p_h+p_l)
270 v64si k
= (i
>>23)-0x7f;
271 v64si n
= VECTOR_INIT (0);
272 VECTOR_IF (i
>0x3f000000, cond
) /* if |z| > 0.5, set n = [z+0.5] */
273 VECTOR_COND_MOVE (n
, j
+(0x00800000>>(k
+1)), cond
);
274 k
= ((n
&0x7fffffff)>>23)-0x7f; /* new k for n */
276 SET_FLOAT_WORD(t
,n
&~(0x007fffff>>k
), cond
);
277 VECTOR_COND_MOVE (n
, ((n
&0x007fffff)|0x00800000)>>(23-k
), cond
);
278 VECTOR_COND_MOVE (n
, -n
, cond
& (j
<0));
279 VECTOR_COND_MOVE (p_h
, p_h
- t
, cond
);
282 GET_FLOAT_WORD(is
,t
, NO_COND
);
283 SET_FLOAT_WORD(t
,is
&0xfffff000, NO_COND
);
285 v64sf v
= (p_l
-(t
-p_h
))*lg2
+t
*lg2_l
;
289 t1
= z
- t
*(P1
+t
*(P2
+t
*(P3
+t
*(P4
+t
*P5
))));
290 v64sf r
= (z
*t1
)/(t1
-2.0f
)-(w
+z
*w
);
291 z
= VECTOR_INIT (1.0f
)-(r
-z
);
292 GET_FLOAT_WORD(j
,z
, NO_COND
);
294 VECTOR_IF ((j
>>23)<=0, cond
)
295 VECTOR_COND_MOVE (z
, v64sf_scalbnf_aux(z
, n
, __mask
), cond
); /* subnormal output */
297 SET_FLOAT_WORD(z
, j
, cond
);
299 VECTOR_RETURN (s
*z
, NO_COND
);
304 DEF_VARIANTS2 (powf
, sf
, sf
)