Path: blob/master/test/micro/org/openjdk/bench/vm/compiler/SkipIntToLongCast.java
41161 views
/*1* Copyright (c) BELLSOFT. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package org.openjdk.bench.vm.compiler;2425import org.openjdk.jmh.annotations.Benchmark;26import org.openjdk.jmh.annotations.BenchmarkMode;27import org.openjdk.jmh.annotations.Fork;28import org.openjdk.jmh.annotations.Measurement;29import org.openjdk.jmh.annotations.Mode;30import org.openjdk.jmh.annotations.OutputTimeUnit;31import org.openjdk.jmh.annotations.Scope;32import org.openjdk.jmh.annotations.Setup;33import org.openjdk.jmh.annotations.State;34import org.openjdk.jmh.annotations.Warmup;3536import java.util.concurrent.TimeUnit;3738@BenchmarkMode(Mode.AverageTime)39@OutputTimeUnit(TimeUnit.NANOSECONDS)40@State(Scope.Thread)41@Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)42@Measurement(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)43@Fork(3)44public class SkipIntToLongCast {4546private static final long ARRAYSIZE_L = 40L;4748public int[] intValues;49public int intValue;5051@Setup52public void setup() {53int size = (int)ARRAYSIZE_L;54intValues = new int[size];55for (int i = 0; i < size; i++) {56intValues[i] = i + 1;57}58}5960@Benchmark61public int skipCastTestRight() {62for (int i = 0; i < ARRAYSIZE_L; i++) {63if (intValues[i] == ARRAYSIZE_L) {64return i;65}66}67return 0;68}6970@Benchmark71public int skipCastTestLeft() {72for (int i = 0; i < ARRAYSIZE_L; i++) {73if (ARRAYSIZE_L == intValues[i]) {74return i;75}76}77return 0;78}7980@Benchmark81public long skipMaskedSmallPositiveCast() {82int value = intValue;83return (long)(value & 0x1) ^ (long)(value & 0x3) ^ (long)(value & 0x7) ^ (long)(value & 0xF) ^84(long)(value & 0x1F) ^ (long)(value & 0x3F) ^ (long)(value & 0x7F) ^ (long)(value & 0xFF) ^85(long)(value & 0x1FF) ^ (long)(value & 0x3FF) ^ (long)(value & 0x7FF) ^ (long)(value & 0xFFF) ^86(long)(value & 0x1FFF) ^ (long)(value & 0x3FFF) ^ (long)(value & 0x7FFF) ^ (long)(value & 0xFFFF);87}88}899091