Skip to content

Commit

Permalink
Compile time improvements :)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pencilcaseman committed Nov 7, 2023
1 parent 4a5c7bf commit 6d6afa8
Show file tree
Hide file tree
Showing 11 changed files with 817 additions and 803 deletions.
18 changes: 7 additions & 11 deletions librapid/include/librapid/array/assignOps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,18 +354,14 @@ namespace librapid {

#if defined(LIBRAPID_HAS_CUDA)
namespace cuda {
template<typename T, typename std::enable_if_t<typetraits::TypeInfo<T>::type !=
::librapid::detail::LibRapidType::Scalar,
int> = 0>
template<typename T>
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto dataSourceExtractor(const T &obj) {
return obj.storage().begin();
}

template<typename T, typename std::enable_if_t<typetraits::TypeInfo<T>::type ==
::librapid::detail::LibRapidType::Scalar,
int> = 0>
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE const auto &dataSourceExtractor(const T &obj) {
return obj;
if constexpr(typetraits::TypeInfo<T>::type ==
::librapid::detail::LibRapidType::Scalar) {
return obj;
} else {
return obj.storage().begin();
}
}

template<typename T>
Expand Down
44 changes: 22 additions & 22 deletions librapid/include/librapid/array/operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Cosine function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto cos(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Cos, VAL> {
Expand All @@ -971,7 +971,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Tangent function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto tan(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Tan, VAL> {
Expand All @@ -986,7 +986,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Arcsine function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto asin(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Asin, VAL> {
Expand All @@ -1001,7 +1001,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Arccosine function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto acos(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Acos, VAL> {
Expand All @@ -1016,7 +1016,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Arctangent function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto atan(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Atan, VAL> {
Expand All @@ -1031,7 +1031,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Hyperbolic sine function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto sinh(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Sinh, VAL> {
Expand All @@ -1046,7 +1046,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Hyperbolic cosine function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto cosh(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Cosh, VAL> {
Expand All @@ -1061,7 +1061,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Hyperbolic tangent function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto tanh(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Tanh, VAL> {
Expand All @@ -1076,7 +1076,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Hyperbolic sine function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto asinh(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Asinh, VAL> {
Expand All @@ -1091,7 +1091,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Hyperbolic cosine function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto acosh(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Acosh, VAL> {
Expand All @@ -1106,7 +1106,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Hyperbolic tangent function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto atanh(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Atanh, VAL> {
Expand All @@ -1121,7 +1121,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Exponential function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto exp(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Exp, VAL> {
Expand All @@ -1136,7 +1136,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Exponential function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto exp2(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Exp2, VAL> {
Expand All @@ -1151,7 +1151,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Exponential function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto exp10(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Exp10, VAL> {
Expand All @@ -1166,7 +1166,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Natural logarithm function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto log(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Log, VAL> {
Expand All @@ -1181,7 +1181,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Base 10 logarithm function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto log10(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Log10, VAL> {
Expand All @@ -1196,7 +1196,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Base 2 logarithm function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto log2(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Log2, VAL> {
Expand All @@ -1211,7 +1211,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Square root function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto sqrt(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Sqrt, VAL> {
Expand All @@ -1226,7 +1226,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Cube root function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto cbrt(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Cbrt, VAL> {
Expand All @@ -1241,7 +1241,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Absolute value function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto abs(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Abs, VAL> {
Expand All @@ -1256,7 +1256,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Floor function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto floor(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Floor, VAL> {
Expand All @@ -1271,7 +1271,7 @@ namespace librapid {
/// \tparam VAL Type of the input
/// \param val The input array or function
/// \return Ceiling function object
template<class VAL, typename std::enable_if_t<IS_ARRAY_OP, int> = 0>
template<class VAL>
requires(detail::IsArrayOp<VAL>)
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto ceil(VAL &&val)
-> detail::Function<typetraits::DescriptorType_t<VAL>, detail::Ceil, VAL> {
Expand Down
12 changes: 6 additions & 6 deletions librapid/include/librapid/array/pseudoConstructors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ namespace librapid {
/// \tparam N Maximum number of dimensions of the Shape
/// \param shape Shape of the Array
/// \return Array filled with zeros
template<typename Scalar = double, typename Backend = backend::CPU, typename ShapeType = Shape,
typename std::enable_if_t<typetraits::IsSizeType<ShapeType>::value, int> = 0>
template<typename Scalar = double, typename Backend = backend::CPU, typename ShapeType = Shape>
requires(typetraits::IsSizeType<ShapeType>::value)
Array<Scalar, Backend> zeros(const ShapeType &shape) {
return Array<Scalar, Backend>(shape, Scalar(0));
}
Expand All @@ -79,8 +79,8 @@ namespace librapid {
/// \tparam N Maximum number of dimensions of the Shape
/// \param shape Shape of the Array
/// \return Array filled with ones
template<typename Scalar = double, typename Backend = backend::CPU, typename ShapeType = Shape,
typename std::enable_if_t<typetraits::IsSizeType<ShapeType>::value, int> = 0>
template<typename Scalar = double, typename Backend = backend::CPU, typename ShapeType = Shape>
requires(typetraits::IsSizeType<ShapeType>::value)
Array<Scalar, Backend> ones(const ShapeType &shape) {
return Array<Scalar, Backend>(shape, Scalar(1));
}
Expand Down Expand Up @@ -109,8 +109,8 @@ namespace librapid {
/// \tparam N Maximum number of dimensions of the Shape
/// \param shape Shape of the Array
/// \return Array filled with numbers from 0 to N-1
template<typename Scalar = int64_t, typename Backend = backend::CPU, typename ShapeType = Shape,
typename std::enable_if_t<typetraits::IsSizeType<ShapeType>::value, int> = 0>
template<typename Scalar = int64_t, typename Backend = backend::CPU, typename ShapeType = Shape>
requires(typetraits::IsSizeType<ShapeType>::value)
Array<Scalar, Backend> ordered(const ShapeType &shape) {
Array<Scalar, Backend> result(shape);
for (size_t i = 0; i < result.size(); i++) { result.storage()[i] = Scalar(i); }
Expand Down
37 changes: 17 additions & 20 deletions librapid/include/librapid/array/shape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,26 +783,10 @@ namespace librapid {
/// \param second Second input
/// \param shapes Remaining (optional) inputs
/// \return True if all inputs have the same shape, false otherwise
template<typename First, typename Second, typename... Rest,
typename std::enable_if_t<typetraits::IsSizeType<First>::value &&
typetraits::IsSizeType<Second>::value &&
(typetraits::IsSizeType<Rest>::value && ...),
int> = 0>
LIBRAPID_NODISCARD LIBRAPID_INLINE bool shapesMatch(const First &first, const Second &second,
const Rest &...shapes) {
if constexpr (sizeof...(Rest) == 0) {
return first == second;
} else {
return first == second && shapesMatch(first, shapes...);
}
}

/// \sa shapesMatch
template<typename First, typename Second, typename... Rest,
typename std::enable_if_t<typetraits::IsSizeType<First>::value &&
typetraits::IsSizeType<Second>::value &&
(typetraits::IsSizeType<Rest>::value && ...),
int> = 0>
template<typename First, typename Second, typename... Rest>
requires(typetraits::IsSizeType<First>::value &&
typetraits::IsSizeType<Second>::value &&
(typetraits::IsSizeType<Rest>::value && ...))
LIBRAPID_NODISCARD LIBRAPID_INLINE bool
shapesMatch(const std::tuple<First, Second, Rest...> &shapes) {
if constexpr (sizeof...(Rest) == 0) {
Expand All @@ -813,6 +797,19 @@ namespace librapid {
[](auto, auto, auto... rest) { return std::make_tuple(rest...); }, shapes));
}
}

/// \sa shapesMatch
// template<typename First, typename Second, typename... Rest>
// requires(typetraits::IsSizeType<First>::value && typetraits::IsSizeType<Second>::value &&
// (typetraits::IsSizeType<Rest>::value && ...))
// LIBRAPID_NODISCARD LIBRAPID_INLINE bool shapesMatch(const First &first, const Second &second,
// const Rest &...shapes) {
// if constexpr (sizeof...(Rest) == 0) {
// return first == second;
// } else {
// return first == second && shapesMatch(first, shapes...);
// }
// }

namespace detail {
template<typename First, typename Second>
Expand Down
Loading

0 comments on commit 6d6afa8

Please sign in to comment.