Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BitUtils: cleanup constexpr usage for msvc clz #9435

Merged
merged 1 commit into from Jan 10, 2021
Merged

BitUtils: cleanup constexpr usage for msvc clz #9435

merged 1 commit into from Jan 10, 2021

Conversation

shuffle2
Copy link
Contributor

@shuffle2 shuffle2 commented Jan 10, 2021

  • Can use _BitScanReverse{64} for all archs we support with msvc
  • Can actually support being used in constexpr evaluation contexts

see https://developercommunity.visualstudio.com/idea/434712/make-intrinsic-functions-supported-in-a-constexpr.html

here is a small test: compiling the following with cl /Ox /std:c++latest clz.cpp /link /debug

int main() {
    const uint32_t val32 = 9;
    const uint64_t val64 = 9;
    int clz1 = CountLeadingZeros(val32);
    int clz2 = CountLeadingZeros(val64);
    constexpr int clz3 = CountLeadingZeros(val32);
    constexpr int clz4 = CountLeadingZeros(val64);
    printf("%d %d %d %d\n", clz1,  clz2,  clz3,  clz4);
    return 0;
}

arm64:

int __cdecl main(int argc, const char **argv, const char **envp)
{
  unsigned int v3; // w10
  __int64 v4; // x1
  unsigned int v5; // w10
  __int64 v6; // x2

  v3 = __clz(9u);
  if ( 1 == (unsigned __int8)(v3 >> 5) )
    v4 = 32i64;
  else
    v4 = v3;
  v5 = __clz(9ui64);
  if ( 1 == (unsigned __int8)(v5 >> 6) )
    v6 = 64i64;
  else
    v6 = v5;
  sub_140003F94("%d %d %d %d\n", v4, v6, 28i64, 60i64);
  return 0;
}

x64:

int __cdecl main(int argc, const char **argv, const char **envp)
{
  unsigned __int64 v3; // rax
  __int64 v4; // rdx
  int v6; // [rsp+20h] [rbp-18h]

  v6 = 60;
  _BitScanReverse((unsigned int *)&v3, 9u);
  v4 = (unsigned int)(31 - v3);
  _BitScanReverse64(&v3, 9ui64);
  j_printf("%d %d %d %d\n", v4, (unsigned int)(63 - v3), 28i64, v6);
  return 0;
}

@lioncash lioncash merged commit 79a234e into dolphin-emu:master Jan 10, 2021
@shuffle2 shuffle2 deleted the constexpr-error branch January 10, 2021 21:19
@JosJuice JosJuice mentioned this pull request Jan 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants