Skip to contentSkip to conversation

[5592] target-ppc: optimize popcntb

Qemu-devel Aurelien Jarno 3 participants 5 messages Nov 1, 2008

lore
Share & tools
lore
#0 lore raw

Revision: 5592

          http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5592
Author:   aurel32
Date:     2008-11-01 00:54:33 +0000 (Sat, 01 Nov 2008)

Log Message:
-----------
target-ppc: optimize popcntb

Suggested by Andrzej Zaborowski.

Modified Paths:
--------------
trunk/target-ppc/op_helper.c

Modified: trunk/target-ppc/op_helper.c
===================================================================

target-ppc/op_helper.c +8 -14
@@ -222,25 +222,19 @@
 
 target_ulong helper_popcntb (target_ulong val)
 {
-    uint32_t ret;
-    int i;
-
-    ret = 0;
-    for (i = 0; i < 32; i += 8)
-        ret |= ctpop8((val >> i) & 0xFF) << i;
-    return ret;
+    val = (val & 0x55555555) + ((val >>  1) & 0x55555555);
+    val = (val & 0x33333333) + ((val >>  2) & 0x33333333);
+    val = (val & 0x0f0f0f0f) + ((val >>  4) & 0x0f0f0f0f);
+    return val;
 }
 
 #if defined(TARGET_PPC64)
 target_ulong helper_popcntb_64 (target_ulong val)
 {
-    uint64_t ret;
-    int i;
-
-    ret = 0;
-    for (i = 0; i < 64; i += 8)
-        ret |= ctpop8((val >> i) & 0xFF) << i;
-    return ret;
+    val = (val & 0x5555555555555555ULL) + ((val >>  1) & 0x5555555555555555ULL);
+    val = (val & 0x3333333333333333ULL) + ((val >>  2) & 0x3333333333333333ULL);
+    val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >>  4) & 0x0f0f0f0f0f0f0f0fULL);
+    return val;
 }
 #endif
 
#1 lore raw
Quoted 2 lines · Aurelien Jarno

Revision: 5592

         http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5592

[...]

Quoted 27 lines

Modified: trunk/target-ppc/op_helper.c
===================================================================

target-ppc/op_helper.c 2008-11-01 00:54:33 UTC (rev 5592) renamed from target-ppc/op_helper.c 2008-11-01 00:54:23 UTC (rev 5591) +4 -10
@@ -222,25 +222,19 @@
 
 target_ulong helper_popcntb (target_ulong val)
 {
-    uint32_t ret;
-    int i;
-
-    ret = 0;
-    for (i = 0; i < 32; i += 8)
-        ret |= ctpop8((val >> i) & 0xFF) << i;
-    return ret;
+    val = (val & 0x55555555) + ((val >>  1) & 0x55555555);
+    val = (val & 0x33333333) + ((val >>  2) & 0x33333333);
+    val = (val & 0x0f0f0f0f) + ((val >>  4) & 0x0f0f0f0f);
+    return val;
 }
 
 #if defined(TARGET_PPC64)
 target_ulong helper_popcntb_64 (target_ulong val)
 {
-    uint64_t ret;
-    int i;
-

target-ppc/op_helper.c 2008-11-01 00:54:33 UTC (rev 5592) line 239

Quoted 10 lines
target-ppc/op_helper.c 2008-11-01 00:54:33 UTC (rev 5592) renamed from target-ppc/op_helper.c 2008-11-01 00:54:23 UTC (rev 5591) +4 -4
-    ret = 0;
-    for (i = 0; i < 64; i += 8)
-        ret |= ctpop8((val >> i) & 0xFF) << i;
-    return ret;
+    val = (val & 0x5555555555555555ULL) + ((val >>  1) & 0x5555555555555555ULL);
+    val = (val & 0x3333333333333333ULL) + ((val >>  2) & 0x3333333333333333ULL);
+    val = (val & 0x0f0f0f0f0f0f0f0fULL) + ((val >>  4) & 0x0f0f0f0f0f0f0f0fULL);
+    return val;
 }
 #endif

Wouldn't it make sense to use builtin's as is done in host-utils.h?

Laurent

#2 lore raw
Quoted 2 lines · Laurent Desnogues

Wouldn't it make sense to use builtin's as is done in host-utils.h?

Forget that, I thought it was traditional bit counting.

Laurent

#3 lore raw

2008/11/1 Laurent Desnogues <[email protected]>:

Quoted 6 lines
Quoted 2 lines · Laurent Desnogues

Wouldn't it make sense to use builtin's as is done in host-utils.h?

Forget that, I thought it was traditional bit counting.

On ppc host there might be a builtin for it, on the x86 Xeon cpus with SSE4 there's also a bitcounting instruction but this approach is actually faster than transferring the number to the MMX register, running the instruction and copying the value back. In the benchmarks I've seen the speed is comparable with table lookup on x86 and -O3.

Cheers

#4 lore raw
Quoted 6 lines · andrzej zaborowski

On ppc host there might be a builtin for it, on the x86 Xeon cpus with SSE4 there's also a bitcounting instruction but this approach is actually faster than transferring the number to the MMX register, running the instruction and copying the value back. In the benchmarks I've seen the speed is comparable with table lookup on x86 and -O3.

Bit tricks are always very sensitive. BTW I would not trust any benchmark that run them in loop for obvious reasons :-)

For those who like that kind of thing, here are some nice refs:

- Knuth: http://www-cs-faculty.stanford.edu/~uno/fasc1a.ps.gz
- Anderson: http://www-graphics.stanford.edu/~seander/bithacks.html
- Arndt: http://www.jjj.de/bitwizardry/bitwizardrypage.html

Many of these tricks are known or obvious, but it's good reading anyway, especially for qemu target and back-end writers.

Laurent