diff --git a/libc/src/__support/endian_internal.h b/libc/src/__support/endian_internal.h index 4ac8709625d3a..07cde7b905c4d 100644 --- a/libc/src/__support/endian_internal.h +++ b/libc/src/__support/endian_internal.h @@ -35,7 +35,7 @@ template <> LIBC_INLINE uint16_t byte_swap(uint16_t value) { #if __has_builtin(__builtin_bswap16) return __builtin_bswap16(value); #else - return (v << 8) | (v >> 8); + return (value << 8) | (value >> 8); #endif // __builtin_bswap16 } @@ -43,8 +43,9 @@ template <> LIBC_INLINE uint32_t byte_swap(uint32_t value) { #if __has_builtin(__builtin_bswap32) return __builtin_bswap32(value); #else - return byte_swap(static_cast(v >> 16)) || - (static_cast(byte_swap(static_cast(v))) + return byte_swap(static_cast(value >> 16)) || + (static_cast( + byte_swap(static_cast(value))) << 16); #endif // __builtin_bswap64 } @@ -53,8 +54,9 @@ template <> LIBC_INLINE uint64_t byte_swap(uint64_t value) { #if __has_builtin(__builtin_bswap64) return __builtin_bswap64(value); #else - return byte_swap(static_cast(v >> 32)) || - (static_cast(byte_swap(static_cast(v))) + return byte_swap(static_cast(value >> 32)) || + (static_cast( + byte_swap(static_cast(value))) << 32); #endif // __builtin_bswap64 } diff --git a/libc/src/__support/macros/config.h b/libc/src/__support/macros/config.h index 501a816d49631..b06a890c9c13c 100644 --- a/libc/src/__support/macros/config.h +++ b/libc/src/__support/macros/config.h @@ -46,6 +46,8 @@ #define __builtin_expect(value, expectation) (value) #define __builtin_unreachable() __assume(0) +#define __builtin_prefetch(X, Y, Z) + #endif // LIBC_COMPILER_IS_MSVC #ifdef __clang__ diff --git a/libc/src/string/memory_utils/CMakeLists.txt b/libc/src/string/memory_utils/CMakeLists.txt index 670db30129572..9cabfb9318012 100644 --- a/libc/src/string/memory_utils/CMakeLists.txt +++ b/libc/src/string/memory_utils/CMakeLists.txt @@ -42,6 +42,7 @@ add_header_library( libc.src.__support.macros.config libc.src.__support.macros.optimization libc.src.__support.macros.properties.architectures + libc.src.__support.macros.properties.compiler ) add_header_library( diff --git a/libc/src/string/memory_utils/op_generic.h b/libc/src/string/memory_utils/op_generic.h index 37603410e3a51..010f2187a4ffd 100644 --- a/libc/src/string/memory_utils/op_generic.h +++ b/libc/src/string/memory_utils/op_generic.h @@ -31,6 +31,7 @@ #include "src/__support/macros/attributes.h" // LIBC_INLINE #include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL #include "src/__support/macros/optimization.h" +#include "src/__support/macros/properties/compiler.h" #include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT64 #include "src/string/memory_utils/op_builtin.h" #include "src/string/memory_utils/utils.h" @@ -39,12 +40,22 @@ static_assert((UINTPTR_MAX == 4294967295U) || (UINTPTR_MAX == 18446744073709551615UL), "We currently only support 32- or 64-bit platforms"); +#ifdef LIBC_COMPILER_IS_MSVC + +namespace LIBC_NAMESPACE_DECL { +using generic_v128 = __m128i; +using generic_v256 = __m256i; +using generic_v512 = __m512i; +} // namespace LIBC_NAMESPACE_DECL + +#else namespace LIBC_NAMESPACE_DECL { // Compiler types using the vector attributes. using generic_v128 = uint8_t __attribute__((__vector_size__(16))); using generic_v256 = uint8_t __attribute__((__vector_size__(32))); using generic_v512 = uint8_t __attribute__((__vector_size__(64))); } // namespace LIBC_NAMESPACE_DECL +#endif // LIBC_COMPILER_IS_MSVC namespace LIBC_NAMESPACE_DECL { namespace generic { diff --git a/libc/src/string/memory_utils/op_x86.h b/libc/src/string/memory_utils/op_x86.h index 8bd84120c4ffa..1b4052747552d 100644 --- a/libc/src/string/memory_utils/op_x86.h +++ b/libc/src/string/memory_utils/op_x86.h @@ -15,6 +15,7 @@ #include "src/__support/macros/attributes.h" // LIBC_INLINE #include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL #include "src/__support/macros/properties/architectures.h" +#include "src/__support/macros/properties/compiler.h" #if defined(LIBC_TARGET_ARCH_IS_X86) @@ -57,7 +58,12 @@ LIBC_INLINE_VAR constexpr bool K_AVX512_BW = LLVM_LIBC_IS_DEFINED(__AVX512BW__); // Memcpy repmovsb implementation struct Memcpy { LIBC_INLINE static void repmovsb(void *dst, const void *src, size_t count) { +#ifdef LIBC_COMPILER_IS_MSVC + __movsb(static_cast(dst), + static_cast(src), count); +#else asm volatile("rep movsb" : "+D"(dst), "+S"(src), "+c"(count) : : "memory"); +#endif // LIBC_COMPILER_IS_MSVC } }; @@ -138,8 +144,10 @@ LIBC_INLINE MemcmpReturnType cmp_neq(CPtr p1, CPtr p2, // When we use these SIMD types in template specialization GCC complains: // "ignoring attributes on template argument ā€˜__m128i’ [-Wignored-attributes]" // Therefore, we disable this warning in this file. +#ifndef LIBC_COMPILER_IS_MSVC #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wignored-attributes" +#endif // !LIBC_COMPILER_IS_MSVC /////////////////////////////////////////////////////////////////////////////// // Specializations for __m128i @@ -366,7 +374,9 @@ LIBC_INLINE MemcmpReturnType cmp_neq<__m512i>(CPtr p1, CPtr p2, size_t offset) { } #endif // __AVX512BW__ +#ifndef LIBC_COMPILER_IS_MSVC #pragma GCC diagnostic pop +#endif // !LIBC_COMPILER_IS_MSVC } // namespace generic } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/string/memory_utils/utils.h b/libc/src/string/memory_utils/utils.h index 0f9c9e36a3dcd..86ff4f12e8c26 100644 --- a/libc/src/string/memory_utils/utils.h +++ b/libc/src/string/memory_utils/utils.h @@ -17,6 +17,7 @@ #include "src/__support/macros/attributes.h" // LIBC_INLINE #include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL #include "src/__support/macros/properties/architectures.h" +#include "src/__support/macros/properties/compiler.h" #include // size_t @@ -90,13 +91,17 @@ LIBC_INLINE void memcpy_inline(void *__restrict dst, // different value of the Size parameter. This doesn't play well with GCC's // Value Range Analysis that wrongly detects out of bounds accesses. We // disable these warnings for the purpose of this function. +#ifndef LIBC_COMPILER_IS_MSVC #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Warray-bounds" #pragma GCC diagnostic ignored "-Wstringop-overread" #pragma GCC diagnostic ignored "-Wstringop-overflow" +#endif // !LIBC_COMPILER_IS_MSVC for (size_t i = 0; i < Size; ++i) static_cast(dst)[i] = static_cast(src)[i]; +#ifndef LIBC_COMPILER_IS_MSVC #pragma GCC diagnostic pop +#endif // !LIBC_COMPILER_IS_MSVC #endif } diff --git a/libc/test/UnitTest/CMakeLists.txt b/libc/test/UnitTest/CMakeLists.txt index f1a83fc601e5e..31d1e9dce8204 100644 --- a/libc/test/UnitTest/CMakeLists.txt +++ b/libc/test/UnitTest/CMakeLists.txt @@ -76,6 +76,7 @@ add_unittest_framework_library( libc.src.__support.CPP.string_view libc.src.__support.CPP.type_traits libc.src.__support.fixed_point.fx_rep + libc.src.__support.macros.properties.compiler libc.src.__support.macros.properties.types libc.src.__support.OSUtil.osutil libc.src.__support.uint128 diff --git a/libc/test/UnitTest/LibcTest.h b/libc/test/UnitTest/LibcTest.h index fbeafd0bacb75..cf098cdd7a49a 100644 --- a/libc/test/UnitTest/LibcTest.h +++ b/libc/test/UnitTest/LibcTest.h @@ -30,6 +30,7 @@ #include "src/__support/CPP/string_view.h" #include "src/__support/CPP/type_traits.h" #include "src/__support/c_string.h" +#include "src/__support/macros/properties/compiler.h" #include "test/UnitTest/ExecuteFunction.h" #include "test/UnitTest/TestLogger.h" @@ -260,7 +261,11 @@ constexpr char const *GetPrettyFunctionParamType(char const *str) { // This function recovers ParamType at compile time by using __PRETTY_FUNCTION__ // It can be customized by using the REGISTER_TYPE_NAME macro below. template static constexpr const char *GetTypeName() { +#ifdef LIBC_COMPILER_IS_MSVC + return GetPrettyFunctionParamType(__FUNCSIG__); +#else return GetPrettyFunctionParamType(__PRETTY_FUNCTION__); +#endif // LIBC_COMPILER_IS_MSVC } template diff --git a/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel index 24baaf1983a08..318397615d0e3 100644 --- a/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel @@ -62,6 +62,7 @@ libc_test_library( "//libc:__support_libc_errno", "//libc:__support_macros_config", "//libc:__support_macros_properties_architectures", + "//libc:__support_macros_properties_compiler", "//libc:__support_macros_properties_types", "//libc:__support_stringutil", "//libc:__support_uint128",