forked from hpfxd/PandaSpigot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0022-Branchless-NibbleArray.patch
69 lines (63 loc) · 2.26 KB
/
0022-Branchless-NibbleArray.patch
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
62
63
64
65
66
67
68
69
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: md_5 <[email protected]>
Date: Sun, 31 Oct 2021 10:28:14 -0400
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 9306f97af94b6559dd28a3f942696ac4d3f6b746..a69c0bc8479d69ce9eefab48f13699d0402aa5cc 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; // PandaSpigot
}
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);
- }
-
+ // PandaSpigot start
+ int shift = (i & 1) << 2;
+ this.a[k] = (byte) (this.a[k] & ~(15 << shift) | (j & 15) << shift);
+ // PandaSpigot 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 0000000000000000000000000000000000000000..448c29f17928bf25a4d44490684925d59fb46e27
--- /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));
+ }
+ }
+}