Main Archive Specials Wiki | FAQ Links Submit Forum

 Branchless ClippingType : Clipping at 0dB, with none of the usual 'if..then..'References : Posted by musicdsp[AT]dsparsons[DOT]co[DOT]ukNotes : I was working on something that I wanted to ensure that the signal never went above 0dB, and a branchless solution occurred to me. It works by playing with the structure of a single type, shifting the sign bit down to make a new mulitplicand. calling MaxZerodB(mydBSample) will ensure that it will never stray over 0dB. By playing with signs or adding/removing offsets, this offers a complete branchless limiting solution, no matter whether dB or not (after all, they're all just numbers...). eg: Limit to <=0 : sample:=MaxZerodB(sample); Limit to <=3 : sample:=MaxZerodB(sample-3)+3; Limit to <=-4 : sample:=MaxZerodB(sample+4)-4; Limit to >=0 : sample:=-MaxZerodB(-sample); Limit to >=2 : sample:=-MaxZerodB(-sample+2)+2; Limit to >=-1.5: sample:=-MaxZerodB(-sample-1.5)-1.5; Whether it actually saves any CPU cycles remains to be seen, but it was an interesting diversion for half an hour :) [Translating from pascal to other languages shouldn't be too hard, and for doubles, you'll need to fiddle it abit :)]Code : function MaxZerodB(dBin:single):single; var tmp:longint; begin //given that leftmost bit of a longint indicates the negative, // if we shift that down to bit0, and multiply dBin by that // it will return dBin, or zero :) tmp:=(longint((@dBin)^) and \$80000000) shr 31; result:=dBin*tmp; end;