zig/test/behavior/popcount.zig
Andrew Kelley 93b854eb74 stage2: implement @ctz and @clz including SIMD
AIR:
 * `array_elem_val` is now allowed to be used with a vector as the array
   type.
 * New instructions: splat, vector_init

AstGen:
 * The splat ZIR instruction uses coerced_ty for the ResultLoc, avoiding
   an unnecessary `as` instruction, since the coercion will be performed
   in Sema.
 * Builtins that accept vectors now ignore the type parameter. Comment
   from this commit reproduced here:

   The accepted proposal #6835 tells us to remove the type parameter from
   these builtins. To stay source-compatible with stage1, we still observe
   the parameter here, but we do not encode it into the ZIR. To implement
   this proposal in stage2, only AstGen code will need to be changed.

Sema:
 * `clz` and `ctz` ZIR instructions are now handled by the same function
   which accept AIR tag and comptime eval function pointer to
   differentiate.
 * `@typeInfo` for vectors is implemented.
 * `@splat` is implemented. It takes advantage of `Value.Tag.repeated` 😎
 * `elemValue` is implemented for vectors, when the index is a scalar.
   Handling a vector index is still TODO.
 * Element-wise coercion is implemented for vectors. It could probably
   be optimized a bit, but it is at least complete & correct.
 * `Type.intInfo` supports vectors, returning int info for the element.
 * `Value.ctz` initial implementation. Needs work.
 * `Value.eql` is implemented for arrays and vectors.

LLVM backend:
 * Implement vector support when lowering `array_elem_val`.
 * Implement vector support when lowering `ctz` and `clz`.
 * Implement `splat` and `vector_init`.
2022-01-12 23:53:26 -07:00

47 lines
1.1 KiB
Zig

const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const Vector = std.meta.Vector;
test "@popCount integers" {
comptime try testPopCountIntegers();
try testPopCountIntegers();
}
fn testPopCountIntegers() !void {
{
var x: u32 = 0xffffffff;
try expect(@popCount(u32, x) == 32);
}
{
var x: u5 = 0x1f;
try expect(@popCount(u5, x) == 5);
}
{
var x: u32 = 0xaa;
try expect(@popCount(u32, x) == 4);
}
{
var x: u32 = 0xaaaaaaaa;
try expect(@popCount(u32, x) == 16);
}
{
var x: u32 = 0xaaaaaaaa;
try expect(@popCount(u32, x) == 16);
}
{
var x: i16 = -1;
try expect(@popCount(i16, x) == 16);
}
{
var x: i8 = -120;
try expect(@popCount(i8, x) == 2);
}
comptime {
try expect(@popCount(u8, @bitCast(u8, @as(i8, -120))) == 2);
}
comptime {
try expect(@popCount(i128, @as(i128, 0b11111111000110001100010000100001000011000011100101010001)) == 24);
}
}