Skip to content

Commit a63eafd

Browse files
Cryolitiaavagin
authored andcommitted
include: don't use GCC's __builtin_ffs on riscv64
Link: SerenityOS/serenity@e300da4 Signed-off-by: PukNgae Cryolitia <[email protected]> --- - cherry-picked Signed-off-by: Alexander Mikhalitsyn <[email protected]>
1 parent f416e0f commit a63eafd

File tree

1 file changed

+110
-1
lines changed

1 file changed

+110
-1
lines changed

include/common/arch/riscv64/asm/bitops.h

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,116 @@
22
#define __CR_ASM_BITOPS_H__
33

44
#include "common/compiler.h"
5-
#include "common/asm-generic/bitops.h"
5+
#include "common/asm/bitsperlong.h"
6+
7+
#define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))
8+
#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_LONG)
9+
10+
#define DECLARE_BITMAP(name, bits) unsigned long name[BITS_TO_LONGS(bits)]
11+
#define BITMAP_SIZE(name) (sizeof(name) * CHAR_BIT)
12+
13+
#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1)
14+
/* Technically wrong, but this avoids compilation errors on some gcc
15+
versions. */
16+
#define BITOP_ADDR(x) "=m"(*(volatile long *)(x))
17+
#else
18+
#define BITOP_ADDR(x) "+m"(*(volatile long *)(x))
19+
#endif
20+
21+
#define ADDR BITOP_ADDR(addr)
22+
23+
static inline void set_bit(int nr, volatile unsigned long *addr)
24+
{
25+
addr += nr / BITS_PER_LONG;
26+
*addr |= (1UL << (nr % BITS_PER_LONG));
27+
}
28+
29+
static inline void change_bit(int nr, volatile unsigned long *addr)
30+
{
31+
addr += nr / BITS_PER_LONG;
32+
*addr ^= (1UL << (nr % BITS_PER_LONG));
33+
}
34+
35+
static inline int test_bit(int nr, volatile const unsigned long *addr)
36+
{
37+
addr += nr / BITS_PER_LONG;
38+
return (*addr & (1UL << (nr % BITS_PER_LONG))) ? -1 : 0;
39+
}
40+
41+
static inline void clear_bit(int nr, volatile unsigned long *addr)
42+
{
43+
addr += nr / BITS_PER_LONG;
44+
*addr &= ~(1UL << (nr % BITS_PER_LONG));
45+
}
46+
47+
/**
48+
* __ffs - find first set bit in word
49+
* @word: The word to search
50+
*
51+
* Undefined if no bit exists, so code should check against 0 first.
52+
*/
53+
static inline unsigned long __ffs(unsigned long word)
54+
{
55+
int p = 0;
56+
57+
for (; p < 8*sizeof(word); ++p) {
58+
if (word & 1) {
59+
break;
60+
}
61+
62+
word >>= 1;
63+
}
64+
65+
return p;
66+
}
67+
68+
#define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
69+
70+
/*
71+
* Find the next set bit in a memory region.
72+
*/
73+
static inline unsigned long find_next_bit(const unsigned long *addr, unsigned long size, unsigned long offset)
74+
{
75+
const unsigned long *p = addr + BITOP_WORD(offset);
76+
unsigned long result = offset & ~(BITS_PER_LONG - 1);
77+
unsigned long tmp;
78+
79+
if (offset >= size)
80+
return size;
81+
size -= result;
82+
offset %= BITS_PER_LONG;
83+
if (offset) {
84+
tmp = *(p++);
85+
tmp &= (~0UL << offset);
86+
if (size < BITS_PER_LONG)
87+
goto found_first;
88+
if (tmp)
89+
goto found_middle;
90+
size -= BITS_PER_LONG;
91+
result += BITS_PER_LONG;
92+
}
93+
while (size & ~(BITS_PER_LONG - 1)) {
94+
if ((tmp = *(p++)))
95+
goto found_middle;
96+
result += BITS_PER_LONG;
97+
size -= BITS_PER_LONG;
98+
}
99+
if (!size)
100+
return result;
101+
tmp = *p;
102+
103+
found_first:
104+
tmp &= (~0UL >> (BITS_PER_LONG - size));
105+
if (tmp == 0UL) /* Are any bits set? */
106+
return result + size; /* Nope. */
107+
found_middle:
108+
return result + __ffs(tmp);
109+
}
110+
111+
#define for_each_bit(i, bitmask) \
112+
for (i = find_next_bit(bitmask, BITMAP_SIZE(bitmask), 0); i < BITMAP_SIZE(bitmask); \
113+
i = find_next_bit(bitmask, BITMAP_SIZE(bitmask), i + 1))
114+
6115

7116
#define BITS_PER_LONG 64
8117

0 commit comments

Comments
 (0)