-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab9a4cd
commit 9110e83
Showing
2 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// Copyright (c) 2024 Markku Rossi | ||
// | ||
// All rights reserved. | ||
// | ||
|
||
package circuits | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/markkurossi/mpc/circuit" | ||
) | ||
|
||
// Rewrite applies rewrite patterns to the circuit. | ||
func (cc *Compiler) Rewrite() { | ||
var stats circuit.Stats | ||
|
||
start := time.Now() | ||
|
||
for _, g := range cc.Gates { | ||
switch g.Op { | ||
case circuit.AND: | ||
// AND(A,A) = A | ||
if g.A == g.B && g.O.NumOutputs() > 0 { | ||
stats[g.Op]++ | ||
g.ShortCircuit(g.A) | ||
} | ||
case circuit.OR: | ||
// OR(A,A) = A | ||
if g.A == g.B && g.O.NumOutputs() > 0 { | ||
stats[g.Op]++ | ||
g.ShortCircuit(g.A) | ||
} | ||
case circuit.XOR: | ||
// XOR(A,A) = 0 | ||
if g.A == g.B && g.O.NumOutputs() > 0 { | ||
stats[g.Op]++ | ||
g.O.SetValue(Zero) | ||
} | ||
} | ||
} | ||
|
||
elapsed := time.Since(start) | ||
|
||
if cc.Params.Diagnostics { | ||
fmt.Printf(" - Rewrite: %12s: %d/%d (%.2f%%)\n", | ||
elapsed, stats.Count(), len(cc.Gates), | ||
float64(stats.Count())/float64(len(cc.Gates))*100) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters