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