Skip to content

Commit

Permalink
Merge pull request #10 from i80287/update-longint
Browse files Browse the repository at this point in the history
Update longint
  • Loading branch information
i80287 authored Oct 25, 2024
2 parents 8abfd17 + f1b787a commit 1e80ada
Show file tree
Hide file tree
Showing 22 changed files with 1,212 additions and 996 deletions.
13 changes: 5 additions & 8 deletions .github/workflows/fedora.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ name: Fedora CI
on: [push, workflow_dispatch]

permissions:
contents: read
contents: read

env:
UBSAN_OPTIONS: print_stacktrace=1
ASAN_OPTIONS: print_stats=1:atexit=1:detect_odr_violation=2
UBSAN_OPTIONS: print_stacktrace=1
ASAN_OPTIONS: print_stats=1:atexit=1:detect_odr_violation=2

jobs:

Expand Down Expand Up @@ -36,8 +36,7 @@ jobs:
- name: Run tests
run: |
cd ./tests
chmod +x ./run_gcc_tests.sh
./run_gcc_tests.sh
chmod +x run_gcc_tests.sh && ./run_gcc_tests.sh
fedora-cmake-clang:
name: test-fedora-clang
Expand All @@ -63,6 +62,4 @@ jobs:
- name: Run tests
run: |
cd ./tests
chmod +x ./run_clang_tests.sh
./run_clang_tests.sh
cd ./tests && chmod +x run_clang_tests.sh && ./run_clang_tests.sh
6 changes: 2 additions & 4 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
macos_versions: [13, 14]
macos_versions: [13, 14, 15]

steps:
- uses: actions/checkout@v4
Expand All @@ -30,6 +30,4 @@ jobs:
- name: Run tests
run: |
cd ./tests
chmod +x run_macos_clang_tests.sh
./run_macos_clang_tests.sh
cd ./tests && chmod +x run_macos_clang_tests.sh && ./run_macos_clang_tests.sh
22 changes: 7 additions & 15 deletions .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ name: Ubuntu CI
on: [push, workflow_dispatch]

permissions:
contents: read
contents: read

env:
UBSAN_OPTIONS: print_stacktrace=1
ASAN_OPTIONS: print_stats=1:atexit=1:detect_odr_violation=2
UBSAN_OPTIONS: print_stacktrace=1
ASAN_OPTIONS: print_stats=1:atexit=1:detect_odr_violation=2

jobs:

