Skip to content

Commit

Permalink
DiscordCoreAPI Release 2.0.7
Browse files Browse the repository at this point in the history
* Updated to keep in track with Jsonifier dependency.
* Added a detect-cpu-architecture script.
  • Loading branch information
RealTimeChris committed Aug 20, 2024
1 parent 974d245 commit 2183ae1
Show file tree
Hide file tree
Showing 40 changed files with 796 additions and 449 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Build-and-Test-CLANG-Ubuntu

on:
workflow_dispatch:
push:
branches:
- main
Expand All @@ -13,41 +14,42 @@ jobs:
strategy:
fail-fast: false
matrix:
clang: [18]
clang: [19]
build_type: [Debug, Release]
std: [20]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install the latest Clang compiler.
working-directory: ./
working-directory: Tests
run: |
sudo apt update && sudo apt upgrade
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 18
chmod u+x llvm.sh
sudo ./llvm.sh 19
- name: Update Vcpkg and install other dependencies.
run: |
sudo apt-get update --fix-missing
sudo apt-get upgrade --fix-missing
run: |
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install nasm
cd /usr/local/share/vcpkg
./bootstrap-vcpkg.sh
git stash
git pull
vcpkg update
/usr/local/share/vcpkg/vcpkg update
- name: Set TERM environment variable
run: echo "export TERM=xterm" >> $GITHUB_ENV

- name: Install the dependencies.
working-directory: ./
run: |
vcpkg install opus:x64-linux
vcpkg install libsodium:x64-linux
vcpkg install openssl:x64-linux
/usr/local/share/vcpkg/vcpkg install opus:x64-linux
/usr/local/share/vcpkg/vcpkg install libsodium:x64-linux
/usr/local/share/vcpkg/vcpkg install openssl:x64-linux
- name: Configure CMake
working-directory: ./
run: |
cmake -S . -B ./Build -DCMAKE_CXX_COMPILER=/usr/bin/clang++-18 -DDEV=true -DDISCORDCOREAPI_TEST=true -DVCPKG_ROOT_DIR=/usr/local/share/vcpkg
cmake -S . -B ./Build -DCMAKE_CXX_COMPILER=/usr/bin/clang++-19 -DDEV=true -DDISCORDCOREAPI_TEST=true -DVCPKG_ROOT_DIR="/usr/local/share/vcpkg/"
- name: Build the Test
working-directory: ./Build
Expand All @@ -57,6 +59,6 @@ jobs:
- name: Run the Test
working-directory: ./Build/Tests
run: |
chmod u+x ./DiscordCoreAPITest
sudo chmod u+x ./DiscordCoreAPITest
./DiscordCoreAPITest
continue-on-error: true
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Build-and-Test-GCC-MacOS

on:
workflow_dispatch:
push:
branches:
- main
Expand All @@ -13,26 +14,26 @@ jobs:
strategy:
fail-fast: false
matrix:
gcc: [13]
gnucxx: [14]
build_type: [Debug, Release]
std: [20]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install the latest g++ compiler.
- name: Install the latest GnuCXX compiler.
run: |
brew install gcc
brew install gcc --force-bottle
- name: Update Vcpkg and install other dependencies.
run: |
brew update
run: |
brew update
brew upgrade
brew install nasm
brew install nano
brew install autoconf
brew install automake
brew install glibtoolize
brew install libtool
which autoconf
export PATH="/opt/homebrew/bin/autoconf:$PATH"
git clone https://github.com/Microsoft/Vcpkg ./vcpkg2
Expand All @@ -42,25 +43,28 @@ jobs:
git pull
./vcpkg update
- name: Set TERM environment variable
run: echo "export TERM=xterm" >> $GITHUB_ENV

