Re: [Algorithms] How to get 3dvector largest coordinate index?
Brought to you by:
vexxed72
From: Glenn F. <ga...@ga...> - 2009-03-01 09:12:53
|
garrrrr! i hate coding in gmail :) union FloatInt { unsigned int i; float f; }; FloatInt x, y, z; x.f = n.x; y.f = n.y; z.f = n.z; x.i &= 0x7FFFFFFF; y.i &= 0x7FFFFFFF; z.i &= 0x7FFFFFFF; return (x.i > y.i) ? (x.i > z.i) ? 0 : 2 : (y.i > z.i) ? 1 : 2; cheers On Sun, Mar 1, 2009 at 1:10 AM, Glenn Fiedler <ga...@ga...> wrote: > if you would like this on GCC with -O2 or greater, you'll need to use > unions instead of aliasing pointers, like this: > union FloatInt > { > unsigned int i; > float f; > }; > > FloatInt nx, ny, nz; > x.f = n.x; > y.f = n.y; > z.f = n.z; > return (x.i > y.i) ? (x.i > z.i) ? 0 : 2 : (y.i > z.i) ? 1 : 2; > > just fyi, you are going to have very VERY difficult optimizer bugs to track > down otherwise - and yes, unions are the only way to guarantee that GCC > generates the correct code in this case > > cheers > > > 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 >> > >> >> >> ------------------------------------------------------------------------------ >> Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, >> CA >> -OSBC tackles the biggest issue in open source: Open Sourcing the >> Enterprise >> -Strategies to boost innovation and cut costs with open source >> participation >> -Receive a $600 discount off the registration fee with the source code: >> SFAD >> http://p.sf.net/sfu/XcvMzF8H >> _______________________________________________ >> GDAlgorithms-list mailing list >> GDA...@li... >> https://lists.sourceforge.net/lists/listinfo/gdalgorithms-list >> Archives: >> http://sourceforge.net/mailarchive/forum.php?forum_name=gdalgorithms-list >> > > |