summaryrefslogtreecommitdiff
path: root/src/curve.h
blob: dc003c1c455a519e8a1596dbae2f21de08f70111 (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
25
#ifndef CURVE_H
#define CURVE_H

#include <limits.h>

#define BIT_OF(X, I) (((X) >> (I)) & 1ULL)

static uintptr_t zorder(int x, int y) {
	uintptr_t z = 0;
	// interleaving bits results in a point on the z-order curve
	for (int i = 0; i < sizeof(x) * CHAR_BIT; i++)
		z |= (BIT_OF(x, i) << i*2) | (BIT_OF(y, i) << (i*2 + 1));
	return z;
}

static void unzorder(uintptr_t z, int *x, int *y) {
	*x = 0; *y = 0;
	for (int i = 0; i < sizeof(z) * CHAR_BIT / 2; i++) {
		*x |= BIT_OF(z, i*2) << i;
		*y |= BIT_OF(z, i*2 + 1) << i;
	}
}

#undef BIT_OF
#endif