aboutsummaryrefslogtreecommitdiff
path: root/std/endian.zig
blob: dbf518d801c49e7c868c051b2c6c1d3c3f750918 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pub inline fn swapIfLe(inline T: type, x: T) -> T {
    swapIf(false, T, x)
}

pub inline fn swapIfBe(inline T: type, x: T) -> T {
    swapIf(true, T, x)
}

pub inline fn swapIf(is_be: bool, inline T: type, x: T) -> T {
    if (@compileVar("is_big_endian") == is_be) swap(T, x) else x
}

pub fn swap(inline T: type, x: T) -> T {
    const x_slice = ([]u8)((&const x)[0...1]);
    var result: T = undefined;
    const result_slice = ([]u8)((&result)[0...1]);
    for (result_slice) |*b, i| {
        *b = x_slice[@sizeOf(T) - i - 1];
    }
    return result;
}