authorgravatar for 19101das@gmail.comDan Ellis Echavarria <19101das@gmail.com> 2022-09-07 07:22:30-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-09-07 15:22:30+03:00
log924679abc46deeaae9284ab6ce928aaddb0fae95
treec0f9bbc4cde23db511d95df915e6acab89e6de3b
parente02b9f458dd7e48bef5b436242ba0ab3550224da
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.simd: change T to u16

The `element_bit_size` would break if `T` was signed due to `ceilPowerOfTwo` only working on unsigned numbers.

1 files changed, 10 insertions(+), 1 deletions(-)

lib/std/simd.zig+10-1
...@@ -9,7 +9,7 @@ const builtin = @import("builtin");...@@ -9,7 +9,7 @@ const builtin = @import("builtin");
9pub fn suggestVectorSizeForCpu(comptime T: type, comptime cpu: std.Target.Cpu) ?usize {9pub fn suggestVectorSizeForCpu(comptime T: type, comptime cpu: std.Target.Cpu) ?usize {
10 // This is guesswork, if you have better suggestions can add it or edit the current here10 // This is guesswork, if you have better suggestions can add it or edit the current here
11 // This can run in comptime only, but stage 1 fails at it, stage 2 can understand it11 // This can run in comptime only, but stage 1 fails at it, stage 2 can understand it
12 const element_bit_size = @maximum(8, std.math.ceilPowerOfTwo(T, @bitSizeOf(T)) catch unreachable);12 const element_bit_size = @maximum(8, std.math.ceilPowerOfTwo(u16, @bitSizeOf(T)) catch unreachable);
13 const vector_bit_size: u16 = blk: {13 const vector_bit_size: u16 = blk: {
14 if (cpu.arch.isX86()) {14 if (cpu.arch.isX86()) {
15 if (T == bool and std.Target.x86.featureSetHas(.prefer_mask_registers)) return 64;15 if (T == bool and std.Target.x86.featureSetHas(.prefer_mask_registers)) return 64;
...@@ -57,6 +57,15 @@ pub fn suggestVectorSize(comptime T: type) ?usize {...@@ -57,6 +57,15 @@ pub fn suggestVectorSize(comptime T: type) ?usize {
57 return suggestVectorSizeForCpu(T, builtin.cpu);57 return suggestVectorSizeForCpu(T, builtin.cpu);
58}58}
5959
60test "suggestVectorSizeForCpu works with signed and unsigned values" {
61 comptime var cpu = std.Target.Cpu.baseline(std.Target.Cpu.Arch.x86_64);
62 comptime cpu.features.addFeature(@enumToInt(std.Target.x86.Feature.avx512f));
63 const signed_integer_size = suggestVectorSizeForCpu(i32, cpu).?;
64 const unsigned_integer_size = suggestVectorSizeForCpu(u32, cpu).?;
65 try std.testing.expectEqual(@as(usize, 16), unsigned_integer_size);
66 try std.testing.expectEqual(@as(usize, 16), signed_integer_size);
67}
68
60fn vectorLength(comptime VectorType: type) comptime_int {69fn vectorLength(comptime VectorType: type) comptime_int {
61 return switch (@typeInfo(VectorType)) {70 return switch (@typeInfo(VectorType)) {
62 .Vector => |info| info.len,71 .Vector => |info| info.len,