diff options
Diffstat (limited to 'src/new.c')
-rw-r--r-- | src/new.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/new.c b/src/new.c new file mode 100644 index 0000000..3bdd6d0 --- /dev/null +++ b/src/new.c @@ -0,0 +1,24 @@ +#include <stdint.h> + +static const uint64_t divtab[244] = { + [1] = 0xffffffff + 1l, + [3] = 0x55555555 + 1l, + [9] = 0x1c71c71c + 1l, + [27] = 0x097b425e + 1l, + [81] = 0x0329161f + 1l, + [243] = 0x010db20a + 1l +}; + +#define FASTDIV(n, C) (((n) * (C)) >> 32) +#define FASTMOD(n, d, C) ((n) - (d) * FASTDIV((n), (C))) + +int new(int i, int x, int y) +{ + uint64_t A, B, C; + + A = FASTDIV(i, divtab[y]); + B = FASTMOD(A, 3, divtab[3]); + C = FASTMOD(A + (uint64_t)x, 3, divtab[3]); + + return i + (C - B) * y; +} |