1 | /** |
---|
2 | * @file rtems/score/timestamp64.h |
---|
3 | * |
---|
4 | * This include file contains helpers for manipulating |
---|
5 | * 64-bit integer timestamps. |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2009. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.com/license/LICENSE. |
---|
15 | * |
---|
16 | * $Id$ |
---|
17 | */ |
---|
18 | |
---|
19 | #ifndef _RTEMS_SCORE_TIMESTAMP64_H |
---|
20 | #define _RTEMS_SCORE_TIMESTAMP64_H |
---|
21 | |
---|
22 | /** |
---|
23 | * @defgroup SuperCore Timestamp64 |
---|
24 | * |
---|
25 | * @ingroup Score |
---|
26 | * |
---|
27 | * This handler encapsulates functionality related to manipulating |
---|
28 | * the 64 bit integer implementation of SuperCore Timestamps. |
---|
29 | */ |
---|
30 | /**@{*/ |
---|
31 | |
---|
32 | #ifdef __cplusplus |
---|
33 | extern "C" { |
---|
34 | #endif |
---|
35 | |
---|
36 | /* |
---|
37 | * This .h file is not for general use. It is an alternative |
---|
38 | * implementation of Timestamps and should only be used that way. |
---|
39 | */ |
---|
40 | #ifndef _RTEMS_SCORE_TIMESTAMP_H |
---|
41 | #error "Should only be included by rtems/score/timestamp.h" |
---|
42 | #endif |
---|
43 | |
---|
44 | /* |
---|
45 | * Verify something is defined. |
---|
46 | */ |
---|
47 | #if CPU_TIMESTAMP_USE_INT64 != TRUE && CPU_TIMESTAMP_USE_INT64_INLINE != TRUE |
---|
48 | #error "SuperCore Timestamp64 implementation included but not defined." |
---|
49 | #endif |
---|
50 | |
---|
51 | /** |
---|
52 | * Define the Timestamp control type. |
---|
53 | */ |
---|
54 | typedef int64_t Timestamp64_Control; |
---|
55 | |
---|
56 | static inline void _Timestamp64_implementation_Set( |
---|
57 | Timestamp64_Control *_time, |
---|
58 | Timestamp64_Control _seconds, |
---|
59 | Timestamp64_Control _nanoseconds |
---|
60 | ) |
---|
61 | { |
---|
62 | *_time = _seconds * 1000000000L + _nanoseconds; |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * @brief Set Timestamp to Seconds Nanosecond |
---|
67 | * |
---|
68 | * This method sets the timestamp to the specified seconds and nanoseconds |
---|
69 | * value. |
---|
70 | * |
---|
71 | * @param[in] _time points to the timestamp instance to validate. |
---|
72 | * @param[in] _seconds is the seconds portion of the timestamp |
---|
73 | * @param[in] _nanoseconds is the nanoseconds portion of the timestamp |
---|
74 | */ |
---|
75 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
76 | #define _Timestamp64_Set( _time, _seconds, _nanoseconds ) \ |
---|
77 | _Timestamp64_implementation_Set( _time, _seconds, _nanoseconds ) |
---|
78 | #else |
---|
79 | void _Timestamp64_Set( |
---|
80 | Timestamp64_Control *_time, |
---|
81 | Timestamp64_Control _seconds, |
---|
82 | Timestamp64_Control _nanoseconds |
---|
83 | ); |
---|
84 | #endif |
---|
85 | |
---|
86 | static inline void _Timestamp64_implementation_Set_to_zero( |
---|
87 | Timestamp64_Control *_time |
---|
88 | ) |
---|
89 | { |
---|
90 | *_time = 0; |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * @brief Zero Timestamp |
---|
95 | * |
---|
96 | * This method sets the timestamp to zero. |
---|
97 | * value. |
---|
98 | * |
---|
99 | * @param[in] _time points to the timestamp instance to zero. |
---|
100 | */ |
---|
101 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
102 | #define _Timestamp64_Set_to_zero( _time ) \ |
---|
103 | _Timestamp64_implementation_Set_to_zero( _time ) |
---|
104 | #else |
---|
105 | void _Timestamp64_Set_to_zero( |
---|
106 | Timestamp64_Control *_time |
---|
107 | ); |
---|
108 | #endif |
---|
109 | |
---|
110 | /** |
---|
111 | * @brief Is Timestamp Valid |
---|
112 | * |
---|
113 | * This method determines the validity of a timestamp. |
---|
114 | * |
---|
115 | * @param[in] _time points to the timestamp instance to validate. |
---|
116 | * |
---|
117 | * @return This method returns true if @a time is valid and |
---|
118 | * false otherwise. |
---|
119 | */ |
---|
120 | #define _Timestamp64_Is_valid( _time ) \ |
---|
121 | (1) |
---|
122 | |
---|
123 | static inline bool _Timestamp64_implementation_Less_than( |
---|
124 | const Timestamp64_Control *_lhs, |
---|
125 | const Timestamp64_Control *_rhs |
---|
126 | ) |
---|
127 | { |
---|
128 | return *_lhs < *_rhs; |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * @brief Timestamp Less Than Operator |
---|
133 | * |
---|
134 | * This method is the less than operator for timestamps. |
---|
135 | * |
---|
136 | * @param[in] _lhs points to the left hand side timestamp |
---|
137 | * @param[in] _rhs points to the right hand side timestamp |
---|
138 | * |
---|
139 | * @return This method returns true if @a _lhs is less than the @a _rhs and |
---|
140 | * false otherwise. |
---|
141 | */ |
---|
142 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
143 | #define _Timestamp64_Less_than( _lhs, _rhs ) \ |
---|
144 | _Timestamp64_implementation_Less_than( _lhs, _rhs ) |
---|
145 | #else |
---|
146 | bool _Timestamp64_Less_than( |
---|
147 | const Timestamp64_Control *_lhs, |
---|
148 | const Timestamp64_Control *_rhs |
---|
149 | ); |
---|
150 | #endif |
---|
151 | |
---|
152 | static inline bool _Timestamp64_implementation_Equal_to( |
---|
153 | const Timestamp64_Control *_lhs, |
---|
154 | const Timestamp64_Control *_rhs |
---|
155 | ) |
---|
156 | { |
---|
157 | return *_lhs == *_rhs; |
---|
158 | } |
---|
159 | |
---|
160 | #define _Timestamp64_Greater_than( _lhs, _rhs ) \ |
---|
161 | _Timestamp64_Less_than( _rhs, _lhs ) |
---|
162 | |
---|
163 | /** |
---|
164 | * @brief Timestamp equal to Operator |
---|
165 | * |
---|
166 | * This method is the is equal to than operator for timestamps. |
---|
167 | * |
---|
168 | * @param[in] _lhs points to the left hand side timestamp |
---|
169 | * @param[in] _rhs points to the right hand side timestamp |
---|
170 | * |
---|
171 | * @return This method returns true if @a _lhs is equal to @a _rhs and |
---|
172 | * false otherwise. |
---|
173 | */ |
---|
174 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
175 | #define _Timestamp64_Equal_to( _lhs, _rhs ) \ |
---|
176 | _Timestamp64_implementation_Equal_to( _lhs, _rhs ) |
---|
177 | #else |
---|
178 | bool _Timestamp64_Equal_to( |
---|
179 | const Timestamp64_Control *_lhs, |
---|
180 | const Timestamp64_Control *_rhs |
---|
181 | ); |
---|
182 | #endif |
---|
183 | |
---|
184 | static inline void _Timestamp64_implementation_Add_to( |
---|
185 | Timestamp64_Control *_time, |
---|
186 | const Timestamp64_Control *_add |
---|
187 | ) |
---|
188 | { |
---|
189 | *_time += *_add; |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * @brief Add to a Timestamp |
---|
194 | * |
---|
195 | * This routine adds two timestamps. The second argument is added |
---|
196 | * to the first. |
---|
197 | * |
---|
198 | * @param[in] _time points to the base time to be added to |
---|
199 | * @param[in] _add points to the timestamp to add to the first argument |
---|
200 | * |
---|
201 | * @return This method returns the number of seconds @a time increased by. |
---|
202 | */ |
---|
203 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
204 | #define _Timestamp64_Add_to( _time, _add ) \ |
---|
205 | _Timestamp64_implementation_Add_to( _time, _add ) |
---|
206 | #else |
---|
207 | void _Timestamp64_Add_to( |
---|
208 | Timestamp64_Control *_time, |
---|
209 | const Timestamp64_Control *_add |
---|
210 | ); |
---|
211 | #endif |
---|
212 | |
---|
213 | /** |
---|
214 | * @brief Add to a Timestamp (At Clock Tick) |
---|
215 | * |
---|
216 | * This routine adds two timestamps. The second argument is added |
---|
217 | * to the first. |
---|
218 | * |
---|
219 | * @node This routine places a special requirement on the addition |
---|
220 | * operation. It must return the number of units that the |
---|
221 | * seconds field changed as the result of the addition. Since this |
---|
222 | * operation is ONLY used as part of processing a clock tick, |
---|
223 | * it is generally safe to assume that only one second changed. |
---|
224 | * |
---|
225 | * @param[in] _time points to the base time to be added to |
---|
226 | * @param[in] _add points to the timestamp to add to the first argument |
---|
227 | * |
---|
228 | * @return This method returns the number of seconds @a time increased by. |
---|
229 | */ |
---|
230 | static inline uint32_t _Timestamp64_Add_to_at_tick( |
---|
231 | Timestamp64_Control *_time, |
---|
232 | const Timestamp64_Control *_add |
---|
233 | ) |
---|
234 | { |
---|
235 | Timestamp64_Control _start = *_time / 1000000000L; |
---|
236 | *_time += *_add; |
---|
237 | if ( ((*_time) / 1000000000L) != _start ) { |
---|
238 | return 1; |
---|
239 | } |
---|
240 | return 0; |
---|
241 | } |
---|
242 | |
---|
243 | /** |
---|
244 | * @brief Convert Timestamp to Number of Ticks |
---|
245 | * |
---|
246 | * This routine convert the @a time timestamp to the corresponding number |
---|
247 | * of clock ticks. |
---|
248 | * |
---|
249 | * @param[in] _time points to the time to be converted |
---|
250 | * |
---|
251 | * @return This method returns the number of ticks computed. |
---|
252 | */ |
---|
253 | uint32_t _Timestamp64_To_ticks( |
---|
254 | const Timestamp64_Control *_time |
---|
255 | ); |
---|
256 | |
---|
257 | /** |
---|
258 | * @brief Convert Ticks to Timestamp |
---|
259 | * |
---|
260 | * This routine converts the @a _ticks value to the corresponding |
---|
261 | * timestamp format @a _time. |
---|
262 | * |
---|
263 | * @param[in] _time points to the timestamp format time result |
---|
264 | * @param[out] _ticks points to the number of ticks to be filled in |
---|
265 | */ |
---|
266 | void _Timestamp64_From_ticks( |
---|
267 | uint32_t _ticks, |
---|
268 | Timestamp64_Control *_time |
---|
269 | ); |
---|
270 | |
---|
271 | static inline void _Timestamp64_implementation_Subtract( |
---|
272 | const Timestamp64_Control *_start, |
---|
273 | const Timestamp64_Control *_end, |
---|
274 | Timestamp64_Control *_result |
---|
275 | ) |
---|
276 | { |
---|
277 | *_result = *_end - *_start; |
---|
278 | } |
---|
279 | |
---|
280 | /** |
---|
281 | * @brief Subtract Two Timestamp |
---|
282 | * |
---|
283 | * This routine subtracts two timestamps. @a result is set to |
---|
284 | * @a end - @a start. |
---|
285 | * |
---|
286 | * @param[in] _start points to the starting time |
---|
287 | * @param[in] _end points to the ending time |
---|
288 | * @param[out] _result points to the difference between |
---|
289 | * starting and ending time. |
---|
290 | */ |
---|
291 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
292 | #define _Timestamp64_Subtract( _start, _end, _result ) \ |
---|
293 | _Timestamp64_implementation_Subtract( _start, _end, _result ) |
---|
294 | #else |
---|
295 | void _Timestamp64_Subtract( |
---|
296 | const Timestamp64_Control *_start, |
---|
297 | const Timestamp64_Control *_end, |
---|
298 | Timestamp64_Control *_result |
---|
299 | ); |
---|
300 | #endif |
---|
301 | |
---|
302 | static inline void _Timestamp64_implementation_Divide_by_integer( |
---|
303 | const Timestamp64_Control *_time, |
---|
304 | uint32_t _iterations, |
---|
305 | Timestamp64_Control *_result |
---|
306 | ) |
---|
307 | { |
---|
308 | *_result = *_time / _iterations; |
---|
309 | } |
---|
310 | |
---|
311 | /** |
---|
312 | * @brief Divide Timestamp By Integer |
---|
313 | * |
---|
314 | * This routine divides a timestamp by an integer value. The expected |
---|
315 | * use is to assist in benchmark calculations where you typically |
---|
316 | * divide a duration by a number of iterations. |
---|
317 | * |
---|
318 | * @param[in] _time points to the total |
---|
319 | * @param[in] _iterations is the number of iterations |
---|
320 | * @param[out] _result points to the average time. |
---|
321 | */ |
---|
322 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
323 | #define _Timestamp64_Divide_by_integer( _time, _iterations, _result ) \ |
---|
324 | _Timestamp64_implementation_Divide_by_integer( _time, _iterations, _result ) |
---|
325 | #else |
---|
326 | void _Timestamp64_Divide_by_integer( |
---|
327 | const Timestamp64_Control *_time, |
---|
328 | uint32_t _iterations, |
---|
329 | Timestamp64_Control *_result |
---|
330 | ); |
---|
331 | #endif |
---|
332 | |
---|
333 | /** |
---|
334 | * @brief Divide Timestamp |
---|
335 | * |
---|
336 | * This routine divides a timestamp by another timestamp. The |
---|
337 | * intended use is for calculating percentages to three decimal points. |
---|
338 | * |
---|
339 | * @param[in] _lhs points to the left hand number |
---|
340 | * @param[in] _rhs points to the right hand number |
---|
341 | * @param[out] _ival_percentage points to the integer portion of the average |
---|
342 | * @param[out] _fval_percentage points to the thousandths of percentage |
---|
343 | */ |
---|
344 | void _Timestamp64_Divide( |
---|
345 | const Timestamp64_Control *_lhs, |
---|
346 | const Timestamp64_Control *_rhs, |
---|
347 | uint32_t *_ival_percentage, |
---|
348 | uint32_t *_fval_percentage |
---|
349 | ); |
---|
350 | |
---|
351 | static inline uint32_t _Timestamp64_implementation_Get_seconds( |
---|
352 | const Timestamp64_Control *_time |
---|
353 | ) |
---|
354 | { |
---|
355 | return (uint32_t) (*_time / 1000000000L); |
---|
356 | } |
---|
357 | |
---|
358 | /** |
---|
359 | * @brief Get Seconds Portion of Timestamp |
---|
360 | * |
---|
361 | * This method returns the seconds portion of the specified timestamp |
---|
362 | * |
---|
363 | * @param[in] _time points to the timestamp |
---|
364 | * |
---|
365 | * @return The seconds portion of @a _time. |
---|
366 | */ |
---|
367 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
368 | #define _Timestamp64_Get_seconds( _time ) \ |
---|
369 | _Timestamp64_implementation_Get_seconds( _time ) |
---|
370 | #else |
---|
371 | uint32_t _Timestamp64_Get_seconds( |
---|
372 | const Timestamp64_Control *_time |
---|
373 | ); |
---|
374 | #endif |
---|
375 | |
---|
376 | static inline uint32_t _Timestamp64_implementation_Get_nanoseconds( |
---|
377 | const Timestamp64_Control *_time |
---|
378 | ) |
---|
379 | { |
---|
380 | return (uint32_t) (*_time % 1000000000L); |
---|
381 | } |
---|
382 | |
---|
383 | /** |
---|
384 | * @brief Get Nanoseconds Portion of Timestamp |
---|
385 | * |
---|
386 | * This method returns the nanoseconds portion of the specified timestamp |
---|
387 | * |
---|
388 | * @param[in] _time points to the timestamp |
---|
389 | * |
---|
390 | * @return The nanoseconds portion of @a _time. |
---|
391 | */ |
---|
392 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
393 | #define _Timestamp64_Get_nanoseconds( _time ) \ |
---|
394 | _Timestamp64_implementation_Get_nanoseconds( _time ) |
---|
395 | #else |
---|
396 | uint32_t _Timestamp64_Get_nanoseconds( |
---|
397 | const Timestamp64_Control *_time |
---|
398 | ); |
---|
399 | #endif |
---|
400 | |
---|
401 | static inline void _Timestamp64_implementation_To_timespec( |
---|
402 | const Timestamp64_Control *_timestamp, |
---|
403 | struct timespec *_timespec |
---|
404 | ) |
---|
405 | { |
---|
406 | _timespec->tv_sec = (time_t) (*_timestamp / 1000000000L); |
---|
407 | _timespec->tv_nsec = (long) (*_timestamp % 1000000000L); |
---|
408 | } |
---|
409 | |
---|
410 | /** |
---|
411 | * @brief Convert Timestamp to struct timespec |
---|
412 | * |
---|
413 | * This method returns the seconds portion of the specified timestamp |
---|
414 | * |
---|
415 | * @param[in] _timestamp points to the timestamp |
---|
416 | * @param[out] _timespec points to the timespec |
---|
417 | */ |
---|
418 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
419 | #define _Timestamp64_To_timespec( _timestamp, _timespec ) \ |
---|
420 | _Timestamp64_implementation_To_timespec( _timestamp, _timespec ) |
---|
421 | #else |
---|
422 | void _Timestamp64_To_timespec( |
---|
423 | const Timestamp64_Control *_timestamp, |
---|
424 | struct timespec *_timespec |
---|
425 | ); |
---|
426 | #endif |
---|
427 | |
---|
428 | #ifdef __cplusplus |
---|
429 | } |
---|
430 | #endif |
---|
431 | |
---|
432 | /**@}*/ |
---|
433 | |
---|
434 | #endif |
---|
435 | /* end of include file */ |
---|