1bbbf1280Sopenharmony_ci// polynomial for approximating e^x
2bbbf1280Sopenharmony_ci//
3bbbf1280Sopenharmony_ci// Copyright (c) 2019, Arm Limited.
4bbbf1280Sopenharmony_ci// SPDX-License-Identifier: MIT
5bbbf1280Sopenharmony_ci
6bbbf1280Sopenharmony_cideg = 5; // poly degree
7bbbf1280Sopenharmony_ciN = 128; // table entries
8bbbf1280Sopenharmony_cib = log(2)/(2*N);  // interval
9bbbf1280Sopenharmony_cib = b + b*0x1p-16; // increase interval for non-nearest rounding (TOINT_NARROW)
10bbbf1280Sopenharmony_cia = -b;
11bbbf1280Sopenharmony_ci
12bbbf1280Sopenharmony_ci// find polynomial with minimal abs error
13bbbf1280Sopenharmony_ci
14bbbf1280Sopenharmony_ci// return p that minimizes |exp(x) - poly(x) - x^d*p(x)|
15bbbf1280Sopenharmony_ciapprox = proc(poly,d) {
16bbbf1280Sopenharmony_ci  return remez(exp(x)-poly(x), deg-d, [a;b], x^d, 1e-10);
17bbbf1280Sopenharmony_ci};
18bbbf1280Sopenharmony_ci
19bbbf1280Sopenharmony_ci// first 2 coeffs are fixed, iteratively find optimal double prec coeffs
20bbbf1280Sopenharmony_cipoly = 1 + x;
21bbbf1280Sopenharmony_cifor i from 2 to deg do {
22bbbf1280Sopenharmony_ci  p = roundcoefficients(approx(poly,i), [|D ...|]);
23bbbf1280Sopenharmony_ci  poly = poly + x^i*coeff(p,0);
24bbbf1280Sopenharmony_ci};
25bbbf1280Sopenharmony_ci
26bbbf1280Sopenharmony_cidisplay = hexadecimal;
27bbbf1280Sopenharmony_ciprint("rel error:", accurateinfnorm(1-poly(x)/exp(x), [a;b], 30));
28bbbf1280Sopenharmony_ciprint("abs error:", accurateinfnorm(exp(x)-poly(x), [a;b], 30));
29bbbf1280Sopenharmony_ciprint("in [",a,b,"]");
30bbbf1280Sopenharmony_ci// double interval error for non-nearest rounding
31bbbf1280Sopenharmony_ciprint("rel2 error:", accurateinfnorm(1-poly(x)/exp(x), [2*a;2*b], 30));
32bbbf1280Sopenharmony_ciprint("abs2 error:", accurateinfnorm(exp(x)-poly(x), [2*a;2*b], 30));
33bbbf1280Sopenharmony_ciprint("in [",2*a,2*b,"]");
34bbbf1280Sopenharmony_ciprint("coeffs:");
35bbbf1280Sopenharmony_cifor i from 0 to deg do coeff(poly,i);
36