summaryrefslogtreecommitdiffstats
path: root/cpukit/score/include/rtems/score/timestamp64.h
blob: 4bba7c9a2dbf8e64bf70319d4e9d9c2a8434a1e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/**
 *  @file  rtems/score/timestamp64.h
 *
 *  This include file contains helpers for manipulating
 *  64-bit integer timestamps.
 */

/*
 *  COPYRIGHT (c) 1989-2009.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  $Id$
 */

#ifndef _RTEMS_SCORE_TIMESTAMP64_H
#define _RTEMS_SCORE_TIMESTAMP64_H

/**
 *  @defgroup SuperCore Timestamp64
 *
 *  @ingroup Score
 *
 *  This handler encapsulates functionality related to manipulating
 *  the 64 bit integer implementation of SuperCore Timestamps.
 */
/**@{*/

#ifdef __cplusplus
extern "C" {
#endif

/*
 *  This .h file is not for general use.  It is an alternative
 *  implementation of Timestamps and should only be used that way.
 */
#ifndef _RTEMS_SCORE_TIMESTAMP_H
  #error "Should only be included by rtems/score/timestamp.h"
#endif

/*
 *  Verify something is defined.
 */
#if CPU_TIMESTAMP_USE_INT64 != TRUE && CPU_TIMESTAMP_USE_INT64_INLINE != TRUE
  #error "SuperCore Timestamp64 implementation included but not defined."
#endif

/**
 *   Define the Timestamp control type.
 */
typedef int64_t Timestamp64_Control;

static inline void _Timestamp64_implementation_Set(
  Timestamp64_Control *_time,
  Timestamp64_Control  _seconds,
  Timestamp64_Control  _nanoseconds
)
{
  *_time = _seconds * 1000000000L + _nanoseconds;
}

/**
 *  @brief Set Timestamp to Seconds Nanosecond
 *
 *  This method sets the timestamp to the specified seconds and nanoseconds
 *  value.
 *
 *  @param[in] _time points to the timestamp instance to validate.
 *  @param[in] _seconds is the seconds portion of the timestamp
 *  @param[in] _nanoseconds is the nanoseconds portion of the timestamp
 */
#if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE
  #define _Timestamp64_Set( _time, _seconds, _nanoseconds ) \
    _Timestamp64_implementation_Set( _time, _seconds, _nanoseconds )
#else
  void _Timestamp64_Set(
    Timestamp64_Control *_time,
    Timestamp64_Control  _seconds,
    Timestamp64_Control  _nanoseconds
  );
#endif

static inline void _Timestamp64_implementation_Set_to_zero(
  Timestamp64_Control *_time
)
{
  *_time = 0;
}

/**
 *  @brief Zero Timestamp
 *
 *  This method sets the timestamp to zero.
 *  value.
 *
 *  @param[in] _time points to the timestamp instance to zero.
 */
#if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE
  #define _Timestamp64_Set_to_zero( _time ) \
    _Timestamp64_implementation_Set_to_zero( _time )
#else
  void _Timestamp64_Set_to_zero(
    Timestamp64_Control *_time
  );
#endif

/**
 *  @brief Is Timestamp Valid
 *
 *  This method determines the validity of a timestamp.
 *
 *  @param[in] _time points to the timestamp instance to validate.
 *
 *  @return This method returns true if @a time is valid and
 *          false otherwise.
 */
#define _Timestamp64_Is_valid( _time ) \
  (1)

static inline bool _Timestamp64_implementation_Less_than(
  const Timestamp64_Control *_lhs,
  const Timestamp64_Control *_rhs
)
{
  return *_lhs < *_rhs;
}

/**
 *  @brief Timestamp Less Than Operator
 *
 *  This method is the less than operator for timestamps.
 *
 *  @param[in] _lhs points to the left hand side timestamp
 *  @param[in] _rhs points to the right hand side timestamp
 *
 *  @return This method returns true if @a _lhs is less than the @a _rhs and
 *          false otherwise.
 */
#if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE
  #define _Timestamp64_Less_than( _lhs, _rhs ) \
    _Timestamp64_implementation_Less_than( _lhs, _rhs )
#else
  bool _Timestamp64_Less_than(
    const Timestamp64_Control *_lhs,
    const Timestamp64_Control *_rhs
  );
#endif

static inline bool _Timestamp64_implementation_Equal_to(
  const Timestamp64_Control *_lhs,
  const Timestamp64_Control *_rhs
)
{
  return *_lhs == *_rhs;
}

#define _Timestamp64_Greater_than( _lhs, _rhs ) \
  _Timestamp64_Less_than( _rhs, _lhs )

/**
 *  @brief Timestamp equal to Operator
 *
 *  This method is the is equal to than operator for timestamps.
 *
 *  @param[in] _lhs points to the left hand side timestamp
 *  @param[in] _rhs points to the right hand side timestamp
 *
 *  @return This method returns true if @a _lhs is equal to  @a _rhs and
 *          false otherwise.
 */
#if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE
  #define _Timestamp64_Equal_to( _lhs, _rhs ) \
    _Timestamp64_implementation_Equal_to( _lhs, _rhs )
#else
  bool _Timestamp64_Equal_to(
    const Timestamp64_Control *_lhs,
    const Timestamp64_Control *_rhs
  );
#endif

static inline void _Timestamp64_implementation_Add_to(
  Timestamp64_Control       *_time,
  const Timestamp64_Control *_add
)
{
  *_time += *_add;
}

/**
 *  @brief Add to a Timestamp
 *
 *  This routine adds two timestamps.  The second argument is added
 *  to the first.
 *
 *  @param[in] _time points to the base time to be added to
 *  @param[in] _add points to the timestamp to add to the first argument
 *
 *  @return This method returns the number of seconds @a time increased by.
 */
#if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE
  #define _Timestamp64_Add_to( _time, _add ) \
    _Timestamp64_implementation_Add_to( _time, _add )
#else
  void _Timestamp64_Add_to(
    Timestamp64_Control       *_time,
    const Timestamp64_Control *_add
  );
#endif

/**
 *  @brief Add to a Timestamp (At Clock Tick)
 *
 *  This routine adds two timestamps.  The second argument is added
 *  to the first.
 *
 *  @node This routine places a special requirement on the addition
 *        operation.  It must return the number of units that the
 *        seconds field changed as the result of the addition.  Since this
 *        operation is ONLY used as part of processing a clock tick,
 *        it is generally safe to assume that only one second changed.
 *
 *  @param[in] _time points to the base time to be added to
 *  @param[in] _add points to the timestamp to add to the first argument
 *
 *  @return This method returns the number of seconds @a time increased by.
 */
static inline uint32_t _Timestamp64_Add_to_at_tick(
  Timestamp64_Control *_time,
  const Timestamp64_Control *_add
)
{
  Timestamp64_Control _start = *_time / 1000000000L;
  *_time += *_add;
  if ( ((*_time) / 1000000000L) != _start ) {
    return 1;
  }
  return 0;
}

/**
 *  @brief Convert Timestamp to Number of Ticks
 *
 *  This routine convert the @a time timestamp to the corresponding number
 *  of clock ticks.
 *
 *  @param[in] _time points to the time to be converted
 *
 *  @return This method returns the number of ticks computed.
 */
uint32_t _Timestamp64_To_ticks(
  const Timestamp64_Control *_time
);

/**
 *  @brief Convert Ticks to Timestamp
 *
 *  This routine converts the @a _ticks value to the corresponding
 *  timestamp format @a _time.
 *
 *  @param[in] _time points to the timestamp format time result
 *  @param[out] _ticks points to the number of ticks to be filled in
 */
void _Timestamp64_From_ticks(
  uint32_t             _ticks,
  Timestamp64_Control *_time
);

static inline void _Timestamp64_implementation_Subtract(
  const Timestamp64_Control *_start,
  const Timestamp64_Control *_end,
  Timestamp64_Control       *_result
)
{
  *_result = *_end - *_start;
}

/**
 *  @brief Subtract Two Timestamp
 *
 *  This routine subtracts two timestamps.  @a result is set to
 *  @a end - @a start.
 *
 *  @param[in] _start points to the starting time
 *  @param[in] _end points to the ending time
 *  @param[out] _result points to the difference between
 *             starting and ending time.
 */
#if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE
  #define _Timestamp64_Subtract( _start, _end, _result ) \
    _Timestamp64_implementation_Subtract( _start, _end, _result )
#else
  void _Timestamp64_Subtract(
    const Timestamp64_Control *_start,
    const Timestamp64_Control *_end,
    Timestamp64_Control       *_result
  );
#endif

static inline void _Timestamp64_implementation_Divide_by_integer(
  const Timestamp64_Control *_time,
  uint32_t             _iterations,
  Timestamp64_Control *_result
)
{
  *_result = *_time / _iterations;
}

/**
 *  @brief Divide Timestamp By Integer
 *
 *  This routine divides a timestamp by an integer value.  The expected
 *  use is to assist in benchmark calculations where you typically
 *  divide a duration by a number of iterations.
 *
 *  @param[in] _time points to the total
 *  @param[in] _iterations is the number of iterations
 *  @param[out] _result points to the average time.
 */
#if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE
  #define _Timestamp64_Divide_by_integer( _time, _iterations, _result ) \
    _Timestamp64_implementation_Divide_by_integer( _time, _iterations, _result )
#else
  void _Timestamp64_Divide_by_integer(
    const Timestamp64_Control *_time,
    uint32_t                   _iterations,
    Timestamp64_Control       *_result
  );
#endif

/**
 *  @brief Divide Timestamp
 *
 *  This routine divides a timestamp by another timestamp.  The
 *  intended use is for calculating percentages to three decimal points.
 *
 *  @param[in] _lhs points to the left hand number
 *  @param[in] _rhs points to the right hand number
 *  @param[out] _ival_percentage points to the integer portion of the average
 *  @param[out] _fval_percentage points to the thousandths of percentage
 */
void _Timestamp64_Divide(
  const Timestamp64_Control *_lhs,
  const Timestamp64_Control *_rhs,
  uint32_t                  *_ival_percentage,
  uint32_t                  *_fval_percentage
);

static inline uint32_t _Timestamp64_implementation_Get_seconds(
  const Timestamp64_Control *_time
)
{
  return (uint32_t) (*_time / 1000000000L);
}

/**
 *  @brief Get Seconds Portion of Timestamp
 *
 *  This method returns the seconds portion of the specified timestamp
 *
 *  @param[in] _time points to the timestamp
 *
 *  @return The seconds portion of @a _time.
 */
#if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE
  #define _Timestamp64_Get_seconds( _time ) \
    _Timestamp64_implementation_Get_seconds( _time )
#else
  uint32_t _Timestamp64_Get_seconds(
    const Timestamp64_Control *_time
  );
#endif

static inline uint32_t _Timestamp64_implementation_Get_nanoseconds(
  const Timestamp64_Control *_time
)
{
  return (uint32_t) (*_time % 1000000000L);
}

/**
 *  @brief Get Nanoseconds Portion of Timestamp
 *
 *  This method returns the nanoseconds portion of the specified timestamp
 *
 *  @param[in] _time points to the timestamp
 *
 *  @return The nanoseconds portion of @a _time.
 */
#if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE
  #define _Timestamp64_Get_nanoseconds( _time ) \
    _Timestamp64_implementation_Get_nanoseconds( _time )
#else
  uint32_t _Timestamp64_Get_nanoseconds(
    const Timestamp64_Control *_time
  );
#endif

static inline void _Timestamp64_implementation_To_timespec(
  const Timestamp64_Control *_timestamp,
  struct timespec           *_timespec
)
{
  _timespec->tv_sec = (time_t) (*_timestamp / 1000000000L);
  _timespec->tv_nsec = (long) (*_timestamp % 1000000000L);
}

/**
 *  @brief Convert Timestamp to struct timespec
 *
 *  This method returns the seconds portion of the specified timestamp
 *
 *  @param[in] _timestamp points to the timestamp
 *  @param[out] _timespec points to the timespec
 */
#if CPU_TIMESTAMP_USE_INT64_INLINE == TRUE
  #define _Timestamp64_To_timespec( _timestamp, _timespec  ) \
    _Timestamp64_implementation_To_timespec( _timestamp, _timespec )
#else
  void _Timestamp64_To_timespec(
    const Timestamp64_Control *_timestamp,
    struct timespec           *_timespec
  );
#endif

#ifdef __cplusplus
}
#endif

/**@}*/

#endif
/* end of include file */