Module: xenomai-forge
Branch: master
Commit: 9d2fbd5a88bd09760239ff175d437dccc4b4d16b
URL:    
http://git.xenomai.org/?p=xenomai-forge.git;a=commit;h=9d2fbd5a88bd09760239ff175d437dccc4b4d16b

Author: Philippe Gerum <r...@xenomai.org>
Date:   Sat Nov 26 14:55:47 2011 +0100

copperplate/clockobj: optimize ticks->timespec conversion

---

 include/copperplate/clockobj.h |  128 ++++++++++++++++++++++++++--------------
 lib/copperplate/clockobj.c     |   35 +++++------
 2 files changed, 101 insertions(+), 62 deletions(-)

diff --git a/include/copperplate/clockobj.h b/include/copperplate/clockobj.h
index 287eed6..e3180d9 100644
--- a/include/copperplate/clockobj.h
+++ b/include/copperplate/clockobj.h
@@ -105,9 +105,6 @@ void clockobj_get_date(struct clockobj *clkobj,
 void clockobj_get_time(struct clockobj *clkobj,
                       ticks_t *pticks, ticks_t *ptsc);
 
-void clockobj_ticks_to_timespec(struct clockobj *clkobj,
-                               ticks_t ticks, struct timespec *ts);
-
 void __clockobj_ticks_to_timeout(struct clockobj *clkobj, clockid_t clk_id,
                                 ticks_t ticks, struct timespec *ts);
 
@@ -139,6 +136,64 @@ int clockobj_destroy(struct clockobj *clkobj);
 }
 #endif
 
+#ifdef CONFIG_XENO_COBALT
+
+#include <asm/xenomai/arith.h>
+#include <asm-generic/xenomai/timeconv.h>
+
+static inline ticks_t clockobj_get_tsc(void)
+{
+       /* Guaranteed to be the source of CLOCK_COPPERPLATE. */
+       return __xn_rdtsc();
+}
+
+static inline sticks_t clockobj_ns_to_tsc(sticks_t ns)
+{
+       return xnarch_ns_to_tsc(ns);
+}
+
+static inline sticks_t clockobj_tsc_to_ns(sticks_t tsc)
+{
+       return xnarch_tsc_to_ns(tsc);
+}
+
+static inline
+void clockobj_ns_to_timespec(ticks_t ns, struct timespec *ts)
+{
+       unsigned long rem;
+
+       ts->tv_sec = xnarch_divrem_billion(ns, &rem);
+       ts->tv_nsec = rem;
+}
+
+#else /* CONFIG_XENO_MERCURY */
+
+static inline ticks_t clockobj_get_tsc(void)
+{
+       struct timespec now;
+       __RT(clock_gettime(CLOCK_COPPERPLATE, &now));
+       return (ticks_t)now.tv_sec * 1000000000ULL + now.tv_nsec;
+}
+
+static inline sticks_t clockobj_ns_to_tsc(sticks_t ns)
+{
+       return ns;
+}
+
+static inline sticks_t clockobj_tsc_to_ns(sticks_t tsc)
+{
+       return tsc;
+}
+
+static inline
+void clockobj_ns_to_timespec(ticks_t ns, struct timespec *ts)
+{
+       ts->tv_sec = ns / 1000000000ULL;
+       ts->tv_nsec = ns - (ts->tv_sec * 1000000000ULL);
+}
+
+#endif /* CONFIG_XENO_MERCURY */
+
 static inline
 void clockobj_ticks_to_timeout(struct clockobj *clkobj,
                               ticks_t ticks, struct timespec *ts)
