[llvm-commits] [PATCH][Support] Add Endian.h which contains generic code to deal with endian specific data.

Michael Spencer bigcheesegs at gmail.com
Tue Sep 14 14:53:26 PDT 2010


The attached patch adds Endian.h to Support. This patch is a
prerequisite for the object file library I am writing.

Endian.h contains:

* SwapByteOrderIfDifferent
This is a templated function that takes a value_type, and returns the
value if host and target are the same, or returns the value with its
endianness swapped if they are different.
(host and target are int's because MSVC doesn't support enums used
like this. see:
http://stackoverflow.com/questions/2763836/sfinae-failing-with-enum-template-parameter)

* endian
This is a templated struct for reading endian specific data directly
from memory. It is typedefed based on the host platform's endianness
(well, not yet, because there's no LLVM_IS_BIG_ENDIAN or such yet).

* packed_endian_specific_integral
This is the most important thing included in this patch. This class
allows the portable creation of packed structs of any integral integer
value_type and endianness. Any struct that would be POD if it used
normal integral data types is still POD if it uses these.

This is used extensively in the object file library to directly access
memory mapped object files. In the case when host and target
endianness match (and the compiler inlines), using these should
produce identical code to "*reinterpret_cast<value_type*>(memory)".

- Michael Spencer
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0003-Support-Add-Endian.h.patch
Type: application/octet-stream
Size: 5855 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20100914/5421b98d/attachment.obj>


More information about the llvm-commits mailing list