Skip to content

Commit 17d0717

Browse files
author
kevyuu
committed
Fix ternary_operation
1 parent ded5d8f commit 17d0717

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

include/nbl/builtin/hlsl/emulated/int64_t.hlsl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,7 @@ struct left_shift_operator<T NBL_PARTIAL_REQ_BOT(concepts::EmulatedIntegralScala
208208
const uint32_t shift = bigShift ? bits - ComponentBitWidth : ComponentBitWidth - bits;
209209
const type_t shifted = type_t::create(bigShift ? vector<uint32_t, 2>(0, operand.__getLSB() << shift)
210210
: vector<uint32_t, 2>(operand.__getLSB() << bits, (operand.__getMSB() << bits) | (operand.__getLSB() >> shift)));
211-
ternary_operator<type_t> ternary;
212-
return ternary(bool(bits), shifted, operand);
211+
return select<bool, type_t>(bool(bits), shifted, operand);
213212
}
214213

215214
// If `_bits > 63` or `_bits < 0` the result is undefined
@@ -235,8 +234,8 @@ struct arithmetic_right_shift_operator<emulated_uint64_t>
235234
const uint32_t shift = bigShift ? bits - ComponentBitWidth : ComponentBitWidth - bits;
236235
const type_t shifted = type_t::create(bigShift ? vector<uint32_t, 2>(operand.__getMSB() >> shift, 0)
237236
: vector<uint32_t, 2>((operand.__getMSB() << shift) | (operand.__getLSB() >> bits), operand.__getMSB() >> bits));
238-
ternary_operator<type_t> ternary;
239-
return ternary(bool(bits), shifted, operand);
237+
238+
return select<bool, type_t>(bool(bits), shifted, operand);
240239
}
241240

242241
// If `_bits > 63` the result is undefined
@@ -262,8 +261,7 @@ struct arithmetic_right_shift_operator<emulated_int64_t>
262261
const uint32_t shift = bigShift ? bits - ComponentBitWidth : ComponentBitWidth - bits;
263262
const type_t shifted = type_t::create(bigShift ? vector<uint32_t, 2>(uint32_t(int32_t(operand.__getMSB()) >> shift), int32_t(operand.__getMSB()) < 0 ? ~uint32_t(0) : uint32_t(0))
264263
: vector<uint32_t, 2>((operand.__getMSB() << shift) | (operand.__getLSB() >> bits), uint32_t(int32_t(operand.__getMSB()) >> bits)));
265-
ternary_operator<type_t> ternary;
266-
return ternary(bool(bits), shifted, operand);
264+
return select<bool , type_t>(bool(bits), shifted, operand);
267265
}
268266

269267
// If `_bits > 63` or `_bits < 0` the result is undefined

include/nbl/builtin/hlsl/functional.hlsl

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,16 +235,35 @@ struct maximum
235235
NBL_CONSTEXPR_STATIC_INLINE T identity = numeric_limits<scalar_t>::lowest; // TODO: `all_components<T>`
236236
};
237237

238-
template<typename T NBL_STRUCT_CONSTRAINABLE >
238+
#ifndef __HLSL_VERSION
239+
template<typename F1, typename F2 > requires(is_same_v<std::invoke_result_t<F1>, std::invoke_result_t<F2>()> )
239240
struct ternary_operator
240241
{
241-
using type_t = T;
242-
243-
NBL_CONSTEXPR_FUNC T operator()(NBL_CONST_REF_ARG(bool) condition, NBL_CONST_REF_ARG(T) lhs, NBL_CONST_REF_ARG(T) rhs)
244-
{
245-
return select<bool, T>(condition, lhs, rhs);
246-
}
242+
using type_t = std::invoke_result_t<F1>;
243+
244+
constexpr inline type_t operator()(const bool condition, const F1& lhs, const F2& rhs)
245+
{
246+
if (condition)
247+
return std::invoke(lhs);
248+
else
249+
return std::invoke(rhs);
250+
}
247251
};
252+
#else
253+
template<typename F1, typename F2 NBL_PRIMARY_REQUIRES(is_same_v<decltype(experimental::declval<F1>()()),decltype(experimental::declval<F2>()())> )
254+
struct ternary_operator
255+
{
256+
using type_t = decltype(experimental::declval<F1>().operator());
257+
258+
NBL_CONSTEXPR_FUNC type_t operator()(const bool condition, NBL_CONST_REF_ARG(F1) lhs, NBL_CONST_REF_ARG(F2) rhs)
259+
{
260+
if (condition)
261+
return lhs();
262+
else
263+
return rhs();
264+
}
265+
};
266+
#endif
248267

249268
// ----------------------------------------------------------------- SHIFT OPERATORS --------------------------------------------------------------------
250269

0 commit comments

Comments
 (0)