Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libc cleanup #1568

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ OBJDUMP := $(MIPS_BINUTILS_PREFIX)objdump
EMULATOR ?=
EMU_FLAGS ?=

INC := -Iinclude -Isrc -Ibuild -I.
INC := -Iinclude -Iinclude/libc -Isrc -Ibuild -I.

# Check code syntax with host compiler
CHECK_WARNINGS := -Wall -Wextra -Wno-format-security -Wno-unknown-pragmas -Wno-unused-parameter -Wno-unused-variable -Wno-missing-braces
Expand Down
7 changes: 0 additions & 7 deletions include/alloca.h

This file was deleted.

2 changes: 1 addition & 1 deletion include/attributes.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef ATTRIBUTES_H
#define ATTRIBUTES_H

#ifndef __GNUC__
#if !defined(__GNUC__) && !defined(__attribute__)
#define __attribute__(x)
#endif

Expand Down
1 change: 1 addition & 0 deletions include/fault.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define FAULT_H

#include "ultra64.h"
#include "attributes.h"
#include "padmgr.h"

// These are the same as the 3-bit ansi color codes
Expand Down
34 changes: 0 additions & 34 deletions include/fp.h

This file was deleted.

30 changes: 10 additions & 20 deletions include/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@
#include "macros.h"

f32 fabsf(f32 f);
#ifndef __sgi
#define fabsf(f) __builtin_fabsf((f32)(f))
#else
#ifdef __sgi
#pragma intrinsic(fabsf)
#else
#define fabsf(f) __builtin_fabsf((f32)(f))
#endif

f32 sqrtf(f32 f);
#ifdef __sgi
#pragma intrinsic(sqrtf)
#endif
Dragorn421 marked this conversation as resolved.
Show resolved Hide resolved

f64 sqrt(f64 f);
#ifdef __sgi
#pragma intrinsic(sqrt)
#endif

