Re: [Algorithms] How to get 3dvector largest coordinate index?
Brought to you by:
vexxed72
From: Sylvain G. V. <vi...@ii...> - 2009-03-01 07:49:20
|
What's about something like: yx = SignBit(x-y)>>31 // 1 if y > x yz = SignBit(z-y)>>31 zx = SignBit(x-z)>>31 zy = SignBit(y-z)>>31 return (yx&yz) | ((zx&zy)<<1); 4 subs (int or float, doesn't matter), a few simple bit instructions, no jump (untested, just got back from a diner)? ----- Original Message ----- From: Matt J <mjo...@gm...> Date: Saturday, February 28, 2009 6:17 pm Subject: Re: [Algorithms] How to get 3dvector largest coordinate index? To: Game Development Algorithms <gda...@li...> > I did some profiling on MSVC 2009, and integer methods are ~10% > faster: > unsigned int x = 0x7FFFFFFF & *(unsigned int *)&n.x; > unsigned int y = 0x7FFFFFFF & *(unsigned int *)&n.y; > unsigned int z = 0x7FFFFFFF & *(unsigned int *)&n.z; > > return (x > y) ? (x > z) ? 0 : 2 : (y > z) ? 1 : 2; > > For some odd reason only when using integers can it simplify the > two inner > ternary methods using bit 'magic'. If we use floats or use SSE the > integermethods are about ~5% faster because this compiler generates > a bunch of > jumps. The method above (as well as mine) generates 1 jump. > > I actually eliminated all jumps but since I use one too many > registers it > slows it down. I think the CPU is smart enough to look ahead if you > get down > to one conditional ..so yeah I think if you use the stack it can > slow things > down slightly but you do see a gain if you use integers. I assume > conditional jumps would be the best way, assuming the compiler can > do it > depth first because you need 3 conditionals - the inner two, and > then a > conditional move on the results of those two > > Asm: > > > ; _n$ = edx > > ; 61 : unsigned int x = 0x7FFFFFFF & *(unsigned int *)&n.x; > > mov eax, DWORD PTR [edx] > > ; 62 : unsigned int y = 0x7FFFFFFF & *(unsigned int *)&n.y; > > mov ecx, DWORD PTR [edx+4] > > ; 63 : unsigned int z = 0x7FFFFFFF & *(unsigned int *)&n.z; > > mov edx, DWORD PTR [edx+8] > and eax, 2147483647 ; 7fffffffH > and ecx, 2147483647 ; 7fffffffH > and edx, 2147483647 ; 7fffffffH > > ; 69 : return (x > y) ? (x > z) ? 0 : 2 : (y > z) ? 1 : 2; > > cmp eax, ecx > jbe SHORT $LN3@coord_inde > cmp edx, eax > sbb eax, eax > and eax, -2 ; fffffffeH > add eax, 2 > > ; 70 : } > > ret 0 > $LN3@coord_inde: > > ; 69 : return (x > y) ? (x > z) ? 0 : 2 : (y > z) ? 1 : 2; > > cmp edx, ecx > sbb eax, eax > add eax, 2 > > ; 70 : } > > ret > |