1 /*
2 * Copyright (c) 2007 - 2015 Joseph Gaeddert
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/resource.h>
27
28 #include "liquid.internal.h"
29
30 #define CRC_BENCH_API(CRC,N) \
31 ( struct rusage *_start, \
32 struct rusage *_finish, \
33 unsigned long int *_num_iterations) \
34 { crc_bench(_start, _finish, _num_iterations, CRC, N); }
35
36 // Helper function to keep code base small
crc_bench(struct rusage * _start,struct rusage * _finish,unsigned long int * _num_iterations,crc_scheme _crc,unsigned int _n)37 void crc_bench(struct rusage *_start,
38 struct rusage *_finish,
39 unsigned long int *_num_iterations,
40 crc_scheme _crc,
41 unsigned int _n)
42 {
43 // normalize number of iterations
44 if (_crc != LIQUID_CRC_CHECKSUM)
45 *_num_iterations /= 8;
46
47 unsigned long int i;
48
49 // create arrays
50 unsigned char msg[_n];
51 unsigned int key = 0;
52
53 // initialze message
54 for (i=0; i<_n; i++)
55 msg[i] = rand() & 0xff;
56
57 // start trials
58 getrusage(RUSAGE_SELF, _start);
59 for (i=0; i<(*_num_iterations); i++) {
60 key ^= crc_generate_key(_crc, msg, _n);
61 key ^= crc_generate_key(_crc, msg, _n);
62 key ^= crc_generate_key(_crc, msg, _n);
63 key ^= crc_generate_key(_crc, msg, _n);
64 }
65 getrusage(RUSAGE_SELF, _finish);
66 *_num_iterations *= 4;
67 }
68
69 //
70 // BENCHMARKS
71 //
72 void benchmark_crc_checksum_n256 CRC_BENCH_API(LIQUID_CRC_CHECKSUM, 256)
73
74 void benchmark_crc_crc8_n256 CRC_BENCH_API(LIQUID_CRC_8, 256)
75 void benchmark_crc_crc16_n256 CRC_BENCH_API(LIQUID_CRC_16, 256)
76 void benchmark_crc_crc24_n256 CRC_BENCH_API(LIQUID_CRC_24, 256)
77 void benchmark_crc_crc32_n256 CRC_BENCH_API(LIQUID_CRC_32, 256)
78
79