You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用x86 Intrinsic快速交错两个__mm256浮点向量?

Efficient AVX2 Interleaving for __mm256 Real/Imaginary Vectors

Given your __mm256 vectors of separate real (a) and imaginary (b) components, the most efficient AVX2 implementation to produce the interleaved vectors c and d leverages 256-bit unpack instructions combined with lane extraction—this minimizes operations compared to splitting into 128-bit vectors first.

Optimal Code Implementation

#include <immintrin.h>

// Note: _mm256_set_ps takes values in reverse order (highest index first)
__mm256 a = _mm256_set_ps(7.0f, 6.0f, 5.0f, 4.0f, 3.0f, 2.0f, 1.0f, 0.0f);
__mm256 b = _mm256_set_ps(17.0f, 16.0f, 15.0f, 14.0f, 13.0f, 12.0f, 11.0f, 10.0f);

// Step 1: Unpack low and high pairs within each 128-bit lane
__mm256 temp_low = _mm256_unpacklo_ps(a, b); // Lower lane: [a0,b0,a1,b1], Upper lane: [a4,b4,a5,b5]
__mm256 temp_high = _mm256_unpackhi_ps(a, b); // Lower lane: [a2,b2,a3,b3], Upper lane: [a6,b6,a7,b7]

// Step 2: Combine lanes to form target vectors
__mm256 c = _mm256_setr_m128(_mm256_castps256_ps128(temp_low), _mm256_castps256_ps128(temp_high));
__mm256 d = _mm256_setr_m128(_mm256_extractf128_ps(temp_low, 1), _mm256_extractf128_ps(temp_high, 1));

How This Works

  1. 256-bit Unpack Instructions:

    • _mm256_unpacklo_ps(a, b) operates on each 128-bit lane independently: it interleaves the first two elements of a's lane with the first two of b's lane.
    • _mm256_unpackhi_ps(a, b) does the same for the last two elements of each lane.
  2. Lane Combination:

    • c concatenates the lower lanes of both temp vectors to get the first four interleaved real/imaginary pairs.
    • d uses the upper lanes of both temp vectors to form the last four interleaved pairs.

Efficiency Advantage

Your initial plan of splitting into 128-bit vectors first adds extra extract/cast steps. This approach uses only 6 total instructions (2 unpack, 2 extract, 2 lane concatenation), with unpack operations being single uops on modern x86 CPUs—making it the most efficient AVX2 solution for this task.

Context Note

Since FFT libraries like Intel IPP and FFTW require interleaved complex data, this method provides minimal-overhead conversion from your separate real/imaginary vectors to the required format.

内容的提问来源于stack exchange,提问作者Zvi Vered

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 02:54:55