Document known issue with Fnv1a algorithms in javadoc (#257)

Co-authored-by: Wilhelm Schuster <[email protected]>
This commit is contained in:
WNP78andWilhelm Schuster authored and GitHub committed 2026-01-13 11:29:25 +01:00
1 parent e4bf6c5982
commit fc95d00884
2 files changed
+33 -19

No files matched your search

@@ -3,16 +3,21 @@ package com.gtnewhorizon.gtnhlib.hash;
/**
* The 32-bit version of <a href="http://www.isthe.com/chongo/tech/comp/fnv/index.html">Fowler/Noll/Vo 1-alternate</a>
* hash. To use this class, you need to keep track of a single {@code int} worth of state yourself, for example:
*
*
* <pre>
* int hash = Fnv1a32.initialState();
* hash = Fnv1a32.hashStep(hash, value1);
* hash = Fnv1a32.hashStep(hash, value2);
* hash = Fnv1a32.hashStep(hash, value3);
* </pre>
*
*
* The final hash is just the state after performing all the hashing steps, there is no separate finalization step
* needed.
* <p>
* The values produced by this class will not be the same as a true Fnv1a 32-bit implementation, because of a bug where
* Java's signed byte type causes any octet with the highest (sign) bit set to flip the upper 24 bytes of the state in
* the XOR step. As it's just flipping all the bits, it shouldn't affect dispersion that much in theory, so it is left
* in to not change the behaviour of existing worldgen code.
*/
@SuppressWarnings("unused") // Public API
public final class Fnv1a32 {
@@ -31,19 +36,21 @@ public final class Fnv1a32 {
/**
* Hashes a single byte into the given state, and returns the updated state.
*
*
* @param prevState The state to update
* @param value The value to hash into the new state
* @return The new hash state, computed by hashing the value into the previous state
*/
public static int hashStep(final int prevState, final byte value) {
// bug: see class javadoc, if this is to be fixed (ie on a major/breaking version) then:
// return (prevState ^ (value & 0xFF)) * PRIME32;
return (prevState ^ value) * PRIME32;
}
/**
* Hashes a single value into the given state byte by byte, and returns the updated state. Booleans are converted to
* a 0/1 byte first.
*
*
* @param prevState The state to update
* @param value The value to hash into the new state
* @return The new hash state, computed by hashing the value into the previous state
@@ -55,7 +62,7 @@ public final class Fnv1a32 {
/**
* Hashes a single value into the given state byte by byte, and returns the updated state. Chars are hashed like
* their underlying short representation.
*
*
* @param prevState The state to update
* @param value The value to hash into the new state
* @return The new hash state, computed by hashing the value into the previous state
@@ -67,7 +74,7 @@ public final class Fnv1a32 {
/**
* Hashes a single value into the given state byte by byte, and returns the updated state. Integers are hashed in
* order from most to least significant byte.
*
*
* @param prevState The state to update
* @param value The value to hash into the new state
* @return The new hash state, computed by hashing the value into the previous state
@@ -81,7 +88,7 @@ public final class Fnv1a32 {
/**
* Hashes a single value into the given state byte by byte, and returns the updated state. Integers are hashed in
* order from most to least significant byte.
*
*
* @param prevState The state to update
* @param value The value to hash into the new state
* @return The new hash state, computed by hashing the value into the previous state
@@ -97,7 +104,7 @@ public final class Fnv1a32 {
/**
* Hashes a single value into the given state byte by byte, and returns the updated state. Integers are hashed in
* order from most to least significant byte.
*
*
* @param prevState The state to update
* @param value The value to hash into the new state
* @return The new hash state, computed by hashing the value into the previous state
@@ -117,7 +124,7 @@ public final class Fnv1a32 {
/**
* Hashes a single value into the given state byte by byte, and returns the updated state. Floats are hashed in
* order from most to least significant byte of their integer representation.
*
*
* @param prevState The state to update
* @param value The value to hash into the new state
* @return The new hash state, computed by hashing the value into the previous state
@@ -129,7 +136,7 @@ public final class Fnv1a32 {
/**
* Hashes a single value into the given state byte by byte, and returns the updated state. Floats are hashed in
* order from most to least significant byte of their integer representation.
*
*
* @param prevState The state to update
* @param value The value to hash into the new state
* @return The new hash state, computed by hashing the value into the previous state
@@ -140,7 +147,7 @@ public final class Fnv1a32 {
/**
* Hashes an array of values by hashing each individual element.
*
*
* @param prevState The state to update
* @param array The array to hash
* @return The new hash state, computed by hashing the values into the previous state
@@ -154,7 +161,7 @@ public final class Fnv1a32 {
/**
* Hashes an array of values by hashing each individual element.
*
*
* @param prevState The state to update
* @param array The array to hash
* @return The new hash state, computed by hashing the values into the previous state
@@ -168,7 +175,7 @@ public final class Fnv1a32 {
/**
* Hashes an array of values by hashing each individual element.
*
*
* @param prevState The state to update
* @param array The array to hash
* @return The new hash state, computed by hashing the values into the previous state
@@ -182,7 +189,7 @@ public final class Fnv1a32 {
/**
* Hashes an array of values by hashing each individual element.
*
*
* @param prevState The state to update
* @param array The array to hash
* @return The new hash state, computed by hashing the values into the previous state
@@ -196,7 +203,7 @@ public final class Fnv1a32 {
/**
* Hashes an array of values by hashing each individual element.
*
*
* @param prevState The state to update
* @param array The array to hash
* @return The new hash state, computed by hashing the values into the previous state
@@ -210,7 +217,7 @@ public final class Fnv1a32 {
/**
* Hashes an array of values by hashing each individual element.
*
*
* @param prevState The state to update
* @param array The array to hash
* @return The new hash state, computed by hashing the values into the previous state
@@ -224,7 +231,7 @@ public final class Fnv1a32 {
/**
* Hashes an array of values by hashing each individual element.
*
*
* @param prevState The state to update
* @param array The array to hash
* @return The new hash state, computed by hashing the values into the previous state
@@ -238,7 +245,7 @@ public final class Fnv1a32 {
/**
* Hashes an array of values by hashing each individual element.
*
*
* @param prevState The state to update
* @param array The array to hash
* @return The new hash state, computed by hashing the values into the previous state
@@ -252,7 +259,7 @@ public final class Fnv1a32 {
/**
* Hashes any {@link CharSequence} (including {@link String}) by hashing each individual code unit in it.
*
*
* @param prevState The state to update
* @param array The array to hash
* @return The new hash state, computed by hashing the values into the previous state
@@ -13,6 +13,11 @@ package com.gtnewhorizon.gtnhlib.hash;
*
* The final hash is just the state after performing all the hashing steps, there is no separate finalization step
* needed.
*
* The values produced by this class will not be the same as a true Fnv1a 64-bit implementation, because of a bug where
* Java's signed byte type causes any octet with the highest (sign) bit set to flip the upper 56 bytes of the state in
* the XOR step. As it's just flipping all the bits, it shouldn't affect dispersion that much in theory, so it is left
* in to not change the behaviour of existing worldgen code.
*/
@SuppressWarnings("unused") // Public API
public final class Fnv1a64 {
@@ -37,6 +42,8 @@ public final class Fnv1a64 {
* @return The new hash state, computed by hashing the value into the previous state
*/
public static long hashStep(final long prevState, final byte value) {
// bug: see class javadoc, if this is to be fixed (ie on a major/breaking version) then:
// return (prevState ^ (value & 0xFFL)) * PRIME64;
return (prevState ^ value) * PRIME64;
}