/
VectorSupport.java
666 lines (571 loc) · 23.5 KB
/
VectorSupport.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/*
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.vm.vector;
import jdk.internal.vm.annotation.IntrinsicCandidate;
import jdk.internal.misc.Unsafe;
import java.util.function.*;
public class VectorSupport {
static {
registerNatives();
}
private static final Unsafe U = Unsafe.getUnsafe();
// Unary
public static final int VECTOR_OP_ABS = 0;
public static final int VECTOR_OP_NEG = 1;
public static final int VECTOR_OP_SQRT = 2;
// Binary
public static final int VECTOR_OP_ADD = 4;
public static final int VECTOR_OP_SUB = 5;
public static final int VECTOR_OP_MUL = 6;
public static final int VECTOR_OP_DIV = 7;
public static final int VECTOR_OP_MIN = 8;
public static final int VECTOR_OP_MAX = 9;
public static final int VECTOR_OP_AND = 10;
public static final int VECTOR_OP_OR = 11;
public static final int VECTOR_OP_XOR = 12;
// Ternary
public static final int VECTOR_OP_FMA = 13;
// Broadcast int
public static final int VECTOR_OP_LSHIFT = 14;
public static final int VECTOR_OP_RSHIFT = 15;
public static final int VECTOR_OP_URSHIFT = 16;
public static final int VECTOR_OP_CAST = 17;
public static final int VECTOR_OP_REINTERPRET = 18;
// Mask manipulation operations
public static final int VECTOR_OP_MASK_TRUECOUNT = 19;
public static final int VECTOR_OP_MASK_FIRSTTRUE = 20;
public static final int VECTOR_OP_MASK_LASTTRUE = 21;
public static final int VECTOR_OP_MASK_TOLONG = 22;
// Rotate operations
public static final int VECTOR_OP_LROTATE = 23;
public static final int VECTOR_OP_RROTATE = 24;
// Math routines
public static final int VECTOR_OP_TAN = 101;
public static final int VECTOR_OP_TANH = 102;
public static final int VECTOR_OP_SIN = 103;
public static final int VECTOR_OP_SINH = 104;
public static final int VECTOR_OP_COS = 105;
public static final int VECTOR_OP_COSH = 106;
public static final int VECTOR_OP_ASIN = 107;
public static final int VECTOR_OP_ACOS = 108;
public static final int VECTOR_OP_ATAN = 109;
public static final int VECTOR_OP_ATAN2 = 110;
public static final int VECTOR_OP_CBRT = 111;
public static final int VECTOR_OP_LOG = 112;
public static final int VECTOR_OP_LOG10 = 113;
public static final int VECTOR_OP_LOG1P = 114;
public static final int VECTOR_OP_POW = 115;
public static final int VECTOR_OP_EXP = 116;
public static final int VECTOR_OP_EXPM1 = 117;
public static final int VECTOR_OP_HYPOT = 118;
// See src/hotspot/share/opto/subnode.hpp
// struct BoolTest, and enclosed enum mask
public static final int BT_eq = 0; // 0000
public static final int BT_ne = 4; // 0100
public static final int BT_le = 5; // 0101
public static final int BT_ge = 7; // 0111
public static final int BT_lt = 3; // 0011
public static final int BT_gt = 1; // 0001
public static final int BT_overflow = 2; // 0010
public static final int BT_no_overflow = 6; // 0110
// never = 8 1000
// illegal = 9 1001
// Unsigned comparisons apply to BT_le, BT_ge, BT_lt, BT_gt for integral types
public static final int BT_unsigned_compare = 0b10000;
public static final int BT_ule = BT_le | BT_unsigned_compare;
public static final int BT_uge = BT_ge | BT_unsigned_compare;
public static final int BT_ult = BT_lt | BT_unsigned_compare;
public static final int BT_ugt = BT_gt | BT_unsigned_compare;
// BasicType codes, for primitives only:
public static final int
T_FLOAT = 6,
T_DOUBLE = 7,
T_BYTE = 8,
T_SHORT = 9,
T_INT = 10,
T_LONG = 11;
/* ============================================================================ */
public static class VectorSpecies<E> {}
public static class VectorPayload {
private final Object payload; // array of primitives
public VectorPayload(Object payload) {
this.payload = payload;
}
protected final Object getPayload() {
return VectorSupport.maybeRebox(this).payload;
}
}
public static class Vector<E> extends VectorPayload {
public Vector(Object payload) {
super(payload);
}
}
public static class VectorShuffle<E> extends VectorPayload {
public VectorShuffle(Object payload) {
super(payload);
}
}
public static class VectorMask<E> extends VectorPayload {
public VectorMask(Object payload) {
super(payload);
}
}
/* ============================================================================ */
public interface BroadcastOperation<VM extends VectorPayload,
S extends VectorSpecies<?>> {
VM broadcast(long l, S s);
}
@IntrinsicCandidate
public static
<VM extends VectorPayload,
S extends VectorSpecies<E>,
E>
VM broadcastCoerced(Class<? extends VM> vmClass, Class<E> eClass,
int length,
long bits, S s,
BroadcastOperation<VM, S> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.broadcast(bits, s);
}
/* ============================================================================ */
public interface ShuffleIotaOperation<S extends VectorSpecies<?>,
SH extends VectorShuffle<?>> {
SH apply(int length, int start, int step, S s);
}
@IntrinsicCandidate
public static
<E,
S extends VectorSpecies<E>,
SH extends VectorShuffle<E>>
SH shuffleIota(Class<E> eClass, Class<? extends SH> shClass, S s,
int length,
int start, int step, int wrap,
ShuffleIotaOperation<S, SH> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.apply(length, start, step, s);
}
public interface ShuffleToVectorOperation<V extends Vector<?>,
SH extends VectorShuffle<?>> {
V apply(SH sh);
}
@IntrinsicCandidate
public static
<V extends Vector<E>,
SH extends VectorShuffle<E>,
E>
V shuffleToVector(Class<? extends Vector<E>> vClass, Class<E> eClass, Class<? extends SH> shClass, SH sh,
int length,
ShuffleToVectorOperation<V, SH> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.apply(sh);
}
/* ============================================================================ */
public interface IndexOperation<V extends Vector<?>,
S extends VectorSpecies<?>> {
V index(V v, int step, S s);
}
//FIXME @IntrinsicCandidate
public static
<V extends Vector<E>,
E,
S extends VectorSpecies<E>>
V indexVector(Class<? extends V> vClass, Class<E> eClass,
int length,
V v, int step, S s,
IndexOperation<V, S> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.index(v, step, s);
}
/* ============================================================================ */
public interface ReductionOperation<V extends Vector<?>,
M extends VectorMask<?>> {
long apply(V v, M m);
}
@IntrinsicCandidate
public static
<V extends Vector<E>,
M extends VectorMask<E>,
E>
long reductionCoerced(int oprId,
Class<? extends V> vClass, Class<? extends M> mClass, Class<E> eClass,
int length,
V v, M m,
ReductionOperation<V, M> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.apply(v, m);
}
/* ============================================================================ */
public interface VecExtractOp<V extends Vector<?>> {
long apply(V v, int i);
}
@IntrinsicCandidate
public static
<V extends Vector<E>,
E>
long extract(Class<? extends V> vClass, Class<E> eClass,
int length,
V v, int i,
VecExtractOp<V> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.apply(v, i);
}
/* ============================================================================ */
public interface VecInsertOp<V extends Vector<?>> {
V apply(V v, int i, long val);
}
@IntrinsicCandidate
public static
<V extends Vector<E>,
E>
V insert(Class<? extends V> vClass, Class<E> eClass,
int length,
V v, int i, long val,
VecInsertOp<V> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.apply(v, i, val);
}
/* ============================================================================ */
public interface UnaryOperation<V extends Vector<?>,
M extends VectorMask<?>> {
V apply(V v, M m);
}
@IntrinsicCandidate
public static
<V extends Vector<E>,
M extends VectorMask<E>,
E>
V unaryOp(int oprId,
Class<? extends V> vClass, Class<? extends M> mClass, Class<E> eClass,
int length,
V v, M m,
UnaryOperation<V, M> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.apply(v, m);
}
/* ============================================================================ */
public interface BinaryOperation<VM extends VectorPayload,
M extends VectorMask<?>> {
VM apply(VM v1, VM v2, M m);
}
@IntrinsicCandidate
public static
<VM extends VectorPayload,
M extends VectorMask<E>,
E>
VM binaryOp(int oprId,
Class<? extends VM> vmClass, Class<? extends M> mClass, Class<E> eClass,
int length,
VM v1, VM v2, M m,
BinaryOperation<VM, M> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.apply(v1, v2, m);
}
/* ============================================================================ */
public interface TernaryOperation<V extends Vector<?>,
M extends VectorMask<?>> {
V apply(V v1, V v2, V v3, M m);
}
@IntrinsicCandidate
public static
<V extends Vector<E>,
M extends VectorMask<E>,
E>
V ternaryOp(int oprId,
Class<? extends V> vClass, Class<? extends M> mClass, Class<E> eClass,
int length,
V v1, V v2, V v3, M m,
TernaryOperation<V, M> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.apply(v1, v2, v3, m);
}
/* ============================================================================ */
// Memory operations
public interface LoadOperation<C,
VM extends VectorPayload,
S extends VectorSpecies<?>> {
VM load(C container, int index, S s);
}
@IntrinsicCandidate
public static
<C,
VM extends VectorPayload,
E,
S extends VectorSpecies<E>>
VM load(Class<? extends VM> vmClass, Class<E> eClass,
int length,
Object base, long offset,
C container, int index, S s,
LoadOperation<C, VM, S> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.load(container, index, s);
}
/* ============================================================================ */
public interface LoadVectorMaskedOperation<C,
V extends Vector<?>,
S extends VectorSpecies<?>,
M extends VectorMask<?>> {
V load(C container, int index, S s, M m);
}
@IntrinsicCandidate
public static
<C,
V extends Vector<?>,
E,
S extends VectorSpecies<E>,
M extends VectorMask<E>>
V loadMasked(Class<? extends V> vClass, Class<M> mClass, Class<E> eClass,
int length,
Object base, long offset,
M m, C container, int index, S s,
LoadVectorMaskedOperation<C, V, S, M> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.load(container, index, s, m);
}
/* ============================================================================ */
public interface LoadVectorOperationWithMap<C,
V extends Vector<?>,
S extends VectorSpecies<?>,
M extends VectorMask<?>> {
V loadWithMap(C container, int index, int[] indexMap, int indexM, S s, M m);
}
@IntrinsicCandidate
public static
<C,
V extends Vector<?>,
W extends Vector<Integer>,
S extends VectorSpecies<E>,
M extends VectorMask<E>,
E>
V loadWithMap(Class<? extends V> vClass, Class<M> mClass, Class<E> eClass,
int length,
Class<? extends Vector<Integer>> vectorIndexClass,
Object base, long offset,
W index_vector,
M m, C container, int index, int[] indexMap, int indexM, S s,
LoadVectorOperationWithMap<C, V, S, M> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.loadWithMap(container, index, indexMap, indexM, s, m);
}
/* ============================================================================ */
public interface StoreVectorOperation<C,
V extends Vector<?>> {
void store(C container, int index, V v);
}
@IntrinsicCandidate
public static
<C,
V extends Vector<?>>
void store(Class<?> vClass, Class<?> eClass,
int length,
Object base, long offset,
V v, C container, int index,
StoreVectorOperation<C, V> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
defaultImpl.store(container, index, v);
}
public interface StoreVectorMaskedOperation<C,
V extends Vector<?>,
M extends VectorMask<?>> {
void store(C container, int index, V v, M m);
}
@IntrinsicCandidate
public static
<C,
V extends Vector<E>,
M extends VectorMask<E>,
E>
void storeMasked(Class<? extends V> vClass, Class<M> mClass, Class<E> eClass,
int length,
Object base, long offset,
V v, M m, C container, int index,
StoreVectorMaskedOperation<C, V, M> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
defaultImpl.store(container, index, v, m);
}
/* ============================================================================ */
public interface StoreVectorOperationWithMap<C,
V extends Vector<?>,
M extends VectorMask<?>> {
void storeWithMap(C container, int index, V v, int[] indexMap, int indexM, M m);
}
@IntrinsicCandidate
public static
<C,
V extends Vector<E>,
W extends Vector<Integer>,
M extends VectorMask<E>,
E>
void storeWithMap(Class<? extends V> vClass, Class<M> mClass, Class<E> eClass,
int length,
Class<? extends Vector<Integer>> vectorIndexClass,
Object base, long offset,
W index_vector,
V v, M m, C container, int index, int[] indexMap, int indexM,
StoreVectorOperationWithMap<C, V, M> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
defaultImpl.storeWithMap(container, index, v, indexMap, indexM, m);
}
/* ============================================================================ */
@IntrinsicCandidate
public static
<M extends VectorMask<E>,
E>
boolean test(int cond,
Class<?> mClass, Class<?> eClass,
int length,
M m1, M m2,
BiFunction<M, M, Boolean> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.apply(m1, m2);
}
/* ============================================================================ */
public interface VectorCompareOp<V extends Vector<?>,
M extends VectorMask<?>> {
M apply(int cond, V v1, V v2, M m);
}
@IntrinsicCandidate
public static
<V extends Vector<E>,
M extends VectorMask<E>,
E>
M compare(int cond,
Class<? extends V> vectorClass, Class<M> mClass, Class<E> eClass,
int length,
V v1, V v2, M m,
VectorCompareOp<V, M> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.apply(cond, v1, v2, m);
}
/* ============================================================================ */
public interface VectorRearrangeOp<V extends Vector<?>,
SH extends VectorShuffle<?>,
M extends VectorMask<?>> {
V apply(V v, SH sh, M m);
}
@IntrinsicCandidate
public static
<V extends Vector<E>,
SH extends VectorShuffle<E>,
M extends VectorMask<E>,
E>
V rearrangeOp(Class<? extends V> vClass, Class<SH> shClass, Class<M> mClass, Class<E> eClass,
int length,
V v, SH sh, M m,
VectorRearrangeOp<V, SH, M> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.apply(v, sh, m);
}
/* ============================================================================ */
public interface VectorBlendOp<V extends Vector<?>,
M extends VectorMask<?>> {
V apply(V v1, V v2, M m);
}
@IntrinsicCandidate
public static
<V extends Vector<E>,
M extends VectorMask<E>,
E>
V blend(Class<? extends V> vClass, Class<M> mClass, Class<E> eClass,
int length,
V v1, V v2, M m,
VectorBlendOp<V, M> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.apply(v1, v2, m);
}
/* ============================================================================ */
public interface VectorBroadcastIntOp<V extends Vector<?>,
M extends VectorMask<?>> {
V apply(V v, int n, M m);
}
@IntrinsicCandidate
public static
<V extends Vector<E>,
M extends VectorMask<E>,
E>
V broadcastInt(int opr,
Class<? extends V> vClass, Class<? extends M> mClass, Class<E> eClass,
int length,
V v, int n, M m,
VectorBroadcastIntOp<V, M> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.apply(v, n, m);
}
/* ============================================================================ */
public interface VectorConvertOp<VOUT extends VectorPayload,
VIN extends VectorPayload,
S extends VectorSpecies<?>> {
VOUT apply(VIN v, S s);
}
// Users of this intrinsic assume that it respects
// REGISTER_ENDIAN, which is currently ByteOrder.LITTLE_ENDIAN.
// See javadoc for REGISTER_ENDIAN.
@IntrinsicCandidate
public static <VOUT extends VectorPayload,
VIN extends VectorPayload,
S extends VectorSpecies<?>>
VOUT convert(int oprId,
Class<?> fromVectorClass, Class<?> fromeClass, int fromVLen,
Class<?> toVectorClass, Class<?> toeClass, int toVLen,
VIN v, S s,
VectorConvertOp<VOUT, VIN, S> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.apply(v, s);
}
/* ============================================================================ */
@IntrinsicCandidate
public static
<VP extends VectorPayload>
VP maybeRebox(VP v) {
// The fence is added here to avoid memory aliasing problems in C2 between scalar & vector accesses.
// TODO: move the fence generation into C2. Generate only when reboxing is taking place.
U.loadFence();
return v;
}
/* ============================================================================ */
public interface VectorMaskOp<M extends VectorMask<?>> {
long apply(M m);
}
@IntrinsicCandidate
public static
<M extends VectorMask<E>,
E>
long maskReductionCoerced(int oper,
Class<? extends M> mClass, Class<?> eClass,
int length,
M m,
VectorMaskOp<M> defaultImpl) {
assert isNonCapturingLambda(defaultImpl) : defaultImpl;
return defaultImpl.apply(m);
}
/* ============================================================================ */
// query the JVM's supported vector sizes and types
public static native int getMaxLaneCount(Class<?> etype);
/* ============================================================================ */
public static boolean isNonCapturingLambda(Object o) {
return o.getClass().getDeclaredFields().length == 0;
}
/* ============================================================================ */
private static native int registerNatives();
}