From owner-freebsd-hackers@FreeBSD.ORG Thu Oct 16 22:48:37 2014 Return-Path: Delivered-To: freebsd-hackers@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id A25132C4; Thu, 16 Oct 2014 22:48:37 +0000 (UTC) Received: from tensor.andric.com (unknown [IPv6:2001:7b8:3a7:1:2d0:b7ff:fea0:8c26]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (Client CN "tensor.andric.com", Issuer "CAcert Class 3 Root" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 5FDEBC1E; Thu, 16 Oct 2014 22:48:37 +0000 (UTC) Received: from [IPv6:2001:7b8:3a7::35d1:185d:a69:50df] (unknown [IPv6:2001:7b8:3a7:0:35d1:185d:a69:50df]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) by tensor.andric.com (Postfix) with ESMTPSA id 7C737B80A; Fri, 17 Oct 2014 00:48:33 +0200 (CEST) Content-Type: multipart/signed; boundary="Apple-Mail=_4BFEECBE-F3E4-4609-9EA6-CAD13806F845"; protocol="application/pgp-signature"; micalg=pgp-sha1 Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) Subject: Re: struct bintime From: Dimitry Andric In-Reply-To: Date: Fri, 17 Oct 2014 00:48:14 +0200 Message-Id: <353C79A7-1D1D-48CF-8B42-7BA369D22321@FreeBSD.org> References: To: Jeremie Le Hen X-Mailer: Apple Mail (2.1878.6) Cc: freebsd-hackers@FreeBSD.org X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 16 Oct 2014 22:48:37 -0000 --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 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--