xref: /freebsd/contrib/arm-optimized-routines/pl/math/tools/sinpi.sollya (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1// polynomial for approximating sinpi(x)
2//
3// Copyright (c) 2023, Arm Limited.
4// SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
5
6deg = 19;  // polynomial degree
7a = -1/2; // interval
8b = 1/2;
9
10// find even polynomial with minimal abs error compared to sinpi(x)
11
12// f = sin(pi* x);
13f = pi*x;
14c = 1;
15for i from 1 to 80 do { c = 2*i*(2*i + 1)*c; f = f + (-1)^i*(pi*x)^(2*i+1)/c; };
16
17// return p that minimizes |f(x) - poly(x) - x^d*p(x)|
18approx = proc(poly,d) {
19  return remez(f(x)-poly(x), deg-d, [a;b], x^d, 1e-10);
20};
21
22// first coeff is predefine, iteratively find optimal double prec coeffs
23poly = pi*x;
24for i from 0 to (deg-1)/2 do {
25  p = roundcoefficients(approx(poly,2*i+1), [|D ...|]);
26  poly = poly + x^(2*i+1)*coeff(p,0);
27};
28
29display = hexadecimal;
30print("abs error:", accurateinfnorm(sin(pi*x)-poly(x), [a;b], 30));
31print("in [",a,b,"]");
32print("coeffs:");
33for i from 0 to deg do coeff(poly,i);
34