forked from key4hep/EDM4hep
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the Vertex have a proper
type
bitfield instead of a primary
…
…field (key4hep#329) * Add bit field utility functions * Use bit field utilities in MCParticle * Make the Vertex primary field a proper type bitfield * Keep existing function name for less downstream breakage * Fix ExtraCode to actually get it generated properly * Add more utility functionality for checking / setting bitfields * Fix README links
- Loading branch information
Showing
5 changed files
with
209 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "edm4hep/utils/bit_utils.h" | ||
|
||
#include <catch2/catch_template_test_macros.hpp> | ||
#include <catch2/catch_test_macros.hpp> | ||
|
||
#include <cstdint> | ||
#include <tuple> | ||
|
||
// The integet types that we us as type fields in EDM4hep | ||
using BitFieldTypes = std::tuple<int32_t, uint32_t, int64_t, int16_t, uint64_t>; | ||
|
||
TEMPLATE_LIST_TEST_CASE("Bitfield utils set and get", "[bit_utils]", BitFieldTypes) { | ||
using namespace edm4hep; | ||
auto bitField = TestType{}; | ||
|
||
for (auto i = 0u; i < sizeof(TestType) * 8; ++i) { | ||
REQUIRE_FALSE(utils::checkBit(bitField, i)); | ||
} | ||
|
||
bitField = utils::setBit(bitField, 3, true); | ||
REQUIRE(utils::checkBit(bitField, 3)); | ||
|
||
bitField = utils::setBit(bitField, 4, true); | ||
REQUIRE(utils::checkBit(bitField, 3)); | ||
REQUIRE(utils::checkBit(bitField, 4)); | ||
|
||
bitField = utils::setBit(bitField, 3, false); | ||
REQUIRE_FALSE(utils::checkBit(bitField, 3)); | ||
REQUIRE(utils::checkBit(bitField, 4)); | ||
} | ||
|
||
TEMPLATE_LIST_TEST_CASE("Bitfield utils set multiple", "[bit_utils]", BitFieldTypes) { | ||
using namespace edm4hep; | ||
auto bitField = TestType{}; | ||
bitField = utils::setBits(bitField, true, 3, 4, 7); | ||
|
||
REQUIRE(utils::checkBit(bitField, 3)); | ||
REQUIRE(utils::checkBit(bitField, 4)); | ||
REQUIRE(utils::checkBit(bitField, 7)); | ||
REQUIRE_FALSE(utils::checkBit(bitField, 1)); | ||
REQUIRE_FALSE(utils::checkBit(bitField, 2)); | ||
REQUIRE_FALSE(utils::checkBit(bitField, 5)); | ||
REQUIRE_FALSE(utils::checkBit(bitField, 6)); | ||
REQUIRE_FALSE(utils::checkBit(bitField, 8)); | ||
} | ||
|
||
TEMPLATE_LIST_TEST_CASE("Bitfield utils check all ", "[bit_utils]", BitFieldTypes) { | ||
using namespace edm4hep; | ||
auto bitField = TestType{}; | ||
bitField = utils::setBits(bitField, true, 3, 4, 7); | ||
|
||
REQUIRE(utils::checkAllBits(bitField, 7, 3, 4)); | ||
REQUIRE(utils::checkAllBits(bitField, 3, 4)); | ||
REQUIRE_FALSE(utils::checkAllBits(bitField, 2, 3, 4, 7)); | ||
REQUIRE_FALSE(utils::checkAllBits(bitField, 2, 3, 4)); | ||
} | ||
|
||
TEMPLATE_LIST_TEST_CASE("Bitfield utils check any ", "[bit_utils]", BitFieldTypes) { | ||
using namespace edm4hep; | ||
auto bitField = TestType{}; | ||
bitField = utils::setBits(bitField, true, 3, 4, 7); | ||
|
||
REQUIRE(utils::checkAnyBits(bitField, 3, 4)); | ||
REQUIRE(utils::checkAnyBits(bitField, 3)); | ||
REQUIRE(utils::checkAnyBits(bitField, 1, 2, 3)); | ||
REQUIRE_FALSE(utils::checkAnyBits(bitField, 1, 2, 6, 8)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#ifndef EDM4HEP_UTILS_BIT_UTILS_HH | ||
#define EDM4HEP_UTILS_BIT_UTILS_HH | ||
|
||
#include <type_traits> | ||
|
||
namespace edm4hep::utils { | ||
|
||
/// Set a bit in the passed bitfield to the desired value | ||
/// | ||
/// @tparam T any integer type that can be used as a bitfield, typically an | ||
/// unsigned integer type | ||
/// | ||
/// @param bitfield The bitfield for which the bit should be set | ||
/// @param bit The bit (number) that should be set | ||
/// @param value The value to which the bit should be set | ||
/// | ||
/// @returns The new value of the bitfield after setting bits | ||
template <typename T> | ||
constexpr T setBit(T bitfield, int bit, bool value) { | ||
return (bitfield & ~(0x1 << bit)) | (value << bit); | ||
} | ||
|
||
/// Set multiple bits to one desired value in the passed bitfield | ||
/// | ||
/// @tparam T any integer type that can be used as a bitfield, typically an | ||
/// unsigned integer type | ||
/// @tparam Bits A variable number of bits (numbers) that should be set | ||
/// | ||
/// @param bitfield The bitfield for which the bit should be set | ||
/// @param value The value to which the bit should be set | ||
/// @param bits The bits that should be set | ||
template <typename T, typename... Bits> | ||
constexpr T setBits(T bitfield, bool value, Bits... bits) { | ||
static_assert((std::is_same_v<Bits, int> && ...), "All bit numbers to set must be integers"); | ||
static_assert(sizeof...(bits) > 0, "Need at least one bit to set"); | ||
|
||
for (auto n : {bits...}) { | ||
bitfield = setBit(bitfield, n, value); | ||
} | ||
return bitfield; | ||
} | ||
|
||
/// Check if a bit is set in the bitfield | ||
/// | ||
/// @tparam T any integer type that can be used as a bitfield, typically an | ||
/// unsigned integer type | ||
/// | ||
/// @param bitfield The bitfield that should be checked | ||
/// @param bit The bit (number) that should be checked | ||
/// | ||
/// @returns true if the passed bit is set in the bitfield and false otherwise | ||
template <typename T> | ||
constexpr bool checkBit(T bitfield, int bit) { | ||
return bitfield & (0x1 << bit); | ||
} | ||
|
||
/// Check if all the passed bits are set in the bitfield | ||
/// | ||
/// @tparam T any integer type that can be used as a bitfield, typically an | ||
/// unsigned integer type | ||
/// @tparam Bits A variable number of bits (numbers) that should be checked | ||
/// | ||
/// @param bitfield The bitfield that should be checked | ||
/// @param bits The bits that should be checked | ||
/// | ||
/// @returns true if all the passed bits are set in the bitfield and false | ||
/// otherwise | ||
template <typename T, typename... Bits> | ||
constexpr bool checkAllBits(T bitfield, Bits... bits) { | ||
static_assert((std::is_same_v<Bits, int> && ...), "All bit numbers to set must be integers"); | ||
static_assert(sizeof...(bits) > 0, "Need at least one bit to check"); | ||
return (true && ... && checkBit(bitfield, bits)); | ||
} | ||
|
||
/// Check if any of the passed bits is set in the bitfield | ||
/// | ||
/// @tparam T any integer type that can be used as a bitfield, typically an | ||
/// unsigned integer type | ||
/// @tparam Bits A variable number of bits (numbers) that should be checked | ||
/// | ||
/// @param bitfield The bitfield that should be checked | ||
/// @param bits The bits that should be checked | ||
/// | ||
/// @returns true if any of the passed bits is set in the bitfield and false | ||
/// otherwise | ||
template <typename T, typename... Bits> | ||
constexpr bool checkAnyBits(T bitfield, Bits... bits) { | ||
static_assert((std::is_same_v<Bits, int> && ...), "All bit numbers to set must be integers"); | ||
static_assert(sizeof...(bits) > 0, "Need at least one bit to check"); | ||
return (false || ... || checkBit(bitfield, bits)); | ||
} | ||
|
||
} // namespace edm4hep::utils | ||
|
||
#endif // EDM4HEP_UTILS_BIT_UTILS_HH |