src/core/math_func.hpp
author peter1138
Tue, 22 Jan 2008 07:27:06 +0000
changeset 8374 7a1b6c89cb89
parent 8130 d2eb7d04f6e1
child 8610 3d2183c29dc2
permissions -rw-r--r--
(svn r11940) -Codechange: Store short filename once per open file instead of once per sprite cache entry. Not all file types need this, but most of the time no sprite cache entry needed it either.
8091
674be8638d74 (svn r11652) -Codechange: add the svn $ header for several files
smatz
parents: 7970
diff changeset
     1
/* $Id$ */
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
     2
8091
674be8638d74 (svn r11652) -Codechange: add the svn $ header for several files
smatz
parents: 7970
diff changeset
     3
/** @file math_func.hpp Integer math functions */
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
     4
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
     5
#ifndef MATH_FUNC_HPP
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
     6
#define MATH_FUNC_HPP
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
     7
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
     8
#ifdef min
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
     9
#undef min
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    10
#endif
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    11
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    12
#ifdef max
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    13
#undef max
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    14
#endif
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    15
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    16
#ifdef abs
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    17
#undef abs
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    18
#endif
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    19
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    20
/**
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    21
 * Returns the maximum of two values.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    22
 *
1e7a22ea20d4 (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.
1e7a22ea20d4 (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.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    25
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    26
 * @param a The first value
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    27
 * @param b The second value
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    28
 * @return The greater value or a if equals
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    29
 */
1e7a22ea20d4 (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)
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    31
{
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    32
	return (a >= b) ? a : b;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    33
}
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    34
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    35
/**
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    36
 * Returns the minimum of two values.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    37
 *
1e7a22ea20d4 (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.
1e7a22ea20d4 (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.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    40
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    41
 * @param a The first value
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    42
 * @param b The second value
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    43
 * @return The smaller value or b if equals
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    44
 */
1e7a22ea20d4 (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)
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    46
{
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    47
	return (a < b) ? a : b;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    48
}
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    49
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    50
/**
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    51
 * Returns the minimum of two integer.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    52
 *
1e7a22ea20d4 (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.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    54
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    55
 * @param a The first integer
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    56
 * @param b The second integer
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    57
 * @return The smaller value
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    58
 */
1e7a22ea20d4 (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)
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    60
{
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    61
	return (a < b) ? a : b;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    62
}
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    63
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    64
/**
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    65
 * Returns the minimum of two unsigned integers.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    66
 *
1e7a22ea20d4 (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.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    68
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    69
 * @param a The first unsigned integer
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    70
 * @param b The second unsigned integer
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    71
 * @return The smaller value
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    72
 */
1e7a22ea20d4 (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)
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    74
{
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    75
	return (a < b) ? a : b;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    76
}
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    77
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    78
/**
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    79
 * Returns the absolute value of (scalar) variable.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    80
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    81
 * @note assumes variable to be signed
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    82
 * @param a The value we want to unsign
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    83
 * @return The unsigned value
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    84
 */
