Skip to content
Snippets Groups Projects
Commit ad1944c8 authored by David Majnemer's avatar David Majnemer
Browse files

[immintrin] Reimplement _bit_scan_{forward,reverse}

There is no need to use a target-specific intrinsic to implement
_bit_scan_forward or _bit_scan_reverse, reimplementing them using
generic intrinsics makes it more likely that the middle end will
understand what's going on.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@272564 91177308-0d34-0410-b5e6-96231b3b80d8
parent f0c013a4
No related branches found
No related tags found
No related merge requests found
...@@ -172,13 +172,13 @@ _rdrand32_step(unsigned int *__p) ...@@ -172,13 +172,13 @@ _rdrand32_step(unsigned int *__p)
/* __bit_scan_forward */ /* __bit_scan_forward */
static __inline__ int __attribute__((__always_inline__, __nodebug__)) static __inline__ int __attribute__((__always_inline__, __nodebug__))
_bit_scan_forward(int __A) { _bit_scan_forward(int __A) {
return __builtin_ia32_bit_scan_forward(__A); return __builtin_ctz(__A);
} }
/* __bit_scan_reverse */ /* __bit_scan_reverse */
static __inline__ int __attribute__((__always_inline__, __nodebug__)) static __inline__ int __attribute__((__always_inline__, __nodebug__))
_bit_scan_reverse(int __A) { _bit_scan_reverse(int __A) {
return __builtin_ia32_bit_scan_reverse(__A); return 31 - __builtin_clz(__A);
} }
#ifdef __x86_64__ #ifdef __x86_64__
......
...@@ -7,11 +7,13 @@ ...@@ -7,11 +7,13 @@
int test_bit_scan_forward(int a) { int test_bit_scan_forward(int a) {
return _bit_scan_forward(a); return _bit_scan_forward(a);
// CHECK: @test_bit_scan_forward // CHECK: @test_bit_scan_forward
// CHECK: call i32 @llvm.x86.bit.scan.forward // CHECK: %[[call:.*]] = call i32 @llvm.cttz.i32(
// CHECK: ret i32 %[[call]]
} }
int test_bit_scan_reverse(int a) { int test_bit_scan_reverse(int a) {
return _bit_scan_reverse(a); return _bit_scan_reverse(a);
// CHECK: @test_bit_scan_reverse // CHECK: %[[call:.*]] = call i32 @llvm.ctlz.i32(
// CHECK: call i32 @llvm.x86.bit.scan.reverse // CHECK: %[[sub:.*]] = sub nsw i32 31, %2
// CHECK: ret i32 %[[sub]]
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment