Skip to content

Commit

Permalink
[libc] Add few macro definitions to make it easy to accommodate Windows.
Browse files Browse the repository at this point in the history
The new macro definitions have been used to add Windows specific
specializations.
  • Loading branch information
Siva Chandra Reddy committed Jun 18, 2021
1 parent 3105333 commit 37afd67
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 14 deletions.
16 changes: 11 additions & 5 deletions libc/utils/FPUtil/FPBits.h
Expand Up @@ -9,6 +9,8 @@
#ifndef LLVM_LIBC_UTILS_FPUTIL_FP_BITS_H
#define LLVM_LIBC_UTILS_FPUTIL_FP_BITS_H

#include "PlatformDefs.h"

#include "utils/CPP/TypeTraits.h"

#include <stdint.h>
Expand Down Expand Up @@ -39,13 +41,17 @@ template <typename T> struct FPUIntType {};
template <> struct FPUIntType<float> { using Type = uint32_t; };
template <> struct FPUIntType<double> { using Type = uint64_t; };

#if !(defined(__x86_64__) || defined(__i386__))
// TODO: This has to be extended for visual studio where long double and
// double are equivalent.
#ifdef LONG_DOUBLE_IS_DOUBLE
template <> struct MantissaWidth<long double> {
static constexpr unsigned value = MantissaWidth<double>::value;
};
template <> struct FPUIntType<long double> {
using Type = FPUIntType<double>::Type;
};
#elif !defined(SPECIAL_X86_LONG_DOUBLE)
template <> struct MantissaWidth<long double> {
static constexpr unsigned value = 112;
};

template <> struct FPUIntType<long double> { using Type = __uint128_t; };
#endif

Expand Down Expand Up @@ -150,7 +156,7 @@ template <typename T> union FPBits {
} // namespace fputil
} // namespace __llvm_libc

#if defined(__x86_64__) || defined(__i386__)
#ifdef SPECIAL_X86_LONG_DOUBLE
#include "utils/FPUtil/LongDoubleBitsX86.h"
#endif

Expand Down
5 changes: 3 additions & 2 deletions libc/utils/FPUtil/ManipulationFunctions.h
Expand Up @@ -12,6 +12,7 @@
#include "FPBits.h"
#include "NearestIntegerOperations.h"
#include "NormalFloat.h"
#include "PlatformDefs.h"

#include "utils/CPP/TypeTraits.h"

Expand Down Expand Up @@ -178,8 +179,8 @@ static inline T nextafter(T from, T to) {
} // namespace fputil
} // namespace __llvm_libc

#if (defined(__x86_64__) || defined(__i386__))
#ifdef SPECIAL_X86_LONG_DOUBLE
#include "NextAfterLongDoubleX86.h"
#endif // defined(__x86_64__) || defined(__i386__)
#endif // SPECIAL_X86_LONG_DOUBLE

#endif // LLVM_LIBC_UTILS_FPUTIL_MANIPULATION_FUNCTIONS_H
4 changes: 2 additions & 2 deletions libc/utils/FPUtil/NormalFloat.h
Expand Up @@ -169,7 +169,7 @@ template <typename T> struct NormalFloat {
}
};

#if defined(__x86_64__) || defined(__i386__)
#ifdef SPECIAL_X86_LONG_DOUBLE
template <>
inline void NormalFloat<long double>::initFromBits(FPBits<long double> bits) {
sign = bits.encoding.sign;
Expand Down Expand Up @@ -256,7 +256,7 @@ template <> inline NormalFloat<long double>::operator long double() const {
result.encoding.implicitBit = 1;
return static_cast<long double>(result);
}
#endif
#endif // SPECIAL_X86_LONG_DOUBLE

} // namespace fputil
} // namespace __llvm_libc
Expand Down
24 changes: 24 additions & 0 deletions libc/utils/FPUtil/PlatformDefs.h
@@ -0,0 +1,24 @@
//===-- Platform specific macro definitions ---------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_UTILS_FPUTIL_PLATFORM_DEFS_H
#define LLVM_LIBC_UTILS_FPUTIL_PLATFORM_DEFS_H

#if defined(__x86_64__) || defined(__i386__)
#define X87_FPU
#endif

#if defined(_WIN32) || defined(_WIN64)
#define LONG_DOUBLE_IS_DOUBLE
#endif

#if !defined(LONG_DOUBLE_IS_DOUBLE) && defined(X87_FPU)
#define SPECIAL_X86_LONG_DOUBLE
#endif

#endif // LLVM_LIBC_UTILS_FPUTIL_PLATFORM_DEFS_H
12 changes: 9 additions & 3 deletions libc/utils/FPUtil/Sqrt.h
Expand Up @@ -10,6 +10,7 @@
#define LLVM_LIBC_UTILS_FPUTIL_SQRT_H

#include "FPBits.h"
#include "PlatformDefs.h"

#include "utils/CPP/TypeTraits.h"

Expand Down Expand Up @@ -61,7 +62,12 @@ template <> inline void normalize<double>(int &exponent, uint64_t &mantissa) {
}
}

#if !(defined(__x86_64__) || defined(__i386__))
#ifdef LONG_DOUBLE_IS_DOUBLE
template <>
inline void normalize<long double>(int &exponent, uint64_t &mantissa) {
normalize<double>(exponent, mantissa);
}
#elif !defined(SPECIAL_X86_LONG_DOUBLE)
template <>
inline void normalize<long double>(int &exponent, __uint128_t &mantissa) {
// Use binary search to shift the leading 1 bit similar to float.
Expand Down Expand Up @@ -179,8 +185,8 @@ static inline T sqrt(T x) {
} // namespace fputil
} // namespace __llvm_libc

#if (defined(__x86_64__) || defined(__i386__))
#ifdef SPECIAL_X86_LONG_DOUBLE
#include "SqrtLongDoubleX86.h"
#endif // defined(__x86_64__) || defined(__i386__)
#endif // SPECIAL_X86_LONG_DOUBLE

#endif // LLVM_LIBC_UTILS_FPUTIL_SQRT_H
2 changes: 0 additions & 2 deletions libc/utils/FPUtil/SqrtLongDoubleX86.h
Expand Up @@ -17,7 +17,6 @@
namespace __llvm_libc {
namespace fputil {

#if (defined(__x86_64__) || defined(__i386__))
namespace internal {

template <>
Expand Down Expand Up @@ -136,7 +135,6 @@ template <> inline long double sqrt<long double, 0>(long double x) {
return out;
}
}
#endif // defined(__x86_64__) || defined(__i386__)

} // namespace fputil
} // namespace __llvm_libc
Expand Down

0 comments on commit 37afd67

Please sign in to comment.