xref: /openbsd-src/sys/dev/pci/drm/include/linux/bitops.h (revision 1bb76ff151c0aba8e3312a604e4cd2e5195cf4b7)
1*1bb76ff1Sjsg /*	$OpenBSD: bitops.h,v 1.5 2023/01/01 01:34:58 jsg Exp $	*/
27f4dd379Sjsg /*
37f4dd379Sjsg  * Copyright (c) 2013, 2014, 2015 Mark Kettenis
47f4dd379Sjsg  *
57f4dd379Sjsg  * Permission to use, copy, modify, and distribute this software for any
67f4dd379Sjsg  * purpose with or without fee is hereby granted, provided that the above
77f4dd379Sjsg  * copyright notice and this permission notice appear in all copies.
87f4dd379Sjsg  *
97f4dd379Sjsg  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
107f4dd379Sjsg  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
117f4dd379Sjsg  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
127f4dd379Sjsg  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
137f4dd379Sjsg  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
147f4dd379Sjsg  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
157f4dd379Sjsg  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167f4dd379Sjsg  */
177f4dd379Sjsg 
187f4dd379Sjsg #ifndef _LINUX_BITOPS_H
197f4dd379Sjsg #define _LINUX_BITOPS_H
207f4dd379Sjsg 
217f4dd379Sjsg #include <sys/types.h>
227f4dd379Sjsg #include <sys/param.h>
235ca02815Sjsg #include <lib/libkern/libkern.h>
245ca02815Sjsg 
257f4dd379Sjsg #include <asm/bitsperlong.h>
267f4dd379Sjsg #include <linux/atomic.h>
277f4dd379Sjsg 
287f4dd379Sjsg #define BIT(x)		(1UL << (x))
297f4dd379Sjsg #define BIT_ULL(x)	(1ULL << (x))
30c349dbc7Sjsg #define BIT_MASK(x)	(1UL << ((x) % BITS_PER_LONG))
317f4dd379Sjsg #define BITS_PER_BYTE	8
327f4dd379Sjsg 
337f4dd379Sjsg #define GENMASK(h, l)		(((~0UL) >> (BITS_PER_LONG - (h) - 1)) & ((~0UL) << (l)))
347f4dd379Sjsg #define GENMASK_ULL(h, l)	(((~0ULL) >> (BITS_PER_LONG_LONG - (h) - 1)) & ((~0ULL) << (l)))
357f4dd379Sjsg 
36c349dbc7Sjsg #define BITS_PER_TYPE(x)	(8 * sizeof(x))
377f4dd379Sjsg #define BITS_TO_LONGS(x)	howmany((x), 8 * sizeof(long))
387f4dd379Sjsg 
39c349dbc7Sjsg /* despite the name these are really ctz */
40c349dbc7Sjsg #define __ffs(x)		__builtin_ctzl(x)
41c349dbc7Sjsg #define __ffs64(x)		__builtin_ctzll(x)
42*1bb76ff1Sjsg #define ffz(x)			__ffs(~(x))
43c349dbc7Sjsg 
447f4dd379Sjsg static inline uint8_t
hweight8(uint32_t x)457f4dd379Sjsg hweight8(uint32_t x)
467f4dd379Sjsg {
477f4dd379Sjsg 	x = (x & 0x55) + ((x & 0xaa) >> 1);
487f4dd379Sjsg 	x = (x & 0x33) + ((x & 0xcc) >> 2);
497f4dd379Sjsg 	x = (x + (x >> 4)) & 0x0f;
507f4dd379Sjsg 	return (x);
517f4dd379Sjsg }
527f4dd379Sjsg 
537f4dd379Sjsg static inline uint16_t
hweight16(uint32_t x)547f4dd379Sjsg hweight16(uint32_t x)
557f4dd379Sjsg {
567f4dd379Sjsg 	x = (x & 0x5555) + ((x & 0xaaaa) >> 1);
577f4dd379Sjsg 	x = (x & 0x3333) + ((x & 0xcccc) >> 2);
587f4dd379Sjsg 	x = (x + (x >> 4)) & 0x0f0f;
597f4dd379Sjsg 	x = (x + (x >> 8)) & 0x00ff;
607f4dd379Sjsg 	return (x);
617f4dd379Sjsg }
627f4dd379Sjsg 
637f4dd379Sjsg static inline uint32_t
hweight32(uint32_t x)647f4dd379Sjsg hweight32(uint32_t x)
657f4dd379Sjsg {
667f4dd379Sjsg 	x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
677f4dd379Sjsg 	x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
687f4dd379Sjsg 	x = (x + (x >> 4)) & 0x0f0f0f0f;
697f4dd379Sjsg 	x = (x + (x >> 8));
707f4dd379Sjsg 	x = (x + (x >> 16)) & 0x000000ff;
717f4dd379Sjsg 	return x;
727f4dd379Sjsg }
737f4dd379Sjsg 
747f4dd379Sjsg static inline uint32_t
hweight64(uint64_t x)757f4dd379Sjsg hweight64(uint64_t x)
767f4dd379Sjsg {
777f4dd379Sjsg 	x = (x & 0x5555555555555555ULL) + ((x & 0xaaaaaaaaaaaaaaaaULL) >> 1);
787f4dd379Sjsg 	x = (x & 0x3333333333333333ULL) + ((x & 0xccccccccccccccccULL) >> 2);
797f4dd379Sjsg 	x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
807f4dd379Sjsg 	x = (x + (x >> 8));
817f4dd379Sjsg 	x = (x + (x >> 16));
827f4dd379Sjsg 	x = (x + (x >> 32)) & 0x000000ff;
837f4dd379Sjsg 	return x;
847f4dd379Sjsg }
857f4dd379Sjsg 
865ca02815Sjsg static inline unsigned long
hweight_long(unsigned long x)875ca02815Sjsg hweight_long(unsigned long x)
885ca02815Sjsg {
895ca02815Sjsg #ifdef __LP64__
905ca02815Sjsg 	return hweight64(x);
915ca02815Sjsg #else
925ca02815Sjsg 	return hweight32(x);
935ca02815Sjsg #endif
945ca02815Sjsg }
955ca02815Sjsg 
96978220acSjsg static inline int64_t
sign_extend64(uint64_t value,int index)977f4dd379Sjsg sign_extend64(uint64_t value, int index)
987f4dd379Sjsg {
997f4dd379Sjsg 	uint8_t shift = 63 - index;
100978220acSjsg 	return (int64_t)(value << shift) >> shift;
1017f4dd379Sjsg }
1027f4dd379Sjsg 
1037f4dd379Sjsg static inline int
fls64(long long mask)1047f4dd379Sjsg fls64(long long mask)
1057f4dd379Sjsg {
1067f4dd379Sjsg 	int bit;
1077f4dd379Sjsg 
1087f4dd379Sjsg 	if (mask == 0)
1097f4dd379Sjsg 		return (0);
1107f4dd379Sjsg 	for (bit = 1; mask != 1; bit++)
1117f4dd379Sjsg 		mask = (unsigned long long)mask >> 1;
1127f4dd379Sjsg 	return (bit);
1137f4dd379Sjsg }
1147f4dd379Sjsg 
1155ca02815Sjsg static inline int
__fls(long mask)1165ca02815Sjsg __fls(long mask)
1175ca02815Sjsg {
1185ca02815Sjsg 	return (flsl(mask) - 1);
1195ca02815Sjsg }
1205ca02815Sjsg 
1217f4dd379Sjsg static inline uint32_t
ror32(uint32_t word,unsigned int shift)1227f4dd379Sjsg ror32(uint32_t word, unsigned int shift)
1237f4dd379Sjsg {
1247f4dd379Sjsg 	return (word >> shift) | (word << (32 - shift));
1257f4dd379Sjsg }
1267f4dd379Sjsg 
1277f4dd379Sjsg #endif
128