From 6c1fed2b29ab331108c45af788193ea7ccf4703a Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sun, 11 Jul 2004 03:50:52 +0000 Subject: [PATCH] fix illegal aliasing --- stepmania/src/EnumHelper.h | 33 ++++++++++++++++++++++++++++++++- stepmania/src/RageUtil.h | 31 ------------------------------- stepmania/src/ScreenSetTime.cpp | 1 + 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/stepmania/src/EnumHelper.h b/stepmania/src/EnumHelper.h index a26dd12fb7..a5f0f468a1 100644 --- a/stepmania/src/EnumHelper.h +++ b/stepmania/src/EnumHelper.h @@ -1,7 +1,38 @@ #ifndef EnumHelper_H #define EnumHelper_H -#define FOREACH_ENUM( e, max, var ) for( e var=(e)0; var +static inline void enum_add( T &val, int iAmt ) +{ + union conv + { + T value; + int i; + }; + + conv c; + c.value = val; + c.i += iAmt; + val = c.value; +} + +#define FOREACH_ENUM( e, max, var ) for( e var=(e)0; var( var, +1 ) ) static const CString EMPTY_STRING; diff --git a/stepmania/src/RageUtil.h b/stepmania/src/RageUtil.h index d181683dd9..2fd1571bff 100644 --- a/stepmania/src/RageUtil.h +++ b/stepmania/src/RageUtil.h @@ -166,37 +166,6 @@ inline float froundf( const float f, const float fRoundInterval ) return int( (f + fRoundInterval/2)/fRoundInterval ) * fRoundInterval; } -/* - * Safely add an integer to an enum. - * - * This is illegal: - * - * ((int&)val) += iAmt; - * - * It breaks aliasing rules; the compiler is allowed to assume that "val" doesn't - * change (unless it's declared volatile), and in some cases, you'll end up getting - * old values for "val" following the add. (What's probably really happening is - * that the memory location is being added to, but the value is stored in a register, - * and breaking aliasing rules mean the compiler doesn't know that the register - * value is invalid.) - * - * Always do these conversions through a union. - */ -template -static inline void enum_add( T &val, int iAmt ) -{ - union conv - { - T value; - int i; - }; - - conv c; - c.value = val; - c.i += iAmt; - val = c.value; -} - // Move val toward other_val by to_move. void fapproach( float& val, float other_val, float to_move ); diff --git a/stepmania/src/ScreenSetTime.cpp b/stepmania/src/ScreenSetTime.cpp index 51f11b72c4..88be794700 100644 --- a/stepmania/src/ScreenSetTime.cpp +++ b/stepmania/src/ScreenSetTime.cpp @@ -12,6 +12,7 @@ #include "ThemeManager.h" #include "RageDisplay.h" #include "Bookkeeper.h" +#include "EnumHelper.h" #include "arch/ArchHooks/ArchHooks.h" static const CString SetTimeSelectionNames[NUM_SET_TIME_SELECTIONS] = {