use round() for rounding

Replace casting of values incremented by +0.5/-0.5 with round().
This commit is contained in:
Miroslav Lichvar 2021-09-23 10:01:50 +02:00
parent 9600993c28
commit d0d9a3fa43
4 changed files with 4 additions and 22 deletions

View file

@ -2604,7 +2604,7 @@ broadcast_timeout(void *arg)
int poll;
destination = ARR_GetElement(broadcasts, (long)arg);
poll = log(destination->interval) / log(2.0) + 0.5;
poll = round(log(destination->interval) / log(2.0));
UTI_ZeroNtp64(&orig_ts);
zero_local_timestamp(&recv_ts);

View file

@ -572,10 +572,7 @@ RCL_AddCookedPulse(RCL_Instance instance, struct timespec *cooked_time,
}
/* Align the offset to the reference sample */
if ((ref_sample.offset - offset) >= 0.0)
shift = (long)((ref_sample.offset - offset) * rate + 0.5) / (double)rate;
else
shift = (long)((ref_sample.offset - offset) * rate - 0.5) / (double)rate;
shift = round((ref_sample.offset - offset) * rate) / rate;
offset += shift;

View file

@ -97,21 +97,6 @@ static int have_setoffset;
updated in the kernel */
static int tick_update_hz;
/* ================================================== */
inline static long
our_round(double x)
{
long y;
if (x > 0.0)
y = x + 0.5;
else
y = x - 0.5;
return y;
}
/* ================================================== */
/* Positive means currently fast of true time, i.e. jump backwards */
@ -149,7 +134,7 @@ set_frequency(double freq_ppm)
double required_freq;
int required_delta_tick;
required_delta_tick = our_round(freq_ppm / dhz);
required_delta_tick = round(freq_ppm / dhz);
/* Older kernels (pre-2.6.18) don't apply the frequency offset exactly as
set by adjtimex() and a scaling constant (that depends on the internal

2
util.c
View file

@ -123,7 +123,7 @@ UTI_DoubleToTimeval(double a, struct timeval *b)
b->tv_sec = a;
frac_part = 1.0e6 * (a - b->tv_sec);
b->tv_usec = frac_part > 0 ? frac_part + 0.5 : frac_part - 0.5;
b->tv_usec = round(frac_part);
UTI_NormaliseTimeval(b);
}