Use std::hypot() where possible
authorzandobersek@gmail.com <zandobersek@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 3 Feb 2020 14:14:29 +0000 (14:14 +0000)
committerzandobersek@gmail.com <zandobersek@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 3 Feb 2020 14:14:29 +0000 (14:14 +0000)
https://bugs.webkit.org/show_bug.cgi?id=198483

Reviewed by Carlos Garcia Campos.

Use std::hypot() where possible, allowing for a possibly more precise
calculation of square roots of sums of two or three square values.

Source/WebCore:

* css/CSSGradientValue.cpp:
(WebCore::horizontalEllipseRadius):
* platform/audio/FFTFrame.cpp:
(WebCore::FFTFrame::print):
* platform/graphics/FloatPoint.cpp:
(WebCore::FloatPoint::length const): Deleted.
* platform/graphics/FloatPoint.h:
(WebCore::FloatPoint::length const):
* platform/graphics/FloatPoint3D.h:
(WebCore::FloatPoint3D::length const):
* platform/graphics/FloatSize.cpp:
(WebCore::FloatSize::diagonalLength const): Deleted.
* platform/graphics/FloatSize.h:
(WebCore::FloatSize::diagonalLength const):
* platform/graphics/GeometryUtilities.cpp:
(WebCore::euclidianDistance):
* platform/graphics/GraphicsContext.cpp:
(WebCore::GraphicsContext::computeLineBoundsAndAntialiasingModeForText):
* platform/graphics/PathTraversalState.cpp:
(WebCore::distanceLine):
* platform/graphics/cairo/CairoOperations.cpp:
(WebCore::Cairo::computeLineBoundsAndAntialiasingModeForText):
* platform/graphics/cairo/PathCairo.cpp:
(WebCore::Path::addArcTo):
* platform/graphics/cg/GraphicsContextCG.cpp:
(WebCore::GraphicsContext::roundToDevicePixels):
* platform/graphics/filters/FETurbulence.cpp:
(WebCore::FETurbulence::initPaint):
* platform/graphics/transforms/AffineTransform.cpp:
(WebCore::AffineTransform::xScale const):
(WebCore::AffineTransform::yScale const):
* platform/graphics/transforms/RotateTransformOperation.cpp:
(WebCore::RotateTransformOperation::blend):
* platform/graphics/transforms/TransformationMatrix.cpp:
(WebCore::v3Length):
(WebCore::decompose2):
(WebCore::TransformationMatrix::rotate3d):
* rendering/RenderThemeIOS.mm:
(WebCore::shortened):
* rendering/style/BasicShapes.cpp:
(WebCore::BasicShapeCircle::floatValueForRadiusInBox const):
* rendering/svg/SVGRenderingContext.cpp:
(WebCore::SVGRenderingContext::calculateScreenFontSizeScalingFactor):
* svg/SVGAnimateMotionElement.cpp:
(WebCore::SVGAnimateMotionElement::calculateDistance):
* svg/SVGLengthContext.cpp:
(WebCore::SVGLengthContext::valueForLength):
(WebCore::SVGLengthContext::convertValueFromUserUnitsToPercentage const):
(WebCore::SVGLengthContext::convertValueFromPercentageToUserUnits const):
* svg/SVGTransformDistance.cpp:
(WebCore::SVGTransformDistance::distance const):
* svg/properties/SVGAnimationAdditiveValueFunctionImpl.h:

Source/WebKit:

* Platform/classifier/ResourceLoadStatisticsClassifier.cpp:
(WebKit::vectorLength):
* UIProcess/ios/forms/WKFocusedFormControlView.mm:
(-[WKFocusedFormControlView scrollOffsetForCrownInputOffset:]):

Tools:

* TestWebKitAPI/mac/DragAndDropSimulatorMac.mm:
(-[DragAndDropSimulator initialProgressForMouseDrag]):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@255559 268f45cc-cd09-0410-ab3c-d52691b4dbfc

