ysyx-workbench/abstract-machine/klib/src/stdlib.c
xinyangli 8e4feb4010 NJU-ProjectN/abstract-machine ics2023 initialized
NJU-ProjectN/abstract-machine 3348db971fd860be5cb28e21c18f9d0e65d0c96a Merge pull request #8 from Jasonyanyusong/master
2023-12-21 00:20:42 +08:00

45 lines
962 B
C

#include <am.h>
#include <klib.h>
#include <klib-macros.h>
#if !defined(__ISA_NATIVE__) || defined(__NATIVE_USE_KLIB__)
static unsigned long int next = 1;
int rand(void) {
// RAND_MAX assumed to be 32767
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}
void srand(unsigned int seed) {
next = seed;
}
int abs(int x) {
return (x < 0 ? -x : x);
}
int atoi(const char* nptr) {
int x = 0;
while (*nptr == ' ') { nptr ++; }
while (*nptr >= '0' && *nptr <= '9') {
x = x * 10 + *nptr - '0';
nptr ++;
}
return x;
}
void *malloc(size_t size) {
// On native, malloc() will be called during initializaion of C runtime.
// Therefore do not call panic() here, else it will yield a dead recursion:
// panic() -> putchar() -> (glibc) -> malloc() -> panic()
#if !(defined(__ISA_NATIVE__) && defined(__NATIVE_USE_KLIB__))
panic("Not implemented");
#endif
return NULL;
}
void free(void *ptr) {
}
#endif