Skip to content

Support @ctz and @popCount with comptime_ints #23634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 46 additions & 8 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23492,15 +23492,25 @@ fn zirBitCount(
const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0);
const operand = try sema.resolveInst(inst_data.operand);
const operand_ty = sema.typeOf(operand);
_ = try sema.checkIntOrVector(block, operand, operand_src);
const bits = operand_ty.intInfo(zcu).bits;

if (try sema.typeHasOnePossibleValue(operand_ty)) |val| {
return Air.internedToRef(val.toIntern());
const operand_ty_tag = operand_ty.zigTypeTag(zcu);
if (air_tag == .clz) {
_ = try sema.checkIntOrVector(block, operand, operand_src);
} else {
_ = try sema.checkIntOrVectorAllowComptime(block, operand_ty, operand_src);
}

const result_scalar_ty = try pt.smallestUnsignedInt(bits);
switch (operand_ty.zigTypeTag(zcu)) {
const result_scalar_ty = if (operand_ty_tag == .comptime_int)
operand_ty
else blk: {
const bits = operand_ty.intInfo(zcu).bits;

if (try sema.typeHasOnePossibleValue(operand_ty)) |val| {
return Air.internedToRef(val.toIntern());
}

break :blk try pt.smallestUnsignedInt(bits);
};
switch (operand_ty_tag) {
.vector => {
const vec_len = operand_ty.vectorLen(zcu);
const result_ty = try pt.vectorType(.{
Expand All @@ -23526,11 +23536,39 @@ fn zirBitCount(
return block.addTyOp(air_tag, result_ty, operand);
}
},
.int => {
.int, .comptime_int => {
if (try sema.resolveValueResolveLazy(operand)) |val| {
if (val.isUndef(zcu)) return pt.undefRef(result_scalar_ty);
if (operand_ty_tag == .comptime_int) {
var space: InternPool.Key.Int.Storage.BigIntSpace = undefined;
const bigint = val.toBigInt(&space, zcu);
switch (air_tag) {
.ctz => {
if (bigint.eqlZero()) {
return sema.fail(
block,
operand_src,
"cannot count number of least-significant zeroes in integer '0' of type 'comptime_int'",
.{},
);
}
},
.popcount => {
if (!bigint.positive) {
return sema.fail(
block,
operand_src,
"cannot count number of bits set in negative integer '{}' of type 'comptime_int'",
.{val.fmtValueSema(pt, sema)},
);
}
},
else => unreachable,
}
}
return pt.intRef(result_scalar_ty, comptimeOp(val, operand_ty, zcu));
} else {
std.debug.assert(operand_ty_tag != .comptime_int);
try sema.requireRuntimeBlock(block, src, operand_src);
return block.addTyOp(air_tag, result_scalar_ty, operand);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -924,13 +924,13 @@ pub fn clz(val: Value, ty: Type, zcu: *Zcu) u64 {
pub fn ctz(val: Value, ty: Type, zcu: *Zcu) u64 {
var bigint_buf: BigIntSpace = undefined;
const bigint = val.toBigInt(&bigint_buf, zcu);
return bigint.ctz(ty.intInfo(zcu).bits);
return bigint.ctz(if (ty.toIntern() == .comptime_int_type) std.math.maxInt(std.math.big.Limb) else ty.intInfo(zcu).bits);
}

pub fn popCount(val: Value, ty: Type, zcu: *Zcu) u64 {
var bigint_buf: BigIntSpace = undefined;
const bigint = val.toBigInt(&bigint_buf, zcu);
return @intCast(bigint.popCount(ty.intInfo(zcu).bits));
return @intCast(bigint.popCount(if (ty.toIntern() == .comptime_int_type) undefined else ty.intInfo(zcu).bits));
}

pub fn bitReverse(val: Value, ty: Type, pt: Zcu.PerThread, arena: Allocator) !Value {
Expand Down
7 changes: 7 additions & 0 deletions test/behavior/math.zig
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ fn testCtz() !void {
try expect(testOneCtz(u16, 0b00000000) == 16);
}

test "@ctz comptime_int" {
try expectEqual(5, @ctz(0b10100000));
try expectEqual(1, @ctz(0b10001010));
try expectEqual(0, @ctz(-1));
try expectEqual(1, @ctz(-2));
}

fn testOneCtz(comptime T: type, x: T) u32 {
return @ctz(x);
}
Expand Down
9 changes: 9 additions & 0 deletions test/behavior/popcount.zig
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ fn testPopCountIntegers() !void {
}
}

test "@popCount comptime_int" {
try expectEqual(0, @popCount(0));
try expectEqual(32, @popCount(0xffffffff));
try expectEqual(5, @popCount(0x1f));
try expectEqual(4, @popCount(0xaa));
try expectEqual(16, @popCount(0xaaaaaaaa));
try expectEqual(24, @popCount(0b11111111000110001100010000100001000011000011100101010001));
}

test "@popCount vectors" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
Expand Down
9 changes: 9 additions & 0 deletions test/cases/compile_errors/ctz-comptime_int-zero.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export fn entry() void {
_ = @ctz(0);
}

// error
// backend=stage2
// target=native
//
// :2:14: error: cannot count number of least-significant zeroes in integer '0' of type 'comptime_int'
9 changes: 9 additions & 0 deletions test/cases/compile_errors/popCount-negative-comptime_int.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export fn entry() void {
_ = @popCount(-1);
}

// error
// backend=stage2
// target=native
//
// :2:19: error: cannot count number of bits set in negative integer '-1' of type 'comptime_int'
Loading