integer-format: Add some useful helper functions.
authorBen Pfaff <blp@cs.stanford.edu>
Mon, 26 Nov 2018 00:38:47 +0000 (16:38 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Tue, 25 Dec 2018 19:36:12 +0000 (11:36 -0800)
These functions already existed in float-format.c, but they are more
generally useful, so this commit moves them into integer-format.h.

src/libpspp/float-format.c
src/libpspp/integer-format.h

index 03d8c99062d7af3be1e20bc1af7e4d509d9c2eef..03f9411613938addd72905bb1dae5ab48c28760d 100644 (file)
@@ -213,58 +213,6 @@ get_bits (uint64_t x, int ofs, int cnt)
   return (x >> ofs) & ((UINT64_C(1) << cnt) - 1);
 }
 
-/* Returns the 32-bit unsigned integer at P,
-   which need not be aligned. */
-static inline uint32_t
-get_uint32 (const void *p)
-{
-  uint32_t x;
-  memcpy (&x, p, sizeof x);
-  return x;
-}
-
-/* Returns the 64-bit unsigned integer at P,
-   which need not be aligned. */
-static inline uint64_t
-get_uint64 (const void *p)
-{
-  uint64_t x;
-  memcpy (&x, p, sizeof x);
-  return x;
-}
-
-/* Stores 32-bit unsigned integer X at P,
-   which need not be aligned. */
-static inline void
-put_uint32 (uint32_t x, void *p)
-{
-  memcpy (p, &x, sizeof x);
-}
-
-/* Stores 64-bit unsigned integer X at P,
-   which need not be aligned. */
-static inline void
-put_uint64 (uint64_t x, void *p)
-{
-  memcpy (p, &x, sizeof x);
-}
-
-/* Returns NATIVE converted to a form that, when stored in
-   memory, will be in little-endian byte order. */
-static inline uint32_t
-native_to_le32 (uint32_t native)
-{
-  return INTEGER_NATIVE == INTEGER_LSB_FIRST ? native : bswap_32 (native);
-}
-
-/* Returns NATIVE converted to a form that, when stored in
-   memory, will be in big-endian byte order. */
-static inline uint32_t
-native_to_be32 (uint32_t native)
-{
-  return INTEGER_NATIVE == INTEGER_MSB_FIRST ? native : bswap_32 (native);
-}
-
 /* Returns NATIVE converted to a form that, when stored in
    memory, will be in VAX-endian byte order. */
 static inline uint32_t
@@ -274,22 +222,6 @@ native_to_vax32 (uint32_t native)
                          ((native & 0x00ff00ff) << 8));
 }
 
-/* Returns NATIVE converted to a form that, when stored in
-   memory, will be in little-endian byte order. */
-static inline uint64_t
-native_to_le64 (uint64_t native)
-{
-  return INTEGER_NATIVE == INTEGER_LSB_FIRST ? native : bswap_64 (native);
-}
-
-/* Returns NATIVE converted to a form that, when stored in
-   memory, will be in big-endian byte order. */
-static inline uint64_t
-native_to_be64 (uint64_t native)
-{
-  return INTEGER_NATIVE == INTEGER_MSB_FIRST ? native : bswap_64 (native);
-}
-
 /* Returns NATIVE converted to a form that, when stored in
    memory, will be in VAX-endian byte order. */
 static inline uint64_t
@@ -301,22 +233,6 @@ native_to_vax64 (uint64_t native)
                          ((native & UINT64_C(0x0000000000ff00ff)) << 40));
 }
 
-/* Given LE, obtained from memory in little-endian format,
-   returns its value. */
-static inline uint32_t
-le_to_native32 (uint32_t le)
-{
-  return INTEGER_NATIVE == INTEGER_LSB_FIRST ? le : bswap_32 (le);
-}
-
-/* Given BE, obtained from memory in big-endian format, returns
-   its value. */
-static inline uint32_t
-be_to_native32 (uint32_t be)
-{
-  return INTEGER_NATIVE == INTEGER_MSB_FIRST ? be : bswap_32 (be);
-}
-
 /* Given VAX, obtained from memory in VAX-endian format, returns
    its value. */
 static inline uint32_t
@@ -326,22 +242,6 @@ vax_to_native32 (uint32_t vax)
   return ((be & 0xff00ff00) >> 8) | ((be & 0x00ff00ff) << 8);
 }
 
-/* Given LE, obtained from memory in little-endian format,
-   returns its value. */
-static inline uint64_t
-le_to_native64 (uint64_t le)
-{
-  return INTEGER_NATIVE == INTEGER_LSB_FIRST ? le : bswap_64 (le);
-}
-
-/* Given BE, obtained from memory in big-endian format, returns
-   its value. */
-static inline uint64_t
-be_to_native64 (uint64_t be)
-{
-  return INTEGER_NATIVE == INTEGER_MSB_FIRST ? be : bswap_64 (be);
-}
-
 /* Given VAX, obtained from memory in VAX-endian format, returns
    its value. */
 static inline uint64_t
index e888292f27d0214ab19fc746c7650777f882067a..2ea61a26483e334bae43351c7ee258ea8494e15c 100644 (file)
@@ -19,6 +19,7 @@
 
 #include <byteswap.h>
 #include <stdint.h>