30 files changed:
Source/WebCore/ChangeLog
Source/WebCore/css/CSSGradientValue.cpp
Source/WebCore/platform/audio/FFTFrame.cpp
Source/WebCore/platform/graphics/FloatPoint.cpp
Source/WebCore/platform/graphics/FloatPoint.h
Source/WebCore/platform/graphics/FloatPoint3D.h
Source/WebCore/platform/graphics/FloatSize.cpp
Source/WebCore/platform/graphics/FloatSize.h
Source/WebCore/platform/graphics/GeometryUtilities.cpp
Source/WebCore/platform/graphics/GraphicsContext.cpp
Source/WebCore/platform/graphics/PathTraversalState.cpp
Source/WebCore/platform/graphics/cairo/CairoOperations.cpp
Source/WebCore/platform/graphics/cairo/PathCairo.cpp
Source/WebCore/platform/graphics/cg/GraphicsContextCG.cpp
Source/WebCore/platform/graphics/filters/FETurbulence.cpp
Source/WebCore/platform/graphics/transforms/AffineTransform.cpp
Source/WebCore/platform/graphics/transforms/RotateTransformOperation.cpp
Source/WebCore/platform/graphics/transforms/TransformationMatrix.cpp
Source/WebCore/rendering/RenderThemeIOS.mm
Source/WebCore/rendering/style/BasicShapes.cpp
Source/WebCore/rendering/svg/SVGRenderingContext.cpp
Source/WebCore/svg/SVGAnimateMotionElement.cpp
Source/WebCore/svg/SVGLengthContext.cpp
Source/WebCore/svg/SVGTransformDistance.cpp
Source/WebCore/svg/properties/SVGAnimationAdditiveValueFunctionImpl.h
Source/WebKit/ChangeLog
Source/WebKit/Platform/classifier/ResourceLoadStatisticsClassifier.cpp
Source/WebKit/UIProcess/ios/forms/WKFocusedFormControlView.mm
Tools/ChangeLog
Tools/TestWebKitAPI/mac/DragAndDropSimulatorMac.mm

index 2976827..72f03a4 100644 (file)
@@ -1,3 +1,66 @@
+2020-02-03  Zan Dobersek  <zdobersek@igalia.com>
+
+        Use std::hypot() where possible
+        https://bugs.webkit.org/show_bug.cgi?id=198483
+
+        Reviewed by Carlos Garcia Campos.
+
+        Use std::hypot() where possible, allowing for a possibly more precise
+        calculation of square roots of sums of two or three square values.
+
+        * css/CSSGradientValue.cpp:
+        (WebCore::horizontalEllipseRadius):
+        * platform/audio/FFTFrame.cpp:
+        (WebCore::FFTFrame::print):
+        * platform/graphics/FloatPoint.cpp:
+        (WebCore::FloatPoint::length const): Deleted.
+        * platform/graphics/FloatPoint.h:
+        (WebCore::FloatPoint::length const):
+        * platform/graphics/FloatPoint3D.h:
+        (WebCore::FloatPoint3D::length const):
+        * platform/graphics/FloatSize.cpp:
+        (WebCore::FloatSize::diagonalLength const): Deleted.
+        * platform/graphics/FloatSize.h:
+        (WebCore::FloatSize::diagonalLength const):
+        * platform/graphics/GeometryUtilities.cpp:
+        (WebCore::euclidianDistance):
+        * platform/graphics/GraphicsContext.cpp:
+        (WebCore::GraphicsContext::computeLineBoundsAndAntialiasingModeForText):
+        * platform/graphics/PathTraversalState.cpp:
+        (WebCore::distanceLine):
+        * platform/graphics/cairo/CairoOperations.cpp:
+        (WebCore::Cairo::computeLineBoundsAndAntialiasingModeForText):
+        * platform/graphics/cairo/PathCairo.cpp:
+        (WebCore::Path::addArcTo):
+        * platform/graphics/cg/GraphicsContextCG.cpp:
+        (WebCore::GraphicsContext::roundToDevicePixels):
+        * platform/graphics/filters/FETurbulence.cpp:
+        (WebCore::FETurbulence::initPaint):
+        * platform/graphics/transforms/AffineTransform.cpp:
+        (WebCore::AffineTransform::xScale const):
+        (WebCore::AffineTransform::yScale const):
+        * platform/graphics/transforms/RotateTransformOperation.cpp:
+        (WebCore::RotateTransformOperation::blend):
+        * platform/graphics/transforms/TransformationMatrix.cpp:
+        (WebCore::v3Length):
+        (WebCore::decompose2):
+        (WebCore::TransformationMatrix::rotate3d):
+        * rendering/RenderThemeIOS.mm:
+        (WebCore::shortened):
+        * rendering/style/BasicShapes.cpp:
+        (WebCore::BasicShapeCircle::floatValueForRadiusInBox const):
+        * rendering/svg/SVGRenderingContext.cpp:
+        (WebCore::SVGRenderingContext::calculateScreenFontSizeScalingFactor):
+        * svg/SVGAnimateMotionElement.cpp:
+        (WebCore::SVGAnimateMotionElement::calculateDistance):
+        * svg/SVGLengthContext.cpp:
+        (WebCore::SVGLengthContext::valueForLength):
+        (WebCore::SVGLengthContext::convertValueFromUserUnitsToPercentage const):
+        (WebCore::SVGLengthContext::convertValueFromPercentageToUserUnits const):
+        * svg/SVGTransformDistance.cpp:
+        (WebCore::SVGTransformDistance::distance const):
+        * svg/properties/SVGAnimationAdditiveValueFunctionImpl.h:
+
 2020-02-03  Jonathan Kingston  <jonathan@jooped.co.uk>
 
         Crash in WebKitAccessible
