c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * Copyright (c) 2016-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * Permission is hereby granted, free of charge, to any person obtaining a copy
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * of this software and associated documentation files (the "Software"), to deal
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * in the Software without restriction, including without limitation the rights
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * copies of the Software, and to permit persons to whom the Software is
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * furnished to do so, subject to the following conditions:
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * The above copyright notice and this permission notice shall be included in
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * all copies or substantial portions of the Software.
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * These prototypes exist to catch bugs in the code generating macros below.
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek/* return byte swapped input */
08259c1f206026ca9b9f4b4e97603943c6093defJosef 'Jeff' Sipekstatic inline uint64_t i_bswap_64(uint64_t in);
08259c1f206026ca9b9f4b4e97603943c6093defJosef 'Jeff' Sipekstatic inline uint32_t i_bswap_32(uint32_t in);
08259c1f206026ca9b9f4b4e97603943c6093defJosef 'Jeff' Sipekstatic inline uint16_t i_bswap_16(uint16_t in);
08259c1f206026ca9b9f4b4e97603943c6093defJosef 'Jeff' Sipekstatic inline uint8_t i_bswap_8(uint8_t in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek/* load an unaligned cpu native endian number from memory */
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint64_t cpu64_to_cpu_unaligned(const void *in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint32_t cpu32_to_cpu_unaligned(const void *in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint16_t cpu16_to_cpu_unaligned(const void *in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint8_t cpu8_to_cpu_unaligned(const void *in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek/* load an unaligned big endian number from memory */
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint64_t be64_to_cpu_unaligned(const void *in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint32_t be32_to_cpu_unaligned(const void *in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint16_t be16_to_cpu_unaligned(const void *in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint8_t be8_to_cpu_unaligned(const void *in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek/* load an unaligned little endian number from memory */
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint64_t le64_to_cpu_unaligned(const void *in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint32_t le32_to_cpu_unaligned(const void *in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint16_t le16_to_cpu_unaligned(const void *in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint8_t le8_to_cpu_unaligned(const void *in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek/* store into memory a cpu native endian number as a big endian number */
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline void cpu64_to_be_unaligned(uint64_t in, void *out);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline void cpu32_to_be_unaligned(uint32_t in, void *out);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline void cpu16_to_be_unaligned(uint16_t in, void *out);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline void cpu8_to_be_unaligned(uint8_t in, void *out);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek/* store into memory a cpu native endian number as a little endian number */
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline void cpu64_to_le_unaligned(uint64_t in, void *out);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline void cpu32_to_le_unaligned(uint32_t in, void *out);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline void cpu16_to_le_unaligned(uint16_t in, void *out);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline void cpu8_to_le_unaligned(uint8_t in, void *out);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek/* convert a big endian input into cpu native endian */
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint64_t be64_to_cpu(uint64_t in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint32_t be32_to_cpu(uint32_t in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint16_t be16_to_cpu(uint16_t in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint8_t be8_to_cpu(uint8_t in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek/* convert a cpu native endian input into big endian */
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint64_t cpu64_to_be(uint64_t in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint32_t cpu32_to_be(uint32_t in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint16_t cpu16_to_be(uint16_t in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint8_t cpu8_to_be(uint8_t in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek/* convert a little endian input into cpu native endian */
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint64_t le64_to_cpu(uint64_t in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint32_t le32_to_cpu(uint32_t in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint16_t le16_to_cpu(uint16_t in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint8_t le8_to_cpu(uint8_t in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek/* convert a cpu native endian input into little endian */
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint64_t cpu64_to_le(uint64_t in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint32_t cpu32_to_le(uint32_t in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint16_t cpu16_to_le(uint16_t in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint8_t cpu8_to_le(uint8_t in);
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * byte swapping
08259c1f206026ca9b9f4b4e97603943c6093defJosef 'Jeff' Sipekstatic inline uint64_t i_bswap_64(uint64_t in)
08259c1f206026ca9b9f4b4e97603943c6093defJosef 'Jeff' Sipekstatic inline uint32_t i_bswap_32(uint32_t in)
08259c1f206026ca9b9f4b4e97603943c6093defJosef 'Jeff' Sipekstatic inline uint16_t i_bswap_16(uint16_t in)
08259c1f206026ca9b9f4b4e97603943c6093defJosef 'Jeff' Sipekstatic inline uint8_t i_bswap_8(uint8_t in)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * unaligned big-endian integer
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint64_t be64_to_cpu_unaligned(const void *in)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline void cpu64_to_be_unaligned(uint64_t in, void *out)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint32_t be32_to_cpu_unaligned(const void *in)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline void cpu32_to_be_unaligned(uint32_t in, void *out)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint16_t be16_to_cpu_unaligned(const void *in)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline void cpu16_to_be_unaligned(uint16_t in, void *out)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint8_t be8_to_cpu_unaligned(const void *in)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline void cpu8_to_be_unaligned(uint8_t in, void *out)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * unaligned little-endian & cpu-endian integers
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint##size##_t le##size##_to_cpu_unaligned(const void *in)\
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek uint##size##_t x = be##size##_to_cpu_unaligned(in); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek /* we read a LE int as BE, so we always have to byte swap */ \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline void cpu##size##_to_le_unaligned(uint##size##_t in, \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek /* we'll be writing in BE, so we always have to byte swap */ \
08259c1f206026ca9b9f4b4e97603943c6093defJosef 'Jeff' Sipek cpu##size##_to_be_unaligned(i_bswap_##size(in), out); \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint##size##_t cpu##size##_to_cpu_unaligned(const void *in)\
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek uint##size##_t x = be##size##_to_cpu_unaligned(in); \
08259c1f206026ca9b9f4b4e97603943c6093defJosef 'Jeff' Sipek#define GEN(size) __GEN(size, i_bswap_##size(x))
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek * byte ordering
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipekstatic inline uint##size##_t from##size##_to_##to(uint##size##_t x) \
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#define __GEN(from, size, to, be, le) ___GEN(from, size, to, be)
c9d76e2baf653fa5504d6849e5c1aae32eff3c53Josef 'Jeff' Sipek#define __GEN(from, size, to, be, le) ___GEN(from, size, to, le)
08259c1f206026ca9b9f4b4e97603943c6093defJosef 'Jeff' Sipek __GEN(be, size, cpu, x, i_bswap_##size(x)) \
08259c1f206026ca9b9f4b4e97603943c6093defJosef 'Jeff' Sipek __GEN(cpu, size, be, x, i_bswap_##size(x)) \
08259c1f206026ca9b9f4b4e97603943c6093defJosef 'Jeff' Sipek __GEN(le, size, cpu, i_bswap_##size(x), x) \