lntue
LLVM Libc now has all C23 basic math functions for all 5 floating point types
1. Reduce x = x mod 2*pi
2. Reduce index = floor(x / 10^-n), and i - index = 10^n * (x mod 10^-n)
With limited input range and required precision as in the tests, you can combine these 2 range reduction steps:
1. Choose the reduced range as power of 2 instead of power of 10 for cheaper modulus operation, let say 2^-N = 2^-7.
2. Avoid the division in modd(x, CONST_2PI) by multiplying by 2^N / pi.
3. Avoid the round trip double -> int -> double by using the floor function / instruction.
Here is the updated version of cos_table_*_LERP which should have higher throughput and lower latency:
double cos_table_128_LERP(double x) {
x = fabs(x);
double prod = x * TWO_TO_SEVEN_OVER_PI;
double id = floor(prod);
double x = prod - id; /* after this step, 0 <= x < 1 */
int i = ((int)id) & 0xFF; /* i = id mod 2^8 */
return lerp(x, COS_TABLE[i], COS_TABLE[i + 1]);
}
You can also optimize lerp a bit more with the formula: lerp(w, v1, v2) = (1 - w) * v1 + w * v2 = w * (v2 - v1) + v1
We do employ this range reduction strategy in a more accurate way for trig functions in LLVM libc: - with FMA instructions: https://github.com/llvm/llvm-project/blob/main/libc/src/math/generic/range_reduction_fma.h
- without FMA instructions: https://github.com/llvm/llvm-project/blob/main/libc/src/math/generic/range_reduction.h1. Reduce x = x mod 2pi
2. Reduce index = 10^n * (x / 10^-n), and i - index = 10^n * (x mod 10^-n)
With limited input range and required precision as in the tests, you can combine these 2 range reduction steps:
1. Choose the reduced range as power of 2 instead of power of 10 for cheaper modulus operation, let say `2^-N = 2^-7`.
2. Avoid the division in `modd(x, CONST_2PI)` by multiplying by `2^N / pi`.
3. Avoid the round trip `double -> int -> double` by using the `floor` function / instruction.
Here is the updated version of `cos_table__LERP` which should have higher throughput and lower latency:
``` double cos_table_128_LERP(double x) { x = fabs(x); double prod = x TWO_TO_SEVEN_OVER_PI;
}
```Maybe correct rounding (and hence consistency) will be made into the standard requirements (at least for single and double precisions) in the near future?
Other than that, some of the overview references about Remez algorithm (should be a bit closer to your taste) that I like are:
- J.M. Muller et. al., "Handbook of Floating-Point Arithmetic", section 10.3.2 (and follow references in that section).
- N. Brisebarre and S. Chevillard, "Efficient polynomial L-approximations," 18th IEEE Symposium on Computer Arithmetic (ARITH '07), which is implemented in Sollya tool: https://www.sollya.org/sollya-2.0/help.php?name=fpminimax
So using the cosine of sum formula:
* cos(x + y) = cos(x) cos(y) - sin(x) sin(y)
* ~ cos(x) - x*y.- The CORE-MATH project: https://gitlab.inria.fr/core-math/core-math/-/blob/master/sr...
- The RLIBM project: https://github.com/rutgers-apl/The-RLIBM-Project/blob/main/l...
- The LLVM libc project: https://github.com/llvm/llvm-project/blob/main/libc/src/math...