/* * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_MATH_EXTRAS_H_ #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_MATH_EXTRAS_H_ #include <cmath> #include <cstddef> #include <limits> #include "base/check_op.h" #include "build/build_config.h" #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h" #if defined(COMPILER_MSVC) // Make math.h behave like other platforms. #define _USE_MATH_DEFINES // Even if math.h was already included, including math.h again with // _USE_MATH_DEFINES adds the extra defines. #include <math.h> #include <stdint.h> #endif #if BUILDFLAG(IS_OPENBSD) #include <machine/ieee.h> #include <sys/types.h> #endif constexpr double kPiDouble = …; constexpr float kPiFloat = …; constexpr double kPiOverTwoDouble = …; constexpr float kPiOverTwoFloat = …; constexpr double kPiOverFourDouble = …; constexpr float kPiOverFourFloat = …; constexpr double kTwoPiDouble = …; constexpr float kTwoPiFloat = …; constexpr double Deg2rad(double d) { … } constexpr double Rad2deg(double r) { … } constexpr double Deg2grad(double d) { … } constexpr double Grad2deg(double g) { … } constexpr double Turn2deg(double t) { … } constexpr double Deg2turn(double d) { … } constexpr double Rad2grad(double r) { … } constexpr double Grad2rad(double g) { … } constexpr double Turn2grad(double t) { … } constexpr double Grad2turn(double g) { … } constexpr double Rad2turn(double r) { … } constexpr double Turn2rad(double t) { … } constexpr float Deg2rad(float d) { … } constexpr float Rad2deg(float r) { … } constexpr float Deg2grad(float d) { … } constexpr float Grad2deg(float g) { … } constexpr float Turn2deg(float t) { … } constexpr float Deg2turn(float d) { … } constexpr float Rad2grad(float r) { … } constexpr float Grad2rad(float g) { … } constexpr float Turn2grad(float t) { … } constexpr float Grad2turn(float g) { … } constexpr double RoundHalfTowardsPositiveInfinity(double value) { … } constexpr float RoundHalfTowardsPositiveInfinity(float value) { … } // ClampTo() is implemented by templated helper classes (to allow for partial // template specialization) as well as several helper functions. // This helper function can be called when we know that: // (1) The type signednesses match so the compiler will not produce signed vs. // unsigned warnings // (2) The default type promotions/conversions are sufficient to handle things // correctly template <typename LimitType, typename ValueType> inline constexpr LimitType ClampToDirectComparison(ValueType value, LimitType min, LimitType max) { … } // For any floating-point limits, or integral limits smaller than int64_t, we // can cast the limits to double without losing precision; then the only cases // where |value| can't be represented accurately as a double are the ones where // it's outside the limit range anyway. So doing all comparisons as doubles // will give correct results. // // In some cases, we can get better performance by using // ClampToDirectComparison(). We use a templated class to switch between these // two cases (instead of simply using a conditional within one function) in // order to only compile the ClampToDirectComparison() code for cases where it // will actually be used; this prevents the compiler from emitting warnings // about unsafe code (even though we wouldn't actually be executing that code). template <bool can_use_direct_comparison, typename LimitType, typename ValueType> class ClampToNonLongLongHelper; ClampToNonLongLongHelper<true, LimitType, ValueType>; ClampToNonLongLongHelper<false, LimitType, ValueType>; // The unspecialized version of this templated class handles clamping to // anything other than [u]int64_t limits. It simply uses the class above // to toggle between the "fast" and "safe" clamp implementations. template <typename LimitType, typename ValueType> class ClampToHelper { … }; // Clamping to [u]int64_t limits requires more care. These may not be // accurately representable as doubles, so instead we cast |value| to the // limit type. But that cast is undefined if |value| is floating point and // outside the representable range of the limit type, so we also have to check // for that case explicitly. ClampToHelper<int64_t, ValueType>; // This specialization handles the case where the above partial specialization // would be potentially incorrect. template <> class ClampToHelper<int64_t, uint64_t> { … }; // This is similar to the partial specialization that clamps to int64_t, but // because the lower-bound check is done for integer value types as well, we // don't need a <uint64_t, int64_t> full specialization. ClampToHelper<uint64_t, ValueType>; template <typename T> constexpr T DefaultMaximumForClamp() { … } template <typename T> constexpr T DefaultMinimumForClamp() { … } // And, finally, the actual function for people to call. template <typename LimitType, typename ValueType> constexpr LimitType ClampTo( ValueType value, LimitType min = DefaultMinimumForClamp<LimitType>(), LimitType max = DefaultMaximumForClamp<LimitType>()) { … } template <typename LimitType, typename ValueType> constexpr LimitType ClampToWithNaNTo0( ValueType value, LimitType min = DefaultMinimumForClamp<LimitType>(), LimitType max = DefaultMaximumForClamp<LimitType>()) { … } constexpr bool IsWithinIntRange(float x) { … } static constexpr size_t GreatestCommonDivisor(size_t a, size_t b) { … } constexpr size_t LowestCommonMultiple(size_t a, size_t b) { … } #endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_MATH_EXTRAS_H_