Path: blob/main/tools/build/cross-build/include/linux/sys/endian.h
39586 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright 2018-2020 Alex Richardson <[email protected]>4*5* This software was developed by SRI International and the University of6* Cambridge Computer Laboratory (Department of Computer Science and7* Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the8* DARPA SSITH research programme.9*10* This software was developed by SRI International and the University of11* Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)12* ("CTSRD"), as part of the DARPA CRASH research programme.13*14* Redistribution and use in source and binary forms, with or without15* modification, are permitted provided that the following conditions16* are met:17* 1. Redistributions of source code must retain the above copyright18* notice, this list of conditions and the following disclaimer.19* 2. Redistributions in binary form must reproduce the above copyright20* notice, this list of conditions and the following disclaimer in the21* documentation and/or other materials provided with the distribution.22*23* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND24* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE25* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE26* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE27* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL28* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS29* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)30* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT31* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY32* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF33* SUCH DAMAGE.34*/35#pragma once3637#if __has_include_next(<sys/endian.h>)38#include_next <sys/endian.h>39#endif4041#if __has_include(<endian.h>)42#include <endian.h>43#endif4445/* Linux uses a double underscore, FreeBSD a single one */46#define _LITTLE_ENDIAN __LITTLE_ENDIAN47#define _BIG_ENDIAN __BIG_ENDIAN48#define _BYTE_ORDER __BYTE_ORDER4950/*51* Ensure all these are constant expressions (which is not the case for some52* of the glibc versions depending on compiler optimization level)53*/5455#undef bswap6456#define bswap64(a) __builtin_bswap64(a)5758#undef bswap3259#define bswap32(a) __builtin_bswap32(a)6061#undef bswap1662#define bswap16(a) __builtin_bswap16(a)6364#undef __bswap_6465#define __bswap_64(a) __builtin_bswap64(a)6667#undef __bswap_3268#define __bswap_32(a) __builtin_bswap32(a)6970#undef __bswap_1671#define __bswap_16(a) __builtin_bswap16(a)7273/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */7475static __inline uint16_t76be16dec(const void *pp)77{78uint8_t const *p = (uint8_t const *)pp;7980return ((p[0] << 8) | p[1]);81}8283static __inline uint32_t84be32dec(const void *pp)85{86uint8_t const *p = (uint8_t const *)pp;8788return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);89}9091static __inline uint64_t92be64dec(const void *pp)93{94uint8_t const *p = (uint8_t const *)pp;9596return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));97}9899static __inline uint16_t100le16dec(const void *pp)101{102uint8_t const *p = (uint8_t const *)pp;103104return ((p[1] << 8) | p[0]);105}106107static __inline uint32_t108le32dec(const void *pp)109{110uint8_t const *p = (uint8_t const *)pp;111112return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);113}114115static __inline uint64_t116le64dec(const void *pp)117{118uint8_t const *p = (uint8_t const *)pp;119120return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));121}122123static __inline void124be16enc(void *pp, uint16_t u)125{126uint8_t *p = (uint8_t *)pp;127128p[0] = (u >> 8) & 0xff;129p[1] = u & 0xff;130}131132static __inline void133be32enc(void *pp, uint32_t u)134{135uint8_t *p = (uint8_t *)pp;136137p[0] = (u >> 24) & 0xff;138p[1] = (u >> 16) & 0xff;139p[2] = (u >> 8) & 0xff;140p[3] = u & 0xff;141}142143static __inline void144be64enc(void *pp, uint64_t u)145{146uint8_t *p = (uint8_t *)pp;147148be32enc(p, (uint32_t)(u >> 32));149be32enc(p + 4, (uint32_t)(u & 0xffffffffU));150}151152static __inline void153le16enc(void *pp, uint16_t u)154{155uint8_t *p = (uint8_t *)pp;156157p[0] = u & 0xff;158p[1] = (u >> 8) & 0xff;159}160161static __inline void162le32enc(void *pp, uint32_t u)163{164uint8_t *p = (uint8_t *)pp;165166p[0] = u & 0xff;167p[1] = (u >> 8) & 0xff;168p[2] = (u >> 16) & 0xff;169p[3] = (u >> 24) & 0xff;170}171172static __inline void173le64enc(void *pp, uint64_t u)174{175uint8_t *p = (uint8_t *)pp;176177le32enc(p, (uint32_t)(u & 0xffffffffU));178le32enc(p + 4, (uint32_t)(u >> 32));179}180181182