1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_MATH_FUNCTIONS_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_MATH_FUNCTIONS_H_
#include <cfloat>
#include <cmath>
#include <optional>
#include <type_traits>
#include <utility>
#include "base/notreached.h"
#include "ui/gfx/geometry/sin_cos_degrees.h"
namespace blink {
namespace {
template <class ValueType>
std::pair<ValueType, ValueType> GetNearestMultiples(ValueType a, ValueType b) {
using std::swap;
bool is_negative = a < 0.0;
a = std::abs(a);
b = std::abs(b);
// To get rid of rounding and range problems we use std::fmod
// to get the lower to A multiple of B.
ValueType c = -std::fmod(a, b);
ValueType lower = a + c;
// Sort a, b, c by increasing magnitude so that a + b + c later can
// avoid rounding problems (e.g., if one number is very small compared to
// other).
if (std::abs(a) > std::abs(c)) {
swap(a, c);
}
if (std::abs(a) > std::abs(b)) {
swap(a, b);
}
if (std::abs(b) > std::abs(c)) {
swap(b, c);
}
ValueType upper = a + b + c;
if (is_negative) {
swap(lower, upper);
lower = -lower;
upper = -upper;
}
return {lower, upper};
}
template <class OperatorType, typename ValueType>
std::optional<ValueType> PreCheckSteppedValueFunctionArguments(OperatorType op,
ValueType a,
ValueType b) {
// In round(A, B), if B is 0, the result is NaN.
// In mod(A, B) or rem(A, B), if B is 0, the result is NaN.
// If A and B are both infinite, the result is NaN.
if (b == 0.0 || (std::isinf(a) && std::isinf(b))) {
return std::numeric_limits<ValueType>::quiet_NaN();
}
// If A is exactly equal to an integer multiple of B,
// round() resolves to A exactly.
// If A is infinite but B is finite, the result is the same infinity.
if (OperatorType::kRoundNearest <= op && op <= OperatorType::kRoundToZero &&
(std::fmod(a, b) == 0.0 || (std::isinf(a) && !std::isinf(b)))) {
return a;
}
// In mod(A, B) or rem(A, B), if A is infinite, the result is NaN.
if (OperatorType::kMod <= op && op <= OperatorType::kRem && std::isinf(a)) {
return std::numeric_limits<ValueType>::quiet_NaN();
}
return {};
}
template <typename T>
requires std::floating_point<T>
T TanDegrees(T degrees) {
// Use table values for tan() if possible.
// We pick a pretty arbitrary limit that should be safe.
if (degrees > -90000000.0 && degrees < 90000000.0) {
// Make sure 0, 45, 90, 135, 180, 225 and 270 degrees get exact results.
T n45degrees = degrees / 45.0;
int octant = static_cast<int>(n45degrees);
if (octant == n45degrees) {
constexpr std::array<T, 8> kTanN45 = {
/* 0deg */ 0.0,
/* 45deg */ 1.0,
/* 90deg */ std::numeric_limits<T>::infinity(),
/* 135deg */ -1.0,
/* 180deg */ 0.0,
/* 225deg */ 1.0,
/* 270deg */ -std::numeric_limits<T>::infinity(),
/* 315deg */ -1.0,
};
return kTanN45[octant & 7];
}
}
// Slow path for non-table cases.
T x = Deg2rad(degrees);
return std::tan(x);
}
} // namespace
template <class OperatorType, typename ValueType>
requires std::is_enum_v<OperatorType> && std::floating_point<ValueType>
ValueType EvaluateTrigonometricFunction(
OperatorType op,
ValueType a,
std::optional<ValueType>(b) = std::nullopt) {
switch (op) {
case OperatorType::kSin: {
return gfx::SinCosDegrees(a).sin;
}
case OperatorType::kCos: {
return gfx::SinCosDegrees(a).cos;
}
case OperatorType::kTan: {
return TanDegrees(a);
}
case OperatorType::kAsin: {
ValueType value = Rad2deg(std::asin(a));
DCHECK(value >= -90 && value <= 90 || std::isnan(value));
return value;
}
case OperatorType::kAcos: {
ValueType value = Rad2deg(std::acos(a));
DCHECK(value >= 0 && value <= 180 || std::isnan(value));
return value;
}
case OperatorType::kAtan: {
ValueType value = Rad2deg(std::atan(a));
DCHECK(value >= -90 && value <= 90 || std::isnan(value));
return value;
}
case OperatorType::kAtan2: {
DCHECK(b.has_value());
ValueType value = Rad2deg(std::atan2(a, b.value()));
DCHECK(value >= -180 && value <= 180 || std::isnan(value));
return value;
}
default:
NOTREACHED();
}
}
template <class OperatorType, typename ValueType>
requires std::is_enum_v<OperatorType> && std::floating_point<ValueType>
ValueType EvaluateSteppedValueFunction(OperatorType op,
ValueType a,
ValueType b) {
// https://drafts.csswg.org/css-values/#round-infinities
std::optional<ValueType> pre_check =
PreCheckSteppedValueFunctionArguments(op, a, b);
if (pre_check.has_value()) {
return pre_check.value();
}
// If A is finite but B is infinite, the result depends
// on the <rounding-strategy> and the sign of A:
// -- nearest, to-zero
// If A is positive or +0.0, return +0.0. Otherwise, return -0.0.
// -- up
// If A is positive (not zero), return +∞. If A is +0.0, return +0.0.
// Otherwise, return -0.0. down If A is negative (not zero), return −∞. If A
// is -0.0, return -0.0. Otherwise, return +0.0. If A is infinite but B is
// finite, the result is the same infinity.
auto [lower, upper] = GetNearestMultiples(a, b);
switch (op) {
case OperatorType::kRoundNearest: {
if (!std::isinf(a) && std::isinf(b)) {
return std::signbit(a) ? -0.0 : +0.0;
} else {
// In the negative case we need to swap lower and upper for the nearest
// rounding. This also means tie-breaking should pick the lower rather
// than upper,
const bool a_is_negative = a < 0.0;
if (a_is_negative) {
using std::swap;
swap(lower, upper);
}
const ValueType distance = std::abs(std::fmod(a, b));
const ValueType half_b = std::abs(b) / 2;
if (distance < half_b || (a_is_negative && distance == half_b)) {
return lower;
} else {
return upper;
}
}
}
case OperatorType::kRoundUp: {
if (!std::isinf(a) && std::isinf(b)) {
if (!a) {
return a;
} else {
return std::signbit(a) ? -0.0
: std::numeric_limits<ValueType>::infinity();
}
} else {
return upper;
}
}
case OperatorType::kRoundDown: {
if (!std::isinf(a) && std::isinf(b)) {
if (!a) {
return a;
} else {
return std::signbit(a) ? -std::numeric_limits<ValueType>::infinity()
: +0.0;
}
} else {
return lower;
}
}
case OperatorType::kRoundToZero: {
if (!std::isinf(a) && std::isinf(b)) {
return std::signbit(a) ? -0.0 : +0.0;
} else {
return std::abs(upper) < std::abs(lower) ? upper : lower;
}
}
case OperatorType::kMod: {
// In mod(A, B) only, if B is infinite and A has opposite sign to B
// (including an oppositely-signed zero), the result is NaN.
if (std::isinf(b) && std::signbit(a) != std::signbit(b)) {
return std::numeric_limits<ValueType>::quiet_NaN();
}
// If both arguments are positive or both are negative:
// the value of the function is equal to the value of A
// shifted by the integer multiple of B that brings
// the value between zero and B.
// If the A value and the B step are on opposite sides of zero:
// mod() (short for “modulus”) continues to choose the integer
// multiple of B that puts the value between zero and B.
// std::fmod - the returned value has the same sign as A
// and is less than B in magnitude.
ValueType result = std::fmod(a, b);
if (std::signbit(result) != std::signbit(b)) {
// When the absolute values of arguments are the same, but they
// appear on different sides from zero, the result of std::fmod will be
// either -0 (e.g. mod(-1, 1)), or +0 (e.g. mod(1, -1)), we need to swap
// the sign of the resulting zero to match the sign of the B.
if (std::abs(a) == std::abs(b)) {
return -result;
}
// If the result is on opposite side of zero from B,
// put it between 0 and B. As the result of std::fmod is less
// than B in magnitude, adding B would perform a correct shift.
return result + b;
}
return result;
}
case OperatorType::kRem: {
// If both arguments are positive or both are negative:
// the value of the function is equal to the value of A
// shifted by the integer multiple of B that brings
// the value between zero and B.
// If the A value and the B step are on opposite sides of zero:
// rem() (short for "remainder") chooses the integer multiple of B
// that puts the value between zero and -B,
// avoiding changing the sign of the value.
// std::fmod - the returned value has the same sign as A
// and is less than B in magnitude.
return std::fmod(a, b);
}
default:
NOTREACHED();
}
}
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_MATH_FUNCTIONS_H_
|