changeset 1436:cf0bae787044

add: squirrel hash
author sam <sam@basx.dev>
date Mon, 20 Jan 2025 23:12:30 +0700
parents 7de637b83272
children e54ef155de10
files semicongine/contrib/algorithms/noise.nim
diffstat 1 files changed, 15 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/semicongine/contrib/algorithms/noise.nim	Sun Jan 19 23:58:44 2025 +0700
+++ b/semicongine/contrib/algorithms/noise.nim	Mon Jan 20 23:12:30 2025 +0700
@@ -3,6 +3,21 @@
 
 import ../../core
 
+# nice hash function, from a GDC talk
+proc squirrel3(position: uint32, seed: uint32 = 0'u32): uint32 =
+  const
+    NOISE1 = 0xB5297A4D'u32
+    NOISE2 = 0x68E31DA4'u32
+    NOISE3 = 0x1B56C4E9'u32
+  result = position
+  result += seed
+  result *= NOISE1
+  result = result xor (result shr 8'u32)
+  result += NOISE2
+  result = result xor (result shl 8'u32)
+  result *= NOISE3
+  result = result xor (result shr 8'u32)
+
 proc randomGradient(pos: Vec2f, seed: uint64 = 0): Vec2f =
   let randomAngle: float32 =
     TAU * (float32(int(hash((pos.x, pos.y, seed)))) / float32(high(int)))