Romain Guy | 5d4bae7 | 2016-11-08 09:49:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 UI_SCALAR_H |
| 18 | #define UI_SCALAR_H |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <cmath> |
| 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | template<typename T> |
| 26 | static constexpr T saturate(T v) noexcept { |
| 27 | return T(std::min(T(1), std::max(T(0), v))); |
| 28 | } |
| 29 | |
| 30 | template<typename T> |
| 31 | static constexpr T clamp(T v, T min, T max) noexcept { |
| 32 | return T(std::min(max, std::max(min, v))); |
| 33 | } |
| 34 | |
| 35 | template<typename T> |
| 36 | static constexpr T mix(T x, T y, T a) noexcept { |
| 37 | return x * (T(1) - a) + y * a; |
| 38 | } |
| 39 | |
| 40 | template<typename T> |
| 41 | static constexpr T lerp(T x, T y, T a) noexcept { |
| 42 | return mix(x, y, a); |
| 43 | } |
| 44 | |
| 45 | namespace details { |
| 46 | static int asInt(float x) { |
| 47 | return *reinterpret_cast<int*>(&x); |
| 48 | } |
| 49 | |
| 50 | static float asFloat(int x) { |
| 51 | return *reinterpret_cast<float*>(&x); |
| 52 | } |
| 53 | |
| 54 | static constexpr float inversesqrtNewtonRaphson(float x, float inverseSqrtX) { |
| 55 | return inverseSqrtX * (-x * 0.5f * (inverseSqrtX * inverseSqrtX) + 1.5f); |
| 56 | } |
| 57 | |
| 58 | static constexpr float rcpNewtonRaphson(float x, float rcpX) { |
| 59 | return rcpX * (-rcpX * x + 2.0f); |
| 60 | } |
| 61 | |
| 62 | static const float inverseSqrtFast(float f, int c) { |
| 63 | int v = details::asInt(f); |
| 64 | v = c - (v >> 1); |
| 65 | return details::asFloat(v); |
| 66 | } |
| 67 | |
| 68 | static const float rcpFast(float f, int c) { |
| 69 | int v = details::asInt(f); |
| 70 | v = c - v; |
| 71 | return details::asFloat(v); |
| 72 | } |
| 73 | } // namespace details |
| 74 | |
| 75 | /** |
| 76 | * Approximates an inverse square root using a specified |
| 77 | * number of Newton-Raphson iterations. The number of iterations |
| 78 | * can be: |
| 79 | * |
| 80 | * - 0, with a precision of ~3.4% over the full range |
| 81 | * - 1, with a precision of ~0.2% over the full range |
| 82 | * - 2, with a precision of ~4e-4% over the full range |
| 83 | */ |
| 84 | template<int> |
| 85 | static float inversesqrtFast(float f) noexcept; |
| 86 | |
| 87 | template<> |
| 88 | float inversesqrtFast<0>(float f) noexcept { |
| 89 | return details::inverseSqrtFast(f, 0x5f3759df); |
| 90 | } |
| 91 | |
| 92 | template<> |
| 93 | float inversesqrtFast<1>(float f) noexcept { |
| 94 | float x = details::inverseSqrtFast(f, 0x5f375a86); |
| 95 | return details::inversesqrtNewtonRaphson(f, x); |
| 96 | } |
| 97 | |
| 98 | template<> |
| 99 | float inversesqrtFast<2>(float f) noexcept { |
| 100 | float x = details::inverseSqrtFast(f, 0x5f375a86); |
| 101 | x = details::inversesqrtNewtonRaphson(f, x); |
| 102 | x = details::inversesqrtNewtonRaphson(f, x); |
| 103 | return x; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Approximates a square root using a specified number of |
| 108 | * Newton-Raphson iterations. The number of iterations can be: |
| 109 | * |
| 110 | * - 0, with a precision of ~0.7% over the full range |
| 111 | * - 1, with a precision of ~0.2% over the full range |
| 112 | * - 2, with a precision of ~4e-4% over the full range |
| 113 | */ |
| 114 | template<int> |
| 115 | static float sqrtFast(float f) noexcept; |
| 116 | |
| 117 | template<> |
| 118 | float sqrtFast<0>(float f) noexcept { |
| 119 | int v = details::asInt(f); |
| 120 | v = 0x1fbd1df5 + (v >> 1); |
| 121 | return details::asFloat(v); |
| 122 | } |
| 123 | |
| 124 | template<> |
| 125 | float sqrtFast<1>(float f) noexcept { |
| 126 | return f * inversesqrtFast<1>(f); |
| 127 | } |
| 128 | |
| 129 | template<> |
| 130 | float sqrtFast<2>(float f) noexcept { |
| 131 | return f * inversesqrtFast<2>(f); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Approximates a reciprocal using a specified number |
| 136 | * of Newton-Raphson iterations. The number of iterations |
| 137 | * can be: |
| 138 | * |
| 139 | * - 0, with a precision of ~0.4% over the full range |
| 140 | * - 1, with a precision of ~0.02% over the full range |
| 141 | * - 2, with a precision of ~5e-5% over the full range |
| 142 | */ |
| 143 | template<int> |
| 144 | static float rcpFast(float f) noexcept; |
| 145 | |
| 146 | template<> |
| 147 | float rcpFast<0>(float f) noexcept { |
| 148 | return details::rcpFast(f, 0x7ef311c2); |
| 149 | } |
| 150 | |
| 151 | template<> |
| 152 | float rcpFast<1>(float f) noexcept { |
| 153 | float x = details::rcpFast(f, 0x7ef311c3); |
| 154 | return details::rcpNewtonRaphson(f, x); |
| 155 | } |
| 156 | |
| 157 | template<> |
| 158 | float rcpFast<2>(float f) noexcept { |
| 159 | float x = details::rcpFast(f, 0x7ef312ac); |
| 160 | x = details::rcpNewtonRaphson(f, x); |
| 161 | x = details::rcpNewtonRaphson(f, x); |
| 162 | return x; |
| 163 | } |
| 164 | |
| 165 | } // namespace std |
| 166 | |
| 167 | #endif // UI_SCALAR_H |