src/core/math_func.hpp
author rubidium
Fri, 21 Dec 2007 19:21:21 +0000
changeset 8609 8c0c3e9dd6a0
parent 8587 6db234b2b897
child 8619 c2434269c3eb
permissions -rw-r--r--
(svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
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
/**
8609
8c0c3e9dd6a0 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8587
diff changeset
    21
 * The largest value that can be entered in a variable
8c0c3e9dd6a0 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8587
diff changeset
    22
 * @param type the type of the variable
8c0c3e9dd6a0 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8587
diff changeset
    23
 */
8c0c3e9dd6a0 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8587
diff changeset
    24
#define MAX_UVALUE(type) ((type)~(type)0)
8c0c3e9dd6a0 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8587
diff changeset
    25
8c0c3e9dd6a0 (svn r11674) -Codechange: refactor some functions out of macros.h into more logical locations.
rubidium
parents: 8587
diff changeset
    26
/**
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    27
 * Returns the maximum of two values.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    28
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    29
 * 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
    30
 * 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
    31
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    32
 * @param a The first value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    33
 * @param b The second value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    34
 * @return The greater value or a if equals
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
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
    37
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    38
	return (a >= b) ? a : b;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    39
}
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
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    42
 * Returns the minimum of two values.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    43
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    44
 * 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
    45
 * 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
    46
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    47
 * @param a The first value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    48
 * @param b The second value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    49
 * @return The smaller value or b if equals
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
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
    52
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    53
	return (a < b) ? a : b;
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
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
 * Returns the minimum of two integer.
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
 * 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
    60
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    61
 * @param a The first integer
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    62
 * @param b The second integer
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    63
 * @return The smaller value
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
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
    66
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    67
	return (a < b) ? a : b;
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
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
 * Returns the minimum of two unsigned integers.
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
 * 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
    74
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    75
 * @param a The first unsigned integer
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    76
 * @param b The second unsigned integer
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    77
 * @return The smaller value
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
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
    80
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    81
	return (a < b) ? a : b;
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
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    84
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    85
 * Returns the absolute value of (scalar) variable.
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
 * @note assumes variable to be signed
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    88
 * @param a The value we want to unsign
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    89
 * @return The unsigned value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    90
 */
8450
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8433
diff changeset
    91
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
    92
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    93
	return (a < (T)0) ? -a : a;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    94
}
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
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    97
 * 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
    98
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    99
 * @note n must be a power of 2
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   100
 * @param x The min value
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   101
 * @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
   102
 * @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
   103
 */
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   104
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
   105
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   106
	n--;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   107
	return (T)((x + n) & ~(n));
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
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
 * Clamp an integer between an interval.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   112
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   113
 * 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
   114
 * 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
   115
 * 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
   116
 * which side of the interval was 'left'.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   117
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   118
 * @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
   119
 *       unexpected results.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   120
 * @param a The value to clamp/truncate.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   121
 * @param min The minimum of the interval.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   122
 * @param max the maximum of the interval.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   123
 * @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
   124
 * @see ClampU(uint, uint, uint)
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
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
   127
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   128
	if (a <= min) return min;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   129
	if (a >= max) return max;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   130
	return a;
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
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
 * Clamp an unsigned integer between an interval.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   135
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   136
 * 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
   137
 * 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
   138
 * 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
   139
 * which side of the interval was 'left'.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   140
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   141
 * @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
   142
 *       unexpected results.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   143
 * @param a The value to clamp/truncate.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   144
 * @param min The minimum of the interval.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   145
 * @param max the maximum of the interval.
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   146
 * @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
   147
 * @see Clamp(int, int, int)
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
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
   150
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   151
	if (a <= min) return min;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   152
	if (a >= max) return max;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   153
	return a;
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
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
 * 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
   158
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   159
 * 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
   160
 * 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
   161
 * 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
   162
 * 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
   163
 * 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
   164
 * 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
   165
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   166
 * @param a The 64-bit value to clamps
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   167
 * @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
   168
 * @see Clamp(int, int, int)
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
static inline int32 ClampToI32(const int64 a)
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   171
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   172
	if (a <= (int32)0x80000000) return 0x80000000;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   173
	if (a >= (int32)0x7FFFFFFF) return 0x7FFFFFFF;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   174
	return (int32)a;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   175
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   176
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   177
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   178
 * Returns the (absolute) difference between two (scalar) variables
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   179
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   180
 * @param a The first scalar
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   181
 * @param b The second scalar
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   182
 * @return The absolute difference between the given scalars
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   183
 */
8466
9ce95e16f9f9 (svn r11526) -Codechange: Rename the function delta fitting to the naming style
skidd13
parents: 8450
diff changeset
   184
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
   185
	return (a < b) ? b - a : a - b;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   186
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   187
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   188
/**
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   189
 * 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
   190
 *
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   191
 * 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
   192
 * 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
   193
 * base+size this returns false.
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
 * @param x The value to check
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   196
 * @param base The base value of the interval
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   197
 * @param size The size of the interval
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   198
 * @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
   199
 */
8450
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8433
diff changeset
   200
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
   201
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   202
	return (uint)(x - base) < size;
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   203
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   204
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
 * 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
   207
 *
8450
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8433
diff changeset
   208
 * 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
   209
 *
8450
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8433
diff changeset
   210
 * @param a The value to check
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   211
 * @param min The minimum of the interval
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   212
 * @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
   213
 * @see IsInsideBS()
8433
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   214
 */
8450
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8433
diff changeset
   215
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
   216
{
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   217
	return (uint)(x - min) < (max - min);
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   218
}
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   219
9bbc38806eeb (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   220
#endif /* MATH_FUNC_HPP */