llvm.org GIT mirror llvm / 5dc5797
Revert "ADT: add <bit> header, implement C++20 bit_cast, use" Bots sad. Looks like missing std::is_trivially_copyable. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@341730 91177308-0d34-0410-b5e6-96231b3b80d8 JF Bastien 1 year, 11 months ago
4 changed file(s) with 39 addition(s) and 61 deletion(s). Raw diff Collapse all Expand all
+0
-37
include/llvm/ADT/bit.h less more
None //===-- llvm/ADT/bit.h - C++20 ----------------------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the C++20 header.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_ADT_BIT_H
14 #define LLVM_ADT_BIT_H
15
16 #include
17 #include
18
19 namespace llvm {
20
21 template <
22 typename To, typename From,
23 typename = typename std::enable_if::type,
24 typename =
25 typename std::enable_if::value>::type,
26 typename =
27 typename std::enable_if::value>::type>
28 inline To bit_cast(const From &from) noexcept {
29 typename std::aligned_storage::type storage;
30 std::memcpy(&storage, &from, sizeof(To));
31 return reinterpret_cast(storage);
32 }
33
34 } // namespace llvm
35
36 #endif
1818 #include "llvm/ADT/Optional.h"
1919 #include "llvm/ADT/SmallString.h"
2020 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/bit.h"
2221 #include "llvm/Config/llvm-config.h"
2322 #include "llvm/Support/Debug.h"
2423 #include "llvm/Support/ErrorHandling.h"
712711 }
713712
714713 APInt llvm::APIntOps::RoundDoubleToAPInt(double Double, unsigned width) {
715 uint64_t I = bit_cast(Double);
714 union {
715 double D;
716 uint64_t I;
717 } T;
718 T.D = Double;
716719
717720 // Get the sign bit from the highest order bit
718 bool isNeg = I >> 63;
721 bool isNeg = T.I >> 63;
719722
720723 // Get the 11-bit exponent and adjust for the 1023 bit bias
721 int64_t exp = ((I >> 52) & 0x7ff) - 1023;
724 int64_t exp = ((T.I >> 52) & 0x7ff) - 1023;
722725
723726 // If the exponent is negative, the value is < 0 so just return 0.
724727 if (exp < 0)
725728 return APInt(width, 0u);
726729
727730 // Extract the mantissa by clearing the top 12 bits (sign + exponent).
728 uint64_t mantissa = (I & (~0ULL >> 12)) | 1ULL << 52;
731 uint64_t mantissa = (T.I & (~0ULL >> 12)) | 1ULL << 52;
729732
730733 // If the exponent doesn't shift all bits out of the mantissa
731734 if (exp < 52)
802805
803806 // The leading bit of mantissa is implicit, so get rid of it.
804807 uint64_t sign = isNeg ? (1ULL << (APINT_BITS_PER_WORD - 1)) : 0;
805 uint64_t I = sign | (exp << 52) | mantissa;
806 return bit_cast(I);
808 union {
809 double D;
810 uint64_t I;
811 } T;
812 T.I = sign | (exp << 52) | mantissa;
813 return T.D;
807814 }
808815
809816 // Truncate to new width.
1515
1616 #include "llvm/ADT/APFloat.h"
1717 #include "llvm/ADT/APInt.h"
18 #include "llvm/ADT/bit.h"
1918 #include "llvm/Support/ErrorHandling.h"
2019 #include "llvm/Support/MathExtras.h"
2120 #include
342341 //
343342 static inline float getFPImmFloat(unsigned Imm) {
344343 // We expect an 8-bit binary encoding of a floating-point number here.
344 union {
345 uint32_t I;
346 float F;
347 } FPUnion;
345348
346349 uint8_t Sign = (Imm >> 7) & 0x1;
347350 uint8_t Exp = (Imm >> 4) & 0x7;
348351 uint8_t Mantissa = Imm & 0xf;
349352
350 // 8-bit FP IEEE Float Encoding
353 // 8-bit FP iEEEE Float Encoding
351354 // abcd efgh aBbbbbbc defgh000 00000000 00000000
352355 //
353356 // where B = NOT(b);
354357
355 uint32_t I = 0;
356 I |= Sign << 31;
357 I |= ((Exp & 0x4) != 0 ? 0 : 1) << 30;
358 I |= ((Exp & 0x4) != 0 ? 0x1f : 0) << 25;
359 I |= (Exp & 0x3) << 23;
360 I |= Mantissa << 19;
361 return bit_cast(I);
358 FPUnion.I = 0;
359 FPUnion.I |= Sign << 31;
360 FPUnion.I |= ((Exp & 0x4) != 0 ? 0 : 1) << 30;
361 FPUnion.I |= ((Exp & 0x4) != 0 ? 0x1f : 0) << 25;
362 FPUnion.I |= (Exp & 0x3) << 23;
363 FPUnion.I |= Mantissa << 19;
364 return FPUnion.F;
362365 }
363366
364367 /// getFP16Imm - Return an 8-bit floating-point version of the 16-bit
626626 //
627627 inline float getFPImmFloat(unsigned Imm) {
628628 // We expect an 8-bit binary encoding of a floating-point number here.
629 union {
630 uint32_t I;
631 float F;
632 } FPUnion;
629633
630634 uint8_t Sign = (Imm >> 7) & 0x1;
631635 uint8_t Exp = (Imm >> 4) & 0x7;
632636 uint8_t Mantissa = Imm & 0xf;
633637
634 // 8-bit FP IEEE Float Encoding
638 // 8-bit FP iEEEE Float Encoding
635639 // abcd efgh aBbbbbbc defgh000 00000000 00000000
636640 //
637641 // where B = NOT(b);
638 uint32_t I = 0;
639 I |= Sign << 31;
640 I |= ((Exp & 0x4) != 0 ? 0 : 1) << 30;
641 I |= ((Exp & 0x4) != 0 ? 0x1f : 0) << 25;
642 I |= (Exp & 0x3) << 23;
643 I |= Mantissa << 19;
644 return bit_cast(F);
642
643 FPUnion.I = 0;
644 FPUnion.I |= Sign << 31;
645 FPUnion.I |= ((Exp & 0x4) != 0 ? 0 : 1) << 30;
646 FPUnion.I |= ((Exp & 0x4) != 0 ? 0x1f : 0) << 25;
647 FPUnion.I |= (Exp & 0x3) << 23;
648 FPUnion.I |= Mantissa << 19;
649 return FPUnion.F;
645650 }
646651
647652 /// getFP16Imm - Return an 8-bit floating-point version of the 16-bit