source: rtems/cpukit/score/src/timespecdivide.c @ 25f5730f

4.11
Last change on this file since 25f5730f 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
RevLine 
[28352fae]1/**
[f7f1d77]2 * @file
3 *
4 * @brief Divide Timespec By Integer
5 * @ingroup Timespec
[f919582d]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
[c499856]14 *  http://www.rtems.org/license/LICENSE.
[f919582d]15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <rtems/score/timespec.h>
[f031df0e]22#include <rtems/score/todimpl.h>
[f919582d]23
24void _Timespec_Divide(
[c3330a8]25  const struct timespec *lhs,
26  const struct timespec *rhs,
27  uint32_t              *ival_percentage,
28  uint32_t              *fval_percentage
[f919582d]29)
30{
[c3330a8]31  uint64_t left, right, answer;
[f919582d]32
33  /*
34   *  For math simplicity just convert the timespec to nanoseconds
35   *  in a 64-bit integer.
36   */
[c3330a8]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;
[f919582d]41
[b0ac06f8]42  if ( right == 0 ) {
[c3330a8]43    *ival_percentage = 0;
[b0ac06f8]44    *fval_percentage = 0;
[c3330a8]45    return;
46  }
[f919582d]47
48  /*
[b0ac06f8]49   *  Put it back in the timespec result.
50   *
51   *  TODO: Rounding on the last digit of the fval.
[f919582d]52   */
53
[3462c34]54  answer = (left * 100000) / right;
[c3330a8]55
56  *ival_percentage = answer / 1000;
[b0ac06f8]57  *fval_percentage = answer % 1000;
[f919582d]58}
Note: See TracBrowser for help on using the repository browser.