x86: trivial printk optimizations
In arch/x86/boot/printf.c gets rid of unused tail of digits: const char *digits = "0123456789abcdefghijklmnopqrstuvwxyz"; (we are using 0-9a-f only) Uses smaller/faster lowercasing (by ORing with 0x20) if we know that we work on numbers/digits. Makes strtoul smaller, and also we are getting rid of static const char small_digits[] = "0123456789abcdefx"; static const char large_digits[] = "0123456789ABCDEFX"; since this works equally well: static const char digits[16] = "0123456789ABCDEF"; Size savings: $ size vmlinux.org vmlinux text data bss dec hex filename 877320 112252 90112 1079684 107984 vmlinux.org 877048 112252 90112 1079412 107874 vmlinux It may be also a tiny bit faster because code has less branches now, but I doubt it is measurable. [ hugh@veritas.com: uppercase pointers fix ] Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com> Cc: Andi Kleen <ak@suse.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
This commit is contained in:
parent
b6fbb669c8
commit
9b706aee7d
2 changed files with 41 additions and 32 deletions
|
@ -33,8 +33,8 @@ static int skip_atoi(const char **s)
|
||||||
#define PLUS 4 /* show plus */
|
#define PLUS 4 /* show plus */
|
||||||
#define SPACE 8 /* space if plus */
|
#define SPACE 8 /* space if plus */
|
||||||
#define LEFT 16 /* left justified */
|
#define LEFT 16 /* left justified */
|
||||||
#define SPECIAL 32 /* 0x */
|
#define SMALL 32 /* Must be 32 == 0x20 */
|
||||||
#define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
|
#define SPECIAL 64 /* 0x */
|
||||||
|
|
||||||
#define do_div(n,base) ({ \
|
#define do_div(n,base) ({ \
|
||||||
int __res; \
|
int __res; \
|
||||||
|
@ -45,12 +45,16 @@ __res; })
|
||||||
static char *number(char *str, long num, int base, int size, int precision,
|
static char *number(char *str, long num, int base, int size, int precision,
|
||||||
int type)
|
int type)
|
||||||
{
|
{
|
||||||
char c, sign, tmp[66];
|
/* we are called with base 8, 10 or 16, only, thus don't need "G..." */
|
||||||
const char *digits = "0123456789abcdefghijklmnopqrstuvwxyz";
|
static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
|
||||||
|
|
||||||
|
char tmp[66];
|
||||||
|
char c, sign, locase;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (type & LARGE)
|
/* locase = 0 or 0x20. ORing digits or letters with 'locase'
|
||||||
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
* produces same digits or (maybe lowercased) letters */
|
||||||
|
locase = (type & SMALL);
|
||||||
if (type & LEFT)
|
if (type & LEFT)
|
||||||
type &= ~ZEROPAD;
|
type &= ~ZEROPAD;
|
||||||
if (base < 2 || base > 36)
|
if (base < 2 || base > 36)
|
||||||
|
@ -81,7 +85,7 @@ static char *number(char *str, long num, int base, int size, int precision,
|
||||||
tmp[i++] = '0';
|
tmp[i++] = '0';
|
||||||
else
|
else
|
||||||
while (num != 0)
|
while (num != 0)
|
||||||
tmp[i++] = digits[do_div(num, base)];
|
tmp[i++] = (digits[do_div(num, base)] | locase);
|
||||||
if (i > precision)
|
if (i > precision)
|
||||||
precision = i;
|
precision = i;
|
||||||
size -= precision;
|
size -= precision;
|
||||||
|
@ -95,7 +99,7 @@ static char *number(char *str, long num, int base, int size, int precision,
|
||||||
*str++ = '0';
|
*str++ = '0';
|
||||||
else if (base == 16) {
|
else if (base == 16) {
|
||||||
*str++ = '0';
|
*str++ = '0';
|
||||||
*str++ = digits[33];
|
*str++ = ('X' | locase);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!(type & LEFT))
|
if (!(type & LEFT))
|
||||||
|
@ -244,9 +248,9 @@ int vsprintf(char *buf, const char *fmt, va_list args)
|
||||||
base = 8;
|
base = 8;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'X':
|
|
||||||
flags |= LARGE;
|
|
||||||
case 'x':
|
case 'x':
|
||||||
|
flags |= SMALL;
|
||||||
|
case 'X':
|
||||||
base = 16;
|
base = 16;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,9 @@
|
||||||
#include <asm/page.h> /* for PAGE_SIZE */
|
#include <asm/page.h> /* for PAGE_SIZE */
|
||||||
#include <asm/div64.h>
|
#include <asm/div64.h>
|
||||||
|
|
||||||
|
/* Works only for digits and letters, but small and fast */
|
||||||
|
#define TOLOWER(x) ((x) | 0x20)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* simple_strtoul - convert a string to an unsigned long
|
* simple_strtoul - convert a string to an unsigned long
|
||||||
* @cp: The start of the string
|
* @cp: The start of the string
|
||||||
|
@ -41,17 +44,17 @@ unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
|
||||||
if (*cp == '0') {
|
if (*cp == '0') {
|
||||||
base = 8;
|
base = 8;
|
||||||
cp++;
|
cp++;
|
||||||
if ((toupper(*cp) == 'X') && isxdigit(cp[1])) {
|
if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
|
||||||
cp++;
|
cp++;
|
||||||
base = 16;
|
base = 16;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (base == 16) {
|
} else if (base == 16) {
|
||||||
if (cp[0] == '0' && toupper(cp[1]) == 'X')
|
if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
|
||||||
cp += 2;
|
cp += 2;
|
||||||
}
|
}
|
||||||
while (isxdigit(*cp) &&
|
while (isxdigit(*cp) &&
|
||||||
(value = isdigit(*cp) ? *cp-'0' : toupper(*cp)-'A'+10) < base) {
|
(value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
|
||||||
result = result*base + value;
|
result = result*base + value;
|
||||||
cp++;
|
cp++;
|
||||||
}
|
}
|
||||||
|
@ -92,17 +95,17 @@ unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
|
||||||
if (*cp == '0') {
|
if (*cp == '0') {
|
||||||
base = 8;
|
base = 8;
|
||||||
cp++;
|
cp++;
|
||||||
if ((toupper(*cp) == 'X') && isxdigit(cp[1])) {
|
if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
|
||||||
cp++;
|
cp++;
|
||||||
base = 16;
|
base = 16;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (base == 16) {
|
} else if (base == 16) {
|
||||||
if (cp[0] == '0' && toupper(cp[1]) == 'X')
|
if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
|
||||||
cp += 2;
|
cp += 2;
|
||||||
}
|
}
|
||||||
while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
|
while (isxdigit(*cp)
|
||||||
? toupper(*cp) : *cp)-'A'+10) < base) {
|
&& (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
|
||||||
result = result*base + value;
|
result = result*base + value;
|
||||||
cp++;
|
cp++;
|
||||||
}
|
}
|
||||||
|
@ -360,24 +363,25 @@ static noinline char* put_dec(char *buf, unsigned long long num)
|
||||||
#define PLUS 4 /* show plus */
|
#define PLUS 4 /* show plus */
|
||||||
#define SPACE 8 /* space if plus */
|
#define SPACE 8 /* space if plus */
|
||||||
#define LEFT 16 /* left justified */
|
#define LEFT 16 /* left justified */
|
||||||
#define SPECIAL 32 /* 0x */
|
#define SMALL 32 /* Must be 32 == 0x20 */
|
||||||
#define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
|
#define SPECIAL 64 /* 0x */
|
||||||
|
|
||||||
static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
|
static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
|
||||||
{
|
{
|
||||||
char sign,tmp[66];
|
/* we are called with base 8, 10 or 16, only, thus don't need "G..." */
|
||||||
const char *digits;
|
static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
|
||||||
/* we are called with base 8, 10 or 16, only, thus don't need "g..." */
|
|
||||||
static const char small_digits[] = "0123456789abcdefx"; /* "ghijklmnopqrstuvwxyz"; */
|
char tmp[66];
|
||||||
static const char large_digits[] = "0123456789ABCDEFX"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
|
char sign;
|
||||||
|
char locase;
|
||||||
int need_pfx = ((type & SPECIAL) && base != 10);
|
int need_pfx = ((type & SPECIAL) && base != 10);
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
digits = (type & LARGE) ? large_digits : small_digits;
|
/* locase = 0 or 0x20. ORing digits or letters with 'locase'
|
||||||
|
* produces same digits or (maybe lowercased) letters */
|
||||||
|
locase = (type & SMALL);
|
||||||
if (type & LEFT)
|
if (type & LEFT)
|
||||||
type &= ~ZEROPAD;
|
type &= ~ZEROPAD;
|
||||||
if (base < 2 || base > 36)
|
|
||||||
return NULL;
|
|
||||||
sign = 0;
|
sign = 0;
|
||||||
if (type & SIGN) {
|
if (type & SIGN) {
|
||||||
if ((signed long long) num < 0) {
|
if ((signed long long) num < 0) {
|
||||||
|
@ -404,7 +408,7 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int
|
||||||
tmp[i++] = '0';
|
tmp[i++] = '0';
|
||||||
/* Generic code, for any base:
|
/* Generic code, for any base:
|
||||||
else do {
|
else do {
|
||||||
tmp[i++] = digits[do_div(num,base)];
|
tmp[i++] = (digits[do_div(num,base)] | locase);
|
||||||
} while (num != 0);
|
} while (num != 0);
|
||||||
*/
|
*/
|
||||||
else if (base != 10) { /* 8 or 16 */
|
else if (base != 10) { /* 8 or 16 */
|
||||||
|
@ -412,7 +416,7 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int
|
||||||
int shift = 3;
|
int shift = 3;
|
||||||
if (base == 16) shift = 4;
|
if (base == 16) shift = 4;
|
||||||
do {
|
do {
|
||||||
tmp[i++] = digits[((unsigned char)num) & mask];
|
tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
|
||||||
num >>= shift;
|
num >>= shift;
|
||||||
} while (num);
|
} while (num);
|
||||||
} else { /* base 10 */
|
} else { /* base 10 */
|
||||||
|
@ -444,7 +448,7 @@ static char *number(char *buf, char *end, unsigned long long num, int base, int
|
||||||
++buf;
|
++buf;
|
||||||
if (base == 16) {
|
if (base == 16) {
|
||||||
if (buf < end)
|
if (buf < end)
|
||||||
*buf = digits[16]; /* for arbitrary base: digits[33]; */
|
*buf = ('X' | locase);
|
||||||
++buf;
|
++buf;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -644,6 +648,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
case 'p':
|
case 'p':
|
||||||
|
flags |= SMALL;
|
||||||
if (field_width == -1) {
|
if (field_width == -1) {
|
||||||
field_width = 2*sizeof(void *);
|
field_width = 2*sizeof(void *);
|
||||||
flags |= ZEROPAD;
|
flags |= ZEROPAD;
|
||||||
|
@ -680,9 +685,9 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
|
||||||
base = 8;
|
base = 8;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'X':
|
|
||||||
flags |= LARGE;
|
|
||||||
case 'x':
|
case 'x':
|
||||||
|
flags |= SMALL;
|
||||||
|
case 'X':
|
||||||
base = 16;
|
base = 16;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue