lib: add a smaller atoi
authorAlan Cox <alan@linux.intel.com>
Sat, 13 Jun 2015 21:38:49 +0000 (22:38 +0100)
committerAlan Cox <alan@linux.intel.com>
Sat, 13 Jun 2015 21:38:49 +0000 (22:38 +0100)
Library/libs/Makefile
Library/libs/atoi_small.c [new file with mode: 0644]
Library/tools/libclean

index e9416cf..8b05386 100644 (file)
@@ -18,7 +18,7 @@ SRC_CRT0NS = crt0nostdio$(PLATFORM).s
 OBJ_CRT0NS = $(SRC_CRT0NS:.s=.rel)
 SRC_ASM = enter.s
 OBJ_ASM = $(SRC_ASM:.s=.rel)
-SRC_C =  __argv.c abort.c asctime.c assert.c atexit.c
+SRC_C =  __argv.c abort.c asctime.c assert.c atexit.c atoi_small.c
 SRC_C += bcmp.c bcopy.c bsearch.c bzero.c calloc.c cfree.c clock.c closedir.c
 SRC_C += clock_gettime.c clock_getres.c clock_settime.c
 SRC_C += creat.c crypt.c ctime.c difftime.c err.c errno.c error.c
diff --git a/Library/libs/atoi_small.c b/Library/libs/atoi_small.c
new file mode 100644 (file)
index 0000000..aaf9c36
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ *     An implementation of atoi avoiding longs and 8bit unfriendly
+ *     math
+ */
+#include <stdlib.h>
+#include <ctype.h>
+
+/**************************** atoi.c ****************************/
+int atoi(const char *str)
+{
+       int r = 0;
+       uint8_t minus = 0;
+
+       while(isspace(*str))
+               str++;
+       if (*str == '-') {
+               minus = 1;
+               str++;
+       }
+       while(*str) {
+               uint8_t c = *str++;
+               c -= '0';
+               if (c > 9)
+                       break;
+               r = ((r << 2) + r) << 1;
+               r += c;
+       }
+       return minus ? - r : r;
+}
index 1f7d793..1a9fb04 100755 (executable)
@@ -23,5 +23,5 @@ cp ${SDCC_LIB}/z80/z80.lib tmp.lib
 sdar d tmp.lib putchar.rel heap.rel fstubs.rel errno.rel
 sdar d tmp.lib rand.rel _calloc.rel _malloc.rel _realloc.rel _free.rel
 sdar d tmp.lib printf_large.rel puts.rel gets.rel assert.rel time.rel
-sdar d tmp.lib tolower.rel toupper.rel _ltoa.rel _itoa.rel abs.rel
+sdar d tmp.lib tolower.rel toupper.rel _ltoa.rel _itoa.rel abs.rel atoi.rel
 mv tmp.lib sdccz80.lib