英文:
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编译器上报告错误
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论