9
0
mirror of https://github.com/Dreeam-qwq/Gale.git synced 2025-12-19 14:59:29 +00:00
Files
Gale/patches/api/0003-SIMD-support.patch
2022-12-13 14:28:30 +01:00

120 lines
4.3 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Martijn Muijsers <martijnmuijsers@live.nl>
Date: Thu, 24 Nov 2022 01:21:32 +0100
Subject: [PATCH] SIMD support
License: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html)
Gale - https://galemc.org
This patch is based on the following patch:
"Add SIMD utilities"
By: Kevin Raneri <kevin.raneri@gmail.com>
As part of: Pufferfish (https://github.com/pufferfish-gg/Pufferfish)
Licensed under: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html)
diff --git a/build.gradle.kts b/build.gradle.kts
index fdb495c0e0f1ce702a590e6a7bcdcf40fd418d1c..2c3c8fbba6247a44b1782dbed45a00fdaa051473 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -89,6 +89,7 @@ tasks.withType<JavaCompile> {
compilerArgs.add("-Xlint:-module")
compilerArgs.add("-Xlint:-removal")
compilerArgs.add("-Xlint:-dep-ann")
+ compilerArgs.add("--add-modules=jdk.incubator.vector") // Gale - Pufferfish - SIMD support
}
// Gale end - hide irrelevant compilation warnings
diff --git a/src/main/java/gg/pufferfish/pufferfish/simd/SIMDChecker.java b/src/main/java/gg/pufferfish/pufferfish/simd/SIMDChecker.java
new file mode 100644
index 0000000000000000000000000000000000000000..d51f9de6ac01f518ca9577827dc719daad78df4c
--- /dev/null
+++ b/src/main/java/gg/pufferfish/pufferfish/simd/SIMDChecker.java
@@ -0,0 +1,44 @@
+// Gale - Pufferfish - SIMD support
+
+package gg.pufferfish.pufferfish.simd;
+
+import jdk.incubator.vector.FloatVector;
+import jdk.incubator.vector.IntVector;
+import jdk.incubator.vector.VectorSpecies;
+
+import org.slf4j.Logger;
+
+/**
+ * Basically, java is annoying and we have to push this out to its own class.
+ */
+@Deprecated
+public class SIMDChecker {
+
+ @Deprecated
+ public static boolean canEnable(Logger logger, boolean logVectorSizesToConsole) {
+ try {
+ if (SIMDDetection.getJavaVersion() != 17 && SIMDDetection.getJavaVersion() != 18) {
+ return false;
+ } else {
+ SIMDDetection.testRun = true;
+
+ VectorSpecies<Integer> ISPEC = IntVector.SPECIES_PREFERRED;
+ VectorSpecies<Float> FSPEC = FloatVector.SPECIES_PREFERRED;
+
+ if (logVectorSizesToConsole) {
+ logger.info("Max SIMD vector size on this system is " + ISPEC.vectorBitSize() + " bits (int)");
+ logger.info("Max SIMD vector size on this system is " + FSPEC.vectorBitSize() + " bits (float)");
+ }
+
+ if (ISPEC.elementSize() < 2 || FSPEC.elementSize() < 2) {
+ SIMDDetection.unsupportingLaneSize = true;
+ return false;
+ }
+
+ return true;
+ }
+ } catch (NoClassDefFoundError | Exception ignored) {} // Basically, we don't do anything. This lets us detect if it's not functional and disable it.
+ return false;
+ }
+
+}
diff --git a/src/main/java/gg/pufferfish/pufferfish/simd/SIMDDetection.java b/src/main/java/gg/pufferfish/pufferfish/simd/SIMDDetection.java
new file mode 100644
index 0000000000000000000000000000000000000000..ad304cfa228060adc12902da4604ccef31f27445
--- /dev/null
+++ b/src/main/java/gg/pufferfish/pufferfish/simd/SIMDDetection.java
@@ -0,0 +1,37 @@
+// Gale - Pufferfish - SIMD support
+
+package gg.pufferfish.pufferfish.simd;
+
+import org.slf4j.Logger;
+
+@Deprecated
+public class SIMDDetection {
+
+ public static boolean isEnabled = false;
+ public static boolean testRun = false;
+ public static boolean unsupportingLaneSize = false;
+
+ @Deprecated
+ public static boolean canEnable(Logger logger, boolean logVectorSizesToConsole) {
+ try {
+ return SIMDChecker.canEnable(logger, logVectorSizesToConsole);
+ } catch (NoClassDefFoundError | Exception ignored) {
+ return false;
+ }
+ }
+
+ @Deprecated
+ public static int getJavaVersion() {
+ // https://stackoverflow.com/a/2591122
+ String version = System.getProperty("java.version");
+ if(version.startsWith("1.")) {
+ version = version.substring(2, 3);
+ } else {
+ int dot = version.indexOf(".");
+ if(dot != -1) { version = version.substring(0, dot); }
+ }
+ version = version.split("-")[0]; // Azul is stupid
+ return Integer.parseInt(version);
+ }
+
+}