Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 17 Oct 2014 00:51:19 +0200
From:      Luigi Rizzo <rizzo@iet.unipi.it>
To:        Jeremie Le Hen <jlh@freebsd.org>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: struct bintime
Message-ID:  <20141016225119.GD10204@onelab2.iet.unipi.it>
In-Reply-To: <CAGSa5y2voSswSV4XFVR8%2BOqdftsfPWNSHYiptp-BMJ_hSp5u2A@mail.gmail.com>
References:  <CAGSa5y2voSswSV4XFVR8%2BOqdftsfPWNSHYiptp-BMJ_hSp5u2A@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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@FreeBSD.org
> _______________________________________________
> freebsd-hackers@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20141016225119.GD10204>