binarybuffer: Invert buf_cmp* return value and rename to buf_eq*

The current semantics are a bit confusing, as the return value looks
like memcmp (0/false being equal) but the bool return type means one
likely expects true to mean equal. Make this clearer by switching them
out for buf_eq* functions that do that instead.

Checkpatch-ignore: UNSPECIFIED_INT
Change-Id: Iee0c5af794316aab5327cb9c168051fabd3bc1cb
Signed-off-by: Jessica Clarke <jrtc27@jrtc27.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/8490
Tested-by: jenkins
Reviewed-by: Evgeniy Naydanov <evgeniy.naydanov@syntacore.com>
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
This commit is contained in:
Jessica Clarke 2024-09-13 18:10:48 +01:00 committed by Antonio Borneo
parent 5159c59915
commit 1ae6b07b45
4 changed files with 20 additions and 20 deletions

View File

@ -57,49 +57,49 @@ void *buf_cpy(const void *from, void *_to, unsigned size)
return _to;
}
static bool buf_cmp_masked(uint8_t a, uint8_t b, uint8_t m)
static bool buf_eq_masked(uint8_t a, uint8_t b, uint8_t m)
{
return (a & m) != (b & m);
return (a & m) == (b & m);
}
static bool buf_cmp_trailing(uint8_t a, uint8_t b, uint8_t m, unsigned trailing)
static bool buf_eq_trailing(uint8_t a, uint8_t b, uint8_t m, unsigned trailing)
{
uint8_t mask = (1 << trailing) - 1;
return buf_cmp_masked(a, b, mask & m);
return buf_eq_masked(a, b, mask & m);
}
bool buf_cmp(const void *_buf1, const void *_buf2, unsigned size)
bool buf_eq(const void *_buf1, const void *_buf2, unsigned size)
{
if (!_buf1 || !_buf2)
return _buf1 != _buf2;
return _buf1 == _buf2;
unsigned last = size / 8;
if (memcmp(_buf1, _buf2, last) != 0)
return true;
return false;
unsigned trailing = size % 8;
if (!trailing)
return false;
return true;
const uint8_t *buf1 = _buf1, *buf2 = _buf2;
return buf_cmp_trailing(buf1[last], buf2[last], 0xff, trailing);
return buf_eq_trailing(buf1[last], buf2[last], 0xff, trailing);
}
bool buf_cmp_mask(const void *_buf1, const void *_buf2,
bool buf_eq_mask(const void *_buf1, const void *_buf2,
const void *_mask, unsigned size)
{
if (!_buf1 || !_buf2)
return _buf1 != _buf2 || _buf1 != _mask;
return _buf1 == _buf2 && _buf1 == _mask;
const uint8_t *buf1 = _buf1, *buf2 = _buf2, *mask = _mask;
unsigned last = size / 8;
for (unsigned i = 0; i < last; i++) {
if (buf_cmp_masked(buf1[i], buf2[i], mask[i]))
return true;
if (!buf_eq_masked(buf1[i], buf2[i], mask[i]))
return false;
}
unsigned trailing = size % 8;
if (!trailing)
return false;
return buf_cmp_trailing(buf1[last], buf2[last], mask[last], trailing);
return true;
return buf_eq_trailing(buf1[last], buf2[last], mask[last], trailing);
}
void *buf_set_ones(void *_buf, unsigned size)

View File

@ -172,8 +172,8 @@ static inline uint64_t buf_get_u64(const uint8_t *_buffer,
*/
uint32_t flip_u32(uint32_t value, unsigned width);
bool buf_cmp(const void *buf1, const void *buf2, unsigned size);
bool buf_cmp_mask(const void *buf1, const void *buf2,
bool buf_eq(const void *buf1, const void *buf2, unsigned size);
bool buf_eq_mask(const void *buf1, const void *buf2,
const void *mask, unsigned size);
/**

View File

@ -881,9 +881,9 @@ static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
int compare_failed;
if (in_check_mask)
compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
compare_failed = !buf_eq_mask(captured, in_check_value, in_check_mask, num_bits);
else
compare_failed = buf_cmp(captured, in_check_value, num_bits);
compare_failed = !buf_eq(captured, in_check_value, num_bits);
if (compare_failed) {
char *captured_str, *in_check_value_str;

View File

@ -932,7 +932,7 @@ static int svf_check_tdo(void)
index_var = svf_check_tdo_para[i].buffer_offset;
len = svf_check_tdo_para[i].bit_len;
if ((svf_check_tdo_para[i].enabled)
&& buf_cmp_mask(&svf_tdi_buffer[index_var], &svf_tdo_buffer[index_var],
&& !buf_eq_mask(&svf_tdi_buffer[index_var], &svf_tdo_buffer[index_var],
&svf_mask_buffer[index_var], len)) {
LOG_ERROR("tdo check error at line %d",
svf_check_tdo_para[i].line_num);