1 /* -*- mode: C -*-
2  *
3  * Copyright (c) 2007-2010 The University of Utah
4  * All rights reserved.
5  *
6  * This file is part of `csmith', a random generator of C programs.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  *   * Redistributions of source code must retain the above copyright notice,
12  *     this list of conditions and the following disclaimer.
13  *
14  *   * Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef RANDOM_RUNTIME_H
32 #define RANDOM_RUNTIME_H
33 
34 #ifdef CSMITH_MINIMAL
35 #include "csmith_minimal.h"
36 #else
37 
38 /*****************************************************************************/
39 
40 #include <string.h>
41 
42 #define __STDC_LIMIT_MACROS
43 #include "random_inc.h"
44 
45 static uint32_t crc32_tab[256];
46 static uint32_t crc32_context = 0xFFFFFFFFUL;
47 
48 static void
crc32_gentab(void)49 crc32_gentab (void)
50 {
51 	uint32_t crc;
52 	const uint32_t poly = 0xEDB88320UL;
53 	int i, j;
54 
55 	for (i = 0; i < 256; i++) {
56 		crc = i;
57 		for (j = 8; j > 0; j--) {
58 			if (crc & 1) {
59 				crc = (crc >> 1) ^ poly;
60 			} else {
61 				crc >>= 1;
62 			}
63 		}
64 		crc32_tab[i] = crc;
65 	}
66 }
67 
68 static void
crc32_byte(uint8_t b)69 crc32_byte (uint8_t b) {
70 	crc32_context =
71 		((crc32_context >> 8) & 0x00FFFFFF) ^
72 		crc32_tab[(crc32_context ^ b) & 0xFF];
73 }
74 
75 #if defined(__SPLAT__) || defined (__COMPCERT__) || defined(NO_LONGLONG)
76 static void
crc32_8bytes(uint32_t val)77 crc32_8bytes (uint32_t val)
78 {
79 	crc32_byte ((val>>0) & 0xff);
80 	crc32_byte ((val>>8) & 0xff);
81 	crc32_byte ((val>>16) & 0xff);
82 	crc32_byte ((val>>24) & 0xff);
83 }
84 
85 static void
transparent_crc(uint32_t val,char * vname,int flag)86 transparent_crc (uint32_t val, char* vname, int flag)
87 {
88 	crc32_8bytes(val);
89 	if (flag) {
90   		printf("...checksum after hashing %s : %X\n", vname, crc32_context ^ 0xFFFFFFFFU);
91 	}
92 }
93 #else
94 static void
crc32_8bytes(uint64_t val)95 crc32_8bytes (uint64_t val)
96 {
97 	crc32_byte ((val>>0) & 0xff);
98 	crc32_byte ((val>>8) & 0xff);
99 	crc32_byte ((val>>16) & 0xff);
100 	crc32_byte ((val>>24) & 0xff);
101 	crc32_byte ((val>>32) & 0xff);
102 	crc32_byte ((val>>40) & 0xff);
103 	crc32_byte ((val>>48) & 0xff);
104 	crc32_byte ((val>>56) & 0xff);
105 }
106 
107 static void
transparent_crc(uint64_t val,char * vname,int flag)108 transparent_crc (uint64_t val, char* vname, int flag)
109 {
110 	crc32_8bytes(val);
111 	if (flag) {
112   		printf("...checksum after hashing %s : %lX\n", vname, crc32_context ^ 0xFFFFFFFFUL);
113 	}
114 }
115 #endif
116 
117 /*****************************************************************************/
118 
119 #endif
120 
121 #endif /* RANDOM_RUNTIME_H */
122 
123 /*
124  * Local Variables:
125  * c-basic-offset: 4
126  * tab-width: 4
127  * End:
128  */
129 
130 /* End of file. */
131