[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-libc-dev] optimizing strtol()/strtoul()

From: Lou Cypher
Subject: [avr-libc-dev] optimizing strtol()/strtoul()
Date: Mon, 15 Sep 2003 14:09:26 +0200

I'm using avr-libc- as enclosed with latest WinAVR.

a) I noted a couple of variables, in strtol() and strtoul(), not well 
optimized for the target, specifically:
        register int neg = 0, any;
that don't need to be 16-bit wide, infact "neg" is used as a boolean 
variable, and "any" serves as a three states flag.

Using a "char" (or signed char, wanting to keep the same usage for "any", 
that spans -1, 0, 1) would save space in program space (and possibly on 
stack), plus in execution time.

b) Another small improvement with coding is in the conversion loop:
                else if (isalpha(c))
                        c -= isupper(c) ? 'A' - 10 : 'a' - 10;
calling isupper(), after knowing it to be isalpha() makes code bigger; a 
"clean" optimization could be
        c = toupper(c) - 'A' - 10;
Yes, avoiding the ternary ?: makes code smaller, though not faster, since 
toupper() does an implicit isalpha().
The "dirty", smaller, hack is bit masking (assuming we're all using ASCII 
        c = (c & ~0x20) - 'A' - 10;
It's obvious that this optimization puts in strtol.c/strtoul.c a control 
that's to be elsewhere (ctype.h), so it is totally questionable.

I'm sure there can be other possible improvements, these ones just popped up 
to my mind, peeking at the source code.

Luca Matteini

reply via email to

[Prev in Thread] Current Thread [Next in Thread]