1// polynomial used for __v_log(x)
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
7deg = 6; // poly degree
8a = -0x1.fc1p-9;
9b = 0x1.009p-8;
10
11// find log(1+x)/x polynomial with minimal relative error
12// (minimal relative error polynomial for log(1+x) is the same * x)
13deg = deg-1; // because of /x
14
15// f = log(1+x)/x; using taylor series
16f = 0;
17for i from 0 to 60 do { f = f + (-x)^i/(i+1); };
18
19// return p that minimizes |f(x) - poly(x) - x^d*p(x)|/|f(x)|
20approx = proc(poly,d) {
21  return remez(1 - poly(x)/f(x), deg-d, [a;b], x^d/f(x), 1e-10);
22};
23
24// first coeff is fixed, iteratively find optimal double prec coeffs
25poly = 1;
26for i from 1 to deg do {
27  p = roundcoefficients(approx(poly,i), [|D ...|]);
28  poly = poly + x^i*coeff(p,0);
29};
30
31display = hexadecimal;
32print("rel error:", accurateinfnorm(1-poly(x)/f(x), [a;b], 30));
33print("in [",a,b,"]");
34print("coeffs:");
35for i from 0 to deg do coeff(poly,i);
36