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