tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Typo(s) in sys/time.h ?



On Thu, 5 Jan 2017, Patrick Welche wrote:

On Thu, Jan 05, 2017 at 12:05:35PM +0800, Paul Goyette wrote:
On Thu, 5 Jan 2017, Paul Goyette wrote:

Currently, we have

static __inline struct bintime
ms2bintime(uint64_t ms)
{
       struct bintime bt;

       bt.sec = (time_t)(ms / 1000U);
       bt.frac = (((ms % 1000U) >> 32)/1000U) >> 32;

       return bt;
}

As far as I can tell, this will result in bt.frac always set to zero,
since the (ms % 1000U) portion is a number less than 1000, and if you
shift a number less than 1000 to the right by 32 bits, you end up with
zero!

I suspect we should have a left shift here, rather than a right shift:

       bt.frac = (((ms % 1000U) << 32)/1000U) >> 32;

---------------------------------^^           ^^
                                                ^^
also here --------------------------------------^^

Why also here? (Guessing shift left to help division, then shift back again?)

You're shifting left a total of 64 bits in order to normalize the 64-bit fraction. Remember, for bintime, this is a (binary) fraction..

Do the math...

start with 500 ms

	( ms % 1000U ) --> 500(decimal) == 0x1f4

	0x1f4 << 32 --> 0x1f400000000

	Now divide that by 0x3e8 (ie, 1000(decimal)) --> 0x80000000

	Shift left again to get the fraction 0x8000000000000000 which
	is 0.5 in decimal!



+------------------+--------------------------+------------------------+
| Paul Goyette     | PGP Key fingerprint:     | E-mail addresses:      |
| (Retired)        | FA29 0E3B 35AF E8AE 6651 | paul at whooppee.com   |
| Kernel Developer | 0786 F758 55DE 53BA 7731 | pgoyette at netbsd.org |
+------------------+--------------------------+------------------------+


Home | Main Index | Thread Index | Old Index