working on it

This commit is contained in:
Dawid Sobczak 2025-04-05 10:55:40 +01:00
parent 56a6e78765
commit 35a88970c2
1094 changed files with 51093 additions and 51 deletions

View file

@ -0,0 +1,4 @@
int abs(int a)
{
return a>0 ? a : -a;
}

View file

@ -0,0 +1,6 @@
#include <stdlib.h>
double atof(const char *s)
{
return strtod(s, 0);
}

View file

@ -0,0 +1,15 @@
#include <stdlib.h>
#include <ctype.h>
int atoi(const char *s)
{
int n=0, neg=0;
while (isspace(*s)) s++;
switch (*s) {
case '-': neg=1;
case '+': s++;
}
while (isdigit(*s))
n = 10*n + *s++ - '0';
return neg ? -n : n;
}

View file

@ -0,0 +1,16 @@
#include <stdlib.h>
#include <ctype.h>
long atol(const char *s)
{
long n=0;
int neg=0;
while (isspace(*s)) s++;
switch (*s) {
case '-': neg=1;
case '+': s++;
}
while (isdigit(*s))
n = 10*n + *s++ - '0';
return neg ? -n : n;
}

View file

@ -0,0 +1,16 @@
#include <stdlib.h>
#include <ctype.h>
long long atoll(const char *s)
{
long long n=0;
int neg=0;
while (isspace(*s)) s++;
switch (*s) {
case '-': neg=1;
case '+': s++;
}
while (isdigit(*s))
n = 10*n + *s++ - '0';
return neg ? -n : n;
}

View file

@ -0,0 +1,20 @@
#include <stdlib.h>
void *bsearch(const void *key, const void *base, size_t nel, size_t width, int (*cmp)(const void *, const void *))
{
void *try;
int sign;
while (nel > 0) {
try = (char *)base + width*(nel/2);
sign = cmp(key, try);
if (!sign) return try;
else if (nel == 1) break;
else if (sign < 0)
nel /= 2;
else {
base = try;
nel -= nel/2;
}
}
return NULL;
}

View file

@ -0,0 +1,6 @@
#include <stdlib.h>
div_t div(int num, int den)
{
return (div_t){ num/den, num%den };
}

View file

@ -0,0 +1,23 @@
#include <math.h>
#include <inttypes.h>
double frexp(double x, int *e)
{
union { double d; uint64_t i; } y = { x };
int ee = y.i>>52 & 0x7ff;
if (!ee) {
if (x) {
x = frexp(x*0x1p64, e);
*e -= 64;
} else *e = 0;
return x;
} else if (ee == 0x7ff) {
return x;
}
*e = ee - 0x3fe;
y.i &= 0x800fffffffffffffull;
y.i |= 0x3fe0000000000000ull;
return y.d;
}

View file

@ -0,0 +1,23 @@
#include <math.h>
#include <inttypes.h>
float frexpf(float x, int *e)
{
union { float f; uint32_t i; } y = { x };
int ee = y.i>>23 & 0xff;
if (!ee) {
if (x) {
x = frexpf(x*0x1p64, e);
*e -= 64;
} else *e = 0;
return x;
} else if (ee == 0xff) {
return x;
}
*e = ee - 0x7e;
y.i &= 0x807ffffful;
y.i |= 0x3f000000ul;
return y.f;
}

View file

@ -0,0 +1,25 @@
#include <math.h>
#include <inttypes.h>
/* This version is for 80-bit little endian long double */
long double frexpl(long double x, int *e)
{
union { long double ld; uint16_t hw[5]; } y = { x };
int ee = y.hw[4]&0x7fff;
if (!ee) {
if (x) {
x = frexpl(x*0x1p64, e);
*e -= 64;
} else *e = 0;
return x;
} else if (ee == 0x7fff) {
return x;
}
*e = ee - 0x3ffe;
y.hw[4] &= 0x8000;
y.hw[4] |= 0x3ffe;
return y.ld;
}

View file

@ -0,0 +1,6 @@
#include <inttypes.h>
intmax_t imaxabs(intmax_t a)
{
return a>0 ? a : -a;
}

View file

@ -0,0 +1,6 @@
#include <inttypes.h>
imaxdiv_t imaxdiv(intmax_t num, intmax_t den)
{
return (imaxdiv_t){ num/den, num%den };
}

View file

@ -0,0 +1,4 @@
long labs(long a)
{
return a>0 ? a : -a;
}

View file

@ -0,0 +1,6 @@
#include <stdlib.h>
ldiv_t ldiv(long num, long den)
{
return (ldiv_t){ num/den, num%den };
}

View file

