-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMult.cxx
More file actions
352 lines (286 loc) · 12.9 KB
/
Mult.cxx
File metadata and controls
352 lines (286 loc) · 12.9 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <array>
#include <vector>
#include <cstdint>
#include <stdexcept>
#include <iostream>
#include <chrono>
#include <functional>
using namespace std::chrono;
// =============================================================================================================================================================
// template < int Size > struct Variable;
// template < int Size > std::ostream& operator<< ( std::ostream& aStr , const Variable< Size >& A );
struct T
{
bool value;
};
template < int Size >
struct Variable
{
Variable()
{}
Variable( uint64_t aValue )
{
if( Size > 64 ) throw std::runtime_error( "Variable size exceeded 64-bits" );
#pragma GCC unroll 65534
#pragma GCC ivdep
for( int i(0); i!=Size; ++i ){
value[i] = new T{ bool( aValue & 0x1 ) };
aValue >>= 1;
}
if( aValue ) throw std::runtime_error( "Value does not fit size" );
}
Variable( const Variable<Size>& ) = default;
operator uint64_t() const
{
uint64_t lRet(0);
for( int i(0); i!=Size; ++i ) if( value[i]->value ) lRet |= ( 1<<i );
return lRet;
}
Variable< 1 > operator() ( const int& index ) const __attribute__((always_inline,flatten))
{
Variable< 1 > lRet;
lRet.value[0] = value[index];
return lRet;
}
template< int lo , int hi >
Variable< hi - lo > _slice_ () const
{
Variable< hi - lo > lRet;
for( auto src( value.begin() + lo ) , dest( lRet.value.begin() ); src != value.begin() + hi; ++src, ++dest ) *dest = *src;
return lRet;
}
Variable< Size >& operator= ( uint64_t aValue ) __attribute__((always_inline,flatten))
{
if( Size > 64 ) throw std::runtime_error( "Variable size exceeded 64-bits" );
#pragma GCC unroll 65534
#pragma GCC ivdep
for( int i(0); i!=Size; ++i ){
value[i]->value = bool( aValue & 0x1 );
aValue >>= 1;
}
if( aValue ) throw std::runtime_error( "Value does not fit size" );
return *this;
}
T* operator[] ( const std::size_t& i ) __attribute__((always_inline,flatten))
{ return value.at(i); }
const T* operator[] ( const std::size_t& i ) const __attribute__((always_inline,flatten))
{ return value.at(i); }
std::array< T* , Size > value;
};
#define slice(lo,hi) template _slice_<lo,hi>()
template < int Size >
std::ostream& operator<< ( std::ostream& aStr , const Variable< Size >& A )
{
aStr << "0b";
for( int i(Size-1); i>=0; --i ) std::cout << A[i]->value;
return aStr;
}
template < int SizeA , int SizeB >
Variable< SizeA + SizeB > operator& ( const Variable< SizeA >& A , const Variable< SizeB >& B )
{
Variable< SizeA + SizeB > lRet( 0 );
auto dest( lRet.value.begin() );
for( auto src( A.value.begin() ); src != A.value.end(); ++src, ++dest ) *dest = *src;
for( auto src( B.value.begin() ); src != B.value.end(); ++src, ++dest ) *dest = *src;
return lRet;
};
// =============================================================================================================================================================
struct NonTrivial
{
NonTrivial()
{
NonTrivial::instances.push_back( this );
}
virtual void Forward() = 0;
static void Run()
{
for( auto& i : NonTrivial::instances ) (*i).Forward();
}
static std::vector< NonTrivial* > instances;
};
std::vector< NonTrivial* > NonTrivial::instances;
// =============================================================================================================================================================
template < int Size >
struct Not : public NonTrivial
{
Not( const Variable< Size >& aA ) : A( aA ) , P( 0 ) {}
void Forward() __attribute__((always_inline,flatten)) {
#pragma GCC unroll 65534
#pragma GCC ivdep
for( auto p(P.value.begin()) , a(A.value.begin()) ; p!=P.value.end() ; ++p , ++a ) (**p).value = not (**a).value;
}
Variable< Size > A , P;
};
// =============================================================================================================================================================
// =============================================================================================================================================================
template < int Size >
struct And : public NonTrivial
{
And( const Variable< Size >& aA , const Variable< Size >& aB ) : A( aA ) , B( aB ) , P( 0 ) {}
void Forward() __attribute__((always_inline,flatten)) {
#pragma GCC unroll 65534
#pragma GCC ivdep
for( auto p(P.value.begin()) , a(A.value.begin()) , b(B.value.begin()) ; p!=P.value.end() ; ++p , ++a , ++b ) (**p).value = (**a).value and (**b).value;
}
Variable< Size > A , B , P;
};
// =============================================================================================================================================================
// =============================================================================================================================================================
template < int Size >
struct Or : public NonTrivial
{
Or( const Variable< Size >& aA , const Variable< Size >& aB ) : A( aA ) , B( aB ) , P( 0 ) {}
void Forward() __attribute__((always_inline,flatten)) {
#pragma GCC unroll 65534
#pragma GCC ivdep
for( auto p(P.value.begin()) , a(A.value.begin()) , b(B.value.begin()) ; p!=P.value.end() ; ++p , ++a , ++b ) (**p).value = (**a).value or (**b).value;
}
Variable< Size > A , B , P;
};
// =============================================================================================================================================================
// =============================================================================================================================================================
template < int Size >
struct Xor : public NonTrivial
{
Xor( const Variable< Size >& aA , const Variable< Size >& aB ) : A( aA ) , B( aB ) , P( 0 ) {}
void Forward() __attribute__((always_inline,flatten)) {
#pragma GCC unroll 65534
#pragma GCC ivdep
for( auto p(P.value.begin()) , a(A.value.begin()) , b(B.value.begin()) ; p!=P.value.end() ; ++p , ++a , ++b ) (**p).value = (**a).value xor (**b).value;
}
Variable< Size > A , B , P;
};
// =============================================================================================================================================================
// =============================================================================================================================================================
template < int Size >
struct Xnor : public NonTrivial
{
Xnor( const Variable< Size >& aA , const Variable< Size >& aB ) : A( aA ) , B( aB ) , P( 0 ) {}
void Forward() __attribute__((always_inline,flatten)) {
#pragma GCC unroll 65534
#pragma GCC ivdep
for( auto p(P.value.begin()) , a(A.value.begin()) , b(B.value.begin()) ; p!=P.value.end() ; ++p , ++a , ++b ) (**p).value = not( (**a).value xor (**b).value );
}
Variable< Size > A , B , P;
};
// =============================================================================================================================================================
// =============================================================================================================================================================
template < int Size >
struct Mux : public NonTrivial {
Mux( const Variable< 1 >& x , const Variable< Size >& a , const Variable< Size >& b ) : X(x) , A(a) , B(b) , P( 0 ) {}
void Forward() __attribute__((always_inline,flatten)) {
const bool lSwitch( X[0]->value );
#pragma GCC unroll 65534
#pragma GCC ivdep
for( auto p(P.value.begin()) , a(A.value.begin()) , b(B.value.begin()) ; p!=P.value.end() ; ++p , ++a , ++b ) (**p).value = lSwitch ? (**a).value : (**b).value;
}
Variable< 1 > X; Variable< Size > A , B , P;
};
// =============================================================================================================================================================
// =============================================================================================================================================================
template < int Size >
struct Fanout : public NonTrivial {
Fanout( const Variable< 1 >& x ) : X(x) , P( 0 ) {}
void Forward() __attribute__((always_inline,flatten)) {
const bool lValue( X[0]->value );
#pragma GCC unroll 65534
#pragma GCC ivdep
for( auto p(P.value.begin()) ; p!=P.value.end() ; ++p ) (**p).value = lValue;
}
Variable< 1 > X; Variable< Size > P;
};
// =============================================================================================================================================================
// =============================================================================================================================================================
template < int Size >
struct Add
{
static constexpr int SizeHi = Size/2;
static constexpr int SizeLo = Size - SizeHi;
static constexpr int OutputSize = Size + 1;
Add( const Variable< Size >& A , const Variable< Size >& B ) :
AdderLo( A.slice( 0 , SizeLo ) , B.slice( 0 , SizeLo ) ), AdderHi( A.slice( SizeLo , Size ) , B.slice( SizeLo , Size ) ),
MuxP( AdderLo.P(SizeLo) , AdderHi.Q , AdderHi.P ), MuxQ( AdderLo.Q(SizeLo) , AdderHi.Q , AdderHi.P ),
P( AdderLo.P.slice( 0 , SizeLo ) & MuxP.P ), Q( AdderLo.Q.slice( 0 , SizeLo ) & MuxQ.P )
{}
void Forward() __attribute__((always_inline,flatten))
{
AdderLo.Forward(), AdderHi.Forward(), MuxP.Forward(), MuxQ.Forward();
}
Add< SizeLo > AdderLo;
Add< SizeHi > AdderHi;
Mux< SizeHi+1 > MuxP , MuxQ;
Variable< OutputSize > P , Q;
};
template <>
struct Add< 1 >
{
static constexpr int OutputSize = 2;
Add( const Variable< 1 >& aA , const Variable< 1 >& aB ) :
mFanoutA( aA ) , mFanoutB( aB ),
mXor( mFanoutA.P(0) , mFanoutB.P(0) ), mAnd( mFanoutA.P(1) , mFanoutB.P(1) ), mXnor( mFanoutA.P(2) , mFanoutB.P(2) ), mOr( mFanoutA.P(3) , mFanoutB.P(3) ),
P( mXor.P & mAnd.P ) , Q( mXnor.P & mOr.P )
{}
void Forward() __attribute__((always_inline,flatten))
{
mFanoutA.Forward(), mFanoutB.Forward(), mXor.Forward(), mAnd.Forward(), mXnor.Forward(), mOr.Forward();
}
Fanout<4> mFanoutA , mFanoutB;
Xor<1> mXor;
And<1> mAnd;
Xnor<1> mXnor;
Or<1> mOr;
Variable< OutputSize > P , Q;
};
// =============================================================================================================================================================
// =============================================================================================================================================================
template < int SizeA , int SizeB >
struct Mult
{
static constexpr int SizeAHi = SizeA/2;
static constexpr int SizeALo = SizeA - SizeAHi;
static constexpr int SumSize = SizeB + SizeAHi;
static constexpr int OutputSize = SizeA + SizeB;
Mult( const Variable< SizeA >& A , const Variable< SizeB >& B ) :
MultLo( B , A.slice( 0 , SizeALo ) ) , MultHi( B , A.slice( SizeALo , SizeA ) ) ,
Adder( MultLo.P.slice(SizeALo,SizeALo+SizeB) & Variable<SizeAHi>(0) , MultHi.P ),
P( MultLo.P.slice(0,SizeALo) & Adder.P.slice(0,SumSize) )
{}
void Forward() __attribute__((always_inline,flatten))
{
MultLo.Forward(), MultHi.Forward(), Adder.Forward();
}
Mult< SizeB , SizeALo > MultLo;
Mult< SizeB , SizeAHi > MultHi;
Add< SumSize > Adder;
Variable< OutputSize > P;
};
template <>
struct Mult< 1 , 1 > : public And<1>
{
static constexpr int OutputSize = 2;
Mult( const Variable< 1 >& A , const Variable< 1 >& B ) : And( A , B ) , P( And::P & Variable<1>(0) )
{}
Variable< OutputSize > P;
};
// =============================================================================================================================================================
// =============================================================================================================================================================
int main()
{
Variable< 8 > A( 0 ) , B( 0 );
Mult< 8,8 > lMult( A , B );
{ auto start = high_resolution_clock::now(); \
for( uint64_t x(0); x!=20; ++x ) for( uint64_t i(0); i!=255; ++i ) for( uint64_t j(0); j!=255; ++j ) {
A = i; B = j;
NonTrivial::Run();
}
std::cout << "Using global Run : " << duration_cast<microseconds>( high_resolution_clock::now() - start).count()/1000000.0 << "s" << std::endl;
}
// { auto start = high_resolution_clock::now(); \
// for( uint64_t x(0); x!=20; ++x ) for( uint64_t i(0); i!=255; ++i ) for( uint64_t j(0); j!=255; ++j ) {
// A = i; B = j;
// lMult.Forward();
// }
// std::cout << "Using recursive calls: " << duration_cast<microseconds>( high_resolution_clock::now() - start).count()/1000000.0 << "s" << std::endl;
// }
};
// =============================================================================================================================================================