Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22137

Why different assembling code generated? Which is better?

$
0
0
#include <cstdint>

uint64_t hr1(const uint64_t x, const bool a, const int n) noexcept
{
    if (a) {
        return x | (a << n);
    }
    return x;
}

uint64_t hr2(const uint64_t x, const bool a, const int n)
{
    return x | ((a ? 1ull : 0) << n);
}

https://godbolt.org/z/gy_65H

hr1(unsigned long, bool, int):
  mov rax, rdi
  test sil, sil
  jne .L4
  ret
.L4:
  mov ecx, edx
  mov esi, 1
  sal esi, cl
  movsx rsi, esi
  or rax, rsi
  ret
hr2(unsigned long, bool, int):
  mov ecx, edx
  movzx esi, sil
  sal rsi, cl
  mov rax, rsi
  or rax, rdi
  ret

Why clang and gcc cannot optimize first function as second?


Viewing all articles
Browse latest Browse all 22137

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>