-/* PSPP - computes sample statistics.
- Copyright (C) 2006 Free Software Foundation, Inc.
+/* PSPP - a program for statistical analysis.
+ 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 the Free Software Foundation; either version 2 of the
- License, or (at your option) any later version.
+ 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
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
- This program 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 GNU
- General Public License for more details.
+ This program 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
+ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- 02110-1301, USA. */
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef LIBPSPP_INTEGER_FORMAT_H
#define LIBPSPP_INTEGER_FORMAT_H 1
#include <byteswap.h>
#include <stdint.h>
-#include <libpspp/str.h>
+#include "libpspp/str.h"
/* An integer format. */
enum integer_format
#endif
};
-void integer_convert (enum integer_format, const void *,
+/* Endian 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)
+
+/* Endian conversion functions.
+
+ Use these in preference to the macros above. */
+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);
uint64_t integer_get (enum integer_format, const void *, size_t);