diff --git a/Chaos/Padding/PadderIso7816.hpp b/Chaos/Padding/PadderIso7816.hpp index 547f6e7..c5af640 100644 --- a/Chaos/Padding/PadderIso7816.hpp +++ b/Chaos/Padding/PadderIso7816.hpp @@ -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::max()) @@ -73,7 +73,7 @@ public: return { - .IsOkay_ = static_cast(isOkay), + .IsOkay_ = Branchless::ToBool(isOkay), .PadSize_ = Branchless::Sel(isOkay, padSize, 0) }; } diff --git a/Chaos/Padding/PadderPkcs7.hpp b/Chaos/Padding/PadderPkcs7.hpp index b25f554..325a01f 100644 --- a/Chaos/Padding/PadderPkcs7.hpp +++ b/Chaos/Padding/PadderPkcs7.hpp @@ -66,7 +66,7 @@ public: return { - .IsOkay_ = static_cast(isOkay), + .IsOkay_ = Branchless::ToBool(isOkay), .PadSize_ = Branchless::Sel(isOkay, padSize, 0) }; } diff --git a/Chaos/Service/Branchless.hpp b/Chaos/Service/Branchless.hpp index 3c3dff4..0ff13be 100644 --- a/Chaos/Service/Branchless.hpp +++ b/Chaos/Service/Branchless.hpp @@ -10,6 +10,9 @@ namespace Chaos::Service struct Branchless { + static constexpr uint8_t FalseMask = 0x00; + static constexpr uint8_t TrueMask = 0xFF; + template static constexpr OutUInt MsbMask(InUInt in) noexcept { @@ -79,6 +82,14 @@ struct Branchless return (mask & onTrue) | (~mask & onFalse); } + + template + static constexpr bool ToBool(UInt mask) noexcept + { + static_assert(std::is_unsigned_v); + + return Sel(mask, 1, 0); + } }; } // namespace Chaos::Service