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/e_pow.c in Newlib. */
28 #include "amdgcnmach.h"
32 dp_h
[] = { 0.0, 5.84962487220764160156e-01,}, /* 0x3FE2B803, 0x40000000 */
33 dp_l
[] = { 0.0, 1.35003920212974897128e-08,}, /* 0x3E4CFDEB, 0x43CFD006 */
37 two53
= 9007199254740992.0, /* 0x43400000, 0x00000000 */
38 /* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */
39 L1
= 5.99999999999994648725e-01, /* 0x3FE33333, 0x33333303 */
40 L2
= 4.28571428578550184252e-01, /* 0x3FDB6DB6, 0xDB6FABFF */
41 L3
= 3.33333329818377432918e-01, /* 0x3FD55555, 0x518F264D */
42 L4
= 2.72728123808534006489e-01, /* 0x3FD17460, 0xA91D4101 */
43 L5
= 2.30660745775561754067e-01, /* 0x3FCD864A, 0x93C9DB65 */
44 L6
= 2.06975017800338417784e-01, /* 0x3FCA7E28, 0x4A454EEF */
45 P1
= 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */
46 P2
= -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */
47 P3
= 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */
48 P4
= -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */
49 P5
= 4.13813679705723846039e-08, /* 0x3E663769, 0x72BEA4D0 */
50 lg2
= 6.93147180559945286227e-01, /* 0x3FE62E42, 0xFEFA39EF */
51 lg2_h
= 6.93147182464599609375e-01, /* 0x3FE62E43, 0x00000000 */
52 lg2_l
= -1.90465429995776804525e-09, /* 0xBE205C61, 0x0CA86C39 */
53 ovt
= 8.0085662595372944372e-0017, /* -(1024-log2(ovfl+.5ulp)) */
54 cp
= 9.61796693925975554329e-01, /* 0x3FEEC709, 0xDC3A03FD =2/(3ln2) */
55 cp_h
= 9.61796700954437255859e-01, /* 0x3FEEC709, 0xE0000000 =(float)cp */
56 cp_l
= -7.02846165095275826516e-09, /* 0xBE3E2FE0, 0x145B01F5 =tail of cp_h*/
57 ivln2
= 1.44269504088896338700e+00, /* 0x3FF71547, 0x652B82FE =1/ln2 */
58 ivln2_h
= 1.44269502162933349609e+00, /* 0x3FF71547, 0x60000000 =24b 1/ln2*/
59 ivln2_l
= 1.92596299112661746887e-08; /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/
61 v64df
v64df_sqrt_aux (v64df
, v64di
);
62 v64df
v64df_scalbn_aux (v64df
, v64si
, v64di
);
64 static v64df
v64df_math_oflow (v64di sign
)
67 return VECTOR_MERGE (VECTOR_INIT (-0x1p
769),
68 VECTOR_INIT (0x1p
769), sign
) * 0x1p
769;
71 static v64df
v64df_math_uflow (v64di sign
)
74 return VECTOR_MERGE (VECTOR_INIT (-0x1p
-767),
75 VECTOR_INIT (0x1p
-767), sign
) * 0x1p
-767;
78 static v64si
v64df_issignaling_inline (v64df x
)
80 v64si __mask
= VECTOR_INIT (-1);
82 GET_HIGH_WORD (ix
, x
, NO_COND
);
83 /* Use IEEE-754 2008 encoding - i.e. exponent bits all 1, MSB of
84 significand is 0 for signalling NaN. */
85 return ((ix
& 0x7ff00000) == 0x7ff00000) & ((ix
& 0x00080000) == 0);
88 #if defined (__has_builtin) && __has_builtin (__builtin_gcn_fabsv)
90 DEF_VD_MATH_FUNC (v64df
, pow
, v64df x
, v64df y
)
92 FUNCTION_INIT (v64df
);
95 EXTRACT_WORDS(hx
,lx
,x
);
96 EXTRACT_WORDS(hy
,ly
,y
);
97 v64si ix
= hx
&0x7fffffff;
98 v64si iy
= hy
&0x7fffffff;
100 /* y==zero: x**0 = 1 unless x is snan */
101 VECTOR_IF ((iy
|ly
)==0, cond
)
102 VECTOR_RETURN (x
+ y
, cond
& v64df_issignaling_inline(x
));
103 VECTOR_RETURN (VECTOR_INIT (1.0), cond
);
106 /* x|y==NaN return NaN unless x==1 then return 1 */
107 VECTOR_IF ((ix
> 0x7ff00000) | ((ix
==0x7ff00000)&(lx
!=0))
108 | (iy
> 0x7ff00000) | ((iy
==0x7ff00000)&(ly
!=0)), cond
)
109 VECTOR_RETURN (VECTOR_INIT (1.0), cond
& ((hx
-0x3ff00000)|lx
)==0
110 & ~v64df_issignaling_inline(y
));
111 VECTOR_RETURN (x
+ y
, cond
);
114 /* determine if y is an odd int when x < 0
115 * yisint = 0 ... y is not an integer
116 * yisint = 1 ... y is an odd int
117 * yisint = 2 ... y is an even int
119 v64si yisint
= VECTOR_INIT (0);
121 VECTOR_IF (hx
< 0, cond
)
122 VECTOR_IF2(iy
>=0x43400000, cond2
, cond
)
123 VECTOR_COND_MOVE (yisint
, VECTOR_INIT (2), cond2
); /* even integer y */
124 VECTOR_ELSEIF2 (iy
>=0x3ff00000, cond2
, cond
)
125 v64si k
= (iy
>>20)-0x3ff; /* exponent */
126 VECTOR_IF2 (k
>20, cond3
, cond2
)
127 v64si j
= ly
>>(52-k
);
128 VECTOR_COND_MOVE (yisint
, 2-(j
&1), cond3
& (j
<<(52-k
))==ly
);
129 VECTOR_ELSEIF2 (ly
==0, cond3
, cond2
)
130 v64si j
= iy
>>(20-k
);
131 VECTOR_COND_MOVE (yisint
, 2-(j
&1), cond3
& (j
<<(20-k
))==iy
);
136 /* special value of y */
137 VECTOR_IF (ly
==0, cond
)
138 VECTOR_IF2 (iy
==0x7ff00000, cond2
, cond
) /* y is +-inf */
139 VECTOR_IF2 (((ix
-0x3ff00000)|lx
)==0, cond3
, cond2
)
140 VECTOR_RETURN (VECTOR_INIT (1.0), cond3
); /* +-1**+-inf = 1 */
141 VECTOR_ELSEIF2 (ix
>= 0x3ff00000, cond3
, cond2
) /* (|x|>1)**+-inf = inf,0 */
142 VECTOR_RETURN (y
, cond3
& hy
>=0);
143 VECTOR_RETURN (VECTOR_INIT (0.0), cond3
);
144 VECTOR_ELSE2 (cond3
, cond2
) /* (|x|<1)**-,+inf = inf,0 */
145 VECTOR_RETURN (-y
, cond3
& hy
<0);
146 VECTOR_RETURN (VECTOR_INIT (0.0), cond3
);
149 VECTOR_IF2 (iy
==0x3ff00000, cond2
, cond
) /* y is +-1 */
150 VECTOR_RETURN (VECTOR_INIT (1.0) / x
, cond2
& hy
<0);
151 VECTOR_RETURN (x
, cond2
);
153 VECTOR_RETURN (x
*x
, cond
& hy
==0x40000000); /* y is 2 */
156 VECTOR_RETURN (v64df_sqrt_aux (x
, __mask
), cond
& (hy
==0x3fe00000) & (hx
>=0));
159 v64df ax
= __builtin_gcn_fabsv(x
);
160 /* special value of x */
161 VECTOR_IF (lx
==0, cond
)
162 VECTOR_IF2 ((ix
==0x7ff00000)|(ix
==0)|(ix
==0x3ff00000), cond2
, cond
)
163 v64df z
= ax
; /*x is +-0,+-inf,+-1*/
164 VECTOR_COND_MOVE (z
, VECTOR_INIT (1.0) / z
, cond2
& (hy
<0)); /* z = (1/|x|) */
165 VECTOR_IF2 (hx
<0, cond3
, cond2
)
166 VECTOR_IF2 (((ix
-0x3ff00000)|yisint
)==0, cond4
, cond3
)
167 VECTOR_COND_MOVE (z
, (z
-z
)/(z
-z
), cond4
); /* (-1)**non-int is NaN */
168 VECTOR_ELSEIF2 (yisint
==1, cond4
, cond3
)
169 VECTOR_COND_MOVE (z
, -z
, cond4
); /* (x<0)**odd = -(|x|**odd) */
172 VECTOR_RETURN (z
, cond2
);
176 /* (x<0)**(non-int) is NaN */
177 VECTOR_RETURN ((x
-x
)/(x
-x
), ((((hx
>> 31) & 1) - 1)|yisint
)==0);
182 VECTOR_IF(iy
>0x41e00000, cond
) /* if |y| > 2**31 */
183 VECTOR_IF2 (iy
>0x43f00000, cond2
, cond
) /* if |y| > 2**64, must o/uflow */
184 VECTOR_IF2 (ix
<=0x3fefffff, cond3
, cond2
)
185 VECTOR_RETURN (v64df_math_oflow (VECTOR_INIT (0L)), cond3
& (hy
<0));
186 VECTOR_RETURN (v64df_math_uflow (VECTOR_INIT (0L)), cond3
);
188 VECTOR_IF2 (ix
>=0x3ff00000, cond3
, cond2
)
189 VECTOR_RETURN (v64df_math_oflow (VECTOR_INIT (0L)), cond3
& (hy
>0));
190 VECTOR_RETURN (v64df_math_uflow (VECTOR_INIT (0L)), cond3
);
193 /* over/underflow if x is not close to one */
194 VECTOR_IF2 (ix
<0x3fefffff, cond2
, cond
)
195 VECTOR_RETURN (v64df_math_oflow (VECTOR_INIT (0L)), cond2
& (hy
<0));
196 VECTOR_RETURN (v64df_math_uflow (VECTOR_INIT (0L)), cond2
);
198 VECTOR_IF2 (ix
>0x3ff00000, cond2
, cond
)
199 VECTOR_RETURN (v64df_math_oflow (VECTOR_INIT (0L)), cond2
& (hy
>0));
200 VECTOR_RETURN (v64df_math_uflow (VECTOR_INIT (0L)), cond2
);
202 /* now |1-x| is tiny <= 2**-20, suffice to compute
203 log(x) by x-x^2/2+x^3/3-x^4/4 */
204 v64df t
= ax
-1; /* t has 20 trailing zeros */
205 v64df w
= (t
*t
)*(0.5-t
*(0.3333333333333333333333-t
*0.25));
206 v64df u
= ivln2_h
*t
; /* ivln2_h has 21 sig. bits */
207 v64df v
= t
*ivln2_l
-w
*ivln2
;
208 VECTOR_COND_MOVE (t1
, u
+v
, cond
);
209 SET_LOW_WORD (t1
, VECTOR_INIT (0), cond
);
210 VECTOR_COND_MOVE (t2
, v
-(t1
-u
), cond
);
212 v64si n
= VECTOR_INIT (0);
213 /* take care subnormal number */
214 VECTOR_IF2 (ix
<0x00100000, cond2
, cond
)
215 VECTOR_COND_MOVE (ax
, ax
* two53
, cond2
);
216 VECTOR_COND_MOVE (n
, n
- 53, cond2
);
217 GET_HIGH_WORD (ix
, ax
, cond2
);
219 n
+= ((ix
)>>20)-0x3ff;
220 v64si j
= ix
&0x000fffff;
221 /* determine interval */
222 ix
= j
|0x3ff00000; /* normalize ix */
224 VECTOR_IF2 (j
<=0x3988E, cond2
, cond
)
225 VECTOR_COND_MOVE (k
, VECTOR_INIT (0), cond2
); /* |x|<sqrt(3/2) */
226 VECTOR_ELSEIF2 (j
<0xBB67A, cond2
, cond
)
227 VECTOR_COND_MOVE (k
, VECTOR_INIT (1), cond2
); /* |x|<sqrt(3) */
228 VECTOR_ELSE2 (cond2
, cond
)
229 VECTOR_COND_MOVE (k
, VECTOR_INIT (0), cond2
);
230 VECTOR_COND_MOVE (n
, n
+ 1, cond2
);
231 VECTOR_COND_MOVE (ix
, ix
- 0x00100000, cond2
);
233 SET_HIGH_WORD (ax
, ix
, cond
);
235 /* compute s = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
236 v64df bp_k
= VECTOR_MERGE (VECTOR_INIT (bp
[1]), VECTOR_INIT (bp
[0]), k
== 1);
237 v64df u
= ax
-bp_k
; /* bp[0]=1.0, bp[1]=1.5 */
238 v64df v
= 1.0/(ax
+bp_k
);
241 SET_LOW_WORD (s_h
, VECTOR_INIT (0), cond
);
242 /* t_h=ax+bp[k] High */
243 v64df t_h
= VECTOR_INIT (0.0);
244 SET_HIGH_WORD (t_h
,((ix
>>1)|0x20000000)+0x00080000+(k
<<18), cond
);
245 v64df t_l
= ax
- (t_h
-bp_k
);
246 v64df s_l
= v
*((u
-s_h
*t_h
)-s_h
*t_l
);
247 /* compute log(ax) */
249 v64df r
= s2
*s2
*(L1
+s2
*(L2
+s2
*(L3
+s2
*(L4
+s2
*(L5
+s2
*L6
)))));
253 SET_LOW_WORD (t_h
, VECTOR_INIT (0), cond
);
254 t_l
= r
-((t_h
-3.0)-s2
);
255 /* u+v = s*(1+...) */
258 /* 2/(3log2)*(s+...) */
260 SET_LOW_WORD (p_h
, VECTOR_INIT (0), cond
);
261 v64df p_l
= v
-(p_h
-u
);
262 v64df z_h
= cp_h
*p_h
; /* cp_h+cp_l = 2/(3*log2) */
263 v64df dp_l_k
= VECTOR_MERGE (VECTOR_INIT (dp_l
[1]), VECTOR_INIT (dp_l
[0]), k
== 1);
264 v64df z_l
= cp_l
*p_h
+p_l
*cp
+dp_l_k
;
265 /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */
266 v64df t
= __builtin_convertvector (n
, v64df
);
267 v64df dp_h_k
= VECTOR_MERGE (VECTOR_INIT (dp_h
[1]), VECTOR_INIT (dp_h
[0]), k
== 1);
268 VECTOR_COND_MOVE (t1
, ((z_h
+z_l
)+dp_h_k
)+t
, cond
);
269 SET_LOW_WORD (t1
, VECTOR_INIT (0), cond
);
270 VECTOR_COND_MOVE (t2
, z_l
-(((t1
-t
)-dp_h_k
)-z_h
), cond
);
273 v64df s
= VECTOR_INIT (1.0); /* s (sign of result -ve**odd) = -1 else = 1 */
274 VECTOR_COND_MOVE (s
, VECTOR_INIT (-1.0), /* (-ve)**(odd int) */
275 ((hx
>>31) != 0)&(yisint
== 1));
277 /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
279 SET_LOW_WORD (y1
, VECTOR_INIT (0), NO_COND
);
280 v64df p_l
= (y
-y1
)*t1
+y
*t2
;
284 EXTRACT_WORDS(j
, i
, z
);
285 VECTOR_IF (j
>=0x40900000, cond
) /* z >= 1024 */
287 v64di cond_di
= __builtin_convertvector (cond
, v64di
);
288 VECTOR_RETURN (v64df_math_oflow(s
<0), cond
& (((j
-0x40900000)|i
)!=0)); /* overflow */
289 VECTOR_RETURN (v64df_math_oflow(s
<0), cond_di
& (p_l
+ovt
>z
-p_h
)); /* overflow */
290 VECTOR_ELSEIF ((j
&0x7fffffff)>=0x4090cc00, cond
) /* z <= -1075 */
292 v64di cond_di
= __builtin_convertvector (cond
, v64di
);
293 VECTOR_RETURN (v64df_math_uflow(s
<0), cond
& (((j
-0xc090cc00)|i
)!=0)); /* underflow */
294 VECTOR_RETURN (v64df_math_uflow(s
<0), cond_di
& (p_l
<=z
-p_h
)); /* underflow */
298 * compute 2**(p_h+p_l)
301 v64si k
= (i
>>20)-0x3ff;
302 v64si n
= VECTOR_INIT (0);
303 VECTOR_IF (i
>0x3fe00000, cond
) /* if |z| > 0.5, set n = [z+0.5] */
304 VECTOR_COND_MOVE (n
, j
+(0x00100000>>(k
+1)), cond
);
305 k
= ((n
&0x7fffffff)>>20)-0x3ff; /* new k for n */
306 v64df t
= VECTOR_INIT (0.0);
307 SET_HIGH_WORD(t
, n
&~(0x000fffff>>k
), cond
);
308 VECTOR_COND_MOVE (n
, ((n
&0x000fffff)|0x00100000)>>(20-k
), cond
);
309 VECTOR_COND_MOVE (n
, -n
, cond
& (j
<0));
310 VECTOR_COND_MOVE (p_h
, p_h
- t
, cond
);
313 SET_LOW_WORD(t
, VECTOR_INIT (0), NO_COND
);
315 v64df v
= (p_l
-(t
-p_h
))*lg2
+t
*lg2_l
;
319 t1
= z
- t
*(P1
+t
*(P2
+t
*(P3
+t
*(P4
+t
*P5
))));
320 v64df r
= (z
*t1
)/(t1
-two
)-(w
+z
*w
);
321 z
= VECTOR_INIT (1.0)-(r
-z
);
322 GET_HIGH_WORD(j
,z
, NO_COND
);
324 VECTOR_IF ((j
>>20)<=0, cond
)
325 VECTOR_COND_MOVE (z
, v64df_scalbn_aux (z
, n
, __mask
), cond
); /* subnormal output */
327 SET_HIGH_WORD(z
,j
, cond
);
329 VECTOR_RETURN (s
*z
, NO_COND
);
334 DEF_VARIANTS2 (pow
, df
, df
)