Last active
March 25, 2025 03:14
-
-
Save rota1001/6a13112a8ab8d85a1f95070077de8e38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const uint64_t a1 = 1431655765, a2 = 858993459, a3 = 613566760, a4 = 477218078, | |
a5 = 390489239, a6 = 328862160, a7 = 317788895; | |
// 2 + a1*x^2 + a2*x^4 + a3*x^6 + a4*x^8 + a5*x^10 + ... | |
const uint64_t ln2 = 1488522236; | |
int log_test(int x) | |
{ | |
if (x <= 0) | |
return 0; | |
uint64_t y = x, f, s, hfsq; | |
int shift = __builtin_clzll(y) - 32; | |
y <<= shift; | |
f = y - 0x80000000u; | |
s = (f << 31) / ((2ull << 31) + f); | |
hfsq = (f * f) >> 32; // (f * f / 2) >> 31 | |
uint64_t s2 = (s * s) >> 31, s4 = (s2 * s2) >> 31; | |
y = (s4 * (a2 + ((s4 * (a4 + ((s4 * a6) >> 31))) >> 31))) >> 31; | |
y += (s2 * (a1 + ((s4 * (a3 + ((s4 * (a5 + ((s4 * a7) >> 31))) >> 31))) >> 31))) >> 31; | |
y = (y * s) >> 31; | |
y = f - (hfsq - ((s * (hfsq + y)) >> 31)); | |
return ((23 - shift) * ln2 + y) >> 23; // (31 - shift - 8) >> (31 - 8) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment