Skip to content

Commit 8e8c55c

Browse files
committed
fix conditionalAbsOrMax
1 parent b2af579 commit 8e8c55c

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

include/nbl/builtin/hlsl/math/functions.hlsl

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,39 @@ bool partitionRandVariable(float leftProb, NBL_REF_ARG(float) xi, NBL_REF_ARG(fl
130130
}
131131

132132

133+
namespace impl
134+
{
133135
// TODO: impl signed integer versions
134136
// @ return abs(x) if cond==true, max(x,0.0) otherwise
135-
template <typename T NBL_FUNC_REQUIRES(is_floating_point_v<T> || concepts::FloatingPointVector<T> || concepts::FloatingPointVectorial<T>)
136-
T conditionalAbsOrMax(bool cond, T x, T limit);
137+
template<typename T NBL_PRIMARY_REQUIRES(is_floating_point_v<T> || concepts::FloatingPointVector<T> || concepts::FloatingPointVectorial<T>)
138+
struct ConditionalAbsOrMax;
139+
140+
template<>
141+
struct ConditionalAbsOrMax<float>
142+
{
143+
static float absOrMax(bool cond, float x, float limit)
144+
{
145+
const float condAbs = nbl::hlsl::bit_cast<float32_t, uint32_t>(nbl::hlsl::bit_cast<uint32_t, float32_t>(x) & uint32_t(cond ? 0x7fFFffFFu : 0xffFFffFFu));
146+
return nbl::hlsl::max<float>(condAbs,limit);
147+
}
148+
};
137149

138-
template <>
139-
float conditionalAbsOrMax<float>(bool cond, float x, float limit)
150+
template<uint32_t N>
151+
struct ConditionalAbsOrMax<vector<float, N> >
140152
{
141-
const float condAbs = nbl::hlsl::bit_cast<float32_t, uint32_t>(nbl::hlsl::bit_cast<uint32_t, float32_t>(x) & uint(cond ? 0x7fFFffFFu : 0xffFFffFFu));
142-
return nbl::hlsl::max<float>(condAbs,limit);
153+
static vector<float, N> absOrMax(bool cond, NBL_CONST_REF_ARG(vector<float, N>) x, NBL_CONST_REF_ARG(vector<float, N>) limit)
154+
{
155+
const vector<float, N> condAbs = nbl::hlsl::bit_cast<vector<float, N>, vector<uint32_t, N> >(nbl::hlsl::bit_cast<vector<uint32_t, N>, vector<float, N> >(x) & nbl::hlsl::mix((vector<uint32_t, N>)0x7fFFffFFu, (vector<uint32_t, N>)0xffFFffFFu, promote<vector<bool, N>, bool>(cond)));
156+
return nbl::hlsl::max<vector<float, N> >(condAbs,limit);
157+
}
158+
};
159+
143160
}
144161

145-
template <uint16_t N>
146-
vector<float, N> conditionalAbsOrMax<vector<float, N> >(bool cond, NBL_CONST_REF_ARG(vector<float, N>) x, NBL_CONST_REF_ARG(vector<float, N>) limit)
162+
template<typename T>
163+
T conditionalAbsOrMax(bool cond, NBL_CONST_REF_ARG(T) x, NBL_CONST_REF_ARG(T) limit)
147164
{
148-
const vector<float, N> condAbs = nbl::hlsl::bit_cast<vector<float, N>, vector<uint, N> >(nbl::hlsl::bit_cast<vector<uint, N>, vector<float, N> >(x) & nbl::hlsl::mix((vector<uint, N>)0x7fFFffFFu, (vector<uint, N>)0xffFFffFFu, promote<vector<bool, N>, bool>(cond)));
149-
return nbl::hlsl::max<vector<float, N> >(condAbs,limit);
165+
return impl::ConditionalAbsOrMax<T>::absOrMax(cond, x, limit);
150166
}
151167

152168

0 commit comments

Comments
 (0)