Skip to content

Instantly share code, notes, and snippets.

@mikeb01
mikeb01 / PopCntBenchmark.java
Created August 30, 2012 10:46
PopCnt Benchmark
public class PopCntBenchmark extends SimpleBenchmark
{
public int timeIntegerBitCount(int iterations)
{
int result = 0;
for (int i = 0; i < iterations; i++)
{
result += Integer.bitCount(i);
}
return result;
@mikeb01
mikeb01 / intrinsic.asm
Created August 30, 2012 09:49
Intrinsic Assembly
popcnt %r11d,%ebx
add 0x8(%rsi),%ebx ;*iadd
; - PopCntTest::main@12 (line 9)
@mikeb01
mikeb01 / popcnt.asm
Created August 30, 2012 09:39
Generated assembly
mov %r11d,%r8d
shr %r8d
and $0x55555555,%r8d
sub %r8d,%r11d ;*isub
; - java.lang.Integer::bitCount@7 (line 1143)
; - PopCntTest::main@9 (line 9)
mov %r11d,%r8d
shr $0x2,%r8d
and $0x33333333,%r11d
and $0x33333333,%r8d
@mikeb01
mikeb01 / Integer.java
Created August 30, 2012 09:30
Integer.bitCount Implementation
public final class Integer
{
public static int bitCount(int i)
{
// HD, Figure 5-2
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
@mikeb01
mikeb01 / PopCntTest.java
Created August 30, 2012 09:18
POPCNT Demo
public class PopCntTest
{
public static void main(String[] args)
{
int result = 0;
for (int i = 0; i < 1000000000; i++)
{
result += Integer.bitCount(i);
}
package com.lmax.disruptor;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
extern "C" {
/* from sys/osfmk/i386/mp.c */
extern void mp_rendezvous(void (*setup_func)(void *),
void (*action_func)(void *),
void (*teardown_func)(void *),
void *arg);
extern int cpu_number(void);
}
#include <stdio.h>
static void cpuid(int op1, int op2, int *data) {
asm("cpuid"
: "=a" (data[0]), "=b" (data[1]), "=c" (data[2]), "=d" (data[3])
: "a"(op1), "c"(op2));
}
int main (int argc, const char * argv[]) {
int values[4];
#include <stdio.h>
static void cpuid(int op1, int op2, int *data) {
asm("cpuid"
: "=a" (data[0]), "=b" (data[1]), "=c" (data[3]), "=d" (data[2])
: "a"(op1), "c"(op2));
}
int main(int argc, char** argv) {
int values[5];
public void serialisePublishing(final long sequence,
final Sequence cursor,
final int batchSize) {
// Guard condition, limit the number of pending publications
int counter = RETRIES;
while (sequence - cursor.get() > pendingPublication.length()) {
if (--counter == 0) {
Thread.yield();
counter = RETRIES;