--- nono/lib/missing_endian.h 2026/04/29 17:04:28 1.1 +++ nono/lib/missing_endian.h 2026/04/29 17:04:34 1.1.1.3 @@ -1,6 +1,7 @@ // // nono -// Copyright (C) 2018 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // #pragma once @@ -18,46 +19,33 @@ # endif #endif -static inline uint16 be16toh(uint16 val) -{ -#if _BYTE_ORDER == _BIG_ENDIAN - return val; -#elif defined(HAVE___BUILTIN_BSWAP16) - return __builtin_bswap16(val); -#else -#error no be16toh available +// __builtin_bswap* がないような環境があったらその時考える +#if !defined(HAVE___BUILTIN_BSWAP16) || \ + !defined(HAVE___BUILTIN_BSWAP32) || \ + !defined(HAVE___BUILTIN_BSWAP64) +#error No __builtin_bswap* #endif -} -static inline uint32 be32toh(uint32 val) -{ #if _BYTE_ORDER == _BIG_ENDIAN - return val; -#elif defined(HAVE___BUILTIN_BSWAP32) - return __builtin_bswap32(val); +#define be16toh(x) ((uint16)(x)) +#define be32toh(x) ((uint32)(x)) +#define be64toh(x) ((uint64)(x)) +#define le16toh(x) __builtin_bswap16((uint16)(x)) +#define le32toh(x) __builtin_bswap32((uint32)(x)) +#define le64toh(x) __builtin_bswap64((uint64)(x)) #else -#error no be32toh available +#define be16toh(x) __builtin_bswap16((uint16)(x)) +#define be32toh(x) __builtin_bswap32((uint32)(x)) +#define be64toh(x) __builtin_bswap64((uint64)(x)) +#define le16toh(x) ((uint16)(x)) +#define le32toh(x) ((uint32)(x)) +#define le64toh(x) ((uint64)(x)) #endif -} -static inline uint16 htobe16(uint16 val) -{ -#if _BYTE_ORDER == _BIG_ENDIAN - return val; -#elif defined(HAVE___BUILTIN_BSWAP16) - return __builtin_bswap16(val); -#else -#error no htobe16 available -#endif -} - -static inline uint32 htobe32(uint32 val) -{ -#if _BYTE_ORDER == _BIG_ENDIAN - return val; -#elif defined(HAVE___BUILTIN_BSWAP32) - return __builtin_bswap32(val); -#else -#error no htobe32 available -#endif -} +#define htobe16(x) be16toh(x) +#define htobe32(x) be32toh(x) +#define htobe64(x) be64toh(x) + +#define htole16(x) le16toh(x) +#define htole32(x) le32toh(x) +#define htole64(x) le64toh(x)