blob: a2e22b963c52eb47c9ec52b5183b6f449ade63eb [file] [log] [blame]
Nigel Taocb17fdd2017-12-07 12:59:03 +11001// 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
Nigel Tao670b45b2019-02-02 08:10:08 +110015// ----------------
16
Nigel Taocb17fdd2017-12-07 12:59:03 +110017// 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//
Nigel Tao44f5b2d2017-12-08 11:14:30 +110029// 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
Nigel Taocb17fdd2017-12-07 12:59:03 +110031
Nigel Tao44f5b2d2017-12-08 11:14:30 +110032#include <stdbool.h>
Nigel Taocb17fdd2017-12-07 12:59:03 +110033#include <stdint.h>
34#include <stdio.h>
Nigel Tao44f5b2d2017-12-08 11:14:30 +110035#include <string.h>
Nigel Taocb17fdd2017-12-07 12:59:03 +110036#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__)
Nigel Taoc0bd9df2020-03-26 21:55:44 +110043const char* g_cc = "clang";
44const char* g_cc_version = __clang_version__;
Nigel Taocb17fdd2017-12-07 12:59:03 +110045#elif defined(__GNUC__)
Nigel Taoc0bd9df2020-03-26 21:55:44 +110046const char* g_cc = "gcc";
47const char* g_cc_version = __VERSION__;
Nigel Taocb17fdd2017-12-07 12:59:03 +110048#elif defined(_MSC_VER)
Nigel Taoc0bd9df2020-03-26 21:55:44 +110049const char* g_cc = "cl";
50const char* g_cc_version = "???";
Nigel Taocb17fdd2017-12-07 12:59:03 +110051#else
Nigel Taoc0bd9df2020-03-26 21:55:44 +110052const char* g_cc = "cc";
53const char* g_cc_version = "???";
Nigel Taocb17fdd2017-12-07 12:59:03 +110054#endif
55
Nigel Tao68920952020-03-03 11:25:18 +110056struct {
57 int remaining_argc;
58 char** remaining_argv;
59
60 bool no_check;
Nigel Taoc0bd9df2020-03-26 21:55:44 +110061} g_flags = {0};
Nigel Tao68920952020-03-03 11:25:18 +110062
63const char* //
64parse_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")) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +110086 g_flags.no_check = true;
Nigel Tao68920952020-03-03 11:25:18 +110087 continue;
88 }
89
90 return "main: unrecognized flag argument";
91 }
92
Nigel Taoc0bd9df2020-03-26 21:55:44 +110093 g_flags.remaining_argc = argc - c;
94 g_flags.remaining_argv = argv + c;
Nigel Tao68920952020-03-03 11:25:18 +110095 return NULL;
96}
97
Nigel Tao2914bae2020-02-26 09:40:30 +110098static uint32_t //
99calculate_hash(uint8_t* x_ptr, size_t x_len) {
Nigel Taocb17fdd2017-12-07 12:59:03 +1100100 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
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100159uint8_t g_buffer[BUFFER_SIZE] = {0};
Nigel Taocb17fdd2017-12-07 12:59:03 +1100160
Nigel Tao2914bae2020-02-26 09:40:30 +1100161int //
162main(int argc, char** argv) {
Nigel Tao68920952020-03-03 11:25:18 +1100163 const char* err_msg = parse_flags(argc, argv);
164 if (err_msg) {
165 fprintf(stderr, "%s\n", err_msg);
166 return 1;
Nigel Tao44f5b2d2017-12-08 11:14:30 +1100167 }
168
Nigel Taocb17fdd2017-12-07 12:59:03 +1100169 struct timeval bench_start_tv;
170 gettimeofday(&bench_start_tv, NULL);
171
172 const int num_reps = 1000;
Nigel Tao44f5b2d2017-12-08 11:14:30 +1100173
174 // If changing BUFFER_SIZE, re-calculate this expected value with
175 // https://play.golang.org/p/IU2T58P00C
Nigel Taocb17fdd2017-12-07 12:59:03 +1100176 const uint32_t expected_hash_of_1mib_of_zeroes = 0x00f00001UL;
Nigel Tao44f5b2d2017-12-08 11:14:30 +1100177
Nigel Taocb17fdd2017-12-07 12:59:03 +1100178 int num_bad = 0;
Nigel Taod5075c72021-11-02 14:07:58 +1100179 for (int i = 0; i < num_reps; i++) {
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100180 uint32_t actual_hash = calculate_hash(g_buffer, BUFFER_SIZE);
181 if (!g_flags.no_check && (actual_hash != expected_hash_of_1mib_of_zeroes)) {
Nigel Taocb17fdd2017-12-07 12:59:03 +1100182 num_bad++;
183 }
184 }
185 if (num_bad) {
186 printf("num_bad: %d\n", num_bad);
187 return 1;
188 }
189
190 struct timeval bench_finish_tv;
191 gettimeofday(&bench_finish_tv, NULL);
192
193 int64_t micros =
194 (int64_t)(bench_finish_tv.tv_sec - bench_start_tv.tv_sec) * 1000000 +
195 (int64_t)(bench_finish_tv.tv_usec - bench_start_tv.tv_usec);
Nigel Tao44f5b2d2017-12-08 11:14:30 +1100196 int64_t numer = ((int64_t)num_reps) * BUFFER_SIZE * 1000000;
197 int64_t denom = micros * ONE_MIBIBYTE;
198 int64_t mib_per_s = denom ? numer / denom : 0;
Nigel Taocb17fdd2017-12-07 12:59:03 +1100199
Nigel Taoc0bd9df2020-03-26 21:55:44 +1100200 printf("%8d MiB/s, %s %s\n", (int)mib_per_s, g_cc, g_cc_version);
Nigel Taocb17fdd2017-12-07 12:59:03 +1100201 return 0;
202}