summaryrefslogtreecommitdiff
path: root/nbench1.h
blob: 77f2bb56b51a539bc0f38d0447b9214388760214 (plain)
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
** nbench1.h
** Header for nbench1.c
** BYTEmark (tm)
** BYTE's Native Mode Benchmarks
** Rick Grehan, BYTE Magazine
**
** Creation:
** Revision: 3/95;10/95
**
** DISCLAIMER
** The source, executable, and documentation files that comprise
** the BYTEmark benchmarks are made available on an "as is" basis.
** This means that we at BYTE Magazine have made every reasonable
** effort to verify that the there are no errors in the source and
** executable code.  We cannot, however, guarantee that the programs
** are error-free.  Consequently, McGraw-HIll and BYTE Magazine make
** no claims in regard to the fitness of the source code, executable
** code, and documentation of the BYTEmark.
**  Furthermore, BYTE Magazine, McGraw-Hill, and all employees
** of McGraw-Hill cannot be held responsible for any damages resulting
** from the use of this code or the results obtained from using
** this code.
*/
#include <stdint.h>

/*
** DEFINES
*/
/* #define DEBUG */

/*
** EXTERNALS
*/
extern unsigned long global_min_ticks;

extern SortStruct global_numsortstruct;
extern SortStruct global_strsortstruct;
extern BitOpStruct global_bitopstruct;
extern EmFloatStruct global_emfloatstruct;
extern FourierStruct global_fourierstruct;
extern AssignStruct global_assignstruct;
extern IDEAStruct global_ideastruct;
extern HuffStruct global_huffstruct;
extern NNetStruct global_nnetstruct;
extern LUStruct global_lustruct;

/* External PROTOTYPES */
/*extern unsigned long abs_randwc(unsigned long num);*/     /* From MISC */
/*extern long randnum(long lngval);*/
extern int32_t randwc(int32_t num);
extern uint32_t abs_randwc(uint32_t num);
extern int32_t randnum(int32_t lngval);

extern void *AllocateMemory(unsigned long nbytes,    /* From SYSSPEC */
	int *errorcode);
extern void FreeMemory(void *mempointer,
	int *errorcode);
extern void MoveMemory(void *destination,
		void *source, unsigned long nbytes);
extern void ReportError(char *context, int errorcode);
extern unsigned long StartStopwatch();
extern unsigned long StopStopwatch(unsigned long startticks);
extern unsigned long TicksToSecs(unsigned long tickamount);
extern double TicksToFracSecs(unsigned long tickamount);

/*****************
** NUMERIC SORT **
*****************/

/*
** PROTOTYPES
*/
void DoNumSort(void);
static unsigned long DoNumSortIteration(long *arraybase,
		unsigned long arraysize,
		unsigned int numarrays);
static void LoadNumArrayWithRand(long *array,
		unsigned long arraysize,
		unsigned int numarrays);
static void NumHeapSort(long *array,
		unsigned long bottom,
		unsigned long top);
static void NumSift(long *array,
		unsigned long i,
		unsigned long j);


/****************
** STRING SORT **
*****************
*/


/*
** PROTOTYPES
*/
void DoStringSort(void);
static unsigned long DoStringSortIteration(unsigned char *arraybase,
		unsigned int numarrays,
		unsigned long arraysize);
static unsigned long *LoadStringArray(unsigned char *strarray,
		unsigned int numarrays,
		unsigned long *strings,
		unsigned long arraysize);
static void stradjust(unsigned long *optrarray,
		unsigned char *strarray,
		unsigned long nstrings,
		unsigned long i,
		unsigned char l);
static void StrHeapSort(unsigned long *optrarray,
		unsigned char *strarray,
		unsigned long numstrings,
		unsigned long bottom,
		unsigned long top);
static int str_is_less(unsigned long *optrarray,
		unsigned char *strarray,
		unsigned long numstrings,
		unsigned long a,
		unsigned long b);
static void strsift(unsigned long *optrarray,
		unsigned char *strarray,
		unsigned long numstrings,
		unsigned long i,
		unsigned long j);

/************************
** BITFIELD OPERATIONS **
*************************
*/

/*
** PROTOTYPES
*/
void DoBitops(void);
static unsigned long DoBitfieldIteration(unsigned long *bitarraybase,
		unsigned long *bitoparraybase,
		long bitoparraysize,
		unsigned long *nbitops);
static void ToggleBitRun(unsigned long *bitmap,
		unsigned long bit_addr,
		unsigned long nbits,
		unsigned int val);
