2017-02-12 14:22:35 -08:00
|
|
|
const mem = @import("mem.zig");
|
2017-05-01 10:12:38 -07:00
|
|
|
const builtin = @import("builtin");
|
2017-02-12 14:22:35 -08:00
|
|
|
|
|
|
|
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 {
|
2017-05-01 10:12:38 -07:00
|
|
|
if (builtin.is_big_endian == is_be) swap(T, x) else x
|
2016-08-17 20:11:04 -07:00
|
|
|
}
|
|
|
|
|
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;
|
2017-05-19 07:39:59 -07:00
|
|
|
mem.writeInt(buf[0..], x, false);
|
2017-02-12 14:22:35 -08:00
|
|
|
return mem.readInt(buf, T, true);
|
2016-08-17 20:11:04 -07:00
|
|
|
}
|