Skip to content

Commit

Permalink
minor code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
KRM7 committed Aug 18, 2023
1 parent d02b7d2 commit cd227aa
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 60 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ else() # GNU style compiler interface
set(GAPP_WARN_FLAGS "${GAPP_WARN_FLAGS} -Werror -pedantic-errors")
endif()

set(GAPP_OPT_FLAGS "-O3 -fno-math-errno -fno-signed-zeros -fno-trapping-math -freciprocal-math -fno-rounding-math")
set(GAPP_OPT_FLAGS "-O3 -fno-math-errno -fno-trapping-math -freciprocal-math -fno-signed-zeros -fno-associative-math -fno-finite-math-only")
if(GAPP_USE_MARCH_NATIVE)
set(GAPP_OPT_FLAGS "${GAPP_OPT_FLAGS} -march=native")
endif()
Expand Down Expand Up @@ -139,9 +139,9 @@ target_include_directories(gapp SYSTEM INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURR

target_compile_features(gapp PUBLIC "cxx_std_20")
target_compile_definitions(gapp PUBLIC "$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:GAPP_BUILD_STATIC>")
target_compile_definitions(gapp PUBLIC "$<$<BOOL:${GAPP_DISABLE_EXCEPTIONS}>:GAPP_NO_EXCEPTIONS>")
target_compile_options(gapp PUBLIC "$<$<CXX_COMPILER_ID:MSVC>:-Zc:preprocessor>")

# Dependencies
find_package(TBB)
if(TBB_FOUND AND GAPP_LINK_TBB AND NOT MSVC)
target_link_libraries(gapp PUBLIC "TBB::tbb")
Expand Down
2 changes: 1 addition & 1 deletion src/utility/math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,4 @@ namespace gapp::math
return integral;
}

} // namespace gapp::math
} // namespace gapp::math
33 changes: 16 additions & 17 deletions src/utility/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@
/** Math utility classes and functions. */
namespace gapp::math
{
template<typename T>
inline constexpr T inf = std::numeric_limits<T>::infinity();

template<typename T>
inline constexpr T eps = std::numeric_limits<T>::epsilon();

template<typename T>
inline constexpr T small = std::numeric_limits<T>::min();

template<typename T>
inline constexpr T large = std::numeric_limits<T>::max();

using Point = std::vector<double>;


/**
* This class contains the global absolute and relative tolerance values used
* for comparing floating-point values in the GAs.
Expand All @@ -36,7 +51,7 @@ namespace gapp::math

private:
GAPP_API inline constinit static std::atomic<double> absolute_tolerance = 1E-12;
GAPP_API inline constinit static std::atomic<double> relative_tolerance = 10 * std::numeric_limits<double>::epsilon();
GAPP_API inline constinit static std::atomic<double> relative_tolerance = 10 * eps<double>;

friend class ScopedTolerances;
};
Expand Down Expand Up @@ -84,22 +99,6 @@ namespace gapp::math
double old_absolute_tolerance;
double old_relative_tolerance;
};


template<typename T>
inline constexpr T inf = std::numeric_limits<T>::infinity();

template<typename T>
inline constexpr T eps = std::numeric_limits<T>::epsilon();

template<typename T>
inline constexpr T small = std::numeric_limits<T>::min();

template<typename T>
inline constexpr T large = std::numeric_limits<T>::max();


using Point = std::vector<double>;


/* Comparison function for floating point numbers. Returns -1 if (lhs < rhs), +1 if (lhs > rhs), and 0 if (lhs == rhs). */
Expand Down
22 changes: 6 additions & 16 deletions src/utility/rng.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,9 @@ namespace gapp::rng
template<std::floating_point RealType = double>
inline RealType randomReal(RealType lbound, RealType ubound);

/** Generate a random floating-point value from a standard normal distribution. */
template<std::floating_point RealType = double>
inline RealType randomNormal();

/** Generate a random floating-point value from a normal distribution with the specified mean and std deviation. */
template<std::floating_point RealType = double>
inline RealType randomNormal(RealType mean, RealType SD);
inline RealType randomNormal(RealType mean = 0.0, RealType SD = 1.0);

/** Generate a random integer value from a binomial distribution with the parameters n and p. */
template<std::integral IntType = int>
Expand Down Expand Up @@ -192,21 +188,15 @@ namespace gapp::rng
return std::uniform_real_distribution{ lbound, ubound }(rng::prng);
}

template<std::floating_point RealType>
RealType randomNormal()
{
// keep the distribution for the state
thread_local std::normal_distribution<RealType> dist{ 0.0, 1.0 };

return dist(rng::prng);
}

template<std::floating_point RealType>
RealType randomNormal(RealType mean, RealType SD)
{
GAPP_ASSERT(SD >= 0.0);

return (SD == 0.0) ? mean : std::normal_distribution{ mean, SD }(rng::prng);
// keep the distribution for the state
thread_local std::normal_distribution<RealType> dist;

return SD * dist(rng::prng) + mean;
}

template<std::integral IntType>
Expand Down Expand Up @@ -298,7 +288,7 @@ namespace gapp::rng
GAPP_ASSERT(range_len >= count);

const bool select_many = (count > 0.6 * range_len);
const bool huge_range = range_len >= (1ull << 20);
const bool huge_range = (range_len >= 65536);

if (huge_range) [[unlikely]] return rng::sampleUniqueSet(lbound, ubound, count);

Expand Down
27 changes: 9 additions & 18 deletions src/utility/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,16 @@ namespace gapp::detail



namespace _
{
template<typename Derived, template<typename...> class BaseTempl>
struct is_derived_from_spec_impl
{
private:
template<typename... TArgs>
static std::true_type f(BaseTempl<TArgs...>*);

static std::false_type f(...);

public:
using type = decltype(f(static_cast<Derived*>(nullptr)));
};

} // namespace _

template<typename Derived, template<typename...> class BaseTempl>
using is_derived_from_spec_of = typename _::is_derived_from_spec_impl<Derived, BaseTempl>::type;
struct is_derived_from_spec_of
{
private:
template<typename... TArgs>
static std::true_type f(BaseTempl<TArgs...>*);
static std::false_type f(...);
public:
inline constexpr static bool value = decltype( f(static_cast<Derived*>(nullptr)) )::value;
};

template<typename Derived, template<typename...> class BaseTempl>
inline constexpr bool is_derived_from_spec_of_v = is_derived_from_spec_of<Derived, BaseTempl>::value;
Expand Down
8 changes: 2 additions & 6 deletions src/utility/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
#endif


#ifndef GAPP_NO_EXCEPTIONS
#if defined(__cpp_exceptions)
# define GAPP_THROW(exception_type, msg) throw exception_type(msg)
#else
# define GAPP_THROW(exception_type, msg) GAPP_UNREACHABLE()
Expand Down Expand Up @@ -157,13 +157,9 @@ namespace gapp::detail
{
return high - low;
}
else if (same_sign(low, high))
{
return high - low;
}
else
{
return static_cast<size_t>(-(low + 1)) + high + 1;
return same_sign(low, high) ? high - low : -(low + 1) + high + 1;
}
}

Expand Down

0 comments on commit cd227aa

Please sign in to comment.