summaryrefslogtreecommitdiffstats
path: root/all_pairs/source/ndes/ndes.c
blob: 5d00d94f950802dfa4d5c22248e98906038f5a18 (plain) (blame)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
  This program is part of the TACLeBench benchmark suite.
  Version V 1.x

  Name: ndes

  Author: unknown

  Function: A lot of bit manipulation, shifts, array and matrix calculations.

  Source:

  Changes: no major functional changes

  License: May be used, modified, and re-distributed freely.

*/

/* A read from this address will result in an known value of 1 */
#include "extra.h"
#define KNOWN_VALUE 1
#define NDES_WORSTCASE 1
/*
  Declaration of global variables
*/

typedef struct NDES_IMMENSE {
  unsigned long l, r;
} ndes_immense;
typedef struct NDES_GREAT {
  unsigned long l, c, r;
} ndes_great;

unsigned long ndes_bit[33];
ndes_immense ndes_inp, ndes_key, ndes_out;
int ndes_newkey, ndes_isw;

static ndes_immense ndes_icd;
static char ndes_ipc1[57];
static char ndes_ipc2[49];


#ifdef NDES_WORSTCASE
int ndes_value = 1;
#else
int ndes_value = 0;
#endif

/* Forward funtion prototypes */

void ndes_des( ndes_immense inp, ndes_immense key, int *newkey, int isw,
               ndes_immense *out );
void ndes_cyfun( unsigned long ir, ndes_great k, unsigned long *iout );
unsigned long ndes_getbit( ndes_immense source, int bitno, int nbits );
void ndes_ks( /*immense key, */int n, ndes_great *kn );
void ndes_init( void );
int ndes_return( void );
void ndes_main( void );
//int main( void );

/*
   Initialization
*/
void ndes_init()
{
  unsigned int i;
  volatile char ndes_ipc1_tmp[57] = {
    0, 57, 49, 41, 33, 25, 17, 9, 1, 58, 50,
    42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60,
    52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38,
    30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4
  };
  volatile char ndes_ipc2_tmp[49] = {
    0, 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21,
    10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31,
    37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34,
    53, 46, 42, 50, 36, 29, 32
  };
  for ( i = 0; i < 57; i++ )
    ndes_ipc1[i] = ndes_ipc1_tmp[i];
  for ( i = 0; i < 49; i++ )
    ndes_ipc2[i] = ndes_ipc2_tmp[i];

  ndes_inp.l = KNOWN_VALUE * 35;
  ndes_inp.r = KNOWN_VALUE * 26;
  ndes_key.l = KNOWN_VALUE * 2;
  ndes_key.r = KNOWN_VALUE * 16;

  ndes_newkey = ndes_value;
  ndes_isw = ndes_value;
}
/*
   Arithmetic functions
*/
void ndes_des( ndes_immense inp, ndes_immense key, int *newkey, int isw,
               ndes_immense *out )
{
  volatile char ip[65] = {
    0, 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36,
    28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40,
    32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35,
    27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39,
    31, 23, 15, 7
  };

  volatile char ipm[65] = {
    0, 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15,
    55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13,
    53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11,
    51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9,
    49, 17, 57, 25
  };
  static ndes_great kns[17];

  #ifdef NDES_WORSTCASE
  static int initflag = 1;
  #else
  static int initflag = 0;
  #endif

  int ii, i, j, k;
  unsigned long ic, shifter;
  ndes_immense itmp;
  ndes_great pg;

  if ( initflag ) {
    initflag = 0;
    ndes_bit[1] = shifter = 1L;

    _Pragma( "loopbound min 31 max 31" )
    for ( j = 2; j <= 32; j++ )
      ndes_bit[j] = ( shifter <<= 1 );
  }

  if ( *newkey ) {
    *newkey = 0;
    ndes_icd.r = ndes_icd.l = 0L;

    _Pragma( "loopbound min 28 max 28" )
    for ( j = 28, k = 56; j >= 1; j--, k-- ) {
      ndes_icd.r = ( ndes_icd.r << 1 ) | ndes_getbit( key, ndes_ipc1[j], 32 );
      ndes_icd.l = ndes_icd.l << 1;
      ndes_icd.l = ( ndes_icd.l ) | ndes_getbit( key, ndes_ipc1[k], 32 );
    }

    _Pragma( "loopbound min 16 max 16" )
    for ( i = 1; i <= 16; i++ ) {
      pg = kns[i];
      ndes_ks( /* key,*/ i, &pg );
      kns[i] = pg;
    }
  }

  itmp.r = itmp.l = 0L;

  _Pragma( "loopbound min 32 max 32" )
  for ( j = 32, k = 64; j >= 1; j--, k-- ) {
	itmp.r = itmp.r << 1;
    itmp.r = ( itmp.r ) | ndes_getbit( inp, ip[j], 32 );
	itmp.l = itmp.l << 1;
	itmp.l = ( itmp.l ) | ndes_getbit( inp, ip[k], 32 );
  }
  _Pragma( "loopbound min 16 max 16" )
  for ( i = 1; i <= 16; i++ ) {
    ii = ( isw == 1 ? 17 - i : i );
    ndes_cyfun( itmp.l, kns[ii], &ic );
    ic ^= itmp.r;
    itmp.r = itmp.l;
    itmp.l = ic;
  }

  ic = itmp.r;
  itmp.r = itmp.l;
  itmp.l = ic;
  ( *out ).r = ( *out ).l = 0L;

  _Pragma( "loopbound min 32 max 32" )
  for ( j = 32, k = 64; j >= 1; j--, k-- ) {
	( *out ).r = ( *out ).r << 1;
	( *out ).r = ( ( *out ).r ) | ndes_getbit( itmp, ipm[j], 32 );
	( *out ).l = ( *out ).l << 1;
	( *out ).l = ( ( *out ).l ) | ndes_getbit( itmp, ipm[k], 32 );
  }
}


