spigot/CraftBukkit-Patches/0122-Branchless-NibbleArray.patch
2018-07-22 12:00:00 +10:00

72 lines
2.1 KiB
Diff

From 1625678e4d828420f63a945c0463a38aa20d9033 Mon Sep 17 00:00:00 2001
From: md_5 <git@md-5.net>
Date: Sat, 30 Jul 2016 11:29:14 +1000
Subject: [PATCH] Branchless NibbleArray
diff --git a/src/main/java/net/minecraft/server/NibbleArray.java b/src/main/java/net/minecraft/server/NibbleArray.java
index 1509c772e..79c5bc429 100644
--- a/src/main/java/net/minecraft/server/NibbleArray.java
+++ b/src/main/java/net/minecraft/server/NibbleArray.java
@@ -30,18 +30,16 @@ public class NibbleArray {
public int a(int i) {
int j = this.c(i);
- return this.b(i) ? this.a[j] & 15 : this.a[j] >> 4 & 15;
+ return this.a[j] >> ((i & 1) << 2) & 15; // Spigot
}
public void a(int i, int j) {
int k = this.c(i);
- if (this.b(i)) {
- this.a[k] = (byte) (this.a[k] & 240 | j & 15);
- } else {
- this.a[k] = (byte) (this.a[k] & 15 | (j & 15) << 4);
- }
-
+ // Spigot start
+ int shift = (i & 1) << 2;
+ this.a[k] = (byte) (this.a[k] & ~(15 << shift) | (j & 15) << shift);
+ // Spigot end
}
private boolean b(int i) {
diff --git a/src/test/java/org/bukkit/NibbleArrayTest.java b/src/test/java/org/bukkit/NibbleArrayTest.java
new file mode 100644
index 000000000..0131ff04a
--- /dev/null
+++ b/src/test/java/org/bukkit/NibbleArrayTest.java
@@ -0,0 +1,29 @@
+package org.bukkit;
+
+import java.util.Random;
+import net.minecraft.server.NibbleArray;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class NibbleArrayTest {
+
+ private static final int NIBBLE_SIZE = 4096;
+
+ @Test
+ public void testNibble() {
+ Random random = new Random();
+ byte[] classic = new byte[NIBBLE_SIZE];
+ NibbleArray nibble = new NibbleArray();
+
+ for (int i = 0; i < classic.length; i++) {
+ byte number = (byte) (random.nextInt() & 0xF);
+
+ classic[i] = number;
+ nibble.a(i, number);
+ }
+
+ for (int i = 0; i < classic.length; i++) {
+ Assert.assertEquals("Nibble array mismatch", classic[i], nibble.a(i));
+ }
+ }
+}
--
2.17.1