Skip to content

Commit 70a88fa

Browse files
author
kevyuu
committed
Refactor unary operator in hlsl functionals
1 parent 99b25ff commit 70a88fa

File tree

1 file changed

+29
-53
lines changed

1 file changed

+29
-53
lines changed

include/nbl/builtin/hlsl/functional.hlsl

Lines changed: 29 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -134,41 +134,6 @@ ALIAS_STD(divides,/)
134134
NBL_CONSTEXPR_STATIC_INLINE T identity = T(1);
135135
};
136136

137-
#ifndef __HLSL_VERSION
138-
139-
template<typename T>
140-
struct bit_not : std::bit_not<T>
141-
{
142-
using type_t = T;
143-
};
144-
145-
#else
146-
147-
template<typename T NBL_STRUCT_CONSTRAINABLE >
148-
struct bit_not
149-
{
150-
using type_t = T;
151-
152-
T operator()(NBL_CONST_REF_ARG(T) operand)
153-
{
154-
return ~operand;
155-
}
156-
};
157-
158-
// The default version above only works for fundamental scalars, vectors and matrices. This is because you can't call `~x` unless `x` is one of the former.
159-
// Similarly, calling `x.operator~()` is not valid for the aforementioned, and only for types overriding this operator. So, we need a specialization.
160-
template<typename T> NBL_PARTIAL_REQ_TOP(!(concepts::Scalar<T> || concepts::Vector<T> || concepts::Matrix<T>))
161-
struct bit_not<T NBL_PARTIAL_REQ_BOT(!(concepts::Scalar<T> || concepts::Vector<T> || concepts::Matrix<T>)) >
162-
{
163-
using type_t = T;
164-
165-
T operator()(NBL_CONST_REF_ARG(T) operand)
166-
{
167-
return operand.operator~();
168-
}
169-
};
170-
171-
#endif
172137

173138
ALIAS_STD(equal_to, ==) };
174139
ALIAS_STD(not_equal_to, !=) };
@@ -488,27 +453,38 @@ struct logical_right_shift_operator
488453
};
489454

490455
// ----------------------------------------------------------------- UNARY OPERATORS --------------------------------------------------------------------
491-
template<typename T NBL_STRUCT_CONSTRAINABLE>
492-
struct unary_minus_operator
493-
{
494-
using type_t = T;
495-
496-
NBL_CONSTEXPR_FUNC T operator()(NBL_CONST_REF_ARG(T) operand)
497-
{
498-
return operand.operator-();
499-
}
456+
#ifndef __HLSL_VERSION
457+
#define NBL_UNARY_OP_SPECIALIZATION(NAME, OP) template<typename T> \
458+
struct NAME : std::NAME<T> { \
459+
using type_t = T; \
500460
};
461+
#else
462+
#define NBL_UNARY_OP_SPECIALIZATION(NAME, OP) template<typename T NBL_STRUCT_CONSTRAINABLE> \
463+
struct NAME \
464+
{ \
465+
using type_t = T; \
466+
NBL_CONSTEXPR_FUNC T operator()(NBL_CONST_REF_ARG(T) operand) \
467+
{ \
468+
return operand.operator OP(); \
469+
} \
470+
}; \
471+
template<typename T> NBL_PARTIAL_REQ_TOP(concepts::Scalar<T> || concepts::Vector<T> || concepts::Matrix<T> ) \
472+
struct NAME<T NBL_PARTIAL_REQ_BOT(concepts::Scalar<T> || concepts::Vector<T> || concepts::Matrix<T> ) > \
473+
{ \
474+
using type_t = T; \
475+
NBL_CONSTEXPR_FUNC T operator()(const T operand) \
476+
{ \
477+
return (OP operand); \
478+
} \
479+
};
480+
#endif
481+
482+
NBL_UNARY_OP_SPECIALIZATION(bit_not, ~)
483+
NBL_UNARY_OP_SPECIALIZATION(negate, -)
501484

502-
template<typename T> NBL_PARTIAL_REQ_TOP(is_fundamental_v<T>)
503-
struct unary_minus_operator<T NBL_PARTIAL_REQ_BOT(is_fundamental_v<T>) >
504-
{
505-
using type_t = T;
506-
NBL_CONSTEXPR_FUNC T operator()(const T operand)
507-
{
508-
return -operand;
509-
}
510-
};
511485

486+
487+
#endif
512488
} //namespace nbl
513489
} //namespace hlsl
514490

0 commit comments

Comments
 (0)