use std::mem;
#[repr(C, align(64))]
#[derive(Clone, Debug)]
pub struct Tile {
pub origin: Origin,
pub input: u64,
pub output: u64,
pub confidence: f32,
pub safety: u32,
pub bytecode_ptr: u64,
pub trace: u64,
pub tensor_payload: [f32; 16],
pub provenance_head: u32,
pub self_play_gen: u16,
pub hydraulic_flux: f32,
pub constraints: ConstraintBlock,
}
impl Tile {
pub fn new(id: u64) -> Self {
Self {
origin: Origin::new(id),
input: 0,
output: 0,
confidence: 0.5,
safety: 1,
bytecode_ptr: 0,
trace: 0,
tensor_payload: [0.0; 16],
provenance_head: 0,
self_play_gen: 0,
hydraulic_flux: 0.0,
constraints: ConstraintBlock::new(),
}
}
pub fn vector_2d(&self) -> [f32; 2] {
[self.tensor_payload[0], self.tensor_payload[1]]
}
pub fn set_vector_2d(&mut self, vec: [f32; 2]) {
self.tensor_payload[0] = vec[0];
self.tensor_payload[1] = vec[1];
}
pub fn reset(&mut self) {
self.input = 0;
self.output = 0;
self.confidence = 0.5;
self.tensor_payload = [0.0; 16];
self.provenance_head = 0;
self.hydraulic_flux = 0.0;
self.constraints = ConstraintBlock::new();
}
}
#[repr(C, align(64))]
#[derive(Clone, Copy, Debug)]
pub struct Origin {
pub id: u64,
pub reference_frame: [[f32; 3]; 3],
pub rate_of_change: [f32; 3],
}
impl Origin {
pub fn new(id: u64) -> Self {
Self {
id,
reference_frame: [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
rate_of_change: [0.0; 3],
}
}
pub fn reset(&mut self) {
self.reference_frame = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
self.rate_of_change = [0.0; 3];
}
}
#[repr(C, align(64))]
#[derive(Clone, Copy, Debug)]
pub struct ConstraintBlock {
pub snap_target: [f32; 3],
pub holonomy_matrix: [[f32; 3]; 3],
pub holonomy_norm: f32,
pub ricci_curvature: [[f32; 4]; 4],
pub ricci_scalar: f32,
pub rigid_cluster_id: u64,
pub percolation_p: f32,
pub gluing_map: [f32; 2],
pub gluing_status: u32,
pub lvq_codebook_idx: u32,
pub omega_density: f32,
pub constraint_tolerance: f32,
pub persistence_hash: u64,
}
impl Default for ConstraintBlock {
fn default() -> Self {
Self::new()
}
}
impl ConstraintBlock {
pub fn new() -> Self {
Self {
snap_target: [0.0; 3],
holonomy_matrix: [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
holonomy_norm: 0.0,
ricci_curvature: [[0.0; 4]; 4],
ricci_scalar: 0.0,
rigid_cluster_id: 0,
percolation_p: 0.6602741, gluing_map: [0.0; 2],
gluing_status: 0,
lvq_codebook_idx: 0,
omega_density: 0.0,
constraint_tolerance: 0.05,
persistence_hash: 0,
}
}
pub fn reset(&mut self) {
*self = Self::new();
}
pub fn compute_holonomy_norm(&mut self) {
let mut sum = 0.0f32;
for i in 0..3 {
for j in 0..3 {
let val = self.holonomy_matrix[i][j] - if i == j { 1.0 } else { 0.0 };
sum += val * val;
}
}
self.holonomy_norm = sum.sqrt() / (2.0 * self.percolation_p.sqrt());
}
}
const _: () = assert!(mem::size_of::<Tile>() == 384, "Tile must be 384 bytes");
const _: () = assert!(mem::size_of::<Origin>() == 64, "Origin must be 64 bytes");
const _: () = assert!(
mem::size_of::<ConstraintBlock>() == 192,
"ConstraintBlock must be 192 bytes"
);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tile_size() {
assert_eq!(mem::size_of::<Tile>(), 384);
}
#[test]
fn test_origin_size() {
assert_eq!(mem::size_of::<Origin>(), 64);
}
#[test]
fn test_constraint_block_size() {
assert_eq!(mem::size_of::<ConstraintBlock>(), 192);
}
#[test]
fn test_tile_creation() {
let tile = Tile::new(42);
assert_eq!(tile.origin.id, 42);
assert_eq!(tile.confidence, 0.5);
assert_eq!(tile.safety, 1);
}
#[test]
fn test_vector_2d() {
let mut tile = Tile::new(0);
tile.set_vector_2d([0.6, 0.8]);
let vec = tile.vector_2d();
assert_eq!(vec[0], 0.6);
assert_eq!(vec[1], 0.8);
}
#[test]
fn test_holonomy_norm() {
let mut cb = ConstraintBlock::new();
cb.holonomy_matrix = [[1.0, 0.1, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
cb.compute_holonomy_norm();
assert!(cb.holonomy_norm > 0.0);
}
}