Clamping short to unsigned char

2019-07-11 03:15发布

I have a simple C function as follows:

unsigned char clamp(short value){
    if (value < 0) return 0;
    if (value > 0xff) return 0xff;
    return value;
}

Is it possible to rewrite it without using any if / else branching while being efficient?

EDIT:

I basically wish to see if some bitwise arithmetic based implementation of clamping is possible. Objective is to process images on GPU (Graphics Processing Unit). This type of code will run on each pixel. I guess that if branches can be avoided, then overall throughput over the GPU would be higher.

A solution like (value <0? 0 : ((value > 255) ? 255 : value) ) is simply a rehash of if/else branching with syntactic sugar. So I am not looking for it.

EDIT 2:

I can cut it down to a single if as follows but I am not able to think better:

unsigned char clamp(short value){
    int more = value >> 8;
    if(more){
        int sign = !(more >> 7);
        return sign * 0xff;
    }
    return value;
}

EDIT 3:

Just saw a very nice implementation of this in FFmpeg code:

/**
 * Clip a signed integer value into the 0-255 range.
 * @param a value to clip
 * @return clipped value
 */
static av_always_inline av_const uint8_t av_clip_uint8_c(int a)
{
    if (a&(~0xFF)) return (-a)>>31;
    else           return a;
}

This certainly works and reduces it to one if nicely.

标签: c
8条回答
Root(大扎)
2楼-- · 2019-07-11 04:00

How about:

unsigned char clamp (short value) {
    unsigned char r = (value >> 15);          /* uses arithmetic right-shift */
    unsigned char s = !!(value & 0x7f00) * 0xff;
    unsigned char v = (value & 0xff);
    return (v | s) & ~r;
}

But I seriously doubt that it executes any faster than your original version involving branches.

查看更多
ら.Afraid
3楼-- · 2019-07-11 04:01

If you just want to avoid the actual if/else, using the ? : operator:

return value < 0 ? 0 : (value > 0xff ? 0xff : value);

However, in terms of efficiency this shouldn't be any different.

In practice, you shouldn't worry about efficiency with something so trivial as this. Let the compiler do the optimization.

查看更多
登录 后发表回答