18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2000,2002,2005 Silicon Graphics, Inc.
48c2ecf20Sopenharmony_ci * All Rights Reserved.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci#ifndef __XFS_BIT_H__
78c2ecf20Sopenharmony_ci#define	__XFS_BIT_H__
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci/*
108c2ecf20Sopenharmony_ci * XFS bit manipulation routines.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci/*
148c2ecf20Sopenharmony_ci * masks with n high/low bits set, 64-bit values
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_cistatic inline uint64_t xfs_mask64hi(int n)
178c2ecf20Sopenharmony_ci{
188c2ecf20Sopenharmony_ci	return (uint64_t)-1 << (64 - (n));
198c2ecf20Sopenharmony_ci}
208c2ecf20Sopenharmony_cistatic inline uint32_t xfs_mask32lo(int n)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	return ((uint32_t)1 << (n)) - 1;
238c2ecf20Sopenharmony_ci}
248c2ecf20Sopenharmony_cistatic inline uint64_t xfs_mask64lo(int n)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	return ((uint64_t)1 << (n)) - 1;
278c2ecf20Sopenharmony_ci}
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/* Get high bit set out of 32-bit argument, -1 if none set */
308c2ecf20Sopenharmony_cistatic inline int xfs_highbit32(uint32_t v)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	return fls(v) - 1;
338c2ecf20Sopenharmony_ci}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* Get high bit set out of 64-bit argument, -1 if none set */
368c2ecf20Sopenharmony_cistatic inline int xfs_highbit64(uint64_t v)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	return fls64(v) - 1;
398c2ecf20Sopenharmony_ci}
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* Get low bit set out of 32-bit argument, -1 if none set */
428c2ecf20Sopenharmony_cistatic inline int xfs_lowbit32(uint32_t v)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	return ffs(v) - 1;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/* Get low bit set out of 64-bit argument, -1 if none set */
488c2ecf20Sopenharmony_cistatic inline int xfs_lowbit64(uint64_t v)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	uint32_t	w = (uint32_t)v;
518c2ecf20Sopenharmony_ci	int		n = 0;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	if (w) {	/* lower bits */
548c2ecf20Sopenharmony_ci		n = ffs(w);
558c2ecf20Sopenharmony_ci	} else {	/* upper bits */
568c2ecf20Sopenharmony_ci		w = (uint32_t)(v >> 32);
578c2ecf20Sopenharmony_ci		if (w) {
588c2ecf20Sopenharmony_ci			n = ffs(w);
598c2ecf20Sopenharmony_ci			if (n)
608c2ecf20Sopenharmony_ci				n += 32;
618c2ecf20Sopenharmony_ci		}
628c2ecf20Sopenharmony_ci	}
638c2ecf20Sopenharmony_ci	return n - 1;
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/* Return whether bitmap is empty (1 == empty) */
678c2ecf20Sopenharmony_ciextern int xfs_bitmap_empty(uint *map, uint size);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/* Count continuous one bits in map starting with start_bit */
708c2ecf20Sopenharmony_ciextern int xfs_contig_bits(uint *map, uint size, uint start_bit);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/* Find next set bit in map */
738c2ecf20Sopenharmony_ciextern int xfs_next_bit(uint *map, uint size, uint start_bit);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#endif	/* __XFS_BIT_H__ */
76