comparison CMakeLists.txt @ 23:e26874655738

*: huge refactor, new major release (hahaha) I keep finding things that are broken... The problem NOW was that vec would unintentionally build some functions with extended instruction sets, which is Bad and would mean that for all intents and purposes the CPU detection was completely broken. Now vec is no longer header only either. Boohoo. However this gives a lot more flexibility to vec since we no longer want or need to care about C++ crap. The NEON and Altivec implementations have not been updated which means they won't compile hence why they're commented out in the cmake build file.
author Paper <paper@tflc.us>
date Sun, 24 Nov 2024 02:52:40 -0500
parents 41dd962abdd1
children 92156fe32755
comparison
equal deleted inserted replaced
22:fbcd3fa6f8fc 23:e26874655738
1 cmake_minimum_required(VERSION 3.23) 1 cmake_minimum_required(VERSION 3.23)
2 2
3 project(vec VERSION 2.0.0 DESCRIPTION "a tiny C99 SIMD vector library") 3 project(vec VERSION 3.0.0 DESCRIPTION "a tiny C99 SIMD vector library")
4 4
5 add_library(vec SHARED src/vec.c) 5 add_library(vec SHARED "src/vec.c;src/cpu.c;src/impl/generic.c;src/impl/fallback.c")
6
7 target_sources(vec PUBLIC
8 $<INSTALL_INTERFACE:vec/vec.h>
9 $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include/vec/vec.h>
10 $<INSTALL_INTERFACE:vec/impl/integer.h>
11 $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include/vec/impl/integer.h>
12 )
13 6
14 include(CheckCCompilerFlag) 7 include(CheckCCompilerFlag)
15 8
16 if(MSVC) 9 if(MSVC)
17 # TODO ? 10 # Untested!
18 else() 11
19 check_c_compiler_flag("-maltivec" COMPILER_HAS_ALTIVEC) 12 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
20 if(COMPILER_HAS_ALTIVEC) 13 set(COMPILER_HAS_MMX OFF)
21 target_compile_options(vec PRIVATE "-maltivec") 14 else()
22 endif() 15 set(COMPILER_HAS_MMX ON)
16 set(COMPILER_MMX_FLAGS "") # none?
17 endif()
18 check_c_compiler_flag("/arch:SSE2" COMPILER_HAS_SSE2)
19 if(COMPILER_HAS_SSE2)
20 set(COMPILER_SSE2_FLAGS "/arch:SSE2")
21 endif()
22 check_c_compiler_flag("/arch:SSE4.2" COMPILER_HAS_SSE41)
23 if(COMPILER_HAS_SSE41)
24 set(COMPILER_SSE41_FLAGS "/arch:SSE4.2")
25 endif()
26 check_c_compiler_flag("/arch:AVX2" COMPILER_HAS_AVX2)
27 if(COMPILER_HAS_AVX2)
28 set(COMPILER_AVX2_FLAGS "/arch:AVX2")
29 endif()
30 check_c_compiler_flag("/arch:AVX512" COMPILER_HAS_AVX512F)
31 if(COMPILER_HAS_AVX512F)
32 set(COMPILER_AVX512F_FLAGS "/arch:AVX512")
33 endif()
34 # TODO we have to try_compile to detect NEON
35 else()
36 #check_c_compiler_flag("-maltivec" COMPILER_HAS_ALTIVEC)
37 #if(COMPILER_HAS_ALTIVEC)
38 # set(COMPILER_ALTIVEC_FLAGS "-maltivec")
39 #endif()
40 #check_c_compiler_flag("-mfpu=neon" COMPILER_HAS_NEON)
41 #if(COMPILER_HAS_NEON)
42 # set(COMPILER_NEON_FLAGS "-mfpu=neon")
43 #endif()
23 check_c_compiler_flag("-mmmx" COMPILER_HAS_MMX) 44 check_c_compiler_flag("-mmmx" COMPILER_HAS_MMX)
24 if(COMPILER_HAS_MMX) 45 if(COMPILER_HAS_MMX)
25 target_compile_options(vec PRIVATE "-mmmx") 46 set(COMPILER_MMX_FLAGS "-mmmx")
26 endif() 47 endif()
27 check_c_compiler_flag("-msse2" COMPILER_HAS_SSE2) 48 check_c_compiler_flag("-msse2" COMPILER_HAS_SSE2)
28 if(COMPILER_HAS_SSE2) 49 if(COMPILER_HAS_SSE2)
29 target_compile_options(vec PRIVATE "-msse2") 50 set(COMPILER_SSE2_FLAGS "-msse2")
30 endif() 51 endif()
31 check_c_compiler_flag("-msse4.1" COMPILER_HAS_SSE41) 52 check_c_compiler_flag("-msse4.1" COMPILER_HAS_SSE41)
32 if(COMPILER_HAS_SSE41) 53 if(COMPILER_HAS_SSE41)
33 target_compile_options(vec PRIVATE "-msse4.1") 54 set(COMPILER_SSE41_FLAGS "-msse4.1")
34 endif() 55 endif()
35 check_c_compiler_flag("-mavx2" COMPILER_HAS_AVX2) 56 check_c_compiler_flag("-mavx2" COMPILER_HAS_AVX2)
36 if(COMPILER_HAS_AVX2) 57 if(COMPILER_HAS_AVX2)
37 target_compile_options(vec PRIVATE "-mavx2") 58 set(COMPILER_AVX2_FLAGS "-mavx2")
38 endif() 59 endif()
39 check_c_compiler_flag("-mavx512f" COMPILER_HAS_AVX512F) 60 check_c_compiler_flag("-mavx512f" COMPILER_HAS_AVX512F)
40 if(COMPILER_HAS_AVX512F) 61 if(COMPILER_HAS_AVX512F)
41 target_compile_options(vec PRIVATE "-mavx512f") 62 set(COMPILER_AVX512F_FLAGS "-mavx512f")
42 endif() 63 endif()
43 endif() 64 endif()
65
66 if(COMPILER_HAS_ALTIVEC)
67 target_sources(vec PRIVATE "src/impl/ppc/altivec.c")
68 set_source_files_properties("src/impl/ppc/altivec.c" PROPERTIES COMPILE_FLAGS "${COMPILER_ALTIVEC_FLAGS}")
69 target_compile_definitions(vec PRIVATE "-DVEC_COMPILER_HAS_ALTIVEC")
70 endif()
71
72 if(COMPILER_HAS_NEON)
73 target_sources(vec PRIVATE "src/impl/arm/neon.c")
74 set_source_files_properties("src/impl/arm/neon.c" PROPERTIES COMPILE_FLAGS "${COMPILER_NEON_FLAGS}")
75 target_compile_definitions(vec PRIVATE "-DVEC_COMPILER_HAS_NEON")
76 endif()
77
78 if(COMPILER_HAS_MMX)
79 target_sources(vec PRIVATE "src/impl/x86/mmx.c")
80 set_source_files_properties("src/impl/x86/mmx.c" PROPERTIES COMPILE_FLAGS "${COMPILER_MMX_FLAGS}")
81 target_compile_definitions(vec PRIVATE "-DVEC_COMPILER_HAS_MMX")
82 endif()
83
84 if(COMPILER_HAS_SSE2)
85 target_sources(vec PRIVATE "src/impl/x86/sse2.c")
86 set_source_files_properties("src/impl/x86/sse2.c" PROPERTIES COMPILE_FLAGS "${COMPILER_SSE2_FLAGS}")
87 target_compile_definitions(vec PRIVATE "-DVEC_COMPILER_HAS_SSE2")
88 endif()
89
90 if(COMPILER_HAS_SSE41)
91 target_sources(vec PRIVATE "src/impl/x86/sse41.c")
92 set_source_files_properties("src/impl/x86/sse41.c" PROPERTIES COMPILE_FLAGS "${COMPILER_SSE41_FLAGS}")
93 target_compile_definitions(vec PRIVATE "-DVEC_COMPILER_HAS_SSE41")
94 endif()
95
96 if(COMPILER_HAS_AVX2)
97 target_sources(vec PRIVATE "src/impl/x86/avx2.c")
98 set_source_files_properties("src/impl/x86/avx2.c" PROPERTIES COMPILE_FLAGS "${COMPILER_AVX2_FLAGS}")
99 target_compile_definitions(vec PRIVATE "-DVEC_COMPILER_HAS_AVX2")
100 endif()
101
102 if(COMPILER_HAS_AVX512F)
103 target_sources(vec PRIVATE "src/impl/x86/avx512f.c")
104 set_source_files_properties("src/impl/x86/avx512f.c" PROPERTIES COMPILE_FLAGS "${COMPILER_AVX512F_FLAGS}")
105 target_compile_definitions(vec PRIVATE "-DVEC_COMPILER_HAS_AVX512F")
106 endif()
107
44 108
45 ######################################################################### 109 #########################################################################
46 # integer types 110 # integer types; it's nice to accommodate for older broken systems that
111 # may not have stdint.h.
47 112
48 include(CheckTypeSize) 113 include(CheckTypeSize)
49 114
50 check_type_size("int16_t" INT16_T_SIZE LANGUAGE C) 115 check_type_size("int16_t" INT16_T_SIZE LANGUAGE C)
51 check_type_size("uint16_t" UINT16_T_SIZE LANGUAGE C) 116 check_type_size("uint16_t" UINT16_T_SIZE LANGUAGE C)
59 check_type_size("short" SHORT_SIZE LANGUAGE C) 124 check_type_size("short" SHORT_SIZE LANGUAGE C)
60 check_type_size("int" INT_SIZE LANGUAGE C) 125 check_type_size("int" INT_SIZE LANGUAGE C)
61 check_type_size("long" LONG_SIZE LANGUAGE C) 126 check_type_size("long" LONG_SIZE LANGUAGE C)
62 check_type_size("long long" LONG_LONG_SIZE LANGUAGE C) 127 check_type_size("long long" LONG_LONG_SIZE LANGUAGE C)
63 check_type_size("uintptr_t" UINTPTR_T_SIZE LANGUAGE C) 128 check_type_size("uintptr_t" UINTPTR_T_SIZE LANGUAGE C)
129 check_type_size("size_t" SIZE_T_SIZE LANGUAGE C)
64 130
65 if(INT16_T_SIZE EQUAL 2) 131 if(INT16_T_SIZE EQUAL 2)
66 set(SIZE16 "int16_t") 132 set(SIZE16 "int16_t")
67 elseif(SHORT_SIZE EQUAL 2) 133 elseif(SHORT_SIZE EQUAL 2)
68 set(SIZE16 "short") 134 set(SIZE16 "short")
69 elseif(INT_SIZE EQUAL 2) 135 elseif(INT_SIZE EQUAL 2)
70 set(SIZE16 "int") 136 set(SIZE16 "int")
137 else()
138 message(FATAL_ERROR "Failed to find a signed 16-bit integer type.")
71 endif() 139 endif()
72 140
73 if(UINT16_T_SIZE EQUAL 2) 141 if(UINT16_T_SIZE EQUAL 2)
74 set(USIZE16 "uint16_t") 142 set(USIZE16 "uint16_t")
75 elseif(U_INT16_T_SIZE EQUAL 2) 143 elseif(U_INT16_T_SIZE EQUAL 2)
76 set(USIZE16 "u_int16_t") 144 set(USIZE16 "u_int16_t")
77 elseif(SHORT_SIZE EQUAL 2) 145 elseif(SHORT_SIZE EQUAL 2)
78 set(USIZE16 "unsigned short") 146 set(USIZE16 "unsigned short")
79 elseif(INT_SIZE EQUAL 2) 147 elseif(INT_SIZE EQUAL 2)
80 set(USIZE16 "unsigned int") 148 set(USIZE16 "unsigned int")
149 else()
150 message(FATAL_ERROR "Failed to find an unsigned 16-bit integer type.")
81 endif() 151 endif()
82 152
83 if(INT32_T_SIZE EQUAL 4) 153 if(INT32_T_SIZE EQUAL 4)
84 set(SIZE32 "int32_t") 154 set(SIZE32 "int32_t")
85 elseif(SHORT_SIZE EQUAL 4) 155 elseif(SHORT_SIZE EQUAL 4)
86 set(SIZE32 "short") 156 set(SIZE32 "short")
87 elseif(INT_SIZE EQUAL 4) 157 elseif(INT_SIZE EQUAL 4)
88 set(SIZE32 "int") 158 set(SIZE32 "int")
89 elseif(LONG_SIZE EQUAL 4) 159 elseif(LONG_SIZE EQUAL 4)
90 set(SIZE32 "long") 160 set(SIZE32 "long")
161 else()
162 message(FATAL_ERROR "Failed to find a signed 32-bit integer type.")
91 endif() 163 endif()
92 164
93 if(UINT32_T_SIZE EQUAL 4) 165 if(UINT32_T_SIZE EQUAL 4)
94 set(USIZE32 "uint32_t") 166 set(USIZE32 "uint32_t")
95 elseif(U_INT32_T_SIZE EQUAL 4) 167 elseif(U_INT32_T_SIZE EQUAL 4)
98 set(USIZE32 "unsigned short") 170 set(USIZE32 "unsigned short")
99 elseif(INT_SIZE EQUAL 4) 171 elseif(INT_SIZE EQUAL 4)
100 set(USIZE32 "unsigned int") 172 set(USIZE32 "unsigned int")
101 elseif(LONG_SIZE EQUAL 4) 173 elseif(LONG_SIZE EQUAL 4)
102 set(USIZE32 "unsigned long") 174 set(USIZE32 "unsigned long")
175 else()
176 message(FATAL_ERROR "Failed to find an unsigned 32-bit integer type.")
103 endif() 177 endif()
104 178
105 if(INT64_T_SIZE EQUAL 8) 179 if(INT64_T_SIZE EQUAL 8)
106 set(SIZE64 "int64_t") 180 set(SIZE64 "int64_t")
107 elseif(SHORT_SIZE EQUAL 8) 181 elseif(SHORT_SIZE EQUAL 8)
110 set(SIZE64 "int") 184 set(SIZE64 "int")
111 elseif(LONG_SIZE EQUAL 8) 185 elseif(LONG_SIZE EQUAL 8)
112 set(SIZE64 "long") 186 set(SIZE64 "long")
113 elseif(LONG_LONG_SIZE EQUAL 8) 187 elseif(LONG_LONG_SIZE EQUAL 8)
114 set(SIZE64 "long long") 188 set(SIZE64 "long long")
189 else()
190 message(FATAL_ERROR "Failed to find a signed 64-bit integer type.")
115 endif() 191 endif()
116 192
117 if(UINT64_T_SIZE EQUAL 8) 193 if(UINT64_T_SIZE EQUAL 8)
118 set(USIZE64 "uint64_t") 194 set(USIZE64 "uint64_t")
119 elseif(U_INT64_T_SIZE EQUAL 8) 195 elseif(U_INT64_T_SIZE EQUAL 8)
124 set(USIZE64 "unsigned int") 200 set(USIZE64 "unsigned int")
125 elseif(LONG_SIZE EQUAL 8) 201 elseif(LONG_SIZE EQUAL 8)
126 set(USIZE64 "unsigned long") 202 set(USIZE64 "unsigned long")
127 elseif(LONG_LONG_SIZE EQUAL 8) 203 elseif(LONG_LONG_SIZE EQUAL 8)
128 set(USIZE64 "unsigned long long") 204 set(USIZE64 "unsigned long long")
205 else()
206 message(FATAL_ERROR "Failed to find an unsigned 64-bit integer type.")
129 endif() 207 endif()
130 208
131 if(CMAKE_SIZEOF_VOID_P EQUAL UINTPTR_T_SIZE) 209 if(CMAKE_SIZEOF_VOID_P EQUAL UINTPTR_T_SIZE)
132 set(USIZEPTR "uintptr_t") 210 set(USIZEPTR "uintptr_t")
133 elseif(CMAKE_SIZEOF_VOID_P EQUAL 1) 211 elseif(CMAKE_SIZEOF_VOID_P LESS_EQUAL 1)
134 set(USIZEPTR "unsigned char") 212 set(USIZEPTR "unsigned char")
135 elseif(CMAKE_SIZEOF_VOID_P EQUAL 2) 213 elseif(CMAKE_SIZEOF_VOID_P LESS_EQUAL 2)
136 set(USIZEPTR "${USIZE16}") 214 set(USIZEPTR "${USIZE16}")
137 elseif(CMAKE_SIZEOF_VOID_P EQUAL 4) 215 elseif(CMAKE_SIZEOF_VOID_P LESS_EQUAL 4)
138 set(USIZEPTR "${USIZE32}") 216 set(USIZEPTR "${USIZE32}")
139 elseif(CMAKE_SIZEOF_VOID_P EQUAL 8) 217 elseif(CMAKE_SIZEOF_VOID_P LESS_EQUAL 8)
140 set(USIZEPTR "${USIZE64}") 218 set(USIZEPTR "${USIZE64}")
141 endif() 219 else()
142 220 message(FATAL_ERROR "Failed to find an integer type that can fit a pointer.")
143 configure_file(include/vec/impl/integer.h.in include/vec/impl/integer.h @ONLY) 221 endif()
144 222
145 target_compile_definitions(vec PRIVATE "VEC_HAVE_IMPL_INTEGER_H") 223 if(NOT SIZE_T_SIZE EQUAL 0 AND NOT SIZE_T_SIZE EQUAL "")
224 set(USIZESIZE "size_t")
225 else()
226 # should be close enough I guess
227 set(USIZESIZE "${USIZEPTR}")
228 endif()
229
230 configure_file(include/vec/types.h.in include/vec/types.h @ONLY)
146 231
147 ######################################################################### 232 #########################################################################
148 233
234 target_sources(vec PUBLIC
235 $<INSTALL_INTERFACE:vec/vec.h>
236 $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include/vec/vec.h>
237 $<INSTALL_INTERFACE:vec/types.h>
238 $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include/vec/types.h>
239 $<INSTALL_INTERFACE:vec/cpu.h>
240 $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include/vec/cpu.h>
241 )
242
149 target_compile_features(vec PRIVATE $<IF:$<COMPILE_FEATURES:c_std_11>,c_std_11,c_std_99>) 243 target_compile_features(vec PRIVATE $<IF:$<COMPILE_FEATURES:c_std_11>,c_std_11,c_std_99>)
150 target_include_directories(vec PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include;${CMAKE_CURRENT_BINARY_DIR}/include/vec") 244 target_include_directories(vec PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include;${CMAKE_CURRENT_BINARY_DIR}/include")
151 245
152 # Installing 246 # Installing
153 247
154 include(GNUInstallDirs) 248 include(GNUInstallDirs)
155 249