Commit bbc6f78c authored by Flavio Ceolin's avatar Flavio Ceolin Committed by Anas Nashif
Browse files

lib: timeutil: Code guideline fixes



Fixes violations code guideline related with essential types rules:

- Ensure computations are done in the destination precision
- Avoid mixing signed and unsigned integers in computations and
  comparisons

Signed-off-by: default avatarFlavio Ceolin <flavio.ceolin@intel.com>
Signed-off-by: default avatarAbramo Bagnara <abramo.bagnara@bugseng.com>
parent 259c805c
Loading
Loading
Loading
Loading
+29 −19
Original line number Diff line number Diff line
@@ -36,8 +36,8 @@ static int64_t time_days_from_civil(int64_t y,
	}

	int64_t era = (y >= 0 ? y : y - 399) / 400;
	unsigned int yoe = y - era * 400;
	unsigned int doy = (153U * (m + (m > 2 ? -3 : 9)) + 2U) / 5U + d;
	unsigned int yoe = (unsigned int)(int64_t)(y - era * 400);
	unsigned int doy = (153U * (m > 2 ? m - 3 : m + 9) + 2U) / 5U + d;
	unsigned int doe = yoe * 365U + yoe / 4U - yoe / 100U + doy;

	return era * 146097 + (time_t)doe - 719468;
@@ -46,8 +46,8 @@ static int64_t time_days_from_civil(int64_t y,
int64_t timeutil_timegm64(const struct tm *tm)
{
	int64_t y = 1900 + (int64_t)tm->tm_year;
	unsigned int m = tm->tm_mon + 1;
	unsigned int d = tm->tm_mday - 1;
	unsigned int m = (unsigned int)tm->tm_mon + 1;
	unsigned int d = (unsigned int)tm->tm_mday - 1;
	int64_t ndays = time_days_from_civil(y, m, d);
	int64_t time = tm->tm_sec;

@@ -100,7 +100,7 @@ int timeutil_sync_state_set_skew(struct timeutil_sync_state *tsp, float skew,
{
	int rv = -EINVAL;

	if (skew > 0) {
	if (skew > 0.0F) {
		tsp->skew = skew;
		if (base != NULL) {
			tsp->base = *base;
@@ -114,15 +114,17 @@ int timeutil_sync_state_set_skew(struct timeutil_sync_state *tsp, float skew,

float timeutil_sync_estimate_skew(const struct timeutil_sync_state *tsp)
{
	float rv = 0;
	float rv = 0.0F;

	if ((tsp->base.ref != 0) && (tsp->latest.ref != 0)
	    && (tsp->latest.local > tsp->base.local)) {
		const struct timeutil_sync_config *cfg = tsp->cfg;
		double ref_delta = tsp->latest.ref - tsp->base.ref;
		double local_delta = tsp->latest.local - tsp->base.local;
		uint64_t uref_delta = tsp->latest.ref - tsp->base.ref;
		double ref_delta = (double)uref_delta;
		uint64_t ulocal_delta = tsp->latest.local - tsp->base.local;
		double local_delta = (double)ulocal_delta;

		rv = ref_delta * cfg->local_Hz / local_delta / cfg->ref_Hz;
		rv = (float)(ref_delta * (double)cfg->local_Hz / local_delta / (double)cfg->ref_Hz);
	}

	return rv;
@@ -133,22 +135,25 @@ int timeutil_sync_ref_from_local(const struct timeutil_sync_state *tsp,
{
	int rv = -EINVAL;

	if ((tsp->skew > 0) && (tsp->base.ref > 0) && (refp != NULL)) {
	if ((tsp->skew > 0.0F) && (tsp->base.ref > 0) && (refp != NULL)) {
		const struct timeutil_sync_config *cfg = tsp->cfg;
		int64_t local_delta = local - tsp->base.local;
		uint64_t udelta = local - tsp->base.local;
		int64_t local_delta = (int64_t)udelta;
		/* (x * 1.0) != x for large values of x.
		 * Therefore only apply the multiplication if the skew is not one.
		 */
		if (tsp->skew != 1.0f) {
			local_delta *= (double)tsp->skew;
			double ddelta = (double)local_delta * tsp->skew;

			local_delta = (int64_t)ddelta;
		}
		int64_t ref_delta = local_delta * cfg->ref_Hz / cfg->local_Hz;
		int64_t ref_delta = local_delta * (int64_t)cfg->ref_Hz / (int64_t)cfg->local_Hz;
		int64_t ref_abs = (int64_t)tsp->base.ref + ref_delta;

		if (ref_abs < 0) {
			rv = -ERANGE;
		} else {
			*refp = ref_abs;
			*refp = (uint64_t)ref_abs;
			rv = (tsp->skew != 1.0f) ? 1 : 0;
		}
	}
@@ -161,16 +166,19 @@ int timeutil_sync_local_from_ref(const struct timeutil_sync_state *tsp,
{
	int rv = -EINVAL;

	if ((tsp->skew > 0) && (tsp->base.ref > 0) && (localp != NULL)) {
	if ((tsp->skew > 0.0F) && (tsp->base.ref > 0) && (localp != NULL)) {
		const struct timeutil_sync_config *cfg = tsp->cfg;
		int64_t ref_delta = (int64_t)(ref - tsp->base.ref);
		uint64_t uref_delta = ref - tsp->base.ref;
		int64_t ref_delta = (int64_t)uref_delta;
		/* (x / 1.0) != x for large values of x.
		 * Therefore only apply the division if the skew is not one.
		 */
		int64_t local_delta = (ref_delta * cfg->local_Hz) / cfg->ref_Hz;
		int64_t local_delta = (ref_delta * (int64_t)cfg->local_Hz) / (int64_t)cfg->ref_Hz;

		if (tsp->skew != 1.0f) {
			local_delta /= (double)tsp->skew;
			double dlocal_delta = (double)local_delta / tsp->skew;

			local_delta = (int64_t)dlocal_delta;
		}
		int64_t local_abs = (int64_t)tsp->base.local
				    + (int64_t)local_delta;
@@ -184,7 +192,9 @@ int timeutil_sync_local_from_ref(const struct timeutil_sync_state *tsp,

int32_t timeutil_sync_skew_to_ppb(float skew)
{
	int64_t ppb64 = (int64_t)((1.0 - (double)skew) * 1E9);
	double ppbd = (1.0 - skew) * 1E9;

	int64_t ppb64 = (int64_t)ppbd;
	int32_t ppb32 = (int32_t)ppb64;

	return (ppb64 == ppb32) ? ppb32 : INT32_MIN;