| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <stdbool.h> |
| 15 |
#include <stdint.h> |
| 16 |
#include <stdio.h> |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
#include <isc/ascii.h> |
| 20 |
#include <isc/random.h> |
| 21 |
#include <isc/time.h> |
| 22 |
|
| 23 |
#define SIZE (1024 * 1024) |
| 24 |
|
| 25 |
typedef void |
| 26 |
copy_fn(void *a, void *b, unsigned int len); |
| 27 |
|
| 28 |
static void |
| 29 |
time_it(copy_fn *copier, void *a, void *b, const char *name) { |
| 30 |
isc_time_t start; |
| 31 |
start = isc_time_now_hires(); |
| 32 |
|
| 33 |
copier(a, b, SIZE); |
| 34 |
|
| 35 |
isc_time_t finish; |
| 36 |
finish = isc_time_now_hires(); |
| 37 |
|
| 38 |
uint64_t microseconds = isc_time_microdiff(&finish, &start); |
| 39 |
printf("%f for %s\n", (double)microseconds / 1000000.0, name); |
| 40 |
} |
| 41 |
|
| 42 |
static void |
| 43 |
copy_raw(void *a, void *b, unsigned int size) { |
| 44 |
memmove(a, b, size); |
| 45 |
} |
| 46 |
|
| 47 |
static void |
| 48 |
copy_toupper(void *va, void *vb, unsigned int size) { |
| 49 |
uint8_t *a = va, *b = vb; |
| 50 |
while (size-- > 0) { |
| 51 |
*a++ = isc_ascii_toupper(*b++); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
static void |
| 56 |
copy_tolower8(void *a, void *b, unsigned int size) { |
| 57 |
isc_ascii_lowercopy(a, b, size); |
| 58 |
} |
| 59 |
|
| 60 |
#define TOLOWER(c) ((c) + ('a' - 'A') * (((c) >= 'A') ^ ((c) > 'Z'))) |
| 61 |
|
| 62 |
static void |
| 63 |
copy_tolower1(void *va, void *vb, unsigned int size) { |
| 64 |
for (uint8_t *a = va, *b = vb; size-- > 0; a++, b++) { |
| 65 |
*a = TOLOWER(*b); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
static bool |
| 70 |
cmp_tolower1(void *va, void *vb, unsigned int size) { |
| 71 |
for (uint8_t *a = va, *b = vb; size-- > 0; a++, b++) { |
| 72 |
if (TOLOWER(*a) != TOLOWER(*b)) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
} |
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
static bool oldskool_result; |
| 80 |
|
| 81 |
static void |
| 82 |
cmp_oldskool(void *va, void *vb, unsigned int size) { |
| 83 |
uint8_t *a = va, *b = vb, c; |
| 84 |
|
| 85 |
while (size > 3) { |
| 86 |
c = isc_ascii_tolower(a[0]); |
| 87 |
if (c != isc_ascii_tolower(b[0])) { |
| 88 |
goto diff; |
| 89 |
} |
| 90 |
c = isc_ascii_tolower(a[1]); |
| 91 |
if (c != isc_ascii_tolower(b[1])) { |
| 92 |
goto diff; |
| 93 |
} |
| 94 |
c = isc_ascii_tolower(a[2]); |
| 95 |
if (c != isc_ascii_tolower(b[2])) { |
| 96 |
goto diff; |
| 97 |
} |
| 98 |
c = isc_ascii_tolower(a[3]); |
| 99 |
if (c != isc_ascii_tolower(b[3])) { |
| 100 |
goto diff; |
| 101 |
} |
| 102 |
size -= 4; |
| 103 |
a += 4; |
| 104 |
b += 4; |
| 105 |
} |
| 106 |
while (size-- > 0) { |
| 107 |
c = isc_ascii_tolower(*a++); |
| 108 |
if (c != isc_ascii_tolower(*b++)) { |
| 109 |
goto diff; |
| 110 |
} |
| 111 |
} |
| 112 |
oldskool_result = true; |
| 113 |
return; |
| 114 |
diff: |
| 115 |
oldskool_result = false; |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
static bool tolower1_result; |
| 120 |
|
| 121 |
static void |
| 122 |
vcmp_tolower1(void *a, void *b, unsigned int size) { |
| 123 |
tolower1_result = cmp_tolower1(a, b, size); |
| 124 |
} |
| 125 |
|
| 126 |
static bool swar_result; |
| 127 |
|
| 128 |
static void |
| 129 |
cmp_swar(void *a, void *b, unsigned int size) { |
| 130 |
swar_result = isc_ascii_lowerequal(a, b, size); |
| 131 |
} |
| 132 |
|
| 133 |
static bool chunk_result; |
| 134 |
static unsigned int chunk_size; |
| 135 |
|
| 136 |
static void |
| 137 |
cmp_chunks1(void *va, void *vb, unsigned int size) { |
| 138 |
uint8_t *a = va, *b = vb; |
| 139 |
|
| 140 |
chunk_result = false; |
| 141 |
while (size >= chunk_size) { |
| 142 |
if (!cmp_tolower1(a, b, chunk_size)) { |
| 143 |
return; |
| 144 |
} |
| 145 |
size -= chunk_size; |
| 146 |
a += chunk_size; |
| 147 |
b += chunk_size; |
| 148 |
} |
| 149 |
chunk_result = cmp_tolower1(a, b, size); |
| 150 |
} |
| 151 |
|
| 152 |
static void |
| 153 |
cmp_chunks8(void *va, void *vb, unsigned int size) { |
| 154 |
uint8_t *a = va, *b = vb; |
| 155 |
|
| 156 |
while (size >= chunk_size) { |
| 157 |
if (!isc_ascii_lowerequal(a, b, chunk_size)) { |
| 158 |
goto diff; |
| 159 |
} |
| 160 |
size -= chunk_size; |
| 161 |
a += chunk_size; |
| 162 |
b += chunk_size; |
| 163 |
} |
| 164 |
chunk_result = isc_ascii_lowerequal(a, b, size); |
| 165 |
return; |
| 166 |
diff: |
| 167 |
chunk_result = false; |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
static void |
| 172 |
cmp_oldchunks(void *va, void *vb, unsigned int size) { |
| 173 |
uint8_t *a = va, *b = vb; |
| 174 |
|
| 175 |
while (size >= chunk_size) { |
| 176 |
cmp_oldskool(a, b, chunk_size); |
| 177 |
if (!oldskool_result) { |
| 178 |
return; |
| 179 |
} |
| 180 |
size -= chunk_size; |
| 181 |
a += chunk_size; |
| 182 |
b += chunk_size; |
| 183 |
} |
| 184 |
cmp_oldskool(a, b, size); |
| 185 |
} |
| 186 |
|
| 187 |
int |
| 188 |
main(void) { |
| 189 |
static uint8_t bytes[SIZE]; |
| 190 |
|
| 191 |
isc_random_buf(bytes, SIZE); |
| 192 |
|
| 193 |
static uint8_t raw_dest[SIZE]; |
| 194 |
time_it(copy_raw, raw_dest, bytes, "memmove"); |
| 195 |
|
| 196 |
static uint8_t toupper_dest[SIZE]; |
| 197 |
time_it(copy_toupper, toupper_dest, bytes, "toupper"); |
| 198 |
|
| 199 |
static uint8_t tolower1_dest[SIZE]; |
| 200 |
time_it(copy_tolower1, tolower1_dest, bytes, "tolower1"); |
| 201 |
|
| 202 |
static uint8_t tolower8_dest[SIZE]; |
| 203 |
time_it(copy_tolower8, tolower8_dest, bytes, "tolower8"); |
| 204 |
|
| 205 |
time_it(cmp_oldskool, toupper_dest, tolower1_dest, "oldskool"); |
| 206 |
printf("-> %s\n", oldskool_result ? "same" : "WAT"); |
| 207 |
|
| 208 |
time_it(vcmp_tolower1, tolower1_dest, tolower8_dest, "tolower1"); |
| 209 |
printf("-> %s\n", tolower1_result ? "same" : "WAT"); |
| 210 |
|
| 211 |
time_it(cmp_swar, toupper_dest, tolower8_dest, "swar"); |
| 212 |
printf("-> %s\n", swar_result ? "same" : "WAT"); |
| 213 |
|
| 214 |
for (chunk_size = 3; chunk_size <= 15; chunk_size += 2) { |
| 215 |
time_it(cmp_chunks1, toupper_dest, raw_dest, "chunks1"); |
| 216 |
printf("%u -> %s\n", chunk_size, chunk_result ? "same" : "WAT"); |
| 217 |
time_it(cmp_chunks8, toupper_dest, raw_dest, "chunks8"); |
| 218 |
printf("%u -> %s\n", chunk_size, chunk_result ? "same" : "WAT"); |
| 219 |
time_it(cmp_oldchunks, toupper_dest, raw_dest, "oldchunks"); |
| 220 |
printf("%u -> %s\n", chunk_size, |
| 221 |
oldskool_result ? "same" : "WAT"); |
| 222 |
} |
| 223 |
} |