5
|
1 /*
|
|
2 * SpanDSP - a series of DSP components for telephony
|
|
3 *
|
|
4 * bit_operations.c - Various bit level operations, such as bit reversal
|
|
5 *
|
|
6 * Written by Steve Underwood <steveu@coppice.org>
|
|
7 *
|
|
8 * Copyright (C) 2006 Steve Underwood
|
|
9 *
|
|
10 * All rights reserved.
|
|
11 *
|
|
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.
|
|
15 *
|
|
16 *
|
|
17 * This program is distributed in the hope that it will be useful,
|
|
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
20 * GNU General Public License for more details.
|
|
21 *
|
|
22 * You should have received a copy of the GNU General Public License
|
|
23 * along with this program; if not, write to the Free Software
|
|
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
25 *
|
|
26 * $Id: bit_operations.c,v 1.5 2006/11/30 15:41:47 steveu Exp $
|
|
27 */
|
|
28
|
|
29 /*! \file */
|
|
30
|
|
31 #ifdef HAVE_CONFIG_H
|
|
32 #include "config.h"
|
|
33 #endif
|
|
34
|
|
35 #include <inttypes.h>
|
|
36 #include <stdlib.h>
|
|
37 #include <fcntl.h>
|
|
38 #include <string.h>
|
|
39 #include <assert.h>
|
|
40 #include <memory.h>
|
|
41
|
|
42 #include "spandsp/telephony.h"
|
|
43 #include "spandsp/bit_operations.h"
|
|
44
|
|
45 uint16_t bit_reverse16(uint16_t x)
|
|
46 {
|
|
47 x = (x >> 8) | (x << 8);
|
|
48 x = ((x & 0xF0F0) >> 4) | ((x & 0x0F0F) << 4);
|
|
49 x = ((x & 0xCCCC) >> 2) | ((x & 0x3333) << 2);
|
|
50 return ((x & 0xAAAA) >> 1) | ((x & 0x5555) << 1);
|
|
51 }
|
|
52 /*- End of function --------------------------------------------------------*/
|
|
53
|
|
54 uint32_t bit_reverse32(uint32_t x)
|
|
55 {
|
|
56 x = (x >> 16) | (x << 16);
|
|
57 x = ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8);
|
|
58 x = ((x & 0xF0F0F0F0) >> 4) | ((x & 0x0F0F0F0F) << 4);
|
|
59 x = ((x & 0xCCCCCCCC) >> 2) | ((x & 0x33333333) << 2);
|
|
60 return ((x & 0xAAAAAAAA) >> 1) | ((x & 0x55555555) << 1);
|
|
61 }
|
|
62 /*- End of function --------------------------------------------------------*/
|
|
63
|
|
64 uint32_t bit_reverse_4bytes(uint32_t x)
|
|
65 {
|
|
66 x = (x >> 4) | (x << 4);
|
|
67 x = ((x & 0xCCCCCCCC) >> 2) | ((x & 0x33333333) << 2);
|
|
68 return ((x & 0xAAAAAAAA) >> 1) | ((x & 0x55555555) << 1);
|
|
69 }
|
|
70 /*- End of function --------------------------------------------------------*/
|
|
71
|
|
72 int one_bits32(uint32_t x)
|
|
73 {
|
|
74 x = x - ((x >> 1) & 0x55555555);
|
|
75 /* We now have 16 2-bit counts */
|
|
76 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
|
|
77 /* We now have 8 4-bit counts */
|
|
78 x = (x + (x >> 4)) & 0x0F0F0F0F;
|
|
79 /* We now have 4 8-bit counts */
|
|
80 #if defined(__i386__) || defined(__x86_64__)
|
|
81 /* If multiply is fast */
|
|
82 return (x*0x01010101) >> 24;
|
|
83 #else
|
|
84 /* If multiply is slow */
|
|
85 x += (x >> 8);
|
|
86 x += (x >> 16);
|
|
87 return (x & 0x0000003F);
|
|
88 #endif
|
|
89 }
|
|
90 /*- End of function --------------------------------------------------------*/
|
|
91
|
|
92 uint32_t make_mask32(uint32_t x)
|
|
93 {
|
|
94 x |= (x >> 1);
|
|
95 x |= (x >> 2);
|
|
96 x |= (x >> 4);
|
|
97 x |= (x >> 8);
|
|
98 x |= (x >> 16);
|
|
99 return x;
|
|
100 }
|
|
101 /*- End of function --------------------------------------------------------*/
|
|
102
|
|
103 uint16_t make_mask16(uint16_t x)
|
|
104 {
|
|
105 x |= (x >> 1);
|
|
106 x |= (x >> 2);
|
|
107 x |= (x >> 4);
|
|
108 x |= (x >> 8);
|
|
109 return x;
|
|
110 }
|
|
111 /*- End of function --------------------------------------------------------*/
|
|
112 /*- End of file ------------------------------------------------------------*/
|