1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2006, 2010, 2011, 2012 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 #ifndef LIBPSPP_INTEGER_FORMAT_H
18 #define LIBPSPP_INTEGER_FORMAT_H 1
23 #include "libpspp/str.h"
25 /* An integer format. */
28 INTEGER_MSB_FIRST, /* Big-endian: MSB at lowest address. */
29 INTEGER_LSB_FIRST, /* Little-endian: LSB at lowest address. */
30 INTEGER_VAX, /* VAX-endian: little-endian 16-bit words
31 in big-endian order. */
33 /* Native endianness. */
34 #ifdef WORDS_BIGENDIAN
35 INTEGER_NATIVE = INTEGER_MSB_FIRST
37 INTEGER_NATIVE = INTEGER_LSB_FIRST
41 /* Byte-order conversion macros.
43 These are intended for use only in contexts where a function cannot be
44 called, e.g. static initializers or case labels.
46 #ifdef WORDS_BIGENDIAN
47 #define CPU_TO_BE16(X) ((uint16_t) (X))
48 #define CPU_TO_BE32(X) ((uint32_t) (X))
49 #define CPU_TO_BE64(X) ((uint64_t) (X))
50 #define CPU_TO_LE16(X) ((uint16_t) bswap_16 ((uint16_t) (X)))
51 #define CPU_TO_LE32(X) ((uint32_t) bswap_32 ((uint32_t) (X)))
52 #define CPU_TO_LE64(X) ((uint64_t) bswap_64 ((uint64_t) (X)))
53 #else /* !WORDS_BIGENDIAN */
54 #define CPU_TO_BE16(X) ((uint16_t) bswap_16 ((uint16_t) (X)))
55 #define CPU_TO_BE32(X) ((uint32_t) bswap_32 ((uint32_t) (X)))
56 #define CPU_TO_BE64(X) ((uint64_t) bswap_64 ((uint64_t) (X)))
57 #define CPU_TO_LE16(X) ((uint16_t) (X))
58 #define CPU_TO_LE32(X) ((uint32_t) (X))
59 #define CPU_TO_LE64(X) ((uint64_t) (X))
60 #endif /* !WORDS_BIGENDIAN */
61 #define BE16_TO_CPU(X) CPU_TO_BE16 (X)
62 #define BE32_TO_CPU(X) CPU_TO_BE32 (X)
63 #define BE64_TO_CPU(X) CPU_TO_BE64 (X)
64 #define LE16_TO_CPU(X) CPU_TO_LE16 (X)
65 #define LE32_TO_CPU(X) CPU_TO_LE32 (X)
66 #define LE64_TO_CPU(X) CPU_TO_LE64 (X)
68 /* Byte-order conversion functions.
70 These should be preferred to the macros. */
71 static inline uint16_t cpu_to_be16 (uint16_t x) { return CPU_TO_BE16 (x); }
72 static inline uint32_t cpu_to_be32 (uint32_t x) { return CPU_TO_BE32 (x); }
73 static inline uint64_t cpu_to_be64 (uint64_t x) { return CPU_TO_BE64 (x); }
74 static inline uint16_t cpu_to_le16 (uint16_t x) { return CPU_TO_LE16 (x); }
75 static inline uint32_t cpu_to_le32 (uint32_t x) { return CPU_TO_LE32 (x); }
76 static inline uint64_t cpu_to_le64 (uint64_t x) { return CPU_TO_LE64 (x); }
77 static inline uint16_t be16_to_cpu (uint16_t x) { return BE16_TO_CPU (x); }
78 static inline uint32_t be32_to_cpu (uint32_t x) { return BE32_TO_CPU (x); }
79 static inline uint64_t be64_to_cpu (uint64_t x) { return BE64_TO_CPU (x); }
80 static inline uint16_t le16_to_cpu (uint16_t x) { return LE16_TO_CPU (x); }
81 static inline uint32_t le32_to_cpu (uint32_t x) { return LE32_TO_CPU (x); }
82 static inline uint64_t le64_to_cpu (uint64_t x) { return LE64_TO_CPU (x); }
84 /* Generic conversion functions. */
85 void integer_convert (enum integer_format, const void *,
86 enum integer_format, void *,
88 uint64_t integer_get (enum integer_format, const void *, size_t);
89 void integer_put (uint64_t, enum integer_format, void *, size_t);
91 bool integer_identify (uint64_t expected_value, const void *, size_t,
92 enum integer_format *);
94 #endif /* libpspp/integer-format.h */