Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 21 Oct 2002 11:24:22 +1000 (EST)
From:      Bruce Evans <bde@zeta.org.au>
To:        Garrett Wollman <wollman@lcs.mit.edu>
Cc:        "M. Warner Losh" <imp@bsdimp.com>, <cvs-committers@FreeBSD.org>, <cvs-all@FreeBSD.org>
Subject:   Re: [src] cvs commit: src/sys/i386/pci pci_bus.c 
Message-ID:  <20021021110621.J8959-100000@gamplex.bde.org>
In-Reply-To: <200210202019.g9KKJ2xU054169@khavrinen.lcs.mit.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, 20 Oct 2002, Garrett Wollman wrote:

> <<On Sun, 20 Oct 2002 12:30:17 -0600 (MDT), "M. Warner Losh" <imp@bsdimp.com> said:
>
> > We'd need it for each width.  The PCI standard defines that no
> > hardware decoding the address is to return 0xff for each of the bytes
> > read.
>
> The correct way to write that would be ~(appropriate_width_unsigned_type)0.

Er, no.  E.g., ~(uint8_t)0 is no different from ~0, since the operand of
`~' suffers (sic) the default promotions before the operand is applied,
and Standard C's "value-preserving" promotions are especially broken for
(uint8_t)0.

A less incorrect way to write this is

    (appropriate_width_unsigned_type)~(appropriate_width_unsigned_type)0.

This works on normal 2's complement machines but fails on normal 1's
complement machines.  E.g., when the unsigned type is uint8_t, the
intermediate result is ~0 again.  ~0 is -1 on normal 2's complement
machines and -0 on normal 1's complement machines.  Casting this to
uint8_t gives 0xff on normal 2's complement machines and (I think) 0
on normal 1's complement machines.  On abnormal machines (even 2's
complement ones), you also have to worry about trap representations.
~0 probably sets trap bits if there are any, so it can probably cause
a trap either when it set or when it is used.

A correct way to write this is closer to the original code:

    (appropriate_width_unsigned_type)-1

The result of this is clearly specified in Standard C (C90 6.2.1.2) and is
independent of the type of arithmetic (1's complement, etc.).

Bruce


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe cvs-all" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20021021110621.J8959-100000>