brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.4 KiB · ecbc7e0 Raw
43 lines · plain
1// polynomial for approximating log2(1+x)2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6 7deg = 7; // poly degree8// interval ~= 1/(2*N), where N is the table entries9a= -0x1.f45p-8;10b=  0x1.f45p-8;11 12ln2 = evaluate(log(2),0);13invln2hi = double(1/ln2 + 0x1p21) - 0x1p21; // round away last 21 bits14invln2lo = double(1/ln2 - invln2hi);15 16// find log2(1+x) polynomial with minimal absolute error17f = log(1+x)/ln2;18 19// return p that minimizes |f(x) - poly(x) - x^d*p(x)|20approx = proc(poly,d) {21  return remez(f(x) - poly(x), deg-d, [a;b], x^d, 1e-10);22};23 24// first coeff is fixed, iteratively find optimal double prec coeffs25poly = x*(invln2lo + invln2hi);26for i from 2 to deg do {27  p = roundcoefficients(approx(poly,i), [|D ...|]);28  poly = poly + x^i*coeff(p,0);29};30 31display = hexadecimal;32print("invln2hi:", invln2hi);33print("invln2lo:", invln2lo);34print("abs error:", accurateinfnorm(f(x)-poly(x), [a;b], 30));35//// relative error computation fails if f(0)==036//// g = f(x)/x = log2(1+x)/x; using taylor series37//g = 0;38//for i from 0 to 60 do { g = g + (-x)^i/(i+1)/ln2; };39//print("rel error:", accurateinfnorm(1-(poly(x)/x)/g(x), [a;b], 30));40print("in [",a,b,"]");41print("coeffs:");42for i from 0 to deg do coeff(poly,i);43