void cleararena(void);
void bootproc(void);
Expand All @@ -30,12 +36,9 @@ void Locale_ResetRegion(void);
u32 func_80001F48(void);
u32 func_80001F8C(void);
u32 Locale_IsRegionNative(void);
NORETURN void __assert(const char* exp, const char* file, s32 line);
void isPrintfInit(void);
void osSyncPrintfUnused(const char* fmt, ...);
void osSyncPrintf(const char* fmt, ...);
void rmonPrintf(const char* fmt, ...);
void* is_proutSyncPrintf(void* arg, const char* str, u32 count);
void* is_proutSyncPrintf(void* arg, const char* str, size_t count);
NORETURN void func_80002384(const char* exp, const char* file, u32 line);
OSPiHandle* osDriveRomInit(void);
void Mio0_Decompress(Yaz0Header* hdr, u8* dst);
Expand All @@ -57,8 +60,6 @@ void LogUtils_CheckValidPointer(const char* exp, void* ptr, const char* file, s3
void LogUtils_LogThreadId(const char* name, s32 line);
void LogUtils_HungupThread(const char* name, s32 line);
void LogUtils_ResetHungup(void);
s32 vsprintf(char* dst, const char* fmt, va_list args);
s32 sprintf(char* dst, const char* fmt, ...);
void __osPiCreateAccessQueue(void);
void __osPiGetAccess(void);
void __osPiRelAccess(void);
Expand All @@ -75,7 +76,6 @@ void __osDispatchThread(void);
void __osCleanupThread(void);
void __osDequeueThread(OSThread** queue, OSThread* thread);
void osDestroyThread(OSThread* thread);
void bzero(void* __s, s32 __n);
void osCreateThread(OSThread* thread, OSId id, void (*entry)(void*), void* arg, void* sp, OSPri pri);
void __osSetSR(u32);
u32 __osGetSR(void);
Expand All @@ -92,12 +92,8 @@ void osViSetMode(OSViMode* mode);
u32 __osProbeTLB(void*);
u32 osGetMemSize(void);
void osSetEventMesg(OSEvent e, OSMesgQueue* mq, OSMesg msg);
s32 _Printf(PrintCallback, void* arg, const char* fmt, va_list ap);
void osUnmapTLBAll(void);
s32 osEPiStartDma(OSPiHandle* handle, OSIoMesg* mb, s32 direction);
const char* strchr(const char* str, s32 ch);
u32 strlen(const char* str);
void* memcpy(void* dst, const void* src, size_t size);
void osInvalICache(void* vaddr, s32 nbytes);
void osCreateMesgQueue(OSMesgQueue* mq, OSMesg* msg, s32 count);
void osInvalDCache(void* vaddr, s32 nbytes);
Expand All @@ -108,15 +104,13 @@ OSPri osGetThreadPri(OSThread* thread);
s32 __osEPiRawReadIo(OSPiHandle* handle, u32 devAddr, u32* data);
void osViSwapBuffer(void* frameBufPtr);
s32 __osEPiRawStartDma(OSPiHandle* handle, s32 direction, u32 cartAddr, void* dramAddr, size_t size);
u32 bcmp(void* __sl, void* __s2, u32 __n);
OSTime osGetTime(void);
void __osTimerServicesInit(void);
void __osTimerInterrupt(void);
void __osSetTimerIntr(OSTime time);
OSTime __osInsertTimer(OSTimer* timer);
u32 osGetCount(void);
void __osSetCompare(u32);
void* bcopy(void* __src, void* __dest, u32 __n);
s32 __osDisableInt(void);
void __osRestoreInt(s32);
void __osViInit(void);
Expand All @@ -132,10 +126,6 @@ void osMapTLBRdb(void);
void osYieldThread(void);
u32 __osGetCause(void);
s32 __osEPiRawWriteIo(OSPiHandle* handle, u32 devAddr, u32 data);
void _Litob(_Pft* args, u8 type);
ldiv_t ldiv(s32 num, s32 denom);
lldiv_t lldiv(s64 num, s64 denom);
void _Ldtob(_Pft* args, u8 type);
s32 __osSiRawWriteIo(void* devAddr, u32 val);
void osCreateViManager(OSPri pri);
OSViContext* __osViGetCurrentContext(void);
Expand Down
9 changes: 9 additions & 0 deletions include/libc/alloca.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef ALLOCA_H
#define ALLOCA_H

#include "stddef.h"

void* alloca(size_t);
#define alloca(size) __builtin_alloca(size)

#endif
45 changes: 45 additions & 0 deletions include/libc/assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef ASSERT_H
#define ASSERT_H

#if !defined(__GNUC__) && !defined(__attribute__)
#define __attribute__(x)
#endif
Dragorn421 marked this conversation as resolved.
Show resolved Hide resolved

// Runtime assertions

__attribute__((noreturn)) void __assert(const char* assertion, const char* file, int line);
Dragorn421 marked this conversation as resolved.
Show resolved Hide resolved

// assert for matching
#ifndef NDEBUG
# ifndef NON_MATCHING
# define ASSERT(cond, msg, file, line) ((cond) ? ((void)0) : __assert(msg, file, line))
# else
# define ASSERT(cond, msg, file, line) ((cond) ? ((void)0) : __assert(#cond, __FILE__, __LINE__))
# endif
#else
# define ASSERT(cond, msg, file, line) ((void)0)
#endif

// standard assert macro
#ifndef NDEBUG
# define assert(cond) ASSERT(cond, #cond, __FILE__, __LINE__)
#else
# define assert(cond) ((void)0)
#endif

// Static/compile-time assertions

#if defined(__GNUC__) || (__STDC_VERSION__ >= 201112L)
# define static_assert(cond, msg) _Static_assert(cond, msg)
#else
# ifndef GLUE
# define GLUE(a, b) a##b
# endif
# ifndef GLUE2
# define GLUE2(a, b) GLUE(a, b)
# endif

# define static_assert(cond, msg) typedef char GLUE2(static_assertion_failed, __LINE__)[(cond) ? 1 : -1]
#endif

#endif
39 changes: 26 additions & 13 deletions include/libc/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,37 @@

#define M_PI 3.14159265358979323846f
#define M_SQRT2 1.41421356237309504880f
#define FLT_MAX 340282346638528859811704183484516925440.0f
#define MAXFLOAT 3.40282347e+38f
#define SHT_MAX 32767.0f
Comment on lines 7 to 9
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a known original name or a standard name for this?
am fine with it if so, but it just goes against our convention for similar things

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the standard C name for the largest representable float define in math.h.
FLT_MAX is another standard C name, but is part of float.h instead, which we don't currently have. It might be nice to add additional standard headers in the future, but there's some questions about exactly how much to add so I opted not to add too many new headers for now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically ISO C has FLT_MAX in float.h, which is not a header we currently have. I think it may be nice to add float.h and limits.h at some point, or I can attempt to add them now if it's much desired?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with the way it is now personally

#define SHT_MINV (1.0f / SHT_MAX)

typedef union {
struct {
u32 hi;
u32 lo;
} word;
float floorf(float);
double floor(double);
long lfloorf(float);
long lfloor(double);

f64 d;
} du;
float ceilf(float);
double ceil(double);
long lceilf(float);
long lceil(double);

typedef union {
u32 i;
f32 f;
} fu;
float truncf(float);
double trunc(double);
long ltruncf(float);
long ltrunc(double);

extern f32 __libm_qnan_f;
float nearbyintf(float);
double nearbyint(double);
long lnearbyintf(float);
long lnearbyint(double);

float roundf(float);
double round(double);
long lroundf(float);
long lround(double);

extern float qNaN0x3FFFFF;
extern float qNaN0x10000;
extern float sNaN0x3FFFFF;

#endif
72 changes: 43 additions & 29 deletions include/libc/stdarg.h
Original file line number Diff line number Diff line change
@@ -1,43 +1,57 @@
#ifndef STDARG_H
#define STDARG_H

// When building with GCC, use the official vaarg macros to avoid warnings
// and possibly bad codegen.
// When building with GCC, use the official vaarg macros to avoid warnings and possibly bad codegen.

#ifdef __GNUC__
#define va_list __builtin_va_list

#define va_list __builtin_va_list
#define va_start __builtin_va_start
#define va_arg __builtin_va_arg
#define va_end __builtin_va_end
#define va_arg __builtin_va_arg
#define va_end __builtin_va_end

#else

#ifndef _VA_LIST_
# define _VA_LIST_
typedef char* va_list;
#define _FP 1
#define _INT 0
#endif

#define _INT 0
#define _FP 1
#define _STRUCT 2

#define _VA_FP_SAVE_AREA 0x10
#define _VA_ALIGN(p, a) (((u32)(((char*)p) + ((a) > 4 ? (a) : 4) - 1)) & -((a) > 4 ? (a) : 4))
#define va_start(vp, parmN) (vp = ((va_list)&parmN + sizeof(parmN)))

#define __va_stack_arg(list, mode) \
( \
((list) = (char*)_VA_ALIGN(list, __builtin_alignof(mode)) + \
_VA_ALIGN(sizeof(mode), 4)), \
(((char*)list) - (_VA_ALIGN(sizeof(mode), 4) - sizeof(mode))))

#define __va_double_arg(list, mode) \
( \
(((s32)list & 0x1) /* 1 byte aligned? */ \
? (list = (char*)((s32)list + 7), (char*)((s32)list - 6 - _VA_FP_SAVE_AREA)) \
: (((s32)list & 0x2) /* 2 byte aligned? */ \
? (list = (char*)((s32)list + 10), (char*)((s32)list - 24 - _VA_FP_SAVE_AREA)) \
: __va_stack_arg(list, mode))))

#define va_arg(list, mode) ((mode*)(((__builtin_classof(mode) == _FP && \
__builtin_alignof(mode) == sizeof(f64)) \
? __va_double_arg(list, mode) \
: __va_stack_arg(list, mode))))[-1]

