1 //----------------------------------------------------------------------
2 // Copyright (C) 2006 - 2007 by the FRESCOR consortium:
4 // Universidad de Cantabria, SPAIN
5 // University of York, UK
6 // Scuola Superiore Sant'Anna, ITALY
7 // Kaiserslautern University, GERMANY
8 // Univ. Politecnica Valencia, SPAIN
9 // Czech Technical University in Prague, CZECH REPUBLIC
11 // Thales Communication S.A. FRANCE
12 // Visual Tools S.A. SPAIN
13 // Rapita Systems Ltd UK
16 // See http://www.frescor.org
18 // The FRESCOR project (FP6/2005/IST/5-034026) is funded
19 // in part by the European Union Sixth Framework Programme
20 // The European Union is not liable of any use that may be
24 // based on previous work (FSF) done in the FIRST project
26 // Copyright (C) 2005 Mälardalen University, SWEDEN
27 // Scuola Superiore S.Anna, ITALY
28 // Universidad de Cantabria, SPAIN
29 // University of York, UK
31 // This file is part of FOSA (Frsh Operating System Abstraction)
33 // FOSA is free software; you can redistribute it and/or modify it
34 // under terms of the GNU General Public License as published by the
35 // Free Software Foundation; either version 2, or (at your option) any
36 // later version. FOSA is distributed in the hope that it will be
37 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
38 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 // General Public License for more details. You should have received a
40 // copy of the GNU General Public License along with FOSA; see file
41 // COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
42 // Cambridge, MA 02139, USA.
44 // As a special exception, including FOSA header files in a file,
45 // instantiating FOSA generics or templates, or linking other files
46 // with FOSA objects to produce an executable application, does not
47 // by itself cause the resulting executable application to be covered
48 // by the GNU General Public License. This exception does not
49 // however invalidate any other reasons why the executable file might be
50 // covered by the GNU Public License.
51 // -----------------------------------------------------------------------
53 //==============================================
54 // ******** ****** ******** **********
55 // **///// /** ** **////// /** /**
56 // ** /** ** /** /** /**
57 // ******* /** ** /********* /**********
58 // **//// /** ** ////////** /**//////**
59 // ** /** ** /** /** /**
60 // ** /** ** ******** /** /**
61 // // /******/ //////// // //
63 // FOSA(Frescor Operating System Adaptation layer)
64 //================================================
65 #ifndef FOSA_TIME_NUMERIC_H_
66 #define FOSA_TIME_NUMERIC_H_
68 #include "fosa_types.h"
70 /***************************************/
71 /* C O N V E R S I O N M A C R O S */
72 /***************************************/
74 #if FOSA_TIME_BASE == 9 /* nanoseconds */
76 #define fosa_time_to_msec(time) (time) / 1000000
77 #define msec_to_fosa_time(msec) (msec) * 1000000
78 #define fosa_time_to_usec(time) (time) / 1000
79 #define usec_to_fosa_time(usec) (usec) * 1000
80 #define fosa_time_to_nsec(time) (time)
81 #define nsec_to_fosa_time(nsec) (nsec)
84 #define timespec_to_fosa_time(time_type, tspec) ((time_type) (tspec).tv_sec) * 1000000000 + (time_type) (tspec).tv_nsec
86 #define fosa_time_to_timespec(tspec, time) \
88 (tspec).tv_sec = (time) / 1000000000; \
89 (tspec).tv_nsec = (time) % 1000000000; \
92 #define fosa_time_to_double(time) (double) (time) * 1e-9;
93 #define fosa_double_to_time(time_type, time) (time_type) (time * 1e9)
97 #if FOSA_TIME_BASE == 6 /* microseconds */
99 #define fosa_time_to_msec(time) (time) / 1000
100 #define msec_to_fosa_time(msec) (msec) * 1000
101 #define fosa_time_to_usec(time) (time)
102 #define usec_to_fosa_time(usec) (usec)
103 #define fosa_time_to_nsec(time) (time) * 1000
104 #define nsec_to_fosa_time(nsec) (nsec) / 1000
106 #define timespec_to_fosa_time(time_type, tspec) ( (time_type) (tspec).tv_sec) * 1000000 + (time_type) (tspec).tv_nsec/1000
108 #define fosa_time_to_timespec(tspec,time) \
110 (tspec).tv_sec = (time) / 1000000; \
111 (tspec).tv_nsec = ( (time) % 1000000) * 1000; \
114 #define fosa_time_to_double(time) (double) (time) * 1e-6;
115 #define fosa_double_to_time(time_type, time) (time_type) (time * 1e6)
119 #if FOSA_TIME_BASE == 3 /* miliseconds */
121 #define fosa_time_to_msec(time) (time)
122 #define msec_to_fosa_time(msec) (msec)
123 #define fosa_time_to_usec(time) (time) * 1000
124 #define usec_to_fosa_time(usec) (usec) / 1000
125 #define fosa_time_to_nsec(time) (time) * 1000000
126 #define nsec_to_fosa_time(nsec) (nsec) / 1000000
128 #define timespec_to_fosa_time(time_type, tspec) ( (time_type) (tspec).tv_sec) * 1000 + (time_type) (tspec).tv_nsec/1000000
130 #define fosa_time_to_timespec(tspec, time) \
132 (tspec).tv_sec = (time) / 1000; \
133 (tspec).tv_nsec = ( (time) % 1000) * 1000000; \
136 #define fosa_time_to_double(time) (double) (time) * 1e-3;
137 #define fosa_double_to_time(time_type, time) (time_type) (time) * 1e3
144 /* Arithmetic and comparison */
145 /*****************************/
146 static inline fosa_abs_time_t fosa_abs_time_incr(fosa_abs_time_t base, fosa_rel_time_t interval)
148 fosa_abs_time_t result;
150 result = base + interval;
156 // ---------------------------------------------------------
158 static inline fosa_abs_time_t fosa_abs_time_decr(fosa_abs_time_t base, fosa_rel_time_t interval)
160 fosa_abs_time_t result;
162 result = base - interval;
169 // ---------------------------------------------------------
171 static inline fosa_rel_time_t fosa_abs_time_extract_interval(fosa_abs_time_t past, fosa_abs_time_t future)
173 fosa_rel_time_t result;
175 result = future - past;
181 // ---------------------------------------------------------
183 static inline fosa_rel_time_t fosa_rel_time_add(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
185 fosa_rel_time_t result;
187 result = relt1 + relt2;
194 // ---------------------------------------------------------
196 static inline fosa_rel_time_t fosa_rel_time_decr(fosa_rel_time_t total, fosa_rel_time_t part)
198 fosa_rel_time_t result;
200 result = total - part;
205 // ---------------------------------------------------------
208 static inline fosa_rel_time_t fosa_rel_time_times_integer(fosa_rel_time_t time, long multiplier)
210 fosa_rel_time_t result;
212 result = time * multiplier;
218 // ---------------------------------------------------------
220 static inline fosa_rel_time_t fosa_rel_time_divided_by_integer(fosa_rel_time_t time, long divider)
222 return time / divider;
231 static inline bool fosa_abs_time_smaller(fosa_abs_time_t abst1, fosa_abs_time_t abst2)
235 result = abst1 < abst2;
240 // -----------------------------------------------------------
242 static inline bool fosa_rel_time_smaller(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
246 result = relt1 < relt2;
252 // -----------------------------------------------------------
254 static inline bool fosa_abs_time_smaller_or_equal(fosa_abs_time_t abst1, fosa_abs_time_t abst2)
258 result = abst1 <= abst2;
264 // -----------------------------------------------------------
266 static inline bool fosa_rel_time_smaller_or_equal(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
270 result = relt1 <= relt2;
276 // -----------------------------------------------------------
278 static inline bool fosa_rel_time_equal(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
282 result = relt1 == relt2;
288 // -----------------------------------------------------------
290 static inline bool fosa_abs_time_equal(fosa_abs_time_t abst1, fosa_abs_time_t abst2)
294 result = abst1 == abst2;
299 // -----------------------------------------------------------
301 static inline bool fosa_rel_time_is_null(fosa_rel_time_t relt)
311 // -----------------------------------------------------------
313 static inline bool fosa_abs_time_is_null(fosa_abs_time_t abst)
326 static inline fosa_rel_time_t fosa_msec_to_rel_time(long msec)
328 fosa_rel_time_t result;
330 result = msec_to_fosa_time( (fosa_rel_time_t) msec );
335 // --------------------------------------------------
337 static inline long fosa_rel_time_to_msec(fosa_rel_time_t relt)
341 result = fosa_time_to_msec(relt);
347 // --------------------------------------------------
350 static inline fosa_rel_time_t fosa_msec_to_abs_time(long msec)
352 fosa_abs_time_t result;
354 result = msec_to_fosa_time( (fosa_abs_time_t) msec);
359 // --------------------------------------------------
361 static inline long fosa_abs_time_to_msec(fosa_abs_time_t abst)
365 result = fosa_time_to_msec(abst);
371 // --------------------------------------------------
373 static inline fosa_rel_time_t fosa_usec_to_rel_time(long usec)
375 fosa_rel_time_t result;
377 result = usec_to_fosa_time((fosa_rel_time_t ) usec);
382 // --------------------------------------------------
384 static inline long fosa_rel_time_to_usec(fosa_rel_time_t relt)
388 result = fosa_time_to_usec(relt);
394 // --------------------------------------------------
396 static inline fosa_abs_time_t fosa_usec_to_abs_time(long usec)
398 fosa_abs_time_t result;
400 result = usec_to_fosa_time( (fosa_abs_time_t) usec);
405 // --------------------------------------------------
407 static inline long fosa_abs_time_to_usec(fosa_abs_time_t abst)
411 result = fosa_time_to_usec(abst);
418 // --------------------------------------------------
420 static inline fosa_rel_time_t fosa_nsec_to_rel_time(long nsec)
422 fosa_rel_time_t result;
424 result = nsec_to_fosa_time((fosa_rel_time_t ) nsec);
429 // --------------------------------------------------
431 static inline long fosa_rel_time_to_nsec(fosa_rel_time_t relt)
435 result = fosa_time_to_nsec(relt);
441 // --------------------------------------------------
443 static inline fosa_abs_time_t fosa_nsec_to_abs_time(long nsec)
445 fosa_abs_time_t result;
447 result = nsec_to_fosa_time( (fosa_abs_time_t) nsec);
452 // --------------------------------------------------
454 static inline long fosa_abs_time_to_nsec(fosa_abs_time_t abst)
458 result = fosa_time_to_nsec(abst);
463 // --------------------------------------------------
465 static inline fosa_rel_time_t fosa_timespec_to_rel_time(struct timespec time_tspec)
467 fosa_rel_time_t result;
469 result = timespec_to_fosa_time(fosa_rel_time_t, time_tspec);
474 // --------------------------------------------------
476 static inline struct timespec fosa_rel_time_to_timespec(fosa_rel_time_t relt)
478 struct timespec result;
480 fosa_time_to_timespec(result, relt);
485 // --------------------------------------------------
487 static inline fosa_abs_time_t fosa_timespec_to_abs_time(struct timespec time_tspec)
489 fosa_abs_time_t result;
491 result = timespec_to_fosa_time(fosa_abs_time_t, time_tspec);
496 // --------------------------------------------------
498 static inline struct timespec fosa_abs_time_to_timespec(fosa_abs_time_t abst)
500 struct timespec result;
502 fosa_time_to_timespec(result, abst);
509 // --------------------------------------------------
511 static inline double fosa_rel_time_to_double(fosa_rel_time_t relt)
515 result = fosa_time_to_double(relt);
520 // --------------------------------------------------
522 static inline double fosa_double_to_rel_time(double time)
524 fosa_rel_time_t result;
526 result = fosa_double_to_time(fosa_rel_time_t, time);
533 #endif /* !FOSA_TIME_H_ */