tricu

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

float-adders.tri (1585B)


      1 -- Full-adder source-form benchmark.
      2 !import native_math as N
      3 !import kernels as K
      4 !import multiply as M
      5 !export *
      6 triage = K.triage
      7 lazy = K.lazy
      8 choose = K.choose
      9 head = K.head
     10 tail = K.tail
     11 map = K.map
     12 -- Ordered mux: when abit is zero, inspect carry BEFORE bbit, as native does.
     13 ordered a b carry = triage
     14   (lazy (_ : N.successor b) (_ : b) carry)
     15   (_ : t)
     16   (abit ar : triage (lazy (_ : N.successor a) (_ : a) carry) (_ : t)
     17     (bbit br : let neg = K.not carry in
     18       t (choose (choose carry neg bbit) (choose neg carry bbit) abit)
     19         (ordered ar br (choose (K.either bbit carry) (K.both carry bbit) abit))) b) a
     20 -- Inline each mux expression at its use site.
     21 inline a b carry = triage
     22   (lazy (_ : N.successor b) (_ : b) carry)
     23   (_ : t)
     24   (abit ar : triage (lazy (_ : N.successor a) (_ : a) carry) (_ : t)
     25     (bbit br : t (choose (choose carry (K.not carry) bbit) (choose (K.not carry) carry bbit) abit)
     26       (inline ar br (choose (K.either bbit carry) (K.both carry bbit) abit))) b) a
     27 nativeAdd a b = N.add a b
     28 originalMuxAdd a b = M.addMux a b t
     29 orderedAdd a b = ordered a b t
     30 inlineAdd a b = inline a b t
     31 -- Retain native multiplication recurrence, replace only its adder.
     32 orderedMultiply a b = let rec
     33   walk remaining = triage t (_ : t)
     34     (bit rest : lazy (_ : ordered a (K.node t (walk rest)) t)
     35       (_ : K.node t (walk rest)) bit) remaining
     36   in walk b
     37 inlineMultiply a b = let rec
     38   walk remaining = triage t (_ : t)
     39     (bit rest : lazy (_ : inline a (K.node t (walk rest)) t)
     40       (_ : K.node t (walk rest)) bit) remaining
     41   in walk b
     42 spin x = spin x