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