src/core/math_func.hpp
author Tero Marttila <terom@fixme.fi>
Fri, 19 Dec 2008 02:13:39 +0200
changeset 10440 0a91ea45b0e8
parent 9597 825e5483799b
permissions -rw-r--r--
adjust the random land gen a bit to work with mini-maps
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
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
    30
template <typename T>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
    31
static FORCEINLINE T max(const T a, const T b)
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    32
{
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    33
	return (a >= b) ? a : b;
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
/**
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    37
 * Returns the minimum of two values.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    38
 *
1e7a22ea20d4 (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.
1e7a22ea20d4 (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.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    41
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    42
 * @param a The first value
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    43
 * @param b The second value
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    44
 * @return The smaller value or b if equals
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    45
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
    46
template <typename T>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
    47
static FORCEINLINE T min(const T a, const T b)
7937
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
	return (a < b) ? a : b;
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
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
 * Returns the minimum of two integer.
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
 * 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
    56
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    57
 * @param a The first integer
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    58
 * @param b The second integer
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    59
 * @return The smaller value
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    60
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
    61
static FORCEINLINE int min(const int a, const int b)
7937
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
	return (a < b) ? a : b;
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
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
 * Returns the minimum of two 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
 * 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
    70
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    71
 * @param a The first unsigned integer
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    72
 * @param b The second unsigned integer
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    73
 * @return The smaller value
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    74
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
    75
static FORCEINLINE uint minu(const uint a, const uint b)
7937
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
	return (a < b) ? a : b;
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
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
 * Returns the absolute value of (scalar) variable.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    82
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    83
 * @note assumes variable to be signed
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    84
 * @param a The value we want to unsign
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    85
 * @return The unsigned value
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    86
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
    87
template <typename T>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
    88
static FORCEINLINE T abs(const T a)
7937
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
	return (a < (T)0) ? -a : a;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    91
}
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
/**
1e7a22ea20d4 (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
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    95
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    96
 * @note n must be a power of 2
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
    97
 * @param x The min value
1e7a22ea20d4 (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
1e7a22ea20d4 (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
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   100
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
   101
template <typename T>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
   102
static FORCEINLINE T Align(const T x, uint n)
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   103
{
9597
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   104
	assert((n & (n - 1)) == 0 && n != 0);
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   105
	n--;
9597
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   106
	return (T)((x + n) & ~((T)n));
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   107
}
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   108
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   109
/**
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   110
 * Return the smallest multiple of n equal or greater than x
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   111
 * Applies to pointers only
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   112
 *
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   113
 * @note n must be a power of 2
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   114
 * @param x The min value
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   115
 * @param n The base of the number we are searching
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   116
 * @return The smallest multiple of n equal or greater than x
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   117
 * @see Align()
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   118
 */
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   119
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   120
assert_compile(sizeof(size_t) == sizeof(void *));
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   121
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   122
template <typename T>
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   123
static FORCEINLINE T *AlignPtr(T *x, uint n)
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   124
{
825e5483799b (svn r13639) -Codechange: rewrite 32bpp-anim and 32bpp-optimized drawing and encoding so it uses similiar scheme as 8bpp-optimized
smatz
parents: 9576
diff changeset
   125
	return (T *)Align((size_t)x, n);
7937
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
/**
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   129
 * Clamp an integer between an interval.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   130
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   131
 * 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
   132
 * 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
   133
 * 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
   134
 * which side of the interval was 'left'.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   135
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   136
 * @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
   137
 *       unexpected results.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   138
 * @param a The value to clamp/truncate.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   139
 * @param min The minimum of the interval.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   140
 * @param max the maximum of the interval.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   141
 * @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
   142
 * @see ClampU(uint, uint, uint)
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   143
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
   144
static FORCEINLINE int Clamp(const int a, const int min, const int max)
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   145
{
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   146
	if (a <= min) return min;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   147
	if (a >= max) return max;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   148
	return a;
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
/**
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   152
 * Clamp an unsigned integer between an interval.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   153
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   154
 * 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
   155
 * 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
   156
 * 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
   157
 * which side of the interval was 'left'.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   158
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   159
 * @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
   160
 *       unexpected results.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   161
 * @param a The value to clamp/truncate.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   162
 * @param min The minimum of the interval.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   163
 * @param max the maximum of the interval.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   164
 * @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
   165
 * @see Clamp(int, int, int)
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   166
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
   167
static FORCEINLINE uint ClampU(const uint a, const uint min, const uint max)
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   168
{
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   169
	if (a <= min) return min;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   170
	if (a >= max) return max;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   171
	return a;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   172
}
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
/**
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   175
 * 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
   176
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   177
 * 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
   178
 * 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
   179
 * 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
   180
 * 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
   181
 * 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
   182
 * 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
   183
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   184
 * @param a The 64-bit value to clamps
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   185
 * @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
   186
 * @see Clamp(int, int, int)
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   187
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
   188
static FORCEINLINE int32 ClampToI32(const int64 a)
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   189
{
8825
c5af79334951 (svn r12573) -Codechange: use defined constants instead of numbers in math_func.hpp
smatz
parents: 8824
diff changeset
   190
	if (a <= INT32_MIN) return INT32_MIN;
c5af79334951 (svn r12573) -Codechange: use defined constants instead of numbers in math_func.hpp
smatz
parents: 8824
diff changeset
   191
	if (a >= INT32_MAX) return INT32_MAX;
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   192
	return (int32)a;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   193
}
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   194
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   195
/**
8742
01a07b0abd98 (svn r12438) -Fix-ish: typo spotted by Biblo.
rubidium
parents: 8621
diff changeset
   196
 * Reduce an unsigned 64-bit int to an unsigned 16-bit one
8610
3d2183c29dc2 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8130
diff changeset
   197
 *
3d2183c29dc2 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8130
diff changeset
   198
 * @param a The 64-bit value to clamp
3d2183c29dc2 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8130
diff changeset
   199
 * @return The 64-bit value reduced to a 16-bit value
3d2183c29dc2 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8130
diff changeset
   200
 * @see ClampU(uint, uint, uint)
3d2183c29dc2 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8130
diff changeset
   201
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
   202
static FORCEINLINE uint16 ClampToU16(const uint64 a)
8610
3d2183c29dc2 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8130
diff changeset
   203
{
8825
c5af79334951 (svn r12573) -Codechange: use defined constants instead of numbers in math_func.hpp
smatz
parents: 8824
diff changeset
   204
	return (uint16)(a <= UINT16_MAX ? a : UINT16_MAX);
8610
3d2183c29dc2 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8130
diff changeset
   205
}
3d2183c29dc2 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8130
diff changeset
   206
3d2183c29dc2 (svn r12192) -Fix: clamp various town variables to 16bit prior to returning the value
smatz
parents: 8130
diff changeset
   207
/**
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   208
 * Returns the (absolute) difference between two (scalar) variables
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   209
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   210
 * @param a The first scalar
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   211
 * @param b The second scalar
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   212
 * @return The absolute difference between the given scalars
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   213
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
   214
template <typename T>
9576
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
   215
static FORCEINLINE T Delta(const T a, const T b)
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
   216
{
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   217
	return (a < b) ? b - a : a - b;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   218
}
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   219
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   220
/**
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   221
 * 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
   222
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   223
 * 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
   224
 * 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
   225
 * base+size this returns false.
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   226
 *
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   227
 * @param x The value to check
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   228
 * @param base The base value of the interval
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   229
 * @param size The size of the interval
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   230
 * @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
   231
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
   232
template <typename T>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
   233
static FORCEINLINE 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
   234
{
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   235
	return (uint)(x - base) < size;
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   236
}
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   237
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   238
/**
7954
57b51c69c072 (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 7937
diff changeset
   239
 * 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
   240
 *
7954
57b51c69c072 (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 7937
diff changeset
   241
 * 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
   242
 *
7954
57b51c69c072 (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 7937
diff changeset
   243
 * @param a The value to check
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   244
 * @param min The minimum of the interval
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   245
 * @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
   246
 * @see IsInsideBS()
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   247
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
   248
template <typename T>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
   249
static FORCEINLINE 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
   250
{
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   251
	return (uint)(x - min) < (max - min);
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   252
}
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   253
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   254
/**
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   255
 * 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
   256
 * @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
   257
 * @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
   258
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
   259
template <typename T>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 8825
diff changeset
   260
static FORCEINLINE void Swap(T &a, T &b)
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   261
{
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   262
	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
   263
	a = b;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   264
	b = t;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   265
}
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8123
diff changeset
   266
7937
1e7a22ea20d4 (svn r11490) -Codechange: Split the math functions to their own header
skidd13
parents:
diff changeset
   267
#endif /* MATH_FUNC_HPP */