Skip to content

Commit 2b2feb3

Browse files
committed
StandardOperations refactored
1 parent 821b530 commit 2b2feb3

1 file changed

Lines changed: 42 additions & 153 deletions

File tree

Lines changed: 42 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,69 @@
11
namespace GraphBLAS.FSharp.Backend.Common
22

3-
open GraphBLAS.FSharp.Backend.Common
4-
53
type AtLeastOne<'a, 'b when 'a: struct and 'b: struct> =
64
| Both of 'a * 'b
75
| Left of 'a
86
| Right of 'b
97

108
module StandardOperations =
11-
let boolSum =
12-
<@ fun (_: bool option) (_: bool option) -> Some true @>
13-
14-
let intSum =
15-
<@ fun (x: int option) (y: int option) ->
16-
let mutable res = 0
9+
let inline mkNumericSum zero =
10+
<@ fun (x: 't option) (y: 't option) ->
11+
let mutable res = zero
1712

1813
match x, y with
1914
| Some f, Some s -> res <- f + s
2015
| Some f, None -> res <- f
2116
| None, Some s -> res <- s
2217
| None, None -> ()
2318

24-
if res = 0 then None else Some res @>
19+
if res = zero then None else Some res @>
2520

26-
let byteSum =
27-
<@ fun (x: byte option) (y: byte option) ->
28-
let mutable res = 0uy
29-
30-
match x, y with
31-
| Some f, Some s -> res <- f + s
32-
| Some f, None -> res <- f
33-
| None, Some s -> res <- s
34-
| None, None -> ()
35-
36-
if res = 0uy then None else Some res @>
37-
38-
let floatSum =
39-
<@ fun (x: float option) (y: float option) ->
40-
let mutable res = 0.0
41-
42-
match x, y with
43-
| Some f, Some s -> res <- f + s
44-
| Some f, None -> res <- f
45-
| None, Some s -> res <- s
46-
| None, None -> ()
47-
48-
if res = 0 then None else Some res @>
49-
50-
let float32Sum =
51-
<@ fun (x: float32 option) (y: float32 option) ->
52-
let mutable res = 0.0f
53-
54-
match x, y with
55-
| Some f, Some s -> res <- f + s
56-
| Some f, None -> res <- f
57-
| None, Some s -> res <- s
58-
| None, None -> ()
59-
60-
if res = 0f then None else Some res @>
61-
62-
let boolSumAtLeastOne =
63-
<@ fun (_: AtLeastOne<bool, bool>) -> Some true @>
64-
65-
let intSumAtLeastOne =
66-
<@ fun (values: AtLeastOne<int, int>) ->
67-
let mutable res = 0
21+
let inline mkNumericSumAtLeastOne zero =
22+
<@ fun (values: AtLeastOne<'t, 't>) ->
23+
let mutable res = zero
6824

6925
match values with
7026
| Both (f, s) -> res <- f + s
7127
| Left f -> res <- f
7228
| Right s -> res <- s
7329

74-
if res = 0 then None else Some res @>
30+
if res = zero then None else Some res @>
7531

76-
let byteSumAtLeastOne =
77-
<@ fun (values: AtLeastOne<byte, byte>) ->
78-
let mutable res = 0uy
32+
let inline mkNumericMul zero =
33+
<@ fun (x: 't option) (y: 't option) ->
34+
let mutable res = zero
7935

80-
match values with
81-
| Both (f, s) -> res <- f + s
82-
| Left f -> res <- f
83-
| Right s -> res <- s
36+
match x, y with
37+
| Some f, Some s -> res <- f * s
38+
| _ -> ()
8439

85-
if res = 0uy then None else Some res @>
40+
if res = zero then None else Some res @>
8641

87-
let floatSumAtLeastOne =
88-
<@ fun (values: AtLeastOne<float, float>) ->
89-
let mutable res = 0.0
42+
let inline mkNumericMulAtLeastOne zero =
43+
<@ fun (values: AtLeastOne<'t, 't>) ->
44+
let mutable res = zero
9045

9146
match values with
92-
| Both (f, s) -> res <- f + s
93-
| Left f -> res <- f
94-
| Right s -> res <- s
47+
| Both (f, s) -> res <- f * s
48+
| _ -> ()
9549

96-
if res = 0.0 then None else Some res @>
50+
if res = zero then None else Some res @>
9751

