Merge branch 'main' of https://github.com/bumbread/ciabatta into main

This commit is contained in:
NeGate 2022-06-08 18:03:05 -04:00
commit d873ab18ef
51 changed files with 274 additions and 107 deletions

16
code/math/abs.c Normal file
View File

@ -0,0 +1,16 @@
#include <math.h>
double fabs(double x) {
if(x >= 0) {
return x;
}
return -x;
}
float fabsf(float x) {
if(x >= 0) {
return x;
}
return -x;
}

0
code/math/acos.c Normal file
View File

0
code/math/acosh.c Normal file
View File

0
code/math/asin.c Normal file
View File

0
code/math/asinh.c Normal file
View File

0
code/math/atan.c Normal file
View File

0
code/math/atanh.c Normal file
View File

0
code/math/cbrt.c Normal file
View File

0
code/math/ceil.c Normal file
View File

0
code/math/copysign.c Normal file
View File

0
code/math/cosh.c Normal file
View File

0
code/math/erf.c Normal file
View File

0
code/math/erfc.c Normal file
View File

0
code/math/exp.c Normal file
View File

0
code/math/fdim.c Normal file
View File

0
code/math/floats.c Normal file
View File

0
code/math/floor.c Normal file
View File

0
code/math/fma.c Normal file
View File

12
code/math/fmax.c Normal file
View File

@ -0,0 +1,12 @@
double fmax(double x, double y) {
if(x>y) return x;
return y;
}
float fmaxf(float x, float y) {
if(x>y) return x;
return y;
}

18
code/math/fmod.c Normal file
View File

@ -0,0 +1,18 @@
double fmod(double x, double y) {
int n = 0;
while(y < x*n) {
++n;
}
return y - n*x;
}
float fmodf(float x, float y) {
int n = 0;
while(y < x*n) {
++n;
}
return y - n*x;
}

0
code/math/frexp.c Normal file
View File

0
code/math/hypot.c Normal file
View File

0
code/math/ilogb.c Normal file
View File

0
code/math/ldexp.c Normal file
View File

0
code/math/lgamma.c Normal file
View File

0
code/math/llrint.c Normal file
View File

0
code/math/llround.c Normal file
View File

0
code/math/log.c Normal file
View File

0
code/math/lrint.c Normal file
View File

0
code/math/mod.c Normal file
View File

0
code/math/nan.c Normal file
View File

0
code/math/nearbyint.c Normal file
View File

0
code/math/nextafter.c Normal file
View File

0
code/math/pow.c Normal file
View File

0
code/math/remainder.c Normal file
View File

0
code/math/remquo.c Normal file
View File

0
code/math/rint.c Normal file
View File

0
code/math/scalbln.c Normal file
View File

0
code/math/scalbnf.c Normal file
View File

0
code/math/sin.c Normal file
View File

0
code/math/sinh.c Normal file
View File

0
code/math/sqrt.c Normal file
View File

0
code/math/tan.c Normal file
View File

0
code/math/tanh.c Normal file
View File

0
code/math/tgamma.c Normal file
View File

0
code/math/trunc.c Normal file
View File

View File

@ -1,6 +1,8 @@
#pragma once #pragma once
#include <_os.h>
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <Windows.h> #include <Windows.h>

View File

