Add ssse3 implementation of __memcmp16.

__memcmp16 was missing in x86. Also added C-version for backward
compatibility. Added bionic test for __memcmp16 and for wmemcmp.

Change-Id: I33718441e7ee343cdb021d91dbeaf9ce2d4d7eb4
Signed-off-by: Alexander Ivchenko <alexander.ivchenko@intel.com>
diff --git a/tests/string_test.cpp b/tests/string_test.cpp
index 91d1904..ef43f5d 100644
--- a/tests/string_test.cpp
+++ b/tests/string_test.cpp
@@ -131,14 +131,15 @@
 // expected result and then run function and compare what we got.
 // These tests contributed by Intel Corporation.
 // TODO: make these tests more intention-revealing and less random.
+template<class Character>
 struct StringTestState {
   StringTestState(size_t MAX_LEN) : MAX_LEN(MAX_LEN) {
     int max_alignment = 64;
 
     // TODO: fix the tests to not sometimes use twice their specified "MAX_LEN".
-    glob_ptr = reinterpret_cast<char*>(valloc(2 * MAX_LEN + max_alignment));
-    glob_ptr1 = reinterpret_cast<char*>(valloc(2 * MAX_LEN + max_alignment));
-    glob_ptr2 = reinterpret_cast<char*>(valloc(2 * MAX_LEN + max_alignment));
+    glob_ptr = reinterpret_cast<Character*>(valloc(2 * sizeof(Character) * MAX_LEN + max_alignment));
+    glob_ptr1 = reinterpret_cast<Character*>(valloc(2 * sizeof(Character) * MAX_LEN + max_alignment));
+    glob_ptr2 = reinterpret_cast<Character*>(valloc(2 * sizeof(Character) * MAX_LEN + max_alignment));
 
     InitLenArray();
 
@@ -163,12 +164,12 @@
   }
 
   const size_t MAX_LEN;
-  char *ptr, *ptr1, *ptr2;
+  Character *ptr, *ptr1, *ptr2;
   size_t n;
   int len[ITER + 1];
 
  private:
-  char *glob_ptr, *glob_ptr1, *glob_ptr2;
+  Character *glob_ptr, *glob_ptr1, *glob_ptr2;
 
   // Calculate input lengths and fill state.len with them.
   // Test small lengths with more density than big ones. Manually push
@@ -188,7 +189,7 @@
 };
 
 TEST(string, strcat) {
-  StringTestState state(SMALL);
+  StringTestState<char> state(SMALL);
   for (size_t i = 1; i < state.n; i++) {
     for (size_t j = 0; j < POS_ITER; j++) {
       state.NewIteration();
@@ -380,7 +381,7 @@
 TEST(string, strchr) {
   int seek_char = random() & 255;
 
-  StringTestState state(SMALL);
+  StringTestState<char> state(SMALL);
   for (size_t i = 1; i < state.n; i++) {
     for (size_t j = 0; j < POS_ITER; j++) {
       state.NewIteration();
@@ -411,7 +412,7 @@
 }
 
 TEST(string, strcmp) {
-  StringTestState state(SMALL);
+  StringTestState<char> state(SMALL);
   for (size_t i = 1; i < state.n; i++) {
     for (size_t j = 0; j < POS_ITER; j++) {
       state.NewIteration();
@@ -448,7 +449,7 @@
 }
 
 TEST(string, strcpy) {
-  StringTestState state(SMALL);
+  StringTestState<char> state(SMALL);
   for (size_t j = 0; j < POS_ITER; j++) {
     state.NewIteration();
 
@@ -476,7 +477,7 @@
 
 #if __BIONIC__
 TEST(string, strlcat) {
-  StringTestState state(SMALL);
+  StringTestState<char> state(SMALL);
   for (size_t i = 0; i < state.n; i++) {
     for (size_t j = 0; j < POS_ITER; j++) {
       state.NewIteration();
@@ -505,7 +506,7 @@
 
 #if __BIONIC__
 TEST(string, strlcpy) {
-  StringTestState state(SMALL);
+  StringTestState<char> state(SMALL);
   for (size_t j = 0; j < POS_ITER; j++) {
     state.NewIteration();
 
@@ -539,7 +540,7 @@
 #endif
 
 TEST(string, strncat) {
-  StringTestState state(SMALL);
+  StringTestState<char> state(SMALL);
   for (size_t i = 1; i < state.n; i++) {
     for (size_t j = 0; j < POS_ITER; j++) {
       state.NewIteration();
@@ -565,7 +566,7 @@
 }
 
 TEST(string, strncmp) {
-  StringTestState state(SMALL);
+  StringTestState<char> state(SMALL);
   for (size_t i = 1; i < state.n; i++) {
     for (size_t j = 0; j < POS_ITER; j++) {
       state.NewIteration();
@@ -602,7 +603,7 @@
 }
 
 TEST(string, strncpy) {
-  StringTestState state(SMALL);
+  StringTestState<char> state(SMALL);
   for (size_t j = 0; j < ITER; j++) {
     state.NewIteration();
 
@@ -630,7 +631,7 @@
 
 TEST(string, strrchr) {
   int seek_char = random() & 255;
-  StringTestState state(SMALL);
+  StringTestState<char> state(SMALL);
   for (size_t i = 1; i < state.n; i++) {
     for (size_t j = 0; j < POS_ITER; j++) {
       state.NewIteration();
@@ -662,7 +663,7 @@
 
 TEST(string, memchr) {
   int seek_char = random() & 255;
-  StringTestState state(SMALL);
+  StringTestState<char> state(SMALL);
   for (size_t i = 0; i < state.n; i++) {
     for (size_t j = 0; j < POS_ITER; j++) {
       state.NewIteration();
@@ -685,7 +686,7 @@
 
 TEST(string, memrchr) {
   int seek_char = random() & 255;
-  StringTestState state(SMALL);
+  StringTestState<char> state(SMALL);
   for (size_t i = 0; i < state.n; i++) {
     for (size_t j = 0; j < POS_ITER; j++) {
       state.NewIteration();
@@ -707,7 +708,7 @@
 }
 
 TEST(string, memcmp) {
-  StringTestState state(SMALL);
+  StringTestState<char> state(SMALL);
   for (size_t i = 0; i < state.n; i++) {
     for (size_t j = 0; j < POS_ITER; j++) {
       state.NewIteration();
@@ -728,8 +729,61 @@
   }
 }
 
+#if defined(__BIONIC__)
+extern "C" int __memcmp16(const unsigned short *ptr1, const unsigned short *ptr2, size_t n);
+
+TEST(string, __memcmp16) {
+  StringTestState<unsigned short> state(SMALL);
+
+  for (size_t i = 0; i < state.n; i++) {
+    for (size_t j = 0; j < POS_ITER; j++) {
+      state.NewIteration();
+
+      unsigned short mask = 0xffff;
+      unsigned short c1 = rand() & mask;
+      unsigned short c2 = rand() & mask;
+
+      std::fill(state.ptr1, state.ptr1 + state.MAX_LEN, c1);
+      std::fill(state.ptr2, state.ptr2 + state.MAX_LEN, c1);
+
+      int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]);
+      state.ptr2[pos] = c2;
+
+      int expected = (static_cast<unsigned short>(c1) - static_cast<unsigned short>(c2));
+      int actual = __memcmp16(state.ptr1, state.ptr2, (size_t) state.MAX_LEN);
+
+      ASSERT_EQ(expected, actual);
+    }
+  }
+}
+#endif
+
+TEST(string, wmemcmp) {
+  StringTestState<wchar_t> state(SMALL);
+
+  for (size_t i = 0; i < state.n; i++) {
+    for (size_t j = 0; j < POS_ITER; j++) {
+      state.NewIteration();
+
+      long long mask = ((long long) 1 << 8 * sizeof(wchar_t)) - 1;
+      int c1 = rand() & mask;
+      int c2 = rand() & mask;
+      wmemset(state.ptr1, c1, state.MAX_LEN);
+      wmemset(state.ptr2, c1, state.MAX_LEN);
+
+      int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]);
+      state.ptr2[pos] = c2;
+
+      int expected = (static_cast<int>(c1) - static_cast<int>(c2));
+      int actual = wmemcmp(state.ptr1, state.ptr2, (size_t) state.MAX_LEN);
+
+      ASSERT_EQ(signum(expected), signum(actual));
+    }
+  }
+}
+
 TEST(string, memcpy) {
-  StringTestState state(LARGE);
+  StringTestState<char> state(LARGE);
   int rand = random() & 255;
   for (size_t i = 0; i < state.n - 1; i++) {
     for (size_t j = 0; j < POS_ITER; j++) {
@@ -751,7 +805,7 @@
 }
 
 TEST(string, memset) {
-  StringTestState state(LARGE);
+  StringTestState<char> state(LARGE);
   char ch = random () & 255;
   for (size_t i = 0; i < state.n - 1; i++) {
     for (size_t j = 0; j < POS_ITER; j++) {
@@ -773,7 +827,7 @@
 }
 
 TEST(string, memmove) {
-  StringTestState state(LARGE);
+  StringTestState<char> state(LARGE);
   for (size_t i = 0; i < state.n - 1; i++) {
     for (size_t j = 0; j < POS_ITER; j++) {
       state.NewIteration();
@@ -794,7 +848,7 @@
 }
 
 TEST(string, bcopy) {
-  StringTestState state(LARGE);
+  StringTestState<char> state(LARGE);
   for (size_t i = 0; i < state.n; i++) {
     for (size_t j = 0; j < POS_ITER; j++) {
       state.NewIteration();
@@ -813,7 +867,7 @@
 }
 
 TEST(string, bzero) {
-  StringTestState state(LARGE);
+  StringTestState<char> state(LARGE);
   for (size_t j = 0; j < ITER; j++) {
     state.NewIteration();