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