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