From d8881eed0e832060c66e7e2f30fbedd5b93e7e89 Mon Sep 17 00:00:00 2001 From: RealTimeChris <40668522+RealTimeChris@users.noreply.github.com> Date: Tue, 3 Dec 2024 01:49:27 -0500 Subject: [PATCH] DiscordCoreAPI v2.0.9 --- CMake/BuildFeatureTester.bat | 2 - CMake/BuildFeatureTester.sh | 3 - CMake/CMakeLists.txt | 5 - CMake/DCADetectArchitecture.cmake | 117 ----------- CMake/main.cpp | 184 ------------------ CMakeLists.txt | 7 +- .../discordcoreapi/JsonSpecializations.hpp | 19 +- Include/discordcoreapi/Utilities/Base.hpp | 94 +-------- Include/discordcoreapi/Utilities/Config.hpp | 161 +++++++++++++++ Include/discordcoreapi/Utilities/ISA/AVX.hpp | 2 +- Include/discordcoreapi/Utilities/ISA/AVX2.hpp | 4 +- .../discordcoreapi/Utilities/ISA/AVX512.hpp | 4 +- .../discordcoreapi/Utilities/ISA/Fallback.hpp | 2 +- .../Utilities/ISA/ISADetectionBase.hpp | 62 ++++++ Include/discordcoreapi/Utilities/ISA/Neon.hpp | 2 +- Library/CMakeLists.txt | 29 ++- Tests/main.cpp | 2 + Vcpkg/ports/discordcoreapi/portfile.cmake | 15 +- Vcpkg/ports/discordcoreapi/vcpkg.json | 4 +- Vcpkg/versions/d-/discordcoreapi.json | 10 + 20 files changed, 284 insertions(+), 444 deletions(-) delete mode 100644 CMake/BuildFeatureTester.bat delete mode 100644 CMake/BuildFeatureTester.sh delete mode 100644 CMake/CMakeLists.txt delete mode 100644 CMake/DCADetectArchitecture.cmake delete mode 100644 CMake/main.cpp create mode 100644 Include/discordcoreapi/Utilities/Config.hpp create mode 100644 Include/discordcoreapi/Utilities/ISA/ISADetectionBase.hpp diff --git a/CMake/BuildFeatureTester.bat b/CMake/BuildFeatureTester.bat deleted file mode 100644 index 62c7a1dd1..000000000 --- a/CMake/BuildFeatureTester.bat +++ /dev/null @@ -1,2 +0,0 @@ -"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 \ No newline at end of file diff --git a/CMake/BuildFeatureTester.sh b/CMake/BuildFeatureTester.sh deleted file mode 100644 index 4cd8e7aed..000000000 --- a/CMake/BuildFeatureTester.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -cmake -S ./ -B ./Build -DCMAKE_BUILD_TYPE=Release -cmake --build ./Build --config=Release diff --git a/CMake/CMakeLists.txt b/CMake/CMakeLists.txt deleted file mode 100644 index a1565c512..000000000 --- a/CMake/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -cmake_minimum_required(VERSION 3.10) -project(FeatureDetection) -set(CMAKE_CXX_STANDARD 17) -# Add your source file(s) -add_executable(feature_detector main.cpp) diff --git a/CMake/DCADetectArchitecture.cmake b/CMake/DCADetectArchitecture.cmake deleted file mode 100644 index f4b44e5ff..000000000 --- a/CMake/DCADetectArchitecture.cmake +++ /dev/null @@ -1,117 +0,0 @@ -# JsonifierDetectArchitecture.cmake - Script for detecting the CPU architecture. -# MIT License -# Copyright (c) 2023 RealTimeChris -# https://discordcoreapi.com - -if (UNIX OR APPLE) - file(WRITE "${CMAKE_CURRENT_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_CURRENT_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() - execute_process( - COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/../CMake/BuildFeatureTester.sh" - WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../CMake" - ) - set(FEATURE_TESTER_FILE "${CMAKE_CURRENT_SOURCE_DIR}/../CMake/Build/feature_detector") -elseif(WIN32) - file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/../CMake/BuildFeatureTester.bat" "\"${CMAKE_COMMAND}\" -S ./ -B ./Build -DCMAKE_BUILD_TYPE=Release -\"${CMAKE_COMMAND}\" --build ./Build --config=Release") - execute_process( - COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/../CMake/BuildFeatureTester.bat" - WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../CMake" - ) - set(FEATURE_TESTER_FILE "${CMAKE_CURRENT_SOURCE_DIR}/../CMake/Build/Release/feature_detector.exe") -endif() - -execute_process( - COMMAND "${FEATURE_TESTER_FILE}" - RESULT_VARIABLE DCA_CPU_INSTRUCTIONS_NEW -) - -function(is_numeric value result_var) - string(REGEX MATCH "^[0-9]+(\\.[0-9]+)?$" is_numeric_match "${value}") - if(is_numeric_match) - set(${result_var} TRUE PARENT_SCOPE) - else() - set(${result_var} FALSE PARENT_SCOPE) - endif() -endfunction() - -is_numeric("${DCA_CPU_INSTRUCTIONS_NEW}" is_numeric_result) - -set(AVX_FLAG "") -if (is_numeric_result) -math(EXPR DCA_CPU_INSTRUCTIONS_NUMERIC "${DCA_CPU_INSTRUCTIONS_NEW}") -math(EXPR DCA_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 DCA_CPU_INSTRUCTIONS "( ${DCA_CPU_INSTRUCTIONS} | ${INSTRUCTION_SET_NUMERIC_VALUE} )") - set(AVX_FLAG "${AVX_FLAG};${INSTRUCTION_SET_FLAG}" PARENT_SCOPE) - endif() -endfunction() - -math(EXPR INSTRUCTION_PRESENT "( ${DCA_CPU_INSTRUCTIONS_NUMERIC} & 0x1 )") -if(INSTRUCTION_PRESENT) - math(EXPR DCA_CPU_INSTRUCTIONS "${DCA_CPU_INSTRUCTIONS} | 1 << 0" OUTPUT_FORMAT DECIMAL) -endif() -math(EXPR INSTRUCTION_PRESENT "( ${DCA_CPU_INSTRUCTIONS_NUMERIC} & 0x2 )") -if(INSTRUCTION_PRESENT) - math(EXPR DCA_CPU_INSTRUCTIONS "${DCA_CPU_INSTRUCTIONS} | 1 << 1" OUTPUT_FORMAT DECIMAL) -endif() -math(EXPR INSTRUCTION_PRESENT "( ${DCA_CPU_INSTRUCTIONS_NUMERIC} & 0x4 )") -if(INSTRUCTION_PRESENT) - math(EXPR DCA_CPU_INSTRUCTIONS "${DCA_CPU_INSTRUCTIONS} | 1 << 2" OUTPUT_FORMAT DECIMAL) -endif() -math(EXPR INSTRUCTION_PRESENT "( ${DCA_CPU_INSTRUCTIONS_NUMERIC} & 0x8 )") -if(INSTRUCTION_PRESENT) - math(EXPR DCA_CPU_INSTRUCTIONS "${DCA_CPU_INSTRUCTIONS} | 1 << 3" OUTPUT_FORMAT DECIMAL) -endif() - -math(EXPR INSTRUCTION_PRESENT "( ${DCA_CPU_INSTRUCTIONS_NUMERIC} & 0x10 )") -if(INSTRUCTION_PRESENT) - math(EXPR DCA_CPU_INSTRUCTIONS "${DCA_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 DCA_CPU_INSTRUCTIONS "${DCA_CPU_INSTRUCTIONS} | 1 << 7" OUTPUT_FORMAT DECIMAL) -elseif(INSTRUCTION_PRESENT256) - math(EXPR DCA_CPU_INSTRUCTIONS "${DCA_CPU_INSTRUCTIONS} | 1 << 6" OUTPUT_FORMAT DECIMAL) -elseif(INSTRUCTION_PRESENT128) - math(EXPR DCA_CPU_INSTRUCTIONS "${DCA_CPU_INSTRUCTIONS} | 1 << 5" 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() -else() -set(DCA_CPU_INSTRUCTIONS "0") -endif() -set(AVX_FLAG "${AVX_FLAG}" CACHE STRING "AVX flags" FORCE) -set(DCA_CPU_INSTRUCTIONS "${DCA_CPU_INSTRUCTIONS}" CACHE STRING "CPU Instruction Sets" FORCE) diff --git a/CMake/main.cpp b/CMake/main.cpp deleted file mode 100644 index 1943a5f86..000000000 --- a/CMake/main.cpp +++ /dev/null @@ -1,184 +0,0 @@ -#include -#include -#include -#include - -#if defined(_MSC_VER) - #include -#elif defined(HAVE_GCC_GET_CPUID) && defined(USE_GCC_GET_CPUID) - #include -#endif - -enum instruction_set { - DEFAULT = 0x0, - LZCNT = 0x1, - POPCNT = 0x2, - BMI1 = 0x4, - BMI2 = 0x8, - NEON = 0x10, - AVX = 0x20, - AVX2 = 0x40, - AVX512F = 0x80, -}; - -namespace { - constexpr uint32_t cpuidAvx2Bit = 1ul << 5; - constexpr uint32_t cpuidBmi1Bit = 1ul << 3; - constexpr uint32_t cpuidBmi2Bit = 1ul << 8; - constexpr uint32_t cpuidAvx512Bit = 1ul << 16; - constexpr uint64_t cpuidAvx256Saved = 1ull << 2; - constexpr uint64_t cpuidAvx512Saved = 7ull << 5; - constexpr uint32_t cpuidOsxSave = (1ul << 26) | (1ul << 27); - constexpr uint32_t cpuidLzcntBit = 1ul << 5; - constexpr uint32_t cpuidPopcntBit = 1ul << 23; -} - -#if defined(__x86_64__) || defined(_M_AMD64) -static inline void cpuid(uint32_t* eax, uint32_t* ebx, uint32_t* ecx, uint32_t* edx); -inline static uint64_t xgetbv(); -#endif - -static void getCPUBrandString(char* brand) { -#if defined(__x86_64__) || defined(_M_AMD64) - uint32_t regs[12]; - regs[0] = 0x80000000; - cpuid(regs, regs + 1, regs + 2, regs + 3); - if (regs[0] < 0x80000004) - return; - regs[0] = 0x80000002; - cpuid(regs, regs + 1, regs + 2, regs + 3); - regs[4] = 0x80000003; - cpuid(regs + 4, regs + 5, regs + 6, regs + 7); - regs[8] = 0x80000004; - cpuid(regs + 8, regs + 9, regs + 10, regs + 11); - - memcpy(brand, regs, sizeof(regs)); -#endif -} - -void printCPUInfo(uint32_t supportedISA) { -#if defined(__x86_64__) || defined(_M_AMD64) - char brand[49] = { 0 }; - getCPUBrandString(brand); - std::cout << "CPU Brand: " << brand << "\n"; -#endif -} - -#if defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC) - -inline static uint32_t detectSupportedArchitectures() { - return instruction_set::NEON; -} - -#elif defined(__x86_64__) || defined(_M_AMD64) - -static inline void cpuid(uint32_t* eax, uint32_t* ebx, uint32_t* ecx, uint32_t* edx) { - #if defined(_MSC_VER) - int32_t cpuInfo[4]; - __cpuidex(cpuInfo, *eax, *ecx); - *eax = cpuInfo[0]; - *ebx = cpuInfo[1]; - *ecx = cpuInfo[2]; - *edx = cpuInfo[3]; - #elif defined(HAVE_GCC_GET_CPUID) && defined(USE_GCC_GET_CPUID) - uint32_t level = *eax; - __get_cpuid(level, eax, ebx, ecx, edx); - #else - uint32_t a = *eax, b, c = *ecx, d; - asm volatile("cpuid" : "=a"(a), "=b"(b), "=c"(c), "=d"(d) : "a"(a), "c"(c)); - *eax = a; - *ebx = b; - *ecx = c; - *edx = d; - #endif -} - -inline static uint64_t xgetbv() { - #if defined(_MSC_VER) - return _xgetbv(0); - #else - uint32_t eax, edx; - asm volatile("xgetbv" : "=a"(eax), "=d"(edx) : "c"(0)); - return (( uint64_t )edx << 32) | eax; - #endif -} - -inline static uint32_t detectSupportedArchitectures() { - std::uint32_t eax = 0; - std::uint32_t ebx = 0; - std::uint32_t ecx = 0; - std::uint32_t edx = 0; - std::uint32_t hostIsa = 0x0; - - eax = 0x1; - ecx = 0x0; - cpuid(&eax, &ebx, &ecx, &edx); - - if (ecx & cpuidLzcntBit) { - hostIsa |= instruction_set::LZCNT; - } - - if (ecx & cpuidPopcntBit) { - hostIsa |= instruction_set::POPCNT; - } - - if ((ecx & cpuidOsxSave) != cpuidOsxSave) { - return hostIsa; - } - - uint64_t xcr0 = xgetbv(); - - if ((xcr0 & cpuidAvx256Saved) == 0) { - return hostIsa; - } - - if (ecx & cpuidAvx256Saved) { - hostIsa |= instruction_set::AVX; - } - - eax = 0x7; - ecx = 0x0; - cpuid(&eax, &ebx, &ecx, &edx); - - if (ebx & cpuidAvx2Bit) { - hostIsa |= instruction_set::AVX2; - } - - if (ebx & cpuidBmi1Bit) { - hostIsa |= instruction_set::BMI1; - } - - if (ebx & cpuidBmi2Bit) { - hostIsa |= instruction_set::BMI2; - } - - if (!((xcr0 & cpuidAvx512Saved) == cpuidAvx512Saved)) { - return hostIsa; - } - - if (ebx & cpuidAvx512Bit) { - hostIsa |= instruction_set::AVX512F; - } - - return hostIsa; -} - -#elif defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC) - -inline static uint32_t detectSupportedArchitectures() { - return instruction_set::NEON; -} - -#else - -inline static uint32_t detectSupportedArchitectures() { - return instruction_set::DEFAULT; -} - -#endif - -int32_t main() { - const auto supportedISA = detectSupportedArchitectures(); - printCPUInfo(supportedISA); - return supportedISA; -} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index d60a04059..16eb293cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,12 +25,15 @@ set(Opus_DIR "C:/Vcpkg/installed/x64-windows-static/share/opus") # Set this one set(unofficial-sodium_DIR "C:/Vcpkg/installed/x64-windows-static/share/unofficial-sodium") # Set this one to the folder location of the file "unofficial-sodiumConfig.cmake". set(Jsonifier_DIR "") # Set this one to the folder location of the file "JsonifierConfig.cmake". set(OPENSSL_ROOT_DIR "C:/Vcpkg/installed/x64-windows-static/") # Set this one to the folder location of the include folder and library folders of OpenSSL. -set(VCPKG_ROOT_DIR "C:/Vcpkg") set(CMAKE_BUILD_TYPES "Release;Debug") option(BUILD_SHARED_LIBS "Build using shared libraries" ON) set(DEV TRUE) -if (EXISTS "${VCPKG_ROOT_DIR}") +if (EXISTS "${_VCPKG_ROOT_DIR}") + set(VCPKG_ROOT_DIR "${_VCPKG_ROOT_DIR}") + set(ENV{VCPKG_INSTALLATION_ROOT} "${_VCPKG_ROOT_DIR}") +else() + set(VCPKG_ROOT_DIR "C:/Vcpkg") set(ENV{VCPKG_INSTALLATION_ROOT} "${VCPKG_ROOT_DIR}") endif() diff --git a/Include/discordcoreapi/JsonSpecializations.hpp b/Include/discordcoreapi/JsonSpecializations.hpp index bac3ebcd8..da826db12 100644 --- a/Include/discordcoreapi/JsonSpecializations.hpp +++ b/Include/discordcoreapi/JsonSpecializations.hpp @@ -69,16 +69,17 @@ namespace jsonifier_internal { template struct serialize_impl { - template JSONIFIER_ALWAYS_INLINE static void impl(value_type_new&& value, buffer_type& buffer, index_type&index, indent_type& indent) noexcept { + template DCA_ALWAYS_INLINE static void impl(value_type_new&& value, buffer_type& buffer, index_type&index, indent_type& indent) noexcept { jsonifier::string newString{ static_cast(value) }; serialize::impl(newString, buffer, index, indent); } }; - template struct parse_impl { - JSONIFIER_ALWAYS_INLINE static void impl(value_type& value, parse_context_type& context) noexcept { + template + struct parse_impl { + DCA_ALWAYS_INLINE static void impl(value_type& value, parse_context_type& context) noexcept { jsonifier::raw_json_data newString{}; - parse::impl(newString, context); + parse::template impl(newString, context); if (newString.getType() == jsonifier::json_type::String) { value = newString.get(); } else { @@ -92,17 +93,17 @@ namespace jsonifier_internal { template struct serialize_impl { - template JSONIFIER_ALWAYS_INLINE static void impl(value_type_new&& value, buffer_type& buffer, index_type& index, indent_type& indent) noexcept { + template DCA_ALWAYS_INLINE static void impl(value_type_new&& value, buffer_type& buffer, index_type& index, indent_type& indent) noexcept { jsonifier::string newString{ static_cast(value) }; serialize::impl(newString, buffer, index, indent); } }; - template - struct parse_impl { - JSONIFIER_ALWAYS_INLINE static void impl(value_type& value, parse_context_type& context) noexcept { + template + struct parse_impl { + DCA_ALWAYS_INLINE static void impl(value_type& value, parse_context_type& context) noexcept { jsonifier::raw_json_data newString{}; - parse::impl(newString, context); + parse::template impl(newString, context); if (newString.getType() == jsonifier::json_type::String) { value = newString.get(); } else { diff --git a/Include/discordcoreapi/Utilities/Base.hpp b/Include/discordcoreapi/Utilities/Base.hpp index 42d15ce1d..35cba56ff 100644 --- a/Include/discordcoreapi/Utilities/Base.hpp +++ b/Include/discordcoreapi/Utilities/Base.hpp @@ -29,99 +29,7 @@ /// \file Base.hpp #pragma once -#if !defined(__GNUC__) - #pragma warning(disable : 4710) - #pragma warning(disable : 4711) - #pragma warning(disable : 4251) - #pragma warning(disable : 4371) - #pragma warning(disable : 4514) - #pragma warning(disable : 4623) - #pragma warning(disable : 4625) - #pragma warning(disable : 4626) - #pragma warning(disable : 4820) - #pragma warning(disable : 5267) - #pragma warning(disable : 5026) - #pragma warning(disable : 5027) - #pragma warning(disable : 5045) - #pragma warning(disable : 5246) -#endif - -#if defined(__clang__) && defined(NDEBUG) && !defined(DCA_INLINE) - #define DCA_INLINE inline __attribute__((always_inline)) -#elif !defined(DCA_INLINE) - #define DCA_INLINE inline -#endif - -#if !defined(DCA_CPU_INSTRUCTIONS) - #define DCA_CPU_INSTRUCTIONS 0 -#endif - -#if !defined(DCA_CHECK_FOR_INSTRUCTION) - #define DCA_CHECK_FOR_INSTRUCTION(x) (DCA_CPU_INSTRUCTIONS & x) -#endif - -#if !defined(DCA_CHECK_FOR_AVX) - #define DCA_CHECK_FOR_AVX(x) (DCA_CPU_INSTRUCTIONS >= x) -#endif - -#if !defined(DCA_NEON) - #define DCA_NEON (1 << 4) -#endif -#if !defined(DCA_AVX) - #define DCA_AVX (1 << 5) -#endif -#if !defined(DCA_AVX2) - #define DCA_AVX2 (1 << 6) -#endif -#if !defined(DCA_AVX512) - #define DCA_AVX512 (1 << 7) -#endif - -#if defined _WIN32 - #if !defined DiscordCoreAPI_EXPORTS_NOPE - #if defined DiscordCoreAPI_EXPORTS - #if !defined DiscordCoreAPI_Dll - #define DiscordCoreAPI_Dll __declspec(dllexport) - #endif - #else - #if !defined DiscordCoreAPI_Dll - #define DiscordCoreAPI_Dll __declspec(dllimport) - #endif - #endif - #else - #define DiscordCoreAPI_Dll - #endif - #if !defined WIN32_LEAN_AND_MEAN - #define WIN32_LEAN_AND_MEAN - #endif - #if !defined WINRT_LEAN_AND_MEAN - #define WINRT_LEAN_AND_MEAN - #endif - #if !defined(NOMINMAX) - #define NOMINMAX - #endif - #include -DCA_INLINE tm getTime(time_t time) { - tm timeNew{}; - gmtime_s(&timeNew, &time); - return timeNew; -} - #include -#else - #if !defined DiscordCoreAPI_Dll - #define DiscordCoreAPI_Dll - #endif - #include - #include - #include - #include - #include - #include - #include -DCA_INLINE tm getTime(time_t time) { - return *gmtime(&time); -} -#endif +#include #include diff --git a/Include/discordcoreapi/Utilities/Config.hpp b/Include/discordcoreapi/Utilities/Config.hpp new file mode 100644 index 000000000..c014d53ab --- /dev/null +++ b/Include/discordcoreapi/Utilities/Config.hpp @@ -0,0 +1,161 @@ +/* + MIT License + + DiscordCoreAPI, A bot library for Discord, written in C++, and featuring explicit multithreading through the usage of custom, asynchronous C++ CoRoutines. + + Copyright 2022, 2023 Chris M. (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. +*/ +/// Config.hpp - Header for the some of the base definitions and includes. +/// Nov 8, 2021 +/// https://discordcoreapi.com +/// \file Base.hpp +#pragma once + +#if !defined(__GNUC__) + #pragma warning(disable : 4710) + #pragma warning(disable : 4711) + #pragma warning(disable : 4251) + #pragma warning(disable : 4371) + #pragma warning(disable : 4514) + #pragma warning(disable : 4623) + #pragma warning(disable : 4625) + #pragma warning(disable : 4626) + #pragma warning(disable : 4820) + #pragma warning(disable : 5267) + #pragma warning(disable : 5026) + #pragma warning(disable : 5027) + #pragma warning(disable : 5045) + #pragma warning(disable : 5246) +#endif + +#if defined(__clang__) || (defined(__GNUC__) && defined(__llvm__)) + #define DCA_CLANG 1 +#elif defined(_MSC_VER) + #pragma warning(disable : 4820) + #pragma warning(disable : 4371) + #pragma warning(disable : 4324) + #define DCA_MSVC 1 +#elif defined(__GNUC__) && !defined(__clang__) + #define DCA_GNUCXX 1 +#endif + +#if defined(DCA_MSVC) + #define DCA_VISUAL_STUDIO 1 + #if defined(DCA_CLANG) + #define DCA_CLANG_VISUAL_STUDIO 1 + #else + #define DCA_REGULAR_VISUAL_STUDIO 1 + #endif +#endif + +#define DCA_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) + +#if defined(macintosh) || defined(Macintosh) || (defined(__APPLE__) && defined(__MACH__)) || defined(TARGET_OS_MAC) + #define DCA_MAC 1 +#elif defined(linux) || defined(__linux) || defined(__linux__) || defined(__gnu_linux__) + #define DCA_LINUX 1 +#elif defined(WIN32) || defined(_WIN32) || defined(_WIN64) + #define DCA_WIN 1 +#else + #error "Undetected platform." +#endif + +#if defined(__cpp_inline_variables) && __cpp_inline_variables >= 201606L + #define DCA_HAS_INLINE_VARIABLE 1 +#elif __cplusplus >= 201703L + #define DCA_HAS_INLINE_VARIABLE 1 +#elif defined(DCA_MSVC) && DCA_MSVC >= 1912 && _MSVC_LANG >= 201703L + #define DCA_HAS_INLINE_VARIABLE 1 +#else + #define DCA_HAS_INLINE_VARIABLE 0 +#endif + +#if DCA_HAS_INLINE_VARIABLE + #define DCA_ALWAYS_INLINE_VARIABLE inline constexpr +#else + #define DCA_ALWAYS_INLINE_VARIABLE static constexpr +#endif + +#if defined(NDEBUG) + #if defined(DCA_MSVC) + #define DCA_ALWAYS_INLINE [[msvc::forceinline]] inline + #define DCA_CLANG_ALWAYS_INLINE inline + #define DCA_INLINE inline + #elif defined(DCA_CLANG) + #define DCA_ALWAYS_INLINE inline __attribute__((always_inline)) + #define DCA_CLANG_ALWAYS_INLINE inline __attribute__((always_inline)) + #define DCA_INLINE inline + #elif defined(DCA_GNUCXX) + #define DCA_ALWAYS_INLINE inline __attribute__((always_inline)) + #define DCA_CLANG_ALWAYS_INLINE inline + #define DCA_INLINE inline + #endif +#else + #define DCA_ALWAYS_INLINE inline + #define DCA_CLANG_ALWAYS_INLINE inline + #define DCA_INLINE inline +#endif + +#if defined _WIN32 + #if !defined DiscordCoreAPI_EXPORTS_NOPE + #if defined DiscordCoreAPI_EXPORTS + #if !defined DiscordCoreAPI_Dll + #define DiscordCoreAPI_Dll __declspec(dllexport) + #endif + #else + #if !defined DiscordCoreAPI_Dll + #define DiscordCoreAPI_Dll __declspec(dllimport) + #endif + #endif + #else + #define DiscordCoreAPI_Dll + #endif + #if !defined WIN32_LEAN_AND_MEAN + #define WIN32_LEAN_AND_MEAN + #endif + #if !defined WINRT_LEAN_AND_MEAN + #define WINRT_LEAN_AND_MEAN + #endif + #if !defined(NOMINMAX) + #define NOMINMAX + #endif + #include +DCA_INLINE tm getTime(time_t time) { + tm timeNew{}; + gmtime_s(&timeNew, &time); + return timeNew; +} + #include +#else + #if !defined DiscordCoreAPI_Dll + #define DiscordCoreAPI_Dll + #endif + #include + #include + #include + #include + #include + #include + #include +DCA_INLINE tm getTime(time_t time) { + return *gmtime(&time); +} +#endif diff --git a/Include/discordcoreapi/Utilities/ISA/AVX.hpp b/Include/discordcoreapi/Utilities/ISA/AVX.hpp index 0fccf4187..22b5c0d77 100644 --- a/Include/discordcoreapi/Utilities/ISA/AVX.hpp +++ b/Include/discordcoreapi/Utilities/ISA/AVX.hpp @@ -29,7 +29,7 @@ /// \file AVX.hpp #pragma once -#include +#include #if DCA_CHECK_FOR_INSTRUCTION(DCA_AVX) && !DCA_CHECK_FOR_INSTRUCTION(DCA_AVX2) && !DCA_CHECK_FOR_INSTRUCTION(DCA_AVX512) diff --git a/Include/discordcoreapi/Utilities/ISA/AVX2.hpp b/Include/discordcoreapi/Utilities/ISA/AVX2.hpp index 24a68dd96..dc30319d0 100644 --- a/Include/discordcoreapi/Utilities/ISA/AVX2.hpp +++ b/Include/discordcoreapi/Utilities/ISA/AVX2.hpp @@ -29,9 +29,9 @@ /// \file AVX2.hpp #pragma once -#include +#include -#if DCA_CHECK_FOR_INSTRUCTION(DCA_AVX2) && !DCA_CHECK_FOR_INSTRUCTION(DCA_AVX) && !DCA_CHECK_FOR_INSTRUCTION(DCA_AVX512) +#if DCA_CHECK_FOR_INSTRUCTION(DCA_AVX2) && !DCA_CHECK_FOR_INSTRUCTION(DCA_AVX512) #include #include diff --git a/Include/discordcoreapi/Utilities/ISA/AVX512.hpp b/Include/discordcoreapi/Utilities/ISA/AVX512.hpp index 487bce979..d5203a732 100644 --- a/Include/discordcoreapi/Utilities/ISA/AVX512.hpp +++ b/Include/discordcoreapi/Utilities/ISA/AVX512.hpp @@ -30,9 +30,9 @@ /// #pragma once -#include +#include -#if DCA_CHECK_FOR_INSTRUCTION(DCA_AVX512) && !DCA_CHECK_FOR_INSTRUCTION(DCA_AVX) && !DCA_CHECK_FOR_INSTRUCTION(DCA_AVX2) +#if DCA_CHECK_FOR_INSTRUCTION(DCA_AVX512) #include #include diff --git a/Include/discordcoreapi/Utilities/ISA/Fallback.hpp b/Include/discordcoreapi/Utilities/ISA/Fallback.hpp index 73113acb8..09b8f6638 100644 --- a/Include/discordcoreapi/Utilities/ISA/Fallback.hpp +++ b/Include/discordcoreapi/Utilities/ISA/Fallback.hpp @@ -29,7 +29,7 @@ /// \file Fallback.hpp #pragma once -#include +#include #if (!DCA_CHECK_FOR_INSTRUCTION(DCA_AVX)) && (!DCA_CHECK_FOR_INSTRUCTION(DCA_AVX2)) && (!DCA_CHECK_FOR_INSTRUCTION(DCA_AVX512)) diff --git a/Include/discordcoreapi/Utilities/ISA/ISADetectionBase.hpp b/Include/discordcoreapi/Utilities/ISA/ISADetectionBase.hpp new file mode 100644 index 000000000..04b201ed9 --- /dev/null +++ b/Include/discordcoreapi/Utilities/ISA/ISADetectionBase.hpp @@ -0,0 +1,62 @@ +/* + MIT License + + DiscordCoreAPI, A bot library for Discord, written in C++, and featuring explicit multithreading through the usage of custom, asynchronous C++ CoRoutines. + + Copyright 2022, 2023 Chris M. (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. +*/ +/// Isadetection.hpp - Header for the detection of the AVX instruction set. +/// Nov 8, 2021 +/// https://discordcoreapi.com +/// \file ISADetection.hpp +#pragma once + +#include + +#if !defined(DCA_CHECK_FOR_INSTRUCTION) + #define DCA_CHECK_FOR_INSTRUCTION(x) (DCA_CPU_INSTRUCTIONS & x) +#endif + +#if defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC) + #define DCA_NEON 1 << 4 +#else + #define DCA_NEON 0 +#endif +#if defined(__AVX__) + #define DCA_AVX 1 << 5 +#else + #define DCA_AVX 0 +#endif +#if defined(__AVX2__) + #define DCA_AVX2 1 << 6 +#else + #define DCA_AVX2 0 +#endif +#if defined(__AVX512__) + #define DCA_AVX512 1 << 7 +#else + #define DCA_AVX512 0 +#endif + +#if !defined(DCA_CPU_INSTRUCTIONS) + #define DCA_CPU_INSTRUCTIONS (DCA_NEON | DCA_AVX | DCA_AVX2 | DCA_AVX512) +#endif + diff --git a/Include/discordcoreapi/Utilities/ISA/Neon.hpp b/Include/discordcoreapi/Utilities/ISA/Neon.hpp index 34004bed1..7966ad57d 100644 --- a/Include/discordcoreapi/Utilities/ISA/Neon.hpp +++ b/Include/discordcoreapi/Utilities/ISA/Neon.hpp @@ -29,7 +29,7 @@ /// \file AVX.hpp #pragma once -#include +#include #if DCA_CHECK_FOR_INSTRUCTION(DCA_NEON) diff --git a/Library/CMakeLists.txt b/Library/CMakeLists.txt index 7133807bb..69f2ecc44 100644 --- a/Library/CMakeLists.txt +++ b/Library/CMakeLists.txt @@ -88,12 +88,9 @@ find_package(OpenSSL REQUIRED) find_package(Opus CONFIG REQUIRED) find_package(unofficial-sodium CONFIG REQUIRED) -if(NOT DEFINED DCA_CPU_INSTRUCTIONS) - include("DCADetectArchitecture") -endif() - -set(DCA_CPU_INSTRUCTIONS "${DCA_CPU_INSTRUCTIONS}" CACHE INTERNAL "For the CPU architecture selection.") -set(AVX_FLAG "${AVX_FLAG}" CACHE INTERNAL "For the CPU flag selection.") +set(DCA_CPU_INSTRUCTIONS + "$,${DCA_CPU_FLAGS},$,/arch:AVX512,-march=native>>" +) target_include_directories( "${LIB_NAME}" PUBLIC @@ -111,20 +108,10 @@ target_link_libraries( "$<$:Opus::opus>" ) -target_compile_definitions( - "${LIB_NAME}" PUBLIC - "$<$>:DiscordCoreAPI_EXPORTS_NOPE>" - "DCA_CPU_INSTRUCTIONS=${DCA_CPU_INSTRUCTIONS}" -) - -include(ProcessorCount) -ProcessorCount(N) -MATH(EXPR THREAD_COUNT "${N} / 3") - target_compile_options( "${LIB_NAME}" PUBLIC + "$,${DCA_CPU_INSTRUCTIONS},$,/arch:AVX512,-march=native>>" "$<$:$<$:/fsanitize=address>>" - "$<$:/MP${THREAD_COUNT}>" "$<$:-fcoroutines>" "$<$:-fcoroutines>" "$<$:-Wextra>" @@ -134,8 +121,14 @@ target_compile_options( "$<$:/Wall>" "$<$:/EHsc>" "$<$:-Wall>" + "$<$:/MP1>" "$<$:/Zi>" - "${AVX_FLAG}" + "${DCA_CPU_FLAGS}" +) + +target_compile_definitions( + "${LIB_NAME}" PUBLIC + "$>,DiscordCoreAPI_EXPORTS_NOPE,>" ) target_link_options( diff --git a/Tests/main.cpp b/Tests/main.cpp index e2eb2c5de..dcb9e9aea 100644 --- a/Tests/main.cpp +++ b/Tests/main.cpp @@ -21,6 +21,8 @@ discord_core_api::co_routine onGuildCreation(const discord_core_api: } int32_t main() { + + std::cout << "CURRENT DCA-INSTRUCTION_TYPE: " << DCA_CPU_INSTRUCTIONS << std::endl; jsonifier::string botToken = ""; jsonifier::vector functionVector{}; functionVector.reserve(5); diff --git a/Vcpkg/ports/discordcoreapi/portfile.cmake b/Vcpkg/ports/discordcoreapi/portfile.cmake index 7a2585a05..2ac156e5f 100644 --- a/Vcpkg/ports/discordcoreapi/portfile.cmake +++ b/Vcpkg/ports/discordcoreapi/portfile.cmake @@ -6,10 +6,21 @@ vcpkg_from_github( OUT_SOURCE_PATH SOURCE_PATH REPO RealTimeChris/DiscordCoreAPI REF "v${VERSION}" - SHA512 344e960491e17e9626f6ab4a42f28fe59842c0c15cf32ef2508e850099105667c651feaa6dd642207413fbeac43283310fe2b9a98a2ebfd4a49716da43e5cade + SHA512 89878716432457bea78e5fc6c29f223f78a733897993596db278fd32afd45b31b0b665f478cb80938527da9325d5afa964d52ea405d1c4be46c79ed690038790 HEAD_REF main ) +# discordcoreapi consumes extreme amounts of memory (>9GB per .cpp file). With our default +# concurrency values this causes hanging and/or OOM killing on Linux build machines and +# warnings on the Windows machines like: +# #[warning]Free memory is lower than 5%; Currently used: 99.99% +# #[warning]Free memory is lower than 5%; Currently used: 99.99% +# #[warning]Free memory is lower than 5%; Currently used: 99.99% +# Cut the requested concurrency in quarter to avoid this. +if(VCPKG_CONCURRENCY GREATER 4) + math(EXPR VCPKG_CONCURRENCY "${VCPKG_CONCURRENCY} / 4") +endif() + vcpkg_cmake_configure( SOURCE_PATH "${SOURCE_PATH}" ) @@ -24,4 +35,4 @@ if(VCPKG_LIBRARY_LINKAGE STREQUAL "static") file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/bin" "${CURRENT_PACKAGES_DIR}/debug/bin") endif() -vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/License.md") +vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/License.md") \ No newline at end of file diff --git a/Vcpkg/ports/discordcoreapi/vcpkg.json b/Vcpkg/ports/discordcoreapi/vcpkg.json index 7c43aed13..f15ce5e97 100644 --- a/Vcpkg/ports/discordcoreapi/vcpkg.json +++ b/Vcpkg/ports/discordcoreapi/vcpkg.json @@ -1,6 +1,6 @@ { "name": "discordcoreapi", - "version": "2.0.7", + "version": "2.0.8", "description": "A Discord bot library written in C++ using custom asynchronous coroutines.", "homepage": "https://discordcoreapi.com", "license": "MIT", @@ -19,4 +19,4 @@ "host": true } ] -} \ No newline at end of file +} diff --git a/Vcpkg/versions/d-/discordcoreapi.json b/Vcpkg/versions/d-/discordcoreapi.json index e1c6e62ff..9902de2ff 100644 --- a/Vcpkg/versions/d-/discordcoreapi.json +++ b/Vcpkg/versions/d-/discordcoreapi.json @@ -1,5 +1,15 @@ { "versions": [ + { + "git-tree": "1371e3f72145af807a144e3fadb1daf58c0b7cdd", + "version": "2.0.7", + "port-version": 1 + }, + { + "git-tree": "b3a2a5137d39b32f98a88f4e454b885a6097929f", + "version": "2.0.7", + "port-version": 0 + }, { "git-tree": "cf7406d145c55d85306226b283a9d7b3224e62c2", "version": "2.0.6",