mirror of
https://github.com/intel/llvm.git
synced 2026-01-26 12:26:52 +08:00
[libc] Some more MSVC compatibility in src/__support. (#158108)
This commit is contained in:
@@ -39,7 +39,7 @@ bit_cast(const From &from) {
|
||||
#if __has_builtin(__builtin_bit_cast)
|
||||
return __builtin_bit_cast(To, from);
|
||||
#else
|
||||
To to;
|
||||
To to{};
|
||||
char *dst = reinterpret_cast<char *>(&to);
|
||||
const char *src = reinterpret_cast<const char *>(&from);
|
||||
#if __has_builtin(__builtin_memcpy_inline)
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace LIBC_NAMESPACE_DECL {
|
||||
namespace cpp {
|
||||
|
||||
// is_destructible
|
||||
#if __has_builtin(__is_destructible)
|
||||
#if __has_builtin(__is_destructible) || defined(LIBC_COMPILER_IS_MSVC)
|
||||
template <typename T>
|
||||
struct is_destructible : bool_constant<__is_destructible(T)> {};
|
||||
#else
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "src/__support/macros/config.h"
|
||||
#include "src/__support/macros/optimization.h"
|
||||
#include "src/__support/macros/properties/architectures.h"
|
||||
#include "src/__support/macros/properties/compiler.h"
|
||||
|
||||
#if defined(LIBC_TARGET_ARCH_IS_AARCH64) && defined(__ARM_FP)
|
||||
#if defined(__APPLE__)
|
||||
@@ -29,9 +30,12 @@
|
||||
// The extra !defined(APPLE) condition is to cause x86_64 MacOS builds to use
|
||||
// the dummy implementations below. Once a proper x86_64 darwin fenv is set up,
|
||||
// the apple condition here should be removed.
|
||||
#elif defined(LIBC_TARGET_ARCH_IS_X86) && !defined(__APPLE__)
|
||||
// TODO: fully support fenv for MSVC.
|
||||
#elif defined(LIBC_TARGET_ARCH_IS_X86) && !defined(__APPLE__) && \
|
||||
!defined(LIBC_COMPILER_IS_MSVC)
|
||||
#include "x86_64/FEnvImpl.h"
|
||||
#elif defined(LIBC_TARGET_ARCH_IS_ARM) && defined(__ARM_FP)
|
||||
#elif defined(LIBC_TARGET_ARCH_IS_ARM) && defined(__ARM_FP) && \
|
||||
!defined(LIBC_COMPILER_IS_MSVC)
|
||||
#include "arm/FEnvImpl.h"
|
||||
#elif defined(LIBC_TARGET_ARCH_IS_ANY_RISCV) && defined(__riscv_flen)
|
||||
#include "riscv/FEnvImpl.h"
|
||||
|
||||
@@ -789,16 +789,16 @@ struct FPRep : public FPRepImpl<fp_type, FPRep<fp_type>> {
|
||||
// Returns the FPType corresponding to C++ type T on the host.
|
||||
template <typename T> LIBC_INLINE static constexpr FPType get_fp_type() {
|
||||
using UnqualT = cpp::remove_cv_t<T>;
|
||||
if constexpr (cpp::is_same_v<UnqualT, float> && __FLT_MANT_DIG__ == 24)
|
||||
if constexpr (cpp::is_same_v<UnqualT, float> && FLT_MANT_DIG == 24)
|
||||
return FPType::IEEE754_Binary32;
|
||||
else if constexpr (cpp::is_same_v<UnqualT, double> && __DBL_MANT_DIG__ == 53)
|
||||
else if constexpr (cpp::is_same_v<UnqualT, double> && DBL_MANT_DIG == 53)
|
||||
return FPType::IEEE754_Binary64;
|
||||
else if constexpr (cpp::is_same_v<UnqualT, long double>) {
|
||||
if constexpr (__LDBL_MANT_DIG__ == 53)
|
||||
if constexpr (LDBL_MANT_DIG == 53)
|
||||
return FPType::IEEE754_Binary64;
|
||||
else if constexpr (__LDBL_MANT_DIG__ == 64)
|
||||
else if constexpr (LDBL_MANT_DIG == 64)
|
||||
return FPType::X86_Binary80;
|
||||
else if constexpr (__LDBL_MANT_DIG__ == 113)
|
||||
else if constexpr (LDBL_MANT_DIG == 113)
|
||||
return FPType::IEEE754_Binary128;
|
||||
}
|
||||
#if defined(LIBC_TYPES_HAS_FLOAT16)
|
||||
|
||||
@@ -16,13 +16,49 @@
|
||||
namespace LIBC_NAMESPACE_DECL {
|
||||
|
||||
// We rely on compiler preprocessor defines to allow for cross compilation.
|
||||
#ifdef LIBC_COMPILER_IS_MSVC
|
||||
#define __BYTE_ORDER__ 0
|
||||
#define __ORDER_LITTLE_ENDIAN__ 0
|
||||
#define __ORDER_BIG_ENDIAN__ 1
|
||||
#else // !LIBC_COMPILER_IS_MSVC
|
||||
#if !defined(__BYTE_ORDER__) || !defined(__ORDER_LITTLE_ENDIAN__) || \
|
||||
!defined(__ORDER_BIG_ENDIAN__)
|
||||
#error "Missing preprocessor definitions for endianness detection."
|
||||
#endif
|
||||
#endif // LIBC_COMPILER_IS_MSVC
|
||||
|
||||
namespace internal {
|
||||
|
||||
template <typename T> LIBC_INLINE T byte_swap(T value);
|
||||
|
||||
template <> LIBC_INLINE uint16_t byte_swap<uint16_t>(uint16_t value) {
|
||||
#if __has_builtin(__builtin_bswap16)
|
||||
return __builtin_bswap16(value);
|
||||
#else
|
||||
return (v << 8) | (v >> 8);
|
||||
#endif // __builtin_bswap16
|
||||
}
|
||||
|
||||
template <> LIBC_INLINE uint32_t byte_swap<uint32_t>(uint32_t value) {
|
||||
#if __has_builtin(__builtin_bswap32)
|
||||
return __builtin_bswap32(value);
|
||||
#else
|
||||
return byte_swap<uint16_t>(static_cast<uint16>(v >> 16)) ||
|
||||
(static_cast<uint32_t>(byte_swap<uint16_t>(static_cast<uint16_t>(v)))
|
||||
<< 16);
|
||||
#endif // __builtin_bswap64
|
||||
}
|
||||
|
||||
template <> LIBC_INLINE uint64_t byte_swap<uint64_t>(uint64_t value) {
|
||||
#if __has_builtin(__builtin_bswap64)
|
||||
return __builtin_bswap64(value);
|
||||
#else
|
||||
return byte_swap<uint32_t>(static_cast<uint32>(v >> 32)) ||
|
||||
(static_cast<uint64_t>(byte_swap<uint32_t>(static_cast<uint32_t>(v)))
|
||||
<< 32);
|
||||
#endif // __builtin_bswap64
|
||||
}
|
||||
|
||||
// Converts uint8_t, uint16_t, uint32_t, uint64_t to its big or little endian
|
||||
// counterpart.
|
||||
// We use explicit template specialization:
|
||||
@@ -53,7 +89,7 @@ template <>
|
||||
template <>
|
||||
LIBC_INLINE uint16_t
|
||||
Endian<__ORDER_LITTLE_ENDIAN__>::to_big_endian<uint16_t>(uint16_t v) {
|
||||
return __builtin_bswap16(v);
|
||||
return byte_swap<uint16_t>(v);
|
||||
}
|
||||
template <>
|
||||
template <>
|
||||
@@ -65,7 +101,7 @@ template <>
|
||||
template <>
|
||||
LIBC_INLINE uint32_t
|
||||
Endian<__ORDER_LITTLE_ENDIAN__>::to_big_endian<uint32_t>(uint32_t v) {
|
||||
return __builtin_bswap32(v);
|
||||
return byte_swap<uint32_t>(v);
|
||||
}
|
||||
template <>
|
||||
template <>
|
||||
@@ -77,7 +113,7 @@ template <>
|
||||
template <>
|
||||
LIBC_INLINE uint64_t
|
||||
Endian<__ORDER_LITTLE_ENDIAN__>::to_big_endian<uint64_t>(uint64_t v) {
|
||||
return __builtin_bswap64(v);
|
||||
return byte_swap<uint64_t>(v);
|
||||
}
|
||||
template <>
|
||||
template <>
|
||||
@@ -109,7 +145,7 @@ template <>
|
||||
template <>
|
||||
LIBC_INLINE uint16_t
|
||||
Endian<__ORDER_BIG_ENDIAN__>::to_little_endian<uint16_t>(uint16_t v) {
|
||||
return __builtin_bswap16(v);
|
||||
return byte_swap<uint16_t>(v);
|
||||
}
|
||||
template <>
|
||||
template <>
|
||||
@@ -121,7 +157,7 @@ template <>
|
||||
template <>
|
||||
LIBC_INLINE uint32_t
|
||||
Endian<__ORDER_BIG_ENDIAN__>::to_little_endian<uint32_t>(uint32_t v) {
|
||||
return __builtin_bswap32(v);
|
||||
return byte_swap<uint32_t>(v);
|
||||
}
|
||||
template <>
|
||||
template <>
|
||||
@@ -133,7 +169,7 @@ template <>
|
||||
template <>
|
||||
LIBC_INLINE uint64_t
|
||||
Endian<__ORDER_BIG_ENDIAN__>::to_little_endian<uint64_t>(uint64_t v) {
|
||||
return __builtin_bswap64(v);
|
||||
return byte_swap<uint64_t>(v);
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
||||
@@ -44,6 +44,7 @@
|
||||
#endif
|
||||
|
||||
#define __builtin_expect(value, expectation) (value)
|
||||
#define __builtin_unreachable() __assume(0)
|
||||
|
||||
#endif // LIBC_COMPILER_IS_MSVC
|
||||
|
||||
|
||||
@@ -55,13 +55,23 @@ mask_leading_zeros() {
|
||||
// Returns whether 'a + b' overflows, the result is stored in 'res'.
|
||||
template <typename T>
|
||||
[[nodiscard]] LIBC_INLINE constexpr bool add_overflow(T a, T b, T &res) {
|
||||
#if __has_builtin(__builtin_add_overflow)
|
||||
return __builtin_add_overflow(a, b, &res);
|
||||
#else
|
||||
res = a + b;
|
||||
return (res < a) || (res < b);
|
||||
#endif // __builtin_add_overflow
|
||||
}
|
||||
|
||||
// Returns whether 'a - b' overflows, the result is stored in 'res'.
|
||||
template <typename T>
|
||||
[[nodiscard]] LIBC_INLINE constexpr bool sub_overflow(T a, T b, T &res) {
|
||||
#if __has_builtin(__builtin_sub_overflow)
|
||||
return __builtin_sub_overflow(a, b, &res);
|
||||
#else
|
||||
res = a - b;
|
||||
return (res > a);
|
||||
#endif // __builtin_sub_overflow
|
||||
}
|
||||
|
||||
#define RETURN_IF(TYPE, BUILTIN) \
|
||||
|
||||
Reference in New Issue
Block a user