find_bit.c (0337966d121ebebf73a1c346123e8112796e684e) | find_bit.c (0ade34c37012ea5c516d9aa4d19a56e9f40a55ed) |
---|---|
1/* bit search implementation 2 * 3 * Copied from lib/find_bit.c to tools/lib/find_bit.c 4 * 5 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved. 6 * Written by David Howells (dhowells@redhat.com) 7 * 8 * Copyright (C) 2008 IBM Corporation --- 8 unchanged lines hidden (view full) --- 17 * as published by the Free Software Foundation; either version 18 * 2 of the License, or (at your option) any later version. 19 */ 20 21#include <linux/bitops.h> 22#include <linux/bitmap.h> 23#include <linux/kernel.h> 24 | 1/* bit search implementation 2 * 3 * Copied from lib/find_bit.c to tools/lib/find_bit.c 4 * 5 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved. 6 * Written by David Howells (dhowells@redhat.com) 7 * 8 * Copyright (C) 2008 IBM Corporation --- 8 unchanged lines hidden (view full) --- 17 * as published by the Free Software Foundation; either version 18 * 2 of the License, or (at your option) any later version. 19 */ 20 21#include <linux/bitops.h> 22#include <linux/bitmap.h> 23#include <linux/kernel.h> 24 |
25#if !defined(find_next_bit) | 25#if !defined(find_next_bit) || !defined(find_next_zero_bit) || \ 26 !defined(find_next_and_bit) |
26 27/* | 27 28/* |
28 * This is a common helper function for find_next_bit and 29 * find_next_zero_bit. The difference is the "invert" argument, which 30 * is XORed with each fetched word before searching it for one bits. | 29 * This is a common helper function for find_next_bit, find_next_zero_bit, and 30 * find_next_and_bit. The differences are: 31 * - The "invert" argument, which is XORed with each fetched word before 32 * searching it for one bits. 33 * - The optional "addr2", which is anded with "addr1" if present. |
31 */ | 34 */ |
32static unsigned long _find_next_bit(const unsigned long *addr, 33 unsigned long nbits, unsigned long start, unsigned long invert) | 35static inline unsigned long _find_next_bit(const unsigned long *addr1, 36 const unsigned long *addr2, unsigned long nbits, 37 unsigned long start, unsigned long invert) |
34{ 35 unsigned long tmp; 36 37 if (unlikely(start >= nbits)) 38 return nbits; 39 | 38{ 39 unsigned long tmp; 40 41 if (unlikely(start >= nbits)) 42 return nbits; 43 |
40 tmp = addr[start / BITS_PER_LONG] ^ invert; | 44 tmp = addr1[start / BITS_PER_LONG]; 45 if (addr2) 46 tmp &= addr2[start / BITS_PER_LONG]; 47 tmp ^= invert; |
41 42 /* Handle 1st word. */ 43 tmp &= BITMAP_FIRST_WORD_MASK(start); 44 start = round_down(start, BITS_PER_LONG); 45 46 while (!tmp) { 47 start += BITS_PER_LONG; 48 if (start >= nbits) 49 return nbits; 50 | 48 49 /* Handle 1st word. */ 50 tmp &= BITMAP_FIRST_WORD_MASK(start); 51 start = round_down(start, BITS_PER_LONG); 52 53 while (!tmp) { 54 start += BITS_PER_LONG; 55 if (start >= nbits) 56 return nbits; 57 |
51 tmp = addr[start / BITS_PER_LONG] ^ invert; | 58 tmp = addr1[start / BITS_PER_LONG]; 59 if (addr2) 60 tmp &= addr2[start / BITS_PER_LONG]; 61 tmp ^= invert; |
52 } 53 54 return min(start + __ffs(tmp), nbits); 55} 56#endif 57 58#ifndef find_next_bit 59/* 60 * Find the next set bit in a memory region. 61 */ 62unsigned long find_next_bit(const unsigned long *addr, unsigned long size, 63 unsigned long offset) 64{ | 62 } 63 64 return min(start + __ffs(tmp), nbits); 65} 66#endif 67 68#ifndef find_next_bit 69/* 70 * Find the next set bit in a memory region. 71 */ 72unsigned long find_next_bit(const unsigned long *addr, unsigned long size, 73 unsigned long offset) 74{ |
65 return _find_next_bit(addr, size, offset, 0UL); | 75 return _find_next_bit(addr, NULL, size, offset, 0UL); |
66} 67#endif 68 69#ifndef find_first_bit 70/* 71 * Find the first set bit in a memory region. 72 */ 73unsigned long find_first_bit(const unsigned long *addr, unsigned long size) --- 25 unchanged lines hidden (view full) --- 99 return size; 100} 101#endif 102 103#ifndef find_next_zero_bit 104unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, 105 unsigned long offset) 106{ | 76} 77#endif 78 79#ifndef find_first_bit 80/* 81 * Find the first set bit in a memory region. 82 */ 83unsigned long find_first_bit(const unsigned long *addr, unsigned long size) --- 25 unchanged lines hidden (view full) --- 109 return size; 110} 111#endif 112 113#ifndef find_next_zero_bit 114unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, 115 unsigned long offset) 116{ |
107 return _find_next_bit(addr, size, offset, ~0UL); | 117 return _find_next_bit(addr, NULL, size, offset, ~0UL); |
108} 109#endif | 118} 119#endif |
120 121#ifndef find_next_and_bit 122unsigned long find_next_and_bit(const unsigned long *addr1, 123 const unsigned long *addr2, unsigned long size, 124 unsigned long offset) 125{ 126 return _find_next_bit(addr1, addr2, size, offset, 0UL); 127} 128#endif |
|