@ -1,11 +1,84 @@
#include "win.h" #include "win.h"
#include <stddef.h>
#include <stdio.h>
// It's just mapped directly to HANDLE // It's just mapped directly to HANDLE
struct FILE { struct FILE {
int unused; int unused;
}; };
int _os_del_file(char const *filename) {
int ok = DeleteFileA(filename);
return ok != 0;
}
int _os_mov_file(char const *old, char const *new) {
int ok = MoveFileA(old, new);
return ok != 0;
}
char *_os_tmpname(char *buffer) {
static UINT uniq = 0;
DWORD path_len = GetTempPathA(L_tmpnam, buffer);
if(path_len == 0) return NULL;
UINT ok = GetTempFileNameA(buffer, "", uniq, buffer);
if(ok == 0) return NULL;
return buffer;
}
FILE *_os_fopen(char const *restrict name, _OS_ModeFlags flags) {
DWORD desaddr = 0;
DWORD share = 0;
DWORD disp = 0;
switch(flags.base_mode) {
case 'r': {
desaddr = GENERIC_READ;
if(!flags.update) {
share = FILE_SHARE_READ;
}
disp = OPEN_EXISTING;
if(flags.update) {
disp = OPEN_ALWAYS;
}
} break;
case 'w': {
desaddr = GENERIC_WRITE;
disp = CREATE_ALWAYS;
} break;
case 'a': {
desaddr = GENERIC_WRITE;
} break;
}
if(flags.exclusive) {
disp = CREATE_NEW;
}
HANDLE fileHandle = CreateFileA(
name,
desaddr,
share,
NULL,
disp,
FILE_ATTRIBUTE_NORMAL,
NULL
);
FILE *file = (FILE *)fileHandle;
if(fileHandle == INVALID_HANDLE_VALUE) {
file = NULL;
}
return file;
}
int _os_fclose(FILE *file) {
HANDLE fileHandle = (HANDLE)file;
BOOL ok = CloseHandle(fileHandle);
return ok != 0;
}
void _os_file_write(void* ctx, size_t n, const char str[]) { void _os_file_write(void* ctx, size_t n, const char str[]) {
DWORD written = 0; DWORD written = 0;
WriteFile((HANDLE) ctx, str, n, &written, NULL); WriteFile((HANDLE) ctx, str, n, &written, NULL);

View File

@ -1,13 +1,71 @@
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <ctype.h> #include <ctype.h>
#include <stdbool.h>
#include <_os.h> #include <_os.h>
#define __STDC_WANT_LIB_EXT1__ 1 #define __STDC_WANT_LIB_EXT1__ 1
#include <string.h> #include <string.h>
#include <_os.h> int remove(const char *filename)
{
return _os_del_file(filename);
}
int rename(const char *old, const char *new)
{
return _os_mov_file(old, new);
}
char *tmpnam(char *s)
{
static char static_buffer[L_tmpnam];
char *buffer = s;
if(s == NULL) buffer = static_buffer;
return _os_tmpname(buffer);
}
FILE *fopen(const char *restrict filename, const char *restrict mode)
{
// Basically defined UB here by introducing missing modes
// It is simpler to implement that way I think.
int base_mode = mode[0];
int binary = 0;
int exclusive = 0;
int update = 0;
for(; *mode != 0; ++mode) {
if(*mode == 'x') exclusive = 1;
if(*mode == 'b') binary = 1;
if(*mode == '+') update = 1;
}
if(base_mode == 'r' && exclusive) return NULL;
if(base_mode == 'a' && exclusive) return NULL;
_OS_ModeFlags mode_flags = {
.base_mode = base_mode,
.binary = binary,
.update = update,
.exclusive = exclusive,
};
return _os_fopen(filename, mode_flags);
}
int fclose(FILE *stream)
{
return _os_fclose(stream);
}
// TODO:kekw:
FILE *freopen(
const char *restrict filename,
const char *restrict mode,
FILE *restrict stream)
{
fclose(stream);
return NULL;
}
typedef void(*OutputFunc)(void* ctx, size_t n, const char str[]); typedef void(*OutputFunc)(void* ctx, size_t n, const char str[]);
@ -28,10 +86,10 @@ typedef struct {
FILE *stdout, *stderr, *stdin; FILE *stdout, *stderr, *stdin;
#define CALL_PRINTF(fmt_func, ctx, out, fmt) \ #define CALL_PRINTF(fmt_func, ctx, out, fmt) \
va_list args; \ va_list args; \
va_start(args, fmt); \ va_start(args, fmt); \
int result = fmt_func(ctx, out, fmt, args); \ int result = fmt_func(ctx, out, fmt, args); \
va_end(args) va_end(args)
static void string_write(void *ctx, size_t n, const char *restrict str) { static void string_write(void *ctx, size_t n, const char *restrict str) {
StrPrintCtx *c = ctx; StrPrintCtx *c = ctx;

View File

@ -15,7 +15,24 @@
#endif #endif
// OS-dependent IO Functions // OS-dependent IO Functions
void _os_file_write(void* ctx, size_t n, const char str[]);
_Noreturn void _os_exit(int code); // TODO: see if we need this or will it be easier for linux to just pass
void _os_init_eh(); // the mode string.
typedef struct _OS_ModeFlags {
int base_mode;
int binary;
int update;
int exclusive;
} _OS_ModeFlags;
typedef struct FILE FILE; typedef struct FILE FILE;
int _os_del_file(char const *filename);
int _os_mov_file(char const *old, char const *new);
char *_os_tmpname(char *buffer);
FILE *_os_fopen(char const *restrict name, _OS_ModeFlags flags);
int _os_fclose(FILE *file);
void _os_file_write(void* ctx, size_t n, const char str[]);
void _os_init_eh();
_Noreturn void _os_exit(int code);

View File

@ -11,221 +11,192 @@ typedef double double_t;
#define INFINITY ((float)(_HUGE_ENUF * _HUGE_ENUF)) #define INFINITY ((float)(_HUGE_ENUF * _HUGE_ENUF))
#define HUGE_VAL ((double)INFINITY) #define HUGE_VAL ((double)INFINITY)
#define HUGE_VALF ((float)INFINITY) #define HUGE_VALF ((float)INFINITY)
#define HUGE_VALL ((long double)INFINITY)
// Positive NaN
#define NAN (-(float)(INFINITY * 0.0F)) #define NAN (-(float)(INFINITY * 0.0F))
#define FP_SUBNORMAL (-2) // FP_ILOGB0
#define FP_NORMAL (-1) // FP_ILOGBNAN
#define FP_ZERO 0
#define FP_INFINITE 1
#define FP_NAN 2
//FP_INFINITE #define MATH_ERRNO 0
//FP_NAN #define MATH_ERREXCEPT 1
//FP_NORMAL
//FP_SUBNORMAL
//FP_ZERO
#define FP_FAST_FMA 1
#define FP_FAST_FMAF 1
#define FP_FAST_FMAL 1
#define FP_ILOGB0 (-0x7FFFFFFFu - 1u) #define math_errhandling MATH_ERRNO
#define FP_ILOGBNAN 0x7FFFFFFFu
#define MATH_ERRNO 1 #define FP_INFINITE 0
#define MATH_ERREXCEPT 2 #define FP_NAN 1
#define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT) #define FP_NORMAL 2
#define FP_SUBNORMAL 3
// TODO: implement this #define FP_ZERO 4
#define fpclassify(x) 0 #define fpclassify(x) ( \
_is_float(x) ? _fpclassify_f(x) :
// HACK: If its fine just remove the comment _is_double(x) ? _fpclassify_d(x) :
#define isfinite(x) ((double)(x) != HUGE_VAL && (double)(x) != -HUGE_VAL) FP_NAN)
#define isinf(x) (!(isinf(x)))
#define isnan(x) 0
#define isnormal(x) 0
#define signbit(x) 0
// TODO: implement this, search it up, they're a bit different semantics than
// the names might suggest
#define isgreater(x, y) 0
#define isgreaterequal(x, y) 0
#define isless(x, y) 0
#define islessequal(x, y) 0
#define islessgreater(x, y) 0
#define isunordered(x, y) 0
double acos(double x); double acos(double x);
float acosf(float x); float acosf(float x);
long double acosl(long double x);
double asin(double x); double asin(double x);
float asinf(float x); float asinf(float x);
long double asinl(long double x);
double atan(double x); double atan(double x);
float atanf(float x); float atanf(float x);
long double atanl(long double x);
double atan2(double y, double x); double atan2(double y, double x);
float atan2f(float y, float x); float atan2f(float y, float x);
long double atan2l(long double y, long double x);
double cos(double x); double cos(double x);
float cosf(float x); float cosf(float x);
long double cosl(long double x);
double sin(double x); double sin(double x);
float sinf(float x); float sinf(float x);
long double sinl(long double x);
double tan(double x); double tan(double x);
float tanf(float x); float tanf(float x);
long double tanl(long double x);
double acosh(double x); double acosh(double x);
float acoshf(float x); float acoshf(float x);
long double acoshl(long double x);
double asinh(double x); double asinh(double x);
float asinhf(float x); float asinhf(float x);
long double asinhl(long double x);
double atanh(double x); double atanh(double x);
float atanhf(float x); float atanhf(float x);
long double atanhl(long double x);
double cosh(double x); double cosh(double x);
float coshf(float x); float coshf(float x);
long double coshl(long double x);
double sinh(double x); double sinh(double x);
float sinhf(float x); float sinhf(float x);
long double sinhl(long double x);
double tanh(double x); double tanh(double x);
float tanhf(float x); float tanhf(float x);
long double tanhl(long double x);
double exp(double x); double exp(double x);
float expf(float x); float expf(float x);
long double expl(long double x);
double exp2(double x); double exp2(double x);
float exp2f(float x); float exp2f(float x);
long double exp2l(long double x);
double expm1(double x); double expm1(double x);
float expm1f(float x); float expm1f(float x);
long double expm1l(long double x);
double frexp(double value, int *exp); double frexp(double value, int *exp);
float frexpf(float value, int *exp); float frexpf(float value, int *exp);
long double frexpl(long double value, int *exp);
int ilogb(double x); int ilogb(double x);
int ilogbf(float x); int ilogbf(float x);
int ilogbl(long double x);
double ldexp(double x, int exp); double ldexp(double x, int exp);
float ldexpf(float x, int exp); float ldexpf(float x, int exp);
long double ldexpl(long double x, int exp);
double log(double x); double log(double x);
float logf(float x); float logf(float x);
long double logl(long double x);
double log10(double x); double log10(double x);
float log10f(float x); float log10f(float x);
long double log10l(long double x);
double log1p(double x); double log1p(double x);
float log1pf(float x); float log1pf(float x);
long double log1pl(long double x);
double log2(double x); double log2(double x);
float log2f(float x); float log2f(float x);
long double log2l(long double x);
double logb(double x); double logb(double x);
float logbf(float x); float logbf(float x);
long double logbl(long double x);
double modf(double value, double *iptr); double modf(double value, double *iptr);
float modff(float value, float *iptr); float modff(float value, float *iptr);
long double modfl(long double value, long double *iptr);
double scalbn(double x, int n); double scalbn(double x, int n);
float scalbnf(float x, int n); float scalbnf(float x, int n);
long double scalbnl(long double x, int n);
double scalbln(double x, long int n); double scalbln(double x, long int n);
float scalblnf(float x, long int n); float scalblnf(float x, long int n);
long double scalblnl(long double x, long int n);
double cbrt(double x); double cbrt(double x);
float cbrtf(float x); float cbrtf(float x);
long double cbrtl(long double x);
double fabs(double x); double fabs(double x);
float fabsf(float x); float fabsf(float x);
long double fabsl(long double x);
double hypot(double x, double y); double hypot(double x, double y);
float hypotf(float x, float y); float hypotf(float x, float y);
long double hypotl(long double x, long double y);
double pow(double x, double y); double pow(double x, double y);
float powf(float x, float y); float powf(float x, float y);
long double powl(long double x, long double y);
double sqrt(double x); double sqrt(double x);
float sqrtf(float x); float sqrtf(float x);
long double sqrtl(long double x);
double erf(double x); double erf(double x);
float erff(float x); float erff(float x);
long double erfl(long double x);
double erfc(double x); double erfc(double x);
float erfcf(float x); float erfcf(float x);
long double erfcl(long double x);
double lgamma(double x); double lgamma(double x);
float lgammaf(float x); float lgammaf(float x);
long double lgammal(long double x);
double tgamma(double x); double tgamma(double x);
float tgammaf(float x); float tgammaf(float x);
long double tgammal(long double x);
double ceil(double x); double ceil(double x);
float ceilf(float x); float ceilf(float x);
long double ceill(long double x);
double floor(double x); double floor(double x);
float floorf(float x); float floorf(float x);
long double floorl(long double x);
double nearbyint(double x); double nearbyint(double x);
float nearbyintf(float x); float nearbyintf(float x);
long double nearbyintl(long double x);
double rint(double x); double rint(double x);
float rintf(float x); float rintf(float x);
long double rintl(long double x);
long int lrint(double x); long int lrint(double x);
long int lrintf(float x); long int lrintf(float x);
long int lrintl(long double x);
long long int llrint(double x); long long int llrint(double x);
long long int llrintf(float x); long long int llrintf(float x);
long long int llrintl(long double x);
double round(double x); double round(double x);
float roundf(float x); float roundf(float x);
long double roundl(long double x);
long int lround(double x); long int lround(double x);
long int lroundf(float x); long int lroundf(float x);
long int lroundl(long double x);
long long int llround(double x); long long int llround(double x);
long long int llroundf(float x); long long int llroundf(float x);
long long int llroundl(long double x);
double trunc(double x); double trunc(double x);
float truncf(float x); float truncf(float x);
long double truncl(long double x);
double fmod(double x, double y); double fmod(double x, double y);
float fmodf(float x, float y); float fmodf(float x, float y);
long double fmodl(long double x, long double y);
double remainder(double x, double y); double remainder(double x, double y);
float remainderf(float x, float y); float remainderf(float x, float y);
long double remainderl(long double x, long double y);
double remquo(double x, double y, int *quo); double remquo(double x, double y, int *quo);
float remquof(float x, float y, int *quo); float remquof(float x, float y, int *quo);
long double remquol(long double x, long double y, int *quo);
double copysign(double x, double y); double copysign(double x, double y);
float copysignf(float x, float y); float copysignf(float x, float y);
long double copysignl(long double x, long double y);
double nan(const char *tagp); double nan(const char *tagp);
float nanf(const char *tagp); float nanf(const char *tagp);
long double nanl(const char *tagp);
double nextafter(double x, double y); double nextafter(double x, double y);
float nextafterf(float x, float y); float nextafterf(float x, float y);
long double nextafterl(long double x, long double y);
double nexttoward(double x, long double y);
float nexttowardf(float x, long double y);
long double nexttowardl(long double x, long double y);
double fdim(double x, double y); double fdim(double x, double y);
float fdimf(float x, float y); float fdimf(float x, float y);
long double fdiml(long double x, long double y);
double fmax(double x, double y); double fmax(double x, double y);
float fmaxf(float x, float y); float fmaxf(float x, float y);
long double fmaxl(long double x, long double y);
double fmin(double x, double y); double fmin(double x, double y);
float fminf(float x, float y); float fminf(float x, float y);
long double fminl(long double x, long double y);
double fma(double x, double y, double z); double fma(double x, double y, double z);
float fmaf(float x, float y, float z); float fmaf(float x, float y, float z);
long double fmal(long double x, long double y, long double z);