xref: /freebsd/contrib/arm-optimized-routines/math/tools/exp2.sollya (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1// polynomial for approximating 2^x
2//
3// Copyright (c) 2019, Arm Limited.
4// SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
5
6// exp2f parameters
7deg = 3; // poly degree
8N = 32;  // table entries
9b = 1/(2*N); // interval
10a = -b;
11
12//// exp2 parameters
13//deg = 5; // poly degree
14//N = 128; // table entries
15//b = 1/(2*N); // interval
16//a = -b;
17
18// find polynomial with minimal relative error
19
20f = 2^x;
21
22// return p that minimizes |f(x) - poly(x) - x^d*p(x)|/|f(x)|
23approx = proc(poly,d) {
24  return remez(1 - poly(x)/f(x), deg-d, [a;b], x^d/f(x), 1e-10);
25};
26// return p that minimizes |f(x) - poly(x) - x^d*p(x)|
27approx_abs = proc(poly,d) {
28  return remez(f(x) - poly(x), deg-d, [a;b], x^d, 1e-10);
29};
30
31// first coeff is fixed, iteratively find optimal double prec coeffs
32poly = 1;
33for i from 1 to deg do {
34  p = roundcoefficients(approx(poly,i), [|D ...|]);
35//  p = roundcoefficients(approx_abs(poly,i), [|D ...|]);
36  poly = poly + x^i*coeff(p,0);
37};
38
39display = hexadecimal;
40print("rel error:", accurateinfnorm(1-poly(x)/2^x, [a;b], 30));
41print("abs error:", accurateinfnorm(2^x-poly(x), [a;b], 30));
42print("in [",a,b,"]");
43// double interval error for non-nearest rounding:
44print("rel2 error:", accurateinfnorm(1-poly(x)/2^x, [2*a;2*b], 30));
45print("abs2 error:", accurateinfnorm(2^x-poly(x), [2*a;2*b], 30));
46print("in [",2*a,2*b,"]");
47print("coeffs:");
48for i from 0 to deg do coeff(poly,i);
49