-
-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce basic support for Vector API (#2890)
* Introduce basic support for Vector API * add modules to javadoc too * add assumption comments
- Loading branch information
Showing
15 changed files
with
341 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
worldedit-core/src/main/java/com/fastasyncworldedit/core/internal/simd/SimdSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.fastasyncworldedit.core.internal.simd; | ||
|
||
import com.fastasyncworldedit.core.configuration.Settings; | ||
import com.fastasyncworldedit.core.extent.filter.block.DelegateFilter; | ||
import com.fastasyncworldedit.core.function.mask.SingleBlockStateMask; | ||
import com.fastasyncworldedit.core.queue.Filter; | ||
import com.sk89q.worldedit.function.mask.InverseSingleBlockStateMask; | ||
import com.sk89q.worldedit.function.mask.Mask; | ||
import com.sk89q.worldedit.function.pattern.Pattern; | ||
import com.sk89q.worldedit.internal.util.LogManagerCompat; | ||
import com.sk89q.worldedit.world.block.BaseBlock; | ||
import com.sk89q.worldedit.world.block.BlockStateHolder; | ||
import jdk.incubator.vector.ShortVector; | ||
import jdk.incubator.vector.VectorOperators; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
public class SimdSupport { | ||
|
||
private static final boolean VECTOR_API_PRESENT; | ||
|
||
static { | ||
boolean vectorApiPresent = false; | ||
try { | ||
Class.forName("jdk.incubator.vector.Vector"); | ||
vectorApiPresent = true; | ||
} catch (ClassNotFoundException ignored) { | ||
} | ||
VECTOR_API_PRESENT = vectorApiPresent; | ||
if (!VECTOR_API_PRESENT && Settings.settings().EXPERIMENTAL.USE_VECTOR_API) { | ||
LogManagerCompat.getLogger() | ||
.warn("FAWE use-vector-api is enabled but --add-modules=jdk.incubator.vector is not set."); | ||
} | ||
} | ||
|
||
public static boolean useVectorApi() { | ||
return VECTOR_API_PRESENT && Settings.settings().EXPERIMENTAL.USE_VECTOR_API; | ||
} | ||
|
||
public static @Nullable VectorizedMask vectorizedTargetMask(Mask mask) { | ||
if (!useVectorApi()) { | ||
return null; | ||
} | ||
return switch (mask) { | ||
case SingleBlockStateMask single -> vectorizedTargetMask(single.getBlockState().getOrdinalChar()); | ||
case InverseSingleBlockStateMask inverse -> vectorizedTargetMaskInverse(inverse.getBlockState().getOrdinalChar()); | ||
default -> null; | ||
}; | ||
} | ||
|
||
private static VectorizedMask vectorizedTargetMask(char ordinal) { | ||
return (set, get) -> get.compare(VectorOperators.EQ, (short) ordinal); | ||
} | ||
|
||
private static VectorizedMask vectorizedTargetMaskInverse(char ordinal) { | ||
return (set, get) -> get.compare(VectorOperators.NE, (short) ordinal); | ||
} | ||
|
||
public static @Nullable VectorizedFilter vectorizedPattern(Pattern pattern) { | ||
if (!useVectorApi()) { | ||
return null; | ||
} | ||
return switch (pattern) { | ||
case BaseBlock block -> { | ||
if (block.getNbtReference() == null) { | ||
yield new VectorizedPattern<>(block, block.getOrdinalChar()); | ||
} | ||
yield null; | ||
} | ||
case BlockStateHolder<?> blockStateHolder -> new VectorizedPattern<>( | ||
blockStateHolder, | ||
blockStateHolder.getOrdinalChar() | ||
); | ||
default -> null; | ||
}; | ||
} | ||
|
||
private static final class VectorizedPattern<T extends Filter> extends DelegateFilter<T> implements VectorizedFilter { | ||
|
||
private final char ordinal; | ||
|
||
public VectorizedPattern(final T parent, char ordinal) { | ||
super(parent); | ||
this.ordinal = ordinal; | ||
} | ||
|
||
@Override | ||
public ShortVector applyVector(final ShortVector get, final ShortVector set) { | ||
return ShortVector.broadcast(ShortVector.SPECIES_PREFERRED, ordinal); | ||
} | ||
|
||
@Override | ||
public Filter newInstance(final Filter other) { | ||
return new VectorizedPattern<>(other, ordinal); | ||
} | ||
|
||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...re/src/main/java/com/fastasyncworldedit/core/internal/simd/VectorizedCharFilterBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.fastasyncworldedit.core.internal.simd; | ||
|
||
import com.fastasyncworldedit.core.extent.filter.block.CharFilterBlock; | ||
import com.fastasyncworldedit.core.queue.Filter; | ||
import com.sk89q.worldedit.extent.Extent; | ||
import jdk.incubator.vector.ShortVector; | ||
import jdk.incubator.vector.VectorSpecies; | ||
|
||
public class VectorizedCharFilterBlock extends CharFilterBlock { | ||
|
||
public VectorizedCharFilterBlock(final Extent extent) { | ||
super(extent); | ||
} | ||
|
||
@Override | ||
public synchronized void filter(final Filter filter) { | ||
if (!(filter instanceof VectorizedFilter vecFilter)) { | ||
throw new IllegalStateException("Unexpected VectorizedCharFilterBlock " + filter); | ||
} | ||
final VectorSpecies<Short> species = ShortVector.SPECIES_PREFERRED; | ||
initSet(); // set array is null before | ||
char[] setArr = this.setArr; | ||
assert setArr != null; | ||
char[] getArr = this.getArr; | ||
// assume setArr.length == getArr.length == 4096 | ||
for (int i = 0; i < 4096; i += species.length()) { | ||
ShortVector set = ShortVector.fromCharArray(species, setArr, i); | ||
ShortVector get = ShortVector.fromCharArray(species, getArr, i); | ||
ShortVector res = vecFilter.applyVector(get, set); | ||
res.intoCharArray(setArr, i); | ||
} | ||
} | ||
} |
Oops, something went wrong.