File size: 2,622 Bytes
b60a0ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//! 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<whole{u64::MAX}else if index==whole&&rest!=0{(1u64<<rest)-1}else{0};
  if value & !mask != 0{return false;}
 }
 true
}
#[cfg(test)] mod padding_mask_tests {
 use super::*;
 #[test] fn every_single_bit_matches_bitwise_padding_all_widths(){
  for width in 1..=144 {assert!(fits_width([0;3],width));for bit in 0..192{
   let mut row=[0;3];row[bit/64]=1u64<<(bit%64);
   assert_eq!(fits_width(row,width),bit<width,"width={width} bit={bit}");
  }}assert!(!fits_width([0;3],0));assert!(!fits_width([0;3],145));
 }
 #[test] fn mixed_rows_match_original_predicate(){
  let mut seed=17u64;for width in 1..=144{for _ in 0..64{
   let row=std::array::from_fn(|_|{seed^=seed<<13;seed^=seed>>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);
  }}
 }
}