]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - include/fosa_time_numeric.h
360f5d4527d2ee9d9fd011f9222efeef7d7e22ed
[frescor/fosa.git] / include / fosa_time_numeric.h
1 //----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2007 by the FRESCOR consortium:
3 //
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
10 //    ENEA                                   SWEDEN
11 //    Thales Communication S.A.              FRANCE
12 //    Visual Tools S.A.                      SPAIN
13 //    Rapita Systems Ltd                     UK
14 //    Evidence                               ITALY
15 //
16 //    See http://www.frescor.org
17 //
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
21 //        made of this code.
22 //
23 //
24 //  based on previous work (FSF) done in the FIRST project
25 //
26 //   Copyright (C) 2005  Mälardalen University, SWEDEN
27 //                       Scuola Superiore S.Anna, ITALY
28 //                       Universidad de Cantabria, SPAIN
29 //                       University of York, UK
30 //
31 // This file is part of FOSA (Frsh Operating System Abstraction)
32 //
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.
43 //
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 // -----------------------------------------------------------------------
52 //fosa_time_numeric.h
53 //==============================================
54 //  ********  ******    ********  **********
55 //  **///// /**    **  **//////  /**     /**
56 //  **      /**    ** /**        /**     /**
57 //  ******* /**    ** /********* /**********
58 //  **////  /**    ** ////////** /**//////**
59 //  **      /**    **        /** /**     /**
60 //  **      /**    **  ********  /**     /**
61 //  //       /******/  ////////   //      // 
62 //
63 // FOSA(Frescor Operating System Adaptation layer)
64 //================================================
65 #ifndef         FOSA_TIME_NUMERIC_H_
66 #define         FOSA_TIME_NUMERIC_H_
67
68 #include "fosa_types.h"
69
70 /***************************************/
71 /* C O N V E R S I O N    M A C R O S  */
72 /***************************************/
73
74 #if FOSA_TIME_BASE == 9  /* nanoseconds */
75
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)
82
83
84 #define timespec_to_fosa_time(time_type, tspec)  ((time_type) (tspec).tv_sec) * 1000000000 + (time_type) (tspec).tv_nsec
85
86 #define fosa_time_to_timespec(tspec, time) \
87 do { \
88     (tspec).tv_sec = (time) / 1000000000; \
89     (tspec).tv_nsec = (time) % 1000000000; \
90 } while(0)
91
92 #define fosa_time_to_double(time)  (double) (time) * 1e-9;
93 #define fosa_double_to_time(time_type, time) (time_type) (time * 1e9)
94
95 #endif
96
97 #if FOSA_TIME_BASE == 6  /* microseconds */
98
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
105
106 #define timespec_to_fosa_time(time_type, tspec) ( (time_type) (tspec).tv_sec) * 1000000 + (time_type) (tspec).tv_nsec/1000
107
108 #define fosa_time_to_timespec(tspec,time) \
109 do { \
110     (tspec).tv_sec = (time) / 1000000; \
111     (tspec).tv_nsec = ( (time) % 1000000) * 1000; \
112 } while(0)
113
114 #define fosa_time_to_double(time)  (double) (time) * 1e-6;
115 #define fosa_double_to_time(time_type, time) (time_type) (time * 1e6)
116
117 #endif
118
119 #if FOSA_TIME_BASE == 3  /* miliseconds */
120
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
127
128 #define timespec_to_fosa_time(time_type, tspec) ( (time_type) (tspec).tv_sec) * 1000 + (time_type) (tspec).tv_nsec/1000000
129
130 #define fosa_time_to_timespec(tspec, time) \
131 do { \
132     (tspec).tv_sec = (time) / 1000; \
133     (tspec).tv_nsec = ( (time) % 1000) * 1000000; \
134 } while(0)
135
136 #define fosa_time_to_double(time)  (double) (time) * 1e-3;
137 #define fosa_double_to_time(time_type, time) (time_type) (time) * 1e3
138
139
140 #endif
141
142
143
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)
147 {
148     fosa_abs_time_t result;
149     
150     result = base + interval;
151
152     return result;
153 }
154
155
156 // ---------------------------------------------------------
157
158 static inline fosa_abs_time_t fosa_abs_time_decr(fosa_abs_time_t base, fosa_rel_time_t interval)
159 {
160     fosa_abs_time_t result;
161
162     result =  base - interval;
163
164     return result;
165 }
166
167
168
169 // ---------------------------------------------------------
170
171 static inline fosa_rel_time_t fosa_abs_time_extract_interval(fosa_abs_time_t past, fosa_abs_time_t future)
172 {
173     fosa_rel_time_t result;
174
175     result = future - past;
176
177     return result;
178 }
179
180
181 // ---------------------------------------------------------
182
183 static inline fosa_rel_time_t fosa_rel_time_add(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
184 {
185     fosa_rel_time_t result;
186
187     result = relt1 + relt2;
188
189     return result;
190 }
191     
192
193
194 // ---------------------------------------------------------
195
196 static inline fosa_rel_time_t fosa_rel_time_decr(fosa_rel_time_t total, fosa_rel_time_t part)
197 {
198     fosa_rel_time_t result;
199
200     result = total - part;
201
202     return result;
203 }
204
205 // ---------------------------------------------------------
206
207
208 static inline fosa_rel_time_t fosa_rel_time_times_integer(fosa_rel_time_t time, long multiplier)
209 {
210     fosa_rel_time_t result;
211
212     result = time * multiplier;
213
214     return result;
215 }
216
217
218 // ---------------------------------------------------------
219
220 static inline fosa_rel_time_t fosa_rel_time_divided_by_integer(fosa_rel_time_t time, long divider)
221 {
222     return time / divider;
223 }
224
225
226
227
228
229 /* Comparison */
230 /**************/
231 static inline bool fosa_abs_time_smaller(fosa_abs_time_t abst1, fosa_abs_time_t abst2)
232 {
233     bool result;
234
235     result = abst1 < abst2;
236
237     return result;
238 }
239
240 // -----------------------------------------------------------
241
242 static inline bool fosa_rel_time_smaller(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
243 {
244     bool result;
245
246     result = relt1 < relt2;
247
248     return result;
249 }
250
251
252 // -----------------------------------------------------------
253
254 static inline bool fosa_abs_time_smaller_or_equal(fosa_abs_time_t abst1, fosa_abs_time_t abst2)
255 {
256     bool result;
257
258     result = abst1 <= abst2;
259
260     return result;
261 }
262
263
264 // -----------------------------------------------------------
265
266 static inline bool fosa_rel_time_smaller_or_equal(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
267 {
268     bool result;
269
270     result = relt1 <= relt2;
271
272     return result;
273 }
274
275
276 // -----------------------------------------------------------
277
278 static inline bool fosa_rel_time_equal(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
279 {
280     bool result;
281
282     result = relt1 == relt2;
283
284     return result;
285 }
286
287
288 // -----------------------------------------------------------
289
290 static inline bool fosa_abs_time_equal(fosa_abs_time_t abst1, fosa_abs_time_t abst2)
291 {
292     bool result;
293
294     result = abst1 == abst2;
295
296     return result;
297 }
298
299 // -----------------------------------------------------------
300
301 static inline bool fosa_rel_time_is_null(fosa_rel_time_t relt)
302 {
303     bool result;
304
305     result = relt == 0;
306
307     return result;
308 }
309
310
311 // -----------------------------------------------------------
312
313 static inline bool fosa_abs_time_is_null(fosa_abs_time_t abst)
314 {
315     bool result;
316
317     result = abst == 0;
318
319     return result;
320 }
321
322
323
324 /* Conversion */
325 /**************/
326 static inline fosa_rel_time_t fosa_msec_to_rel_time(long msec)
327 {
328     fosa_rel_time_t result;
329
330     result = msec_to_fosa_time( (fosa_rel_time_t) msec );
331
332     return result;
333 }
334
335 // --------------------------------------------------
336
337 static inline long fosa_rel_time_to_msec(fosa_rel_time_t relt)
338 {
339     long result;
340
341     result = fosa_time_to_msec(relt);
342
343     return result;
344 }
345
346
347 // --------------------------------------------------
348
349
350 static inline fosa_rel_time_t fosa_msec_to_abs_time(long msec)
351 {
352     fosa_abs_time_t result;
353
354     result = msec_to_fosa_time( (fosa_abs_time_t) msec);
355
356     return result;
357 }
358
359 // --------------------------------------------------
360
361 static inline long fosa_abs_time_to_msec(fosa_abs_time_t abst)
362 {
363     long result;
364
365     result = fosa_time_to_msec(abst);
366
367     return result;
368 }
369
370
371 // --------------------------------------------------
372
373 static inline fosa_rel_time_t fosa_usec_to_rel_time(long usec)
374 {
375     fosa_rel_time_t result;
376
377     result = usec_to_fosa_time((fosa_rel_time_t ) usec);
378
379     return result;
380 }
381
382 // --------------------------------------------------
383
384 static inline long fosa_rel_time_to_usec(fosa_rel_time_t relt)
385 {
386     long result;
387
388     result = fosa_time_to_usec(relt);
389
390     return result;
391 }
392
393
394 // --------------------------------------------------
395
396 static inline fosa_abs_time_t fosa_usec_to_abs_time(long usec)
397 {
398     fosa_abs_time_t result;
399
400     result = usec_to_fosa_time( (fosa_abs_time_t) usec);
401
402     return result;
403 }
404
405 // --------------------------------------------------
406
407 static inline long fosa_abs_time_to_usec(fosa_abs_time_t abst)
408 {
409     long result;
410
411     result = fosa_time_to_usec(abst);
412
413     return result;
414 }
415
416
417
418 // --------------------------------------------------
419
420 static inline fosa_rel_time_t fosa_nsec_to_rel_time(long nsec)
421 {
422     fosa_rel_time_t result;
423
424     result = nsec_to_fosa_time((fosa_rel_time_t ) nsec);
425
426     return result;
427 }
428
429 // --------------------------------------------------
430
431 static inline long fosa_rel_time_to_nsec(fosa_rel_time_t relt)
432 {
433     long result;
434
435     result = fosa_time_to_nsec(relt);
436
437     return result;
438 }
439
440
441 // --------------------------------------------------
442
443 static inline fosa_abs_time_t fosa_nsec_to_abs_time(long nsec)
444 {
445     fosa_abs_time_t result;
446
447     result = nsec_to_fosa_time( (fosa_abs_time_t) nsec);
448
449     return result;
450 }
451
452 // --------------------------------------------------
453
454 static inline long fosa_abs_time_to_nsec(fosa_abs_time_t abst)
455 {
456     long result;
457
458     result = fosa_time_to_nsec(abst);
459
460     return result;
461 }
462
463 // --------------------------------------------------
464
465 static inline fosa_rel_time_t fosa_timespec_to_rel_time(struct timespec time_tspec)
466 {
467     fosa_rel_time_t result;
468
469     result = timespec_to_fosa_time(fosa_rel_time_t, time_tspec);
470
471     return result;
472 }
473
474 // --------------------------------------------------
475
476 static inline struct timespec fosa_rel_time_to_timespec(fosa_rel_time_t relt)
477 {
478     struct timespec result;
479
480     fosa_time_to_timespec(result, relt);
481
482     return result;
483 }
484
485 // --------------------------------------------------
486
487 static inline fosa_abs_time_t fosa_timespec_to_abs_time(struct timespec time_tspec)
488 {
489     fosa_abs_time_t result;
490
491     result = timespec_to_fosa_time(fosa_abs_time_t, time_tspec);
492
493     return result;
494 }
495
496 // --------------------------------------------------
497
498 static inline struct timespec fosa_abs_time_to_timespec(fosa_abs_time_t abst)
499 {
500     struct timespec result;
501
502     fosa_time_to_timespec(result, abst);
503
504     return result;
505 }
506
507
508
509 // --------------------------------------------------
510
511 static inline double fosa_rel_time_to_double(fosa_rel_time_t relt)
512 {
513     double result;
514
515     result = fosa_time_to_double(relt);
516     return result;
517 }
518
519
520 // --------------------------------------------------
521
522 static inline double fosa_double_to_rel_time(double time)
523 {
524     fosa_rel_time_t result;
525
526     result = fosa_double_to_time(fosa_rel_time_t, time);
527     return result;
528 }
529
530
531
532
533 #endif      /* !FOSA_TIME_H_ */