Skip to content

Commit

Permalink
Use system-provided (glibc/FreeBSD) bswap function and uint32_t
Browse files Browse the repository at this point in the history
Fix C11 (gnu11) build
  • Loading branch information
slokhorst committed Oct 10, 2016
1 parent c27023b commit aea61b7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 131 deletions.
37 changes: 0 additions & 37 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -824,27 +824,6 @@ else
fi


# Check sizes of integer types
AC_CHECK_SIZEOF(short)
AC_CHECK_SIZEOF(int)
AC_CHECK_SIZEOF(long int)
AC_CHECK_SIZEOF(long long)
AC_CHECK_SIZEOF(int *)
AC_CHECK_SIZEOF(void *)

if test "$ac_cv_sizeof_short" = "4"; then
TEMP_CFLAGS="${TEMP_CFLAGS} -DTYPE_32BIT=\"short\""
else
if test "$ac_cv_sizeof_int" = "4"; then
TEMP_CFLAGS="${TEMP_CFLAGS} -DTYPE_32BIT=\"int\""
else
if test "$ac_cv_sizeof_long_int" = "4"; then
TEMP_CFLAGS="${TEMP_CFLAGS} -DTYPE_32BIT=\"long int\""
fi
fi
fi


OPTIMIZECPU="yes"

AC_ARG_WITH(optimizecpu,
Expand Down Expand Up @@ -1099,22 +1078,6 @@ else
fi


AC_MSG_CHECKING(for bswap instruction)
AC_LINK_IFELSE([
AC_LANG_PROGRAM([ ],
[unsigned int __x=0;
register unsigned int __v;
__asm("bswap %0" : "=r" (__v) : "0" (__x));])
],
[
TEMP_CFLAGS="${TEMP_CFLAGS} -DHAVE_BSWAP"
AC_MSG_RESULT(yes)
],
[
AC_MSG_RESULT(no)
])


if test "${DEVELOPER_FLAGS}" = "yes"; then
TEMP_CFLAGS="${TEMP_CFLAGS} -W -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Waggregate-return -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Winline -Wredundant-decls -Wno-long-long -ggdb -g3"
fi
Expand Down
105 changes: 11 additions & 94 deletions rotate.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,102 +30,19 @@
* v1 (28-Aug-2004) - initial version
*/
#include "rotate.h"

#ifndef __uint32
/**
* We don't have a 32-bit unsigned integer type, so define it, given
* a 32-bit type was found by configure.
*/
# ifdef TYPE_32BIT
typedef unsigned TYPE_32BIT __uint32;
# else
# error "Failed to find a 32-bit integer type."
# endif
#endif

/*=============================================================================
Start of code from bits/byteswap.h
=============================================================================*/

/**
* The code below is copied (with modification) from bits/byteswap.h. It provides
* a macro/function named rot__bswap_32 that swaps the bytes in a 32-bit integer,
* preferably using the bswap assembler instruction if configure found support
* for it.
*
* It would be neater to simply include byteswap.h and use the bswap_32 macro
* defined there, but the problem is that the bswap asm instruction would then
* only be used for certain processor architectures, excluding athlon (and
* probably athlon64 as well). Moreover, byteswap.h doesn't seem to exist on
* FreeBSD. So, we rely on the HAVE_BSWAP macro defined by configure instead.
*
* Note that the macro names have been prefixed with "rot" in order to avoid
* collision since we have the include chain rotate.h -> motion.h -> netcam.h ->
* netinet/in.h -> ... -> byteswap.h -> bits/byteswap.h.
*/

/* Swap bytes in 32 bit value. This is used as a fallback and for constants. */
#define rot__bswap_constant_32(x) \
((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
(((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))

#ifdef __GNUC__
# if (__GNUC__ >= 2) && (i386 || __i386 || __i386__)
/* We're on an Intel-compatible platform, so we can use inline Intel assembler
* for the swapping.
*/
# ifndef HAVE_BSWAP
/* Bswap is not available, we have to use three instructions instead. */
# define rot__bswap_32(x) \
(__extension__ \
({ register __uint32 __v, __x = (x); \
if (__builtin_constant_p (__x)) \
__v = rot__bswap_constant_32 (__x); \
else \
__asm__ ("rorw $8, %w0;" \
"rorl $16, %0;" \
"rorw $8, %w0" \
: "=r" (__v) \
: "0" (__x) \
: "cc"); \
__v; }))
# else
# define rot__bswap_32(x) \
(__extension__ \
({ register __uint32 __v, __x = (x); \
if (__builtin_constant_p (__x)) \
__v = rot__bswap_constant_32 (__x); \
else \
__asm__ ("bswap %0" : "=r" (__v) : "0" (__x)); \
__v; }))
# endif
# else
/* Non-Intel platform or too old version of gcc. */
# define rot__bswap_32(x) \
(__extension__ \
({ register __uint32 __x = (x); \
rot__bswap_constant_32 (__x); }))
# endif
#include <stdint.h>
#ifdef BSD
#include <sys/endian.h>
#define bswap_32(x) bswap32(x)
#else
/* Not a GNU compiler. */
static inline __uint32 rot__bswap_32(__uint32 __bsx)
{
return __bswap_constant_32 (__bsx);
}
#include <byteswap.h>
#endif

/*=============================================================================
End of code from bits/byteswap.h
=============================================================================*/

/* Finally define a macro with a more appropriate name, to be used below. */
#define swap_bytes(x) rot__bswap_32(x)

/**
* reverse_inplace_quad
*
* Reverses a block of memory in-place, 4 bytes at a time. This function
* requires the __uint32 type, which is 32 bits wide.
* requires the uint32_t type, which is 32 bits wide.
*
* Parameters:
*
Expand All @@ -136,13 +53,13 @@ static inline __uint32 rot__bswap_32(__uint32 __bsx)
*/
static void reverse_inplace_quad(unsigned char *src, int size)
{
__uint32 *nsrc = (__uint32 *)src; /* first quad */
__uint32 *ndst = (__uint32 *)(src + size - 4); /* last quad */
register __uint32 tmp;
uint32_t *nsrc = (uint32_t *)src; /* first quad */
uint32_t *ndst = (uint32_t *)(src + size - 4); /* last quad */
register uint32_t tmp;

while (nsrc < ndst) {
tmp = swap_bytes(*ndst);
*ndst-- = swap_bytes(*nsrc);
tmp = bswap_32(*ndst);
*ndst-- = bswap_32(*nsrc);
*nsrc++ = tmp;
}
}
Expand Down

0 comments on commit aea61b7

Please sign in to comment.