xref: /openbmc/u-boot/arch/nds32/include/asm/bitops.h (revision baefb63a)
1 /*
2  * Copyright 1995, Russell King.
3  * Various bits and pieces copyrights include:
4  *  Linus Torvalds (test_bit).
5  *
6  * Copyright (C) 2011 Andes Technology Corporation
7  * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
8  *
9  * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1).
10  *
11  * Please note that the code in this file should never be included
12  * from user space.  Many of these are not implemented in assembler
13  * since they would be too costly.  Also, they require priviledged
14  * instructions (which are not available from user mode) to ensure
15  * that they are atomic.
16  */
17 
18 #ifndef __ASM_NDS_BITOPS_H
19 #define __ASM_NDS_BITOPS_H
20 
21 #ifdef __KERNEL__
22 
23 #include <asm/system.h>
24 #include <asm-generic/bitops/fls.h>
25 #include <asm-generic/bitops/__fls.h>
26 #include <asm-generic/bitops/fls64.h>
27 #include <asm-generic/bitops/__ffs.h>
28 
29 #define smp_mb__before_clear_bit()	do { } while (0)
30 #define smp_mb__after_clear_bit()	do { } while (0)
31 
32 /*
33  * Function prototypes to keep gcc -Wall happy.
34  */
35 extern void set_bit(int nr, void *addr);
36 
37 static inline void __set_bit(int nr, void *addr)
38 {
39 	int *a = (int *)addr;
40 	int mask;
41 
42 	a += nr >> 5;
43 	mask = 1 << (nr & 0x1f);
44 	*a |= mask;
45 }
46 
47 extern void clear_bit(int nr, void *addr);
48 
49 static inline void __clear_bit(int nr, void *addr)
50 {
51 	int *a = (int *)addr;
52 	int mask;
53 	unsigned long flags;
54 
55 	a += nr >> 5;
56 	mask = 1 << (nr & 0x1f);
57 	local_irq_save(flags);
58 	*a &= ~mask;
59 	local_irq_restore(flags);
60 }
61 
62 extern void change_bit(int nr, void *addr);
63 
64 static inline void __change_bit(int nr, void *addr)
65 {
66 	int mask;
67 	unsigned long *ADDR = (unsigned long *)addr;
68 
69 	ADDR += nr >> 5;
70 	mask = 1 << (nr & 31);
71 	*ADDR ^= mask;
72 }
73 
74 extern int test_and_set_bit(int nr, void *addr);
75 
76 static inline int __test_and_set_bit(int nr, void *addr)
77 {
78 	int mask, retval;
79 	unsigned int *a = (unsigned int *)addr;
80 
81 	a += nr >> 5;
82 	mask = 1 << (nr & 0x1f);
83 	retval = (mask & *a) != 0;
84 	*a |= mask;
85 	return retval;
86 }
87 
88 extern int test_and_clear_bit(int nr, void *addr);
89 
90 static inline int __test_and_clear_bit(int nr, void *addr)
91 {
92 	int mask, retval;
93 	unsigned int *a = (unsigned int *)addr;
94 
95 	a += nr >> 5;
96 	mask = 1 << (nr & 0x1f);
97 	retval = (mask & *a) != 0;
98 	*a &= ~mask;
99 	return retval;
100 }
101 
102 extern int test_and_change_bit(int nr, void *addr);
103 
104 static inline int __test_and_change_bit(int nr, void *addr)
105 {
106 	int mask, retval;
107 	unsigned int *a = (unsigned int *)addr;
108 
109 	a += nr >> 5;
110 	mask = 1 << (nr & 0x1f);
111 	retval = (mask & *a) != 0;
112 	*a ^= mask;
113 	return retval;
114 }
115 
116 extern int find_first_zero_bit(void *addr, unsigned size);
117 extern int find_next_zero_bit(void *addr, int size, int offset);
118 
119 /*
120  * This routine doesn't need to be atomic.
121  */
122 static inline int test_bit(int nr, const void *addr)
123 {
124 	return ((unsigned char *) addr)[nr >> 3] & (1U << (nr & 7));
125 }
126 
127 /*
128  * ffz = Find First Zero in word. Undefined if no zero exists,
129  * so code should check against ~0UL first..
130  */
131 static inline unsigned long ffz(unsigned long word)
132 {
133 	int k;
134 
135 	word = ~word;
136 	k = 31;
137 	if (word & 0x0000ffff) {
138 		k -= 16; word <<= 16;
139 	}
140 	if (word & 0x00ff0000) {
141 		k -= 8;  word <<= 8;
142 	}
143 	if (word & 0x0f000000) {
144 		k -= 4;  word <<= 4;
145 	}
146 	if (word & 0x30000000) {
147 		k -= 2;  word <<= 2;
148 	}
149 	if (word & 0x40000000)
150 		k -= 1;
151 
152 	return k;
153 }
154 
155 /*
156  * ffs: find first bit set. This is defined the same way as
157  * the libc and compiler builtin ffs routines, therefore
158  * differs in spirit from the above ffz (man ffs).
159  */
160 
161 /*
162  * redefined in include/linux/bitops.h
163  * #define ffs(x) generic_ffs(x)
164  */
165 
166 /*
167  * hweightN: returns the hamming weight (i.e. the number
168  * of bits set) of a N-bit word
169  */
170 
171 #define hweight32(x) generic_hweight32(x)
172 #define hweight16(x) generic_hweight16(x)
173 #define hweight8(x) generic_hweight8(x)
174 
175 #define ext2_set_bit			test_and_set_bit
176 #define ext2_clear_bit			test_and_clear_bit
177 #define ext2_test_bit			test_bit
178 #define ext2_find_first_zero_bit	find_first_zero_bit
179 #define ext2_find_next_zero_bit		find_next_zero_bit
180 
181 /* Bitmap functions for the minix filesystem. */
182 #define minix_test_and_set_bit(nr, addr)	test_and_set_bit(nr, addr)
183 #define minix_set_bit(nr, addr)			set_bit(nr, addr)
184 #define minix_test_and_clear_bit(nr, addr)	test_and_clear_bit(nr, addr)
185 #define minix_test_bit(nr, addr)		test_bit(nr, addr)
186 #define minix_find_first_zero_bit(addr, size)	find_first_zero_bit(addr, size)
187 
188 #endif /* __KERNEL__ */
189 
190 #endif /* __ASM_NDS_BITOPS_H */
191