History

bitops: define const_*() versions of the non-atomics

Define const_*() variants of the non-atomic bitops to be used when the input arguments are compile-time constants, so that the compiler will be always able to resolve those to compile-time constants as well. Those are mostly direct aliases for generic_*() with one exception for const_test_bit(): the original one is declared atomic-safe and thus doesn't discard the `volatile` qualifier, so in order to let optimize code, define it separately disregarding the qualifier. Add them to the compile-time type checks as well just in case.

Alexander Lobakin committed 4 years ago

Commit bb7379b1 parent 0e862832 files · +32 −0Browse files

Unified · Split

2 files
  1. include/asm-generic/bitops/generic-non-atomic.h +31 −0
  2. include/linux/bitops.h +1 −0
include/asm-generic/bitops/generic-non-atomic.h+31−0
@@ -127,4 +127,35 @@ generic_test_bit(unsigned long nr, const volatile unsigned long *addr) · 126 unmodified lines
127127 return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
128128 }
129129
130+/*
131+ * const_*() definitions provide good compile-time optimizations when
132+ * the passed arguments can be resolved at compile time.
133+ */
134+#define const___set_bit generic___set_bit
135+#define const___clear_bit generic___clear_bit
136+#define const___change_bit generic___change_bit
137+#define const___test_and_set_bit generic___test_and_set_bit
138+#define const___test_and_clear_bit generic___test_and_clear_bit
139+#define const___test_and_change_bit generic___test_and_change_bit
140+
141+/**
142+ * const_test_bit - Determine whether a bit is set
143+ * @nr: bit number to test
144+ * @addr: Address to start counting from
145+ *
146+ * A version of generic_test_bit() which discards the `volatile` qualifier to
147+ * allow a compiler to optimize code harder. Non-atomic and to be called only
148+ * for testing compile-time constants, e.g. by the corresponding macros, not
149+ * directly from "regular" code.
150+ */
151+static __always_inline bool
152+const_test_bit(unsigned long nr, const volatile unsigned long *addr)
153+{
154+ const unsigned long *p = (const unsigned long *)addr + BIT_WORD(nr);
155+ unsigned long mask = BIT_MASK(nr);
156+ unsigned long val = *p;
157+
158+ return !!(val & mask);
159+}
160+
130161 #endif /* __ASM_GENERIC_BITOPS_GENERIC_NON_ATOMIC_H */
include/linux/bitops.h+1−0
@@ -37,6 +37,7 @@ extern unsigned long __sw_hweight64(__u64 w); · 36 unmodified lines
3737 /* Check that the bitops prototypes are sane */
3838 #define __check_bitop_pr(name) \
3939 static_assert(__same_type(arch_##name, generic_##name) && \
40+ __same_type(const_##name, generic_##name) && \
4041 __same_type(name, generic_##name))
4142
4243 __check_bitop_pr(__set_bit);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t