1// polynomial used for __v_log10(x)
2//
3// Copyright (c) 2019-2023, Arm Limited.
4// SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
5
6deg = 6; // poly degree
7a = -0x1.fc1p-9;
8b = 0x1.009p-8;
9
10// find log(1+x)/x polynomial with minimal relative error
11// (minimal relative error polynomial for log(1+x) is the same * x)
12deg = deg-1; // because of /x
13
14// f = log(1+x)/x; using taylor series
15f = 0;
16for i from 0 to 60 do { f = f + (-x)^i/(i+1); };
17
18// return p that minimizes |f(x) - poly(x) - x^d*p(x)|/|f(x)|
19approx = proc(poly,d) {
20  return remez(1 - poly(x)/f(x), deg-d, [a;b], x^d/f(x), 1e-10);
21};
22
23// first coeff is fixed, iteratively find optimal double prec coeffs
24poly = 1;
25for i from 1 to deg do {
26  p = roundcoefficients(approx(poly,i), [|D ...|]);
27  poly = poly + x^i*coeff(p,0);
28};
29
30// scale coefficients by 1/ln(10)
31ln10 = evaluate(log(10),0);
32poly = poly/ln10;
33
34display = hexadecimal;
35print("rel error:", accurateinfnorm(1-poly(x)/f(x), [a;b], 30));
36print("in [",a,b,"]");
37print("coeffs:");
38for i from 0 to deg do double(coeff(poly,i));
39