Skip to content

Commit

Permalink
optimize compile time
Browse files Browse the repository at this point in the history
  • Loading branch information
wjr-z committed Jun 28, 2024
1 parent 15e4864 commit 7554c13
Show file tree
Hide file tree
Showing 34 changed files with 1,536 additions and 1,375 deletions.
29 changes: 29 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 3.14)
project(wjr
LANGUAGES CXX
)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

set(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS} -std=c++17 -march=native -Wall -Wextra")

set(WJR_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
set(WJR_LIB_DIR ${PROJECT_SOURCE_DIR}/src)

file(GLOB_RECURSE WJR_SRCS ${WJR_LIB_DIR}/*.cpp)

target_include_directories(wjr PUBLIC
${WJR_INCLUDE_DIR}
)

target_precompile_headers(
wjr
PUBLIC ${WJR_INCLUDE_DIR}/wjr/preprocessor.hpp
PUBLIC ${WJR_INCLUDE_DIR}/wjr/assert.hpp
PUBLIC ${WJR_INCLUDE_DIR}/wjr/type_traits.hpp
PUBLIC ${WJR_INCLUDE_DIR}/wjr/x86/simd/simd.hpp
)

add_library(wjr STATIC ${WJR_SRCS})
9 changes: 2 additions & 7 deletions include/wjr/assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,10 @@
*
*/

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <tuple>
#include <type_traits>
#include <utility>

#include <wjr/preprocessor.hpp>

namespace wjr {

#ifndef WJR_DEBUG_LEVEL
#if defined(NDEBUG)
#define WJR_DEBUG_LEVEL 0
Expand All @@ -50,6 +43,8 @@ namespace wjr {
#error "WJR_DEBUG_LEVEL must be 0 ~ 3"
#endif

namespace wjr {

#define WJR_DEBUG_IF(level, expr0, expr1) \
WJR_PP_BOOL_IF(WJR_PP_GT(WJR_DEBUG_LEVEL, level), expr0, expr1)

Expand Down
13 changes: 6 additions & 7 deletions include/wjr/biginteger/biginteger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,21 @@ class default_biginteger_vector_storage {
~default_biginteger_vector_storage() noexcept = default;

void destroy(_Alty &al) noexcept {
if (WJR_BUILTIN_CONSTANT_P(data() == nullptr) && data() == nullptr) {
if (WJR_BUILTIN_CONSTANT_P_TRUE(data() == nullptr)) {
return;
}

const size_type __size = size();

if (WJR_BUILTIN_CONSTANT_P(__size == 0) && __size == 0) {
if (WJR_BUILTIN_CONSTANT_P_TRUE(__size == 0)) {
return;
}

destroy_n_using_allocator(data(), __size, al);
}

void destroy_and_deallocate(_Alty &al) noexcept {
if (WJR_BUILTIN_CONSTANT_P(capacity() == 0) && capacity() == 0) {
if (WJR_BUILTIN_CONSTANT_P_TRUE(capacity() == 0)) {
return;
}

Expand Down Expand Up @@ -237,15 +237,15 @@ inline int32_t __compare_si_impl(const biginteger_data *lhs, int64_t rhs) noexce
/// @private
template <typename T, WJR_REQUIRES(is_nonbool_integral_v<T>)>
int32_t __compare_impl(const biginteger_data *lhs, T rhs) noexcept {
if (WJR_BUILTIN_CONSTANT_P(rhs == 0) && rhs == 0) {
if (WJR_BUILTIN_CONSTANT_P_TRUE(rhs == 0)) {
const int32_t ssize = lhs->get_ssize();
return ssize == 0 ? 0 : ssize < 0 ? -1 : 1;
}

if constexpr (std::is_unsigned_v<T>) {
return __compare_ui_impl(lhs, rhs);
} else {
if (WJR_BUILTIN_CONSTANT_P(rhs >= 0) && rhs >= 0) {
if (WJR_BUILTIN_CONSTANT_P_TRUE(rhs >= 0)) {
return __compare_ui_impl(lhs, to_unsigned(rhs));
}

Expand Down Expand Up @@ -770,8 +770,7 @@ void sqr(basic_biginteger<S> &dst, const biginteger_data &src) noexcept {
template <typename S>
void mul(basic_biginteger<S> &dst, const biginteger_data &lhs,
const biginteger_data &rhs) noexcept {
if (WJR_BUILTIN_CONSTANT_P(std::addressof(lhs) == std::addressof(rhs)) &&
std::addressof(lhs) == std::addressof(rhs)) {
if (WJR_BUILTIN_CONSTANT_P_TRUE(std::addressof(lhs) == std::addressof(rhs))) {
sqr(dst, lhs);
return;
}
Expand Down
16 changes: 0 additions & 16 deletions include/wjr/container/generic/details.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,6 @@

namespace wjr {

WJR_REGISTER_HAS_TYPE(container_begin,
std::begin(std::declval<std::add_lvalue_reference_t<Container>>()),
Container);
WJR_REGISTER_HAS_TYPE(container_cbegin,
std::cbegin(std::declval<std::add_lvalue_reference_t<Container>>()),
Container);
WJR_REGISTER_HAS_TYPE(container_end,
std::end(std::declval<std::add_lvalue_reference_t<Container>>()),
Container);
WJR_REGISTER_HAS_TYPE(container_cend,
std::cend(std::declval<std::add_lvalue_reference_t<Container>>()),
Container);
WJR_REGISTER_HAS_TYPE(container_size,
std::size(std::declval<std::add_lvalue_reference_t<Container>>()),
Container);

WJR_REGISTER_HAS_TYPE(__container_resize,
std::declval<std::add_lvalue_reference_t<Container>>().resize(
std::declval<Size>(), std::declval<Args>()...),
Expand Down
27 changes: 14 additions & 13 deletions include/wjr/container/generic/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,11 @@ class __default_vector_storage_impl {

WJR_CONSTEXPR20 void
destroy(_Alty &al) noexcept(std::is_nothrow_destructible_v<value_type>) {
if (WJR_BUILTIN_CONSTANT_P(data() == nullptr) && data() == nullptr) {
if (WJR_BUILTIN_CONSTANT_P_TRUE(data() == nullptr)) {
return;
}

if (WJR_BUILTIN_CONSTANT_P(size() == 0) && size() == 0) {
if (WJR_BUILTIN_CONSTANT_P_TRUE(size() == 0)) {
return;
}

Expand All @@ -295,11 +295,11 @@ class __default_vector_storage_impl {

WJR_CONSTEXPR20 void destroy_and_deallocate(_Alty &al) noexcept(
std::is_nothrow_destructible_v<value_type>) {
if (WJR_BUILTIN_CONSTANT_P(data() == nullptr) && data() == nullptr) {
if (WJR_BUILTIN_CONSTANT_P_TRUE(data() == nullptr)) {
return;
}

if (WJR_BUILTIN_CONSTANT_P(capacity() == 0) && capacity() == 0) {
if (WJR_BUILTIN_CONSTANT_P_TRUE(capacity() == 0)) {
return;
}

Expand Down Expand Up @@ -404,7 +404,7 @@ class __static_vector_storage_impl {

WJR_CONSTEXPR20 void
destroy(_Alty &al) noexcept(std::is_nothrow_destructible_v<value_type>) {
if (WJR_BUILTIN_CONSTANT_P(size() == 0) && size() == 0) {
if (WJR_BUILTIN_CONSTANT_P_TRUE(size() == 0)) {
return;
}

Expand Down Expand Up @@ -558,15 +558,15 @@ class __fixed_vector_storage_impl {

private:
WJR_PURE WJR_INTRINSIC_INLINE bool __is_null_data() const {
return WJR_BUILTIN_CONSTANT_P(data() == nullptr) && data() == nullptr;
return WJR_BUILTIN_CONSTANT_P_TRUE(data() == nullptr);
}

WJR_PURE WJR_INTRINSIC_INLINE bool __is_zero_size() const {
return WJR_BUILTIN_CONSTANT_P(size() == 0) && size() == 0;
return WJR_BUILTIN_CONSTANT_P_TRUE(size() == 0);
}

WJR_PURE WJR_INTRINSIC_INLINE bool __is_zero_capacity() const {
return WJR_BUILTIN_CONSTANT_P(capacity() == 0) && capacity() == 0;
return WJR_BUILTIN_CONSTANT_P_TRUE(capacity() == 0);
}

public:
Expand Down Expand Up @@ -712,7 +712,7 @@ class __sso_vector_storage_impl {

WJR_CONSTEXPR20 void
destroy(_Alty &al) noexcept(std::is_nothrow_destructible_v<value_type>) {
if (WJR_BUILTIN_CONSTANT_P(size() == 0) && size() == 0) {
if (WJR_BUILTIN_CONSTANT_P_TRUE(size() == 0)) {
return;
}

Expand Down Expand Up @@ -1379,8 +1379,8 @@ class basic_vector {
template <typename Iter, WJR_REQUIRES(is_iterator_v<Iter>)>
WJR_CONSTEXPR20 iterator insert(const_iterator pos, Iter first, Iter last) {
const auto old_pos = static_cast<size_type>(pos - cbegin());
__range_insert(data() + old_pos, to_contiguous_address(first), to_contiguous_address(last),
iterator_category_t<Iter>());
__range_insert(data() + old_pos, to_contiguous_address(first),
to_contiguous_address(last), iterator_category_t<Iter>());
return begin() + old_pos;
}

Expand Down Expand Up @@ -1500,8 +1500,9 @@ class basic_vector {
template <typename Iter, WJR_REQUIRES(is_iterator_v<Iter>)>
WJR_CONSTEXPR20 basic_vector &replace(const_iterator from, const_iterator to,
Iter first, Iter last) {
__range_replace(__get_pointer(from), __get_pointer(to), to_contiguous_address(first),
to_contiguous_address(last), iterator_category_t<Iter>());
__range_replace(__get_pointer(from), __get_pointer(to),
to_contiguous_address(first), to_contiguous_address(last),
iterator_category_t<Iter>());
return *this;
}

Expand Down
72 changes: 72 additions & 0 deletions include/wjr/generic/math/bignum-config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#ifndef WJR_GENERIC_MATH_BIGNUM_CONFIG_HPP__
#define WJR_GENERIC_MATH_BIGNUM_CONFIG_HPP__

#ifndef WJR_TOOM22_MUL_THRESHOLD
#define WJR_TOOM22_MUL_THRESHOLD 22
#endif

#ifndef WJR_TOOM33_MUL_THRESHOLD
#define WJR_TOOM33_MUL_THRESHOLD 84
#endif

#ifndef WJR_TOOM44_MUL_THRESHOLD
#define WJR_TOOM44_MUL_THRESHOLD 208
#endif

#ifndef WJR_TOOM55_MUL_THRESHOLD
#define WJR_TOOM55_MUL_THRESHOLD 800
#endif

#ifndef WJR_TOOM32_TO_TOOM43_MUL_THRESHOLD
#define WJR_TOOM32_TO_TOOM43_MUL_THRESHOLD 73
#endif

#ifndef WJR_TOOM32_TO_TOOM53_MUL_THRESHOLD
#define WJR_TOOM32_TO_TOOM53_MUL_THRESHOLD 153
#endif

#ifndef WJR_TOOM42_TO_TOOM53_MUL_THRESHOLD
#define WJR_TOOM42_TO_TOOM53_MUL_THRESHOLD 137
#endif

#ifndef WJR_TOOM42_TO_TOOM63_MUL_THRESHOLD
#define WJR_TOOM42_TO_TOOM63_MUL_THRESHOLD 153
#endif

#ifndef WJR_TOOM2_SQR_THRESHOLD
#define WJR_TOOM2_SQR_THRESHOLD 34
#endif

#ifndef WJR_TOOM3_SQR_THRESHOLD
#define WJR_TOOM3_SQR_THRESHOLD 124
#endif

#ifndef WJR_TOOM4_SQR_THRESHOLD
#define WJR_TOOM4_SQR_THRESHOLD 288
#endif

#ifndef WJR_TOOM5_SQR_THRESHOLD
#define WJR_TOOM5_SQR_THRESHOLD 980
#endif

#ifndef WJR_DC_DIV_QR_THRESHOLD
#define WJR_DC_DIV_QR_THRESHOLD (WJR_TOOM22_MUL_THRESHOLD * 2)
#endif // WJR_DC_DIV_QR_THRESHOLD

#ifndef WJR_DC_BIGNUM_TO_CHARS_THRESHOLD
#define WJR_DC_BIGNUM_TO_CHARS_THRESHOLD 20
#endif

#ifndef WJR_DC_BIGNUM_TO_CHARS_PRECOMPUTE_THRESHOLD
#define WJR_DC_BIGNUM_TO_CHARS_PRECOMPUTE_THRESHOLD 20
#endif

#ifndef WJR_DC_BIGNUM_FROM_CHARS_THRESHOLD
#define WJR_DC_BIGNUM_FROM_CHARS_THRESHOLD 1670
#endif

#ifndef WJR_DC_BIGNUM_FROM_CHARS_PRECOMPUTE_THRESHOLD
#define WJR_DC_BIGNUM_FROM_CHARS_PRECOMPUTE_THRESHOLD 3105
#endif

#endif // WJR_GENERIC_MATH_BIGNUM_CONFIG_HPP__
6 changes: 3 additions & 3 deletions include/wjr/math/add.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ WJR_INTRINSIC_CONSTEXPR_E U addc_1(uint64_t *dst, const uint64_t *src0, size_t n
WJR_ASSERT_L2(WJR_IS_SAME_OR_INCR_P(dst, n, src0, n));
WJR_ASSERT_ASSUME(c_in <= 1);

if (WJR_BUILTIN_CONSTANT_P(n == 1) && n == 1) {
if (WJR_BUILTIN_CONSTANT_P_TRUE(n == 1)) {
uint8_t overflow = 0;
dst[0] = addc_cc(src0[0], src1, c_in, overflow);
return static_cast<U>(overflow);
Expand Down Expand Up @@ -377,8 +377,8 @@ WJR_INTRINSIC_CONSTEXPR_E void __add_128(uint64_t &al, uint64_t &ah, uint64_t lo
uint64_t hi0, uint64_t lo1,
uint64_t hi1) noexcept {
#if WJR_HAS_BUILTIN(__BUILTIN_ADD_128) || WJR_HAS_BUILTIN(__ASM_ADD_128)
if (is_constant_evaluated() || (WJR_BUILTIN_CONSTANT_P(lo0 == 0) && lo0 == 0) ||
(WJR_BUILTIN_CONSTANT_P(lo1 == 0) && lo1 == 0)) {
if (is_constant_evaluated() || WJR_BUILTIN_CONSTANT_P_TRUE(lo0 == 0) ||
WJR_BUILTIN_CONSTANT_P_TRUE(lo1 == 0) || WJR_BUILTIN_CONSTANT_P(lo0 + lo1)) {
return __fallback_add_128(al, ah, lo0, hi0, lo1, hi1);
}

Expand Down
68 changes: 1 addition & 67 deletions include/wjr/math/bignum-config.hpp
Original file line number Diff line number Diff line change
@@ -1,72 +1,6 @@
#ifndef WJR_MATH_BIGNUM_CONFIG_HPP__
#define WJR_MATH_BIGNUM_CONFIG_HPP__

#ifndef WJR_TOOM22_MUL_THRESHOLD
#define WJR_TOOM22_MUL_THRESHOLD 22
#endif

#ifndef WJR_TOOM33_MUL_THRESHOLD
#define WJR_TOOM33_MUL_THRESHOLD 84
#endif

#ifndef WJR_TOOM44_MUL_THRESHOLD
#define WJR_TOOM44_MUL_THRESHOLD 208
#endif

#ifndef WJR_TOOM55_MUL_THRESHOLD
#define WJR_TOOM55_MUL_THRESHOLD 800
#endif

#ifndef WJR_TOOM32_TO_TOOM43_MUL_THRESHOLD
#define WJR_TOOM32_TO_TOOM43_MUL_THRESHOLD 73
#endif

#ifndef WJR_TOOM32_TO_TOOM53_MUL_THRESHOLD
#define WJR_TOOM32_TO_TOOM53_MUL_THRESHOLD 153
#endif

#ifndef WJR_TOOM42_TO_TOOM53_MUL_THRESHOLD
#define WJR_TOOM42_TO_TOOM53_MUL_THRESHOLD 137
#endif

#ifndef WJR_TOOM42_TO_TOOM63_MUL_THRESHOLD
#define WJR_TOOM42_TO_TOOM63_MUL_THRESHOLD 153
#endif

#ifndef WJR_TOOM2_SQR_THRESHOLD
#define WJR_TOOM2_SQR_THRESHOLD 34
#endif

#ifndef WJR_TOOM3_SQR_THRESHOLD
#define WJR_TOOM3_SQR_THRESHOLD 124
#endif

#ifndef WJR_TOOM4_SQR_THRESHOLD
#define WJR_TOOM4_SQR_THRESHOLD 288
#endif

#ifndef WJR_TOOM5_SQR_THRESHOLD
#define WJR_TOOM5_SQR_THRESHOLD 980
#endif

#ifndef WJR_DC_DIV_QR_THRESHOLD
#define WJR_DC_DIV_QR_THRESHOLD (WJR_TOOM22_MUL_THRESHOLD * 2)
#endif // WJR_DC_DIV_QR_THRESHOLD

#ifndef WJR_DC_BIGNUM_TO_CHARS_THRESHOLD
#define WJR_DC_BIGNUM_TO_CHARS_THRESHOLD 20
#endif

#ifndef WJR_DC_BIGNUM_TO_CHARS_PRECOMPUTE_THRESHOLD
#define WJR_DC_BIGNUM_TO_CHARS_PRECOMPUTE_THRESHOLD 20
#endif

#ifndef WJR_DC_BIGNUM_FROM_CHARS_THRESHOLD
#define WJR_DC_BIGNUM_FROM_CHARS_THRESHOLD 1670
#endif

#ifndef WJR_DC_BIGNUM_FROM_CHARS_PRECOMPUTE_THRESHOLD
#define WJR_DC_BIGNUM_FROM_CHARS_PRECOMPUTE_THRESHOLD 3105
#endif
#include <wjr/generic/math/bignum-config.hpp>

#endif // WJR_MATH_BIGNUM_CONFIG_HPP__
Loading

0 comments on commit 7554c13

Please sign in to comment.