xref: /minix3/common/lib/libc/string/popcount32.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1 /*	$NetBSD: popcount32.c,v 1.5 2015/05/29 19:39:41 matt Exp $	*/
2 /*-
3  * Copyright (c) 2009 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Joerg Sonnenberger.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: popcount32.c,v 1.5 2015/05/29 19:39:41 matt Exp $");
36 
37 #if !defined(_KERNEL) && !defined(_STANDALONE)
38 #include <limits.h>
39 #include <stdint.h>
40 #include <strings.h>
41 #else
42 #include <lib/libkern/libkern.h>
43 #include <machine/limits.h>
44 #endif
45 
46 #ifndef popcount32	// might be a builtin
47 
48 /*
49  * This a hybrid algorithm for bit counting between parallel counting and
50  * using multiplication.  The idea is to sum up the bits in each Byte, so
51  * that the final accumulation can be done with a single multiplication.
52  * If the platform has a slow multiplication instruction, it can be replaced
53  * by the commented out version below.
54  */
55 
56 unsigned int
popcount32(uint32_t v)57 popcount32(uint32_t v)
58 {
59 	unsigned int c;
60 
61 	v = v - ((v >> 1) & 0x55555555U);
62 	v = (v & 0x33333333U) + ((v >> 2) & 0x33333333U);
63 	v = (v + (v >> 4)) & 0x0f0f0f0fU;
64 	c = (v * 0x01010101U) >> 24;
65 	/*
66 	 * v = (v >> 16) + v;
67 	 * v = (v >> 8) + v;
68 	 * c = v & 255;
69 	 */
70 
71 	return c;
72 }
73 
74 #if UINT_MAX == 0xffffffffU
75 __strong_alias(popcount, popcount32)
76 #endif
77 
78 #if ULONG_MAX == 0xffffffffU
79 __strong_alias(popcountl, popcount32)
80 #endif
81 
82 #endif	/* !popcount32 */
83