xref: /freebsd/contrib/arm-optimized-routines/math/tools/log.sollya (revision 072a4ba82a01476eaee33781ccd241033eefcf0b)
131914882SAlex Richardson// polynomial for approximating log(1+x)
231914882SAlex Richardson//
331914882SAlex Richardson// Copyright (c) 2019, Arm Limited.
4*072a4ba8SAndrew Turner// SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
531914882SAlex Richardson
631914882SAlex Richardsondeg = 12; // poly degree
731914882SAlex Richardson// |log(1+x)| > 0x1p-4 outside the interval
831914882SAlex Richardsona = -0x1p-4;
931914882SAlex Richardsonb =  0x1.09p-4;
1031914882SAlex Richardson
1131914882SAlex Richardson// find log(1+x)/x polynomial with minimal relative error
1231914882SAlex Richardson// (minimal relative error polynomial for log(1+x) is the same * x)
1331914882SAlex Richardsondeg = deg-1; // because of /x
1431914882SAlex Richardson
1531914882SAlex Richardson// f = log(1+x)/x; using taylor series
1631914882SAlex Richardsonf = 0;
1731914882SAlex Richardsonfor i from 0 to 60 do { f = f + (-x)^i/(i+1); };
1831914882SAlex Richardson
1931914882SAlex Richardson// return p that minimizes |f(x) - poly(x) - x^d*p(x)|/|f(x)|
2031914882SAlex Richardsonapprox = proc(poly,d) {
2131914882SAlex Richardson  return remez(1 - poly(x)/f(x), deg-d, [a;b], x^d/f(x), 1e-10);
2231914882SAlex Richardson};
2331914882SAlex Richardson
2431914882SAlex Richardson// first coeff is fixed, iteratively find optimal double prec coeffs
2531914882SAlex Richardsonpoly = 1;
2631914882SAlex Richardsonfor i from 1 to deg do {
2731914882SAlex Richardson  p = roundcoefficients(approx(poly,i), [|D ...|]);
2831914882SAlex Richardson  poly = poly + x^i*coeff(p,0);
2931914882SAlex Richardson};
3031914882SAlex Richardson
3131914882SAlex Richardsondisplay = hexadecimal;
3231914882SAlex Richardsonprint("rel error:", accurateinfnorm(1-poly(x)/f(x), [a;b], 30));
3331914882SAlex Richardsonprint("in [",a,b,"]");
3431914882SAlex Richardsonprint("coeffs:");
3531914882SAlex Richardsonfor i from 0 to deg do coeff(poly,i);
36