首页 > 解决方案 > 您如何将此“无符号整数”标量代码移植到“有符号整数”向量?

问题描述

我需要将Xorshift算法从标量移植到矢量代码(SSE/SIMD使用构建的版本-march=nocona)。我正在使用算法的 uint32_t 版本(直接取自 wiki):

#include <stdint.h>

struct xorshift32_state {
  uint32_t a;
};

/* The state word must be initialized to non-zero */
uint32_t xorshift32(struct xorshift32_state *state)
{
    /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
    uint32_t x = state->a;
    x ^= x << 13;
    x ^= x >> 17;
    x ^= x << 5;
    return state->a = x;
}

主要问题是:

  1. 它使用 uint32,因此(按照标准)它会自动环绕
  2. 由于 SSE3“限制”,我会继续使用 m128i(我相信它已签名,并给我我需要的所有操作)
  3. 有符号溢出是 C++ 标准中未定义的行为

您将如何使用 SIMD 管理此移植?处理 epu32 并减去 uint32 最大值的一半(然后添加)?

标签: c++undefined-behaviorsseintrinsicsunsigned-integer

解决方案


推荐阅读