Mario Kart 64
Loading...
Searching...
No Matches
macros.h
Go to the documentation of this file.
1#ifndef _MACROS_H_
2#define _MACROS_H_
3
4#include <math.h>
5#include <libultraship.h>
6
7#ifndef __sgi
8#define GLOBAL_ASM(...)
9#endif
10
11#if !defined(__sgi) && (!defined(NON_MATCHING) || !defined(AVOID_UB))
12// asm-process isn't supported outside of IDO, and undefined behavior causes
13// crashes.
14// #error Matching build is only possible on IDO; please build with NON_MATCHING=1.
15#endif
16
17#define ARRAY_COUNT(arr) (s32)(sizeof(arr) / sizeof(arr[0]))
18
19#define GLUE(a, b) a##b
20#define GLUE2(a, b) GLUE(a, b)
21
22// Avoid compiler warnings for unused variables
23#ifdef __GNUC__
24#define UNUSED __attribute__((unused))
25#else
26#define UNUSED
27#endif
28
29// Avoid undefined behaviour for non-returning functions
30#ifdef __GNUC__
31#define NORETURN __attribute__((noreturn))
32#else
33#define NORETURN
34#endif
35
36// Avoid undefined behaviour for non-returning functions
37#ifdef __GNUC__
38#define NO_REORDER __attribute__((no_reorder))
39#else
40#define NO_REORDER
41#endif
42
43// Static assertions
44#ifdef __GNUC__
45#define STATIC_ASSERT(cond, msg) _Static_assert(cond, msg)
46#else
47#define STATIC_ASSERT(cond, msg) typedef char GLUE2(static_assertion_failed, __LINE__)[(cond) ? 1 : -1]
48#endif
49
50// Align to 8-byte boundary for DMA requirements
51#ifdef __GNUC__
52#define ALIGNED8
53#else
54#define ALIGNED8
55#endif
56
57// Align to 16-byte boundary for audio lib requirements
58#ifdef __GNUC__
59#define ALIGNED16
60#else
61#define ALIGNED16
62#endif
63
64// Fixed point macros
65#define FTOFIX(f) ((s32) ((f) * 65536.0))
66#define ITOFIX(i) ((s32) ((i) << 16))
67#define FIXTOF(x) ((double) ((x) / 65536.0))
68#define FIXTOI(x) ((s32) ((x) >> 16))
69
70// Split fixed-point values into its integer or fractional parts.
71#define toFixedInt(f) (FTOFIX(f) >> 16)
72#define toFrac(f) (FTOFIX(f) & 0xFFFF)
73
74// Setup a fixed-point matrix using floats or doubles. Recommend using doubles for more precision.
75#define toFixedPointMatrix(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16) \
76 { \
77 { ((toFixedInt(x1)) << 16) | toFixedInt(x2), ((toFixedInt(x3)) << 16) | toFixedInt(x4), \
78 (toFixedInt(x5) << 16) | toFixedInt(x6), (toFixedInt(x7) << 16) | toFixedInt(x8) }, \
79 { ((toFixedInt(x9)) << 16) | toFixedInt(x10), ((toFixedInt(x11)) << 16) | toFixedInt(x12), \
80 (toFixedInt(x13) << 16) | toFixedInt(x14), (toFixedInt(x15) << 16) | toFixedInt(x16) }, \
81 { ((toFrac(x1)) << 16) | toFrac(x2), ((toFrac(x3)) << 16) | toFrac(x4), (toFrac(x5) << 16) | toFrac(x6), \
82 (toFrac(x7) << 16) | toFrac(x8) }, \
83 { \
84 ((toFrac(x9)) << 16) | toFrac(x10), ((toFrac(x11)) << 16) | toFrac(x12), \
85 (toFrac(x13) << 16) | toFrac(x14), (toFrac(x15) << 16) | toFrac(x16) \
86 } \
87 }
88
89// convert a virtual address to physical.
90#ifndef TARGET_N64
91#define VIRTUAL_TO_PHYSICAL(addr) (addr)
92
93// convert a physical address to virtual.
94#define PHYSICAL_TO_VIRTUAL(addr) (addr)
95
96// another way of converting virtual to physical
97#define VIRTUAL_TO_PHYSICAL2(addr) (addr)
98#else
99#define VIRTUAL_TO_PHYSICAL(addr) ((uintptr_t) (addr) & 0x1FFFFFFF)
100
101// convert a physical address to virtual.
102#define PHYSICAL_TO_VIRTUAL(addr) ((uintptr_t) (addr) | 0x80000000)
103
104// another way of converting virtual to physical
105#define VIRTUAL_TO_PHYSICAL2(addr) ((u8*) (addr) - 0x80000000U)
106#endif
107
108// aligns an address to the next 16 bytes
109#define ALIGN16(val) (((val) + 0xF) & ~0xF)
110
111// Envelopes are always stored as big endian, to match sequence files which are
112// byte blobs and can embed envelopes. Hence this byteswapping macro.
113#ifndef BSWAP16
114#if IS_BIG_ENDIAN
115#define BSWAP16(x) (x)
116#else
117#define BSWAP16(x) (((x) & 0xff) << 8 | (((x) >> 8) & 0xff))
118#endif
119#endif
120
127#define GET_PACKED_END(dl) (((u8*) dl) + sizeof(dl) - sizeof(dl[0]) - 0x07000000)
128
129#ifndef MAX
130#define MAX(a, b) ((a) > (b) ? (a) : (b))
131#endif
132
133#ifndef MIN
134#define MIN(a, b) ((a) < (b) ? (a) : (b))
135#endif
136
137#ifndef CLAMP
138#define CLAMP(var, min, max) ((var) < (min) ? min : (var) > (max) ? max : var)
139#endif
140
141#endif