[flang-commits] [flang] 1e6672a - [Flang][Runtime] Simplify StringLength implementation

via flang-commits flang-commits at lists.llvm.org
Wed Jul 17 23:27:04 PDT 2024


Author: serge-sans-paille
Date: 2024-07-18T08:25:18+02:00
New Revision: 1e6672af2497042d5dad0236c2ad9e61f879ac07

URL: https://github.com/llvm/llvm-project/commit/1e6672af2497042d5dad0236c2ad9e61f879ac07
DIFF: https://github.com/llvm/llvm-project/commit/1e6672af2497042d5dad0236c2ad9e61f879ac07.diff

LOG: [Flang][Runtime] Simplify StringLength implementation

This implementation relies on arithmetic conversion, let's see what
happens when we do

    std::size_t length{std::strlen(string)};
    if (length <= std::numeric_limits<std::int64_t>::max())
        return static_cast<std::int64_t>(length);

1) if size_t == uint32_t (or lower), then the comparison operator
   invokes integral promotion to uint64_t, the comparison happens, it's
   fine.

2) if size_t == uint64_t, then the comparison is done between unsigned
   types, which implies a conversion of
   std::numeric_limits<std::int64_t>::max() to uint64_t, which happens
   without accuracy loss, fine

3) if size_t == uint128_t (or higher), then we invoke integral promotion
   of std::int64_t, it's also fine.

So this snippet has the same behavior as the existing one, while being
easier to read.

Added: 
    

Modified: 
    flang/runtime/command.cpp

Removed: 
    


################################################################################
diff  --git a/flang/runtime/command.cpp b/flang/runtime/command.cpp
index e642248a25e68..a555e26f96a66 100644
--- a/flang/runtime/command.cpp
+++ b/flang/runtime/command.cpp
@@ -47,13 +47,9 @@ pid_t RTNAME(GetPID)() { return getpid(); }
 // Returns the length of the \p string. Assumes \p string is valid.
 static std::int64_t StringLength(const char *string) {
   std::size_t length{std::strlen(string)};
-  if constexpr (sizeof(std::size_t) < sizeof(std::int64_t)) {
+  if (length <= std::numeric_limits<std::int64_t>::max())
     return static_cast<std::int64_t>(length);
-  } else {
-    std::size_t max{std::numeric_limits<std::int64_t>::max()};
-    return length > max ? 0 // Just fail.
-                        : static_cast<std::int64_t>(length);
-  }
+  return 0;
 }
 
 static void FillWithSpaces(const Descriptor &value, std::size_t offset = 0) {


        


More information about the flang-commits mailing list