index 75d8095..014a55c 100644 (file)
@@ -1133,7 +1133,7 @@ static inline float horizontalEllipseRadius(const FloatSize& p, float aspectRati
     // x^2/a^2 + y^2/b^2 = 1
     // a/b = aspectRatio, b = a/aspectRatio
     // a = sqrt(x^2 + y^2/(1/r^2))
-    return sqrtf(p.width() * p.width() + (p.height() * p.height()) / (1 / (aspectRatio * aspectRatio)));
+    return std::hypot(p.width(), p.height() * aspectRatio);
 }
 
 // FIXME: share code with the linear version
index 4c15206..3b5b569 100644 (file)
@@ -259,7 +259,7 @@ void FFTFrame::print()
     int n = m_FFTSize / 2;
 
     for (int i = 1; i < n; i++) {
-        double mag = sqrt(realP[i] * realP[i] + imagP[i] * imagP[i]);
+        double mag = std::hypot(realP[i], imagP[i]);
         double phase = atan2(realP[i], imagP[i]);
 
         LOG(WebAudio, "[%d] (%f %f)\n", i, mag, phase);
index 3b661ae..fbab72a 100644 (file)
@@ -64,11 +64,6 @@ float FloatPoint::slopeAngleRadians() const
     return atan2f(m_y, m_x);
 }
 
-float FloatPoint::length() const
-{
-    return sqrtf(lengthSquared());
-}
-
 FloatPoint FloatPoint::matrixTransform(const AffineTransform& transform) const
 {
     double newX, newY;
index cefd8d4..455bb25 100644 (file)
@@ -147,7 +147,11 @@ public:
     }
 
     float slopeAngleRadians() const;
-    float length() const;
+
+    float length() const
+    {
+        return std::hypot(m_x, m_y);
+    }
 
     float lengthSquared() const
     {
index fde468d..51e6e7e 100644 (file)
@@ -119,7 +119,7 @@ public:
     }
 
     float lengthSquared() const { return this->dot(*this); }
