Blame view

RIOT/sys/timex/timex.c 2.36 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  /**
   * Timex implementation
   *
   * Copyright (C) 2009, 2010, 2013, 2014 Freie Universitaet Berlin (FUB).
   * Copyright (C) 2013, 2014 INRIA.
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @file
   * @author Kaspar Schleiser <kaspar@schleiser.de>
   * @author Oliver Hahm <oliver.hahm@inria.fr>
   * @author Christian Mehlis <mehlis@inf.fu-berlin.de>
   * @author Daniel Jentsch <d.jentsch@fu-berlin.de>
   *
   */
  
  #include <stdio.h>
  #include <inttypes.h>
  
  #define ENABLE_DEBUG (0)
  #include "debug.h"
  
  #include "timex.h"
  
  timex_t timex_add(const timex_t a, const timex_t b)
  {
  #if ENABLE_DEBUG
      if (!timex_isnormalized(&a) || !timex_isnormalized(&b)) {
          puts("timex_add on denormalized value");
      }
  #endif
  
      timex_t result;
      result.seconds = a.seconds + b.seconds;
      result.microseconds = a.microseconds + b.microseconds;
  
      if (result.microseconds > US_PER_SEC) {
          result.microseconds -= US_PER_SEC;
          result.seconds++;
      }
  
      return result;
  }
  
  timex_t timex_set(uint32_t seconds, uint32_t microseconds)
  {
      timex_t result;
      result.seconds = seconds;
      result.microseconds = microseconds;
  
  #if ENABLE_DEBUG
      if (!timex_isnormalized(&result)) {
          puts("timex_set on denormalized value");
      }
  #endif
  
      return result;
  }
  
  timex_t timex_sub(const timex_t a, const timex_t b)
  {
  #if ENABLE_DEBUG
      if (!timex_isnormalized(&a) || !timex_isnormalized(&b)) {
          puts("timex_sub on denormalized value");
      }
  #endif
  
      timex_t result;
  
      if (a.microseconds >= b.microseconds) {
          result.seconds = a.seconds - b.seconds;
          result.microseconds = a.microseconds - b.microseconds;
      }
      else {
          result.seconds = a.seconds - b.seconds - 1;
          result.microseconds = a.microseconds + US_PER_SEC - b.microseconds;
      }
  
      return result;
  }
  
  int timex_cmp(const timex_t a, const timex_t b)
  {
  #if ENABLE_DEBUG
      if (!timex_isnormalized(&a) || !timex_isnormalized(&b)) {
          puts("timex_cmp on denormalized value");
      }
  #endif
  
      if (a.seconds < b.seconds) {
          return -1;
      }
  
      if (a.seconds == b.seconds) {
          if (a.microseconds < b.microseconds) {
              return -1;
          }
  
          if (a.microseconds == b.microseconds) {
              return 0;
          }
      }
  
      return 1;
  }