rev: 21c5e6d2f665d0e8b4ff917a51b664c895dab2ed tukan/tukan/packing.sc -rw-r--r-- 2.9 KiB
21c5e6d2f665 — Leonard Ritter * renamed project from Liminal to Tukan 2 years ago
`                                                                                `
```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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80```
```using import glm

# source:
# https://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/

# "Insert" a 0 bit after each of the 16 low bits of x
fn part1by1 (x)
"u32 <- u32"
let x = (x & 0x0000ffff)              # x = ---- ---- ---- ---- fedc ba98 7654 3210
let x = ((x ^ (x << 8)) & 0x00ff00ff) # x = ---- ---- fedc ba98 ---- ---- 7654 3210
let x = ((x ^ (x << 4)) & 0x0f0f0f0f) # x = ---- fedc ---- ba98 ---- 7654 ---- 3210
let x = ((x ^ (x << 2)) & 0x33333333) # x = --fe --dc --ba --98 --76 --54 --32 --10
let x = ((x ^ (x << 1)) & 0x55555555) # x = -f-e -d-c -b-a -9-8 -7-6 -5-4 -3-2 -1-0
x

# Inverse of Part1By1 - "delete" all odd-indexed bits
fn compact1by1 (x)
"u32 <- u32"
let x = (x & 0x55555555)              # x = -f-e -d-c -b-a -9-8 -7-6 -5-4 -3-2 -1-0
let x = ((x ^ (x >> 1)) & 0x33333333) # x = --fe --dc --ba --98 --76 --54 --32 --10
let x = ((x ^ (x >> 2)) & 0x0f0f0f0f) # x = ---- fedc ---- ba98 ---- 7654 ---- 3210
let x = ((x ^ (x >> 4)) & 0x00ff00ff) # x = ---- ---- fedc ba98 ---- ---- 7654 3210
let x = ((x ^ (x >> 8)) & 0x0000ffff) # x = ---- ---- ---- ---- fedc ba98 7654 3210
x

# "Insert" two 0 bits after each of the 10 low bits of x
fn part1by2 (x)
"u32 <- u32"
let x = (x & 0x000003ff)               # x = ---- ---- ---- ---- ---- --98 7654 3210
let x = ((x ^ (x << 16)) & 0xff0000ff) # x = ---- --98 ---- ---- ---- ---- 7654 3210
let x = ((x ^ (x <<  8)) & 0x0300f00f) # x = ---- --98 ---- ---- 7654 ---- ---- 3210
let x = ((x ^ (x <<  4)) & 0x030c30c3) # x = ---- --98 ---- 76-- --54 ---- 32-- --10
let x = ((x ^ (x <<  2)) & 0x09249249) # x = ---- 9--8 --7- -6-- 5--4 --3- -2-- 1--0
x

# Inverse of Part1By2 - "delete" all bits not at positions divisible by 3
fn compact1by2 (x)
"u32 <- u32"
let x = (x & 0x09249249)               # x = ---- 9--8 --7- -6-- 5--4 --3- -2-- 1--0
let x = ((x ^ (x >>  2)) & 0x030c30c3) # x = ---- --98 ---- 76-- --54 ---- 32-- --10
let x = ((x ^ (x >>  4)) & 0x0300f00f) # x = ---- --98 ---- ---- 7654 ---- ---- 3210
let x = ((x ^ (x >>  8)) & 0xff0000ff) # x = ---- --98 ---- ---- ---- ---- 7654 3210
let x = ((x ^ (x >> 16)) & 0x000003ff) # x = ---- ---- ---- ---- ---- --98 7654 3210
x

do
fn pack-morton2x16 (v)
"u32 <- uvec2"
|
part1by1 v.x
(part1by1 v.y) << 1

fn unpack-morton2x16 (p)
"uvec2 <- u32"
uvec2
compact1by1 p
compact1by1 (p >> 1)

fn pack-morton3x10 (v)
"u32 <- (uvec3)"
|
part1by2 v.x
(part1by2 v.y) << 1
(part1by2 v.z) << 2

fn unpack-morton3x10 (p)
"uvec3 <- (u32)"
uvec3
compact1by2 p
compact1by2 (p >> 1)
compact1by2 (p >> 2)

if main-module?
print
unpack-morton2x16
pack-morton2x16 (uvec2 12345 54321)

locals;
```