From a2f95d50985660324812286771dbfdcf7da638a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jukka=20Jyl=C3=A4nki?= Date: Thu, 6 Feb 2014 17:07:04 -0500 Subject: [PATCH] Add optimized versions of musl libc string and memory comparison functions. --- emcc | 7 ++ src/library.js | 66 ------------------- system/lib/libc.symbols | 5 ++ system/lib/libc/musl/src/string/memcmp.c | 8 +++ system/lib/libc/musl/src/string/strcasecmp.c | 9 +++ system/lib/libc/musl/src/string/strcmp.c | 7 ++ system/lib/libc/musl/src/string/strncasecmp.c | 10 +++ system/lib/libc/musl/src/string/strncmp.c | 9 +++ tools/shared.py | 2 +- 9 files changed, 56 insertions(+), 67 deletions(-) create mode 100644 system/lib/libc/musl/src/string/memcmp.c create mode 100644 system/lib/libc/musl/src/string/strcasecmp.c create mode 100644 system/lib/libc/musl/src/string/strcmp.c create mode 100644 system/lib/libc/musl/src/string/strncasecmp.c create mode 100644 system/lib/libc/musl/src/string/strncmp.c diff --git a/emcc b/emcc index 6bf747072..a5432397e 100755 --- a/emcc +++ b/emcc @@ -1476,6 +1476,13 @@ try: ['stdlib', [ 'atof.c', 'strtod.c', + ]], + ['string', [ + 'memcmp.c', + 'strcasecmp.c', + 'strcmp.c', + 'strncasecmp.c', + 'strncmp.c', ]] ] for directory, sources in musl_files: diff --git a/src/library.js b/src/library.js index 26ce8457c..98d996ab3 100644 --- a/src/library.js +++ b/src/library.js @@ -3754,10 +3754,6 @@ LibraryManager.library = { return pdest; }, - strcmp__deps: ['strncmp'], - strcmp: function(px, py) { - return _strncmp(px, py, TOTAL_MEMORY); - }, // We always assume ASCII locale. strcoll: 'strcmp', strcoll_l__deps: ['strcoll'], @@ -3765,68 +3761,6 @@ LibraryManager.library = { return _strcoll(px, py); // no locale support yet }, - strcasecmp__asm: true, - strcasecmp__sig: 'iii', - strcasecmp__deps: ['strncasecmp'], - strcasecmp: function(px, py) { - px = px|0; py = py|0; - return _strncasecmp(px, py, -1)|0; - }, - - strncmp: function(px, py, n) { - var i = 0; - while (i < n) { - var x = {{{ makeGetValue('px', 'i', 'i8', 0, 1) }}}; - var y = {{{ makeGetValue('py', 'i', 'i8', 0, 1) }}}; - if (x == y && x == 0) return 0; - if (x == 0) return -1; - if (y == 0) return 1; - if (x == y) { - i ++; - continue; - } else { - return x > y ? 1 : -1; - } - } - return 0; - }, - - strncasecmp__asm: true, - strncasecmp__sig: 'iiii', - strncasecmp__deps: ['tolower'], - strncasecmp: function(px, py, n) { - px = px|0; py = py|0; n = n|0; - var i = 0, x = 0, y = 0; - while ((i>>>0) < (n>>>0)) { - x = _tolower({{{ makeGetValueAsm('px', 'i', 'i8', 0, 1) }}})|0; - y = _tolower({{{ makeGetValueAsm('py', 'i', 'i8', 0, 1) }}})|0; - if (((x|0) == (y|0)) & ((x|0) == 0)) return 0; - if ((x|0) == 0) return -1; - if ((y|0) == 0) return 1; - if ((x|0) == (y|0)) { - i = (i + 1)|0; - continue; - } else { - return ((x>>>0) > (y>>>0) ? 1 : -1)|0; - } - } - return 0; - }, - - memcmp__asm: true, - memcmp__sig: 'iiii', - memcmp: function(p1, p2, num) { - p1 = p1|0; p2 = p2|0; num = num|0; - var i = 0, v1 = 0, v2 = 0; - while ((i|0) < (num|0)) { - v1 = {{{ makeGetValueAsm('p1', 'i', 'i8', true) }}}; - v2 = {{{ makeGetValueAsm('p2', 'i', 'i8', true) }}}; - if ((v1|0) != (v2|0)) return ((v1|0) > (v2|0) ? 1 : -1)|0; - i = (i+1)|0; - } - return 0; - }, - memchr: function(ptr, chr, num) { chr = unSign(chr); for (var i = 0; i < num; i++) { diff --git a/system/lib/libc.symbols b/system/lib/libc.symbols index 6f80ef905..705b2c880 100644 --- a/system/lib/libc.symbols +++ b/system/lib/libc.symbols @@ -72,7 +72,12 @@ W realloc_in_place T scalbn T scalbnl + T memcpy T strtod + T strcmp + T strncmp + T strcasecmp + T strncasecmp T strtod_l T strtof T strtof_l diff --git a/system/lib/libc/musl/src/string/memcmp.c b/system/lib/libc/musl/src/string/memcmp.c new file mode 100644 index 000000000..bdbce9f0f --- /dev/null +++ b/system/lib/libc/musl/src/string/memcmp.c @@ -0,0 +1,8 @@ +#include + +int memcmp(const void *vl, const void *vr, size_t n) +{ + const unsigned char *l=vl, *r=vr; + for (; n && *l == *r; n--, l++, r++); + return n ? *l-*r : 0; +} diff --git a/system/lib/libc/musl/src/string/strcasecmp.c b/system/lib/libc/musl/src/string/strcasecmp.c new file mode 100644 index 000000000..02fd5f8ca --- /dev/null +++ b/system/lib/libc/musl/src/string/strcasecmp.c @@ -0,0 +1,9 @@ +#include +#include + +int strcasecmp(const char *_l, const char *_r) +{ + const unsigned char *l=(void *)_l, *r=(void *)_r; + for (; *l && *r && (*l == *r || tolower(*l) == tolower(*r)); l++, r++); + return tolower(*l) - tolower(*r); +} diff --git a/system/lib/libc/musl/src/string/strcmp.c b/system/lib/libc/musl/src/string/strcmp.c new file mode 100644 index 000000000..808bd8370 --- /dev/null +++ b/system/lib/libc/musl/src/string/strcmp.c @@ -0,0 +1,7 @@ +#include + +int strcmp(const char *l, const char *r) +{ + for (; *l==*r && *l; l++, r++); + return *(unsigned char *)l - *(unsigned char *)r; +} diff --git a/system/lib/libc/musl/src/string/strncasecmp.c b/system/lib/libc/musl/src/string/strncasecmp.c new file mode 100644 index 000000000..24659721d --- /dev/null +++ b/system/lib/libc/musl/src/string/strncasecmp.c @@ -0,0 +1,10 @@ +#include +#include + +int strncasecmp(const char *_l, const char *_r, size_t n) +{ + const unsigned char *l=(void *)_l, *r=(void *)_r; + if (!n--) return 0; + for (; *l && *r && n && (*l == *r || tolower(*l) == tolower(*r)); l++, r++, n--); + return tolower(*l) - tolower(*r); +} diff --git a/system/lib/libc/musl/src/string/strncmp.c b/system/lib/libc/musl/src/string/strncmp.c new file mode 100644 index 000000000..e228843f0 --- /dev/null +++ b/system/lib/libc/musl/src/string/strncmp.c @@ -0,0 +1,9 @@ +#include + +int strncmp(const char *_l, const char *_r, size_t n) +{ + const unsigned char *l=(void *)_l, *r=(void *)_r; + if (!n--) return 0; + for (; *l && *r && n && *l == *r ; l++, r++, n--); + return *l - *r; +} diff --git a/tools/shared.py b/tools/shared.py index 3b0b92a3a..dbf1e3ef4 100644 --- a/tools/shared.py +++ b/tools/shared.py @@ -345,7 +345,7 @@ def find_temp_directory(): # we re-check sanity when the settings are changed) # We also re-check sanity and clear the cache when the version changes -EMSCRIPTEN_VERSION = '1.10.1' +EMSCRIPTEN_VERSION = '1.10.2' def generate_sanity(): return EMSCRIPTEN_VERSION + '|' + get_llvm_target() + '|' + LLVM_ROOT + '|' + get_clang_version()