tech-misc archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Endian-specific types



> On Apr 11, 2016, at 3:46 PM, Mouse <mouse%Rodents-Montreal.ORG@localhost> wrote:
> 
>>> It uses __le16, what should I convert that to in NetBSD?
>> What does it do?
> 
> I _think_ __le16 is just uint16 (or maybe int16) but with an additional
> annotation for humans and (potentially?) automated codewalkers that the
> value is thought of as little-endian.
> 
> I have never really understood their use of it.  IMO values in
> integer-typed variables should not even conceptually have endiannesses;
> endianness is a property of not the variable or its value but rather of
> a serialization of that value.  

True, if you're dealing with data serialization, and if this is merely an annotation.

But possibly it's a data type with actually different semantics, as in "this value is a 16 bit integer, represented in memory as a 2 byte little endian item".  If so, it's definitely useful for some situations.

This is useful for device registers, which (depending on the sanity of the hardware designer) may have a specified byte order.  If so, using types like this either for pointers to CSRs, or as fields in structs which map the layout of registers in I/O space, is very helpful.  It isolates the byte order issue nicely (especially in the struct case) and avoids bugs caused by forgetting a byte swap operation in one out of 100 CSR references.

I used this many years ago in drivers written in C++ (where you can easily do this with classes and type conversion methods).  It made the code a lot cleaner and more reliable.

	paul



Home | Main Index | Thread Index | Old Index