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 // Timestamp conversion macros.
35 #ifdef __cplusplus
36 #define MSEC_TO_NANOS(x) (static_cast<uint64_t>(x * UINT64_C(1000000)))
37 #else
38 #define MSEC_TO_NANOS(x) ((uint64_t)(x * UINT64_C(1000000))) // NOLINT
39 #endif
40
41 #define SEC_TO_NANOS(x) MSEC_TO_NANOS(x * UINT64_C(1000))
42 #define MIN_TO_NANOS(x) SEC_TO_NANOS (x * UINT64_C(60))
43 #define HRS_TO_NANOS(x) MIN_TO_NANOS (x * UINT64_C(60))
44 #define DAYS_TO_NANOS(x) HRS_TO_NANOS (x * UINT64_C(24))
45
46 // Sample rate to period conversion.
47 #ifdef __cplusplus
48 #define HZ_TO_PERIOD_NANOS(hz) \
49 (SEC_TO_NANOS(1024) / (static_cast<uint64_t>(hz * 1024)))
50 #else
51 #define HZ_TO_PERIOD_NANOS(hz) \
52 (SEC_TO_NANOS(1024) / ((uint64_t)(hz * 1024))) // NOLINT
53 #endif
54
55 // Unit conversion: nanoseconds to seconds.
56 #define NANOS_TO_SEC (1.0e-9f)
57
58 // Unit conversion: milli-degrees to radians.
59 #define MDEG_TO_RAD (NANO_PI / 180.0e3f)
60
61 // Unit conversion: radians to milli-degrees.
62 #define RAD_TO_MDEG (180.0e3f / NANO_PI)
63
64 // Time check helper macro that returns true if:
65 // i. 't1' is equal to or exceeds 't2' plus 't_delta'.
66 // ii. Or, a negative timestamp delta occurred since,
67 // 't1' should always >= 't2'. This prevents potential lockout conditions
68 // if the timer count 't1' rolls over or an erroneously large
69 // timestamp is passed through.
70 #define NANO_TIMER_CHECK_T1_GEQUAL_T2_PLUS_DELTA(t1, t2, t_delta) \
71 (((t1) >= (t2) + (t_delta)) || ((t1) < (t2)))
72
73 #ifdef __cplusplus
74 extern "C" {
75 #endif
76
77 // This conversion function may be necessary for embedded hardware that can't
78 // cast a uint64_t to a float directly. This conversion function was taken from:
79 // [android]//device/google/contexthub/firmware/os/core/floatRt.c
floatFromUint64(uint64_t v)80 static inline float floatFromUint64(uint64_t v) {
81 uint32_t hi = v >> 32;
82 uint32_t lo = (uint32_t) v;
83
84 if (!hi) { // This is very fast for cases where 'v' fits into a uint32_t.
85 return (float)lo;
86 } else {
87 return ((float)hi) * 4294967296.0f + (float)lo;
88 }
89 }
90
91 #ifdef __cplusplus
92 }
93 #endif
94
95 #endif // LOCATION_LBS_CONTEXTHUB_NANOAPPS_COMMON_MATH_MACROS_H_
96