integer-format: New functions and macros for endian conversion.
authorBen Pfaff <blp@cs.stanford.edu>
Fri, 27 Aug 2010 05:49:53 +0000 (22:49 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Mon, 16 Feb 2015 19:10:55 +0000 (11:10 -0800)
src/libpspp/integer-format.h

index e888292f27d0214ab19fc746c7650777f882067a..a8a3cb32756125c5333bf1fa3021d3624c047488 100644 (file)
@@ -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);