summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/bitmath_func.hpp15
-rw-r--r--src/core/endian_func.hpp22
-rw-r--r--src/core/math_func.hpp6
-rw-r--r--src/core/overflowsafe_type.hpp2
-rw-r--r--src/core/random_func.cpp2
5 files changed, 46 insertions, 1 deletions
diff --git a/src/core/bitmath_func.hpp b/src/core/bitmath_func.hpp
index 357a52212..6f9c35806 100644
--- a/src/core/bitmath_func.hpp
+++ b/src/core/bitmath_func.hpp
@@ -276,4 +276,19 @@ template<typename T> static inline T ROR(const T x, const uint8 n)
return (T)(x >> n | x << (sizeof(x) * 8 - n));
}
+/**
+ * Do an operation for each set set bit in a value.
+ *
+ * This macros is used to do an operation for each set
+ * bit in a variable. The first variable can be reused
+ * in the operation due to it's the bit position counter.
+ * The second variable will be cleared during the usage
+ *
+ * @param i The position counter
+ * @param b The value which we check for set bits
+ */
+#define FOR_EACH_SET_BIT(i, b) \
+ for (i = 0; b != 0; i++, b >>= 1) \
+ if (b & 1)
+
#endif /* BITMATH_FUNC_HPP */
diff --git a/src/core/endian_func.hpp b/src/core/endian_func.hpp
new file mode 100644
index 000000000..545e6b986
--- /dev/null
+++ b/src/core/endian_func.hpp
@@ -0,0 +1,22 @@
+/* $Id$ */
+
+/** @file endian_func.hpp */
+
+#ifndef ENDIAN_FUNC_H
+#define ENDIAN_FUNC_H
+
+static inline uint16 ReadLE16Aligned(const void *x)
+{
+ return FROM_LE16(*(const uint16*)x);
+}
+
+static inline uint16 ReadLE16Unaligned(const void *x)
+{
+#ifdef OTTD_ALIGNMENT
+ return ((const byte*)x)[0] | ((const byte*)x)[1] << 8;
+#else
+ return FROM_LE16(*(const uint16*)x);
+#endif
+}
+
+#endif /* ENDIAN_FUNC_H */
diff --git a/src/core/math_func.hpp b/src/core/math_func.hpp
index c05e7ffeb..242b17b95 100644
--- a/src/core/math_func.hpp
+++ b/src/core/math_func.hpp
@@ -18,6 +18,12 @@
#endif
/**
+ * The largest value that can be entered in a variable
+ * @param type the type of the variable
+ */
+#define MAX_UVALUE(type) ((type)~(type)0)
+
+/**
* Returns the maximum of two values.
*
* This function returns the greater value of two given values.
diff --git a/src/core/overflowsafe_type.hpp b/src/core/overflowsafe_type.hpp
index 632e261a6..8f1f404a7 100644
--- a/src/core/overflowsafe_type.hpp
+++ b/src/core/overflowsafe_type.hpp
@@ -5,6 +5,8 @@
#ifndef OVERFLOWSAFE_TYPE_HPP
#define OVERFLOWSAFE_TYPE_HPP
+#include "math_func.hpp"
+
/**
* Overflow safe template for integers, i.e. integers that will never overflow
* you multiply the maximum value with 2, or add 2, or substract somethng from
diff --git a/src/core/random_func.cpp b/src/core/random_func.cpp
index 7e2f7fbf9..803250040 100644
--- a/src/core/random_func.cpp
+++ b/src/core/random_func.cpp
@@ -3,9 +3,9 @@
/** @file random_func.cpp */
#include "../stdafx.h"
-#include "../macros.h"
#include "../variables.h"
#include "random_func.hpp"
+#include "bitmath_func.hpp"
uint32 InteractiveRandom()
{