- name: Install the dependencies.
working-directory: /Users/runner/work/DiscordCoreAPI/DiscordCoreAPI/vcpkg2/
run: |
/Users/runner/work/DiscordCoreAPI/DiscordCoreAPI/vcpkg2/vcpkg install opus:x64-osx
/Users/runner/work/DiscordCoreAPI/DiscordCoreAPI/vcpkg2/vcpkg install libsodium:x64-osx
/Users/runner/work/DiscordCoreAPI/DiscordCoreAPI/vcpkg2/vcpkg install openssl:x64-osx
which g++
./vcpkg2/vcpkg install opus:x64-osx
./vcpkg2/vcpkg install libsodium:x64-osx
./vcpkg2/vcpkg install openssl:x64-osx
- name: Print failure log on failure
if: ${{ failure() }}
run: cat /Users/runner/work/DiscordCoreAPI/DiscordCoreAPI/vcpkg2/buildtrees/libsodium/autoconf-x64-osx-err.log
- name: Check if file exists (for debugging)
if: failure()
run: |
if [ -f /Users/runner/work/DiscordCoreAPI/DiscordCoreAPI/vcpkg2/buildtrees/libsodium/autoconf-x64-osx-err.log ]; then echo "File exists"; else echo "File does not exist"; fi
- name: Print file contents upon failure
if: failure()
run: |
echo "Command failed. Printing /Users/runner/work/DiscordCoreAPI/DiscordCoreAPI/vcpkg2/buildtrees/libsodium/autoconf-x64-osx-err.log contents:"
cat /Users/runner/work/DiscordCoreAPI/DiscordCoreAPI/vcpkg2/buildtrees/libsodium/autoconf-x64-osx-err.log || echo "File not found or empty"
- name: Configure CMake
working-directory: ./
run: |
cmake -S . -B ./Build -DCMAKE_CXX_COMPILER=/usr/local/opt/gcc@13/bin/g++-13 -DDEV=true -DDISCORDCOREAPI_TEST=true -DVCPKG_ROOT_DIR=./vcpkg2
cmake -S . -B ./Build -DCMAKE_CXX_COMPILER=/usr/bin/g++-14 -DDEV=true -DDISCORDCOREAPI_TEST=true -DVCPKG_ROOT_DIR=/usr/local/share/vcpkg
- name: Build the Test
working-directory: ./Build
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Build-and-Test-MSVC-Windows

on:
workflow_dispatch:
push:
branches:
- main
Expand All @@ -18,8 +19,8 @@ jobs:
std: [20]

steps:
- uses: actions/checkout@v3

