Move popcount et al to src/common and add popcount32/popcount64.

Requested by rmind@. MD should now override popcount32/popcount64 and
provide the aliases as fitting.
This commit is contained in:
joerg 2009-07-21 14:55:32 +00:00
parent e593aba25a
commit 0578c2ad0f
9 changed files with 86 additions and 79 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: popcount.c,v 1.1 2009/07/21 13:18:44 joerg Exp $ */
/* $NetBSD: popcount32.c,v 1.1 2009/07/21 14:55:32 joerg Exp $ */
/*-
* Copyright (c) 2009 The NetBSD Foundation, Inc.
* All rights reserved.
@ -32,13 +32,12 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: popcount.c,v 1.1 2009/07/21 13:18:44 joerg Exp $");
__RCSID("$NetBSD: popcount32.c,v 1.1 2009/07/21 14:55:32 joerg Exp $");
#include <limits.h>
#if !defined(_KERNEL) && !defined(_STANDALONE)
#include <strings.h>
#if UINT_MAX > 0xffffffffUL
#error "Unsupported architecture"
#else
#include <lib/libkern/libkern.h>
#endif
/*
@ -50,13 +49,13 @@ __RCSID("$NetBSD: popcount.c,v 1.1 2009/07/21 13:18:44 joerg Exp $");
*/
unsigned int
popcount(unsigned int v)
popcount32(uint32_t v)
{
unsigned int c;
v = v - ((v >> 1) & 0x55555555U);
v = (v & 0x33333333U) + ((v >> 2) & 0x33333333U);
v = ((v + (v >> 4)) & 0x0f0f0f0fU;
v = (v + (v >> 4)) & 0x0f0f0f0fU;
c = (v * 0x01010101U) >> 24;
/*
* v = (v >> 16) + v;
@ -66,3 +65,11 @@ popcount(unsigned int v)
return c;
}
#if UINT_MAX == 0xffffffffUL
__strong_alias(popcount, popcount32);
#endif
#if ULONG_MAX == 0xffffffffU
__strong_alias(popcountl, popcount32);
#endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: popcountll.c,v 1.1 2009/07/21 13:18:44 joerg Exp $ */
/* $NetBSD: popcount64.c,v 1.1 2009/07/21 14:55:32 joerg Exp $ */
/*-
* Copyright (c) 2009 The NetBSD Foundation, Inc.
* All rights reserved.
@ -32,31 +32,33 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: popcountll.c,v 1.1 2009/07/21 13:18:44 joerg Exp $");
__RCSID("$NetBSD: popcount64.c,v 1.1 2009/07/21 14:55:32 joerg Exp $");
#if !defined(_KERNEL) && !defined(_STANDALONE)
#include <limits.h>
#include <strings.h>
#if ULONGLONG_MAX > 0xffffffffffffffffULL
#error "Unsupported architecture"
#else
#include <lib/libkern/libkern.h>
#include <machine/limits.h>
#endif
/*
* If unsigned long long is larger than size_t, the follow assumes that
* If uint64_t is larger than size_t, the follow assumes that
* splitting into 32bit halfes is faster.
*
* The native pocountll version is based on the same ideas as popcount(3),
* see popcount.c for comments.
* The native pocount64 version is based on the same ideas as popcount64(3),
* see popcount64.c for comments.
*/
#if ULONGLONG_MAX > SIZE_MAX
#if SIZE_MAX < 0xffffffffffffffffULL
unsigned int
popcountll(unsigned long long v)
popcount64(uint64_t v)
{
return popcount(v >> 32) + popcount(v & 0xffffffffU);
return popcount32(v >> 32) + popcount32(v & 0xffffffffU);
}
#else
unsigned int
popcountll(unsigned long long v)
popcount64(uint64_t v)
{
unsigned int c;
@ -68,3 +70,12 @@ popcountll(unsigned long long v)
return c;
}
#endif
#if ULONG_MAX == 0xffffffffffffffffULL
__strong_alias(popcountl, popcount64);
#endif
#if ULLONG_MAX == 0xffffffffffffffffULL
__strong_alias(popcountll, popcount64);
#endif

View File

@ -1,4 +1,4 @@
# $NetBSD: mi,v 1.1286 2009/07/21 14:18:50 njoly Exp $
# $NetBSD: mi,v 1.1287 2009/07/21 14:55:32 joerg Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
#
@ -6946,6 +6946,8 @@
./usr/share/man/cat3/popcount.0 comp-c-catman .cat
./usr/share/man/cat3/popcountl.0 comp-c-catman .cat
./usr/share/man/cat3/popcountll.0 comp-c-catman .cat
./usr/share/man/cat3/popcount32.0 comp-c-catman .cat
./usr/share/man/cat3/popcount64.0 comp-c-catman .cat
./usr/share/man/cat3/popen.0 comp-c-catman .cat
./usr/share/man/cat3/pos_form_cursor.0 comp-c-catman .cat
./usr/share/man/cat3/posix_memalign.0 comp-c-catman .cat
@ -12406,6 +12408,8 @@
./usr/share/man/html3/popcount.html comp-c-htmlman html
./usr/share/man/html3/popcountl.html comp-c-htmlman html
./usr/share/man/html3/popcountll.html comp-c-htmlman html
./usr/share/man/html3/popcount32.html comp-c-htmlman html
./usr/share/man/html3/popcount64.html comp-c-htmlman html
./usr/share/man/html3/popen.html comp-c-htmlman html
./usr/share/man/html3/pos_form_cursor.html comp-c-htmlman html
./usr/share/man/html3/posix_memalign.html comp-c-htmlman html
@ -17860,6 +17864,8 @@
./usr/share/man/man3/popcount.3 comp-c-man .man
./usr/share/man/man3/popcountl.3 comp-c-man .man
./usr/share/man/man3/popcountll.3 comp-c-man .man
./usr/share/man/man3/popcount32.3 comp-c-man .man
./usr/share/man/man3/popcount64.3 comp-c-man .man
./usr/share/man/man3/popen.3 comp-c-man .man
./usr/share/man/man3/pos_form_cursor.3 comp-c-man .man
./usr/share/man/man3/posix_memalign.3 comp-c-man .man

View File

@ -1,4 +1,4 @@
/* $NetBSD: strings.h,v 1.14 2009/07/21 13:18:43 joerg Exp $ */
/* $NetBSD: strings.h,v 1.15 2009/07/21 14:55:33 joerg Exp $ */
/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
@ -46,6 +46,8 @@ typedef _BSD_SIZE_T_ size_t;
#include <sys/cdefs.h>
#include <sys/inttypes.h>
__BEGIN_DECLS
int bcmp(const void *, const void *, size_t);
void bcopy(const void *, void *, size_t);
@ -55,6 +57,8 @@ char *index(const char *, int);
unsigned int popcount(unsigned int) __constfunc;
unsigned int popcountl(unsigned long) __constfunc;
unsigned int popcountll(unsigned long long) __constfunc;
unsigned int popcount32(uint32_t) __constfunc;
unsigned int popcount64(uint64_t) __constfunc;
char *rindex(const char *, int);
int strcasecmp(const char *, const char *);
int strncasecmp(const char *, const char *, size_t);

View File

@ -1,10 +1,10 @@
# from: @(#)Makefile.inc 8.1 (Berkeley) 6/4/93
# $NetBSD: Makefile.inc,v 1.73 2009/07/21 13:18:43 joerg Exp $
# $NetBSD: Makefile.inc,v 1.74 2009/07/21 14:55:33 joerg Exp $
# string sources
.PATH: ${ARCHDIR}/string ${.CURDIR}/string
SRCS+= bm.c popcountl.c stpcpy.c stpncpy.c \
SRCS+= bm.c stpcpy.c stpncpy.c \
strcasecmp.c strncasecmp.c strcasestr.c strcoll.c strdup.c \
strerror.c strlcat.c strlcpy.c strnlen.c \
strmode.c strsignal.c strtok.c \
@ -55,11 +55,11 @@ SRCS+= strchr.c
.if empty(SRCS:Mstrrchr.S)
SRCS+= strrchr.c
.endif
.if empty(SRCS:Mpopcount.S)
SRCS+= popcount.c
.if empty(SRCS:Mpopcount32.S)
SRCS+= popcount32.c
.endif
.if empty(SRCS:Mpopcountll.S)
SRCS+= popcountll.c
.if empty(SRCS:Mpopcount64.S)
SRCS+= popcount64.c
.endif
MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 index.3 \
@ -74,6 +74,8 @@ MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 index.3 \
MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3
MLINKS+=popcount.3 popcountl.3
MLINKS+=popcount.3 popcountll.3
MLINKS+=popcount.3 popcount32.3
MLINKS+=popcount.3 popcount64.3
MLINKS+=strcasecmp.3 strncasecmp.3
MLINKS+=strcat.3 strncat.3
MLINKS+=strcmp.3 strncmp.3

View File

@ -1,4 +1,4 @@
.\" $NetBSD: popcount.3,v 1.2 2009/07/21 13:21:41 wiz Exp $
.\" $NetBSD: popcount.3,v 1.3 2009/07/21 14:55:33 joerg Exp $
.\"
.\" Copyright (c) 2009 The NetBSD Foundation, Inc.
.\" All rights reserved.
@ -33,7 +33,9 @@
.Sh NAME
.Nm popcount ,
.Nm popcountl ,
.Nm popcountll
.Nm popcountll ,
.Nm popcount32 ,
.Nm popcount64
.Nd count number of bits set in a bit string
.Sh LIBRARY
.Lb libc
@ -45,6 +47,10 @@
.Fn popcountl "unsigned long value"
.Ft unsigned int
.Fn popcountll "unsigned long long value"
.Ft unsigned int
.Fn popcount32 "uint32_t value"
.Ft unsigned int
.Fn popcount64 "uint64_t value"
.Sh DESCRIPTION
The
.Nm
@ -56,7 +62,9 @@ functions returns the number of bits set in
The
.Fn popcount ,
.Fn popcountl ,
.Fn popcountll ,
.Fn popcount32
and
.Fn popcountll
.Fn popcount64
functions appeared in
.Nx 6.0 .

View File

@ -1,46 +0,0 @@
/* $NetBSD: popcountl.c,v 1.1 2009/07/21 13:18:44 joerg Exp $ */
/*-
* Copyright (c) 2009 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Joerg Sonnenberger.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
* COPYRIGHT HOLDERS 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 <sys/cdefs.h>
__RCSID("$NetBSD: popcountl.c,v 1.1 2009/07/21 13:18:44 joerg Exp $");
#include <limits.h>
#include <strings.h>
#if ULONG_MAX == UINT_MAX
__strong_alias(popcountl, popcount);
#elif ULONG_MAX == ULLONG_MAX
__strong_alias(popcountll, popcount);
#else
#error "Unsupporting architecture"
#endif

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile.libkern,v 1.2 2009/03/25 01:26:13 darran Exp $
# $NetBSD: Makefile.libkern,v 1.3 2009/07/21 14:55:33 joerg Exp $
#
# Variable definitions for libkern.
@ -86,3 +86,13 @@ SRCS+= strchr.c
.if empty(SRCS:Mstrrchr.S)
SRCS+= strrchr.c
.endif
# if no machine specific popcount32(3), build generic version
.if empty(SRCS:Mpopcount32.S)
SRCS+= popcount32.c
.endif
# if no machine specific popcount64(3), build generic version
.if empty(SRCS:Mpopcount64.S)
SRCS+= popcount64.c
.endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: libkern.h,v 1.91 2009/05/13 02:50:32 pgoyette Exp $ */
/* $NetBSD: libkern.h,v 1.92 2009/07/21 14:55:33 joerg Exp $ */
/*-
* Copyright (c) 1992, 1993
@ -339,4 +339,9 @@ int snprintb_m(char *, size_t, const char *, uint64_t, size_t);
int kheapsort(void *, size_t, size_t, int (*)(const void *, const void *),
void *);
uint32_t crc32(uint32_t, const uint8_t *, size_t);
unsigned int popcount(unsigned int) __constfunc;
unsigned int popcountl(unsigned long) __constfunc;
unsigned int popcountll(unsigned long long) __constfunc;
unsigned int popcount32(uint32_t) __constfunc;
unsigned int popcount64(uint64_t) __constfunc;
#endif /* !_LIB_LIBKERN_LIBKERN_H_ */