Skip to content

Commit

Permalink
Merge pull request #4 from mrdcvlsc/use--uint128-t-if-available
Browse files Browse the repository at this point in the history
Enable the use of `__uint128_t` or `uint64_t` if available
  • Loading branch information
mrdcvlsc authored Jun 10, 2024
2 parents cfcf714 + 46e081d commit 6a1079d
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 33 deletions.
34 changes: 22 additions & 12 deletions .github/workflows/ctests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
72 changes: 56 additions & 16 deletions include/epi/epi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,57 @@
// 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;
#elif defined(ENV_16_BIT)
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 {

Expand Down Expand Up @@ -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<uint64_t, LARGEST_AVAILABLE_T, 128>;
using uint192_t = whole_number<uint64_t, LARGEST_AVAILABLE_T, 192>;
Expand All @@ -1227,7 +1247,7 @@ namespace epi {
using uint512_t = whole_number<uint64_t, LARGEST_AVAILABLE_T, 512>;
using uint1024_t = whole_number<uint64_t, LARGEST_AVAILABLE_T, 1024>;

#elif defined(ENV_64_BIT)
#elif (defined(ENV_64_BIT) || defined(ENV_32_BIT_EXTENDED))

using uint128_t = whole_number<uint32_t, uint64_t, 128>;
using uint192_t = whole_number<uint32_t, uint64_t, 192>;
Expand All @@ -1246,6 +1266,26 @@ namespace epi {
using uint1024_t = whole_number<uint16_t, uint32_t, 1024>;

#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
Expand Down Expand Up @@ -1289,4 +1329,4 @@ namespace std {
} // namespace std
#endif

#endif
#endif
9 changes: 8 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/comp-info.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <iostream>
#include "../include/epi/epi.hpp"

int main() {
std::cout << "epi::LIMB_WIDENESS_COMP_INFO = " << epi::LIMB_WIDENESS_COMP_INFO << "\n";
return 0;
}
8 changes: 4 additions & 4 deletions tests/small_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6a1079d

Please sign in to comment.