authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-05-03 11:48:30+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-05-03 16:39:21+02:00
logd48789467d95ddf0582c855d01f32bef48726e29
tree6eb72560902e0b44b189e35311b437fc3507daa5
parent098bee0e5657bb6dcd92b2b2fa8056ffce893ffc

x86_64: use math.zig to check isPowerOfTwo and calc log2_int


1 files changed, 8 insertions(+), 10 deletions(-)

src/arch/x86_64/CodeGen.zig+8-10
...@@ -1085,8 +1085,7 @@ fn airTrunc(self: *Self, inst: Air.Inst.Index) !void {...@@ -1085,8 +1085,7 @@ fn airTrunc(self: *Self, inst: Air.Inst.Index) !void {
1085 // when truncating a `u16` to `u5`, for example, those top 3 bits in the result1085 // when truncating a `u16` to `u5`, for example, those top 3 bits in the result
1086 // have to be removed. this only happens if the dst if not a power-of-two size.1086 // have to be removed. this only happens if the dst if not a power-of-two size.
1087 const dst_bit_size = dst_ty.bitSize(self.target.*);1087 const dst_bit_size = dst_ty.bitSize(self.target.*);
1088 const is_power_of_two = (dst_bit_size & (dst_bit_size - 1)) == 0;1088 if (!math.isPowerOfTwo(dst_bit_size) or dst_bit_size < 8) {
1089 if (!is_power_of_two or dst_bit_size < 8) {
1090 const max_reg_bit_width = Register.rax.size();1089 const max_reg_bit_width = Register.rax.size();
1091 const shift = @intCast(u6, max_reg_bit_width - dst_ty.bitSize(self.target.*));1090 const shift = @intCast(u6, max_reg_bit_width - dst_ty.bitSize(self.target.*));
1092 const mask = (~@as(u64, 0)) >> shift;1091 const mask = (~@as(u64, 0)) >> shift;
...@@ -5125,8 +5124,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: i32, mcv: MCValue, opts: Inl...@@ -5125,8 +5124,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: i32, mcv: MCValue, opts: Inl
5125 }5124 }
51265125
5127 const base_reg = opts.dest_stack_base orelse .rbp;5126 const base_reg = opts.dest_stack_base orelse .rbp;
5128 const is_power_of_two = (abi_size % 2) == 0;5127 if (!math.isPowerOfTwo(abi_size)) {
5129 if (!is_power_of_two) {
5130 self.register_manager.freezeRegs(&.{reg});5128 self.register_manager.freezeRegs(&.{reg});
5131 defer self.register_manager.unfreezeRegs(&.{reg});5129 defer self.register_manager.unfreezeRegs(&.{reg});
51325130
...@@ -5135,31 +5133,31 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: i32, mcv: MCValue, opts: Inl...@@ -5135,31 +5133,31 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: i32, mcv: MCValue, opts: Inl
5135 var next_offset = stack_offset;5133 var next_offset = stack_offset;
5136 var remainder = abi_size;5134 var remainder = abi_size;
5137 while (remainder > 0) {5135 while (remainder > 0) {
5138 const closest_power_of_two = @as(u6, 1) << @intCast(u3, math.log2(remainder));5136 const nearest_power_of_two = @as(u6, 1) << math.log2_int(u3, @intCast(u3, remainder));
51395137
5140 _ = try self.addInst(.{5138 _ = try self.addInst(.{
5141 .tag = .mov,5139 .tag = .mov,
5142 .ops = (Mir.Ops{5140 .ops = (Mir.Ops{
5143 .reg1 = base_reg,5141 .reg1 = base_reg,
5144 .reg2 = registerAlias(tmp_reg, closest_power_of_two),5142 .reg2 = registerAlias(tmp_reg, nearest_power_of_two),
5145 .flags = 0b10,5143 .flags = 0b10,
5146 }).encode(),5144 }).encode(),
5147 .data = .{ .imm = @bitCast(u32, -next_offset) },5145 .data = .{ .imm = @bitCast(u32, -next_offset) },
5148 });5146 });
51495147
5150 if (closest_power_of_two > 1) {5148 if (nearest_power_of_two > 1) {
5151 _ = try self.addInst(.{5149 _ = try self.addInst(.{
5152 .tag = .shr,5150 .tag = .shr,
5153 .ops = (Mir.Ops{5151 .ops = (Mir.Ops{
5154 .reg1 = tmp_reg,5152 .reg1 = tmp_reg,
5155 .flags = 0b10,5153 .flags = 0b10,
5156 }).encode(),5154 }).encode(),
5157 .data = .{ .imm = closest_power_of_two * 8 },5155 .data = .{ .imm = nearest_power_of_two * 8 },
5158 });5156 });
5159 }5157 }
51605158
5161 remainder -= closest_power_of_two;5159 remainder -= nearest_power_of_two;
5162 next_offset -= closest_power_of_two;5160 next_offset -= nearest_power_of_two;
5163 }5161 }
5164 } else {5162 } else {
5165 _ = try self.addInst(.{5163 _ = try self.addInst(.{