NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
ut_algorithm.h
1#ifndef NW4R_UT_ALGORITHM_H
2#define NW4R_UT_ALGORITHM_H
3#include <nw4r/types_nw4r.h>
4
5namespace nw4r {
6namespace ut {
7namespace {
8
9/******************************************************************************
10 *
11 * Value operations
12 *
13 ******************************************************************************/
14template <typename T> inline T Max(T t1, T t2) {
15 return (t1 < t2) ? t2 : t1;
16}
17
18template <typename T> inline T Min(T t1, T t2) {
19 return (t1 > t2) ? t2 : t1;
20}
21
22template <typename T> inline T Clamp(T value, T min, T max) {
23 return value > max ? max : (value < min ? min : value);
24}
25
26template <typename T> inline T Abs(T x) {
27 // Static cast needed to break abs optimization
28 return x < 0 ? static_cast<T>(-x) : static_cast<T>(x);
29}
30
31template <> f32 inline Abs(register f32 x) {
32 register f32 ax;
33
34 // clang-format off
35 asm {
36 fabs ax, x
37 }
38 // clang-format on
39
40 return ax;
41}
42
43/******************************************************************************
44 *
45 * Bit operations
46 *
47 ******************************************************************************/
48template <typename T> inline T BitExtract(T bits, int pos, int len) {
49 T mask = (1 << len) - 1;
50 return (bits >> pos) & mask;
51}
52
53template <typename T> inline bool TestBit(T t, int pos) {
54 return BitExtract<T>(t, sizeof(T), pos);
55}
56
57/******************************************************************************
58 *
59 * Pointer arithmetic
60 *
61 ******************************************************************************/
62inline ulong GetIntPtr(const void* pPtr) {
63 return reinterpret_cast<ulong>(pPtr);
64}
65
66template <typename T>
67inline const void* AddOffsetToPtr(const void* pBase, T offset) {
68 return reinterpret_cast<const void*>(GetIntPtr(pBase) + offset);
69}
70template <typename T> inline void* AddOffsetToPtr(void* pBase, T offset) {
71 return reinterpret_cast<void*>(GetIntPtr(pBase) + offset);
72}
73
74inline s32 GetOffsetFromPtr(const void* pStart, const void* pEnd) {
75 return static_cast<s32>(GetIntPtr(pEnd) - GetIntPtr(pStart));
76}
77
78inline int ComparePtr(const void* pPtr1, const void* pPtr2) {
79 return static_cast<int>(GetIntPtr(pPtr1) - GetIntPtr(pPtr2));
80}
81
82/******************************************************************************
83 *
84 * Rounding
85 *
86 ******************************************************************************/
87template <typename T> inline T RoundUp(T t, unsigned int alignment) {
88 return (alignment + t - 1) & ~(alignment - 1);
89}
90
91template <typename T> inline void* RoundUp(T* pPtr, unsigned int alignment) {
92 ulong value = reinterpret_cast<ulong>(pPtr);
93 ulong rounded = (alignment + value - 1) & ~(alignment - 1);
94 return reinterpret_cast<void*>(rounded);
95}
96
97template <typename T> inline T RoundDown(T t, unsigned int alignment) {
98 return t & ~(alignment - 1);
99}
100
101template <typename T> inline void* RoundDown(T* pPtr, unsigned int alignment) {
102 ulong value = reinterpret_cast<ulong>(pPtr);
103 ulong rounded = value & ~(alignment - 1);
104 return reinterpret_cast<void*>(rounded);
105}
106
107} // namespace
108} // namespace ut
109} // namespace nw4r
110
111#endif
Debugging library which includes various utilities used by the rest of nw4r.
Definition ut_list.cpp:4