-    float length() const { return sqrtf(lengthSquared()); }
+    float length() const { return std::hypot(m_x, m_y, m_z); }
     
     float distanceTo(const FloatPoint3D& a) const;
 
index 6c5741d..d54024d 100644 (file)
@@ -50,11 +50,6 @@ FloatSize FloatSize::constrainedBetween(const FloatSize& min, const FloatSize& m
     };
 }
 
-float FloatSize::diagonalLength() const
-{
-    return sqrtf(diagonalLengthSquared());
-}
-
 bool FloatSize::isZero() const
 {
     return fabs(m_width) < std::numeric_limits<float>::epsilon() && fabs(m_height) < std::numeric_limits<float>::epsilon();
index bc6b253..8bad61b 100644 (file)
@@ -123,7 +123,10 @@ public:
            m_height < other.m_height ? m_height : other.m_height);
     }
 
-    WEBCORE_EXPORT float diagonalLength() const;
+    float diagonalLength() const
+    {
+        return std::hypot(m_width, m_height);
+    }
 
     float diagonalLengthSquared() const
     {
index 0099cef..bad2e81 100644 (file)
@@ -32,7 +32,7 @@ namespace WebCore {
 float euclidianDistance(const FloatPoint& p1, const FloatPoint& p2)
 {
     FloatSize delta = p1 - p2;
-    return sqrt(delta.width() * delta.width() + delta.height() * delta.height());
+    return std::hypot(delta.width(), delta.height());
 }
 
 float findSlope(const FloatPoint& p1, const FloatPoint& p2, float& c)
index 269272b..c2e3c01 100644 (file)
@@ -1117,7 +1117,7 @@ FloatRect GraphicsContext::computeLineBoundsAndAntialiasingModeForText(const Flo
 
     AffineTransform transform = getCTM(GraphicsContext::DefinitelyIncludeDeviceScale);
     // Just compute scale in x dimension, assuming x and y scales are equal.
-    float scale = transform.b() ? sqrtf(transform.a() * transform.a() + transform.b() * transform.b()) : transform.a();
+    float scale = transform.b() ? std::hypot(transform.a(), transform.b()) : transform.a();
     if (scale < 1.0) {
         // This code always draws a line that is at least one-pixel line high,
         // which tends to visually overwhelm text at small scales. To counter this
index 8a78222..7d3e9ba 100644 (file)
@@ -35,9 +35,7 @@ static inline FloatPoint midPoint(const FloatPoint& first, const FloatPoint& sec
 
 static inline float distanceLine(const FloatPoint& start, const FloatPoint& end)
 {
-    float dx = end.x() - start.x();
-    float dy = end.y() - start.y();
-    return sqrtf(dx * dx + dy * dy);
+    return std::hypot(end.x() - start.x(), end.y() - start.y());
 }
 
 struct QuadraticBezier {
index bcdcf76..68984d5 100644 (file)
@@ -395,7 +395,7 @@ FloatRect computeLineBoundsAndAntialiasingModeForText(PlatformContextCairo& plat
 
     AffineTransform transform = Cairo::State::getCTM(platformContext);
     // Just compute scale in x dimension, assuming x and y scales are equal.
-    float scale = transform.b() ? sqrtf(transform.a() * transform.a() + transform.b() * transform.b()) : transform.a();
+    float scale = transform.b() ? std::hypot(transform.a(), transform.b()) : transform.a();
     if (scale < 1.0) {
         // This code always draws a line that is at least one-pixel line high,
         // which tends to visually overwhelm text at small scales. To counter this
index f04f190..e1b554c 100644 (file)
@@ -242,8 +242,8 @@ void Path::addArcTo(const FloatPoint& p1, const FloatPoint& p2, float radius)
 
     FloatPoint p1p0((p0.x() - p1.x()),(p0.y() - p1.y()));
     FloatPoint p1p2((p2.x() - p1.x()),(p2.y() - p1.y()));
-    float p1p0_length = sqrtf(p1p0.x() * p1p0.x() + p1p0.y() * p1p0.y());
-    float p1p2_length = sqrtf(p1p2.x() * p1p2.x() + p1p2.y() * p1p2.y());
+    float p1p0_length = std::hypot(p1p0.x(), p1p0.y());
+    float p1p2_length = std::hypot(p1p2.x(), p1p2.y());
 
     double cos_phi = (p1p0.x() * p1p2.x() + p1p0.y() * p1p2.y()) / (p1p0_length * p1p2_length);
     // all points on a line logic
@@ -265,7 +265,7 @@ void Path::addArcTo(const FloatPoint& p1, const FloatPoint& p2, float radius)
     FloatPoint t_p1p0((p1.x() + factor_p1p0 * p1p0.x()), (p1.y() + factor_p1p0 * p1p0.y()));
 
     FloatPoint orth_p1p0(p1p0.y(), -p1p0.x());
-    float orth_p1p0_length = sqrt(orth_p1p0.x() * orth_p1p0.x() + orth_p1p0.y() * orth_p1p0.y());
+    float orth_p1p0_length = std::hypot(orth_p1p0.x(), orth_p1p0.y());
     float factor_ra = radius / orth_p1p0_length;
 
     // angle between orth_p1p0 and p1p2 to get the right vector orthographic to p1p0
index c54b0b9..e0df395 100644 (file)
@@ -1551,8 +1551,8 @@ FloatRect GraphicsContext::roundToDevicePixels(const FloatRect& rect, RoundingMo
         return roundedIntRect(rect);
     }
 
-    float deviceScaleX = sqrtf(deviceMatrix.a * deviceMatrix.a + deviceMatrix.b * deviceMatrix.b);
-    float deviceScaleY = sqrtf(deviceMatrix.c * deviceMatrix.c + deviceMatrix.d * deviceMatrix.d);
+    float deviceScaleX = std::hypot(deviceMatrix.a, deviceMatrix.b);
+    float deviceScaleY = std::hypot(deviceMatrix.c, deviceMatrix.d);
 
     CGPoint deviceOrigin = CGPointMake(rect.x() * deviceScaleX, rect.y() * deviceScaleY);
     CGPoint deviceLowerRight = CGPointMake((rect.x() + rect.width()) * deviceScaleX,
index 8227879..6d60cdd 100644 (file)
@@ -154,7 +154,7 @@ void FETurbulence::initPaint(PaintingData& paintingData)
                 gradient[0] = static_cast<float>((paintingData.random() % (2 * s_blockSize)) - s_blockSize) / s_blockSize;
                 gradient[1] = static_cast<float>((paintingData.random() % (2 * s_blockSize)) - s_blockSize) / s_blockSize;
             } while (!gradient[0] && !gradient[1]);
-            normalizationFactor = sqrtf(gradient[0] * gradient[0] + gradient[1] * gradient[1]);
+            normalizationFactor = std::hypot(gradient[0], gradient[1]);
             gradient[0] /= normalizationFactor;
             gradient[1] /= normalizationFactor;
         }
index 34398a3..3c2cc2a 100644 (file)
@@ -85,12 +85,12 @@ bool AffineTransform::isIdentity() const
 
 double AffineTransform::xScale() const
 {
-    return sqrt(m_transform[0] * m_transform[0] + m_transform[1] * m_transform[1]);
+    return std::hypot(m_transform[0], m_transform[1]);
 }
 
 double AffineTransform::yScale() const
 {
-    return sqrt(m_transform[2] * m_transform[2] + m_transform[3] * m_transform[3]);
+    return std::hypot(m_transform[2], m_transform[3]);
 }
 
 static double det(const std::array<double, 6>& transform)
index 7316d33..962e7c1 100644 (file)
@@ -84,7 +84,7 @@ Ref<TransformOperation> RotateTransformOperation::blend(const TransformOperation
     double x = -decomp.quaternionX;
     double y = -decomp.quaternionY;
     double z = -decomp.quaternionZ;
-    double length = sqrt(x * x + y * y + z * z);
+    double length = std::hypot(x, y, z);
     double angle = 0;
     
     if (length > 0.00001) {
index 394109d..ee30a21 100644 (file)
@@ -254,7 +254,7 @@ static void v4MulPointByMatrix(const Vector4 p, const TransformationMatrix::Matr
 
 static double v3Length(Vector3 a)
 {
-    return sqrt((a[0] * a[0]) + (a[1] * a[1]) + (a[2] * a[2]));
+    return std::hypot(a[0], a[1], a[2]);
 }
 
 static void v3Scale(Vector3 v, double desiredLength)
@@ -300,8 +300,8 @@ static bool decompose2(const TransformationMatrix::Matrix4& matrix, Transformati
     result.translateY = matrix[3][1];
 
     // Compute scaling factors.
-    result.scaleX = sqrt(row0x * row0x + row0y * row0y);
-    result.scaleY = sqrt(row1x * row1x + row1y * row1y);
+    result.scaleX = std::hypot(row0x, row0y);
+    result.scaleY = std::hypot(row1x, row1y);
 
     // If determinant is negative, one axis was flipped.
     double determinant = row0x * row1y - row0y * row1x;
@@ -813,7 +813,7 @@ TransformationMatrix& TransformationMatrix::scale3d(double sx, double sy, double
 TransformationMatrix& TransformationMatrix::rotate3d(double x, double y, double z, double angle)
 {
     // Normalize the axis of rotation
-    double length = sqrt(x * x + y * y + z * z);
+    double length = std::hypot(x, y, z);
     if (length == 0) {
         // A direction vector that cannot be normalized, such as [0, 0, 0], will cause the rotation to not be applied.
         return *this;
index 47455a0..09a12dc 100644 (file)
@@ -359,7 +359,7 @@ static CGPoint shortened(CGPoint start, CGPoint end, float width)
 {
     float x = end.x - start.x;
     float y = end.y - start.y;
-    float ratio = (!x && !y) ? 0 : width / sqrtf(x * x + y * y);
+    float ratio = (!x && !y) ? 0 : width / std::hypot(x, y);
     return CGPointMake(start.x + x * ratio, start.y + y * ratio);
 }
 
index ca681af..7a82343 100644 (file)
@@ -28,7 +28,6 @@
  */
 
 #include "config.h"
-
 #include "BasicShapes.h"
 
 #include "BasicShapeFunctions.h"
@@ -40,7 +39,7 @@
 #include "RenderBox.h"
 #include "SVGPathByteStream.h"
 #include "SVGPathUtilities.h"
-
+#include <wtf/MathExtras.h>
 #include <wtf/NeverDestroyed.h>
 #include <wtf/TinyLRUCache.h>
 #include <wtf/text/TextStream.h>
@@ -159,7 +158,7 @@ bool BasicShapeCircle::operator==(const BasicShape& other) const
 float BasicShapeCircle::floatValueForRadiusInBox(float boxWidth, float boxHeight) const
 {
     if (m_radius.type() == BasicShapeRadius::Value)
-        return floatValueForLength(m_radius.value(), sqrtf((boxWidth * boxWidth + boxHeight * boxHeight) / 2));
+        return floatValueForLength(m_radius.value(), std::hypot(boxWidth, boxHeight) / sqrtOfTwoFloat);
 
     float centerX = floatValueForCenterCoordinate(m_centerX, boxWidth);
     float centerY = floatValueForCenterCoordinate(m_centerY, boxHeight);
index d78694a..6075f63 100644 (file)
@@ -38,6 +38,7 @@
 #include "SVGLengthContext.h"
 #include "SVGResources.h"
 #include "SVGResourcesCache.h"
+#include <wtf/MathExtras.h>
 
 namespace WebCore {
 
@@ -201,7 +202,7 @@ static AffineTransform& currentContentTransformation()
 float SVGRenderingContext::calculateScreenFontSizeScalingFactor(const RenderObject& renderer)
 {
     AffineTransform ctm = calculateTransformationToOutermostCoordinateSystem(renderer);
-    return narrowPrecisionToFloat(sqrt((pow(ctm.xScale(), 2) + pow(ctm.yScale(), 2)) / 2));
+    return narrowPrecisionToFloat(std::hypot(ctm.xScale(), ctm.yScale()) / sqrtOfTwoDouble);
 }
 
 AffineTransform SVGRenderingContext::calculateTransformationToOutermostCoordinateSystem(const RenderObject& renderer)
index d7db0ac..5d05211 100644 (file)
@@ -282,7 +282,7 @@ Optional<float> SVGAnimateMotionElement::calculateDistance(const String& fromStr
     if (!parsePoint(toString, to))
         return { };
     FloatSize diff = to - from;
-    return sqrtf(diff.width() * diff.width() + diff.height() * diff.height());
+    return std::hypot(diff.width(), diff.height());
 }
 
 void SVGAnimateMotionElement::updateAnimationMode()
index 2322fa2..e68877d 100644 (file)
@@ -32,6 +32,7 @@
 #include "RenderSVGViewportContainer.h"
 #include "RenderView.h"
 #include "SVGSVGElement.h"
+#include <wtf/MathExtras.h>
 
 namespace WebCore {
 
@@ -105,7 +106,7 @@ float SVGLengthContext::valueForLength(const Length& length, SVGLengthMode lengt
     case SVGLengthMode::Height:
         return floatValueForLength(length, viewportSize.height());
     case SVGLengthMode::Other:
-        return floatValueForLength(length, std::sqrt(viewportSize.diagonalLengthSquared() / 2));
+        return floatValueForLength(length, viewportSize.diagonalLength() / sqrtOfTwoFloat);
     };
     return 0;
 }
@@ -192,7 +193,7 @@ ExceptionOr<float> SVGLengthContext::convertValueFromUserUnitsToPercentage(float
     case SVGLengthMode::Height:
         return value / viewportSize.height() * 100;
     case SVGLengthMode::Other:
-        return value / (std::sqrt(viewportSize.diagonalLengthSquared() / 2)) * 100;
+        return value / (viewportSize.diagonalLength() / sqrtOfTwoFloat) * 100;
     };
 
     ASSERT_NOT_REACHED();
@@ -211,7 +212,7 @@ ExceptionOr<float> SVGLengthContext::convertValueFromPercentageToUserUnits(float
     case SVGLengthMode::Height:
         return value * viewportSize.height();
     case SVGLengthMode::Other:
-        return value * std::sqrt(viewportSize.diagonalLengthSquared() / 2);
+        return value * viewportSize.diagonalLength() / sqrtOfTwoFloat;
     };
 
     ASSERT_NOT_REACHED();
index 8f8bfbb..681543e 100644 (file)
@@ -217,11 +217,11 @@ float SVGTransformDistance::distance() const
     case SVGTransformValue::SVG_TRANSFORM_UNKNOWN:
         return 0;
     case SVGTransformValue::SVG_TRANSFORM_ROTATE:
-        return sqrtf(m_angle * m_angle + m_cx * m_cx + m_cy * m_cy);
+        return std::hypot(m_angle, m_cx, m_cy);
     case SVGTransformValue::SVG_TRANSFORM_SCALE:
-        return static_cast<float>(sqrt(m_transform.a() * m_transform.a() + m_transform.d() * m_transform.d()));
+        return static_cast<float>(std::hypot(m_transform.a(), m_transform.d()));
     case SVGTransformValue::SVG_TRANSFORM_TRANSLATE:
-        return static_cast<float>(sqrt(m_transform.e() * m_transform.e() + m_transform.f() * m_transform.f()));
+        return static_cast<float>(std::hypot(m_transform.e(), m_transform.f()));
     case SVGTransformValue::SVG_TRANSFORM_SKEWX:
     case SVGTransformValue::SVG_TRANSFORM_SKEWY:
         return m_angle;
index df82acd..e8febe0 100644 (file)
@@ -102,7 +102,7 @@ public:
         float red = fromColor.red() - toColor.red();
         float green = fromColor.green() - toColor.green();
         float blue = fromColor.blue() - toColor.blue();
-        return sqrtf(red * red + green * green + blue * blue);
+        return std::hypot(red, green, blue);
     }
 
 private:
index 406b31d..fda31e8 100644 (file)
@@ -1,3 +1,18 @@
+2020-02-03  Zan Dobersek  <zdobersek@igalia.com>
+
+        Use std::hypot() where possible
+        https://bugs.webkit.org/show_bug.cgi?id=198483
+
+        Reviewed by Carlos Garcia Campos.
+
+        Use std::hypot() where possible, allowing for a possibly more precise
+        calculation of square roots of sums of two or three square values.
+
+        * Platform/classifier/ResourceLoadStatisticsClassifier.cpp:
+        (WebKit::vectorLength):
+        * UIProcess/ios/forms/WKFocusedFormControlView.mm:
+        (-[WKFocusedFormControlView scrollOffsetForCrownInputOffset:]):
+
 2020-02-01  Michael Catanzaro  <mcatanzaro@gnome.org>
 
         Silence compiler warnings
index b4492a4..dd38cf5 100644 (file)
@@ -34,7 +34,7 @@ using namespace WebCore;
 
 static double vectorLength(unsigned a, unsigned b, unsigned c)
 {
-    return sqrt(a * a + b * b + c * c);
+    return std::hypot(a, b, c);
 }
 
 static const auto featureVectorLengthThresholdHigh = 3;
index d72adab..fe742e4 100644 (file)
@@ -396,7 +396,7 @@ static NSDictionary *submitActionNameFontAttributes()
     }
 
     if (targetHighlightedFrameDeltaX || targetHighlightedFrameDeltaY) {
-        CGFloat distanceToTarget = std::sqrt(targetHighlightedFrameDeltaX * targetHighlightedFrameDeltaX + targetHighlightedFrameDeltaY * targetHighlightedFrameDeltaY);
+        CGFloat distanceToTarget = std::hypot(targetHighlightedFrameDeltaX, targetHighlightedFrameDeltaY)
         unitVectorToTarget = CGVector { targetHighlightedFrameDeltaX / distanceToTarget, targetHighlightedFrameDeltaY / distanceToTarget };
     }
 
index 48b4264..c91221a 100644 (file)
@@ -1,3 +1,16 @@
+2020-02-03  Zan Dobersek  <zdobersek@igalia.com>
+
+        Use std::hypot() where possible
+        https://bugs.webkit.org/show_bug.cgi?id=198483
+
+        Reviewed by Carlos Garcia Campos.
+
+        Use std::hypot() where possible, allowing for a possibly more precise
+        calculation of square roots of sums of two or three square values.
+
+        * TestWebKitAPI/mac/DragAndDropSimulatorMac.mm:
+        (-[DragAndDropSimulator initialProgressForMouseDrag]):
+
 2020-02-03  Víctor Manuel Jáquez Leal  <vjaquez@igalia.com>
 
         [JHBuild] remove libvpx
index b205679..e666894 100644 (file)
@@ -151,7 +151,7 @@ static NSImage *defaultExternalDragImage()
 
 - (double)initialProgressForMouseDrag
 {
-    double totalDistance = std::sqrt(std::pow(_startLocationInWindow.x - _endLocationInWindow.x, 2) + std::pow(_startLocationInWindow.y - _endLocationInWindow.y, 2));
+    double totalDistance = std::hypot(_startLocationInWindow.x - _endLocationInWindow.x, _startLocationInWindow.y - _endLocationInWindow.y);
     return !totalDistance ? 1 : std::min<double>(1, initialMouseDragDistance / totalDistance);
 }