This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH][RFC] Avoid table lookup in isascii, isxdigit.
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: libc-alpha at sourceware dot org
- Date: Tue, 9 Apr 2013 13:57:06 +0200
- Subject: [PATCH][RFC] Avoid table lookup in isascii, isxdigit.
Hi, I looked to ctype and thougth if it is possible to decrease table
size to 512 bytes. I did not succeed but made following three
optimizations.
1. In isdigit use & instead &&. Gcc contains bug that optimizes first
but not second case.
2. Refactor isdigit macro
3. Write isxdigit and isascii.
Following code is enclosed in
#ifndef _ISOMAC
# if !defined __NO_CTYPE && !defined NOT_IN_libc
Is there reason why we cannot also transform user code in this way?
---
include/ctype.h | 23 ++++++++++++++++++++---
1 files changed, 20 insertions(+), 3 deletions(-)
diff --git a/include/ctype.h b/include/ctype.h
index 6a18039..7f9e928 100644
--- a/include/ctype.h
+++ b/include/ctype.h
@@ -56,11 +56,28 @@ __ctype_tolower_loc (void)
/* The spec says that isdigit must only match the decimal digits. We
can check this without a memory access. */
# undef isdigit
-# define isdigit(c) ({ int __c = (c); __c >= '0' && __c <= '9'; })
+# define isdigit(c) ({ int __c = (c); (__c >= '0') & (__c <= '9'); })
# undef isdigit_l
-# define isdigit_l(c, l) ({ int __c = (c); __c >= '0' && __c <= '9'; })
+# define isdigit_l(c, l) isdigit(c)
# undef __isdigit_l
-# define __isdigit_l(c, l) ({ int __c = (c); __c >= '0' && __c <= '9'; })
+# define __isdigit_l(c, l) isdigit(c)
+
+/* The spec says that isxdigit must only match the hexadecimal digits. We
+ can check this without a memory access. */
+# undef isxdigit
+# define isxdigit(c) ({ int __c = (c); ((__c >= '0') & (__c <= '9')) || ((__c >= 'a') & (__c <= 'f')) || ((__c >= 'A') & (__c <= 'F')); })
+# undef isxdigit_l
+# define isxdigit_l(c, l) isxdigit(c)
+# undef __isxdigit_l
+# define __isxdigit_l(c, l) isxdigit(c)
+
+# undef isascii
+# define isascii(c) ({ int __c = (c); (__c & 128 == 0); })
+# undef isascii_l
+# define isascii_l(c, l) isascii(c)
+# undef __isascii_l
+# define __isascii_l(c, l) isascii(c)
+
# endif
#endif
--
1.7.4.4