Path: blob/main/tools/build/cross-build/include/mac/sys/endian.h
39587 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#include <machine/endian.h>3839#include <stdint.h>4041/*42* General byte order swapping functions.43*/44#define bswap16(x) __builtin_bswap16(x)45#define bswap32(x) __builtin_bswap32(x)46#define bswap64(x) __builtin_bswap64(x)4748#define _BYTE_ORDER __DARWIN_BYTE_ORDER49#define _LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN50#define _BIG_ENDIAN __DARWIN_BIG_ENDIAN51/*52* Host to big endian, host to little endian, big endian to host, and little53* endian to host byte order functions as detailed in byteorder(9).54*/55#if _BYTE_ORDER == _LITTLE_ENDIAN56#define htobe16(x) bswap16((x))57#define htobe32(x) bswap32((x))58#define htobe64(x) bswap64((x))59#define htole16(x) ((uint16_t)(x))60#define htole32(x) ((uint32_t)(x))61#define htole64(x) ((uint64_t)(x))6263#define be16toh(x) bswap16((x))64#define be32toh(x) bswap32((x))65#define be64toh(x) bswap64((x))66#define le16toh(x) ((uint16_t)(x))67#define le32toh(x) ((uint32_t)(x))68#define le64toh(x) ((uint64_t)(x))69#else /* _BYTE_ORDER != _LITTLE_ENDIAN */70#define htobe16(x) ((uint16_t)(x))71#define htobe32(x) ((uint32_t)(x))72#define htobe64(x) ((uint64_t)(x))73#define htole16(x) bswap16((x))74#define htole32(x) bswap32((x))75#define htole64(x) bswap64((x))7677#define be16toh(x) ((uint16_t)(x))78#define be32toh(x) ((uint32_t)(x))79#define be64toh(x) ((uint64_t)(x))80#define le16toh(x) bswap16((x))81#define le32toh(x) bswap32((x))82#define le64toh(x) bswap64((x))83#endif /* _BYTE_ORDER == _LITTLE_ENDIAN */8485/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */8687static __inline uint16_t88be16dec(const void *pp)89{90uint8_t const *p = (uint8_t const *)pp;9192return ((p[0] << 8) | p[1]);93}9495static __inline uint32_t96be32dec(const void *pp)97{98uint8_t const *p = (uint8_t const *)pp;99100return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);101}102103static __inline uint64_t104be64dec(const void *pp)105{106uint8_t const *p = (uint8_t const *)pp;107108return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));109}110111static __inline uint16_t112le16dec(const void *pp)113{114uint8_t const *p = (uint8_t const *)pp;115116return ((p[1] << 8) | p[0]);117}118119static __inline uint32_t120le32dec(const void *pp)121{122uint8_t const *p = (uint8_t const *)pp;123124return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);125}126127static __inline uint64_t128le64dec(const void *pp)129{130uint8_t const *p = (uint8_t const *)pp;131132return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));133}134135static __inline void136be16enc(void *pp, uint16_t u)137{138uint8_t *p = (uint8_t *)pp;139140p[0] = (u >> 8) & 0xff;141p[1] = u & 0xff;142}143144static __inline void145be32enc(void *pp, uint32_t u)146{147uint8_t *p = (uint8_t *)pp;148149p[0] = (u >> 24) & 0xff;150p[1] = (u >> 16) & 0xff;151p[2] = (u >> 8) & 0xff;152p[3] = u & 0xff;153}154155static __inline void156be64enc(void *pp, uint64_t u)157{158uint8_t *p = (uint8_t *)pp;159160be32enc(p, (uint32_t)(u >> 32));161be32enc(p + 4, (uint32_t)(u & 0xffffffffU));162}163164static __inline void165le16enc(void *pp, uint16_t u)166{167uint8_t *p = (uint8_t *)pp;168169p[0] = u & 0xff;170p[1] = (u >> 8) & 0xff;171}172173static __inline void174le32enc(void *pp, uint32_t u)175{176uint8_t *p = (uint8_t *)pp;177178p[0] = u & 0xff;179p[1] = (u >> 8) & 0xff;180p[2] = (u >> 16) & 0xff;181p[3] = (u >> 24) & 0xff;182}183184static __inline void185le64enc(void *pp, uint64_t u)186{187uint8_t *p = (uint8_t *)pp;188189le32enc(p, (uint32_t)(u & 0xffffffffU));190le32enc(p + 4, (uint32_t)(u >> 32));191}192193194