[systemd-commits] src/shared src/test src/timedate

Zbigniew Jędrzejewski-Szmek zbyszek at kemper.freedesktop.org
Fri Mar 14 18:44:36 PDT 2014


 src/shared/util.h            |    6 ++++++
 src/test/test-util.c         |   12 ++++++++++++
 src/timedate/timedate-sntp.c |   25 +++----------------------
 3 files changed, 21 insertions(+), 22 deletions(-)

New commits:
commit 8fe90522fb74dd84ff791a7546fee70047672396
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Fri Mar 14 21:43:56 2014 -0400

    timedated: use builtins for integer log and exp

diff --git a/src/shared/util.h b/src/shared/util.h
index c596d79..7752b1e 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -782,6 +782,12 @@ static inline unsigned u32ctz(uint32_t n) {
 #endif
 }
 
+static inline int log2i(int x) {
+        assert(x > 0);
+
+        return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
+}
+
 static inline bool logind_running(void) {
         return access("/run/systemd/seats/", F_OK) >= 0;
 }
diff --git a/src/test/test-util.c b/src/test/test-util.c
index 6297182..a624772 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -645,6 +645,17 @@ static void test_hexdump(void) {
         hexdump(stdout, data, sizeof(data));
 }
 
+static void test_log2i(void) {
+        assert_se(log2i(1) == 0);
+        assert_se(log2i(2) == 1);
+        assert_se(log2i(3) == 1);
+        assert_se(log2i(4) == 2);
+        assert_se(log2i(32) == 5);
+        assert_se(log2i(33) == 5);
+        assert_se(log2i(63) == 5);
+        assert_se(log2i(INT_MAX) == sizeof(int)*8-2);
+}
+
 int main(int argc, char *argv[]) {
         log_parse_environment();
         log_open();
@@ -687,6 +698,7 @@ int main(int argc, char *argv[]) {
         test_in_set();
         test_writing_tmpfile();
         test_hexdump();
+        test_log2i();
 
         return 0;
 }
diff --git a/src/timedate/timedate-sntp.c b/src/timedate/timedate-sntp.c
index 608177f..4d992c4 100644
--- a/src/timedate/timedate-sntp.c
+++ b/src/timedate/timedate-sntp.c
@@ -138,25 +138,6 @@ struct SNTPContext {
 
 static int sntp_arm_timer(SNTPContext *sntp);
 
-static int log2i(int a) {
-        int exp = 0;
-
-        assert(a > 0);
-
-        while (a > 0) {
-                a >>= 1;
-                exp++;
-        }
-
-        return exp;
-}
-
-static double log2d(int a) {
-        if (a < 0)
-                return 1.0 / (1UL << - a);
-        return 1UL << a;
-}
-
 static double ntp_ts_to_d(const struct ntp_ts *ts) {
         return be32toh(ts->sec) + ((double)be32toh(ts->frac) / UINT_MAX);
 }
@@ -531,21 +512,21 @@ static int sntp_receive_response(sd_event_source *source, int fd, uint32_t reven
                   "  dest         : %f\n"
                   "  offset       : %+f sec\n"
                   "  delay        : %+f sec\n"
-                  "  packet count : %llu\n"
+                  "  packet count : %"PRIu64"\n"
                   "  jitter/spike : %f (%s)\n"
                   "  poll interval: %llu\n",
                   NTP_FIELD_LEAP(ntpmsg->field),
                   NTP_FIELD_VERSION(ntpmsg->field),
                   NTP_FIELD_MODE(ntpmsg->field),
                   ntpmsg->stratum,
-                  log2d(ntpmsg->precision), ntpmsg->precision,
+                  exp2(ntpmsg->precision), ntpmsg->precision,
                   ntpmsg->stratum == 1 ? ntpmsg->refid : "n/a",
                   origin - OFFSET_1900_1970,
                   recv - OFFSET_1900_1970,
                   trans - OFFSET_1900_1970,
                   dest - OFFSET_1900_1970,
                   offset, delay,
-                  (unsigned long long)sntp->packet_count,
+                  sntp->packet_count,
                   sntp->samples_jitter, spike ? "yes" : "no",
                   sntp->poll_interval / USEC_PER_SEC);
 



More information about the systemd-commits mailing list