static void FlipBitRun(unsigned long *bitmap,
		unsigned long bit_addr,
		unsigned long nbits);

/****************************
** EMULATED FLOATING POINT **
****************************/
typedef struct
{
	uint8_t type;        /* Indicates, NORMAL, SUBNORMAL, etc. */
	uint8_t sign;        /* Mantissa sign */
	short exp;      /* Signed exponent...no bias */
	uint16_t mantissa[INTERNAL_FPF_PRECISION];
} InternalFPF;

/*
** PROTOTYPES
*/
void DoEmFloat(void);

/*
** EXTERNALS
*/
extern void SetupCPUEmFloatArrays(InternalFPF *abase,
	InternalFPF *bbase, InternalFPF *cbase,
	unsigned long arraysize);
extern unsigned long DoEmFloatIteration(InternalFPF *abase,
	InternalFPF *bbase, InternalFPF *cbase,
	unsigned long arraysize, unsigned long loops);

/*************************
** FOURIER COEFFICIENTS **
*************************/

/*
** PROTOTYPES
*/
void DoFourier(void);
static unsigned long DoFPUTransIteration(double *abase,
		double *bbase,
		unsigned long arraysize);
static double TrapezoidIntegrate(double x0,
		double x1,
		int nsteps,
		double omegan,
		int select);
static double thefunction(double x,
		double omegan,
		int select);

/*************************
** ASSIGNMENT ALGORITHM **
*************************/

/*
** DEFINES
*/

#define ASSIGNROWS 101L
#define ASSIGNCOLS 101L

/*
** TYPEDEFS
*/
typedef struct {
	union {
		long *p;
		long (*ap)[ASSIGNROWS][ASSIGNCOLS];
	} ptrs;
} longptr;

/*
** PROTOTYPES
*/
void DoAssign(void);
static unsigned long DoAssignIteration(long *arraybase,
		unsigned long numarrays);
static void LoadAssignArrayWithRand(long *arraybase,
		unsigned long numarrays);
static void LoadAssign(long arraybase[][ASSIGNCOLS]);
static void CopyToAssign(long arrayfrom[][ASSIGNCOLS],
		long arrayto[][ASSIGNCOLS]);
static void Assignment(long arraybase[][ASSIGNCOLS]);
static void calc_minimum_costs(long tableau[][ASSIGNCOLS]);
static int first_assignments(long tableau[][ASSIGNCOLS],
		short assignedtableau[][ASSIGNCOLS]);
static void second_assignments(long tableau[][ASSIGNCOLS],
		short assignedtableau[][ASSIGNCOLS]);

/********************
** IDEA ENCRYPTION **
********************/

/*
** DEFINES
*/
#define IDEAKEYSIZE 16
#define IDEABLOCKSIZE 8
#define ROUNDS 8
#define KEYLEN (6*ROUNDS+4)

/*
** MACROS
*/
#define low16(x) ((x) & 0x0FFFF)
#define MUL(x,y) (x=mul(low16(x),y))


typedef uint16_t IDEAkey[KEYLEN];

/*
** PROTOTYPES
*/
void DoIDEA(void);
static unsigned long DoIDEAIteration(unsigned char *plain1,
	unsigned char *crypt1, unsigned char *plain2,
	unsigned long arraysize, unsigned long nloops,
	IDEAkey Z, IDEAkey DK);
static uint16_t mul(register uint16_t a, register uint16_t b);
static uint16_t inv(uint16_t x);
static void en_key_idea(uint16_t userkey[8], IDEAkey Z);
static void de_key_idea(IDEAkey Z, IDEAkey DK);
static void cipher_idea(uint16_t in[4], uint16_t out[4], IDEAkey Z);

/************************
** HUFFMAN COMPRESSION **
************************/

/*
** DEFINES
*/
#define EXCLUDED 32000L          /* Big positive value */

/*
** TYPEDEFS
*/
typedef struct {
	unsigned char c;                /* Byte value */
	float freq;             /* Frequency */
	int parent;             /* Parent node */
	int left;               /* Left pointer = 0 */
	int right;              /* Right pointer = 1 */
} huff_node;

/*
** GLOBALS
*/
static huff_node *hufftree;             /* The huffman tree */
static long plaintextlen;               /* Length of plaintext */

/*
** PROTOTYPES
*/
void DoHuffman();
static void create_text_line(char *dt,long nchars);
static void create_text_block(char *tb, unsigned long tblen,
		unsigned short maxlinlen);
