| 1 |
/* |
| 2 |
* mptBaseUtils.h |
| 3 |
* -------------- |
| 4 |
* Purpose: Various useful utility functions. |
| 5 |
* Notes : (currently none) |
| 6 |
* Authors: OpenMPT Devs |
| 7 |
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details. |
| 8 |
*/ |
| 9 |
|
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include "openmpt/all/BuildSettings.hpp" |
| 14 |
|
| 15 |
|
| 16 |
#include "mpt/base/algorithm.hpp" |
| 17 |
#include "mpt/base/arithmetic_shift.hpp" |
| 18 |
#include "mpt/base/array.hpp" |
| 19 |
#include "mpt/base/bit.hpp" |
| 20 |
#include "mpt/base/constexpr_throw.hpp" |
| 21 |
#include "mpt/base/detect_arch.hpp" |
| 22 |
#include "mpt/base/detect_compiler.hpp" |
| 23 |
#include "mpt/base/math.hpp" |
| 24 |
#include "mpt/base/memory.hpp" |
| 25 |
#include "mpt/base/numeric.hpp" |
| 26 |
#include "mpt/base/saturate_cast.hpp" |
| 27 |
#include "mpt/base/saturate_round.hpp" |
| 28 |
#include "mpt/base/utility.hpp" |
| 29 |
#include "mpt/base/wrapping_divide.hpp" |
| 30 |
|
| 31 |
#include "mptBaseMacros.h" |
| 32 |
#include "mptBaseTypes.h" |
| 33 |
|
| 34 |
#include <algorithm> |
| 35 |
#include <limits> |
| 36 |
#include <numeric> |
| 37 |
#include <utility> |
| 38 |
|
| 39 |
#include <cmath> |
| 40 |
#include <cstdlib> |
| 41 |
|
| 42 |
#include <math.h> |
| 43 |
#include <stdlib.h> |
| 44 |
|
| 45 |
#if MPT_COMPILER_MSVC |
| 46 |
#include <intrin.h> |
| 47 |
#endif |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
OPENMPT_NAMESPACE_BEGIN |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
template <typename T> |
| 56 |
inline void Clear(T& x) |
| 57 |
{ |
| 58 |
static_assert(!std::is_pointer<T>::value); |
| 59 |
mpt::reset(x); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
// Memset given object to zero. |
| 64 |
template <class T> |
| 65 |
inline void MemsetZero(T& a) |
| 66 |
{ |
| 67 |
static_assert(std::is_pointer<T>::value == false, "Won't memset pointers."); |
| 68 |
mpt::memclear(a); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
// Limits 'val' to given range. If 'val' is less than 'lowerLimit', 'val' is set to value 'lowerLimit'. |
| 74 |
// Similarly if 'val' is greater than 'upperLimit', 'val' is set to value 'upperLimit'. |
| 75 |
// If 'lowerLimit' > 'upperLimit', 'val' won't be modified. |
| 76 |
template<class T, class C> |
| 77 |
inline void Limit(T& val, const C lowerLimit, const C upperLimit) |
| 78 |
{ |
| 79 |
if(lowerLimit > upperLimit) return; |
| 80 |
if(val < lowerLimit) val = lowerLimit; |
| 81 |
else if(val > upperLimit) val = upperLimit; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
// Like Limit, but returns value |
| 86 |
template<class T, class C> |
| 87 |
inline T Clamp(T val, const C lowerLimit, const C upperLimit) |
| 88 |
{ |
| 89 |
if(val < lowerLimit) return lowerLimit; |
| 90 |
else if(val > upperLimit) return upperLimit; |
| 91 |
else return val; |
| 92 |
} |
| 93 |
|
| 94 |
// Like Limit, but with upperlimit only. |
| 95 |
template<class T, class C> |
| 96 |
inline void LimitMax(T& val, const C upperLimit) |
| 97 |
{ |
| 98 |
if(val > upperLimit) |
| 99 |
val = upperLimit; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
namespace Util |
| 105 |
{ |
| 106 |
|
| 107 |
// Returns maximum value of given integer type. |
| 108 |
template <class T> constexpr T MaxValueOfType(const T&) {static_assert(std::numeric_limits<T>::is_integer == true, "Only integer types are allowed."); return (std::numeric_limits<T>::max)();} |
| 109 |
|
| 110 |
} // namespace Util |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
namespace Util { |
| 115 |
|
| 116 |
// Multiply two 32-bit integers, receive 64-bit result. |
| 117 |
// MSVC generates unnecessarily complicated code for the unoptimized variant using _allmul. |
| 118 |
MPT_CONSTEXPR20_FUN int64 mul32to64(int32 a, int32 b) |
| 119 |
{ |
| 120 |
#if MPT_COMPILER_MSVC && (MPT_ARCH_X86 || MPT_ARCH_AMD64) |
| 121 |
MPT_MAYBE_CONSTANT_IF(MPT_IS_CONSTANT_EVALUATED20()) |
| 122 |
{ |
| 123 |
return static_cast<int64>(a) * b; |
| 124 |
} else |
| 125 |
{ |
| 126 |
return __emul(a, b); |
| 127 |
} |
| 128 |
#else |
| 129 |
return static_cast<int64>(a) * b; |
| 130 |
#endif |
| 131 |
} |
| 132 |
|
| 133 |
MPT_CONSTEXPR20_FUN uint64 mul32to64_unsigned(uint32 a, uint32 b) |
| 134 |
{ |
| 135 |
#if MPT_COMPILER_MSVC && (MPT_ARCH_X86 || MPT_ARCH_AMD64) |
| 136 |
MPT_MAYBE_CONSTANT_IF(MPT_IS_CONSTANT_EVALUATED20()) |
| 137 |
{ |
| 138 |
return static_cast<uint64>(a) * b; |
| 139 |
} else |
| 140 |
{ |
| 141 |
return __emulu(a, b); |
| 142 |
} |
| 143 |
#else |
| 144 |
return static_cast<uint64>(a) * b; |
| 145 |
#endif |
| 146 |
} |
| 147 |
|
| 148 |
MPT_CONSTEXPR20_FUN int32 muldiv(int32 a, int32 b, int32 c) |
| 149 |
{ |
| 150 |
return mpt::saturate_cast<int32>( mul32to64( a, b ) / c ); |
| 151 |
} |
| 152 |
|
| 153 |
MPT_CONSTEXPR20_FUN int32 muldivr(int32 a, int32 b, int32 c) |
| 154 |
{ |
| 155 |
return mpt::saturate_cast<int32>( ( mul32to64( a, b ) + ( c / 2 ) ) / c ); |
| 156 |
} |
| 157 |
|
| 158 |
// Do not use overloading because catching unsigned version by accident results in slower X86 code. |
| 159 |
MPT_CONSTEXPR20_FUN uint32 muldiv_unsigned(uint32 a, uint32 b, uint32 c) |
| 160 |
{ |
| 161 |
return mpt::saturate_cast<uint32>( mul32to64_unsigned( a, b ) / c ); |
| 162 |
} |
| 163 |
MPT_CONSTEXPR20_FUN uint32 muldivr_unsigned(uint32 a, uint32 b, uint32 c) |
| 164 |
{ |
| 165 |
return mpt::saturate_cast<uint32>( ( mul32to64_unsigned( a, b ) + ( c / 2u ) ) / c ); |
| 166 |
} |
| 167 |
|
| 168 |
constexpr MPT_FORCEINLINE int32 muldivrfloor(int64 a, uint32 b, uint32 c) |
| 169 |
{ |
| 170 |
a *= b; |
| 171 |
a += c / 2u; |
| 172 |
return (a >= 0) ? mpt::saturate_cast<int32>(a / c) : mpt::saturate_cast<int32>((a - (c - 1)) / c); |
| 173 |
} |
| 174 |
|
| 175 |
} // namespace Util |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
OPENMPT_NAMESPACE_END |