-
Notifications
You must be signed in to change notification settings - Fork 0
/
VerifyNetworks.h
125 lines (108 loc) · 2.48 KB
/
VerifyNetworks.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//------------------------------------------------------------------------------
// <auto-generated>
// This code was auto-generated
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
#ifndef VERIFY_NETWORKS_H
#define VERIFY_NETWORKS_H
#include <inttypes.h>
#include <algorithm>
#include "ArrayHelpers.h"
namespace verification
{
int current_permutation = 0;
int current_2_pow_size = 1;
bool NextPermutation(int* arr, int size)
{
//Gray Code Progression
/**
* __asm__(
* "bsf %[perm],%[shift]\n\t"
* : [shift] "r"(shift)
* : [perm] "r"(current_permutation)
* )
*
*/
++current_permutation;
int shift = current_2_pow_size;
int current;
for (current = 0 ; current < size; ++current)
{
shift = shift >> 1;
if (current_permutation % shift == 0)
{
arr[current] = !arr[current];
return (current_permutation < current_2_pow_size);
}
}
return false;
}
int Sum(int* arr, int arraySize)
{
int result = 0;
for (int i = 0; i < arraySize; i += 1)
{
result += arr[i];
}
return result;
}
bool VerifyNetwork(int size, void(*network)(int*,size_t))
{
int* arr = (int*) malloc(sizeof(int) * size * 2);
int* toSort = arr + size;
int ones = 0;
for (int index = 0; index < size; index += 1)
{
arr[index] = 0;
}
current_permutation = 0;
current_2_pow_size = 1 << size; // pow(2, size)
do
{
ones = Sum(arr, size);
CopyArray(arr, toSort, size);
network(toSort, size);
if (__builtin_expect(!IsSorted(toSort, size) | ones != Sum(toSort, size), 0))
{
return false;
}
} while (NextPermutation(arr, size));
return true;
}
template <typename Swap>
bool VerifySwap()
{
SortableRef left;
SortableRef right;
uint64_t largerKey = 5;
uint64_t largerKeyRef = 436862519;
uint64_t smallerKey = 4;
uint64_t smallerKeyRef = 436862520;
left.key = largerKey;
left.reference = largerKeyRef;
// left.reference = 32;
right.key = smallerKey;
right.reference = smallerKeyRef;
Swap::swap(left, right);
if (left.key != smallerKey
|| left.reference != smallerKeyRef
|| right.key != largerKey
|| right.reference != largerKeyRef)
{
return false;
}
Swap::swap(left, right);
if (left.key != smallerKey
|| left.reference != smallerKeyRef
|| right.key != largerKey
|| right.reference != largerKeyRef)
{
return false;
}
return true;
}
} // namespace verification
#endif // VERIFY_NETWORKS_H