1#pragma once
2
3#include <basis/seadRawPrint.h>
4#include <basis/seadTypes.h>
5
6namespace sead
7{
8class PtrUtil
9{
10public:
11 // XXX: these probably do not match Nintendo's implementation
12
13 static void* roundUpPow2(const void* ptr, u32 n)
14 {
15 const uintptr_t result = uintptr_t(ptr) + (n - uintptr_t(ptr) % n) % n;
16 return reinterpret_cast<void*>(result);
17 }
18
19 static void* roundUpN(const void* ptr, u32 n)
20 {
21 const uintptr_t result = uintptr_t(ptr) + (n - uintptr_t(ptr) % n) % n;
22 return reinterpret_cast<void*>(result);
23 }
24
25 static void* roundDownPow2(const void* ptr, u32 n)
26 {
27 const uintptr_t result = uintptr_t(ptr) - uintptr_t(ptr) % n;
28 return reinterpret_cast<void*>(result);
29 }
30
31 static void* roundDownN(const void* ptr, u32 n)
32 {
33 const uintptr_t result = uintptr_t(ptr) - uintptr_t(ptr) % n;
34 return reinterpret_cast<void*>(result);
35 }
36
37 static void* addOffset(const void* ptr, intptr_t offset)
38 {
39 return reinterpret_cast<void*>(uintptr_t(ptr) + offset);
40 }
41
42 static intptr_t diff(const void* ptr1, const void* ptr2)
43 {
44 return intptr_t(ptr1) - intptr_t(ptr2);
45 }
46
47 static bool isInclude(const void* ptr, const void* begin, const void* end)
48 {
49 return uintptr_t(begin) <= uintptr_t(ptr) && uintptr_t(ptr) <= uintptr_t(end);
50 }
51
52 static bool isAligned(const void* ptr, s32 alignment)
53 {
54 SEAD_ASSERT(alignment != 0);
55 return uintptr_t(ptr) % alignment == 0;
56 }
57 static bool isAlignedPow2(const void* ptr, u32 n) { return (uintptr_t(ptr) & (n - 1)) == 0; }
58 static bool isAlignedN(const void* ptr, s32 n) { return uintptr_t(ptr) % n == 0; }
59};
60} // namespace sead
61