xref: /minix3/common/lib/libc/string/popcount64.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: popcount64.c,v 1.8 2015/05/29 19:39:41 matt Exp $	*/
2b6cbf720SGianluca Guida /*-
3b6cbf720SGianluca Guida  * Copyright (c) 2009 The NetBSD Foundation, Inc.
4b6cbf720SGianluca Guida  * All rights reserved.
5b6cbf720SGianluca Guida  *
6b6cbf720SGianluca Guida  * This code is derived from software contributed to The NetBSD Foundation
7b6cbf720SGianluca Guida  * by Joerg Sonnenberger.
8b6cbf720SGianluca Guida  *
9b6cbf720SGianluca Guida  * Redistribution and use in source and binary forms, with or without
10b6cbf720SGianluca Guida  * modification, are permitted provided that the following conditions
11b6cbf720SGianluca Guida  * are met:
12b6cbf720SGianluca Guida  *
13b6cbf720SGianluca Guida  * 1. Redistributions of source code must retain the above copyright
14b6cbf720SGianluca Guida  *    notice, this list of conditions and the following disclaimer.
15b6cbf720SGianluca Guida  * 2. Redistributions in binary form must reproduce the above copyright
16b6cbf720SGianluca Guida  *    notice, this list of conditions and the following disclaimer in
17b6cbf720SGianluca Guida  *    the documentation and/or other materials provided with the
18b6cbf720SGianluca Guida  *    distribution.
19b6cbf720SGianluca Guida  *
20b6cbf720SGianluca Guida  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21b6cbf720SGianluca Guida  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22b6cbf720SGianluca Guida  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23b6cbf720SGianluca Guida  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24b6cbf720SGianluca Guida  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25b6cbf720SGianluca Guida  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26b6cbf720SGianluca Guida  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27b6cbf720SGianluca Guida  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28b6cbf720SGianluca Guida  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29b6cbf720SGianluca Guida  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30b6cbf720SGianluca Guida  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b6cbf720SGianluca Guida  * SUCH DAMAGE.
32b6cbf720SGianluca Guida  */
33b6cbf720SGianluca Guida 
34b6cbf720SGianluca Guida #include <sys/cdefs.h>
35*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: popcount64.c,v 1.8 2015/05/29 19:39:41 matt Exp $");
36b6cbf720SGianluca Guida 
37b6cbf720SGianluca Guida #if !defined(_KERNEL) && !defined(_STANDALONE)
38b6cbf720SGianluca Guida #include <limits.h>
39f14fb602SLionel Sambuc #include <stdint.h>
40b6cbf720SGianluca Guida #include <strings.h>
41b6cbf720SGianluca Guida #else
42b6cbf720SGianluca Guida #include <lib/libkern/libkern.h>
43b6cbf720SGianluca Guida #include <machine/limits.h>
44b6cbf720SGianluca Guida #endif
45b6cbf720SGianluca Guida 
46*0a6a1f1dSLionel Sambuc #ifndef popcount64	// might be defined to use a __builtin
47*0a6a1f1dSLionel Sambuc 
48b6cbf720SGianluca Guida /*
49b6cbf720SGianluca Guida  * If uint64_t is larger than size_t, the follow assumes that
50*0a6a1f1dSLionel Sambuc  * splitting into 32bit halves is faster.
51b6cbf720SGianluca Guida  *
52b6cbf720SGianluca Guida  * The native pocount64 version is based on the same ideas as popcount32(3),
53b6cbf720SGianluca Guida  * see popcount32.c for comments.
54b6cbf720SGianluca Guida  */
55b6cbf720SGianluca Guida 
56b6cbf720SGianluca Guida #if SIZE_MAX < 0xffffffffffffffffULL
57b6cbf720SGianluca Guida unsigned int
popcount64(uint64_t v)58b6cbf720SGianluca Guida popcount64(uint64_t v)
59b6cbf720SGianluca Guida {
60b6cbf720SGianluca Guida 	return popcount32((uint32_t)(v >> 32)) +
61b6cbf720SGianluca Guida 	    popcount32((uint32_t)(v & 0xffffffffULL));
62b6cbf720SGianluca Guida }
63b6cbf720SGianluca Guida #else
64b6cbf720SGianluca Guida unsigned int
popcount64(uint64_t v)65b6cbf720SGianluca Guida popcount64(uint64_t v)
66b6cbf720SGianluca Guida {
67b6cbf720SGianluca Guida 	unsigned int c;
68b6cbf720SGianluca Guida 
69f14fb602SLionel Sambuc 	v = v - ((v >> 1) & (uint64_t)0x5555555555555555ULL);
70f14fb602SLionel Sambuc 	v = (v & (uint64_t)0x3333333333333333ULL) +
71f14fb602SLionel Sambuc 	    ((v >> 2) & (uint64_t)0x3333333333333333ULL);
72f14fb602SLionel Sambuc 	v = ((v + (v >> 4)) & (uint64_t)0x0f0f0f0f0f0f0f0fULL) *
73f14fb602SLionel Sambuc 	    (uint64_t)0x0101010101010101ULL;
74f14fb602SLionel Sambuc 	c = (unsigned int)(v >> 56);
75b6cbf720SGianluca Guida 
76b6cbf720SGianluca Guida 	return c;
77b6cbf720SGianluca Guida }
78b6cbf720SGianluca Guida #endif
79b6cbf720SGianluca Guida 
80b6cbf720SGianluca Guida #if ULONG_MAX == 0xffffffffffffffffULL
81b6cbf720SGianluca Guida __strong_alias(popcountl, popcount64)
82b6cbf720SGianluca Guida #endif
83b6cbf720SGianluca Guida 
84b6cbf720SGianluca Guida #if ULLONG_MAX == 0xffffffffffffffffULL
85b6cbf720SGianluca Guida __strong_alias(popcountll, popcount64)
86b6cbf720SGianluca Guida #endif
87b6cbf720SGianluca Guida 
880cdf705cSLionel Sambuc #if defined(__minix)
890cdf705cSLionel Sambuc __strong_alias(popcountti2, popcount64)
90*0a6a1f1dSLionel Sambuc #endif /* defined(__minix) */
91*0a6a1f1dSLionel Sambuc 
92*0a6a1f1dSLionel Sambuc #endif /* !popcount64 */
93