1 // Copyright 2017 The Wuffs Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // ----------------
16
17 // This file contains a hand-written C implementation of the Adler32 hash
18 // function, based on the code generated by the Wuffs std/zlib implementation.
19 // Its purpose is to benchmark different compilers, or different versions of
20 // the same compiler family.
21 //
22 // For example, on a Debian Testing system as of November 2017:
23 //
24 // $ clang-5.0 -O3 adler32-standalone.c; ./a.out
25 // 2311 MiB/s, clang 5.0.0 (tags/RELEASE_500/rc2)
26 // $ gcc -O3 adler32-standalone.c; ./a.out
27 // 3052 MiB/s, gcc 7.2.1 20171025
28 //
29 // which suggest that clang's code performs at about 76% the throughput of
30 // gcc's. This is filed as https://bugs.llvm.org/show_bug.cgi?id=35567
31
32 #include <stdbool.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <sys/time.h>
37
38 #define BUFFER_SIZE (1024 * 1024)
39 #define ONE_MIBIBYTE (1024 * 1024)
40
41 // The order matters here. Clang also defines "__GNUC__".
42 #if defined(__clang__)
43 const char* g_cc = "clang";
44 const char* g_cc_version = __clang_version__;
45 #elif defined(__GNUC__)
46 const char* g_cc = "gcc";
47 const char* g_cc_version = __VERSION__;
48 #elif defined(_MSC_VER)
49 const char* g_cc = "cl";
50 const char* g_cc_version = "???";
51 #else
52 const char* g_cc = "cc";
53 const char* g_cc_version = "???";
54 #endif
55
56 struct {
57 int remaining_argc;
58 char** remaining_argv;
59
60 bool no_check;
61 } g_flags = {0};
62
63 const char* //
parse_flags(int argc,char ** argv)64 parse_flags(int argc, char** argv) {
65 int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name.
66 for (; c < argc; c++) {
67 char* arg = argv[c];
68 if (*arg++ != '-') {
69 break;
70 }
71
72 // A double-dash "--foo" is equivalent to a single-dash "-foo". As special
73 // cases, a bare "-" is not a flag (some programs may interpret it as
74 // stdin) and a bare "--" means to stop parsing flags.
75 if (*arg == '\x00') {
76 break;
77 } else if (*arg == '-') {
78 arg++;
79 if (*arg == '\x00') {
80 c++;
81 break;
82 }
83 }
84
85 if (!strcmp(arg, "no-check")) {
86 g_flags.no_check = true;
87 continue;
88 }
89
90 return "main: unrecognized flag argument";
91 }
92
93 g_flags.remaining_argc = argc - c;
94 g_flags.remaining_argv = argv + c;
95 return NULL;
96 }
97
98 static uint32_t //
calculate_hash(uint8_t * x_ptr,size_t x_len)99 calculate_hash(uint8_t* x_ptr, size_t x_len) {
100 uint32_t s1 = 1;
101 uint32_t s2 = 0;
102
103 while (x_len > 0) {
104 uint8_t* prefix_ptr;
105 size_t prefix_len;
106 if (x_len > 5552) {
107 prefix_ptr = x_ptr;
108 prefix_len = 5552;
109 x_ptr = x_ptr + 5552;
110 x_len = x_len - 5552;
111 } else {
112 prefix_ptr = x_ptr;
113 prefix_len = x_len;
114 x_ptr = NULL;
115 x_len = 0;
116 }
117
118 uint8_t* p = prefix_ptr;
119 uint8_t* end0 = prefix_ptr + (prefix_len / 8) * 8;
120 while (p < end0) {
121 s1 += ((uint32_t)(*p));
122 s2 += s1;
123 p++;
124 s1 += ((uint32_t)(*p));
125 s2 += s1;
126 p++;
127 s1 += ((uint32_t)(*p));
128 s2 += s1;
129 p++;
130 s1 += ((uint32_t)(*p));
131 s2 += s1;
132 p++;
133 s1 += ((uint32_t)(*p));
134 s2 += s1;
135 p++;
136 s1 += ((uint32_t)(*p));
137 s2 += s1;
138 p++;
139 s1 += ((uint32_t)(*p));
140 s2 += s1;
141 p++;
142 s1 += ((uint32_t)(*p));
143 s2 += s1;
144 p++;
145 }
146 uint8_t* end1 = prefix_ptr + prefix_len;
147 while (p < end1) {
148 s1 += ((uint32_t)(*p));
149 s2 += s1;
150 p++;
151 }
152
153 s1 %= 65521;
154 s2 %= 65521;
155 }
156 return (s2 << 16) | s1;
157 }
158
159 uint8_t g_buffer[BUFFER_SIZE] = {0};
160
161 int //
main(int argc,char ** argv)162 main(int argc, char** argv) {
163 const char* err_msg = parse_flags(argc, argv);
164 if (err_msg) {
165 fprintf(stderr, "%s\n", err_msg);
166 return 1;
167 }
168
169 struct timeval bench_start_tv;
170 gettimeofday(&bench_start_tv, NULL);
171
172 const int num_reps = 1000;
173
174 // If changing BUFFER_SIZE, re-calculate this expected value with
175 // https://play.golang.org/p/IU2T58P00C
176 const uint32_t expected_hash_of_1mib_of_zeroes = 0x00f00001UL;
177
178 int i;
179 int num_bad = 0;
180 for (i = 0; i < num_reps; i++) {
181 uint32_t actual_hash = calculate_hash(g_buffer, BUFFER_SIZE);
182 if (!g_flags.no_check && (actual_hash != expected_hash_of_1mib_of_zeroes)) {
183 num_bad++;
184 }
185 }
186 if (num_bad) {
187 printf("num_bad: %d\n", num_bad);
188 return 1;
189 }
190
191 struct timeval bench_finish_tv;
192 gettimeofday(&bench_finish_tv, NULL);
193
194 int64_t micros =
195 (int64_t)(bench_finish_tv.tv_sec - bench_start_tv.tv_sec) * 1000000 +
196 (int64_t)(bench_finish_tv.tv_usec - bench_start_tv.tv_usec);
197 int64_t numer = ((int64_t)num_reps) * BUFFER_SIZE * 1000000;
198 int64_t denom = micros * ONE_MIBIBYTE;
199 int64_t mib_per_s = denom ? numer / denom : 0;
200
201 printf("%8d MiB/s, %s %s\n", (int)mib_per_s, g_cc, g_cc_version);
202 return 0;
203 }
204