/
bswap_c.in
75 lines (70 loc) · 1.92 KB
/
bswap_c.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
Copyright (C) 2012, Parrot Foundation.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#ifdef PARROT_HAS_HEADER_BYTESWAP
# include <byteswap.h> /* GNU */
# ifndef bswap_16
# define bswap_16(x) __bswap_16(x)
# define bswap_32(x) __bswap_32(x)
# define bswap_64(x) __bswap_64(x)
# endif
#elif defined(PARROT_HAS_HEADER_ENDIAN) \
|| defined(PARROT_HAS_HEADER_SYS_ENDIAN)
# ifdef PARROT_HAS_HEADER_ENDIAN /* linux */
# include <endian.h>
# elif defined(PARROT_HAS_HEADER_SYS_ENDIAN) /* FreeBSD */
# include <sys/endian.h>
# endif
# define bswap_16(x) __bswap_16(x)
# define bswap_32(x) __bswap_32(x)
# ifdef __bswap_64
# define bswap_64(x) __bswap_64(x)
# endif
#elif defined(PARROT_HAS_HEADER_LIBKERN_OSBYTEORDER) && defined(__APPLE__)
# include <libkern/OSByteOrder.h>
# define bswap_16(x) OSSwapInt16(x)
# define bswap_32(x) OSSwapInt32(x)
# ifdef OSSwapInt64
# define bswap_64(x) OSSwapInt64(x)
# endif
#elif defined(PARROT_HAS_HEADER_MACHINE_ENDIAN) && defined(__APPLE__)
# include <machine/endian.h>
# define bswap_16(x) __DARWIN_OSSwapInt16(x)
# define bswap_32(x) __DARWIN_OSSwapInt32(x)
# ifdef __DARWIN_OSSwapInt64
# define bswap_64(x) __DARWIN_OSSwapInt64(x)
# endif
#elif defined(PARROT_HAS_HEADER_SYS_BYTEORDER)
# define bswap_16(x) BSWAP_16(x)
# define bswap_32(x) BSWAP_32(x)
# ifdef BSWAP_64
# define bswap_64(x) BSWAP_64(x)
# endif
#elif defined(__MSC_VER)
# define bswap_16(x) _byteswap_ushort(x)
# define bswap_32(x) _byteswap_ulong(x)
# ifdef _byteswap_uint64
# define bswap_64(x) _byteswap_uint64(x)
# endif
#endif
int
main()
{
unsigned long l = 0x12345678;
l = bswap_16((short)l);
l = bswap_32(l);
#if __WORDSIZE == 64
l = bswap_64(l);
#endif
printf("0x%lx", l);
return EXIT_SUCCESS;
}
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/