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