98-
let float32SumAtLeastOne =
99-
<@ fun (values: AtLeastOne<float32, float32>) ->
100-
let mutable res = 0f
52+
let boolSum =
53+
<@ fun (_: bool option) (_: bool option) -> Some true @>
10154

102-
match values with
103-
| Both (f, s) -> res <- f + s
104-
| Left f -> res <- f
105-
| Right s -> res <- s
55+
let intSum = mkNumericSum 0
56+
let byteSum = mkNumericSum 0uy
57+
let floatSum = mkNumericSum 0.0
58+
let float32Sum = mkNumericSum 0f
10659

107-
if res = 0f then None else Some res @>
60+
let boolSumAtLeastOne =
61+
<@ fun (_: AtLeastOne<bool, bool>) -> Some true @>
62+
63+
let intSumAtLeastOne = mkNumericSumAtLeastOne 0
64+
let byteSumAtLeastOne = mkNumericSumAtLeastOne 0uy
65+
let floatSumAtLeastOne = mkNumericSumAtLeastOne 0.0
66+
let float32SumAtLeastOne = mkNumericSumAtLeastOne 0f
10867

10968
let boolMul =
11069
<@ fun (x: bool option) (y: bool option) ->
@@ -116,45 +75,10 @@ module StandardOperations =
11675

11776
if res then Some true else None @>
11877

119-
let intMul =
120-
<@ fun (x: int option) (y: int option) ->
121-
let mutable res = 0
122-
123-
match x, y with
124-
| Some f, Some s -> res <- f * s
125-
| _ -> ()
126-
127-
if res = 0 then None else Some res @>
128-
129-
let byteMul =
130-
<@ fun (x: byte option) (y: byte option) ->
131-
let mutable res = 0uy
132-
133-
match x, y with
134-
| Some f, Some s -> res <- f * s
135-
| _ -> ()
136-
137-
if res = 0uy then None else Some res @>
138-
139-
let floatMul =
140-
<@ fun (x: float option) (y: float option) ->
141-
let mutable res = 0.0
142-
143-
match x, y with
144-
| Some f, Some s -> res <- f * s
145-
| _ -> ()
146-
147-
if res = 0 then None else Some res @>
148-
149-
let float32Mul =
150-
<@ fun (x: float32 option) (y: float32 option) ->
151-
let mutable res = 0.0f
152-
153-
match x, y with
154-
| Some f, Some s -> res <- f * s
155-
| _ -> ()
156-
157-
if res = 0f then None else Some res @>
78+
let intMul = mkNumericMul 0
79+
let byteMul = mkNumericMul 0uy
80+
let floatMul = mkNumericMul 0.0
81+
let float32Mul = mkNumericMul 0f
15882

15983
let boolMulAtLeastOne =
16084
<@ fun (values: AtLeastOne<bool, bool>) ->
@@ -166,42 +90,7 @@ module StandardOperations =
16690

16791
if res then None else (Some true) @>
16892

169-
let intMulAtLeastOne =
170-
<@ fun (values: AtLeastOne<int, int>) ->
171-
let mutable res = 0
172-
173-
match values with
174-
| Both (f, s) -> res <- f * s
175-
| _ -> ()
176-
177-
if res = 0 then None else Some res @>
178-
179-
let byteMulAtLeastOne =
180-
<@ fun (values: AtLeastOne<byte, byte>) ->
181-
let mutable res = 0uy
182-
183-
match values with
184-
| Both (f, s) -> res <- f * s
185-
| _ -> ()
186-
187-
if res = 0uy then None else Some res @>
188-
189-
let floatMulAtLeastOne =
190-
<@ fun (values: AtLeastOne<float, float>) ->
191-
let mutable res = 0.0
192-
193-
match values with
194-
| Both (f, s) -> res <- f * s
195-
| _ -> ()
196-
197-
if res = 0.0 then None else Some res @>
198-
199-
let float32MulAtLeastOne =
200-
<@ fun (values: AtLeastOne<float32, float32>) ->
201-
let mutable res = 0f
202-
203-
match values with
204-
| Both (f, s) -> res <- f * s
205-
| _ -> ()
206-
207-
if res = 0f then None else Some res @>
93+
let intMulAtLeastOne = mkNumericMulAtLeastOne 0
94+
let byteMulAtLeastOne = mkNumericMulAtLeastOne 0uy
95+
let floatMulAtLeastOne = mkNumericMulAtLeastOne 0.0
96+
let float32MulAtLeastOne = mkNumericMulAtLeastOne 0f

0 commit comments

Comments
 (0)