From ef93d2760e45085ea03796d579bb79e3a293892e Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Tue, 8 Apr 2014 20:11:57 +1000 Subject: [PATCH] Add some commonly used functions to a common header and check headers in configure.ac --- configure.ac | 5 +++ src/ckpool.c | 13 +++--- src/ckpool.h | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 6 deletions(-) create mode 100644 src/ckpool.h diff --git a/configure.ac b/configure.ac index 5c00a70b..10fd9111 100644 --- a/configure.ac +++ b/configure.ac @@ -31,4 +31,9 @@ AC_FUNC_ALLOCA PKG_PROG_PKG_CONFIG() +AC_CHECK_HEADERS(stdio.h stdlib.h fcntl.h sys/time.h unistd.h) +AC_CHECK_HEADERS(ctype.h errno.h) +AC_CHECK_HEADERS(endian.h sys/endian.h arpa/inet.h) +AC_CHECK_HEADERS(alloca.h pthread.h) + AC_OUTPUT([Makefile] [src/Makefile]) diff --git a/src/ckpool.c b/src/ckpool.c index 4815aa44..0134b360 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -1,9 +1,3 @@ -#include -#include -#include - -#include "libckpool.h" - /* * Copyright 2011-2014 Con Kolivas * @@ -13,6 +7,13 @@ * any later version. See COPYING for more details. */ +#include +#include +#include + +#include "ckpool.h" +#include "libckpool.h" + int main(void) { return 0; diff --git a/src/ckpool.h b/src/ckpool.h new file mode 100644 index 00000000..85a61dbd --- /dev/null +++ b/src/ckpool.h @@ -0,0 +1,121 @@ +/* + * Copyright 2014 Con Kolivas + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) + * any later version. See COPYING for more details. + */ + +/* This file should contain all exported functions of libckpool */ + +#ifndef CKPOOL_H +#define CKPOOL_H + +#include "config.h" + +#if HAVE_BYTESWAP_H +# include +#endif + +#if HAVE_ENDIAN_H +# include +#elif HAVE_SYS_ENDIAN_H +# include +#endif + +/* This assumes htobe32 is a macro in endian.h, and if it doesn't exist, then + * htobe64 also won't exist */ +#ifndef htobe32 +# if __BYTE_ORDER == __LITTLE_ENDIAN +# define htole16(x) (x) +# define le16toh(x) (x) +# define htole32(x) (x) +# define htole64(x) (x) +# define le32toh(x) (x) +# define le64toh(x) (x) +# define be32toh(x) bswap_32(x) +# define be64toh(x) bswap_64(x) +# define htobe16(x) bswap_16(x) +# define htobe32(x) bswap_32(x) +# define htobe64(x) bswap_64(x) +# elif __BYTE_ORDER == __BIG_ENDIAN +# define htole16(x) bswap_16(x) +# define le16toh(x) bswap_16(x) +# define htole32(x) bswap_32(x) +# define le32toh(x) bswap_32(x) +# define le64toh(x) bswap_64(x) +# define htole64(x) bswap_64(x) +# define be32toh(x) (x) +# define be64toh(x) (x) +# define htobe16(x) (x) +# define htobe32(x) (x) +# define htobe64(x) (x) +# endif +#endif + +#define unlikely(expr) (__builtin_expect(!!(expr), 0)) +#define likely(expr) (__builtin_expect(!!(expr), 1)) +#define __maybe_unused __attribute__((unused)) +#define uninitialised_var(x) x = x + +static inline void swap256(void *dest_p, const void *src_p) +{ + uint32_t *dest = dest_p; + const uint32_t *src = src_p; + + dest[0] = src[7]; + dest[1] = src[6]; + dest[2] = src[5]; + dest[3] = src[4]; + dest[4] = src[3]; + dest[5] = src[2]; + dest[6] = src[1]; + dest[7] = src[0]; +} + +static inline void bswap256(void *dest_p, const void *src_p) +{ + uint32_t *dest = dest_p; + const uint32_t *src = src_p; + + dest[0] = bswap32(src[7]); + dest[1] = bswap32(src[6]); + dest[2] = bswap32(src[5]); + dest[3] = bswap32(src[4]); + dest[4] = bswap32(src[3]); + dest[5] = bswap32(src[2]); + dest[6] = bswap32(src[1]); + dest[7] = bswap32(src[0]); +} + +static inline void flip80(void *dest_p, const void *src_p) +{ + uint32_t *dest = dest_p; + const uint32_t *src = src_p; + int i; + + for (i = 0; i < 20; i++) + dest[i] = swab32(src[i]); +} + +static inline void flip32(void *dest_p, const void *src_p) +{ + uint32_t *dest = dest_p; + const uint32_t *src = src_p; + int i; + + for (i = 0; i < 8; i++) + dest[i] = swab32(src[i]); +} + +static inline void flip80(void *dest_p, const void *src_p) +{ + uint32_t *dest = dest_p; + const uint32_t *src = src_p; + int i; + + for (i = 0; i < 20; i++) + dest[i] = swab32(src[i]); +} +#endif /* CKPOOL_H */