blob: 94bf32f2c449b39f36444fcccd3984c51069af45 [file] [log] [blame]
Pierre Blancharde9e23e52022-04-11 14:42:01 +01001// polynomial for approximating log10f(1+x)
2//
Joe Ramsayf0f80b82023-01-06 09:10:57 +00003// Copyright (c) 2019-2023, Arm Limited.
Pierre Blancharde9e23e52022-04-11 14:42:01 +01004// SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
5
Pierre Blanchard38fb9e72022-04-19 17:54:25 +01006// Computation of log10f(1+x) will be carried out in double precision
Pierre Blancharde9e23e52022-04-11 14:42:01 +01007
Pierre Blanchard38fb9e72022-04-19 17:54:25 +01008deg = 4; // poly degree
9// [OFF; 2*OFF] is divided in 2^4 intervals with OFF~0.7
10a = -0.04375;
11b = 0.04375;
Pierre Blancharde9e23e52022-04-11 14:42:01 +010012
Pierre Blanchard38fb9e72022-04-19 17:54:25 +010013// find log(1+x)/x polynomial with minimal relative error
14// (minimal relative error polynomial for log(1+x) is the same * x)
Pierre Blancharde9e23e52022-04-11 14:42:01 +010015deg = deg-1; // because of /x
16
17// f = log(1+x)/x; using taylor series
18f = 0;
19for i from 0 to 60 do { f = f + (-x)^i/(i+1); };
Pierre Blancharde9e23e52022-04-11 14:42:01 +010020
21// return p that minimizes |f(x) - poly(x) - x^d*p(x)|/|f(x)|
22approx = proc(poly,d) {
23 return remez(1 - poly(x)/f(x), deg-d, [a;b], x^d/f(x), 1e-10);
24};
25
26// first coeff is fixed, iteratively find optimal double prec coeffs
Pierre Blanchard38fb9e72022-04-19 17:54:25 +010027poly = 1;
Pierre Blancharde9e23e52022-04-11 14:42:01 +010028for i from 1 to deg do {
Pierre Blanchard38fb9e72022-04-19 17:54:25 +010029 p = roundcoefficients(approx(poly,i), [|D ...|]);
Pierre Blancharde9e23e52022-04-11 14:42:01 +010030 poly = poly + x^i*coeff(p,0);
31};
Pierre Blanchard38fb9e72022-04-19 17:54:25 +010032
Pierre Blancharde9e23e52022-04-11 14:42:01 +010033display = hexadecimal;
Pierre Blancharde9e23e52022-04-11 14:42:01 +010034print("rel error:", accurateinfnorm(1-poly(x)/f(x), [a;b], 30));
35print("in [",a,b,"]");
36print("coeffs:");
Pierre Blanchard38fb9e72022-04-19 17:54:25 +010037for i from 0 to deg do double(coeff(poly,i));