-
Notifications
You must be signed in to change notification settings - Fork 146
/
Main.java
61 lines (47 loc) · 1.83 KB
/
Main.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
package com.mscharhag.vectorapi;
import jdk.incubator.vector.*;
public class Main {
static final VectorSpecies<Float> SPECIES = FloatVector.SPECIES_128;
public static void main(String[] args) {
example();
loopExample();
}
public static void example() {
float[] a = new float[] {1f, 2f, 3f, 4f};
float[] b = new float[] {5f, 8f, 10f, 12f};
FloatVector first = FloatVector.fromArray(FloatVector.SPECIES_128, a, 0);
FloatVector second = FloatVector.fromArray(FloatVector.SPECIES_128, b, 0);
FloatVector result = first
.add(second)
.pow(2)
.neg();
System.out.println(result);
float[] resultArray = new float[4];
result.intoArray(resultArray, 0);
for (int i = 0; i < resultArray.length; i++) {
System.out.println(resultArray[i]);
}
}
public static void loopExample() {
float[] a = randomFloatArray(10_000);
float[] b = randomFloatArray(10_000);
float[] c = new float[10_000];
for (int i = 0; i < a.length; i += SPECIES.length()) {
VectorMask<Float> mask = SPECIES.indexInRange(i, a.length);
FloatVector first = FloatVector.fromArray(SPECIES, a, i, mask);
FloatVector second = FloatVector.fromArray(SPECIES, b, i, mask);
first.add(second).intoArray(c, i, mask);
}
System.out.println("result (last 10 elements):");
for (int i = a.length - 10; i < a.length; i++) {
System.out.printf("%d. %f + %f = %f%n", i, a[i], b[i], c[i]);
}
}
public static float[] randomFloatArray(int size) {
float[] array = new float[size];
for (int i = 0; i < size; i++) {
array[i] = (float) Math.random() * 10000;
}
return array;
}
}