1// polynomial for approximating log(1+x)
2//
3// Copyright (c) 2019, Arm Limited.
4// SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
5
6deg = 6; // poly degree
7// interval ~= 1/(2*N), where N is the table entries
8a = -0x1.fp-9;
9b =  0x1.fp-9;
10
11// find log(1+x) polynomial with minimal absolute error
12f = log(1+x);
13
14// return p that minimizes |f(x) - poly(x) - x^d*p(x)|
15approx = proc(poly,d) {
16  return remez(f(x) - poly(x), deg-d, [a;b], x^d, 1e-10);
17};
18
19// first coeff is fixed, iteratively find optimal double prec coeffs
20poly = x;
21for i from 2 to deg do {
22  p = roundcoefficients(approx(poly,i), [|D ...|]);
23  poly = poly + x^i*coeff(p,0);
24};
25
26display = hexadecimal;
27print("abs error:", accurateinfnorm(f(x)-poly(x), [a;b], 30));
28// relative error computation fails if f(0)==0
29// g = f(x)/x = log(1+x)/x; using taylor series
30g = 0;
31for i from 0 to 60 do { g = g + (-x)^i/(i+1); };
32print("rel error:", accurateinfnorm(1-poly(x)/x/g(x), [a;b], 30));
33print("in [",a,b,"]");
34print("coeffs:");
35for i from 0 to deg do coeff(poly,i);
36