Browse code

bit test: bit_test_and_reset() added

The function returns the bit found at offset position
in a bitstring and resets the bit to 0.

Miklos Tirpak authored on 05/01/2011 10:40:20
Showing 1 changed files
... ...
@@ -18,6 +18,7 @@
18 18
  * History
19 19
  * -------
20 20
  *  2010-04-26	Initial version (Miklos)
21
+ *  2011-01-05	bit_test_and_reset added (Miklos)
21 22
  */
22 23
 
23 24
 /* Bit test functions:
... ...
@@ -30,6 +31,11 @@
30 30
  *      in a bitstring pointed by addr, and sets
31 31
  *      the bit at the given offset.
32 32
  *
33
+ *  - int bit_test_and_reset(int offset, unsigned int *addr)
34
+ *      Returns the bit found at offset position 
35
+ *      in a bitstring pointed by addr, and resets
36
+ *      the bit at the given offset.
37
+ *
33 38
  * Note that 0 <= offset <= 128, Make sure that addr points to
34 39
  * a large enough memory area.
35 40
  */
... ...
@@ -86,6 +92,24 @@ static inline int bit_test_and_set(int offset, unsigned int *addr)
86 86
 	return (int)v;
87 87
 }
88 88
 
89
+/* Returns the bit found at offset position in the bitstring
90
+ * pointed by addr and resets it to 0.
91
+ * Note that the CPU can access 4 bytes starting from addr,
92
+ * hence 0 <= offset < 128 holds. Make sure that addr points
93
+ * to a memory area that is large enough.
94
+ */
95
+static inline int bit_test_and_reset(int offset, unsigned int *addr)
96
+{
97
+	unsigned char	v;
98
+
99
+	asm volatile(
100
+		" btr %2, %1 \n\t"
101
+		" setc %0 \n\t"
102
+		: "=qm" (v) : "m" (*addr), "r" (offset)
103
+	);
104
+	return (int)v;
105
+}
106
+
89 107
 #else /* BIT_TEST_ASM */
90 108
 
91 109
 /* Returns the bit found at offset position in the bitstring
... ...
@@ -116,6 +140,24 @@ static inline int bit_test_and_set(int offset, unsigned int *addr)
116 116
 	return res;
117 117
 }
118 118
 
119
+/* Returns the bit found at offset position in the bitstring
120
+ * pointed by addr and resets it to 0.
121
+ * Note that offset can be grater than 32, make sure that addr points
122
+ * to a memory area that is large enough.
123
+ */
124
+static inline int bit_test_and_reset(int offset, unsigned int *addr)
125
+{
126
+	unsigned int	*i;
127
+	int	mask, res;
128
+
129
+	i = addr + offset/32;
130
+	mask = 1U << (offset % 32);
131
+	res = ((*i) & mask) ? 1 : 0;
132
+	(*i) &= ~mask;
133
+
134
+	return res;
135
+}
136
+
119 137
 #endif /* BIT_TEST_ASM */
120 138
 
121 139
 #endif /* #ifndef _BIT_TEST_H */