| 1 | |
| 2 | |
| 3 | |
| 4 | // Based on: |
| 5 | // fast_log |
| 6 | // https://github.com/nadavrot/fast_log/tree/main |
| 7 | // MIT License |
| 8 | // Copyright (c) 2022 Nadav Rotem |
| 9 | // |
| 10 | // This version is modified/simplified to better suite openMSX: |
| 11 | // * double -> float |
| 12 | // * simplified fast_frexpf() (no longer works for negative or zero values) |
| 13 | // * calculate log base-2 instead of natural-log. |
| 14 | |
| 15 | // Main advantage over std::log() or std::log2(): |
| 16 | // * A lot faster, and can be (auto-)vectorized (verified gcc-11 and above) |
| 17 | // Main disadvantage: |
| 18 | // * Not as accurate, max error: ~0.25%. std::log() is accurate to 1 ULP. |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | // Similar to std::frexp(): |
| 25 | // decompose 'x' into 'm' and 'e' so that |
| 26 | // x == m * pow(2, e) |
| 27 | // with: 0.5 <= m < 1.0 |
| 28 | // This function has some limitations |
| 29 | // * It doesn't work at all for negative values. |
| 30 | // * It gives an inaccurate result for x=0 (it returns m=0.5, e=-126, that's |
| 31 | // very close but not exact). |
| 32 | // These limitations allow this version to be much faster: it can easily be |
| 33 | // inlined and it's suitable for (auto-)vectorization. |
| 34 | inline std::pair<float, int> |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | // Compute a fast _approximation_ for the base-2 logarithm of 'x'. |
| 46 | inline float |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |