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