162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (c) 2000,2002,2005 Silicon Graphics, Inc. 462306a36Sopenharmony_ci * All Rights Reserved. 562306a36Sopenharmony_ci */ 662306a36Sopenharmony_ci#ifndef __XFS_BIT_H__ 762306a36Sopenharmony_ci#define __XFS_BIT_H__ 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci/* 1062306a36Sopenharmony_ci * XFS bit manipulation routines. 1162306a36Sopenharmony_ci */ 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci/* 1462306a36Sopenharmony_ci * masks with n high/low bits set, 64-bit values 1562306a36Sopenharmony_ci */ 1662306a36Sopenharmony_cistatic inline uint64_t xfs_mask64hi(int n) 1762306a36Sopenharmony_ci{ 1862306a36Sopenharmony_ci return (uint64_t)-1 << (64 - (n)); 1962306a36Sopenharmony_ci} 2062306a36Sopenharmony_cistatic inline uint32_t xfs_mask32lo(int n) 2162306a36Sopenharmony_ci{ 2262306a36Sopenharmony_ci return ((uint32_t)1 << (n)) - 1; 2362306a36Sopenharmony_ci} 2462306a36Sopenharmony_cistatic inline uint64_t xfs_mask64lo(int n) 2562306a36Sopenharmony_ci{ 2662306a36Sopenharmony_ci return ((uint64_t)1 << (n)) - 1; 2762306a36Sopenharmony_ci} 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci/* Get high bit set out of 32-bit argument, -1 if none set */ 3062306a36Sopenharmony_cistatic inline int xfs_highbit32(uint32_t v) 3162306a36Sopenharmony_ci{ 3262306a36Sopenharmony_ci return fls(v) - 1; 3362306a36Sopenharmony_ci} 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci/* Get high bit set out of 64-bit argument, -1 if none set */ 3662306a36Sopenharmony_cistatic inline int xfs_highbit64(uint64_t v) 3762306a36Sopenharmony_ci{ 3862306a36Sopenharmony_ci return fls64(v) - 1; 3962306a36Sopenharmony_ci} 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci/* Get low bit set out of 32-bit argument, -1 if none set */ 4262306a36Sopenharmony_cistatic inline int xfs_lowbit32(uint32_t v) 4362306a36Sopenharmony_ci{ 4462306a36Sopenharmony_ci return ffs(v) - 1; 4562306a36Sopenharmony_ci} 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci/* Get low bit set out of 64-bit argument, -1 if none set */ 4862306a36Sopenharmony_cistatic inline int xfs_lowbit64(uint64_t v) 4962306a36Sopenharmony_ci{ 5062306a36Sopenharmony_ci uint32_t w = (uint32_t)v; 5162306a36Sopenharmony_ci int n = 0; 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci if (w) { /* lower bits */ 5462306a36Sopenharmony_ci n = ffs(w); 5562306a36Sopenharmony_ci } else { /* upper bits */ 5662306a36Sopenharmony_ci w = (uint32_t)(v >> 32); 5762306a36Sopenharmony_ci if (w) { 5862306a36Sopenharmony_ci n = ffs(w); 5962306a36Sopenharmony_ci if (n) 6062306a36Sopenharmony_ci n += 32; 6162306a36Sopenharmony_ci } 6262306a36Sopenharmony_ci } 6362306a36Sopenharmony_ci return n - 1; 6462306a36Sopenharmony_ci} 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci/* Return whether bitmap is empty (1 == empty) */ 6762306a36Sopenharmony_ciextern int xfs_bitmap_empty(uint *map, uint size); 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci/* Count continuous one bits in map starting with start_bit */ 7062306a36Sopenharmony_ciextern int xfs_contig_bits(uint *map, uint size, uint start_bit); 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci/* Find next set bit in map */ 7362306a36Sopenharmony_ciextern int xfs_next_bit(uint *map, uint size, uint start_bit); 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci#endif /* __XFS_BIT_H__ */ 76