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