From 17b9f68f801df2b16e8f9ef31f5510fae7cfc97e Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Tue, 8 Jan 2013 14:21:41 -0600 Subject: [PATCH] add better tolower to go with the toupper --- src/include/switch_utils.h | 73 ++++++++++++++++++++++++++++++++++++-- src/switch_utils.c | 2 +- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/src/include/switch_utils.h b/src/include/switch_utils.h index 4705069728..485d7c3495 100644 --- a/src/include/switch_utils.h +++ b/src/include/switch_utils.h @@ -42,7 +42,9 @@ SWITCH_BEGIN_EXTERN_C -/* https://code.google.com/p/stringencoders/wiki/PerformanceAscii */ +/* https://code.google.com/p/stringencoders/wiki/PerformanceAscii + http://www.azillionmonkeys.com/qed/asmexample.html +*/ static inline uint32_t switch_toupper(uint32_t eax) { uint32_t ebx = (0x7f7f7f7ful & eax) + 0x05050505ul; @@ -51,6 +53,17 @@ static inline uint32_t switch_toupper(uint32_t eax) return eax - ebx; } +/* https://code.google.com/p/stringencoders/wiki/PerformanceAscii + http://www.azillionmonkeys.com/qed/asmexample.html +*/ +static inline uint32_t switch_tolower(uint32_t eax) +{ + uint32_t ebx = (0x7f7f7f7ful & eax) + 0x25252525ul; + ebx = (0x7f7f7f7ful & ebx) + 0x1a1a1a1aul; + ebx = ((ebx & ~eax) >> 2) & 0x20202020ul; + return eax + ebx; +} + #ifdef FS_64BIT static inline void switch_toupper_max(char *s) @@ -81,6 +94,34 @@ static inline void switch_toupper_max(char *s) } +static inline void switch_tolower_max(char *s) +{ + uint64_t *b,*p; + char *c; + size_t l; + + l = strlen(s); + + p = (uint64_t *) s; + + while (l > 8) { + b = p; + *b = (uint32_t) switch_tolower(*b); + b++; + p++; + l -= 8; + } + + c = (char *)p; + + while(l > 0) { + *c = (char) switch_tolower(*c); + c++; + l--; + } + +} + #else static inline void switch_toupper_max(char *s) @@ -109,12 +150,40 @@ static inline void switch_toupper_max(char *s) l--; } +} + +static inline void switch_tolower_max(char *s) +{ + uint32_t *b,*p; + char *c; + size_t l; + + l = strlen(s); + + p = (uint32_t *) s; + + while (l > 4) { + b = p; + *b = (uint32_t) switch_tolower(*b); + b++; + p++; + l -= 4; + } + + c = (char *)p; + + while(l > 0) { + *c = (char) switch_tolower(*c); + c++; + l--; + } + } #endif SWITCH_DECLARE(int) old_switch_toupper(int c); -SWITCH_DECLARE(int) switch_tolower(int c); +SWITCH_DECLARE(int) old_switch_tolower(int c); SWITCH_DECLARE(int) switch_isalnum(int c); SWITCH_DECLARE(int) switch_isalpha(int c); SWITCH_DECLARE(int) switch_iscntrl(int c); diff --git a/src/switch_utils.c b/src/switch_utils.c index 83b77744a3..3051f156cf 100644 --- a/src/switch_utils.c +++ b/src/switch_utils.c @@ -2656,7 +2656,7 @@ const short _switch_C_tolower_[1 + SWITCH_CTYPE_NUM_CHARS] = { const short *_switch_tolower_tab_ = _switch_C_tolower_; -SWITCH_DECLARE(int) switch_tolower(int c) +SWITCH_DECLARE(int) old_switch_tolower(int c) { if ((unsigned int) c > 255) return (c);