summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsmatz <smatz@openttd.org>2011-04-01 12:17:23 +0000
committersmatz <smatz@openttd.org>2011-04-01 12:17:23 +0000
commitd6a70ea1d92094b5d2f642e949cabf81416ce04b (patch)
tree645f1a419e6b17cdb26d2460e7f248b9064d9a5f /src
parentfff18f6087fe571eeaafe0f42531c430bed8b756 (diff)
downloadopenttd-d6a70ea1d92094b5d2f642e949cabf81416ce04b.tar.xz
(svn r22284) -Codechange [FS#4564]: cast values to uint before computing modulus in direction_func.h, so compiler can generate superior code (adf88)
Diffstat (limited to 'src')
-rw-r--r--src/direction_func.h13
1 files changed, 9 insertions, 4 deletions
diff --git a/src/direction_func.h b/src/direction_func.h
index 8c8170a15..f3b714a8e 100644
--- a/src/direction_func.h
+++ b/src/direction_func.h
@@ -35,7 +35,9 @@ static inline Direction ReverseDir(Direction d)
*/
static inline DirDiff DirDifference(Direction d0, Direction d1)
{
- return (DirDiff)((d0 + 8 - d1) % 8);
+ /* Cast to uint so compiler can use bitmask. If the difference is negative
+ * and we used int instead of uint, further "+ 8" would have to be added. */
+ return (DirDiff)((uint)(d0 - d1) % 8);
}
/**
@@ -51,7 +53,8 @@ static inline DirDiff DirDifference(Direction d0, Direction d1)
*/
static inline DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
{
- return (DirDiff)((d + delta) % 8);
+ /* Cast to uint so compiler can use bitmask. Result can never be negative. */
+ return (DirDiff)((uint)(d + delta) % 8);
}
/**
@@ -66,7 +69,8 @@ static inline DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
*/
static inline Direction ChangeDir(Direction d, DirDiff delta)
{
- return (Direction)((d + delta) % 8);
+ /* Cast to uint so compiler can use bitmask. Result can never be negative. */
+ return (Direction)((uint)(d + delta) % 8);
}
@@ -94,7 +98,8 @@ static inline DiagDirection ReverseDiagDir(DiagDirection d)
*/
static inline DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
{
- return (DiagDirection)((d + delta) % 4);
+ /* Cast to uint so compiler can use bitmask. Result can never be negative. */
+ return (DiagDirection)((uint)(d + delta) % 4);
}
/**