168651Skris/*
268651Skris * Copyright 2007 Haiku Inc. All rights reserved.
368651Skris * Distributed under the terms of the MIT License.
468651Skris */
568651Skris#ifndef _FBSD_COMPAT_SYS_TYPES_H_
668651Skris#define _FBSD_COMPAT_SYS_TYPES_H_
768651Skris
868651Skris
968651Skris#include <posix/stdint.h>
1068651Skris#include <posix/sys/types.h>
1168651Skris
1268651Skris#include <sys/cdefs.h>
13296465Sdelphij
14296465Sdelphij#include <machine/endian.h>
15296465Sdelphij#include <sys/_types.h>
16296465Sdelphij
17296465Sdelphij
18296465Sdelphijtypedef int boolean_t;
19296465Sdelphijtypedef __const char* c_caddr_t;
20296465Sdelphijtypedef uint64_t u_quad_t;
21296465Sdelphij
22296465Sdelphij
23296465Sdelphij#ifdef __POPCNT__
24296465Sdelphij#define	__bitcount64(x)	__builtin_popcountll((u_int64_t)(x))
25296465Sdelphij#define	__bitcount32(x)	__builtin_popcount((u_int32_t)(x))
26296465Sdelphij#define	__bitcount16(x)	__builtin_popcount((u_int16_t)(x))
27296465Sdelphij#define	__bitcountl(x)	__builtin_popcountl((unsigned long)(x))
28296465Sdelphij#define	__bitcount(x)	__builtin_popcount((unsigned int)(x))
29296465Sdelphij#else
30296465Sdelphij/*
31296465Sdelphij * Population count algorithm using SWAR approach
32296465Sdelphij * - "SIMD Within A Register".
33296465Sdelphij */
34296465Sdelphijstatic __inline u_int16_t
35296465Sdelphij__bitcount16(u_int16_t _x)
36296465Sdelphij{
37296465Sdelphij
38296465Sdelphij	_x = (_x & 0x5555) + ((_x & 0xaaaa) >> 1);
39296465Sdelphij	_x = (_x & 0x3333) + ((_x & 0xcccc) >> 2);
40296465Sdelphij	_x = (_x + (_x >> 4)) & 0x0f0f;
41296465Sdelphij	_x = (_x + (_x >> 8)) & 0x00ff;
42296465Sdelphij	return (_x);
43296465Sdelphij}
44296465Sdelphij
45296465Sdelphijstatic __inline u_int32_t
4668651Skris__bitcount32(u_int32_t _x)
47{
48
49	_x = (_x & 0x55555555) + ((_x & 0xaaaaaaaa) >> 1);
50	_x = (_x & 0x33333333) + ((_x & 0xcccccccc) >> 2);
51	_x = (_x + (_x >> 4)) & 0x0f0f0f0f;
52	_x = (_x + (_x >> 8));
53	_x = (_x + (_x >> 16)) & 0x000000ff;
54	return (_x);
55}
56
57#ifdef __LP64__
58static __inline u_int64_t
59__bitcount64(u_int64_t _x)
60{
61
62	_x = (_x & 0x5555555555555555) + ((_x & 0xaaaaaaaaaaaaaaaa) >> 1);
63	_x = (_x & 0x3333333333333333) + ((_x & 0xcccccccccccccccc) >> 2);
64	_x = (_x + (_x >> 4)) & 0x0f0f0f0f0f0f0f0f;
65	_x = (_x + (_x >> 8));
66	_x = (_x + (_x >> 16));
67	_x = (_x + (_x >> 32)) & 0x000000ff;
68	return (_x);
69}
70
71#define	__bitcountl(x)	__bitcount64((unsigned long)(x))
72#else
73static __inline u_int64_t
74__bitcount64(u_int64_t _x)
75{
76
77	return (__bitcount32(_x >> 32) + __bitcount32(_x));
78}
79
80#define	__bitcountl(x)	__bitcount32((unsigned long)(x))
81#endif
82#define	__bitcount(x)	__bitcount32((unsigned int)(x))
83#endif
84
85
86#endif
87