4.115
Line | |
---|
1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Divide Timespec By Integer |
---|
5 | * @ingroup Timespec |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2007. |
---|
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 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/system.h> |
---|
22 | #include <sys/types.h> |
---|
23 | #include <rtems/score/timespec.h> |
---|
24 | #include <rtems/score/tod.h> |
---|
25 | |
---|
26 | void _Timespec_Divide( |
---|
27 | const struct timespec *lhs, |
---|
28 | const struct timespec *rhs, |
---|
29 | uint32_t *ival_percentage, |
---|
30 | uint32_t *fval_percentage |
---|
31 | ) |
---|
32 | { |
---|
33 | uint64_t left, right, answer; |
---|
34 | |
---|
35 | /* |
---|
36 | * For math simplicity just convert the timespec to nanoseconds |
---|
37 | * in a 64-bit integer. |
---|
38 | */ |
---|
39 | left = lhs->tv_sec * (uint64_t)TOD_NANOSECONDS_PER_SECOND; |
---|
40 | left += lhs->tv_nsec; |
---|
41 | right = rhs->tv_sec * (uint64_t)TOD_NANOSECONDS_PER_SECOND; |
---|
42 | right += rhs->tv_nsec; |
---|
43 | |
---|
44 | if ( right == 0 ) { |
---|
45 | *ival_percentage = 0; |
---|
46 | *fval_percentage = 0; |
---|
47 | return; |
---|
48 | } |
---|
49 | |
---|
50 | /* |
---|
51 | * Put it back in the timespec result. |
---|
52 | * |
---|
53 | * TODO: Rounding on the last digit of the fval. |
---|
54 | */ |
---|
55 | |
---|
56 | answer = (left * 100000) / right; |
---|
57 | |
---|
58 | *ival_percentage = answer / 1000; |
---|
59 | *fval_percentage = answer % 1000; |
---|
60 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.