1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- #![no_std]
- pub mod bloom_filter {
- use konst::primitive::{parse_usize, parse_u32};
- use konst::unwrap_ctx;
- use konst::option::unwrap_or;
- pub const ADDRESS_BITS: usize = unwrap_ctx!(parse_usize(unwrap_or!(option_env!("BLOOMFILTER_ADDRESS_BITS"), "30"))); // default = 0x1E = 30
- pub const BITS: usize = 1 << ADDRESS_BITS; // default = 0x40_000_000 = 128MB
- pub const ADDRESS_MASK: usize = BITS - 1;
- pub const WORD_BITS: usize = 0x8; // 8
- pub type WordType = u8;
- pub const ADDRESS_BITS_WORD: usize = 0x3; // log_2(0x8)
- pub const ADDRESS_MASK_WORD: usize = WORD_BITS - 1; // 0x7
- pub const ADDRESS_BITS_CHUNK: usize = unwrap_ctx!(parse_usize(unwrap_or!(option_env!("BLOOMFILTER_ADDRESS_BITS_CHUNK"), "18"))); // default = 0x12 = 18 = log_2(32KB)
- pub const CHUNK_BITS: usize = 1 << ADDRESS_BITS_CHUNK; // default = 0x40000 = 32KB (per-cpu map value size limit)
- pub const CHUNK_BYTES: usize = CHUNK_BITS >> 0x3;
- pub const CHUNK_SIZE: usize = CHUNK_BITS >> ADDRESS_BITS_WORD; // WORD_SIZE * CHUNK_SIZE = 32Kb
- pub type ChunkType = [WordType; CHUNK_SIZE];
- pub const ADDRESS_MASK_CHUNK: usize = CHUNK_BITS - 1;
- pub const BUFFER_SIZE: usize = BITS >> ADDRESS_BITS_WORD; // 0x20_0000
- pub const MAP_SIZE: usize = BITS >> ADDRESS_BITS_CHUNK;
- pub const HASH_COUNT: u32 = unwrap_ctx!(parse_u32(unwrap_or!(option_env!("BLOOMFILTER_HASH_COUNT"), "10")));
- // Hashing based on jhash.h from kernel // TODO improve reference
- const HASH_INITVAL: u32 = 0xdeadbeef;
- #[inline(always)]
- pub fn hash(key: u32, initial_value: u32) -> u32 {
- let mut b = HASH_INITVAL.wrapping_add(initial_value);
- let mut c = b;
- let mut a = key.wrapping_add(b);
- c ^= b;
- c = c.wrapping_sub(b.rotate_left(14));
- a ^= c;
- a = a.wrapping_sub(c.rotate_left(11));
- b ^= a;
- b = b.wrapping_sub(a.rotate_left(25));
- c ^= b;
- c = c.wrapping_sub(b.rotate_left(16));
- a ^= c;
- a = a.wrapping_sub(c.rotate_left(4));
- b ^= a;
- b = b.wrapping_sub(a.rotate_left(14));
- c ^= b;
- c = c.wrapping_sub(b.rotate_left(24));
- return c & ADDRESS_MASK as u32;
- }
- }
|