1 /*
   2  * Copyright (c) 2019, 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.vm.compiler;
  24 
  25 import org.openjdk.jmh.annotations.*;
  26 import org.openjdk.jmh.infra.*;
  27 
  28 import java.util.concurrent.TimeUnit;
  29 
  30 @BenchmarkMode(Mode.AverageTime)
  31 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  32 @State(Scope.Thread)
  33 public class BitSetAndReset {
  34     private static final int COUNT = 10_000;
  35 
  36     private static final long MASK63 = 0x8000_0000_0000_0000L;
  37     private static final long MASK31 = 0x0000_0000_8000_0000L;
  38     private static final long MASK15 = 0x0000_0000_0000_8000L;
  39     private static final long MASK00 = 0x0000_0000_0000_0001L;
  40 
  41     private long andq, orq;
  42     private boolean success = true;
  43 
  44     @TearDown(Level.Iteration)
  45     public void finish() {
  46         if (!success)
  47             throw new AssertionError("Failure while setting or clearing long vector bits!");
  48     }
  49 
  50     @Benchmark
  51     public void bitSet(Blackhole bh) {
  52         for (int i=0; i<COUNT; i++) {
  53             andq = MASK63 | MASK31 | MASK15 | MASK00;
  54             orq = 0;
  55             bh.consume(test63());
  56             bh.consume(test31());
  57             bh.consume(test15());
  58             bh.consume(test00());
  59             success &= andq == 0 && orq == (MASK63 | MASK31 | MASK15 | MASK00);
  60         }
  61     }
  62 
  63     private long test63() {
  64         andq &= ~MASK63;
  65         orq |= MASK63;
  66         return 0L;
  67     }
  68     private long test31() {
  69         andq &= ~MASK31;
  70         orq |= MASK31;
  71         return 0L;
  72     }
  73     private long test15() {
  74         andq &= ~MASK15;
  75         orq |= MASK15;
  76         return 0L;
  77     }
  78     private long test00() {
  79         andq &= ~MASK00;
  80         orq |= MASK00;
  81         return 0L;
  82     }
  83 
  84     private static final long MASK62 = 0x4000_0000_0000_0000L;
  85     private static final long MASK61 = 0x2000_0000_0000_0000L;
  86     private static final long MASK60 = 0x1000_0000_0000_0000L;
  87 
  88     private long orq63, orq62, orq61, orq60;
  89 
  90     @Benchmark
  91     public void throughput(Blackhole bh) {
  92         for (int i=0; i<COUNT; i++) {
  93             orq63 = orq62 = orq61 = orq60 = 0;
  94             bh.consume(testTp());
  95         }
  96     }
  97 
  98     private long testTp() {
  99         orq63 |= MASK63;
 100         orq62 |= MASK62;
 101         orq61 |= MASK61;
 102         orq60 |= MASK60;
 103         return 0L;
 104     }
 105 }