10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0 21cfc4a9cSDave Chinner /* 31cfc4a9cSDave Chinner * Copyright (c) 2000-2005 Silicon Graphics, Inc. 41cfc4a9cSDave Chinner * All Rights Reserved. 51cfc4a9cSDave Chinner */ 61cfc4a9cSDave Chinner #include "xfs.h" 71cfc4a9cSDave Chinner #include "xfs_log_format.h" 81cfc4a9cSDave Chinner #include "xfs_bit.h" 91cfc4a9cSDave Chinner 101cfc4a9cSDave Chinner /* 111cfc4a9cSDave Chinner * XFS bit manipulation routines, used in non-realtime code. 121cfc4a9cSDave Chinner */ 131cfc4a9cSDave Chinner 141cfc4a9cSDave Chinner /* 151cfc4a9cSDave Chinner * Return whether bitmap is empty. 161cfc4a9cSDave Chinner * Size is number of words in the bitmap, which is padded to word boundary 171cfc4a9cSDave Chinner * Returns 1 for empty, 0 for non-empty. 181cfc4a9cSDave Chinner */ 191cfc4a9cSDave Chinner int 201cfc4a9cSDave Chinner xfs_bitmap_empty(uint *map, uint size) 211cfc4a9cSDave Chinner { 221cfc4a9cSDave Chinner uint i; 231cfc4a9cSDave Chinner 241cfc4a9cSDave Chinner for (i = 0; i < size; i++) { 251d4292bfSJia He if (map[i] != 0) 261d4292bfSJia He return 0; 271cfc4a9cSDave Chinner } 281cfc4a9cSDave Chinner 291d4292bfSJia He return 1; 301cfc4a9cSDave Chinner } 311cfc4a9cSDave Chinner 321cfc4a9cSDave Chinner /* 331cfc4a9cSDave Chinner * Count the number of contiguous bits set in the bitmap starting with bit 341cfc4a9cSDave Chinner * start_bit. Size is the size of the bitmap in words. 351cfc4a9cSDave Chinner */ 361cfc4a9cSDave Chinner int 371cfc4a9cSDave Chinner xfs_contig_bits(uint *map, uint size, uint start_bit) 381cfc4a9cSDave Chinner { 391cfc4a9cSDave Chinner uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT); 401cfc4a9cSDave Chinner uint result = 0; 411cfc4a9cSDave Chinner uint tmp; 421cfc4a9cSDave Chinner 431cfc4a9cSDave Chinner size <<= BIT_TO_WORD_SHIFT; 441cfc4a9cSDave Chinner 451cfc4a9cSDave Chinner ASSERT(start_bit < size); 461cfc4a9cSDave Chinner size -= start_bit & ~(NBWORD - 1); 471cfc4a9cSDave Chinner start_bit &= (NBWORD - 1); 481cfc4a9cSDave Chinner if (start_bit) { 491cfc4a9cSDave Chinner tmp = *p++; 501cfc4a9cSDave Chinner /* set to one first offset bits prior to start */ 511cfc4a9cSDave Chinner tmp |= (~0U >> (NBWORD-start_bit)); 521cfc4a9cSDave Chinner if (tmp != ~0U) 531cfc4a9cSDave Chinner goto found; 541cfc4a9cSDave Chinner result += NBWORD; 551cfc4a9cSDave Chinner size -= NBWORD; 561cfc4a9cSDave Chinner } 571cfc4a9cSDave Chinner while (size) { 581cfc4a9cSDave Chinner if ((tmp = *p++) != ~0U) 591cfc4a9cSDave Chinner goto found; 601cfc4a9cSDave Chinner result += NBWORD; 611cfc4a9cSDave Chinner size -= NBWORD; 621cfc4a9cSDave Chinner } 631cfc4a9cSDave Chinner return result - start_bit; 641cfc4a9cSDave Chinner found: 651cfc4a9cSDave Chinner return result + ffz(tmp) - start_bit; 661cfc4a9cSDave Chinner } 671cfc4a9cSDave Chinner 681cfc4a9cSDave Chinner /* 691cfc4a9cSDave Chinner * This takes the bit number to start looking from and 701cfc4a9cSDave Chinner * returns the next set bit from there. It returns -1 711cfc4a9cSDave Chinner * if there are no more bits set or the start bit is 721cfc4a9cSDave Chinner * beyond the end of the bitmap. 731cfc4a9cSDave Chinner * 741cfc4a9cSDave Chinner * Size is the number of words, not bytes, in the bitmap. 751cfc4a9cSDave Chinner */ 761cfc4a9cSDave Chinner int xfs_next_bit(uint *map, uint size, uint start_bit) 771cfc4a9cSDave Chinner { 781cfc4a9cSDave Chinner uint * p = ((unsigned int *) map) + (start_bit >> BIT_TO_WORD_SHIFT); 791cfc4a9cSDave Chinner uint result = start_bit & ~(NBWORD - 1); 801cfc4a9cSDave Chinner uint tmp; 811cfc4a9cSDave Chinner 821cfc4a9cSDave Chinner size <<= BIT_TO_WORD_SHIFT; 831cfc4a9cSDave Chinner 841cfc4a9cSDave Chinner if (start_bit >= size) 851cfc4a9cSDave Chinner return -1; 861cfc4a9cSDave Chinner size -= result; 871cfc4a9cSDave Chinner start_bit &= (NBWORD - 1); 881cfc4a9cSDave Chinner if (start_bit) { 891cfc4a9cSDave Chinner tmp = *p++; 901cfc4a9cSDave Chinner /* set to zero first offset bits prior to start */ 911cfc4a9cSDave Chinner tmp &= (~0U << start_bit); 921cfc4a9cSDave Chinner if (tmp != 0U) 931cfc4a9cSDave Chinner goto found; 941cfc4a9cSDave Chinner result += NBWORD; 951cfc4a9cSDave Chinner size -= NBWORD; 961cfc4a9cSDave Chinner } 971cfc4a9cSDave Chinner while (size) { 981cfc4a9cSDave Chinner if ((tmp = *p++) != 0U) 991cfc4a9cSDave Chinner goto found; 1001cfc4a9cSDave Chinner result += NBWORD; 1011cfc4a9cSDave Chinner size -= NBWORD; 1021cfc4a9cSDave Chinner } 1031cfc4a9cSDave Chinner return -1; 1041cfc4a9cSDave Chinner found: 1051cfc4a9cSDave Chinner return result + ffs(tmp) - 1; 1061cfc4a9cSDave Chinner } 107