• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// polynomial for approximating 2^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-exception
6
7// exp2f parameters
8deg = 3; // poly degree
9N = 32;  // table entries
10b = 1/(2*N); // interval
11a = -b;
12
13//// exp2 parameters
14//deg = 5; // poly degree
15//N = 128; // table entries
16//b = 1/(2*N); // interval
17//a = -b;
18
19// find polynomial with minimal relative error
20
21f = 2^x;
22
23// return p that minimizes |f(x) - poly(x) - x^d*p(x)|/|f(x)|
24approx = proc(poly,d) {
25  return remez(1 - poly(x)/f(x), deg-d, [a;b], x^d/f(x), 1e-10);
26};
27// return p that minimizes |f(x) - poly(x) - x^d*p(x)|
28approx_abs = proc(poly,d) {
29  return remez(f(x) - poly(x), deg-d, [a;b], x^d, 1e-10);
30};
31
32// first coeff is fixed, iteratively find optimal double prec coeffs
33poly = 1;
34for 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);
38};
39
40display = hexadecimal;
41print("rel error:", accurateinfnorm(1-poly(x)/2^x, [a;b], 30));
42print("abs error:", accurateinfnorm(2^x-poly(x), [a;b], 30));
43print("in [",a,b,"]");
44// double interval error for non-nearest rounding:
45print("rel2 error:", accurateinfnorm(1-poly(x)/2^x, [2*a;2*b], 30));
46print("abs2 error:", accurateinfnorm(2^x-poly(x), [2*a;2*b], 30));
47print("in [",2*a,2*b,"]");
48print("coeffs:");
49for i from 0 to deg do coeff(poly,i);
50