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