Skip to content

Commit 1e6672a

Browse files
[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.
1 parent 0e986e3 commit 1e6672a

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

flang/runtime/command.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,9 @@ pid_t RTNAME(GetPID)() { return getpid(); }
4747
// Returns the length of the \p string. Assumes \p string is valid.
4848
static std::int64_t StringLength(const char *string) {
4949
std::size_t length{std::strlen(string)};
50-
if constexpr (sizeof(std::size_t) < sizeof(std::int64_t)) {
50+
if (length <= std::numeric_limits<std::int64_t>::max())
5151
return static_cast<std::int64_t>(length);
52-
} else {
53-
std::size_t max{std::numeric_limits<std::int64_t>::max()};
54-
return length > max ? 0 // Just fail.
55-
: static_cast<std::int64_t>(length);
56-
}
52+
return 0;
5753
}
5854

5955
static void FillWithSpaces(const Descriptor &value, std::size_t offset = 0) {

0 commit comments

Comments
 (0)