From 924679abc46deeaae9284ab6ce928aaddb0fae95 Mon Sep 17 00:00:00 2001 From: Dan Ellis Echavarria <19101das@gmail.com> Date: Wed, 7 Sep 2022 07:22:30 -0500 Subject: [PATCH] std.simd: change T to u16 The `element_bit_size` would break if `T` was signed due to `ceilPowerOfTwo` only working on unsigned numbers. --- lib/std/simd.zig | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/std/simd.zig b/lib/std/simd.zig index b2655758c0..972bf136e9 100644 --- a/lib/std/simd.zig +++ b/lib/std/simd.zig @@ -9,7 +9,7 @@ const builtin = @import("builtin"); pub fn suggestVectorSizeForCpu(comptime T: type, comptime cpu: std.Target.Cpu) ?usize { // This is guesswork, if you have better suggestions can add it or edit the current here // This can run in comptime only, but stage 1 fails at it, stage 2 can understand it - const element_bit_size = @maximum(8, std.math.ceilPowerOfTwo(T, @bitSizeOf(T)) catch unreachable); + const element_bit_size = @maximum(8, std.math.ceilPowerOfTwo(u16, @bitSizeOf(T)) catch unreachable); const vector_bit_size: u16 = blk: { if (cpu.arch.isX86()) { if (T == bool and std.Target.x86.featureSetHas(.prefer_mask_registers)) return 64; @@ -57,6 +57,15 @@ pub fn suggestVectorSize(comptime T: type) ?usize { return suggestVectorSizeForCpu(T, builtin.cpu); } +test "suggestVectorSizeForCpu works with signed and unsigned values" { + comptime var cpu = std.Target.Cpu.baseline(std.Target.Cpu.Arch.x86_64); + comptime cpu.features.addFeature(@enumToInt(std.Target.x86.Feature.avx512f)); + const signed_integer_size = suggestVectorSizeForCpu(i32, cpu).?; + const unsigned_integer_size = suggestVectorSizeForCpu(u32, cpu).?; + try std.testing.expectEqual(@as(usize, 16), unsigned_integer_size); + try std.testing.expectEqual(@as(usize, 16), signed_integer_size); +} + fn vectorLength(comptime VectorType: type) comptime_int { return switch (@typeInfo(VectorType)) { .Vector => |info| info.len,