master
  1const builtin = @import("builtin");
  2const std = @import("std");
  3const expect = std.testing.expect;
  4const expectEqual = std.testing.expectEqual;
  5
  6test "@popCount integers" {
  7    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
  8    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
  9    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
 10    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; // TODO
 11
 12    try comptime testPopCountIntegers();
 13    try testPopCountIntegers();
 14}
 15
 16test "@popCount 128bit integer" {
 17    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
 18    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
 19    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 20    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
 21    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
 22
 23    comptime {
 24        try expect(@popCount(@as(u128, 0b11111111000110001100010000100001000011000011100101010001)) == 24);
 25        try expect(@popCount(@as(i128, 0b11111111000110001100010000100001000011000011100101010001)) == 24);
 26    }
 27
 28    {
 29        var x: u128 = 0b11111111000110001100010000100001000011000011100101010001;
 30        _ = &x;
 31        try expect(@popCount(x) == 24);
 32    }
 33
 34    try expect(@popCount(@as(i128, 0b11111111000110001100010000100001000011000011100101010001)) == 24);
 35}
 36
 37fn testPopCountIntegers() !void {
 38    {
 39        var x: u32 = 0xffffffff;
 40        _ = &x;
 41        try expect(@popCount(x) == 32);
 42    }
 43    {
 44        var x: u5 = 0x1f;
 45        _ = &x;
 46        try expect(@popCount(x) == 5);
 47    }
 48    {
 49        var x: u32 = 0xaa;
 50        _ = &x;
 51        try expect(@popCount(x) == 4);
 52    }
 53    {
 54        var x: u32 = 0xaaaaaaaa;
 55        _ = &x;
 56        try expect(@popCount(x) == 16);
 57    }
 58    {
 59        var x: u32 = 0xaaaaaaaa;
 60        _ = &x;
 61        try expect(@popCount(x) == 16);
 62    }
 63    {
 64        var x: i16 = -1;
 65        _ = &x;
 66        try expect(@popCount(x) == 16);
 67    }
 68    {
 69        var x: i8 = -120;
 70        _ = &x;
 71        try expect(@popCount(x) == 2);
 72    }
 73    comptime {
 74        try expect(@popCount(@as(u8, @bitCast(@as(i8, -120)))) == 2);
 75    }
 76}
 77
 78test "@popCount vectors" {
 79    if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
 80    if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
 81    if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
 82    if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
 83    if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
 84    if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
 85
 86    try comptime testPopCountVectors();
 87    try testPopCountVectors();
 88}
 89
 90fn testPopCountVectors() !void {
 91    {
 92        var x: @Vector(8, u32) = [1]u32{0xffffffff} ** 8;
 93        _ = &x;
 94        const expected = [1]u6{32} ** 8;
 95        const result: [8]u6 = @popCount(x);
 96        try expect(std.mem.eql(u6, &expected, &result));
 97    }
 98    {
 99        var x: @Vector(8, i16) = [1]i16{-1} ** 8;
100        _ = &x;
101        const expected = [1]u5{16} ** 8;
102        const result: [8]u5 = @popCount(x);
103        try expect(std.mem.eql(u5, &expected, &result));
104    }
105}