X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=include%2Fwimlib%2Fendianness.h;h=ed0b7ec443eb26cf679810a26820278306d6db25;hb=4a20aae0dd8469a352517a0b107416ffa99ccc55;hp=2d1e9a672916dec488e6b307c324a608e2482526;hpb=e8c3ca2d1d0cac3d64985b45a9f654d2029a7518;p=wimlib diff --git a/include/wimlib/endianness.h b/include/wimlib/endianness.h index 2d1e9a67..ed0b7ec4 100644 --- a/include/wimlib/endianness.h +++ b/include/wimlib/endianness.h @@ -1,85 +1,110 @@ +/* + * endianness.h - macros and inline functions for endianness conversion + * + * The following copying information applies to this specific source code file: + * + * Written in 2014-2015 by Eric Biggers + * + * To the extent possible under law, the author(s) have dedicated all copyright + * and related and neighboring rights to this software to the public domain + * worldwide via the Creative Commons Zero 1.0 Universal Public Domain + * Dedication (the "CC0"). + * + * This software is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the CC0 for more details. + * + * You should have received a copy of the CC0 along with this software; if not + * see . + */ + #ifndef _WIMLIB_ENDIANNESS_H #define _WIMLIB_ENDIANNESS_H +#include "wimlib/compiler.h" #include "wimlib/types.h" -/* Watch out for conflict with ntfs-3g/endian.h ... */ - +/* Watch out for conflict with ntfs-3g/endians.h ... */ #ifndef _NTFS_ENDIANS_H -static inline u16 -bswap16(u16 n) -{ - return (n << 8) | (n >> 8); -} +#define bswap16_const(n) \ + ((((u16)(n) & 0x00FF) << 8) | \ + (((u16)(n) & 0xFF00) >> 8)) + +#define bswap32_const(n) \ + ((((u32)(n) & 0x000000FF) << 24) | \ + (((u32)(n) & 0x0000FF00) << 8) | \ + (((u32)(n) & 0x00FF0000) >> 8) | \ + (((u32)(n) & 0xFF000000) >> 24)) -static inline u32 -bswap32(u32 n) +#define bswap64_const(n) \ + ((((u64)(n) & 0x00000000000000FF) << 56) | \ + (((u64)(n) & 0x000000000000FF00) << 40) | \ + (((u64)(n) & 0x0000000000FF0000) << 24) | \ + (((u64)(n) & 0x00000000FF000000) << 8) | \ + (((u64)(n) & 0x000000FF00000000) >> 8) | \ + (((u64)(n) & 0x0000FF0000000000) >> 24) | \ + (((u64)(n) & 0x00FF000000000000) >> 40) | \ + (((u64)(n) & 0xFF00000000000000) >> 56)) + +static forceinline u16 do_bswap16(u16 n) { -#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) - return __builtin_bswap32(n); +#ifdef compiler_bswap16 + return compiler_bswap16(n); #else - return (n << 24) | ((n & 0xff00) << 8) | ((n & 0xff0000) >> 8) | - (n >> 24); + return bswap16_const(n); #endif } -static inline u64 -bswap64(u64 n) +static forceinline u32 do_bswap32(u32 n) { -#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) - return __builtin_bswap64(n); +#ifdef compiler_bswap32 + return compiler_bswap32(n); #else - return (n << 56) | ((n & 0xff00) << 40) | ((n & 0xff0000) << 24) | - ((n & 0xff000000) << 8) | ((n & 0xff00000000) >> 8) | - ((n & 0xff0000000000) >> 24) | - ((n & 0xff000000000000) >> 40) | (n >> 56); + return bswap32_const(n); #endif } -# ifdef WORDS_BIGENDIAN -# define le16_to_cpu(n) bswap16(n) -# define le32_to_cpu(n) bswap32(n) -# define le64_to_cpu(n) bswap64(n) -# define cpu_to_le16(n) bswap16(n) -# define cpu_to_le32(n) bswap32(n) -# define cpu_to_le64(n) bswap64(n) -# else -# define cpu_to_le16(n) (n) -# define cpu_to_le32(n) (n) -# define cpu_to_le64(n) (n) -# define le16_to_cpu(n) (n) -# define le32_to_cpu(n) (n) -# define le64_to_cpu(n) (n) -# endif -#endif /* _NTFS_ENDIANS_H */ - -static inline void -array_cpu_to_le32(u32 *p, size_t n) -{ - for (size_t i = 0; i < n; i++) - p[i] = cpu_to_le32(p[i]); -} - -static inline void -array_le32_to_cpu(u32 *p, size_t n) +static forceinline u64 do_bswap64(u64 n) { - for (size_t i = 0; i < n; i++) - p[i] = le32_to_cpu(p[i]); +#ifdef compiler_bswap64 + return compiler_bswap64(n); +#else + return bswap64_const(n); +#endif } -static inline void -array_cpu_to_le64(u64 *p, size_t n) -{ - for (size_t i = 0; i < n; i++) - p[i] = cpu_to_le64(p[i]); -} +#define bswap16(n) (__builtin_constant_p(n) ? bswap16_const(n) : do_bswap16(n)) +#define bswap32(n) (__builtin_constant_p(n) ? bswap32_const(n) : do_bswap32(n)) +#define bswap64(n) (__builtin_constant_p(n) ? bswap64_const(n) : do_bswap64(n)) -static inline void -array_le64_to_cpu(u64 *p, size_t n) -{ - for (size_t i = 0; i < n; i++) - p[i] = le64_to_cpu(p[i]); -} +#if CPU_IS_BIG_ENDIAN +# define cpu_to_le16(n) ((_force_attr le16)bswap16(n)) +# define cpu_to_le32(n) ((_force_attr le32)bswap32(n)) +# define cpu_to_le64(n) ((_force_attr le64)bswap64(n)) +# define le16_to_cpu(n) bswap16((_force_attr u16)(le16)(n)) +# define le32_to_cpu(n) bswap32((_force_attr u32)(le32)(n)) +# define le64_to_cpu(n) bswap64((_force_attr u64)(le64)(n)) +# define cpu_to_be16(n) ((_force_attr be16)(u16)(n)) +# define cpu_to_be32(n) ((_force_attr be32)(u32)(n)) +# define cpu_to_be64(n) ((_force_attr be64)(u64)(n)) +# define be16_to_cpu(n) ((_force_attr u16)(be16)(n)) +# define be32_to_cpu(n) ((_force_attr u32)(be32)(n)) +# define be64_to_cpu(n) ((_force_attr u64)(be64)(n)) +#else +# define cpu_to_le16(n) ((_force_attr le16)(u16)(n)) +# define cpu_to_le32(n) ((_force_attr le32)(u32)(n)) +# define cpu_to_le64(n) ((_force_attr le64)(u64)(n)) +# define le16_to_cpu(n) ((_force_attr u16)(le16)(n)) +# define le32_to_cpu(n) ((_force_attr u32)(le32)(n)) +# define le64_to_cpu(n) ((_force_attr u64)(le64)(n)) +# define cpu_to_be16(n) ((_force_attr be16)bswap16(n)) +# define cpu_to_be32(n) ((_force_attr be32)bswap32(n)) +# define cpu_to_be64(n) ((_force_attr be64)bswap64(n)) +# define be16_to_cpu(n) bswap16((_force_attr u16)(be16)(n)) +# define be32_to_cpu(n) bswap32((_force_attr u32)(be32)(n)) +# define be64_to_cpu(n) bswap64((_force_attr u64)(be64)(n)) +#endif +#endif /* _NTFS_ENDIANS_H */ #endif /* _WIMLIB_ENDIANNESS_H */