https://github.com/tniessen/constexpr-secded
Header-only implementation of SECDED encoding as C/C++ constant expressions
https://github.com/tniessen/constexpr-secded
coding-theory constexpr memory-safety
Last synced: 9 months ago
JSON representation
Header-only implementation of SECDED encoding as C/C++ constant expressions
- Host: GitHub
- URL: https://github.com/tniessen/constexpr-secded
- Owner: tniessen
- License: mit
- Created: 2020-10-02T02:09:45.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-02-01T18:32:07.000Z (about 2 years ago)
- Last Synced: 2025-01-25T05:44:08.695Z (about 1 year ago)
- Topics: coding-theory, constexpr, memory-safety
- Language: C++
- Homepage:
- Size: 12.7 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# constexpr-secded
This is a header-only library that implements SECDED encoding through constant
expressions only, and was designed to enhance memory safety for low-level
systems with high memory error rates.
The implementation relies on Hamming codes with an additional parity bit. The
respective parity check matrices are listed in
[the code](include/constexpr-secded.h).
The code complies with the C89, C99, C11, C++11, C++14, and C++17 standards.
## C++ functions
These functions can be used in C++11 and newer.
#### `constexpr std::uint8_t secded_encode_4_bits(std::uint8_t v)`
> Encodes the lowest 4 bits of an unsigned integer into a 8-bit unsigned integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 4 bits.
#### `constexpr std::uint16_t secded_encode_4_bits(std::uint8_t v)`
> Encodes the lowest 4 bits of an unsigned integer into a 8-bit unsigned integer, which is then expanded to a 16-bit unsigned integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 8 bits, and these differences are distributed across at least 2 bytes.
#### `constexpr std::uint32_t secded_encode_4_bits(std::uint8_t v)`
> Encodes the lowest 4 bits of an unsigned integer into a 8-bit unsigned integer, which is then expanded to a 32-bit unsigned integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 16 bits, and these differences are distributed across at least 4 bytes.
#### `constexpr std::uint64_t secded_encode_4_bits(std::uint8_t v)`
> Encodes the lowest 4 bits of an unsigned integer into a 8-bit unsigned integer, which is then expanded to a 64-bit unsigned integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 32 bits, and these differences are distributed across at least 8 bytes.
#### `constexpr std::int8_t secded_encode_4_bits(std::int8_t v)`
> Encodes the lowest 4 bits of a signed integer into a 8-bit signed integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 4 bits.
#### `constexpr std::int16_t secded_encode_4_bits(std::int8_t v)`
> Encodes the lowest 4 bits of a signed integer into a 8-bit signed integer, which is then expanded to a 16-bit signed integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 8 bits, and these differences are distributed across at least 2 bytes.
#### `constexpr std::int32_t secded_encode_4_bits(std::int8_t v)`
> Encodes the lowest 4 bits of a signed integer into a 8-bit signed integer, which is then expanded to a 32-bit signed integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 16 bits, and these differences are distributed across at least 4 bytes.
#### `constexpr std::int64_t secded_encode_4_bits(std::int8_t v)`
> Encodes the lowest 4 bits of a signed integer into a 8-bit signed integer, which is then expanded to a 64-bit signed integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 32 bits, and these differences are distributed across at least 8 bytes.
#### `constexpr std::uint16_t secded_encode_11_bits(std::uint16_t v)`
> Encodes the lowest 11 bits of an unsigned integer into a 16-bit unsigned integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 4 bits.
#### `constexpr std::uint32_t secded_encode_11_bits(std::uint16_t v)`
> Encodes the lowest 11 bits of an unsigned integer into a 16-bit unsigned integer, which is then expanded to a 32-bit unsigned integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 8 bits, and these differences are distributed across at least 2 bytes.
#### `constexpr std::uint64_t secded_encode_11_bits(std::uint16_t v)`
> Encodes the lowest 11 bits of an unsigned integer into a 16-bit unsigned integer, which is then expanded to a 64-bit unsigned integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 16 bits, and these differences are distributed across at least 4 bytes.
#### `constexpr std::int16_t secded_encode_11_bits(std::int16_t v)`
> Encodes the lowest 11 bits of a signed integer into a 16-bit signed integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 4 bits.
#### `constexpr std::int32_t secded_encode_11_bits(std::int16_t v)`
> Encodes the lowest 11 bits of a signed integer into a 16-bit signed integer, which is then expanded to a 32-bit signed integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 8 bits, and these differences are distributed across at least 2 bytes.
#### `constexpr std::int64_t secded_encode_11_bits(std::int16_t v)`
> Encodes the lowest 11 bits of a signed integer into a 16-bit signed integer, which is then expanded to a 64-bit signed integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 16 bits, and these differences are distributed across at least 4 bytes.
#### `constexpr std::uint32_t secded_encode_26_bits(std::uint32_t v)`
> Encodes the lowest 26 bits of an unsigned integer into a 32-bit unsigned integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 4 bits.
#### `constexpr std::uint64_t secded_encode_26_bits(std::uint32_t v)`
> Encodes the lowest 26 bits of an unsigned integer into a 32-bit unsigned integer, which is then expanded to a 64-bit unsigned integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 8 bits, and these differences are distributed across at least 2 bytes.
#### `constexpr std::int32_t secded_encode_26_bits(std::int32_t v)`
> Encodes the lowest 26 bits of a signed integer into a 32-bit signed integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 4 bits.
#### `constexpr std::int64_t secded_encode_26_bits(std::int32_t v)`
> Encodes the lowest 26 bits of a signed integer into a 32-bit signed integer, which is then expanded to a 64-bit signed integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 8 bits, and these differences are distributed across at least 2 bytes.
#### `constexpr std::uint64_t secded_encode_57_bits(std::uint64_t v)`
> Encodes the lowest 57 bits of an unsigned integer into a 64-bit unsigned integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 4 bits.
#### `constexpr std::int64_t secded_encode_57_bits(std::int64_t v)`
> Encodes the lowest 57 bits of a signed integer into a 64-bit signed integer.
>
> If the argument `v` is a constant expression, then the call to this function is a constant expression as well.
>
> For `x != y`, applying this function to `x` and `y` separately will result in two values that differ in at least 4 bits.
## C/C++ macros
These macros can be used in C89 and newer. C++ code should use the safer C++
functions above instead.
#### `SECDED_4_BITS_TO_UNSIGNED_8(v)`
> Encodes the lowest 4 bits of an unsigned integer into a 8-bit unsigned integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 4 bits.
#### `SECDED_4_BITS_TO_SIGNED_8(v)`
> Encodes the lowest 4 bits of a signed integer into a 8-bit signed integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 4 bits.
#### `SECDED_4_BITS_TO_UNSIGNED_16(v)`
> Encodes the lowest 4 bits of an unsigned integer into a 8-bit unsigned integer, which is then expanded to a 16-bit unsigned integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 8 bits, and these differences are distributed across at least 2 bytes.
#### `SECDED_4_BITS_TO_SIGNED_16(v)`
> Encodes the lowest 4 bits of a signed integer into a 8-bit signed integer, which is then expanded to a 16-bit signed integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 8 bits, and these differences are distributed across at least 2 bytes.
#### `SECDED_4_BITS_TO_UNSIGNED_32(v)`
> Encodes the lowest 4 bits of an unsigned integer into a 8-bit unsigned integer, which is then expanded to a 32-bit unsigned integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 16 bits, and these differences are distributed across at least 4 bytes.
#### `SECDED_4_BITS_TO_SIGNED_32(v)`
> Encodes the lowest 4 bits of a signed integer into a 8-bit signed integer, which is then expanded to a 32-bit signed integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 16 bits, and these differences are distributed across at least 4 bytes.
#### `SECDED_4_BITS_TO_UNSIGNED_64(v)`
> Encodes the lowest 4 bits of an unsigned integer into a 8-bit unsigned integer, which is then expanded to a 64-bit unsigned integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 32 bits, and these differences are distributed across at least 8 bytes.
#### `SECDED_4_BITS_TO_SIGNED_64(v)`
> Encodes the lowest 4 bits of a signed integer into a 8-bit signed integer, which is then expanded to a 64-bit signed integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 32 bits, and these differences are distributed across at least 8 bytes.
#### `SECDED_11_BITS_TO_UNSIGNED_16(v)`
> Encodes the lowest 11 bits of an unsigned integer into a 16-bit unsigned integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 4 bits.
#### `SECDED_11_BITS_TO_SIGNED_16(v)`
> Encodes the lowest 11 bits of a signed integer into a 16-bit signed integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 4 bits.
#### `SECDED_11_BITS_TO_UNSIGNED_32(v)`
> Encodes the lowest 11 bits of an unsigned integer into a 16-bit unsigned integer, which is then expanded to a 32-bit unsigned integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 8 bits, and these differences are distributed across at least 2 bytes.
#### `SECDED_11_BITS_TO_SIGNED_32(v)`
> Encodes the lowest 11 bits of a signed integer into a 16-bit signed integer, which is then expanded to a 32-bit signed integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 8 bits, and these differences are distributed across at least 2 bytes.
#### `SECDED_11_BITS_TO_UNSIGNED_64(v)`
> Encodes the lowest 11 bits of an unsigned integer into a 16-bit unsigned integer, which is then expanded to a 64-bit unsigned integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 16 bits, and these differences are distributed across at least 4 bytes.
#### `SECDED_11_BITS_TO_SIGNED_64(v)`
> Encodes the lowest 11 bits of a signed integer into a 16-bit signed integer, which is then expanded to a 64-bit signed integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 16 bits, and these differences are distributed across at least 4 bytes.
#### `SECDED_26_BITS_TO_UNSIGNED_32(v)`
> Encodes the lowest 26 bits of an unsigned integer into a 32-bit unsigned integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 4 bits.
#### `SECDED_26_BITS_TO_SIGNED_32(v)`
> Encodes the lowest 26 bits of a signed integer into a 32-bit signed integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 4 bits.
#### `SECDED_26_BITS_TO_UNSIGNED_64(v)`
> Encodes the lowest 26 bits of an unsigned integer into a 32-bit unsigned integer, which is then expanded to a 64-bit unsigned integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 8 bits, and these differences are distributed across at least 2 bytes.
#### `SECDED_26_BITS_TO_SIGNED_64(v)`
> Encodes the lowest 26 bits of a signed integer into a 32-bit signed integer, which is then expanded to a 64-bit signed integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 8 bits, and these differences are distributed across at least 2 bytes.
#### `SECDED_57_BITS_TO_UNSIGNED_64(v)`
> Encodes the lowest 57 bits of an unsigned integer into a 64-bit unsigned integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 4 bits.
#### `SECDED_57_BITS_TO_SIGNED_64(v)`
> Encodes the lowest 57 bits of a signed integer into a 64-bit signed integer.
>
> Be aware that this is a macro that may evaluate `v` multiple times.
>
> For `x != y`, applying this macro to `x` and `y` separately will result in two values that differ in at least 4 bits.