• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #ifndef FLOWGRAPH_UTILITIES_H
18 #define FLOWGRAPH_UTILITIES_H
19 
20 #include <unistd.h>
21 
22 using namespace FLOWGRAPH_OUTER_NAMESPACE::flowgraph;
23 
24 class FlowgraphUtilities {
25 public:
26 // This was copied from audio_utils/primitives.h
27 /**
28  * Convert a single-precision floating point value to a Q0.31 integer value.
29  * Rounds to nearest, ties away from 0.
30  *
31  * Values outside the range [-1.0, 1.0) are properly clamped to -2147483648 and 2147483647,
32  * including -Inf and +Inf. NaN values are considered undefined, and behavior may change
33  * depending on hardware and future implementation of this function.
34  */
clamp32FromFloat(float f)35 static int32_t clamp32FromFloat(float f)
36 {
37     static const float scale = (float)(1UL << 31);
38     static const float limpos = 1.;
39     static const float limneg = -1.;
40 
41     if (f <= limneg) {
42         return INT32_MIN;
43     } else if (f >= limpos) {
44         return INT32_MAX;
45     }
46     f *= scale;
47     /* integer conversion is through truncation (though int to float is not).
48      * ensure that we round to nearest, ties away from 0.
49      */
50     return f > 0 ? f + 0.5 : f - 0.5;
51 }
52 
53 };
54 
55 #endif // FLOWGRAPH_UTILITIES_H
56