2017-02-12 14:22:35 -08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-02-12 14:22:35 -08:00
|
|
|
pub fn swapIfBe(comptime T: type, x: T) -> T {
|
2016-08-17 20:11:04 -07:00
|
|
|
swapIf(true, T, x)
|
|
|
|
}
|
|
|
|
|
2017-02-12 14:22:35 -08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:51:37 -08:00
|
|
|
pub fn swap(comptime T: type, x: T) -> T {
|
2017-02-12 14:22:35 -08:00
|
|
|
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
|
|
|
}
|