Skip to content

Commit 97e45ad

Browse files
Merge pull request #563 from Devsh-Graphics-Programming/numeric_limits
numeric_limits
2 parents ba832ee + 8b5ac57 commit 97e45ad

File tree

11 files changed

+386
-66
lines changed

11 files changed

+386
-66
lines changed

include/nbl/builtin/hlsl/bit.hlsl

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef _NBL_BUILTIN_HLSL_BIT_INCLUDED_
22
#define _NBL_BUILTIN_HLSL_BIT_INCLUDED_
33

4+
#include <nbl/builtin/hlsl/spirv_intrinsics/core.hlsl>
45
#include <nbl/builtin/hlsl/cpp_compat.hlsl>
56

67
#ifndef __HLSL_VERSION
@@ -12,6 +13,7 @@ namespace nbl::hlsl
1213
NBL_ALIAS_TEMPLATE_FUNCTION(std::rotl, rotl);
1314
NBL_ALIAS_TEMPLATE_FUNCTION(std::rotr, rotr);
1415
NBL_ALIAS_TEMPLATE_FUNCTION(std::countl_zero, countl_zero);
16+
NBL_ALIAS_TEMPLATE_FUNCTION(std::bit_cast, bit_cast);
1517

1618
}
1719
#else
@@ -20,6 +22,13 @@ namespace nbl
2022
namespace hlsl
2123
{
2224

25+
template<class T, class U>
26+
T bit_cast(U val)
27+
{
28+
static_assert(sizeof(T) <= sizeof(U));
29+
return spirv::bitcast<T, U>(val);
30+
}
31+
2332
template<typename T, typename S>
2433
T rotl(T x, S s);
2534
template<typename T, typename S>
@@ -57,21 +66,29 @@ T rotr(T x, S s)
5766
}
5867
}
5968

60-
template<typename T>
61-
uint16_t countl_zero(T n)
69+
namespace impl
6270
{
63-
uint16_t result = 0u;
64-
for(int32_t bits_log2=6; bits_log2>=0; bits_log2--)
65-
{
66-
const uint16_t shift = bits_log2 ? uint16_t(1)<<(bits_log2-1) : 0;
67-
const uint64_t loMask = bits_log2 ? (1ull<<shift)-1 : 0;
68-
const bool chooseHigh = n&(loMask<<shift);
69-
n = uint16_t((chooseHigh ? (n>shift):n)&loMask);
71+
template<uint16_t bits>
72+
uint16_t clz(uint64_t N)
73+
{
74+
static const uint64_t SHIFT = bits>>1;
75+
static const uint64_t LO_MASK = (1ull<<SHIFT)-1;
76+
const bool CHOOSE_HIGH = N & (LO_MASK<<SHIFT);
77+
const uint64_t NEXT = (CHOOSE_HIGH ? (N>>SHIFT):N)&LO_MASK;
78+
const uint16_t value = uint16_t(clz<SHIFT>(NEXT) + (CHOOSE_HIGH ? 0:SHIFT));
79+
return value;
80+
}
7081

71-
result += uint16_t(chooseHigh ? 0ull : shift);
72-
}
7382

74-
return result;
83+
template<>
84+
uint16_t clz<1>(uint64_t N) { return uint16_t(1u-N&1); }
85+
86+
}
87+
88+
template<typename T>
89+
uint16_t countl_zero(T n)
90+
{
91+
return impl::clz<sizeof(T)*8>(n);
7592
}
7693

7794
}

include/nbl/builtin/hlsl/cpp_compat.hlsl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#ifndef _NBL_BUILTIN_HLSL_CPP_COMPAT_INCLUDED_
22
#define _NBL_BUILTIN_HLSL_CPP_COMPAT_INCLUDED_
33

4+
#include <nbl/builtin/hlsl/macros.h>
45

56
#ifndef __HLSL_VERSION
67
#include <type_traits>
8+
#include <bit>
79

810
#define ARROW ->
911
#define NBL_CONSTEXPR constexpr
12+
#define NBL_CONSTEXPR_STATIC constexpr static
1013
#define NBL_CONSTEXPR_STATIC_INLINE constexpr static inline
1114

1215
#define NBL_ALIAS_TEMPLATE_FUNCTION(origFunctionName, functionAlias) \
@@ -33,7 +36,6 @@ using add_pointer = std::add_pointer<T>;
3336
// it includes vector and matrix
3437
#include <nbl/builtin/hlsl/cpp_compat/intrinsics.h>
3538

36-
3739
#else
3840

3941
#define ARROW .arrow().

include/nbl/builtin/hlsl/cpp_compat/matrix.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ NBL_TYPEDEF_MATRICES_FOR_SCALAR(int64_t);
7474
NBL_TYPEDEF_MATRICES_FOR_SCALAR(uint16_t);
7575
NBL_TYPEDEF_MATRICES_FOR_SCALAR(uint32_t);
7676
NBL_TYPEDEF_MATRICES_FOR_SCALAR(uint64_t);
77-
// TODO: halfMxN with std::float16_t
77+
NBL_TYPEDEF_MATRICES_FOR_SCALAR(float16_t);
7878
NBL_TYPEDEF_MATRICES_FOR_SCALAR(float32_t);
7979
NBL_TYPEDEF_MATRICES_FOR_SCALAR(float64_t);
8080

include/nbl/builtin/hlsl/cpp_compat/vector.hlsl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <glm/glm.hpp>
88
#include <glm/detail/_swizzle.hpp>
99
#include <stdint.h>
10+
#include <openexr/IlmBase/Half/half.h>
1011

1112
namespace nbl::hlsl
1213
{
@@ -32,6 +33,12 @@ using uint32_t1 = vector<uint32_t, 1>;
3233

3334
// TODO: halfN -> needs class implementation or C++23 std:float16_t
3435

36+
using float16_t = half;
37+
using float16_t4 = vector<float16_t, 4>;
38+
using float16_t3 = vector<float16_t, 3>;
39+
using float16_t2 = vector<float16_t, 2>;
40+
using float16_t1 = vector<float16_t, 1>;
41+
3542
using float32_t = float;
3643
using float32_t4 = vector<float32_t, 4>;
3744
using float32_t3 = vector<float32_t, 3>;

0 commit comments

Comments
 (0)