c++ - code optimising for if-else function -
i want write partition function. output values obtained comparing values of input dx , dy. below code run slow. suggestion speedup code?
i search web, seems using lookup table or sse instructions possible. little idea in implementing them.
thank much.
inline int fastatan2( int dy, int dx ) { if(dy<0) { dx *= -1; dy *= -1; } if( dx > 0 ){ if( dy < dx ) return (dy <= (( 17560*dx)>>16))? 0:1; else return (dy <= (( 244583*dx)>>16))? 2:3; }else{ if( dy < -dx ) return (dy <= (( -17560*dx)>>16))? 0:5; else return (dy <= ((-244583*dx)>>16))? 4:3; } }
here answer. first remove branches , implement sse. haven't check speed yet.
const int t1 = 17560; const int t2 = 244583; const int shift = 16; int = 2*((dx ^ dy) >= 0)-1; //check (dy,dx) opposite sign dy = abs(dy); dx = abs(dx); int b1 = (dy < dx); int b2 = (dy > ((t1*dx) >> shift)); int b3 = (dx > ((t1*dy) >> shift)); const int table1[]={3,2,3,2,0,0,1,1}; int ori = table1[b3 + b2*2 + b1*4]; //implement bit shuffle (_mm_shuffle_epi8) const int table2[]={0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5}; ori = table2[a*ori + 6]; //implement bit shuffle (_mm_shuffle_epi8) return ori;
the code above fast quantization of angle shown below. there differences @ (dx==0, dy==0) , boundary cases (abs(dx)==abs(dy), @ angle 45, 135 degrees).
int exact_atan2( int dx, int dy ){ float angle = atan2((float)dy, (float)dx) / 3.14159265359f * 180.f; float o = (angle + 360.f) / (180.f / 6) ; int ori = int(o+0.5)%6; return ori; }
Comments
Post a Comment