/* MIT License Copyright (c) 2023 Vladimir Pleskonjic Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include #include #include "fastapprox/fastlog.h" // LOG FUNCTIONS // Log functions below approximate log2f(x), // where x is positive and not NaN, infinity, nor a subnormal. // Each assumes IEEE 754 representation of float on the system. // Remez polynomials have been generated // using https://github.com/samhocevar/lolremez. float remezLogDeg1(float x) { // IEEE 754 representation constants. const int32_t mantissaLen = 23; const int32_t mantissaMask = (1 << mantissaLen) - 1; const int32_t baseExponent = -127; // Reinterpret x as int in a standard compliant way. int32_t xi; memcpy(&xi, &x, sizeof(xi)); // Calculate exponent of x. float e = (float)((xi >> mantissaLen) + baseExponent); // Calculate mantissa of x. It will be in range [1, 2). float m; int32_t mxi = (xi & mantissaMask) | ((-baseExponent) << mantissaLen); memcpy(&m, &mxi, sizeof(m)); // Use Remez algorithm-generated approximation polynomial // for log2(a) where a is in range [1, 2]. float l = 1.0f; l = l * m + -0.95696431f; // Add exponent to the calculation. // Final log is log2(m*2^e)=log2(m)+e. l += e; return l; } float remezLogDeg2(float x) { // IEEE 754 representation constants. const int32_t mantissaLen = 23; const int32_t mantissaMask = (1 << mantissaLen) - 1; const int32_t baseExponent = -127; // Reinterpret x as int in a standard compliant way. int32_t xi; memcpy(&xi, &x, sizeof(xi)); // Calculate exponent of x. float e = (float)((xi >> mantissaLen) + baseExponent); // Calculate mantissa of x. It will be in range [1, 2). float m; int32_t mxi = (xi & mantissaMask) | ((-baseExponent) << mantissaLen); memcpy(&m, &mxi, sizeof(m)); // Use Remez algorithm-generated approximation polynomial // for log2(a) where a is in range [1, 2]. float l = -0.34484842f; l = l * m + 2.0246658f; l = l * m + -1.6748776f; // Add exponent to the calculation. // Final log is log2(m*2^e)=log2(m)+e. l += e; return l; } float remezLogDeg3(float x) { // IEEE 754 representation constants. const int32_t mantissaLen = 23; const int32_t mantissaMask = (1 << mantissaLen) - 1; const int32_t baseExponent = -127; // Reinterpret x as int in a standard compliant way. int32_t xi; memcpy(&xi, &x, sizeof(xi)); // Calculate exponent of x. float e = (float)((xi >> mantissaLen) + baseExponent); // Calculate mantissa of x. It will be in range [1, 2). float m; int32_t mxi = (xi & mantissaMask) | ((-baseExponent) << mantissaLen); memcpy(&m, &mxi, sizeof(m)); // Use Remez algorithm-generated approximation polynomial // for log2(a) where a is in range [1, 2]. float l = 0.15824871f; l = l * m + -1.051875f; l = l * m + 3.0478842f; l = l * m + -2.1536207f; // Add exponent to the calculation. // Final log is log2(m*2^e)=log2(m)+e. l += e; return l; } float remezLogDeg4(float x) { // IEEE 754 representation constants. const int32_t mantissaLen = 23; const int32_t mantissaMask = (1 << mantissaLen) - 1; const int32_t baseExponent = -127; // Reinterpret x as int in a standard compliant way. int32_t xi; memcpy(&xi, &x, sizeof(xi)); // Calculate exponent of x. float e = (float)((xi >> mantissaLen) + baseExponent); // Calculate mantissa of x. It will be in range [1, 2). float m; int32_t mxi = (xi & mantissaMask) | ((-baseExponent) << mantissaLen); memcpy(&m, &mxi, sizeof(m)); // Use Remez algorithm-generated approximation polynomial // for log2(a) where a is in range [1, 2]. float l = -0.081615806f; l = l * m + 0.64514238f; l = l * m + -2.1206751f; l = l * m + 4.0700908f; l = l * m + -2.5128546f; // Add exponent to the calculation. // Final log is log2(m*2^e)=log2(m)+e. l += e; return l; } float remezLogDeg5(float x) { // IEEE 754 representation constants. const int32_t mantissaLen = 23; const int32_t mantissaMask = (1 << mantissaLen) - 1; const int32_t baseExponent = -127; // Reinterpret x as int in a standard compliant way. int32_t xi; memcpy(&xi, &x, sizeof(xi)); // Calculate exponent of x. float e = (float)((xi >> mantissaLen) + baseExponent); // Calculate mantissa of x. It will be in range [1, 2). float m; int32_t mxi = (xi & mantissaMask) | ((-baseExponent) << mantissaLen); memcpy(&m, &mxi, sizeof(m)); // Use Remez algorithm-generated approximation polynomial // for log2(a) where a is in range [1, 2]. float l = 0.04487361f; l = l * m + -0.41656369f; l = l * m + 1.6311488f; l = l * m + -3.5507929f; l = l * m + 5.091711f; l = l * m + -2.800364f; // Add exponent to the calculation. // Final log is log2(m*2^e)=log2(m)+e. l += e; return l; } // BENCHMARK CODE double benchmark(float (*func)(float), int n, float *res) { clock_t t0 = clock(); for (int i = 0; i < n; ++i) { res[i] = func(((float)i + 1) * 0.01f); } clock_t t1 = clock(); return (double)(t1 - t0) / (double)CLOCKS_PER_SEC; } double benchmark01(float (*func)(float), int n, float *res) { clock_t t0 = clock(); for (int i = 0; i < n; ++i) { res[i] = func(((float)i + 1) * (1 / (float)n)); } clock_t t1 = clock(); return (double)(t1 - t0) / (double)CLOCKS_PER_SEC; } void test( const char *name, float (*func)(float), bool call01, int n, const float *log, float *res) { const int repeats = 5; double t = 0; for (int i = 0; i < repeats; ++i) { if (call01) t += benchmark01(func, n, res); else t += benchmark(func, n, res); } t /= repeats; double err = 0; for (int i = 0; i < n; ++i) { double currErr = fabs(res[i] - log[i]); if (currErr > err) err = currErr; } printf("%s:\n", name); printf("\ttime=%f; err=%f\n", t, err); } int main(void) { const int n = 1000000000; float *log = malloc(n * sizeof(*log)); float *res = malloc(n * sizeof(*res)); for (int i = 0; i < n; ++i) { log[i] = log2f(((float)i + 1) * 0.01f); } test("baseline", log2f, false, n, log, res); test("fastapprox_fasterlog2", fasterlog2, false, n, log, res); test("fastapprox_fastlog2", fastlog2, false, n, log, res); test("remezLogDeg1", remezLogDeg1, false, n, log, res); test("remezLogDeg2", remezLogDeg2, false, n, log, res); test("remezLogDeg3", remezLogDeg3, false, n, log, res); test("remezLogDeg4", remezLogDeg4, false, n, log, res); test("remezLogDeg5", remezLogDeg5, false, n, log, res); printf("=====\n"); for (int i = 0; i < n; ++i) { log[i] = log2f(((float)i + 1) * (1 / (float)n)); } test("baseline", log2f, true, n, log, res); test("fastapprox_fasterlog2", fasterlog2, true, n, log, res); test("fastapprox_fastlog2", fastlog2, true, n, log, res); test("remezLogDeg1", remezLogDeg1, true, n, log, res); test("remezLogDeg2", remezLogDeg2, true, n, log, res); test("remezLogDeg3", remezLogDeg3, true, n, log, res); test("remezLogDeg4", remezLogDeg4, true, n, log, res); test("remezLogDeg5", remezLogDeg5, true, n, log, res); free(res); free(log); return 0; }