xref: /freebsd/sys/compat/linuxkpi/common/include/linux/bitfield.h (revision a0ca4af9455b844c5e094fc1b09b1390ffa979fc)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020-2024 The FreeBSD Foundation
5  *
6  * This software was developed by Björn Zeeb under sponsorship from
7  * the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #ifndef	_LINUXKPI_LINUX_BITFIELD_H
32 #define	_LINUXKPI_LINUX_BITFIELD_H
33 
34 #include <linux/types.h>
35 #include <asm/byteorder.h>
36 
37 /* Use largest possible type. */
38 static inline uint64_t ___lsb(uint64_t f) { return (f & -f); }
39 static inline uint64_t ___bitmask(uint64_t f) { return (f / ___lsb(f)); }
40 
41 #define	_uX_get_bits(_n)						\
42 	static __inline uint ## _n ## _t				\
43 	u ## _n ## _get_bits(uint ## _n ## _t v, uint ## _n ## _t f)	\
44 	{								\
45 		return ((v & f) / ___lsb(f));				\
46 	}
47 
48 _uX_get_bits(64)
49 _uX_get_bits(32)
50 _uX_get_bits(16)
51 _uX_get_bits(8)
52 
53 #define	_leX_get_bits(_n)						\
54 	static __inline uint ## _n ## _t				\
55 	le ## _n ## _get_bits(__le ## _n v, uint ## _n ## _t f)		\
56 	{								\
57 		return ((le ## _n ## _to_cpu(v) & f) / ___lsb(f));	\
58 	}
59 
60 _leX_get_bits(64)
61 _leX_get_bits(32)
62 _leX_get_bits(16)
63 
64 #define	_uX_encode_bits(_n)						\
65 	static __inline uint ## _n ## _t				\
66 	u ## _n ## _encode_bits(uint ## _n ## _t v, uint ## _n ## _t f)	\
67 	{								\
68 		return ((v & ___bitmask(f)) * ___lsb(f));		\
69 	}
70 
71 _uX_encode_bits(64)
72 _uX_encode_bits(32)
73 _uX_encode_bits(16)
74 _uX_encode_bits(8)
75 
76 #define	_leX_encode_bits(_n)						\
77 	static __inline uint ## _n ## _t				\
78 	le ## _n ## _encode_bits(__le ## _n v, uint ## _n ## _t f)	\
79 	{								\
80 		return (cpu_to_le ## _n((v & ___bitmask(f)) * ___lsb(f))); \
81 	}
82 
83 _leX_encode_bits(64)
84 _leX_encode_bits(32)
85 _leX_encode_bits(16)
86 
87 #define	_leXp_replace_bits(_n)						\
88 	static __inline void						\
89 	le ## _n ## p_replace_bits(uint ## _n ## _t *p,			\
90 	    uint ## _n ## _t v, uint ## _n ## _t f)			\
91 	{								\
92 		*p = (*p & ~(cpu_to_le ## _n(f))) |			\
93 		     le ## _n ## _encode_bits(v, f);			\
94 	}
95 
96 _leXp_replace_bits(64)
97 _leXp_replace_bits(32)
98 _leXp_replace_bits(16)
99 
100 #define	_uXp_replace_bits(_n)						\
101 	static __inline void						\
102 	u ## _n ## p_replace_bits(uint ## _n ## _t *p,			\
103 	    uint ## _n ## _t v, uint ## _n ## _t f)			\
104 	{								\
105 		*p = (*p & ~f) | u ## _n ## _encode_bits(v, f);		\
106 	}
107 
108 _uXp_replace_bits(64)
109 _uXp_replace_bits(32)
110 _uXp_replace_bits(16)
111 _uXp_replace_bits(8)
112 
113 #define	_uX_replace_bits(_n)						\
114 	static __inline uint ## _n ## _t				\
115 	u ## _n ## _replace_bits(uint ## _n ## _t p,			\
116 	    uint ## _n ## _t v, uint ## _n ## _t f)			\
117 	{								\
118 		return ((p & ~f) | u ## _n ## _encode_bits(v, f));	\
119 	}
120 
121 _uX_replace_bits(64)
122 _uX_replace_bits(32)
123 _uX_replace_bits(16)
124 _uX_replace_bits(8)
125 
126 #define	__bf_shf(x)	(__builtin_ffsll(x) - 1)
127 
128 #define	FIELD_FIT(_mask, _value)					\
129 	(!(((typeof(_mask))(_value) << __bf_shf(_mask)) & ~(_mask)))
130 
131 #define	FIELD_PREP(_mask, _value)					\
132 	(((typeof(_mask))(_value) << __bf_shf(_mask)) & (_mask))
133 
134 /* Likely would need extra sanity checks compared to FIELD_PREP()? */
135 #define	FIELD_PREP_CONST(_mask, _value)					\
136 	(((typeof(_mask))(_value) << __bf_shf(_mask)) & (_mask))
137 
138 #define	FIELD_GET(_mask, _value)					\
139 	((typeof(_mask))(((_value) & (_mask)) >> __bf_shf(_mask)))
140 
141 #endif	/* _LINUXKPI_LINUX_BITFIELD_H */
142