1*05767d91SRobert Wu /*
2*05767d91SRobert Wu * Copyright 2020 The Android Open Source Project
3*05767d91SRobert Wu *
4*05767d91SRobert Wu * Licensed under the Apache License, Version 2.0 (the "License");
5*05767d91SRobert Wu * you may not use this file except in compliance with the License.
6*05767d91SRobert Wu * You may obtain a copy of the License at
7*05767d91SRobert Wu *
8*05767d91SRobert Wu * http://www.apache.org/licenses/LICENSE-2.0
9*05767d91SRobert Wu *
10*05767d91SRobert Wu * Unless required by applicable law or agreed to in writing, software
11*05767d91SRobert Wu * distributed under the License is distributed on an "AS IS" BASIS,
12*05767d91SRobert Wu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*05767d91SRobert Wu * See the License for the specific language governing permissions and
14*05767d91SRobert Wu * limitations under the License.
15*05767d91SRobert Wu */
16*05767d91SRobert Wu
17*05767d91SRobert Wu #ifndef FLOWGRAPH_UTILITIES_H
18*05767d91SRobert Wu #define FLOWGRAPH_UTILITIES_H
19*05767d91SRobert Wu
20*05767d91SRobert Wu #include <math.h>
21*05767d91SRobert Wu #include <unistd.h>
22*05767d91SRobert Wu
23*05767d91SRobert Wu using namespace FLOWGRAPH_OUTER_NAMESPACE::flowgraph;
24*05767d91SRobert Wu
25*05767d91SRobert Wu class FlowgraphUtilities {
26*05767d91SRobert Wu public:
27*05767d91SRobert Wu // This was copied from audio_utils/primitives.h
28*05767d91SRobert Wu /**
29*05767d91SRobert Wu * Convert a single-precision floating point value to a Q0.31 integer value.
30*05767d91SRobert Wu * Rounds to nearest, ties away from 0.
31*05767d91SRobert Wu *
32*05767d91SRobert Wu * Values outside the range [-1.0, 1.0) are properly clamped to -2147483648 and 2147483647,
33*05767d91SRobert Wu * including -Inf and +Inf. NaN values are considered undefined, and behavior may change
34*05767d91SRobert Wu * depending on hardware and future implementation of this function.
35*05767d91SRobert Wu */
clamp32FromFloat(float f)36*05767d91SRobert Wu static int32_t clamp32FromFloat(float f)
37*05767d91SRobert Wu {
38*05767d91SRobert Wu static const float scale = (float)(1UL << 31);
39*05767d91SRobert Wu static const float limpos = 1.;
40*05767d91SRobert Wu static const float limneg = -1.;
41*05767d91SRobert Wu
42*05767d91SRobert Wu if (f <= limneg) {
43*05767d91SRobert Wu return INT32_MIN;
44*05767d91SRobert Wu } else if (f >= limpos) {
45*05767d91SRobert Wu return INT32_MAX;
46*05767d91SRobert Wu }
47*05767d91SRobert Wu f *= scale;
48*05767d91SRobert Wu /* integer conversion is through truncation (though int to float is not).
49*05767d91SRobert Wu * ensure that we round to nearest, ties away from 0.
50*05767d91SRobert Wu */
51*05767d91SRobert Wu return f > 0 ? f + 0.5 : f - 0.5;
52*05767d91SRobert Wu }
53*05767d91SRobert Wu
54*05767d91SRobert Wu /**
55*05767d91SRobert Wu * Convert a single-precision floating point value to a Q0.23 integer value, stored in a
56*05767d91SRobert Wu * 32 bit signed integer (technically stored as Q8.23, but clamped to Q0.23).
57*05767d91SRobert Wu *
58*05767d91SRobert Wu * Values outside the range [-1.0, 1.0) are properly clamped to -8388608 and 8388607,
59*05767d91SRobert Wu * including -Inf and +Inf. NaN values are considered undefined, and behavior may change
60*05767d91SRobert Wu * depending on hardware and future implementation of this function.
61*05767d91SRobert Wu */
clamp24FromFloat(float f)62*05767d91SRobert Wu static int32_t clamp24FromFloat(float f)
63*05767d91SRobert Wu {
64*05767d91SRobert Wu static const float scale = 1 << 23;
65*05767d91SRobert Wu return (int32_t) lroundf(fmaxf(fminf(f * scale, scale - 1.f), -scale));
66*05767d91SRobert Wu }
67*05767d91SRobert Wu
68*05767d91SRobert Wu };
69*05767d91SRobert Wu
70*05767d91SRobert Wu #endif // FLOWGRAPH_UTILITIES_H
71