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