Created
June 5, 2020 14:37
-
-
Save nmmmnu/ee7ca71d879fcb7072d8471be7309351 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdint> | |
#include <utility> | |
namespace convertor{ | |
constexpr uint16_t MAX_KEY = 0b00000000'00000011'11111111; // 1023 | |
constexpr uint32_t MAX_VAL = 0b00111111'11111111'11111111; // 4'194'303 | |
constexpr | |
uint16_t val_i16(uint16_t, uint32_t const val){ | |
return val & 0b11111111'11111111; | |
} | |
constexpr | |
uint16_t key_i16(uint16_t const key, uint32_t const val){ | |
uint32_t const x = val & 0b00000000'00111111'00000000'00000000; | |
return key & 0b00000011'11111111 | (x >> 6); | |
} | |
constexpr | |
uint16_t i16_key(uint16_t const key, uint16_t){ | |
return key & 0b00000011'11111111; | |
} | |
constexpr | |
uint32_t i16_val(uint16_t const key, uint16_t const val){ | |
uint32_t x = key & ~0b00000011'11111111; | |
return (x << 6) | val; | |
} | |
static_assert(i16_key(0b00000000'00000001, 0x00'0000) == 0b00000000'00000001, ""); | |
static_assert(i16_key(0b00000011'11111111, 0x00'0000) == 0b00000011'11111111, ""); | |
static_assert(i16_val(0b00000000'00000000, 0x00'0001) == 0x00'0001, ""); | |
static_assert(i16_val(0b00000000'00000000, 0x00'FFFF) == 0x00'FFFF, ""); | |
static_assert(i16_val(0b00000100'00000000, 0x00'0000) == 0x01'0000, ""); | |
static_assert(i16_val(0b00001100'00000000, 0x00'0000) == 0x03'0000, ""); | |
static_assert(i16_val(0b00011100'00000000, 0x00'0000) == 0x07'0000, ""); | |
static_assert(val_i16(0, 0x00'0001) == 0x0001, ""); | |
static_assert(val_i16(0, 0x01'FFFF) == 0xFFFF, ""); | |
static_assert(i16_val(0b00000000'00000000, 0x00'0001) == 0x00'0001, ""); | |
static_assert(i16_val(0b00000000'00000001, 0x00'FFFF) == 0x00'FFFF, ""); | |
static_assert(i16_val(0b00000100'00000000, 0x00'0000) == 0x01'0000, ""); | |
static_assert(i16_val(0b00001100'00000000, 0x00'0000) == 0x03'0000, ""); | |
static_assert(i16_val(0b00011100'00000000, 0x00'0000) == 0x07'0000, ""); | |
}; | |
int main(){ | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment