src/core/math_func.hpp
author rubidium
Thu, 24 Apr 2008 12:15:24 +0000
branch0.6
changeset 10326 2820eb37d828
parent 9117 5aaa21219f68
permissions -rw-r--r--
(svn r12867) [0.6] -Backport from trunk r12706, r12642, r12622, r12572, r12542:
- Fix: Do not crash very hard on unrecognised savegames, just go back to the intro menu instead (r12706)
- Fix: Remove buggy buoys at tile 0 from old TTDP savegames (r12642)
- Fix: Infinite loop in case your compiler decides that enums are unsigned by default (r12622)
- Fix: min() has 32bit arguments, clamping of 64bit values did not work (r12572)
- Fix: Do not install scenarios into the current user's homedir when running 'make install', that is silly. Simply always install scenarios system wide instead (r12542)
8587
6db234b2b897 (svn r11652) -Codechange: add the svn $ header for several files
smatz
parents: 8466
diff changeset
     1
/* $Id$ */
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
     2
8587
6db234b2b897 (svn r11652) -Codechange: add the svn $ header for several files
smatz
parents: 8466
diff changeset
     3
/** @file math_func.hpp Integer math functions */
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
     4
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
     5
#ifndef MATH_FUNC_HPP
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
     6
#define MATH_FUNC_HPP
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
     7
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
     8
#ifdef min
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
     9
#undef min
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    10
#endif
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    11
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    12
#ifdef max
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    13
#undef max
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    14
#endif
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    15
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    16
#ifdef abs
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    17
#undef abs
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    18
#endif
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    19
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    20
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    21
 * Returns the maximum of two values.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    22
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    23
 * This function returns the greater value of two given values.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    24
 * If they are equal the value of a is returned.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    25
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    26
 * @param a The first value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    27
 * @param b The second value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    28
 * @return The greater value or a if equals
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    29
 */
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    30
template<typename T> static inline T max(const T a, const T b)
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    31
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    32
	return (a >= b) ? a : b;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    33
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    34
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    35
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    36
 * Returns the minimum of two values.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    37
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    38
 * This function returns the smaller value of two given values.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    39
 * If they are equal the value of b is returned.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    40
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    41
 * @param a The first value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    42
 * @param b The second value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    43
 * @return The smaller value or b if equals
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    44
 */
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    45
template<typename T> static inline T min(const T a, const T b)
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    46
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    47
	return (a < b) ? a : b;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    48
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    49
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    50
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    51
 * Returns the minimum of two integer.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    52
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    53
 * This function returns the smaller value of two given integers.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    54
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    55
 * @param a The first integer
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    56
 * @param b The second integer
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    57
 * @return The smaller value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    58
 */
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    59
static inline int min(const int a, const int b)
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    60
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    61
	return (a < b) ? a : b;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    62
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    63
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    64
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    65
 * Returns the minimum of two unsigned integers.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    66
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    67
 * This function returns the smaller value of two given unsigned integers.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    68
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    69
 * @param a The first unsigned integer
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    70
 * @param b The second unsigned integer
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    71
 * @return The smaller value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    72
 */
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    73
static inline uint minu(const uint a, const uint b)
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    74
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    75
	return (a < b) ? a : b;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    76
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    77
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    78
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    79
 * Returns the absolute value of (scalar) variable.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    80
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    81
 * @note assumes variable to be signed
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    82
 * @param a The value we want to unsign
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    83
 * @return The unsigned value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    84
 */
8450
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8433
diff changeset
    85
