forked from OSchip/llvm-project
[flang] Clean up some static_casts
Original-commit: flang-compiler/f18@a5f81388a2 Reviewed-on: https://github.com/flang-compiler/f18/pull/671 Tree-same-pre-rewrite: false
This commit is contained in:
parent
3f15d46f63
commit
b5408d26ed
|
@ -62,7 +62,7 @@ private:
|
||||||
// The base-2 logarithm of the least significant bit that can arise
|
// The base-2 logarithm of the least significant bit that can arise
|
||||||
// in a subnormal IEEE floating-point number.
|
// in a subnormal IEEE floating-point number.
|
||||||
static constexpr int minLog2AnyBit{
|
static constexpr int minLog2AnyBit{
|
||||||
-static_cast<int>(Real::exponentBias) - Real::precision};
|
-int{Real::exponentBias} - Real::precision};
|
||||||
static constexpr int maxDigits{3 - minLog2AnyBit / log10Radix};
|
static constexpr int maxDigits{3 - minLog2AnyBit / log10Radix};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -55,8 +55,7 @@ template<int PRECISION> struct BinaryFloatingPointNumber {
|
||||||
static constexpr int exponentBits{bits - 1 - significandBits};
|
static constexpr int exponentBits{bits - 1 - significandBits};
|
||||||
static constexpr int maxExponent{(1 << exponentBits) - 1};
|
static constexpr int maxExponent{(1 << exponentBits) - 1};
|
||||||
static constexpr int exponentBias{maxExponent / 2};
|
static constexpr int exponentBias{maxExponent / 2};
|
||||||
static constexpr RawType significandMask{
|
static constexpr RawType significandMask{(RawType{1} << significandBits) - 1};
|
||||||
(static_cast<RawType>(1) << significandBits) - 1};
|
|
||||||
|
|
||||||
BinaryFloatingPointNumber() {} // zero
|
BinaryFloatingPointNumber() {} // zero
|
||||||
BinaryFloatingPointNumber(const BinaryFloatingPointNumber &that) = default;
|
BinaryFloatingPointNumber(const BinaryFloatingPointNumber &that) = default;
|
||||||
|
@ -83,13 +82,13 @@ template<int PRECISION> struct BinaryFloatingPointNumber {
|
||||||
constexpr RawType Fraction() const {
|
constexpr RawType Fraction() const {
|
||||||
RawType sig{Significand()};
|
RawType sig{Significand()};
|
||||||
if (implicitMSB && BiasedExponent() > 0) {
|
if (implicitMSB && BiasedExponent() > 0) {
|
||||||
sig |= static_cast<RawType>(1) << significandBits;
|
sig |= RawType{1} << significandBits;
|
||||||
}
|
}
|
||||||
return sig;
|
return sig;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr bool IsZero() const {
|
constexpr bool IsZero() const {
|
||||||
return (raw & ((static_cast<RawType>(1) << (bits - 1)) - 1)) == 0;
|
return (raw & ((RawType{1} << (bits - 1)) - 1)) == 0;
|
||||||
}
|
}
|
||||||
constexpr bool IsNaN() const {
|
constexpr bool IsNaN() const {
|
||||||
return BiasedExponent() == maxExponent && Significand() != 0;
|
return BiasedExponent() == maxExponent && Significand() != 0;
|
||||||
|
@ -103,7 +102,7 @@ template<int PRECISION> struct BinaryFloatingPointNumber {
|
||||||
}
|
}
|
||||||
constexpr bool IsNegative() const { return (raw >> (bits - 1)) & 1; }
|
constexpr bool IsNegative() const { return (raw >> (bits - 1)) & 1; }
|
||||||
|
|
||||||
constexpr void Negate() { raw ^= static_cast<RawType>(1) << (bits - 1); }
|
constexpr void Negate() { raw ^= RawType{1} << (bits - 1); }
|
||||||
|
|
||||||
RawType raw{0};
|
RawType raw{0};
|
||||||
};
|
};
|
||||||
|
|
|
@ -136,7 +136,7 @@ template<int PREC> class IntermediateFloat {
|
||||||
public:
|
public:
|
||||||
static constexpr int precision{PREC};
|
static constexpr int precision{PREC};
|
||||||
using IntType = HostUnsignedIntType<precision>;
|
using IntType = HostUnsignedIntType<precision>;
|
||||||
static constexpr IntType topBit{static_cast<IntType>(1) << (precision - 1)};
|
static constexpr IntType topBit{IntType{1} << (precision - 1)};
|
||||||
static constexpr IntType mask{topBit + (topBit - 1)};
|
static constexpr IntType mask{topBit + (topBit - 1)};
|
||||||
|
|
||||||
IntermediateFloat() {}
|
IntermediateFloat() {}
|
||||||
|
@ -179,8 +179,7 @@ public:
|
||||||
private:
|
private:
|
||||||
static constexpr int guardBits{3}; // guard, round, sticky
|
static constexpr int guardBits{3}; // guard, round, sticky
|
||||||
using GuardType = int;
|
using GuardType = int;
|
||||||
static constexpr GuardType oneHalf{
|
static constexpr GuardType oneHalf{GuardType{1} << (guardBits - 1)};
|
||||||
static_cast<GuardType>(1) << (guardBits - 1)};
|
|
||||||
|
|
||||||
IntType value_{0};
|
IntType value_{0};
|
||||||
GuardType guard_{0};
|
GuardType guard_{0};
|
||||||
|
@ -265,7 +264,7 @@ BigRadixFloatingPointNumber<PREC, LOG10RADIX>::ConvertToBinary() {
|
||||||
if (digits_ == 0) { // zero value
|
if (digits_ == 0) { // zero value
|
||||||
if (isNegative_) {
|
if (isNegative_) {
|
||||||
using Raw = typename Binary::RawType;
|
using Raw = typename Binary::RawType;
|
||||||
Raw negZero{static_cast<Raw>(1) << (Binary::bits - 1)};
|
Raw negZero{Raw{1} << (Binary::bits - 1)};
|
||||||
return {Binary{negZero}};
|
return {Binary{negZero}};
|
||||||
} else {
|
} else {
|
||||||
return {Binary{}};
|
return {Binary{}};
|
||||||
|
@ -360,11 +359,9 @@ BigRadixFloatingPointNumber<PREC, LOG10RADIX>::ConvertToBinary(const char *&p) {
|
||||||
using Binary = BinaryFloatingPointNumber<PREC>;
|
using Binary = BinaryFloatingPointNumber<PREC>;
|
||||||
using Raw = typename Binary::RawType;
|
using Raw = typename Binary::RawType;
|
||||||
static constexpr Raw inf{
|
static constexpr Raw inf{
|
||||||
static_cast<Raw>(Binary::maxExponent) << Binary::significandBits};
|
Raw{Binary::maxExponent} << Binary::significandBits};
|
||||||
static constexpr Raw nan{
|
static constexpr Raw nan{inf | (Raw{1} << (Binary::significandBits - 2))};
|
||||||
inf | (static_cast<Raw>(1) << (Binary::significandBits - 2))};
|
static constexpr Raw negInf{inf | (Raw{1} << (Binary::bits - 1))};
|
||||||
static constexpr Raw negInf{
|
|
||||||
inf | (static_cast<Raw>(1) << (Binary::bits - 1))};
|
|
||||||
if (toupper(p[0]) == 'N' && toupper(p[1]) == 'A' && toupper(p[2]) == 'N') {
|
if (toupper(p[0]) == 'N' && toupper(p[1]) == 'A' && toupper(p[2]) == 'N') {
|
||||||
// NaN
|
// NaN
|
||||||
p += 3;
|
p += 3;
|
||||||
|
|
|
@ -33,48 +33,70 @@ template<typename UINT, UINT DENOM> inline constexpr UINT FastDivision(UINT n) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#if USE_INT_DIVIDE_WORKAROUNDS
|
#if USE_INT_DIVIDE_WORKAROUNDS
|
||||||
template<> inline constexpr std::uint64_t FastDivision<std::uint64_t, 10000000000000000u>(std::uint64_t n) {
|
template<>
|
||||||
return (static_cast<__uint128_t>(0x39a5652fb1137857) * n) >> (64 + 51);
|
inline constexpr std::uint64_t FastDivision<std::uint64_t, 10000000000000000u>(
|
||||||
|
std::uint64_t n) {
|
||||||
|
cast<__uint128_t{0x39a5652fb1137857} * n) >> (64 + 51);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> inline constexpr std::uint64_t FastDivision<std::uint64_t, 100000000000000u>(std::uint64_t n) {
|
template<>
|
||||||
return (static_cast<__uint128_t>(0xb424dc35095cd81) * n) >> (64 + 42);
|
inline constexpr std::uint64_t FastDivision<std::uint64_t, 100000000000000u>(
|
||||||
|
std::uint64_t n) {
|
||||||
|
return (__uint128_t{0xb424dc35095cd81} * n) >> (64 + 42);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> inline constexpr std::uint32_t FastDivision<std::uint32_t, 1000000u>(std::uint32_t n) {
|
template<>
|
||||||
return (static_cast<std::uint64_t>(0x431bde83) * n) >> (32 + 18);
|
inline constexpr std::uint32_t FastDivision<std::uint32_t, 1000000u>(
|
||||||
|
std::uint32_t n) {
|
||||||
|
return (std::uint64_t{0x431bde83} * n) >> (32 + 18);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> inline constexpr std::uint32_t FastDivision<std::uint32_t, 10000u>(std::uint32_t n) {
|
template<>
|
||||||
return (static_cast<std::uint64_t>(0xd1b71759) * n) >> (32 + 13);
|
inline constexpr std::uint32_t FastDivision<std::uint32_t, 10000u>(
|
||||||
|
std::uint32_t n) {
|
||||||
|
return (std::uint64_t{0xd1b71759} * n) >> (32 + 13);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> inline constexpr std::uint64_t FastDivision<std::uint64_t, 10u>(std::uint64_t n) {
|
template<>
|
||||||
return (static_cast<__uint128_t>(0xcccccccccccccccd) * n) >> (64 + 3);
|
inline constexpr std::uint64_t FastDivision<std::uint64_t, 10u>(
|
||||||
|
std::uint64_t n) {
|
||||||
|
return (__uint128_t{0xcccccccccccccccd} * n) >> (64 + 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> inline constexpr std::uint32_t FastDivision<std::uint32_t, 10u>(std::uint32_t n) {
|
template<>
|
||||||
return (static_cast<std::uint64_t>(0xcccccccd) * n) >> (32 + 3);
|
inline constexpr std::uint32_t FastDivision<std::uint32_t, 10u>(
|
||||||
|
std::uint32_t n) {
|
||||||
|
return (std::uint64_t{0xcccccccd} * n) >> (32 + 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> inline constexpr std::uint64_t FastDivision<std::uint64_t, 5u>(std::uint64_t n) {
|
template<>
|
||||||
return (static_cast<__uint128_t>(0xcccccccccccccccd) * n) >> (64 + 2);
|
inline constexpr std::uint64_t FastDivision<std::uint64_t, 5u>(
|
||||||
|
std::uint64_t n) {
|
||||||
|
return (__uint128_t{0xcccccccccccccccd} * n) >> (64 + 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> inline constexpr std::uint32_t FastDivision<std::uint32_t, 5u>(std::uint32_t n) {
|
template<>
|
||||||
return (static_cast<std::uint64_t>(0xcccccccd) * n) >> (32 + 2);
|
inline constexpr std::uint32_t FastDivision<std::uint32_t, 5u>(
|
||||||
|
std::uint32_t n) {
|
||||||
|
return (std::uint64_t{0xcccccccd} * n) >> (32 + 2);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static_assert(FastDivision<std::uint64_t, 10000000000000000u>(9999999999999999u) == 0);
|
static_assert(
|
||||||
static_assert(FastDivision<std::uint64_t, 10000000000000000u>(10000000000000000u) == 1);
|
FastDivision<std::uint64_t, 10000000000000000u>(9999999999999999u) == 0);
|
||||||
static_assert(FastDivision<std::uint64_t, 100000000000000u>(99999999999999u) == 0);
|
static_assert(
|
||||||
static_assert(FastDivision<std::uint64_t, 100000000000000u>(100000000000000u) == 1);
|
FastDivision<std::uint64_t, 10000000000000000u>(10000000000000000u) == 1);
|
||||||
|
static_assert(
|
||||||
|
FastDivision<std::uint64_t, 100000000000000u>(99999999999999u) == 0);
|
||||||
|
static_assert(
|
||||||
|
FastDivision<std::uint64_t, 100000000000000u>(100000000000000u) == 1);
|
||||||
static_assert(FastDivision<std::uint32_t, 1000000u>(999999u) == 0);
|
static_assert(FastDivision<std::uint32_t, 1000000u>(999999u) == 0);
|
||||||
static_assert(FastDivision<std::uint32_t, 1000000u>(1000000u) == 1);
|
static_assert(FastDivision<std::uint32_t, 1000000u>(1000000u) == 1);
|
||||||
static_assert(FastDivision<std::uint64_t, 10>(18446744073709551615u) == 1844674407370955161u);
|
static_assert(FastDivision<std::uint64_t, 10>(18446744073709551615u) ==
|
||||||
|
1844674407370955161u);
|
||||||
static_assert(FastDivision<std::uint32_t, 10>(4294967295u) == 429496729u);
|
static_assert(FastDivision<std::uint32_t, 10>(4294967295u) == 429496729u);
|
||||||
static_assert(FastDivision<std::uint64_t, 5>(18446744073709551615u) == 3689348814741910323u);
|
static_assert(FastDivision<std::uint64_t, 5>(18446744073709551615u) ==
|
||||||
|
3689348814741910323u);
|
||||||
static_assert(FastDivision<std::uint32_t, 5>(4294967295u) == 858993459u);
|
static_assert(FastDivision<std::uint32_t, 5>(4294967295u) == 858993459u);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue