1 /*
2  * Copyright (c) 2013 by Farsight Security, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdbool.h>
18 #include <stddef.h>
19 #include <stdint.h>
20 
21 #include <stdio.h>
22 
23 #include "crc32c.h"
24 
25 static uint32_t my_crc32c_first(const uint8_t *buf, size_t len);
26 
27 my_crc32c_fp my_crc32c = my_crc32c_first;
28 
29 /* crc32c-slicing.c */
30 uint32_t my_crc32c_slicing(const uint8_t *, size_t);
31 
32 /* crc32c-sse42.c */
33 #if __GNUC__ >= 3 && defined(__x86_64__)
34 bool my_crc32c_sse42_supported(void);
35 uint32_t my_crc32c_sse42(const uint8_t *, size_t);
36 #endif
37 
38 #if defined(__GNUC__)
39 __attribute__((constructor))
40 #endif
41 static void
my_crc32c_runtime_detection(void)42 my_crc32c_runtime_detection(void)
43 {
44 #if __GNUC__ >= 3 && defined(__x86_64__)
45 	if (my_crc32c_sse42_supported()) {
46 		my_crc32c = my_crc32c_sse42;
47 	} else {
48 		my_crc32c = my_crc32c_slicing;
49 	}
50 #else
51 	my_crc32c = my_crc32c_slicing;
52 #endif
53 }
54 
55 static uint32_t
my_crc32c_first(const uint8_t * buf,size_t len)56 my_crc32c_first(const uint8_t *buf, size_t len) {
57 	my_crc32c_runtime_detection();
58 	return my_crc32c(buf, len);
59 }
60