From: Ben Pfaff Date: Fri, 27 Aug 2010 05:49:53 +0000 (-0700) Subject: integer-format: New functions and macros for endian conversion. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?p=pspp;a=commitdiff_plain;h=2ca3267c1110bbff675c560b19d02defb96ee2f9 integer-format: New functions and macros for endian conversion. --- diff --git a/src/libpspp/integer-format.h b/src/libpspp/integer-format.h index e888292f27..a8a3cb3275 100644 --- a/src/libpspp/integer-format.h +++ b/src/libpspp/integer-format.h @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2006, 2011 Free Software Foundation, Inc. + Copyright (C) 2006, 2010, 2011, 2012 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -38,6 +38,50 @@ enum integer_format #endif }; +/* Byte-order conversion macros. + + These are intended for use only in contexts where a function cannot be + called, e.g. static initializers or case labels. +*/ +#ifdef WORDS_BIGENDIAN +#define CPU_TO_BE16(X) ((uint16_t) (X)) +#define CPU_TO_BE32(X) ((uint32_t) (X)) +#define CPU_TO_BE64(X) ((uint64_t) (X)) +#define CPU_TO_LE16(X) ((uint16_t) bswap_16 ((uint16_t) (X))) +#define CPU_TO_LE32(X) ((uint32_t) bswap_32 ((uint32_t) (X))) +#define CPU_TO_LE64(X) ((uint64_t) bswap_64 ((uint64_t) (X))) +#else /* !WORDS_BIGENDIAN */ +#define CPU_TO_BE16(X) ((uint16_t) bswap_16 ((uint16_t) (X))) +#define CPU_TO_BE32(X) ((uint32_t) bswap_32 ((uint32_t) (X))) +#define CPU_TO_BE64(X) ((uint64_t) bswap_64 ((uint64_t) (X))) +#define CPU_TO_LE16(X) ((uint16_t) (X)) +#define CPU_TO_LE32(X) ((uint32_t) (X)) +#define CPU_TO_LE64(X) ((uint64_t) (X)) +#endif /* !WORDS_BIGENDIAN */ +#define BE16_TO_CPU(X) CPU_TO_BE16 (X) +#define BE32_TO_CPU(X) CPU_TO_BE32 (X) +#define BE64_TO_CPU(X) CPU_TO_BE64 (X) +#define LE16_TO_CPU(X) CPU_TO_LE16 (X) +#define LE32_TO_CPU(X) CPU_TO_LE32 (X) +#define LE64_TO_CPU(X) CPU_TO_LE64 (X) + +/* Byte-order conversion functions. + + These should be preferred to the macros. */ +static inline uint16_t cpu_to_be16 (uint16_t x) { return CPU_TO_BE16 (x); } +static inline uint32_t cpu_to_be32 (uint32_t x) { return CPU_TO_BE32 (x); } +static inline uint64_t cpu_to_be64 (uint64_t x) { return CPU_TO_BE64 (x); } +static inline uint16_t cpu_to_le16 (uint16_t x) { return CPU_TO_LE16 (x); } +static inline uint32_t cpu_to_le32 (uint32_t x) { return CPU_TO_LE32 (x); } +static inline uint64_t cpu_to_le64 (uint64_t x) { return CPU_TO_LE64 (x); } +static inline uint16_t be16_to_cpu (uint16_t x) { return BE16_TO_CPU (x); } +static inline uint32_t be32_to_cpu (uint32_t x) { return BE32_TO_CPU (x); } +static inline uint64_t be64_to_cpu (uint64_t x) { return BE64_TO_CPU (x); } +static inline uint16_t le16_to_cpu (uint16_t x) { return LE16_TO_CPU (x); } +static inline uint32_t le32_to_cpu (uint32_t x) { return LE32_TO_CPU (x); } +static inline uint64_t le64_to_cpu (uint64_t x) { return LE64_TO_CPU (x); } + +/* Generic conversion functions. */ void integer_convert (enum integer_format, const void *, enum integer_format, void *, size_t);