1
0
Fork 0
mirror of https://github.com/zeldaret/oot.git synced 2025-07-03 22:44:30 +00:00

[headers 9] Add src/libc64/ and new "z64" rand.h (#2164)

* rand.h -> libc64/qrand.h

* Add rand.h with z64 rand wrappers

* yeet comment

* code/rand.c -> libc64/qrand.c

* fixup

* move libc64 source to src/libc64/

* fix

* bss

* update file splits disasm metadata
This commit is contained in:
Dragorn421 2024-09-08 23:11:41 +02:00 committed by GitHub
parent a903f8b8bc
commit c7ec814d78
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 52 additions and 49 deletions

View file

@ -1,895 +0,0 @@
#include "global.h"
#include "fault.h"
#include "osMalloc.h"
#include "terminal.h"
#if PLATFORM_GC
#define FILL_ALLOC_BLOCK_FLAG (1 << 0)
#define FILL_FREE_BLOCK_FLAG (1 << 1)
#define CHECK_FREE_BLOCK_FLAG (1 << 2)
#define NODE_MAGIC 0x7373
#define BLOCK_UNINIT_MAGIC 0xAB
#define BLOCK_UNINIT_MAGIC_32 0xABABABAB
#define BLOCK_ALLOC_MAGIC 0xCD
#define BLOCK_ALLOC_MAGIC_32 0xCDCDCDCD
#define BLOCK_FREE_MAGIC 0xEF
#define BLOCK_FREE_MAGIC_32 0xEFEFEFEF
#define NODE_IS_VALID(node) (((node) != NULL) && ((node)->magic == NODE_MAGIC))
#if OOT_DEBUG
#define NODE_GET_NEXT(node) ArenaImpl_GetNextBlock(node)
#define NODE_GET_PREV(node) ArenaImpl_GetPrevBlock(node)
#define SET_DEBUG_INFO(node, file, line, arena) ArenaImpl_SetDebugInfo(node, file, line, arena)
#define FILL_UNINIT_BLOCK(arena, node, size) memset(node, BLOCK_UNINIT_MAGIC, size)
#define FILL_ALLOC_BLOCK(arena, alloc, size) \
if ((arena)->flag & FILL_ALLOC_BLOCK_FLAG) \
memset(alloc, BLOCK_ALLOC_MAGIC, size)
#define FILL_FREE_BLOCK_HEADER(arena, node) \
if ((arena)->flag & FILL_FREE_BLOCK_FLAG) \
memset(node, BLOCK_FREE_MAGIC, sizeof(ArenaNode))
#define FILL_FREE_BLOCK_CONTENTS(arena, node) \
if ((arena)->flag & FILL_FREE_BLOCK_FLAG) \
memset((void*)((u32)(node) + sizeof(ArenaNode)), BLOCK_FREE_MAGIC, (node)->size)
#define CHECK_FREE_BLOCK(arena, node) \
if ((arena)->flag & CHECK_FREE_BLOCK_FLAG) \
__osMalloc_FreeBlockTest(arena, node)
#define CHECK_ALLOC_FAILURE(arena, ptr) (void)0
#else
#define NODE_GET_NEXT(node) (NODE_IS_VALID((node)->next) ? (node)->next : NULL)
#define NODE_GET_PREV(node) (NODE_IS_VALID((node)->prev) ? (node)->prev : NULL)
#define SET_DEBUG_INFO(node, file, line, arena) (void)0
#define FILL_UNINIT_BLOCK(arena, node, size) (void)0
#define FILL_ALLOC_BLOCK(arena, alloc, size) (void)0
#define FILL_FREE_BLOCK_HEADER(arena, node) (void)0
#define FILL_FREE_BLOCK_CONTENTS(arena, node) (void)0
#define CHECK_FREE_BLOCK(arena, node) (void)0
// Number of allocation failures across all arenas.
u32 gTotalAllocFailures = 0; // "Arena_failcnt"
#define CHECK_ALLOC_FAILURE(arena, ptr) \
do { \
if ((ptr) == NULL) { \
gTotalAllocFailures++; \
(arena)->allocFailures++; \
} \
} while (0)
#endif
OSMesg sArenaLockMsg;
void __osMallocAddBlock(Arena* arena, void* start, s32 size);
#if OOT_DEBUG
u32 __osMalloc_FreeBlockTest_Enable;
u32 ArenaImpl_GetFillAllocBlock(Arena* arena) {
return (arena->flag & FILL_ALLOC_BLOCK_FLAG) != 0;
}
u32 ArenaImpl_GetFillFreeBlock(Arena* arena) {
return (arena->flag & FILL_FREE_BLOCK_FLAG) != 0;
}
u32 ArenaImpl_GetCheckFreeBlock(Arena* arena) {
return (arena->flag & CHECK_FREE_BLOCK_FLAG) != 0;
}
void ArenaImpl_SetFillAllocBlock(Arena* arena) {
arena->flag |= FILL_ALLOC_BLOCK_FLAG;
}
void ArenaImpl_SetFillFreeBlock(Arena* arena) {
arena->flag |= FILL_FREE_BLOCK_FLAG;
}
void ArenaImpl_SetCheckFreeBlock(Arena* arena) {
arena->flag |= CHECK_FREE_BLOCK_FLAG;
}
void ArenaImpl_UnsetFillAllocBlock(Arena* arena) {
arena->flag &= ~FILL_ALLOC_BLOCK_FLAG;
}
void ArenaImpl_UnsetFillFreeBlock(Arena* arena) {
arena->flag &= ~FILL_FREE_BLOCK_FLAG;
}
void ArenaImpl_UnsetCheckFreeBlock(Arena* arena) {
arena->flag &= ~CHECK_FREE_BLOCK_FLAG;
}
void ArenaImpl_SetDebugInfo(ArenaNode* node, const char* file, int line, Arena* arena) {
node->filename = file;
node->line = line;
node->threadId = osGetThreadId(NULL);
node->arena = arena;
node->time = osGetTime();
}
#endif
void ArenaImpl_LockInit(Arena* arena) {
osCreateMesgQueue(&arena->lockQueue, &sArenaLockMsg, 1);
}
void ArenaImpl_Lock(Arena* arena) {
osSendMesg(&arena->lockQueue, NULL, OS_MESG_BLOCK);
}
void ArenaImpl_Unlock(Arena* arena) {
osRecvMesg(&arena->lockQueue, NULL, OS_MESG_BLOCK);
}
#if OOT_DEBUG
ArenaNode* ArenaImpl_GetNextBlock(ArenaNode* node) {
ArenaNode* next = node->next;
if (next != NULL && (next == NULL || (next->magic != NODE_MAGIC))) {
osSyncPrintf(VT_COL(RED, WHITE) T("緊急事態!メモリリーク発見! (block=%08x)\n",
"Emergency! Memory leak detected! (block=%08x)\n") VT_RST,
next);
next = NULL;
node->next = NULL;
}
return next;
}
ArenaNode* ArenaImpl_GetPrevBlock(ArenaNode* node) {
ArenaNode* prev = node->prev;
if (prev != NULL && (prev == NULL || (prev->magic != NODE_MAGIC))) {
osSyncPrintf(VT_COL(RED, WHITE) T("緊急事態!メモリリーク発見! (block=%08x)\n",
"Emergency! Memory leak detected! (block=%08x)\n") VT_RST,
prev);
prev = NULL;
node->prev = NULL;
}
return prev;
}
#endif
ArenaNode* ArenaImpl_GetLastBlock(Arena* arena) {
ArenaNode* last = NULL;
ArenaNode* iter;
if (arena != NULL && NODE_IS_VALID(arena->head)) {
iter = arena->head;
while (iter != NULL) {
last = iter;
iter = NODE_GET_NEXT(last);
}
}
return last;
}
void __osMallocInit(Arena* arena, void* start, s32 size) {
bzero(arena, sizeof(Arena));
ArenaImpl_LockInit(arena);
__osMallocAddBlock(arena, start, size);
arena->isInit = true;
}
void __osMallocAddBlock(Arena* arena, void* start, s32 size) {
s32 diff;
s32 size2;
ArenaNode* firstNode;
ArenaNode* lastNode;
if (start != NULL) {
firstNode = (ArenaNode*)ALIGN16((u32)start);
diff = (s32)firstNode - (s32)start;
size2 = (size - diff) & ~0xF;
if (size2 > (s32)sizeof(ArenaNode)) {
FILL_UNINIT_BLOCK(arena, firstNode, size2);
firstNode->next = NULL;
firstNode->prev = NULL;
firstNode->size = size2 - sizeof(ArenaNode);
firstNode->isFree = true;
firstNode->magic = NODE_MAGIC;
ArenaImpl_Lock(arena);
lastNode = ArenaImpl_GetLastBlock(arena);
if (lastNode == NULL) {
arena->head = firstNode;
arena->start = start;
} else {
firstNode->prev = lastNode;
lastNode->next = firstNode;
}
ArenaImpl_Unlock(arena);
}
}
}
#if OOT_DEBUG
void ArenaImpl_RemoveAllBlocks(Arena* arena) {
ArenaNode* iter;
ArenaNode* next;
ArenaImpl_Lock(arena);
iter = arena->head;
while (iter != NULL) {
next = NODE_GET_NEXT(iter);
memset(iter, BLOCK_UNINIT_MAGIC, iter->size + sizeof(ArenaNode));
iter = next;
}
ArenaImpl_Unlock(arena);
}
#endif
void __osMallocCleanup(Arena* arena) {
#if OOT_DEBUG
ArenaImpl_RemoveAllBlocks(arena);
#endif
bzero(arena, sizeof(*arena));
}
s32 __osMallocIsInitialized(Arena* arena) {
return arena->isInit;
}
#if OOT_DEBUG
void __osMalloc_FreeBlockTest(Arena* arena, ArenaNode* node) {
ArenaNode* node2 = node;
u32* start;
u32* end;
u32* iter;
if (__osMalloc_FreeBlockTest_Enable) {
start = (u32*)((u32)node + sizeof(ArenaNode));
end = (u32*)((u32)start + node2->size);
iter = start;
while (iter < end) {
if (*iter != BLOCK_UNINIT_MAGIC_32 && *iter != BLOCK_FREE_MAGIC_32) {
osSyncPrintf(VT_COL(RED, WHITE)
T("緊急事態!メモリリーク検出! (block=%08x s=%08x e=%08x p=%08x)\n",
"Emergency! Memory leak detected! (block=%08x s=%08x e=%08x p=%08x)\n") VT_RST,
node, start, end, iter);
__osDisplayArena(arena);
return;
}
iter++;
}
}
}
void* __osMalloc_NoLockDebug(Arena* arena, u32 size, const char* file, int line) {
ArenaNode* iter;
u32 blockSize;
ArenaNode* newNode;
void* alloc = NULL;
ArenaNode* next;
size = ALIGN16(size);
blockSize = ALIGN16(size) + sizeof(ArenaNode);
iter = arena->head;
while (iter != NULL) {
if (iter->isFree && iter->size >= size) {
CHECK_FREE_BLOCK(arena, iter);
if (blockSize < iter->size) {
newNode = (ArenaNode*)((u32)iter + blockSize);
newNode->next = NODE_GET_NEXT(iter);
newNode->prev = iter;
newNode->size = iter->size - blockSize;
newNode->isFree = true;
newNode->magic = NODE_MAGIC;
iter->next = newNode;
iter->size = size;
next = NODE_GET_NEXT(newNode);
if (next) {
next->prev = newNode;
}
}
iter->isFree = false;
SET_DEBUG_INFO(iter, file, line, arena);
alloc = (void*)((u32)iter + sizeof(ArenaNode));
FILL_ALLOC_BLOCK(arena, alloc, size);
break;
}
iter = NODE_GET_NEXT(iter);
}
return alloc;
}
void* __osMallocDebug(Arena* arena, u32 size, const char* file, int line) {
void* alloc;
ArenaImpl_Lock(arena);
alloc = __osMalloc_NoLockDebug(arena, size, file, line);
ArenaImpl_Unlock(arena);
return alloc;
}
void* __osMallocRDebug(Arena* arena, u32 size, const char* file, int line) {
ArenaNode* iter;
ArenaNode* newNode;
u32 blockSize;
ArenaNode* next;
void* allocR = NULL;
size = ALIGN16(size);
ArenaImpl_Lock(arena);
iter = ArenaImpl_GetLastBlock(arena);
while (iter != NULL) {
if (iter->isFree && iter->size >= size) {
CHECK_FREE_BLOCK(arena, iter);
blockSize = ALIGN16(size) + sizeof(ArenaNode);
if (blockSize < iter->size) {
newNode = (ArenaNode*)((u32)iter + (iter->size - size));
newNode->next = NODE_GET_NEXT(iter);
newNode->prev = iter;
newNode->size = size;
newNode->magic = NODE_MAGIC;
iter->next = newNode;
iter->size -= blockSize;
next = NODE_GET_NEXT(newNode);
if (next) {
next->prev = newNode;
}
iter = newNode;
}
iter->isFree = false;
SET_DEBUG_INFO(iter, file, line, arena);
allocR = (void*)((u32)iter + sizeof(ArenaNode));
FILL_ALLOC_BLOCK(arena, allocR, size);
break;
}
iter = NODE_GET_PREV(iter);
}
ArenaImpl_Unlock(arena);
return allocR;
}
#endif
void* __osMalloc_NoLock(Arena* arena, u32 size) {
ArenaNode* iter;
u32 blockSize;
ArenaNode* newNode;
void* alloc = NULL;
ArenaNode* next;
size = ALIGN16(size);
blockSize = ALIGN16(size) + sizeof(ArenaNode);
iter = arena->head;
while (iter != NULL) {
if (iter->isFree && iter->size >= size) {
CHECK_FREE_BLOCK(arena, iter);
if (blockSize < iter->size) {
newNode = (ArenaNode*)((u32)iter + blockSize);
newNode->next = NODE_GET_NEXT(iter);
newNode->prev = iter;
newNode->size = iter->size - blockSize;
newNode->isFree = true;
newNode->magic = NODE_MAGIC;
iter->next = newNode;
iter->size = size;
next = NODE_GET_NEXT(newNode);
if (next) {
next->prev = newNode;
}
}
iter->isFree = false;
SET_DEBUG_INFO(iter, NULL, 0, arena);
alloc = (void*)((u32)iter + sizeof(ArenaNode));
FILL_ALLOC_BLOCK(arena, alloc, size);
break;
}
iter = NODE_GET_NEXT(iter);
}
CHECK_ALLOC_FAILURE(arena, alloc);
return alloc;
}
void* __osMalloc(Arena* arena, u32 size) {
void* alloc;
ArenaImpl_Lock(arena);
alloc = __osMalloc_NoLock(arena, size);
ArenaImpl_Unlock(arena);
return alloc;
}
void* __osMallocR(Arena* arena, u32 size) {
ArenaNode* iter;
ArenaNode* allocNode;
ArenaNode* newNode;
ArenaNode* next;
void* alloc = NULL;
u32 blockSize;
size = ALIGN16(size);
blockSize = ALIGN16(size) + sizeof(ArenaNode);
ArenaImpl_Lock(arena);
iter = ArenaImpl_GetLastBlock(arena);
while (iter != NULL) {
if (iter->isFree && iter->size >= size) {
CHECK_FREE_BLOCK(arena, iter);
if (blockSize < iter->size) {
allocNode = (ArenaNode*)((u32)iter + (iter->size - size));
allocNode->next = NODE_GET_NEXT(iter);
newNode = allocNode;
newNode->prev = iter;
newNode->size = size;
newNode->magic = NODE_MAGIC;
iter->next = newNode;
iter->size -= blockSize;
next = NODE_GET_NEXT(newNode);
if (next) {
next->prev = newNode;
}
iter = newNode;
}
iter->isFree = false;
SET_DEBUG_INFO(iter, NULL, 0, arena);
alloc = (void*)((u32)iter + sizeof(ArenaNode));
FILL_ALLOC_BLOCK(arena, alloc, size);
break;
}
iter = NODE_GET_PREV(iter);
}
CHECK_ALLOC_FAILURE(arena, alloc);
ArenaImpl_Unlock(arena);
return alloc;
}
void __osFree_NoLock(Arena* arena, void* ptr) {
ArenaNode* node;
ArenaNode* next;
ArenaNode* prev;
if (ptr == NULL) {
return;
}
node = (ArenaNode*)((u32)ptr - sizeof(ArenaNode));
if (!NODE_IS_VALID(node)) {
PRINTF(VT_COL(RED, WHITE) T("__osFree:不正解放(%08x)\n", "__osFree: Unauthorized release (%08x)\n") VT_RST,
ptr);
return;
}
if (node->isFree) {
PRINTF(VT_COL(RED, WHITE) T("__osFree:二重解放(%08x)\n", "__osFree: Double release (%08x)\n") VT_RST, ptr);
return;
}
#if OOT_DEBUG
if (arena != node->arena && arena != NULL) {
PRINTF(VT_COL(RED, WHITE)
T("__osFree:確保時と違う方法で解放しようとした (%08x:%08x)\n",
"__osFree:Tried to release in a different way than when it was secured (%08x:%08x)\n") VT_RST,
arena, node->arena);
return;
}
#endif
next = NODE_GET_NEXT(node);
prev = NODE_GET_PREV(node);
node->isFree = true;
SET_DEBUG_INFO(node, NULL, 0, arena);
FILL_FREE_BLOCK_CONTENTS(arena, node);
if ((u32)next == (u32)node + sizeof(ArenaNode) + node->size && next->isFree) {
ArenaNode* newNext = NODE_GET_NEXT(next);
if (newNext != NULL) {
newNext->prev = node;
}
node->size += next->size + sizeof(ArenaNode);
FILL_FREE_BLOCK_HEADER(arena, next);
node->next = newNext;
next = newNext;
}
if (prev != NULL && prev->isFree && (u32)node == (u32)prev + sizeof(ArenaNode) + prev->size) {
if (next) {
next->prev = prev;
}
prev->next = next;
prev->size += node->size + sizeof(ArenaNode);
FILL_FREE_BLOCK_HEADER(arena, node);
}
}
void __osFree(Arena* arena, void* ptr) {
ArenaImpl_Lock(arena);
__osFree_NoLock(arena, ptr);
ArenaImpl_Unlock(arena);
}
#if OOT_DEBUG
void __osFree_NoLockDebug(Arena* arena, void* ptr, const char* file, int line) {
ArenaNode* node;
ArenaNode* next;
ArenaNode* prev;
ArenaNode* newNext;
if (ptr == NULL) {
return;
}
node = (ArenaNode*)((u32)ptr - sizeof(ArenaNode));
if (!NODE_IS_VALID(node)) {
PRINTF(VT_COL(RED, WHITE)
T("__osFree:不正解放(%08x) [%s:%d ]\n", "__osFree: Unauthorized release (%08x) [%s:%d ]\n") VT_RST,
ptr, file, line);
return;
}
if (node->isFree) {
PRINTF(VT_COL(RED, WHITE) T("__osFree:二重解放(%08x) [%s:%d ]\n", "__osFree: Double release (%08x) [%s:%d ]\n")
VT_RST,
ptr, file, line);
return;
}
if (arena != node->arena && arena != NULL) {
PRINTF(VT_COL(RED, WHITE)
T("__osFree:確保時と違う方法で解放しようとした (%08x:%08x)\n",
"__osFree:Tried to release in a different way than when it was secured (%08x:%08x)\n") VT_RST,
arena, node->arena);
return;
}
next = NODE_GET_NEXT(node);
prev = NODE_GET_PREV(node);
node->isFree = true;
SET_DEBUG_INFO(node, file, line, arena);
FILL_FREE_BLOCK_CONTENTS(arena, node);
if ((u32)next == (u32)node + sizeof(ArenaNode) + node->size && next->isFree) {
newNext = NODE_GET_NEXT(next);
if (newNext != NULL) {
newNext->prev = node;
}
node->size += next->size + sizeof(ArenaNode);
FILL_FREE_BLOCK_HEADER(arena, next);
node->next = newNext;
next = newNext;
}
if (prev != NULL && prev->isFree && (u32)node == (u32)prev + sizeof(ArenaNode) + prev->size) {
if (next != NULL) {
next->prev = prev;
}
prev->next = next;
prev->size += node->size + sizeof(ArenaNode);
FILL_FREE_BLOCK_HEADER(arena, node);
}
}
void __osFreeDebug(Arena* arena, void* ptr, const char* file, int line) {
ArenaImpl_Lock(arena);
__osFree_NoLockDebug(arena, ptr, file, line);
ArenaImpl_Unlock(arena);
}
#endif
void* __osRealloc(Arena* arena, void* ptr, u32 newSize) {
ArenaNode* node;
void* newAlloc;
ArenaNode* next;
ArenaNode* newNext;
ArenaNode* overNext;
ArenaNode* newNext2;
ArenaNode* next2;
u32 sizeDiff;
ArenaNode* overNext2;
ArenaNode localCopy;
u32 blockSize;
s32 pad;
newSize = ALIGN16(newSize);
osSyncPrintf("__osRealloc(%08x, %d)\n", ptr, newSize);
ArenaImpl_Lock(arena);
if (ptr == NULL) {
ptr = __osMalloc_NoLock(arena, newSize);
} else if (newSize == 0) {
__osFree_NoLock(arena, ptr);
ptr = NULL;
} else {
node = (ArenaNode*)((u32)ptr - sizeof(ArenaNode));
if (newSize == node->size) {
osSyncPrintf(T("メモリブロックサイズが変わらないためなにもしません\n",
"Does nothing because the memory block size does not change\n"));
} else if (node->size < newSize) {
next = NODE_GET_NEXT(node);
sizeDiff = newSize - node->size;
if ((u32)next == ((u32)node + node->size + sizeof(ArenaNode)) && next->isFree && next->size >= sizeDiff) {
osSyncPrintf(T("現メモリブロックの後ろにフリーブロックがあるので結合します\n",
"Merge because there is a free block after the current memory block\n"));
next->size -= sizeDiff;
overNext = NODE_GET_NEXT(next);
newNext = (ArenaNode*)((u32)next + sizeDiff);
if (overNext != NULL) {
overNext->prev = newNext;
}
node->next = newNext;
node->size = newSize;
memmove(node->next, next, sizeof(ArenaNode));
} else {
osSyncPrintf(T("新たにメモリブロックを確保して内容を移動します\n",
"Allocate a new memory block and move the contents\n"));
newAlloc = __osMalloc_NoLock(arena, newSize);
if (newAlloc != NULL) {
bcopy(ptr, newAlloc, node->size);
__osFree_NoLock(arena, ptr);
}
ptr = newAlloc;
}
} else if (newSize < node->size) {
next2 = NODE_GET_NEXT(node);
if (next2 != NULL && next2->isFree) {
blockSize = ALIGN16(newSize) + sizeof(ArenaNode);
osSyncPrintf(T("現メモリブロックの後ろのフリーブロックを大きくしました\n",
"Increased free block behind current memory block\n"));
newNext2 = (ArenaNode*)((u32)node + blockSize);
localCopy = *next2;
*newNext2 = localCopy;
newNext2->size += node->size - newSize;
node->next = newNext2;
node->size = newSize;
overNext2 = NODE_GET_NEXT(newNext2);
if (overNext2 != NULL) {
overNext2->prev = newNext2;
}
} else if (newSize + sizeof(ArenaNode) < node->size) {
blockSize = ALIGN16(newSize) + sizeof(ArenaNode);
osSyncPrintf(T("現メモリブロックの後ろにフリーブロックがないので生成します\n",
"Generated because there is no free block after the current memory block\n"));
newNext2 = (ArenaNode*)((u32)node + blockSize);
newNext2->next = NODE_GET_NEXT(node);
newNext2->prev = node;
newNext2->size = node->size - blockSize;
newNext2->isFree = true;
newNext2->magic = NODE_MAGIC;
node->next = newNext2;
node->size = newSize;
overNext2 = NODE_GET_NEXT(newNext2);
if (overNext2 != NULL) {
overNext2->prev = newNext2;
}
} else {
osSyncPrintf(
T("フリーブロック生成するだけの空きがありません\n", "There is no room to generate free blocks\n"));
ptr = NULL;
}
}
CHECK_ALLOC_FAILURE(arena, ptr);
}
ArenaImpl_Unlock(arena);
return ptr;
}
#if OOT_DEBUG
void* __osReallocDebug(Arena* arena, void* ptr, u32 newSize, const char* file, int line) {
return __osRealloc(arena, ptr, newSize);
}
#endif
void ArenaImpl_GetSizes(Arena* arena, u32* outMaxFree, u32* outFree, u32* outAlloc) {
ArenaNode* iter;
ArenaImpl_Lock(arena);
*outMaxFree = 0;
*outFree = 0;
*outAlloc = 0;
iter = arena->head;
while (iter != NULL) {
if (iter->isFree) {
*outFree += iter->size;
if (*outMaxFree < iter->size) {
*outMaxFree = iter->size;
}
} else {
*outAlloc += iter->size;
}
iter = NODE_GET_NEXT(iter);
}
ArenaImpl_Unlock(arena);
}
#if OOT_DEBUG
void __osDisplayArena(Arena* arena) {
u32 freeSize;
u32 allocatedSize;
u32 maxFree;
ArenaNode* iter;
ArenaNode* next;
if (!__osMallocIsInitialized(arena)) {
osSyncPrintf(T("アリーナは初期化されていません\n", "Arena is not initialized\n"));
return;
}
ArenaImpl_Lock(arena);
maxFree = 0;
freeSize = 0;
allocatedSize = 0;
osSyncPrintf(T("アリーナの内容 (0x%08x)\n", "Arena contents (0x%08x)\n"), arena);
osSyncPrintf(T("メモリブロック範囲 status サイズ [時刻 s ms us ns: TID:src:行]\n",
"Memory node range status size [time s ms us ns: TID:src:line]\n"));
iter = arena->head;
while (iter != NULL) {
if (iter != NULL && iter->magic == NODE_MAGIC) {
next = iter->next;
osSyncPrintf("%08x-%08x%c %s %08x", iter, ((u32)iter + sizeof(ArenaNode) + iter->size),
(next == NULL) ? '$' : (iter != next->prev ? '!' : ' '),
iter->isFree ? T("空き", "Free") : T("確保", "Secure"), iter->size);
if (!iter->isFree) {
osSyncPrintf(" [%016llu:%2d:%s:%d]", OS_CYCLES_TO_NSEC(iter->time), iter->threadId,
iter->filename != NULL ? iter->filename : "**NULL**", iter->line);
}
osSyncPrintf("\n");
if (iter->isFree) {
freeSize += iter->size;
if (maxFree < iter->size) {
maxFree = iter->size;
}
} else {
allocatedSize += iter->size;
}
} else {
osSyncPrintf("%08x Block Invalid\n", iter);
next = NULL;
}
iter = next;
}
osSyncPrintf(T("確保ブロックサイズの合計 0x%08x バイト\n", "Total reserved node size 0x%08x bytes\n"),
allocatedSize);
osSyncPrintf(T("空きブロックサイズの合計 0x%08x バイト\n", "Total free node size 0x%08x bytes\n"), freeSize);
osSyncPrintf(T("最大空きブロックサイズ 0x%08x バイト\n", "Maximum free node size 0x%08x bytes\n"), maxFree);
ArenaImpl_Unlock(arena);
}
#endif
void ArenaImpl_FaultClient(Arena* arena) {
u32 freeSize;
u32 allocatedSize;
u32 maxFree;
ArenaNode* iter;
ArenaNode* next;
Fault_Printf("ARENA INFO (0x%08x)\n", arena);
if (!__osMallocIsInitialized(arena)) {
Fault_Printf("Arena is uninitalized\n", arena);
return;
}
maxFree = 0;
freeSize = 0;
allocatedSize = 0;
Fault_Printf("Memory Block Region status size\n");
iter = arena->head;
while (iter != NULL) {
if (iter != NULL && iter->magic == NODE_MAGIC) {
next = iter->next;
Fault_Printf("%08x-%08x%c %s %08x", iter, ((u32)iter + sizeof(ArenaNode) + iter->size),
(!next) ? '$' : (iter != next->prev ? '!' : ' '), iter->isFree ? "F" : "A", iter->size);
Fault_Printf("\n");
if (iter->isFree) {
freeSize += iter->size;
if (maxFree < iter->size) {
maxFree = iter->size;
}
} else {
allocatedSize += iter->size;
}
} else {
Fault_SetFontColor(0xF801);
Fault_Printf("%08x Block Invalid\n", iter);
next = NULL;
}
iter = next;
}
Fault_SetFontColor(0x7F1);
Fault_Printf("Total Alloc Block Size %08x\n", allocatedSize);
Fault_Printf("Total Free Block Size %08x\n", freeSize);
Fault_Printf("Largest Free Block Size %08x\n", maxFree);
}
s32 __osCheckArena(Arena* arena) {
ArenaNode* iter;
u32 error = 0;
ArenaImpl_Lock(arena);
osSyncPrintf(
T("アリーナの内容をチェックしています... (%08x)\n", "Checking the contents of the arena... (%08x)\n"),
arena);
iter = arena->head;
while (iter != NULL) {
//! @bug: Probably intended to be `!NODE_IS_VALID(iter)`
if (NODE_IS_VALID(iter)) {
#if OOT_DEBUG
osSyncPrintf(VT_COL(RED, WHITE) T("おおっと!! (%08x %08x)\n", "Oops!! (%08x %08x)\n") VT_RST, iter,
iter->magic);
#else
osSyncPrintf(T("おおっと!! (%08x %08x)\n", "Oops!! (%08x %08x)\n"), iter, iter->magic);
#endif
error = 1;
break;
}
iter = NODE_GET_NEXT(iter);
}
if (error == 0) {
osSyncPrintf(T("アリーナはまだ、いけそうです\n", "The arena is still going well\n"));
}
ArenaImpl_Unlock(arena);
return error;
}
#if OOT_DEBUG
u8 ArenaImpl_GetAllocFailures(Arena* arena) {
return arena->allocFailures;
}
#endif
#endif

View file

@ -1,474 +0,0 @@
#include "global.h"
#include "fault.h"
#include "osMalloc.h"
#if PLATFORM_N64
#define NODE_MAGIC 0x7373
#define NODE_IS_VALID(node) ((node)->magic == NODE_MAGIC)
#define NODE_GET_NEXT(node) ((node)->next)
#define NODE_GET_PREV(node) ((node)->prev)
#define SET_DEBUG_INFO(node, f, l, a) \
{ \
(node)->filename = (f); \
(node)->line = (l); \
(node)->threadId = osGetThreadId(NULL); \
(node)->arena = (a); \
(node)->time = osGetTime(); \
} \
(void)0
#define FILL_ALLOC_BLOCK(arena, alloc, size) (void)0
#define FILL_FREE_BLOCK_HEADER(arena, node) (void)0
#define FILL_FREE_BLOCK_CONTENTS(arena, node) (void)0
#define CHECK_FREE_BLOCK(arena, node) (void)0
// Number of allocation failures across all arenas.
u32 gTotalAllocFailures = 0; // "Arena_failcnt"
#define CHECK_ALLOC_FAILURE(arena, ptr) \
do { \
if ((ptr) == NULL) { \
gTotalAllocFailures++; \
(arena)->allocFailures++; \
} \
} while (0)
void __osMallocInit(Arena* arena, void* start, s32 size) {
ArenaNode* firstNode = (ArenaNode*)ALIGN16((u32)start);
size -= (u8*)firstNode - (u8*)start;
size &= ~0xF;
firstNode->next = NULL;
firstNode->prev = NULL;
firstNode->size = size - sizeof(ArenaNode);
firstNode->isFree = true;
firstNode->magic = NODE_MAGIC;
arena->head = firstNode;
arena->start = start;
arena->size = size;
}
void __osMallocCleanup(Arena* arena) {
bzero(arena, sizeof(*arena));
}
s32 __osMallocIsInitialized(Arena* arena) {
return arena->start != NULL;
}
void* __osMallocDebug(Arena* arena, u32 size, const char* file, int line) {
ArenaNode* iter;
u32 blockSize;
ArenaNode* newNode;
void* alloc = NULL;
ArenaNode* next;
size = ALIGN16(size);
blockSize = ALIGN16(size) + sizeof(ArenaNode);
iter = arena->head;
while (iter != NULL) {
if (iter->isFree && iter->size >= size) {
CHECK_FREE_BLOCK(arena, iter);
if (blockSize < iter->size) {
newNode = (ArenaNode*)((u32)iter + blockSize);
newNode->next = NODE_GET_NEXT(iter);
newNode->prev = iter;
newNode->size = iter->size - blockSize;
newNode->isFree = true;
newNode->magic = NODE_MAGIC;
iter->next = newNode;
iter->size = size;
next = NODE_GET_NEXT(newNode);
if (next) {
next->prev = newNode;
}
}
iter->isFree = false;
SET_DEBUG_INFO(iter, file, line, arena);
alloc = (void*)((u32)iter + sizeof(ArenaNode));
FILL_ALLOC_BLOCK(arena, alloc, size);
break;
}
iter = NODE_GET_NEXT(iter);
}
CHECK_ALLOC_FAILURE(arena, alloc);
return alloc;
}
void* __osMallocRDebug(Arena* arena, u32 size, const char* file, int line) {
ArenaNode* iter;
ArenaNode* newNode;
u32 blockSize;
u32 nodeSize;
ArenaNode* next;
void* allocR = NULL;
ArenaNode* next2;
size = ALIGN16(size);
iter = arena->head;
next2 = NODE_GET_NEXT(iter);
while (next2 != NULL) {
iter = next2;
next2 = NODE_GET_NEXT(next2);
}
while (iter != NULL) {
if (iter->isFree && iter->size >= size) {
CHECK_FREE_BLOCK(arena, iter);
blockSize = ALIGN16(size) + sizeof(ArenaNode);
nodeSize = iter->size;
if (blockSize < nodeSize) {
newNode = (ArenaNode*)((u32)iter + (iter->size - size));
newNode->next = NODE_GET_NEXT(iter);
newNode->prev = iter;
newNode->size = size;
newNode->magic = NODE_MAGIC;
iter->next = newNode;
iter->size -= blockSize;
next = NODE_GET_NEXT(newNode);
if (next) {
next->prev = newNode;
}
iter = newNode;
}
iter->isFree = false;
SET_DEBUG_INFO(iter, file, line, arena);
allocR = (void*)((u32)iter + sizeof(ArenaNode));
FILL_ALLOC_BLOCK(arena, allocR, size);
break;
}
iter = NODE_GET_PREV(iter);
}
CHECK_ALLOC_FAILURE(arena, allocR);
return allocR;
}
void* __osMalloc(Arena* arena, u32 size) {
ArenaNode* iter;
u32 blockSize;
ArenaNode* newNode;
void* alloc = NULL;
ArenaNode* next;
size = ALIGN16(size);
blockSize = ALIGN16(size) + sizeof(ArenaNode);
iter = arena->head;
while (iter != NULL) {
if (iter->isFree && iter->size >= size) {
CHECK_FREE_BLOCK(arena, iter);
if (blockSize < iter->size) {
newNode = (ArenaNode*)((u32)iter + blockSize);
newNode->next = NODE_GET_NEXT(iter);
newNode->prev = iter;
newNode->size = iter->size - blockSize;
newNode->isFree = true;
newNode->magic = NODE_MAGIC;
iter->next = newNode;
iter->size = size;
next = NODE_GET_NEXT(newNode);
if (next) {
next->prev = newNode;
}
}
iter->isFree = false;
SET_DEBUG_INFO(iter, NULL, 0, arena);
alloc = (void*)((u32)iter + sizeof(ArenaNode));
FILL_ALLOC_BLOCK(arena, alloc, size);
break;
}
iter = NODE_GET_NEXT(iter);
}
CHECK_ALLOC_FAILURE(arena, alloc);
return alloc;
}
void* __osMallocR(Arena* arena, u32 size) {
ArenaNode* iter;
ArenaNode* newNode;
u32 blockSize;
u32 nodeSize;
ArenaNode* next;
void* allocR = NULL;
ArenaNode* next2;
size = ALIGN16(size);
iter = arena->head;
next2 = NODE_GET_NEXT(iter);
while (next2 != NULL) {
iter = next2;
next2 = NODE_GET_NEXT(next2);
}
while (iter != NULL) {
if (iter->isFree && iter->size >= size) {
CHECK_FREE_BLOCK(arena, iter);
blockSize = ALIGN16(size) + sizeof(ArenaNode);
nodeSize = iter->size;
if (blockSize < nodeSize) {
newNode = (ArenaNode*)((u32)iter + (iter->size - size));
newNode->next = NODE_GET_NEXT(iter);
newNode->prev = iter;
newNode->size = size;
newNode->magic = NODE_MAGIC;
iter->next = newNode;
iter->size -= blockSize;
next = NODE_GET_NEXT(newNode);
if (next) {
next->prev = newNode;
}
iter = newNode;
}
iter->isFree = false;
SET_DEBUG_INFO(iter, NULL, 0, arena);
allocR = (void*)((u32)iter + sizeof(ArenaNode));
FILL_ALLOC_BLOCK(arena, allocR, size);
break;
}
iter = NODE_GET_PREV(iter);
}
CHECK_ALLOC_FAILURE(arena, allocR);
return allocR;
}
void __osFree(Arena* arena, void* ptr) {
ArenaNode* node;
ArenaNode* next;
ArenaNode* prev;
if (ptr == NULL) {
return;
}
node = (ArenaNode*)((u32)ptr - sizeof(ArenaNode));
if (!NODE_IS_VALID(node)) {
(void)T("__osFree:不正解放(%08x)\n", "__osFree: Unauthorized release (%08x)\n");
osSetIntMask(OS_IM_ALL);
return;
}
if (node->isFree) {
(void)T("__osFree:二重解放(%08x)\n", "__osFree: Double release (%08x)\n");
osSetIntMask(OS_IM_ALL);
return;
}
if (arena != node->arena && arena != NULL) {
(void)T("__osFree:arena(%08x)が__osMallocのarena(%08x)と一致しない\n",
"__osFree:arena(%08x) and __osMalloc:arena(%08x) do not match\n");
}
node->isFree = true;
SET_DEBUG_INFO(node, NULL, 0, arena);
if (node->next != NULL) {
next = node->next;
if (next->isFree) {
if (next->next != NULL) {
next->next->prev = node;
}
node->size += next->size + sizeof(ArenaNode);
node->next = next->next;
}
}
if (node->prev != NULL) {
prev = node->prev;
if (prev->isFree) {
prev->size += node->size + sizeof(ArenaNode);
prev->next = NODE_GET_NEXT(node);
if (node->next != NULL) {
node->next->prev = prev;
}
}
}
}
void __osFreeDebug(Arena* arena, void* ptr, const char* file, int line) {
ArenaNode* node;
ArenaNode* next;
ArenaNode* prev;
if (ptr == NULL) {
return;
}
node = (ArenaNode*)((u32)ptr - sizeof(ArenaNode));
if (!NODE_IS_VALID(node)) {
(void)T("__osFree:不正解放(%08x)\n", "__osFree: Unauthorized release (%08x)\n");
osSetIntMask(OS_IM_ALL);
return;
}
if (node->isFree) {
(void)T("__osFree:二重解放(%08x)\n", "__osFree: Double release (%08x)\n");
osSetIntMask(OS_IM_ALL);
return;
}
if (arena != node->arena && arena != NULL) {
(void)T("__osFree:arena(%08x)が__osMallocのarena(%08x)と一致しない\n",
"__osFree:arena(%08x) and __osMalloc:arena(%08x) do not match\n");
}
node->isFree = true;
SET_DEBUG_INFO(node, file, line, arena);
if (node->next != NULL) {
next = node->next;
if (next->isFree) {
if (next->next != NULL) {
next->next->prev = node;
}
node->size += next->size + sizeof(ArenaNode);
node->next = next->next;
}
}
if (node->prev != NULL) {
prev = node->prev;
if (prev->isFree) {
prev->size += node->size + sizeof(ArenaNode);
prev->next = NODE_GET_NEXT(node);
if (node->next != NULL) {
node->next->prev = prev;
}
}
}
}
void* __osRealloc(Arena* arena, void* ptr, u32 newSize) {
ArenaNode* node;
void* newAlloc;
ArenaNode* next;
ArenaNode* newNext;
u32 sizeDiff;
(void)"__osRealloc(%08x, %d)\n";
osSetIntMask(OS_IM_ALL);
if (ptr == NULL) {
ptr = __osMalloc(arena, newSize);
} else if (newSize == 0) {
__osFree(arena, ptr);
ptr = NULL;
} else {
newSize = ALIGN16(newSize);
node = (ArenaNode*)((u32)ptr - sizeof(ArenaNode));
if (newSize == node->size) {
// Do nothing
} else if (node->size < newSize) {
next = NODE_GET_NEXT(node);
sizeDiff = newSize - node->size;
if (next != NULL && next->isFree && next->size >= sizeDiff) {
next->size -= sizeDiff;
newNext = (ArenaNode*)((u32)next + sizeDiff);
if (NODE_GET_NEXT(next) != NULL) {
NODE_GET_NEXT(next)->prev = newNext;
}
node->next = newNext;
node->size = newSize;
memmove(node->next, next, sizeof(ArenaNode));
} else {
newAlloc = __osMalloc(arena, newSize);
if (newAlloc != NULL) {
memcpy(ptr, newAlloc, node->size);
__osFree(arena, ptr);
}
ptr = newAlloc;
}
} else if (newSize < node->size) {
(void)T("メモリブロックの縮小機能はまだインプリメントしていません\n",
"Memory block shrinking functionality is not yet implemented\n");
}
}
CHECK_ALLOC_FAILURE(arena, ptr);
return ptr;
}
void* __osReallocDebug(Arena* arena, void* ptr, u32 newSize, const char* file, int line) {
return __osRealloc(arena, ptr, newSize);
}
void ArenaImpl_GetSizes(Arena* arena, u32* outMaxFree, u32* outFree, u32* outAlloc) {
ArenaNode* iter;
*outMaxFree = 0;
*outFree = 0;
*outAlloc = 0;
iter = arena->head;
while (iter != NULL) {
if (iter->isFree) {
*outFree += iter->size;
if (*outMaxFree < iter->size) {
*outMaxFree = iter->size;
}
} else {
*outAlloc += iter->size;
}
iter = NODE_GET_NEXT(iter);
}
}
s32 __osCheckArena(Arena* arena) {
ArenaNode* iter;
(void)T("アリーナの内容をチェックしています... (%08x)\n", "Checking the arena contents... (%08x)\n");
iter = arena->head;
while (iter != NULL) {
if (!NODE_IS_VALID(iter)) {
(void)T("おおっと!! (%08x %08x)\n", "Oops!! (%08x %08x)\n");
return 1;
}
iter = NODE_GET_NEXT(iter);
}
(void)T("アリーナはまだ、いけそうです\n", "The arena is still going well\n");
return 0;
}
u8 ArenaImpl_GetAllocFailures(Arena* arena) {
return arena->allocFailures;
}
#endif

View file

@ -1,167 +0,0 @@
#include "ultra64/asm.h"
.set noreorder
.section .data
.balign 16
#if !PLATFORM_N64
DATA(qNaN0x3FFFFF)
.word 0x7FBFFFFF
ENDDATA(qNaN0x3FFFFF)
DATA(qNaN0x10000)
.word 0x7F810000
ENDDATA(qNaN0x10000)
DATA(sNaN0x3FFFFF)
.word 0x7FFFFFFF
ENDDATA(sNaN0x3FFFFF)
#endif
.section .text
.balign 16
LEAF(floorf)
floor.w.s $f12, $f12
jr $ra
cvt.s.w $f0, $f12
END(floorf)
LEAF(floor)
floor.w.d $f12, $f12
jr $ra
cvt.d.w $f0, $f12
END(floor)
LEAF(lfloorf)
floor.w.s $f4, $f12
mfc1 $v0, $f4
jr $ra
nop
END(lfloorf)
LEAF(lfloor)
floor.w.d $f4, $f12
mfc1 $v0, $f4
jr $ra
nop
END(lfloor)
LEAF(ceilf)
ceil.w.s $f12, $f12
jr $ra
cvt.s.w $f0, $f12
END(ceilf)
LEAF(ceil)
ceil.w.d $f12, $f12
jr $ra
cvt.d.w $f0, $f12
END(ceil)
LEAF(lceilf)
ceil.w.s $f4, $f12
mfc1 $v0, $f4
jr $ra
nop
END(lceilf)
LEAF(lceil)
ceil.w.d $f4, $f12
mfc1 $v0, $f4
jr $ra
nop
END(lceil)
LEAF(truncf)
trunc.w.s $f12, $f12
jr $ra
cvt.s.w $f0, $f12
END(truncf)
LEAF(trunc)
trunc.w.d $f12, $f12
jr $ra
cvt.d.w $f0, $f12
END(trunc)
LEAF(ltruncf)
trunc.w.s $f4, $f12
mfc1 $v0, $f4
jr $ra
nop
END(ltruncf)
LEAF(ltrunc)
trunc.w.d $f4, $f12
mfc1 $v0, $f4
jr $ra
nop
END(ltrunc)
LEAF(nearbyintf)
round.w.s $f12, $f12
jr $ra
cvt.s.w $f0, $f12
END(nearbyintf)
LEAF(nearbyint)
round.w.d $f12, $f12
jr $ra
cvt.d.w $f0, $f12
END(nearbyint)
LEAF(lnearbyintf)
round.w.s $f4, $f12
mfc1 $v0, $f4
jr $ra
nop
END(lnearbyintf)
LEAF(lnearbyint)
round.w.d $f4, $f12
mfc1 $v0, $f4
jr $ra
nop
END(lnearbyint)
LEAF(roundf)
li.s $f4, 0.5
nop
add.s $f0, $f12, $f4
floor.w.s $f0, $f0
jr $ra
cvt.s.w $f0, $f0
END(roundf)
LEAF(round)
li.d $f4, 0.5
nop
add.d $f0, $f12, $f4
floor.w.d $f0, $f0
jr $ra
cvt.d.w $f0, $f0
END(round)
LEAF(lroundf)
li.s $f4, 0.5
nop
add.s $f0, $f12, $f4
floor.w.s $f0, $f0
mfc1 $v0, $f0
jr $ra
nop
END(lroundf)
LEAF(lround)
li.d $f4, 0.5
nop
add.d $f0, $f12, $f4
floor.w.d $f0, $f0
mfc1 $v0, $f0
jr $ra
nop
END(lround)

View file

@ -1,248 +0,0 @@
#include "z64math.h"
#include "macros.h"
#if !PLATFORM_N64
s32 gUseAtanContFrac;
#endif
/**
* @param angle radians
* @return tan(angle)
*/
f32 Math_FTanF(f32 angle) {
f32 sin = sinf(angle);
f32 cos = cosf(angle);
return sin / cos;
}
f32 Math_FFloorF(f32 x) {
return floorf(x);
}
f32 Math_FCeilF(f32 x) {
return ceilf(x);
}
#if PLATFORM_N64
f64 Math_FAbs(f64 x) {
return x < 0.0 ? -x : x;
}
f32 Math_FAbsF(f32 x) {
return x < 0.0f ? -x : x;
}
#endif
f32 Math_FRoundF(f32 x) {
return roundf(x);
}
f32 Math_FTruncF(f32 x) {
return truncf(x);
}
f32 Math_FNearbyIntF(f32 x) {
return nearbyintf(x);
}
#if !PLATFORM_N64
/* Arctangent approximation using a Taylor series (one quadrant) */
f32 Math_FAtanTaylorQF(f32 x) {
static const f32 coeffs[] = {
-1.0f / 3, +1.0f / 5, -1.0f / 7, +1.0f / 9, -1.0f / 11, +1.0f / 13, -1.0f / 15, +1.0f / 17, 0.0f,
};
f32 poly = x;
f32 sq = SQ(x);
f32 exp = x * sq;
const f32* c = coeffs;
f32 term;
while (true) {
term = *c++ * exp;
if (poly + term == poly) {
break;
}
poly += term;
exp *= sq;
}
return poly;
}
/* Ditto for two quadrants */
f32 Math_FAtanTaylorF(f32 x) {
f32 t;
f32 q;
if (x > 0.0f) {
t = x;
} else if (x < 0.0f) {
t = -x;
} else if (x == 0.0f) {
return 0.0f;
} else {
return qNaN0x10000;
}
if (t <= M_SQRT2 - 1.0f) {
return Math_FAtanTaylorQF(x);
}
if (t >= M_SQRT2 + 1.0f) {
q = M_PI / 2 - Math_FAtanTaylorQF(1.0f / t);
} else {
q = M_PI / 4 - Math_FAtanTaylorQF((1.0f - t) / (1.0f + t));
}
if (x > 0.0f) {
return q;
} else {
return -q;
}
}
#endif
/* Arctangent approximation using a continued fraction */
f32 Math_FAtanContFracF(f32 x) {
s32 sector;
f32 z;
f32 conv;
f32 sq;
s32 i;
#if PLATFORM_N64
if (x > 1.0f) {
sector = 1;
x = 1.0f / x;
} else if (x < -1.0f) {
sector = -1;
x = 1.0f / x;
} else {
sector = 0;
}
#else
if (x >= -1.0f && x <= 1.0f) {
sector = 0;
} else if (x > 1.0f) {
sector = 1;
x = 1.0f / x;
} else if (x < -1.0f) {
sector = -1;
x = 1.0f / x;
} else {
return qNaN0x10000;
}
#endif
sq = SQ(x);
conv = 0.0f;
#if PLATFORM_N64
z = 24.0f;
i = 24;
#else
z = 8.0f;
i = 8;
#endif
while (i != 0) {
conv = SQ(z) * sq / (2.0f * z + 1.0f + conv);
z -= 1.0f;
i--;
}
#if PLATFORM_N64
if (sector > 0) {
return M_PI / 2 - (x / (1.0f + conv));
} else if (sector < 0) {
return -M_PI / 2 - (x / (1.0f + conv));
} else {
return x / (1.0f + conv);
}
#else
conv = x / (1.0f + conv);
if (sector == 0) {
return conv;
} else if (sector > 0) {
return M_PI / 2 - conv;
} else {
return -M_PI / 2 - conv;
}
#endif
}
#if !PLATFORM_N64
/**
* @return arctan(x) in radians, in (-pi/2,pi/2) range
*/
f32 Math_FAtanF(f32 x) {
if (!gUseAtanContFrac) {
return Math_FAtanTaylorF(x);
} else {
return Math_FAtanContFracF(x);
}
}
#endif
/**
* @return angle to (x,y) from vector (1,0) around (0,0) in radians, in (-pi,pi] range
*/
f32 Math_FAtan2F(f32 y, f32 x) {
#if PLATFORM_N64
if (y == 0.0f && x == 0.0f) {
return 0.0f;
}
if (x == 0.0f) {
if (y < 0.0f) {
return -M_PI / 2;
} else {
return M_PI / 2;
}
} else if (x < 0.0f) {
if (y < 0.0f) {
return -(M_PI - Math_FAtanContFracF(fabs(y / x)));
} else {
return M_PI - Math_FAtanContFracF(fabs(y / x));
}
} else { // x > 0.0f
return Math_FAtanContFracF(y / x);
}
#else
if (x == 0.0f) {
if (y == 0.0f) {
return 0.0f;
} else if (y > 0.0f) {
return M_PI / 2;
} else if (y < 0.0f) {
return -M_PI / 2;
} else {
return qNaN0x10000;
}
} else if (x >= 0.0f) {
return Math_FAtanF(y / x);
} else { // x < 0.0f
if (y < 0.0f) {
return Math_FAtanF(y / x) - M_PI;
} else {
return M_PI - Math_FAtanF(-(y / x));
}
}
#endif
}
/**
* @return arcsin(x) in radians, in [-pi/2,pi/2] range
*/
f32 Math_FAsinF(f32 x) {
return Math_FAtan2F(x, sqrtf(1.0f - SQ(x)));
}
/**
* @return arccos(x) in radians, in [0,pi] range
*/
f32 Math_FAcosF(f32 x) {
return M_PI / 2 - Math_FAsinF(x);
}

View file

@ -1,17 +0,0 @@
#include "global.h"
s32 PrintUtils_VPrintf(PrintCallback* pfn, const char* fmt, va_list args) {
return _Printf(*pfn, pfn, fmt, args);
}
s32 PrintUtils_Printf(PrintCallback* pfn, const char* fmt, ...) {
s32 ret;
va_list args;
va_start(args, fmt);
ret = PrintUtils_VPrintf(pfn, fmt, args);
va_end(args);
return ret;
}

View file

@ -1,197 +0,0 @@
/**
* @file rand.c
*
* This file implements the primary random number generator the game relies on. The generator is pseudo-random and
* implemented as a Linear Congruential Generator (LCG).
*
* A LCG computes random numbers sequentially via the relation
* X(n+1) = (a * X(n) + c) mod m
* where m is the modulus, a is the multiplier and c is the increment.
*
* These three parameters (a,c,m) completely specify the LCG and should be chosen such that
* - m > 0
* - 0 < a < m
* - 0 <= c < m
*
* The period of the LCG (a, c, m) is the smallest period p such that X(n + p) = X(n), past n=p the sequence will repeat
* itself in its outputs.
* A good LCG should have the maximum possible period, which will be equal to m as there are at most m possible values
* for X. This occurs when (Hull, T.E., & Dobell, A.R. (1962). Random Number Generators. Siam Review, 4, 230-254.):
* - m,c are relatively prime, that is the only integer that divides both m and c with no remainder is 1.
* - a - 1 is divisible by all prime factors of m.
* - a - 1 is divisible by 4 if m is divisible by 4.
*
* Ideally m is chosen to be a large power of 2 so that the modulo operation is inexpensive to compute. In this case the
* prime factors of m = 2^k are just k copies of 2. For k > 1 m is divisible by 4, so a - 1 must be divisible by 4. 2^k
* and c can easily be made relatively prime by making c an odd number.
* If we let k=32 to match the size of an integer, the modulo operation is made implicit by the width of the data type
* and becomes free to compute.
*
* The parameter a should be selected such that a-1 is divisible by 4 (and hence divisible by 2) and c should be any odd
* number. The precise values should fare well against the spectral test, a measure of "how random" a particular LCG is.
* A pair (a,c) that satisfies these requirements is (1664525, 1013904223), recommended by "Numerical Recipes in C: The
* Art of Scientific Computing" (p. 284).
*
* Therefore, the LCG with parameters (1664525, 1013904223, 2^32) that is implemented in this file has a maximal period
* of 2^32 and produces high-quality pseudo-random numbers.
*
* @note If sampling the LCG for a n-bit number it is important to use the upper n bits instead of the lower n bits of
* the LCG output. The lower n bits only have a period of 2^n which may significantly worsen the quality of the
* resulting random numbers compared to the quality of the full 32-bit result.
*
* @note Original name: qrand.c
*/
#include "rand.h"
#include "z64math.h"
#define RAND_MULTIPLIER 1664525
#define RAND_INCREMENT 1013904223
/**
* The latest generated random number, used to generate the next number in the sequence.
*
* @note Original name: __qrand_idum
*/
static u32 sRandInt = 1;
#if !PLATFORM_N64
/**
* Space to store a value to be re-interpreted as a float.
*
* @note Orignal name: __qrand_itemp
*/
static FloatInt sRandFloat;
#endif
/**
* Gets the next integer in the sequence of pseudo-random numbers.
*
* @note Original name: qrand
*/
u32 Rand_Next(void) {
#if PLATFORM_N64
u32 next = sRandInt * RAND_MULTIPLIER + RAND_INCREMENT;
sRandInt = next;
return next;
#else
return sRandInt = sRandInt * RAND_MULTIPLIER + RAND_INCREMENT;
#endif
}
/**
* Seeds the pseudo-random number generator by providing a starting value.
*
* @note Original name: sqrand
*/
void Rand_Seed(u32 seed) {
sRandInt = seed;
}
/**
* Returns a pseudo-random floating-point number between 0.0f and 1.0f, by generating the next integer and masking it
* to an IEEE-754 compliant floating-point number between 1.0f and 2.0f, returning the result subtract 1.0f.
*
* @note This technique for generating pseudo-random floats is recommended as a particularly fast but potentially
* non-portable generator in "Numerical Recipes in C: The Art of Scientic Computing", pp. 284-5.
*
* @note Original name: fqrand
*/
f32 Rand_ZeroOne(void) {
#if PLATFORM_N64
fu v;
f32 vf;
// Note this samples the lower 23 bits, effectively reducing the LCG period from 2^32 to 2^23.
// This was fixed in Gamecube versions and Majora's Mask.
v.i = (Rand_Next() & 0x007FFFFF) | 0x3F800000;
vf = v.f - 1.0f;
return vf;
#else
sRandInt = sRandInt * RAND_MULTIPLIER + RAND_INCREMENT;
// Samples the upper 23 bits to avoid effectively reducing the LCG period.
sRandFloat.i = (sRandInt >> 9) | 0x3F800000;
return sRandFloat.f - 1.0f;
#endif
}
#if !PLATFORM_N64
/**
* Returns a pseudo-random floating-point number between -0.5f and 0.5f by the same manner in which Rand_ZeroOne
* generates its result.
*
* @see Rand_ZeroOne
*
* @note Original name: fqrand2
*/
f32 Rand_Centered(void) {
sRandInt = sRandInt * RAND_MULTIPLIER + RAND_INCREMENT;
sRandFloat.i = (sRandInt >> 9) | 0x3F800000;
return sRandFloat.f - 1.5f;
}
#endif
//! All functions below are unused variants of the above four, that use a provided random number variable instead of the
//! internal `sRandInt`
/**
* Seeds a pseudo-random number at rndNum with a provided starting value.
*
* @see Rand_Seed
*
* @note Original name: sqrand_r
*/
void Rand_Seed_Variable(u32* rndNum, u32 seed) {
*rndNum = seed;
}
/**
* Generates the next pseudo-random integer from the provided rndNum.
*
* @see Rand_Next
*
* @note Original name: qrand_r
*/
u32 Rand_Next_Variable(u32* rndNum) {
return *rndNum = (*rndNum) * RAND_MULTIPLIER + RAND_INCREMENT;
}
/**
* Generates the next pseudo-random floating-point number between 0.0f and 1.0f from the provided rndNum.
*
* @see Rand_ZeroOne
*
* @note Original name: fqrand_r
*/
f32 Rand_ZeroOne_Variable(u32* rndNum) {
#if PLATFORM_N64
fu v;
f32 vf;
u32 next = Rand_Next_Variable(rndNum);
v.i = (next & 0x007FFFFF) | 0x3F800000;
vf = v.f - 1.0f;
return vf;
#else
u32 next = (*rndNum) * RAND_MULTIPLIER + RAND_INCREMENT;
sRandFloat.i = ((*rndNum = next) >> 9) | 0x3F800000;
return sRandFloat.f - 1.0f;
#endif
}
#if !PLATFORM_N64
/**
* Generates the next pseudo-random floating-point number between -0.5f and 0.5f from the provided rndNum.
*
* @see Rand_ZeroOne, Rand_Centered
*
* @note Original name: fqrand2_r
*/
f32 Rand_Centered_Variable(u32* rndNum) {
u32 next = (*rndNum) * RAND_MULTIPLIER + RAND_INCREMENT;
sRandFloat.i = ((*rndNum = next) >> 9) | 0x3F800000;
return sRandFloat.f - 1.5f;
}
#endif

View file

@ -1,172 +0,0 @@
#include "global.h"
#include "osMalloc.h"
#define LOG_SEVERITY_NOLOG 0
#define LOG_SEVERITY_ERROR 2
#define LOG_SEVERITY_VERBOSE 3
Arena gSystemArena;
#if OOT_DEBUG
s32 gSystemArenaLogSeverity = LOG_SEVERITY_NOLOG;
void SystemArena_CheckPointer(void* ptr, u32 size, const char* name, const char* action) {
if (ptr == NULL) {
if (gSystemArenaLogSeverity >= LOG_SEVERITY_ERROR) {
PRINTF(T("%s: %u バイトの%sに失敗しました\n", "%s: %u bytes %s failed\n"), name, size, action);
__osDisplayArena(&gSystemArena);
return;
}
} else if (gSystemArenaLogSeverity >= LOG_SEVERITY_VERBOSE) {
PRINTF(T("%s: %u バイトの%sに成功しました\n", "%s: %u bytes %s succeeded\n"), name, size, action);
}
}
#define SYSTEM_ARENA_CHECK_POINTER(ptr, size, name, action) SystemArena_CheckPointer(ptr, size, name, action)
#else
#define SYSTEM_ARENA_CHECK_POINTER(ptr, size, name, action) (void)0
#endif
void* SystemArena_Malloc(u32 size) {
DECLARE_INTERRUPT_MASK
void* ptr;
DISABLE_INTERRUPTS();
ptr = __osMalloc(&gSystemArena, size);
RESTORE_INTERRUPTS();
SYSTEM_ARENA_CHECK_POINTER(ptr, size, "malloc", "確保"); // "Secure"
return ptr;
}
#if OOT_DEBUG
void* SystemArena_MallocDebug(u32 size, const char* file, int line) {
DECLARE_INTERRUPT_MASK
void* ptr;
DISABLE_INTERRUPTS();
ptr = __osMallocDebug(&gSystemArena, size, file, line);
RESTORE_INTERRUPTS();
SYSTEM_ARENA_CHECK_POINTER(ptr, size, "malloc_DEBUG", "確保"); // "Secure"
return ptr;
}
#endif
void* SystemArena_MallocR(u32 size) {
DECLARE_INTERRUPT_MASK
void* ptr;
DISABLE_INTERRUPTS();
ptr = __osMallocR(&gSystemArena, size);
RESTORE_INTERRUPTS();
SYSTEM_ARENA_CHECK_POINTER(ptr, size, "malloc_r", "確保"); // "Secure"
return ptr;
}
#if OOT_DEBUG
void* SystemArena_MallocRDebug(u32 size, const char* file, int line) {
DECLARE_INTERRUPT_MASK
void* ptr;
DISABLE_INTERRUPTS();
ptr = __osMallocRDebug(&gSystemArena, size, file, line);
RESTORE_INTERRUPTS();
SYSTEM_ARENA_CHECK_POINTER(ptr, size, "malloc_r_DEBUG", "確保"); // "Secure"
return ptr;
}
#endif
void* SystemArena_Realloc(void* ptr, u32 newSize) {
DECLARE_INTERRUPT_MASK
DISABLE_INTERRUPTS();
ptr = __osRealloc(&gSystemArena, ptr, newSize);
RESTORE_INTERRUPTS();
SYSTEM_ARENA_CHECK_POINTER(ptr, newSize, "realloc", "再確保"); // "Re-securing"
return ptr;
}
#if OOT_DEBUG
void* SystemArena_ReallocDebug(void* ptr, u32 newSize, const char* file, int line) {
DECLARE_INTERRUPT_MASK
DISABLE_INTERRUPTS();
ptr = __osReallocDebug(&gSystemArena, ptr, newSize, file, line);
RESTORE_INTERRUPTS();
SYSTEM_ARENA_CHECK_POINTER(ptr, newSize, "realloc_DEBUG", "再確保"); // "Re-securing"
return ptr;
}
#endif
void SystemArena_Free(void* ptr) {
DECLARE_INTERRUPT_MASK
DISABLE_INTERRUPTS();
__osFree(&gSystemArena, ptr);
RESTORE_INTERRUPTS();
}
#if OOT_DEBUG
void SystemArena_FreeDebug(void* ptr, const char* file, int line) {
DECLARE_INTERRUPT_MASK
DISABLE_INTERRUPTS();
__osFreeDebug(&gSystemArena, ptr, file, line);
RESTORE_INTERRUPTS();
}
#endif
void* SystemArena_Calloc(u32 num, u32 size) {
DECLARE_INTERRUPT_MASK
void* ret;
u32 n = num * size;
DISABLE_INTERRUPTS();
ret = __osMalloc(&gSystemArena, n);
RESTORE_INTERRUPTS();
if (ret != NULL) {
bzero(ret, n);
}
SYSTEM_ARENA_CHECK_POINTER(ret, n, "calloc", "確保");
return ret;
}
#if OOT_DEBUG
void SystemArena_Display(void) {
PRINTF(T("システムヒープ表示\n", "System heap display\n"));
__osDisplayArena(&gSystemArena);
}
#endif
void SystemArena_GetSizes(u32* outMaxFree, u32* outFree, u32* outAlloc) {
ArenaImpl_GetSizes(&gSystemArena, outMaxFree, outFree, outAlloc);
}
void SystemArena_Check(void) {
__osCheckArena(&gSystemArena);
}
void SystemArena_Init(void* start, u32 size) {
#if OOT_DEBUG
gSystemArenaLogSeverity = LOG_SEVERITY_NOLOG;
#endif
__osMallocInit(&gSystemArena, start, size);
}
void SystemArena_Cleanup(void) {
#if OOT_DEBUG
gSystemArenaLogSeverity = LOG_SEVERITY_NOLOG;
#endif
__osMallocCleanup(&gSystemArena);
}
s32 SystemArena_IsInitialized(void) {
return __osMallocIsInitialized(&gSystemArena);
}

View file

@ -1,6 +1,7 @@
#include "global.h"
#include "fault.h"
#include "quake.h"
#include "rand.h"
#include "terminal.h"
#include "overlays/actors/ovl_Arms_Hook/z_arms_hook.h"