zig/std/endian.zig

20 lines
469 B
Zig
Raw Normal View History

const mem = @import("mem.zig");
pub fn swapIfLe(comptime T: type, x: T) -> T {
2016-08-17 20:11:04 -07:00
swapIf(false, T, x)
}
pub fn swapIfBe(comptime T: type, x: T) -> T {
2016-08-17 20:11:04 -07:00
swapIf(true, T, x)
}
pub fn swapIf(is_be: bool, comptime T: type, x: T) -> T {
2016-08-17 20:11:04 -07:00
if (@compileVar("is_big_endian") == is_be) swap(T, x) else x
}
pub fn swap(comptime T: type, x: T) -> T {
var buf: [@sizeOf(T)]u8 = undefined;
mem.writeInt(buf[0...], x, false);
return mem.readInt(buf, T, true);
2016-08-17 20:11:04 -07:00
}