diff options
author | William Wilgus <wilgus.william@gmail.com> | 2025-02-02 13:08:55 -0500 |
---|---|---|
committer | William Wilgus <me.theuser@yahoo.com> | 2025-02-02 13:57:58 -0500 |
commit | 231d55297262a10a326e0be90c60c77d106f014b (patch) | |
tree | c9ba1e7c349eecbbf1d983a91d0aee11838e4265 | |
parent | cf42dd6b1256a4228cb9f37a3a6bbff7954020b4 (diff) | |
download | rockbox-231d552972.tar.gz rockbox-231d552972.zip |
isdigit replace with a slightly faster and shorter conditional
in testing of three ways of doing this
current: ((_ctype_+1)[(unsigned char)(c)]&_N)
alt1(this patch): (((unsigned int) (c) - '0') < 10)
alt2: ((unsigned int)(c ^ 0x30) < 10)
alt1 and alt2 are the same in terms of speed and instructions (on arm v7) but alt2 has one more
instruction on mips
(across several archs in godbolt mips, armv7v8, x86) and on ARM7 (clipzip) device about 9% faster
less false positives for both alt1 and 2 when you start supplying more than 8bits
not sure if that matters in practice though
I tried similar with isxdigit but could only get to within 1 instruction of the ctype implementation
although it negated the array lookup I saw no discernable speed difference on device
https://godbolt.org/z/qGvh4hqnG
Change-Id: I5c9e8fd3915709853e0e33427038e20a068058b6
-rw-r--r-- | firmware/libc/include/ctype.h | 5 |
1 files changed, 4 insertions, 1 deletions
diff --git a/firmware/libc/include/ctype.h b/firmware/libc/include/ctype.h index 648e06dc5c..f5f9e13211 100644 --- a/firmware/libc/include/ctype.h +++ b/firmware/libc/include/ctype.h @@ -46,7 +46,7 @@ extern const unsigned char _ctype_[257]; #define isalpha(c) ((_ctype_+1)[(unsigned char)(c)]&(_U|_L)) #define isupper(c) ((_ctype_+1)[(unsigned char)(c)]&_U) #define islower(c) ((_ctype_+1)[(unsigned char)(c)]&_L) -#define isdigit(c) ((_ctype_+1)[(unsigned char)(c)]&_N) +/*#define isdigit(c) ((_ctype_+1)[(unsigned char)(c)]&_N)*/ #define isxdigit(c) ((_ctype_+1)[(unsigned char)(c)]&(_X|_N)) #define isspace(c) ((_ctype_+1)[(unsigned char)(c)]&_S) #define ispunct(c) ((_ctype_+1)[(unsigned char)(c)]&_P) @@ -54,6 +54,9 @@ extern const unsigned char _ctype_[257]; #define isprint(c) ((_ctype_+1)[(unsigned char)(c)]&(_P|_U|_L|_N|_B)) #define isgraph(c) ((_ctype_+1)[(unsigned char)(c)]&(_P|_U|_L|_N)) #define iscntrl(c) ((_ctype_+1)[(unsigned char)(c)]&_C) + +/* should be slightly faster without array access */ +#define isdigit(c) (((unsigned int) (c) - '0') < 10) /* Non-gcc versions will get the library versions, and will be slightly slower */ #ifdef __GNUC__ |