Date: Fri, 17 Oct 2014 00:48:14 +0200 From: Dimitry Andric <dim@FreeBSD.org> To: Jeremie Le Hen <jlh@FreeBSD.org> Cc: freebsd-hackers@FreeBSD.org Subject: Re: struct bintime Message-ID: <353C79A7-1D1D-48CF-8B42-7BA369D22321@FreeBSD.org> 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
--Apple-Mail=_4BFEECBE-F3E4-4609-9EA6-CAD13806F845 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii On 17 Oct 2014, at 00:15, Jeremie Le Hen <jlh@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. >=20 > Can someone explain it to me please? >=20 > static __inline void > bintime2timeval(const struct bintime *_bt, struct timeval *_tv) > { >=20 > _tv->tv_sec =3D _bt->sec; > _tv->tv_usec =3D ((uint64_t)1000000 * (uint32_t)(_bt->frac >> = 32)) >> 32; > } Maybe it's easier to grok if you read it as: usec =3D (10^6 * (frac / 2^32)) / 2^32 or even: usec =3D 10^6 * (frac / 2^64) and the other way around: frac =3D (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 --Apple-Mail=_4BFEECBE-F3E4-4609-9EA6-CAD13806F845 Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=signature.asc Content-Type: application/pgp-signature; name=signature.asc Content-Description: Message signed with OpenPGP using GPGMail -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.22 (Darwin) iEYEARECAAYFAlRASzsACgkQsF6jCi4glqNb9QCg5prZlWvI1JyvzhqdNJC0h2gM B1kAniyO5+eJGWXqyuj2AoBDhljA3Gy8 =RE2w -----END PGP SIGNATURE----- --Apple-Mail=_4BFEECBE-F3E4-4609-9EA6-CAD13806F845--
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?353C79A7-1D1D-48CF-8B42-7BA369D22321>