From owner-freebsd-hackers@FreeBSD.ORG Thu Oct 16 22:47:42 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 2715D1DF; Thu, 16 Oct 2014 22:47:42 +0000 (UTC) Received: from onelab2.iet.unipi.it (onelab2.iet.unipi.it [131.114.59.238]) by mx1.freebsd.org (Postfix) with ESMTP id DEE92C11; Thu, 16 Oct 2014 22:47:41 +0000 (UTC) Received: by onelab2.iet.unipi.it (Postfix, from userid 275) id 7F31A7300A; Fri, 17 Oct 2014 00:51:19 +0200 (CEST) Date: Fri, 17 Oct 2014 00:51:19 +0200 From: Luigi Rizzo To: Jeremie Le Hen Subject: Re: struct bintime Message-ID: <20141016225119.GD10204@onelab2.iet.unipi.it> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) 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:47:42 -0000 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"