Skip to content

Commit 4afd072

Browse files
author
kevyuu
committed
implement constexpr findLSB
1 parent e0013cb commit 4afd072

File tree

4 files changed

+18
-25
lines changed

4 files changed

+18
-25
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,14 @@ requires concepts::IntegralScalar<T>
348348
struct find_lsb_helper<T>
349349
{
350350
using return_t = int32_t;
351-
static inline T __call(const T arg)
351+
NBL_CONSTEXPR_FUNC static inline T __call(const T arg)
352352
{
353+
if constexpr (std::is_constant_evaluated())
354+
{
355+
for (T ix = T(0); ix < sizeof(size_t) * 8; ix++)
356+
if ((T(1) << ix) & arg) return ix;
357+
return ~T(0);
358+
}
353359
return glm::findLSB<T>(arg);
354360
}
355361
};
@@ -369,7 +375,7 @@ requires std::is_enum_v<EnumType>
369375
struct find_lsb_helper<EnumType>
370376
{
371377
using return_t = int32_t;
372-
static int32_t __call(NBL_CONST_REF_ARG(EnumType) val)
378+
NBL_CONSTEXPR_FUNC static int32_t __call(NBL_CONST_REF_ARG(EnumType) val)
373379
{
374380
using underlying_t = std::underlying_type_t<EnumType>;
375381
return find_lsb_helper<underlying_t>::__call(static_cast<underlying_t>(val));

include/nbl/builtin/hlsl/cpp_compat/intrinsics.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ inline typename matrix_traits<Matrix>::scalar_type determinant(NBL_CONST_REF_ARG
6868
}
6969

7070
template<typename T>
71-
inline typename cpp_compat_intrinsics_impl::find_lsb_helper<T>::return_t findLSB(NBL_CONST_REF_ARG(T) val)
71+
NBL_CONSTEXPR_FUNC inline typename cpp_compat_intrinsics_impl::find_lsb_helper<T>::return_t findLSB(NBL_CONST_REF_ARG(T) val)
7272
{
7373
return cpp_compat_intrinsics_impl::find_lsb_helper<T>::__call(val);
7474
}

include/nbl/core/util/bitflag.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ namespace nbl::hlsl::cpp_compat_intrinsics_impl
6060
struct find_lsb_helper<core::bitflag<ENUM_TYPE>>
6161
{
6262
using return_t = int32_t;
63-
static return_t __call(NBL_CONST_REF_ARG(core::bitflag<ENUM_TYPE>) val)
63+
NBL_CONSTEXPR_FUNC static return_t __call(NBL_CONST_REF_ARG(core::bitflag<ENUM_TYPE>) val)
6464
{
6565
return find_lsb_helper<ENUM_TYPE>::__call(val.value);
6666
}

src/nbl/asset/ECommonEnums.cpp

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,6 @@
33
namespace nbl::asset
44
{
55

6-
constexpr static int32_t findLSB(size_t val)
7-
{
8-
if constexpr(std::is_constant_evaluated())
9-
{
10-
for (size_t ix=0ull; ix<sizeof(size_t)*8; ix++)
11-
if ((0x1ull << ix) & val) return ix;
12-
return ~0u;
13-
} else
14-
{
15-
return hlsl::findLSB(val);
16-
}
17-
}
18-
196
core::bitflag<PIPELINE_STAGE_FLAGS> allPreviousStages(core::bitflag<PIPELINE_STAGE_FLAGS> stages)
207
{
218
struct PerStagePreviousStages
@@ -49,7 +36,7 @@ core::bitflag<PIPELINE_STAGE_FLAGS> allPreviousStages(core::bitflag<PIPELINE_STA
4936

5037
constexpr void add(PIPELINE_STAGE_FLAGS stageFlag, PIPELINE_STAGE_FLAGS previousStageFlags)
5138
{
52-
const auto bitIx = findLSB(static_cast<size_t>(stageFlag));
39+
const auto bitIx = hlsl::findLSB(static_cast<size_t>(stageFlag));
5340
data[bitIx] |= previousStageFlags;
5441
}
5542

@@ -61,7 +48,7 @@ core::bitflag<PIPELINE_STAGE_FLAGS> allPreviousStages(core::bitflag<PIPELINE_STA
6148
core::bitflag<PIPELINE_STAGE_FLAGS> retval = PIPELINE_STAGE_FLAGS::NONE;
6249
while (bool(stages.value))
6350
{
64-
const auto bitIx = findLSB(static_cast<size_t>(stages.value));
51+
const auto bitIx = hlsl::findLSB(static_cast<size_t>(stages.value));
6552
retval |= bitToAccess[bitIx];
6653
stages ^= static_cast<PIPELINE_STAGE_FLAGS>(0x1u<<bitIx);
6754
}
@@ -101,7 +88,7 @@ core::bitflag<PIPELINE_STAGE_FLAGS> allLaterStages(core::bitflag<PIPELINE_STAGE_
10188

10289
constexpr void add(PIPELINE_STAGE_FLAGS stageFlag, PIPELINE_STAGE_FLAGS laterStageFlags)
10390
{
104-
const auto bitIx = findLSB(static_cast<size_t>(stageFlag));
91+
const auto bitIx = hlsl::findLSB(static_cast<size_t>(stageFlag));
10592
data[bitIx] |= laterStageFlags;
10693
}
10794

@@ -113,7 +100,7 @@ core::bitflag<PIPELINE_STAGE_FLAGS> allLaterStages(core::bitflag<PIPELINE_STAGE_
113100
core::bitflag<PIPELINE_STAGE_FLAGS> retval = PIPELINE_STAGE_FLAGS::NONE;
114101
while (bool(stages.value))
115102
{
116-
const auto bitIx = findLSB(static_cast<size_t>(stages.value));
103+
const auto bitIx = hlsl::findLSB(static_cast<size_t>(stages.value));
117104
retval |= bitToAccess[bitIx];
118105
stages ^= static_cast<PIPELINE_STAGE_FLAGS>(0x1u<<bitIx);
119106
}
@@ -179,7 +166,7 @@ core::bitflag<ACCESS_FLAGS> allAccessesFromStages(core::bitflag<PIPELINE_STAGE_F
179166

180167
constexpr void init(PIPELINE_STAGE_FLAGS stageFlag, ACCESS_FLAGS accessFlags)
181168
{
182-
const auto bitIx = findLSB(static_cast<size_t>(stageFlag));
169+
const auto bitIx = hlsl::findLSB(static_cast<size_t>(stageFlag));
183170
data[bitIx] = accessFlags;
184171
}
185172

@@ -193,7 +180,7 @@ core::bitflag<ACCESS_FLAGS> allAccessesFromStages(core::bitflag<PIPELINE_STAGE_F
193180
core::bitflag<ACCESS_FLAGS> retval = ACCESS_FLAGS::NONE;
194181
while (bool(stages.value))
195182
{
196-
const auto bitIx = findLSB(static_cast<size_t>(stages.value));
183+
const auto bitIx = hlsl::findLSB(static_cast<size_t>(stages.value));
197184
retval |= bitToAccess[bitIx];
198185
stages ^= static_cast<PIPELINE_STAGE_FLAGS>(0x1u<<bitIx);
199186
}
@@ -259,7 +246,7 @@ core::bitflag<PIPELINE_STAGE_FLAGS> allStagesFromAccesses(core::bitflag<ACCESS_F
259246
private:
260247
constexpr void init(ACCESS_FLAGS accessFlags, PIPELINE_STAGE_FLAGS stageFlags)
261248
{
262-
const auto bitIx = findLSB(static_cast<size_t>(accessFlags));
249+
const auto bitIx = hlsl::findLSB(static_cast<size_t>(accessFlags));
263250
data[bitIx] = stageFlags;
264251
}
265252

@@ -270,7 +257,7 @@ core::bitflag<PIPELINE_STAGE_FLAGS> allStagesFromAccesses(core::bitflag<ACCESS_F
270257
core::bitflag<PIPELINE_STAGE_FLAGS> retval = PIPELINE_STAGE_FLAGS::NONE;
271258
while (bool(accesses.value))
272259
{
273-
const auto bitIx = findLSB(static_cast<size_t>(accesses.value));
260+
const auto bitIx = hlsl::findLSB(static_cast<size_t>(accesses.value));
274261
retval |= bitToStage[bitIx];
275262
accesses ^= static_cast<ACCESS_FLAGS>(0x1u<<bitIx);
276263
}

0 commit comments

Comments
 (0)