xref: /netbsd-src/usr.bin/audio/common/auconv.h (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: auconv.h,v 1.5 2008/04/28 20:24:12 martin Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Lennart Augustsson.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/types.h>
33 #include <sys/audioio.h>
34 
35 /* Convert between signed and unsigned. */
36 static inline void change_sign8(u_char *, int);
37 static inline void change_sign16_le(u_char *, int);
38 static inline void change_sign16_be(u_char *, int);
39 static inline void change_sign32_le(u_char *, int);
40 static inline void change_sign32_be(u_char *, int);
41 /* Convert between little and big endian. */
42 static inline void swap_bytes(u_char *, int);
43 static inline void swap_bytes32(u_char *, int);
44 static inline void swap_bytes_change_sign16_le(u_char *, int);
45 static inline void swap_bytes_change_sign16_be(u_char *, int);
46 static inline void change_sign16_swap_bytes_le(u_char *, int);
47 static inline void change_sign16_swap_bytes_be(u_char *, int);
48 static inline void swap_bytes_change_sign32_le(u_char *, int);
49 static inline void swap_bytes_change_sign32_be(u_char *, int);
50 static inline void change_sign32_swap_bytes_le(u_char *, int);
51 static inline void change_sign32_swap_bytes_be(u_char *, int);
52 
53 static inline void
54 change_sign8(u_char *p, int cc)
55 {
56 	while (--cc >= 0) {
57 		*p ^= 0x80;
58 		++p;
59 	}
60 }
61 
62 static inline void
63 change_sign16_le(u_char *p, int cc)
64 {
65 	while ((cc -= 2) >= 0) {
66 		p[1] ^= 0x80;
67 		p += 2;
68 	}
69 }
70 
71 static inline void
72 change_sign16_be(u_char *p, int cc)
73 {
74 	while ((cc -= 2) >= 0) {
75 		p[0] ^= 0x80;
76 		p += 2;
77 	}
78 }
79 
80 static inline void
81 change_sign32_le(u_char *p, int cc)
82 {
83 	while ((cc -= 4) >= 0) {
84 		p[3] ^= 0x80;
85 		p += 4;
86 	}
87 }
88 
89 static inline void
90 change_sign32_be(u_char *p, int cc)
91 {
92 	while ((cc -= 4) >= 0) {
93 		p[0] ^= 0x80;
94 		p += 4;
95 	}
96 }
97 
98 static inline void
99 swap_bytes(u_char *p, int cc)
100 {
101 	u_char t;
102 
103 	while ((cc -= 2) >= 0) {
104 		t = p[0];
105 		p[0] = p[1];
106 		p[1] = t;
107 		p += 2;
108 	}
109 }
110 
111 static inline void
112 swap_bytes32(u_char *p, int cc)
113 {
114 	u_char t;
115 
116 	while ((cc -= 4) >= 0) {
117 		t = p[0];
118 		p[0] = p[3];
119 		p[3] = t;
120 		t = p[1];
121 		p[1] = p[2];
122 		p[2] = t;
123 		p += 4;
124 	}
125 }
126 
127 static inline void
128 swap_bytes_change_sign16_le(u_char *p, int cc)
129 {
130 	u_char t;
131 
132 	while ((cc -= 2) >= 0) {
133 		t = p[1];
134 		p[1] = p[0] ^ 0x80;
135 		p[0] = t;
136 		p += 2;
137 	}
138 }
139 
140 static inline void
141 swap_bytes_change_sign16_be(u_char *p, int cc)
142 {
143 	u_char t;
144 
145 	while ((cc -= 2) >= 0) {
146 		t = p[0];
147 		p[0] = p[1] ^ 0x80;
148 		p[1] = t;
149 		p += 2;
150 	}
151 }
152 
153 static inline void
154 change_sign16_swap_bytes_le(u_char *p, int cc)
155 {
156 	swap_bytes_change_sign16_be(p, cc);
157 }
158 
159 static inline void
160 change_sign16_swap_bytes_be(u_char *p, int cc)
161 {
162 	swap_bytes_change_sign16_le(p, cc);
163 }
164 
165 static inline void
166 swap_bytes_change_sign32_le(u_char *p, int cc)
167 {
168 	u_char t;
169 
170 	while ((cc -= 4) >= 0) {
171 		t = p[3];
172 		p[3] = p[0] ^ 0x80;
173 		p[0] = t;
174 		t = p[1];
175 		p[1] = p[2];
176 		p[2] = t;
177 		p += 4;
178 	}
179 }
180 
181 static inline void
182 swap_bytes_change_sign32_be(u_char *p, int cc)
183 {
184 	u_char t;
185 
186 	while ((cc -= 4) >= 0) {
187 		t = p[0];
188 		p[0] = p[3] ^ 0x80;
189 		p[3] = t;
190 		t = p[1];
191 		p[1] = p[2];
192 		p[2] = t;
193 		p += 4;
194 	}
195 }
196 
197 static inline void
198 change_sign32_swap_bytes_le(u_char *p, int cc)
199 {
200 	swap_bytes_change_sign32_be(p, cc);
201 }
202 
203 static inline void
204 change_sign32_swap_bytes_be(u_char *p, int cc)
205 {
206 	swap_bytes_change_sign32_le(p, cc);
207 }
208