This is the mail archive of the
mailing list for the glibc project.
Faster string to integer conversion
- From: Faissal Bensefia <faissaloo at me dot com>
- To: libc-alpha at sourceware dot org
- Date: Sun, 10 Apr 2016 10:52:30 +0100
- Subject: Faster string to integer conversion
- Authentication-results: sourceware.org; auth=none
The way glibc does strlen got me thinking, and I believe I've devised a
way to do something similar for string to integer conversion. Using Sean
Anderson's hasless, Juha JÃrvi's hasmore (both of which can be found
some things I came up with. I'll outline the algorithm here, I'll be
happy to explain more if necessary. In theory, this should be
significantly faster than what is being done in things such as strtol
etc in glibc:
1. We handle the first 2 characters normally to check the base.
2. If the base is 10 we don't handle it number by number as normal,
instead we continue on...
3. Instead we take a double word, we'll call this 'snippet' when we do
calculations with it
4. Increment the string pointer by double word size (so, 4).
5. Check hasless(snippet,0x30), if true, start checking for a 0
terminator and if a zero terminator is not found, nor whitespace (which
we skip) assume there was an invalid character.
6. Check hasmore(snippet,0x39), if true, assume there was an invalid
7. If neither hasless nor hasmore have returned true then xor 0x30303030
from the value.
8. We then multiply the current total for the number by 10000
9. And add:
10. Loop back to step 3
Thank you for your time,