1141cc406Sopenharmony_cidnl AC_NEED_BYTEORDER_H ( HEADER-TO-GENERATE )
2141cc406Sopenharmony_cidnl Copyright 2001-2002 by Dan Fandrich <dan@coneharvesters.com>
3141cc406Sopenharmony_cidnl This file may be copied and used freely without restrictions.  No warranty
4141cc406Sopenharmony_cidnl is expressed or implied.
5141cc406Sopenharmony_cidnl
6141cc406Sopenharmony_cidnl Create a header file that guarantees that byte swapping macros of the
7141cc406Sopenharmony_cidnl ntohl variety as well as the extended types included in OpenBSD and
8141cc406Sopenharmony_cidnl NetBSD such as le32toh are defined.  If possible, the standard ntohl
9141cc406Sopenharmony_cidnl are overloaded as they are optimized for the given platform, but when
10141cc406Sopenharmony_cidnl this is not possible (e.g. on a big-endian machine) they are defined
11141cc406Sopenharmony_cidnl in this file.
12141cc406Sopenharmony_ci
13141cc406Sopenharmony_cidnl Look for a symbol in a header file
14141cc406Sopenharmony_cidnl AC_HAVE_SYMBOL ( IDENTIFIER, HEADER-FILE, ACTION-IF-FOUND, ACTION-IF-NOT-FOUND )
15141cc406Sopenharmony_ciAC_DEFUN([AC_HAVE_SYMBOL],
16141cc406Sopenharmony_ci[
17141cc406Sopenharmony_ciAC_MSG_CHECKING(for $1 in $2)
18141cc406Sopenharmony_ciAC_EGREP_CPP([symbol is present|\<$1\>],[
19141cc406Sopenharmony_ci#include <$2>
20141cc406Sopenharmony_ci#ifdef $1
21141cc406Sopenharmony_ci 	symbol is present
22141cc406Sopenharmony_ci#endif
23141cc406Sopenharmony_ci	],
24141cc406Sopenharmony_ci[AC_MSG_RESULT(yes)
25141cc406Sopenharmony_ci$3
26141cc406Sopenharmony_ci],
27141cc406Sopenharmony_ci[AC_MSG_RESULT(no)
28141cc406Sopenharmony_ci$4
29141cc406Sopenharmony_ci])])
30141cc406Sopenharmony_ci
31141cc406Sopenharmony_ci
32141cc406Sopenharmony_cidnl Create a header file that defines extended byte swapping macros
33141cc406Sopenharmony_ciAC_DEFUN([AC_NEED_BYTEORDER_H],
34141cc406Sopenharmony_ci[
35141cc406Sopenharmony_ciac_byteorder_h=`echo ifelse($1, , _byteorder.h, $1)`
36141cc406Sopenharmony_cichangequote(, )dnl
37141cc406Sopenharmony_ciac_dir=`echo $ac_byteorder_h|sed 's%/[^/][^/]*$%%'`
38141cc406Sopenharmony_cichangequote([, ])dnl
39141cc406Sopenharmony_ciif test "$ac_dir" != "$ac_byteorder" && test "$ac_dir" != .; then
40141cc406Sopenharmony_ci  # The file is in a subdirectory.
41141cc406Sopenharmony_ci  test ! -d "$ac_dir" && mkdir "$ac_dir"
42141cc406Sopenharmony_cifi
43141cc406Sopenharmony_ci
44141cc406Sopenharmony_ci# We're only interested in the target CPU, but it's not always set
45141cc406Sopenharmony_cieffective_target="$target"
46141cc406Sopenharmony_ciif test "x$effective_target" = xNONE -o "x$effective_target" = x ; then
47141cc406Sopenharmony_ci	effective_target="$host"
48141cc406Sopenharmony_cifi
49141cc406Sopenharmony_ciAC_SUBST(effective_target)
50141cc406Sopenharmony_ci
51141cc406Sopenharmony_ciac_byteorder=_byteorder.tmp
52141cc406Sopenharmony_cicat > "$ac_byteorder" << EOF
53141cc406Sopenharmony_ci/* This file is generated automatically by configure */
54141cc406Sopenharmony_ci/* It is valid only for the system type ${effective_target} */
55141cc406Sopenharmony_ci
56141cc406Sopenharmony_ci#ifndef __BYTEORDER_H
57141cc406Sopenharmony_ci#define __BYTEORDER_H
58141cc406Sopenharmony_ci
59141cc406Sopenharmony_ciEOF
60141cc406Sopenharmony_ci
61141cc406Sopenharmony_cidnl First, do an endian check
62141cc406Sopenharmony_ciAC_C_BIGENDIAN
63141cc406Sopenharmony_ci
64141cc406Sopenharmony_cidnl Look for NetBSD-style extended byte swapping macros
65141cc406Sopenharmony_ciAC_HAVE_SYMBOL(le32toh,machine/endian.h,
66141cc406Sopenharmony_ci [HAVE_LE32TOH=1
67141cc406Sopenharmony_ci cat >> "$ac_byteorder" << EOF
68141cc406Sopenharmony_ci/* extended byte swapping macros are already available */
69141cc406Sopenharmony_ci#include <machine/endian.h>
70141cc406Sopenharmony_ci
71141cc406Sopenharmony_ciEOF],
72141cc406Sopenharmony_ci
73141cc406Sopenharmony_ci[
74141cc406Sopenharmony_ci
75141cc406Sopenharmony_cidnl Look for standard byte swapping macros
76141cc406Sopenharmony_ciAC_HAVE_SYMBOL(ntohl,arpa/inet.h,
77141cc406Sopenharmony_ci [cat >> "$ac_byteorder" << EOF
78141cc406Sopenharmony_ci/* ntohl and relatives live here */
79141cc406Sopenharmony_ci#include <arpa/inet.h>
80141cc406Sopenharmony_ci
81141cc406Sopenharmony_ciEOF],
82141cc406Sopenharmony_ci
83141cc406Sopenharmony_ci [AC_HAVE_SYMBOL(ntohl,netinet/in.h,
84141cc406Sopenharmony_ci  [cat >> "$ac_byteorder" << EOF
85141cc406Sopenharmony_ci/* ntohl and relatives live here */
86141cc406Sopenharmony_ci#include <netinet/in.h>
87141cc406Sopenharmony_ci
88141cc406Sopenharmony_ciEOF],true)])
89141cc406Sopenharmony_ci])
90141cc406Sopenharmony_ci
91141cc406Sopenharmony_cidnl Look for generic byte swapping macros
92141cc406Sopenharmony_ci
93141cc406Sopenharmony_cidnl OpenBSD
94141cc406Sopenharmony_ciAC_HAVE_SYMBOL(swap32,machine/endian.h,
95141cc406Sopenharmony_ci [cat >> "$ac_byteorder" << EOF
96141cc406Sopenharmony_ci/* swap32 and swap16 are defined in machine/endian.h */
97141cc406Sopenharmony_ci
98141cc406Sopenharmony_ciEOF],
99141cc406Sopenharmony_ci
100141cc406Sopenharmony_ci [
101141cc406Sopenharmony_cidnl Linux GLIBC
102141cc406Sopenharmony_ci  AC_HAVE_SYMBOL(bswap_32,byteswap.h,
103141cc406Sopenharmony_ci   [cat >> "$ac_byteorder" << EOF
104141cc406Sopenharmony_ci/* Define generic byte swapping functions */
105141cc406Sopenharmony_ci#include <byteswap.h>
106141cc406Sopenharmony_ci#define swap16(x) bswap_16(x)
107141cc406Sopenharmony_ci#define swap32(x) bswap_32(x)
108141cc406Sopenharmony_ci#define swap64(x) bswap_64(x)
109141cc406Sopenharmony_ci
110141cc406Sopenharmony_ciEOF],
111141cc406Sopenharmony_ci
112141cc406Sopenharmony_ci   [
113141cc406Sopenharmony_cidnl NetBSD
114141cc406Sopenharmony_ci  	AC_HAVE_SYMBOL(bswap32,machine/endian.h,
115141cc406Sopenharmony_ci    dnl We're already including machine/endian.h if this test succeeds
116141cc406Sopenharmony_ci  	 [cat >> "$ac_byteorder" << EOF
117141cc406Sopenharmony_ci/* Define generic byte swapping functions */
118141cc406Sopenharmony_ciEOF
119141cc406Sopenharmony_ci	if test "$HAVE_LE32TOH" != "1"; then
120141cc406Sopenharmony_ci		echo '#include <machine/endian.h>'>> "$ac_byteorder"
121141cc406Sopenharmony_ci	fi
122141cc406Sopenharmony_cicat >> "$ac_byteorder" << EOF
123141cc406Sopenharmony_ci#define swap16(x) bswap16(x)
124141cc406Sopenharmony_ci#define swap32(x) bswap32(x)
125141cc406Sopenharmony_ci#define swap64(x) bswap64(x)
126141cc406Sopenharmony_ci
127141cc406Sopenharmony_ciEOF],
128141cc406Sopenharmony_ci
129141cc406Sopenharmony_ci   [
130141cc406Sopenharmony_cidnl FreeBSD
131141cc406Sopenharmony_ci  	AC_HAVE_SYMBOL(__byte_swap_long,sys/types.h,
132141cc406Sopenharmony_ci  	 [cat >> "$ac_byteorder" << EOF
133141cc406Sopenharmony_ci/* Define generic byte swapping functions */
134141cc406Sopenharmony_ci#include <sys/types.h>
135141cc406Sopenharmony_ci#define swap16(x) __byte_swap_word(x)
136141cc406Sopenharmony_ci#define swap32(x) __byte_swap_long(x)
137141cc406Sopenharmony_ci/* No optimized 64 bit byte swapping macro is available */
138141cc406Sopenharmony_ci#define swap64(x) ((uint64_t)(((uint64_t)(x) << 56) & 0xff00000000000000ULL | \\
139141cc406Sopenharmony_ci			      ((uint64_t)(x) << 40) & 0x00ff000000000000ULL | \\
140141cc406Sopenharmony_ci			      ((uint64_t)(x) << 24) & 0x0000ff0000000000ULL | \\
141141cc406Sopenharmony_ci			      ((uint64_t)(x) << 8)  & 0x000000ff00000000ULL | \\
142141cc406Sopenharmony_ci			      ((x) >> 8)  & 0x00000000ff000000ULL | \\
143141cc406Sopenharmony_ci			      ((x) >> 24) & 0x0000000000ff0000ULL | \\
144141cc406Sopenharmony_ci			      ((x) >> 40) & 0x000000000000ff00ULL | \\
145141cc406Sopenharmony_ci			      ((x) >> 56) & 0x00000000000000ffULL))
146141cc406Sopenharmony_ci
147141cc406Sopenharmony_ciEOF],
148141cc406Sopenharmony_ci
149141cc406Sopenharmony_ci  	 [
150141cc406Sopenharmony_cidnl OS X
151141cc406Sopenharmony_ci  	AC_HAVE_SYMBOL(NXSwapLong,machine/byte_order.h,
152141cc406Sopenharmony_ci  	 [cat >> "$ac_byteorder" << EOF
153141cc406Sopenharmony_ci/* Define generic byte swapping functions */
154141cc406Sopenharmony_ci#include <machine/byte_order.h>
155141cc406Sopenharmony_ci#define swap16(x) NXSwapShort(x)
156141cc406Sopenharmony_ci#define swap32(x) NXSwapLong(x)
157141cc406Sopenharmony_ci#define swap64(x) NXSwapLongLong(x)
158141cc406Sopenharmony_ci
159141cc406Sopenharmony_ciEOF],
160141cc406Sopenharmony_ci         [
161141cc406Sopenharmony_ci	if test $ac_cv_c_bigendian = yes; then
162141cc406Sopenharmony_ci		cat >> "$ac_byteorder" << EOF
163141cc406Sopenharmony_ci/* No other byte swapping functions are available on this big-endian system */
164141cc406Sopenharmony_ci#define swap16(x)	((uint16_t)(((x) << 8) | ((uint16_t)(x) >> 8)))
165141cc406Sopenharmony_ci#define swap32(x)	((uint32_t)(((uint32_t)(x) << 24) & 0xff000000UL | \\
166141cc406Sopenharmony_ci				    ((uint32_t)(x) << 8)  & 0x00ff0000UL | \\
167141cc406Sopenharmony_ci				    ((x) >> 8)  & 0x0000ff00UL | \\
168141cc406Sopenharmony_ci				    ((x) >> 24) & 0x000000ffUL))
169141cc406Sopenharmony_ci#define swap64(x) ((uint64_t)(((uint64_t)(x) << 56) & 0xff00000000000000ULL | \\
170141cc406Sopenharmony_ci			      ((uint64_t)(x) << 40) & 0x00ff000000000000ULL | \\
171141cc406Sopenharmony_ci			      ((uint64_t)(x) << 24) & 0x0000ff0000000000ULL | \\
172141cc406Sopenharmony_ci			      ((uint64_t)(x) << 8)  & 0x000000ff00000000ULL | \\
173141cc406Sopenharmony_ci			      ((x) >> 8)  & 0x00000000ff000000ULL | \\
174141cc406Sopenharmony_ci			      ((x) >> 24) & 0x0000000000ff0000ULL | \\
175141cc406Sopenharmony_ci			      ((x) >> 40) & 0x000000000000ff00ULL | \\
176141cc406Sopenharmony_ci			      ((x) >> 56) & 0x00000000000000ffULL))
177141cc406Sopenharmony_ci
178141cc406Sopenharmony_ciEOF
179141cc406Sopenharmony_ci	else
180141cc406Sopenharmony_ci cat >> "$ac_byteorder" << EOF
181141cc406Sopenharmony_ci/* Use these as generic byteswapping macros on this little endian system */
182141cc406Sopenharmony_ci#define swap16(x)		ntohs(x)
183141cc406Sopenharmony_ci#define swap32(x)		ntohl(x)
184141cc406Sopenharmony_ci/* No optimized 64 bit byte swapping macro is available */
185141cc406Sopenharmony_ci#define swap64(x) ((uint64_t)(((uint64_t)(x) << 56) & 0xff00000000000000ULL | \\
186141cc406Sopenharmony_ci			      ((uint64_t)(x) << 40) & 0x00ff000000000000ULL | \\
187141cc406Sopenharmony_ci			      ((uint64_t)(x) << 24) & 0x0000ff0000000000ULL | \\
188141cc406Sopenharmony_ci			      ((uint64_t)(x) << 8)  & 0x000000ff00000000ULL | \\
189141cc406Sopenharmony_ci			      ((x) >> 8)  & 0x00000000ff000000ULL | \\
190141cc406Sopenharmony_ci			      ((x) >> 24) & 0x0000000000ff0000ULL | \\
191141cc406Sopenharmony_ci			      ((x) >> 40) & 0x000000000000ff00ULL | \\
192141cc406Sopenharmony_ci			      ((x) >> 56) & 0x00000000000000ffULL))
193141cc406Sopenharmony_ci
194141cc406Sopenharmony_ciEOF
195141cc406Sopenharmony_ci	fi
196141cc406Sopenharmony_ci])
197141cc406Sopenharmony_ci  	  ])
198141cc406Sopenharmony_ci    ])
199141cc406Sopenharmony_ci  ])
200141cc406Sopenharmony_ci])
201141cc406Sopenharmony_ci
202141cc406Sopenharmony_ci
203141cc406Sopenharmony_ci[
204141cc406Sopenharmony_ciif test "$HAVE_LE32TOH" != "1"; then
205141cc406Sopenharmony_ci cat >> "$ac_byteorder" << EOF
206141cc406Sopenharmony_ci/* The byte swapping macros have the form: */
207141cc406Sopenharmony_ci/*   EENN[a]toh or htoEENN[a] where EE is be (big endian) or */
208141cc406Sopenharmony_ci/* le (little-endian), NN is 16 or 32 (number of bits) and a, */
209141cc406Sopenharmony_ci/* if present, indicates that the endian side is a pointer to an */
210141cc406Sopenharmony_ci/* array of uint8_t bytes instead of an integer of the specified length. */
211141cc406Sopenharmony_ci/* h refers to the host's ordering method. */
212141cc406Sopenharmony_ci
213141cc406Sopenharmony_ci/* So, to convert a 32-bit integer stored in a buffer in little-endian */
214141cc406Sopenharmony_ci/* format into a uint32_t usable on this machine, you could use: */
215141cc406Sopenharmony_ci/*   uint32_t value = le32atoh(&buf[3]); */
216141cc406Sopenharmony_ci/* To put that value back into the buffer, you could use: */
217141cc406Sopenharmony_ci/*   htole32a(&buf[3], value); */
218141cc406Sopenharmony_ci
219141cc406Sopenharmony_ci/* Define aliases for the standard byte swapping macros */
220141cc406Sopenharmony_ci/* Arguments to these macros must be properly aligned on natural word */
221141cc406Sopenharmony_ci/* boundaries in order to work properly on all architectures */
222141cc406Sopenharmony_ci#ifndef htobe16
223141cc406Sopenharmony_ci#define htobe16(x) htons(x)
224141cc406Sopenharmony_ci#endif
225141cc406Sopenharmony_ci#ifndef htobe32
226141cc406Sopenharmony_ci#define htobe32(x) htonl(x)
227141cc406Sopenharmony_ci#endif
228141cc406Sopenharmony_ci#ifndef be16toh
229141cc406Sopenharmony_ci#define be16toh(x) ntohs(x)
230141cc406Sopenharmony_ci#endif
231141cc406Sopenharmony_ci#ifndef be32toh
232141cc406Sopenharmony_ci#define be32toh(x) ntohl(x)
233141cc406Sopenharmony_ci#endif
234141cc406Sopenharmony_ci
235141cc406Sopenharmony_ci#define HTOBE16(x) (x) = htobe16(x)
236141cc406Sopenharmony_ci#define HTOBE32(x) (x) = htobe32(x)
237141cc406Sopenharmony_ci#define BE32TOH(x) (x) = be32toh(x)
238141cc406Sopenharmony_ci#define BE16TOH(x) (x) = be16toh(x)
239141cc406Sopenharmony_ci
240141cc406Sopenharmony_ciEOF
241141cc406Sopenharmony_ci
242141cc406Sopenharmony_ci if test $ac_cv_c_bigendian = yes; then
243141cc406Sopenharmony_ci  cat >> "$ac_byteorder" << EOF
244141cc406Sopenharmony_ci/* Define our own extended byte swapping macros for big-endian machines */
245141cc406Sopenharmony_ci#ifndef htole16
246141cc406Sopenharmony_ci#define htole16(x)      swap16(x)
247141cc406Sopenharmony_ci#endif
248141cc406Sopenharmony_ci#ifndef htole32
249141cc406Sopenharmony_ci#define htole32(x)      swap32(x)
250141cc406Sopenharmony_ci#endif
251141cc406Sopenharmony_ci#ifndef le16toh
252141cc406Sopenharmony_ci#define le16toh(x)      swap16(x)
253141cc406Sopenharmony_ci#endif
254141cc406Sopenharmony_ci#ifndef le32toh
255141cc406Sopenharmony_ci#define le32toh(x)      swap32(x)
256141cc406Sopenharmony_ci#endif
257141cc406Sopenharmony_ci
258141cc406Sopenharmony_ci#ifndef htobe64
259141cc406Sopenharmony_ci#define htobe64(x)      (x)
260141cc406Sopenharmony_ci#endif
261141cc406Sopenharmony_ci#ifndef be64toh
262141cc406Sopenharmony_ci#define be64toh(x)      (x)
263141cc406Sopenharmony_ci#endif
264141cc406Sopenharmony_ci
265141cc406Sopenharmony_ci#define HTOLE16(x)      (x) = htole16(x)
266141cc406Sopenharmony_ci#define HTOLE32(x)      (x) = htole32(x)
267141cc406Sopenharmony_ci#define LE16TOH(x)      (x) = le16toh(x)
268141cc406Sopenharmony_ci#define LE32TOH(x)      (x) = le32toh(x)
269141cc406Sopenharmony_ci
270141cc406Sopenharmony_ci#define HTOBE64(x)      (void) (x)
271141cc406Sopenharmony_ci#define BE64TOH(x)      (void) (x)
272141cc406Sopenharmony_ci
273141cc406Sopenharmony_ciEOF
274141cc406Sopenharmony_ci else
275141cc406Sopenharmony_ci  cat >> "$ac_byteorder" << EOF
276141cc406Sopenharmony_ci/* On little endian machines, these macros are null */
277141cc406Sopenharmony_ci#ifndef htole16
278141cc406Sopenharmony_ci#define htole16(x)      (x)
279141cc406Sopenharmony_ci#endif
280141cc406Sopenharmony_ci#ifndef htole32
281141cc406Sopenharmony_ci#define htole32(x)      (x)
282141cc406Sopenharmony_ci#endif
283141cc406Sopenharmony_ci#ifndef htole64
284141cc406Sopenharmony_ci#define htole64(x)      (x)
285141cc406Sopenharmony_ci#endif
286141cc406Sopenharmony_ci#ifndef le16toh
287141cc406Sopenharmony_ci#define le16toh(x)      (x)
288141cc406Sopenharmony_ci#endif
289141cc406Sopenharmony_ci#ifndef le32toh
290141cc406Sopenharmony_ci#define le32toh(x)      (x)
291141cc406Sopenharmony_ci#endif
292141cc406Sopenharmony_ci#ifndef le64toh
293141cc406Sopenharmony_ci#define le64toh(x)      (x)
294141cc406Sopenharmony_ci#endif
295141cc406Sopenharmony_ci
296141cc406Sopenharmony_ci#define HTOLE16(x)      (void) (x)
297141cc406Sopenharmony_ci#define HTOLE32(x)      (void) (x)
298141cc406Sopenharmony_ci#define HTOLE64(x)      (void) (x)
299141cc406Sopenharmony_ci#define LE16TOH(x)      (void) (x)
300141cc406Sopenharmony_ci#define LE32TOH(x)      (void) (x)
301141cc406Sopenharmony_ci#define LE64TOH(x)      (void) (x)
302141cc406Sopenharmony_ci
303141cc406Sopenharmony_ci/* These don't have standard aliases */
304141cc406Sopenharmony_ci#ifndef htobe64
305141cc406Sopenharmony_ci#define htobe64(x)      swap64(x)
306141cc406Sopenharmony_ci#endif
307141cc406Sopenharmony_ci#ifndef be64toh
308141cc406Sopenharmony_ci#define be64toh(x)      swap64(x)
309141cc406Sopenharmony_ci#endif
310141cc406Sopenharmony_ci
311141cc406Sopenharmony_ci#define HTOBE64(x)      (x) = htobe64(x)
312141cc406Sopenharmony_ci#define BE64TOH(x)      (x) = be64toh(x)
313141cc406Sopenharmony_ci
314141cc406Sopenharmony_ciEOF
315141cc406Sopenharmony_ci fi
316141cc406Sopenharmony_cifi
317141cc406Sopenharmony_ci
318141cc406Sopenharmony_cicat >> "$ac_byteorder" << EOF
319141cc406Sopenharmony_ci/* Define the C99 standard length-specific integer types */
320141cc406Sopenharmony_ci#include <_stdint.h>
321141cc406Sopenharmony_ci
322141cc406Sopenharmony_ciEOF
323141cc406Sopenharmony_ci
324141cc406Sopenharmony_cicase "${effective_target}" in
325141cc406Sopenharmony_ci i[3456]86-*)
326141cc406Sopenharmony_ci  cat >> "$ac_byteorder" << EOF
327141cc406Sopenharmony_ci/* Here are some macros to create integers from a byte array */
328141cc406Sopenharmony_ci/* These are used to get and put integers from/into a uint8_t array */
329141cc406Sopenharmony_ci/* with a specific endianness.  This is the most portable way to generate */
330141cc406Sopenharmony_ci/* and read messages to a network or serial device.  Each member of a */
331141cc406Sopenharmony_ci/* packet structure must be handled separately. */
332141cc406Sopenharmony_ci
333141cc406Sopenharmony_ci/* The i386 and compatibles can handle unaligned memory access, */
334141cc406Sopenharmony_ci/* so use the optimized macros above to do this job */
335141cc406Sopenharmony_ci#define be16atoh(x)     be16toh(*(uint16_t*)(x))
336141cc406Sopenharmony_ci#define be32atoh(x)     be32toh(*(uint32_t*)(x))
337141cc406Sopenharmony_ci#define be64atoh(x)     be64toh(*(uint64_t*)(x))
338141cc406Sopenharmony_ci#define le16atoh(x)     le16toh(*(uint16_t*)(x))
339141cc406Sopenharmony_ci#define le32atoh(x)     le32toh(*(uint32_t*)(x))
340141cc406Sopenharmony_ci#define le64atoh(x)     le64toh(*(uint64_t*)(x))
341141cc406Sopenharmony_ci
342141cc406Sopenharmony_ci#define htobe16a(a,x)   *(uint16_t*)(a) = htobe16(x)
343141cc406Sopenharmony_ci#define htobe32a(a,x)   *(uint32_t*)(a) = htobe32(x)
344141cc406Sopenharmony_ci#define htobe64a(a,x)   *(uint64_t*)(a) = htobe64(x)
345141cc406Sopenharmony_ci#define htole16a(a,x)   *(uint16_t*)(a) = htole16(x)
346141cc406Sopenharmony_ci#define htole32a(a,x)   *(uint32_t*)(a) = htole32(x)
347141cc406Sopenharmony_ci#define htole64a(a,x)   *(uint64_t*)(a) = htole64(x)
348141cc406Sopenharmony_ci
349141cc406Sopenharmony_ciEOF
350141cc406Sopenharmony_ci  ;;
351141cc406Sopenharmony_ci
352141cc406Sopenharmony_ci *)
353141cc406Sopenharmony_ci  cat >> "$ac_byteorder" << EOF
354141cc406Sopenharmony_ci/* Here are some macros to create integers from a byte array */
355141cc406Sopenharmony_ci/* These are used to get and put integers from/into a uint8_t array */
356141cc406Sopenharmony_ci/* with a specific endianness.  This is the most portable way to generate */
357141cc406Sopenharmony_ci/* and read messages to a network or serial device.  Each member of a */
358141cc406Sopenharmony_ci/* packet structure must be handled separately. */
359141cc406Sopenharmony_ci
360141cc406Sopenharmony_ci/* Non-optimized but portable macros */
361141cc406Sopenharmony_ci#define be16atoh(x)     ((uint16_t)(((x)[0]<<8)|(x)[1]))
362141cc406Sopenharmony_ci#define be32atoh(x)     ((uint32_t)(((x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3]))
363141cc406Sopenharmony_ci#define be64atoh(x)     ((uint64_t)(((x)[0]<<56)|((x)[1]<<48)|((x)[2]<<40)| \\
364141cc406Sopenharmony_ci        ((x)[3]<<32)|((x)[4]<<24)|((x)[5]<<16)|((x)[6]<<8)|(x)[7]))
365141cc406Sopenharmony_ci#define le16atoh(x)     ((uint16_t)(((x)[1]<<8)|(x)[0]))
366141cc406Sopenharmony_ci#define le32atoh(x)     ((uint32_t)(((x)[3]<<24)|((x)[2]<<16)|((x)[1]<<8)|(x)[0]))
367141cc406Sopenharmony_ci#define le64atoh(x)     ((uint64_t)(((x)[7]<<56)|((x)[6]<<48)|((x)[5]<<40)| \\
368141cc406Sopenharmony_ci        ((x)[4]<<32)|((x)[3]<<24)|((x)[2]<<16)|((x)[1]<<8)|(x)[0]))
369141cc406Sopenharmony_ci
370141cc406Sopenharmony_ci#define htobe16a(a,x)   (a)[0]=(uint8_t)((x)>>8), (a)[1]=(uint8_t)(x)
371141cc406Sopenharmony_ci#define htobe32a(a,x)   (a)[0]=(uint8_t)((x)>>24), (a)[1]=(uint8_t)((x)>>16), \\
372141cc406Sopenharmony_ci        (a)[2]=(uint8_t)((x)>>8), (a)[3]=(uint8_t)(x)
373141cc406Sopenharmony_ci#define htobe64a(a,x)   (a)[0]=(uint8_t)((x)>>56), (a)[1]=(uint8_t)((x)>>48), \\
374141cc406Sopenharmony_ci        (a)[2]=(uint8_t)((x)>>40), (a)[3]=(uint8_t)((x)>>32), \\
375141cc406Sopenharmony_ci        (a)[4]=(uint8_t)((x)>>24), (a)[5]=(uint8_t)((x)>>16), \\
376141cc406Sopenharmony_ci        (a)[6]=(uint8_t)((x)>>8), (a)[7]=(uint8_t)(x)
377141cc406Sopenharmony_ci#define htole16a(a,x)   (a)[1]=(uint8_t)((x)>>8), (a)[0]=(uint8_t)(x)
378141cc406Sopenharmony_ci#define htole32a(a,x)   (a)[3]=(uint8_t)((x)>>24), (a)[2]=(uint8_t)((x)>>16), \\
379141cc406Sopenharmony_ci        (a)[1]=(uint8_t)((x)>>8), (a)[0]=(uint8_t)(x)
380141cc406Sopenharmony_ci#define htole64a(a,x)   (a)[7]=(uint8_t)((x)>>56), (a)[6]=(uint8_t)((x)>>48), \\
381141cc406Sopenharmony_ci        (a)[5]=(uint8_t)((x)>>40), (a)[4]=(uint8_t)((x)>>32), \\
382141cc406Sopenharmony_ci        (a)[3]=(uint8_t)((x)>>24), (a)[2]=(uint8_t)((x)>>16), \\
383141cc406Sopenharmony_ci        (a)[1]=(uint8_t)((x)>>8), (a)[0]=(uint8_t)(x)
384141cc406Sopenharmony_ci
385141cc406Sopenharmony_ciEOF
386141cc406Sopenharmony_ci  ;;
387141cc406Sopenharmony_ciesac
388141cc406Sopenharmony_ci]
389141cc406Sopenharmony_ci
390141cc406Sopenharmony_cicat >> "$ac_byteorder" << EOF
391141cc406Sopenharmony_ci#endif /*__BYTEORDER_H*/
392141cc406Sopenharmony_ciEOF
393141cc406Sopenharmony_ci
394141cc406Sopenharmony_ciif cmp -s $ac_byteorder_h $ac_byteorder 2>/dev/null; then
395141cc406Sopenharmony_ci  AC_MSG_NOTICE([$ac_byteorder_h is unchanged])
396141cc406Sopenharmony_ci  rm $ac_byteorder
397141cc406Sopenharmony_cielse
398141cc406Sopenharmony_ci  rm -f $ac_byteorder_h
399141cc406Sopenharmony_ci  mv $ac_byteorder $ac_byteorder_h
400141cc406Sopenharmony_cifi
401141cc406Sopenharmony_ci])
402