CSL  5.2
lo_endian.h
Go to the documentation of this file.
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 #include <stdint.h>
22 
23 #ifdef WIN32
24 #include <winsock2.h>
25 #include <ws2tcpip.h>
26 #else
27 #include <netinet/in.h>
28 #endif
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #define lo_swap16(x) htons(x)
35 
36 #define lo_swap32(x) htonl(x)
37 
38 /* These macros come from the Linux kernel */
39 
40 #ifndef lo_swap16
41 #define lo_swap16(x) \
42 ({ \
43  uint16_t __x = (x); \
44  ((uint16_t)( \
45  (((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \
46  (((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \
47 })
48 #warning USING UNOPTIMISED ENDIAN STUFF
49 #endif
50 
51 #ifndef lo_swap32
52 #define lo_swap32(x) \
53 ({ \
54  uint32_t __x = (x); \
55  ((uint32_t)( \
56  (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
57  (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
58  (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
59  (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \
60 })
61 #endif
62 
63 #if 0
64 #ifndef lo_swap64
65 #define lo_swap64(x) \
66 ({ \
67  uint64_t __x = (x); \
68  ((uint64_t)( \
69  (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \
70  (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
71  (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
72  (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
73  (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
74  (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
75  (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
76  (uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \
77 })
78 #endif
79 #else
80 
81 typedef union {
82  uint64_t all;
83  struct {
84  uint32_t a;
85  uint32_t b;
86  } part;
87 } lo_split64;
88 
89 static inline uint64_t lo_swap64(uint64_t x)
90 {
91  lo_split64 in, out;
92 
93  in.all = x;
94  out.part.a = lo_swap32(in.part.b);
95  out.part.b = lo_swap32(in.part.a);
96 
97  return out.all;
98 }
99 #endif
100 
101 /* Host to OSC and OSC to Host conversion macros */
102 
103 #if 0
104 #define lo_htoo16(x) (x)
105 #define lo_htoo32(x) (x)
106 #define lo_htoo64(x) (x)
107 #define lo_otoh16(x) (x)
108 #define lo_otoh32(x) (x)
109 #define lo_otoh64(x) (x)
110 #else
111 #define lo_htoo16 lo_swap16
112 #define lo_htoo32 lo_swap32
113 #define lo_htoo64 lo_swap64
114 #define lo_otoh16 lo_swap16
115 #define lo_otoh32 lo_swap32
116 #define lo_otoh64 lo_swap64
117 #endif
118 
119 #ifdef __cplusplus
120 }
121 #endif
122 
123 #endif
124 
125 /* vi:set ts=8 sts=4 sw=4: */