Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/cranelift/codegen/shared/src/constant_hash.rs
1692 views
1
//! This module provides a primitive hash function.
2
3
/// A primitive hash function for matching opcodes.
4
pub fn simple_hash(s: &str) -> usize {
5
let mut h: u32 = 5381;
6
for c in s.chars() {
7
h = (h ^ c as u32).wrapping_add(h.rotate_right(6));
8
}
9
h as usize
10
}
11
12
#[cfg(test)]
13
mod tests {
14
use super::simple_hash;
15
16
#[test]
17
fn basic() {
18
assert_eq!(simple_hash("Hello"), 0x2fa70c01);
19
assert_eq!(simple_hash("world"), 0x5b0c31d5);
20
}
21
}
22
23