librz/util/num: handling minimum st64 in rz_num_abs (#4952)

This commit is contained in:
Tushar Jain 2025-03-02 20:14:09 +05:30 committed by GitHub
parent 58a26a7494
commit ea4bb2dfc9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 1 deletions

View file

@ -101,7 +101,14 @@ RZ_API size_t rz_num_base_of_string(RzNum *num, RZ_NONNULL const char *str);
RZ_API double rz_num_get_float(RzNum *num, const char *str);
RZ_API bool rz_num_is_hex_prefix(const char *p);
static inline st64 rz_num_abs(st64 num) {
/**
* \brief Absolute value of a 64-bit number. Store result in `ut64`
* \return unsigned 64-bit number
*/
static inline ut64 rz_num_abs(st64 num) {
if (num == ST64_MIN) {
return UT64_GT0;
}
return num < 0 ? -num : num;
}

View file

@ -169,6 +169,13 @@ bool test_rz_num_bitmask() {
mu_end;
}
bool test_rz_num_abs() {
mu_assert_eq(rz_num_abs(ST64_MAX), ST64_MAX, "rz_num_abs(2^63 - 1) = 2^63 - 1");
mu_assert_eq(rz_num_abs(0), 0, "rz_num_abs(0) = 0");
mu_assert_eq(rz_num_abs(ST64_MIN), UT64_GT0, "rz_num_abs(-2^63) = 2^63");
mu_end;
}
bool all_tests() {
mu_run_test(test_rz_num_units);
mu_run_test(test_rz_num_minmax_swap_i);
@ -179,6 +186,7 @@ bool all_tests() {
mu_run_test(test_rz_num_str_split_list);
mu_run_test(test_rz_num_align_delta);
mu_run_test(test_rz_num_bitmask);
mu_run_test(test_rz_num_abs);
return tests_passed != tests_run;
}