xref: /freebsd/contrib/arm-optimized-routines/pl/math/tools/v_log2f.sollya (revision 9247238cc4b8835892a47701136b0fd073f8d67c)
1// polynomial used for __v_log2f(x)
2//
3// Copyright (c) 2022-2023, Arm Limited.
4// SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
5
6deg = 9; // poly degree
7a = -1/3;
8b = 1/3;
9
10ln2 = evaluate(log(2),0);
11invln2 = single(1/ln2);
12
13// find log2(1+x)/x polynomial with minimal relative error
14// (minimal relative error polynomial for log2(1+x) is the same * x)
15deg = deg-1; // because of /x
16
17// f = log2(1+x)/x; using taylor series
18f = 0;
19for i from 0 to 60 do { f = f + (-x)^i/(i+1); };
20f = f * invln2;
21
22// return p that minimizes |f(x) - poly(x) - x^d*p(x)|/|f(x)|
23approx = proc(poly,d) {
24  return remez(1 - poly(x)/f(x), deg-d, [a;b], x^d/f(x), 1e-10);
25};
26
27// first coeff is fixed, iteratively find optimal double prec coeffs
28poly = invln2;
29for i from 1 to deg do {
30  p = roundcoefficients(approx(poly,i), [|SG ...|]);
31  poly = poly + x^i*coeff(p,0);
32};
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 coeff(poly,i);
39