//! Exact canonical decimal encoding, caller-owned stack storage, no heap allocation. pub fn decimal_into(row:[u64;3],output:&mut [u8;58])->&[u8]{ const BASE:u128=10_000_000_000_000_000_000; let mut value=row;let mut chunks=[0u64;4];let mut count=0; while value.iter().any(|&limb|limb!=0){ let mut remainder=0u128; for limb in value.iter_mut().rev(){let dividend=(remainder<<64)|u128::from(*limb);*limb=(dividend/BASE)as u64;remainder=dividend%BASE;} chunks[count]=remainder as u64;count+=1; } if count==0{output[0]=b'0';return &output[..1];} let mut cursor=0; for index in (0..count).rev(){ let mut number=chunks[index];let mut digits=[b'0';19];let mut begin=19; while number!=0{begin-=1;digits[begin]=b'0'+(number%10)as u8;number/=10;} if index!=count-1{begin=0;}else if begin==19{begin=18;} let length=19-begin;output[cursor..cursor+length].copy_from_slice(&digits[begin..]);cursor+=length; } &output[..cursor] } #[cfg(test)]mod tests{ use super::*; #[test]fn exact_actual_legacy_decimal_bytes(){ let mut seed=0x61003u64;let mut rows=vec![[0;3],[u64::MAX;3]]; for bit in 0..192{let mut row=[0;3];row[bit/64]=1u64<<(bit%64);rows.push(row);} for _ in 0..10000{let mut row=[0;3];for limb in &mut row{seed^=seed<<13;seed^=seed>>7;seed^=seed<<17;*limb=seed;}rows.push(row);} for row in rows{let mut output=[0u8;58];assert_eq!(decimal_into(row,&mut output),crate::compact_bank::decimal(row).as_bytes());} } } /// Exact padding predicate; only widths accepted by native CUDA admission. pub fn fits_width(limbs:[u64;3],width:usize)->bool { if !(1..=144).contains(&width){return false;} let whole=width/64;let rest=width%64; for (index,value) in limbs.into_iter().enumerate(){ let mask=if index>7;seed^=seed<<17;seed}); let old=!(width..192).any(|bit|row[bit/64]&(1u64<<(bit%64))!=0); assert_eq!(fits_width(row,width),old); }} } }