Subject: Re: typeof (Re: CVS commit: src)
To: None <tech-kern@NetBSD.org, tech-userlevel@NetBSD.org>
From: Alan Barrett <apb@cequrux.com>
List: tech-userlevel
Date: 09/03/2006 17:43:11
On Sat, 02 Sep 2006, David Young wrote:
> On Sat, Sep 02, 2006 at 01:48:54PM +0200, Alan Barrett wrote:
> > #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))
> > 
> > Why do you need to cast at all?
>
> __x may be narrower than __mask.  __mask is ordinarily a uintmax_t,

Ah, now I see: __SHIFTIN(x,0xF00) is fine, because nobody would use a
literal mask that's wider than x, but __SHIFTIN(x,__BITS(12,9)) is bad,
because __BITS yields uintmax_t.

I suppose you have to use Joerg's suggestion of testing sizeof(__x).

--apb (Alan Barrett)