Archived

This topic is now archived and is closed to further replies.

madman_k

swap macro done strange

Recommended Posts

i was browsing some code i somehow got (i don''t remember how, but i think it was a tutorial source) and i found this macro definition. #define SWAP(A,B) { (A) ^= (B); (B) ^= (A); (A) ^= (B); } I had never seen a swap implemented this way before and I was wondering if anyone here had. Also wondering if you think it will be more effective then a simple templated inline Swap function in terms of speed? Thanks

Share this post


Link to post
Share on other sites
The x86 CPU has a XCHG (exchange) instruction.
Do not to try to be too clever.

And C++ does have a standard std::swap() function.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
thanks. Also, i found some other odd functions there:

#define BIG_NUM ((double)1.5 * (double)(1 << 26) * (double)(1 << 26))

inline int
Round(double a)
{
a += BIG_NUM;
return *(int*)(&a);
}

inline int
Trunc(double a)
{
a += BIG_NUM - 0.5;
return *(int*)(&a);
}

why not for trunc just do:
inline int trunc(double a)
{
return (int) a;
}

and for round:
inline int round(double a)
{
return (int) (a + .5f);
}

do you think there''s a reason behind the way he did it above?

Share this post


Link to post
Share on other sites