Merge pull request #18 from cryb-to/bitwise

Fix ffs() / fls() and add unit tests.
This commit is contained in:
Dag-Erling Smørgrav 2018-11-17 17:43:51 +02:00 committed by GitHub
commit 96704ea4a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 203 additions and 30 deletions

View file

@ -79,9 +79,12 @@ static inline int cryb_ffs(int n) {
#elif HAVE___BUILTIN_CTZ
return (n ? __builtin_ctz(n) : 0);
#else
int i = 8 * sizeof n - 1;
for (i > 0)
if (n & (1 << --i))
unsigned int m;
unsigned int i;
if (n == 0)
return (0);
for (i = m = 1; i <= sizeof n * 8; ++i, m <<= 1)
if ((unsigned int)n & m)
break;
return (i);
#endif
@ -93,9 +96,12 @@ static inline int cryb_ffsl(long int n) {
#elif HAVE___BUILTIN_CLZ
return (n ? __builtin_ctz(n) : 0);
#else
int i = 8 * sizeof n - 1;
for (i > 0)
if (n & (1 << --i))
unsigned long int m;
unsigned int i;
if (n == 0)
return (0);
for (i = m = 1; i <= sizeof n * 8; ++i, m <<= 1)
if ((unsigned long int)n & m)
break;
return (i);
#endif
@ -107,9 +113,12 @@ static inline int cryb_ffsll(long long int n) {
#elif HAVE___BUILTIN_CLZ
return (n ? __builtin_ctz(n) : 0);
#else
int i = 8 * sizeof n - 1;
for (i > 0)
if (n & (1 << --i))
unsigned long long int m;
unsigned int i;
if (n == 0)
return (0);
for (i = m = 1; i <= sizeof n * 8; ++i, m <<= 1)
if ((unsigned long long int)n & m)
break;
return (i);
#endif
@ -121,9 +130,12 @@ static inline int cryb_fls(int n) {
#elif HAVE___BUILTIN_CLZ
return (n ? (8 * sizeof n) - __builtin_clz(n) : 0);
#else
int i = 8 * sizeof n - 1;
for (i > 0)
if (n & (1 << --i))
unsigned int m;
unsigned int i;
if (n == 0)
return (0);
for (i = sizeof n * 8, m = 1U << (i - 1); i > 0; --i, m >>= 1)
if ((unsigned int)n & m)
break;
return (i);
#endif
@ -135,9 +147,12 @@ static inline int cryb_flsl(long int n) {
#elif HAVE___BUILTIN_CLZ
return (n ? (8 * sizeof n) - __builtin_clzl(n) : 0);
#else
int i = 8 * sizeof n - 1;
for (i > 0)
if (n & (1 << --i))
unsigned long int m;
unsigned int i;
if (n == 0)
return (0);
for (i = sizeof n * 8, m = 1UL << (i - 1); i > 0; --i, m >>= 1)
if ((unsigned long int)n & m)
break;
return (i);
#endif
@ -149,9 +164,12 @@ static inline int cryb_flsll(long long int n) {
#elif HAVE___BUILTIN_CLZ
return (n ? (8 * sizeof n) - __builtin_clzll(n) : 0);
#else
int i = 8 * sizeof n - 1;
for (i > 0)
if (n & (1 << --i))
unsigned long long int m;
unsigned int i;
if (n == 0)
return (0);
for (i = sizeof n * 8, m = 1ULL << (i - 1); i > 0; --i, m >>= 1)
if ((unsigned long long int)n & m)
break;
return (i);
#endif

7
t/.gitignore vendored
View file

@ -10,6 +10,7 @@
/t_digest
/t_enc
/t_endian
/t_ffs_fls
/t_fletcher
/t_hash
/t_hmac_sha1
@ -32,10 +33,10 @@
/t_memcpy_s
/t_memset_s
/t_mpi
/t_mpi_addsub
/t_mpi_add_sub
/t_mpi_compar
/t_mpi_gcd
/t_mpi_muldiv
/t_mpi_mul_div
/t_murmur3_32
/t_oath
/t_pearson
@ -43,7 +44,7 @@
/t_rc4
/t_rfc3986
/t_rfc4648
/t_rolror
/t_rol_ror
/t_salsa
/t_sha1
/t_sha1_openssl

View file

@ -94,19 +94,20 @@ TESTS += t_core
t_core_LDADD = $(libt) $(libcore)
TESTS += t_assert
t_assert_LDADD = $(libt) $(libcore)
TESTS += t_ctype t_endian t_memcpy_s t_memset_s t_strchrnul t_strlcat t_strlcmp t_strlcpy
TESTS += t_string t_wstring
EXTRA_DIST += t__string.c
TESTS += t_ctype t_endian
t_ctype_LDADD = $(libt) $(libcore)
t_endian_LDADD = $(libt) $(libcore)
TESTS += t_memcpy_s t_memset_s
t_memcpy_s_LDADD = $(libt) $(libcore)
t_memset_s_LDADD = $(libt) $(libcore)
TESTS += t_strchrnul t_strlcat t_strlcmp t_strlcpy
t_strchrnul_LDADD = $(libt) $(libcore)
t_strlcat_LDADD = $(libt) $(libcore)
t_strlcmp_LDADD = $(libt) $(libcore)
t_strlcpy_LDADD = $(libt) $(libcore)
TESTS += t_rolror
t_rolror_LDADD = $(libt) $(libcore)
TESTS += t_ffs_fls t_rol_ror
t_ffs_fls_LDADD = $(libt) $(libcore)
t_rol_ror_LDADD = $(libt) $(libcore)
TESTS += t_string t_wstring
EXTRA_DIST += t__string.c
t_string_LDADD = $(libt) $(libcore)
@ -217,12 +218,12 @@ endif CRYB_MAC
# libcryb-mpi
if CRYB_MPI
TESTS += t_mpi t_mpi_addsub t_mpi_compar t_mpi_gcd t_mpi_muldiv
TESTS += t_mpi t_mpi_add_sub t_mpi_compar t_mpi_gcd t_mpi_mul_div
t_mpi_LDADD = $(libt) $(libmpi)
t_mpi_addsub_LDADD = $(libt) $(libmpi)
t_mpi_add_sub_LDADD = $(libt) $(libmpi)
t_mpi_compar_LDADD = $(libt) $(libmpi)
t_mpi_gcd_LDADD = $(libt) $(libmpi)
t_mpi_muldiv_LDADD = $(libt) $(libmpi)
t_mpi_mul_div_LDADD = $(libt) $(libmpi)
noinst_HEADERS += t_mpi.h
endif CRYB_MPI

153
t/t_ffs_fls.c Normal file
View file

@ -0,0 +1,153 @@
/*-
* Copyright (c) 2014-2018 Dag-Erling Smørgrav
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "cryb/impl.h"
#include <stddef.h>
#include <stdint.h>
#include <unistd.h>
/* test our own code, not the compiler's */
#undef HAVE___BUILTIN_CLZ
#undef HAVE___BUILTIN_CTZ
#undef HAVE___BUILTIN_FFS
#undef HAVE___BUILTIN_FFSL
#undef HAVE___BUILTIN_FFSLL
#undef HAVE___BUILTIN_FLS
#undef HAVE___BUILTIN_FLSL
#undef HAVE___BUILTIN_FLSLL
#include <cryb/bitwise.h>
#include <cryb/test.h>
static int
t_ffs(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
int u;
int n, ret;
ret = t_compare_i(0, cryb_ffs(0));
for (u = 1, n = 1; u != 0; u <<= 1, n++) {
t_printv("ffs(0x%08x) == %d\n", u, cryb_ffs(u));
ret &= t_compare_i(n, cryb_ffs(u));
}
return (ret);
}
static int
t_ffsl(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
long int u;
int n, ret;
ret = t_compare_i(0, cryb_ffs(0));
for (u = 1, n = 1; u != 0; u <<= 1, n++)
ret &= t_compare_i(n, cryb_ffsl(u));
return (ret);
}
static int
t_ffsll(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
long long int u;
int n, ret;
ret = t_compare_i(0, cryb_ffs(0));
for (u = 1, n = 1; u != 0; u <<= 1, n++)
ret &= t_compare_i(n, cryb_ffsll(u));
return (ret);
}
static int
t_fls(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
unsigned int u;
int n, ret;
ret = t_compare_i(0, cryb_fls(0));
for (u = 1, n = 1; u != 0; u <<= 1, n++) {
t_printv("fls(0x%08x) == %d\n", u, cryb_fls(u));
ret &= t_compare_i(n, cryb_fls(u));
}
return (ret);
}
static int
t_flsl(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
unsigned long int u;
int n, ret;
ret = t_compare_i(0, cryb_flsl(0));
for (u = 1, n = 1; u != 0; u <<= 1, n++) {
t_printv("flsl(0x%08lx) == %d\n", u, cryb_flsl(u));
ret &= t_compare_i(n, cryb_flsl(u));
}
return (ret);
}
static int
t_flsll(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
unsigned long long int u;
int n, ret;
ret = t_compare_i(0, cryb_flsll(0));
for (u = 1, n = 1; u != 0; u <<= 1, n++) {
t_printv("flsll(0x%016llx) == %d\n", u, cryb_flsll(u));
ret &= t_compare_i(n, cryb_flsll(u));
}
return (ret);
}
/***************************************************************************
* Boilerplate
*/
static int
t_prepare(int argc CRYB_UNUSED, char *argv[] CRYB_UNUSED)
{
t_add_test(t_ffs, 0, "ffs");
t_add_test(t_ffsl, 0, "ffsl");
t_add_test(t_ffsll, 0, "ffsll");
t_add_test(t_fls, 0, "fls");
t_add_test(t_flsl, 0, "flsl");
t_add_test(t_flsll, 0, "flsll");
return (0);
}
int
main(int argc, char *argv[])
{
t_main(t_prepare, NULL, argc, argv);
}