[all-commits] [llvm/llvm-project] 1e6672: [Flang][Runtime] Simplify StringLength implementation

serge-sans-paille via All-commits all-commits at lists.llvm.org
Wed Jul 17 23:27:22 PDT 2024


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 1e6672af2497042d5dad0236c2ad9e61f879ac07
      https://github.com/llvm/llvm-project/commit/1e6672af2497042d5dad0236c2ad9e61f879ac07
  Author: serge-sans-paille <sguelton at mozilla.com>
  Date:   2024-07-18 (Thu, 18 Jul 2024)

  Changed paths:
    M flang/runtime/command.cpp

  Log Message:
  -----------
  [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.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list