#define _VA_ALIGN(p, a) (((unsigned int)(((char*)p) + ((a) > 4 ? (a) : 4) - 1)) & -((a) > 4 ? (a) : 4))

#define va_start(list, parmN) (list = ((va_list)&parmN + sizeof(parmN)))

#define __va_stack_arg(list, mode) \
( \
((list) = (char*)_VA_ALIGN(list, __builtin_alignof(mode)) + \
_VA_ALIGN(sizeof(mode), 4)), \
(((char*)list) - (_VA_ALIGN(sizeof(mode), 4) - sizeof(mode))) \
)

#define __va_double_arg(list, mode) \
( \
(((long)list & 0x1) /* 1 byte aligned? */ \
? (list = (char*)((long)list + 7), (char*)((long)list - 6 - _VA_FP_SAVE_AREA)) \
: (((long)list & 0x2) /* 2 byte aligned? */ \
? (list = (char*)((long)list + 10), (char*)((long)list - 24 - _VA_FP_SAVE_AREA)) \
: __va_stack_arg(list, mode))) \
)

#define va_arg(list, mode) \
((mode*)(((__builtin_classof(mode) == _FP && \
__builtin_alignof(mode) == sizeof(double)) \
? __va_double_arg(list, mode) \
: __va_stack_arg(list, mode))))[-1]

/* No cleanup processing is required for the end of a varargs list: */
#define va_end(__list)

#endif
#endif /* __GNUC__ */

#endif
7 changes: 6 additions & 1 deletion include/libc/stdbool.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

#ifndef __cplusplus

#define bool u32
#if (__STDC_VERSION__ >= 199901L)
#define bool _Bool
#else
#define bool unsigned int
#endif

#define false 0
#define true 1

Expand Down
12 changes: 9 additions & 3 deletions include/libc/stddef.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@

#define NULL ((void*)0)

#if !defined(_SIZE_T) && !defined(_SIZE_T_)
#define _SIZE_T

#if !defined(_MIPS_SZLONG) || (_MIPS_SZLONG == 32)
typedef unsigned int size_t;
#endif
#if defined(_MIPS_SZLONG) && (_MIPS_SZLONG == 64)
typedef unsigned long size_t;
#else
typedef unsigned int size_t;
typedef unsigned long size_t;
#endif

#endif

#ifdef __GNUC__
Expand Down
Loading