Nigel Tao | cb17fdd | 2017-12-07 12:59:03 +1100 | [diff] [blame] | 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 | |
Nigel Tao | 670b45b | 2019-02-02 08:10:08 +1100 | [diff] [blame] | 15 | // ---------------- |
| 16 | |
Nigel Tao | cb17fdd | 2017-12-07 12:59:03 +1100 | [diff] [blame] | 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 | // |
Nigel Tao | 44f5b2d | 2017-12-08 11:14:30 +1100 | [diff] [blame] | 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 |
Nigel Tao | cb17fdd | 2017-12-07 12:59:03 +1100 | [diff] [blame] | 31 | |
Nigel Tao | 44f5b2d | 2017-12-08 11:14:30 +1100 | [diff] [blame] | 32 | #include <stdbool.h> |
Nigel Tao | cb17fdd | 2017-12-07 12:59:03 +1100 | [diff] [blame] | 33 | #include <stdint.h> |
| 34 | #include <stdio.h> |
Nigel Tao | 44f5b2d | 2017-12-08 11:14:30 +1100 | [diff] [blame] | 35 | #include <string.h> |
Nigel Tao | cb17fdd | 2017-12-07 12:59:03 +1100 | [diff] [blame] | 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__) |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 43 | const char* g_cc = "clang"; |
| 44 | const char* g_cc_version = __clang_version__; |
Nigel Tao | cb17fdd | 2017-12-07 12:59:03 +1100 | [diff] [blame] | 45 | #elif defined(__GNUC__) |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 46 | const char* g_cc = "gcc"; |
| 47 | const char* g_cc_version = __VERSION__; |
Nigel Tao | cb17fdd | 2017-12-07 12:59:03 +1100 | [diff] [blame] | 48 | #elif defined(_MSC_VER) |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 49 | const char* g_cc = "cl"; |
| 50 | const char* g_cc_version = "???"; |
Nigel Tao | cb17fdd | 2017-12-07 12:59:03 +1100 | [diff] [blame] | 51 | #else |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 52 | const char* g_cc = "cc"; |
| 53 | const char* g_cc_version = "???"; |
Nigel Tao | cb17fdd | 2017-12-07 12:59:03 +1100 | [diff] [blame] | 54 | #endif |
| 55 | |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 56 | struct { |
| 57 | int remaining_argc; |
| 58 | char** remaining_argv; |
| 59 | |
| 60 | bool no_check; |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 61 | } g_flags = {0}; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 62 | |
| 63 | const char* // |
| 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")) { |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 86 | g_flags.no_check = true; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 87 | continue; |
| 88 | } |
| 89 | |
| 90 | return "main: unrecognized flag argument"; |
| 91 | } |
| 92 | |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 93 | g_flags.remaining_argc = argc - c; |
| 94 | g_flags.remaining_argv = argv + c; |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 95 | return NULL; |
| 96 | } |
| 97 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 98 | static uint32_t // |
| 99 | calculate_hash(uint8_t* x_ptr, size_t x_len) { |
Nigel Tao | cb17fdd | 2017-12-07 12:59:03 +1100 | [diff] [blame] | 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 | |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 159 | uint8_t g_buffer[BUFFER_SIZE] = {0}; |
Nigel Tao | cb17fdd | 2017-12-07 12:59:03 +1100 | [diff] [blame] | 160 | |
Nigel Tao | 2914bae | 2020-02-26 09:40:30 +1100 | [diff] [blame] | 161 | int // |
| 162 | main(int argc, char** argv) { |
Nigel Tao | 6892095 | 2020-03-03 11:25:18 +1100 | [diff] [blame] | 163 | const char* err_msg = parse_flags(argc, argv); |
| 164 | if (err_msg) { |
| 165 | fprintf(stderr, "%s\n", err_msg); |
| 166 | return 1; |
Nigel Tao | 44f5b2d | 2017-12-08 11:14:30 +1100 | [diff] [blame] | 167 | } |
| 168 | |
Nigel Tao | cb17fdd | 2017-12-07 12:59:03 +1100 | [diff] [blame] | 169 | struct timeval bench_start_tv; |
| 170 | gettimeofday(&bench_start_tv, NULL); |
| 171 | |
| 172 | const int num_reps = 1000; |
Nigel Tao | 44f5b2d | 2017-12-08 11:14:30 +1100 | [diff] [blame] | 173 | |
| 174 | // If changing BUFFER_SIZE, re-calculate this expected value with |
| 175 | // https://play.golang.org/p/IU2T58P00C |
Nigel Tao | cb17fdd | 2017-12-07 12:59:03 +1100 | [diff] [blame] | 176 | const uint32_t expected_hash_of_1mib_of_zeroes = 0x00f00001UL; |
Nigel Tao | 44f5b2d | 2017-12-08 11:14:30 +1100 | [diff] [blame] | 177 | |
Nigel Tao | cb17fdd | 2017-12-07 12:59:03 +1100 | [diff] [blame] | 178 | int num_bad = 0; |
Nigel Tao | d5075c7 | 2021-11-02 14:07:58 +1100 | [diff] [blame] | 179 | for (int i = 0; i < num_reps; i++) { |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 180 | 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 Tao | cb17fdd | 2017-12-07 12:59:03 +1100 | [diff] [blame] | 182 | 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 Tao | 44f5b2d | 2017-12-08 11:14:30 +1100 | [diff] [blame] | 196 | 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 Tao | cb17fdd | 2017-12-07 12:59:03 +1100 | [diff] [blame] | 199 | |
Nigel Tao | c0bd9df | 2020-03-26 21:55:44 +1100 | [diff] [blame] | 200 | printf("%8d MiB/s, %s %s\n", (int)mib_per_s, g_cc, g_cc_version); |
Nigel Tao | cb17fdd | 2017-12-07 12:59:03 +1100 | [diff] [blame] | 201 | return 0; |
| 202 | } |