Skip to content

Commit 0c70ec0

Browse files
authored
Merge pull request #4934 from laytan/vendor-libc-additions
vendor/libc: a bunch of additions
2 parents 408b3af + 140c902 commit 0c70ec0

15 files changed

+855
-15
lines changed

vendor/libc/ctype.odin

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package odin_libc
2+
3+
@(require, linkage="strong", link_name="isdigit")
4+
isdigit :: proc "c" (c: i32) -> b32 {
5+
switch c {
6+
case '0'..='9': return true
7+
case: return false
8+
}
9+
}
10+
11+
@(require, linkage="strong", link_name="isblank")
12+
isblank :: proc "c" (c: i32) -> b32 {
13+
switch c {
14+
case '\t', ' ': return true
15+
case: return false
16+
}
17+
}
18+
19+
@(require, linkage="strong", link_name="isspace")
20+
isspace :: proc "c" (c: i32) -> b32 {
21+
switch c {
22+
case '\t', ' ', '\n', '\v', '\f', '\r': return true
23+
case: return false
24+
}
25+
}
26+
27+
@(require, linkage="strong", link_name="toupper")
28+
toupper :: proc "c" (c: i32) -> i32 {
29+
if c >= 'a' && c <= 'z' {
30+
return c - ('a' - 'A')
31+
}
32+
return c
33+
}

vendor/libc/include/alloca.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifdef __cplusplus
2+
extern "C" {
3+
#endif
4+
5+
#pragma once
6+
7+
#include <stddef.h>
8+
9+
void *alloca(size_t); /* built-in for gcc */
10+
11+
#if defined(__GNUC__) && __GNUC__ >= 3
12+
/* built-in for gcc 3 */
13+
#undef alloca
14+
#undef __alloca
15+
#define alloca(size) __alloca(size)
16+
#define __alloca(size) __builtin_alloca(size)
17+
#endif
18+
19+
#ifdef __cplusplus
20+
}
21+
#endif

