Skip to content

Commit d487894

Browse files
committed
x86_64: use math.zig to check isPowerOfTwo and calc log2_int
1 parent 098bee0 commit d487894

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

src/arch/x86_64/CodeGen.zig

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,8 +1085,7 @@ fn airTrunc(self: *Self, inst: Air.Inst.Index) !void {
10851085
// when truncating a `u16` to `u5`, for example, those top 3 bits in the result
10861086
// have to be removed. this only happens if the dst if not a power-of-two size.
10871087
const dst_bit_size = dst_ty.bitSize(self.target.*);
1088-
const is_power_of_two = (dst_bit_size & (dst_bit_size - 1)) == 0;
1089-
if (!is_power_of_two or dst_bit_size < 8) {
1088+
if (!math.isPowerOfTwo(dst_bit_size) or dst_bit_size < 8) {
10901089
const max_reg_bit_width = Register.rax.size();
10911090
const shift = @intCast(u6, max_reg_bit_width - dst_ty.bitSize(self.target.*));
10921091
const mask = (~@as(u64, 0)) >> shift;
@@ -5125,8 +5124,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: i32, mcv: MCValue, opts: Inl
51255124
}
51265125

51275126
const base_reg = opts.dest_stack_base orelse .rbp;
5128-
const is_power_of_two = (abi_size % 2) == 0;
5129-
if (!is_power_of_two) {
5127+
if (!math.isPowerOfTwo(abi_size)) {
51305128
self.register_manager.freezeRegs(&.{reg});
51315129
defer self.register_manager.unfreezeRegs(&.{reg});
51325130

@@ -5135,31 +5133,31 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: i32, mcv: MCValue, opts: Inl
51355133
var next_offset = stack_offset;
51365134
var remainder = abi_size;
51375135
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

51405138
_ = try self.addInst(.{
51415139
.tag = .mov,
51425140
.ops = (Mir.Ops{
51435141
.reg1 = base_reg,
5144-
.reg2 = registerAlias(tmp_reg, closest_power_of_two),
5142+
.reg2 = registerAlias(tmp_reg, nearest_power_of_two),
51455143
.flags = 0b10,
51465144
}).encode(),
51475145
.data = .{ .imm = @bitCast(u32, -next_offset) },
51485146
});
51495147

5150-
if (closest_power_of_two > 1) {
5148+
if (nearest_power_of_two > 1) {
51515149
_ = try self.addInst(.{
51525150
.tag = .shr,
51535151
.ops = (Mir.Ops{
51545152
.reg1 = tmp_reg,
51555153
.flags = 0b10,
51565154
}).encode(),
5157-
.data = .{ .imm = closest_power_of_two * 8 },
5155+
.data = .{ .imm = nearest_power_of_two * 8 },
51585156
});
51595157
}
51605158

5161-
remainder -= closest_power_of_two;
5162-
next_offset -= closest_power_of_two;
5159+
remainder -= nearest_power_of_two;
5160+
next_offset -= nearest_power_of_two;
51635161
}
51645162
} else {
51655163
_ = try self.addInst(.{

0 commit comments

Comments
 (0)