std::experimental::simd<T,Abi>::copy_from

< cpp‎ | experimental‎ | simd‎ | simd
template< class U, class Flags >
void copy_from( const U* mem, Flags flags );
(并行 TS v2)

加载函数替换 simd 的所有元素,使得对于范围 [0size()) 中的所有 i,第 i 元素以 static_cast<T>(mem[i]) 赋值。

参数

mem - 指向数组的指针,其中 [memmem + size()) 是有效范围
flags - 若为类型 vector_aligned_tag,则加载构造函数可能假设 mem 指向按 memory_alignment_v<simd, U> 对齐的存储
类型要求
-
U 必须为可向量化类型。
-
is_simd_flag_type_v<Flags> 必须为 true

示例

#include <cstddef>
#include <experimental/simd>
#include <iostream>
#include <numeric>
namespace stdx = std::experimental;
 
void print(auto const& a)
{
    for (std::size_t i{}; i != std::size(a); ++i)
        std::cout << a[i] << ' ';
    std::cout << '\n';
}
 
int main()
{
    alignas(stdx::memory_alignment_v<stdx::native_simd<int>>)
        std::array<int, stdx::native_simd<int>::size() * 2> mem = {};
    std::iota(mem.begin(), mem.end(), 0);
    print(mem);
 
    stdx::native_simd<int> a; // 未初始化
 
    a.copy_from(&mem[0], stdx::vector_aligned);
    print(a);
 
    a.copy_from(&mem[1], stdx::element_aligned); // vector_aligned 很可能崩溃
    print(a);
}

可能的输出:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1 2 3 4 5 6 7
1 2 3 4 5 6 7 8

参阅

(并行 TS v2)
获得 vector_aligned 的适当对齐
(类模板)
(并行 TS v2)
向连续内存存储 simd 的元素
(公开成员函数)
(并行 TS v2)
构造 simd 对象
(公开成员函数)