Skip to content

Commit 5f4f95a

Browse files
f3schdavidrohr
authored andcommitted
GPU: add constexpr version of qStr2Tag
Signed-off-by: Felix Schlepper <felix.schlepper@cern.ch>
1 parent b62d3d6 commit 5f4f95a

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

GPU/GPUTracking/utils/strtag.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@
1515
#ifndef STRTAG_H
1616
#define STRTAG_H
1717

18-
#include <stdexcept>
18+
#include <cstring>
19+
#include <cstdint>
1920
#include <string>
21+
#include <type_traits>
2022

21-
template <class T = uint64_t>
22-
constexpr T qStr2Tag(const char* str)
23+
template <class T = uint64_t, std::size_t N>
24+
constexpr T qStr2Tag(const char (&str)[N])
2325
{
24-
if (strlen(str) != sizeof(T)) {
25-
throw std::runtime_error("Invalid tag length");
26+
static_assert(std::is_trivially_copyable_v<T>);
27+
static_assert(N - 1 == sizeof(T), "Invalid tag length");
28+
T value{};
29+
for (std::size_t i = 0; i < sizeof(T); ++i) {
30+
value |= T(static_cast<unsigned char>(str[i])) << (i * 8);
2631
}
27-
T tmp;
28-
for (uint32_t i = 0; i < sizeof(T); i++) {
29-
((char*)&tmp)[i] = str[i];
30-
}
31-
return tmp;
32+
return value;
3233
}
3334

3435
template <class T>

0 commit comments

Comments
 (0)