1 /*
2 * Copyright (C) 2017 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 // This file contains frequently used constants and helper macros.
18
19 #include <stdint.h>
20
21 #ifndef LOCATION_LBS_CONTEXTHUB_NANOAPPS_COMMON_MATH_MACROS_H_
22 #define LOCATION_LBS_CONTEXTHUB_NANOAPPS_COMMON_MATH_MACROS_H_
23
24 // Constants.
25 #define NANO_PI (3.14159265359f)
26 #define INVALID_TEMPERATURE_CELSIUS (-274.0f)
27
28 // Common math operations.
29 #define NANO_ABS(x) ((x) > 0 ? (x) : -(x))
30 #define NANO_MAX(a, b) ((a) > (b)) ? (a) : (b)
31 #define NANO_MIN(a, b) ((a) < (b)) ? (a) : (b)
32 #define SIGMOID(x) (1 / (1 + expf(-x)))
33
34 // Floor macro implementation for platforms that do not supply the standard
35 // floorf() math function.
36 #define NANO_FLOOR(x) ((int)(x) - ((x) < (int)(x))) // NOLINT
37
38 // Timestamp conversion macros.
39 #ifdef __cplusplus
40 #define MSEC_TO_NANOS(x) (static_cast<uint64_t>((x) * UINT64_C(1000000)))
41 #else
42 #define MSEC_TO_NANOS(x) ((uint64_t)((x) * UINT64_C(1000000))) // NOLINT
43 #endif
44
45 #define SEC_TO_NANOS(x) MSEC_TO_NANOS((x) * UINT64_C(1000))
46 #define MIN_TO_NANOS(x) SEC_TO_NANOS ((x) * UINT64_C(60))
47 #define HRS_TO_NANOS(x) MIN_TO_NANOS ((x) * UINT64_C(60))
48 #define DAYS_TO_NANOS(x) HRS_TO_NANOS ((x) * UINT64_C(24))
49
50 // Sample rate to period conversion.
51 #ifdef __cplusplus
52 #define HZ_TO_PERIOD_NANOS(hz) \
53 (SEC_TO_NANOS(1024) / (static_cast<uint64_t>((hz) * 1024)))
54 #else
55 #define HZ_TO_PERIOD_NANOS(hz) \
56 (SEC_TO_NANOS(1024) / ((uint64_t)((hz) * 1024))) // NOLINT
57 #endif
58
59 // Unit conversion: nanoseconds to seconds.
60 #define NANOS_TO_SEC (1.0e-9f)
61
62 // Unit conversion: milli-degrees to radians.
63 #define MDEG_TO_RAD (NANO_PI / 180.0e3f)
64
65 // Unit conversion: radians to milli-degrees.
66 #define RAD_TO_MDEG (180.0e3f / NANO_PI)
67
68 // Time check helper macro that returns true if:
69 // i. 't1' is equal to or exceeds 't2' plus 't_delta'.
70 // ii. Or, a negative timestamp delta occurred since,
71 // 't1' should always >= 't2'. This prevents potential lockout conditions
72 // if the timer count 't1' rolls over or an erroneously large
73 // timestamp is passed through.
74 #define NANO_TIMER_CHECK_T1_GEQUAL_T2_PLUS_DELTA(t1, t2, t_delta) \
75 (((t1) >= (t2) + (t_delta)) || ((t1) < (t2)))
76
77 #ifdef __cplusplus
78 extern "C" {
79 #endif
80
81 // This conversion function may be necessary for embedded hardware that can't
82 // cast a uint64_t to a float directly. This conversion function was taken from:
83 // [android]//device/google/contexthub/firmware/os/core/floatRt.c
floatFromUint64(uint64_t v)84 static inline float floatFromUint64(uint64_t v) {
85 uint32_t hi = v >> 32;
86 uint32_t lo = (uint32_t) v;
87
88 if (!hi) { // This is very fast for cases where 'v' fits into a uint32_t.
89 return (float)lo;
90 } else {
91 return ((float)hi) * 4294967296.0f + (float)lo;
92 }
93 }
94
95 #ifdef __cplusplus
96 }
97 #endif
98
99 #endif // LOCATION_LBS_CONTEXTHUB_NANOAPPS_COMMON_MATH_MACROS_H_
100