| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System.Runtime.CompilerServices; |
| | 6 | |
|
| | 7 | | namespace GDX.Mathematics |
| | 8 | | { |
| | 9 | | public static class FibonacciHash |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Takes a 32-bit length equal to a power of two, |
| | 13 | | /// and returns how many spaces another 32-bit int would need to shift in order to be a valid index within a |
| | 14 | | /// that length. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="pow2Length">A 32-bit int equal to a power of two.</param> |
| | 17 | | /// <returns> |
| | 18 | | /// How many spaces a 32-bit int would need to shift in order to be a valid index within |
| | 19 | | /// <paramref name="pow2Length" />. |
| | 20 | | /// </returns> |
| | 21 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 22 | | public static byte GetRightShiftFromPow2Length(int pow2Length) |
| 0 | 23 | | { |
| | 24 | | LongDoubleConversionUnion u; |
| 0 | 25 | | u.DoubleValue = 0.0; |
| 0 | 26 | | u.LongValue = 0x4330000000000000L + pow2Length; |
| 0 | 27 | | u.DoubleValue -= 4503599627370496.0; |
| 0 | 28 | | int index = (int)(u.LongValue >> 52) - 0x3FF; |
| | 29 | |
|
| 0 | 30 | | return (byte)(32 - index); |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Takes the <paramref name="hash" /> and multiplies it by 2^32 divided by the golden ratio, |
| | 35 | | /// then right shifts it by <paramref name="shift" /> to fit within a given power-of-two size. |
| | 36 | | /// </summary> |
| | 37 | | /// <param name="hash">The key to find an index for.</param> |
| | 38 | | /// <param name="shift">How far to right shift in order to fit within a given power-of-two size.</param> |
| | 39 | | /// <returns>The index to store the <paramref name="hash" />.</returns> |
| | 40 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 41 | | public static int GetIndexFromHash(int hash, byte shift) |
| 0 | 42 | | { |
| 0 | 43 | | int fibonacci = unchecked((int)(unchecked((uint)hash) * 2654435769)); |
| 0 | 44 | | return fibonacci >> shift; |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Takes the <paramref name="hash" /> and finds an index within the provided <paramref name="pow2Length" /> |
| | 49 | | /// Fibonacci hashing. |
| | 50 | | /// </summary> |
| | 51 | | /// <param name="hash">The hash to find an index for.</param> |
| | 52 | | /// <param name="pow2Length">The power-of-two array length to find an index within.</param> |
| | 53 | | /// <returns></returns> |
| | 54 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 55 | | public static int GetIndexFromHash(int hash, int pow2Length) |
| 0 | 56 | | { |
| | 57 | | LongDoubleConversionUnion u; |
| 0 | 58 | | u.DoubleValue = 0.0; |
| 0 | 59 | | u.LongValue = 0x4330000000000000L + pow2Length; |
| 0 | 60 | | u.DoubleValue -= 4503599627370496.0; |
| 0 | 61 | | int index = (int)(u.LongValue >> 52) - 0x3FF; |
| | 62 | |
|
| 0 | 63 | | int shift = 32 - index; |
| | 64 | |
|
| 0 | 65 | | int fibonacci = unchecked((int)(unchecked((uint)hash) * 2654435769)); |
| 0 | 66 | | return fibonacci >> shift; |
| 0 | 67 | | } |
| | 68 | | } |
| | 69 | | } |