怎么办饱和加用C? [英] How to do saturating addition in C?

查看:142
本文介绍了怎么办饱和加用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是最好的(最清洁,最高效的)的方式来编写C饱和加?

What is the best (cleanest, most efficient) way to write saturating addition in C?

函数或宏应该增加两个无符号输入(需要16位和32位版本),并返回,如果总和溢出所有位一(0xFFFF或0xFFFFFFFF的)。

The function or macro should add two unsigned inputs (need both 16- and 32-bit versions) and return all-bits-one (0xFFFF or 0xFFFFFFFF) if the sum overflows.

目标是x86和使用gcc(4.1.2),ARM和Visual Studio(用于模拟而已,所以后备实现是OK那里)。

Target is x86 and ARM using gcc (4.1.2) and Visual Studio (for simulation only, so a fallback implementation is OK there).

推荐答案

您可能需要移植的C code在这里,你的编译器将变成正确的ARM汇编。 ARM拥有有条件的动作,而这些可以在有条件的溢出。然后,该算法变得添加,以及目的地有条件设定为无符号(-1)如果检测溢出

You probably want portable C code here, which your compiler will turn into proper ARM assembly. ARM has conditional moves, and these can be conditional on overflow. The algorithm then becomes add, and conditionally set the destination to unsigned(-1) if overflow was detected.

uint16_t add16(uint16_t a, uint16_t b)
{
  uint16_t c = a + b;
  if (c<a) /* Can only happen due to overflow */
    c = -1;
  return c;
}

请注意,该不同之处在于它可以解决,而不是依靠其它计算来检测溢出溢流,其它算法

Note that this differs from the other algorithms in that it corrects overflow, instead of relying on another calculation to detect overflow.

X86-64铛3.7 -O3输出adds32 :比其他任何答案显著更好:

x86-64 clang 3.7 -O3 output for adds32: significantly better than any other answer:

    add     edi, esi
    mov     eax, -1
    cmovae  eax, edi
    ret

的ARMv7: GCC 4.8 -O3 -mcpu =的Cortex-A15 -fverbose-ASM 为adds32 输出:

ARMv7: gcc 4.8 -O3 -mcpu=cortex-a15 -fverbose-asm output for adds32:

    adds    r0, r0, r1      @ c, a, b
    it      cs
    movcs   r0, #-1         @ conditional-move
    bx      lr

16位:仍然没有使用ARM的无符号饱和加法指令( UADD16

    add     r1, r1, r0        @ tmp114, a
    movw    r3, #65535      @ tmp116,
    uxth    r1, r1  @ c, tmp114
    cmp     r0, r1    @ a, c
    ite     ls        @
    movls   r0, r1        @,, c
    movhi   r0, r3        @,, tmp116
    bx      lr  @

这篇关于怎么办饱和加用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