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

AVX2代码合并指令时崩溃:非法指令报错根因排查求助

AVX2指令嵌套调用触发非法指令崩溃的根因排查

问题背景

我需要将无符号范围[0,255]转换为有符号范围[127,-128],使用的核心AVX2指令如下:

__m256i shifted = _mm256_xor_si256(a, _mm256_set1_epi8(-128));
__m256i maskCount = _mm256_cmpgt_epi8(shifted, zerosShifted);

现象差异

  • 拆分临时变量定义时,代码可正常运行:
    __m256i temp1 = _mm256_setzero_si256();
    __m256i temp2 = _mm256_set1_epi8(-128);
    __m256i zerosShifted = _mm256_xor_si256(temp1,temp2);
    
  • 直接嵌套调用时,程序触发非法指令崩溃:
    __m256i zerosShifted = _mm256_xor_si256(_mm256_setzero_si256(),_mm256_set1_epi8(-128));
    

注:我知晓可以直接将寄存器赋值为-128,仅需排查该嵌套调用崩溃的根因。

完整AVX2代码

__m256i *pIn0, *pOut0;
__m256i a;
__m256i zerosShifted = _mm256_xor_si256(_mm256_setzero_si256(), _mm256_set1_epi8(-128));
__m256i ones = _mm256_set1_epi8(1);
__m256i zeros = _mm256_setzero_si256();
for (int x = 1; x < height - 1 ; x++)
{
    int y = 1;
    for (; y < width - 32; y+=32)
    {
        a = _MM256_LOAD_SI256((__m256i*)&boundary_image_array[x*width + y]);

        __m256i shifted = _mm256_xor_si256(a, _mm256_set1_epi8(-128));
        __m256i maskCount = _mm256_cmpgt_epi8(shifted, zerosShifted);

        maskCount = _mm256_and_si256(maskCount, ones);

        __m256i imageAgg = _mm256_blendv_epi8(a, zeros, maskCount);

        __m256i pixelCount = _mm256_sad_epu8(maskCount, zeros);
        __m256i imgAggSum = _mm256_sad_epu8(imageAgg, zeros);

        boundary_pixel_count_r += _mm256_extract_epi64(pixelCount, 0) + _mm256_extract_epi64(pixelCount, 1)
            + _mm256_extract_epi64(pixelCount, 2) + _mm256_extract_epi64(pixelCount, 3);

        boundary_grad_image_agg += _mm256_extract_epi64(imgAggSum, 0) + _mm256_extract_epi64(imgAggSum, 1)
            + _mm256_extract_epi64(imgAggSum, 2) + _mm256_extract_epi64(imgAggSum, 3);
    }
    for (; y < width - 1 ; y++)
    {
        //! Determine the boundary stats:
        if (boundary_image_array[x*width + y] > 0)
        {
            boundary_pixel_count_r++;
            boundary_grad_image_agg += boundary_image_array[x*width + y];
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 21:51:00