From 4eed3157327f8406921658442a11af7e9d84d603 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 4 Oct 2013 00:36:22 -0700 Subject: Use hardware support for byteswapping on glibc x86 etc. On Fedora 19 x86-64, the new bswap_64 needs 1 instruction, whereas the old swap64 needed 30. * admin/merge-gnulib (GNULIB_MODULES): Add byteswap. * lib/byteswap.in.h, m4/byteswap.m4: New files, copied from Gnulib. * lib/gnulib.mk, m4/gnulib-comp.m4: Regenerate. * src/fringe.c (init_fringe_bitmap) [WORDS_BIGENDIAN]: * src/sound.c (le2hl, le2hs, be2hl) [!WINDOWSNT]: Use byteswap.h's macros to swap bytes. * src/lisp.h (swap16, swap32, swap64): Remove. All uses replaced by bswap_16, bswap_32, bswap_64. --- src/ChangeLog | 9 +++++++++ src/data.c | 5 +++-- src/fringe.c | 4 +++- src/lisp.h | 24 ------------------------ src/sound.c | 29 +++++------------------------ src/xsettings.c | 11 +++++++---- 6 files changed, 27 insertions(+), 55 deletions(-) (limited to 'src') diff --git a/src/ChangeLog b/src/ChangeLog index 4fae03d6965..38fb35ddf54 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,14 @@ 2013-10-04 Paul Eggert + Use hardware support for byteswapping on glibc x86 etc. + On Fedora 19 x86-64, the new bswap_64 needs 1 instruction, + whereas the old swap64 needed 30. + * fringe.c (init_fringe_bitmap) [WORDS_BIGENDIAN]: + * sound.c (le2hl, le2hs, be2hl) [!WINDOWSNT]: + Use byteswap.h's macros to swap bytes. + * lisp.h (swap16, swap32, swap64): Remove. + All uses replaced by bswap_16, bswap_32, bswap_64. + * bytecode.c (exec_byte_code): Use some more volatile variables to work around local variables getting clobbered by longjmp. Port to pre-C99, which doesn't allow decls after stmts. diff --git a/src/data.c b/src/data.c index b268616fd26..a6bfe50a3bb 100644 --- a/src/data.c +++ b/src/data.c @@ -21,6 +21,7 @@ along with GNU Emacs. If not, see . */ #include #include +#include #include #include "lisp.h" @@ -3185,9 +3186,9 @@ size_t_to_host_endian (size_t val) { #ifdef WORDS_BIGENDIAN # if BITS_PER_SIZE_T == 64 - return swap64 (val); + return bswap_64 (val); # else - return swap32 (val); + return bswap_32 (val); # endif #else return val; diff --git a/src/fringe.c b/src/fringe.c index 85103f7f08d..3537027f1dd 100644 --- a/src/fringe.c +++ b/src/fringe.c @@ -20,6 +20,8 @@ along with GNU Emacs. If not, see . */ #include #include +#include + #include "lisp.h" #include "frame.h" #include "window.h" @@ -1519,7 +1521,7 @@ init_fringe_bitmap (int which, struct fringe_bitmap *fb, int once_p) | (swap_nibble[(b>>12) & 0xf])); b >>= (16 - fb->width); #ifdef WORDS_BIGENDIAN - b = ((b >> 8) | (b << 8)); + b = bswap_16 (b); #endif *bits++ = b; } diff --git a/src/lisp.h b/src/lisp.h index f949978ceed..2f0279ef542 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -4366,30 +4366,6 @@ functionp (Lisp_Object object) return 0; } -INLINE uint16_t -swap16 (uint16_t val) -{ - return (val << 8) | (val & 0xFF); -} - -INLINE uint32_t -swap32 (uint32_t val) -{ - uint32_t low = swap16 (val & 0xFFFF); - uint32_t high = swap16 (val >> 16); - return (low << 16) | high; -} - -#ifdef UINT64_MAX -INLINE uint64_t -swap64 (uint64_t val) -{ - uint64_t low = swap32 (val & 0xFFFFFFFF); - uint64_t high = swap32 (val >> 32); - return (low << 32) | high; -} -#endif - #if ((SIZE_MAX >> 31) >> 1) & 1 # define BITS_PER_SIZE_T 64 #else diff --git a/src/sound.c b/src/sound.c index 27e06b8abab..f8c6b483056 100644 --- a/src/sound.c +++ b/src/sound.c @@ -55,6 +55,8 @@ along with GNU Emacs. If not, see . */ /* BEGIN: Non Windows Includes */ #ifndef WINDOWSNT +#include + #include /* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention @@ -461,8 +463,7 @@ static u_int32_t le2hl (u_int32_t value) { #ifdef WORDS_BIGENDIAN - unsigned char *p = (unsigned char *) &value; - value = p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24); + value = bswap_32 (value); #endif return value; } @@ -475,8 +476,7 @@ static u_int16_t le2hs (u_int16_t value) { #ifdef WORDS_BIGENDIAN - unsigned char *p = (unsigned char *) &value; - value = p[0] + (p[1] << 8); + value = bswap_16 (value); #endif return value; } @@ -489,30 +489,11 @@ static u_int32_t be2hl (u_int32_t value) { #ifndef WORDS_BIGENDIAN - unsigned char *p = (unsigned char *) &value; - value = p[3] + (p[2] << 8) + (p[1] << 16) + (p[0] << 24); + value = bswap_32 (value); #endif return value; } - -#if 0 /* Currently not used. */ - -/* Convert 16-bit value VALUE which is in big-endian byte-order - to host byte-order. */ - -static u_int16_t -be2hs (u_int16_t value) -{ -#ifndef WORDS_BIGENDIAN - unsigned char *p = (unsigned char *) &value; - value = p[1] + (p[0] << 8); -#endif - return value; -} - -#endif /* 0 */ - /*********************************************************************** RIFF-WAVE (*.wav) ***********************************************************************/ diff --git a/src/xsettings.c b/src/xsettings.c index 8fe82fec74b..e5a66c4cf0a 100644 --- a/src/xsettings.c +++ b/src/xsettings.c @@ -22,6 +22,9 @@ along with GNU Emacs. If not, see . */ #include #include #include + +#include + #include "lisp.h" #include "xterm.h" #include "xsettings.h" @@ -405,7 +408,7 @@ parse_settings (unsigned char *prop, if (bytes < 12) return BadLength; memcpy (&n_settings, prop+8, 4); - if (my_bo != that_bo) n_settings = swap32 (n_settings); + if (my_bo != that_bo) n_settings = bswap_32 (n_settings); bytes_parsed = 12; memset (settings, 0, sizeof (*settings)); @@ -427,7 +430,7 @@ parse_settings (unsigned char *prop, memcpy (&nlen, prop+bytes_parsed, 2); bytes_parsed += 2; - if (my_bo != that_bo) nlen = swap16 (nlen); + if (my_bo != that_bo) nlen = bswap_16 (nlen); if (bytes_parsed+nlen > bytes) return BadLength; to_cpy = nlen > 127 ? 127 : nlen; memcpy (name, prop+bytes_parsed, to_cpy); @@ -454,7 +457,7 @@ parse_settings (unsigned char *prop, if (want_this) { memcpy (&ival, prop+bytes_parsed, 4); - if (my_bo != that_bo) ival = swap32 (ival); + if (my_bo != that_bo) ival = bswap_32 (ival); } bytes_parsed += 4; break; @@ -463,7 +466,7 @@ parse_settings (unsigned char *prop, if (bytes_parsed+4 > bytes) return BadLength; memcpy (&vlen, prop+bytes_parsed, 4); bytes_parsed += 4; - if (my_bo != that_bo) vlen = swap32 (vlen); + if (my_bo != that_bo) vlen = bswap_32 (vlen); if (want_this) { to_cpy = vlen > 127 ? 127 : vlen; -- cgit v1.2.1