l4-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] time functions to libl4


From: Johan Rydberg
Subject: [PATCH] time functions to libl4
Date: Mon, 07 Feb 2005 20:38:17 +0100
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

Hi,

This little patch implements time related functions in schedule.h.
Comments are welcome.

2005-02-07  Johan Rydberg  <address@hidden>

        * l4/schedule.h (_L4_time_add): Implement.
        (_L4_time_sub): Likewise.
        (_L4_time_add_usec): Likewise.
        (_L4_time_sub_usec): Likewise.
        (_L4_time_make): New function.
        (_L4_time_alter_exps): Likewise.
        (_L4_TIME_PERIOD_M_MAX): Define.

diff -rN -u hurd-l4-old/hurd-l4/libl4/l4/schedule.h 
hurd-l4-new/hurd-l4/libl4/l4/schedule.h
--- hurd-l4-old/hurd-l4/libl4/l4/schedule.h     2005-02-07 18:43:54.000000000 
+0100
+++ hurd-l4-new/hurd-l4/libl4/l4/schedule.h     2005-02-07 20:05:43.000000000 
+0100
@@ -51,6 +51,7 @@
 #define _L4_TIME_M_BITS                (10)
 #define _L4_TIME_PERIOD_E_BITS (5)
 #define _L4_TIME_PERIOD_E_MAX  ((1 << _L4_TIME_PERIOD_E_BITS) - 1)
+#define _L4_TIME_PERIOD_M_MAX  ((1 << _L4_TIME_M_BITS) - 1)
 
 /* The maximum time period that can be specified has all mantisse and
    all exponent bits set.  */
@@ -61,21 +62,47 @@
 #define _L4_zero_time  ((_L4_time_t) (1 << _L4_TIME_M_BITS))
 
 
-static inline _L4_time_t
+/* Alter A and B so that they both have the same exponent.  */
+static inline void
 _L4_attribute_always_inline
-_L4_time_add_usec (_L4_time_t time, _L4_word_t usec)
+_L4_time_alter_exps (__L4_time_t *a, __L4_time_t *b)
 {
-  /* FIXME: Implement me.  */
-  return 0;
+  if (a->period.e < b->period.e)
+    {
+      __L4_time_t *c = a;
+      a = b, b = c;
+    }
+
+  while (a->period.e != b->period.e)
+    {
+      a->period.m *= 2;
+      a->period.e --;
+    }
 }
 
 
 static inline _L4_time_t
 _L4_attribute_always_inline
-_L4_time_sub_usec (_L4_time_t time, _L4_word_t usec)
+_L4_time_make (_L4_word_t val)
 {
-  /* FIXME: Implement me.  */
-  return 0;
+  __L4_time_t t = { 0, };
+
+  if (val != 0)
+    {
+      while (!(val & 1))
+       {
+         val >>= 1;
+         t.period.e ++;
+       }
+      t.period.m = val;
+    }
+  else
+    {
+      /* Zero time is specified as mantisse 0 and exponent 1.  */
+      t.period.e = 1;
+    }
+
+  return t.raw;
 }
 
 
@@ -83,8 +110,34 @@
 _L4_attribute_always_inline
 _L4_time_add (_L4_time_t time1, _L4_time_t time2)
 {
-  /* FIXME: Implement me.  */
-  return 0;
+  __L4_time_t a = { .raw = time1 };
+  __L4_time_t b = { .raw = time2 };
+  int res;
+
+  _L4_time_alter_exps (&a, &b);
+  res = a.period.m + b.period.m;
+
+  if (res > _L4_TIME_PERIOD_M_MAX)
+    /* Overflow in the mantisse, try to see if it is possible to shift
+       down the result, and increase the exponent.  */
+    {
+      while (!(res & 1))
+       {
+         res >>= 1;
+         a.period.e ++;
+       }
+    }
+
+  a.period.m = res;
+  return a.raw;
+}
+
+
+static inline _L4_time_t
+_L4_attribute_always_inline
+_L4_time_add_usec (_L4_time_t time, _L4_word_t usec)
+{
+  return _L4_time_add (time, _L4_time_make (usec));
 }
 
 
@@ -92,8 +145,35 @@
 _L4_attribute_always_inline
 _L4_time_sub (_L4_time_t time1, _L4_time_t time2)
 {
-  /* FIXME: Implement me.  */
-  return 0;
+  __L4_time_t a = { .raw = time1 };
+  __L4_time_t b = { .raw = time2 };
+
+  _L4_time_alter_exps (&a, &b);
+
+  /* If this underflows (b's mantisse is greater than a's) there is
+     really nothing to do, since negative time is not supported.  */
+  a.period.m = a.period.m - b.period.m;
+  return a.raw;
+}
+
+
+static inline _L4_time_t
+_L4_attribute_always_inline
+_L4_time_sub_usec (_L4_time_t time, _L4_word_t usec)
+{
+  return _L4_time_sub (time, _L4_time_make (usec));
+}
+
+
+static inline int
+_L4_attribute_always_inline
+_L4_time_cmp (_L4_time_t time1, _L4_time_t time2)
+{
+  __L4_time_t a = { .raw = time1 };
+  __L4_time_t b = { .raw = time2 };
+  
+  _L4_time_alter_exps (&a, &b);
+  return (a.period.m - b.period.m);
 }
 
 
@@ -101,8 +181,7 @@
 _L4_attribute_always_inline
 _L4_is_time_longer (_L4_time_t time1, _L4_time_t time2)
 {
-  /* FIXME: Implement me.  */
-  return 0;
+  return (_L4_time_cmp (time1, time2) > 0 ? 1 : 0);
 }
 
 
@@ -110,8 +189,7 @@
 _L4_attribute_always_inline
 _L4_is_time_shorter (_L4_time_t time1, _L4_time_t time2)
 {
-  /* FIXME: Implement me.  */
-  return 0;
+  return (_L4_time_cmp (time1, time2) < 0 ? 1 : 0);
 }
 
 
@@ -119,8 +197,7 @@
 _L4_attribute_always_inline
 _L4_is_time_equal (_L4_time_t time1, _L4_time_t time2)
 {
-  /* FIXME: Implement me.  */
-  return 0;
+  return (_L4_time_cmp (time1, time2) == 0);
 }
 
 
@@ -128,8 +205,7 @@
 _L4_attribute_always_inline
 _L4_is_time_not_equal (_L4_time_t time1, _L4_time_t time2)
 {
-  /* FIXME: Implement me.  */
-  return 0;
+  return (_L4_time_cmp (time1, time2) != 0);
 }
 
 






reply via email to

[Prev in Thread] Current Thread [Next in Thread]