Main Archive Specials Wiki | FAQ Links Submit Forum

 Clipping without branchingType : Min, max and clipReferences : Posted by Laurent de Soras Notes : It may reduce accuracy for small numbers. I.e. if you clip to [-1; 1], fractional part of the result will be quantized to 23 bits (or more, depending on the bit depth of the temporary results). Thus, 1e-20 will be rounded to 0. The other (positive) side effect is the denormal number elimination.Code : float max (float x, float a) { x -= a; x += fabs (x); x *= 0.5; x += a; return (x); } float min (float x, float b) { x = b - x; x += fabs (x) x *= 0.5; x = b - x; return (x); } float clip (float x, float a, float b) { x1 = fabs (x-a); x2 = fabs (x-b); x = x1 + (a+b); x -= x2; x *= 0.5; return (x); }

 CommentsAdded on : 15/04/02 by kleps[ AT ]refx[ DOT ]netComment : AFAIK, the fabs() is using if()...Added on : 27/05/02 by andy[AT]vellocet[ DOT ]comComment : fabs/fabsf do not use if and are quicker than: if (x<0) x = -x; Do the speed tests yourself if you don't believe me!Added on : 26/06/02 by kaleja[ AT ]estarcion[ DOT ]comComment : Depends on CPU and optimization options, but yes, Visual C++/x86/full optimization uses intrinsic fabs, which is very cool.Added on : 21/10/03 by lennart[ DOT ]denninger[AT]guerrilla-games[ DOT ]comComment : And ofcourse you could always use one of those nifty bit-tricks for fabs :) (Handy when you don't want to link with the math-library, like when coding a softsynth for a 4Kb-executable demo :))Added on : 30/01/04 by andy[ AT ]a2hd[ DOT ]comComment : according to my benchmarks (using the cpu clock cycle counter), fabs and the 'nifty bit tricks' have identicle performance characterstics, EXCEPT that with the nifty bit trick, sometimes it has a -horrible- penalty, which depends on the context..., maybe it does not optimize consistently?  I use libmath fabs now.  (i'm using gcc-3.3/linux on a P3)Added on : 12/04/04 by kaleja[ AT ]estarcion[ DOT ]comComment : Precision can be a major problem with these. In particular, if you have an algorithm that blows up with negative input, don't guard via clip( in, 0.0, 1.0 ) - it will occasionally go negative.

 Add your own comment Comments are displayed in fixed width, no HTML code allowed! Email: Comment: Are you human?

Site created and maintained by Bram
Graphic design by line.out | Server sponsered by fxpansion