OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 The Native Client Authors. All rights reserved. | 2 * Copyright 2014 The Native Client Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 #ifndef GLIBCEMU_MACHINE_ENDIAN_H | 6 #ifndef GLIBCEMU_MACHINE_ENDIAN_H |
7 #define GLIBCEMU_MACHINE_ENDIAN_H 1 | 7 #define GLIBCEMU_MACHINE_ENDIAN_H 1 |
8 | 8 |
9 #include_next <machine/endian.h> | 9 #include_next <machine/endian.h> |
10 | 10 |
11 #include <_ansi.h> | 11 #include <_ansi.h> |
12 | 12 |
13 #define __byte_swap_constant_16(x) \ | 13 #define __byte_swap_constant_16(x) \ |
14 ( (((unsigned short)(x) & 0xff) << 8) | \ | 14 ( (((unsigned short)(x) & 0xff) << 8) | \ |
15 ((unsigned short)(x) >> 8) ) | 15 ((unsigned short)(x) >> 8) ) |
16 | 16 |
17 #define __byte_swap_constant_32(x) \ | 17 #define __byte_swap_constant_32(x) \ |
18 ( (((unsigned int)(x) & 0xff) << 24) | \ | 18 ( (((unsigned int)(x) & 0xff) << 24) | \ |
19 (((unsigned int)(x) << 8) & 0x00ff0000) | \ | 19 (((unsigned int)(x) << 8) & 0x00ff0000) | \ |
20 (((unsigned int)(x) >> 8) & 0x0000ff00) | \ | 20 (((unsigned int)(x) >> 8) & 0x0000ff00) | \ |
21 ((unsigned int)(x) >> 24) ) | 21 ((unsigned int)(x) >> 24) ) |
22 | 22 |
23 | 23 |
24 _ELIDABLE_INLINE unsigned short int | 24 _ELIDABLE_INLINE unsigned short int |
25 __inline_bswap_16 (unsigned short int x) | 25 __inline_bswap_16 (unsigned short int x) |
26 { | 26 { |
27 return __byte_swap_constant_16(x); | 27 return __byte_swap_constant_16(x); |
28 } | 28 } |
29 | 29 |
30 _ELIDABLE_INLINE unsigned long int | |
31 __inline_bswap_32 (unsigned long int x) | |
32 { | |
33 return __byte_swap_constant_32(x); | |
34 } | |
35 | |
36 #define __byte_swap_16(x) \ | 30 #define __byte_swap_16(x) \ |
37 (__builtin_constant_p(x) ? __byte_swap_constant_16(x) : __inline_bswap_16(x)) | 31 (__builtin_constant_p(x) ? __byte_swap_constant_16(x) : __inline_bswap_16(x)) |
38 | 32 |
39 #define __byte_swap_32(x) \ | 33 #define __byte_swap_32(x) \ |
40 (__builtin_constant_p(x) ? __byte_swap_constant_32(x) : __inline_bswap_32(x)) | 34 (__builtin_constant_p(x) ? __byte_swap_constant_32(x) : __builtin_bswap32(x)) |
41 | 35 |
42 #define ntohs(x) (unsigned int)__byte_swap_16(x) | 36 #define ntohs(x) (unsigned int)__byte_swap_16(x) |
43 #define ntohl(x) (unsigned int)__byte_swap_32(x) | 37 #define ntohl(x) (unsigned int)__byte_swap_32(x) |
44 #define htons(x) (unsigned int)__byte_swap_16(x) | 38 #define htons(x) (unsigned int)__byte_swap_16(x) |
45 #define htonl(x) (unsigned int)__byte_swap_32(x) | 39 #define htonl(x) (unsigned int)__byte_swap_32(x) |
46 | 40 |
47 #endif | 41 #endif |
OLD | NEW |