summaryrefslogtreecommitdiff
path: root/src/mastur2.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mastur2.c')
-rw-r--r--src/mastur2.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/mastur2.c b/src/mastur2.c
new file mode 100644
index 0000000..1be6d1d
--- /dev/null
+++ b/src/mastur2.c
@@ -0,0 +1,19 @@
+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 A[] = {0, 1, -1};
+static const int C[][3] = {{1, 1, -2}, {1, 1, -2}, {-2, 1, 1}};
+
+int mastur2(const int i, const int x, const int y)
+{
+ return i + A[x] * C[x][FASTMOD(FASTDIV(i, y), 3)] * y;
+}