162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (c) 2000-2005 Silicon Graphics, Inc. 462306a36Sopenharmony_ci * All Rights Reserved. 562306a36Sopenharmony_ci */ 662306a36Sopenharmony_ci#include "xfs.h" 762306a36Sopenharmony_ci#include "xfs_log_format.h" 862306a36Sopenharmony_ci#include "xfs_bit.h" 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci/* 1162306a36Sopenharmony_ci * XFS bit manipulation routines, used in non-realtime code. 1262306a36Sopenharmony_ci */ 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci/* 1562306a36Sopenharmony_ci * Return whether bitmap is empty. 1662306a36Sopenharmony_ci * Size is number of words in the bitmap, which is padded to word boundary 1762306a36Sopenharmony_ci * Returns 1 for empty, 0 for non-empty. 1862306a36Sopenharmony_ci */ 1962306a36Sopenharmony_ciint 2062306a36Sopenharmony_cixfs_bitmap_empty(uint *map, uint size) 2162306a36Sopenharmony_ci{ 2262306a36Sopenharmony_ci uint i; 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci for (i = 0; i < size; i++) { 2562306a36Sopenharmony_ci if (map[i] != 0) 2662306a36Sopenharmony_ci return 0; 2762306a36Sopenharmony_ci } 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci return 1; 3062306a36Sopenharmony_ci} 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci/* 3362306a36Sopenharmony_ci * Count the number of contiguous bits set in the bitmap starting with bit 3462306a36Sopenharmony_ci * start_bit. Size is the size of the bitmap in words. 3562306a36Sopenharmony_ci */ 3662306a36Sopenharmony_ciint 3762306a36Sopenharmony_cixfs_contig_bits(uint *map, uint size, uint start_bit) 3862306a36Sopenharmony_ci{ 3962306a36Sopenharmony_ci uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT); 4062306a36Sopenharmony_ci uint result = 0; 4162306a36Sopenharmony_ci uint tmp; 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ci size <<= BIT_TO_WORD_SHIFT; 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci ASSERT(start_bit < size); 4662306a36Sopenharmony_ci size -= start_bit & ~(NBWORD - 1); 4762306a36Sopenharmony_ci start_bit &= (NBWORD - 1); 4862306a36Sopenharmony_ci if (start_bit) { 4962306a36Sopenharmony_ci tmp = *p++; 5062306a36Sopenharmony_ci /* set to one first offset bits prior to start */ 5162306a36Sopenharmony_ci tmp |= (~0U >> (NBWORD-start_bit)); 5262306a36Sopenharmony_ci if (tmp != ~0U) 5362306a36Sopenharmony_ci goto found; 5462306a36Sopenharmony_ci result += NBWORD; 5562306a36Sopenharmony_ci size -= NBWORD; 5662306a36Sopenharmony_ci } 5762306a36Sopenharmony_ci while (size) { 5862306a36Sopenharmony_ci if ((tmp = *p++) != ~0U) 5962306a36Sopenharmony_ci goto found; 6062306a36Sopenharmony_ci result += NBWORD; 6162306a36Sopenharmony_ci size -= NBWORD; 6262306a36Sopenharmony_ci } 6362306a36Sopenharmony_ci return result - start_bit; 6462306a36Sopenharmony_cifound: 6562306a36Sopenharmony_ci return result + ffz(tmp) - start_bit; 6662306a36Sopenharmony_ci} 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci/* 6962306a36Sopenharmony_ci * This takes the bit number to start looking from and 7062306a36Sopenharmony_ci * returns the next set bit from there. It returns -1 7162306a36Sopenharmony_ci * if there are no more bits set or the start bit is 7262306a36Sopenharmony_ci * beyond the end of the bitmap. 7362306a36Sopenharmony_ci * 7462306a36Sopenharmony_ci * Size is the number of words, not bytes, in the bitmap. 7562306a36Sopenharmony_ci */ 7662306a36Sopenharmony_ciint xfs_next_bit(uint *map, uint size, uint start_bit) 7762306a36Sopenharmony_ci{ 7862306a36Sopenharmony_ci uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT); 7962306a36Sopenharmony_ci uint result = start_bit & ~(NBWORD - 1); 8062306a36Sopenharmony_ci uint tmp; 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci size <<= BIT_TO_WORD_SHIFT; 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci if (start_bit >= size) 8562306a36Sopenharmony_ci return -1; 8662306a36Sopenharmony_ci size -= result; 8762306a36Sopenharmony_ci start_bit &= (NBWORD - 1); 8862306a36Sopenharmony_ci if (start_bit) { 8962306a36Sopenharmony_ci tmp = *p++; 9062306a36Sopenharmony_ci /* set to zero first offset bits prior to start */ 9162306a36Sopenharmony_ci tmp &= (~0U << start_bit); 9262306a36Sopenharmony_ci if (tmp != 0U) 9362306a36Sopenharmony_ci goto found; 9462306a36Sopenharmony_ci result += NBWORD; 9562306a36Sopenharmony_ci size -= NBWORD; 9662306a36Sopenharmony_ci } 9762306a36Sopenharmony_ci while (size) { 9862306a36Sopenharmony_ci if ((tmp = *p++) != 0U) 9962306a36Sopenharmony_ci goto found; 10062306a36Sopenharmony_ci result += NBWORD; 10162306a36Sopenharmony_ci size -= NBWORD; 10262306a36Sopenharmony_ci } 10362306a36Sopenharmony_ci return -1; 10462306a36Sopenharmony_cifound: 10562306a36Sopenharmony_ci return result + ffs(tmp) - 1; 10662306a36Sopenharmony_ci} 107