struct bintime

Luigi Rizzo rizzo at iet.unipi.it
Thu Oct 16 22:47:42 UTC 2014


On Fri, Oct 17, 2014 at 12:15:57AM +0200, Jeremie Le Hen wrote:
> Hi,
> 
> 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;
> }

bt->frac has 64 bits representing the fractional part of the second,
call it f, with 0 <= f < 1

(uint32_t)(_bt->frac >> 32)  is equivalent to \floor{f * 2^32}
you then multiply by 10^6 and do an integer division by 2^32 so
the overall expression is

tv_usec = \floor{ ( 10^6 * \floor{f * 2^32} ) / 2^32 } = \floor{ 10^6*f }

i.e. the number of microseconds.

cheers
luigi

> Thanks!
> -- 
> Jeremie Le Hen
> jlh at FreeBSD.org
> _______________________________________________
> freebsd-hackers at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe at freebsd.org"


More information about the freebsd-hackers mailing list