Debian Codegraph @

crt/aws-c-common/cmake/AwsSIMD.cmake

raw
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0.
3
4 include(CheckCCompilerFlag)
5 include(CheckIncludeFile)
6
7 if (MSVC)
8 set(AWS_AVX2_FLAG "/arch:AVX2")
9 set(AWS_AVX512_FLAG "/arch:AVX512")
10 set(AWS_AVX512vL_FLAG "")
11 set(AWS_CLMUL_FLAG "")
12 set(AWS_SSE4_2_FLAG "")
13 set(AWS_ARMv8_1_FLAG "/arch:arm8.1")
14 set(WERROR_FLAG "")
15 else()
16 set(AWS_AVX2_FLAG "-mavx -mavx2")
17 set(AWS_AVX512_FLAG "-mavx512f -mvpclmulqdq")
18 set(AWS_AVX512vL_FLAG "-mavx512vl")
19 set(AWS_CLMUL_FLAG "-mpclmul")
20 set(AWS_SSE4_2_FLAG "-msse4.2")
21
22 # AWS Graviton3 processors use neoverse-v1
23 check_c_compiler_flag("-mtune=neoverse-v1" HAVE_MTUNE_NEOVERSE_V1)
24 if (HAVE_MTUNE_NEOVERSE_V1)
25 set(AWS_ARMv8_1_FLAG "-march=armv8-a+crc+crypto -mtune=neoverse-v1")
26 else()
27 set(AWS_ARMv8_1_FLAG "-march=armv8-a+crc+crypto")
28 endif()
29 set(WERROR_FLAG "-Werror")
30 endif()
31
32 if (USE_CPU_EXTENSIONS)
33 set(AVX_CFLAGS ${AWS_SSE4_2_FLAG})
34
35 check_c_compiler_flag(${AWS_AVX2_FLAG} HAVE_M_AVX2_FLAG)
36 if (HAVE_M_AVX2_FLAG)
37 set(AVX_CFLAGS "${AWS_AVX2_FLAG} ${AVX_CFLAGS}")
38 endif()
39
40 check_c_compiler_flag("${AWS_AVX512_FLAG} ${AWS_CLMUL_FLAG}" HAVE_M_AVX512_FLAG)
41 if (HAVE_M_AVX512_FLAG)
42 set(AVX_CFLAGS "${AWS_AVX512_FLAG} ${AWS_CLMUL_FLAG} ${AVX_CFLAGS}")
43 endif()
44
45 set(old_flags "${CMAKE_REQUIRED_FLAGS}")
46 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${AVX_CFLAGS} ${WERROR_FLAG}")
47
48 check_c_source_compiles("
49 #include <immintrin.h>
50 #include <emmintrin.h>
51 #include <string.h>
52
53 int main() {
54 __m256i vec;
55 memset(&vec, 0, sizeof(vec));
56
57 _mm256_shuffle_epi8(vec, vec);
58 _mm256_set_epi32(1,2,3,4,5,6,7,8);
59 _mm256_permutevar8x32_epi32(vec, vec);
60
61 return 0;
62 }" AWS_HAVE_AVX2_INTRINSICS)
63
64 check_c_source_compiles("
65 #include <immintrin.h>
66
67 int main() {
68 __m512 a = _mm512_setzero_ps();
69 return 0;
70 }" AWS_HAVE_AVX512_INTRINSICS)
71
72 check_c_source_compiles("
73 #include <immintrin.h>
74 #include <string.h>
75
76 int main() {
77 __m256i vec;
78 memset(&vec, 0, sizeof(vec));
79 return (int)_mm256_extract_epi64(vec, 2);
80 }" AWS_HAVE_MM256_EXTRACT_EPI64)
81
82 check_c_source_compiles("
83 #include <wmmintrin.h>
84 #include <emmintrin.h>
85 int main() {
86 __m128i a = _mm_setzero_si128();
87 __m128i b = _mm_setzero_si128();
88 __m128i result = _mm_clmulepi64_si128(a, b, 0x00);
89 (void)result;
90 return 0;
91 }" AWS_HAVE_CLMUL)
92
93 set(CMAKE_REQUIRED_FLAGS "${old_flags} ${AWS_ARMv8_1_FLAG} ${WERROR_FLAG}")
94 check_c_source_compiles("
95 #include <arm_acle.h>
96 int main() {
97 int crc = __crc32d(0, 1);
98 return 0;
99 }" AWS_HAVE_ARM32_CRC)
100
101 check_c_source_compiles("
102 #include <stdatomic.h>
103 int main() {
104 _Atomic int var = 0;
105 atomic_fetch_add_explicit(&var, 1, memory_order_relaxed);
106 return 0;
107 }" AWS_HAVE_ARMv8_1)
108
109 set(CMAKE_REQUIRED_FLAGS "${old_flags}")
110
111 endif() # USE_CPU_EXTENSIONS
112
113 # The part where the definition is added to the compiler flags has been moved to config.h.in
114 # see git history for more details.
115
116 # Adds AVX flags, if any, that are supported. These files will be built with
117 # available avx intrinsics enabled.
118 # Usage: simd_add_source_avx(target file1.c file2.c ...)
119 function(simd_add_source_avx target)
120 foreach(file ${ARGN})
121 target_sources(${target} PRIVATE ${file})
122 set_source_files_properties(${file} PROPERTIES COMPILE_FLAGS " ${AVX_CFLAGS}")
123 endforeach()
124 endfunction(simd_add_source_avx)
125
126 # The part where the definition is added to the compiler flags has been moved to config.h.in
127 # see git history for more details.
128
129 # Adds compiler flags to the source and adds the source to target.
130 # Unfortunately the flags have to be passed as strings. Predefined flags are
131 # at the top of this file.
132 # Usage: simd_append_source_and_features(target file1.c ${AWS_AVX512_FLAG} ${AWS_AVX2_FLAG} ...)
133 function(simd_append_source_and_features target file)
134 set(CC_FLAGS "")
135 foreach(flag ${ARGN})
136 set(CC_FLAGS "${CC_FLAGS} ${flag}")
137 endforeach()
138
139 target_sources(${target} PRIVATE ${file})
140 set_source_files_properties(${file} PROPERTIES COMPILE_FLAGS " ${CC_FLAGS}")
141 endfunction(simd_append_source_and_features)