source: rtems/cpukit/score/src/timespecdivide.c

Last change on this file was c499856, checked in by Chris Johns <chrisj@…>, on Mar 20, 2014 at 9:10:47 PM

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 1.2 KB
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.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <rtems/score/timespec.h>
22#include <rtems/score/todimpl.h>
23
24void _Timespec_Divide(
25  const struct timespec *lhs,
26  const struct timespec *rhs,
27  uint32_t              *ival_percentage,
28  uint32_t              *fval_percentage
29)
30{
31  uint64_t left, right, answer;
32
33  /*
34   *  For math simplicity just convert the timespec to nanoseconds
35   *  in a 64-bit integer.
36   */
37  left   = lhs->tv_sec * (uint64_t)TOD_NANOSECONDS_PER_SECOND;
38  left  += lhs->tv_nsec;
39  right  = rhs->tv_sec * (uint64_t)TOD_NANOSECONDS_PER_SECOND;
40  right += rhs->tv_nsec;
41
42  if ( right == 0 ) {
43    *ival_percentage = 0;
44    *fval_percentage = 0;
45    return;
46  }
47
48  /*
49   *  Put it back in the timespec result.
50   *
51   *  TODO: Rounding on the last digit of the fval.
52   */
53
54  answer = (left * 100000) / right;
55
56  *ival_percentage = answer / 1000;
57  *fval_percentage = answer % 1000;
58}
Note: See TracBrowser for help on using the repository browser.