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