[llvm] 75767a0 - [Support] MathExtras.h - use llvm::bitcast<> for float-bits cast helpers. NFCI.

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 23 10:27:35 PDT 2022


Author: Simon Pilgrim
Date: 2022-08-23T18:27:13+01:00
New Revision: 75767a0f9a926641edbef08e31ec2148ff45da67

URL: https://github.com/llvm/llvm-project/commit/75767a0f9a926641edbef08e31ec2148ff45da67
DIFF: https://github.com/llvm/llvm-project/commit/75767a0f9a926641edbef08e31ec2148ff45da67.diff

LOG: [Support] MathExtras.h - use llvm::bitcast<> for float-bits cast helpers. NFCI.

Added: 
    

Modified: 
    llvm/include/llvm/Support/MathExtras.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h
index bade1e41d089e..2e5d77e3290f5 100644
--- a/llvm/include/llvm/Support/MathExtras.h
+++ b/llvm/include/llvm/Support/MathExtras.h
@@ -617,38 +617,30 @@ inline uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B) {
 
 /// This function takes a 64-bit integer and returns the bit equivalent double.
 inline double BitsToDouble(uint64_t Bits) {
-  double D;
   static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes");
-  memcpy(&D, &Bits, sizeof(Bits));
-  return D;
+  return llvm::bit_cast<double>(Bits);
 }
 
 /// This function takes a 32-bit integer and returns the bit equivalent float.
 inline float BitsToFloat(uint32_t Bits) {
-  float F;
   static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes");
-  memcpy(&F, &Bits, sizeof(Bits));
-  return F;
+  return llvm::bit_cast<float>(Bits);
 }
 
 /// This function takes a double and returns the bit equivalent 64-bit integer.
 /// Note that copying doubles around changes the bits of NaNs on some hosts,
 /// notably x86, so this routine cannot be used if these bits are needed.
 inline uint64_t DoubleToBits(double Double) {
-  uint64_t Bits;
   static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes");
-  memcpy(&Bits, &Double, sizeof(Double));
-  return Bits;
+  return llvm::bit_cast<uint64_t>(Double);
 }
 
 /// This function takes a float and returns the bit equivalent 32-bit integer.
 /// Note that copying floats around changes the bits of NaNs on some hosts,
 /// notably x86, so this routine cannot be used if these bits are needed.
 inline uint32_t FloatToBits(float Float) {
-  uint32_t Bits;
   static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes");
-  memcpy(&Bits, &Float, sizeof(Float));
-  return Bits;
+  return llvm::bit_cast<uint32_t>(Float);
 }
 
 /// A and B are either alignments or offsets. Return the minimum alignment that


        


More information about the llvm-commits mailing list