byteorder.h revision 0fbce082b59df2738e0c893f5251cb0116e1835b
/*
* Copyright (c) 2016-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef BYTEORDER_H
#define BYTEORDER_H
/*
* These prototypes exist to catch bugs in the code generating macros below.
*/
/* return byte swapped input */
/* load an unaligned cpu native endian number from memory */
/* load an unaligned big endian number from memory */
/* load an unaligned little endian number from memory */
/* store into memory a cpu native endian number as a big endian number */
/* store into memory a cpu native endian number as a little endian number */
/* convert a big endian input into cpu native endian */
/* convert a cpu native endian input into big endian */
/* convert a little endian input into cpu native endian */
/* convert a cpu native endian input into little endian */
/*
* byte swapping
*/
{
}
{
}
{
}
{
return (in & 0xff);
}
/*
* unaligned big-endian integer
*/
{
return (((uint64_t) p[0] << 56) |
((uint64_t) p[7]));
}
{
}
{
return (((uint32_t) p[0] << 24) |
((uint32_t) p[3]));
}
{
}
{
return (((uint16_t) p[0] << 8) |
((uint16_t) p[1]));
}
{
}
{
}
{
*p = in;
}
/*
* unaligned little-endian & cpu-endian integers
*/
{ \
/* we read a LE int as BE, so we always have to byte swap */ \
} \
void *out) \
{ \
/* we'll be writing in BE, so we always have to byte swap */ \
} \
{ \
return bswap; \
}
#if WORDS_BIGENDIAN
#else
#endif
GEN(64)
GEN(32)
GEN(16)
GEN(8)
/*
* byte ordering
*/
{ \
return bswap; \
}
#if WORDS_BIGENDIAN
#else
#endif
GEN(64)
GEN(32)
GEN(16)
GEN(8)
#endif