@triton.jit
def _calculate_hilbert_distance(fx, fy, fz, space_size):
    x = ((fx + 1) / 2 * space_size).to(tl.int64)
    y = ((fy + 1) / 2 * space_size).to(tl.int64)
    z = ((fz + 1) / 2 * space_size).to(tl.int64)
    x = tl.minimum(tl.maximum(x, 0), space_size - 1)
    y = tl.minimum(tl.maximum(y, 0), space_size - 1)
    z = tl.minimum(tl.maximum(z, 0), space_size - 1)
    for i in tl.static_range(15, 0, -1):
        q = 1 << i
        p = q - 1
        x ^= tl.where(x & q, p, 0)
        cond = y & q
        t = (x ^ y) & p
        x ^= tl.where(cond, p, t)
        y ^= tl.where(cond, 0, t)
        cond = z & q
        t = (x ^ z) & p
        x ^= tl.where(cond, p, t)
        z ^= tl.where(cond, 0, t)
    y ^= x
    z ^= y
    t = 0
    for i in tl.static_range(15, 0, -1):
        q = 1 << i
        t ^= tl.where(z & q, q - 1, 0)
    x ^= t
    y ^= t
    z ^= t
    ret = 0
    for i in tl.static_range(0, 16):
        q = 1 << i
        ret |= (x & q) << 2 * i + 2
        ret |= (y & q) << 2 * i + 1
        ret |= (z & q) << 2 * i + 0
    return ret
