bstone wrote:... and make the code more efficient by removing unnecessary branching.
In any sane compiler, this is unnecessary. In debug mode you'll get overhead either way (either through if (4 == 4) not being eliminated, or by a call not being inlined). In fact I wouldn't be surprised if the if (4==4) being eliminated even in debug mode since it doesn't interfere with debugging. In optimised mode both cases trivially disappear due to local optimisations. However using extra templates definitely makes the code less readable.
Using template specialisation would avoid the symbol being linked even in debug mode. However it would be better to actually fix the problem (if indeed this is the problem), since it all it takes is someone to instantiate the class in a particular way and it would break XP again.
You are the second (possibly third) person who reads this code and thinks that if (sizeof(T) == 4) { } introduces extra overhead... I don't see anyone providing any evidence of this however and it does not make any sense.
The original code was modified by Sinbad to compile on VC2005. This diff may be the cause of the problem, you should try changing it back and see if that helps. Although since you already tried commenting out the entire line and that did not help, it is unlikely to make a difference.
Code: Select all
--- a/OgreMain/include/OgreAtomicWrappers.h Thu May 22 21:56:12 2008 +0000
+++ b/OgreMain/include/OgreAtomicWrappers.h Fri May 23 10:23:51 2008 +0000
@@ -213,12 +213,16 @@
bool cas (const T &old, const T &nu)
{
if (sizeof(T)==2) {
- return InterlockedCompareExchange16((SHORT*)&mField, static_cast<SHORT>(nu), static_cast<SHORT>(old)) == static_cast<SHORT>(old);
- } else if (sizeof(T)==4) {
- return InterlockedCompareExchange((LONG*)&mField, static_cast<LONG>(nu), static_cast<LONG>(old)) == static_cast<LONG>(old);
- } else if (sizeof(T)==2) {
- return InterlockedCompareExchange64((LONGLONG*)&mField, static_cast<LONGLONG>(nu), static_cast<LONGLONG>(old)) == static_cast<LONGLONG>(old);
- } else {
+ return _InterlockedCompareExchange16((SHORT*)&mField, static_cast<SHORT>(nu), static_cast<SHORT>(old)) == static_cast<SHORT>(old);
+ }
+ else if (sizeof(T)==4)
+ {
+ return _InterlockedCompareExchange((LONG*)&mField, static_cast<LONG>(nu), static_cast<LONG>(old)) == static_cast<LONG>(old);
+ }
+ else if (sizeof(T)==8) {
+ return _InterlockedCompareExchange64((LONGLONG*)&mField, static_cast<LONGLONG>(nu), static_cast<LONGLONG>(old)) == static_cast<LONGLONG>(old);
+ }
+ else {
OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED,"Only 16, 32, and 64 bit scalars supported in win32.","AtomicScalar::cas");
}
}