| 1 | const builtin = @import("builtin"); |
| 2 | const std = @import("std"); |
| 3 | const expect = std.testing.expect; |
| 4 | const expectEqual = std.testing.expectEqual; |
| 5 | |
| 6 | test "@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 | |
| 16 | test "@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 | |
| 37 | fn 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 | |
| 78 | test "@popCount vectors" { |
| 79 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 80 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO |
| 81 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO |
| 82 | if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; |
| 83 | if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; |
| 84 | |
| 85 | try comptime testPopCountVectors(); |
| 86 | try testPopCountVectors(); |
| 87 | } |
| 88 | |
| 89 | fn testPopCountVectors() !void { |
| 90 | { |
| 91 | var x: @Vector(8, u32) = @splat(0xffffffff); |
| 92 | _ = &x; |
| 93 | const expected: [8]u6 = @splat(32); |
| 94 | const result: [8]u6 = @popCount(x); |
| 95 | try expect(std.mem.eql(u6, &expected, &result)); |
| 96 | } |
| 97 | { |
| 98 | var x: @Vector(8, i16) = @splat(-1); |
| 99 | _ = &x; |
| 100 | const expected: [8]u5 = @splat(16); |
| 101 | const result: [8]u5 = @popCount(x); |
| 102 | try expect(std.mem.eql(u5, &expected, &result)); |
| 103 | } |
| 104 | } |