Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Z
ZKG
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jakob Schneider Villumsen
ZKG
Merge requests
!11
Add mod2 gates
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Add mod2 gates
ops
into
main
Overview
0
Commits
3
Pipelines
0
Changes
2
Merged
Jakob Schneider Villumsen
requested to merge
ops
into
main
1 year ago
Overview
0
Commits
3
Pipelines
0
Changes
2
Expand
0
0
Merge request reports
Compare
main
version 2
bec5d696
1 year ago
version 1
f55eb5ed
1 year ago
main (base)
and
latest version
latest version
a3055b70
3 commits,
1 year ago
version 2
bec5d696
2 commits,
1 year ago
version 1
f55eb5ed
1 commit,
1 year ago
2 files
+
49
−
13
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
Search (e.g. *.vue) (Ctrl+P)
src/futhark/circuitmod2.fut
+
33
−
13
Options
import "util"
import "circuit"
module CircuitMod2 : Circuit = {
type s = u32
type t = {
p1 :
u32
,
p2 :
u32
,
p3 :
u32
p1 :
s
,
p2 :
s
,
p3 :
s
}
type s = u32
-- TODO: Rewrite this module using [32]bool for parallelism instead of u32. Maybe we get better performance?
def set_bit (x: u32) (i: u32) (b: u32): u32 =
if (b & 1u32) == 1u32 then x | (1u32 << i) else x & (! (1u32 << i))
def get_bit (x: u32) (i: u32): u32 =
(x >> i) & 1u32
def right_rotate (x: u32) (n: u32): u32 =
(x >> n) | (x << (32u32 - n))
-- | Returns the addition neutral element (zero).
--
def zero : t = { p1 = 0, p2 = 0, p3 = 0 }
def map (f: s -> s) ({p1, p2, p3}: t) =
{
@@ -42,6 +38,30 @@ module CircuitMod2 : Circuit = {
def (!!) = NEG
def and (r: t) (a: t) (b: t): t =
{
p1 = (a.p1 & b.p2) ^ (a.p2 & b.p1) ^ (a.p1 & b.p1) ^ r.p1 ^ r.p2,
p2 = (a.p2 & b.p3) ^ (a.p3 & b.p2) ^ (a.p2 & b.p2) ^ r.p2 ^ r.p3,
p3 = (a.p3 & b.p1) ^ (a.p1 & b.p3) ^ (a.p3 & b.p3) ^ r.p3 ^ r.p1
}
def add_bit_i (((r, a, b), c): ((t, t, t), t)) (idx: i32) =
let i = u32.i32 idx
let a' = map2 (^) a c |> map (get_bit i)
let b' = map2 (^) b c |> map (get_bit i)
let t1 = (a'.p1 & b'.p2) ^ (a'.p2 & b'.p1) ^ (get_bit i r.p2)
let t2 = (a'.p2 & b'.p3) ^ (a'.p3 & b'.p2) ^ (get_bit i r.p3)
let t3 = (a'.p3 & b'.p1) ^ (a'.p1 & b'.p3) ^ (get_bit i r.p1)
let c1 = c.p1 |> set_bit (i + 1) (t1 ^ (a'.p1 & b'.p1) ^ (get_bit i c.p1) ^ get_bit i r.p1)
let c2 = c.p2 |> set_bit (i + 1) (t2 ^ (a'.p2 & b'.p2) ^ (get_bit i c.p2) ^ get_bit i r.p2)
let c3 = c.p3 |> set_bit (i + 1) (t3 ^ (a'.p3 & b'.p3) ^ (get_bit i c.p3) ^ get_bit i r.p3)
let c' = { p1 = c1, p2 = c2, p3 = c3 }
in ((r, a', b), c')
def add (r: t) (a: t) (b: t): t =
let c = foldl add_bit_i ((r, a, b), zero) (0i32..1i32..<32i32) |> snd
in a ^^ b ^^ c
--| Unary addition
def (+) (alpha: s) (a: t) : t = a with p1 = a.p1 + alpha
Loading