1bbbf1280Sopenharmony_ci/*
2bbbf1280Sopenharmony_ci * Double-precision x^y function.
3bbbf1280Sopenharmony_ci *
4bbbf1280Sopenharmony_ci * Copyright (c) 2018-2020, Arm Limited.
5bbbf1280Sopenharmony_ci * SPDX-License-Identifier: MIT
6bbbf1280Sopenharmony_ci */
7bbbf1280Sopenharmony_ci
8bbbf1280Sopenharmony_ci#include <float.h>
9bbbf1280Sopenharmony_ci#include <math.h>
10bbbf1280Sopenharmony_ci#include <stdint.h>
11bbbf1280Sopenharmony_ci#include "math_config.h"
12bbbf1280Sopenharmony_ci
13bbbf1280Sopenharmony_ci/*
14bbbf1280Sopenharmony_ciWorst-case error: 0.54 ULP (~= ulperr_exp + 1024*Ln2*relerr_log*2^53)
15bbbf1280Sopenharmony_cirelerr_log: 1.3 * 2^-68 (Relative error of log, 1.5 * 2^-68 without fma)
16bbbf1280Sopenharmony_ciulperr_exp: 0.509 ULP (ULP error of exp, 0.511 ULP without fma)
17bbbf1280Sopenharmony_ci*/
18bbbf1280Sopenharmony_ci
19bbbf1280Sopenharmony_ci#define T __pow_log_data.tab
20bbbf1280Sopenharmony_ci#define A __pow_log_data.poly
21bbbf1280Sopenharmony_ci#define Ln2hi __pow_log_data.ln2hi
22bbbf1280Sopenharmony_ci#define Ln2lo __pow_log_data.ln2lo
23bbbf1280Sopenharmony_ci#define N (1 << POW_LOG_TABLE_BITS)
24bbbf1280Sopenharmony_ci#define OFF 0x3fe6955500000000
25bbbf1280Sopenharmony_ci
26bbbf1280Sopenharmony_ci/* Top 12 bits of a double (sign and exponent bits).  */
27bbbf1280Sopenharmony_cistatic inline uint32_t
28bbbf1280Sopenharmony_citop12 (double x)
29bbbf1280Sopenharmony_ci{
30bbbf1280Sopenharmony_ci  return asuint64 (x) >> 52;
31bbbf1280Sopenharmony_ci}
32bbbf1280Sopenharmony_ci
33bbbf1280Sopenharmony_ci/* Compute y+TAIL = log(x) where the rounded result is y and TAIL has about
34bbbf1280Sopenharmony_ci   additional 15 bits precision.  IX is the bit representation of x, but
35bbbf1280Sopenharmony_ci   normalized in the subnormal range using the sign bit for the exponent.  */
36bbbf1280Sopenharmony_cistatic inline double_t
37bbbf1280Sopenharmony_cilog_inline (uint64_t ix, double_t *tail)
38bbbf1280Sopenharmony_ci{
39bbbf1280Sopenharmony_ci  /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
40bbbf1280Sopenharmony_ci  double_t z, r, y, invc, logc, logctail, kd, hi, t1, t2, lo, lo1, lo2, p;
41bbbf1280Sopenharmony_ci  uint64_t iz, tmp;
42bbbf1280Sopenharmony_ci  int k, i;
43bbbf1280Sopenharmony_ci
44bbbf1280Sopenharmony_ci  /* x = 2^k z; where z is in range [OFF,2*OFF) and exact.
45bbbf1280Sopenharmony_ci     The range is split into N subintervals.
46bbbf1280Sopenharmony_ci     The ith subinterval contains z and c is near its center.  */
47bbbf1280Sopenharmony_ci  tmp = ix - OFF;
48bbbf1280Sopenharmony_ci  i = (tmp >> (52 - POW_LOG_TABLE_BITS)) % N;
49bbbf1280Sopenharmony_ci  k = (int64_t) tmp >> 52; /* arithmetic shift */
50bbbf1280Sopenharmony_ci  iz = ix - (tmp & 0xfffULL << 52);
51bbbf1280Sopenharmony_ci  z = asdouble (iz);
52bbbf1280Sopenharmony_ci  kd = (double_t) k;
53bbbf1280Sopenharmony_ci
54bbbf1280Sopenharmony_ci  /* log(x) = k*Ln2 + log(c) + log1p(z/c-1).  */
55bbbf1280Sopenharmony_ci  invc = T[i].invc;
56bbbf1280Sopenharmony_ci  logc = T[i].logc;
57bbbf1280Sopenharmony_ci  logctail = T[i].logctail;
58bbbf1280Sopenharmony_ci
59bbbf1280Sopenharmony_ci  /* Note: 1/c is j/N or j/N/2 where j is an integer in [N,2N) and
60bbbf1280Sopenharmony_ci     |z/c - 1| < 1/N, so r = z/c - 1 is exactly representible.  */
61bbbf1280Sopenharmony_ci#if HAVE_FAST_FMA
62bbbf1280Sopenharmony_ci  r = fma (z, invc, -1.0);
63bbbf1280Sopenharmony_ci#else
64bbbf1280Sopenharmony_ci  /* Split z such that rhi, rlo and rhi*rhi are exact and |rlo| <= |r|.  */
65bbbf1280Sopenharmony_ci  double_t zhi = asdouble ((iz + (1ULL << 31)) & (-1ULL << 32));
66bbbf1280Sopenharmony_ci  double_t zlo = z - zhi;
67bbbf1280Sopenharmony_ci  double_t rhi = zhi * invc - 1.0;
68bbbf1280Sopenharmony_ci  double_t rlo = zlo * invc;
69bbbf1280Sopenharmony_ci  r = rhi + rlo;
70bbbf1280Sopenharmony_ci#endif
71bbbf1280Sopenharmony_ci
72bbbf1280Sopenharmony_ci  /* k*Ln2 + log(c) + r.  */
73bbbf1280Sopenharmony_ci  t1 = kd * Ln2hi + logc;
74bbbf1280Sopenharmony_ci  t2 = t1 + r;
75bbbf1280Sopenharmony_ci  lo1 = kd * Ln2lo + logctail;
76bbbf1280Sopenharmony_ci  lo2 = t1 - t2 + r;
77bbbf1280Sopenharmony_ci
78bbbf1280Sopenharmony_ci  /* Evaluation is optimized assuming superscalar pipelined execution.  */
79bbbf1280Sopenharmony_ci  double_t ar, ar2, ar3, lo3, lo4;
80bbbf1280Sopenharmony_ci  ar = A[0] * r; /* A[0] = -0.5.  */
81bbbf1280Sopenharmony_ci  ar2 = r * ar;
82bbbf1280Sopenharmony_ci  ar3 = r * ar2;
83bbbf1280Sopenharmony_ci  /* k*Ln2 + log(c) + r + A[0]*r*r.  */
84bbbf1280Sopenharmony_ci#if HAVE_FAST_FMA
85bbbf1280Sopenharmony_ci  hi = t2 + ar2;
86bbbf1280Sopenharmony_ci  lo3 = fma (ar, r, -ar2);
87bbbf1280Sopenharmony_ci  lo4 = t2 - hi + ar2;
88bbbf1280Sopenharmony_ci#else
89bbbf1280Sopenharmony_ci  double_t arhi = A[0] * rhi;
90bbbf1280Sopenharmony_ci  double_t arhi2 = rhi * arhi;
91bbbf1280Sopenharmony_ci  hi = t2 + arhi2;
92bbbf1280Sopenharmony_ci  lo3 = rlo * (ar + arhi);
93bbbf1280Sopenharmony_ci  lo4 = t2 - hi + arhi2;
94bbbf1280Sopenharmony_ci#endif
95bbbf1280Sopenharmony_ci  /* p = log1p(r) - r - A[0]*r*r.  */
96bbbf1280Sopenharmony_ci#if POW_LOG_POLY_ORDER == 8
97bbbf1280Sopenharmony_ci  p = (ar3
98bbbf1280Sopenharmony_ci       * (A[1] + r * A[2] + ar2 * (A[3] + r * A[4] + ar2 * (A[5] + r * A[6]))));
99bbbf1280Sopenharmony_ci#endif
100bbbf1280Sopenharmony_ci  lo = lo1 + lo2 + lo3 + lo4 + p;
101bbbf1280Sopenharmony_ci  y = hi + lo;
102bbbf1280Sopenharmony_ci  *tail = hi - y + lo;
103bbbf1280Sopenharmony_ci  return y;
104bbbf1280Sopenharmony_ci}
105bbbf1280Sopenharmony_ci
106bbbf1280Sopenharmony_ci#undef N
107bbbf1280Sopenharmony_ci#undef T
108bbbf1280Sopenharmony_ci#define N (1 << EXP_TABLE_BITS)
109bbbf1280Sopenharmony_ci#define InvLn2N __exp_data.invln2N
110bbbf1280Sopenharmony_ci#define NegLn2hiN __exp_data.negln2hiN
111bbbf1280Sopenharmony_ci#define NegLn2loN __exp_data.negln2loN
112bbbf1280Sopenharmony_ci#define Shift __exp_data.shift
113bbbf1280Sopenharmony_ci#define T __exp_data.tab
114bbbf1280Sopenharmony_ci#define C2 __exp_data.poly[5 - EXP_POLY_ORDER]
115bbbf1280Sopenharmony_ci#define C3 __exp_data.poly[6 - EXP_POLY_ORDER]
116bbbf1280Sopenharmony_ci#define C4 __exp_data.poly[7 - EXP_POLY_ORDER]
117bbbf1280Sopenharmony_ci#define C5 __exp_data.poly[8 - EXP_POLY_ORDER]
118bbbf1280Sopenharmony_ci#define C6 __exp_data.poly[9 - EXP_POLY_ORDER]
119bbbf1280Sopenharmony_ci
120bbbf1280Sopenharmony_ci/* Handle cases that may overflow or underflow when computing the result that
121bbbf1280Sopenharmony_ci   is scale*(1+TMP) without intermediate rounding.  The bit representation of
122bbbf1280Sopenharmony_ci   scale is in SBITS, however it has a computed exponent that may have
123bbbf1280Sopenharmony_ci   overflown into the sign bit so that needs to be adjusted before using it as
124bbbf1280Sopenharmony_ci   a double.  (int32_t)KI is the k used in the argument reduction and exponent
125bbbf1280Sopenharmony_ci   adjustment of scale, positive k here means the result may overflow and
126bbbf1280Sopenharmony_ci   negative k means the result may underflow.  */
127bbbf1280Sopenharmony_cistatic inline double
128bbbf1280Sopenharmony_cispecialcase (double_t tmp, uint64_t sbits, uint64_t ki)
129bbbf1280Sopenharmony_ci{
130bbbf1280Sopenharmony_ci  double_t scale, y;
131bbbf1280Sopenharmony_ci
132bbbf1280Sopenharmony_ci  if ((ki & 0x80000000) == 0)
133bbbf1280Sopenharmony_ci    {
134bbbf1280Sopenharmony_ci      /* k > 0, the exponent of scale might have overflowed by <= 460.  */
135bbbf1280Sopenharmony_ci      sbits -= 1009ull << 52;
136bbbf1280Sopenharmony_ci      scale = asdouble (sbits);
137bbbf1280Sopenharmony_ci      y = 0x1p1009 * (scale + scale * tmp);
138bbbf1280Sopenharmony_ci      return check_oflow (eval_as_double (y));
139bbbf1280Sopenharmony_ci    }
140bbbf1280Sopenharmony_ci  /* k < 0, need special care in the subnormal range.  */
141bbbf1280Sopenharmony_ci  sbits += 1022ull << 52;
142bbbf1280Sopenharmony_ci  /* Note: sbits is signed scale.  */
143bbbf1280Sopenharmony_ci  scale = asdouble (sbits);
144bbbf1280Sopenharmony_ci  y = scale + scale * tmp;
145bbbf1280Sopenharmony_ci  if (fabs (y) < 1.0)
146bbbf1280Sopenharmony_ci    {
147bbbf1280Sopenharmony_ci      /* Round y to the right precision before scaling it into the subnormal
148bbbf1280Sopenharmony_ci	 range to avoid double rounding that can cause 0.5+E/2 ulp error where
149bbbf1280Sopenharmony_ci	 E is the worst-case ulp error outside the subnormal range.  So this
150bbbf1280Sopenharmony_ci	 is only useful if the goal is better than 1 ulp worst-case error.  */
151bbbf1280Sopenharmony_ci      double_t hi, lo, one = 1.0;
152bbbf1280Sopenharmony_ci      if (y < 0.0)
153bbbf1280Sopenharmony_ci	one = -1.0;
154bbbf1280Sopenharmony_ci      lo = scale - y + scale * tmp;
155bbbf1280Sopenharmony_ci      hi = one + y;
156bbbf1280Sopenharmony_ci      lo = one - hi + y + lo;
157bbbf1280Sopenharmony_ci      y = eval_as_double (hi + lo) - one;
158bbbf1280Sopenharmony_ci      /* Fix the sign of 0.  */
159bbbf1280Sopenharmony_ci      if (y == 0.0)
160bbbf1280Sopenharmony_ci	y = asdouble (sbits & 0x8000000000000000);
161bbbf1280Sopenharmony_ci      /* The underflow exception needs to be signaled explicitly.  */
162bbbf1280Sopenharmony_ci      force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022);
163bbbf1280Sopenharmony_ci    }
164bbbf1280Sopenharmony_ci  y = 0x1p-1022 * y;
165bbbf1280Sopenharmony_ci  return check_uflow (eval_as_double (y));
166bbbf1280Sopenharmony_ci}
167bbbf1280Sopenharmony_ci
168bbbf1280Sopenharmony_ci#define SIGN_BIAS (0x800 << EXP_TABLE_BITS)
169bbbf1280Sopenharmony_ci
170bbbf1280Sopenharmony_ci/* Computes sign*exp(x+xtail) where |xtail| < 2^-8/N and |xtail| <= |x|.
171bbbf1280Sopenharmony_ci   The sign_bias argument is SIGN_BIAS or 0 and sets the sign to -1 or 1.  */
172bbbf1280Sopenharmony_cistatic inline double
173bbbf1280Sopenharmony_ciexp_inline (double_t x, double_t xtail, uint32_t sign_bias)
174bbbf1280Sopenharmony_ci{
175bbbf1280Sopenharmony_ci  uint32_t abstop;
176bbbf1280Sopenharmony_ci  uint64_t ki, idx, top, sbits;
177bbbf1280Sopenharmony_ci  /* double_t for better performance on targets with FLT_EVAL_METHOD==2.  */
178bbbf1280Sopenharmony_ci  double_t kd, z, r, r2, scale, tail, tmp;
179bbbf1280Sopenharmony_ci
180bbbf1280Sopenharmony_ci  abstop = top12 (x) & 0x7ff;
181bbbf1280Sopenharmony_ci  if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54)))
182bbbf1280Sopenharmony_ci    {
183bbbf1280Sopenharmony_ci      if (abstop - top12 (0x1p-54) >= 0x80000000)
184bbbf1280Sopenharmony_ci	{
185bbbf1280Sopenharmony_ci	  /* Avoid spurious underflow for tiny x.  */
186bbbf1280Sopenharmony_ci	  /* Note: 0 is common input.  */
187bbbf1280Sopenharmony_ci	  double_t one = WANT_ROUNDING ? 1.0 + x : 1.0;
188bbbf1280Sopenharmony_ci	  return sign_bias ? -one : one;
189bbbf1280Sopenharmony_ci	}
190bbbf1280Sopenharmony_ci      if (abstop >= top12 (1024.0))
191bbbf1280Sopenharmony_ci	{
192bbbf1280Sopenharmony_ci	  /* Note: inf and nan are already handled.  */
193bbbf1280Sopenharmony_ci	  if (asuint64 (x) >> 63)
194bbbf1280Sopenharmony_ci	    return __math_uflow (sign_bias);
195bbbf1280Sopenharmony_ci	  else
196bbbf1280Sopenharmony_ci	    return __math_oflow (sign_bias);
197bbbf1280Sopenharmony_ci	}
198bbbf1280Sopenharmony_ci      /* Large x is special cased below.  */
199bbbf1280Sopenharmony_ci      abstop = 0;
200bbbf1280Sopenharmony_ci    }
201bbbf1280Sopenharmony_ci
202bbbf1280Sopenharmony_ci  /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)].  */
203bbbf1280Sopenharmony_ci  /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N].  */
204bbbf1280Sopenharmony_ci  z = InvLn2N * x;
205bbbf1280Sopenharmony_ci#if TOINT_INTRINSICS
206bbbf1280Sopenharmony_ci  kd = roundtoint (z);
207bbbf1280Sopenharmony_ci  ki = converttoint (z);
208bbbf1280Sopenharmony_ci#elif EXP_USE_TOINT_NARROW
209bbbf1280Sopenharmony_ci  /* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes.  */
210bbbf1280Sopenharmony_ci  kd = eval_as_double (z + Shift);
211bbbf1280Sopenharmony_ci  ki = asuint64 (kd) >> 16;
212bbbf1280Sopenharmony_ci  kd = (double_t) (int32_t) ki;
213bbbf1280Sopenharmony_ci#else
214bbbf1280Sopenharmony_ci  /* z - kd is in [-1, 1] in non-nearest rounding modes.  */
215bbbf1280Sopenharmony_ci  kd = eval_as_double (z + Shift);
216bbbf1280Sopenharmony_ci  ki = asuint64 (kd);
217bbbf1280Sopenharmony_ci  kd -= Shift;
218bbbf1280Sopenharmony_ci#endif
219bbbf1280Sopenharmony_ci  r = x + kd * NegLn2hiN + kd * NegLn2loN;
220bbbf1280Sopenharmony_ci  /* The code assumes 2^-200 < |xtail| < 2^-8/N.  */
221bbbf1280Sopenharmony_ci  r += xtail;
222bbbf1280Sopenharmony_ci  /* 2^(k/N) ~= scale * (1 + tail).  */
223bbbf1280Sopenharmony_ci  idx = 2 * (ki % N);
224bbbf1280Sopenharmony_ci  top = (ki + sign_bias) << (52 - EXP_TABLE_BITS);
225bbbf1280Sopenharmony_ci  tail = asdouble (T[idx]);
226bbbf1280Sopenharmony_ci  /* This is only a valid scale when -1023*N < k < 1024*N.  */
227bbbf1280Sopenharmony_ci  sbits = T[idx + 1] + top;
228bbbf1280Sopenharmony_ci  /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1).  */
229bbbf1280Sopenharmony_ci  /* Evaluation is optimized assuming superscalar pipelined execution.  */
230bbbf1280Sopenharmony_ci  r2 = r * r;
231bbbf1280Sopenharmony_ci  /* Without fma the worst case error is 0.25/N ulp larger.  */
232bbbf1280Sopenharmony_ci  /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp.  */
233bbbf1280Sopenharmony_ci#if EXP_POLY_ORDER == 4
234bbbf1280Sopenharmony_ci  tmp = tail + r + r2 * C2 + r * r2 * (C3 + r * C4);
235bbbf1280Sopenharmony_ci#elif EXP_POLY_ORDER == 5
236bbbf1280Sopenharmony_ci  tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5);
237bbbf1280Sopenharmony_ci#elif EXP_POLY_ORDER == 6
238bbbf1280Sopenharmony_ci  tmp = tail + r + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6);
239bbbf1280Sopenharmony_ci#endif
240bbbf1280Sopenharmony_ci  if (unlikely (abstop == 0))
241bbbf1280Sopenharmony_ci    return specialcase (tmp, sbits, ki);
242bbbf1280Sopenharmony_ci  scale = asdouble (sbits);
243bbbf1280Sopenharmony_ci  /* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there
244bbbf1280Sopenharmony_ci     is no spurious underflow here even without fma.  */
245bbbf1280Sopenharmony_ci  return eval_as_double (scale + scale * tmp);
246bbbf1280Sopenharmony_ci}
247bbbf1280Sopenharmony_ci
248bbbf1280Sopenharmony_ci/* Returns 0 if not int, 1 if odd int, 2 if even int.  The argument is
249bbbf1280Sopenharmony_ci   the bit representation of a non-zero finite floating-point value.  */
250bbbf1280Sopenharmony_cistatic inline int
251bbbf1280Sopenharmony_cicheckint (uint64_t iy)
252bbbf1280Sopenharmony_ci{
253bbbf1280Sopenharmony_ci  int e = iy >> 52 & 0x7ff;
254bbbf1280Sopenharmony_ci  if (e < 0x3ff)
255bbbf1280Sopenharmony_ci    return 0;
256bbbf1280Sopenharmony_ci  if (e > 0x3ff + 52)
257bbbf1280Sopenharmony_ci    return 2;
258bbbf1280Sopenharmony_ci  if (iy & ((1ULL << (0x3ff + 52 - e)) - 1))
259bbbf1280Sopenharmony_ci    return 0;
260bbbf1280Sopenharmony_ci  if (iy & (1ULL << (0x3ff + 52 - e)))
261bbbf1280Sopenharmony_ci    return 1;
262bbbf1280Sopenharmony_ci  return 2;
263bbbf1280Sopenharmony_ci}
264bbbf1280Sopenharmony_ci
265bbbf1280Sopenharmony_ci/* Returns 1 if input is the bit representation of 0, infinity or nan.  */
266bbbf1280Sopenharmony_cistatic inline int
267bbbf1280Sopenharmony_cizeroinfnan (uint64_t i)
268bbbf1280Sopenharmony_ci{
269bbbf1280Sopenharmony_ci  return 2 * i - 1 >= 2 * asuint64 (INFINITY) - 1;
270bbbf1280Sopenharmony_ci}
271bbbf1280Sopenharmony_ci
272bbbf1280Sopenharmony_cidouble
273bbbf1280Sopenharmony_cipow (double x, double y)
274bbbf1280Sopenharmony_ci{
275bbbf1280Sopenharmony_ci  uint32_t sign_bias = 0;
276bbbf1280Sopenharmony_ci  uint64_t ix, iy;
277bbbf1280Sopenharmony_ci  uint32_t topx, topy;
278bbbf1280Sopenharmony_ci
279bbbf1280Sopenharmony_ci  ix = asuint64 (x);
280bbbf1280Sopenharmony_ci  iy = asuint64 (y);
281bbbf1280Sopenharmony_ci  topx = top12 (x);
282bbbf1280Sopenharmony_ci  topy = top12 (y);
283bbbf1280Sopenharmony_ci  if (unlikely (topx - 0x001 >= 0x7ff - 0x001
284bbbf1280Sopenharmony_ci		|| (topy & 0x7ff) - 0x3be >= 0x43e - 0x3be))
285bbbf1280Sopenharmony_ci    {
286bbbf1280Sopenharmony_ci      /* Note: if |y| > 1075 * ln2 * 2^53 ~= 0x1.749p62 then pow(x,y) = inf/0
287bbbf1280Sopenharmony_ci	 and if |y| < 2^-54 / 1075 ~= 0x1.e7b6p-65 then pow(x,y) = +-1.  */
288bbbf1280Sopenharmony_ci      /* Special cases: (x < 0x1p-126 or inf or nan) or
289bbbf1280Sopenharmony_ci	 (|y| < 0x1p-65 or |y| >= 0x1p63 or nan).  */
290bbbf1280Sopenharmony_ci      if (unlikely (zeroinfnan (iy)))
291bbbf1280Sopenharmony_ci	{
292bbbf1280Sopenharmony_ci	  if (2 * iy == 0)
293bbbf1280Sopenharmony_ci	    return issignaling_inline (x) ? x + y : 1.0;
294bbbf1280Sopenharmony_ci	  if (ix == asuint64 (1.0))
295bbbf1280Sopenharmony_ci	    return issignaling_inline (y) ? x + y : 1.0;
296bbbf1280Sopenharmony_ci	  if (2 * ix > 2 * asuint64 (INFINITY)
297bbbf1280Sopenharmony_ci	      || 2 * iy > 2 * asuint64 (INFINITY))
298bbbf1280Sopenharmony_ci	    return x + y;
299bbbf1280Sopenharmony_ci	  if (2 * ix == 2 * asuint64 (1.0))
300bbbf1280Sopenharmony_ci	    return 1.0;
301bbbf1280Sopenharmony_ci	  if ((2 * ix < 2 * asuint64 (1.0)) == !(iy >> 63))
302bbbf1280Sopenharmony_ci	    return 0.0; /* |x|<1 && y==inf or |x|>1 && y==-inf.  */
303bbbf1280Sopenharmony_ci	  return y * y;
304bbbf1280Sopenharmony_ci	}
305bbbf1280Sopenharmony_ci      if (unlikely (zeroinfnan (ix)))
306bbbf1280Sopenharmony_ci	{
307bbbf1280Sopenharmony_ci	  double_t x2 = x * x;
308bbbf1280Sopenharmony_ci	  if (ix >> 63 && checkint (iy) == 1)
309bbbf1280Sopenharmony_ci	    {
310bbbf1280Sopenharmony_ci	      x2 = -x2;
311bbbf1280Sopenharmony_ci	      sign_bias = 1;
312bbbf1280Sopenharmony_ci	    }
313bbbf1280Sopenharmony_ci	  if (WANT_ERRNO && 2 * ix == 0 && iy >> 63)
314bbbf1280Sopenharmony_ci	    return __math_divzero (sign_bias);
315bbbf1280Sopenharmony_ci	  /* Without the barrier some versions of clang hoist the 1/x2 and
316bbbf1280Sopenharmony_ci	     thus division by zero exception can be signaled spuriously.  */
317bbbf1280Sopenharmony_ci	  return iy >> 63 ? opt_barrier_double (1 / x2) : x2;
318bbbf1280Sopenharmony_ci	}
319bbbf1280Sopenharmony_ci      /* Here x and y are non-zero finite.  */
320bbbf1280Sopenharmony_ci      if (ix >> 63)
321bbbf1280Sopenharmony_ci	{
322bbbf1280Sopenharmony_ci	  /* Finite x < 0.  */
323bbbf1280Sopenharmony_ci	  int yint = checkint (iy);
324bbbf1280Sopenharmony_ci	  if (yint == 0)
325bbbf1280Sopenharmony_ci	    return __math_invalid (x);
326bbbf1280Sopenharmony_ci	  if (yint == 1)
327bbbf1280Sopenharmony_ci	    sign_bias = SIGN_BIAS;
328bbbf1280Sopenharmony_ci	  ix &= 0x7fffffffffffffff;
329bbbf1280Sopenharmony_ci	  topx &= 0x7ff;
330bbbf1280Sopenharmony_ci	}
331bbbf1280Sopenharmony_ci      if ((topy & 0x7ff) - 0x3be >= 0x43e - 0x3be)
332bbbf1280Sopenharmony_ci	{
333bbbf1280Sopenharmony_ci	  /* Note: sign_bias == 0 here because y is not odd.  */
334bbbf1280Sopenharmony_ci	  if (ix == asuint64 (1.0))
335bbbf1280Sopenharmony_ci	    return 1.0;
336bbbf1280Sopenharmony_ci	  if ((topy & 0x7ff) < 0x3be)
337bbbf1280Sopenharmony_ci	    {
338bbbf1280Sopenharmony_ci	      /* |y| < 2^-65, x^y ~= 1 + y*log(x).  */
339bbbf1280Sopenharmony_ci	      if (WANT_ROUNDING)
340bbbf1280Sopenharmony_ci		return ix > asuint64 (1.0) ? 1.0 + y : 1.0 - y;
341bbbf1280Sopenharmony_ci	      else
342bbbf1280Sopenharmony_ci		return 1.0;
343bbbf1280Sopenharmony_ci	    }
344bbbf1280Sopenharmony_ci	  return (ix > asuint64 (1.0)) == (topy < 0x800) ? __math_oflow (0)
345bbbf1280Sopenharmony_ci							 : __math_uflow (0);
346bbbf1280Sopenharmony_ci	}
347bbbf1280Sopenharmony_ci      if (topx == 0)
348bbbf1280Sopenharmony_ci	{
349bbbf1280Sopenharmony_ci	  /* Normalize subnormal x so exponent becomes negative.  */
350bbbf1280Sopenharmony_ci	  /* Without the barrier some versions of clang evalutate the mul
351bbbf1280Sopenharmony_ci	     unconditionally causing spurious overflow exceptions.  */
352bbbf1280Sopenharmony_ci	  ix = asuint64 (opt_barrier_double (x) * 0x1p52);
353bbbf1280Sopenharmony_ci	  ix &= 0x7fffffffffffffff;
354bbbf1280Sopenharmony_ci	  ix -= 52ULL << 52;
355bbbf1280Sopenharmony_ci	}
356bbbf1280Sopenharmony_ci    }
357bbbf1280Sopenharmony_ci
358bbbf1280Sopenharmony_ci  double_t lo;
359bbbf1280Sopenharmony_ci  double_t hi = log_inline (ix, &lo);
360bbbf1280Sopenharmony_ci  double_t ehi, elo;
361bbbf1280Sopenharmony_ci#if HAVE_FAST_FMA
362bbbf1280Sopenharmony_ci  ehi = y * hi;
363bbbf1280Sopenharmony_ci  elo = y * lo + fma (y, hi, -ehi);
364bbbf1280Sopenharmony_ci#else
365bbbf1280Sopenharmony_ci  double_t yhi = asdouble (iy & -1ULL << 27);
366bbbf1280Sopenharmony_ci  double_t ylo = y - yhi;
367bbbf1280Sopenharmony_ci  double_t lhi = asdouble (asuint64 (hi) & -1ULL << 27);
368bbbf1280Sopenharmony_ci  double_t llo = hi - lhi + lo;
369bbbf1280Sopenharmony_ci  ehi = yhi * lhi;
370bbbf1280Sopenharmony_ci  elo = ylo * lhi + y * llo; /* |elo| < |ehi| * 2^-25.  */
371bbbf1280Sopenharmony_ci#endif
372bbbf1280Sopenharmony_ci  return exp_inline (ehi, elo, sign_bias);
373bbbf1280Sopenharmony_ci}
374bbbf1280Sopenharmony_ci#if USE_GLIBC_ABI
375bbbf1280Sopenharmony_cistrong_alias (pow, __pow_finite)
376bbbf1280Sopenharmony_cihidden_alias (pow, __ieee754_pow)
377bbbf1280Sopenharmony_ci# if LDBL_MANT_DIG == 53
378bbbf1280Sopenharmony_cilong double powl (long double x, long double y) { return pow (x, y); }
379bbbf1280Sopenharmony_ci# endif
380bbbf1280Sopenharmony_ci#endif
381