|
| 1 | +# MIT License |
| 2 | +# |
| 3 | +# Copyright (c) 2023 RealTimeChris |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 6 | +# software and associated documentation files (the "Software"), to deal in the Software |
| 7 | +# without restriction, including without limitation the rights to use, copy, modify, merge, |
| 8 | +# publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
| 9 | +# persons to whom the Software is furnished to do so, subject to the following conditions: |
| 10 | +# |
| 11 | +# The above copyright notice and this permission notice shall be included in all copies or |
| 12 | +# substantial portions of the Software. |
| 13 | +# |
| 14 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 15 | +# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 16 | +# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 17 | +# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 18 | +# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 19 | +# DEALINGS IN THE SOFTWARE. |
| 20 | +# |
| 21 | +# CMakeLists.txt - The CMake script for building this library. |
| 22 | +# Dec 17, 2022 |
| 23 | +# https://github.com/RealTimeChris/Jsonifier |
| 24 | +include(CheckCXXSourceRuns) |
| 25 | + |
| 26 | +function(check_instruction_set INSTRUCTION_SET_NAME INSTRUCTION_SET_FLAG INSTRUCTION_SET_INTRINSIC) |
| 27 | + |
| 28 | + set(INSTRUCTION_SET_CODE " |
| 29 | + #include <immintrin.h> |
| 30 | + #include <stdint.h> |
| 31 | + int main() |
| 32 | + { |
| 33 | + ${INSTRUCTION_SET_INTRINSIC}; |
| 34 | + return 0; |
| 35 | + } |
| 36 | + ") |
| 37 | + |
| 38 | + set(CMAKE_REQUIRED_FLAGS "${INSTRUCTION_SET_FLAG}") |
| 39 | + CHECK_CXX_SOURCE_RUNS("${INSTRUCTION_SET_CODE}" "${INSTRUCTION_SET_NAME}") |
| 40 | + if(${INSTRUCTION_SET_NAME}) |
| 41 | + set(AVX_FLAG "${INSTRUCTION_SET_FLAG}" PARENT_SCOPE) |
| 42 | + set(AVX_NAME "${INSTRUCTION_SET_NAME}" PARENT_SCOPE) |
| 43 | + else() |
| 44 | + message(STATUS "Instruction set ${INSTRUCTION_SET_NAME} not supported. Falling back to the previous instruction set.") |
| 45 | + return() |
| 46 | + endif() |
| 47 | +endfunction() |
| 48 | + |
| 49 | +if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") |
| 50 | + set(INSTRUCTION_SETS |
| 51 | + "T_AVX?/arch:AVX?auto result = _mm_testz_ps(__m128{}, __m128{})" |
| 52 | + "T_AVX2?/arch:AVX2?auto result = _mm256_abs_epi16(__m256i{})" |
| 53 | + "T_AVX512?/arch:AVX512?auto result = _mm512_add_ps(__m512i{}, __m512i{}).auto result2 = _mm512_cmplt_epu8_mask(__m512i{}, __m512i{}).char newArray[64]{}.auto result = _mm_loadu_epi64(newArray)" |
| 54 | + ) |
| 55 | +else() |
| 56 | + set(INSTRUCTION_SETS |
| 57 | + "T_AVX?-mavx.-mpclmul.-mbmi?auto result = _mm_testz_ps(__m128{}, __m128{})" |
| 58 | + "T_AVX2?-mavx2.-mpclmul.-mbmi?auto result = _mm256_abs_epi16(__m256i{})" |
| 59 | + "T_AVX512?-mavx512bw.-mavx512vl.-mavx512f.-mpclmul.-mbmi?auto result = _mm512_add_ps(__m512i{}, __m512i{}).auto result2 = _mm512_cmplt_epu8_mask(__m512i{}, __m512i{}).char newArray[64]{}.auto result = _mm_loadu_epi64(newArray)" |
| 60 | +) |
| 61 | +endif() |
| 62 | + |
| 63 | +set(CMAKE_REQUIRED_FLAGS_SAVE "${CMAKE_REQUIRED_FLAGS}") |
| 64 | + |
| 65 | +set(AVX_NAME "T_Fallback") |
| 66 | + |
| 67 | +foreach(INSTRUCTION_SET IN LISTS INSTRUCTION_SETS) |
| 68 | + string(REPLACE "?" ";" CURRENT_LIST "${INSTRUCTION_SET}") |
| 69 | + list(GET CURRENT_LIST 0 INSTRUCTION_SET_NAME) |
| 70 | + list(GET CURRENT_LIST 1 INSTRUCTION_SET_FLAG) |
| 71 | + string(REPLACE "." ";" INSTRUCTION_SET_FLAG "${INSTRUCTION_SET_FLAG}") |
| 72 | + list(GET CURRENT_LIST 2 INSTRUCTION_SET_INTRINSIC) |
| 73 | + string(REPLACE "." ";" INSTRUCTION_SET_INTRINSIC "${INSTRUCTION_SET_INTRINSIC}") |
| 74 | + check_instruction_set("${INSTRUCTION_SET_NAME}" "${INSTRUCTION_SET_FLAG}" "${INSTRUCTION_SET_INTRINSIC}") |
| 75 | +endforeach() |
| 76 | + |
| 77 | +message(STATUS "Detected CPU Architecture: ${AVX_NAME}") |
| 78 | +set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS_SAVE}") |
0 commit comments