1
0
Fork 0
forked from len0rd/rockbox

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
This commit is contained in:
William Wilgus 2025-02-02 13:08:55 -05:00 committed by William Wilgus
parent cf42dd6b12
commit 231d552972

View file

@ -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__