unsigned os_t

Tony van der Hoff tony at mk-net.demon.co.uk
Fri Jul 20 10:53:07 BST 2001


On 19 Jul 2001, Alexander Thoukydides <alex at thouky.co.uk> wrote:

> 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.
> 
You're quite right. I'd made a rather fundamental error in my logic.

> > 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.

No, I won't change it. Thanks for putting me right.

-- 
Tony van der Hoff         | MailTo:tony at mk-net.demon.co.uk
                          | MailTo:avanderhoff at iee.org
Buckinghamshire, England  | http:www.mk-net.demon.co.uk



More information about the oslib-user mailing list