source: rtems-libbsd/rtemsbsd/include/machine/bitstring.h @ fb2aa6e

55-freebsd-126-freebsd-12
Last change on this file since fb2aa6e was fb2aa6e, checked in by Kevin Kirspel <kevin-kirspel@…>, on 05/17/17 at 12:40:32

Add bitcount inlinesfor RTEMS. These are found in FREEBSDs types.h

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*-
2 * Copyright (c) 1982, 1986, 1991, 1993, 1994
3 *      The Regents of the University of California.  All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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 the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 *      @(#)types.h     8.6 (Berkeley) 2/19/95
35 * $FreeBSD$
36 */
37
38#ifndef _RTEMS_BSD_MACHINE_BITSTRING_H_
39#define _RTEMS_BSD_MACHINE_BITSTRING_H_
40
41#ifndef _SYS_BITSTRING_H_
42#error "the header file <sys/bitstring.h> must be included first"
43#endif
44
45#ifdef __POPCNT__
46#define __bitcount64(x) __builtin_popcountll((__uint64_t)(x))
47#define __bitcount32(x) __builtin_popcount((__uint32_t)(x))
48#define __bitcount16(x) __builtin_popcount((__uint16_t)(x))
49#define __bitcountl(x)  __builtin_popcountl((unsigned long)(x))
50#define __bitcount(x)   __builtin_popcount((unsigned int)(x))
51#else
52/*
53 * Population count algorithm using SWAR approach
54 * - "SIMD Within A Register".
55 */
56static __inline __uint16_t
57__bitcount16(__uint16_t _x)
58{
59
60        _x = (_x & 0x5555) + ((_x & 0xaaaa) >> 1);
61        _x = (_x & 0x3333) + ((_x & 0xcccc) >> 2);
62        _x = (_x + (_x >> 4)) & 0x0f0f;
63        _x = (_x + (_x >> 8)) & 0x00ff;
64        return (_x);
65}
66
67static __inline __uint32_t
68__bitcount32(__uint32_t _x)
69{
70
71        _x = (_x & 0x55555555) + ((_x & 0xaaaaaaaa) >> 1);
72        _x = (_x & 0x33333333) + ((_x & 0xcccccccc) >> 2);
73        _x = (_x + (_x >> 4)) & 0x0f0f0f0f;
74        _x = (_x + (_x >> 8));
75        _x = (_x + (_x >> 16)) & 0x000000ff;
76        return (_x);
77}
78
79#ifdef __LP64__
80static __inline __uint64_t
81__bitcount64(__uint64_t _x)
82{
83
84        _x = (_x & 0x5555555555555555) + ((_x & 0xaaaaaaaaaaaaaaaa) >> 1);
85        _x = (_x & 0x3333333333333333) + ((_x & 0xcccccccccccccccc) >> 2);
86        _x = (_x + (_x >> 4)) & 0x0f0f0f0f0f0f0f0f;
87        _x = (_x + (_x >> 8));
88        _x = (_x + (_x >> 16));
89        _x = (_x + (_x >> 32)) & 0x000000ff;
90        return (_x);
91}
92
93#define __bitcountl(x)  __bitcount64((unsigned long)(x))
94#else
95static __inline __uint64_t
96__bitcount64(__uint64_t _x)
97{
98
99        return (__bitcount32(_x >> 32) + __bitcount32(_x));
100}
101
102#define __bitcountl(x)  __bitcount32((unsigned long)(x))
103#endif
104#define __bitcount(x)   __bitcount32((unsigned int)(x))
105#endif
106
107#endif /* !_RTEMS_BSD_MACHINE_BITSTRING_H_ */
Note: See TracBrowser for help on using the repository browser.