diff options
Diffstat (limited to 'src/main/java/org/linaro/benchmarks/algorithm/BitopsNSieve.java')
-rw-r--r-- | src/main/java/org/linaro/benchmarks/algorithm/BitopsNSieve.java | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/main/java/org/linaro/benchmarks/algorithm/BitopsNSieve.java b/src/main/java/org/linaro/benchmarks/algorithm/BitopsNSieve.java new file mode 100644 index 0000000..8904fe7 --- /dev/null +++ b/src/main/java/org/linaro/benchmarks/algorithm/BitopsNSieve.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2015 Linaro Limited. Ported to Java from: + * https://github.com/WebKit/webkit/blob/master/PerformanceTests/SunSpider/tests/sunspider-1.0.2/bitops-nsieve-bits.js + * + * Description: Bitops prime number sieve, used for finding prime numbers. + * Main Focus: Bitwise operations. + * + */ + +// The Great Computer Language Shootout +// http://shootout.alioth.debian.org/ +// +// Contributed by Ian Osgood + +// http://benchmarksgame.alioth.debian.org/license.html (BSD 3-clause license) +// See NOTICE file for the license + +package org.linaro.benchmarks.algorithm; + +import java.lang.Integer; +import java.lang.System; +import org.openjdk.jmh.annotations.*; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@State(Scope.Benchmark) + +public class BitopsNSieve { + private static final int BITOPS_NSIEVE_EXPECTED = -1431657430; + + // As per the original, this function is not used. + private String pad(int n, int width) { + String s = Integer.toString(n); + while (s.length() < width) { + s = ' ' + s; + } + return s; + } + + private void primes(int[] isPrime, int n) { + // As per the original, this variable can be optimised out. + int count = 0; + final int m = 10000 << n; + final int size = m + 31 >> 5; + + for (int i = 0; i < size; i++) { + isPrime[i] = 0xffffffff; + } + + for (int i = 2; i < m; i++) { + if ((isPrime[i >> 5] & 1 << (i & 31)) != 0) { + for (int j = i + 1; j < m; j += i) { + isPrime[j >> 5] &= ~ (1 << (j & 31)); + } + count++; + } + } + } + + private int[] sieve() { + int[] isPrime = null; + // As per the original, this loop will have just one iteration. + for (int i = 4; i <= 4; i++) { + isPrime = new int[(10000 << i) + 31 >> 5]; + primes(isPrime, i); + } + return isPrime; + } + + @Benchmark + public void jmhTimeBitopsNSieve() { + sieve(); + } +} |