@ -0,0 +1,4 @@
long long llabs(long long a)
{
return a>0 ? a : -a;
}

View file

@ -0,0 +1,6 @@
#include <stdlib.h>
lldiv_t lldiv(long long num, long long den)
{
return (lldiv_t){ num/den, num%den };
}

View file

@ -0,0 +1,50 @@
#include <stdlib.h>
#include <string.h>
/* A simple heap sort implementation.. only in-place O(nlogn) sort I know. */
#define MIN(a, b) ((a)<(b) ? (a) : (b))
static void swap(char *a, char *b, size_t len)
{
char tmp[256];
size_t l;
while (len) {
l = MIN(sizeof tmp, len);
memcpy(tmp, a, l);
memcpy(a, b, l);
memcpy(b, tmp, l);
a += l;
b += l;
len -= l;
}
}
static void sift(char *base, size_t root, size_t nel, size_t width, int (*cmp)(const void *, const void *))
{
size_t max;
while (2*root <= nel) {
max = 2*root;
if (max < nel && cmp(base+max*width, base+(max+1)*width) < 0)
max++;
if (max && cmp(base+root*width, base+max*width) < 0) {
swap(base+root*width, base+max*width, width);
root = max;
} else break;
}
}
void qsort(void *_base, size_t nel, size_t width, int (*cmp)(const void *, const void *))
{
char *base = _base;
size_t i;
if (!nel) return;
for (i=(nel+1)/2; i; i--)
sift(base, i-1, nel-1, width, cmp);
for (i=nel-1; i; i--) {
swap(base, base+i*width, width);
sift(base, 0, i-1, width, cmp);
}
}

View file

@ -0,0 +1,6 @@
#include <stdlib.h>
double strtod(const char *s, char **p)
{
return strtold(s, p);
}

View file

@ -0,0 +1,6 @@
#include <stdlib.h>
float strtof(const char *s, char **p)
{
return strtold(s, p);
}

View file

@ -0,0 +1,25 @@
#include <inttypes.h>
#include <errno.h>
#include <ctype.h>
intmax_t strtoimax(const char *s1, char **p, int base)
{
const unsigned char *s = (const void *)s1;
int sign = 0;
uintmax_t x;
/* Initial whitespace */
for (; isspace(*s); s++);
/* Optional sign */
if (*s == '-') sign = *s++;
else if (*s == '+') s++;
x = strtoumax((const void *)s, p, base);
if (x > INTMAX_MAX) {
if (!sign || -x != INTMAX_MIN)
errno = ERANGE;
return sign ? INTMAX_MIN : INTMAX_MAX;
}
return sign ? -x : x;
}

View file

@ -0,0 +1,17 @@
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <limits.h>
long strtol(const char *s, char **p, int base)
{
intmax_t x = strtoimax(s, p, base);
if (x > LONG_MAX) {
errno = ERANGE;
return LONG_MAX;
} else if (x < LONG_MIN) {
errno = ERANGE;
return LONG_MIN;
}
return x;
}

View file

@ -0,0 +1,99 @@
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
static long double negate_ld(long double d) {
register unsigned long long *p = (unsigned long long *)&d;
p[1] ^= 1ul<<15;
return *(long double *)p;
}
long double strtold(const char *s1, char **p)
{
const unsigned char *s = s1;
long double x = 0;
long double frac;
int sign = 0;
int nonzero = 0;
int radix = '.';
long e;
if (!p) p = (char **)&s1;
/* Initial whitespace */
for (; isspace(*s); s++);
/* Optional sign */
if (*s == '-') sign = *s++;
else if (*s == '+') s++;
/* Handle infinities and NaNs. */
if ((s[0]|32)=='i' && (s[1]|32)=='n' && (s[2]|32)=='f') {
*p = (char *)s + 3;
return sign ? -1.0/0.0 : 1.0/0.0;
} else if ((s[0]|32)=='n' && (s[1]|32)=='a' && (s[2]|32)=='n') {
*p = (char *)s + 3;
return 0.0/0.0;
}
/* Possible hex float */
if (s[0]=='0' && (s[1]|32)=='x') {
/* Mantissa must be non-degenerate */
if (!isxdigit(s[2]) && (s[2]!=radix || !isxdigit(s[3]))) {
/* Decimal float 0, 'x' extraneous */
*p = (char *)++s;
return 0;
}
/* We have a real hex float */
s += 2;
for (; isxdigit(*s); s++) {
x = 16*x + (isdigit(*s)?*s-'0':(*s|32)-'a');
if (*s!='0') nonzero=1;
}
if (*s == radix) {
frac = 1.0/16.0;
for (s++; isxdigit(*s); s++) {
x += frac * (isdigit(*s)?*s-'0':(*s|32)-'a');
frac *= 1.0/16.0;
if (*s!='0') nonzero=1;
}
}
if ((*s|32) == 'p') {
e = strtol(s+1, (void *)&s, 10);
for (; e>0; e--) x *= 2.0;
for (; e<0; e++) x *= 0.5;
}
if ((nonzero && !x) || !(1.0/x))
errno = ERANGE;
*p = (char *)s;
return sign ? negate_ld(x) : x;
}
/* Mantissa must be non-degenerate */
if (!isdigit(s[0]) && (s[0]!=radix || !isdigit(s[1]))) {
*p = (char *)s1;
return 0;
}
for (; isdigit(*s); s++) {
x = 10*x + *s-'0';
if (*s!='0') nonzero=1;
}
if (*s == radix) {
frac = 10.0;
for (s++; isdigit(*s); s++) {
x += (*s-'0') / frac;
frac *= 10.0;
if (*s!='0') nonzero=1;
}
}
if ((*s|32)=='e') {
e = strtol(++s, (void *)&s, 10);
for (; e>0; e--) x *= 10.0;
for (; e<0; e++) x /= 10.0;
}
if ((nonzero && !x) || !(1.0/x))
errno = ERANGE;
*p = (char*)s;
return sign ? negate_ld(x) : x;
}

