1 |
< |
/* $MidnightBSD$ */ |
1 |
> |
/* $MidnightBSD: src/sys/i386/include/endian.h,v 1.3 2012/03/31 17:05:09 laffer1 Exp $ */ |
2 |
|
/*- |
3 |
|
* Copyright (c) 1987, 1991 Regents of the University of California. |
4 |
|
* All rights reserved. |
28 |
|
* SUCH DAMAGE. |
29 |
|
* |
30 |
|
* @(#)endian.h 7.8 (Berkeley) 4/3/91 |
31 |
< |
* $FreeBSD: src/sys/i386/include/endian.h,v 1.41 2005/03/02 21:33:26 joerg Exp $ |
31 |
> |
* $FreeBSD$ |
32 |
|
*/ |
33 |
|
|
34 |
|
#ifndef _MACHINE_ENDIAN_H_ |
70 |
|
|
71 |
|
#if defined(__GNUCLIKE_ASM) && defined(__GNUCLIKE_BUILTIN_CONSTANT_P) |
72 |
|
|
73 |
< |
#define __byte_swap_int_var(x) \ |
74 |
< |
__extension__ ({ register __uint32_t __X = (x); \ |
75 |
< |
__asm ("bswap %0" : "+r" (__X)); \ |
76 |
< |
__X; }) |
73 |
> |
#define __bswap64_const(_x) \ |
74 |
> |
(((_x) >> 56) | \ |
75 |
> |
(((_x) >> 40) & (0xffULL << 8)) | \ |
76 |
> |
(((_x) >> 24) & (0xffULL << 16)) | \ |
77 |
> |
(((_x) >> 8) & (0xffULL << 24)) | \ |
78 |
> |
(((_x) << 8) & (0xffULL << 32)) | \ |
79 |
> |
(((_x) << 24) & (0xffULL << 40)) | \ |
80 |
> |
(((_x) << 40) & (0xffULL << 48)) | \ |
81 |
> |
((_x) << 56)) |
82 |
|
|
83 |
< |
#ifdef __OPTIMIZE__ |
83 |
> |
#define __bswap32_const(_x) \ |
84 |
> |
(((_x) >> 24) | \ |
85 |
> |
(((_x) & (0xff << 16)) >> 8) | \ |
86 |
> |
(((_x) & (0xff << 8)) << 8) | \ |
87 |
> |
((_x) << 24)) |
88 |
|
|
89 |
< |
#define __byte_swap_int_const(x) \ |
81 |
< |
((((x) & 0xff000000) >> 24) | \ |
82 |
< |
(((x) & 0x00ff0000) >> 8) | \ |
83 |
< |
(((x) & 0x0000ff00) << 8) | \ |
84 |
< |
(((x) & 0x000000ff) << 24)) |
85 |
< |
#define __byte_swap_int(x) (__builtin_constant_p(x) ? \ |
86 |
< |
__byte_swap_int_const(x) : __byte_swap_int_var(x)) |
89 |
> |
#define __bswap16_const(_x) (__uint16_t)((_x) << 8 | (_x) >> 8) |
90 |
|
|
88 |
– |
#else /* __OPTIMIZE__ */ |
89 |
– |
|
90 |
– |
#define __byte_swap_int(x) __byte_swap_int_var(x) |
91 |
– |
|
92 |
– |
#endif /* __OPTIMIZE__ */ |
93 |
– |
|
91 |
|
static __inline __uint64_t |
92 |
< |
__bswap64(__uint64_t _x) |
92 |
> |
__bswap64_var(__uint64_t __x) |
93 |
|
{ |
94 |
|
|
95 |
< |
return ((_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) | |
99 |
< |
((_x >> 8) & 0xff000000) | ((_x << 8) & ((__uint64_t)0xff << 32)) | |
100 |
< |
((_x << 24) & ((__uint64_t)0xff << 40)) | |
101 |
< |
((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56))); |
95 |
> |
return __bswap64_const(__x); |
96 |
|
} |
97 |
|
|
98 |
+ |
|
99 |
|
static __inline __uint32_t |
100 |
< |
__bswap32(__uint32_t _x) |
100 |
> |
__bswap32_var(__uint32_t _x) |
101 |
|
{ |
102 |
|
|
103 |
< |
return (__byte_swap_int(_x)); |
103 |
> |
__asm ("bswap %0" : "+r" (_x)); |
104 |
> |
return (_x); |
105 |
|
} |
106 |
|
|
107 |
|
static __inline __uint16_t |
108 |
< |
__bswap16(__uint16_t _x) |
108 |
> |
__bswap16_var(__uint16_t _x) |
109 |
|
{ |
110 |
< |
return (_x << 8 | _x >> 8); |
110 |
> |
|
111 |
> |
return (__bswap16_const(_x)); |
112 |
|
} |
113 |
+ |
|
114 |
+ |
#define __bswap64(_x) \ |
115 |
+ |
(__builtin_constant_p(_x) ? \ |
116 |
+ |
__bswap64_const((__uint64_t)(_x)) : __bswap64_var(_x)) |
117 |
+ |
|
118 |
+ |
#define __bswap32(_x) \ |
119 |
+ |
(__builtin_constant_p(_x) ? \ |
120 |
+ |
__bswap32_const((__uint32_t)(_x)) : __bswap32_var(_x)) |
121 |
+ |
|
122 |
+ |
#define __bswap16(_x) \ |
123 |
+ |
((__uint16_t)(__builtin_constant_p(_x) ? \ |
124 |
+ |
__bswap16_const((__uint16_t)(_x)) : __bswap16_var(_x))) |
125 |
|
|
126 |
|
#define __htonl(x) __bswap32(x) |
127 |
|
#define __htons(x) __bswap16(x) |