-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbro_ska.c
186 lines (168 loc) · 3.86 KB
/
bro_ska.c
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Generic multiplication (+ add) routines for matrices smaller than 128x128
* For legacy systems that don't have SSE2
*
* Pierre Karpman (CWI)
* 2017-07
*/
#include "bro_ska.h"
/*
* assert(V->ncols <= 64)
* assert(A->ncols <= 64)
*/
void mul_64_64_bro_ska(mzd_t *res, mzd_t *V, mzd_t *A, int clear)
{
unsigned acols = A->ncols;
unsigned vcols = V->ncols;
unsigned vrows = V->nrows;
for (int j = 0; j < vrows; j++)
{
uint64_t m, acc = 0;
uint64_t v = mzd_read_bits(V, j, 0, vcols);
v = ~v;
for (int i = 0; i < vcols; i++)
{
m = (v & 1) - 1;
// up to (64 - acols) bits of junk may be accumulated
// but they're eventually not copied
acc ^= (m & mzd_read_bits(A, i, 0, 64));
v >>= 1;
}
if (clear)
{
mzd_and_bits(res, j, 0, acols, 0);
}
mzd_xor_bits(res, j, 0, acols, acc);
}
return;
}
/*
* assert(V->ncols > 64)
* assert(V->ncols <= 128)
* assert(A->ncols <= 64)
*/
void mul_128_64_bro_ska(mzd_t *res, mzd_t *V, mzd_t *A, int clear)
{
unsigned acols = A->ncols;
unsigned vcols = V->ncols;
unsigned vrows = V->nrows;
for (int j = 0; j < vrows; j++)
{
uint64_t mlo, mhi;
uint64_t acc0 = 0;
uint64_t acc1 = 0;
uint64_t vlo = mzd_read_bits(V, j, 0, 64);
uint64_t vhi = mzd_read_bits(V, j, 64, vcols-64);
vlo = ~vlo;
vhi = ~vhi;
int i = 0;
for (; i < 64; i++)
{
mlo = (vlo & 1) - 1;
// up to (64 - acols) bits of junk may be accumulated
// but they're eventually not copied
acc0 ^= (mlo & mzd_read_bits(A, i, 0, 64));
vlo >>= 1;
}
for (; i < vcols; i++)
{
mhi = (vhi & 1) - 1;
acc1 ^= (mhi & mzd_read_bits(A, i, 0, 64)); // ditto
vhi >>= 1;
}
if (clear)
{
mzd_and_bits(res, j, 0, acols, 0);
}
mzd_xor_bits(res, j, 0, acols, acc0);
mzd_xor_bits(res, j, 0, acols, acc1);
}
return;
}
/*
* assert(V->ncols <= 64)
* assert(A->ncols > 64)
* assert(A->ncols <= 128)
*/
void mul_64_128_bro_ska(mzd_t *res, mzd_t *V, mzd_t *A, int clear)
{
unsigned acols = A->ncols;
unsigned vcols = V->ncols;
unsigned vrows = V->nrows;
for (int j = 0; j < vrows; j++)
{
uint64_t m;
uint64_t acclo = 0;
uint64_t acchi = 0;
uint64_t v = mzd_read_bits(V, j, 0, vcols);
v = ~v;
for (int i = 0; i < vcols; i++)
{
m = (v & 1) - 1;
acclo ^= (m & mzd_read_bits(A, i, 0, 64));
// up to (128 - acols) bits of junk may be accumulated
// but they're eventually not copied
acchi ^= (m & mzd_read_bits(A, i, 64, 64));
v >>= 1;
}
if (clear)
{
mzd_and_bits(res, j, 0, 64, 0);
mzd_and_bits(res, j, 64, acols-64, 0);
}
mzd_xor_bits(res, j, 0, 64, acclo);
mzd_xor_bits(res, j, 64, acols-64, acchi);
}
return;
}
/*
* assert(V->ncols > 64)
* assert(V->ncols <= 128)
* assert(A->ncols > 64)
* assert(A->ncols <= 128)
*/
void mul_128_128_bro_ska(mzd_t *res, mzd_t *V, mzd_t *A, int clear)
{
unsigned acols = A->ncols;
unsigned vcols = V->ncols;
unsigned vrows = V->nrows;
for (int j = 0; j < vrows; j++)
{
uint64_t mlo, mhi;
uint64_t acc0lo = 0;
uint64_t acc0hi = 0;
uint64_t acc1lo = 0;
uint64_t acc1hi = 0;
uint64_t vlo = mzd_read_bits(V, j, 0, 64);
uint64_t vhi = mzd_read_bits(V, j, 64, vcols-64);
vlo = ~vlo;
vhi = ~vhi;
int i = 0;
for (; i < 64; i++)
{
mlo = (vlo & 1) - 1;
acc0lo ^= (mlo & mzd_read_bits(A, i, 0, 64));
// up to (128 - acols) bits of junk may be accumulated
// but they're eventually not copied
acc0hi ^= (mlo & mzd_read_bits(A, i, 64, 64));
vlo >>= 1;
}
for (; i < vcols; i++)
{
mhi = (vhi & 1) - 1;
acc1lo ^= (mhi & mzd_read_bits(A, i, 0, 64));
acc1hi ^= (mhi & mzd_read_bits(A, i, 64, 64)); // ditto
vhi >>= 1;
}
if (clear)
{
mzd_and_bits(res, j, 0, 64, 0);
mzd_and_bits(res, j, 64, acols-64, 0);
}
mzd_xor_bits(res, j, 0, 64, acc0lo);
mzd_xor_bits(res, j, 0, 64, acc1lo);
mzd_xor_bits(res, j, 64, acols-64, acc0hi);
mzd_xor_bits(res, j, 64, acols-64, acc1hi);
}
return;
}