hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/include/linux/bitfield.h
....@@ -1,15 +1,7 @@
1
+/* SPDX-License-Identifier: GPL-2.0-only */
12 /*
23 * Copyright (C) 2014 Felix Fietkau <nbd@nbd.name>
34 * Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
4
- *
5
- * This program is free software; you can redistribute it and/or modify
6
- * it under the terms of the GNU General Public License version 2
7
- * as published by the Free Software Foundation
8
- *
9
- * This program is distributed in the hope that it will be useful,
10
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- * GNU General Public License for more details.
135 */
146
157 #ifndef _LINUX_BITFIELD_H
....@@ -49,6 +41,22 @@
4941
5042 #define __bf_shf(x) (__builtin_ffsll(x) - 1)
5143
44
+#define __scalar_type_to_unsigned_cases(type) \
45
+ unsigned type: (unsigned type)0, \
46
+ signed type: (unsigned type)0
47
+
48
+#define __unsigned_scalar_typeof(x) typeof( \
49
+ _Generic((x), \
50
+ char: (unsigned char)0, \
51
+ __scalar_type_to_unsigned_cases(char), \
52
+ __scalar_type_to_unsigned_cases(short), \
53
+ __scalar_type_to_unsigned_cases(int), \
54
+ __scalar_type_to_unsigned_cases(long), \
55
+ __scalar_type_to_unsigned_cases(long long), \
56
+ default: (x)))
57
+
58
+#define __bf_cast_unsigned(type, x) ((__unsigned_scalar_typeof(type))(x))
59
+
5260 #define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx) \
5361 ({ \
5462 BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \
....@@ -57,10 +65,24 @@
5765 BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ? \
5866 ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \
5967 _pfx "value too large for the field"); \
60
- BUILD_BUG_ON_MSG((_mask) > (typeof(_reg))~0ull, \
68
+ BUILD_BUG_ON_MSG(__bf_cast_unsigned(_mask, _mask) > \
69
+ __bf_cast_unsigned(_reg, ~0ull), \
6170 _pfx "type of reg too small for mask"); \
6271 __BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \
6372 (1ULL << __bf_shf(_mask))); \
73
+ })
74
+
75
+/**
76
+ * FIELD_MAX() - produce the maximum value representable by a field
77
+ * @_mask: shifted mask defining the field's length and position
78
+ *
79
+ * FIELD_MAX() returns the maximum value that can be held in the field
80
+ * specified by @_mask.
81
+ */
82
+#define FIELD_MAX(_mask) \
83
+ ({ \
84
+ __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_MAX: "); \
85
+ (typeof(_mask))((_mask) >> __bf_shf(_mask)); \
6486 })
6587
6688 /**
....@@ -118,6 +140,7 @@
118140 {
119141 return field / field_multiplier(field);
120142 }
143
+#define field_max(field) ((typeof(field))field_mask(field))
121144 #define ____MAKE_OP(type,base,to,from) \
122145 static __always_inline __##type type##_encode_bits(base v, base field) \
123146 { \