Debian Codegraph @

src/core/lo/lo_endian.h

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