diff options
author | Elliott Hughes | 2014-12-08 22:32:11 -0600 |
---|---|---|
committer | Elliott Hughes | 2014-12-08 22:35:08 -0600 |
commit | 56e017306eb55c5deea795f89d2fb657757b4164 (patch) | |
tree | 1e5fb8ca5c536803b35d3de3bca638282ecb60f3 | |
parent | aa0002de89ee4753bce6e4d714e5d4cc4d9709eb (diff) | |
download | platform-bionic-56e017306eb55c5deea795f89d2fb657757b4164.tar.gz platform-bionic-56e017306eb55c5deea795f89d2fb657757b4164.tar.xz platform-bionic-56e017306eb55c5deea795f89d2fb657757b4164.zip |
Simplify our endian.h implementation.
We can rely on the compiler's builtins. Tested on arm, arm64, mips, x86,
and x86-64.
Change-Id: I0f774ed7e85b3c791a3178d8ef17c6500e6a9ace
-rw-r--r-- | libc/arch-arm/include/machine/endian.h | 75 | ||||
-rw-r--r-- | libc/arch-mips/include/machine/endian.h | 66 | ||||
-rw-r--r-- | libc/arch-x86/include/machine/endian.h | 42 | ||||
-rw-r--r-- | libc/arch-x86_64/include/machine/endian.h | 42 | ||||
-rw-r--r-- | libc/include/arpa/nameser_compat.h | 47 | ||||
-rw-r--r-- | libc/include/machine/endian.h (renamed from libc/arch-arm64/include/machine/endian.h) | 23 | ||||
-rw-r--r-- | libc/include/sys/endian.h | 198 |
7 files changed, 21 insertions, 472 deletions
diff --git a/libc/arch-arm/include/machine/endian.h b/libc/arch-arm/include/machine/endian.h deleted file mode 100644 index 04bba20d..00000000 --- a/libc/arch-arm/include/machine/endian.h +++ /dev/null | |||
@@ -1,75 +0,0 @@ | |||
1 | /* $OpenBSD: endian.h,v 1.3 2005/12/13 00:35:23 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (C) 2010 The Android Open Source Project | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * * Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * * Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in | ||
14 | * the documentation and/or other materials provided with the | ||
15 | * distribution. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
20 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
21 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
23 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | ||
24 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | ||
25 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | ||
27 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #ifndef _ARM_ENDIAN_H_ | ||
32 | #define _ARM_ENDIAN_H_ | ||
33 | |||
34 | #ifdef __GNUC__ | ||
35 | |||
36 | /* According to RealView Assembler User's Guide, REV and REV16 are available | ||
37 | * in Thumb code and 16-bit instructions when used in Thumb-2 code. | ||
38 | * | ||
39 | * REV Rd, Rm | ||
40 | * Rd and Rm must both be Lo registers. | ||
41 | * | ||
42 | * REV16 Rd, Rm | ||
43 | * Rd and Rm must both be Lo registers. | ||
44 | * | ||
45 | * The +l constraint takes care of this without constraining us in ARM mode. | ||
46 | */ | ||
47 | #define __swap16md(x) ({ \ | ||
48 | register u_int16_t _x = (x); \ | ||
49 | __asm__ __volatile__("rev16 %0, %0" : "+l" (_x)); \ | ||
50 | _x; \ | ||
51 | }) | ||
52 | |||
53 | #define __swap32md(x) ({ \ | ||
54 | register u_int32_t _x = (x); \ | ||
55 | __asm__ __volatile__("rev %0, %0" : "+l" (_x)); \ | ||
56 | _x; \ | ||
57 | }) | ||
58 | |||
59 | #define __swap64md(x) ({ \ | ||
60 | u_int64_t _swap64md_x = (x); \ | ||
61 | (u_int64_t) __swap32md(_swap64md_x >> 32) | \ | ||
62 | (u_int64_t) __swap32md(_swap64md_x & 0xffffffff) << 32; \ | ||
63 | }) | ||
64 | |||
65 | /* Tell sys/endian.h we have MD variants of the swap macros. */ | ||
66 | #define MD_SWAP | ||
67 | |||
68 | #endif /* __GNUC__ */ | ||
69 | |||
70 | #define _BYTE_ORDER _LITTLE_ENDIAN | ||
71 | #define __STRICT_ALIGNMENT | ||
72 | #include <sys/types.h> | ||
73 | #include <sys/endian.h> | ||
74 | |||
75 | #endif /* !_ARM_ENDIAN_H_ */ | ||
diff --git a/libc/arch-mips/include/machine/endian.h b/libc/arch-mips/include/machine/endian.h deleted file mode 100644 index 9270e9d9..00000000 --- a/libc/arch-mips/include/machine/endian.h +++ /dev/null | |||
@@ -1,66 +0,0 @@ | |||
1 | /* $OpenBSD: endian.h,v 1.5 2006/02/27 23:35:59 miod Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2001-2002 Opsycon AB (www.opsycon.se / www.opsycon.com) | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * | ||
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS | ||
16 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | ||
19 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
25 | * SUCH DAMAGE. | ||
26 | * | ||
27 | */ | ||
28 | |||
29 | #ifndef _MIPS64_ENDIAN_H_ | ||
30 | #define _MIPS64_ENDIAN_H_ | ||
31 | |||
32 | #ifdef __GNUC__ | ||
33 | |||
34 | #if defined(__mips_isa_rev) && (__mips_isa_rev >= 2) | ||
35 | #define __swap16md(x) ({ \ | ||
36 | register uint16_t _x = (x); \ | ||
37 | register uint16_t _r; \ | ||
38 | __asm volatile ("wsbh %0, %1" : "=r" (_r) : "r" (_x)); \ | ||
39 | _r; \ | ||
40 | }) | ||
41 | |||
42 | #define __swap32md(x) ({ \ | ||
43 | register uint32_t _x = (x); \ | ||
44 | register uint32_t _r; \ | ||
45 | __asm volatile ("wsbh %0, %1; rotr %0, %0, 16" : "=r" (_r) : "r" (_x)); \ | ||
46 | _r; \ | ||
47 | }) | ||
48 | |||
49 | #define __swap64md(x) ({ \ | ||
50 | uint64_t _swap64md_x = (x); \ | ||
51 | (uint64_t) __swap32md(_swap64md_x >> 32) | \ | ||
52 | (uint64_t) __swap32md(_swap64md_x & 0xffffffff) << 32; \ | ||
53 | }) | ||
54 | |||
55 | /* Tell sys/endian.h we have MD variants of the swap macros. */ | ||
56 | #define MD_SWAP | ||
57 | |||
58 | #endif /* __mips32r2__ */ | ||
59 | #endif /* __GNUC__ */ | ||
60 | |||
61 | #define _BYTE_ORDER _LITTLE_ENDIAN | ||
62 | #define __STRICT_ALIGNMENT | ||
63 | #include <sys/types.h> | ||
64 | #include <sys/endian.h> | ||
65 | |||
66 | #endif /* _MIPS64_ENDIAN_H_ */ | ||
diff --git a/libc/arch-x86/include/machine/endian.h b/libc/arch-x86/include/machine/endian.h deleted file mode 100644 index 5feebd8d..00000000 --- a/libc/arch-x86/include/machine/endian.h +++ /dev/null | |||
@@ -1,42 +0,0 @@ | |||
1 | /* $OpenBSD: endian.h,v 1.17 2011/03/12 04:03:04 guenther Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1997 Niklas Hallqvist. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * | ||
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
25 | */ | ||
26 | |||
27 | #ifndef _MACHINE_ENDIAN_H_ | ||
28 | #define _MACHINE_ENDIAN_H_ | ||
29 | |||
30 | /* Use GCC builtins */ | ||
31 | #define __swap16md(x) __builtin_bswap16(x) | ||
32 | #define __swap32md(x) __builtin_bswap32(x) | ||
33 | #define __swap64md(x) __builtin_bswap64(x) | ||
34 | |||
35 | /* Tell sys/endian.h we have MD variants of the swap macros. */ | ||
36 | #define MD_SWAP | ||
37 | |||
38 | #define _BYTE_ORDER _LITTLE_ENDIAN | ||
39 | #include <sys/types.h> | ||
40 | #include <sys/endian.h> | ||
41 | |||
42 | #endif /* _MACHINE_ENDIAN_H_ */ | ||
diff --git a/libc/arch-x86_64/include/machine/endian.h b/libc/arch-x86_64/include/machine/endian.h deleted file mode 100644 index 2c493b27..00000000 --- a/libc/arch-x86_64/include/machine/endian.h +++ /dev/null | |||
@@ -1,42 +0,0 @@ | |||
1 | /* $OpenBSD: endian.h,v 1.5 2011/03/12 22:27:48 guenther Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1997 Niklas Hallqvist. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * | ||
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
25 | */ | ||
26 | |||
27 | #ifndef _MACHINE_ENDIAN_H_ | ||
28 | #define _MACHINE_ENDIAN_H_ | ||
29 | |||
30 | /* Use GCC builtins */ | ||
31 | #define __swap16md(x) __builtin_bswap16(x) | ||
32 | #define __swap32md(x) __builtin_bswap32(x) | ||
33 | #define __swap64md(x) __builtin_bswap64(x) | ||
34 | |||
35 | /* Tell sys/endian.h we have MD variants of the swap macros. */ | ||
36 | #define MD_SWAP | ||
37 | |||
38 | #define _BYTE_ORDER _LITTLE_ENDIAN | ||
39 | #include <sys/types.h> | ||
40 | #include <sys/endian.h> | ||
41 | |||
42 | #endif /* _MACHINE_ENDIAN_H_ */ | ||
diff --git a/libc/include/arpa/nameser_compat.h b/libc/include/arpa/nameser_compat.h index e060f601..539864e4 100644 --- a/libc/include/arpa/nameser_compat.h +++ b/libc/include/arpa/nameser_compat.h | |||
@@ -44,53 +44,6 @@ | |||
44 | 44 | ||
45 | #include <endian.h> | 45 | #include <endian.h> |
46 | 46 | ||
47 | #ifndef BYTE_ORDER | ||
48 | #if (BSD >= 199103) | ||
49 | # include <machine/endian.h> | ||
50 | #else | ||
51 | #ifdef __linux__ | ||
52 | # include <endian.h> | ||
53 | #else | ||
54 | #define LITTLE_ENDIAN 1234 /* least-significant byte first (vax, pc) */ | ||
55 | #define BIG_ENDIAN 4321 /* most-significant byte first (IBM, net) */ | ||
56 | #define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp)*/ | ||
57 | |||
58 | #if defined(vax) || defined(ns32000) || defined(sun386) || defined(i386) || \ | ||
59 | defined(MIPSEL) || defined(_MIPSEL) || defined(BIT_ZERO_ON_RIGHT) || \ | ||
60 | defined(__i386__) || defined(__i386) || defined(__amd64__) || \ | ||
61 | defined(__x86_64__) || defined(MIPSEL) || defined(_MIPSEL) || \ | ||
62 | defined(BIT_ZERO_ON_RIGHT) || defined(__alpha__) || defined(__alpha) || \ | ||
63 | (defined(__Lynx__) && defined(__x86__)) | ||
64 | #define BYTE_ORDER LITTLE_ENDIAN | ||
65 | #endif | ||
66 | |||
67 | #if defined(sel) || defined(pyr) || defined(mc68000) || defined(sparc) || \ | ||
68 | defined(is68k) || defined(tahoe) || defined(ibm032) || defined(ibm370) || \ | ||
69 | defined(MIPSEB) || defined(_MIPSEB) || defined(_IBMR2) || defined(DGUX) ||\ | ||
70 | defined(apollo) || defined(__convex__) || defined(_CRAY) || \ | ||
71 | defined(__hppa) || defined(__hp9000) || \ | ||
72 | defined(__hp9000s300) || defined(__hp9000s700) || \ | ||
73 | defined(__hp3000s900) || defined(__hpux) || defined(MPE) || \ | ||
74 | defined (BIT_ZERO_ON_LEFT) || defined(m68k) || defined(__sparc) || \ | ||
75 | (defined(__Lynx__) && \ | ||
76 | (defined(__68k__) || defined(__sparc__) || defined(__powerpc__))) | ||
77 | #define BYTE_ORDER BIG_ENDIAN | ||
78 | #endif | ||
79 | #endif /* __linux */ | ||
80 | #endif /* BSD */ | ||
81 | #endif /* BYTE_ORDER */ | ||
82 | |||
83 | #if !defined(BYTE_ORDER) || \ | ||
84 | (BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN && \ | ||
85 | BYTE_ORDER != PDP_ENDIAN) | ||
86 | /* you must determine what the correct bit order is for | ||
87 | * your compiler - the next line is an intentional error | ||
88 | * which will force your compiles to bomb until you fix | ||
89 | * the above macros. | ||
90 | */ | ||
91 | #error "Undefined or invalid BYTE_ORDER"; | ||
92 | #endif | ||
93 | |||
94 | /* | 47 | /* |
95 | * Structure for query header. The order of the fields is machine- and | 48 | * Structure for query header. The order of the fields is machine- and |
96 | * compiler-dependent, depending on the byte/bit order and the layout | 49 | * compiler-dependent, depending on the byte/bit order and the layout |
diff --git a/libc/arch-arm64/include/machine/endian.h b/libc/include/machine/endian.h index b9544afe..ac895192 100644 --- a/libc/arch-arm64/include/machine/endian.h +++ b/libc/include/machine/endian.h | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (C) 2013 The Android Open Source Project | 2 | * Copyright (C) 2014 The Android Open Source Project |
3 | * All rights reserved. | 3 | * All rights reserved. |
4 | * | 4 | * |
5 | * Redistribution and use in source and binary forms, with or without | 5 | * Redistribution and use in source and binary forms, with or without |
@@ -26,23 +26,10 @@ | |||
26 | * SUCH DAMAGE. | 26 | * SUCH DAMAGE. |
27 | */ | 27 | */ |
28 | 28 | ||
29 | #ifndef _AARCH64_ENDIAN_H_ | 29 | #ifndef _MACHINE_ENDIAN_H_ |
30 | #define _AARCH64_ENDIAN_H_ | 30 | #define _MACHINE_ENDIAN_H_ |
31 | 31 | ||
32 | #ifdef __GNUC__ | 32 | /* This file is for BSD source compatibility only. Use <endian.h> or <sys/endian.h> instead. */ |
33 | |||
34 | /* Use GCC builtins */ | ||
35 | #define __swap16md(x) __builtin_bswap16(x) | ||
36 | #define __swap32md(x) __builtin_bswap32(x) | ||
37 | #define __swap64md(x) __builtin_bswap64(x) | ||
38 | |||
39 | /* Tell sys/endian.h we have MD variants of the swap macros. */ | ||
40 | #define MD_SWAP | ||
41 | |||
42 | #endif /* __GNUC__ */ | ||
43 | |||
44 | #define _BYTE_ORDER _LITTLE_ENDIAN | ||
45 | #include <sys/types.h> | ||
46 | #include <sys/endian.h> | 33 | #include <sys/endian.h> |
47 | 34 | ||
48 | #endif /* _AARCH64_ENDIAN_H_ */ | 35 | #endif /* _MACHINE_ENDIAN_H_ */ |
diff --git a/libc/include/sys/endian.h b/libc/include/sys/endian.h index be4c9056..c62ba7f2 100644 --- a/libc/include/sys/endian.h +++ b/libc/include/sys/endian.h | |||
@@ -1,5 +1,3 @@ | |||
1 | /* $OpenBSD: endian.h,v 1.17 2006/01/06 18:53:05 millert Exp $ */ | ||
2 | |||
3 | /*- | 1 | /*- |
4 | * Copyright (c) 1997 Niklas Hallqvist. All rights reserved. | 2 | * Copyright (c) 1997 Niklas Hallqvist. All rights reserved. |
5 | * | 3 | * |
@@ -24,155 +22,44 @@ | |||
24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | */ | 23 | */ |
26 | 24 | ||
27 | /* | ||
28 | * Generic definitions for little- and big-endian systems. Other endianesses | ||
29 | * has to be dealt with in the specific machine/endian.h file for that port. | ||
30 | * | ||
31 | * This file is meant to be included from a little- or big-endian port's | ||
32 | * machine/endian.h after setting _BYTE_ORDER to either 1234 for little endian | ||
33 | * or 4321 for big.. | ||
34 | */ | ||
35 | |||
36 | #ifndef _SYS_ENDIAN_H_ | 25 | #ifndef _SYS_ENDIAN_H_ |
37 | #define _SYS_ENDIAN_H_ | 26 | #define _SYS_ENDIAN_H_ |
38 | 27 | ||
39 | #include <sys/cdefs.h> | 28 | #include <sys/cdefs.h> |
40 | #include <machine/endian.h> | ||
41 | 29 | ||
42 | #include <stdint.h> | 30 | #include <stdint.h> |
43 | 31 | ||
44 | #define _LITTLE_ENDIAN 1234 | 32 | #define _LITTLE_ENDIAN 1234 |
45 | #define _BIG_ENDIAN 4321 | 33 | #define _BIG_ENDIAN 4321 |
46 | #define _PDP_ENDIAN 3412 | 34 | #define _PDP_ENDIAN 3412 |
35 | #define _BYTE_ORDER _LITTLE_ENDIAN | ||
36 | #define __LITTLE_ENDIAN_BITFIELD | ||
47 | 37 | ||
48 | #if __BSD_VISIBLE | 38 | #if __BSD_VISIBLE |
49 | #define LITTLE_ENDIAN _LITTLE_ENDIAN | 39 | #define LITTLE_ENDIAN _LITTLE_ENDIAN |
50 | #define BIG_ENDIAN _BIG_ENDIAN | 40 | #define BIG_ENDIAN _BIG_ENDIAN |
51 | #define PDP_ENDIAN _PDP_ENDIAN | 41 | #define PDP_ENDIAN _PDP_ENDIAN |
52 | #define BYTE_ORDER _BYTE_ORDER | 42 | #define BYTE_ORDER _BYTE_ORDER |
53 | #endif | 43 | #endif |
54 | 44 | ||
55 | #ifdef __GNUC__ | 45 | #ifndef __LITTLE_ENDIAN |
56 | 46 | #define __LITTLE_ENDIAN _LITTLE_ENDIAN | |
57 | #define __swap16gen(x) __statement({ \ | 47 | #endif |
58 | __uint16_t __swap16gen_x = (x); \ | 48 | #ifndef __BIG_ENDIAN |
59 | \ | 49 | #define __BIG_ENDIAN _BIG_ENDIAN |
60 | (__uint16_t)((__swap16gen_x & 0xff) << 8 | \ | 50 | #endif |
61 | (__swap16gen_x & 0xff00) >> 8); \ | 51 | #define __BYTE_ORDER _BYTE_ORDER |
62 | }) | ||
63 | |||
64 | #define __swap32gen(x) __statement({ \ | ||
65 | __uint32_t __swap32gen_x = (x); \ | ||
66 | \ | ||
67 | (__uint32_t)((__swap32gen_x & 0xff) << 24 | \ | ||
68 | (__swap32gen_x & 0xff00) << 8 | \ | ||
69 | (__swap32gen_x & 0xff0000) >> 8 | \ | ||
70 | (__swap32gen_x & 0xff000000) >> 24); \ | ||
71 | }) | ||
72 | |||
73 | #define __swap64gen(x) __statement({ \ | ||
74 | __uint64_t __swap64gen_x = (x); \ | ||
75 | \ | ||
76 | (__uint64_t)((__swap64gen_x & 0xff) << 56 | \ | ||
77 | (__swap64gen_x & 0xff00ULL) << 40 | \ | ||
78 | (__swap64gen_x & 0xff0000ULL) << 24 | \ | ||
79 | (__swap64gen_x & 0xff000000ULL) << 8 | \ | ||
80 | (__swap64gen_x & 0xff00000000ULL) >> 8 | \ | ||
81 | (__swap64gen_x & 0xff0000000000ULL) >> 24 | \ | ||
82 | (__swap64gen_x & 0xff000000000000ULL) >> 40 | \ | ||
83 | (__swap64gen_x & 0xff00000000000000ULL) >> 56); \ | ||
84 | }) | ||
85 | |||
86 | #else /* __GNUC__ */ | ||
87 | |||
88 | /* Note that these macros evaluate their arguments several times. */ | ||
89 | #define __swap16gen(x) \ | ||
90 | (__uint16_t)(((__uint16_t)(x) & 0xff) << 8 | ((__uint16_t)(x) & 0xff00) >> 8) | ||
91 | |||
92 | #define __swap32gen(x) \ | ||
93 | (__uint32_t)(((__uint32_t)(x) & 0xff) << 24 | \ | ||
94 | ((__uint32_t)(x) & 0xff00) << 8 | ((__uint32_t)(x) & 0xff0000) >> 8 |\ | ||
95 | ((__uint32_t)(x) & 0xff000000) >> 24) | ||
96 | |||
97 | #define __swap64gen(x) \ | ||
98 | (__uint64_t)((((__uint64_t)(x) & 0xff) << 56) | \ | ||
99 | ((__uint64_t)(x) & 0xff00ULL) << 40 | \ | ||
100 | ((__uint64_t)(x) & 0xff0000ULL) << 24 | \ | ||
101 | ((__uint64_t)(x) & 0xff000000ULL) << 8 | \ | ||
102 | ((__uint64_t)(x) & 0xff00000000ULL) >> 8 | \ | ||
103 | ((__uint64_t)(x) & 0xff0000000000ULL) >> 24 | \ | ||
104 | ((__uint64_t)(x) & 0xff000000000000ULL) >> 40 | \ | ||
105 | ((__uint64_t)(x) & 0xff00000000000000ULL) >> 56) | ||
106 | |||
107 | #endif /* __GNUC__ */ | ||
108 | |||
109 | /* | ||
110 | * Define MD_SWAP if you provide swap{16,32}md functions/macros that are | ||
111 | * optimized for your architecture, These will be used for swap{16,32} | ||
112 | * unless the argument is a constant and we are using GCC, where we can | ||
113 | * take advantage of the CSE phase much better by using the generic version. | ||
114 | */ | ||
115 | #ifdef MD_SWAP | ||
116 | #if __GNUC__ | ||
117 | |||
118 | #define __swap16(x) __statement({ \ | ||
119 | __uint16_t __swap16_x = (x); \ | ||
120 | \ | ||
121 | __builtin_constant_p(x) ? __swap16gen(__swap16_x) : \ | ||
122 | __swap16md(__swap16_x); \ | ||
123 | }) | ||
124 | |||
125 | #define __swap32(x) __statement({ \ | ||
126 | __uint32_t __swap32_x = (x); \ | ||
127 | \ | ||
128 | __builtin_constant_p(x) ? __swap32gen(__swap32_x) : \ | ||
129 | __swap32md(__swap32_x); \ | ||
130 | }) | ||
131 | |||
132 | #define __swap64(x) __statement({ \ | ||
133 | __uint64_t __swap64_x = (x); \ | ||
134 | \ | ||
135 | __builtin_constant_p(x) ? __swap64gen(__swap64_x) : \ | ||
136 | __swap64md(__swap64_x); \ | ||
137 | }) | ||
138 | |||
139 | #endif /* __GNUC__ */ | ||
140 | |||
141 | #else /* MD_SWAP */ | ||
142 | #define __swap16 __swap16gen | ||
143 | #define __swap32 __swap32gen | ||
144 | #define __swap64 __swap64gen | ||
145 | #endif /* MD_SWAP */ | ||
146 | 52 | ||
147 | #define __swap16_multi(v, n) do { \ | 53 | #define __swap16 __builtin_bswap16 |
148 | __size_t __swap16_multi_n = (n); \ | 54 | #define __swap32 __builtin_bswap32 |
149 | __uint16_t *__swap16_multi_v = (v); \ | 55 | #define __swap64 __builtin_bswap64 |
150 | \ | ||
151 | while (__swap16_multi_n) { \ | ||
152 | *__swap16_multi_v = swap16(*__swap16_multi_v); \ | ||
153 | __swap16_multi_v++; \ | ||
154 | __swap16_multi_n--; \ | ||
155 | } \ | ||
156 | } while (0) | ||
157 | 56 | ||
158 | #if __BSD_VISIBLE | 57 | #if __BSD_VISIBLE |
159 | #define swap16 __swap16 | 58 | #define swap16 __swap16 |
160 | #define swap32 __swap32 | 59 | #define swap32 __swap32 |
161 | #define swap64 __swap64 | 60 | #define swap64 __swap64 |
162 | #define swap16_multi __swap16_multi | 61 | #define swap16_multi __swap16_multi |
163 | #endif /* __BSD_VISIBLE */ | ||
164 | |||
165 | #if _BYTE_ORDER == _LITTLE_ENDIAN | ||
166 | 62 | ||
167 | /* Can be overridden by machine/endian.h before inclusion of this file. */ | ||
168 | #ifndef _QUAD_HIGHWORD | ||
169 | #define _QUAD_HIGHWORD 1 | ||
170 | #endif | ||
171 | #ifndef _QUAD_LOWWORD | ||
172 | #define _QUAD_LOWWORD 0 | ||
173 | #endif | ||
174 | |||
175 | #if __BSD_VISIBLE | ||
176 | #define htobe16 __swap16 | 63 | #define htobe16 __swap16 |
177 | #define htobe32 __swap32 | 64 | #define htobe32 __swap32 |
178 | #define htobe64 __swap64 | 65 | #define htobe64 __swap64 |
@@ -205,49 +92,6 @@ __END_DECLS | |||
205 | #define htonq(x) __swap64(x) | 92 | #define htonq(x) __swap64(x) |
206 | #define ntohq(x) __swap64(x) | 93 | #define ntohq(x) __swap64(x) |
207 | 94 | ||
208 | #define __LITTLE_ENDIAN_BITFIELD | ||
209 | |||
210 | #endif /* _BYTE_ORDER */ | ||
211 | |||
212 | #if _BYTE_ORDER == _BIG_ENDIAN | ||
213 | |||
214 | /* Can be overridden by machine/endian.h before inclusion of this file. */ | ||
215 | #ifndef _QUAD_HIGHWORD | ||
216 | #define _QUAD_HIGHWORD 0 | ||
217 | #endif | ||
218 | #ifndef _QUAD_LOWWORD | ||
219 | #define _QUAD_LOWWORD 1 | ||
220 | #endif | ||
221 | |||
222 | #if __BSD_VISIBLE | ||
223 | #define htole16 __swap16 | ||
224 | #define htole32 __swap32 | ||
225 | #define htole64 __swap64 | ||
226 | #define letoh16 __swap16 | ||
227 | #define letoh32 __swap32 | ||
228 | #define letoh64 __swap64 | ||
229 | |||
230 | #define htobe16(x) (x) | ||
231 | #define htobe32(x) (x) | ||
232 | #define htobe64(x) (x) | ||
233 | #define betoh16(x) (x) | ||
234 | #define betoh32(x) (x) | ||
235 | #define betoh64(x) (x) | ||
236 | #endif /* __BSD_VISIBLE */ | ||
237 | |||
238 | #define htons(x) (x) | ||
239 | #define htonl(x) (x) | ||
240 | #define ntohs(x) (x) | ||
241 | #define ntohl(x) (x) | ||
242 | |||
243 | /* Bionic additions */ | ||
244 | #define ntohq(x) (x) | ||
245 | #define htonq(x) (x) | ||
246 | |||
247 | #define __BIG_ENDIAN_BITFIELD | ||
248 | |||
249 | #endif /* _BYTE_ORDER */ | ||
250 | |||
251 | #if __BSD_VISIBLE | 95 | #if __BSD_VISIBLE |
252 | #define NTOHL(x) (x) = ntohl((u_int32_t)(x)) | 96 | #define NTOHL(x) (x) = ntohl((u_int32_t)(x)) |
253 | #define NTOHS(x) (x) = ntohs((u_int16_t)(x)) | 97 | #define NTOHS(x) (x) = ntohs((u_int16_t)(x)) |
@@ -255,16 +99,6 @@ __END_DECLS | |||
255 | #define HTONS(x) (x) = htons((u_int16_t)(x)) | 99 | #define HTONS(x) (x) = htons((u_int16_t)(x)) |
256 | #endif | 100 | #endif |
257 | 101 | ||
258 | |||
259 | #define __BYTE_ORDER _BYTE_ORDER | ||
260 | #ifndef __LITTLE_ENDIAN | ||
261 | #define __LITTLE_ENDIAN _LITTLE_ENDIAN | ||
262 | #endif | ||
263 | #ifndef __BIG_ENDIAN | ||
264 | #define __BIG_ENDIAN _BIG_ENDIAN | ||
265 | #endif | ||
266 | |||
267 | |||
268 | #ifdef __BSD_VISIBLE | 102 | #ifdef __BSD_VISIBLE |
269 | /* | 103 | /* |
270 | * glibc-compatible beXXtoh/leXXtoh synonyms for htobeXX/htoleXX. | 104 | * glibc-compatible beXXtoh/leXXtoh synonyms for htobeXX/htoleXX. |