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