Expand All @@ -34,9 +34,7 @@ jobs:
- name: Run tests
run: |
cd ./tests
chmod +x ./run_gcc_tests.sh
./run_gcc_tests.sh
cd ./tests && chmod +x run_gcc_tests.sh && ./run_gcc_tests.sh
ubuntu-cmake-clang:
name: test-clang-on-ubuntu-${{ matrix.ubuntu_version }}
Expand All @@ -61,9 +59,7 @@ jobs:
- name: Run tests
run: |
cd ./tests
chmod +x ./run_clang_tests.sh
./run_clang_tests.sh
cd ./tests && chmod +x run_clang_tests.sh && ./run_clang_tests.sh
ubuntu-cmake-gcc-mingw-w64-i686-posix:
name: test-gcc-mingw-w64-i686-posix-on-ubuntu-${{ matrix.ubuntu_version }}
Expand Down Expand Up @@ -93,9 +89,7 @@ jobs:
- name: Run tests
run: |
cd ./tests
chmod +x ./run_gcc_mingw_32_tests.sh
./run_gcc_mingw_32_tests.sh
cd ./tests && chmod +x run_gcc_mingw_32_tests.sh && ./run_gcc_mingw_32_tests.sh
ubuntu-cmake-gcc-mingw-w64-x86_64-posix:
name: test-gcc-mingw-w64-x86_64-posix-on-ubuntu-${{ matrix.ubuntu_version }}
Expand Down Expand Up @@ -125,6 +119,4 @@ jobs:
- name: Run tests
run: |
cd ./tests
chmod +x ./run_gcc_mingw_64_tests.sh
./run_gcc_mingw_64_tests.sh
cd ./tests && chmod +x run_gcc_mingw_64_tests.sh && ./run_gcc_mingw_64_tests.sh
30 changes: 17 additions & 13 deletions number_theory/fibonacci_num.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace detail {

ATTRIBUTE_ACCESS(read_write, 1)
ATTRIBUTE_ACCESS(read_only, 2)
constexpr void matrix_mul(uint64_t (&m1)[2][2], const uint64_t (&m2)[2][2]) noexcept {
const uint64_t tmp[2][2] = {
constexpr void matrix_mul(std::uint64_t (&m1)[2][2], const std::uint64_t (&m2)[2][2]) noexcept {
const std::uint64_t tmp[2][2] = {
{m1[0][0] * m2[0][0] + m1[0][1] * m2[1][0], m1[0][0] * m2[0][1] + m1[0][1] * m2[1][1]},
{m1[1][0] * m2[0][0] + m1[1][1] * m2[1][0], m1[1][0] * m2[0][1] + m1[1][1] * m2[1][1]},
};
Expand All @@ -27,22 +27,23 @@ constexpr void matrix_mul(uint64_t (&m1)[2][2], const uint64_t (&m2)[2][2]) noex

struct fibs_pair {
/// @brief F_{n - 1}
uint64_t fib_n_1;
std::uint64_t fib_n_1;
/// @brief F_n
uint64_t fib_n;
std::uint64_t fib_n;
};

/// @brief Returns pair (F_{n - 1}, F_n), where F_n
/// is a n-th Fibonacci number modulo 2^64.
/// Here we suppose that F_{-1} = 0, F_0 = 1, F_1 = 1.
/// @param n
/// @return (F_{n - 1}, F_n)
ATTRIBUTE_CONST constexpr fibs_pair fibonacci_nums(uint32_t n) noexcept {
uint64_t p[2][2] = {
[[nodiscard]]
ATTRIBUTE_CONST constexpr fibs_pair fibonacci_nums(std::uint32_t n) noexcept {
std::uint64_t p[2][2] = {
{0, 1},
{1, 1},
};
uint64_t fibmatrix[2][2] = {
std::uint64_t fibmatrix[2][2] = {
{1, 0},
{0, 1},
};
Expand All @@ -67,7 +68,8 @@ ATTRIBUTE_CONST constexpr fibs_pair fibonacci_nums(uint32_t n) noexcept {
/// Here we suppose that F_{-1} = 0, F_0 = 1, F_1 = 1.
/// @param n
/// @return F_n
ATTRIBUTE_CONST constexpr uint64_t fibonacci_num(uint32_t n) noexcept {
[[nodiscard]]
ATTRIBUTE_CONST constexpr std::uint64_t fibonacci_num(std::uint32_t n) noexcept {
return fibonacci_nums(n).fib_n;
}

Expand All @@ -83,7 +85,7 @@ ATTRIBUTE_CONST constexpr uint64_t fibonacci_num(uint32_t n) noexcept {
/// fibonacci_num(91) == 7540113804746346429
/// fibonacci_num(92) == 12200160415121876738
/// fibonacci_num(93) == 1293530146158671551 <- overflow
inline constexpr uint32_t kMaxFibNonOverflowU64 = 92;
inline constexpr std::uint32_t kMaxFibNonOverflowU64 = 92;

#if defined(INTEGERS_128_BIT_HPP)

Expand All @@ -92,7 +94,7 @@ namespace detail {

ATTRIBUTE_ACCESS(read_write, 1)
ATTRIBUTE_ACCESS(read_only, 2)
I128_CONSTEXPR void matrix_mul(uint128_t m1[2][2], const uint128_t m2[2][2]) noexcept {
I128_CONSTEXPR void matrix_mul(uint128_t (&m1)[2][2], const uint128_t (&m2)[2][2]) noexcept {
const uint128_t tmp[2][2] = {
{m1[0][0] * m2[0][0] + m1[0][1] * m2[1][0], m1[0][0] * m2[0][1] + m1[0][1] * m2[1][1]},
{m1[1][0] * m2[0][0] + m1[1][1] * m2[1][0], m1[1][0] * m2[0][1] + m1[1][1] * m2[1][1]},
Expand All @@ -117,7 +119,8 @@ struct fibs_pair_u128 {
/// Here we suppose that F_{-1} = 0, F_0 = 1, F_1 = 1.
/// @param n
/// @return (F_{n - 1}, F_n)
ATTRIBUTE_CONST I128_CONSTEXPR fibs_pair_u128 fibonacci_nums_u128(uint32_t n) noexcept {
[[nodiscard]]
ATTRIBUTE_CONST I128_CONSTEXPR fibs_pair_u128 fibonacci_nums_u128(std::uint32_t n) noexcept {
uint128_t p[2][2] = {
{0, 1},
{1, 1},
Expand Down Expand Up @@ -147,7 +150,8 @@ ATTRIBUTE_CONST I128_CONSTEXPR fibs_pair_u128 fibonacci_nums_u128(uint32_t n) no
/// Here we suppose that F_{-1} = 0, F_0 = 1, F_1 = 1.
/// @param n
/// @return F_n
ATTRIBUTE_CONST I128_CONSTEXPR uint128_t fibonacci_num_u128(uint32_t n) noexcept {
[[nodiscard]]
ATTRIBUTE_CONST I128_CONSTEXPR uint128_t fibonacci_num_u128(std::uint32_t n) noexcept {
return fibonacci_nums_u128(n).fib_n;
}

Expand All @@ -165,7 +169,7 @@ ATTRIBUTE_CONST I128_CONSTEXPR uint128_t fibonacci_num_u128(uint32_t n) noexcept
/// fibonacci_num_u128(186) == 198239973509362327032045173661212819077
/// ^
/// overflow
inline constexpr uint32_t kMaxFibNonOverflowU128 = 185;
inline constexpr std::uint32_t kMaxFibNonOverflowU128 = 185;

#endif // INTEGERS_128_BIT_HPP

Expand Down
6 changes: 5 additions & 1 deletion number_theory/integers_128_bit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ typedef std::_Signed128 int128_t;

#else

#ifdef defined(__clang__) || defined(__GNUC__)
#if defined(INTEGERS_128_BIT_HPP_WARN_IF_UNSUPPORED) && INTEGERS_128_BIT_HPP_WARN_IF_UNSUPPORED

#if defined(__clang__) || defined(__GNUC__)
// cppcheck-suppress [preprocessorErrorDirective]
#warning "Unsupported compiler, typedef 128-bit integer specific for your compiler"
#elif defined(_MSC_VER)
// cppcheck-suppress [preprocessorErrorDirective]
#pragma message WARN("your warning message here")
#endif

#endif

#define HAS_INT128_TYPEDEF 0

#endif
Expand Down
Loading

0 comments on commit 1e80ada

Please sign in to comment.