void ndes_cyfun( unsigned long ir, ndes_great k, unsigned long *iout )
{
  volatile long iet[49] = {0, 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9,
                         10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19,
                         20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29,
                         28, 29, 30, 31, 32, 1
                        };
  volatile long ipp[33] = {0, 16, 7, 20, 21, 29, 12, 28, 17, 1, 15,
                         23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13,
                         30, 6, 22, 11, 4, 25
                        };
  volatile long is[16][4][9] = {{{ 0 , 14 , 15 , 10 , 7 , 2 , 12 , 4 , 13 },
	{ 0 , 0 , 3 , 13 , 13 , 14 , 10 , 13 , 1 },
	{ 0 , 4 , 0 ,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 },
	{ 0 , 5 , 9 , 11 , 10 , 9 , 11 , 15 , 14 },
	{ 0 , 10 , 3 , 10 , 2 , 3 , 13 , 5 , 3 },
	{ 0 , 0 , 5 , 5 , 7 , 4 , 0 , 2 , 5 }},
	{{ 0 , 0 , 5 , 2 , 4 , 14 , 5 , 6 , 12 },
	{ 0 , 3 , 11 , 15 , 14 , 8 , 3 , 8 , 9 },
	{ 0 , 5 , 2 , 14 , 8 , 0 , 11 , 9 , 5 },
	{ 0 , 6 , 14 , 2 , 2 , 5 , 8 , 3 , 6 }},
	{{ 0 , 7 , 10 , 8 , 15 , 9 , 11 , 1 , 7 },
	{ 0 , 8 , 5 , 1 , 9 , 6 , 8 , 6 , 2 },
	{ 0 , 0 , 15 , 7 , 4 , 14 , 6 , 2 , 8 },
	{ 0 , 13 , 9 , 12 , 14 , 3 , 13 , 12 , 11 }}};

  volatile char ibin[16] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15};
  ndes_great ie;
  unsigned long itmp, ietmp1, ietmp2;
  char iec[9];
  int irow, icol, iss, l, m;
  int volatile j, jj;
  unsigned long *p;

  p = ndes_bit;
  ie.r = ie.c = ie.l = 0;

  _Pragma( "loopbound min 16 max 16" )
  for ( j = 16, l = 32, m = 48; j >= 1; j--, l--, m-- ) {
    ie.r = ( ie.r << 1 ) | ( p[iet[j]] & ir ? 1 : 0 );
    ie.c = ( ie.c << 1 ) | ( p[iet[l]] & ir ? 1 : 0 );
    ie.l = ( ie.l << 1 ) | ( p[iet[m]] & ir ? 1 : 0 );
  }
  ie.r ^= k.r;
  ie.c ^= k.c;
  ie.l ^= k.l;
  ietmp1 = ( ( unsigned long ) ie.c << 16 ) + ( unsigned long ) ie.r;
  ietmp2 = ( ( unsigned long ) ie.l << 8 ) + ( ( unsigned long ) ie.c >> 8 );

  _Pragma( "loopbound min 4 max 4" )
  for ( j = 1, m = 5; j <= 4; j++, m++ ) {
    iec[j] = ietmp1 & 0x3fL;
    iec[m] = ietmp2 & 0x3fL;
    ietmp1 >>= 6;
    ietmp2 >>= 6;
  }

  itmp = 0L;

  _Pragma( "loopbound min 8 max 8" )
  for ( jj = 8; jj >= 1; jj-- ) {
    j = iec[jj];
    irow = ( ( j & 0x1 ) << 1 ) + ( ( j & 0x20 ) >> 5 );
    icol = ( ( j & 0x2 ) << 2 ) + ( j & 0x4 )
           + ( ( j & 0x8 ) >> 2 ) + ( ( j & 0x10 ) >> 4 );
    iss = is[icol][irow][jj];
    itmp = ( itmp << 4 ) | ibin[iss];
  }

  *iout = 0L;
  p = ndes_bit;

  _Pragma( "loopbound min 32 max 32" )
  for ( j = 32; j >= 1; j-- )
	*iout = ( *iout << 1 );
	*iout |= ( p[ipp[j]] & itmp ? 1 : 0 );
}

