master
 1const std = @import("std");
 2const builtin = @import("builtin");
 3const common = @import("common.zig");
 4
 5pub const panic = common.panic;
 6
 7comptime {
 8    @export(&__bswapsi2, .{ .name = "__bswapsi2", .linkage = common.linkage, .visibility = common.visibility });
 9    @export(&__bswapdi2, .{ .name = "__bswapdi2", .linkage = common.linkage, .visibility = common.visibility });
10    @export(&__bswapti2, .{ .name = "__bswapti2", .linkage = common.linkage, .visibility = common.visibility });
11}
12
13// bswap - byteswap
14// - bswapXi2 for unoptimized big and little endian
15// ie for u32
16// DE AD BE EF   <- little|big endian
17// FE BE AD DE   <- big|little endian
18// ff 00 00 00 >> 3*8 (leftmost  byte)
19// 00 ff 00 00 >> 1*8 (2nd left  byte)
20// 00 00 ff 00 << 1*8 (2n right  byte)
21// 00 00 00 ff << 3*8 (rightmost byte)
22
23inline fn bswapXi2(comptime T: type, a: T) T {
24    switch (@bitSizeOf(T)) {
25        32 => {
26            // zig fmt: off
27            return (((a & 0xff000000) >> 24)
28                 |  ((a & 0x00ff0000) >> 8 )
29                 |  ((a & 0x0000ff00) << 8 )
30                 |  ((a & 0x000000ff) << 24));
31            // zig fmt: on
32        },
33        64 => {
34            // zig fmt: off
35            return (((a & 0xff00000000000000) >> 56)
36                 |  ((a & 0x00ff000000000000) >> 40 )
37                 |  ((a & 0x0000ff0000000000) >> 24 )
38                 |  ((a & 0x000000ff00000000) >> 8 )
39                 |  ((a & 0x00000000ff000000) << 8 )
40                 |  ((a & 0x0000000000ff0000) << 24 )
41                 |  ((a & 0x000000000000ff00) << 40 )
42                 |  ((a & 0x00000000000000ff) << 56));
43            // zig fmt: on
44        },
45        128 => {
46            // zig fmt: off
47            return (((a & 0xff000000000000000000000000000000) >> 120)
48                 |  ((a & 0x00ff0000000000000000000000000000) >> 104)
49                 |  ((a & 0x0000ff00000000000000000000000000) >> 88 )
50                 |  ((a & 0x000000ff000000000000000000000000) >> 72 )
51                 |  ((a & 0x00000000ff0000000000000000000000) >> 56 )
52                 |  ((a & 0x0000000000ff00000000000000000000) >> 40 )
53                 |  ((a & 0x000000000000ff000000000000000000) >> 24 )
54                 |  ((a & 0x00000000000000ff0000000000000000) >> 8  )
55                 |  ((a & 0x0000000000000000ff00000000000000) << 8  )
56                 |  ((a & 0x000000000000000000ff000000000000) << 24 )
57                 |  ((a & 0x00000000000000000000ff0000000000) << 40 )
58                 |  ((a & 0x0000000000000000000000ff00000000) << 56 )
59                 |  ((a & 0x000000000000000000000000ff000000) << 72 )
60                 |  ((a & 0x00000000000000000000000000ff0000) << 88 )
61                 |  ((a & 0x0000000000000000000000000000ff00) << 104)
62                 |  ((a & 0x000000000000000000000000000000ff) << 120));
63            // zig fmt: on
64        },
65        else => unreachable,
66    }
67}
68
69pub fn __bswapsi2(a: u32) callconv(.c) u32 {
70    return bswapXi2(u32, a);
71}
72
73pub fn __bswapdi2(a: u64) callconv(.c) u64 {
74    return bswapXi2(u64, a);
75}
76
77pub fn __bswapti2(a: u128) callconv(.c) u128 {
78    return bswapXi2(u128, a);
79}
80
81test {
82    _ = @import("bswapsi2_test.zig");
83    _ = @import("bswapdi2_test.zig");
84    _ = @import("bswapti2_test.zig");
85}