@@ -149,6 +204,20 @@ void clockobj_ticks_to_timeout(struct clockobj *clkobj,
 #ifdef CONFIG_XENO_LORES_CLOCK_DISABLED
 
 static inline
+void __clockobj_ticks_to_timespec(struct clockobj *clkobj,
+                                 ticks_t ticks, struct timespec *ts)
+{
+       clockobj_ns_to_timespec(ticks, ts);
+}
+
+static inline
+void clockobj_ticks_to_timespec(struct clockobj *clkobj,
+                               ticks_t ticks, struct timespec *ts)
+{
+       __clockobj_ticks_to_timespec(clkobj, ticks, ts);
+}
+
+static inline
 int __clockobj_set_resolution(struct clockobj *clkobj,
                              unsigned int resolution_ns)
 {
@@ -186,6 +255,18 @@ static inline sticks_t clockobj_ticks_to_ns(struct 
clockobj *clkobj,
 
 #else /* !CONFIG_XENO_LORES_CLOCK_DISABLED */
 
+void __clockobj_ticks_to_timespec(struct clockobj *clkobj,
+                                 ticks_t ticks, struct timespec *ts);
+
+static inline
+void clockobj_ticks_to_timespec(struct clockobj *clkobj,
+                               ticks_t ticks, struct timespec *ts)
+{
+       read_lock_nocancel(&clkobj->lock);
+       __clockobj_ticks_to_timespec(clkobj, ticks, ts);
+       read_unlock(&clkobj->lock);
+}
+
 static inline
 int __clockobj_set_resolution(struct clockobj *clkobj,
                              unsigned int resolution_ns)
@@ -219,45 +300,4 @@ static inline sticks_t clockobj_ticks_to_ns(struct 
clockobj *clkobj,
 
 #endif /* !CONFIG_XENO_LORES_CLOCK_DISABLED */
 
-#ifdef CONFIG_XENO_COBALT
-
-#include <asm-generic/xenomai/timeconv.h>
-
-static inline ticks_t clockobj_get_tsc(void)
-{
-       /* Guaranteed to be the source of CLOCK_COPPERPLATE. */
-       return __xn_rdtsc();
-}
-
-static inline sticks_t clockobj_ns_to_tsc(sticks_t ns)
-{
-       return xnarch_ns_to_tsc(ns);
-}
-
-static inline sticks_t clockobj_tsc_to_ns(sticks_t tsc)
-{
-       return xnarch_tsc_to_ns(tsc);
-}
-
-#else /* CONFIG_XENO_MERCURY */
-
-static inline ticks_t clockobj_get_tsc(void)
-{
-       struct timespec now;
-       __RT(clock_gettime(CLOCK_COPPERPLATE, &now));
-       return (ticks_t)now.tv_sec * 1000000000ULL + now.tv_nsec;
-}
-
-static inline sticks_t clockobj_ns_to_tsc(sticks_t ns)
-{
-       return ns;
-}
-
-static inline sticks_t clockobj_tsc_to_ns(sticks_t tsc)
-{
-       return tsc;
-}
-
-#endif /* CONFIG_XENO_MERCURY */
-
 #endif /* _COPPERPLATE_CLOCKOBJ_H */
diff --git a/lib/copperplate/clockobj.c b/lib/copperplate/clockobj.c
index f7410b5..fe447f8 100644
--- a/lib/copperplate/clockobj.c
+++ b/lib/copperplate/clockobj.c
@@ -80,25 +80,24 @@ void timespec_adds(struct timespec *r,
        }
 }
 
-static void ticks_to_timespec(struct clockobj *clkobj,
-                             ticks_t ticks,
-                             struct timespec *ts)
+#ifndef CONFIG_XENO_LORES_CLOCK_DISABLED
+
+void __clockobj_ticks_to_timespec(struct clockobj *clkobj,
+                                 ticks_t ticks,
+                                 struct timespec *ts)
 {
-       unsigned int freq = clockobj_get_frequency(clkobj);
+       unsigned int freq;
 
-       ts->tv_sec = ticks / freq;
-       ts->tv_nsec = ticks - (ts->tv_sec * freq);
-       if (clockobj_get_resolution(clkobj) > 1)
+       if (clockobj_get_resolution(clkobj) > 1) {
+               freq = clockobj_get_frequency(clkobj);
+               ts->tv_sec = ticks / freq;
+               ts->tv_nsec = ticks - (ts->tv_sec * freq);
                ts->tv_nsec *= clockobj_get_resolution(clkobj);
+       } else
+               clockobj_ns_to_timespec(ticks, ts);
 }
 
-void clockobj_ticks_to_timespec(struct clockobj *clkobj,
-                               ticks_t ticks, struct timespec *ts)
-{
-       read_lock_nocancel(&clkobj->lock);
-       ticks_to_timespec(clkobj, ticks, ts);
-       read_unlock(&clkobj->lock);
-}
+#endif /* !CONFIG_XENO_LORES_CLOCK_DISABLED */
 
 void __clockobj_ticks_to_timeout(struct clockobj *clkobj,
                                 clockid_t clk_id,
@@ -108,7 +107,7 @@ void __clockobj_ticks_to_timeout(struct clockobj *clkobj,
 
        read_lock_nocancel(&clkobj->lock);
        __RT(clock_gettime(clk_id, ts));
-       ticks_to_timespec(clkobj, ticks, &delta);
+       __clockobj_ticks_to_timespec(clkobj, ticks, &delta);
        read_unlock(&clkobj->lock);
        timespec_add(ts, ts, &delta);
 }
@@ -212,7 +211,7 @@ void clockobj_caltime_to_timeout(struct clockobj *clkobj, 
const struct tm *tm,
 
        read_lock_nocancel(&clkobj->lock);
        clockobj_caltime_to_ticks(clkobj, tm, rticks, &ticks);
-       ticks_to_timespec(clkobj, ticks, ts);
+       __clockobj_ticks_to_timespec(clkobj, ticks, ts);
        timespec_sub(ts, ts, &clkobj->offset);
        read_unlock(&clkobj->lock);
 }
@@ -230,7 +229,7 @@ void clockobj_set_date(struct clockobj *clkobj,
        if (resolution_ns)
                __clockobj_set_resolution(clkobj, resolution_ns);
 
-       ticks_to_timespec(clkobj, ticks, &clkobj->epoch);
+       __clockobj_ticks_to_timespec(clkobj, ticks, &clkobj->epoch);
        timespec_sub(&clkobj->offset, &clkobj->epoch, &now);
 
        read_unlock(&clkobj->lock);
@@ -360,7 +359,7 @@ void clockobj_ticks_to_clock(struct clockobj *clkobj,
        read_lock_nocancel(&clkobj->lock);
        __RT(clock_gettime(CLOCK_COPPERPLATE, &now));
        /* Absolute timeout, CLOCK_COPPERPLATE-based. */
-       ticks_to_timespec(clkobj, ticks, &ts);
+       __clockobj_ticks_to_timespec(clkobj, ticks, &ts);
        read_unlock(&clkobj->lock);
        /* Offset from CLOCK_COPPERPLATE epoch. */
        timespec_sub(timeout, &ts, &now);


_______________________________________________
Xenomai-git mailing list
Xenomai-git@gna.org
https://mail.gna.org/listinfo/xenomai-git

Reply via email to