- uses: actions/checkout@v4
- name: Update Vcpkg and install other dependencies.
run: |
cd C:/vcpkg
Expand Down
2 changes: 2 additions & 0 deletions CMake/BuildFeatureTester.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin/cmake.exe" -S ./ -B ./Build -DCMAKE_BUILD_TYPE=Release
"C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/bin/cmake.exe" --build ./Build --config=Release
3 changes: 3 additions & 0 deletions CMake/BuildFeatureTester.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
cmake -S ./ -B ./Build -DCMAKE_BUILD_TYPE=Release
cmake --build ./Build --config=Release
5 changes: 5 additions & 0 deletions CMake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 3.10)
project(FeatureDetection LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
# Add your source file(s)
add_executable(feature_detector main.cpp)
182 changes: 83 additions & 99 deletions CMake/DCADetectArchitecture.cmake
Original file line number Diff line number Diff line change
@@ -1,120 +1,104 @@
# MIT License
#
# Copyright (c) 2023 RealTimeChris
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# DCADetectArchitecture.cmake - Script for detecting the CPU architecture.
# Sept 18, 2023
# JsonifierDetectArchitecture.cmake - Script for detecting the CPU architecture.
# MIT License
# Copyright (c) 2023 RealTimeChris
# https://discordcoreapi.com
include(CheckCXXSourceRuns)

function(jsonifier_check_instruction_set INSTRUCTION_SET_NAME INSTRUCTION_SET_IN_FLAG INSTRUCTION_SET_OUT_FLAG INSTRUCTION_SET_INTRINSIC)
set(INSTRUCTION_SET_CODE "
#include <immintrin.h>
#include <stdint.h>
int32_t main()
{
${INSTRUCTION_SET_INTRINSIC};
return 0;
}")

set(CMAKE_REQUIRED_FLAGS "${INSTRUCTION_SET_IN_FLAG}")
set(CHECK_RESULT_VAR "${INSTRUCTION_SET_NAME}")
CHECK_CXX_SOURCE_RUNS("${INSTRUCTION_SET_CODE}" "DiscordCoreAPI-${CHECK_RESULT_VAR}")
if(DiscordCoreAPI-${CHECK_RESULT_VAR})
set(${INSTRUCTION_SET_NAME} "${INSTRUCTION_SET_OUT_FLAG}" PARENT_SCOPE)
list(APPEND JSONIFIER_CPU_INSTRUCTIONS "${INSTRUCTION_SET_NAME}")
string(REPLACE ";" "," JSONIFIER_CPU_INSTRUCTIONS "${JSONIFIER_CPU_INSTRUCTIONS}")
set(JSONIFIER_CPU_INSTRUCTIONS "${JSONIFIER_CPU_INSTRUCTIONS}" PARENT_SCOPE)
else()
message(STATUS "Instruction set ${INSTRUCTION_SET_NAME} not supported.")
return()
if (UNIX OR APPLE)
file(WRITE "${CMAKE_SOURCE_DIR}/CMake/BuildFeatureTester.sh" "#!/bin/bash
\"${CMAKE_COMMAND}\" -S ./ -B ./Build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
\"${CMAKE_COMMAND}\" --build ./Build --config=Release")
execute_process(
COMMAND chmod +x "${CMAKE_SOURCE_DIR}/CMake/BuildFeatureTester.sh"
RESULT_VARIABLE CHMOD_RESULT
)
if(NOT ${CHMOD_RESULT} EQUAL 0)
message(FATAL_ERROR "Failed to set executable permissions for BuildFeatureTester.sh")
endif()
endfunction()

set(INSTRUCTION_SET_NAMES "POPCNT" "LZCNT" "BMI" "BMI2" "AVX" "AVX2" "AVX512")
set(INSTRUCTION_SET_CODE
"auto result = _mm_popcnt_u64(uint64_t{})"
"auto result = _lzcnt_u64(int64_t{})"
"auto result = _blsr_u64(uint64_t{}).result = _tzcnt_u32(uint16_t{})"
"auto result = _pdep_u64(uint64_t{}, uint64_t{})"
"auto result = _mm_castsi128_pd(__m128i{}).auto result01 = _mm_setzero_si128()"
"auto result = _mm256_add_epi32(__m256i{}, __m256i{})"
"auto result = _mm_abs_epi64 (__m128i{}).auto result01 = _mm512_abs_epi16(__m512i{})"
)

set(INDEX_SET "0" "1" "2" "3" "4" "5" "6")

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set(INSTRUCTION_SET_IN_FLAGS "/arch:AVX" "/arch:AVX" "/arch:AVX" "/arch:AVX" "/arch:AVX" "/arch:AVX2" "/arch:AVX512")
set(INSTRUCTION_SET_OUT_FLAGS "/arch:AVX" "/arch:AVX" "/arch:AVX" "/arch:AVX" "/arch:AVX" "/arch:AVX2" "/arch:AVX512")
else()
set(INSTRUCTION_SET_IN_FLAGS "-march=native" "-march=native" "-march=native" "-march=native" "-march=native" "-march=native" "-march=native")
set(INSTRUCTION_SET_OUT_FLAGS "-mpopcnt" "-mlzcnt" "-mbmi" "-mbmi2" "-mavx.-mpclmul" "-mavx2.-mpclmul" "-mavx512f.-mavx512bw.-mavx512vl.-mpclmul")
execute_process(
COMMAND "${CMAKE_SOURCE_DIR}/CMake/BuildFeatureTester.sh"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/CMake/"
)
set(FEATURE_TESTER_FILE "${CMAKE_SOURCE_DIR}/CMake/Build/feature_detector")
elseif(WIN32)
file(WRITE "${CMAKE_SOURCE_DIR}/CMake/BuildFeatureTester.bat" "\"${CMAKE_COMMAND}\" -S ./ -B ./Build -DCMAKE_BUILD_TYPE=Release
\"${CMAKE_COMMAND}\" --build ./Build --config=Release")
execute_process(
COMMAND "${CMAKE_SOURCE_DIR}/CMake/BuildFeatureTester.bat"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/CMake/"
)
set(FEATURE_TESTER_FILE "${CMAKE_SOURCE_DIR}/CMake/Build/Release/feature_detector.exe")
endif()

set(CMAKE_REQUIRED_FLAGS_SAVE "${CMAKE_REQUIRED_FLAGS}")
execute_process(
COMMAND "${FEATURE_TESTER_FILE}"
RESULT_VARIABLE DCA_CPU_INSTRUCTIONS_NEW
)

if(("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64") OR ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i386") OR ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64"))
set(AVX_FLAG "")

foreach(CURRENT_INDEX IN LISTS INDEX_SET)
list(GET INSTRUCTION_SET_NAMES "${CURRENT_INDEX}" INSTRUCTION_SET_NAME)
list(GET INSTRUCTION_SET_CODE "${CURRENT_INDEX}" INSTRUCTION_SET_INTRINSIC)
list(GET INSTRUCTION_SET_IN_FLAGS "${CURRENT_INDEX}" INSTRUCTION_SET_IN_FLAG)
list(GET INSTRUCTION_SET_OUT_FLAGS "${CURRENT_INDEX}" INSTRUCTION_SET_OUT_FLAG)
string(REPLACE "." ";" INSTRUCTION_SET_OUT_FLAG "${INSTRUCTION_SET_OUT_FLAG}")
string(REPLACE "." ";" INSTRUCTION_SET_INTRINSIC "${INSTRUCTION_SET_INTRINSIC}")
jsonifier_check_instruction_set("${INSTRUCTION_SET_NAME}" "${INSTRUCTION_SET_IN_FLAG}" "${INSTRUCTION_SET_OUT_FLAG}" "${INSTRUCTION_SET_INTRINSIC}")
endforeach()
math(EXPR DCA_CPU_INSTRUCTIONS_NUMERIC "${DCA_CPU_INSTRUCTIONS_NEW}")
math(EXPR JSONIFIER_CPU_INSTRUCTIONS 0)

message(STATUS "Detected CPU Architecture: ${JSONIFIER_CPU_INSTRUCTIONS}")
else()
message(STATUS "SSE not supported by architecture ${CMAKE_SYSTEM_PROCESSOR}")
endif()

set(AVX_FLAG)
set(JSONIFIER_CPU_INSTRUCTIONS 0)
function(check_instruction_set INSTRUCTION_SET_NAME INSTRUCTION_SET_FLAG INSTRUCTION_SET_NUMERIC_VALUE)
math(EXPR INSTRUCTION_PRESENT "( ${DCA_CPU_INSTRUCTIONS_NUMERIC} & ${INSTRUCTION_SET_NUMERIC_VALUE} )")
if(INSTRUCTION_PRESENT)
message(STATUS "Instruction Set Found: ${INSTRUCTION_SET_NAME}")
math(EXPR JSONIFIER_CPU_INSTRUCTIONS "( ${JSONIFIER_CPU_INSTRUCTIONS} | ${INSTRUCTION_SET_NUMERIC_VALUE} )")
set(AVX_FLAG "${AVX_FLAG};${INSTRUCTION_SET_FLAG}" PARENT_SCOPE)
endif()
endfunction()

if(NOT "${POPCNT}" STREQUAL "")
list(APPEND AVX_FLAG "${POPCNT}")
math(EXPR INSTRUCTION_PRESENT "( ${DCA_CPU_INSTRUCTIONS_NUMERIC} & 0x1 )")
if(INSTRUCTION_PRESENT)
math(EXPR JSONIFIER_CPU_INSTRUCTIONS "${JSONIFIER_CPU_INSTRUCTIONS} | 1 << 0" OUTPUT_FORMAT DECIMAL)
endif()
if(NOT "${LZCNT}" STREQUAL "")
list(APPEND AVX_FLAG "${LZCNT}")
math(EXPR INSTRUCTION_PRESENT "( ${DCA_CPU_INSTRUCTIONS_NUMERIC} & 0x2 )")
if(INSTRUCTION_PRESENT)
math(EXPR JSONIFIER_CPU_INSTRUCTIONS "${JSONIFIER_CPU_INSTRUCTIONS} | 1 << 1" OUTPUT_FORMAT DECIMAL)
endif()
if(NOT "${BMI}" STREQUAL "")
list(APPEND AVX_FLAG "${BMI}")
math(EXPR INSTRUCTION_PRESENT "( ${DCA_CPU_INSTRUCTIONS_NUMERIC} & 0x4 )")
if(INSTRUCTION_PRESENT)
math(EXPR JSONIFIER_CPU_INSTRUCTIONS "${JSONIFIER_CPU_INSTRUCTIONS} | 1 << 2" OUTPUT_FORMAT DECIMAL)
endif()
if(NOT "${BMI2}" STREQUAL "")
list(APPEND AVX_FLAG "${BMI2}")
math(EXPR INSTRUCTION_PRESENT "( ${DCA_CPU_INSTRUCTIONS_NUMERIC} & 0x8 )")
if(INSTRUCTION_PRESENT)
math(EXPR JSONIFIER_CPU_INSTRUCTIONS "${JSONIFIER_CPU_INSTRUCTIONS} | 1 << 3" OUTPUT_FORMAT DECIMAL)
endif()
if(NOT "${AVX512}" STREQUAL "")
list(APPEND AVX_FLAG "${AVX512}")

math(EXPR INSTRUCTION_PRESENT "( ${DCA_CPU_INSTRUCTIONS_NUMERIC} & 0x10 )")
if(INSTRUCTION_PRESENT)
math(EXPR JSONIFIER_CPU_INSTRUCTIONS "${JSONIFIER_CPU_INSTRUCTIONS} | 1 << 4" OUTPUT_FORMAT DECIMAL)
endif()
math(EXPR INSTRUCTION_PRESENT128 "( ${DCA_CPU_INSTRUCTIONS_NUMERIC} & 0x20 )")
math(EXPR INSTRUCTION_PRESENT256 "( ${DCA_CPU_INSTRUCTIONS_NUMERIC} & 0x40 )")
math(EXPR INSTRUCTION_PRESENT512 "( ${DCA_CPU_INSTRUCTIONS_NUMERIC} & 0x80 )")
if(INSTRUCTION_PRESENT512)
math(EXPR JSONIFIER_CPU_INSTRUCTIONS "${JSONIFIER_CPU_INSTRUCTIONS} | 1 << 7" OUTPUT_FORMAT DECIMAL)
elseif(INSTRUCTION_PRESENT256)
math(EXPR JSONIFIER_CPU_INSTRUCTIONS "${JSONIFIER_CPU_INSTRUCTIONS} | 1 << 6" OUTPUT_FORMAT DECIMAL)
elseif(NOT "${AVX2}" STREQUAL "")
list(APPEND AVX_FLAG "${AVX2}")
elseif(INSTRUCTION_PRESENT128)
math(EXPR JSONIFIER_CPU_INSTRUCTIONS "${JSONIFIER_CPU_INSTRUCTIONS} | 1 << 5" OUTPUT_FORMAT DECIMAL)
elseif(NOT "${AVX}" STREQUAL "")
list(APPEND AVX_FLAG "${AVX}")
math(EXPR JSONIFIER_CPU_INSTRUCTIONS "${JSONIFIER_CPU_INSTRUCTIONS} | 1 << 4" OUTPUT_FORMAT DECIMAL)
endif()

if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
check_instruction_set("LzCnt" "" 0x1)
check_instruction_set("PopCnt" "" 0x2)
check_instruction_set("Bmi" "" 0x4)
check_instruction_set("Bmi2" "" 0x8)
check_instruction_set("Neon" "" 0x10)
check_instruction_set("Avx" "/arch:AVX" 0x20)
check_instruction_set("Avx2" "/arch:AVX2" 0x40)
check_instruction_set("Avx512" "/arch:AVX512" 0x80)
else()
check_instruction_set("LzCnt" "-mlzcnt" 0x1)
check_instruction_set("PopCnt" "-mpopcnt" 0x2)
check_instruction_set("Bmi" "-mbmi" 0x4)
check_instruction_set("Bmi2" "-mbmi2" 0x8)
check_instruction_set("Neon" "" 0x10)
check_instruction_set("Avx" "-mavx;-mlzcnt;-mpopcnt;-mbmi;-mbmi2" 0x20)
check_instruction_set("Avx2" "-mavx2;-mavx;-mlzcnt;-mpopcnt;-mbmi;-mbmi2" 0x40)
check_instruction_set("Avx512" "-mavx512f;-mavx2;-mavx;-mlzcnt;-mpopcnt;-mbmi;-mbmi2" 0x80)
endif()

set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS_SAVE}")
set(AVX_FLAG "${AVX_FLAG}" CACHE STRING "AVX flags" FORCE)
set(JSONIFIER_CPU_INSTRUCTIONS "${JSONIFIER_CPU_INSTRUCTIONS}" CACHE STRING "CPU Instruction Sets" FORCE)
Loading

0 comments on commit 2183ae1

Please sign in to comment.