cannam@89
|
1 /*
|
cannam@89
|
2 * Copyright (C) 2004 Steve Harris
|
cannam@89
|
3 *
|
cannam@89
|
4 * This program is free software; you can redistribute it and/or
|
cannam@89
|
5 * modify it under the terms of the GNU Lesser General Public License
|
cannam@89
|
6 * as published by the Free Software Foundation; either version 2.1
|
cannam@89
|
7 * of the License, or (at your option) any later version.
|
cannam@89
|
8 *
|
cannam@89
|
9 * This program is distributed in the hope that it will be useful,
|
cannam@89
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
cannam@89
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
cannam@89
|
12 * GNU Lesser General Public License for more details.
|
cannam@89
|
13 *
|
cannam@89
|
14 * $Id$
|
cannam@89
|
15 */
|
cannam@89
|
16
|
cannam@89
|
17 #ifndef LO_ENDIAN_H
|
cannam@89
|
18 #define LO_ENDIAN_H
|
cannam@89
|
19
|
cannam@89
|
20 #include <sys/types.h>
|
cannam@89
|
21
|
cannam@89
|
22 #ifdef _MSC_VER
|
cannam@89
|
23 #define inline __inline
|
cannam@89
|
24 #define uint64_t unsigned __int64
|
cannam@89
|
25 #define uint32_t unsigned __int32
|
cannam@89
|
26 #else
|
cannam@89
|
27 #include <stdint.h>
|
cannam@89
|
28 #endif
|
cannam@89
|
29
|
cannam@89
|
30 #ifdef WIN32
|
cannam@89
|
31 #include <winsock2.h>
|
cannam@89
|
32 #include <ws2tcpip.h>
|
cannam@89
|
33 #else
|
cannam@89
|
34 #include <netinet/in.h>
|
cannam@89
|
35 #endif
|
cannam@89
|
36
|
cannam@89
|
37 #ifdef __cplusplus
|
cannam@89
|
38 extern "C" {
|
cannam@89
|
39 #endif
|
cannam@89
|
40
|
cannam@89
|
41 #define lo_swap16(x) htons(x)
|
cannam@89
|
42
|
cannam@89
|
43 #define lo_swap32(x) htonl(x)
|
cannam@89
|
44
|
cannam@89
|
45 /* These macros come from the Linux kernel */
|
cannam@89
|
46
|
cannam@89
|
47 #ifndef lo_swap16
|
cannam@89
|
48 #define lo_swap16(x) \
|
cannam@89
|
49 ({ \
|
cannam@89
|
50 uint16_t __x = (x); \
|
cannam@89
|
51 ((uint16_t)( \
|
cannam@89
|
52 (((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \
|
cannam@89
|
53 (((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \
|
cannam@89
|
54 })
|
cannam@89
|
55 #warning USING UNOPTIMISED ENDIAN STUFF
|
cannam@89
|
56 #endif
|
cannam@89
|
57
|
cannam@89
|
58 #ifndef lo_swap32
|
cannam@89
|
59 #define lo_swap32(x) \
|
cannam@89
|
60 ({ \
|
cannam@89
|
61 uint32_t __x = (x); \
|
cannam@89
|
62 ((uint32_t)( \
|
cannam@89
|
63 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
|
cannam@89
|
64 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
|
cannam@89
|
65 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
|
cannam@89
|
66 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \
|
cannam@89
|
67 })
|
cannam@89
|
68 #endif
|
cannam@89
|
69
|
cannam@89
|
70 #if 0
|
cannam@89
|
71 #ifndef lo_swap64
|
cannam@89
|
72 #define lo_swap64(x) \
|
cannam@89
|
73 ({ \
|
cannam@89
|
74 uint64_t __x = (x); \
|
cannam@89
|
75 ((uint64_t)( \
|
cannam@89
|
76 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \
|
cannam@89
|
77 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
|
cannam@89
|
78 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
|
cannam@89
|
79 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
|
cannam@89
|
80 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
|
cannam@89
|
81 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
|
cannam@89
|
82 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
|
cannam@89
|
83 (uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \
|
cannam@89
|
84 })
|
cannam@89
|
85 #endif
|
cannam@89
|
86 #else
|
cannam@89
|
87
|
cannam@89
|
88 typedef union {
|
cannam@89
|
89 uint64_t all;
|
cannam@89
|
90 struct {
|
cannam@89
|
91 uint32_t a;
|
cannam@89
|
92 uint32_t b;
|
cannam@89
|
93 } part;
|
cannam@89
|
94 } lo_split64;
|
cannam@89
|
95
|
cannam@89
|
96 static inline uint64_t lo_swap64(uint64_t x)
|
cannam@89
|
97 {
|
cannam@89
|
98 lo_split64 in, out;
|
cannam@89
|
99
|
cannam@89
|
100 in.all = x;
|
cannam@89
|
101 out.part.a = lo_swap32(in.part.b);
|
cannam@89
|
102 out.part.b = lo_swap32(in.part.a);
|
cannam@89
|
103
|
cannam@89
|
104 return out.all;
|
cannam@89
|
105 }
|
cannam@89
|
106 #endif
|
cannam@89
|
107
|
cannam@89
|
108 /* Host to OSC and OSC to Host conversion macros */
|
cannam@89
|
109
|
cannam@89
|
110 #if 0
|
cannam@89
|
111 #define lo_htoo16(x) (x)
|
cannam@89
|
112 #define lo_htoo32(x) (x)
|
cannam@89
|
113 #define lo_htoo64(x) (x)
|
cannam@89
|
114 #define lo_otoh16(x) (x)
|
cannam@89
|
115 #define lo_otoh32(x) (x)
|
cannam@89
|
116 #define lo_otoh64(x) (x)
|
cannam@89
|
117 #else
|
cannam@89
|
118 #define lo_htoo16 lo_swap16
|
cannam@89
|
119 #define lo_htoo32 lo_swap32
|
cannam@89
|
120 #define lo_htoo64 lo_swap64
|
cannam@89
|
121 #define lo_otoh16 lo_swap16
|
cannam@89
|
122 #define lo_otoh32 lo_swap32
|
cannam@89
|
123 #define lo_otoh64 lo_swap64
|
cannam@89
|
124 #endif
|
cannam@89
|
125
|
cannam@89
|
126 #ifdef __cplusplus
|
cannam@89
|
127 }
|
cannam@89
|
128 #endif
|
cannam@89
|
129
|
cannam@89
|
130 #ifdef _MSC_VER
|
cannam@89
|
131 #undef inline
|
cannam@89
|
132 #undef uint64_t
|
cannam@89
|
133 #undef uint32_t
|
cannam@89
|
134 #endif
|
cannam@89
|
135
|
cannam@89
|
136 #endif
|
cannam@89
|
137
|
cannam@89
|
138 /* vi:set ts=8 sts=4 sw=4: */
|