Skip to content

Add Constant Type and Operators - #222

Open
MrBurmark wants to merge 3 commits into
mainfrom
feature/burmark1/constants
Open

Add Constant Type and Operators#222
MrBurmark wants to merge 3 commits into
mainfrom
feature/burmark1/constants

Conversation

@MrBurmark

@MrBurmark MrBurmark commented Aug 15, 2026

Copy link
Copy Markdown
Member

Add a constant type that takes a compile-time value as a template parameter. Support arithmetic between constants that produces a constant.

Deprecate integral_constant in favor of constant.

constant<0> zero;
constant<1> one;
auto two = one + one;
static_assert(two == 2);
static_assert(std::same_as<decltype(two), constant<2>>);

The ultimate goal with this is use it as part of a RAJA::transparent_range class that I'm thinking about adding that stores the input begin, end, and stride exactly as they were given. My hope is to improve some cases in RAJAPerf and beyond where the BASE and RAJA differ by knowledge of such exact values. For example we often assume that ranges start at 0 but RAJA ranges always have a runtime begin value, knowledge of which does not cross the host device barrier. This is responsible for some differences between kernel which takes its ranges on the host and launch which takes ranges on the device.

Comment on lines +200 to +218
/**
* @brief constant yielding logical and for constant values
*/
template < auto lhs_value, auto rhs_value >
CAMP_HOST_DEVICE
constexpr auto operator&&(constant<lhs_value>, constant<rhs_value>)
{
return constant<lhs_value&&rhs_value>{};
}

/**
* @brief constant yielding logical or for constant values
*/
template < auto lhs_value, auto rhs_value >
CAMP_HOST_DEVICE
constexpr auto operator||(constant<lhs_value>, constant<rhs_value>)
{
return constant<lhs_value||rhs_value>{};
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This overloads logical and and or so operations between constants remain constants. However this does break short-circuiting if both operands are constants. I'm not sure if this is a big deal or not since presumably constants are generated in constant expressions. Though of course could always return constant from a function with side-effects. So I'm not sure if I'm going to keep these overloads.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trws Are there additional concerns related to template instantiations in constant expressions and things like that that short-circuiting also has a bearing on?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant