src/zserio/BitFieldUtil.cpp
Line | Count | Source |
1 | | #include "zserio/BitFieldUtil.h" |
2 | | #include "zserio/CppRuntimeException.h" |
3 | | |
4 | | namespace zserio |
5 | | { |
6 | | |
7 | | static void checkBitFieldLength(size_t length) |
8 | 32 | { |
9 | 32 | if (length == 0 || length > 6428 ) |
10 | 8 | { |
11 | 8 | throw CppRuntimeException("Asking for bound of bitfield with invalid length ") << length << "!"; |
12 | 8 | } |
13 | 32 | } |
14 | | |
15 | | int64_t getBitFieldLowerBound(size_t length, bool isSigned) |
16 | 16 | { |
17 | 16 | checkBitFieldLength(length); |
18 | | |
19 | 16 | if (isSigned) |
20 | 6 | { |
21 | 6 | return -static_cast<int64_t>((UINT64_C(1) << (length - 1)) - 1) - 1; |
22 | 6 | } |
23 | 10 | else |
24 | 10 | { |
25 | 10 | return 0; |
26 | 10 | } |
27 | 16 | } |
28 | | |
29 | | uint64_t getBitFieldUpperBound(size_t length, bool isSigned) |
30 | 16 | { |
31 | 16 | checkBitFieldLength(length); |
32 | | |
33 | 16 | if (isSigned) |
34 | 6 | { |
35 | 6 | return (UINT64_C(1) << (length - 1)) - 1; |
36 | 6 | } |
37 | 10 | else |
38 | 10 | { |
39 | 10 | return length == 64 ? UINT64_MAX : ((UINT64_C(1) << length) - 1)9 ; |
40 | 10 | } |
41 | 16 | } |
42 | | |
43 | | } // namespace zserio |