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