View file

@ -0,0 +1,17 @@
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <limits.h>
long long strtoll(const char *s, char **p, int base)
{
intmax_t x = strtoimax(s, p, base);
if (x > LLONG_MAX) {
errno = ERANGE;
return LLONG_MAX;
} else if (x < LLONG_MIN) {
errno = ERANGE;
return LLONG_MIN;
}
return x;
}

View file

@ -0,0 +1,14 @@
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <limits.h>
unsigned long strtoul(const char *s, char **p, int base)
{
uintmax_t x = strtoumax(s, p, base);
if (x > ULONG_MAX) {
errno = ERANGE;
return ULONG_MAX;
}
return x;
}

View file

@ -0,0 +1,14 @@
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <limits.h>
unsigned long long strtoull(const char *s, char **p, int base)
{
uintmax_t x = strtoumax(s, p, base);
if (x > ULLONG_MAX) {
errno = ERANGE;
return ULLONG_MAX;
}
return x;
}

View file

@ -0,0 +1,123 @@
#include <inttypes.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <stdio.h>
/* Lookup table for digit values. -1==255>=36 -> invalid */
static const unsigned char digits[] = {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
-1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1,
-1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
};
uintmax_t strtoumax(const char *s1, char **p, int base)
{
const unsigned char *s = s1;
size_t x1, z1;
uintmax_t x, z=0;
int sign = 0;
int shift;
if (!p) p = (char **)&s1;
/* Initial whitespace */
for (; isspace(*s); s++);
/* Optional sign */
if (*s == '-') sign = *s++;
else if (*s == '+') s++;
/* Default base 8, 10, or 16 depending on prefix */
if (base == 0) {
if (s[0] == '0') {
if ((s[1]|32) == 'x') base = 16;
else base = 8;
} else {
base = 10;
}
}
if ((unsigned)base-2 > 36-2 || digits[*s]>=base) {
*p = (char *)s1;
errno = EINVAL;
return 0;
}
/* Main loops. Only use big types if we have to. */
if (base == 10) {
for (x1=0; isdigit(*s) && x1<=SIZE_MAX/10-10; s++)
x1 = 10*x1 + *s-'0';
for (x=x1; isdigit(*s) && x<=UINTMAX_MAX/10-10; s++)
x = 10*x + *s-'0';
if (isdigit(*s)) {
if (isdigit(s[1]) || 10*x>UINTMAX_MAX-(*s-'0'))
goto overflow;
x = 10*x + *s-'0';
}
} else if (!(base & base/2)) {
if (base == 16) {
if (s[0]=='0' && (s[1]|32)=='x' && digits[s[2]]<16)
s+=2;
shift=4;
z1 = SIZE_MAX/16;
z = UINTMAX_MAX/16;
} else if (base == 8) {
shift=3;
z1 = SIZE_MAX/8;
z = UINTMAX_MAX/8;
} else if (base == 2) {
shift=1;
z1 = SIZE_MAX/2;
z = UINTMAX_MAX/2;
} else if (base == 4) {
shift=2;
z1 = SIZE_MAX/4;
z = UINTMAX_MAX/4;
} else /* if (base == 32) */ {
shift=5;
z1 = SIZE_MAX/32;
z = UINTMAX_MAX/32;
}
for (x1=0; digits[*s]<base && x1<=z1; s++)
x1 = (x1<<shift) + digits[*s];
for (x=x1; digits[*s]<base && x<=z; s++)
x = (x<<shift) + digits[*s];
if (digits[*s] < base) goto overflow;
} else {
z1 = SIZE_MAX/base-base;
for (x1=0; digits[*s]<base && x1<=z1; s++)
x1 = x1*base + digits[*s];
if (digits[*s]<base)
z = UINTMAX_MAX/base-base;
for (x=x1; digits[*s]<base && x<=z; s++)
x = x*base + digits[*s];
if (digits[*s] < base) {
if (digits[s[1]]<base || x*base>UINTMAX_MAX-digits[*s])
goto overflow;
x = x*base + digits[*s];
}
}
*p = (char *)s;
return sign ? -x : x;
overflow:
for (; digits[*s] < base; s++);
*p = (char *)s;
errno = ERANGE;
return UINTMAX_MAX;
}

