[llvm] 828cab5 - [RISCV] Adjust how -1.0 is handled in RISCVLoadFPImm. NFC

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 10 20:26:25 PST 2023


Author: Craig Topper
Date: 2023-03-10T20:26:00-08:00
New Revision: 828cab58ed2547b3d3c1659bdcc9955f94be3803

URL: https://github.com/llvm/llvm-project/commit/828cab58ed2547b3d3c1659bdcc9955f94be3803
DIFF: https://github.com/llvm/llvm-project/commit/828cab58ed2547b3d3c1659bdcc9955f94be3803.diff

LOG: [RISCV] Adjust how -1.0 is handled in RISCVLoadFPImm. NFC

Instead of hardcoding the -1.0 exponent and mantissa separately from the
table, reuse the table entry for 1.0.

When searching the table we can change index 16 to index 0 if the sign
is negative. Or when indexing we can change index 0 to index 16 and remember
that we need to flip the sign.

I'm going to make another patch that has a f16 and f64 table and
this makes the code for those more similar.

Added: 
    

Modified: 
    llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.cpp
    llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.cpp
index 3a931c94fa94..988fefe1449b 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.cpp
@@ -229,36 +229,39 @@ static constexpr std::pair<uint8_t, uint8_t> LoadFPImmArr[] = {
     {0b11111111, 0b10},
 };
 
-int RISCVLoadFPImm::getLoadFPImm(uint8_t Sign, uint8_t Exp, uint8_t Mantissa) {
-  if (Sign == 0b1 && Exp == 0b01111111 && Mantissa == 0b00)
-    return 0;
-
-  if (Sign == 0b0) {
-    auto EMI = llvm::lower_bound(LoadFPImmArr, std::make_pair(Exp, Mantissa));
-    if (EMI != std::end(LoadFPImmArr) && EMI->first == Exp &&
-        EMI->second == Mantissa)
-      return std::distance(std::begin(LoadFPImmArr), EMI) + 1;
+int RISCVLoadFPImm::getLoadFPImm(bool Sign, uint8_t Exp, uint8_t Mantissa) {
+  // Lookup in the table, ignoring the sign.
+  auto EMI = llvm::lower_bound(LoadFPImmArr, std::make_pair(Exp, Mantissa));
+  if (EMI == std::end(LoadFPImmArr) || EMI->first != Exp ||
+      EMI->second != Mantissa)
+    return -1;
+
+  // Table doesn't have entry 0.
+  int Entry = std::distance(std::begin(LoadFPImmArr), EMI) + 1;
+
+  // The only legal negative value is -1.0(entry 0). 1.0 is entry 16.
+  if (Sign) {
+    if (Entry == 16)
+      return 0;
+    return false;
   }
 
-  return -1;
+  return Entry;
 }
 
 float RISCVLoadFPImm::getFPImm(unsigned Imm) {
   assert(Imm != 1 && Imm != 30 && Imm != 31 && "Unsupported immediate");
-  uint8_t Sign;
-  uint8_t Exp;
-  uint8_t Mantissa;
 
+  // Entry 0 is -1.0, the only negative value. Entry 16 is 1.0.
+  uint32_t Sign = 0;
   if (Imm == 0) {
     Sign = 0b1;
-    Exp = 0b01111111;
-    Mantissa = 0b00;
-  } else {
-    Sign = 0b0;
-    Exp = LoadFPImmArr[Imm - 1].first;
-    Mantissa = LoadFPImmArr[Imm - 1].second;
+    Imm = 16;
   }
 
+  uint32_t Exp = LoadFPImmArr[Imm - 1].first;
+  uint32_t Mantissa = LoadFPImmArr[Imm - 1].second;
+
   uint32_t I = Sign << 31 | Exp << 23 | Mantissa << 21;
   return bit_cast<float>(I);
 }

diff  --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h
index 605054b65323..9b684a35418d 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h
@@ -347,7 +347,7 @@ inline static bool isValidRoundingMode(unsigned Mode) {
 //
 
 namespace RISCVLoadFPImm {
-int getLoadFPImm(uint8_t Sign, uint8_t Exp, uint8_t Mantissa);
+int getLoadFPImm(bool Sign, uint8_t Exp, uint8_t Mantissa);
 float getFPImm(unsigned Imm);
 
 /// getLoadFP32Imm - Return a 5-bit binary encoding of the 32-bit
@@ -357,7 +357,7 @@ static inline int getLoadFP32Imm(const APInt &Imm) {
   if (Imm.extractBitsAsZExtValue(21, 0) != 0)
     return -1;
 
-  uint8_t Sign = Imm.extractBitsAsZExtValue(1, 31);
+  bool Sign = Imm.extractBitsAsZExtValue(1, 31);
   uint8_t Exp = Imm.extractBitsAsZExtValue(8, 23);
   uint8_t Mantissa = Imm.extractBitsAsZExtValue(2, 21);
   return getLoadFPImm(Sign, Exp, Mantissa);
@@ -374,7 +374,7 @@ static inline int getLoadFP64Imm(const APInt &Imm) {
   if (Imm.extractBitsAsZExtValue(50, 0) != 0)
     return -1;
 
-  uint8_t Sign = Imm.extractBitsAsZExtValue(1, 63);
+  bool Sign = Imm.extractBitsAsZExtValue(1, 63);
   uint8_t Mantissa = Imm.extractBitsAsZExtValue(2, 50);
   uint8_t Exp;
   if (Imm.extractBitsAsZExtValue(11, 52) == 1)
@@ -398,7 +398,7 @@ static inline int getLoadFP16Imm(const APInt &Imm) {
   if (Imm.extractBitsAsZExtValue(8, 0) != 0)
     return -1;
 
-  uint8_t Sign = Imm.extractBitsAsZExtValue(1, 15);
+  bool Sign = Imm.extractBitsAsZExtValue(1, 15);
   uint8_t Mantissa = Imm.extractBitsAsZExtValue(2, 8);
   uint8_t Exp;
   if (Imm.extractBitsAsZExtValue(5, 10) == 1)


        


More information about the llvm-commits mailing list