ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
bit_encoder.hpp
1// This file is part of the ACTS project.
2//
3// Copyright (C) 2016 CERN for the benefit of the ACTS project
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#pragma once
10
11// Project include(s)
12#include "detray/definitions/detail/qualifiers.hpp"
13
14// System include(s)
15#include <cstdint>
16#include <type_traits>
17
18namespace detray::detail {
19
27template <typename value_t = std::uint64_t>
28class bit_encoder {
29 public:
30 bit_encoder() = delete;
31
37 template <value_t... masks>
38 DETRAY_HOST_DEVICE static constexpr bool is_invalid(value_t v) noexcept {
39 // All bits set to one in the range of a given mask defined as invalid
40 return (((v & masks) == masks) || ...);
41 }
42
49 template <value_t mask>
50 requires(mask != static_cast<value_t>(0))
51 DETRAY_HOST_DEVICE static constexpr value_t get_bits(
52 const value_t v) noexcept {
53 // Use integral constant to enforce compile time evaluation of shift
54 return (v & mask) >> extract_shift(mask);
55 }
56
61 template <value_t mask>
62 requires(mask != static_cast<value_t>(0))
63 DETRAY_HOST_DEVICE static constexpr void set_bits(value_t& v,
64 const value_t id) noexcept {
65 // Use integral constant to enforce compile time evaluation of shift
66 v = (v & ~mask) | ((id << extract_shift(mask)) & mask);
67
68 // Make sure, the value 'id' can be safely encoded with the bits
69 // specified by 'mask' (unless it is an invalid value which has all bits
70 // set to 1)
71 assert(((id == ~static_cast<value_t>(0)) ||
72 (id == static_cast<value_t>(~static_cast<unsigned int>(0))) ||
73 (get_bits<mask>(v) == id)) &&
74 "Not enough bits in mask to encode value");
75 }
76
77 private:
81 DETRAY_HOST_DEVICE
82 static constexpr int extract_shift(value_t mask) noexcept {
83 return __builtin_ctzll(mask);
84 }
85};
86
87} // namespace detray::detail
static constexpr bool is_invalid(value_t v) noexcept
Check whether v sets every bit in any of the supplied invalid masks.
Definition bit_encoder.hpp:38
static constexpr void set_bits(value_t &v, const value_t id) noexcept
Set the masked bits to id in the encoded value.
Definition bit_encoder.hpp:63
static constexpr value_t get_bits(const value_t v) noexcept
Extract the field selected by mask from an encoded value.
Definition bit_encoder.hpp:51
Mask a region on a surface and link it to a volume.
Definition mask.hpp:46