From 46e081deb051f1bfbafbe9d6e1167f898fdf54f7 Mon Sep 17 00:00:00 2001 From: mrdcvlsc Date: Mon, 10 Jun 2024 15:21:59 +0800 Subject: [PATCH] Enable the use of `__uint128_t` or `uint64_t` if available - new macro directive for detecting if the type `__uint128_t` is available to be use as `cast_t` for 64-bit architectures - the same macro directive modifications aims to also detect the availability of the type `uint64_t` in 32-bit architectures to be use as `cast_t` - modified the workflow to force test programs to use `limb_t` of bit wideness 32 and 16 - disable my old test header library's final test output and replace it with cmake's ctest --- .github/workflows/ctests.yml | 34 +++++++++++------ include/epi/epi.hpp | 72 ++++++++++++++++++++++++++++-------- tests/CMakeLists.txt | 9 ++++- tests/comp-info.cpp | 7 ++++ tests/small_test.hpp | 8 ++-- 5 files changed, 97 insertions(+), 33 deletions(-) create mode 100644 tests/comp-info.cpp diff --git a/.github/workflows/ctests.yml b/.github/workflows/ctests.yml index 7229af4..6cf9da9 100644 --- a/.github/workflows/ctests.yml +++ b/.github/workflows/ctests.yml @@ -8,33 +8,43 @@ on: jobs: build-others: - name: ${{ matrix.platform.name }} C++${{matrix.config.cxx_version}} + name: ${{ matrix.platform.name }} ${{matrix.wideness.name}} C++${{matrix.config.cxx_version}} runs-on: ${{ matrix.platform.os }} strategy: fail-fast: false matrix: platform: - - { name: Windows VS2019, os: windows-2019, flags: -DCMAKE_BUILD_TYPE=Debug } - - { name: Windows VS2022, os: windows-2022, flags: -DCMAKE_BUILD_TYPE=Debug } - - { name: Windows Clang, os: windows-latest, flags: -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ } - - { name: Windows GCC, os: windows-latest, flags: -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ } - - { name: Linux Clang, os: ubuntu-latest, flags: -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ } - - { name: Linux GCC, os: ubuntu-latest, flags: -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ } - - { name: MacOS XCode, os: macos-latest, flags: -DCMAKE_BUILD_TYPE=Debug } - - { name: MacOS Clang, os: macos-latest, flags: -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ } + - { name: Windows VS2019, os: windows-2019, flags: -DCMAKE_BUILD_TYPE=Debug } + - { name: Windows VS2022, os: windows-2022, flags: -DCMAKE_BUILD_TYPE=Debug } + - { name: Windows Clang, os: windows-latest, flags: -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ } + - { name: Windows GCC, os: windows-latest, flags: -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ } + - { name: Linux Clang, os: ubuntu-latest, flags: -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ } + - { name: Linux GCC, os: ubuntu-latest, flags: -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ } + - { name: Linux UNIX-POSIX, os: ubuntu-latest, flags: -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=cc -DCMAKE_CXX_COMPILER=c++ } + - { name: MacOS XCode, os: macos-latest, flags: -DCMAKE_BUILD_TYPE=Debug } + - { name: MacOS Clang, os: macos-latest, flags: -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ } config: - { cxx_version: 20 } - - { cxx_version: 23 } + # - { cxx_version: 23 } + wideness: + - { name: auto-bit wide limbs } + - { name: 32-bit wide limb, flag: -DFORCE_LIMB_WIDENESS=32 } + - { name: 16-bit wide limbs, flag: -DFORCE_LIMB_WIDENESS=16 } steps: - uses: actions/checkout@v4 + - name: Update Clang on Windows + continue-on-error: true + run: choco upgrade llvm + - name: Config - run: cmake -S tests -B tests ${{matrix.platform.flags}} -DCMAKE_CXX_STANDARD=${{matrix.config.cxx_version}} + run: cmake -S tests -B tests ${{matrix.platform.flags}} -DCMAKE_CXX_STANDARD=${{matrix.config.cxx_version}} ${{matrix.wideness.flag}} - name: Build run: cmake --build tests --config Debug - name: Tests - run: ctest --test-dir tests --build-config Debug --output-on-failure + run: ctest --test-dir tests --build-config Debug --verbose + # run: ctest --test-dir tests --build-config Debug --output-on-failure diff --git a/include/epi/epi.hpp b/include/epi/epi.hpp index 09a03f1..c8d168d 100644 --- a/include/epi/epi.hpp +++ b/include/epi/epi.hpp @@ -19,26 +19,34 @@ // DETECT THE MAXIMUM WIDTH OF THE LARGEST POD/PRIMITIVE TYPE IN THE SYSTEM // ================================================================================================= -#if (defined(__SIZEOF_INT128__) || defined(UINT128MAX)) && !defined(_MSVC_LANG) - // TODO: find way to check if the following instructions are available - // `__umodti3` & `__udivti3` #define ENV_64BIT_EXTENDED // temporarily disabled - // since some environment does not support `__umodti3` & `__udivti3` +#if (defined(__amd__64__) || defined(__amd_64) || defined(__amd64__) || defined(__aarch64__) || defined(__x86_64__) || defined(__x86_64) || defined(_WIN64) || defined(_M_X64) || defined(_M_AMD64)) #define ENV_64_BIT -#elif (defined(__amd__64__) || defined(__amd_64) || defined(__amd64__) || defined(__aarch64__) || defined(__x86_64__) || defined(__x86_64) || defined(_WIN64)) - #define ENV_64_BIT -#elif (defined(__INTEL__) || defined(__i386__) || defined(_M_IX86) || defined(__arm__) || defined(_WIN32)) + #if ((defined(__SIZEOF_INT128__) || defined(UINT128MAX)) && (!defined(_MSVC_LANG) && !defined(_MSC_VER))) + // TODO: find way to check if the following instructions are available + // `__umodti3` & `__udivti3` #define ENV_64_BIT_EXTENDED // temporarily disabled + // since some environment does not support `__umodti3` & `__udivti3` + #undef ENV_64_BIT + #define ENV_64_BIT_EXTENDED + #endif +#elif (defined(__INTEL__) || defined(__i386__) || defined(_M_IX86) || defined(__arm__) || defined(_ARM) || defined(_M_ARM) || defined(M_ARMT) || defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_3__) || defined()__ARM_ARCH_2__ || defined(__ARM_ARCH_4T__) || defined(_WIN32) || defined(_M_X86) || defined(_M_IX86_FP)) #define ENV_32_BIT + #if defined(UINT64_MAX) + #undef ENV_32_BIT + #define ENV_32_BIT_EXTENDED + #endif #else - #define ENV_16_BIT + #error this architecture is not supported #endif -#if defined(ENV_64BIT_EXTENDED) - #warning COMPILING_UINT128_T_LARGEST_VALUE +#if defined(ENV_64_BIT_EXTENDED) using LARGEST_AVAILABLE_T = __uint128_t; using signed_size_t = std::int64_t; #elif defined(ENV_64_BIT) using LARGEST_AVAILABLE_T = std::uint64_t; using signed_size_t = std::int64_t; +#elif defined(ENV_32_BIT_EXTENDED) +using LARGEST_AVAILABLE_T = std::uint64_t; +using signed_size_t = std::int32_t; #elif defined(ENV_32_BIT) using LARGEST_AVAILABLE_T = std::uint32_t; using signed_size_t = std::int32_t; @@ -46,11 +54,22 @@ using signed_size_t = std::int32_t; using LARGEST_AVAILABLE_T = std::uint16_t; using signed_size_t = std::int16_t; #else - #error largest value detection failed + #error unsupported environment #endif -// dev -// #define _LITTLE_ENDIAN +#if defined(FORCE_32_BIT_LIMBS) + #undef ENV_64_BIT_EXTENDED + #define ENV_64_BIT + #undef ENV_32_BIT_EXTENDED + #undef ENV_32_BIT + #undef ENV_16_BIT +#elif defined(FORCE_16_BIT_LIMBS) + #undef ENV_64_BIT_EXTENDED + #undef ENV_64_BIT + #undef ENV_32_BIT_EXTENDED + #define ENV_32_BIT + #undef ENV_16_BIT +#endif namespace epi { @@ -1218,7 +1237,8 @@ namespace epi { // ================================================================================================= // CREATE PRE-DEFINED TYPES FOR USERS // ================================================================================================= - #if defined(ENV_64BIT_EXTENDED) + + #if defined(ENV_64_BIT_EXTENDED) using uint128_t = whole_number; using uint192_t = whole_number; @@ -1227,7 +1247,7 @@ namespace epi { using uint512_t = whole_number; using uint1024_t = whole_number; - #elif defined(ENV_64_BIT) + #elif (defined(ENV_64_BIT) || defined(ENV_32_BIT_EXTENDED)) using uint128_t = whole_number; using uint192_t = whole_number; @@ -1246,6 +1266,26 @@ namespace epi { using uint1024_t = whole_number; #endif + + // ================================================================================================= + // COMPILE INFORMATION LIMB WIDENESS INDICATORS + + #if defined(ENV_64_BIT_EXTENDED) + static constexpr size_t LIMB_WIDENESS_COMP_INFO = 1; + #elif defined(ENV_64_BIT) + static constexpr size_t LIMB_WIDENESS_COMP_INFO = 2; + #elif defined(ENV_32_BIT_EXTENDED) + static constexpr size_t LIMB_WIDENESS_COMP_INFO = 3; + #elif defined(ENV_32_BIT) + static constexpr size_t LIMB_WIDENESS_COMP_INFO = 4; + #elif defined(ENV_16_BIT) + static constexpr size_t LIMB_WIDENESS_COMP_INFO = 5; + #else + static constexpr size_t LIMB_WIDENESS_COMP_INFO = 6; + #endif + + // ================================================================================================= + #endif } // namespace epi @@ -1289,4 +1329,4 @@ namespace std { } // namespace std #endif -#endif +#endif \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index fdba650..9eee487 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -5,8 +5,15 @@ file(GLOB SOURCES "*.cpp") set(CMAKE_VERBOSE_MAKEFILE ON) set(CMAKE_CXX_STANDARD_REQUIRED True) +set(FORCE_LIMB_WIDENESS "" CACHE STRING "Choose an AES Implementation") -# add_compile_definitions(USER_DEFINITION) +if("${FORCE_LIMB_WIDENESS}" STREQUAL "32") + add_compile_definitions(FORCE_32_BIT_LIMBS) +elseif("${FORCE_LIMB_WIDENESS}" STREQUAL "16") + add_compile_definitions(FORCE_16_BIT_LIMBS) +endif() + +message(FORCE_LIMB_WIDENESS="${FORCE_LIMB_WIDENESS}") if(WIN32) # disable windows asan for now since I don't know how to make it work diff --git a/tests/comp-info.cpp b/tests/comp-info.cpp new file mode 100644 index 0000000..26856d2 --- /dev/null +++ b/tests/comp-info.cpp @@ -0,0 +1,7 @@ +#include +#include "../include/epi/epi.hpp" + +int main() { + std::cout << "epi::LIMB_WIDENESS_COMP_INFO = " << epi::LIMB_WIDENESS_COMP_INFO << "\n"; + return 0; +} \ No newline at end of file diff --git a/tests/small_test.hpp b/tests/small_test.hpp index 535a818..388425f 100644 --- a/tests/small_test.hpp +++ b/tests/small_test.hpp @@ -109,15 +109,15 @@ namespace smlts { int test::get_final_verdict(std::string const &test_name = "") { if (!final_verdict) { - std::cout << "\nFINAL VERDICT [" << test_name << "] TEST : PASSED\n"; + // std::cout << "\nFINAL VERDICT [" << test_name << "] TEST : PASSED\n"; return 0; } - std::cout << "\nFINAL VERDICT [" << test_name << "] TEST : FAILED\n\n"; - std::cout << "Failed on test cases :\n"; + // std::cout << "\nFINAL VERDICT [" << test_name << "] TEST : FAILED\n\n"; + // std::cout << "Failed on test cases :\n"; for (size_t i = 0; i < file.size(); ++i) { - std::cout << "\tfile : " << file[i] << " | line : " << line[i] << "\n"; + // std::cout << "\tfile : " << file[i] << " | line : " << line[i] << "\n"; } return 1;