1/* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#ifndef ANDROID_BASE_ENDIAN_H 18#define ANDROID_BASE_ENDIAN_H 19 20/* A cross-platform equivalent of bionic's <sys/endian.h>. */ 21 22#if defined(__BIONIC__) 23 24#include <sys/endian.h> 25 26#elif defined(__GLIBC__) 27 28/* glibc's <endian.h> is like bionic's <sys/endian.h>. */ 29#include <endian.h> 30 31/* glibc keeps htons and htonl in <netinet/in.h>. */ 32#include <netinet/in.h> 33 34/* glibc doesn't have the 64-bit variants. */ 35#define htonq(x) htobe64(x) 36#define ntohq(x) be64toh(x) 37 38/* glibc has different names to BSD for these. */ 39#define betoh16(x) be16toh(x) 40#define betoh32(x) be32toh(x) 41#define betoh64(x) be64toh(x) 42 43#else 44 45/* Mac OS and Windows have nothing. */ 46 47#define __LITTLE_ENDIAN 1234 48#define LITTLE_ENDIAN __LITTLE_ENDIAN 49 50#define __BIG_ENDIAN 4321 51#define BIG_ENDIAN __BIG_ENDIAN 52 53#define __BYTE_ORDER __LITTLE_ENDIAN 54#define BYTE_ORDER __BYTE_ORDER 55 56#define htons(x) __builtin_bswap16(x) 57#define htonl(x) __builtin_bswap32(x) 58#define htonq(x) __builtin_bswap64(x) 59 60#define ntohs(x) __builtin_bswap16(x) 61#define ntohl(x) __builtin_bswap32(x) 62#define ntohq(x) __builtin_bswap64(x) 63 64#define htobe16(x) __builtin_bswap16(x) 65#define htobe32(x) __builtin_bswap32(x) 66#define htobe64(x) __builtin_bswap64(x) 67 68#define betoh16(x) __builtin_bswap16(x) 69#define betoh32(x) __builtin_bswap32(x) 70#define betoh64(x) __builtin_bswap64(x) 71 72#define htole16(x) (x) 73#define htole32(x) (x) 74#define htole64(x) (x) 75 76#define letoh16(x) (x) 77#define letoh32(x) (x) 78#define letoh64(x) (x) 79 80#define be16toh(x) __builtin_bswap16(x) 81#define be32toh(x) __builtin_bswap32(x) 82#define be64toh(x) __builtin_bswap64(x) 83 84#define le16toh(x) (x) 85#define le32toh(x) (x) 86#define le64toh(x) (x) 87 88#endif 89 90#endif // ANDROID_BASE_ENDIAN_H 91