vendor/libc/include/assert.h

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#ifdef __cplusplus
2+
extern "C" {
3+
#endif
4+
5+
#pragma once
6+
17
#ifdef NDEBUG
28
#define assert(e) ((void)0)
39
#else
@@ -14,3 +20,9 @@ void __odin_libc_assert_fail(const char *, const char *, int, const char *);
1420
(__builtin_expect(!(e), 0) ? __odin_libc_assert_fail(__func__, __ASSERT_FILE_NAME, __LINE__, #e) : (void)0)
1521

1622
#endif /* NDEBUG */
23+
24+
#define static_assert _Static_assert
25+
26+
#ifdef __cplusplus
27+
}
28+
#endif

vendor/libc/include/ctype.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifdef __cplusplus
2+
extern "C" {
3+
#endif
4+
5+
#pragma once
6+
7+
int isdigit(int c);
8+
int isblank(int c);
9+
int isspace(int c);
10+
11+
int toupper(int c);
12+
13+
#ifdef __cplusplus
14+
}
15+
#endif

vendor/libc/include/inttypes.h

Whitespace-only changes.

vendor/libc/include/math.h

+47-2
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,66 @@
1+
#ifdef __cplusplus
2+
extern "C" {
3+
#endif
4+
5+
#pragma once
6+
17
#include <stdbool.h>
28

9+
#define INFINITY (1.0 / 0.0)
10+
#define NAN (0.0 / 0.0)
11+
312
float sqrtf(float);
413
float cosf(float);
514
float sinf(float);
615
float atan2f(float, float);
7-
bool isnan(float);
8-
bool isinf(float);
16+
17+
float floorf(float x);
918
double floor(double x);
19+
float ceilf(float x);
1020
double ceil(double x);
1121
double sqrt(double x);
22+
float powf(float x, float y);
1223
double pow(double x, double y);
24+
float fmodf(float x, float y);
1325
double fmod(double x, double y);
1426
double cos(double x);
27+
float acosf(float x);
1528
double acos(double x);
29+
float fabsf(float x);
1630
double fabs(double x);
1731
int abs(int);
1832
double ldexp(double, int);
1933
double exp(double);
34+
float logf(float);
2035
double log(double);
2136
double sin(double);
37+
double trunc(double);
38+
double log2(double);
39+
double log10(double);
40+
double asin(double);
41+
double atan(double);
42+
double tan(double);
43+
double atan2(double, double);
44+
double modf(double, double*);
45+
46+
bool __isnanf(float);
47+
bool __isnand(double);
48+
#define isnan(x) \
49+
( sizeof(x) == sizeof(float) ? __isnanf((float)(x)) \
50+
: : __isnand((double)(x)))
51+
52+
bool __isinff(float);
53+
bool __isinfd(double);
54+
#define isinf(x) \
55+
( sizeof(x) == sizeof(float) ? __isinff((float)(x)) \
56+
: : __isinfd((double)(x)))
57+
58+
bool __isfinitef(float);
59+
bool __isfinited(double);
60+
#define isfinite(x) \
61+
( sizeof(x) == sizeof(float) ? __isfinitef((float)(x)) \
62+
: : __isfinited((double)(x)))
63+
64+
#ifdef __cplusplus
65+
}
66+
#endif

vendor/libc/include/stdio.h

+56-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
#include <stddef.h>
2-
#include <stdarg.h>
1+
#ifdef __cplusplus
2+
extern "C" {
3+
#endif
34

45
#pragma once
56

7+
#include <alloca.h>
8+
#include <assert.h>
9+
#include <stdarg.h>
10+
#include <stddef.h>
11+
612
typedef struct {} FILE;
713

814
#define SEEK_SET 0
@@ -12,6 +18,8 @@ typedef struct {} FILE;
1218
#define stdout ((FILE *)2)
1319
#define stderr ((FILE *)3)
1420

21+
#define EOF -1
22+
1523
FILE *fopen(const char *, char *);
1624
int fclose(FILE *);
1725
int fseek(FILE *, long, int);
@@ -21,6 +29,10 @@ size_t fwrite(const void *, size_t, size_t, FILE *);
2129

2230
int vfprintf(FILE *, const char *, va_list);
2331
int vsnprintf(char *, size_t, const char *, va_list);
32+
int vsprintf(char *, const char *, va_list);
33+
34+
int putchar(int ch);
35+
int getchar();
2436

2537
static inline int snprintf(char *buf, size_t size, const char *fmt, ...) {
2638
va_list args;
@@ -30,6 +42,14 @@ static inline int snprintf(char *buf, size_t size, const char *fmt, ...) {
3042
return result;
3143
}
3244

45+
static inline int sprintf(char *buf, const char *fmt, ...) {
46+
va_list args;
47+
va_start(args, fmt);
48+
int result = vsprintf(buf, fmt, args);
49+
va_end(args);
50+
return result;
51+
}
52+
3353
static inline int fprintf(FILE *f, const char *fmt, ...) {
3454
va_list args;
3555
va_start(args, fmt);
@@ -45,3 +65,37 @@ static inline int printf(const char *fmt, ...) {
4565
va_end(args);
4666
return result;
4767
}
68+
69+
int __sscanf(const char *str, const char *format, void *ptrs);
70+
71+
static inline int vsscanf(const char *str, const char *format, va_list ap) {
72+
int count = 0;
73+
for (int i = 0; format[i]; i++) {
74+
if (format[i] == '%') {
75+
if (format[i+1] == '%') {
76+
i++;
77+
continue;
78+
}
79+
count++;
80+
}
81+
}
82+
83+
void **ptrs = (void **)(alloca(count*sizeof(void *)));
84+
for (int i = 0; i < count; i++) {
85+
ptrs[i] = va_arg(ap, void *);
86+
}
87+
88+
return __sscanf(str, format, ptrs);
89+
}
90+
91+
static inline int sscanf(const char *str, const char *format, ...) {
92+
va_list args;
93+
va_start(args, format);
94+
int res = vsscanf(str, format, args);
95+
va_end(args);
96+
return res;
97+
}
98+
99+
#ifdef __cplusplus
100+
}
101+
#endif

vendor/libc/include/stdlib.h

+25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#ifdef __cplusplus
2+
extern "C" {
3+
#endif
4+
5+
#pragma once
6+
17
#include <stddef.h>
28

39
void *malloc(size_t size);
@@ -17,3 +23,22 @@ long long atoll(const char *);
1723
double atof(const char *);
1824

1925
long strtol(const char *, char **, int);
26+
double strtod(const char *, char **);
27+
28+
void abort();
29+
void exit(int exit_code);
30+
31+
#define ATEXIT_MAX 32
32+
33+
int atexit(typeof(void (void)) *);
34+
35+
typedef struct {
36+
long int quot;
37+
long int rem;
38+
} ldiv_t;
39+
40+
ldiv_t ldiv(long int number, long int denom);
41+
42+
#ifdef __cplusplus
43+
}
44+
#endif

vendor/libc/include/string.h

+11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
#ifdef __cplusplus
2+
extern "C" {
3+
#endif
4+
5+
#pragma once
6+
17
#include <stddef.h>
28

39
void *memcpy(void *, const void *, size_t);
410
void *memset(void *, int, size_t);
511
void *memmove(void *, void *, size_t);
612
int memcmp(const void *, const void *, size_t);
13+
void *memchr(const void *, int, size_t);
714

815
unsigned long strlen(const char *str);
916

@@ -19,3 +26,7 @@ int strcmp(const char *, const char *);
1926
int strncmp(const char *, const char *, size_t);
2027

2128
char *strstr(const char *, const char *);
29+
30+
#ifdef __cplusplus
31+
}
32+
#endif

vendor/libc/include/time.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifdef __cplusplus
2+
extern "C" {
3+
#endif
4+
5+
#pragma once
6+
7+
#include <stdint.h>
8+
9+
typedef int64_t clock_t;
10+
typedef clock_t time_t;
11+
12+
clock_t clock();
13+
14+
#ifdef __cplusplus
15+
}
16+
#endif

0 commit comments

Comments
 (0)