[llvm] [Flang-rt][build] fixes building warnings under gcc: (PR #173955)

liao jun via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 30 23:44:35 PST 2025


================
@@ -452,7 +452,13 @@ float RTNAME(Rand)(int *i, const char *sourceFile, int line) {
   if (radix == 2) {
     mask = ~(unsigned)0u << (32 - digits + 1);
   } else if (radix == 16) {
-    mask = ~(unsigned)0u << ((8 - digits) * 4 + 1);
+    int shift_val = ((8 - digits) * 4 + 1);
+    if (shift_val < 0) {
+      Terminator terminator{sourceFile, line};
+      terminator.Crash("Radix 16: negative shift for mask. digits value maybe invalid.");
+    } else {
+      mask = ~(unsigned)0u << shift_val;
+    }
----------------
zjlcd wrote:

Oh, i got you!  after `if constexpr`, the code be like:
```
  constexpr int radix = std::numeric_limits<float>::radix;
  constexpr int digits = std::numeric_limits<float>::digits;
  if constexpr (radix == 2) {
    mask = ~(unsigned)0u << (32 - digits + 1);
  } else if constexpr (radix == 16) {
    mask = ~(unsigned)0u << ((8 - digits) * 4 + 1);
  } else {
    Terminator terminator{sourceFile, line};
    terminator.Crash("Radix unknown value.");
  }
````

However, everyone knows that the `if constexpr` branch statement  will evaluates conditions at compile time and only compiles the code branch that meets the condition. using the following testcase:
```
#include <limits>
#include <iostream>

int main() {
    std::cout << "float radix: " << std::numeric_limits<float>::radix << std::endl;
    std::cout << "float digits: " << std::numeric_limits<float>::digits << std::endl;
    std::cout << "double radix: " << std::numeric_limits<double>::radix << std::endl;
    std::cout << "double digits: " << std::numeric_limits<double>::digits << std::endl;
    return 0;
}
```

I got the same results on both Ubuntu-X86_64 and Ubuntu-ARM64 machine under g++, the result be like:

```
float radix: 2
float digits: 24
double radix: 2
double digits: 53
```
This result indicates that the most common modern computing devices support binary floating-point data, if the condition allow, maybe i will try running this testcase on ubuntu-riscv64 machine to see the result(I guess the result maybe the same).

I can not imagine what kind of machine would use hexadecimal floating-point data, the floating-point arithmetic standard that even IEEE-754-2019 not adopts.

To be more radical, I think this `radix=16` branch statement is dead code and need remove; at least, on most common modern computing devices, it won't be executed.




https://github.com/llvm/llvm-project/pull/173955


More information about the llvm-commits mailing list