2 * SpanDSP - a series of DSP components for telephony
4 * bit_operations.h - Various bit level operations, such as bit reversal
6 * Written by Steve Underwood <steveu@coppice.org>
8 * Copyright (C) 2006 Steve Underwood
10 * All rights reserved.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2, as
14 * published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * $Id: bit_operations.h,v 1.11 2006/11/28 15:37:03 steveu Exp $
30 #if !defined(_BIT_OPERATIONS_H_)
31 #define _BIT_OPERATIONS_H_
37 #if defined(__i386__) || defined(__x86_64__)
38 /*! \brief Find the bit position of the highest set bit in a word
39 \param bits The word to be searched
40 \return The bit number of the highest set bit, or -1 if the word is zero. */
41 static __inline__ int top_bit(unsigned int bits)
45 __asm__ (" xorl %[res],%[res];\n"
47 " bsrl %[bits],%[res]\n"
49 : [bits] "rm" (bits));
52 /*- End of function --------------------------------------------------------*/
54 /*! \brief Find the bit position of the lowest set bit in a word
55 \param bits The word to be searched
56 \return The bit number of the lowest set bit, or -1 if the word is zero. */
57 static __inline__ int bottom_bit(unsigned int bits)
61 __asm__ (" xorl %[res],%[res];\n"
63 " bsfl %[bits],%[res]\n"
65 : [bits] "rm" (bits));
68 /*- End of function --------------------------------------------------------*/
70 static __inline__ int top_bit(unsigned int bits)
77 if (bits & 0xFFFF0000)
82 if (bits & 0xFF00FF00)
87 if (bits & 0xF0F0F0F0)
92 if (bits & 0xCCCCCCCC)
97 if (bits & 0xAAAAAAAA)
104 /*- End of function --------------------------------------------------------*/
106 static __inline__ int bottom_bit(unsigned int bits)
113 if (bits & 0x0000FFFF)
118 if (bits & 0x00FF00FF)
123 if (bits & 0x0F0F0F0F)
128 if (bits & 0x33333333)
133 if (bits & 0x55555555)
140 /*- End of function --------------------------------------------------------*/
143 /*! \brief Bit reverse a byte.
144 \param data The byte to be reversed.
145 \return The bit reversed version of data. */
146 static __inline__ uint8_t bit_reverse8(uint8_t x)
148 #if defined(__i386__) || defined(__x86_64__)
149 /* If multiply is fast */
150 return ((x*0x0802U & 0x22110U) | (x*0x8020U & 0x88440U))*0x10101U >> 16;
152 /* If multiply is slow, but we have a barrel shifter */
153 x = (x >> 4) | (x << 4);
154 x = ((x & 0xCC) >> 2) | ((x & 0x33) << 2);
155 return ((x & 0xAA) >> 1) | ((x & 0x55) << 1);
158 /*- End of function --------------------------------------------------------*/
160 /*! \brief Bit reverse a 16 bit word.
161 \param data The word to be reversed.
162 \return The bit reversed version of data. */
163 uint16_t bit_reverse16(uint16_t data);
165 /*! \brief Bit reverse a 32 bit word.
166 \param data The word to be reversed.
167 \return The bit reversed version of data. */
168 uint32_t bit_reverse32(uint32_t data);
170 /*! \brief Bit reverse each of the four bytes in a 32 bit word.
171 \param data The word to be reversed.
172 \return The bit reversed version of data. */
173 uint32_t bit_reverse_4bytes(uint32_t data);
175 /*! \brief Find the number of set bits in a 32 bit word.
176 \param x The word to be searched.
177 \return The number of set bits. */
178 int one_bits32(uint32_t x);
180 /*! \brief Create a mask as wide as the number in a 32 bit word.
181 \param x The word to be searched.
183 uint32_t make_mask32(uint32_t x);
185 /*! \brief Create a mask as wide as the number in a 16 bit word.
186 \param x The word to be searched.
188 uint16_t make_mask16(uint16_t x);
190 /*! \brief Find the least significant one in a word, and return a word
191 with just that bit set.
192 \param x The word to be searched.
193 \return The word with the single set bit. */
194 static __inline__ uint32_t least_significant_one32(uint32_t x)
196 return (x & (-(int32_t) x));
198 /*- End of function --------------------------------------------------------*/
200 /*! \brief Find the most significant one in a word, and return a word
201 with just that bit set.
202 \param x The word to be searched.
203 \return The word with the single set bit. */
204 static __inline__ uint32_t most_significant_one32(uint32_t x)
206 #if defined(__i386__) || defined(__x86_64__)
207 return 1 << top_bit(x);
210 return (x ^ (x >> 1));
213 /*- End of function --------------------------------------------------------*/
215 /*! \brief Find the parity of a byte.
216 \param x The byte to be checked.
217 \return 1 for odd, or 0 for even. */
218 static __inline__ int parity8(uint8_t x)
220 x = (x ^ (x >> 4)) & 0x0F;
221 return (0x6996 >> x) & 1;
223 /*- End of function --------------------------------------------------------*/
225 /*! \brief Find the parity of a 16 bit word.
226 \param x The word to be checked.
227 \return 1 for odd, or 0 for even. */
228 static __inline__ int parity16(uint16_t x)
231 x = (x ^ (x >> 4)) & 0x0F;
232 return (0x6996 >> x) & 1;
234 /*- End of function --------------------------------------------------------*/
236 /*! \brief Find the parity of a 32 bit word.
237 \param x The word to be checked.
238 \return 1 for odd, or 0 for even. */
239 static __inline__ int parity32(uint32_t x)
243 x = (x ^ (x >> 4)) & 0x0F;
244 return (0x6996 >> x) & 1;
246 /*- End of function --------------------------------------------------------*/
253 /*- End of file ------------------------------------------------------------*/