From 7c24e9a9b02b04dcaf9507acb94091ea70a2c02d Mon Sep 17 00:00:00 2001 From: Xavi Del Campo Date: Fri, 31 Jan 2020 10:32:23 +0100 Subject: Imported pristine psxsdk-20190410 from official repo --- libpsx/src/libc/misc.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 libpsx/src/libc/misc.c (limited to 'libpsx/src/libc/misc.c') diff --git a/libpsx/src/libc/misc.c b/libpsx/src/libc/misc.c new file mode 100644 index 0000000..f3a1326 --- /dev/null +++ b/libpsx/src/libc/misc.c @@ -0,0 +1,106 @@ +#include +#include + +static unsigned int rand_seed = 0; +static unsigned int rand_next = 0; + +int abs(int x) +{ + if(x<0)return -x; + + return x; +} + +void srand(unsigned int seed) +{ + rand_seed = seed; +} + +int rand() +{ + rand_next = (rand_next ^ 0x98765432)*0x1357; + + return rand_next % RAND_MAX; +} + +static char *__ulltoa_internal__(unsigned long long value, char *str, int base, int minus_sign, + unsigned long long maxp ) +{ + unsigned long long p; + unsigned long long p3; + int c; + int a; + + p = 1; + + do + { + p3 = p; + p *= base; + + if(maxp && p > maxp) + break; + }while((p >= p3) && !(p % p3)); + + if(minus_sign) + *(str++) = '-'; + + for(a = 0;p3 > 0;p3/=base) + { + c = value / p3; + value %= p3; + + if(c) + a = 1; + + if(a) + { + if(c <= 9) + c += '0'; + else + c = (c - 10) + 'A'; + + *(str++) = c; + } + } + + *str = '\0'; + + return str; +} + +char *ulltoa(unsigned long long value, char *str, int base) +{ + return __ulltoa_internal__(value, str, base, 0, 0); +} + +char *ultoa(unsigned long value, char *str, int base) +{ + return __ulltoa_internal__(value, str, base, 0, (sizeof(long)==8)?0:0xFFFFFFFF); +} + +char *utoa(unsigned int value, char *str, int base) +{ + return __ulltoa_internal__(value, str, base, 0, 0xFFFFFFFF); +} + +char *lltoa(long long value, char *str, int base) +{ + return __ulltoa_internal__((value<0)?-value:value, str, base, value<0, 0); +} + +char *ltoa(long value, char *str, int base) +{ + return __ulltoa_internal__((value<0)?-value:value, str, base, value<0, (sizeof(long)==8)?0:0xFFFFFFFF); +} + +char *itoa(int value, char *str, int base) +{ + return __ulltoa_internal__((value<0)?-value:value, str, base, value<0, 0xFFFFFFFF); +} + +void abort(void) +{ + printf("abort(): Abnormal program termination\n"); + exit(1); +} -- cgit v1.2.3