src/core/math_func.hpp
author skidd13
Sun, 22 Jun 2008 15:41:38 +0000
changeset 11050 091271fcfbb9
parent 11049 f8bbc9635251
child 11082 45ab75d184a0
permissions -rw-r--r--
(svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
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
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
    30
template <typename T>
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
    31
static FORCEINLINE T max(const T a, const T b)
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    32
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    33
	return (a >= b) ? a : b;
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
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    37
 * Returns the minimum of two values.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    38
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    39
 * 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
    40
 * 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
    41
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    42
 * @param a The first value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    43
 * @param b The second value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    44
 * @return The smaller value or b if equals
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    45
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
    46
template <typename T>
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
    47
static FORCEINLINE T min(const T a, const T b)
8433
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
	return (a < b) ? a : b;
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
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
 * Returns the minimum of two integer.
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
 * 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
    56
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    57
 * @param a The first integer
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    58
 * @param b The second integer
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    59
 * @return The smaller value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    60
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
    61
static FORCEINLINE int min(const int a, const int b)
8433
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
	return (a < b) ? a : b;
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
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
 * Returns the minimum of two 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
 * 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
    70
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    71
 * @param a The first unsigned integer
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    72
 * @param b The second unsigned integer
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    73
 * @return The smaller value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    74
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
    75
static FORCEINLINE uint minu(const uint a, const uint b)
8433
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
	return (a < b) ? a : b;
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
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
 * Returns the absolute value of (scalar) variable.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    82
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    83
 * @note assumes variable to be signed
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    84
 * @param a The value we want to unsign
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    85
 * @return The unsigned value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    86
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
    87
template <typename T>
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
    88
static FORCEINLINE T abs(const T a)
8433
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
	return (a < (T)0) ? -a : a;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    91
}
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
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    94
 * 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
    95
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    96
 * @note n must be a power of 2
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    97
 * @param x The min value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    98
 * @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
    99
 * @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
   100
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
   101
template <typename T>
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
   102
static FORCEINLINE T Align(const T x, uint n)
8433
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
	n--;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   105
	return (T)((x + n) & ~(n));
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
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   108
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   109
 * Clamp an integer between an interval.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   110
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   111
 * 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
   112
 * 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
   113
 * 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
   114
 * which side of the interval was 'left'.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   115
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   116
 * @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
   117
 *       unexpected results.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   118
 * @param a The value to clamp/truncate.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   119
 * @param min The minimum of the interval.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   120
 * @param max the maximum of the interval.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   121
 * @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
   122
 * @see ClampU(uint, uint, uint)
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   123
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
   124
static FORCEINLINE int Clamp(const int a, const int min, const int max)
8433
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
	if (a <= min) return min;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   127
	if (a >= max) return max;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   128
	return a;
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
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   131
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   132
 * Clamp an unsigned integer between an interval.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   133
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   134
 * 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
   135
 * 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
   136
 * 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
   137
 * which side of the interval was 'left'.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   138
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   139
 * @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
   140
 *       unexpected results.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   141
 * @param a The value to clamp/truncate.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   142
 * @param min The minimum of the interval.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   143
 * @param max the maximum of the interval.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   144
 * @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
   145
 * @see Clamp(int, int, int)
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   146
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
   147
static FORCEINLINE uint ClampU(const uint a, const uint min, const uint max)
8433
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
	if (a <= min) return min;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   150
	if (a >= max) return max;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   151
	return a;
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
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   154
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   155
 * 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
   156
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   157
 * 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
   158
 * 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
   159
 * 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
   160
 * 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
   161
 * 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
   162
 * 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
   163
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   164
 * @param a The 64-bit value to clamps
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   165
 * @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
   166
 * @see Clamp(int, int, int)
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   167
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
   168
static FORCEINLINE int32 ClampToI32(const int64 a)
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   169
{
9321
3bc631f0ef34 (svn r12573) -Codechange: use defined constants instead of numbers in math_func.hpp
smatz
parents: 9320
diff changeset
   170
	if (a <= INT32_MIN) return INT32_MIN;
3bc631f0ef34 (svn r12573) -Codechange: use defined constants instead of numbers in math_func.hpp
smatz
parents: 9320
diff changeset
   171
	if (a >= INT32_MAX) return INT32_MAX;
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   172
	return (int32)a;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   173
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   174
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   175
/**
9238
5c676f037827 (svn r12438) -Fix-ish: typo spotted by Biblo.
rubidium
parents: 9117
diff changeset
   176
 * Reduce an unsigned 64-bit int to an unsigned 16-bit one
9106
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
 * @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
   179
 * @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
   180
 * @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
   181
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
   182
static FORCEINLINE uint16 ClampToU16(const uint64 a)
9106
604b69ee3879 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8626
diff changeset
   183
{
9321
3bc631f0ef34 (svn r12573) -Codechange: use defined constants instead of numbers in math_func.hpp
smatz
parents: 9320
diff changeset
   184
	return (uint16)(a <= UINT16_MAX ? a : UINT16_MAX);
9106
604b69ee3879 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8626
diff changeset
   185
}
604b69ee3879 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8626
diff changeset
   186
604b69ee3879 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8626
diff changeset
   187
/**
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   188
 * Returns the (absolute) difference between two (scalar) variables
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   189
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   190
 * @param a The first scalar
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   191
 * @param b The second scalar
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   192
 * @return The absolute difference between the given scalars
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   193
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
   194
template <typename T>
11050
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
   195
static FORCEINLINE T Delta(const T a, const T b)
091271fcfbb9 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 11049
diff changeset
   196
{
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   197
	return (a < b) ? b - a : a - b;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   198
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   199
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
 * 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
   202
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   203
 * 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
   204
 * 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
   205
 * base+size this returns false.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   206
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   207
 * @param x The value to check
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   208
 * @param base The base value of the interval
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   209
 * @param size The size of the interval
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   210
 * @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
   211
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
   212
template <typename T>
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
   213
static FORCEINLINE 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
   214
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   215
	return (uint)(x - base) < size;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   216
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   217
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   218
/**
8450
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8433
diff changeset
   219
 * 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
   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
 * 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
   222
 *
8450
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8433
diff changeset
   223
 * @param a The value to check
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   224
 * @param min The minimum of the interval
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   225
 * @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
   226
 * @see IsInsideBS()
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   227
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
   228
template <typename T>
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
   229
static FORCEINLINE 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
   230
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   231
	return (uint)(x - min) < (max - min);
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   232
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   233
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   234
/**
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   235
 * 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
   236
 * @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
   237
 * @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
   238
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
   239
template <typename T>
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9321
diff changeset
   240
static FORCEINLINE void Swap(T &a, T &b)
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   241
{
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   242
	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
   243
	a = b;
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   244
	b = t;
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   245
}
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8619
diff changeset
   246
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   247
#endif /* MATH_FUNC_HPP */