summaryrefslogtreecommitdiff
path: root/libpsx/src/libc/misc.c
diff options
context:
space:
mode:
authorXavi Del Campo <xavi.dcr@tutanota.com>2020-01-31 10:32:23 +0100
committerXavi Del Campo <xavi.dcr@tutanota.com>2020-01-31 10:32:23 +0100
commit7c24e9a9b02b04dcaf9507acb94091ea70a2c02d (patch)
treec28d0748652ad4b4222309e46e6cfc82c0906220 /libpsx/src/libc/misc.c
parenta2b7b6bb1cc2f4a3258b7b2dbc92399d151f864d (diff)
downloadpsxsdk-7c24e9a9b02b04dcaf9507acb94091ea70a2c02d.tar.gz
Imported pristine psxsdk-20190410 from official repo
Diffstat (limited to 'libpsx/src/libc/misc.c')
-rw-r--r--libpsx/src/libc/misc.c106
1 files changed, 106 insertions, 0 deletions
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 <stdio.h>
+#include <stdlib.h>
+
+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);
+}