static unsigned long DoHuffIteration(char *plaintext,
	char *comparray, char *decomparray,
	unsigned long arraysize, unsigned long nloops, huff_node *hufftree);
static void SetCompBit(uint8_t *comparray, uint32_t bitoffset, char bitchar);
static int GetCompBit(uint8_t *comparray, uint32_t bitoffset);

/********************************
** BACK PROPAGATION NEURAL NET **
********************************/

/*
** DEFINES
*/
#define T 1                     /* TRUE */
#define F 0                     /* FALSE */
#define ERR -1
#define MAXPATS 10              /* max number of patterns in data file */
#define IN_X_SIZE 5             /* number of neurodes/row of input layer */
#define IN_Y_SIZE 7             /* number of neurodes/col of input layer */
#define IN_SIZE 35              /* equals IN_X_SIZE*IN_Y_SIZE */
#define MID_SIZE 8              /* number of neurodes in middle layer */
#define OUT_SIZE 8              /* number of neurodes in output layer */
#define MARGIN 0.1              /* how near to 1,0 do we have to come to stop? */
#define BETA 0.09               /* beta learning constant */
#define ALPHA 0.09              /* momentum term constant */
#define STOP 0.1                /* when worst_error less than STOP, training is done */

/*
** GLOBALS
*/
double  mid_wts[MID_SIZE][IN_SIZE];     /* middle layer weights */
double  out_wts[OUT_SIZE][MID_SIZE];    /* output layer weights */
double  mid_out[MID_SIZE];              /* middle layer output */
double  out_out[OUT_SIZE];              /* output layer output */
double  mid_error[MID_SIZE];            /* middle layer errors */
double  out_error[OUT_SIZE];            /* output layer errors */
double  mid_wt_change[MID_SIZE][IN_SIZE]; /* storage for last wt change */
double  out_wt_change[OUT_SIZE][MID_SIZE]; /* storage for last wt change */
double  in_pats[MAXPATS][IN_SIZE];      /* input patterns */
double  out_pats[MAXPATS][OUT_SIZE];    /* desired output patterns */
double  tot_out_error[MAXPATS];         /* measure of whether net is done */
double  out_wt_cum_change[OUT_SIZE][MID_SIZE]; /* accumulated wt changes */
double  mid_wt_cum_change[MID_SIZE][IN_SIZE];  /* accumulated wt changes */

double  worst_error; /* worst error each pass through the data */
double  average_error; /* average error each pass through the data */
double  avg_out_error[MAXPATS]; /* average error each pattern */

int iteration_count;    /* number of passes thru network so far */
int numpats;            /* number of patterns in data file */
int numpasses;          /* number of training passes through data file */
int learned;            /* flag--if TRUE, network has learned all patterns */

/*
** PROTOTYPES
*/
void DoNNET(void);
static unsigned long DoNNetIteration(unsigned long nloops);
static void do_mid_forward(int patt);
static void do_out_forward();
void display_output(int patt);
static void do_forward_pass(int patt);
static void do_out_error(int patt);
static void worst_pass_error();
static void do_mid_error();
static void adjust_out_wts();
static void adjust_mid_wts();
static void do_back_pass(int patt);
static void move_wt_changes();
static int check_out_error();
static void zero_changes();
static void randomize_wts();
static int read_data_file();
/* static int initialize_net(); */

/***********************
**  LU DECOMPOSITION  **
** (Linear Equations) **
***********************/

/*
** DEFINES
*/

#define LUARRAYROWS 101L
#define LUARRAYCOLS 101L

/*
** TYPEDEFS
*/
typedef struct
{       union
	{       double *p;
		double (*ap)[][LUARRAYCOLS];
	} ptrs;
} LUdblptr;

/*
** GLOBALS
*/
double *LUtempvv;

/*
** PROTOTYPES
*/
void DoLU(void);
static void LUFreeMem(double *a, double *b,
	double *abase, double *bbase);
static unsigned long DoLUIteration(double *a, double *b,
	double *abase, double *bbase,
	unsigned long numarrays);
static void build_problem( double a[][LUARRAYCOLS],
	int n, double b[LUARRAYROWS]);
static int ludcmp(double a[][LUARRAYCOLS],
	int n, int indx[], int *d);
static void lubksb(double a[][LUARRAYCOLS],
	int n, int indx[LUARRAYROWS],
	double b[LUARRAYROWS]);
static int lusolve(double a[][LUARRAYCOLS],
	int n, double b[LUARRAYROWS]);