1 /******************************************************************************
2 *
3 * Copyright 2022 Google LLC
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #ifndef __LC3_COMMON_H
20 #define __LC3_COMMON_H
21
22 #include <lc3.h>
23 #include "fastmath.h"
24
25 #include <limits.h>
26 #include <stdalign.h>
27 #include <string.h>
28
29 #ifdef __ARM_ARCH
30 #include <arm_acle.h>
31 #endif
32
33
34 /**
35 * Acivation flags for LC3-Plus and LC3-Plus HR features
36 */
37
38 #ifndef LC3_PLUS
39 #define LC3_PLUS 1
40 #endif
41
42 #ifndef LC3_PLUS_HR
43 #define LC3_PLUS_HR 1
44 #endif
45
46 #if LC3_PLUS
47 #define LC3_IF_PLUS(a, b) (a)
48 #else
49 #define LC3_IF_PLUS(a, b) (b)
50 #endif
51
52 #if LC3_PLUS_HR
53 #define LC3_IF_PLUS_HR(a, b) (a)
54 #else
55 #define LC3_IF_PLUS_HR(a, b) (b)
56 #endif
57
58
59 /**
60 * Hot Function attribute
61 * Selectively disable sanitizer
62 */
63
64 #ifdef __clang__
65
66 #define LC3_HOT \
67 __attribute__((no_sanitize("bounds"))) \
68 __attribute__((no_sanitize("integer")))
69
70 #else /* __clang__ */
71
72 #define LC3_HOT
73
74 #endif /* __clang__ */
75
76
77 /**
78 * Macros
79 * MIN/MAX Minimum and maximum between 2 values
80 * CLIP Clip a value between low and high limits
81 * SATXX Signed saturation on 'xx' bits
82 * ABS Return absolute value
83 */
84
85 #define LC3_MIN(a, b) ( (a) < (b) ? (a) : (b) )
86 #define LC3_MAX(a, b) ( (a) > (b) ? (a) : (b) )
87
88 #define LC3_CLIP(v, min, max) LC3_MIN(LC3_MAX(v, min), max)
89 #define LC3_SAT16(v) LC3_CLIP(v, -(1 << 15), (1 << 15) - 1)
90 #define LC3_SAT24(v) LC3_CLIP(v, -(1 << 23), (1 << 23) - 1)
91
92 #define LC3_ABS(v) ( (v) < 0 ? -(v) : (v) )
93
94
95 #if defined(__ARM_FEATURE_SAT) && !(__GNUC__ < 10)
96
97 #undef LC3_SAT16
98 #define LC3_SAT16(v) __ssat(v, 16)
99
100 #undef LC3_SAT24
101 #define LC3_SAT24(v) __ssat(v, 24)
102
103 #endif /* __ARM_FEATURE_SAT */
104
105
106 /**
107 * Return `true` when high-resolution mode
108 */
lc3_hr(enum lc3_srate sr)109 static inline bool lc3_hr(enum lc3_srate sr) {
110 return LC3_PLUS_HR && (sr >= LC3_SRATE_48K_HR);
111 }
112
113
114 /**
115 * Bandwidth, mapped to Nyquist frequency of samplerates
116 */
117
118 enum lc3_bandwidth {
119 LC3_BANDWIDTH_NB = LC3_SRATE_8K,
120 LC3_BANDWIDTH_WB = LC3_SRATE_16K,
121 LC3_BANDWIDTH_SSWB = LC3_SRATE_24K,
122 LC3_BANDWIDTH_SWB = LC3_SRATE_32K,
123 LC3_BANDWIDTH_FB = LC3_SRATE_48K,
124
125 LC3_BANDWIDTH_FB_HR = LC3_SRATE_48K_HR,
126 LC3_BANDWIDTH_UB_HR = LC3_SRATE_96K_HR,
127
128 LC3_NUM_BANDWIDTH,
129 };
130
131
132 /**
133 * Complex floating point number
134 */
135
136 struct lc3_complex
137 {
138 float re, im;
139 };
140
141
142 #endif /* __LC3_COMMON_H */
143