aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / allen-reflex / src / hv.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Binary hypervectors (default 8192 bits = 128 u64 words). |
| 17 | //! |
| 18 | //! - Item vectors are generated on the fly from a seeded hash of each symbol (splitmix64); |
| 19 | //! no random table is stored. |
| 20 | //! - Binding is XOR; order inside a feature is kept by rotating the i-th symbol's vector |
| 21 | //! by (len - 1 - i) words (a fixed permutation). |
| 22 | //! - Bundling is a weighted majority: an integer counter per bit gets +w for a 1 and -w for |
| 23 | //! a 0; the sign gives the bit, and a fixed seeded tie-break vector settles zeros. |
| 24 | //! - Similarity is 1 - 2 * Hamming / D, in [-1, 1]. |
| 25 | |
| 26 | use crate::features::Feat; |
| 27 | |
| 28 | /// Global seed of all item vectors (frozen with the artifact). |
| 29 | pub const SEED: u64 = 0x414c_4c45_4e52_4546; // "ALLENREF" |
| 30 | |
| 31 | /// splitmix64 step. |
| 32 | #[inline] |
| 33 | pub fn splitmix(state: &mut u64) -> u64 { |
| 34 | *state = state.wrapping_add(0x9E37_79B9_7F4A_7C15); |
| 35 | let mut z = *state; |
| 36 | z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); |
| 37 | z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); |
| 38 | z ^ (z >> 31) |
| 39 | } |
| 40 | |
| 41 | /// Fill `out` with the item vector of (family, value). |
| 42 | pub fn item(fam: u64, val: u64, out: &mut [u64]) { |
| 43 | let mut s = SEED ^ fam.wrapping_mul(0xD6E8_FEB8_6659_FD93) ^ val; |
| 44 | // decorrelate nearby seeds |
| 45 | splitmix(&mut s); |
| 46 | for o in out.iter_mut() { |
| 47 | *o = splitmix(&mut s); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /// XOR-accumulate the bound vector of `f` into `acc`; `tmp` is scratch of the same length. |
| 52 | pub fn bind_into(f: &Feat, acc: &mut [u64], tmp: &mut [u64]) { |
| 53 | let w = acc.len(); |
| 54 | for a in acc.iter_mut() { |
| 55 | *a = 0; |
| 56 | } |
| 57 | let len = f.len as usize; |
| 58 | for j in 0..len { |
| 59 | let (fam, val) = f.syms[j].family(); |
| 60 | item(fam, val, tmp); |
| 61 | let rot = (len - 1 - j) % w; |
| 62 | for x in 0..w { |
| 63 | acc[(x + rot) % w] ^= tmp[x]; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// Encode a feature list into `out` (binary). `cnt` (64 * words), `acc` and `tmp` are scratch. |
| 69 | pub fn encode(feats: &[Feat], out: &mut [u64], cnt: &mut [i32], acc: &mut [u64], tmp: &mut [u64]) { |
| 70 | let w = out.len(); |
| 71 | for c in cnt.iter_mut() { |
| 72 | *c = 0; |
| 73 | } |
| 74 | for f in feats { |
| 75 | bind_into(f, acc, tmp); |
| 76 | let wt = f.w as i32; |
| 77 | for x in 0..w { |
| 78 | let v = acc[x]; |
| 79 | let base = x * 64; |
| 80 | for b in 0..64 { |
| 81 | // +wt for a 1 bit, -wt for a 0 bit |
| 82 | let bit = ((v >> b) & 1) as i32; |
| 83 | cnt[base + b] += (2 * bit - 1) * wt; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | item(7, 0, tmp); // tie-break vector |
| 88 | for x in 0..w { |
| 89 | let mut o = 0u64; |
| 90 | for b in 0..64 { |
| 91 | let c = cnt[x * 64 + b]; |
| 92 | let bit = if c > 0 { |
| 93 | 1 |
| 94 | } else if c < 0 { |
| 95 | 0 |
| 96 | } else { |
| 97 | (tmp[x] >> b) & 1 |
| 98 | }; |
| 99 | o |= bit << b; |
| 100 | } |
| 101 | out[x] = o; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /// Hamming distance. |
| 106 | #[inline] |
| 107 | pub fn hamming(a: &[u64], b: &[u64]) -> u32 { |
| 108 | a.iter().zip(b).map(|(x, y)| (x ^ y).count_ones()).sum() |
| 109 | } |
| 110 | |
| 111 | /// Similarity in [-1, 1]. |
| 112 | #[inline] |
| 113 | pub fn sim(a: &[u64], b: &[u64]) -> f32 { |
| 114 | let d = (a.len() * 64) as f32; |
| 115 | 1.0 - 2.0 * hamming(a, b) as f32 / d |
| 116 | } |
| 117 | |
| 118 | /// Square root without libm (Newton iterations; enough for thresholds and normalisation). |
| 119 | pub fn sqrtf(x: f32) -> f32 { |
| 120 | if x <= 0.0 { |
| 121 | return 0.0; |
| 122 | } |
| 123 | let mut y = f32::from_bits((x.to_bits() >> 1) + 0x1fbd_1df5); |
| 124 | for _ in 0..4 { |
| 125 | y = 0.5 * (y + x / y); |
| 126 | } |
| 127 | y |
| 128 | } |