From 1c7095cb7dfcba3537edf3624a61046c9b772b1f Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 10 May 2023 20:43:54 -0700 Subject: add std.hash.uint32 This is handy if you have a u32 and want a u32 and don't want to take a detour through many layers of abstraction elsewhere in the std.hash namespace. Copied from https://nullprogram.com/blog/2018/07/31/ --- lib/std/hash.zig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'lib/std/hash.zig') diff --git a/lib/std/hash.zig b/lib/std/hash.zig index 5c85b38d55..eca7a70159 100644 --- a/lib/std/hash.zig +++ b/lib/std/hash.zig @@ -36,6 +36,20 @@ const xxhash = @import("hash/xxhash.zig"); pub const XxHash64 = xxhash.XxHash64; pub const XxHash32 = xxhash.XxHash32; +/// This is handy if you have a u32 and want a u32 and don't want to take a +/// detour through many layers of abstraction elsewhere in the std.hash +/// namespace. +/// Copied from https://nullprogram.com/blog/2018/07/31/ +pub fn uint32(input: u32) u32 { + var x: u32 = input; + x ^= x >> 16; + x *%= 0x7feb352d; + x ^= x >> 15; + x *%= 0x846ca68b; + x ^= x >> 16; + return x; +} + test { _ = adler; _ = auto_hash; -- cgit v1.2.3