1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Double-precision log(x) function.
3570af302Sopenharmony_ci *
4570af302Sopenharmony_ci * Copyright (c) 2018, Arm Limited.
5570af302Sopenharmony_ci * SPDX-License-Identifier: MIT
6570af302Sopenharmony_ci */
7570af302Sopenharmony_ci
8570af302Sopenharmony_ci#include <math.h>
9570af302Sopenharmony_ci#include <stdint.h>
10570af302Sopenharmony_ci#include "libm.h"
11570af302Sopenharmony_ci#include "log_data.h"
12570af302Sopenharmony_ci
13570af302Sopenharmony_ci#define T __log_data.tab
14570af302Sopenharmony_ci#define T2 __log_data.tab2
15570af302Sopenharmony_ci#define B __log_data.poly1
16570af302Sopenharmony_ci#define A __log_data.poly
17570af302Sopenharmony_ci#define Ln2hi __log_data.ln2hi
18570af302Sopenharmony_ci#define Ln2lo __log_data.ln2lo
19570af302Sopenharmony_ci#define N (1 << LOG_TABLE_BITS)
20570af302Sopenharmony_ci#define OFF 0x3fe6000000000000
21570af302Sopenharmony_ci
22570af302Sopenharmony_ci#ifdef NEED_MATH_DIVZERO
23570af302Sopenharmony_ci/* base math internal func */
24570af302Sopenharmony_cidouble __math_divzero(uint32_t sign)
25570af302Sopenharmony_ci{
26570af302Sopenharmony_ci    return fp_barrier(sign ? -1.0 : 1.0) / 0.0;
27570af302Sopenharmony_ci}
28570af302Sopenharmony_ci#endif
29570af302Sopenharmony_ci
30570af302Sopenharmony_ci#ifdef NEED_MATH_INVALID
31570af302Sopenharmony_cidouble __math_invalid(double x)
32570af302Sopenharmony_ci{
33570af302Sopenharmony_ci	return (x - x) / (x - x);
34570af302Sopenharmony_ci}
35570af302Sopenharmony_ci#endif
36570af302Sopenharmony_ci
37570af302Sopenharmony_ci/* Top 16 bits of a double.  */
38570af302Sopenharmony_cistatic inline uint32_t top16(double x)
39570af302Sopenharmony_ci{
40570af302Sopenharmony_ci	return asuint64(x) >> 48;
41570af302Sopenharmony_ci}
42570af302Sopenharmony_ci
43570af302Sopenharmony_cidouble log(double x)
44570af302Sopenharmony_ci{
45570af302Sopenharmony_ci	double_t w, z, r, r2, r3, y, invc, logc, kd, hi, lo;
46570af302Sopenharmony_ci	uint64_t ix, iz, tmp;
47570af302Sopenharmony_ci	uint32_t top;
48570af302Sopenharmony_ci	int k, i;
49570af302Sopenharmony_ci
50570af302Sopenharmony_ci	ix = asuint64(x);
51570af302Sopenharmony_ci	top = top16(x);
52570af302Sopenharmony_ci#define LO asuint64(1.0 - 0x1p-4)
53570af302Sopenharmony_ci#define HI asuint64(1.0 + 0x1.09p-4)
54570af302Sopenharmony_ci	if (predict_false(ix - LO < HI - LO)) {
55570af302Sopenharmony_ci		/* Handle close to 1.0 inputs separately.  */
56570af302Sopenharmony_ci		/* Fix sign of zero with downward rounding when x==1.  */
57570af302Sopenharmony_ci		if (WANT_ROUNDING && predict_false(ix == asuint64(1.0)))
58570af302Sopenharmony_ci			return 0;
59570af302Sopenharmony_ci		r = x - 1.0;
60570af302Sopenharmony_ci		r2 = r * r;
61570af302Sopenharmony_ci		r3 = r * r2;
62570af302Sopenharmony_ci		y = r3 *
63570af302Sopenharmony_ci		    (B[1] + r * B[2] + r2 * B[3] +
64570af302Sopenharmony_ci		     r3 * (B[4] + r * B[5] + r2 * B[6] +
65570af302Sopenharmony_ci			   r3 * (B[7] + r * B[8] + r2 * B[9] + r3 * B[10])));
66570af302Sopenharmony_ci		/* Worst-case error is around 0.507 ULP.  */
67570af302Sopenharmony_ci		w = r * 0x1p27;
68570af302Sopenharmony_ci		double_t rhi = r + w - w;
69570af302Sopenharmony_ci		double_t rlo = r - rhi;
70570af302Sopenharmony_ci		w = rhi * rhi * B[0]; /* B[0] == -0.5.  */
71570af302Sopenharmony_ci		hi = r + w;
72570af302Sopenharmony_ci		lo = r - hi + w;
73570af302Sopenharmony_ci		lo += B[0] * rlo * (rhi + r);
74570af302Sopenharmony_ci		y += lo;
75570af302Sopenharmony_ci		y += hi;
76570af302Sopenharmony_ci		return eval_as_double(y);
77570af302Sopenharmony_ci	}
78570af302Sopenharmony_ci	if (predict_false(top - 0x0010 >= 0x7ff0 - 0x0010)) {
79570af302Sopenharmony_ci		/* x < 0x1p-1022 or inf or nan.  */
80570af302Sopenharmony_ci		if (ix * 2 == 0)
81570af302Sopenharmony_ci			return __math_divzero(1);
82570af302Sopenharmony_ci		if (ix == asuint64(INFINITY)) /* log(inf) == inf.  */
83570af302Sopenharmony_ci			return x;
84570af302Sopenharmony_ci		if ((top & 0x8000) || (top & 0x7ff0) == 0x7ff0)
85570af302Sopenharmony_ci			return __math_invalid(x);
86570af302Sopenharmony_ci		/* x is subnormal, normalize it.  */
87570af302Sopenharmony_ci		ix = asuint64(x * 0x1p52);
88570af302Sopenharmony_ci		ix -= 52ULL << 52;
89570af302Sopenharmony_ci	}
90570af302Sopenharmony_ci
91570af302Sopenharmony_ci	/* x = 2^k z; where z is in range [OFF,2*OFF) and exact.
92570af302Sopenharmony_ci	   The range is split into N subintervals.
93570af302Sopenharmony_ci	   The ith subinterval contains z and c is near its center.  */
94570af302Sopenharmony_ci	tmp = ix - OFF;
95570af302Sopenharmony_ci	i = (tmp >> (52 - LOG_TABLE_BITS)) % N;
96570af302Sopenharmony_ci	k = (int64_t)tmp >> 52; /* arithmetic shift */
97570af302Sopenharmony_ci	iz = ix - (tmp & 0xfffULL << 52);
98570af302Sopenharmony_ci	invc = T[i].invc;
99570af302Sopenharmony_ci	logc = T[i].logc;
100570af302Sopenharmony_ci	z = asdouble(iz);
101570af302Sopenharmony_ci
102570af302Sopenharmony_ci	/* log(x) = log1p(z/c-1) + log(c) + k*Ln2.  */
103570af302Sopenharmony_ci	/* r ~= z/c - 1, |r| < 1/(2*N).  */
104570af302Sopenharmony_ci#if __FP_FAST_FMA
105570af302Sopenharmony_ci	/* rounding error: 0x1p-55/N.  */
106570af302Sopenharmony_ci	r = __builtin_fma(z, invc, -1.0);
107570af302Sopenharmony_ci#else
108570af302Sopenharmony_ci	/* rounding error: 0x1p-55/N + 0x1p-66.  */
109570af302Sopenharmony_ci	r = (z - T2[i].chi - T2[i].clo) * invc;
110570af302Sopenharmony_ci#endif
111570af302Sopenharmony_ci	kd = (double_t)k;
112570af302Sopenharmony_ci
113570af302Sopenharmony_ci	/* hi + lo = r + log(c) + k*Ln2.  */
114570af302Sopenharmony_ci	w = kd * Ln2hi + logc;
115570af302Sopenharmony_ci	hi = w + r;
116570af302Sopenharmony_ci	lo = w - hi + r + kd * Ln2lo;
117570af302Sopenharmony_ci
118570af302Sopenharmony_ci	/* log(x) = lo + (log1p(r) - r) + hi.  */
119570af302Sopenharmony_ci	r2 = r * r; /* rounding error: 0x1p-54/N^2.  */
120570af302Sopenharmony_ci	/* Worst case error if |y| > 0x1p-5:
121570af302Sopenharmony_ci	   0.5 + 4.13/N + abs-poly-error*2^57 ULP (+ 0.002 ULP without fma)
122570af302Sopenharmony_ci	   Worst case error if |y| > 0x1p-4:
123570af302Sopenharmony_ci	   0.5 + 2.06/N + abs-poly-error*2^56 ULP (+ 0.001 ULP without fma).  */
124570af302Sopenharmony_ci	y = lo + r2 * A[0] +
125570af302Sopenharmony_ci	    r * r2 * (A[1] + r * A[2] + r2 * (A[3] + r * A[4])) + hi;
126570af302Sopenharmony_ci	return eval_as_double(y);
127570af302Sopenharmony_ci}
128