1 | /** |
---|
2 | * @file rtems/score/timestamp64.h |
---|
3 | * |
---|
4 | * @brief Helpers for Manipulating 64-bit Integer Timestamps |
---|
5 | * |
---|
6 | * This include file contains helpers for manipulating |
---|
7 | * 64-bit integer timestamps. |
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | * COPYRIGHT (c) 1989-2009. |
---|
12 | * On-Line Applications Research Corporation (OAR). |
---|
13 | * |
---|
14 | * The license and distribution terms for this file may be |
---|
15 | * found in the file LICENSE in this distribution or at |
---|
16 | * http://www.rtems.com/license/LICENSE. |
---|
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 64-bit 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 Sets the 64-bit timestamp to zero. |
---|
95 | * |
---|
96 | * This method sets the timestamp to zero value. |
---|
97 | * |
---|
98 | * @param[in] _time points to the timestamp instance to zero. |
---|
99 | */ |
---|
100 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
101 | #define _Timestamp64_Set_to_zero( _time ) \ |
---|
102 | _Timestamp64_implementation_Set_to_zero( _time ) |
---|
103 | #else |
---|
104 | void _Timestamp64_Set_to_zero( |
---|
105 | Timestamp64_Control *_time |
---|
106 | ); |
---|
107 | #endif |
---|
108 | |
---|
109 | /** |
---|
110 | * @brief Determines the validity of a 64-bit timestamp. |
---|
111 | * |
---|
112 | * This method determines the validity of a timestamp. |
---|
113 | * |
---|
114 | * @param[in] _time points to the timestamp instance to validate. |
---|
115 | * |
---|
116 | * @retval This method returns true if @a time is valid and |
---|
117 | * false otherwise. |
---|
118 | */ |
---|
119 | #define _Timestamp64_Is_valid( _time ) \ |
---|
120 | (1) |
---|
121 | |
---|
122 | static inline bool _Timestamp64_implementation_Less_than( |
---|
123 | const Timestamp64_Control *_lhs, |
---|
124 | const Timestamp64_Control *_rhs |
---|
125 | ) |
---|
126 | { |
---|
127 | return *_lhs < *_rhs; |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * @brief The "less than" operator for 64-bit timestamps. |
---|
132 | * |
---|
133 | * This method is the less than operator for timestamps. |
---|
134 | * |
---|
135 | * @param[in] _lhs points to the left hand side timestamp |
---|
136 | * @param[in] _rhs points to the right hand side timestamp |
---|
137 | * |
---|
138 | * @retval This method returns true if @a _lhs is less than the @a _rhs and |
---|
139 | * false otherwise. |
---|
140 | */ |
---|
141 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
142 | #define _Timestamp64_Less_than( _lhs, _rhs ) \ |
---|
143 | _Timestamp64_implementation_Less_than( _lhs, _rhs ) |
---|
144 | #else |
---|
145 | bool _Timestamp64_Less_than( |
---|
146 | const Timestamp64_Control *_lhs, |
---|
147 | const Timestamp64_Control *_rhs |
---|
148 | ); |
---|
149 | #endif |
---|
150 | |
---|
151 | static inline bool _Timestamp64_implementation_Equal_to( |
---|
152 | const Timestamp64_Control *_lhs, |
---|
153 | const Timestamp64_Control *_rhs |
---|
154 | ) |
---|
155 | { |
---|
156 | return *_lhs == *_rhs; |
---|
157 | } |
---|
158 | |
---|
159 | #define _Timestamp64_Greater_than( _lhs, _rhs ) \ |
---|
160 | _Timestamp64_Less_than( _rhs, _lhs ) |
---|
161 | |
---|
162 | /** |
---|
163 | * @brief The "equal to" operator for 64-bit timestamps. |
---|
164 | * |
---|
165 | * This method is the is equal to than operator for timestamps. |
---|
166 | * |
---|
167 | * @param[in] _lhs points to the left hand side timestamp |
---|
168 | * @param[in] _rhs points to the right hand side timestamp |
---|
169 | * |
---|
170 | * @retval This method returns true if @a _lhs is equal to @a _rhs and |
---|
171 | * false otherwise. |
---|
172 | */ |
---|
173 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
174 | #define _Timestamp64_Equal_to( _lhs, _rhs ) \ |
---|
175 | _Timestamp64_implementation_Equal_to( _lhs, _rhs ) |
---|
176 | #else |
---|
177 | bool _Timestamp64_Equal_to( |
---|
178 | const Timestamp64_Control *_lhs, |
---|
179 | const Timestamp64_Control *_rhs |
---|
180 | ); |
---|
181 | #endif |
---|
182 | |
---|
183 | static inline void _Timestamp64_implementation_Add_to( |
---|
184 | Timestamp64_Control *_time, |
---|
185 | const Timestamp64_Control *_add |
---|
186 | ) |
---|
187 | { |
---|
188 | *_time += *_add; |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | * @brief Add two 64-bit timestamps. |
---|
193 | * |
---|
194 | * This routine adds two timestamps. The second argument is added |
---|
195 | * to the first. |
---|
196 | * |
---|
197 | * @param[in] _time points to the base time to be added to |
---|
198 | * @param[in] _add points to the timestamp to add to the first argument |
---|
199 | * |
---|
200 | * @retval This method returns the number of seconds @a time increased by. |
---|
201 | */ |
---|
202 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
203 | #define _Timestamp64_Add_to( _time, _add ) \ |
---|
204 | _Timestamp64_implementation_Add_to( _time, _add ) |
---|
205 | #else |
---|
206 | void _Timestamp64_Add_to( |
---|
207 | Timestamp64_Control *_time, |
---|
208 | const Timestamp64_Control *_add |
---|
209 | ); |
---|
210 | #endif |
---|
211 | |
---|
212 | /** |
---|
213 | * @brief Convert 64-bit timestamp to number of ticks. |
---|
214 | * |
---|
215 | * This routine convert the @a time timestamp to the corresponding number |
---|
216 | * of clock ticks. |
---|
217 | * |
---|
218 | * @param[in] _time points to the time to be converted |
---|
219 | * |
---|
220 | * @retval This method returns the number of ticks computed. |
---|
221 | */ |
---|
222 | uint32_t _Timestamp64_To_ticks( |
---|
223 | const Timestamp64_Control *_time |
---|
224 | ); |
---|
225 | |
---|
226 | /** |
---|
227 | * @brief Convert ticks to 64-bit timestamp. |
---|
228 | * |
---|
229 | * This routine converts the @a _ticks value to the corresponding |
---|
230 | * timestamp format @a _time. |
---|
231 | * |
---|
232 | * @param[in] _time points to the timestamp format time result |
---|
233 | * @param[out] _ticks points to the number of ticks to be filled in |
---|
234 | */ |
---|
235 | void _Timestamp64_From_ticks( |
---|
236 | uint32_t _ticks, |
---|
237 | Timestamp64_Control *_time |
---|
238 | ); |
---|
239 | |
---|
240 | static inline void _Timestamp64_implementation_Subtract( |
---|
241 | const Timestamp64_Control *_start, |
---|
242 | const Timestamp64_Control *_end, |
---|
243 | Timestamp64_Control *_result |
---|
244 | ) |
---|
245 | { |
---|
246 | *_result = *_end - *_start; |
---|
247 | } |
---|
248 | |
---|
249 | /** |
---|
250 | * @brief Subtract two 64-bit timestamps. |
---|
251 | * |
---|
252 | * This routine subtracts two timestamps. @a result is set to |
---|
253 | * @a end - @a start. |
---|
254 | * |
---|
255 | * @param[in] _start points to the starting time |
---|
256 | * @param[in] _end points to the ending time |
---|
257 | * @param[out] _result points to the difference between |
---|
258 | * starting and ending time. |
---|
259 | */ |
---|
260 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
261 | #define _Timestamp64_Subtract( _start, _end, _result ) \ |
---|
262 | _Timestamp64_implementation_Subtract( _start, _end, _result ) |
---|
263 | #else |
---|
264 | void _Timestamp64_Subtract( |
---|
265 | const Timestamp64_Control *_start, |
---|
266 | const Timestamp64_Control *_end, |
---|
267 | Timestamp64_Control *_result |
---|
268 | ); |
---|
269 | #endif |
---|
270 | |
---|
271 | static inline void _Timestamp64_implementation_Divide_by_integer( |
---|
272 | const Timestamp64_Control *_time, |
---|
273 | uint32_t _iterations, |
---|
274 | Timestamp64_Control *_result |
---|
275 | ) |
---|
276 | { |
---|
277 | *_result = *_time / _iterations; |
---|
278 | } |
---|
279 | |
---|
280 | /** |
---|
281 | * @brief Divide 64-bit timestamp by an integer. |
---|
282 | * |
---|
283 | * This routine divides a timestamp by an integer value. The expected |
---|
284 | * use is to assist in benchmark calculations where you typically |
---|
285 | * divide a duration by a number of iterations. |
---|
286 | * |
---|
287 | * @param[in] _time points to the total |
---|
288 | * @param[in] _iterations is the number of iterations |
---|
289 | * @param[out] _result points to the average time. |
---|
290 | */ |
---|
291 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
292 | #define _Timestamp64_Divide_by_integer( _time, _iterations, _result ) \ |
---|
293 | _Timestamp64_implementation_Divide_by_integer( _time, _iterations, _result ) |
---|
294 | #else |
---|
295 | void _Timestamp64_Divide_by_integer( |
---|
296 | const Timestamp64_Control *_time, |
---|
297 | uint32_t _iterations, |
---|
298 | Timestamp64_Control *_result |
---|
299 | ); |
---|
300 | #endif |
---|
301 | |
---|
302 | /** |
---|
303 | * @brief Divide 64-bit timestamp by another 64-bit timestamp. |
---|
304 | * |
---|
305 | * This routine divides a timestamp by another timestamp. The |
---|
306 | * intended use is for calculating percentages to three decimal points. |
---|
307 | * |
---|
308 | * @param[in] _lhs points to the left hand number |
---|
309 | * @param[in] _rhs points to the right hand number |
---|
310 | * @param[out] _ival_percentage points to the integer portion of the average |
---|
311 | * @param[out] _fval_percentage points to the thousandths of percentage |
---|
312 | */ |
---|
313 | void _Timestamp64_Divide( |
---|
314 | const Timestamp64_Control *_lhs, |
---|
315 | const Timestamp64_Control *_rhs, |
---|
316 | uint32_t *_ival_percentage, |
---|
317 | uint32_t *_fval_percentage |
---|
318 | ); |
---|
319 | |
---|
320 | static inline uint32_t _Timestamp64_implementation_Get_seconds( |
---|
321 | const Timestamp64_Control *_time |
---|
322 | ) |
---|
323 | { |
---|
324 | return (uint32_t) (*_time / 1000000000L); |
---|
325 | } |
---|
326 | |
---|
327 | /** |
---|
328 | * @brief Get seconds portion of a 64-bit timestamp. |
---|
329 | * |
---|
330 | * This method returns the seconds portion of the specified timestamp |
---|
331 | * |
---|
332 | * @param[in] _time points to the timestamp |
---|
333 | * |
---|
334 | * @retval The seconds portion of @a _time. |
---|
335 | */ |
---|
336 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
337 | #define _Timestamp64_Get_seconds( _time ) \ |
---|
338 | _Timestamp64_implementation_Get_seconds( _time ) |
---|
339 | #else |
---|
340 | uint32_t _Timestamp64_Get_seconds( |
---|
341 | const Timestamp64_Control *_time |
---|
342 | ); |
---|
343 | #endif |
---|
344 | |
---|
345 | static inline uint32_t _Timestamp64_implementation_Get_nanoseconds( |
---|
346 | const Timestamp64_Control *_time |
---|
347 | ) |
---|
348 | { |
---|
349 | return (uint32_t) (*_time % 1000000000L); |
---|
350 | } |
---|
351 | |
---|
352 | /** |
---|
353 | * @brief Get nanoseconds portion of a 64-bit timestamp. |
---|
354 | * |
---|
355 | * This method returns the nanoseconds portion of the specified timestamp |
---|
356 | * |
---|
357 | * @param[in] _time points to the timestamp |
---|
358 | * |
---|
359 | * @retval The nanoseconds portion of @a _time. |
---|
360 | */ |
---|
361 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
362 | #define _Timestamp64_Get_nanoseconds( _time ) \ |
---|
363 | _Timestamp64_implementation_Get_nanoseconds( _time ) |
---|
364 | #else |
---|
365 | uint32_t _Timestamp64_Get_nanoseconds( |
---|
366 | const Timestamp64_Control *_time |
---|
367 | ); |
---|
368 | #endif |
---|
369 | |
---|
370 | static inline void _Timestamp64_implementation_To_timespec( |
---|
371 | const Timestamp64_Control *_timestamp, |
---|
372 | struct timespec *_timespec |
---|
373 | ) |
---|
374 | { |
---|
375 | _timespec->tv_sec = (time_t) (*_timestamp / 1000000000L); |
---|
376 | _timespec->tv_nsec = (long) (*_timestamp % 1000000000L); |
---|
377 | } |
---|
378 | |
---|
379 | /** |
---|
380 | * @brief Convert 64-bit timestamp to struct timespec. |
---|
381 | * |
---|
382 | * This method returns the seconds portion of the specified timestamp |
---|
383 | * |
---|
384 | * @param[in] _timestamp points to the timestamp |
---|
385 | * @param[out] _timespec points to the timespec |
---|
386 | */ |
---|
387 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
388 | #define _Timestamp64_To_timespec( _timestamp, _timespec ) \ |
---|
389 | _Timestamp64_implementation_To_timespec( _timestamp, _timespec ) |
---|
390 | #else |
---|
391 | void _Timestamp64_To_timespec( |
---|
392 | const Timestamp64_Control *_timestamp, |
---|
393 | struct timespec *_timespec |
---|
394 | ); |
---|
395 | #endif |
---|
396 | |
---|
397 | static inline void _Timestamp64_implementation_To_timeval( |
---|
398 | const Timestamp64_Control *_timestamp, |
---|
399 | struct timeval *_timeval |
---|
400 | ) |
---|
401 | { |
---|
402 | _timeval->tv_sec = (time_t) (*_timestamp / 1000000000U); |
---|
403 | _timeval->tv_usec = (suseconds_t) ((*_timestamp % 1000000000U) / 1000U); |
---|
404 | } |
---|
405 | |
---|
406 | /** |
---|
407 | * @brief Convert 64-bit timestamp to struct timeval. |
---|
408 | * |
---|
409 | * This method returns the seconds portion of the specified timestamp |
---|
410 | * |
---|
411 | * @param[in] _timestamp points to the timestamp |
---|
412 | * @param[out] _timeval points to the timeval |
---|
413 | */ |
---|
414 | #if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE |
---|
415 | #define _Timestamp64_To_timeval( _timestamp, _timeval ) \ |
---|
416 | _Timestamp64_implementation_To_timeval( _timestamp, _timeval ) |
---|
417 | #else |
---|
418 | void _Timestamp64_To_timeval( |
---|
419 | const Timestamp64_Control *_timestamp, |
---|
420 | struct timeval *_timeval |
---|
421 | ); |
---|
422 | #endif |
---|
423 | |
---|
424 | #ifdef __cplusplus |
---|
425 | } |
---|
426 | #endif |
---|
427 | |
---|
428 | /**@}*/ |
---|
429 | |
---|
430 | #endif |
---|
431 | /* end of include file */ |
---|