1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.openjdk.bench.java.util.concurrent; 24 25 26 /** 27 * Generic problem for concurrency tests. 28 * 29 * @author Aleksey Shipilev (aleksey.shipilev@oracle.com) 30 */ 31 public class Problem { 32 33 /* 34 * Implementation notes: 35 * 36 * This problem makes its bidding to confuse loop unrolling and CSE, and as such break loop optimizations. 37 * Should loop optimizations be allowed, the performance with different (l, r) could change non-linearly. 38 */ 39 40 private final int[] data; 41 private final int size; 42 43 public Problem(int size) { 44 this.size = size; 45 data = new int[size]; 46 } 47 48 public long solve() { 49 return solve(0, size); 50 } 51 52 public long solve(int l, int r) { 53 long sum = 0; 54 for (int c = l; c < r; c++) { 55 int v = hash(data[c]); 56 if (filter(v)) { 57 sum += v; 58 } 59 } 60 return sum; 61 } 62 63 public int size() { 64 return size; 65 } 66 67 public static int hash(int x) { 68 x ^= (x << 21); 69 x ^= (x >>> 31); 70 x ^= (x << 4); 71 return x; 72 } 73 74 public static boolean filter(int i) { 75 return ((i & 0b101) == 0); 76 } 77 78 }