aboutsummaryrefslogtreecommitdiff
path: root/src/common.h
blob: 2b23a8d3fe5801282710fad543ac83f5c20822b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/******************************************************************************
 *
 *  Copyright 2022 Google LLC
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#ifndef __LC3_COMMON_H
#define __LC3_COMMON_H

#include <lc3.h>
#include "fastmath.h"

#include <limits.h>
#include <stdalign.h>
#include <string.h>

#ifdef __ARM_ARCH
#include <arm_acle.h>
#endif


/**
 * Acivation flags for LC3-Plus and LC3-Plus HR features
 */

#ifndef LC3_PLUS
#define LC3_PLUS 1
#endif

#ifndef LC3_PLUS_HR
#define LC3_PLUS_HR 1
#endif

#if LC3_PLUS
#define LC3_IF_PLUS(a, b) (a)
#else
#define LC3_IF_PLUS(a, b) (b)
#endif

#if LC3_PLUS_HR
#define LC3_IF_PLUS_HR(a, b) (a)
#else
#define LC3_IF_PLUS_HR(a, b) (b)
#endif


/**
 * Hot Function attribute
 * Selectively disable sanitizer
 */

#ifdef __clang__

#define LC3_HOT \
    __attribute__((no_sanitize("bounds"))) \
    __attribute__((no_sanitize("integer")))

#else /* __clang__ */

#define LC3_HOT

#endif /* __clang__ */


/**
 * Macros
 * MIN/MAX  Minimum and maximum between 2 values
 * CLIP     Clip a value between low and high limits
 * SATXX    Signed saturation on 'xx' bits
 * ABS      Return absolute value
 */

#define LC3_MIN(a, b)  ( (a) < (b) ?  (a) : (b) )
#define LC3_MAX(a, b)  ( (a) > (b) ?  (a) : (b) )

#define LC3_CLIP(v, min, max)  LC3_MIN(LC3_MAX(v, min), max)
#define LC3_SAT16(v)  LC3_CLIP(v, -(1 << 15), (1 << 15) - 1)
#define LC3_SAT24(v)  LC3_CLIP(v, -(1 << 23), (1 << 23) - 1)

#define LC3_ABS(v)  ( (v) < 0 ? -(v) : (v) )


#if defined(__ARM_FEATURE_SAT) && !(__GNUC__ < 10)

#undef  LC3_SAT16
#define LC3_SAT16(v) __ssat(v, 16)

#undef  LC3_SAT24
#define LC3_SAT24(v) __ssat(v, 24)

#endif /* __ARM_FEATURE_SAT */


/**
 * Return `true` when high-resolution mode
 */
static inline bool lc3_hr(enum lc3_srate sr) {
    return LC3_PLUS_HR && (sr >= LC3_SRATE_48K_HR);
}


/**
 * Bandwidth, mapped to Nyquist frequency of samplerates
 */

enum lc3_bandwidth {
    LC3_BANDWIDTH_NB = LC3_SRATE_8K,
    LC3_BANDWIDTH_WB = LC3_SRATE_16K,
    LC3_BANDWIDTH_SSWB = LC3_SRATE_24K,
    LC3_BANDWIDTH_SWB = LC3_SRATE_32K,
    LC3_BANDWIDTH_FB = LC3_SRATE_48K,

    LC3_BANDWIDTH_FB_HR = LC3_SRATE_48K_HR,
    LC3_BANDWIDTH_UB_HR = LC3_SRATE_96K_HR,

    LC3_NUM_BANDWIDTH,
};


/**
 * Complex floating point number
 */

struct lc3_complex
{
    float re, im;
};


#endif /* __LC3_COMMON_H */