tricu

A lazy functional language based on Tree Calculus
Log | Files | Refs | README | LICENSE

float-near-jam.tri (3838B)


      1 -- Benchmark-only reduced-width near alignment. Same positive input contract as A.
      2 !import native_math as N
      3 !import kernels as K
      4 !import quantize as Q
      5 !import fast as F
      6 !import aligned as A
      7 !export *
      8 -- Round-to-odd right shift: retain quotient, jam any nonzero remainder into bit 0.
      9 jam count b = let split = K.shiftL count b in let q = K.head split in
     10   K.choose q (K.choose (t (t t) (K.tail q)) q (K.tail split)) (N.odd q)
     11 -- Gap 2/3 is still exact; gaps 4..54 use a jammed right shift.
     12 align gap b = K.lazy (_ : F.shiftSelective (N.subtract 3 gap) b)
     13   (_ : jam (N.subtract gap 3) b) (N.lte gap 3)
     14 wide a = F.zeros2 (F.zeros1 a)
     15 nearAdd scale gap a b = F.selective (N.subtract scale 3) (N.add (wide a) (align gap b))
     16 nearSubtract scale gap a b = F.selective (N.subtract scale 3) (N.subtract (wide a) (align gap b))
     17 -- Exact fallback at gaps 0/1; preserve the previously tested far branch >=55.
     18 add scale gap a b = K.lazy (_ : A.add scale gap a b)
     19   (_ : K.lazy (_ : A.add scale gap a b) (_ : nearAdd scale gap a b) (N.lt gap 2)) (N.gte gap 55)
     20 subtract scale gap a b = K.lazy (_ : A.subtract scale gap a b)
     21   (_ : K.lazy (_ : A.subtract scale gap a b) (_ : nearSubtract scale gap a b) (N.lt gap 2)) (N.gte gap 55)
     22 addOne x = let scale = K.head x in let rest = K.tail x in let gap = K.head rest in let ab = K.tail rest in
     23   add scale gap (K.head ab) (K.tail ab)
     24 subtractOne x = let scale = K.head x in let rest = K.tail x in let gap = K.head rest in let ab = K.tail rest in
     25   subtract scale gap (K.head ab) (K.tail ab)
     26 adds xs = K.map addOne xs
     27 subtracts xs = K.map subtractOne xs
     28 controlAdds xs = A.adds xs
     29 controlSubtracts xs = A.subtracts xs
     30 jamOne x = jam (K.head x) (K.tail x)
     31 jams xs = K.map jamOne xs
     32 -- Negative rounding control: discard the sticky remainder.
     33 truncate gap b = K.head (K.shiftL (N.subtract gap 3) b)
     34 truncateOne x = let scale = K.head x in let rest = K.tail x in let gap = K.head rest in let ab = K.tail rest in
     35   F.selective (N.subtract scale 3) (N.add (wide (K.head ab)) (truncate gap (K.tail ab)))
     36 truncateAdds xs = K.map truncateOne xs
     37 spin x = spin x
     38 jamZeroDemand x = K.head (jam 0 (t (t t) (spin x)))
     39 farDemand x = add 2252 55 4503599627370496 (spin x)
     40 cancelDemand x = subtract (spin x) 0 4503599627370496 4503599627370496
     41 truncateSubtractOne x = let scale = K.head x in let rest = K.tail x in let gap = K.head rest in let ab = K.tail rest in
     42   F.selective (N.subtract scale 3) (N.subtract (wide (K.head ab)) (truncate gap (K.tail ab)))
     43 truncateSubtracts xs = K.map truncateSubtractOne xs
     44 -- Use exact arithmetic for gaps 0..7.
     45 -- Dispatch known far gaps directly to the aligned quantizer.
     46 hybridAdd scale gap a b = K.lazy (_ : A.fastFar scale a (_ : A.farAdd scale a))
     47   (_ : K.lazy
     48     (_ : F.selective (N.subtract scale gap) (N.add (F.shiftSelective gap a) b))
     49     (_ : nearAdd scale gap a b) (N.lt gap 8)) (N.gte gap 55)
     50 hybridSubtract scale gap a b = K.lazy (_ : A.fastFar scale a (_ : A.farSubtract scale a))
     51   (_ : K.lazy
     52     (_ : F.selective (N.subtract scale gap) (N.subtract (F.shiftSelective gap a) b))
     53     (_ : nearSubtract scale gap a b) (N.lt gap 8)) (N.gte gap 55)
     54 hybridAddOne x = let scale = K.head x in let rest = K.tail x in let gap = K.head rest in let ab = K.tail rest in
     55   hybridAdd scale gap (K.head ab) (K.tail ab)
     56 hybridSubtractOne x = let scale = K.head x in let rest = K.tail x in let gap = K.head rest in let ab = K.tail rest in
     57   hybridSubtract scale gap (K.head ab) (K.tail ab)
     58 hybridAdds xs = K.map hybridAddOne xs
     59 hybridSubtracts xs = K.map hybridSubtractOne xs
     60 hybridFarDemand x = hybridAdd 2252 55 4503599627370496 (spin x)
     61 hybridCancelDemand x = hybridSubtract (spin x) 0 4503599627370496 4503599627370496
     62 -- Known sticky/odd quotients leave the remaining discarded bits opaque.
     63 jamStickyDemand x = jam 2 (t (t t) (t (spin x) 1))
     64 jamOddDemand x = jam 1 (t (spin x) 1)