| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _I386_BITOPS_H | 
|  | 2 | #define _I386_BITOPS_H | 
|  | 3 |  | 
|  | 4 | /* | 
|  | 5 | * Copyright 1992, Linus Torvalds. | 
|  | 6 | */ | 
|  | 7 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | /** | 
|  | 9 | * find_first_zero_bit - find the first zero bit in a memory region | 
|  | 10 | * @addr: The address to start the search at | 
|  | 11 | * @size: The maximum size to search | 
|  | 12 | * | 
| Randy Dunlap | ec12fa5 | 2008-01-30 13:30:32 +0100 | [diff] [blame] | 13 | * Returns the bit number of the first zero bit, not the number of the byte | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * containing a bit. | 
|  | 15 | */ | 
|  | 16 | static inline int find_first_zero_bit(const unsigned long *addr, unsigned size) | 
|  | 17 | { | 
|  | 18 | int d0, d1, d2; | 
|  | 19 | int res; | 
|  | 20 |  | 
|  | 21 | if (!size) | 
|  | 22 | return 0; | 
| Joe Perches | fd591ac | 2008-03-23 01:01:43 -0700 | [diff] [blame] | 23 | /* This looks at memory. | 
|  | 24 | * Mark it volatile to tell gcc not to move it around | 
|  | 25 | */ | 
|  | 26 | asm volatile("movl $-1,%%eax\n\t" | 
|  | 27 | "xorl %%edx,%%edx\n\t" | 
|  | 28 | "repe; scasl\n\t" | 
|  | 29 | "je 1f\n\t" | 
|  | 30 | "xorl -4(%%edi),%%eax\n\t" | 
|  | 31 | "subl $4,%%edi\n\t" | 
|  | 32 | "bsfl %%eax,%%edx\n" | 
|  | 33 | "1:\tsubl %%ebx,%%edi\n\t" | 
|  | 34 | "shll $3,%%edi\n\t" | 
|  | 35 | "addl %%edi,%%edx" | 
|  | 36 | : "=d" (res), "=&c" (d0), "=&D" (d1), "=&a" (d2) | 
|  | 37 | : "1" ((size + 31) >> 5), "2" (addr), | 
|  | 38 | "b" (addr) : "memory"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | return res; | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | /** | 
|  | 43 | * find_next_zero_bit - find the first zero bit in a memory region | 
|  | 44 | * @addr: The address to base the search on | 
| Randy Dunlap | ec12fa5 | 2008-01-30 13:30:32 +0100 | [diff] [blame] | 45 | * @offset: The bit number to start searching at | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | * @size: The maximum size to search | 
|  | 47 | */ | 
|  | 48 | int find_next_zero_bit(const unsigned long *addr, int size, int offset); | 
|  | 49 |  | 
|  | 50 | /** | 
| Steven Rostedt | cd85c8b | 2005-07-28 08:45:06 -0400 | [diff] [blame] | 51 | * __ffs - find first bit in word. | 
|  | 52 | * @word: The word to search | 
|  | 53 | * | 
|  | 54 | * Undefined if no bit exists, so code should check against 0 first. | 
|  | 55 | */ | 
|  | 56 | static inline unsigned long __ffs(unsigned long word) | 
|  | 57 | { | 
|  | 58 | __asm__("bsfl %1,%0" | 
|  | 59 | :"=r" (word) | 
|  | 60 | :"rm" (word)); | 
|  | 61 | return word; | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | * find_first_bit - find the first set bit in a memory region | 
|  | 66 | * @addr: The address to start the search at | 
|  | 67 | * @size: The maximum size to search | 
|  | 68 | * | 
| Randy Dunlap | ec12fa5 | 2008-01-30 13:30:32 +0100 | [diff] [blame] | 69 | * Returns the bit number of the first set bit, not the number of the byte | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | * containing a bit. | 
|  | 71 | */ | 
| David Howells | d89c145 | 2006-01-06 00:11:59 -0800 | [diff] [blame] | 72 | static inline unsigned find_first_bit(const unsigned long *addr, unsigned size) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { | 
| David Howells | d89c145 | 2006-01-06 00:11:59 -0800 | [diff] [blame] | 74 | unsigned x = 0; | 
| Linus Torvalds | d6d2a2a | 2005-07-29 11:01:22 -0400 | [diff] [blame] | 75 |  | 
|  | 76 | while (x < size) { | 
|  | 77 | unsigned long val = *addr++; | 
|  | 78 | if (val) | 
|  | 79 | return __ffs(val) + x; | 
| Joe Perches | fd591ac | 2008-03-23 01:01:43 -0700 | [diff] [blame] | 80 | x += sizeof(*addr) << 3; | 
| Linus Torvalds | d6d2a2a | 2005-07-29 11:01:22 -0400 | [diff] [blame] | 81 | } | 
| Steven Rostedt | cd85c8b | 2005-07-28 08:45:06 -0400 | [diff] [blame] | 82 | return x; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
|  | 85 | /** | 
|  | 86 | * find_next_bit - find the first set bit in a memory region | 
|  | 87 | * @addr: The address to base the search on | 
| Randy Dunlap | ec12fa5 | 2008-01-30 13:30:32 +0100 | [diff] [blame] | 88 | * @offset: The bit number to start searching at | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | * @size: The maximum size to search | 
|  | 90 | */ | 
|  | 91 | int find_next_bit(const unsigned long *addr, int size, int offset); | 
|  | 92 |  | 
|  | 93 | /** | 
|  | 94 | * ffz - find first zero in word. | 
|  | 95 | * @word: The word to search | 
|  | 96 | * | 
|  | 97 | * Undefined if no zero exists, so code should check against ~0UL first. | 
|  | 98 | */ | 
|  | 99 | static inline unsigned long ffz(unsigned long word) | 
|  | 100 | { | 
|  | 101 | __asm__("bsfl %1,%0" | 
|  | 102 | :"=r" (word) | 
|  | 103 | :"r" (~word)); | 
|  | 104 | return word; | 
|  | 105 | } | 
|  | 106 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | #ifdef __KERNEL__ | 
|  | 108 |  | 
| Akinobu Mita | 1cc2b99 | 2006-03-26 01:39:24 -0800 | [diff] [blame] | 109 | #include <asm-generic/bitops/sched.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 |  | 
|  | 111 | /** | 
|  | 112 | * ffs - find first bit set | 
|  | 113 | * @x: the word to search | 
|  | 114 | * | 
|  | 115 | * This is defined the same way as | 
|  | 116 | * the libc and compiler builtin ffs routines, therefore | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 117 | * differs in spirit from the above ffz() (man ffs). | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | */ | 
|  | 119 | static inline int ffs(int x) | 
|  | 120 | { | 
|  | 121 | int r; | 
|  | 122 |  | 
|  | 123 | __asm__("bsfl %1,%0\n\t" | 
|  | 124 | "jnz 1f\n\t" | 
|  | 125 | "movl $-1,%0\n" | 
|  | 126 | "1:" : "=r" (r) : "rm" (x)); | 
|  | 127 | return r+1; | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | /** | 
| Stephen Hemminger | d832245 | 2006-01-06 00:12:12 -0800 | [diff] [blame] | 131 | * fls - find last bit set | 
|  | 132 | * @x: the word to search | 
|  | 133 | * | 
| Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 134 | * This is defined the same way as ffs(). | 
| Stephen Hemminger | d832245 | 2006-01-06 00:12:12 -0800 | [diff] [blame] | 135 | */ | 
|  | 136 | static inline int fls(int x) | 
|  | 137 | { | 
|  | 138 | int r; | 
|  | 139 |  | 
|  | 140 | __asm__("bsrl %1,%0\n\t" | 
|  | 141 | "jnz 1f\n\t" | 
|  | 142 | "movl $-1,%0\n" | 
|  | 143 | "1:" : "=r" (r) : "rm" (x)); | 
|  | 144 | return r+1; | 
|  | 145 | } | 
|  | 146 |  | 
| Akinobu Mita | 1cc2b99 | 2006-03-26 01:39:24 -0800 | [diff] [blame] | 147 | #include <asm-generic/bitops/hweight.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 |  | 
|  | 149 | #endif /* __KERNEL__ */ | 
|  | 150 |  | 
| Akinobu Mita | 1cc2b99 | 2006-03-26 01:39:24 -0800 | [diff] [blame] | 151 | #include <asm-generic/bitops/fls64.h> | 
|  | 152 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | #ifdef __KERNEL__ | 
|  | 154 |  | 
| Akinobu Mita | 1cc2b99 | 2006-03-26 01:39:24 -0800 | [diff] [blame] | 155 | #include <asm-generic/bitops/ext2-non-atomic.h> | 
|  | 156 |  | 
| Joe Perches | fd591ac | 2008-03-23 01:01:43 -0700 | [diff] [blame] | 157 | #define ext2_set_bit_atomic(lock, nr, addr)			\ | 
|  | 158 | test_and_set_bit((nr), (unsigned long *)(addr)) | 
|  | 159 | #define ext2_clear_bit_atomic(lock, nr, addr)			\ | 
|  | 160 | test_and_clear_bit((nr), (unsigned long *)(addr)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 |  | 
| Akinobu Mita | 1cc2b99 | 2006-03-26 01:39:24 -0800 | [diff] [blame] | 162 | #include <asm-generic/bitops/minix.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 |  | 
|  | 164 | #endif /* __KERNEL__ */ | 
|  | 165 |  | 
|  | 166 | #endif /* _I386_BITOPS_H */ |