template <typename T> static inline T abs(const T a)
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    86
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    87
	return (a < (T)0) ? -a : a;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    88
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    89
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    90
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    91
 * Return the smallest multiple of n equal or greater than x
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    92
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    93
 * @note n must be a power of 2
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    94
 * @param x The min value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    95
 * @param n The base of the number we are searching
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    96
 * @return The smallest multiple of n equal or greater than x
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    97
 */
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    98
template<typename T> static inline T Align(const T x, uint n)
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    99
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   100
	n--;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   101
	return (T)((x + n) & ~(n));
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   102
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   103
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   104
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   105
 * Clamp an integer between an interval.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   106
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   107
 * This function returns a value which is between the given interval of
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   108
 * min and max. If the given value is in this interval the value itself
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   109
 * is returned otherwise the border of the interval is returned, according
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   110
 * which side of the interval was 'left'.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   111
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   112
 * @note The min value must be less or equal of max or you get some
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   113
 *       unexpected results.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   114
 * @param a The value to clamp/truncate.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   115
 * @param min The minimum of the interval.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   116
 * @param max the maximum of the interval.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   117
 * @returns A value between min and max which is closest to a.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   118
 * @see ClampU(uint, uint, uint)
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   119
 */
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   120
static inline int Clamp(const int a, const int min, const int max)
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   121
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   122
	if (a <= min) return min;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   123
	if (a >= max) return max;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   124
	return a;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   125
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   126
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   127
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   128
 * Clamp an unsigned integer between an interval.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   129
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   130
 * This function returns a value which is between the given interval of
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   131
 * min and max. If the given value is in this interval the value itself
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   132
 * is returned otherwise the border of the interval is returned, according
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   133
 * which side of the interval was 'left'.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   134
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   135
 * @note The min value must be less or equal of max or you get some
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   136
 *       unexpected results.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   137
 * @param a The value to clamp/truncate.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   138
 * @param min The minimum of the interval.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   139
 * @param max the maximum of the interval.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   140
 * @returns A value between min and max which is closest to a.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   141
 * @see Clamp(int, int, int)
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   142
 */
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   143
static inline uint ClampU(const uint a, const uint min, const uint max)
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   144
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   145
	if (a <= min) return min;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   146
	if (a >= max) return max;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   147
	return a;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   148
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   149
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   150
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   151
 * Reduce a signed 64-bit int to a signed 32-bit one
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   152
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   153
 * This function clamps a 64-bit integer to a 32-bit integer.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   154
 * If the 64-bit value is smaller than the smallest 32-bit integer
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   155
 * value 0x80000000 this value is returned (the left one bit is the sign bit).
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   156
 * If the 64-bit value is greater than the greatest 32-bit integer value 0x7FFFFFFF
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   157
 * this value is returned. In all other cases the 64-bit value 'fits' in a
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   158
 * 32-bits integer field and so the value is casted to int32 and returned.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   159
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   160
 * @param a The 64-bit value to clamps
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   161
 * @return The 64-bit value reduced to a 32-bit value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   162
 * @see Clamp(int, int, int)
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   163
 */
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   164
static inline int32 ClampToI32(const int64 a)
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   165
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   166
	if (a <= (int32)0x80000000) return 0x80000000;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   167
	if (a >= (int32)0x7FFFFFFF) return 0x7FFFFFFF;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   168
	return (int32)a;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   169
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   170
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   171
/**
9106
604b69ee3879 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8626
diff changeset
   172
 * Reduce an usigned 64-bit int to an unsigned 16-bit one
604b69ee3879 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8626
diff changeset
   173
 *
604b69ee3879 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8626
diff changeset
   174
 * @param a The 64-bit value to clamp
604b69ee3879 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8626
diff changeset
   175
 * @return The 64-bit value reduced to a 16-bit value
604b69ee3879 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8626
diff changeset
   176
 * @see ClampU(uint, uint, uint)
604b69ee3879 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8626
diff changeset
   177
 */
604b69ee3879 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8626
diff changeset
   178
static inline uint16 ClampToU16(const uint64 a)
604b69ee3879 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8626
diff changeset
   179
{
10326
2820eb37d828 (svn r12867) [0.6] -Backport from trunk r12706, r12642, r12622, r12572, r12542:
rubidium
parents: 9117
diff changeset
   180
	return (uint16)(a <= 0xFFFFU ? a : 0xFFFFU);
9106
604b69ee3879 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8626
diff changeset
   181
}
604b69ee3879 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8626
diff changeset
   182
604b69ee3879 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8626
diff changeset
   183
/**
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   184
 * Returns the (absolute) difference between two (scalar) variables
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   185
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   186
 * @param a The first scalar
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   187
 * @param b The second scalar
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   188
 * @return The absolute difference between the given scalars
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   189
 */
8466
9ce95e16f9f9 (svn r11526) -Codechange: Rename the function delta fitting to the naming style
skidd13
parents: 8450
diff changeset
   190
template <typename T> static inline T Delta(const T a, const T b) {
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   191
	return (a < b) ? b - a : a - b;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   192
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   193
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   194
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   195
 * Checks if a value is between a window started at some base point.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   196
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   197
 * This function checks if the value x is between the value of base
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   198
 * and base+size. If x equals base this returns true. If x equals
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   199
 * base+size this returns false.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   200
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   201
 * @param x The value to check
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   202
 * @param base The base value of the interval
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   203
 * @param size The size of the interval
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   204
 * @return True if the value is in the interval, false else.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   205
 */
8450
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8433
diff changeset
   206
template<typename T> static inline bool IsInsideBS(const T x, const uint base, const uint size)
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   207
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   208
	return (uint)(x - base) < size;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   209
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   210
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   211
/**
8450
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8433
diff changeset
   212
 * Checks if a value is in an interval.
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   213
 *
8450
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8433
diff changeset
   214
 * Returns true if a value is in the interval of [min, max).
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   215
 *
8450
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8433
diff changeset
   216
 * @param a The value to check
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   217
 * @param min The minimum of the interval
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   218
 * @param max The maximum of the interval
8450
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8433
diff changeset
   219
 * @see IsInsideBS()
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   220
 */
8450
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8433
diff changeset
   221
template<typename T> static inline bool IsInsideMM(const T x, const uint min, const uint max)
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   222
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   223
	return (uint)(x - min) < (max - min);
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   224
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   225
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   226
/**
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   227
 * Type safe swap operation
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   228
 * @param a variable to swap with b
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   229
 * @param b variable to swap with a
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   230
 */
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   231
template<typename T> void Swap(T& a, T& b)
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   232
{
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   233
	T t = a;
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   234
	a = b;
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   235
	b = t;
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   236
}
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   237
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   238
#endif /* MATH_FUNC_HPP */