如何明确在Zig中利用SIMD?

huangapple go评论52阅读模式
英文:

How can I utilize SIMD in Zig explicitly?

问题

在阅读Zig语言参考后,我发现向量部分提到,如果可能的话,@Vector将使用SIMD指令。下面有一个示例:

const a = @Vector(4, i32){ 1, 2, 3, 4 };
const b = @Vector(4, i32){ 5, 6, 7, 8 };

// 数学操作按元素逐个进行。
const c = a + b;

但是,我想知道是否可以编写一个for循环来原地操作一个单个@Vector,并保证使用SIMD指令。例如,我可以在Julia中如下利用SIMD指令:

arr = Vector{Float64}(under, 32)
@simd for i in eachindex(arr)
    @inbounds arr[i] = 2 * i
end

感谢您的帮助!

英文:

After reading the Zig language reference, I found that the vector section states that a @Vector will use SIMD instructions if possible. There is an example following up as

const a = @Vector(4, i32){ 1, 2, 3, 4 };
const b = @Vector(4, i32){ 5, 6, 7, 8 };

// Math operations take place element-wise.
const c = a + b;

, but I am looking for whether I can write a for loop to manipulate a single @Vector in place and guarantee to use of SIMD instructions. For instance, I can utilize SIMD instructions as follows in Julia.

arr = Vector{Float64}(under, 32)
@simd for i in eachindex(arr)
    @inbounds arr[i] = 2 * i
end

Thanks for your help!

答案1

得分: 1

抱歉,Zig中没有与您提供的Julia代码等效的内容。

我认为在Zig中不需要这样的标记。相信编译器会做正确的事情是合理的:https://godbolt.org/z/KoGbqEvYf。如果编译器没有做到,您可以在zig/llvm编译器上报告错误 如何明确在Zig中利用SIMD?

与之有关的一些问题:7702 9389

英文:

There is unfortunately no Zig equivalent to the Julia code you provided.

I don't think there is need for such tags in Zig. It's reasonable to trust the compiler that it will do the right thing: https://godbolt.org/z/KoGbqEvYf. And if it doesn't, you should file a bug on the zig/llvm compiler 如何明确在Zig中利用SIMD?

Somewhat related: 7702 9389

huangapple
  • 本文由 发表于 2023年2月6日 15:54:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定