From 231d55297262a10a326e0be90c60c77d106f014b Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Sun, 2 Feb 2025 13:08:55 -0500 Subject: [PATCH] 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 --- firmware/libc/include/ctype.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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__