From 3cfd74da9e36e9c5f2c789c33dff9fd5a0e99000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Fri, 16 Jun 2023 09:05:36 -0700
Subject: [PATCH] Fix IntegerUtil#getDivisorNumbers

Use unsigned mod operation for initialization of anc

Also includes
- https://github.com/PaperMC/DataConverter/commit/5a0cefb45e246ef36901fb62448571edd82e7296
- https://github.com/PaperMC/Starlight/commit/acc8ed9634bbe27ec68e8842e420948bfa9707e7
---
 patches/server/MC-Utils.patch                 | 22 +++++++++++++++---
 .../server/Rewrite-dataconverter-system.patch | 23 +++++++++++++++++--
 patches/server/Starlight.patch                | 22 +++++++++++++++---
 3 files changed, 59 insertions(+), 8 deletions(-)

diff --git a/patches/server/MC-Utils.patch b/patches/server/MC-Utils.patch
index 490be31b16..faaa2d8135 100644
--- a/patches/server/MC-Utils.patch
+++ b/patches/server/MC-Utils.patch
@@ -3214,7 +3214,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +    // copied from hacker's delight (signed division magic value)
 +    // http://www.hackersdelight.org/hdcodetxt/magic.c.txt
 +    public static long getDivisorNumbers(final int d) {
-+        final int ad = IntegerUtil.branchlessAbs(d);
++        final int ad = branchlessAbs(d);
 +
 +        if (ad < 2) {
 +            throw new IllegalArgumentException("|number| must be in [2, 2^31 -1], not: " + d);
@@ -3223,11 +3223,27 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +        final int two31 = 0x80000000;
 +        final long mask = 0xFFFFFFFFL; // mask for enforcing unsigned behaviour
 +
++        /*
++         Signed usage:
++         int number;
++         long magic = getDivisorNumbers(div);
++         long mul = magic >>> 32;
++         int sign = number >> 31;
++         int result = (int)(((long)number * mul) >>> magic) - sign;
++         */
++        /*
++         Unsigned usage:
++         int number;
++         long magic = getDivisorNumbers(div);
++         long mul = magic >>> 32;
++         int result = (int)(((long)number * mul) >>> magic);
++         */
++
 +        int p = 31;
 +
 +        // all these variables are UNSIGNED!
 +        int t = two31 + (d >>> 31);
-+        int anc = t - 1 - t%ad;
++        int anc = t - 1 - (int)((t & mask)%ad);
 +        int q1 = (int)((two31 & mask)/(anc & mask));
 +        int r1 = two31 - q1*anc;
 +        int q2 = (int)((two31 & mask)/(ad & mask));
@@ -3255,7 +3271,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +        if (d < 0) {
 +            magicNum = -magicNum;
 +        }
-+        int shift = p - 32;
++        int shift = p;
 +        return ((long)magicNum << 32) | shift;
 +    }
 +
diff --git a/patches/server/Rewrite-dataconverter-system.patch b/patches/server/Rewrite-dataconverter-system.patch
index 6b6fb23eaa..f18bd9e325 100644
--- a/patches/server/Rewrite-dataconverter-system.patch
+++ b/patches/server/Rewrite-dataconverter-system.patch
@@ -1055,6 +1055,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +    public static final int V1_20_PRE6            = 3460;
 +    public static final int V1_20_PRE7            = 3461;
 +    public static final int V1_20_RC1             = 3462;
++    public static final int V1_20                 = 3463;
++    public static final int V1_20_1_RC1           = 3464;
++    public static final int V1_20_1               = 3465;
 +
 +}
 diff --git a/src/main/java/ca/spottedleaf/dataconverter/minecraft/converters/advancements/ConverterAbstractAdvancementsRename.java b/src/main/java/ca/spottedleaf/dataconverter/minecraft/converters/advancements/ConverterAbstractAdvancementsRename.java
@@ -23373,7 +23376,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +    }
 +
 +    public static long getDivisorNumbers(final int d) {
-+        final int ad = Math.abs(d);
++        final int ad = branchlessAbs(d);
 +
 +        if (ad < 2) {
 +            throw new IllegalArgumentException("|number| must be in [2, 2^31 -1], not: " + d);
@@ -23382,6 +23385,22 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +        final int two31 = 0x80000000;
 +        final long mask = 0xFFFFFFFFL; // mask for enforcing unsigned behaviour
 +
++        /*
++         Signed usage:
++         int number;
++         long magic = getDivisorNumbers(div);
++         long mul = magic >>> 32;
++         int sign = number >> 31;
++         int result = (int)(((long)number * mul) >>> magic) - sign;
++         */
++        /*
++         Unsigned usage:
++         int number;
++         long magic = getDivisorNumbers(div);
++         long mul = magic >>> 32;
++         int result = (int)(((long)number * mul) >>> magic);
++         */
++
 +        int p = 31;
 +
 +        // all these variables are UNSIGNED!
@@ -23414,7 +23433,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +        if (d < 0) {
 +            magicNum = -magicNum;
 +        }
-+        int shift = p - 32;
++        int shift = p;
 +        return ((long)magicNum << 32) | shift;
 +    }
 +
diff --git a/patches/server/Starlight.patch b/patches/server/Starlight.patch
index dce678acba..a8767e9628 100644
--- a/patches/server/Starlight.patch
+++ b/patches/server/Starlight.patch
@@ -3951,7 +3951,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +    // copied from hacker's delight (signed division magic value)
 +    // http://www.hackersdelight.org/hdcodetxt/magic.c.txt
 +    public static long getDivisorNumbers(final int d) {
-+        final int ad = IntegerUtil.branchlessAbs(d);
++        final int ad = branchlessAbs(d);
 +
 +        if (ad < 2) {
 +            throw new IllegalArgumentException("|number| must be in [2, 2^31 -1], not: " + d);
@@ -3960,11 +3960,27 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +        final int two31 = 0x80000000;
 +        final long mask = 0xFFFFFFFFL; // mask for enforcing unsigned behaviour
 +
++        /*
++         Signed usage:
++         int number;
++         long magic = getDivisorNumbers(div);
++         long mul = magic >>> 32;
++         int sign = number >> 31;
++         int result = (int)(((long)number * mul) >>> magic) - sign;
++         */
++        /*
++         Unsigned usage:
++         int number;
++         long magic = getDivisorNumbers(div);
++         long mul = magic >>> 32;
++         int result = (int)(((long)number * mul) >>> magic);
++         */
++
 +        int p = 31;
 +
 +        // all these variables are UNSIGNED!
 +        int t = two31 + (d >>> 31);
-+        int anc = t - 1 - t%ad;
++        int anc = t - 1 - (int)((t & mask)%ad);
 +        int q1 = (int)((two31 & mask)/(anc & mask));
 +        int r1 = two31 - q1*anc;
 +        int q2 = (int)((two31 & mask)/(ad & mask));
@@ -3992,7 +4008,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +        if (d < 0) {
 +            magicNum = -magicNum;
 +        }
-+        int shift = p - 32;
++        int shift = p;
 +        return ((long)magicNum << 32) | shift;
 +    }
 +