1 /*
   2  * Copyright (c) 2018, 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
  21  * questions.
  22  */
  23 
  24 package benchmark.jdk.incubator.vector;
  25 
  26 import java.util.concurrent.TimeUnit;
  27 import java.util.function.IntFunction;
  28 
  29 import org.openjdk.jmh.annotations.*;
  30 import org.openjdk.jmh.infra.Blackhole;
  31 
  32 @BenchmarkMode(Mode.Throughput)
  33 @OutputTimeUnit(TimeUnit.MILLISECONDS)
  34 @State(Scope.Benchmark)
  35 @Warmup(iterations = 3, time = 1)
  36 @Measurement(iterations = 5, time = 1)
  37 @Fork(value = 1, jvmArgsPrepend = {"--add-modules=jdk.incubator.vector"})
  38 public class ShortScalar extends AbstractVectorBenchmark {
  39     static final int INVOC_COUNT = 1; // To align with vector benchmarks.
  40 
  41     @Param("1024")
  42     int size;
  43 
  44     short[] fill(IntFunction<Short> f) {
  45         short[] array = new short[size];
  46         for (int i = 0; i < array.length; i++) {
  47             array[i] = f.apply(i);
  48         }
  49         return array;
  50     }
  51 
  52     short[] as, bs, cs, rs;
  53     boolean[] ms, rms;
  54     int[] ss;
  55 
  56     @Setup
  57     public void init() {
  58         as = fill(i -> (short)(2*i));
  59         bs = fill(i -> (short)(i+1));
  60         cs = fill(i -> (short)(i+5));
  61         rs = fill(i -> (short)0);
  62         ms = fillMask(size, i -> (i % 2) == 0);
  63         rms = fillMask(size, i -> false);
  64 
  65         ss = fillInt(size, i -> RANDOM.nextInt(Math.max(i,1)));
  66     }
  67 
  68     final IntFunction<short[]> fa = vl -> as;
  69     final IntFunction<short[]> fb = vl -> bs;
  70     final IntFunction<short[]> fc = vl -> cs;
  71     final IntFunction<short[]> fr = vl -> rs;
  72     final IntFunction<boolean[]> fm = vl -> ms;
  73     final IntFunction<boolean[]> fmr = vl -> rms;
  74     final IntFunction<int[]> fs = vl -> ss;
  75 
  76 
  77     @Benchmark
  78     public void add(Blackhole bh) {
  79         short[] as = fa.apply(size);
  80         short[] bs = fb.apply(size);
  81         short[] rs = fr.apply(size);
  82 
  83         for (int ic = 0; ic < INVOC_COUNT; ic++) {
  84             for (int i = 0; i < as.length; i++) {
  85                 short a = as[i];
  86                 short b = bs[i];
  87                 rs[i] = (short)(a + b);
  88             }
  89         }
  90 
  91         bh.consume(rs);
  92     }
  93 
  94     @Benchmark
  95     public void addMasked(Blackhole bh) {
  96         short[] as = fa.apply(size);
  97         short[] bs = fb.apply(size);
  98         short[] rs = fr.apply(size);
  99         boolean[] ms = fm.apply(size);
 100 
 101         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 102             for (int i = 0; i < as.length; i++) {
 103                 short a = as[i];
 104                 short b = bs[i];
 105                 if (ms[i % ms.length]) {
 106                     rs[i] = (short)(a + b);
 107                 } else {
 108                     rs[i] = a;
 109                 }
 110             }
 111         }
 112         bh.consume(rs);
 113     }
 114 
 115     @Benchmark
 116     public void sub(Blackhole bh) {
 117         short[] as = fa.apply(size);
 118         short[] bs = fb.apply(size);
 119         short[] rs = fr.apply(size);
 120 
 121         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 122             for (int i = 0; i < as.length; i++) {
 123                 short a = as[i];
 124                 short b = bs[i];
 125                 rs[i] = (short)(a - b);
 126             }
 127         }
 128 
 129         bh.consume(rs);
 130     }
 131 
 132     @Benchmark
 133     public void subMasked(Blackhole bh) {
 134         short[] as = fa.apply(size);
 135         short[] bs = fb.apply(size);
 136         short[] rs = fr.apply(size);
 137         boolean[] ms = fm.apply(size);
 138 
 139         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 140             for (int i = 0; i < as.length; i++) {
 141                 short a = as[i];
 142                 short b = bs[i];
 143                 if (ms[i % ms.length]) {
 144                     rs[i] = (short)(a - b);
 145                 } else {
 146                     rs[i] = a;
 147                 }
 148             }
 149         }
 150         bh.consume(rs);
 151     }
 152 
 153 
 154 
 155     @Benchmark
 156     public void mul(Blackhole bh) {
 157         short[] as = fa.apply(size);
 158         short[] bs = fb.apply(size);
 159         short[] rs = fr.apply(size);
 160 
 161         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 162             for (int i = 0; i < as.length; i++) {
 163                 short a = as[i];
 164                 short b = bs[i];
 165                 rs[i] = (short)(a * b);
 166             }
 167         }
 168 
 169         bh.consume(rs);
 170     }
 171 
 172     @Benchmark
 173     public void mulMasked(Blackhole bh) {
 174         short[] as = fa.apply(size);
 175         short[] bs = fb.apply(size);
 176         short[] rs = fr.apply(size);
 177         boolean[] ms = fm.apply(size);
 178 
 179         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 180             for (int i = 0; i < as.length; i++) {
 181                 short a = as[i];
 182                 short b = bs[i];
 183                 if (ms[i % ms.length]) {
 184                     rs[i] = (short)(a * b);
 185                 } else {
 186                     rs[i] = a;
 187                 }
 188             }
 189         }
 190         bh.consume(rs);
 191     }
 192 
 193 
 194     @Benchmark
 195     public void and(Blackhole bh) {
 196         short[] as = fa.apply(size);
 197         short[] bs = fb.apply(size);
 198         short[] rs = fr.apply(size);
 199 
 200         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 201             for (int i = 0; i < as.length; i++) {
 202                 short a = as[i];
 203                 short b = bs[i];
 204                 rs[i] = (short)(a & b);
 205             }
 206         }
 207 
 208         bh.consume(rs);
 209     }
 210 
 211 
 212 
 213     @Benchmark
 214     public void andMasked(Blackhole bh) {
 215         short[] as = fa.apply(size);
 216         short[] bs = fb.apply(size);
 217         short[] rs = fr.apply(size);
 218         boolean[] ms = fm.apply(size);
 219 
 220         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 221             for (int i = 0; i < as.length; i++) {
 222                 short a = as[i];
 223                 short b = bs[i];
 224                 if (ms[i % ms.length]) {
 225                     rs[i] = (short)(a & b);
 226                 } else {
 227                     rs[i] = a;
 228                 }
 229             }
 230         }
 231         bh.consume(rs);
 232     }
 233 
 234 
 235 
 236     @Benchmark
 237     public void or(Blackhole bh) {
 238         short[] as = fa.apply(size);
 239         short[] bs = fb.apply(size);
 240         short[] rs = fr.apply(size);
 241 
 242         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 243             for (int i = 0; i < as.length; i++) {
 244                 short a = as[i];
 245                 short b = bs[i];
 246                 rs[i] = (short)(a | b);
 247             }
 248         }
 249 
 250         bh.consume(rs);
 251     }
 252 
 253 
 254 
 255     @Benchmark
 256     public void orMasked(Blackhole bh) {
 257         short[] as = fa.apply(size);
 258         short[] bs = fb.apply(size);
 259         short[] rs = fr.apply(size);
 260         boolean[] ms = fm.apply(size);
 261 
 262         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 263             for (int i = 0; i < as.length; i++) {
 264                 short a = as[i];
 265                 short b = bs[i];
 266                 if (ms[i % ms.length]) {
 267                     rs[i] = (short)(a | b);
 268                 } else {
 269                     rs[i] = a;
 270                 }
 271             }
 272         }
 273         bh.consume(rs);
 274     }
 275 
 276 
 277 
 278     @Benchmark
 279     public void xor(Blackhole bh) {
 280         short[] as = fa.apply(size);
 281         short[] bs = fb.apply(size);
 282         short[] rs = fr.apply(size);
 283 
 284         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 285             for (int i = 0; i < as.length; i++) {
 286                 short a = as[i];
 287                 short b = bs[i];
 288                 rs[i] = (short)(a ^ b);
 289             }
 290         }
 291 
 292         bh.consume(rs);
 293     }
 294 
 295 
 296 
 297     @Benchmark
 298     public void xorMasked(Blackhole bh) {
 299         short[] as = fa.apply(size);
 300         short[] bs = fb.apply(size);
 301         short[] rs = fr.apply(size);
 302         boolean[] ms = fm.apply(size);
 303 
 304         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 305             for (int i = 0; i < as.length; i++) {
 306                 short a = as[i];
 307                 short b = bs[i];
 308                 if (ms[i % ms.length]) {
 309                     rs[i] = (short)(a ^ b);
 310                 } else {
 311                     rs[i] = a;
 312                 }
 313             }
 314         }
 315         bh.consume(rs);
 316     }
 317 
 318 
 319 
 320 
 321 
 322 
 323 
 324     @Benchmark
 325     public void shiftLeft(Blackhole bh) {
 326         short[] as = fa.apply(size);
 327         short[] bs = fb.apply(size);
 328         short[] rs = fr.apply(size);
 329 
 330         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 331             for (int i = 0; i < as.length; i++) {
 332                 short a = as[i];
 333                 short b = bs[i];
 334                 rs[i] = (short)((a << (b & 0xF)));
 335             }
 336         }
 337 
 338         bh.consume(rs);
 339     }
 340 
 341 
 342 
 343     @Benchmark
 344     public void shiftLeftMasked(Blackhole bh) {
 345         short[] as = fa.apply(size);
 346         short[] bs = fb.apply(size);
 347         short[] rs = fr.apply(size);
 348         boolean[] ms = fm.apply(size);
 349 
 350         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 351             for (int i = 0; i < as.length; i++) {
 352                 short a = as[i];
 353                 short b = bs[i];
 354                 if (ms[i % ms.length]) {
 355                     rs[i] = (short)((a << (b & 0xF)));
 356                 } else {
 357                     rs[i] = a;
 358                 }
 359             }
 360         }
 361         bh.consume(rs);
 362     }
 363 
 364 
 365 
 366 
 367 
 368 
 369 
 370     @Benchmark
 371     public void shiftRight(Blackhole bh) {
 372         short[] as = fa.apply(size);
 373         short[] bs = fb.apply(size);
 374         short[] rs = fr.apply(size);
 375 
 376         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 377             for (int i = 0; i < as.length; i++) {
 378                 short a = as[i];
 379                 short b = bs[i];
 380                 rs[i] = (short)((a >>> (b & 0xF)));
 381             }
 382         }
 383 
 384         bh.consume(rs);
 385     }
 386 
 387 
 388 
 389     @Benchmark
 390     public void shiftRightMasked(Blackhole bh) {
 391         short[] as = fa.apply(size);
 392         short[] bs = fb.apply(size);
 393         short[] rs = fr.apply(size);
 394         boolean[] ms = fm.apply(size);
 395 
 396         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 397             for (int i = 0; i < as.length; i++) {
 398                 short a = as[i];
 399                 short b = bs[i];
 400                 if (ms[i % ms.length]) {
 401                     rs[i] = (short)((a >>> (b & 0xF)));
 402                 } else {
 403                     rs[i] = a;
 404                 }
 405             }
 406         }
 407         bh.consume(rs);
 408     }
 409 
 410 
 411 
 412 
 413 
 414 
 415 
 416     @Benchmark
 417     public void shiftArithmeticRight(Blackhole bh) {
 418         short[] as = fa.apply(size);
 419         short[] bs = fb.apply(size);
 420         short[] rs = fr.apply(size);
 421 
 422         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 423             for (int i = 0; i < as.length; i++) {
 424                 short a = as[i];
 425                 short b = bs[i];
 426                 rs[i] = (short)((a >> (b & 0xF)));
 427             }
 428         }
 429 
 430         bh.consume(rs);
 431     }
 432 
 433 
 434 
 435     @Benchmark
 436     public void shiftArithmeticRightMasked(Blackhole bh) {
 437         short[] as = fa.apply(size);
 438         short[] bs = fb.apply(size);
 439         short[] rs = fr.apply(size);
 440         boolean[] ms = fm.apply(size);
 441 
 442         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 443             for (int i = 0; i < as.length; i++) {
 444                 short a = as[i];
 445                 short b = bs[i];
 446                 if (ms[i % ms.length]) {
 447                     rs[i] = (short)((a >> (b & 0xF)));
 448                 } else {
 449                     rs[i] = a;
 450                 }
 451             }
 452         }
 453         bh.consume(rs);
 454     }
 455 
 456 
 457 
 458 
 459 
 460 
 461 
 462     @Benchmark
 463     public void shiftLeftShift(Blackhole bh) {
 464         short[] as = fa.apply(size);
 465         short[] bs = fb.apply(size);
 466         short[] rs = fr.apply(size);
 467 
 468         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 469             for (int i = 0; i < as.length; i++) {
 470                 short a = as[i];
 471                 short b = bs[i];
 472                 rs[i] = (short)((a << (b & 15)));
 473             }
 474         }
 475 
 476         bh.consume(rs);
 477     }
 478 
 479 
 480 
 481     @Benchmark
 482     public void shiftLeftMaskedShift(Blackhole bh) {
 483         short[] as = fa.apply(size);
 484         short[] bs = fb.apply(size);
 485         short[] rs = fr.apply(size);
 486         boolean[] ms = fm.apply(size);
 487 
 488         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 489             for (int i = 0; i < as.length; i++) {
 490                 short a = as[i];
 491                 short b = bs[i];
 492                 boolean m = ms[i % ms.length];
 493                 rs[i] = (m ? (short)((a << (b & 15))) : a);
 494             }
 495         }
 496 
 497         bh.consume(rs);
 498     }
 499 
 500 
 501 
 502 
 503 
 504 
 505 
 506     @Benchmark
 507     public void shiftRightShift(Blackhole bh) {
 508         short[] as = fa.apply(size);
 509         short[] bs = fb.apply(size);
 510         short[] rs = fr.apply(size);
 511 
 512         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 513             for (int i = 0; i < as.length; i++) {
 514                 short a = as[i];
 515                 short b = bs[i];
 516                 rs[i] = (short)(((a & 0xFFFF) >>> (b & 15)));
 517             }
 518         }
 519 
 520         bh.consume(rs);
 521     }
 522 
 523 
 524 
 525     @Benchmark
 526     public void shiftRightMaskedShift(Blackhole bh) {
 527         short[] as = fa.apply(size);
 528         short[] bs = fb.apply(size);
 529         short[] rs = fr.apply(size);
 530         boolean[] ms = fm.apply(size);
 531 
 532         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 533             for (int i = 0; i < as.length; i++) {
 534                 short a = as[i];
 535                 short b = bs[i];
 536                 boolean m = ms[i % ms.length];
 537                 rs[i] = (m ? (short)(((a & 0xFFFF) >>> (b & 15))) : a);
 538             }
 539         }
 540 
 541         bh.consume(rs);
 542     }
 543 
 544 
 545 
 546 
 547 
 548 
 549 
 550     @Benchmark
 551     public void shiftArithmeticRightShift(Blackhole bh) {
 552         short[] as = fa.apply(size);
 553         short[] bs = fb.apply(size);
 554         short[] rs = fr.apply(size);
 555 
 556         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 557             for (int i = 0; i < as.length; i++) {
 558                 short a = as[i];
 559                 short b = bs[i];
 560                 rs[i] = (short)((a >> (b & 15)));
 561             }
 562         }
 563 
 564         bh.consume(rs);
 565     }
 566 
 567 
 568 
 569     @Benchmark
 570     public void shiftArithmeticRightMaskedShift(Blackhole bh) {
 571         short[] as = fa.apply(size);
 572         short[] bs = fb.apply(size);
 573         short[] rs = fr.apply(size);
 574         boolean[] ms = fm.apply(size);
 575 
 576         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 577             for (int i = 0; i < as.length; i++) {
 578                 short a = as[i];
 579                 short b = bs[i];
 580                 boolean m = ms[i % ms.length];
 581                 rs[i] = (m ? (short)((a >> (b & 15))) : a);
 582             }
 583         }
 584 
 585         bh.consume(rs);
 586     }
 587 
 588 
 589     @Benchmark
 590     public void max(Blackhole bh) {
 591         short[] as = fa.apply(size);
 592         short[] bs = fb.apply(size);
 593         short[] rs = fr.apply(size);
 594 
 595         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 596             for (int i = 0; i < as.length; i++) {
 597                 short a = as[i];
 598                 short b = bs[i];
 599                 rs[i] = (short)(Math.max(a, b));
 600             }
 601         }
 602 
 603         bh.consume(rs);
 604     }
 605 
 606     @Benchmark
 607     public void min(Blackhole bh) {
 608         short[] as = fa.apply(size);
 609         short[] bs = fb.apply(size);
 610         short[] rs = fr.apply(size);
 611 
 612         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 613             for (int i = 0; i < as.length; i++) {
 614                 short a = as[i];
 615                 short b = bs[i];
 616                 rs[i] = (short)(Math.min(a, b));
 617             }
 618         }
 619 
 620         bh.consume(rs);
 621     }
 622 
 623 
 624     @Benchmark
 625     public void andLanes(Blackhole bh) {
 626         short[] as = fa.apply(size);
 627         short r = -1;
 628         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 629             r = -1;
 630             for (int i = 0; i < as.length; i++) {
 631                 r &= as[i];
 632             }
 633         }
 634         bh.consume(r);
 635     }
 636 
 637 
 638 
 639     @Benchmark
 640     public void orLanes(Blackhole bh) {
 641         short[] as = fa.apply(size);
 642         short r = 0;
 643         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 644             r = 0;
 645             for (int i = 0; i < as.length; i++) {
 646                 r |= as[i];
 647             }
 648         }
 649         bh.consume(r);
 650     }
 651 
 652 
 653 
 654     @Benchmark
 655     public void xorLanes(Blackhole bh) {
 656         short[] as = fa.apply(size);
 657         short r = 0;
 658         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 659             r = 0;
 660             for (int i = 0; i < as.length; i++) {
 661                 r ^= as[i];
 662             }
 663         }
 664         bh.consume(r);
 665     }
 666 
 667 
 668     @Benchmark
 669     public void addLanes(Blackhole bh) {
 670         short[] as = fa.apply(size);
 671         short r = 0;
 672         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 673             r = 0;
 674             for (int i = 0; i < as.length; i++) {
 675                 r += as[i];
 676             }
 677         }
 678         bh.consume(r);
 679     }
 680 
 681     @Benchmark
 682     public void mulLanes(Blackhole bh) {
 683         short[] as = fa.apply(size);
 684         short r = 1;
 685         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 686             r = 1;
 687             for (int i = 0; i < as.length; i++) {
 688                 r *= as[i];
 689             }
 690         }
 691         bh.consume(r);
 692     }
 693 
 694     @Benchmark
 695     public void minLanes(Blackhole bh) {
 696         short[] as = fa.apply(size);
 697         short r = Short.MAX_VALUE;
 698         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 699             r = Short.MAX_VALUE;
 700             for (int i = 0; i < as.length; i++) {
 701                 r = (short)Math.min(r, as[i]);
 702             }
 703         }
 704         bh.consume(r);
 705     }
 706 
 707     @Benchmark
 708     public void maxLanes(Blackhole bh) {
 709         short[] as = fa.apply(size);
 710         short r = Short.MIN_VALUE;
 711         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 712             r = Short.MIN_VALUE;
 713             for (int i = 0; i < as.length; i++) {
 714                 r = (short)Math.max(r, as[i]);
 715             }
 716         }
 717         bh.consume(r);
 718     }
 719 
 720 
 721     @Benchmark
 722     public void anyTrue(Blackhole bh) {
 723         boolean[] ms = fm.apply(size);
 724         boolean r = false;
 725         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 726             r = false;
 727             for (int i = 0; i < ms.length; i++) {
 728                 r |= ms[i];
 729             }
 730         }
 731         bh.consume(r);
 732     }
 733 
 734 
 735 
 736     @Benchmark
 737     public void allTrue(Blackhole bh) {
 738         boolean[] ms = fm.apply(size);
 739         boolean r = true;
 740         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 741             r = true;
 742             for (int i = 0; i < ms.length; i++) {
 743                 r &= ms[i];
 744             }
 745         }
 746         bh.consume(r);
 747     }
 748 
 749 
 750     @Benchmark
 751     public void lessThan(Blackhole bh) {
 752         short[] as = fa.apply(size);
 753         short[] bs = fb.apply(size);
 754 
 755         boolean r = false;
 756         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 757             r = false;
 758             for (int i = 0; i < as.length; i++) {
 759                 boolean m = (as[i] < bs[i]);
 760                 r |= m; // accumulate so JIT can't eliminate the computation
 761             }
 762         }
 763 
 764         bh.consume(r);
 765     }
 766 
 767     @Benchmark
 768     public void greaterThan(Blackhole bh) {
 769         short[] as = fa.apply(size);
 770         short[] bs = fb.apply(size);
 771 
 772         boolean r = false;
 773         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 774             r = false;
 775             for (int i = 0; i < as.length; i++) {
 776                 boolean m = (as[i] > bs[i]);
 777                 r |= m; // accumulate so JIT can't eliminate the computation
 778             }
 779         }
 780 
 781         bh.consume(r);
 782     }
 783 
 784     @Benchmark
 785     public void equal(Blackhole bh) {
 786         short[] as = fa.apply(size);
 787         short[] bs = fb.apply(size);
 788 
 789         boolean r = false;
 790         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 791             r = false;
 792             for (int i = 0; i < as.length; i++) {
 793                 boolean m = (as[i] == bs[i]);
 794                 r |= m; // accumulate so JIT can't eliminate the computation
 795             }
 796         }
 797 
 798         bh.consume(r);
 799     }
 800 
 801     @Benchmark
 802     public void notEqual(Blackhole bh) {
 803         short[] as = fa.apply(size);
 804         short[] bs = fb.apply(size);
 805 
 806         boolean r = false;
 807         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 808             r = false;
 809             for (int i = 0; i < as.length; i++) {
 810                 boolean m = (as[i] != bs[i]);
 811                 r |= m; // accumulate so JIT can't eliminate the computation
 812             }
 813         }
 814 
 815         bh.consume(r);
 816     }
 817 
 818     @Benchmark
 819     public void lessThanEq(Blackhole bh) {
 820         short[] as = fa.apply(size);
 821         short[] bs = fb.apply(size);
 822 
 823         boolean r = false;
 824         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 825             r = false;
 826             for (int i = 0; i < as.length; i++) {
 827                 boolean m = (as[i] <= bs[i]);
 828                 r |= m; // accumulate so JIT can't eliminate the computation
 829             }
 830         }
 831 
 832         bh.consume(r);
 833     }
 834 
 835     @Benchmark
 836     public void greaterThanEq(Blackhole bh) {
 837         short[] as = fa.apply(size);
 838         short[] bs = fb.apply(size);
 839 
 840         boolean r = false;
 841         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 842             r = false;
 843             for (int i = 0; i < as.length; i++) {
 844                 boolean m = (as[i] >= bs[i]);
 845                 r |= m; // accumulate so JIT can't eliminate the computation
 846             }
 847         }
 848 
 849         bh.consume(r);
 850     }
 851 
 852     @Benchmark
 853     public void blend(Blackhole bh) {
 854         short[] as = fa.apply(size);
 855         short[] bs = fb.apply(size);
 856         short[] rs = fr.apply(size);
 857         boolean[] ms = fm.apply(size);
 858 
 859         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 860             for (int i = 0; i < as.length; i++) {
 861                 short a = as[i];
 862                 short b = bs[i];
 863                 boolean m = ms[i % ms.length];
 864                 rs[i] = (m ? b : a);
 865             }
 866         }
 867 
 868         bh.consume(rs);
 869     }
 870     void rearrangeShared(int window, Blackhole bh) {
 871         short[] as = fa.apply(size);
 872         int[] order = fs.apply(size);
 873         short[] rs = fr.apply(size);
 874 
 875         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 876             for (int i = 0; i < as.length; i += window) {
 877                 for (int j = 0; j < window; j++) {
 878                     short a = as[i+j];
 879                     int pos = order[j];
 880                     rs[i + pos] = a;
 881                 }
 882             }
 883         }
 884 
 885         bh.consume(rs);
 886     }
 887 
 888     @Benchmark
 889     public void rearrange064(Blackhole bh) {
 890         int window = 64 / Short.SIZE;
 891         rearrangeShared(window, bh);
 892     }
 893 
 894     @Benchmark
 895     public void rearrange128(Blackhole bh) {
 896         int window = 128 / Short.SIZE;
 897         rearrangeShared(window, bh);
 898     }
 899 
 900     @Benchmark
 901     public void rearrange256(Blackhole bh) {
 902         int window = 256 / Short.SIZE;
 903         rearrangeShared(window, bh);
 904     }
 905 
 906     @Benchmark
 907     public void rearrange512(Blackhole bh) {
 908         int window = 512 / Short.SIZE;
 909         rearrangeShared(window, bh);
 910     }
 911 
 912 
 913 
 914 
 915 
 916 
 917 
 918 
 919 
 920 
 921 
 922 
 923 
 924 
 925 
 926 
 927 
 928 
 929 
 930 
 931 
 932     @Benchmark
 933     public void neg(Blackhole bh) {
 934         short[] as = fa.apply(size);
 935         short[] rs = fr.apply(size);
 936 
 937         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 938             for (int i = 0; i < as.length; i++) {
 939                 short a = as[i];
 940                 rs[i] = (short)(-((short)a));
 941             }
 942         }
 943 
 944         bh.consume(rs);
 945     }
 946 
 947     @Benchmark
 948     public void negMasked(Blackhole bh) {
 949         short[] as = fa.apply(size);
 950         short[] rs = fr.apply(size);
 951         boolean[] ms = fm.apply(size);
 952 
 953         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 954             for (int i = 0; i < as.length; i++) {
 955                 short a = as[i];
 956                 boolean m = ms[i % ms.length];
 957                 rs[i] = (m ? (short)(-((short)a)) : a);
 958             }
 959         }
 960 
 961         bh.consume(rs);
 962     }
 963 
 964     @Benchmark
 965     public void abs(Blackhole bh) {
 966         short[] as = fa.apply(size);
 967         short[] rs = fr.apply(size);
 968 
 969         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 970             for (int i = 0; i < as.length; i++) {
 971                 short a = as[i];
 972                 rs[i] = (short)(Math.abs((short)a));
 973             }
 974         }
 975 
 976         bh.consume(rs);
 977     }
 978 
 979     @Benchmark
 980     public void absMasked(Blackhole bh) {
 981         short[] as = fa.apply(size);
 982         short[] rs = fr.apply(size);
 983         boolean[] ms = fm.apply(size);
 984 
 985         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 986             for (int i = 0; i < as.length; i++) {
 987                 short a = as[i];
 988                 boolean m = ms[i % ms.length];
 989                 rs[i] = (m ? (short)(Math.abs((short)a)) : a);
 990             }
 991         }
 992 
 993         bh.consume(rs);
 994     }
 995 
 996 
 997     @Benchmark
 998     public void not(Blackhole bh) {
 999         short[] as = fa.apply(size);
1000         short[] rs = fr.apply(size);
1001 
1002         for (int ic = 0; ic < INVOC_COUNT; ic++) {
1003             for (int i = 0; i < as.length; i++) {
1004                 short a = as[i];
1005                 rs[i] = (short)(~((short)a));
1006             }
1007         }
1008 
1009         bh.consume(rs);
1010     }
1011 
1012 
1013 
1014     @Benchmark
1015     public void notMasked(Blackhole bh) {
1016         short[] as = fa.apply(size);
1017         short[] rs = fr.apply(size);
1018         boolean[] ms = fm.apply(size);
1019 
1020         for (int ic = 0; ic < INVOC_COUNT; ic++) {
1021             for (int i = 0; i < as.length; i++) {
1022                 short a = as[i];
1023                 boolean m = ms[i % ms.length];
1024                 rs[i] = (m ? (short)(~((short)a)) : a);
1025             }
1026         }
1027 
1028         bh.consume(rs);
1029     }
1030 
1031 
1032 
1033 
1034 
1035 }
1036