unsigned os_t
Alexander Thoukydides
alex at thouky.co.uk
Thu Jul 19 17:10:04 BST 2001
On 19 Jul, Tony van der Hoff <tony at mk-net.demon.co.uk> wrote:
> I believe os_t, the monotonic time, should be unsigned, not a signed int as
> at present.
>
> OSLib practice is that anything on which arithmetic can be performed should
> be signed, but I think that in this case the arithmetic is generally to
> compare it with a previous time, and this is made a lot harder by it being
> signed. I always seem to convert it to unsigned anyway.
On the contrary, having it signed makes such comparisons easier. Remember,
you have to cope with the time wrapping around... assuming that something
else doesn't crash in the intervening sixteen months.
An explicit cast to a signed value would otherwise be required for code such
as the following (based on the BASIC sample on PRM 3-185):
os_t oldtime;
/* ... */
os_t newtime = os_read_monotonic_time();
while ((newtime - oldtime) > 0)
oldtime = oldtime + 100;
With os_t as an unsigned value the comparison would always be greater than or
equal to zero.
> If I change it, it will be binary compatible with old code, although anyone
> re-linking would have to recompile.
Unfortunately, that recompilation would change the behaviour of code such as
that shown above. Comparisons such as that are common in my code, and I
expect they are in other people's code also.
--
-- Alexander Thoukydides Email: mailto:alex at thouky.co.uk
-- Cambridge, England WWW: http://www.thouky.co.uk/
-- Any sufficiently advanced bug is indistinguishable from a feature.
More information about the oslib-user
mailing list