From bae3498cb38d20eb022f5e040248c307cbb67614 Mon Sep 17 00:00:00 2001 From: Jason Burmark Date: Fri, 14 Aug 2026 16:49:43 -0700 Subject: [PATCH 1/3] Add camp::constant and operators --- include/camp/number/number.hpp | 242 +++++++++++++++++++++++++++++++++ 1 file changed, 242 insertions(+) diff --git a/include/camp/number/number.hpp b/include/camp/number/number.hpp index 69532116..76c040be 100644 --- a/include/camp/number/number.hpp +++ b/include/camp/number/number.hpp @@ -27,6 +27,248 @@ struct integral_constant { constexpr value_type operator()() const noexcept { return value; } }; +/** + * @brief class that represents a compile time constant value. + * + * Some arithmetic operators are supported for this class so compile-time + * arithmetic may be done transparently. In addition, this class is implicitly + * convertible to its value type so it is usable in arithmetic with other types. + */ +template < auto t_value > +struct constant +{ + using type = constant; + using value_type = decltype(t_value); + static constexpr value_type value = t_value; + + CAMP_HOST_DEVICE + constexpr operator value_type() const noexcept { return value; } + + CAMP_HOST_DEVICE + constexpr value_type operator()() const noexcept { return value; } +}; + +/** + * @brief constant yielding unary plus for a constant value + */ +template < auto value > +CAMP_HOST_DEVICE +constexpr auto operator+(constant) +{ + return constant<+value>{}; +} + +/** + * @brief constant yielding unary minus for a constant value + */ +template < auto value > +CAMP_HOST_DEVICE +constexpr auto operator-(constant) +{ + return constant<-value>{}; +} + +/** + * @brief constant yielding bitwise not for a constant value + */ +template < auto value > +CAMP_HOST_DEVICE +constexpr auto operator~(constant) +{ + return constant<~value>{}; +} + +/** + * @brief constant yielding logical not for a constant value + */ +template < auto value > +CAMP_HOST_DEVICE +constexpr auto operator!(constant) +{ + return constant{}; +} + +/** + * @brief constant yielding plus for constant values + */ +template < auto lhs_value, auto rhs_value > +CAMP_HOST_DEVICE +constexpr auto operator+(constant, constant) +{ + return constant{}; +} + +/** + * @brief constant yielding minus for constant values + */ +template < auto lhs_value, auto rhs_value > +CAMP_HOST_DEVICE +constexpr auto operator-(constant, constant) +{ + return constant{}; +} + +/** + * @brief constant yielding times for constant values + */ +template < auto lhs_value, auto rhs_value > +CAMP_HOST_DEVICE +constexpr auto operator*(constant, constant) +{ + return constant{}; +} + +/** + * @brief constant yielding divide for constant values + */ +template < auto lhs_value, auto rhs_value > +CAMP_HOST_DEVICE +constexpr auto operator/(constant, constant) +{ + return constant{}; +} + +/** + * @brief constant yielding remainder for constant values + */ +template < auto lhs_value, auto rhs_value > +CAMP_HOST_DEVICE +constexpr auto operator%(constant, constant) +{ + return constant{}; +} + +/** + * @brief constant yielding bitwise and for constant values + */ +template < auto lhs_value, auto rhs_value > +CAMP_HOST_DEVICE +constexpr auto operator&(constant, constant) +{ + return constant{}; +} + +/** + * @brief constant yielding bitwise or for constant values + */ +template < auto lhs_value, auto rhs_value > +CAMP_HOST_DEVICE +constexpr auto operator|(constant, constant) +{ + return constant{}; +} + +/** + * @brief constant yielding bitwise xor for constant values + */ +template < auto lhs_value, auto rhs_value > +CAMP_HOST_DEVICE +constexpr auto operator^(constant, constant) +{ + return constant{}; +} + +/** + * @brief constant yielding left shift for constant values + */ +template < auto lhs_value, auto rhs_value > +CAMP_HOST_DEVICE +constexpr auto operator<<(constant, constant) +{ + return constant<(lhs_value<{}; +} + +/** + * @brief constant yielding right shift for constant values + */ +template < auto lhs_value, auto rhs_value > +CAMP_HOST_DEVICE +constexpr auto operator>>(constant, constant) +{ + return constant<(lhs_value>>rhs_value)>{}; +} + +/** + * @brief constant yielding logical and for constant values + */ +template < auto lhs_value, auto rhs_value > +CAMP_HOST_DEVICE +constexpr auto operator&&(constant, constant) +{ + return constant{}; +} + +/** + * @brief constant yielding logical or for constant values + */ +template < auto lhs_value, auto rhs_value > +CAMP_HOST_DEVICE +constexpr auto operator||(constant, constant) +{ + return constant{}; +} + +/** + * @brief constant yielding equals for constant values + */ +template < auto lhs_value, auto rhs_value > +CAMP_HOST_DEVICE +constexpr auto operator==(constant, constant) +{ + return constant{}; +} + +/** + * @brief constant yielding not equals for constant values + */ +template < auto lhs_value, auto rhs_value > +CAMP_HOST_DEVICE +constexpr auto operator!=(constant, constant) +{ + return constant{}; +} + +/** + * @brief constant yielding less than for constant values + */ +template < auto lhs_value, auto rhs_value > +CAMP_HOST_DEVICE +constexpr auto operator<(constant, constant) +{ + return constant<(lhs_value{}; +} + +/** + * @brief constant yielding less than equals for constant values + */ +template < auto lhs_value, auto rhs_value > +CAMP_HOST_DEVICE +constexpr auto operator<=(constant, constant) +{ + return constant<(lhs_value<=rhs_value)>{}; +} + +/** + * @brief constant yielding greater than for constant values + */ +template < auto lhs_value, auto rhs_value > +CAMP_HOST_DEVICE +constexpr auto operator>(constant, constant) +{ + return constant<(lhs_value>rhs_value)>{}; +} + +/** + * @brief constant yielding greater than equals for constant values + */ +template < auto lhs_value, auto rhs_value > +CAMP_HOST_DEVICE +constexpr auto operator>=(constant, constant) +{ + return constant<(lhs_value>=rhs_value)>{}; +} + + /** * @brief Short-form for a whole number * From 579cf4e4c7a3a1e90df30f0387446a4f49be4ef7 Mon Sep 17 00:00:00 2001 From: Jason Burmark Date: Fri, 14 Aug 2026 16:50:55 -0700 Subject: [PATCH 2/3] Stop using integral_constant in gen_seq --- include/camp/number.hpp | 43 ++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/include/camp/number.hpp b/include/camp/number.hpp index 2b223dd0..8b26f9ce 100644 --- a/include/camp/number.hpp +++ b/include/camp/number.hpp @@ -32,43 +32,46 @@ using idx_seq = int_seq; namespace detail { - template + template struct gen_seq; + #if CAMP_USE_MAKE_INTEGER_SEQ + template - struct gen_seq> { + struct gen_seq { using type = __make_integer_seq; }; + #elif CAMP_USE_INTEGER_PACK + template - struct gen_seq> { + struct gen_seq { using type = int_seq; }; + #else - template - struct concat; + template + struct gen_seq_concat; template - struct concat, int_seq> { - using type = typename int_seq::type; + struct gen_seq_concat, int_seq> { + using type = int_seq; }; - template - struct gen_seq - : concat>::type, - typename gen_seq< - T, - integral_constant>::type>:: - type { + template + struct gen_seq : int_seq { }; template - struct gen_seq> : int_seq { + struct gen_seq : int_seq { }; - template - struct gen_seq> : int_seq { + template + struct gen_seq + : gen_seq_concat::type, + typename gen_seq::type + >::type { }; #endif } // namespace detail @@ -77,7 +80,7 @@ namespace detail template struct make_idx_seq { using type = - typename detail::gen_seq>::type; + typename detail::gen_seq::type; }; // TODO: document @@ -108,7 +111,7 @@ using idx_seq_from_t = typename idx_seq_from>::type; // TODO: document template -struct make_int_seq : detail::gen_seq>::type { +struct make_int_seq : detail::gen_seq::type { }; // TODO: document From 7af5d32b98f45ed580b9758e949fa115da39fda8 Mon Sep 17 00:00:00 2001 From: Jason Burmark Date: Fri, 14 Aug 2026 16:51:13 -0700 Subject: [PATCH 3/3] Deprecate integral_constant and use constant --- include/camp/list/list.hpp | 2 +- include/camp/number/number.hpp | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/include/camp/list/list.hpp b/include/camp/list/list.hpp index 36542b5f..021f6b8d 100644 --- a/include/camp/list/list.hpp +++ b/include/camp/list/list.hpp @@ -34,7 +34,7 @@ namespace detail template struct _as_list> { - using type = list...>; + using type = list...>; }; } // namespace detail diff --git a/include/camp/number/number.hpp b/include/camp/number/number.hpp index 76c040be..e4c2b21c 100644 --- a/include/camp/number/number.hpp +++ b/include/camp/number/number.hpp @@ -15,15 +15,24 @@ namespace camp { -// TODO: document, consider making use/match std::integral_constant +// + +/** + * @brief class that represents a compile time constant value + * + * TODO: consider making use/match std::integral_constant + */ template -struct integral_constant { +struct [[deprecated("use constant instead of integral_constant")]] + integral_constant { static constexpr NumT value = v; using value_type = NumT; using type = integral_constant; + CAMP_HOST_DEVICE constexpr operator value_type() const noexcept { return value; } + CAMP_HOST_DEVICE constexpr value_type operator()() const noexcept { return value; } }; @@ -275,7 +284,7 @@ constexpr auto operator>=(constant, constant) * @tparam N The integral value */ template -using num = integral_constant; +using num = constant; using true_type = num; using false_type = num;