Annotation of embedaddon/iperf/src/portable_endian.h, revision 1.1.1.1

1.1       misho       1: // "License": Public Domain
                      2: // I, Mathias Panzenböck, place this file hereby into the public domain. Use it at your own risk for whatever you like.
                      3: 
                      4: #ifndef PORTABLE_ENDIAN_H__
                      5: #define PORTABLE_ENDIAN_H__
                      6: 
                      7: #if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
                      8: 
                      9: #      define __WINDOWS__
                     10: 
                     11: #endif
                     12: 
                     13: // GLIBC / Linux with endian(3) support, which was added in glibc 2.9.
                     14: // Intended to support CentOS 6 and newer.
                     15: #if defined(__linux__) && \
                     16:     ((__GLIBC__ > 3) || \
                     17:      (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 9))
                     18: 
                     19: #      include <endian.h>
                     20: 
                     21: #elif defined(__CYGWIN__)
                     22: 
                     23: #      include <endian.h>
                     24: 
                     25: #elif defined(__APPLE__)
                     26: 
                     27: #      include <libkern/OSByteOrder.h>
                     28: 
                     29: #      define htobe16(x) OSSwapHostToBigInt16(x)
                     30: #      define htole16(x) OSSwapHostToLittleInt16(x)
                     31: #      define be16toh(x) OSSwapBigToHostInt16(x)
                     32: #      define le16toh(x) OSSwapLittleToHostInt16(x)
                     33:  
                     34: #      define htobe32(x) OSSwapHostToBigInt32(x)
                     35: #      define htole32(x) OSSwapHostToLittleInt32(x)
                     36: #      define be32toh(x) OSSwapBigToHostInt32(x)
                     37: #      define le32toh(x) OSSwapLittleToHostInt32(x)
                     38:  
                     39: #      define htobe64(x) OSSwapHostToBigInt64(x)
                     40: #      define htole64(x) OSSwapHostToLittleInt64(x)
                     41: #      define be64toh(x) OSSwapBigToHostInt64(x)
                     42: #      define le64toh(x) OSSwapLittleToHostInt64(x)
                     43: 
                     44: #      define __BYTE_ORDER    BYTE_ORDER
                     45: #      define __BIG_ENDIAN    BIG_ENDIAN
                     46: #      define __LITTLE_ENDIAN LITTLE_ENDIAN
                     47: #      define __PDP_ENDIAN    PDP_ENDIAN
                     48: 
                     49: #elif defined(__OpenBSD__)
                     50: 
                     51: #      include <sys/endian.h>
                     52: 
                     53: #      define be16toh(x) betoh16(x)
                     54: #      define le16toh(x) letoh16(x)
                     55: 
                     56: #      define be32toh(x) betoh32(x)
                     57: #      define le32toh(x) letoh32(x)
                     58: 
                     59: #      define be64toh(x) betoh64(x)
                     60: #      define le64toh(x) letoh64(x)
                     61: 
                     62: #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
                     63: 
                     64: #      include <sys/endian.h>
                     65: 
                     66: #elif defined(__sun) && defined(__SVR4)
                     67: 
                     68: #      include <sys/types.h>
                     69: #      include <netinet/in.h>
                     70: #      include <inttypes.h>
                     71: 
                     72: #      define be64toh(x) ntohll(x)
                     73: #      define htobe64(x) htonll(x)
                     74: 
                     75: #elif defined(__WINDOWS__)
                     76: 
                     77: #      include <winsock2.h>
                     78: #      include <sys/param.h>
                     79: 
                     80: #      if BYTE_ORDER == LITTLE_ENDIAN
                     81: 
                     82: #              define htobe16(x) htons(x)
                     83: #              define htole16(x) (x)
                     84: #              define be16toh(x) ntohs(x)
                     85: #              define le16toh(x) (x)
                     86:  
                     87: #              define htobe32(x) htonl(x)
                     88: #              define htole32(x) (x)
                     89: #              define be32toh(x) ntohl(x)
                     90: #              define le32toh(x) (x)
                     91:  
                     92: #              define htobe64(x) htonll(x)
                     93: #              define htole64(x) (x)
                     94: #              define be64toh(x) ntohll(x)
                     95: #              define le64toh(x) (x)
                     96: 
                     97: #      elif BYTE_ORDER == BIG_ENDIAN
                     98: 
                     99:                /* that would be xbox 360 */
                    100: #              define htobe16(x) (x)
                    101: #              define htole16(x) __builtin_bswap16(x)
                    102: #              define be16toh(x) (x)
                    103: #              define le16toh(x) __builtin_bswap16(x)
                    104:  
                    105: #              define htobe32(x) (x)
                    106: #              define htole32(x) __builtin_bswap32(x)
                    107: #              define be32toh(x) (x)
                    108: #              define le32toh(x) __builtin_bswap32(x)
                    109:  
                    110: #              define htobe64(x) (x)
                    111: #              define htole64(x) __builtin_bswap64(x)
                    112: #              define be64toh(x) (x)
                    113: #              define le64toh(x) __builtin_bswap64(x)
                    114: 
                    115: #      else
                    116: 
                    117: #              error byte order not supported
                    118: 
                    119: #      endif
                    120: 
                    121: #      define __BYTE_ORDER    BYTE_ORDER
                    122: #      define __BIG_ENDIAN    BIG_ENDIAN
                    123: #      define __LITTLE_ENDIAN LITTLE_ENDIAN
                    124: #      define __PDP_ENDIAN    PDP_ENDIAN
                    125: 
                    126: #else
                    127: 
                    128: // Unsupported platforms.
                    129: // Intended to support CentOS 5 but hopefully not too far from
                    130: // the truth because we use the homebrew htonll, et al. implementations
                    131: // that were originally the sole implementation of this functionality
                    132: // in iperf 3.0.
                    133: #      warning platform not supported
                    134: #      include <endian.h>
                    135: #if BYTE_ORDER == BIG_ENDIAN
                    136: #define HTONLL(n) (n)
                    137: #define NTOHLL(n) (n)
                    138: #else
                    139: #define HTONLL(n) ((((unsigned long long)(n) & 0xFF) << 56) | \
                    140:                    (((unsigned long long)(n) & 0xFF00) << 40) | \
                    141:                    (((unsigned long long)(n) & 0xFF0000) << 24) | \
                    142:                    (((unsigned long long)(n) & 0xFF000000) << 8) | \
                    143:                    (((unsigned long long)(n) & 0xFF00000000) >> 8) | \
                    144:                    (((unsigned long long)(n) & 0xFF0000000000) >> 24) | \
                    145:                    (((unsigned long long)(n) & 0xFF000000000000) >> 40) | \
                    146:                    (((unsigned long long)(n) & 0xFF00000000000000) >> 56))
                    147: 
                    148: #define NTOHLL(n) ((((unsigned long long)(n) & 0xFF) << 56) | \
                    149:                    (((unsigned long long)(n) & 0xFF00) << 40) | \
                    150:                    (((unsigned long long)(n) & 0xFF0000) << 24) | \
                    151:                    (((unsigned long long)(n) & 0xFF000000) << 8) | \
                    152:                    (((unsigned long long)(n) & 0xFF00000000) >> 8) | \
                    153:                    (((unsigned long long)(n) & 0xFF0000000000) >> 24) | \
                    154:                    (((unsigned long long)(n) & 0xFF000000000000) >> 40) | \
                    155:                    (((unsigned long long)(n) & 0xFF00000000000000) >> 56))
                    156: #endif
                    157: 
                    158: #define htobe64(n) HTONLL(n)
                    159: #define be64toh(n) NTOHLL(n)
                    160: 
                    161: #endif
                    162: 
                    163: #endif
                    164: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>