1 // polynomial for approximating 2^x
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-exception
8 deg = 3; // poly degree
9 N = 32; // table entries
10 b = 1/(2*N); // interval
14 //deg = 5; // poly degree
15 //N = 128; // table entries
16 //b = 1/(2*N); // interval
19 // find polynomial with minimal relative error
23 // return p that minimizes |f(x) - poly(x) - x^d*p(x)|/|f(x)|
24 approx = proc(poly,d) {
25 return remez(1 - poly(x)/f(x), deg-d, [a;b], x^d/f(x), 1e-10);
27 // return p that minimizes |f(x) - poly(x) - x^d*p(x)|
28 approx_abs = proc(poly,d) {
29 return remez(f(x) - poly(x), deg-d, [a;b], x^d, 1e-10);
32 // first coeff is fixed, iteratively find optimal double prec coeffs
34 for i from 1 to deg do {
35 p = roundcoefficients(approx(poly,i), [|D ...|]);
36 // p = roundcoefficients(approx_abs(poly,i), [|D ...|]);
37 poly = poly + x^i*coeff(p,0);
40 display = hexadecimal;
41 print("rel error:", accurateinfnorm(1-poly(x)/2^x, [a;b], 30));
42 print("abs error:", accurateinfnorm(2^x-poly(x), [a;b], 30));
43 print("in [",a,b,"]");
44 // double interval error for non-nearest rounding:
45 print("rel2 error:", accurateinfnorm(1-poly(x)/2^x, [2*a;2*b], 30));
46 print("abs2 error:", accurateinfnorm(2^x-poly(x), [2*a;2*b], 30));
47 print("in [",2*a,2*b,"]");
49 for i from 0 to deg do coeff(poly,i);