[test-suite] r316918 - [test-suite] Update bitreverse benchmark.
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 30 09:15:19 PDT 2017
Author: maskray
Date: Mon Oct 30 09:15:19 2017
New Revision: 316918
URL: http://llvm.org/viewvc/llvm-project?rev=316918&view=rev
Log:
[test-suite] Update bitreverse benchmark.
Differential Revision: https://reviews.llvm.org/D38962
Modified:
test-suite/trunk/SingleSource/Benchmarks/Misc/revertBits.c
test-suite/trunk/SingleSource/Benchmarks/Misc/revertBits.reference_output
Modified: test-suite/trunk/SingleSource/Benchmarks/Misc/revertBits.c
URL: http://llvm.org/viewvc/llvm-project/test-suite/trunk/SingleSource/Benchmarks/Misc/revertBits.c?rev=316918&r1=316917&r2=316918&view=diff
==============================================================================
--- test-suite/trunk/SingleSource/Benchmarks/Misc/revertBits.c (original)
+++ test-suite/trunk/SingleSource/Benchmarks/Misc/revertBits.c Mon Oct 30 09:15:19 2017
@@ -8,31 +8,36 @@
* new implementation is functionally correct and has a better performance.
*/
#include <stdio.h>
+#include <stdlib.h>
+
#define NUM 0x1000000
-int __attribute__((noinline)) ReverseBits32(int n) {
- n = ((n >> 1) & 0x55555555) | ((n & 0x55555555) << 1);
- n = ((n >> 2) & 0x33333333) | ((n & 0x33333333) << 2);
- n = ((n >> 4) & 0x0F0F0F0F) | ((n & 0x0F0F0F0F) << 4);
+
+unsigned __attribute__((noinline)) ReverseBits32(unsigned n) {
+ n = ((n >> 1) & 0x55555555u) | ((n & 0x55555555u) << 1);
+ n = ((n >> 2) & 0x33333333u) | ((n & 0x33333333u) << 2);
+ n = ((n >> 4) & 0x0F0F0F0Fu) | ((n & 0x0F0F0F0Fu) << 4);
return ((n & 0xff000000u) >> 24) | ((n & 0x00ff0000u) >> 8) |
((n & 0x0000ff00u) << 8) | ((n & 0x000000ffu) << 24);
}
-long long __attribute__((noinline)) ReverseBits64(long long n) {
- n = ((n >> 1) & 0x5555555555555555L) | ((n & 0x5555555555555555L) << 1);
- n = ((n >> 2) & 0x3333333333333333L) | ((n & 0x3333333333333333L) << 2);
- n = ((n >> 4) & 0x0F0F0F0F0F0F0F0FL) | ((n & 0x0F0F0F0F0F0F0F0FL) << 4);
- return ((n & 0xFF00000000000000L) >> 56) |
- ((n & 0x00FF000000000000L) >> 40) |
- ((n & 0x0000FF0000000000L) >> 24) |
- ((n & 0x000000FF00000000L) >> 8) |
- ((n & 0x00000000000000FFL) << 56) |
- ((n & 0x000000000000FF00L) << 40) |
- ((n & 0x0000000000FF0000L) << 24) |
- ((n & 0x00000000FF000000L) << 8);
+unsigned long long __attribute__((noinline)) ReverseBits64(unsigned long long n) {
+ n = ((n >> 1) & 0x5555555555555555ull) | ((n & 0x5555555555555555ull) << 1);
+ n = ((n >> 2) & 0x3333333333333333ull) | ((n & 0x3333333333333333ull) << 2);
+ n = ((n >> 4) & 0x0F0F0F0F0F0F0F0Full) | ((n & 0x0F0F0F0F0F0F0F0Full) << 4);
+ return ((n & 0xFF00000000000000ull) >> 56) |
+ ((n & 0x00FF000000000000ull) >> 40) |
+ ((n & 0x0000FF0000000000ull) >> 24) |
+ ((n & 0x000000FF00000000ull) >> 8) |
+ ((n & 0x00000000000000FFull) << 56) |
+ ((n & 0x000000000000FF00ull) << 40) |
+ ((n & 0x0000000000FF0000ull) << 24) |
+ ((n & 0x00000000FF000000ull) << 8);
}
int main (void) {
- unsigned long long sum = 0;
+ unsigned long long sum32 = 0, sum64 = 0;
+ unsigned int rev32 = strtoll("0x12345678", NULL, 16);
+ unsigned long long rev64 = strtoll("0x0123456789012345", NULL, 16);
// Check for compilers that don't support __has_builtin
#ifndef __has_builtin
@@ -42,21 +47,21 @@ int main (void) {
// Check for compilers that support __has_builtin but not __builtin_bitreverse*
#if (!__has_builtin(__builtin_bitreverse32) ||\
!__has_builtin(__builtin_bitreverse64))
- printf("Sum1 = 0, Sum2 = feff800000800000\n");
+ printf("0x%x -> 0x%x\n", rev32, ReverseBits32(rev32));
+ printf("0x%llx -> 0x%llx\n", rev64, ReverseBits64(rev64));
return 0;
#else
for (int i = 0; i < NUM; ++i) {
- sum += ReverseBits32(__builtin_bitreverse32(i));
- sum -= ReverseBits64(__builtin_bitreverse64(i));
+ sum32 += ReverseBits32(i);
+ sum64 += ReverseBits64(i);
}
- printf("Sum1 = %llx, ", sum);
-
for (int i = 0; i < NUM; ++i) {
- sum -= ReverseBits64(ReverseBits64(0x100000000 + i));
+ sum32 -= __builtin_bitreverse32(i);
+ sum64 -= __builtin_bitreverse64(i);
}
- printf("Sum2 = %llx\n", sum);
-
- return 0;
+ printf("0x%x -> 0x%x\n", rev32, __builtin_bitreverse32(rev32));
+ printf("0x%llx -> 0x%llx\n", rev64, __builtin_bitreverse64(rev64));
+ return sum32 == 0 && sum64 == 0 ? 0 : 1;
#endif
}
Modified: test-suite/trunk/SingleSource/Benchmarks/Misc/revertBits.reference_output
URL: http://llvm.org/viewvc/llvm-project/test-suite/trunk/SingleSource/Benchmarks/Misc/revertBits.reference_output?rev=316918&r1=316917&r2=316918&view=diff
==============================================================================
--- test-suite/trunk/SingleSource/Benchmarks/Misc/revertBits.reference_output (original)
+++ test-suite/trunk/SingleSource/Benchmarks/Misc/revertBits.reference_output Mon Oct 30 09:15:19 2017
@@ -1,2 +1,3 @@
-Sum1 = 0, Sum2 = feff800000800000
+0x12345678 -> 0x1e6a2c48
+0x123456789012345 -> 0xa2c48091e6a2c480
exit 0
More information about the llvm-commits
mailing list