Skip to content

Commit 027662d

Browse files
committed
Added length function for emulated vectors
1 parent 5145bad commit 027662d

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

include/nbl/builtin/hlsl/cpp_compat/impl/intrinsics_impl.hlsl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,19 @@ struct bitReverseAs_helper<T NBL_PARTIAL_REQ_BOT(concepts::UnsignedIntegralScala
631631
}
632632
};
633633

634+
#define VECTORIAL_SPECIALIZATION_CONCEPT concepts::Vectorial<T> && !is_vector_v<T>
635+
template<typename T>
636+
NBL_PARTIAL_REQ_TOP(VECTORIAL_SPECIALIZATION_CONCEPT)
637+
struct length_helper<T NBL_PARTIAL_REQ_BOT(VECTORIAL_SPECIALIZATION_CONCEPT) >
638+
{
639+
using scalar_t = typename vector_traits<T>::scalar_type;
640+
static inline scalar_t __call(NBL_CONST_REF_ARG(T) vec)
641+
{
642+
return scalar_t::sqrt(dot_helper<T>::__call(vec, vec));
643+
}
644+
};
645+
#undef VECTORIAL_SPECIALIZATION_CONCEPT
646+
634647
#ifdef __HLSL_VERSION
635648
// SPIR-V already defines specializations for builtin vector types
636649
#define VECTOR_SPECIALIZATION_CONCEPT concepts::Vectorial<T> && !is_vector_v<T>

include/nbl/builtin/hlsl/emulated/float64_t.hlsl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,46 @@ namespace hlsl
395395
return bit_cast<this_t>(data ^ ieee754::traits<float64_t>::signMask);
396396
}
397397

398+
static this_t sqrt(this_t number)
399+
{
400+
// so it doesn't return NaN for -0.0
401+
bool isZero = !(number.data & 0x7FFFFFFFFFFFFFFFull);
402+
if (isZero)
403+
return number;
404+
405+
bool isNegative = (number.data >> 63) > 0;
406+
if (isNegative)
407+
return bit_cast<this_t>(ieee754::traits<this_t>::quietNaN);
408+
409+
if(!FastMath)
410+
{
411+
bool isInf = cpp_compat_intrinsics_impl::isinf_uint_impl(number.data);
412+
if (isInf)
413+
return number;
414+
}
415+
416+
// find square root initial guess using the fast inverse square root algorithm
417+
nbl::hlsl::emulated_float64_t<true, true> invSquareRoot = number;
418+
{
419+
int64_t i = 0x5fe6eb50c7b537a9ull - (number.data >> 1);
420+
invSquareRoot.data = i;
421+
422+
nbl::hlsl::emulated_float64_t<true, true> threeHalfs = emulated_float64_t<true, true>::create(1.5);
423+
nbl::hlsl::emulated_float64_t<true, true> x2 = number * emulated_float64_t<true, true>::create(0.5);
424+
invSquareRoot = invSquareRoot * (threeHalfs - (x2 * invSquareRoot * invSquareRoot));
425+
}
426+
427+
// find sqrt approximation using the Newton-Raphson method
428+
nbl::hlsl::emulated_float64_t<true, true> squareRoot = nbl::hlsl::emulated_float64_t<true, true>::create(1.0) / invSquareRoot;
429+
const int Iterations = 5;
430+
for (int i = 0; i < Iterations; ++i)
431+
{
432+
squareRoot = nbl::hlsl::emulated_float64_t<true, true>::create(0.5) * (squareRoot + number / squareRoot);
433+
}
434+
435+
return squareRoot;
436+
}
437+
398438
NBL_CONSTEXPR_STATIC bool isFastMathSupported = FastMath;
399439
};
400440

0 commit comments

Comments
 (0)