xref: /btstack/3rd-party/lc3-google/src/common.h (revision 6897da5c53aac5b1f90f41b5b15d0bd43d61dfff)
19a19cd78SMatthias Ringwald /******************************************************************************
29a19cd78SMatthias Ringwald  *
34930cef6SMatthias Ringwald  *  Copyright 2022 Google LLC
49a19cd78SMatthias Ringwald  *
59a19cd78SMatthias Ringwald  *  Licensed under the Apache License, Version 2.0 (the "License");
69a19cd78SMatthias Ringwald  *  you may not use this file except in compliance with the License.
79a19cd78SMatthias Ringwald  *  You may obtain a copy of the License at:
89a19cd78SMatthias Ringwald  *
99a19cd78SMatthias Ringwald  *  http://www.apache.org/licenses/LICENSE-2.0
109a19cd78SMatthias Ringwald  *
119a19cd78SMatthias Ringwald  *  Unless required by applicable law or agreed to in writing, software
129a19cd78SMatthias Ringwald  *  distributed under the License is distributed on an "AS IS" BASIS,
139a19cd78SMatthias Ringwald  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
149a19cd78SMatthias Ringwald  *  See the License for the specific language governing permissions and
159a19cd78SMatthias Ringwald  *  limitations under the License.
169a19cd78SMatthias Ringwald  *
179a19cd78SMatthias Ringwald  ******************************************************************************/
189a19cd78SMatthias Ringwald 
199a19cd78SMatthias Ringwald #ifndef __LC3_COMMON_H
209a19cd78SMatthias Ringwald #define __LC3_COMMON_H
219a19cd78SMatthias Ringwald 
229a19cd78SMatthias Ringwald #include <lc3.h>
234930cef6SMatthias Ringwald #include "fastmath.h"
249a19cd78SMatthias Ringwald 
259a19cd78SMatthias Ringwald #include <limits.h>
26*6897da5cSDirk Helbig #include <stdalign.h>
279a19cd78SMatthias Ringwald #include <string.h>
284930cef6SMatthias Ringwald 
294930cef6SMatthias Ringwald #ifdef __ARM_ARCH
304930cef6SMatthias Ringwald #include <arm_acle.h>
314930cef6SMatthias Ringwald #endif
324930cef6SMatthias Ringwald 
334930cef6SMatthias Ringwald 
344930cef6SMatthias Ringwald /**
35*6897da5cSDirk Helbig  * Acivation flags for LC3-Plus and LC3-Plus HR features
36*6897da5cSDirk Helbig  */
37*6897da5cSDirk Helbig 
38*6897da5cSDirk Helbig #ifndef LC3_PLUS
39*6897da5cSDirk Helbig #define LC3_PLUS 1
40*6897da5cSDirk Helbig #endif
41*6897da5cSDirk Helbig 
42*6897da5cSDirk Helbig #ifndef LC3_PLUS_HR
43*6897da5cSDirk Helbig #define LC3_PLUS_HR 1
44*6897da5cSDirk Helbig #endif
45*6897da5cSDirk Helbig 
46*6897da5cSDirk Helbig #if LC3_PLUS
47*6897da5cSDirk Helbig #define LC3_IF_PLUS(a, b) (a)
48*6897da5cSDirk Helbig #else
49*6897da5cSDirk Helbig #define LC3_IF_PLUS(a, b) (b)
50*6897da5cSDirk Helbig #endif
51*6897da5cSDirk Helbig 
52*6897da5cSDirk Helbig #if LC3_PLUS_HR
53*6897da5cSDirk Helbig #define LC3_IF_PLUS_HR(a, b) (a)
54*6897da5cSDirk Helbig #else
55*6897da5cSDirk Helbig #define LC3_IF_PLUS_HR(a, b) (b)
56*6897da5cSDirk Helbig #endif
57*6897da5cSDirk Helbig 
58*6897da5cSDirk Helbig 
59*6897da5cSDirk Helbig /**
604930cef6SMatthias Ringwald  * Hot Function attribute
614930cef6SMatthias Ringwald  * Selectively disable sanitizer
624930cef6SMatthias Ringwald  */
634930cef6SMatthias Ringwald 
644930cef6SMatthias Ringwald #ifdef __clang__
654930cef6SMatthias Ringwald 
664930cef6SMatthias Ringwald #define LC3_HOT \
674930cef6SMatthias Ringwald     __attribute__((no_sanitize("bounds"))) \
684930cef6SMatthias Ringwald     __attribute__((no_sanitize("integer")))
694930cef6SMatthias Ringwald 
704930cef6SMatthias Ringwald #else /* __clang__ */
714930cef6SMatthias Ringwald 
724930cef6SMatthias Ringwald #define LC3_HOT
734930cef6SMatthias Ringwald 
744930cef6SMatthias Ringwald #endif /* __clang__ */
759a19cd78SMatthias Ringwald 
769a19cd78SMatthias Ringwald 
779a19cd78SMatthias Ringwald /**
789a19cd78SMatthias Ringwald  * Macros
799a19cd78SMatthias Ringwald  * MIN/MAX  Minimum and maximum between 2 values
809a19cd78SMatthias Ringwald  * CLIP     Clip a value between low and high limits
814930cef6SMatthias Ringwald  * SATXX    Signed saturation on 'xx' bits
824930cef6SMatthias Ringwald  * ABS      Return absolute value
839a19cd78SMatthias Ringwald  */
849a19cd78SMatthias Ringwald 
859a19cd78SMatthias Ringwald #define LC3_MIN(a, b)  ( (a) < (b) ?  (a) : (b) )
869a19cd78SMatthias Ringwald #define LC3_MAX(a, b)  ( (a) > (b) ?  (a) : (b) )
879a19cd78SMatthias Ringwald 
884930cef6SMatthias Ringwald #define LC3_CLIP(v, min, max)  LC3_MIN(LC3_MAX(v, min), max)
894930cef6SMatthias Ringwald #define LC3_SAT16(v)  LC3_CLIP(v, -(1 << 15), (1 << 15) - 1)
904930cef6SMatthias Ringwald #define LC3_SAT24(v)  LC3_CLIP(v, -(1 << 23), (1 << 23) - 1)
914930cef6SMatthias Ringwald 
924930cef6SMatthias Ringwald #define LC3_ABS(v)  ( (v) < 0 ? -(v) : (v) )
934930cef6SMatthias Ringwald 
944930cef6SMatthias Ringwald 
95d39ea275SBjoern Hartmann #if defined(__ARM_FEATURE_SAT) && !(__GNUC__ < 10)
964930cef6SMatthias Ringwald 
974930cef6SMatthias Ringwald #undef  LC3_SAT16
984930cef6SMatthias Ringwald #define LC3_SAT16(v) __ssat(v, 16)
994930cef6SMatthias Ringwald 
1004930cef6SMatthias Ringwald #undef  LC3_SAT24
1014930cef6SMatthias Ringwald #define LC3_SAT24(v) __ssat(v, 24)
1024930cef6SMatthias Ringwald 
1034930cef6SMatthias Ringwald #endif /* __ARM_FEATURE_SAT */
1049a19cd78SMatthias Ringwald 
1059a19cd78SMatthias Ringwald 
1069a19cd78SMatthias Ringwald /**
107*6897da5cSDirk Helbig  * Return `true` when high-resolution mode
1089a19cd78SMatthias Ringwald  */
lc3_hr(enum lc3_srate sr)109*6897da5cSDirk Helbig static inline bool lc3_hr(enum lc3_srate sr) {
110*6897da5cSDirk Helbig     return LC3_PLUS_HR && (sr >= LC3_SRATE_48K_HR);
111*6897da5cSDirk Helbig }
1129a19cd78SMatthias Ringwald 
1139a19cd78SMatthias Ringwald 
1149a19cd78SMatthias Ringwald /**
1159a19cd78SMatthias Ringwald  * Bandwidth, mapped to Nyquist frequency of samplerates
1169a19cd78SMatthias Ringwald  */
1179a19cd78SMatthias Ringwald 
1189a19cd78SMatthias Ringwald enum lc3_bandwidth {
1199a19cd78SMatthias Ringwald     LC3_BANDWIDTH_NB = LC3_SRATE_8K,
1209a19cd78SMatthias Ringwald     LC3_BANDWIDTH_WB = LC3_SRATE_16K,
1219a19cd78SMatthias Ringwald     LC3_BANDWIDTH_SSWB = LC3_SRATE_24K,
1229a19cd78SMatthias Ringwald     LC3_BANDWIDTH_SWB = LC3_SRATE_32K,
1239a19cd78SMatthias Ringwald     LC3_BANDWIDTH_FB = LC3_SRATE_48K,
1249a19cd78SMatthias Ringwald 
125*6897da5cSDirk Helbig     LC3_BANDWIDTH_FB_HR = LC3_SRATE_48K_HR,
126*6897da5cSDirk Helbig     LC3_BANDWIDTH_UB_HR = LC3_SRATE_96K_HR,
127*6897da5cSDirk Helbig 
1289a19cd78SMatthias Ringwald     LC3_NUM_BANDWIDTH,
1299a19cd78SMatthias Ringwald };
1309a19cd78SMatthias Ringwald 
1319a19cd78SMatthias Ringwald 
1329a19cd78SMatthias Ringwald /**
1339a19cd78SMatthias Ringwald  * Complex floating point number
1349a19cd78SMatthias Ringwald  */
1359a19cd78SMatthias Ringwald 
1369a19cd78SMatthias Ringwald struct lc3_complex
1379a19cd78SMatthias Ringwald {
1389a19cd78SMatthias Ringwald     float re, im;
1399a19cd78SMatthias Ringwald };
1409a19cd78SMatthias Ringwald 
1419a19cd78SMatthias Ringwald 
1429a19cd78SMatthias Ringwald #endif /* __LC3_COMMON_H */
143