A typical unoptimized implementation looks like follows.
1: unsigned char clamp(short value){2: if (value < 0) return 0;3: if (value > 0xff) return 0xff;4: return value;5: }
After long time, today I ended up seeing a much nicer implementation in FFmpeg code base which basically looks like:.
1: const unsigned char clamp(int a)2: {3: if (a&(~0xFF)) return (-a)>>31;4: else return a;5: }
And I think, with a single if statement, its wonderful!
No comments:
Post a Comment