1 // -----------------------------------------------------------------------
2 // Copyright (C) 2006 - 2009 FRESCOR consortium partners:
4 // Universidad de Cantabria, SPAIN
5 // University of York, UK
6 // Scuola Superiore Sant'Anna, ITALY
7 // Kaiserslautern University, GERMANY
8 // Univ. Politécnica 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 for a link to partners' websites
18 // 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 // FSF API web pages: http://marte.unican.es/fsf/docs
32 // http://shark.sssup.it/contrib/first/docs/
34 // This file is part of FOSA (Frsh Operating System Adaption)
36 // FOSA is free software; you can redistribute it and/or modify it
37 // under terms of the GNU General Public License as published by the
38 // Free Software Foundation; either version 2, or (at your option) any
39 // later version. FOSA is distributed in the hope that it will be
40 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
41 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
42 // General Public License for more details. You should have received a
43 // copy of the GNU General Public License along with FOSA; see file
44 // COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
45 // Cambridge, MA 02139, USA.
47 // As a special exception, including FOSA header files in a file,
48 // instantiating FOSA generics or templates, or linking other files
49 // with FOSA objects to produce an executable application, does not
50 // by itself cause the resulting executable application to be covered
51 // by the GNU General Public License. This exception does not
52 // however invalidate any other reasons why the executable file might be
53 // covered by the GNU Public License.
54 // -----------------------------------------------------------------------
56 //==============================================
57 // ******** ****** ******** **********
58 // **///// /** ** **////// /** /**
59 // ** /** ** /** /** /**
60 // ******* /** ** /********* /**********
61 // **//// /** ** ////////** /**//////**
62 // ** /** ** /** /** /**
63 // ** /** ** ******** /** /**
64 // // /******/ //////// // //
66 // FOSA(Frescor Operating System Adaptation layer)
67 //================================================
68 #ifndef FOSA_TIME_NUMERIC_H_
69 #define FOSA_TIME_NUMERIC_H_
71 #include "fosa_types.h"
73 /***************************************/
74 /* C O N V E R S I O N M A C R O S */
75 /***************************************/
77 #if FOSA_TIME_BASE == 9 /* nanoseconds */
79 #define fosa_time_to_msec(time) (time) / 1000000
80 #define msec_to_fosa_time(msec) (msec) * 1000000
81 #define fosa_time_to_usec(time) (time) / 1000
82 #define usec_to_fosa_time(usec) (usec) * 1000
83 #define fosa_time_to_nsec(time) (time)
84 #define nsec_to_fosa_time(nsec) (nsec)
87 #define timespec_to_fosa_time(time_type, tspec) ((time_type) (tspec).tv_sec) * 1000000000 + (time_type) (tspec).tv_nsec
89 #define fosa_time_to_timespec(tspec, time) \
91 (tspec).tv_sec = (time) / 1000000000; \
92 (tspec).tv_nsec = (time) % 1000000000; \
95 #define fosa_time_to_double(time) (double) (time) * 1e-9;
96 #define fosa_double_to_time(time_type, time) (time_type) (time * 1e9)
100 #if FOSA_TIME_BASE == 6 /* microseconds */
102 #define fosa_time_to_msec(time) (time) / 1000
103 #define msec_to_fosa_time(msec) (msec) * 1000
104 #define fosa_time_to_usec(time) (time)
105 #define usec_to_fosa_time(usec) (usec)
106 #define fosa_time_to_nsec(time) (time) * 1000
107 #define nsec_to_fosa_time(nsec) (nsec) / 1000
109 #define timespec_to_fosa_time(time_type, tspec) ( (time_type) (tspec).tv_sec) * 1000000 + (time_type) (tspec).tv_nsec/1000
111 #define fosa_time_to_timespec(tspec,time) \
113 (tspec).tv_sec = (time) / 1000000; \
114 (tspec).tv_nsec = ( (time) % 1000000) * 1000; \
117 #define fosa_time_to_double(time) (double) (time) * 1e-6;
118 #define fosa_double_to_time(time_type, time) (time_type) (time * 1e6)
122 #if FOSA_TIME_BASE == 3 /* miliseconds */
124 #define fosa_time_to_msec(time) (time)
125 #define msec_to_fosa_time(msec) (msec)
126 #define fosa_time_to_usec(time) (time) * 1000
127 #define usec_to_fosa_time(usec) (usec) / 1000
128 #define fosa_time_to_nsec(time) (time) * 1000000
129 #define nsec_to_fosa_time(nsec) (nsec) / 1000000
131 #define timespec_to_fosa_time(time_type, tspec) ( (time_type) (tspec).tv_sec) * 1000 + (time_type) (tspec).tv_nsec/1000000
133 #define fosa_time_to_timespec(tspec, time) \
135 (tspec).tv_sec = (time) / 1000; \
136 (tspec).tv_nsec = ( (time) % 1000) * 1000000; \
139 #define fosa_time_to_double(time) (double) (time) * 1e-3;
140 #define fosa_double_to_time(time_type, time) (time_type) (time) * 1e3
147 /* Arithmetic and comparison */
148 /*****************************/
149 static inline fosa_abs_time_t fosa_abs_time_incr(fosa_abs_time_t base, fosa_rel_time_t interval)
151 fosa_abs_time_t result;
153 result = base + interval;
159 // ---------------------------------------------------------
161 static inline fosa_abs_time_t fosa_abs_time_decr(fosa_abs_time_t base, fosa_rel_time_t interval)
163 fosa_abs_time_t result;
165 result = base - interval;
172 // ---------------------------------------------------------
174 static inline fosa_rel_time_t fosa_abs_time_extract_interval(fosa_abs_time_t past, fosa_abs_time_t future)
176 fosa_rel_time_t result;
178 result = future - past;
184 // ---------------------------------------------------------
186 static inline fosa_rel_time_t fosa_rel_time_add(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
188 fosa_rel_time_t result;
190 result = relt1 + relt2;
197 // ---------------------------------------------------------
199 static inline fosa_rel_time_t fosa_rel_time_decr(fosa_rel_time_t total, fosa_rel_time_t part)
201 fosa_rel_time_t result;
203 result = total - part;
208 // ---------------------------------------------------------
211 static inline fosa_rel_time_t fosa_rel_time_times_integer(fosa_rel_time_t time, long multiplier)
213 fosa_rel_time_t result;
215 result = time * multiplier;
221 // ---------------------------------------------------------
223 static inline fosa_rel_time_t fosa_rel_time_divided_by_integer(fosa_rel_time_t time, long divider)
225 return time / divider;
234 static inline bool fosa_abs_time_smaller(fosa_abs_time_t abst1, fosa_abs_time_t abst2)
238 result = abst1 < abst2;
243 // -----------------------------------------------------------
245 static inline bool fosa_rel_time_smaller(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
249 result = relt1 < relt2;
255 // -----------------------------------------------------------
257 static inline bool fosa_abs_time_smaller_or_equal(fosa_abs_time_t abst1, fosa_abs_time_t abst2)
261 result = abst1 <= abst2;
267 // -----------------------------------------------------------
269 static inline bool fosa_rel_time_smaller_or_equal(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
273 result = relt1 <= relt2;
279 // -----------------------------------------------------------
281 static inline bool fosa_rel_time_equal(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
285 result = relt1 == relt2;
291 // -----------------------------------------------------------
293 static inline bool fosa_abs_time_equal(fosa_abs_time_t abst1, fosa_abs_time_t abst2)
297 result = abst1 == abst2;
302 // -----------------------------------------------------------
304 static inline bool fosa_rel_time_is_null(fosa_rel_time_t relt)
314 // -----------------------------------------------------------
316 static inline bool fosa_abs_time_is_null(fosa_abs_time_t abst)
329 static inline fosa_rel_time_t fosa_msec_to_rel_time(long msec)
331 fosa_rel_time_t result;
333 result = msec_to_fosa_time( (fosa_rel_time_t) msec );
338 // --------------------------------------------------
340 static inline long fosa_rel_time_to_msec(fosa_rel_time_t relt)
344 result = fosa_time_to_msec(relt);
350 // --------------------------------------------------
353 static inline fosa_rel_time_t fosa_msec_to_abs_time(long msec)
355 fosa_abs_time_t result;
357 result = msec_to_fosa_time( (fosa_abs_time_t) msec);
362 // --------------------------------------------------
364 static inline long fosa_abs_time_to_msec(fosa_abs_time_t abst)
368 result = fosa_time_to_msec(abst);
374 // --------------------------------------------------
376 static inline fosa_rel_time_t fosa_usec_to_rel_time(long usec)
378 fosa_rel_time_t result;
380 result = usec_to_fosa_time((fosa_rel_time_t ) usec);
385 // --------------------------------------------------
387 static inline long fosa_rel_time_to_usec(fosa_rel_time_t relt)
391 result = fosa_time_to_usec(relt);
397 // --------------------------------------------------
399 static inline fosa_abs_time_t fosa_usec_to_abs_time(long usec)
401 fosa_abs_time_t result;
403 result = usec_to_fosa_time( (fosa_abs_time_t) usec);
408 // --------------------------------------------------
410 static inline long fosa_abs_time_to_usec(fosa_abs_time_t abst)
414 result = fosa_time_to_usec(abst);
421 // --------------------------------------------------
423 static inline fosa_rel_time_t fosa_nsec_to_rel_time(long nsec)
425 fosa_rel_time_t result;
427 result = nsec_to_fosa_time((fosa_rel_time_t ) nsec);
432 // --------------------------------------------------
434 static inline long fosa_rel_time_to_nsec(fosa_rel_time_t relt)
438 result = fosa_time_to_nsec(relt);
444 // --------------------------------------------------
446 static inline fosa_abs_time_t fosa_nsec_to_abs_time(long nsec)
448 fosa_abs_time_t result;
450 result = nsec_to_fosa_time( (fosa_abs_time_t) nsec);
455 // --------------------------------------------------
457 static inline long fosa_abs_time_to_nsec(fosa_abs_time_t abst)
461 result = fosa_time_to_nsec(abst);
466 // --------------------------------------------------
468 static inline fosa_rel_time_t fosa_timespec_to_rel_time(struct timespec time_tspec)
470 fosa_rel_time_t result;
472 result = timespec_to_fosa_time(fosa_rel_time_t, time_tspec);
477 // --------------------------------------------------
479 static inline struct timespec fosa_rel_time_to_timespec(fosa_rel_time_t relt)
481 struct timespec result;
483 fosa_time_to_timespec(result, relt);
488 // --------------------------------------------------
490 static inline fosa_abs_time_t fosa_timespec_to_abs_time(struct timespec time_tspec)
492 fosa_abs_time_t result;
494 result = timespec_to_fosa_time(fosa_abs_time_t, time_tspec);
499 // --------------------------------------------------
501 static inline struct timespec fosa_abs_time_to_timespec(fosa_abs_time_t abst)
503 struct timespec result;
505 fosa_time_to_timespec(result, abst);
512 // --------------------------------------------------
514 static inline double fosa_rel_time_to_double(fosa_rel_time_t relt)
518 result = fosa_time_to_double(relt);
523 // --------------------------------------------------
525 static inline double fosa_double_to_rel_time(double time)
527 fosa_rel_time_t result;
529 result = fosa_double_to_time(fosa_rel_time_t, time);
536 #endif /* !FOSA_TIME_H_ */