tree-wide: Use our own macros instead of fabs()/fmax()/fmin()
To make this work, ABS() is made generic so it also works on floats and doubles. While at it, fold the __ABS_INTEGER indirection and the assert_cc(sizeof(long long) == sizeof(intmax_t)) away. The previous form switched between __builtin_llabs (clang) and __builtin_imaxabs (gcc), with the assert keeping the two paths behaviorally identical on every platform we build for. imaxabs was originally chosen because intmax_t is conceptually the widest signed integer type the platform exposes, but the _Generic ABS already casts to (long long) before the call, so the extra width imaxabs could in theory carry was being narrowed away immediately anyway. With both paths collapsed to __builtin_llabs((long long) (a)), the size relationship between long long and intmax_t is no longer relevant. Also add explicit unsigned long long / unsigned long / unsigned int cases that pass the argument through unchanged. The previous default branch cast unsigned values to (long long); for values above LLONG_MAX this reinterprets them as negative, and __builtin_llabs(LLONG_MIN) is UB. Unsigned values are already non-negative, so passing them through is both correct and avoids the narrowing. Smaller unsigned types (unsigned char, unsigned short) still go through the default branch but promote to int first and fit in long long losslessly. Co-developed-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -244,7 +244,7 @@ static int manager_adjust_clock(Manager *m, double offset, int leap_sec) {
|
||||
|
||||
/* For small deltas, tell the kernel to gradually adjust the system clock to the NTP time, larger
|
||||
* deltas are just directly set. */
|
||||
if (fabs(offset) < NTP_MAX_ADJUST) {
|
||||
if (ABS(offset) < NTP_MAX_ADJUST) {
|
||||
tmx = (struct timex) {
|
||||
.modes = ADJ_STATUS | ADJ_NANO | ADJ_OFFSET | ADJ_TIMECONST | ADJ_MAXERROR | ADJ_ESTERROR,
|
||||
.status = STA_PLL,
|
||||
@@ -346,7 +346,7 @@ static bool manager_sample_spike_detection(Manager *m, double offset, double del
|
||||
return false;
|
||||
|
||||
/* always accept offset if we are farther off than the round-trip delay */
|
||||
if (fabs(offset) > delay)
|
||||
if (ABS(offset) > delay)
|
||||
return false;
|
||||
|
||||
/* we need a few samples before looking at them */
|
||||
@@ -354,11 +354,11 @@ static bool manager_sample_spike_detection(Manager *m, double offset, double del
|
||||
return false;
|
||||
|
||||
/* do not accept anything worse than the maximum possible error of the best sample */
|
||||
if (fabs(offset) > m->samples[idx_min].delay)
|
||||
if (ABS(offset) > m->samples[idx_min].delay)
|
||||
return true;
|
||||
|
||||
/* compare the difference between the current offset to the previous offset and jitter */
|
||||
return fabs(offset - m->samples[idx_cur].offset) > 3 * jitter;
|
||||
return ABS(offset - m->samples[idx_cur].offset) > 3 * jitter;
|
||||
}
|
||||
|
||||
static void manager_adjust_poll(Manager *m, double offset, bool spike) {
|
||||
@@ -371,20 +371,20 @@ static void manager_adjust_poll(Manager *m, double offset, bool spike) {
|
||||
}
|
||||
|
||||
/* set to minimal poll interval */
|
||||
if (!spike && fabs(offset) > NTP_ACCURACY_SEC) {
|
||||
if (!spike && ABS(offset) > NTP_ACCURACY_SEC) {
|
||||
m->poll_interval_usec = m->poll_interval_min_usec;
|
||||
return;
|
||||
}
|
||||
|
||||
/* increase polling interval */
|
||||
if (fabs(offset) < NTP_ACCURACY_SEC * 0.25) {
|
||||
if (ABS(offset) < NTP_ACCURACY_SEC * 0.25) {
|
||||
if (m->poll_interval_usec < m->poll_interval_max_usec)
|
||||
m->poll_interval_usec *= 2;
|
||||
return;
|
||||
}
|
||||
|
||||
/* decrease polling interval */
|
||||
if (spike || fabs(offset) > NTP_ACCURACY_SEC * 0.75) {
|
||||
if (spike || ABS(offset) > NTP_ACCURACY_SEC * 0.75) {
|
||||
if (m->poll_interval_usec > m->poll_interval_min_usec)
|
||||
m->poll_interval_usec /= 2;
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user