diff --git a/libc/src/__support/CPP/CMakeLists.txt b/libc/src/__support/CPP/CMakeLists.txt index a9cb67df0b427..bdfbc6151c773 100644 --- a/libc/src/__support/CPP/CMakeLists.txt +++ b/libc/src/__support/CPP/CMakeLists.txt @@ -20,6 +20,7 @@ add_header_library( libc.hdr.stdint_proxy libc.src.__support.macros.attributes libc.src.__support.macros.sanitizer + libc.src.__support.macros.properties.compiler ) add_header_library( @@ -209,6 +210,7 @@ add_object_library( libc.hdr.func.malloc libc.hdr.func.aligned_alloc libc.src.__support.common + libc.src.__support.macros.properties.compiler libc.src.__support.macros.properties.os ) diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h index 8dbb30047faec..f602a7447ec10 100644 --- a/libc/src/__support/CPP/bit.h +++ b/libc/src/__support/CPP/bit.h @@ -16,6 +16,7 @@ #include "src/__support/CPP/type_traits.h" #include "src/__support/macros/attributes.h" #include "src/__support/macros/config.h" +#include "src/__support/macros/properties/compiler.h" #include "src/__support/macros/sanitizer.h" namespace LIBC_NAMESPACE_DECL { @@ -36,7 +37,7 @@ LIBC_INLINE constexpr cpp::enable_if_t< To> bit_cast(const From &from) { MSAN_UNPOISON(&from, sizeof(From)); -#if __has_builtin(__builtin_bit_cast) +#if __has_builtin(__builtin_bit_cast) || defined(LIBC_COMPILER_IS_MSVC) return __builtin_bit_cast(To, from); #else To to{}; diff --git a/libc/src/__support/CPP/new.h b/libc/src/__support/CPP/new.h index fe36de29468a8..67344b09e3833 100644 --- a/libc/src/__support/CPP/new.h +++ b/libc/src/__support/CPP/new.h @@ -14,6 +14,7 @@ #include "hdr/func/malloc.h" #include "src/__support/common.h" #include "src/__support/macros/config.h" +#include "src/__support/macros/properties/compiler.h" #include "src/__support/macros/properties/os.h" #include // For size_t @@ -109,8 +110,12 @@ LIBC_INLINE void *operator new[](size_t, void *p) { return p; } // header file in all libc source files where operator delete is called ensures // that only libc call sites use these replacement operator delete functions. +#ifndef LIBC_COMPILER_IS_MSVC #define DELETE_NAME(name) \ __asm__(LIBC_MACRO_TO_STRING(LIBC_NAMESPACE) "_" LIBC_MACRO_TO_STRING(name)) +#else +#define DELETE_NAME(name) +#endif // LIBC_COMPILER_IS_MSVC void operator delete(void *) noexcept DELETE_NAME(delete); void operator delete(void *, std::align_val_t) noexcept diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h index 10e35ef4fd24a..bb9cefd67b552 100644 --- a/libc/src/__support/big_int.h +++ b/libc/src/__support/big_int.h @@ -95,10 +95,10 @@ LIBC_INLINE constexpr DoubleWide mul2(word a, word b) { #endif else { using half_word = half_width_t; - const auto shiftl = [](word value) -> word { + constexpr auto shiftl = [](word value) -> word { return value << cpp::numeric_limits::digits; }; - const auto shiftr = [](word value) -> word { + constexpr auto shiftr = [](word value) -> word { return value >> cpp::numeric_limits::digits; }; // Here we do a one digit multiplication where 'a' and 'b' are of type @@ -111,19 +111,19 @@ LIBC_INLINE constexpr DoubleWide mul2(word a, word b) { // c result // We convert 'lo' and 'hi' from 'half_word' to 'word' so multiplication // doesn't overflow. - const word a_lo = lo(a); - const word b_lo = lo(b); - const word a_hi = hi(a); - const word b_hi = hi(b); - const word step1 = b_lo * a_lo; // no overflow; - const word step2 = b_lo * a_hi; // no overflow; - const word step3 = b_hi * a_lo; // no overflow; - const word step4 = b_hi * a_hi; // no overflow; + word a_lo = lo(a); + word b_lo = lo(b); + word a_hi = hi(a); + word b_hi = hi(b); + word step1 = b_lo * a_lo; // no overflow; + word step2 = b_lo * a_hi; // no overflow; + word step3 = b_hi * a_lo; // no overflow; + word step4 = b_hi * a_hi; // no overflow; word lo_digit = step1; word hi_digit = step4; - const word no_carry = 0; - word carry; - word _; // unused carry variable. + word no_carry = 0; + word carry = 0; + [[maybe_unused]] word _ = 0; // unused carry variable. lo_digit = add_with_carry(lo_digit, shiftl(step2), no_carry, carry); hi_digit = add_with_carry(hi_digit, shiftr(step2), carry, _); lo_digit = add_with_carry(lo_digit, shiftl(step3), no_carry, carry); diff --git a/libc/src/__support/math_extras.h b/libc/src/__support/math_extras.h index d4dc6dcb4acf6..3d0f2703b69a4 100644 --- a/libc/src/__support/math_extras.h +++ b/libc/src/__support/math_extras.h @@ -25,7 +25,13 @@ LIBC_INLINE constexpr cpp::enable_if_t, T> mask_trailing_ones() { constexpr unsigned T_BITS = CHAR_BIT * sizeof(T); static_assert(count <= T_BITS && "Invalid bit index"); - return count == 0 ? 0 : (T(-1) >> (T_BITS - count)); + // MSVC complains about out of range shifts. + if constexpr (count == 0) + return 0; + else if constexpr (count >= T_BITS) + return T(-1); + else + return T(-1) >> (T_BITS - count); } // Create a bitmask with the count left-most bits set to 1, and all other bits