Merge branch 'branchless-tweaks'
Chaos Ci / test (push) Successful in 3m35s
Chaos Ci / benchmark (push) Successful in 1m55s

This commit is contained in:
hashlag
2026-08-30 02:08:24 +03:00
3 changed files with 15 additions and 4 deletions
+3 -3
View File
@@ -52,8 +52,8 @@ public:
uint8_t padSizeIncrement = 1;
uint8_t padSize = 0;
uint8_t encountered0x80 = 0x00;
uint8_t onlyZerosPast0x80 = 0xFF;
uint8_t encountered0x80 = Branchless::FalseMask;
uint8_t onlyZerosPast0x80 = Branchless::TrueMask;
InputIt it = end;
while (it != begin && padSize < std::numeric_limits<uint8_t>::max())
@@ -73,7 +73,7 @@ public:
return
{
.IsOkay_ = static_cast<bool>(isOkay),
.IsOkay_ = Branchless::ToBool(isOkay),
.PadSize_ = Branchless::Sel<uint8_t>(isOkay, padSize, 0)
};
}
+1 -1
View File
@@ -66,7 +66,7 @@ public:
return
{
.IsOkay_ = static_cast<bool>(isOkay),
.IsOkay_ = Branchless::ToBool(isOkay),
.PadSize_ = Branchless::Sel<uint8_t>(isOkay, padSize, 0)
};
}
+11
View File
@@ -10,6 +10,9 @@ namespace Chaos::Service
struct Branchless
{
static constexpr uint8_t FalseMask = 0x00;
static constexpr uint8_t TrueMask = 0xFF;
template<typename OutUInt, typename InUInt>
static constexpr OutUInt MsbMask(InUInt in) noexcept
{
@@ -79,6 +82,14 @@ struct Branchless
return (mask & onTrue) | (~mask & onFalse);
}
template<typename UInt>
static constexpr bool ToBool(UInt mask) noexcept
{
static_assert(std::is_unsigned_v<UInt>);
return Sel<UInt>(mask, 1, 0);
}
};
} // namespace Chaos::Service