Skip to content

Commit

Permalink
Working native code
Browse files Browse the repository at this point in the history
  • Loading branch information
Axionize committed Dec 5, 2024
1 parent d0ff246 commit 61a9bae
Show file tree
Hide file tree
Showing 9 changed files with 887 additions and 9 deletions.
19 changes: 11 additions & 8 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,14 @@ java {
}
compileClasspath += sourceSets.main.get().output +
sourceSets.getByName("java18").output +
sourceSets.getByName("java21").output +
configurations["jmh"]
runtimeClasspath += sourceSets.main.get().output +
sourceSets.getByName("java18").output +
sourceSets.getByName("java21").output +
configurations["jmh"]
}
}
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17

toolchain {
languageVersion.set(JavaLanguageVersion.of(21))
Expand All @@ -225,7 +223,12 @@ tasks.withType<JavaCompile> {
targetCompatibility = "18"
}
if (name == "compileJava21Java") {
options.compilerArgs.addAll(listOf("--add-modules", "jdk.incubator.vector", "-Xlint:unchecked"))
options.compilerArgs.addAll(listOf("--enable-preview", "--add-modules", "jdk.incubator.vector", "-Xlint:unchecked"))
sourceCompatibility = "21"
targetCompatibility = "21"
}
if (name == "compileJmhJava") {
options.compilerArgs.addAll(listOf("--enable-preview", "--add-modules", "jdk.incubator.vector"))
sourceCompatibility = "21"
targetCompatibility = "21"
}
Expand Down Expand Up @@ -298,10 +301,10 @@ tasks.register<JavaCompile>("compileGeneratedJmh") {
source = fileTree("${buildDir}/generated-sources/jmh")

classpath = sourceSets["jmh"].compileClasspath +
sourceSets["java21"].output +
sourceSets["java21"].compileClasspath
sourceSets["java18"].output +
sourceSets["java18"].compileClasspath +
sourceSets["java21"].output +
sourceSets["java21"].compileClasspath +
sourceSets.main.get().output +
sourceSets.main.get().compileClasspath +
files("${buildDir}/classes/java/jmh")
Expand All @@ -321,7 +324,7 @@ tasks.register("jmh") {
javaexec {
classpath = files(tasks.named("jmhJar").get().outputs.files)
mainClass.set("org.openjdk.jmh.Main")
jvmArgs = listOf("--add-modules", "jdk.incubator.vector")
jvmArgs = listOf("--enable-preview", "--add-modules", "jdk.incubator.vector")

// Initialize the args list with default settings
args = mutableListOf<String>().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public final class SIMDVector3D implements Vector3D {
private DoubleVector vector;

public SIMDVector3D(double x, double y, double z) {
this.vector = DoubleVector.fromArray(SPECIES, new double[]{x, y, z, 0}, 0);
this.vector = DoubleVector.fromArray(SPECIES, new double[]{x, y, z, 0}, 0, LENGTH_3_ARRAY_MASK);
}

@Override
Expand Down
148 changes: 148 additions & 0 deletions src/java21/java/ac/grim/grimac/utils/vector/Java21SIMDVector3D.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package ac.grim.grimac.utils.vector;

import jdk.incubator.vector.*;

public final class Java21SIMDVector3D implements Vector3D {

private static final VectorSpecies<Double> SPECIES = DoubleVector.SPECIES_256;
private static final VectorMask<Double> LENGTH_3_ARRAY_MASK = SPECIES.indexInRange(0, 3);
private static final VectorShuffle<Double> CROSS_PRODUCT_SHUFFLE1 = VectorShuffle.fromArray(SPECIES, new int[]{1, 2, 0, 3}, 0);
private static final VectorShuffle<Double> CROSS_PRODUCT_SHUFFLE2 = VectorShuffle.fromArray(SPECIES, new int[]{2, 0, 1, 3}, 0);
private static final DoubleVector FOURTH_COMPONENT_ZERO = DoubleVector.zero(SPECIES).withLane(3, 1.0);

private DoubleVector vector;

public Java21SIMDVector3D(double x, double y, double z) {
this.vector = DoubleVector.fromArray(SPECIES, new double[]{x, y, z, 0}, 0, LENGTH_3_ARRAY_MASK);
}

@Override
public double getX() {
return this.vector.lane(0);
}

@Override
public double getY() {
return this.vector.lane(1);
}

@Override
public double getZ() {
return this.vector.lane(2);
}

@Override
public Vector3D setX(double x) {
this.vector = this.vector.withLane(0, x);
return this;
}

@Override
public Vector3D setY(double y) {
this.vector = this.vector.withLane(1, y);
return this;
}

@Override
public Vector3D setZ(double z) {
this.vector = this.vector.withLane(2, z);
return this;
}

@Override
public double length() {
return Math.sqrt(lengthSquared());
}

@Override
public double lengthSquared() {
return this.vector.mul(this.vector).reduceLanes(VectorOperators.ADD);
}

@Override
public Vector3D multiply(double m) {
this.vector = this.vector.mul(m);
return this;
}

@Override
public Vector3D normalize() {
// DoubleVector squared = this.vector.mul(this.vector);
// double sum = squared.reduceLanes(VectorOperators.ADD);
// double length = Math.sqrt(sum);
// this.vector = this.vector.div(length);
// return this;
this.vector = this.vector.div(length());
return this;
}

@Override
public Vector3D crossProduct(Vector3D other) {
Java21SIMDVector3D o = (Java21SIMDVector3D) other;

DoubleVector tmp0 = this.vector.rearrange(CROSS_PRODUCT_SHUFFLE1);
DoubleVector tmp1 = o.vector.rearrange(CROSS_PRODUCT_SHUFFLE2);

DoubleVector tmp2 = tmp0.mul(o.vector);
DoubleVector tmp3 = tmp0.mul(tmp1);

DoubleVector tmp4 = tmp2.rearrange(CROSS_PRODUCT_SHUFFLE1);

this.vector = tmp3.sub(tmp4).blend(FOURTH_COMPONENT_ZERO, LENGTH_3_ARRAY_MASK.not());
return this;
}

@Override
public Vector3D add(Vector3D o) {
this.vector = this.vector.add(((Java21SIMDVector3D) o).vector);
return this;
}

@Override
public Vector3D subtract(Vector3D o) {
this.vector = this.vector.sub(((Java21SIMDVector3D) o).vector);
return this;
}

@Override
public Vector3D multiply(Vector3D o) {
this.vector = this.vector.mul(((Java21SIMDVector3D) o).vector);
return this;
}

@Override
public double distance(Vector3D o) {
return Math.sqrt(distanceSquared(o));
}

@Override
public double distanceSquared(Vector3D o) {
DoubleVector diff = this.vector.sub(((Java21SIMDVector3D) o).vector);
return diff.mul(diff).reduceLanes(VectorOperators.ADD);
}

@Override
public double dot(Vector3D o) {
return this.vector.mul(((Java21SIMDVector3D) o).vector).reduceLanes(VectorOperators.ADD);
}


@Override
public Vector3D clone() {
// I think this makes a shallow clone if I do it like this?
// try {
// return (Vector3D) super.clone();
// } catch (CloneNotSupportedException e) {
// throw new Error(e);
// }
try {
Java21SIMDVector3D cloned = (Java21SIMDVector3D) super.clone();
// Create a new DoubleVector with the same values
cloned.vector = DoubleVector.fromArray(SPECIES,
new double[]{this.getX(), this.getY(), this.getZ(), 0}, 0);
return cloned;
} catch (CloneNotSupportedException e) {
throw new Error(e);
}
}
}
Loading

0 comments on commit 61a9bae

Please sign in to comment.