diff options
author | Rich Felker <dalias@aerifal.cx> | 2012-04-13 04:38:56 -0400 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2012-04-13 04:38:56 -0400 |
commit | 11f3e33f9bbb31330b1dc4a51c07f8c58aef2fdd (patch) | |
tree | e5f4623ae84bb909698b1dc3208c7a8873b71cd3 | |
parent | 26832d045f46e68b79277916edbad7512d253704 (diff) | |
download | musl-11f3e33f9bbb31330b1dc4a51c07f8c58aef2fdd.tar.gz musl-11f3e33f9bbb31330b1dc4a51c07f8c58aef2fdd.tar.xz musl-11f3e33f9bbb31330b1dc4a51c07f8c58aef2fdd.zip |
use fast version of the int reading code for the high-order digits too
this increases code size slightly, but it's considerably faster, especially for power-of-2 bases.
-rw-r--r-- | src/internal/intparse.c | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/src/internal/intparse.c b/src/internal/intparse.c index e30a1a58..fba38c0a 100644 --- a/src/internal/intparse.c +++ b/src/internal/intparse.c @@ -83,9 +83,19 @@ int __intparse(struct intparse *v, const void *buf, size_t n) v->state++; v->val = v->small; case 5: - llim = UINTMAX_MAX/b; - for (; n && (d=digits[*s])<b && v->val<=llim && d<=UINTMAX_MAX-v->val*b; n--, s++) - v->val = v->val * b + d; + if (b==10) { + for (; n && *s-'0'<10U && v->val<=UINTMAX_MAX/10 && (*s-'0')<=UINTMAX_MAX-10*v->val; n--, s++) + v->val = v->val * 10 + (*s-'0'); + } else if ((b&-b) == b) { + int bs = "\0\1\2\4\7\3\6\5"[(0x17*b)>>5&7]; + llim = UINTMAX_MAX>>bs; + for (; n && (d=digits[*s])<b && v->val<=llim; n--, s++) + v->val = (v->val<<bs) + d; + } else { + llim = UINTMAX_MAX/b; + for (; n && (d=digits[*s])<b && v->val<=llim && d<=UINTMAX_MAX-b*v->val; n--, s++) + v->val = v->val * b + d; + } if (!n) return 1; if (d >= b) goto finished; v->state++; |