mirror of
https://github.com/ziglang/zig.git
synced 2024-12-04 10:58:58 +00:00
47cf8520ad
See #221
22 lines
568 B
Zig
22 lines
568 B
Zig
pub inline fn swapIfLe(comptime T: type, x: T) -> T {
|
|
swapIf(false, T, x)
|
|
}
|
|
|
|
pub inline fn swapIfBe(comptime T: type, x: T) -> T {
|
|
swapIf(true, T, x)
|
|
}
|
|
|
|
pub inline fn swapIf(is_be: bool, comptime T: type, x: T) -> T {
|
|
if (@compileVar("is_big_endian") == is_be) swap(T, x) else x
|
|
}
|
|
|
|
pub fn swap(comptime 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;
|
|
}
|