Made lib/base conversion functions const where possible

This commit is contained in:
2024-04-28 17:41:00 +05:30
parent cce3259b56
commit 06a883d538
3 changed files with 28 additions and 26 deletions

View File

@@ -70,7 +70,7 @@ word_t word_bctoh(word_t w)
return y.h;
}
hword_t convert_bytes_to_hword(byte_t *bytes)
hword_t convert_bytes_to_hword(const byte_t *bytes)
{
hword_t h = 0;
for (size_t i = 0; i < HWORD_SIZE; ++i)
@@ -78,7 +78,7 @@ hword_t convert_bytes_to_hword(byte_t *bytes)
return h;
}
word_t convert_bytes_to_word(byte_t *bytes)
word_t convert_bytes_to_word(const byte_t *bytes)
{
word_t h = 0;
for (size_t i = 0; i < WORD_SIZE; ++i)

View File

@@ -104,7 +104,8 @@ static const int __i = 1;
#endif
/**
@brief Safely subtract SUB from W, where both are words (64 bit integers).
@brief Safely subtract SUB from W, where both are words (64 bit
integers).
@details In case of underflow (i.e. where W - SUB < 0) returns 0
instead of the underflowed result.
@@ -133,7 +134,7 @@ static const int __i = 1;
code format (little endian) and that they are at least HWORD_SIZE
in size.
*/
hword_t convert_bytes_to_hword(byte_t *buffer);
hword_t convert_bytes_to_hword(const byte_t *buffer);
/**
@brief Convert a half word into a VM byte code format bytes (big
@@ -143,7 +144,7 @@ hword_t convert_bytes_to_hword(byte_t *buffer);
@param buffer: Buffer to store into. We assume the buffer has at
least HWORD_SIZE space.
*/
void convert_hword_to_bytes(hword_t h, byte_t *buffer);
void convert_hword_to_bytes(const hword_t h, byte_t *buffer);
/**
@brief Convert a buffer of bytes to a word.
@@ -152,7 +153,7 @@ void convert_hword_to_bytes(hword_t h, byte_t *buffer);
code format (little endian) and that they are at least WORD_SIZE in
size.
*/
word_t convert_bytes_to_word(byte_t *);
word_t convert_bytes_to_word(const byte_t *);
/**
@brief Convert a word into a VM byte code format bytes (little
@@ -163,27 +164,27 @@ word_t convert_bytes_to_word(byte_t *);
@param buffer: Buffer to store into. We assume the buffer has at
least WORD_SIZE space.
*/
void convert_word_to_bytes(word_t w, byte_t *buffer);
void convert_word_to_bytes(const word_t w, byte_t *buffer);
/**
@brief Convert a half word into bytecode format (little endian)
*/
hword_t hword_htobc(hword_t);
hword_t hword_htobc(const hword_t);
/**
@brief Convert a half word in bytecode format (little endian) to
host format
*/
hword_t hword_bctoh(hword_t);
hword_t hword_bctoh(const hword_t);
/**
@brief Convert a word into bytecode format (little endian)
*/
word_t word_htobc(word_t);
word_t word_htobc(const word_t);
/**
@brief Convert a word in bytecode format (little endian) to host format
*/
word_t word_bctoh(word_t);
word_t word_bctoh(const word_t);
#endif

View File

@@ -18,17 +18,18 @@
void testing_lib_bytes_to_hword(void)
{
byte_t tests[][4] = {{0, 0, 0, 0},
{0xFF, 0xFF, 0xFF, 0xFF},
{1},
{0, 0, 0, 0b10000000},
{0x89, 0xab, 0xcd, 0xef}};
const byte_t tests[][4] = {{0, 0, 0, 0},
{0xFF, 0xFF, 0xFF, 0xFF},
{1},
{0, 0, 0, 0b10000000},
{0x89, 0xab, 0xcd, 0xef}};
hword_t expected[ARR_SIZE(tests)] = {0, HWORD_MAX, 1, 1 << 31, 0xefcdab89};
const hword_t expected[ARR_SIZE(tests)] = {0, HWORD_MAX, 1, 1 << 31,
0xefcdab89};
for (size_t i = 0; i < ARR_SIZE(tests); ++i)
{
hword_t got = convert_bytes_to_hword(tests[i]);
const hword_t got = convert_bytes_to_hword(tests[i]);
if (expected[i] != got)
{
FAIL(__func__, "[%lu] -> Expected 0x%x got 0x%x\n", i, expected[i], got);
@@ -40,18 +41,18 @@ void testing_lib_bytes_to_hword(void)
void testing_lib_bytes_to_word(void)
{
byte_t tests[][8] = {{0, 0, 0, 0},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0x01, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0b10000000},
{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}};
const byte_t tests[][8] = {{0, 0, 0, 0},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0x01, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0b10000000},
{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}};
word_t expected[ARR_SIZE(tests)] = {0, WORD_MAX, 1, 1LU << 63,
const word_t expected[ARR_SIZE(tests)] = {0, WORD_MAX, 1, 1LU << 63,
0xefcdab8967452301};
0xefcdab8967452301};
for (size_t i = 0; i < ARR_SIZE(tests); ++i)
{
word_t got = convert_bytes_to_word(tests[i]);
const word_t got = convert_bytes_to_word(tests[i]);
if (expected[i] != got)
{
FAIL(__func__, "[%lu] -> Expected 0x%lx got 0x%lx\n", i, expected[i],