View file

@ -0,0 +1,25 @@
#include <wchar.h>
#include <wctype.h>
#include <inttypes.h>
#include <errno.h>
intmax_t wcstoimax(const wchar_t *s, wchar_t **p, int base)
{
int sign = 0;
uintmax_t x;
/* Initial whitespace */
for (; iswspace(*s); s++);
/* Optional sign */
if (*s == '-') sign = *s++;
else if (*s == '+') s++;
x = wcstoumax(s, p, base);
if (x > INTMAX_MAX) {
if (!sign || -x != INTMAX_MIN)
errno = ERANGE;
return sign ? INTMAX_MIN : INTMAX_MAX;
}
return sign ? -x : x;
}

View file

@ -0,0 +1,18 @@
#include <wchar.h>
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <limits.h>
long wcstol(const wchar_t *s, wchar_t **p, int base)
{
intmax_t x = wcstoimax(s, p, base);
if (x > LONG_MAX) {
errno = ERANGE;
return LONG_MAX;
} else if (x < LONG_MIN) {
errno = ERANGE;
return LONG_MIN;
}
return x;
}

View file

@ -0,0 +1,18 @@
#include <wchar.h>
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <limits.h>
long long wcstoll(const wchar_t *s, wchar_t **p, int base)
{
intmax_t x = wcstoimax(s, p, base);
if (x > LLONG_MAX) {
errno = ERANGE;
return LLONG_MAX;
} else if (x < LLONG_MIN) {
errno = ERANGE;
return LLONG_MIN;
}
return x;
}

View file

@ -0,0 +1,15 @@
#include <wchar.h>
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <limits.h>
unsigned long wcstoul(const wchar_t *s, wchar_t **p, int base)
{
uintmax_t x = wcstoumax(s, p, base);
if (x > ULONG_MAX) {
errno = ERANGE;
return ULONG_MAX;
}
return x;
}

View file

@ -0,0 +1,15 @@
#include <wchar.h>
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <limits.h>
unsigned long long wcstoull(const wchar_t *s, wchar_t **p, int base)
{
uintmax_t x = wcstoumax(s, p, base);
if (x > ULLONG_MAX) {
errno = ERANGE;
return ULLONG_MAX;
}
return x;
}

View file

@ -0,0 +1,48 @@
#include <wchar.h>
#include <wctype.h>
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
uintmax_t wcstoumax(const wchar_t *s, wchar_t **p, int base)
{
/* Large enough for largest value in binary */
char buf[sizeof(uintmax_t)*8+2];
int sign = 0, skipped=0;
if (!p) p = (wchar_t **)&s;
if (base && (unsigned)base-2 > 36-2) {
*p = (wchar_t *)s;
errno = EINVAL;
return 0;
}
/* Initial whitespace */
for (; iswspace(*s); s++);
/* Optional sign */
if (*s == '-') sign = *s++;
else if (*s == '+') s++;
/* Skip leading zeros but don't allow leading zeros before "0x". */
for (; s[0]=='0' && s[1]=='0'; s++) skipped=1;
if (skipped && (base==0 || base==16) && (s[1]|32)=='x') {
*p = (wchar_t *)(s+1);
return 0;
}
/* Convert to normal char string so we can use strtoumax */
buf[0] = sign;
if (wcstombs(buf+!!sign, s, sizeof buf-1) < 0) return 0;
buf[sizeof buf-1]=0;
/* Compute final position */
if (p) {
if ((base==0 || base==16) && s[0]=='0' && (s[1]|32)=='x' && iswxdigit(s[2])) s+=2;
for(;*s&&((unsigned)*s-'0'<base||((unsigned)*s|32)-'a'<base-10);s++);
*p = (wchar_t *)s;
}
return strtoumax(buf, 0, base);
}