2 * Copyright (C) 2011 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef ANDROID_AUDIO_PRIMITIVES_H
18 #define ANDROID_AUDIO_PRIMITIVES_H
22 #include <sys/cdefs.h>
26 /* The memcpy_* conversion routines are designed to work in-place on same dst as src
27 * buffers only if the types shrink on copy, with the exception of memcpy_to_i16_from_u8().
28 * This allows the loops to go upwards for faster cache access (and may be more flexible
29 * for future optimization later).
33 * Dither and clamp pairs of 32-bit input samples (sums) to 16-bit output samples (out).
34 * Each 32-bit input sample can be viewed as a signed fixed-point Q19.12 of which the
35 * .12 fraction bits are dithered and the 19 integer bits are clamped to signed 16 bits.
36 * Alternatively the input can be viewed as Q4.27, of which the lowest .12 of the fraction
37 * is dithered and the remaining fraction is converted to the output Q.15, with clamping
38 * on the 4 integer guard bits.
40 * For interleaved stereo, c is the number of sample pairs,
41 * and out is an array of interleaved pairs of 16-bit samples per channel.
42 * For mono, c is the number of samples / 2, and out is an array of 16-bit samples.
43 * The name "dither" is a misnomer; the current implementation does not actually dither
44 * but uses truncation. This may change.
45 * The out and sums buffers must either be completely separate (non-overlapping), or
46 * they must both start at the same address. Partially overlapping buffers are not supported.
48 void ditherAndClamp(int32_t* out, const int32_t *sums, size_t c);
50 /* Expand and copy samples from unsigned 8-bit offset by 0x80 to signed 16-bit.
52 * dst Destination buffer
54 * count Number of samples to copy
55 * The destination and source buffers must either be completely separate (non-overlapping), or
56 * they must both start at the same address. Partially overlapping buffers are not supported.
58 void memcpy_to_i16_from_u8(int16_t *dst, const uint8_t *src, size_t count);
60 /* Shrink and copy samples from signed 16-bit to unsigned 8-bit offset by 0x80.
62 * dst Destination buffer
64 * count Number of samples to copy
65 * The destination and source buffers must either be completely separate (non-overlapping), or
66 * they must both start at the same address. Partially overlapping buffers are not supported.
67 * The conversion is done by truncation, without dithering, so it loses resolution.
69 void memcpy_to_u8_from_i16(uint8_t *dst, const int16_t *src, size_t count);
71 /* Copy samples from float to unsigned 8-bit offset by 0x80.
73 * dst Destination buffer
75 * count Number of samples to copy
76 * The destination and source buffers must either be completely separate (non-overlapping), or
77 * they must both start at the same address. Partially overlapping buffers are not supported.
78 * The conversion is done by truncation, without dithering, so it loses resolution.
80 void memcpy_to_u8_from_float(uint8_t *dst, const float *src, size_t count);
82 /* Shrink and copy samples from signed 32-bit fixed-point Q0.31 to signed 16-bit Q0.15.
84 * dst Destination buffer
86 * count Number of samples to copy
87 * The destination and source buffers must either be completely separate (non-overlapping), or
88 * they must both start at the same address. Partially overlapping buffers are not supported.
89 * The conversion is done by truncation, without dithering, so it loses resolution.
91 void memcpy_to_i16_from_i32(int16_t *dst, const int32_t *src, size_t count);
93 /* Shrink and copy samples from single-precision floating-point to signed 16-bit.
94 * Each float should be in the range -1.0 to 1.0. Values outside that range are clamped,
95 * refer to clamp16_from_float().
97 * dst Destination buffer
99 * count Number of samples to copy
100 * The destination and source buffers must either be completely separate (non-overlapping), or
101 * they must both start at the same address. Partially overlapping buffers are not supported.
102 * The conversion is done by truncation, without dithering, so it loses resolution.
104 void memcpy_to_i16_from_float(int16_t *dst, const float *src, size_t count);
106 /* Copy samples from signed fixed-point 32-bit Q4.27 to single-precision floating-point.
107 * The nominal output float range is [-1.0, 1.0] if the fixed-point range is
108 * [0xf8000000, 0x07ffffff]. The full float range is [-16.0, 16.0]. Note the closed range
109 * at 1.0 and 16.0 is due to rounding on conversion to float. See float_from_q4_27() for details.
111 * dst Destination buffer
113 * count Number of samples to copy
114 * The destination and source buffers must either be completely separate (non-overlapping), or
115 * they must both start at the same address. Partially overlapping buffers are not supported.
117 void memcpy_to_float_from_q4_27(float *dst, const int32_t *src, size_t count);
119 /* Copy samples from signed fixed-point 16 bit Q0.15 to single-precision floating-point.
120 * The output float range is [-1.0, 1.0) for the fixed-point range [0x8000, 0x7fff].
121 * No rounding is needed as the representation is exact.
123 * dst Destination buffer
125 * count Number of samples to copy
126 * The destination and source buffers must be completely separate.
128 void memcpy_to_float_from_i16(float *dst, const int16_t *src, size_t count);
130 /* Copy samples from unsigned fixed-point 8 bit to single-precision floating-point.
131 * The output float range is [-1.0, 1.0) for the fixed-point range [0x00, 0xFF].
132 * No rounding is needed as the representation is exact.
134 * dst Destination buffer
136 * count Number of samples to copy
137 * The destination and source buffers must be completely separate.
139 void memcpy_to_float_from_u8(float *dst, const uint8_t *src, size_t count);
141 /* Copy samples from signed fixed-point packed 24 bit Q0.23 to single-precision floating-point.
142 * The packed 24 bit input is stored in native endian format in a uint8_t byte array.
143 * The output float range is [-1.0, 1.0) for the fixed-point range [0x800000, 0x7fffff].
144 * No rounding is needed as the representation is exact.
146 * dst Destination buffer
148 * count Number of samples to copy
149 * The destination and source buffers must be completely separate.
151 void memcpy_to_float_from_p24(float *dst, const uint8_t *src, size_t count);
153 /* Copy samples from signed fixed-point packed 24 bit Q0.23 to signed fixed point 16 bit Q0.15.
154 * The packed 24 bit output is stored in native endian format in a uint8_t byte array.
155 * The data is truncated without rounding.
157 * dst Destination buffer
159 * count Number of samples to copy
160 * The destination and source buffers must either be completely separate (non-overlapping), or
161 * they must both start at the same address. Partially overlapping buffers are not supported.
163 void memcpy_to_i16_from_p24(int16_t *dst, const uint8_t *src, size_t count);
165 /* Copy samples from signed fixed point 16 bit Q0.15 to signed fixed-point packed 24 bit Q0.23.
166 * The packed 24 bit output is assumed to be a native-endian uint8_t byte array.
167 * The output data range is [0x800000, 0x7fff00] (not full).
168 * Nevertheless there is no DC offset on the output, if the input has no DC offset.
170 * dst Destination buffer
172 * count Number of samples to copy
173 * The destination and source buffers must be completely separate.
175 void memcpy_to_p24_from_i16(uint8_t *dst, const int16_t *src, size_t count);
177 /* Copy samples from single-precision floating-point to signed fixed-point packed 24 bit Q0.23.
178 * The packed 24 bit output is assumed to be a native-endian uint8_t byte array.
179 * The data is clamped and rounded to nearest, ties away from zero. See clamp24_from_float()
182 * dst Destination buffer
184 * count Number of samples to copy
185 * The destination and source buffers must either be completely separate (non-overlapping), or
186 * they must both start at the same address. Partially overlapping buffers are not supported.
188 void memcpy_to_p24_from_float(uint8_t *dst, const float *src, size_t count);
190 /* Copy samples from signed fixed-point 32-bit Q8.23 to signed fixed-point packed 24 bit Q0.23.
191 * The packed 24 bit output is assumed to be a native-endian uint8_t byte array.
192 * The data is clamped to the range is [0x800000, 0x7fffff].
194 * dst Destination buffer
196 * count Number of samples to copy
197 * The destination and source buffers must be completely separate.
199 void memcpy_to_p24_from_q8_23(uint8_t *dst, const int32_t *src, size_t count);
201 /* Copy samples from signed fixed point 16-bit Q0.15 to signed fixed-point 32-bit Q8.23.
202 * The output data range is [0xff800000, 0x007fff00] at intervals of 0x100.
204 * dst Destination buffer
206 * count Number of samples to copy
207 * The destination and source buffers must be completely separate.
209 void memcpy_to_q8_23_from_i16(int32_t *dst, const int16_t *src, size_t count);
211 /* Copy samples from single-precision floating-point to signed fixed-point 32-bit Q8.23.
212 * This copy will clamp the Q8.23 representation to [0xff800000, 0x007fffff] even though there
213 * are guard bits available. Fractional lsb is rounded to nearest, ties away from zero.
214 * See clamp24_from_float() for details.
216 * dst Destination buffer
218 * count Number of samples to copy
219 * The destination and source buffers must either be completely separate (non-overlapping), or
220 * they must both start at the same address. Partially overlapping buffers are not supported.
222 void memcpy_to_q8_23_from_float_with_clamp(int32_t *dst, const float *src, size_t count);
224 /* Copy samples from signed fixed point packed 24-bit Q0.23 to signed fixed-point 32-bit Q8.23.
225 * The output data range is [0xff800000, 0x007fffff].
227 * dst Destination buffer
229 * count Number of samples to copy
230 * The destination and source buffers must be completely separate.
232 void memcpy_to_q8_23_from_p24(int32_t *dst, const uint8_t *src, size_t count);
234 /* Copy samples from single-precision floating-point to signed fixed-point 32-bit Q4.27.
235 * The conversion will use the full available Q4.27 range, including guard bits.
236 * Fractional lsb is rounded to nearest, ties away from zero.
237 * See clampq4_27_from_float() for details.
239 * dst Destination buffer
241 * count Number of samples to copy
242 * The destination and source buffers must either be completely separate (non-overlapping), or
243 * they must both start at the same address. Partially overlapping buffers are not supported.
245 void memcpy_to_q4_27_from_float(int32_t *dst, const float *src, size_t count);
247 /* Copy samples from signed fixed-point 32-bit Q8.23 to signed fixed point 16-bit Q0.15.
248 * The data is clamped, and truncated without rounding.
250 * dst Destination buffer
252 * count Number of samples to copy
253 * The destination and source buffers must either be completely separate (non-overlapping), or
254 * they must both start at the same address. Partially overlapping buffers are not supported.
256 void memcpy_to_i16_from_q8_23(int16_t *dst, const int32_t *src, size_t count);
258 /* Copy samples from signed fixed-point 32-bit Q8.23 to single-precision floating-point.
259 * The nominal output float range is [-1.0, 1.0) for the fixed-point
260 * range [0xff800000, 0x007fffff]. The maximum output float range is [-256.0, 256.0).
261 * No rounding is needed as the representation is exact for nominal values.
262 * Rounding for overflow values is to nearest, ties to even.
264 * dst Destination buffer
266 * count Number of samples to copy
267 * The destination and source buffers must either be completely separate (non-overlapping), or
268 * they must both start at the same address. Partially overlapping buffers are not supported.
270 void memcpy_to_float_from_q8_23(float *dst, const int32_t *src, size_t count);
272 /* Copy samples from signed fixed point 16-bit Q0.15 to signed fixed-point 32-bit Q0.31.
273 * The output data range is [0x80000000, 0x7fff0000] at intervals of 0x10000.
275 * dst Destination buffer
277 * count Number of samples to copy
278 * The destination and source buffers must be completely separate.
280 void memcpy_to_i32_from_i16(int32_t *dst, const int16_t *src, size_t count);
282 /* Copy samples from single-precision floating-point to signed fixed-point 32-bit Q0.31.
283 * If rounding is needed on truncation, the fractional lsb is rounded to nearest,
284 * ties away from zero. See clamp32_from_float() for details.
286 * dst Destination buffer
288 * count Number of samples to copy
289 * The destination and source buffers must either be completely separate (non-overlapping), or
290 * they must both start at the same address. Partially overlapping buffers are not supported.
292 void memcpy_to_i32_from_float(int32_t *dst, const float *src, size_t count);
294 /* Copy samples from signed fixed-point 32-bit Q0.31 to single-precision floating-point.
295 * The float range is [-1.0, 1.0] for the fixed-point range [0x80000000, 0x7fffffff].
296 * Rounding is done according to float_from_i32().
298 * dst Destination buffer
300 * count Number of samples to copy
301 * The destination and source buffers must either be completely separate (non-overlapping), or
302 * they must both start at the same address. Partially overlapping buffers are not supported.
304 void memcpy_to_float_from_i32(float *dst, const int32_t *src, size_t count);
306 /* Downmix pairs of interleaved stereo input 16-bit samples to mono output 16-bit samples.
308 * dst Destination buffer
310 * count Number of stereo frames to downmix
311 * The destination and source buffers must be completely separate (non-overlapping).
312 * The current implementation truncates the mean rather than dither, but this may change.
314 void downmix_to_mono_i16_from_stereo_i16(int16_t *dst, const int16_t *src, size_t count);
316 /* Upmix mono input 16-bit samples to pairs of interleaved stereo output 16-bit samples by
319 * dst Destination buffer
321 * count Number of mono samples to upmix
322 * The destination and source buffers must be completely separate (non-overlapping).
324 void upmix_to_stereo_i16_from_mono_i16(int16_t *dst, const int16_t *src, size_t count);
326 /* Return the total number of non-zero 32-bit samples */
327 size_t nonZeroMono32(const int32_t *samples, size_t count);
329 /* Return the total number of non-zero 16-bit samples */
330 size_t nonZeroMono16(const int16_t *samples, size_t count);
332 /* Return the total number of non-zero stereo frames, where a frame is considered non-zero
333 * if either of its constituent 32-bit samples is non-zero
335 size_t nonZeroStereo32(const int32_t *frames, size_t count);
337 /* Return the total number of non-zero stereo frames, where a frame is considered non-zero
338 * if either of its constituent 16-bit samples is non-zero
340 size_t nonZeroStereo16(const int16_t *frames, size_t count);
342 /* Copy frames, selecting source samples based on a source channel mask to fit
343 * the destination channel mask. Unmatched channels in the destination channel mask
344 * are zero filled. Unmatched channels in the source channel mask are dropped.
345 * Channels present in the channel mask are represented by set bits in the
346 * uint32_t value and are matched without further interpretation.
348 * dst Destination buffer
349 * dst_mask Bit mask corresponding to destination channels present
351 * src_mask Bit mask corresponding to source channels present
352 * sample_size Size of each sample in bytes. Must be 1, 2, 3, or 4.
353 * count Number of frames to copy
354 * The destination and source buffers must be completely separate (non-overlapping).
355 * If the sample size is not in range, the function will abort.
357 void memcpy_by_channel_mask(void *dst, uint32_t dst_mask,
358 const void *src, uint32_t src_mask, size_t sample_size, size_t count);
360 /* Copy frames, selecting source samples based on an index array (idxary).
361 * The idxary[] consists of dst_channels number of elements.
362 * The ith element if idxary[] corresponds the ith destination channel.
363 * A non-negative value is the channel index in the source frame.
364 * A negative index (-1) represents filling with 0.
366 * Example: Swapping L and R channels for stereo streams
370 * Example: Copying a mono source to the front center 5.1 channel
378 * This copy allows swizzling of channels or replication of channels.
381 * dst Destination buffer
382 * dst_channels Number of destination channels per frame
384 * src_channels Number of source channels per frame
385 * idxary Array of indices representing channels in the source frame
386 * sample_size Size of each sample in bytes. Must be 1, 2, 3, or 4.
387 * count Number of frames to copy
388 * The destination and source buffers must be completely separate (non-overlapping).
389 * If the sample size is not in range, the function will abort.
391 void memcpy_by_index_array(void *dst, uint32_t dst_channels,
392 const void *src, uint32_t src_channels,
393 const int8_t *idxary, size_t sample_size, size_t count);
395 /* Prepares an index array (idxary) from channel masks, which can be later
396 * used by memcpy_by_index_array(). Returns the number of array elements required.
397 * This may be greater than idxcount, so the return value should be checked
398 * if idxary size is less than 32. Note that idxary is a caller allocated array
399 * of at least as many channels as present in the dst_mask.
400 * Channels present in the channel mask are represented by set bits in the
401 * uint32_t value and are matched without further interpretation.
403 * This function is typically used for converting audio data with different
404 * channel position masks.
407 * idxary Updated array of indices of channels in the src frame for the dst frame
408 * idxcount Number of caller allocated elements in idxary
409 * dst_mask Bit mask corresponding to destination channels present
410 * src_mask Bit mask corresponding to source channels present
412 size_t memcpy_by_index_array_initialization(int8_t *idxary, size_t idxcount,
413 uint32_t dst_mask, uint32_t src_mask);
415 /* Prepares an index array (idxary) from channel masks, which can be later
416 * used by memcpy_by_index_array(). Returns the number of array elements required.
418 * For a source channel index mask, the source channels will map to the destination
419 * channels as if counting the set bits in dst_mask in order from lsb to msb
420 * (zero bits are ignored). The ith bit of the src_mask corresponds to the
421 * ith SET bit of dst_mask and the ith destination channel. Hence, a zero ith
422 * bit of the src_mask indicates that the ith destination channel plays silence.
425 * idxary Updated array of indices of channels in the src frame for the dst frame
426 * idxcount Number of caller allocated elements in idxary
427 * dst_mask Bit mask corresponding to destination channels present
428 * src_mask Bit mask corresponding to source channels present
430 size_t memcpy_by_index_array_initialization_src_index(int8_t *idxary, size_t idxcount,
431 uint32_t dst_mask, uint32_t src_mask);
433 /* Prepares an index array (idxary) from channel mask bits, which can be later
434 * used by memcpy_by_index_array(). Returns the number of array elements required.
436 * This initialization is for a destination channel index mask from a positional
439 * For an destination channel index mask, the input channels will map
440 * to the destination channels, with the ith SET bit in the source bits corresponding
441 * to the ith bit in the destination bits. If there is a zero bit in the middle
442 * of set destination bits (unlikely), the corresponding source channel will
446 * idxary Updated array of indices of channels in the src frame for the dst frame
447 * idxcount Number of caller allocated elements in idxary
448 * dst_mask Bit mask corresponding to destination channels present
449 * src_mask Bit mask corresponding to source channels present
451 size_t memcpy_by_index_array_initialization_dst_index(int8_t *idxary, size_t idxcount,
452 uint32_t dst_mask, uint32_t src_mask);
455 * Clamp (aka hard limit or clip) a signed 32-bit sample to 16-bit range.
457 static inline int16_t clamp16(int32_t sample)
459 if ((sample>>15) ^ (sample>>31))
460 sample = 0x7FFF ^ (sample>>31);
465 * Convert a IEEE 754 single precision float [-1.0, 1.0) to int16_t [-32768, 32767]
466 * with clamping. Note the open bound at 1.0, values within 1/65536 of 1.0 map
467 * to 32767 instead of 32768 (early clamping due to the smaller positive integer subrange).
469 * Values outside the range [-1.0, 1.0) are properly clamped to -32768 and 32767,
470 * including -Inf and +Inf. NaN will generally be treated either as -32768 or 32767,
471 * depending on the sign bit inside NaN (whose representation is not unique).
472 * Nevertheless, strictly speaking, NaN behavior should be considered undefined.
474 * Rounding of 0.5 lsb is to even (default for IEEE 754).
476 static inline int16_t clamp16_from_float(float f)
478 /* Offset is used to expand the valid range of [-1.0, 1.0) into the 16 lsbs of the
479 * floating point significand. The normal shift is 3<<22, but the -15 offset
480 * is used to multiply by 32768.
482 static const float offset = (float)(3 << (22 - 15));
483 /* zero = (0x10f << 22) = 0x43c00000 (not directly used) */
484 static const int32_t limneg = (0x10f << 22) /*zero*/ - 32768; /* 0x43bf8000 */
485 static const int32_t limpos = (0x10f << 22) /*zero*/ + 32767; /* 0x43c07fff */
492 u.f = f + offset; /* recenter valid range */
493 /* Now the valid range is represented as integers between [limneg, limpos].
494 * Clamp using the fact that float representation (as an integer) is an ordered set.
498 else if (u.i > limpos)
500 return u.i; /* Return lower 16 bits, the part of interest in the significand. */
504 * Convert a IEEE 754 single precision float [-1.0, 1.0) to uint8_t [0, 0xff]
505 * with clamping. Note the open bound at 1.0, values within 1/128 of 1.0 map
506 * to 255 instead of 256 (early clamping due to the smaller positive integer subrange).
508 * Values outside the range [-1.0, 1.0) are properly clamped to 0 and 255,
509 * including -Inf and +Inf. NaN will generally be treated either as 0 or 255,
510 * depending on the sign bit inside NaN (whose representation is not unique).
511 * Nevertheless, strictly speaking, NaN behavior should be considered undefined.
513 * Rounding of 0.5 lsb is to even (default for IEEE 754).
515 static inline uint8_t clamp8_from_float(float f)
517 /* Offset is used to expand the valid range of [-1.0, 1.0) into the 16 lsbs of the
518 * floating point significand. The normal shift is 3<<22, but the -7 offset
519 * is used to multiply by 128.
521 static const float offset = (float)((3 << (22 - 7)) + 1 /* to cancel -1.0 */);
522 /* zero = (0x11f << 22) = 0x47c00000 */
523 static const int32_t limneg = (0x11f << 22) /*zero*/;
524 static const int32_t limpos = (0x11f << 22) /*zero*/ + 255; /* 0x47c000ff */
531 u.f = f + offset; /* recenter valid range */
532 /* Now the valid range is represented as integers between [limneg, limpos].
533 * Clamp using the fact that float representation (as an integer) is an ordered set.
539 return u.i; /* Return lower 8 bits, the part of interest in the significand. */
542 /* Convert a single-precision floating point value to a Q0.23 integer value, stored in a
543 * 32 bit signed integer (technically stored as Q8.23, but clamped to Q0.23).
545 * Rounds to nearest, ties away from 0.
547 * Values outside the range [-1.0, 1.0) are properly clamped to -8388608 and 8388607,
548 * including -Inf and +Inf. NaN values are considered undefined, and behavior may change
549 * depending on hardware and future implementation of this function.
551 static inline int32_t clamp24_from_float(float f)
553 static const float scale = (float)(1 << 23);
554 static const float limpos = 0x7fffff / scale;
555 static const float limneg = -0x800000 / scale;
559 } else if (f >= limpos) {
563 /* integer conversion is through truncation (though int to float is not).
564 * ensure that we round to nearest, ties away from 0.
566 return f > 0 ? f + 0.5 : f - 0.5;
569 /* Convert a signed fixed-point 32-bit Q8.23 value to a Q0.23 integer value,
570 * stored in a 32-bit signed integer (technically stored as Q8.23, but clamped to Q0.23).
572 * Values outside the range [-0x800000, 0x7fffff] are clamped to that range.
574 static inline int32_t clamp24_from_q8_23(int32_t ival)
576 static const int32_t limpos = 0x7fffff;
577 static const int32_t limneg = -0x800000;
580 } else if (ival > limpos) {
587 /* Convert a single-precision floating point value to a Q4.27 integer value.
588 * Rounds to nearest, ties away from 0.
590 * Values outside the range [-16.0, 16.0) are properly clamped to -2147483648 and 2147483647,
591 * including -Inf and +Inf. NaN values are considered undefined, and behavior may change
592 * depending on hardware and future implementation of this function.
594 static inline int32_t clampq4_27_from_float(float f)
596 static const float scale = (float)(1UL << 27);
597 static const float limpos = 16.;
598 static const float limneg = -16.;
601 return -0x80000000; /* or 0x80000000 */
602 } else if (f >= limpos) {
606 /* integer conversion is through truncation (though int to float is not).
607 * ensure that we round to nearest, ties away from 0.
609 return f > 0 ? f + 0.5 : f - 0.5;
612 /* Convert a single-precision floating point value to a Q0.31 integer value.
613 * Rounds to nearest, ties away from 0.
615 * Values outside the range [-1.0, 1.0) are properly clamped to -2147483648 and 2147483647,
616 * including -Inf and +Inf. NaN values are considered undefined, and behavior may change
617 * depending on hardware and future implementation of this function.
619 static inline int32_t clamp32_from_float(float f)
621 static const float scale = (float)(1UL << 31);
622 static const float limpos = 1.;
623 static const float limneg = -1.;
626 return -0x80000000; /* or 0x80000000 */
627 } else if (f >= limpos) {
631 /* integer conversion is through truncation (though int to float is not).
632 * ensure that we round to nearest, ties away from 0.
634 return f > 0 ? f + 0.5 : f - 0.5;
637 /* Convert a signed fixed-point 32-bit Q4.27 value to single-precision floating-point.
638 * The nominal output float range is [-1.0, 1.0] if the fixed-point range is
639 * [0xf8000000, 0x07ffffff]. The full float range is [-16.0, 16.0].
641 * Note the closed range at 1.0 and 16.0 is due to rounding on conversion to float.
642 * In more detail: if the fixed-point integer exceeds 24 bit significand of single
643 * precision floating point, the 0.5 lsb in the significand conversion will round
644 * towards even, as per IEEE 754 default.
646 static inline float float_from_q4_27(int32_t ival)
648 /* The scale factor is the reciprocal of the fractional bits.
650 * Since the scale factor is a power of 2, the scaling is exact, and there
651 * is no rounding due to the multiplication - the bit pattern is preserved.
652 * However, there may be rounding due to the fixed-point to float conversion,
653 * as described above.
655 static const float scale = 1. / (float)(1UL << 27);
660 /* Convert an unsigned fixed-point 32-bit U4.28 value to single-precision floating-point.
661 * The nominal output float range is [0.0, 1.0] if the fixed-point range is
662 * [0x00000000, 0x10000000]. The full float range is [0.0, 16.0].
664 * Note the closed range at 1.0 and 16.0 is due to rounding on conversion to float.
665 * In more detail: if the fixed-point integer exceeds 24 bit significand of single
666 * precision floating point, the 0.5 lsb in the significand conversion will round
667 * towards even, as per IEEE 754 default.
669 static inline float float_from_u4_28(uint32_t uval)
671 static const float scale = 1. / (float)(1UL << 28);
676 /* Convert an unsigned fixed-point 16-bit U4.12 value to single-precision floating-point.
677 * The nominal output float range is [0.0, 1.0] if the fixed-point range is
678 * [0x0000, 0x1000]. The full float range is [0.0, 16.0).
680 static inline float float_from_u4_12(uint16_t uval)
682 static const float scale = 1. / (float)(1UL << 12);
687 /* Convert a single-precision floating point value to a U4.28 integer value.
688 * Rounds to nearest, ties away from 0.
690 * Values outside the range [0, 16.0] are properly clamped to [0, 4294967295]
691 * including -Inf and +Inf. NaN values are considered undefined, and behavior may change
692 * depending on hardware and future implementation of this function.
694 static inline uint32_t u4_28_from_float(float f)
696 static const float scale = (float)(1 << 28);
697 static const float limpos = 0xffffffffUL / scale;
701 } else if (f >= limpos) {
704 /* integer conversion is through truncation (though int to float is not).
705 * ensure that we round to nearest, ties away from 0.
707 return f * scale + 0.5;
710 /* Convert a single-precision floating point value to a U4.12 integer value.
711 * Rounds to nearest, ties away from 0.
713 * Values outside the range [0, 16.0) are properly clamped to [0, 65535]
714 * including -Inf and +Inf. NaN values are considered undefined, and behavior may change
715 * depending on hardware and future implementation of this function.
717 static inline uint16_t u4_12_from_float(float f)
719 static const float scale = (float)(1 << 12);
720 static const float limpos = 0xffff / scale;
724 } else if (f >= limpos) {
727 /* integer conversion is through truncation (though int to float is not).
728 * ensure that we round to nearest, ties away from 0.
730 return f * scale + 0.5;
733 /* Convert a signed fixed-point 16-bit Q0.15 value to single-precision floating-point.
734 * The output float range is [-1.0, 1.0) for the fixed-point range
737 * There is no rounding, the conversion and representation is exact.
739 static inline float float_from_i16(int16_t ival)
741 /* The scale factor is the reciprocal of the nominal 16 bit integer
742 * half-sided range (32768).
744 * Since the scale factor is a power of 2, the scaling is exact, and there
745 * is no rounding due to the multiplication - the bit pattern is preserved.
747 static const float scale = 1. / (float)(1UL << 15);
752 /* Convert an unsigned fixed-point 8-bit U0.8 value to single-precision floating-point.
753 * The nominal output float range is [-1.0, 1.0) if the fixed-point range is
756 static inline float float_from_u8(uint8_t uval)
758 static const float scale = 1. / (float)(1UL << 7);
760 return ((int)uval - 128) * scale;
763 /* Convert a packed 24bit Q0.23 value stored native-endian in a uint8_t ptr
764 * to a signed fixed-point 32 bit integer Q0.31 value. The output Q0.31 range
765 * is [0x80000000, 0x7fffff00] for the fixed-point range [0x800000, 0x7fffff].
766 * Even though the output range is limited on the positive side, there is no
767 * DC offset on the output, if the input has no DC offset.
769 * Avoid relying on the limited output range, as future implementations may go
772 static inline int32_t i32_from_p24(const uint8_t *packed24)
775 return (packed24[0] << 8) | (packed24[1] << 16) | (packed24[2] << 24);
778 /* Convert a 32-bit Q0.31 value to single-precision floating-point.
779 * The output float range is [-1.0, 1.0] for the fixed-point range
780 * [0x80000000, 0x7fffffff].
782 * Rounding may occur in the least significant 8 bits for large fixed point
783 * values due to storage into the 24-bit floating-point significand.
784 * Rounding will be to nearest, ties to even.
786 static inline float float_from_i32(int32_t ival)
788 static const float scale = 1. / (float)(1UL << 31);
793 /* Convert a packed 24bit Q0.23 value stored native endian in a uint8_t ptr
794 * to single-precision floating-point. The output float range is [-1.0, 1.0)
795 * for the fixed-point range [0x800000, 0x7fffff].
797 * There is no rounding, the conversion and representation is exact.
799 static inline float float_from_p24(const uint8_t *packed24)
801 return float_from_i32(i32_from_p24(packed24));
804 /* Convert a 24-bit Q8.23 value to single-precision floating-point.
805 * The nominal output float range is [-1.0, 1.0) for the fixed-point
806 * range [0xff800000, 0x007fffff]. The maximum float range is [-256.0, 256.0).
808 * There is no rounding in the nominal range, the conversion and representation
809 * is exact. For values outside the nominal range, rounding is to nearest, ties to even.
811 static inline float float_from_q8_23(int32_t ival)
813 static const float scale = 1. / (float)(1UL << 23);
819 * Multiply-accumulate 16-bit terms with 32-bit result: return a + in*v.
822 int32_t mulAdd(int16_t in, int16_t v, int32_t a)
824 #if defined(__arm__) && !defined(__thumb__)
826 asm( "smlabb %[out], %[in], %[v], %[a] \n"
828 : [in]"%r"(in), [v]"r"(v), [a]"r"(a)
832 return a + in * (int32_t)v;
837 * Multiply 16-bit terms with 32-bit result: return in*v.
840 int32_t mul(int16_t in, int16_t v)
842 #if defined(__arm__) && !defined(__thumb__)
844 asm( "smulbb %[out], %[in], %[v] \n"
846 : [in]"%r"(in), [v]"r"(v)
850 return in * (int32_t)v;
855 * Similar to mulAdd, but the 16-bit terms are extracted from a 32-bit interleaved stereo pair.
858 int32_t mulAddRL(int left, uint32_t inRL, uint32_t vRL, int32_t a)
860 #if defined(__arm__) && !defined(__thumb__)
863 asm( "smlabb %[out], %[inRL], %[vRL], %[a] \n"
865 : [inRL]"%r"(inRL), [vRL]"r"(vRL), [a]"r"(a)
868 asm( "smlatt %[out], %[inRL], %[vRL], %[a] \n"
870 : [inRL]"%r"(inRL), [vRL]"r"(vRL), [a]"r"(a)
876 return a + (int16_t)(inRL&0xFFFF) * (int16_t)(vRL&0xFFFF);
878 return a + (int16_t)(inRL>>16) * (int16_t)(vRL>>16);
884 * Similar to mul, but the 16-bit terms are extracted from a 32-bit interleaved stereo pair.
887 int32_t mulRL(int left, uint32_t inRL, uint32_t vRL)
889 #if defined(__arm__) && !defined(__thumb__)
892 asm( "smulbb %[out], %[inRL], %[vRL] \n"
894 : [inRL]"%r"(inRL), [vRL]"r"(vRL)
897 asm( "smultt %[out], %[inRL], %[vRL] \n"
899 : [inRL]"%r"(inRL), [vRL]"r"(vRL)
905 return (int16_t)(inRL&0xFFFF) * (int16_t)(vRL&0xFFFF);
907 return (int16_t)(inRL>>16) * (int16_t)(vRL>>16);
914 #endif // ANDROID_AUDIO_PRIMITIVES_H