summaryrefslogtreecommitdiff
path: root/src/new.c
blob: 0b223dc00adaa93bff229001ad3d0b600fccdd48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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, d) (((n) * divtab[d]) >> 32)
#define FASTMOD(n, d) ((n) - (d) * FASTDIV((n), (d)))

int new(int i, int x, int y)
{
	uint64_t A, B, C;

	A = FASTDIV(i, y);
	B = FASTMOD(A, 3);
	C = FASTMOD(A + (uint64_t)x, 3);

	return i + (C - B) * y;
}