[flang-commits] [flang] Simplify StringLength implementation (PR #98771)

via flang-commits flang-commits at lists.llvm.org
Sat Jul 13 13:22:18 PDT 2024


https://github.com/serge-sans-paille created https://github.com/llvm/llvm-project/pull/98771

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.

>From 0100fcf642d03d6fbbc8745ce24d9f0d4b8f0bb3 Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Sat, 13 Jul 2024 22:13:23 +0200
Subject: [PATCH] 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.
---
 flang/runtime/command.cpp | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

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