struct bintime
Dimitry Andric
dim at FreeBSD.org
Thu Oct 16 22:48:37 UTC 2014
On 17 Oct 2014, at 00:15, Jeremie Le Hen <jlh at FreeBSD.org> wrote:
> I need to get microseconds from a struct bintime. I found
> bintime2timeval() in sys/time.h which more or less does this, but I
> don't understand how the computation works.
>
> Can someone explain it to me please?
>
> static __inline void
> bintime2timeval(const struct bintime *_bt, struct timeval *_tv)
> {
>
> _tv->tv_sec = _bt->sec;
> _tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(_bt->frac >> 32)) >> 32;
> }
Maybe it's easier to grok if you read it as:
usec = (10^6 * (frac / 2^32)) / 2^32
or even:
usec = 10^6 * (frac / 2^64)
and the other way around:
frac = (usec / 10^6) * 2^64
E.g., bintime divides a second into 2^64 slices, instead of 10^6 or
10^9. And the division by 2^64 in bintime2timeval() is split up in two
shifts, to avoid losing precision.
-Dimitry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 203 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20141017/42300fe4/attachment.sig>
More information about the freebsd-hackers
mailing list