srctree

SuperAuguste parent cbeab678 8e7d9afd
Add 64bit byteswap case, use fewer locals

inlinesplit
src/arch/wasm/CodeGen.zig added: 67, removed: 17, total 50
@@ -6641,21 +6641,42 @@ fn airByteSwap(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
break :result try (try func.binOp(tmp, lsb, ty, .@"or")).toLocal(func, ty);
},
32 => {
const shl_tmp = try func.binOp(operand, .{ .imm32 = 8 }, ty, .shl);
var lhs = try (try func.binOp(shl_tmp, .{ .imm32 = 0xFF00FF00 }, ty, .@"and")).toLocal(func, ty);
defer lhs.free(func);
const shr_tmp = try func.binOp(operand, .{ .imm32 = 8 }, ty, .shr);
var rhs = try (try func.binOp(shr_tmp, .{ .imm32 = 0xFF00FF }, ty, .@"and")).toLocal(func, ty);
defer rhs.free(func);
var tmp_or = try (try func.binOp(lhs, rhs, ty, .@"or")).toLocal(func, ty);
defer tmp_or.free(func);
const shl_tmp = try func.binOp(operand, .{ .imm32 = 8 }, Type.u32, .shl);
const lhs = try func.binOp(shl_tmp, .{ .imm32 = 0xFF00FF00 }, Type.u32, .@"and");
const shr_tmp = try func.binOp(operand, .{ .imm32 = 8 }, Type.u32, .shr);
const rhs = try func.binOp(shr_tmp, .{ .imm32 = 0x00FF00FF }, Type.u32, .@"and");
var tmp_or = try (try func.binOp(lhs, rhs, Type.u32, .@"or")).toLocal(func, Type.u32);
 
const shl = try func.binOp(tmp_or, .{ .imm32 = 16 }, ty, .shl);
const shr = try func.binOp(tmp_or, .{ .imm32 = 16 }, ty, .shr);
const res = if (int_info.signedness == .signed) blk: {
break :blk try func.wrapOperand(shr, Type.u16);
} else shr;
break :result try (try func.binOp(shl, res, ty, .@"or")).toLocal(func, ty);
const shl = try func.binOp(tmp_or, .{ .imm32 = 16 }, Type.u32, .shl);
const shr = try func.binOp(tmp_or, .{ .imm32 = 16 }, Type.u32, .shr);
 
tmp_or.free(func);
 
break :result try (try func.binOp(shl, shr, Type.u32, .@"or")).toLocal(func, Type.u32);
},
64 => {
const shl_tmp_1 = try func.binOp(operand, .{ .imm64 = 8 }, Type.u64, .shl);
const lhs_1 = try func.binOp(shl_tmp_1, .{ .imm64 = 0xFF00FF00FF00FF00 }, Type.u64, .@"and");
 
const shr_tmp_1 = try func.binOp(operand, .{ .imm64 = 8 }, Type.u64, .shr);
const rhs_1 = try func.binOp(shr_tmp_1, .{ .imm64 = 0x00FF00FF00FF00FF }, Type.u64, .@"and");
 
var tmp_or_1 = try (try func.binOp(lhs_1, rhs_1, Type.u64, .@"or")).toLocal(func, Type.u64);
 
const shl_tmp_2 = try func.binOp(tmp_or_1, .{ .imm64 = 16 }, Type.u64, .shl);
const lhs_2 = try func.binOp(shl_tmp_2, .{ .imm64 = 0xFFFF0000FFFF0000 }, Type.u64, .@"and");
 
const shr_tmp_2 = try func.binOp(tmp_or_1, .{ .imm64 = 16 }, Type.u64, .shr);
tmp_or_1.free(func);
const rhs_2 = try func.binOp(shr_tmp_2, .{ .imm64 = 0x0000FFFF0000FFFF }, Type.u64, .@"and");
 
var tmp_or_2 = try (try func.binOp(lhs_2, rhs_2, Type.u64, .@"or")).toLocal(func, Type.u64);
 
const shl = try func.binOp(tmp_or_2, .{ .imm64 = 32 }, Type.u64, .shl);
const shr = try func.binOp(tmp_or_2, .{ .imm64 = 32 }, Type.u64, .shr);
tmp_or_2.free(func);
 
break :result try (try func.binOp(shl, shr, Type.u64, .@"or")).toLocal(func, Type.u64);
},
else => return func.fail("TODO: @byteSwap for integers with bitsize {d}", .{int_info.bits}),
}
 
test/behavior/byteswap.zig added: 67, removed: 17, total 50
@@ -3,7 +3,36 @@ const builtin = @import("builtin");
const expect = std.testing.expect;
 
test "@byteSwap integers" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_wasm) {
// TODO: Remove when self-hosted wasm supports more types for byteswap
const ByteSwapIntTest = struct {
fn run() !void {
try t(u8, 0x12, 0x12);
try t(u16, 0x1234, 0x3412);
try t(u24, 0x123456, 0x563412);
try t(i24, @as(i24, @bitCast(@as(u24, 0xf23456))), 0x5634f2);
try t(i24, 0x1234f6, @as(i24, @bitCast(@as(u24, 0xf63412))));
try t(u32, 0x12345678, 0x78563412);
try t(i32, @as(i32, @bitCast(@as(u32, 0xf2345678))), 0x785634f2);
try t(i32, 0x123456f8, @as(i32, @bitCast(@as(u32, 0xf8563412))));
try t(u64, 0x123456789abcdef1, 0xf1debc9a78563412);
 
try t(u0, @as(u0, 0), 0);
try t(i8, @as(i8, -50), -50);
try t(i16, @as(i16, @bitCast(@as(u16, 0x1234))), @as(i16, @bitCast(@as(u16, 0x3412))));
try t(i24, @as(i24, @bitCast(@as(u24, 0x123456))), @as(i24, @bitCast(@as(u24, 0x563412))));
try t(i32, @as(i32, @bitCast(@as(u32, 0x12345678))), @as(i32, @bitCast(@as(u32, 0x78563412))));
try t(i64, @as(i64, @bitCast(@as(u64, 0x123456789abcdef1))), @as(i64, @bitCast(@as(u64, 0xf1debc9a78563412))));
}
fn t(comptime I: type, input: I, expected_output: I) !void {
try std.testing.expect(expected_output == @byteSwap(input));
}
};
try comptime ByteSwapIntTest.run();
try ByteSwapIntTest.run();
return;
}
 
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;