< prev index next >

test/jdk/jdk/incubator/vector/benchmark/src/main/java/benchmark/jdk/incubator/vector/IntScalar.java

Print this page
rev 55606 : 8221812: Fine-tune jmh test for vector api
Summary: To compare performance of vector api and auto vectorization, vector
api and scalar test cases are updated to keep aligned.
Reviewed-by: duke
   1 /*
   2  * Copyright (c) 2018, 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 
  31 @BenchmarkMode(Mode.Throughput)
  32 @OutputTimeUnit(TimeUnit.MILLISECONDS)
  33 @State(Scope.Benchmark)
  34 @Warmup(iterations = 3, time = 1)
  35 @Measurement(iterations = 5, time = 1)
  36 @Fork(value = 1, jvmArgsPrepend = {"--add-modules=jdk.incubator.vector"})
  37 public class IntScalar extends AbstractVectorBenchmark {


  38     @Param("1024")
  39     int size;
  40 
  41     int[] fill(IntFunction<Integer> f) {
  42         int[] array = new int[size];
  43         for (int i = 0; i < array.length; i++) {
  44             array[i] = f.apply(i);
  45         }
  46         return array;
  47     }
  48 
  49     int[] as, bs, cs, rs;
  50     boolean[] ms, rms;
  51     int[] ss;
  52 
  53     @Setup
  54     public void init() {
  55         as = fill(i -> (int)(2*i));
  56         bs = fill(i -> (int)(i+1));
  57         cs = fill(i -> (int)(i+5));
  58         rs = fill(i -> (int)0);
  59         ms = fillMask(size, i -> (i % 2) == 0);
  60         rms = fillMask(size, i -> false);
  61 
  62         ss = fillInt(size, i -> RANDOM.nextInt(Math.max(i,1)));
  63     }
  64 
  65     final IntFunction<int[]> fa = vl -> as;
  66     final IntFunction<int[]> fb = vl -> bs;
  67     final IntFunction<int[]> fc = vl -> cs;
  68     final IntFunction<int[]> fr = vl -> rs;
  69     final IntFunction<boolean[]> fm = vl -> ms;
  70     final IntFunction<boolean[]> fmr = vl -> rms;
  71     final IntFunction<int[]> fs = vl -> ss;
  72 
  73 
  74     @Benchmark
  75     public Object add() {
  76         int[] as = fa.apply(size);
  77         int[] bs = fb.apply(size);
  78         int[] rs = fr.apply(size);
  79 

  80         for (int i = 0; i < as.length; i++) {
  81             int a = as[i];
  82             int b = bs[i];
  83             rs[i] = (int)(a + b);
  84         }

  85 
  86         return rs;
  87     }
  88 
  89     @Benchmark
  90     public Object addMasked() {
  91         int[] as = fa.apply(size);
  92         int[] bs = fb.apply(size);
  93         int[] rs = fr.apply(size);
  94         boolean[] ms = fm.apply(size);
  95 

  96         for (int i = 0; i < as.length; i++) {
  97             int a = as[i];
  98             int b = bs[i];
  99             if (ms[i % ms.length]) {
 100                 rs[i] = (int)(a + b);
 101             } else {
 102                 rs[i] = a;
 103             }
 104         }
 105         return rs;

 106     }
 107 
 108     @Benchmark
 109     public Object sub() {
 110         int[] as = fa.apply(size);
 111         int[] bs = fb.apply(size);
 112         int[] rs = fr.apply(size);
 113 

 114         for (int i = 0; i < as.length; i++) {
 115             int a = as[i];
 116             int b = bs[i];
 117             rs[i] = (int)(a - b);
 118         }

 119 
 120         return rs;
 121     }
 122 
 123     @Benchmark
 124     public Object subMasked() {
 125         int[] as = fa.apply(size);
 126         int[] bs = fb.apply(size);
 127         int[] rs = fr.apply(size);
 128         boolean[] ms = fm.apply(size);
 129 

 130         for (int i = 0; i < as.length; i++) {
 131             int a = as[i];
 132             int b = bs[i];
 133             if (ms[i % ms.length]) {
 134                 rs[i] = (int)(a - b);
 135             } else {
 136                 rs[i] = a;
 137             }
 138         }
 139         return rs;

 140     }
 141 
 142 
 143 
 144     @Benchmark
 145     public Object mul() {
 146         int[] as = fa.apply(size);
 147         int[] bs = fb.apply(size);
 148         int[] rs = fr.apply(size);
 149 

 150         for (int i = 0; i < as.length; i++) {
 151             int a = as[i];
 152             int b = bs[i];
 153             rs[i] = (int)(a * b);
 154         }

 155 
 156         return rs;
 157     }
 158 
 159     @Benchmark
 160     public Object mulMasked() {
 161         int[] as = fa.apply(size);
 162         int[] bs = fb.apply(size);
 163         int[] rs = fr.apply(size);
 164         boolean[] ms = fm.apply(size);
 165 

 166         for (int i = 0; i < as.length; i++) {
 167             int a = as[i];
 168             int b = bs[i];
 169             if (ms[i % ms.length]) {
 170                 rs[i] = (int)(a * b);
 171             } else {
 172                 rs[i] = a;
 173             }
 174         }
 175         return rs;

 176     }
 177 
 178 
 179     @Benchmark
 180     public Object and() {
 181         int[] as = fa.apply(size);
 182         int[] bs = fb.apply(size);
 183         int[] rs = fr.apply(size);
 184 

 185         for (int i = 0; i < as.length; i++) {
 186             int a = as[i];
 187             int b = bs[i];
 188             rs[i] = (int)(a & b);
 189         }

 190 
 191         return rs;
 192     }
 193 
 194 
 195 
 196     @Benchmark
 197     public Object andMasked() {
 198         int[] as = fa.apply(size);
 199         int[] bs = fb.apply(size);
 200         int[] rs = fr.apply(size);
 201         boolean[] ms = fm.apply(size);
 202 

 203         for (int i = 0; i < as.length; i++) {
 204             int a = as[i];
 205             int b = bs[i];
 206             if (ms[i % ms.length]) {
 207                 rs[i] = (int)(a & b);
 208             } else {
 209                 rs[i] = a;
 210             }
 211         }
 212         return rs;

 213     }
 214 
 215 
 216 
 217     @Benchmark
 218     public Object or() {
 219         int[] as = fa.apply(size);
 220         int[] bs = fb.apply(size);
 221         int[] rs = fr.apply(size);
 222 

 223         for (int i = 0; i < as.length; i++) {
 224             int a = as[i];
 225             int b = bs[i];
 226             rs[i] = (int)(a | b);
 227         }

 228 
 229         return rs;
 230     }
 231 
 232 
 233 
 234     @Benchmark
 235     public Object orMasked() {
 236         int[] as = fa.apply(size);
 237         int[] bs = fb.apply(size);
 238         int[] rs = fr.apply(size);
 239         boolean[] ms = fm.apply(size);
 240 

 241         for (int i = 0; i < as.length; i++) {
 242             int a = as[i];
 243             int b = bs[i];
 244             if (ms[i % ms.length]) {
 245                 rs[i] = (int)(a | b);
 246             } else {
 247                 rs[i] = a;
 248             }
 249         }
 250         return rs;

 251     }
 252 
 253 
 254 
 255     @Benchmark
 256     public Object xor() {
 257         int[] as = fa.apply(size);
 258         int[] bs = fb.apply(size);
 259         int[] rs = fr.apply(size);
 260 

 261         for (int i = 0; i < as.length; i++) {
 262             int a = as[i];
 263             int b = bs[i];
 264             rs[i] = (int)(a ^ b);
 265         }

 266 
 267         return rs;
 268     }
 269 
 270 
 271 
 272     @Benchmark
 273     public Object xorMasked() {
 274         int[] as = fa.apply(size);
 275         int[] bs = fb.apply(size);
 276         int[] rs = fr.apply(size);
 277         boolean[] ms = fm.apply(size);
 278 

 279         for (int i = 0; i < as.length; i++) {
 280             int a = as[i];
 281             int b = bs[i];
 282             if (ms[i % ms.length]) {
 283                 rs[i] = (int)(a ^ b);
 284             } else {
 285                 rs[i] = a;
 286             }
 287         }
 288         return rs;

 289     }
 290 
 291 
 292 
 293     @Benchmark
 294     public Object shiftR() {
 295         int[] as = fa.apply(size);
 296         int[] bs = fb.apply(size);
 297         int[] rs = fr.apply(size);
 298 

 299         for (int i = 0; i < as.length; i++) {
 300             int a = as[i];
 301             int b = bs[i];
 302             rs[i] = (int)((a >>> b));
 303         }

 304 
 305         return rs;
 306     }
 307 
 308 
 309 
 310     @Benchmark
 311     public Object shiftRMasked() {
 312         int[] as = fa.apply(size);
 313         int[] bs = fb.apply(size);
 314         int[] rs = fr.apply(size);
 315         boolean[] ms = fm.apply(size);
 316 

 317         for (int i = 0; i < as.length; i++) {
 318             int a = as[i];
 319             int b = bs[i];
 320             if (ms[i % ms.length]) {
 321                 rs[i] = (int)((a >>> b));
 322             } else {
 323                 rs[i] = a;
 324             }
 325         }
 326         return rs;

 327     }
 328 
 329 
 330 
 331     @Benchmark
 332     public Object shiftL() {
 333         int[] as = fa.apply(size);
 334         int[] bs = fb.apply(size);
 335         int[] rs = fr.apply(size);
 336 

 337         for (int i = 0; i < as.length; i++) {
 338             int a = as[i];
 339             int b = bs[i];
 340             rs[i] = (int)((a << b));
 341         }

 342 
 343         return rs;
 344     }
 345 
 346 
 347 
 348     @Benchmark
 349     public Object shiftLMasked() {
 350         int[] as = fa.apply(size);
 351         int[] bs = fb.apply(size);
 352         int[] rs = fr.apply(size);
 353         boolean[] ms = fm.apply(size);
 354 

 355         for (int i = 0; i < as.length; i++) {
 356             int a = as[i];
 357             int b = bs[i];
 358             if (ms[i % ms.length]) {
 359                 rs[i] = (int)((a << b));
 360             } else {
 361                 rs[i] = a;
 362             }
 363         }
 364         return rs;

 365     }
 366 
 367 
 368 
 369     @Benchmark
 370     public Object aShiftR() {
 371         int[] as = fa.apply(size);
 372         int[] bs = fb.apply(size);
 373         int[] rs = fr.apply(size);
 374 

 375         for (int i = 0; i < as.length; i++) {
 376             int a = as[i];
 377             int b = bs[i];
 378             rs[i] = (int)((a >> b));
 379         }

 380 
 381         return rs;
 382     }
 383 
 384 
 385 
 386     @Benchmark
 387     public Object aShiftRMasked() {
 388         int[] as = fa.apply(size);
 389         int[] bs = fb.apply(size);
 390         int[] rs = fr.apply(size);
 391         boolean[] ms = fm.apply(size);
 392 

 393         for (int i = 0; i < as.length; i++) {
 394             int a = as[i];
 395             int b = bs[i];
 396             if (ms[i % ms.length]) {
 397                 rs[i] = (int)((a >> b));
 398             } else {
 399                 rs[i] = a;
 400             }
 401         }
 402         return rs;

 403     }
 404 
 405 
 406 
 407     @Benchmark
 408     public Object aShiftRShift() {
 409         int[] as = fa.apply(size);
 410         int[] bs = fb.apply(size);
 411         int[] rs = fr.apply(size);
 412 

 413         for (int i = 0; i < as.length; i++) {
 414             int a = as[i];
 415             int b = bs[i];
 416             rs[i] = (int)((a >> b));
 417         }

 418 
 419         return rs;
 420     }
 421 
 422 
 423 
 424     @Benchmark
 425     public Object aShiftRMaskedShift() {
 426         int[] as = fa.apply(size);
 427         int[] bs = fb.apply(size);
 428         int[] rs = fr.apply(size);
 429         boolean[] ms = fm.apply(size);
 430 

 431         for (int i = 0; i < as.length; i++) {
 432             int a = as[i];
 433             int b = bs[i];
 434             boolean m = ms[i % ms.length];
 435             rs[i] = (m ? (int)((a >> b)) : a);
 436         }

 437 
 438         return rs;
 439     }
 440 
 441 
 442 
 443     @Benchmark
 444     public Object shiftRShift() {
 445         int[] as = fa.apply(size);
 446         int[] bs = fb.apply(size);
 447         int[] rs = fr.apply(size);
 448 

 449         for (int i = 0; i < as.length; i++) {
 450             int a = as[i];
 451             int b = bs[i];
 452             rs[i] = (int)((a >>> b));
 453         }

 454 
 455         return rs;
 456     }
 457 
 458 
 459 
 460     @Benchmark
 461     public Object shiftRMaskedShift() {
 462         int[] as = fa.apply(size);
 463         int[] bs = fb.apply(size);
 464         int[] rs = fr.apply(size);
 465         boolean[] ms = fm.apply(size);
 466 

 467         for (int i = 0; i < as.length; i++) {
 468             int a = as[i];
 469             int b = bs[i];
 470             boolean m = ms[i % ms.length];
 471             rs[i] = (m ? (int)((a >>> b)) : a);
 472         }

 473 
 474         return rs;
 475     }
 476 
 477 
 478 
 479     @Benchmark
 480     public Object shiftLShift() {
 481         int[] as = fa.apply(size);
 482         int[] bs = fb.apply(size);
 483         int[] rs = fr.apply(size);
 484 

 485         for (int i = 0; i < as.length; i++) {
 486             int a = as[i];
 487             int b = bs[i];
 488             rs[i] = (int)((a << b));
 489         }

 490 
 491         return rs;
 492     }
 493 
 494 
 495 
 496     @Benchmark
 497     public Object shiftLMaskedShift() {
 498         int[] as = fa.apply(size);
 499         int[] bs = fb.apply(size);
 500         int[] rs = fr.apply(size);
 501         boolean[] ms = fm.apply(size);
 502 

 503         for (int i = 0; i < as.length; i++) {
 504             int a = as[i];
 505             int b = bs[i];
 506             boolean m = ms[i % ms.length];
 507             rs[i] = (m ? (int)((a << b)) : a);
 508         }

 509 
 510         return rs;
 511     }
 512 
 513 
 514 
 515 
 516 
 517 
 518 
 519 
 520 
 521 
 522 
 523 
 524 
 525 
 526     @Benchmark
 527     public Object max() {
 528         int[] as = fa.apply(size);
 529         int[] bs = fb.apply(size);
 530         int[] rs = fr.apply(size);
 531 

 532         for (int i = 0; i < as.length; i++) {
 533             int a = as[i];
 534             int b = bs[i];
 535             rs[i] = (int)(Math.max(a, b));
 536         }

 537 
 538         return rs;
 539     }
 540 
 541     @Benchmark
 542     public Object min() {
 543         int[] as = fa.apply(size);
 544         int[] bs = fb.apply(size);
 545         int[] rs = fr.apply(size);
 546 

 547         for (int i = 0; i < as.length; i++) {
 548             int a = as[i];
 549             int b = bs[i];
 550             rs[i] = (int)(Math.min(a, b));
 551         }

 552 
 553         return rs;
 554     }
 555 
 556 
 557     @Benchmark
 558     public int andAll() {
 559         int[] as = fa.apply(size);
 560         int r = -1;


 561         for (int i = 0; i < as.length; i++) {
 562             r &= as[i];
 563         }
 564         return r;

 565     }
 566 
 567 
 568 
 569     @Benchmark
 570     public int orAll() {
 571         int[] as = fa.apply(size);
 572         int r = 0;


 573         for (int i = 0; i < as.length; i++) {
 574             r |= as[i];
 575         }
 576         return r;

 577     }
 578 
 579 
 580 
 581     @Benchmark
 582     public int xorAll() {
 583         int[] as = fa.apply(size);
 584         int r = 0;


 585         for (int i = 0; i < as.length; i++) {
 586             r ^= as[i];
 587         }
 588         return r;

 589     }
 590 
 591 
 592     @Benchmark
 593     public int addAll() {
 594         int[] as = fa.apply(size);
 595         int r = 0;


 596         for (int i = 0; i < as.length; i++) {
 597             r += as[i];
 598         }
 599         return r;

 600     }
 601 
 602     @Benchmark
 603     public int mulAll() {
 604         int[] as = fa.apply(size);
 605         int r = 1;


 606         for (int i = 0; i < as.length; i++) {
 607             r *= as[i];
 608         }
 609         return r;

 610     }
 611 
 612     @Benchmark
 613     public int minAll() {
 614         int[] as = fa.apply(size);
 615         int r = Integer.MAX_VALUE;


 616         for (int i = 0; i < as.length; i++) {
 617             r = (int)Math.min(r, as[i]);
 618         }
 619         return r;

 620     }
 621 
 622     @Benchmark
 623     public int maxAll() {
 624         int[] as = fa.apply(size);
 625         int r = Integer.MIN_VALUE;


 626         for (int i = 0; i < as.length; i++) {
 627             r = (int)Math.max(r, as[i]);
 628         }
 629         return r;

 630     }
 631 
 632 
 633     @Benchmark
 634     public boolean anyTrue() {
 635         boolean[] ms = fm.apply(size);
 636         boolean r = false;


 637         for (int i = 0; i < ms.length; i++) {
 638             r |= ms[i];
 639         }
 640         return r;

 641     }
 642 
 643 
 644 
 645     @Benchmark
 646     public boolean allTrue() {
 647         boolean[] ms = fm.apply(size);
 648         boolean r = true;


 649         for (int i = 0; i < ms.length; i++) {
 650             r &= ms[i];
 651         }
 652         return r;

 653     }
 654 
 655 
 656     @Benchmark
 657     public boolean lessThan() {
 658         int[] as = fa.apply(size);
 659         int[] bs = fb.apply(size);
 660 
 661         boolean r = false;


 662         for (int i = 0; i < as.length; i++) {
 663             boolean m = (as[i] < bs[i]);
 664             r |= m; // accumulate so JIT can't eliminate the computation
 665         }

 666 
 667         return r;
 668     }
 669 
 670     @Benchmark
 671     public boolean greaterThan() {
 672         int[] as = fa.apply(size);
 673         int[] bs = fb.apply(size);
 674 
 675         boolean r = false;


 676         for (int i = 0; i < as.length; i++) {
 677             boolean m = (as[i] > bs[i]);
 678             r |= m; // accumulate so JIT can't eliminate the computation
 679         }

 680 
 681         return r;
 682     }
 683 
 684     @Benchmark
 685     public boolean equal() {
 686         int[] as = fa.apply(size);
 687         int[] bs = fb.apply(size);
 688 
 689         boolean r = false;


 690         for (int i = 0; i < as.length; i++) {
 691             boolean m = (as[i] == bs[i]);
 692             r |= m; // accumulate so JIT can't eliminate the computation
 693         }

 694 
 695         return r;
 696     }
 697 
 698     @Benchmark
 699     public boolean notEqual() {
 700         int[] as = fa.apply(size);
 701         int[] bs = fb.apply(size);
 702 
 703         boolean r = false;


 704         for (int i = 0; i < as.length; i++) {
 705             boolean m = (as[i] != bs[i]);
 706             r |= m; // accumulate so JIT can't eliminate the computation
 707         }

 708 
 709         return r;
 710     }
 711 
 712     @Benchmark
 713     public boolean lessThanEq() {
 714         int[] as = fa.apply(size);
 715         int[] bs = fb.apply(size);
 716 
 717         boolean r = false;


 718         for (int i = 0; i < as.length; i++) {
 719             boolean m = (as[i] <= bs[i]);
 720             r |= m; // accumulate so JIT can't eliminate the computation
 721         }

 722 
 723         return r;
 724     }
 725 
 726     @Benchmark
 727     public boolean greaterThanEq() {
 728         int[] as = fa.apply(size);
 729         int[] bs = fb.apply(size);
 730 
 731         boolean r = false;


 732         for (int i = 0; i < as.length; i++) {
 733             boolean m = (as[i] >= bs[i]);
 734             r |= m; // accumulate so JIT can't eliminate the computation
 735         }

 736 
 737         return r;
 738     }
 739 
 740     @Benchmark
 741     public Object blend() {
 742         int[] as = fa.apply(size);
 743         int[] bs = fb.apply(size);
 744         int[] rs = fr.apply(size);
 745         boolean[] ms = fm.apply(size);
 746 

 747         for (int i = 0; i < as.length; i++) {
 748             int a = as[i];
 749             int b = bs[i];
 750             boolean m = ms[i % ms.length];
 751             rs[i] = (m ? b : a);
 752         }

 753 
 754         return rs;
 755     }
 756     Object rearrangeShared(int window) {
 757         int[] as = fa.apply(size);
 758         int[] order = fs.apply(size);
 759         int[] rs = fr.apply(size);
 760 

 761         for (int i = 0; i < as.length; i += window) {
 762             for (int j = 0; j < window; j++) {
 763                 int a = as[i+j];
 764                 int pos = order[j];
 765                 rs[i + pos] = a;
 766             }
 767         }

 768 
 769         return rs;
 770     }
 771 
 772     @Benchmark
 773     public Object rearrange064() {
 774         int window = 64 / Integer.SIZE;
 775         return rearrangeShared(window);
 776     }
 777 
 778     @Benchmark
 779     public Object rearrange128() {
 780         int window = 128 / Integer.SIZE;
 781         return rearrangeShared(window);
 782     }
 783 
 784     @Benchmark
 785     public Object rearrange256() {
 786         int window = 256 / Integer.SIZE;
 787         return rearrangeShared(window);
 788     }
 789 
 790     @Benchmark
 791     public Object rearrange512() {
 792         int window = 512 / Integer.SIZE;
 793         return rearrangeShared(window);
 794     }
 795 
 796 
 797 
 798 
 799 
 800 
 801 
 802 
 803 
 804 
 805 
 806 
 807 
 808 
 809 
 810 
 811 
 812 
 813 
 814 
 815 
 816     @Benchmark
 817     public Object neg() {
 818         int[] as = fa.apply(size);
 819         int[] rs = fr.apply(size);
 820 

 821         for (int i = 0; i < as.length; i++) {
 822             int a = as[i];
 823             rs[i] = (int)(-((int)a));
 824         }

 825 
 826         return rs;
 827     }
 828 
 829     @Benchmark
 830     public Object negMasked() {
 831         int[] as = fa.apply(size);
 832         int[] rs = fr.apply(size);
 833         boolean[] ms = fm.apply(size);
 834 

 835         for (int i = 0; i < as.length; i++) {
 836             int a = as[i];
 837             boolean m = ms[i % ms.length];
 838             rs[i] = (m ? (int)(-((int)a)) : a);
 839         }

 840 
 841         return rs;
 842     }
 843 
 844     @Benchmark
 845     public Object abs() {
 846         int[] as = fa.apply(size);
 847         int[] rs = fr.apply(size);
 848 

 849         for (int i = 0; i < as.length; i++) {
 850             int a = as[i];
 851             rs[i] = (int)(Math.abs((int)a));
 852         }

 853 
 854         return rs;
 855     }
 856 
 857     @Benchmark
 858     public Object absMasked() {
 859         int[] as = fa.apply(size);
 860         int[] rs = fr.apply(size);
 861         boolean[] ms = fm.apply(size);
 862 

 863         for (int i = 0; i < as.length; i++) {
 864             int a = as[i];
 865             boolean m = ms[i % ms.length];
 866             rs[i] = (m ? (int)(Math.abs((int)a)) : a);
 867         }

 868 
 869         return rs;
 870     }
 871 
 872 
 873     @Benchmark
 874     public Object not() {
 875         int[] as = fa.apply(size);
 876         int[] rs = fr.apply(size);
 877 

 878         for (int i = 0; i < as.length; i++) {
 879             int a = as[i];
 880             rs[i] = (int)(~((int)a));
 881         }

 882 
 883         return rs;
 884     }
 885 
 886 
 887 
 888     @Benchmark
 889     public Object notMasked() {
 890         int[] as = fa.apply(size);
 891         int[] rs = fr.apply(size);
 892         boolean[] ms = fm.apply(size);
 893 

 894         for (int i = 0; i < as.length; i++) {
 895             int a = as[i];
 896             boolean m = ms[i % ms.length];
 897             rs[i] = (m ? (int)(~((int)a)) : a);
 898         }

 899 
 900         return rs;
 901     }
 902 
 903 
 904 
 905 
 906     @Benchmark
 907     public Object gatherBase0() {
 908         int[] as = fa.apply(size);
 909         int[] is    = fs.apply(size);
 910         int[] rs = fr.apply(size);
 911 

 912         for (int i = 0; i < as.length; i++) {
 913             int ix = 0 + is[i];
 914             rs[i] = as[ix];
 915         }

 916 
 917         return rs;
 918     }
 919 
 920 
 921     Object gather(int window) {
 922         int[] as = fa.apply(size);
 923         int[] is    = fs.apply(size);
 924         int[] rs = fr.apply(size);
 925 

 926         for (int i = 0; i < as.length; i += window) {
 927             for (int j = 0; j < window; j++) {
 928                 int ix = i + is[i + j];
 929                 rs[i + j] = as[ix];
 930             }
 931         }

 932 
 933         return rs;
 934     }
 935 
 936     @Benchmark
 937     public Object gather064() {
 938         int window = 64 / Integer.SIZE;
 939         return gather(window);
 940     }
 941 
 942     @Benchmark
 943     public Object gather128() {
 944         int window = 128 / Integer.SIZE;
 945         return gather(window);
 946     }
 947 
 948     @Benchmark
 949     public Object gather256() {
 950         int window = 256 / Integer.SIZE;
 951         return gather(window);
 952     }
 953 
 954     @Benchmark
 955     public Object gather512() {
 956         int window = 512 / Integer.SIZE;
 957         return gather(window);
 958     }
 959 
 960 
 961 
 962     @Benchmark
 963     public Object scatterBase0() {
 964         int[] as = fa.apply(size);
 965         int[] is    = fs.apply(size);
 966         int[] rs = fr.apply(size);
 967 

 968         for (int i = 0; i < as.length; i++) {
 969             int ix = 0 + is[i];
 970             rs[ix] = as[i];
 971         }

 972 
 973         return rs;
 974     }
 975 
 976     Object scatter(int window) {
 977         int[] as = fa.apply(size);
 978         int[] is    = fs.apply(size);
 979         int[] rs = fr.apply(size);
 980 

 981         for (int i = 0; i < as.length; i += window) {
 982             for (int j = 0; j < window; j++) {
 983                 int ix = i + is[i + j];
 984                 rs[ix] = as[i + j];
 985             }
 986         }

 987 
 988         return rs;
 989     }
 990 
 991     @Benchmark
 992     public Object scatter064() {
 993         int window = 64 / Integer.SIZE;
 994         return scatter(window);
 995     }
 996 
 997     @Benchmark
 998     public Object scatter128() {
 999         int window = 128 / Integer.SIZE;
1000         return scatter(window);
1001     }
1002 
1003     @Benchmark
1004     public Object scatter256() {
1005         int window = 256 / Integer.SIZE;
1006         return scatter(window);
1007     }
1008 
1009     @Benchmark
1010     public Object scatter512() {
1011         int window = 512 / Integer.SIZE;
1012         return scatter(window);
1013     }
1014 
1015 }
1016 
   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 IntScalar extends AbstractVectorBenchmark {
  39     static final int INVOC_COUNT = 1; // To align with vector benchmarks.
  40 
  41     @Param("1024")
  42     int size;
  43 
  44     int[] fill(IntFunction<Integer> f) {
  45         int[] array = new int[size];
  46         for (int i = 0; i < array.length; i++) {
  47             array[i] = f.apply(i);
  48         }
  49         return array;
  50     }
  51 
  52     int[] as, bs, cs, rs;
  53     boolean[] ms, rms;
  54     int[] ss;
  55 
  56     @Setup
  57     public void init() {
  58         as = fill(i -> (int)(2*i));
  59         bs = fill(i -> (int)(i+1));
  60         cs = fill(i -> (int)(i+5));
  61         rs = fill(i -> (int)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<int[]> fa = vl -> as;
  69     final IntFunction<int[]> fb = vl -> bs;
  70     final IntFunction<int[]> fc = vl -> cs;
  71     final IntFunction<int[]> 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         int[] as = fa.apply(size);
  80         int[] bs = fb.apply(size);
  81         int[] rs = fr.apply(size);
  82 
  83         for (int ic = 0; ic < INVOC_COUNT; ic++) {
  84             for (int i = 0; i < as.length; i++) {
  85                 int a = as[i];
  86                 int b = bs[i];
  87                 rs[i] = (int)(a + b);
  88             }
  89         }
  90 
  91         bh.consume(rs);
  92     }
  93 
  94     @Benchmark
  95     public void addMasked(Blackhole bh) {
  96         int[] as = fa.apply(size);
  97         int[] bs = fb.apply(size);
  98         int[] 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                 int a = as[i];
 104                 int b = bs[i];
 105                 if (ms[i % ms.length]) {
 106                     rs[i] = (int)(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         int[] as = fa.apply(size);
 118         int[] bs = fb.apply(size);
 119         int[] rs = fr.apply(size);
 120 
 121         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 122             for (int i = 0; i < as.length; i++) {
 123                 int a = as[i];
 124                 int b = bs[i];
 125                 rs[i] = (int)(a - b);
 126             }
 127         }
 128 
 129         bh.consume(rs);
 130     }
 131 
 132     @Benchmark
 133     public void subMasked(Blackhole bh) {
 134         int[] as = fa.apply(size);
 135         int[] bs = fb.apply(size);
 136         int[] 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                 int a = as[i];
 142                 int b = bs[i];
 143                 if (ms[i % ms.length]) {
 144                     rs[i] = (int)(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         int[] as = fa.apply(size);
 158         int[] bs = fb.apply(size);
 159         int[] rs = fr.apply(size);
 160 
 161         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 162             for (int i = 0; i < as.length; i++) {
 163                 int a = as[i];
 164                 int b = bs[i];
 165                 rs[i] = (int)(a * b);
 166             }
 167         }
 168 
 169         bh.consume(rs);
 170     }
 171 
 172     @Benchmark
 173     public void mulMasked(Blackhole bh) {
 174         int[] as = fa.apply(size);
 175         int[] bs = fb.apply(size);
 176         int[] 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                 int a = as[i];
 182                 int b = bs[i];
 183                 if (ms[i % ms.length]) {
 184                     rs[i] = (int)(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         int[] as = fa.apply(size);
 197         int[] bs = fb.apply(size);
 198         int[] rs = fr.apply(size);
 199 
 200         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 201             for (int i = 0; i < as.length; i++) {
 202                 int a = as[i];
 203                 int b = bs[i];
 204                 rs[i] = (int)(a & b);
 205             }
 206         }
 207 
 208         bh.consume(rs);
 209     }
 210 
 211 
 212 
 213     @Benchmark
 214     public void andMasked(Blackhole bh) {
 215         int[] as = fa.apply(size);
 216         int[] bs = fb.apply(size);
 217         int[] 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                 int a = as[i];
 223                 int b = bs[i];
 224                 if (ms[i % ms.length]) {
 225                     rs[i] = (int)(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         int[] as = fa.apply(size);
 239         int[] bs = fb.apply(size);
 240         int[] rs = fr.apply(size);
 241 
 242         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 243             for (int i = 0; i < as.length; i++) {
 244                 int a = as[i];
 245                 int b = bs[i];
 246                 rs[i] = (int)(a | b);
 247             }
 248         }
 249 
 250         bh.consume(rs);
 251     }
 252 
 253 
 254 
 255     @Benchmark
 256     public void orMasked(Blackhole bh) {
 257         int[] as = fa.apply(size);
 258         int[] bs = fb.apply(size);
 259         int[] 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                 int a = as[i];
 265                 int b = bs[i];
 266                 if (ms[i % ms.length]) {
 267                     rs[i] = (int)(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         int[] as = fa.apply(size);
 281         int[] bs = fb.apply(size);
 282         int[] rs = fr.apply(size);
 283 
 284         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 285             for (int i = 0; i < as.length; i++) {
 286                 int a = as[i];
 287                 int b = bs[i];
 288                 rs[i] = (int)(a ^ b);
 289             }
 290         }
 291 
 292         bh.consume(rs);
 293     }
 294 
 295 
 296 
 297     @Benchmark
 298     public void xorMasked(Blackhole bh) {
 299         int[] as = fa.apply(size);
 300         int[] bs = fb.apply(size);
 301         int[] 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                 int a = as[i];
 307                 int b = bs[i];
 308                 if (ms[i % ms.length]) {
 309                     rs[i] = (int)(a ^ b);
 310                 } else {
 311                     rs[i] = a;
 312                 }
 313             }
 314         }
 315         bh.consume(rs);
 316     }
 317 
 318 
 319 
 320     @Benchmark
 321     public void shiftR(Blackhole bh) {
 322         int[] as = fa.apply(size);
 323         int[] bs = fb.apply(size);
 324         int[] rs = fr.apply(size);
 325 
 326         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 327             for (int i = 0; i < as.length; i++) {
 328                 int a = as[i];
 329                 int b = bs[i];
 330                 rs[i] = (int)((a >>> b));
 331             }
 332         }
 333 
 334         bh.consume(rs);
 335     }
 336 
 337 
 338 
 339     @Benchmark
 340     public void shiftRMasked(Blackhole bh) {
 341         int[] as = fa.apply(size);
 342         int[] bs = fb.apply(size);
 343         int[] rs = fr.apply(size);
 344         boolean[] ms = fm.apply(size);
 345 
 346         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 347             for (int i = 0; i < as.length; i++) {
 348                 int a = as[i];
 349                 int b = bs[i];
 350                 if (ms[i % ms.length]) {
 351                     rs[i] = (int)((a >>> b));
 352                 } else {
 353                     rs[i] = a;
 354                 }
 355             }
 356         }
 357         bh.consume(rs);
 358     }
 359 
 360 
 361 
 362     @Benchmark
 363     public void shiftL(Blackhole bh) {
 364         int[] as = fa.apply(size);
 365         int[] bs = fb.apply(size);
 366         int[] rs = fr.apply(size);
 367 
 368         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 369             for (int i = 0; i < as.length; i++) {
 370                 int a = as[i];
 371                 int b = bs[i];
 372                 rs[i] = (int)((a << b));
 373             }
 374         }
 375 
 376         bh.consume(rs);
 377     }
 378 
 379 
 380 
 381     @Benchmark
 382     public void shiftLMasked(Blackhole bh) {
 383         int[] as = fa.apply(size);
 384         int[] bs = fb.apply(size);
 385         int[] rs = fr.apply(size);
 386         boolean[] ms = fm.apply(size);
 387 
 388         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 389             for (int i = 0; i < as.length; i++) {
 390                 int a = as[i];
 391                 int b = bs[i];
 392                 if (ms[i % ms.length]) {
 393                     rs[i] = (int)((a << b));
 394                 } else {
 395                     rs[i] = a;
 396                 }
 397             }
 398         }
 399         bh.consume(rs);
 400     }
 401 
 402 
 403 
 404     @Benchmark
 405     public void aShiftR(Blackhole bh) {
 406         int[] as = fa.apply(size);
 407         int[] bs = fb.apply(size);
 408         int[] rs = fr.apply(size);
 409 
 410         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 411             for (int i = 0; i < as.length; i++) {
 412                 int a = as[i];
 413                 int b = bs[i];
 414                 rs[i] = (int)((a >> b));
 415             }
 416         }
 417 
 418         bh.consume(rs);
 419     }
 420 
 421 
 422 
 423     @Benchmark
 424     public void aShiftRMasked(Blackhole bh) {
 425         int[] as = fa.apply(size);
 426         int[] bs = fb.apply(size);
 427         int[] rs = fr.apply(size);
 428         boolean[] ms = fm.apply(size);
 429 
 430         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 431             for (int i = 0; i < as.length; i++) {
 432                 int a = as[i];
 433                 int b = bs[i];
 434                 if (ms[i % ms.length]) {
 435                     rs[i] = (int)((a >> b));
 436                 } else {
 437                     rs[i] = a;
 438                 }
 439             }
 440         }
 441         bh.consume(rs);
 442     }
 443 
 444 
 445 
 446     @Benchmark
 447     public void aShiftRShift(Blackhole bh) {
 448         int[] as = fa.apply(size);
 449         int[] bs = fb.apply(size);
 450         int[] rs = fr.apply(size);
 451 
 452         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 453             for (int i = 0; i < as.length; i++) {
 454                 int a = as[i];
 455                 int b = bs[i];
 456                 rs[i] = (int)((a >> b));
 457             }
 458         }
 459 
 460         bh.consume(rs);
 461     }
 462 
 463 
 464 
 465     @Benchmark
 466     public void aShiftRMaskedShift(Blackhole bh) {
 467         int[] as = fa.apply(size);
 468         int[] bs = fb.apply(size);
 469         int[] rs = fr.apply(size);
 470         boolean[] ms = fm.apply(size);
 471 
 472         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 473             for (int i = 0; i < as.length; i++) {
 474                 int a = as[i];
 475                 int b = bs[i];
 476                 boolean m = ms[i % ms.length];
 477                 rs[i] = (m ? (int)((a >> b)) : a);
 478             }
 479         }
 480 
 481         bh.consume(rs);
 482     }
 483 
 484 
 485 
 486     @Benchmark
 487     public void shiftRShift(Blackhole bh) {
 488         int[] as = fa.apply(size);
 489         int[] bs = fb.apply(size);
 490         int[] rs = fr.apply(size);
 491 
 492         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 493             for (int i = 0; i < as.length; i++) {
 494                 int a = as[i];
 495                 int b = bs[i];
 496                 rs[i] = (int)((a >>> b));
 497             }
 498         }
 499 
 500         bh.consume(rs);
 501     }
 502 
 503 
 504 
 505     @Benchmark
 506     public void shiftRMaskedShift(Blackhole bh) {
 507         int[] as = fa.apply(size);
 508         int[] bs = fb.apply(size);
 509         int[] rs = fr.apply(size);
 510         boolean[] ms = fm.apply(size);
 511 
 512         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 513             for (int i = 0; i < as.length; i++) {
 514                 int a = as[i];
 515                 int b = bs[i];
 516                 boolean m = ms[i % ms.length];
 517                 rs[i] = (m ? (int)((a >>> b)) : a);
 518             }
 519         }
 520 
 521         bh.consume(rs);
 522     }
 523 
 524 
 525 
 526     @Benchmark
 527     public void shiftLShift(Blackhole bh) {
 528         int[] as = fa.apply(size);
 529         int[] bs = fb.apply(size);
 530         int[] rs = fr.apply(size);
 531 
 532         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 533             for (int i = 0; i < as.length; i++) {
 534                 int a = as[i];
 535                 int b = bs[i];
 536                 rs[i] = (int)((a << b));
 537             }
 538         }
 539 
 540         bh.consume(rs);
 541     }
 542 
 543 
 544 
 545     @Benchmark
 546     public void shiftLMaskedShift(Blackhole bh) {
 547         int[] as = fa.apply(size);
 548         int[] bs = fb.apply(size);
 549         int[] rs = fr.apply(size);
 550         boolean[] ms = fm.apply(size);
 551 
 552         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 553             for (int i = 0; i < as.length; i++) {
 554                 int a = as[i];
 555                 int b = bs[i];
 556                 boolean m = ms[i % ms.length];
 557                 rs[i] = (m ? (int)((a << b)) : a);
 558             }
 559         }
 560 
 561         bh.consume(rs);
 562     }
 563 
 564 
 565 
 566 
 567 
 568 
 569 
 570 
 571 
 572 
 573 
 574 
 575 
 576 
 577     @Benchmark
 578     public void max(Blackhole bh) {
 579         int[] as = fa.apply(size);
 580         int[] bs = fb.apply(size);
 581         int[] rs = fr.apply(size);
 582 
 583         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 584             for (int i = 0; i < as.length; i++) {
 585                 int a = as[i];
 586                 int b = bs[i];
 587                 rs[i] = (int)(Math.max(a, b));
 588             }
 589         }
 590 
 591         bh.consume(rs);
 592     }
 593 
 594     @Benchmark
 595     public void min(Blackhole bh) {
 596         int[] as = fa.apply(size);
 597         int[] bs = fb.apply(size);
 598         int[] rs = fr.apply(size);
 599 
 600         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 601             for (int i = 0; i < as.length; i++) {
 602                 int a = as[i];
 603                 int b = bs[i];
 604                 rs[i] = (int)(Math.min(a, b));
 605             }
 606         }
 607 
 608         bh.consume(rs);
 609     }
 610 
 611 
 612     @Benchmark
 613     public void andAll(Blackhole bh) {
 614         int[] as = fa.apply(size);
 615         int r = -1;
 616         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 617             r = -1;
 618             for (int i = 0; i < as.length; i++) {
 619                 r &= as[i];
 620             }
 621         }
 622         bh.consume(r);
 623     }
 624 
 625 
 626 
 627     @Benchmark
 628     public void orAll(Blackhole bh) {
 629         int[] as = fa.apply(size);
 630         int r = 0;
 631         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 632             r = 0;
 633             for (int i = 0; i < as.length; i++) {
 634                 r |= as[i];
 635             }
 636         }
 637         bh.consume(r);
 638     }
 639 
 640 
 641 
 642     @Benchmark
 643     public void xorAll(Blackhole bh) {
 644         int[] as = fa.apply(size);
 645         int r = 0;
 646         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 647             r = 0;
 648             for (int i = 0; i < as.length; i++) {
 649                 r ^= as[i];
 650             }
 651         }
 652         bh.consume(r);
 653     }
 654 
 655 
 656     @Benchmark
 657     public void addAll(Blackhole bh) {
 658         int[] as = fa.apply(size);
 659         int r = 0;
 660         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 661             r = 0;
 662             for (int i = 0; i < as.length; i++) {
 663                 r += as[i];
 664             }
 665         }
 666         bh.consume(r);
 667     }
 668 
 669     @Benchmark
 670     public void mulAll(Blackhole bh) {
 671         int[] as = fa.apply(size);
 672         int r = 1;
 673         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 674             r = 1;
 675             for (int i = 0; i < as.length; i++) {
 676                 r *= as[i];
 677             }
 678         }
 679         bh.consume(r);
 680     }
 681 
 682     @Benchmark
 683     public void minAll(Blackhole bh) {
 684         int[] as = fa.apply(size);
 685         int r = Integer.MAX_VALUE;
 686         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 687             r = Integer.MAX_VALUE;
 688             for (int i = 0; i < as.length; i++) {
 689                 r = (int)Math.min(r, as[i]);
 690             }
 691         }
 692         bh.consume(r);
 693     }
 694 
 695     @Benchmark
 696     public void maxAll(Blackhole bh) {
 697         int[] as = fa.apply(size);
 698         int r = Integer.MIN_VALUE;
 699         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 700             r = Integer.MIN_VALUE;
 701             for (int i = 0; i < as.length; i++) {
 702                 r = (int)Math.max(r, as[i]);
 703             }
 704         }
 705         bh.consume(r);
 706     }
 707 
 708 
 709     @Benchmark
 710     public void anyTrue(Blackhole bh) {
 711         boolean[] ms = fm.apply(size);
 712         boolean r = false;
 713         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 714             r = false;
 715             for (int i = 0; i < ms.length; i++) {
 716                 r |= ms[i];
 717             }
 718         }
 719         bh.consume(r);
 720     }
 721 
 722 
 723 
 724     @Benchmark
 725     public void allTrue(Blackhole bh) {
 726         boolean[] ms = fm.apply(size);
 727         boolean r = true;
 728         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 729             r = true;
 730             for (int i = 0; i < ms.length; i++) {
 731                 r &= ms[i];
 732             }
 733         }
 734         bh.consume(r);
 735     }
 736 
 737 
 738     @Benchmark
 739     public void lessThan(Blackhole bh) {
 740         int[] as = fa.apply(size);
 741         int[] bs = fb.apply(size);
 742 
 743         boolean r = false;
 744         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 745             r = false;
 746             for (int i = 0; i < as.length; i++) {
 747                 boolean m = (as[i] < bs[i]);
 748                 r |= m; // accumulate so JIT can't eliminate the computation
 749             }
 750         }
 751 
 752         bh.consume(r);
 753     }
 754 
 755     @Benchmark
 756     public void greaterThan(Blackhole bh) {
 757         int[] as = fa.apply(size);
 758         int[] bs = fb.apply(size);
 759 
 760         boolean r = false;
 761         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 762             r = false;
 763             for (int i = 0; i < as.length; i++) {
 764                 boolean m = (as[i] > bs[i]);
 765                 r |= m; // accumulate so JIT can't eliminate the computation
 766             }
 767         }
 768 
 769         bh.consume(r);
 770     }
 771 
 772     @Benchmark
 773     public void equal(Blackhole bh) {
 774         int[] as = fa.apply(size);
 775         int[] bs = fb.apply(size);
 776 
 777         boolean r = false;
 778         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 779             r = false;
 780             for (int i = 0; i < as.length; i++) {
 781                 boolean m = (as[i] == bs[i]);
 782                 r |= m; // accumulate so JIT can't eliminate the computation
 783             }
 784         }
 785 
 786         bh.consume(r);
 787     }
 788 
 789     @Benchmark
 790     public void notEqual(Blackhole bh) {
 791         int[] as = fa.apply(size);
 792         int[] bs = fb.apply(size);
 793 
 794         boolean r = false;
 795         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 796             r = false;
 797             for (int i = 0; i < as.length; i++) {
 798                 boolean m = (as[i] != bs[i]);
 799                 r |= m; // accumulate so JIT can't eliminate the computation
 800             }
 801         }
 802 
 803         bh.consume(r);
 804     }
 805 
 806     @Benchmark
 807     public void lessThanEq(Blackhole bh) {
 808         int[] as = fa.apply(size);
 809         int[] bs = fb.apply(size);
 810 
 811         boolean r = false;
 812         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 813             r = false;
 814             for (int i = 0; i < as.length; i++) {
 815                 boolean m = (as[i] <= bs[i]);
 816                 r |= m; // accumulate so JIT can't eliminate the computation
 817             }
 818         }
 819 
 820         bh.consume(r);
 821     }
 822 
 823     @Benchmark
 824     public void greaterThanEq(Blackhole bh) {
 825         int[] as = fa.apply(size);
 826         int[] bs = fb.apply(size);
 827 
 828         boolean r = false;
 829         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 830             r = false;
 831             for (int i = 0; i < as.length; i++) {
 832                 boolean m = (as[i] >= bs[i]);
 833                 r |= m; // accumulate so JIT can't eliminate the computation
 834             }
 835         }
 836 
 837         bh.consume(r);
 838     }
 839 
 840     @Benchmark
 841     public void blend(Blackhole bh) {
 842         int[] as = fa.apply(size);
 843         int[] bs = fb.apply(size);
 844         int[] rs = fr.apply(size);
 845         boolean[] ms = fm.apply(size);
 846 
 847         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 848             for (int i = 0; i < as.length; i++) {
 849                 int a = as[i];
 850                 int b = bs[i];
 851                 boolean m = ms[i % ms.length];
 852                 rs[i] = (m ? b : a);
 853             }
 854         }
 855 
 856         bh.consume(rs);
 857     }
 858     void rearrangeShared(int window, Blackhole bh) {
 859         int[] as = fa.apply(size);
 860         int[] order = fs.apply(size);
 861         int[] rs = fr.apply(size);
 862 
 863         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 864             for (int i = 0; i < as.length; i += window) {
 865                 for (int j = 0; j < window; j++) {
 866                     int a = as[i+j];
 867                     int pos = order[j];
 868                     rs[i + pos] = a;
 869                 }
 870             }
 871         }
 872 
 873         bh.consume(rs);
 874     }
 875 
 876     @Benchmark
 877     public void rearrange064(Blackhole bh) {
 878         int window = 64 / Integer.SIZE;
 879         rearrangeShared(window, bh);
 880     }
 881 
 882     @Benchmark
 883     public void rearrange128(Blackhole bh) {
 884         int window = 128 / Integer.SIZE;
 885         rearrangeShared(window, bh);
 886     }
 887 
 888     @Benchmark
 889     public void rearrange256(Blackhole bh) {
 890         int window = 256 / Integer.SIZE;
 891         rearrangeShared(window, bh);
 892     }
 893 
 894     @Benchmark
 895     public void rearrange512(Blackhole bh) {
 896         int window = 512 / Integer.SIZE;
 897         rearrangeShared(window, bh);
 898     }
 899 
 900 
 901 
 902 
 903 
 904 
 905 
 906 
 907 
 908 
 909 
 910 
 911 
 912 
 913 
 914 
 915 
 916 
 917 
 918 
 919 
 920     @Benchmark
 921     public void neg(Blackhole bh) {
 922         int[] as = fa.apply(size);
 923         int[] rs = fr.apply(size);
 924 
 925         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 926             for (int i = 0; i < as.length; i++) {
 927                 int a = as[i];
 928                 rs[i] = (int)(-((int)a));
 929             }
 930         }
 931 
 932         bh.consume(rs);
 933     }
 934 
 935     @Benchmark
 936     public void negMasked(Blackhole bh) {
 937         int[] as = fa.apply(size);
 938         int[] rs = fr.apply(size);
 939         boolean[] ms = fm.apply(size);
 940 
 941         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 942             for (int i = 0; i < as.length; i++) {
 943                 int a = as[i];
 944                 boolean m = ms[i % ms.length];
 945                 rs[i] = (m ? (int)(-((int)a)) : a);
 946             }
 947         }
 948 
 949         bh.consume(rs);
 950     }
 951 
 952     @Benchmark
 953     public void abs(Blackhole bh) {
 954         int[] as = fa.apply(size);
 955         int[] rs = fr.apply(size);
 956 
 957         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 958             for (int i = 0; i < as.length; i++) {
 959                 int a = as[i];
 960                 rs[i] = (int)(Math.abs((int)a));
 961             }
 962         }
 963 
 964         bh.consume(rs);
 965     }
 966 
 967     @Benchmark
 968     public void absMasked(Blackhole bh) {
 969         int[] as = fa.apply(size);
 970         int[] rs = fr.apply(size);
 971         boolean[] ms = fm.apply(size);
 972 
 973         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 974             for (int i = 0; i < as.length; i++) {
 975                 int a = as[i];
 976                 boolean m = ms[i % ms.length];
 977                 rs[i] = (m ? (int)(Math.abs((int)a)) : a);
 978             }
 979         }
 980 
 981         bh.consume(rs);
 982     }
 983 
 984 
 985     @Benchmark
 986     public void not(Blackhole bh) {
 987         int[] as = fa.apply(size);
 988         int[] rs = fr.apply(size);
 989 
 990         for (int ic = 0; ic < INVOC_COUNT; ic++) {
 991             for (int i = 0; i < as.length; i++) {
 992                 int a = as[i];
 993                 rs[i] = (int)(~((int)a));
 994             }
 995         }
 996 
 997         bh.consume(rs);
 998     }
 999 
1000 
1001 
1002     @Benchmark
1003     public void notMasked(Blackhole bh) {
1004         int[] as = fa.apply(size);
1005         int[] rs = fr.apply(size);
1006         boolean[] ms = fm.apply(size);
1007 
1008         for (int ic = 0; ic < INVOC_COUNT; ic++) {
1009             for (int i = 0; i < as.length; i++) {
1010                 int a = as[i];
1011                 boolean m = ms[i % ms.length];
1012                 rs[i] = (m ? (int)(~((int)a)) : a);
1013             }
1014         }
1015 
1016         bh.consume(rs);
1017     }
1018 
1019 
1020 
1021 
1022     @Benchmark
1023     public void gatherBase0(Blackhole bh) {
1024         int[] as = fa.apply(size);
1025         int[] is    = fs.apply(size);
1026         int[] rs = fr.apply(size);
1027 
1028         for (int ic = 0; ic < INVOC_COUNT; ic++) {
1029             for (int i = 0; i < as.length; i++) {
1030                 int ix = 0 + is[i];
1031                 rs[i] = as[ix];
1032             }
1033         }
1034 
1035         bh.consume(rs);
1036     }
1037 
1038 
1039     void gather(int window, Blackhole bh) {
1040         int[] as = fa.apply(size);
1041         int[] is    = fs.apply(size);
1042         int[] rs = fr.apply(size);
1043 
1044         for (int ic = 0; ic < INVOC_COUNT; ic++) {
1045             for (int i = 0; i < as.length; i += window) {
1046                 for (int j = 0; j < window; j++) {
1047                     int ix = i + is[i + j];
1048                     rs[i + j] = as[ix];
1049                 }
1050             }
1051         }
1052 
1053         bh.consume(rs);
1054     }
1055 
1056     @Benchmark
1057     public void gather064(Blackhole bh) {
1058         int window = 64 / Integer.SIZE;
1059         gather(window, bh);
1060     }
1061 
1062     @Benchmark
1063     public void gather128(Blackhole bh) {
1064         int window = 128 / Integer.SIZE;
1065         gather(window, bh);
1066     }
1067 
1068     @Benchmark
1069     public void gather256(Blackhole bh) {
1070         int window = 256 / Integer.SIZE;
1071         gather(window, bh);
1072     }
1073 
1074     @Benchmark
1075     public void gather512(Blackhole bh) {
1076         int window = 512 / Integer.SIZE;
1077         gather(window, bh);
1078     }
1079 
1080 
1081 
1082     @Benchmark
1083     public void scatterBase0(Blackhole bh) {
1084         int[] as = fa.apply(size);
1085         int[] is    = fs.apply(size);
1086         int[] rs = fr.apply(size);
1087 
1088         for (int ic = 0; ic < INVOC_COUNT; ic++) {
1089             for (int i = 0; i < as.length; i++) {
1090                 int ix = 0 + is[i];
1091                 rs[ix] = as[i];
1092             }
1093         }
1094 
1095         bh.consume(rs);
1096     }
1097 
1098     void scatter(int window, Blackhole bh) {
1099         int[] as = fa.apply(size);
1100         int[] is    = fs.apply(size);
1101         int[] rs = fr.apply(size);
1102 
1103         for (int ic = 0; ic < INVOC_COUNT; ic++) {
1104             for (int i = 0; i < as.length; i += window) {
1105                 for (int j = 0; j < window; j++) {
1106                     int ix = i + is[i + j];
1107                     rs[ix] = as[i + j];
1108                 }
1109             }
1110         }
1111 
1112         bh.consume(rs);
1113     }
1114 
1115     @Benchmark
1116     public void scatter064(Blackhole bh) {
1117         int window = 64 / Integer.SIZE;
1118         scatter(window, bh);
1119     }
1120 
1121     @Benchmark
1122     public void scatter128(Blackhole bh) {
1123         int window = 128 / Integer.SIZE;
1124         scatter(window, bh);
1125     }
1126 
1127     @Benchmark
1128     public void scatter256(Blackhole bh) {
1129         int window = 256 / Integer.SIZE;
1130         scatter(window, bh);
1131     }
1132 
1133     @Benchmark
1134     public void scatter512(Blackhole bh) {
1135         int window = 512 / Integer.SIZE;
1136         scatter(window, bh);
1137     }
1138 
1139 }
1140 
< prev index next >