This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Help with bit-field semantics in C and C++


> -----Original Message-----
> From: Mark Mitchell [mailto:mark@codesourcery.com] 
> Sent: 24 August 2004 19:21

>>What's unspecified is what happens when you convert 255 to E. 

> >  Can't we determine by analogy to what happens when you 
> cast 65535 to
> >short?  IOW, isn't this just the same as any other cast of 
> an out-of-range
> >value to a smaller size?
> >  
> >
> Yes, from the point of view of the standard.
> 
> But, the difference is that short tends to be two whole bytes, and if 
> you always look at two bytes when dealing with a short, you 
> never get a 
> value that isn't in the domain of short.  Roger's point is that there 
> are values that fit in the number of bits allcoated to the 
> enum that are 
> not valid values in the underlying type, which creates some 
> additional 
> complexity.

  But 65535 is also a value that fits into the size of a short, but is
outside the valid range; that's why I mentioned the old
signedness-preserving vs. value-preserving dichotomy.

> Introducing mask operations would greatly penalize the well-specified 
> case where the only values you ever assign to an enum are 
> ones that are within its range.

  Agreed.  It should maybe only happen at -O0, or perhaps should be under
control of a -f option.

  Actually, on second thoughts, the compiler could be made to only apply the
mask operation when it knows that a potentially-unsafe (because
size-reducing) cast is being applied.  Would that cover it?

    cheers, 
      DaveK
-- 
Can't think of a witty .sigline today....


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]