public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Faissal Bensefia <faissaloo@me.com>
To: libc-alpha@sourceware.org
Subject: Faster string to integer conversion
Date: Sun, 10 Apr 2016 09:52:00 -0000	[thread overview]
Message-ID: <570A225E.20409@me.com> (raw)

Good day,
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
here:
https://graphics.stanford.edu/~seander/bithacks.html#HasLessInWord) and
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
character.
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:
snippet&0xFF
((snippet>>8)&0xFF)*10
((snippet>>16)&0xFF)*100
((snippet>>24)&0xFF)*1000
10. Loop back to step 3

Thank you for your time,
~Faissal Bensefia

             reply	other threads:[~2016-04-10  9:52 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-10  9:52 Faissal Bensefia [this message]
2016-04-10 19:54 ` Paul Eggert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=570A225E.20409@me.com \
    --to=faissaloo@me.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).