Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

14
Jun

Clipping and Clamping with out ‘if condition’

Given an example, if the signed integer number is more than 255 then set the number to 255(clipping). On the other hand, if the signed integer is less than 0, then set the number to 0(clamping). This is implemented in C using ‘if condition’, which looks like the below code

int ClipNClamp(int val)
{
if(val < 0) val = 0;
if(val > 255) val = 255;
return val;
}

This operation is very popular in many DSP applications, so it is required to optimize the above functionality. One way to achieve optimization is to replace ‘if condition’ with some arithmetic and logical operations. Optimized code without ‘if condition’ is below:

int ClipNClamp(int val)
{
unsigned char op1,op2;
op1 = 255 + (val < 0) + (val > 0xff);
op2 = 256 - (val > 0xff);
return((val & op1)^op2);
}

We again few clock cycles on implementing the functionality using the later code compared to the former implementation. These few cycles will cost us greatly on calling the above function many(multiple of 1000) times. If any of you have better optimized solutions to implement clipping and clamping within a range of 0 to 255, please let me know.Thanks!!!

Popularity: 5%