1 /*
2  * Copyright 2022 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 PATH_SCALAR_H
18 #define PATH_SCALAR_H
19 
20 union floatIntUnion {
21     float   value;
22     int32_t signBitInt;
23 };
24 
float2Bits(float x)25 static inline int32_t float2Bits(float x) noexcept {
26     floatIntUnion data; // NOLINT(cppcoreguidelines-pro-type-member-init)
27     data.value = x;
28     return data.signBitInt;
29 }
30 
isFloatFinite(int32_t bits)31 constexpr bool isFloatFinite(int32_t bits) noexcept {
32     constexpr int32_t kFloatBitsExponentMask = 0x7F800000;
33     return (bits & kFloatBitsExponentMask) != kFloatBitsExponentMask;
34 }
35 
isFinite(float v)36 static inline bool isFinite(float v) noexcept {
37     return isFloatFinite(float2Bits(v));
38 }
39 
40 #pragma clang diagnostic push
41 #pragma ide diagnostic ignored "cppcoreguidelines-narrowing-conversions"
canNormalize(float dx,float dy)42 static bool canNormalize(float dx, float dy) noexcept {
43     return (isFinite(dx) && isFinite(dy)) && (dx || dy);
44 }
45 #pragma clang diagnostic pop
46 
equals(const Point & p1,const Point & p2)47 static bool equals(const Point& p1, const Point& p2) noexcept {
48     return !canNormalize(p1.x - p2.x, p1.y - p2.y);
49 }
50 
isFinite(const float array[],int count)51 constexpr bool isFinite(const float array[], int count) noexcept {
52     float prod = 0.0f;
53     for (int i = 0; i < count; i++) {
54         prod *= array[i];
55     }
56     return prod == 0.0f;
57 }
58 
59 template<typename T>
tabs(T value)60 constexpr T tabs(T value) noexcept {
61     if (value < 0) {
62         value = -value;
63     }
64     return value;
65 }
66 
between(float a,float b,float c)67 constexpr bool between(float a, float b, float c) noexcept {
68     return (a - b) * (c - b) <= 0.0f;
69 }
70 
71 #endif //PATH_SCALAR_H
72