1 /*
2  * MessagePack system dependencies
3  *
4  * Copyright (C) 2008-2010 FURUHASHI Sadayuki
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18 #ifndef MSGPACK_SYSDEP_H__
19 #define MSGPACK_SYSDEP_H__
20 
21 #include <stdlib.h>
22 #include <stddef.h>
23 #if defined(_MSC_VER) && _MSC_VER < 1600
24 typedef __int8 int8_t;
25 typedef unsigned __int8 uint8_t;
26 typedef __int16 int16_t;
27 typedef unsigned __int16 uint16_t;
28 typedef __int32 int32_t;
29 typedef unsigned __int32 uint32_t;
30 typedef __int64 int64_t;
31 typedef unsigned __int64 uint64_t;
32 #elif defined(_MSC_VER)  // && _MSC_VER >= 1600
33 #include <stdint.h>
34 #else
35 #include <stdint.h>
36 #include <stdbool.h>
37 #endif
38 
39 #ifdef _WIN32
40 #define _msgpack_atomic_counter_header <windows.h>
41 typedef long _msgpack_atomic_counter_t;
42 #define _msgpack_sync_decr_and_fetch(ptr) InterlockedDecrement(ptr)
43 #define _msgpack_sync_incr_and_fetch(ptr) InterlockedIncrement(ptr)
44 #elif defined(__GNUC__) && ((__GNUC__*10 + __GNUC_MINOR__) < 41)
45 #define _msgpack_atomic_counter_header "gcc_atomic.h"
46 #else
47 typedef unsigned int _msgpack_atomic_counter_t;
48 #define _msgpack_sync_decr_and_fetch(ptr) __sync_sub_and_fetch(ptr, 1)
49 #define _msgpack_sync_incr_and_fetch(ptr) __sync_add_and_fetch(ptr, 1)
50 #endif
51 
52 #ifdef _WIN32
53 
54 #ifdef __cplusplus
55 /* numeric_limits<T>::min,max */
56 #ifdef max
57 #undef max
58 #endif
59 #ifdef min
60 #undef min
61 #endif
62 #endif
63 
64 #else
65 #include <arpa/inet.h>  /* __BYTE_ORDER */
66 #endif
67 
68 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
69 #if __BYTE_ORDER == __LITTLE_ENDIAN
70 #define __LITTLE_ENDIAN__
71 #elif __BYTE_ORDER == __BIG_ENDIAN
72 #define __BIG_ENDIAN__
73 #elif _WIN32
74 #define __LITTLE_ENDIAN__
75 #endif
76 #endif
77 
78 
79 #ifdef __LITTLE_ENDIAN__
80 
81 #ifdef _WIN32
82 #  if defined(ntohs)
83 #    define _msgpack_be16(x) ntohs(x)
84 #  elif defined(_byteswap_ushort) || (defined(_MSC_VER) && _MSC_VER >= 1400)
85 #    define _msgpack_be16(x) ((uint16_t)_byteswap_ushort((unsigned short)x))
86 #  else
87 #    define _msgpack_be16(x) ( \
88         ((((uint16_t)x) <<  8) ) | \
89         ((((uint16_t)x) >>  8) ) )
90 #  endif
91 #else
92 #  define _msgpack_be16(x) ntohs(x)
93 #endif
94 
95 #ifdef _WIN32
96 #  if defined(ntohl)
97 #    define _msgpack_be32(x) ntohl(x)
98 #  elif defined(_byteswap_ulong) || (defined(_MSC_VER) && _MSC_VER >= 1400)
99 #    define _msgpack_be32(x) ((uint32_t)_byteswap_ulong((unsigned long)x))
100 #  else
101 #    define _msgpack_be32(x) \
102         ( ((((uint32_t)x) << 24)               ) | \
103           ((((uint32_t)x) <<  8) & 0x00ff0000U ) | \
104           ((((uint32_t)x) >>  8) & 0x0000ff00U ) | \
105           ((((uint32_t)x) >> 24)               ) )
106 #  endif
107 #else
108 #  define _msgpack_be32(x) ntohl(x)
109 #endif
110 
111 #if defined(_byteswap_uint64) || (defined(_MSC_VER) && _MSC_VER >= 1400)
112 #  define _msgpack_be64(x) (_byteswap_uint64(x))
113 #elif defined(bswap_64)
114 #  define _msgpack_be64(x) bswap_64(x)
115 #elif defined(__DARWIN_OSSwapInt64)
116 #  define _msgpack_be64(x) __DARWIN_OSSwapInt64(x)
117 #else
118 #define _msgpack_be64(x) \
119     ( ((((uint64_t)x) << 56)                         ) | \
120       ((((uint64_t)x) << 40) & 0x00ff000000000000ULL ) | \
121       ((((uint64_t)x) << 24) & 0x0000ff0000000000ULL ) | \
122       ((((uint64_t)x) <<  8) & 0x000000ff00000000ULL ) | \
123       ((((uint64_t)x) >>  8) & 0x00000000ff000000ULL ) | \
124       ((((uint64_t)x) >> 24) & 0x0000000000ff0000ULL ) | \
125       ((((uint64_t)x) >> 40) & 0x000000000000ff00ULL ) | \
126       ((((uint64_t)x) >> 56)                         ) )
127 #endif
128 
129 #define _msgpack_load16(cast, from) ((cast)( \
130         (((uint16_t)((uint8_t*)(from))[0]) << 8) | \
131         (((uint16_t)((uint8_t*)(from))[1])     ) ))
132 
133 #define _msgpack_load32(cast, from) ((cast)( \
134         (((uint32_t)((uint8_t*)(from))[0]) << 24) | \
135         (((uint32_t)((uint8_t*)(from))[1]) << 16) | \
136         (((uint32_t)((uint8_t*)(from))[2]) <<  8) | \
137         (((uint32_t)((uint8_t*)(from))[3])      ) ))
138 
139 #define _msgpack_load64(cast, from) ((cast)( \
140         (((uint64_t)((uint8_t*)(from))[0]) << 56) | \
141         (((uint64_t)((uint8_t*)(from))[1]) << 48) | \
142         (((uint64_t)((uint8_t*)(from))[2]) << 40) | \
143         (((uint64_t)((uint8_t*)(from))[3]) << 32) | \
144         (((uint64_t)((uint8_t*)(from))[4]) << 24) | \
145         (((uint64_t)((uint8_t*)(from))[5]) << 16) | \
146         (((uint64_t)((uint8_t*)(from))[6]) << 8)  | \
147         (((uint64_t)((uint8_t*)(from))[7])     )  ))
148 
149 #else
150 
151 #define _msgpack_be16(x) (x)
152 #define _msgpack_be32(x) (x)
153 #define _msgpack_be64(x) (x)
154 
155 #define _msgpack_load16(cast, from) ((cast)( \
156         (((uint16_t)((uint8_t*)from)[0]) << 8) | \
157         (((uint16_t)((uint8_t*)from)[1])     ) ))
158 
159 #define _msgpack_load32(cast, from) ((cast)( \
160         (((uint32_t)((uint8_t*)from)[0]) << 24) | \
161         (((uint32_t)((uint8_t*)from)[1]) << 16) | \
162         (((uint32_t)((uint8_t*)from)[2]) <<  8) | \
163         (((uint32_t)((uint8_t*)from)[3])      ) ))
164 
165 #define _msgpack_load64(cast, from) ((cast)( \
166         (((uint64_t)((uint8_t*)from)[0]) << 56) | \
167         (((uint64_t)((uint8_t*)from)[1]) << 48) | \
168         (((uint64_t)((uint8_t*)from)[2]) << 40) | \
169         (((uint64_t)((uint8_t*)from)[3]) << 32) | \
170         (((uint64_t)((uint8_t*)from)[4]) << 24) | \
171         (((uint64_t)((uint8_t*)from)[5]) << 16) | \
172         (((uint64_t)((uint8_t*)from)[6]) << 8)  | \
173         (((uint64_t)((uint8_t*)from)[7])     )  ))
174 #endif
175 
176 
177 #define _msgpack_store16(to, num) \
178     do { uint16_t val = _msgpack_be16(num); memcpy(to, &val, 2); } while(0)
179 #define _msgpack_store32(to, num) \
180     do { uint32_t val = _msgpack_be32(num); memcpy(to, &val, 4); } while(0)
181 #define _msgpack_store64(to, num) \
182     do { uint64_t val = _msgpack_be64(num); memcpy(to, &val, 8); } while(0)
183 
184 /*
185 #define _msgpack_load16(cast, from) \
186     ({ cast val; memcpy(&val, (char*)from, 2); _msgpack_be16(val); })
187 #define _msgpack_load32(cast, from) \
188     ({ cast val; memcpy(&val, (char*)from, 4); _msgpack_be32(val); })
189 #define _msgpack_load64(cast, from) \
190     ({ cast val; memcpy(&val, (char*)from, 8); _msgpack_be64(val); })
191 */
192 
193 
194 #endif /* msgpack/sysdep.h */
195