|
| 1 | +# Accuracy Level |
| 2 | + |
| 3 | +For certain operations, there might be alternative versions available that provide better performance at the cost of lower accuracy. |
| 4 | +In other words, they are faster but also have a small error. |
| 5 | + |
| 6 | +## Fast Math |
| 7 | + |
| 8 | +For several operations in single precision (float32), there are "fast math" versions. These functions are faster since they are hardware accelerated, but are not IEEE compliant for all inputs. |
| 9 | + |
| 10 | +To use this functionality, use the `fast_*` functions from Kernel Float. |
| 11 | + |
| 12 | +```cpp |
| 13 | +kf::vec<float, 4> x = {1.0f, 2.0f, 3.0f, 4.0f}; |
| 14 | + |
| 15 | +// Sine |
| 16 | +kf::vec<float, 4> a = kf::fast_sin(x); |
| 17 | + |
| 18 | +// Square root |
| 19 | +kf::vec<float, 4> b = kf::fast_sqrt(x); |
| 20 | + |
| 21 | +// Reciprocal `1/x` |
| 22 | +kf::vec<float, 4> c = kf::fast_rcp(x); |
| 23 | + |
| 24 | +// Division `a/b` |
| 25 | +kf::vec<float, 4> d = kf::fast_div(a, b); |
| 26 | +``` |
| 27 | +
|
| 28 | +These functions are only functional for 32-bit and 16-bit floats. |
| 29 | +For other input types, the operation falls back to the regular version. |
| 30 | +
|
| 31 | +## Approximate Math |
| 32 | +
|
| 33 | +For 16-bit floats, several approximate functions are provided. |
| 34 | +These use approximations (typically low-degree polynomials) to calculate rough estimates of the functions. |
| 35 | +This can be very fast but also less accurate. |
| 36 | +
|
| 37 | +
|
| 38 | +To use this functionality, use the `approx_*` functions from Kernel Float. For other input types, the operation falls back to the `fast_*` variant. |
| 39 | +
|
| 40 | +```cpp |
| 41 | +kf::vec<half, 4> x = {1.0, 2.0, 3.0, 4.0}; |
| 42 | +
|
| 43 | +// Sine |
| 44 | +kf::vec<half, 4> a = kf::approx_sin(x); |
| 45 | +
|
| 46 | +// Square root |
| 47 | +kf::vec<half, 4> b = kf::approx_sqrt(x); |
| 48 | +
|
| 49 | +// Reciprocal `1/x` |
| 50 | +kf::vec<half, 4> c = kf::approx_rcp(x); |
| 51 | +
|
| 52 | +// Division `a/b` |
| 53 | +kf::vec<half, 4> d = kf::approx_div(a, b); |
| 54 | +``` |
| 55 | + |
| 56 | +You can adjust the degree of approximation by supplying an integer template parameter: |
| 57 | + |
| 58 | + |
| 59 | +```cpp |
| 60 | +// Sine approximation with polynomial of degree 1 |
| 61 | +kf::vec<half, 4> a = kf::approx_sin<1>(x); |
| 62 | + |
| 63 | +// Polynomial of degree 2 |
| 64 | +kf::vec<half, 4> a = kf::approx_sin<2>(x); |
| 65 | + |
| 66 | +// Polynomial of degree 3 |
| 67 | +kf::vec<half, 4> a = kf::approx_sin<3>(x); |
| 68 | +``` |
| 69 | + |
| 70 | +## Tuning Accuracy Level |
| 71 | + |
| 72 | +Many functions in Kernel Float accept an additional Accuracy option as a template parameter. |
| 73 | +This allows you to tune the accuracy level without changing the function name. |
| 74 | + |
| 75 | +There are four possible values for this parameter: |
| 76 | + |
| 77 | +- `kf::accurate_policy`: Use the most accurate version of the function available. |
| 78 | +- `kf::fast_policy`: Use the "fast math" version. |
| 79 | +- `kf::approx_policy<N>`: Use the approximate version with degree `N`. |
| 80 | +- `kf::default_policy`: Use a global default policy (see the next section). |
| 81 | + |
| 82 | +For example, consider this code: |
| 83 | + |
| 84 | +```cpp |
| 85 | +kf::vec<float, 2> input = {1.0f, 2.0f}; |
| 86 | + |
| 87 | +// Use the default policy |
| 88 | +kf::vec<float, 2> a = kf::cos(input); |
| 89 | + |
| 90 | +// Use the default policy |
| 91 | +kf::vec<float, 2> b = kf::cos<kf::default_policy>(input); |
| 92 | + |
| 93 | +// Use the most accurate policy |
| 94 | +kf::vec<float, 2> c = kf::cos<kf::accurate_policy>(input); |
| 95 | + |
| 96 | +// Use the fastest policy |
| 97 | +kf::vec<float, 2> d = kf::cos<kf::fast_policy>(input); |
| 98 | + |
| 99 | +// Use the approximate policy |
| 100 | +kf::vec<float, 2> e = kf::cos<kf::approx_policy<3>>(input); |
| 101 | + |
| 102 | +// You can use aliases to define your own policy |
| 103 | +using my_own_policy = kf::fast_policy; |
| 104 | +kf::vec<float, 2> f = kf::cos<my_own_policy>(input); |
| 105 | +``` |
| 106 | +
|
| 107 | +## Setting `default_policy` |
| 108 | +
|
| 109 | +By default, `kf::default_policy` is set to `kf::accurate_policy`. |
| 110 | +
|
| 111 | +Set the preprocessor option `KERNEL_FLOAT_FAST_MATH=1` to change the default policy to `kf::fast_policy`. |
| 112 | +This will use fast math for all functions and data types that support it. |
0 commit comments