nanosecond: fix up conversion of ticks to nsec by doing the conversion in 2 stages
authorVincent Fu <Vincent.Fu@sandisk.com>
Tue, 11 Apr 2017 17:04:08 +0000 (13:04 -0400)
committerVincent Fu <vincent.fu@sandisk.com>
Wed, 21 Jun 2017 15:50:23 +0000 (11:50 -0400)
commit6d02b37b62ba43630bdf6ddebead5bf474daf40c
treeaa17197fb9de7266d19c472b010a687883818ebc
parente40e581c9c845d1c95804665bc5c0ab820327ef8
nanosecond: fix up conversion of ticks to nsec by doing the conversion in 2 stages

The ticks to nsec conversion is challenging because we care about small
differences between very large numbers. The solution used here is to do the
conversion in two stages.

The first stage carves out large chunks of time. For this patch each chunk is
approximately an hour long. The number of ticks per hour is calculated. When a
conversion is requested, an hour worth of time is accumulated each time this
number of ticks has elapsed.

The remaining ticks are handled in the second stage with the standard
multiplication and shift conversion.

This strategy deals with the multiplication-and-shift overflow problem by
limiting the scale of this operation to at most one hour. This way we can have
reasonable accuracy with this portion of the conversion. At the same time the
first stage allows the ticks to nsec conversion to be valid for long periods
of time.
gettime.c