stage2 更多的工具库 stage1 修复问题

This commit is contained in:
zzy committed 2026-09-18 10:10:53 +08:00
1 parent 16a1f030d4
commit b3e38ba7e4
28 files changed
+2274 -523

No files matched your search

+8 -5
View File
@@ -1,4 +1,5 @@
// Expect retcode = 0
// 哈希工具:FNV-1a 字节哈希 + 整型混合;并给出可直接交给 map 的回调
// (map 的 hash/eq 回调接收指向键字节的指针,这里按 usize / ByteView 解释)。
// 仅提供确定性哈希与等值,不承诺密码学性质。
@@ -7,10 +8,11 @@ type .{ .ByteView } = @import("byte_view.spl");
// FNV-1a 64 位,逐字节;空输入返回 offset basis。
fn hash_bytes(data: *u8, len: usize): usize {
var h: usize = 14695981039346656037;
var h: usize = 14695981039346656037; // FNV-1a 64 位 offset basis
var prime: usize = 1099511628211; // FNV 质数
var i: usize = 0;
while i < len {
h = (h ^ (data[i] as usize)) * 1099511628211;
h = (h ^ (data[i] as usize)) * prime;
i = i + 1;
}
ret h;
@@ -22,12 +24,13 @@ fn hash_byte_view(view: ByteView): usize {
// 整型混合(splitmix 风格的移位-乘法),保证非零与良好扩散。
fn hash_usize_value(value: usize): usize {
var prime: usize = 1099511628211; // splitmix 风格的混合乘子
var h = value;
h = (h ^ (h >> 30)) * 1099511628211;
h = (h ^ (h >> 30)) * prime;
h = h ^ (h >> 27);
h = h * 1099511628211;
h = h * prime;
h = h ^ (h >> 31);
if h == 0 { ret 1099511628211; } // 0 映射到非零
if h == 0 { ret prime; } // 0 映射到非零
ret h;
}