Added a bunch of stuff

This commit is contained in:
Gitea 2020-12-01 17:40:03 -06:00
parent 66350160b0
commit 77c2abd247
66 changed files with 1808 additions and 0 deletions

6
stdlib/abs.c Normal file
View file

@ -0,0 +1,6 @@
#include <stdlib.h>
int abs(int x) {
int y = x < 0 ? -x : x;
return y;
}

8
stdlib/div.c Normal file
View file

@ -0,0 +1,8 @@
#include <stdlib.h>
div_t div(int x, int y) {
div_t ret_val;
ret_val.quot = x / y;
ret_val.rem = x % y;
return ret_val;
}

6
stdlib/labs.c Normal file
View file

@ -0,0 +1,6 @@
#include <stdlib.h>
long int labs(long int x) {
long int y = x < 0 ? -x : x;
return y;
}

8
stdlib/ldiv.c Normal file
View file

@ -0,0 +1,8 @@
#include <stdlib.h>
ldiv_t ldiv(long int x, long int y) {
ldiv_t ret_val;
ret_val.quot = x / y;
ret_val.rem = x % y;
return ret_val;
}

6
stdlib/llabs.c Normal file
View file

@ -0,0 +1,6 @@
#include <stdlib.h>
long long int llabs(long long int x) {
long long int y = x < 0 ? -x : x;
return y;
}

8
stdlib/lldiv.c Normal file
View file

@ -0,0 +1,8 @@
#include <stdlib.h>
lldiv_t lldiv(long long int x, long long int y) {
lldiv_t ret_val;
ret_val.quot = x / y;
ret_val.rem = x % y;
return ret_val;
}

64
stdlib/rand.c Normal file
View file

@ -0,0 +1,64 @@
/*
* This rand() implementation is based on the Mersenne Twister.
* Specifically, it's a C implementation of the pseudocode
* implementation that can be found on the Wikipedia article for
* the Mersenne Twister.
*
* Did we have to do a Mersenne Twister? No. No, we did not.
* Did we? Fuck yes, we did.
*
* Love,
* Kat
*/
#include <stdlib.h>
static int _rand_state[624] = { 0 };
static unsigned int _rand_seed = 5489;
static int _rand_index = 625;
void _rand_init(unsigned int seed) {
_rand_index = 624;
_rand_state[0] = seed;
int i = 0;
for(i = 1; i < 624; i++) {
_rand_state[i] = 1812433253 ;
_rand_state[i] *= _rand_state[i-1] ^ (_rand_state[i-1] >> 30);
_rand_state[i] += i;
}
}
void _rand_twist() {
int i = 0, x, xA;
for(i = 0; i < 624; i++) {
x = _rand_state[i] & 0xFFFF0000 + (_rand_state[(i + 1) % 624] & 0xFFFF);
xA = x % 2 == 0 ? x >> 1 : (x >> 1) ^ 0x9908B0DF;
_rand_state[i] = _rand_state[(i + 397) % 624] ^ xA;
}
_rand_index = 0;
}
int rand(void) {
int y;
if(_rand_index > 624) {
_rand_init(_rand_seed);
}
if(_rand_index >= 624) {
_rand_twist();
}
y = _rand_state[_rand_index];
y ^= y >> 11;
y ^= (y << 7) & 0x9D2C5680;
y ^= (y << 15) & 0xEFC60000;
y ^= (y >> 18);
_rand_index++;
return y % RAND_MAX;
}
void srand(unsigned int seed) {
_rand_seed = seed;
_rand_index = 625;
}