assembly - 添加具有饱和度的 32 位字

标签 assembly x86 sse mmx saturation-arithmetic

您知道使用 MMX/SSE 汇编程序指令添加饱和 32 位有符号字的方法吗?我可以找到 8/16 位版本,但找不到 32 位版本。

最佳答案

您可以通过执行以下步骤来模拟饱和签名添加:

int saturated_add(int a, int b)
{
    int sum = a + (unsigned)b;                // avoid signed-overflow UB
    if (a >= 0 && b >= 0)
        return sum > 0 ? sum : INT32_MAX;     // catch positive wraparound
    else if (a < 0 && b < 0)
        return sum > 0 ? INT32_MIN : sum;     // catch negative wraparound
    else
        return sum;                           // sum of pos + neg always fits
}

Unsigned,更简单,见this stackoverflow posting

在 SSE2 中,以上内容映射到一系列并行比较和 AND/ANDN 操作。不幸的是,硬件中没有可用的单一操作。

关于assembly - 添加具有饱和度的 32 位字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7133149/

相关文章:

assembly - 非常大的地址复制为负值

c - SSE 向量化结果错误

pointers - 汇编中的星号指针 (I32/x86)

assembly - 如何知道程序入口点是ARM还是Thumb模式

assembly - 引导加载程序在 qemu 中工作,但在 virtualbox 和硬件上失败

android - 通过 Cygwin 在 Windows 上为 x86 Android 构建 Android NDK 工具链

x86 - 在 x86-64 中加载和存储长 double

c++ - 堆栈上的 G++ SSE 内存对齐

c++ - 将 SSE 内在函数转换为可读的 C/C++ 代码?

arrays - strb w1, [x22,x23] 当x23达到一定值时导致的段错误