@@ -35,7 +35,7 @@ module Circuit.Language.Expr
35
35
)
36
36
where
37
37
38
- import Data.Field.Galois (Prime , PrimeField (fromP ), GaloisField )
38
+ import Data.Field.Galois (GaloisField , Prime , PrimeField (fromP ))
39
39
import Data.Semiring (Ring (.. ), Semiring (.. ))
40
40
import Data.Sequence ((|>) )
41
41
import Data.Set qualified as Set
@@ -90,6 +90,7 @@ rawWire (VarBool i) = i
90
90
91
91
data UnOp f (ty :: Ty ) where
92
92
UNeg :: UnOp f 'TField
93
+ UNegs :: UnOp f ('TVec n 'TField)
93
94
UNot :: UnOp f 'TBool
94
95
UNots :: UnOp f ('TVec n 'TBool)
95
96
@@ -100,14 +101,19 @@ deriving instance Eq (UnOp f a)
100
101
instance Pretty (UnOp f a ) where
101
102
pretty op = case op of
102
103
UNeg -> text " neg"
104
+ UNegs -> text " negs"
103
105
UNot -> text " !"
104
106
UNots -> text " nots"
105
107
106
108
data BinOp f (a :: Ty ) where
107
109
BAdd :: BinOp f 'TField
110
+ BAdds :: BinOp f (TVec n 'TField)
108
111
BSub :: BinOp f 'TField
112
+ BSubs :: BinOp f (TVec n 'TField)
109
113
BMul :: BinOp f 'TField
114
+ BMuls :: BinOp f (TVec n 'TField)
110
115
BDiv :: BinOp f 'TField
116
+ BDivs :: BinOp f (TVec n 'TField)
111
117
BAnd :: BinOp f 'TBool
112
118
BAnds :: BinOp f (TVec n 'TBool)
113
119
BOr :: BinOp f 'TBool
@@ -122,9 +128,13 @@ deriving instance Eq (BinOp f a)
122
128
instance Pretty (BinOp f a ) where
123
129
pretty op = case op of
124
130
BAdd -> text " +"
131
+ BAdds -> text " .+."
125
132
BSub -> text " -"
133
+ BSubs -> text " .-."
126
134
BMul -> text " *"
135
+ BMuls -> text " .*."
127
136
BDiv -> text " /"
137
+ BDivs -> text " ./."
128
138
BAnd -> text " &&"
129
139
BAnds -> text " .&&."
130
140
BOr -> text " ||"
@@ -140,9 +150,13 @@ opPrecedence BXors = 5
140
150
opPrecedence BAnd = 5
141
151
opPrecedence BAnds = 5
142
152
opPrecedence BSub = 6
153
+ opPrecedence BSubs = 6
143
154
opPrecedence BAdd = 6
155
+ opPrecedence BAdds = 6
144
156
opPrecedence BMul = 7
157
+ opPrecedence BMuls = 7
145
158
opPrecedence BDiv = 8
159
+ opPrecedence BDivs = 8
146
160
147
161
type family NBits a :: Nat where
148
162
NBits (Prime p ) = (Log2 p ) + 1
@@ -253,22 +267,29 @@ evalExpr lookupVar vars expr = case expr of
253
267
case lookupVar i vars of
254
268
Just v -> v == 1
255
269
Nothing -> panic $ " TODO: incorrect bool var lookup: " <> Protolude. show i
256
- EUnOp _ UNeg e1 ->
257
- Protolude. negate $ evalExpr lookupVar vars e1
258
- EUnOp _ UNot e1 ->
259
- not $ evalExpr lookupVar vars e1
260
- EUnOp _ UNots e1 ->
261
- SV. map not $ evalExpr lookupVar vars e1
270
+ EUnOp _ op e1 ->
271
+ let e1' = evalExpr lookupVar vars e1
272
+ in apply e1'
273
+ where
274
+ apply = case op of
275
+ UNeg -> Protolude. negate
276
+ UNegs -> map Protolude. negate
277
+ UNot -> not
278
+ UNots -> map not
262
279
EBinOp _ op e1 e2 ->
263
280
let e1' = evalExpr lookupVar vars e1
264
281
e2' = evalExpr lookupVar vars e2
265
282
in apply e1' e2'
266
283
where
267
284
apply = case op of
268
285
BAdd -> (+)
286
+ BAdds -> SV. zipWith (+)
269
287
BSub -> (-)
288
+ BSubs -> SV. zipWith (-)
270
289
BMul -> (*)
290
+ BMuls -> SV. zipWith (*)
271
291
BDiv -> (/)
292
+ BDivs -> SV. zipWith (/)
272
293
BAnd -> (&&)
273
294
BAnds -> SV. zipWith (&&)
274
295
BOr -> (||)
@@ -420,7 +441,7 @@ bundle_ b =
420
441
class BoolToField b f where
421
442
boolToField :: b -> f
422
443
423
- instance GaloisField f => BoolToField Bool f where
444
+ instance ( GaloisField f ) => BoolToField Bool f where
424
445
boolToField b = fromInteger $ if b then 1 else 0
425
446
426
447
instance BoolToField (Val f 'TBool) (Val f 'TField) where
@@ -437,14 +458,15 @@ instance BoolToField (Expr i f ('TVec n 'TBool)) (Expr i f ('TVec n 'TField)) wh
437
458
438
459
-------------------------------------------------------------------------------
439
460
440
- data UBinOp =
441
- UBAdd |
442
- UBSub |
443
- UBMul |
444
- UBDiv |
445
- UBAnd |
446
- UBOr |
447
- UBXor deriving (Show , Eq , Generic )
461
+ data UBinOp
462
+ = UBAdd
463
+ | UBSub
464
+ | UBMul
465
+ | UBDiv
466
+ | UBAnd
467
+ | UBOr
468
+ | UBXor
469
+ deriving (Show , Eq , Generic )
448
470
449
471
instance Hashable UBinOp
450
472
@@ -506,15 +528,20 @@ instance (Hashable i, Hashable f) => Hashable (Node i f) where
506
528
507
529
untypeUnOp :: UnOp f a -> UUnOp
508
530
untypeUnOp UNeg = UUNeg
531
+ untypeUnOp UNegs = UUNeg
509
532
untypeUnOp UNot = UUNot
510
533
untypeUnOp UNots = UUNot
511
534
{-# INLINE untypeUnOp #-}
512
535
513
536
untypeBinOp :: BinOp f a -> UBinOp
514
537
untypeBinOp BAdd = UBAdd
538
+ untypeBinOp BAdds = UBAdd
515
539
untypeBinOp BSub = UBSub
540
+ untypeBinOp BSubs = UBSub
516
541
untypeBinOp BMul = UBMul
542
+ untypeBinOp BMuls = UBMul
517
543
untypeBinOp BDiv = UBDiv
544
+ untypeBinOp BDivs = UBDiv
518
545
untypeBinOp BAnd = UBAnd
519
546
untypeBinOp BAnds = UBAnd
520
547
untypeBinOp BOr = UBOr
0 commit comments