7954
57b51c69c072 (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 7937
diff changeset
    85
template <typename T> static inline T abs(const T a)
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    86
{
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    87
	return (a < (T)0) ? -a : a;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    88
}
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    89
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    90
/**
1e7a22ea20d4 (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
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    92
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    93
 * @note n must be a power of 2
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    94
 * @param x The min value
1e7a22ea20d4 (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
1e7a22ea20d4 (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
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    97
 */
1e7a22ea20d4 (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)
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    99
{
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   100
	n--;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   101
	return (T)((x + n) & ~(n));
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   102
}
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   103
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   104
/**
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   105
 * Clamp an integer between an interval.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   106
 *
1e7a22ea20d4 (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
1e7a22ea20d4 (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
1e7a22ea20d4 (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
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   110
 * which side of the interval was 'left'.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   111
 *
1e7a22ea20d4 (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
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   113
 *       unexpected results.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   114
 * @param a The value to clamp/truncate.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   115
 * @param min The minimum of the interval.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   116
 * @param max the maximum of the interval.
1e7a22ea20d4 (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.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   118
 * @see ClampU(uint, uint, uint)
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   119
 */
1e7a22ea20d4 (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)
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   121
{
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   122
	if (a <= min) return min;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   123
	if (a >= max) return max;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   124
	return a;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   125
}
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   126
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   127
/**
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   128
 * Clamp an unsigned integer between an interval.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   129
 *
1e7a22ea20d4 (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
1e7a22ea20d4 (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
1e7a22ea20d4 (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
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   133
 * which side of the interval was 'left'.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   134
 *
1e7a22ea20d4 (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
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   136
 *       unexpected results.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   137
 * @param a The value to clamp/truncate.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   138
 * @param min The minimum of the interval.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   139
 * @param max the maximum of the interval.
1e7a22ea20d4 (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.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   141
 * @see Clamp(int, int, int)
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   142
 */
1e7a22ea20d4 (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)
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   144
{
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   145
	if (a <= min) return min;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   146
	if (a >= max) return max;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   147
	return a;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   148
}
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   149
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   150
/**
1e7a22ea20d4 (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
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   152
 *
1e7a22ea20d4 (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.
1e7a22ea20d4 (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
1e7a22ea20d4 (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).
1e7a22ea20d4 (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
1e7a22ea20d4 (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
1e7a22ea20d4 (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.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   159
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   160
 * @param a The 64-bit value to clamps
1e7a22ea20d4 (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
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   162
 * @see Clamp(int, int, int)
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   163
 */
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   164
static inline int32 ClampToI32(const int64 a)
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   165
{
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   166
	if (a <= (int32)0x80000000) return 0x80000000;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   167
	if (a >= (int32)0x7FFFFFFF) return 0x7FFFFFFF;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   168
	return (int32)a;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   169
}
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   170
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   171
/**
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   172
 * Returns the (absolute) difference between two (scalar) variables
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   173
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   174
 * @param a The first scalar
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   175
 * @param b The second scalar
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   176
 * @return The absolute difference between the given scalars
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   177
 */
7970
7d6b9ab57081 (svn r11526) -Codechange: Rename the function delta fitting to the naming style
skidd13
parents: 7954
diff changeset
   178
template <typename T> static inline T Delta(const T a, const T b) {
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   179
	return (a < b) ? b - a : a - b;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   180
}
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   181
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   182
/**
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   183
 * Checks if a value is between a window started at some base point.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   184
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   185
 * This function checks if the value x is between the value of base
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   186
 * and base+size. If x equals base this returns true. If x equals
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   187
 * base+size this returns false.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   188
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   189
 * @param x The value to check
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   190
 * @param base The base value of the interval
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   191
 * @param size The size of the interval
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   192
 * @return True if the value is in the interval, false else.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   193
 */
7954
57b51c69c072 (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 7937
diff changeset
   194
template<typename T> static inline bool IsInsideBS(const T x, const uint base, const uint size)
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   195
{
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   196
	return (uint)(x - base) < size;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   197
}
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   198
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   199
/**
7954
57b51c69c072 (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 7937
diff changeset
   200
 * Checks if a value is in an interval.
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   201
 *
7954
57b51c69c072 (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 7937
diff changeset
   202
 * Returns true if a value is in the interval of [min, max).
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   203
 *
7954
57b51c69c072 (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 7937
diff changeset
   204
 * @param a The value to check
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   205
 * @param min The minimum of the interval
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   206
 * @param max The maximum of the interval
7954
57b51c69c072 (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 7937
diff changeset
   207
 * @see IsInsideBS()
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   208
 */
7954
57b51c69c072 (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 7937
diff changeset
   209
template<typename T> static inline bool IsInsideMM(const T x, const uint min, const uint max)
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   210
{
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   211
	return (uint)(x - min) < (max - min);
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   212
}
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   213
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   214
/**
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   215
 * Type safe swap operation
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   216
 * @param a variable to swap with b
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   217
 * @param b variable to swap with a
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   218
 */
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   219
template<typename T> void Swap(T& a, T& b)
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   220
{
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   221
	T t = a;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   222
	a = b;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   223
	b = t;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   224
}
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   225
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   226
#endif /* MATH_FUNC_HPP */