Subject: Re: typeof (Re: CVS commit: src)
To: Alan Barrett <apb@cequrux.com>
From: David Young <dyoung@pobox.com>
List: tech-userlevel
Date: 09/02/2006 12:21:48
On Sat, Sep 02, 2006 at 01:48:54PM +0200, Alan Barrett wrote:
> Referring to this code added in revision 1.61 of sys/cdefs.h (slightly
> reformatted):
> 
> #define	__LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask))
> #define	__SHIFTOUT(__x, __mask)	\
> 	((typeof(__x))(((__x) & (__mask)) / __LOWEST_SET_BIT(__mask)))
> #define	__SHIFTIN(__x, __mask) \
> 	((typeof(__x))((__x) * __LOWEST_SET_BIT(__mask)))
> #define	__SHIFTOUT_MASK(__mask) __SHIFTOUT((__mask), (__mask))
> 
> On Sat, 02 Sep 2006, David Young wrote:
> > On Sat, Sep 02, 2006 at 12:09:19AM +0100, David Laight wrote:
> > > On Fri, Sep 01, 2006 at 09:01:06AM +0900, YAMAMOTO Takashi wrote:
> > > > is it ok to use typeof here?
> > > Almost certainly not since the header files gets built into tools.
> > Please suggest an alternative.
> 
> Why do you need to cast at all?  As far as I can see, the right thing
> would happen without any casts.  OK, if the inputs are narrower than
> int (e.g. unsigned char), and there's no cast, then the result will be
> (signed) int, but is that a problem?  What other bad effects am I missing?

__x may be narrower than __mask.  __mask is ordinarily a uintmax_t,
and a product or bitwise-AND involving a uintmax_t will yield a uintmax_t.

The problem is to make the macros widthless without requiring a user of
the macro to use a lot of casts.

Dave

-- 
David Young             OJC Technologies
dyoung@ojctech.com      Urbana, IL * (217) 278-3933