Index: libc/src/string/aarch64/memcmp.cpp =================================================================== --- libc/src/string/aarch64/memcmp.cpp +++ libc/src/string/aarch64/memcmp.cpp @@ -45,8 +45,8 @@ LLVM_LIBC_FUNCTION(int, memcmp, (const void *lhs, const void *rhs, size_t count)) { - return memcmp_aarch64(reinterpret_cast(lhs), - reinterpret_cast(rhs), count); + return memcmp_aarch64(static_cast(lhs), + static_cast(rhs), count); } } // namespace __llvm_libc Index: libc/src/string/aarch64/memcpy.cpp =================================================================== --- libc/src/string/aarch64/memcpy.cpp +++ libc/src/string/aarch64/memcpy.cpp @@ -69,8 +69,8 @@ LLVM_LIBC_FUNCTION(void *, memcpy, (void *__restrict dst, const void *__restrict src, size_t size)) { - memcpy_aarch64(reinterpret_cast(dst), - reinterpret_cast(src), size); + memcpy_aarch64(static_cast(dst), static_cast(src), + size); return dst; } Index: libc/src/string/bzero.cpp =================================================================== --- libc/src/string/bzero.cpp +++ libc/src/string/bzero.cpp @@ -13,7 +13,7 @@ namespace __llvm_libc { LLVM_LIBC_FUNCTION(void, bzero, (void *ptr, size_t count)) { - GeneralPurposeMemset(reinterpret_cast(ptr), 0, count); + GeneralPurposeMemset(static_cast(ptr), 0, count); } } // namespace __llvm_libc Index: libc/src/string/memchr.cpp =================================================================== --- libc/src/string/memchr.cpp +++ libc/src/string/memchr.cpp @@ -16,8 +16,8 @@ // TODO: Look at performance benefits of comparing words. LLVM_LIBC_FUNCTION(void *, memchr, (const void *src, int c, size_t n)) { - return internal::find_first_character( - reinterpret_cast(src), c, n); + return internal::find_first_character(static_cast(src), + static_cast(c), n); } } // namespace __llvm_libc Index: libc/src/string/memcmp.cpp =================================================================== --- libc/src/string/memcmp.cpp +++ libc/src/string/memcmp.cpp @@ -15,8 +15,8 @@ // TODO: It is a simple implementation, an optimized version is preparing. LLVM_LIBC_FUNCTION(int, memcmp, (const void *lhs, const void *rhs, size_t count)) { - const unsigned char *_lhs = reinterpret_cast(lhs); - const unsigned char *_rhs = reinterpret_cast(rhs); + const unsigned char *_lhs = static_cast(lhs); + const unsigned char *_rhs = static_cast(rhs); for (size_t i = 0; i < count; ++i) if (_lhs[i] != _rhs[i]) return _lhs[i] - _rhs[i]; Index: libc/src/string/memcpy.cpp =================================================================== --- libc/src/string/memcpy.cpp +++ libc/src/string/memcpy.cpp @@ -61,8 +61,7 @@ LLVM_LIBC_FUNCTION(void *, memcpy, (void *__restrict dst, const void *__restrict src, size_t size)) { - memcpy_impl(reinterpret_cast(dst), - reinterpret_cast(src), size); + memcpy_impl(static_cast(dst), static_cast(src), size); return dst; } Index: libc/src/string/memmove.cpp =================================================================== --- libc/src/string/memmove.cpp +++ libc/src/string/memmove.cpp @@ -15,22 +15,23 @@ namespace __llvm_libc { -static inline void move_byte_forward(char *dest_m, const char *src_m, - size_t count) { - for (size_t offset = 0; count; --count, ++offset) +static inline void move_byte_forward(unsigned char *dest_m, + const unsigned char *src_m, size_t count) { + for (size_t offset = 0; offset != count; ++offset) dest_m[offset] = src_m[offset]; } -static inline void move_byte_backward(char *dest_m, const char *src_m, +static inline void move_byte_backward(unsigned char *dest_m, + const unsigned char *src_m, size_t count) { - for (size_t offset = count - 1; count; --count, --offset) - dest_m[offset] = src_m[offset]; + for (size_t offset = count; offset != 0; --offset) + dest_m[offset - 1] = src_m[offset - 1]; } LLVM_LIBC_FUNCTION(void *, memmove, (void *dest, const void *src, size_t count)) { - char *dest_c = reinterpret_cast(dest); - const char *src_c = reinterpret_cast(src); + unsigned char *dest_c = static_cast(dest); + const unsigned char *src_c = static_cast(src); // If the distance between src_c and dest_c is equal to or greater // than count (integerAbs(src_c - dest_c) >= count), they would not overlap. @@ -41,15 +42,15 @@ // Use memcpy if src_c and dest_c do not overlap. if (__llvm_libc::integerAbs(src_c - dest_c) >= static_cast(count)) - return __llvm_libc::memcpy(dest_c, src_c, count); + return __llvm_libc::memcpy(dest, src, count); // Overlap cases. // If dest_c starts before src_c (dest_c < src_c), copy forward(pointer add 1) // from beginning to end. // If dest_c starts after src_c (dest_c > src_c), copy backward(pointer add // -1) from end to beginning. - // If dest_c and src_c start at the same address (dest_c == src_c), - // just return dest. + // It is extremely unlikely that dest and src are equal, and even if they + // were, nothing of consequence would happen // e.g. forward backward // *--> <--* // src_c : [___abcde_] [_abcde___] @@ -58,7 +59,7 @@ // TODO: Optimize `move_byte_xxx(...)` functions. if (dest_c < src_c) move_byte_forward(dest_c, src_c, count); - if (dest_c > src_c) + else move_byte_backward(dest_c, src_c, count); return dest; } Index: libc/src/string/memory_utils/elements.h =================================================================== --- libc/src/string/memory_utils/elements.h +++ libc/src/string/memory_utils/elements.h @@ -96,9 +96,8 @@ for (size_t i = 0; i < ElementCount; ++i) { const size_t offset = i * Element::kSize; // We make the assumption that 'Equals' si cheaper than 'ThreeWayCompare'. - if (Element::Equals(lhs + offset, rhs + offset)) - continue; - return Element::ThreeWayCompare(lhs + offset, rhs + offset); + if (!Element::Equals(lhs + offset, rhs + offset)) + return Element::ThreeWayCompare(lhs + offset, rhs + offset); } return 0; } @@ -131,10 +130,10 @@ } static int ThreeWayCompare(const char *lhs, const char *rhs) { - if (__llvm_libc::Equals(lhs, rhs)) - return Chained::ThreeWayCompare(lhs + Head::kSize, - rhs + Head::kSize); - return __llvm_libc::ThreeWayCompare(lhs, rhs); + if (!__llvm_libc::Equals(lhs, rhs)) + return __llvm_libc::ThreeWayCompare(lhs, rhs); + return Chained::ThreeWayCompare(lhs + Head::kSize, + rhs + Head::kSize); } static void SplatSet(char *dst, const unsigned char value) { Index: libc/src/string/memrchr.cpp =================================================================== --- libc/src/string/memrchr.cpp +++ libc/src/string/memrchr.cpp @@ -13,12 +13,11 @@ namespace __llvm_libc { LLVM_LIBC_FUNCTION(void *, memrchr, (const void *src, int c, size_t n)) { - const unsigned char *str = reinterpret_cast(src); - const unsigned char ch = c; + const unsigned char *str = static_cast(src); + const unsigned char ch = static_cast(c); for (; n != 0; --n) { - const unsigned char *s = str + n - 1; - if (*s == ch) - return const_cast(s); + if (*(--str) == ch) + return const_cast(str); } return nullptr; } Index: libc/src/string/memset.cpp =================================================================== --- libc/src/string/memset.cpp +++ libc/src/string/memset.cpp @@ -13,7 +13,7 @@ namespace __llvm_libc { LLVM_LIBC_FUNCTION(void *, memset, (void *dst, int value, size_t count)) { - GeneralPurposeMemset(reinterpret_cast(dst), + GeneralPurposeMemset(static_cast(dst), static_cast(value), count); return dst; } Index: libc/src/string/strchr.cpp =================================================================== --- libc/src/string/strchr.cpp +++ libc/src/string/strchr.cpp @@ -14,12 +14,12 @@ // TODO: Look at performance benefits of comparing words. LLVM_LIBC_FUNCTION(char *, strchr, (const char *src, int c)) { - unsigned char *str = - const_cast(reinterpret_cast(src)); - const unsigned char ch = c; - for (; *str && *str != ch; ++str) - ; - return *str == ch ? reinterpret_cast(str) : nullptr; + const char ch = static_cast(c); + for (; *src != ch; ++src) + if (*src == '\0') + return nullptr; + + return const_cast(src); } } // namespace __llvm_libc Index: libc/src/string/strcpy.cpp =================================================================== --- libc/src/string/strcpy.cpp +++ libc/src/string/strcpy.cpp @@ -16,7 +16,7 @@ LLVM_LIBC_FUNCTION(char *, strcpy, (char *__restrict dest, const char *__restrict src)) { - return reinterpret_cast( + return static_cast( __llvm_libc::memcpy(dest, src, internal::string_length(src) + 1)); } Index: libc/src/string/strrchr.cpp =================================================================== --- libc/src/string/strrchr.cpp +++ libc/src/string/strrchr.cpp @@ -13,12 +13,12 @@ namespace __llvm_libc { LLVM_LIBC_FUNCTION(char *, strrchr, (const char *src, int c)) { - const char ch = c; + const char ch = static_cast(c); char *last_occurrence = nullptr; do { if (*src == ch) last_occurrence = const_cast(src); - } while (*src++); + } while (*src++ != '\0'); return last_occurrence; } Index: libc/src/string/strstr.cpp =================================================================== --- libc/src/string/strstr.cpp +++ libc/src/string/strstr.cpp @@ -16,11 +16,11 @@ // TODO: This is a simple brute force implementation. This can be // improved upon using well known string matching algorithms. LLVM_LIBC_FUNCTION(char *, strstr, (const char *haystack, const char *needle)) { - for (size_t i = 0; haystack[i]; ++i) { + for (size_t i = 0; haystack[i] != '\0'; ++i) { size_t j; - for (j = 0; haystack[i + j] && haystack[i + j] == needle[j]; ++j) + for (j = 0; haystack[i + j] != '\0' && haystack[i + j] == needle[j]; ++j) ; - if (!needle[j]) + if (needle[j] == '\0') return const_cast(haystack + i); } return nullptr; Index: libc/src/string/x86_64/memcpy.cpp =================================================================== --- libc/src/string/x86_64/memcpy.cpp +++ libc/src/string/x86_64/memcpy.cpp @@ -101,8 +101,7 @@ LLVM_LIBC_FUNCTION(void *, memcpy, (void *__restrict dst, const void *__restrict src, size_t size)) { - memcpy_x86(reinterpret_cast(dst), reinterpret_cast(src), - size); + memcpy_x86(static_cast(dst), static_cast(src), size); return dst; }