]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
util: introduce SQUARE macro
authorMiroslav Lichvar <mlichvar@redhat.com>
Fri, 17 Aug 2018 09:16:44 +0000 (11:16 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 21 Aug 2018 10:06:57 +0000 (12:06 +0200)
reference.c
samplefilt.c
smooth.c
sources.c
sourcestats.c
util.h

index 71737c885fc33ff23fd6111b9a976eb5ab97ef6e..d6eea69c810566b3c1b8b03f76e7838218610df3 100644 (file)
@@ -325,23 +325,6 @@ REF_GetLeapMode(void)
   return leap_mode;
 }
 
-/* ================================================== */
-
-static double
-Sqr(double x)
-{
-  return x*x;
-}
-
-/* ================================================== */
-#if 0
-static double
-Cube(double x)
-{
-  return x*x*x;
-}
-#endif
-
 /* ================================================== */
 /* Update the drift coefficients to the file. */
 
@@ -1055,8 +1038,8 @@ REF_SetReference(int stratum,
     /* Set new frequency based on weighted average of old and new skew. With
        manual reference the old frequency has no weight. */
 
-    old_weight = leap != LEAP_Unsynchronised ? 1.0 / Sqr(previous_skew) : 0.0;
-    new_weight = 3.0 / Sqr(new_skew);
+    old_weight = leap != LEAP_Unsynchronised ? 1.0 / SQUARE(previous_skew) : 0.0;
+    new_weight = 3.0 / SQUARE(new_skew);
 
     sum_weight = old_weight + new_weight;
 
@@ -1065,7 +1048,7 @@ REF_SetReference(int stratum,
     delta_freq1 = previous_freq - our_frequency;
     delta_freq2 = new_freq - our_frequency;
 
-    skew1 = sqrt((Sqr(delta_freq1) * old_weight + Sqr(delta_freq2) * new_weight) / sum_weight);
+    skew1 = sqrt((SQUARE(delta_freq1) * old_weight + SQUARE(delta_freq2) * new_weight) / sum_weight);
     skew2 = (previous_skew * old_weight + new_skew * new_weight) / sum_weight;
     our_skew = skew1 + skew2;
 
@@ -1125,11 +1108,11 @@ REF_SetReference(int stratum,
 
   /* Update the moving average of squares of offset, quickly on start */
   if (avg2_moving) {
-    avg2_offset += 0.1 * (our_offset * our_offset - avg2_offset);
+    avg2_offset += 0.1 * (SQUARE(our_offset) - avg2_offset);
   } else {
-    if (avg2_offset > 0.0 && avg2_offset < our_offset * our_offset)
+    if (avg2_offset > 0.0 && avg2_offset < SQUARE(our_offset))
       avg2_moving = 1;
-    avg2_offset = our_offset * our_offset;
+    avg2_offset = SQUARE(our_offset);
   }
 }
 
index 2c5f9365f3d472c9fd8ef71649add12736eff4f0..af0b34cd5e40ca900fc17be94553b476be4e7287 100644 (file)
@@ -76,7 +76,7 @@ SPF_CreateInstance(int min_samples, int max_samples, double max_dispersion, doub
   /* Set the first estimate to the system precision */
   filter->avg_var_n = 0;
   filter->avg_var = LCL_GetSysPrecisionAsQuantum() * LCL_GetSysPrecisionAsQuantum();
-  filter->max_var = max_dispersion * max_dispersion;
+  filter->max_var = SQUARE(max_dispersion);
   filter->combine_ratio = combine_ratio;
   filter->samples = MallocArray(NTP_Sample, filter->max_samples);
   filter->selected = MallocArray(int, filter->max_samples);
index a21dcd8830d4b8b011eded6009bdd1c64d960619..4125ea0d4d32cee4006d3a6e373ef9f755e2b68f 100644 (file)
--- a/smooth.c
+++ b/smooth.c
@@ -144,7 +144,7 @@ update_stages(void)
      is equal to the offset that should be smoothed out */
 
   s1 = smooth_offset / max_wander;
-  s2 = smooth_freq * smooth_freq / (2.0 * max_wander * max_wander);
+  s2 = SQUARE(smooth_freq) / (2.0 * SQUARE(max_wander));
   
   /* Calculate the lengths of the 1st and 3rd stage assuming there is no
      frequency limit.  The direction of the 1st stage is selected so that
index 1822912d456e5cf1cab1e6e482e0151e6b46d387..7df6279408ddaae68110fe97f1f7c5d59da7bcf3 100644 (file)
--- a/sources.c
+++ b/sources.c
@@ -550,7 +550,7 @@ combine_sources(int n_sel_sources, struct timespec *ref_time, double *offset,
     src_offset += elapsed * src_frequency;
     src_offset_sd += elapsed * src_frequency_sd;
     offset_weight = 1.0 / sources[index]->sel_info.root_distance;
-    frequency_weight = 1.0 / (src_frequency_sd * src_frequency_sd);
+    frequency_weight = 1.0 / SQUARE(src_frequency_sd);
 
     DEBUG_LOG("combining index=%d oweight=%e offset=%e osd=%e fweight=%e freq=%e fsd=%e skew=%e",
               index, offset_weight, src_offset, src_offset_sd,
@@ -558,13 +558,13 @@ combine_sources(int n_sel_sources, struct timespec *ref_time, double *offset,
 
     sum_offset_weight += offset_weight;
     sum_offset += offset_weight * src_offset;
-    sum2_offset_sd += offset_weight * (src_offset_sd * src_offset_sd +
+    sum2_offset_sd += offset_weight * (SQUARE(src_offset_sd) +
         (src_offset - *offset) * (src_offset - *offset));
 
     sum_frequency_weight += frequency_weight;
     sum_frequency += frequency_weight * src_frequency;
-    inv_sum2_frequency_sd += 1.0 / (src_frequency_sd * src_frequency_sd);
-    inv_sum2_skew += 1.0 / (src_skew * src_skew);
+    inv_sum2_frequency_sd += 1.0 / SQUARE(src_frequency_sd);
+    inv_sum2_skew += 1.0 / SQUARE(src_skew);
 
     combined++;
   }
index e13012a14ff8d734ea812b3fb427429c68eb17aa..2d28aff851cb98c78bffbd1c3e9ca1db7bc12bad 100644 (file)
@@ -553,7 +553,7 @@ SST_DoNewRegression(SST_Stats inst)
       sd_weight = 1.0;
       if (peer_distances[i] > min_distance)
         sd_weight += (peer_distances[i] - min_distance) / sd;
-      weights[i] = sd_weight * sd_weight;
+      weights[i] = SQUARE(sd_weight);
     }
   }
 
diff --git a/util.h b/util.h
index 4c55651b1297c68edc4a6cdecf03721a2eb82210..e3d6767da2d7c8482291bf859966e58efce9daee 100644 (file)
--- a/util.h
+++ b/util.h
@@ -200,4 +200,6 @@ extern void UTI_GetRandomBytes(void *buf, unsigned int len);
 /* Macro to clamp a value between two values */
 #define CLAMP(min, x, max) (MAX((min), MIN((x), (max))))
 
+#define SQUARE(x) ((x) * (x))
+
 #endif /* GOT_UTIL_H */