From 3685977f307931b8e9a9c646e87ee91c3b52170a Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Sat, 13 Jun 2015 22:38:49 +0100 Subject: [PATCH] lib: add a smaller atoi --- Library/libs/Makefile | 2 +- Library/libs/atoi_small.c | 29 +++++++++++++++++++++++++++++ Library/tools/libclean | 2 +- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 Library/libs/atoi_small.c diff --git a/Library/libs/Makefile b/Library/libs/Makefile index e9416cf7..8b053867 100644 --- a/Library/libs/Makefile +++ b/Library/libs/Makefile @@ -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 index 00000000..aaf9c369 --- /dev/null +++ b/Library/libs/atoi_small.c @@ -0,0 +1,29 @@ +/* + * An implementation of atoi avoiding longs and 8bit unfriendly + * math + */ +#include +#include + +/**************************** 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; +} diff --git a/Library/tools/libclean b/Library/tools/libclean index 1f7d7931..1a9fb044 100755 --- a/Library/tools/libclean +++ b/Library/tools/libclean @@ -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 -- 2.34.1