Skip to content

Commit aa9e24d

Browse files
author
kevyuu
committed
Add unary_minus_operator class
1 parent 1eded12 commit aa9e24d

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

include/nbl/builtin/hlsl/cpp_compat/basic.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <type_traits>
99

1010
#define ARROW ->
11+
#define NBL_DEREF_THIS (*this)
1112
#define NBL_CONSTEXPR constexpr // TODO: rename to NBL_CONSTEXPR_VAR
1213
#define NBL_CONSTEXPR_FUNC constexpr
1314
#define NBL_CONSTEXPR_STATIC constexpr static
@@ -44,6 +45,7 @@ namespace nbl::hlsl
4445
#else
4546

4647
#define ARROW .arrow().
48+
#define NBL_DEREF_THIS this
4749
#define NBL_CONSTEXPR const static // TODO: rename to NBL_CONSTEXPR_VAR
4850
#define NBL_CONSTEXPR_FUNC
4951
#define NBL_CONSTEXPR_STATIC const static

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,8 @@ struct emulated_int64_t
5353
constexpr explicit emulated_int64_t(const emulated_uint64_t& other);
5454
#endif
5555

56-
NBL_CONSTEXPR_FUNC this_t operator-() NBL_CONST_MEMBER_FUNC
57-
{
58-
storage_t inverted = ~data;
59-
return create(_static_cast<storage_t>(inverted)) + _static_cast<this_t>(1);
60-
}
56+
NBL_CONSTEXPR_FUNC emulated_int64_t operator-() NBL_CONST_MEMBER_FUNC;
57+
6158
};
6259

6360
// ------------------------------------------------ TYPE TRAITS SATISFIED -----------------------------------------------------
@@ -196,24 +193,25 @@ constexpr emulated_uint64_t::operator I() const noexcept
196193
template<typename T> NBL_PARTIAL_REQ_TOP(concepts::ImitationIntegral64Scalar<T>)
197194
struct left_shift_operator<T NBL_PARTIAL_REQ_BOT(concepts::ImitationIntegral64Scalar<T>) >
198195
{
196+
using type_t = T;
199197
NBL_CONSTEXPR_STATIC uint32_t ComponentBitWidth = uint32_t(8 * sizeof(uint32_t));
200198

201199
// Can't do generic templated definition, see:
202200
//https://github.com/microsoft/DirectXShaderCompiler/issues/7325
203201

204202
// If `_bits > 63` or `_bits < 0` the result is undefined
205-
NBL_CONSTEXPR_FUNC T operator()(NBL_CONST_REF_ARG(T) operand, uint32_t bits)
203+
NBL_CONSTEXPR_FUNC type_t operator()(NBL_CONST_REF_ARG(type_t) operand, uint32_t bits)
206204
{
207205
const bool bigShift = bits >= ComponentBitWidth; // Shift that completely rewrites LSB
208206
const uint32_t shift = bigShift ? bits - ComponentBitWidth : ComponentBitWidth - bits;
209-
const T shifted = T::create(bigShift ? vector<uint32_t, 2>(0, operand.__getLSB() << shift)
207+
const type_t shifted = type_t::create(bigShift ? vector<uint32_t, 2>(0, operand.__getLSB() << shift)
210208
: vector<uint32_t, 2>(operand.__getLSB() << bits, (operand.__getMSB() << bits) | (operand.__getLSB() >> shift)));
211-
ternary_operator<T> ternary;
209+
ternary_operator<type_t> ternary;
212210
return ternary(bool(bits), shifted, operand);
213211
}
214212

215213
// If `_bits > 63` or `_bits < 0` the result is undefined
216-
NBL_CONSTEXPR_FUNC T operator()(NBL_CONST_REF_ARG(T) operand, T bits)
214+
NBL_CONSTEXPR_FUNC type_t operator()(NBL_CONST_REF_ARG(type_t) operand, type_t bits)
217215
{
218216
return operator()(operand, _static_cast<uint32_t>(bits));
219217
}
@@ -381,6 +379,26 @@ NBL_CONSTEXPR_INLINE_VAR emulated_uint64_t minus_assign<emulated_uint64_t>::iden
381379
template<>
382380
NBL_CONSTEXPR_INLINE_VAR emulated_int64_t minus_assign<emulated_int64_t>::identity = minus<emulated_int64_t>::identity;
383381

382+
// --------------------------------- Unary operators ------------------------------------------
383+
// Specializations of the structs found in functional.hlsl
384+
template<>
385+
struct unary_minus_operator<emulated_int64_t>
386+
{
387+
using type_t = emulated_int64_t;
388+
389+
NBL_CONSTEXPR_FUNC type_t operator()(NBL_CONST_REF_ARG(type_t) operand)
390+
{
391+
using storage_t = type_t::storage_t;
392+
storage_t inverted = ~operand.data;
393+
return type_t::create(_static_cast<storage_t>(inverted)) + _static_cast<type_t>(1);
394+
}
395+
};
396+
397+
NBL_CONSTEXPR_INLINE_FUNC emulated_int64_t emulated_int64_t::operator-() NBL_CONST_MEMBER_FUNC
398+
{
399+
unary_minus_operator<emulated_int64_t> unaryMinus;
400+
return unaryMinus(NBL_DEREF_THIS);
401+
}
384402

385403
} //namespace nbl
386404
} //namespace hlsl

include/nbl/builtin/hlsl/functional.hlsl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,17 @@ struct logical_right_shift_operator
487487
}
488488
};
489489

490+
// ----------------------------------------------------------------- UNARY OPERATORS --------------------------------------------------------------------
491+
template<typename T NBL_STRUCT_CONSTRAINABLE>
492+
struct unary_minus_operator
493+
{
494+
using type_t = T;
490495

496+
NBL_CONSTEXPR_FUNC T operator()(NBL_CONST_REF_ARG(T) operand)
497+
{
498+
return -operand;
499+
}
500+
};
491501

492502
} //namespace nbl
493503
} //namespace hlsl

0 commit comments

Comments
 (0)