diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2013-01-10 14:07:30 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2013-01-10 14:07:30 +0000 |
commit | 0f27a84a512545f77a358719c9d3c29a1f323199 (patch) | |
tree | bf4bd5cd25203d4da42c97d78830c23ce4ea2019 | |
parent | 5d419d446892951306c7802ae490961b43a26f4b (diff) | |
download | nuttx-0f27a84a512545f77a358719c9d3c29a1f323199.tar.gz nuttx-0f27a84a512545f77a358719c9d3c29a1f323199.tar.bz2 nuttx-0f27a84a512545f77a358719c9d3c29a1f323199.zip |
Fix rounding in clock_time2ticks(). From Mike Smith.
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5502 42af7a65-404d-4744-a932-0658087f49c3
-rw-r--r-- | nuttx/ChangeLog | 2 | ||||
-rw-r--r-- | nuttx/sched/clock_time2ticks.c | 42 |
2 files changed, 40 insertions, 4 deletions
diff --git a/nuttx/ChangeLog b/nuttx/ChangeLog index 3d124073b..758e8c862 100644 --- a/nuttx/ChangeLog +++ b/nuttx/ChangeLog @@ -3897,4 +3897,6 @@ arch/arm/src/lm so that is can support other members of the Stellaris family. * libc/spawn: Add file action interfaces needed by posix_spawn(). + * sched/clock_time2ticks.c: Another case where time was being + rounded down instead of up (from Mikek Smith). diff --git a/nuttx/sched/clock_time2ticks.c b/nuttx/sched/clock_time2ticks.c index 383264d51..012f4e4f1 100644 --- a/nuttx/sched/clock_time2ticks.c +++ b/nuttx/sched/clock_time2ticks.c @@ -89,14 +89,48 @@ int clock_time2ticks(FAR const struct timespec *reltime, FAR int *ticks) { +#ifdef CONFIG_HAVE_LONG_LONG + int64_t relnsec; + + /* Convert the relative time into nanoseconds. The range of the int64_t is + * sufficiently large that there is no real need for range checking. + */ + + relnsec = (int64_t)reltime->tv_sec * NSEC_PER_SEC + + (int64_t)reltime->tv_nsec; + + /* Convert nanoseconds to clock ticks, rounding up to the smallest integer + * that is greater than or equal to the exact number of tick. + */ + + *ticks = (int)((relnsec + NSEC_PER_TICK - 1) / NSEC_PER_TICK); + return OK; +#else int32_t relusec; - /* Convert the relative time into microseconds.*/ + /* This function uses an int32_t to only the relative time in microseconds. + * that means that the maximum supported relative time is 2,147,487.647 + * seconds + */ + +#if 0 // overkill + DEBUGASSERT(reltime->tv_sec < 2147487 || + reltime->tv_sec == 2147487 && + reltime->tv_nsec <= 647 * NSEC_PER_MSEC); +#endif + + /* Convert the relative time into microseconds, rounding up to the smallest + * value that is greater than or equal to the exact number of microseconds. + */ - relusec = reltime->tv_sec * USEC_PER_SEC + reltime->tv_nsec / NSEC_PER_USEC; + relusec = reltime->tv_sec * USEC_PER_SEC + + (reltime->tv_nsec + NSEC_PER_USEC - 1) / NSEC_PER_USEC; - /* Convert microseconds to clock ticks */ + /* Convert microseconds to clock ticks, rounding up to the smallest integer + * that is greater than or equal to the exact number of tick. + */ - *ticks = relusec / USEC_PER_TICK; + *ticks = (int)((relusec + USEC_PER_TICK - 1) / USEC_PER_TICK); return OK; +#endif } |