]> rtime.felk.cvut.cz Git - frescor/frsh-include.git/blob - timespec_operations.h
Moving remaining "private" .h files stdbool.h timespec_opeartions.h back to
[frescor/frsh-include.git] / timespec_operations.h
1 /*----------------------------------------------------------------------------
2  *-------------------------      M a R T E   O S      ------------------------
3  *----------------------------------------------------------------------------
4  *                                                           V1.0  Dec 2001
5  *
6  *                    't i m e s p e c _ o p e r a t i o n s'
7  *
8  *                                      H
9  *
10  * File 'timespec_operations.h'                                        by MAR.
11  *                                (july 2002)  transformed into macros by Jul.
12  * Some basic operations with the type 'timespec'.
13  *
14  * ----------------------------------------------------------------------
15  *  Copyright (C) 2001   Universidad de Cantabria, SPAIN
16  *
17  *  Authors: Mario Aldea Rivas          aldeam@ctr.unican.es
18  *           Michael Gonzalez Harbour      mgh@ctr.unican.es
19  *
20  * MaRTE OS  is free software; you can  redistribute it and/or  modify it
21  * under the terms of the GNU General Public License  as published by the
22  * Free Software Foundation;  either  version 2, or (at  your option) any
23  * later version.
24  *
25  * MaRTE OS  is distributed  in the  hope  that  it will be   useful, but
26  * WITHOUT  ANY  WARRANTY;     without  even the   implied   warranty  of
27  * MERCHANTABILITY  or  FITNESS FOR A  PARTICULAR PURPOSE.    See the GNU
28  * General Public License for more details.
29  *
30  * You should have received  a  copy of  the  GNU General Public  License
31  * distributed with MaRTE  OS;  see file COPYING.   If not,  write to the
32  * Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
33  * 02111-1307, USA.
34  *
35  * As a  special exception, if you  link this  unit  with other  files to
36  * produce an   executable,   this unit  does  not  by  itself cause  the
37  * resulting executable to be covered by the  GNU General Public License.
38  * This exception does  not however invalidate  any other reasons why the
39  * executable file might be covered by the GNU Public License.
40  *
41  *---------------------------------------------------------------------------*/
42
43 #ifndef _MARTE_MISC_TIMESPEC_OPERATIONS_H_
44 #define _MARTE_MISC_TIMESPEC_OPERATIONS_H_
45
46 #include <time.h>
47
48 #define smaller_timespec(t1, t2) \
49  ( \
50   (t1)->tv_sec < (t2)->tv_sec || ((t1)->tv_sec == (t2)->tv_sec &&   \
51                                      (t1)->tv_nsec < (t2)->tv_nsec) \
52  )
53
54 #define smaller_or_equal_timespec(t1, t2) \
55  ( \
56   (t1)->tv_sec < (t2)->tv_sec || ((t1)->tv_sec == (t2)->tv_sec &&    \
57                                      (t1)->tv_nsec <= (t2)->tv_nsec) \
58  )
59
60 #define incr_timespec(t1, t2) \
61 { \
62   (t1)->tv_sec += (t2)->tv_sec; \
63   (t1)->tv_nsec += (t2)->tv_nsec; \
64   if ((t1)->tv_nsec >= 1000000000) { \
65     (t1)->tv_sec ++; \
66     (t1)->tv_nsec -= 1000000000; \
67   } \
68 }
69
70 #define decr_timespec(t1, t2) \
71 { \
72   if ((t1)->tv_nsec < (t2)->tv_nsec) { \
73     (t1)->tv_sec -= (t2)->tv_sec + 1; \
74     (t1)->tv_nsec = (t1)->tv_nsec + 1000000000 - (t2)->tv_nsec; \
75   } else { \
76     (t1)->tv_sec -= (t2)->tv_sec; \
77     (t1)->tv_nsec -= (t2)->tv_nsec; \
78   } \
79 }
80
81
82 #define  add_timespec( sum , t1 , t2 ) \
83 { \
84   (sum)->tv_sec  = (t1)->tv_sec  + (t2)->tv_sec; \
85   (sum)->tv_nsec = (t1)->tv_nsec + (t2)->tv_nsec; \
86   if ((sum)->tv_nsec >= 1000000000) { \
87     (sum)->tv_sec ++; \
88     (sum)->tv_nsec -= 1000000000; \
89   } \
90 }
91
92 #define float_to_timespec( f1 , t1 ) \
93 ( \
94   (t1)->tv_sec = (int)(f1), \
95   (t1)->tv_nsec = (int)(((f1)-(float)((t1)->tv_sec))*1000000000.0), \
96   (t1) \
97 )
98
99 #define float_to_timespec_value(f1, t1) \
100 ( \
101   (t1).tv_sec = (int)(f1), \
102   (t1).tv_nsec = (int)(((f1)-(float)((t1).tv_sec))*1000000000.0), \
103   (t1) \
104 )
105
106 #endif /* _MARTE_MISC_TIMESPEC_OPERATIONS_H_ */