Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/next/external/cache/sources/wl/include/bcmendian.h
Views: 3959
/*1* Byte order utilities2*3* $Copyright Open Broadcom Corporation$4*5* $Id: bcmendian.h 370210 2012-11-21 05:35:27Z nisar $6*7* This file by default provides proper behavior on little-endian architectures.8* On big-endian architectures, IL_BIGENDIAN should be defined.9*/1011#ifndef _BCMENDIAN_H_12#define _BCMENDIAN_H_1314#include <typedefs.h>1516/* Reverse the bytes in a 16-bit value */17#define BCMSWAP16(val) \18((uint16)((((uint16)(val) & (uint16)0x00ffU) << 8) | \19(((uint16)(val) & (uint16)0xff00U) >> 8)))2021/* Reverse the bytes in a 32-bit value */22#define BCMSWAP32(val) \23((uint32)((((uint32)(val) & (uint32)0x000000ffU) << 24) | \24(((uint32)(val) & (uint32)0x0000ff00U) << 8) | \25(((uint32)(val) & (uint32)0x00ff0000U) >> 8) | \26(((uint32)(val) & (uint32)0xff000000U) >> 24)))2728/* Reverse the two 16-bit halves of a 32-bit value */29#define BCMSWAP32BY16(val) \30((uint32)((((uint32)(val) & (uint32)0x0000ffffU) << 16) | \31(((uint32)(val) & (uint32)0xffff0000U) >> 16)))3233/* Reverse the bytes in a 64-bit value */34#define BCMSWAP64(val) \35((uint64)((((uint64)(val) & 0x00000000000000ffULL) << 56) | \36(((uint64)(val) & 0x000000000000ff00ULL) << 40) | \37(((uint64)(val) & 0x0000000000ff0000ULL) << 24) | \38(((uint64)(val) & 0x00000000ff000000ULL) << 8) | \39(((uint64)(val) & 0x000000ff00000000ULL) >> 8) | \40(((uint64)(val) & 0x0000ff0000000000ULL) >> 24) | \41(((uint64)(val) & 0x00ff000000000000ULL) >> 40) | \42(((uint64)(val) & 0xff00000000000000ULL) >> 56)))4344/* Reverse the two 32-bit halves of a 64-bit value */45#define BCMSWAP64BY32(val) \46((uint64)((((uint64)(val) & 0x00000000ffffffffULL) << 32) | \47(((uint64)(val) & 0xffffffff00000000ULL) >> 32)))484950/* Byte swapping macros51* Host <=> Network (Big Endian) for 16- and 32-bit values52* Host <=> Little-Endian for 16- and 32-bit values53*/54#ifndef hton1655#ifndef IL_BIGENDIAN56#define HTON16(i) BCMSWAP16(i)57#define hton16(i) bcmswap16(i)58#define HTON32(i) BCMSWAP32(i)59#define hton32(i) bcmswap32(i)60#define NTOH16(i) BCMSWAP16(i)61#define ntoh16(i) bcmswap16(i)62#define NTOH32(i) BCMSWAP32(i)63#define ntoh32(i) bcmswap32(i)64#define LTOH16(i) (i)65#define ltoh16(i) (i)66#define LTOH32(i) (i)67#define ltoh32(i) (i)68#define HTOL16(i) (i)69#define htol16(i) (i)70#define HTOL32(i) (i)71#define htol32(i) (i)72#define HTOL64(i) (i)73#define htol64(i) (i)74#else /* IL_BIGENDIAN */75#define HTON16(i) (i)76#define hton16(i) (i)77#define HTON32(i) (i)78#define hton32(i) (i)79#define NTOH16(i) (i)80#define ntoh16(i) (i)81#define NTOH32(i) (i)82#define ntoh32(i) (i)83#define LTOH16(i) BCMSWAP16(i)84#define ltoh16(i) bcmswap16(i)85#define LTOH32(i) BCMSWAP32(i)86#define ltoh32(i) bcmswap32(i)87#define HTOL16(i) BCMSWAP16(i)88#define htol16(i) bcmswap16(i)89#define HTOL32(i) BCMSWAP32(i)90#define htol32(i) bcmswap32(i)91#define HTOL64(i) BCMSWAP64(i)92#define htol64(i) bcmswap64(i)93#endif /* IL_BIGENDIAN */94#endif /* hton16 */9596#ifndef IL_BIGENDIAN97#define ltoh16_buf(buf, i)98#define htol16_buf(buf, i)99#else100#define ltoh16_buf(buf, i) bcmswap16_buf((uint16 *)(buf), (i))101#define htol16_buf(buf, i) bcmswap16_buf((uint16 *)(buf), (i))102#endif /* IL_BIGENDIAN */103104/* Unaligned loads and stores in host byte order */105#ifndef IL_BIGENDIAN106#define load32_ua(a) ltoh32_ua(a)107#define store32_ua(a, v) htol32_ua_store(v, a)108#define load16_ua(a) ltoh16_ua(a)109#define store16_ua(a, v) htol16_ua_store(v, a)110#else111#define load32_ua(a) ntoh32_ua(a)112#define store32_ua(a, v) hton32_ua_store(v, a)113#define load16_ua(a) ntoh16_ua(a)114#define store16_ua(a, v) hton16_ua_store(v, a)115#endif /* IL_BIGENDIAN */116117#define _LTOH16_UA(cp) ((cp)[0] | ((cp)[1] << 8))118#define _LTOH32_UA(cp) ((cp)[0] | ((cp)[1] << 8) | ((cp)[2] << 16) | ((cp)[3] << 24))119#define _NTOH16_UA(cp) (((cp)[0] << 8) | (cp)[1])120#define _NTOH32_UA(cp) (((cp)[0] << 24) | ((cp)[1] << 16) | ((cp)[2] << 8) | (cp)[3])121122#define ltoh_ua(ptr) \123(sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)(ptr) : \124sizeof(*(ptr)) == sizeof(uint16) ? _LTOH16_UA((const uint8 *)(ptr)) : \125sizeof(*(ptr)) == sizeof(uint32) ? _LTOH32_UA((const uint8 *)(ptr)) : \126*(uint8 *)0)127128#define ntoh_ua(ptr) \129(sizeof(*(ptr)) == sizeof(uint8) ? *(const uint8 *)(ptr) : \130sizeof(*(ptr)) == sizeof(uint16) ? _NTOH16_UA((const uint8 *)(ptr)) : \131sizeof(*(ptr)) == sizeof(uint32) ? _NTOH32_UA((const uint8 *)(ptr)) : \132*(uint8 *)0)133134#ifdef __GNUC__135136/* GNU macro versions avoid referencing the argument multiple times, while also137* avoiding the -fno-inline used in ROM builds.138*/139140#define bcmswap16(val) ({ \141uint16 _val = (val); \142BCMSWAP16(_val); \143})144145#define bcmswap32(val) ({ \146uint32 _val = (val); \147BCMSWAP32(_val); \148})149150#define bcmswap64(val) ({ \151uint64 _val = (val); \152BCMSWAP64(_val); \153})154155#define bcmswap32by16(val) ({ \156uint32 _val = (val); \157BCMSWAP32BY16(_val); \158})159160#define bcmswap16_buf(buf, len) ({ \161uint16 *_buf = (uint16 *)(buf); \162uint _wds = (len) / 2; \163while (_wds--) { \164*_buf = bcmswap16(*_buf); \165_buf++; \166} \167})168169#define htol16_ua_store(val, bytes) ({ \170uint16 _val = (val); \171uint8 *_bytes = (uint8 *)(bytes); \172_bytes[0] = _val & 0xff; \173_bytes[1] = _val >> 8; \174})175176#define htol32_ua_store(val, bytes) ({ \177uint32 _val = (val); \178uint8 *_bytes = (uint8 *)(bytes); \179_bytes[0] = _val & 0xff; \180_bytes[1] = (_val >> 8) & 0xff; \181_bytes[2] = (_val >> 16) & 0xff; \182_bytes[3] = _val >> 24; \183})184185#define hton16_ua_store(val, bytes) ({ \186uint16 _val = (val); \187uint8 *_bytes = (uint8 *)(bytes); \188_bytes[0] = _val >> 8; \189_bytes[1] = _val & 0xff; \190})191192#define hton32_ua_store(val, bytes) ({ \193uint32 _val = (val); \194uint8 *_bytes = (uint8 *)(bytes); \195_bytes[0] = _val >> 24; \196_bytes[1] = (_val >> 16) & 0xff; \197_bytes[2] = (_val >> 8) & 0xff; \198_bytes[3] = _val & 0xff; \199})200201#define ltoh16_ua(bytes) ({ \202const uint8 *_bytes = (const uint8 *)(bytes); \203_LTOH16_UA(_bytes); \204})205206#define ltoh32_ua(bytes) ({ \207const uint8 *_bytes = (const uint8 *)(bytes); \208_LTOH32_UA(_bytes); \209})210211#define ntoh16_ua(bytes) ({ \212const uint8 *_bytes = (const uint8 *)(bytes); \213_NTOH16_UA(_bytes); \214})215216#define ntoh32_ua(bytes) ({ \217const uint8 *_bytes = (const uint8 *)(bytes); \218_NTOH32_UA(_bytes); \219})220221#else /* !__GNUC__ */222223/* Inline versions avoid referencing the argument multiple times */224static INLINE uint16225bcmswap16(uint16 val)226{227return BCMSWAP16(val);228}229230static INLINE uint32231bcmswap32(uint32 val)232{233return BCMSWAP32(val);234}235236static INLINE uint64237bcmswap64(uint64 val)238{239return BCMSWAP64(val);240}241242static INLINE uint32243bcmswap32by16(uint32 val)244{245return BCMSWAP32BY16(val);246}247248/* Reverse pairs of bytes in a buffer (not for high-performance use) */249/* buf - start of buffer of shorts to swap */250/* len - byte length of buffer */251static INLINE void252bcmswap16_buf(uint16 *buf, uint len)253{254len = len / 2;255256while (len--) {257*buf = bcmswap16(*buf);258buf++;259}260}261262/*263* Store 16-bit value to unaligned little-endian byte array.264*/265static INLINE void266htol16_ua_store(uint16 val, uint8 *bytes)267{268bytes[0] = val & 0xff;269bytes[1] = val >> 8;270}271272/*273* Store 32-bit value to unaligned little-endian byte array.274*/275static INLINE void276htol32_ua_store(uint32 val, uint8 *bytes)277{278bytes[0] = val & 0xff;279bytes[1] = (val >> 8) & 0xff;280bytes[2] = (val >> 16) & 0xff;281bytes[3] = val >> 24;282}283284/*285* Store 16-bit value to unaligned network-(big-)endian byte array.286*/287static INLINE void288hton16_ua_store(uint16 val, uint8 *bytes)289{290bytes[0] = val >> 8;291bytes[1] = val & 0xff;292}293294/*295* Store 32-bit value to unaligned network-(big-)endian byte array.296*/297static INLINE void298hton32_ua_store(uint32 val, uint8 *bytes)299{300bytes[0] = val >> 24;301bytes[1] = (val >> 16) & 0xff;302bytes[2] = (val >> 8) & 0xff;303bytes[3] = val & 0xff;304}305306/*307* Load 16-bit value from unaligned little-endian byte array.308*/309static INLINE uint16310ltoh16_ua(const void *bytes)311{312return _LTOH16_UA((const uint8 *)bytes);313}314315/*316* Load 32-bit value from unaligned little-endian byte array.317*/318static INLINE uint32319ltoh32_ua(const void *bytes)320{321return _LTOH32_UA((const uint8 *)bytes);322}323324/*325* Load 16-bit value from unaligned big-(network-)endian byte array.326*/327static INLINE uint16328ntoh16_ua(const void *bytes)329{330return _NTOH16_UA((const uint8 *)bytes);331}332333/*334* Load 32-bit value from unaligned big-(network-)endian byte array.335*/336static INLINE uint32337ntoh32_ua(const void *bytes)338{339return _NTOH32_UA((const uint8 *)bytes);340}341342#endif /* !__GNUC__ */343#endif /* !_BCMENDIAN_H_ */344345346