Add rz_num_align_delta()
This commit is contained in:
parent
7ce4721238
commit
ee1dfffae5
2 changed files with 30 additions and 0 deletions
|
|
@ -103,6 +103,21 @@ static inline st64 rz_num_abs(st64 num) {
|
|||
return num < 0 ? -num : num;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Padding to align v to the next alignment-boundary.
|
||||
* \return the least `d` such that `(v + d) % alignment == 0`.
|
||||
*/
|
||||
static inline ut64 rz_num_align_delta(ut64 v, ut64 alignment) {
|
||||
if (!alignment) {
|
||||
return 0;
|
||||
}
|
||||
ut64 excess = v % alignment;
|
||||
if (!excess) {
|
||||
return 0;
|
||||
}
|
||||
return alignment - excess;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -129,6 +129,20 @@ bool test_rz_num_str_split_list() {
|
|||
mu_end;
|
||||
}
|
||||
|
||||
bool test_rz_num_align_delta() {
|
||||
ut64 d = rz_num_align_delta(0, 8);
|
||||
mu_assert_eq(d, 0, "align delta");
|
||||
d = rz_num_align_delta(3, 8);
|
||||
mu_assert_eq(d, 5, "align delta");
|
||||
d = rz_num_align_delta(0x10, 8);
|
||||
mu_assert_eq(d, 0, "align delta");
|
||||
d = rz_num_align_delta(0x11, 8);
|
||||
mu_assert_eq(d, 7, "align delta");
|
||||
d = rz_num_align_delta(0x42, 0);
|
||||
mu_assert_eq(d, 0, "align delta");
|
||||
mu_end;
|
||||
}
|
||||
|
||||
bool all_tests() {
|
||||
mu_run_test(test_rz_num_units);
|
||||
mu_run_test(test_rz_num_minmax_swap_i);
|
||||
|
|
@ -137,6 +151,7 @@ bool all_tests() {
|
|||
mu_run_test(test_rz_num_str_len);
|
||||
mu_run_test(test_rz_num_str_split);
|
||||
mu_run_test(test_rz_num_str_split_list);
|
||||
mu_run_test(test_rz_num_align_delta);
|
||||
return tests_passed != tests_run;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue