A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
Memory.hh
1#pragma once
2
3#include <Logger.hh>
4
5#include <cstdint>
6
7namespace Kinoko {
8
9// We can't include Common.hh, because we have a cyclic dependency
10// Instead, we redefine the types for use in memory code
11typedef int8_t s8;
12typedef int16_t s16;
13typedef int32_t s32;
14typedef int64_t s64;
15
16typedef uint8_t u8;
17typedef uint16_t u16;
18typedef uint32_t u32;
19typedef uint64_t u64;
20
21typedef float f32;
22typedef double f64;
23
24static inline uintptr_t GetAddrNum(const void *p) {
25 return reinterpret_cast<uintptr_t>(p);
26}
27
28static inline void *GetAddrPtr(uintptr_t n) {
29 return reinterpret_cast<void *>(n);
30}
31
32static inline void *AddOffset(const void *p, size_t offset) {
33 return GetAddrPtr(GetAddrNum(p) + offset);
34}
35
36static inline void *SubOffset(const void *p, size_t offset) {
37 return GetAddrPtr(GetAddrNum(p) - offset);
38}
39
40static inline uintptr_t RoundUp(uintptr_t value, uintptr_t alignment) {
41 return (value + alignment - 1) & ~(alignment - 1);
42}
43
44static inline void *RoundUp(void *ptr, uintptr_t alignment) {
45 return GetAddrPtr(RoundUp(GetAddrNum(ptr), alignment));
46}
47
48static inline uintptr_t RoundDown(uintptr_t value, uintptr_t alignment) {
49 return value & ~(alignment - 1);
50}
51
52static inline void *RoundDown(void *ptr, uintptr_t alignment) {
53 return GetAddrPtr(RoundDown(GetAddrNum(ptr), alignment));
54}
55
56} // namespace Kinoko