2017-02-13 06:22:35 +08:00
|
|
|
const mem = @import("mem.zig");
|
2017-05-02 01:12:38 +08:00
|
|
|
const builtin = @import("builtin");
|
2017-02-13 06:22:35 +08:00
|
|
|
|
|
|
|
pub fn swapIfLe(comptime T: type, x: T) -> T {
|
2016-08-18 11:11:04 +08:00
|
|
|
swapIf(false, T, x)
|
|
|
|
}
|
|
|
|
|
2017-02-13 06:22:35 +08:00
|
|
|
pub fn swapIfBe(comptime T: type, x: T) -> T {
|
2016-08-18 11:11:04 +08:00
|
|
|
swapIf(true, T, x)
|
|
|
|
}
|
|
|
|
|
2017-02-13 06:22:35 +08:00
|
|
|
pub fn swapIf(is_be: bool, comptime T: type, x: T) -> T {
|
2017-05-02 01:12:38 +08:00
|
|
|
if (builtin.is_big_endian == is_be) swap(T, x) else x
|
2016-08-18 11:11:04 +08:00
|
|
|
}
|
|
|
|
|
2017-01-23 08:51:37 +08:00
|
|
|
pub fn swap(comptime T: type, x: T) -> T {
|
2017-02-13 06:22:35 +08:00
|
|
|
var buf: [@sizeOf(T)]u8 = undefined;
|
2017-05-19 22:39:59 +08:00
|
|
|
mem.writeInt(buf[0..], x, false);
|
2017-02-13 06:22:35 +08:00
|
|
|
return mem.readInt(buf, T, true);
|
2016-08-18 11:11:04 +08:00
|
|
|
}
|