+#include <string.h>
 
 #include "libpspp/str.h"
 
@@ -46,5 +47,150 @@ void integer_put (uint64_t, enum integer_format, void *, size_t);
 
 bool integer_identify (uint64_t expected_value, const void *, size_t,
                        enum integer_format *);
+\f
+/* Returns the 16-bit unsigned integer at P, which need not be aligned. */
+static inline uint16_t
+get_uint16 (const void *p)
+{
+  uint16_t x;
+  memcpy (&x, p, sizeof x);
+  return x;
+}
+
+/* Returns the 32-bit unsigned integer at P, which need not be aligned. */
+static inline uint32_t
+get_uint32 (const void *p)
+{
+  uint32_t x;
+  memcpy (&x, p, sizeof x);
+  return x;
+}
+
+/* Returns the 64-bit unsigned integer at P, which need not be aligned. */
+static inline uint64_t
+get_uint64 (const void *p)
+{
+  uint64_t x;
+  memcpy (&x, p, sizeof x);
+  return x;
+}
+
+/* Stores 16-bit unsigned integer X at P, which need not be aligned. */
+static inline void
+put_uint16 (uint16_t x, void *p)
+{
+  memcpy (p, &x, sizeof x);
+}
+
+/* Stores 32-bit unsigned integer X at P, which need not be aligned. */
+static inline void
+put_uint32 (uint32_t x, void *p)
+{
+  memcpy (p, &x, sizeof x);
+}
+
+/* Stores 64-bit unsigned integer X at P, which need not be aligned. */
+static inline void
+put_uint64 (uint64_t x, void *p)
+{
+  memcpy (p, &x, sizeof x);
+}
+
+/* Returns NATIVE converted to a form that, when stored in
+   memory, will be in little-endian byte order. */
+static inline uint16_t
+native_to_le16 (uint16_t native)
+{
+  return INTEGER_NATIVE == INTEGER_LSB_FIRST ? native : bswap_16 (native);
+}
+
+/* Returns NATIVE converted to a form that, when stored in
+   memory, will be in big-endian byte order. */
+static inline uint16_t
+native_to_be16 (uint16_t native)
+{
+  return INTEGER_NATIVE == INTEGER_MSB_FIRST ? native : bswap_16 (native);
+}
+
+/* Returns NATIVE converted to a form that, when stored in
+   memory, will be in little-endian byte order. */
+static inline uint32_t
+native_to_le32 (uint32_t native)
+{
+  return INTEGER_NATIVE == INTEGER_LSB_FIRST ? native : bswap_32 (native);
+}
+
+/* Returns NATIVE converted to a form that, when stored in
+   memory, will be in big-endian byte order. */
+static inline uint32_t
+native_to_be32 (uint32_t native)
+{
+  return INTEGER_NATIVE == INTEGER_MSB_FIRST ? native : bswap_32 (native);
+}
+
+/* Returns NATIVE converted to a form that, when stored in
+   memory, will be in little-endian byte order. */
+static inline uint64_t
+native_to_le64 (uint64_t native)
+{
+  return INTEGER_NATIVE == INTEGER_LSB_FIRST ? native : bswap_64 (native);
+}
+
+/* Returns NATIVE converted to a form that, when stored in
+   memory, will be in big-endian byte order. */
+static inline uint64_t
+native_to_be64 (uint64_t native)
+{
+  return INTEGER_NATIVE == INTEGER_MSB_FIRST ? native : bswap_64 (native);
+}
+
+/* Given LE, obtained from memory in little-endian format,
+   returns its value. */
+static inline uint16_t
+le_to_native16 (uint16_t le)
+{
+  return INTEGER_NATIVE == INTEGER_LSB_FIRST ? le : bswap_16 (le);
+}
+
+/* Given BE, obtained from memory in big-endian format, returns
+   its value. */
+static inline uint16_t
+be_to_native16 (uint16_t be)
+{
+  return INTEGER_NATIVE == INTEGER_MSB_FIRST ? be : bswap_16 (be);
+}
+
+/* Given LE, obtained from memory in little-endian format,
+   returns its value. */
+static inline uint32_t
+le_to_native32 (uint32_t le)
+{
+  return INTEGER_NATIVE == INTEGER_LSB_FIRST ? le : bswap_32 (le);
+}
+
+/* Given BE, obtained from memory in big-endian format, returns
+   its value. */
+static inline uint32_t
+be_to_native32 (uint32_t be)
+{
+  return INTEGER_NATIVE == INTEGER_MSB_FIRST ? be : bswap_32 (be);
+}
+
+/* Given LE, obtained from memory in little-endian format,
+   returns its value. */
+static inline uint64_t
+le_to_native64 (uint64_t le)
+{
+  return INTEGER_NATIVE == INTEGER_LSB_FIRST ? le : bswap_64 (le);
+}
+
+/* Given BE, obtained from memory in big-endian format, returns
+   its value. */
+static inline uint64_t
+be_to_native64 (uint64_t be)
+{
+  return INTEGER_NATIVE == INTEGER_MSB_FIRST ? be : bswap_64 (be);
+}
+
 
 #endif /* libpspp/integer-format.h */