summaryrefslogtreecommitdiff
path: root/src/mastur2.c
blob: 554fdc085f54a3b9107f62c966448d6a9b92e68e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
static const unsigned int divtab[244] = {
	[1] = 0xffff + 1l,
	[3] = 0x5555 + 1l,
	[9] = 0x1c71 + 1l,
	[27] = 0x097b + 1l,
	[81] = 0x0329 + 1l,
	[243] = 0x010d + 1l
};

#define FASTDIV(n, d) (((n) * divtab[d]) >> 16)
#define FASTMOD(n, d) ((n) - (d) * FASTDIV((n), (d)))

static const int C[][3] = {{0, 0, 0}, {1, 1, -2}, {2, -1, -1}};

int mastur2(const int i, const int x, const int y)
{
	return i + C[x][FASTMOD(FASTDIV(i, y), 3)] * y;
}