xitoa: use our own int helper
authorAlan Cox <alan@linux.intel.com>
Wed, 20 May 2015 22:37:18 +0000 (23:37 +0100)
committerAlan Cox <alan@linux.intel.com>
Wed, 20 May 2015 22:37:18 +0000 (23:37 +0100)
For stdio and perror using apps it costs us a small amount of memory. For stdio
using apps in general it's free, and for non stdio apps using itoa and not
sucking in the 32bit maths helpers its a big saving.

Library/libs/xitoa.c

index a066c10..2c076f1 100644 (file)
@@ -1,10 +1,34 @@
 /*
- *     Internal wrapper for ltostr. Ought to go away
+ *     Internal wrapper (in theory) but used in various places it's helpful
  */
 #include <stdlib.h>
 
 /*********************** xitoa.c ***************************/
-char *_itoa(int i)
+
+/* Don't go via long - the long version is expensive on an 8bit processor and
+   can often be avoided */
+
+static char buf[7];
+
+char *_uitoa(int i)
 {
-    return _ltoa((long)i);
+       char *p = buf + sizeof(buf);
+       int c;
+
+       *--p = '\0';
+       do {
+               c = i % 10;
+               i /= 10;
+               *--p = '0' + c;
+       } while(i);
+       return p;
+}
+
+char *_itoa(int i) {
+       char *p;
+       if (i >= 0)
+               return _uitoa(i);
+       p = _uitoa(-i);
+       *--p = '-';
+       return p;
 }