diff options
Diffstat (limited to 'sysdeps/generic')
-rw-r--r-- | sysdeps/generic/bits/byteswap.h | 43 | ||||
-rw-r--r-- | sysdeps/generic/bits/htontoh.h | 23 | ||||
-rw-r--r-- | sysdeps/generic/htonl.c | 17 | ||||
-rw-r--r-- | sysdeps/generic/htons.c | 15 |
4 files changed, 60 insertions, 38 deletions
diff --git a/sysdeps/generic/bits/byteswap.h b/sysdeps/generic/bits/byteswap.h new file mode 100644 index 0000000..9404cc4 --- /dev/null +++ b/sysdeps/generic/bits/byteswap.h @@ -0,0 +1,43 @@ +/* Macros to swap the order of bytes in integer values. + Copyright (C) 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#ifndef _BITS_BYTESWAP_H +#define _BITS_BYTESWAP_H 1 + +/* Swap bytes in 16 bit value. */ +#define __bswap_16(x) \ + ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)) + +/* Swap bytes in 32 bit value. */ +#define __bswap_32(x) \ + ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \ + (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) + +#if defined __GNUC__ && __GNUC__ >= 2 +/* Swap bytes in 64 bit value. */ +# define __bswap_64(x) \ + ({ union { unsigned long long int __ll; \ + unsigned long int __l[2]; } __v, __r; \ + __v.__ll = (x); \ + __r.__l[0] = __bswap_32 (__v.__l[1]); \ + __r.__l[1] = __bswap_32 (__v.__l[0]); \ + __r.__ll; }) +#endif + +#endif /* bits/byteswap.h */ diff --git a/sysdeps/generic/bits/htontoh.h b/sysdeps/generic/bits/htontoh.h deleted file mode 100644 index fa4efed..0000000 --- a/sysdeps/generic/bits/htontoh.h +++ /dev/null @@ -1,23 +0,0 @@ -/* Copyright (C) 1997 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the GNU C Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. */ - -#ifndef _NETINET_IN_H -# error "Don't include this file directly, use <netinet/in.h>" -#endif - -/* We cannot give generic optimized versions here. */ diff --git a/sysdeps/generic/htonl.c b/sysdeps/generic/htonl.c index d460d40..f433075 100644 --- a/sysdeps/generic/htonl.c +++ b/sysdeps/generic/htonl.c @@ -19,17 +19,18 @@ #include <netinet/in.h> #undef htonl +#undef ntohl u_int32_t -__htonl (x) +htonl (x) u_int32_t x; { -#if BYTE_ORDER == LITTLE_ENDIAN - x = (x << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | (x >> 24); -#endif - +#if BYTE_ORDER == BIG_ENDIAN return x; +#elif BYTE_ORDER == LITTLE_ENDIAN + return __bswap_32 (x); +#else +# error "What kind of system is this?" +#endif } -strong_alias (__htonl, __ntohl) -weak_alias (__htonl, htonl) -weak_alias (__ntohl, ntohl) +weak_alias (htonl, ntohl) diff --git a/sysdeps/generic/htons.c b/sysdeps/generic/htons.c index a0a0e81..8914f74 100644 --- a/sysdeps/generic/htons.c +++ b/sysdeps/generic/htons.c @@ -19,17 +19,18 @@ #include <netinet/in.h> #undef htons +#undef ntohs u_int16_t __htons (x) u_int16_t x; { -#if BYTE_ORDER == LITTLE_ENDIAN - x = (x << 8) | (x >> 8); -#endif - +#if BYTE_ORDER == BIG_ENDIAN return x; +#elif BYTE_ORDER == LITTLE_ENDIAN + return __bswap_16 (x); +#else +# error "What kind of system is this?" +#endif } -strong_alias (__htons, __ntohs) -weak_alias (__htons, htons) -weak_alias (__ntohs, ntohs) +weak_alias (htons, ntohs) |