unsigned long ndes_getbit( ndes_immense source, int bitno, int nbits )
{
  if ( bitno <= nbits )
    return ndes_bit[bitno] & source.r ? 1L : 0L;
  else
    return ndes_bit[bitno - nbits] & source.l ? 1L : 0L;
}

void ndes_ks( /*ndes_immense key, */int n, ndes_great *kn )
{
  int i, j, k, l;

  if ( n == 1 || n == 2 || n == 9 || n == 16 ) {
    ndes_icd.r = ( ndes_icd.r | ( ( ndes_icd.r & 1L ) << 28 ) ) >> 1;
    ndes_icd.l = ( ndes_icd.l | ( ( ndes_icd.l & 1L ) << 28 ) ) >> 1;
  } else {
    _Pragma( "loopbound min 2 max 2" )
    for ( i = 1; i <= 2; i++ ) {
      ndes_icd.r = ( ndes_icd.r | ( ( ndes_icd.r & 1L ) << 28 ) ) >> 1;
      ndes_icd.l = ( ndes_icd.l | ( ( ndes_icd.l & 1L ) << 28 ) ) >> 1;
    }
  }

  ( *kn ).r = ( *kn ).c = ( *kn ).l = 0;

  _Pragma( "loopbound min 16 max 16" )
  for ( j = 16, k = 32, l = 48; j >= 1; j--, k--, l-- ) {
	( *kn ).r = ( *kn ).r << 1;
    ( *kn ).r = ( ( *kn ).r ) | ( unsigned short )
                ndes_getbit( ndes_icd, ndes_ipc2[j], 28 );
	( *kn ).c = ( *kn ).c << 1;
    ( *kn ).c = ( ( *kn ).c ) | ( unsigned short )
                ndes_getbit( ndes_icd, ndes_ipc2[k], 28 );
	( *kn ).l = ( *kn ).l << 1;
    ( *kn ).l = ( ( *kn ).l ) | ( unsigned short )
                ndes_getbit( ndes_icd, ndes_ipc2[l], 28 );
  }
}

int ndes_return()
{
  return (ndes_icd.r+ndes_icd.l  + (-8390656) ) != 0 ;
}

void _Pragma( "entrypoint" ) ndes_main()
{
  ndes_des( ndes_inp, ndes_key, &ndes_newkey, ndes_isw, &ndes_out );
}

/* main function */

int main( int argc, char **argv )
{
  SET_UP
  for_each_job {
    ndes_init();
    ndes_main();
  }
  WRITE_TO_FILE

  return ( ndes_return() );
}