0

Remove wtf/saturated_arithmetic.h

Move SaturatedSet functions to platform/geometry/layout_unit.h. The ARM
asm versions in wtf/saturated_arithmetic_arm.h were moved there too and
some variables were inlined. Do the same with the corresponding tests.

Bug: 943947
Change-Id: I5485ad83f48016245dca1edae27d41ca9ced2772
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1534169
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Reviewed-by: Bruce Dawson <brucedawson@chromium.org>
Commit-Queue: Henrique Ferreiro <hferreiro@igalia.com>
Cr-Commit-Position: refs/heads/master@{#645508}
This commit is contained in:
Henrique Ferreiro
2019-03-28 21:23:23 +00:00
committed by Commit Bot
parent bdd1b3a25c
commit 693356598a
8 changed files with 131 additions and 256 deletions

@ -31,17 +31,18 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_LAYOUT_UNIT_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_LAYOUT_UNIT_H_
#include <climits>
#include <iosfwd>
#include <limits>
#include "base/compiler_specific.h"
#include "base/numerics/clamped_math.h"
#include "base/numerics/safe_conversions.h"
#include "build/build_config.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator.h"
#include "third_party/blink/renderer/platform/wtf/assertions.h"
#include "third_party/blink/renderer/platform/wtf/forward.h"
#include "third_party/blink/renderer/platform/wtf/saturated_arithmetic.h"
namespace blink {
@ -59,6 +60,30 @@ static const int kFixedPointDenominator = 1 << kLayoutUnitFractionalBits;
const int kIntMaxForLayoutUnit = INT_MAX / kFixedPointDenominator;
const int kIntMinForLayoutUnit = INT_MIN / kFixedPointDenominator;
#if defined(ARCH_CPU_ARM_FAMILY) && defined(ARCH_CPU_32_BITS) && \
defined(COMPILER_GCC) && !defined(OS_NACL) && __OPTIMIZE__
inline int GetMaxSaturatedSetResultForTesting() {
// For ARM Asm version the set function maxes out to the biggest
// possible integer part with the fractional part zero'd out.
// e.g. 0x7fffffc0.
return std::numeric_limits<int>::max() & ~(kFixedPointDenominator - 1);
}
inline int GetMinSaturatedSetResultForTesting() {
return std::numeric_limits<int>::min();
}
#else
ALWAYS_INLINE int GetMaxSaturatedSetResultForTesting() {
// For C version the set function maxes out to max int, this differs from
// the ARM asm version.
return std::numeric_limits<int>::max();
}
ALWAYS_INLINE int GetMinSaturatedSetResultForTesting() {
return std::numeric_limits<int>::min();
}
#endif // CPU(ARM) && COMPILER(GCC)
// TODO(thakis): Remove these two lines once http://llvm.org/PR26504 is resolved
class PLATFORM_EXPORT LayoutUnit;
constexpr inline bool operator<(const LayoutUnit&, const LayoutUnit&);
@ -71,9 +96,9 @@ class LayoutUnit {
template <typename IntegerType>
explicit LayoutUnit(IntegerType value) {
if (std::is_signed<IntegerType>::value)
SetValue(static_cast<int>(value));
SaturatedSet(static_cast<int>(value));
else
SetValue(static_cast<unsigned>(value));
SaturatedSet(static_cast<unsigned>(value));
}
explicit LayoutUnit(uint64_t value) {
value_ = base::saturated_cast<int>(value * kFixedPointDenominator);
@ -246,13 +271,78 @@ class LayoutUnit {
std::numeric_limits<int>::max() / kFixedPointDenominator;
}
ALWAYS_INLINE void SetValue(int value) {
value_ = SaturatedSet<kLayoutUnitFractionalBits>(value);
#if defined(ARCH_CPU_ARM_FAMILY) && defined(ARCH_CPU_32_BITS) && \
defined(COMPILER_GCC) && !defined(OS_NACL) && __OPTIMIZE__
// If we're building ARM 32-bit on GCC we replace the C++ versions with some
// native ARM assembly for speed.
inline void SaturatedSet(int value) {
// Figure out how many bits are left for storing the integer part of
// the fixed point number, and saturate our input to that
enum { Saturate = 32 - kLayoutUnitFractionalBits };
int result;
// The following ARM code will Saturate the passed value to the number of
// bits used for the whole part of the fixed point representation, then
// shift it up into place. This will result in the low
// <kLayoutUnitFractionalBits> bits all being 0's. When the value saturates
// this gives a different result to from the C++ case; in the C++ code a
// saturated value has all the low bits set to 1 (for a +ve number at
// least). This cannot be done rapidly in ARM ... we live with the
// difference, for the sake of speed.
asm("ssat %[output],%[saturate],%[value]\n\t"
"lsl %[output],%[shift]"
: [output] "=r"(result)
: [value] "r"(value), [saturate] "n"(Saturate),
[shift] "n"(kLayoutUnitFractionalBits));
value_ = result;
}
inline void SetValue(unsigned value) {
value_ = SaturatedSet<kLayoutUnitFractionalBits>(value);
inline void SaturatedSet(unsigned value) {
// Here we are being passed an unsigned value to saturate,
// even though the result is returned as a signed integer. The ARM
// instruction for unsigned saturation therefore needs to be given one
// less bit (i.e. the sign bit) for the saturation to work correctly; hence
// the '31' below.
enum { Saturate = 31 - kLayoutUnitFractionalBits };
// The following ARM code will Saturate the passed value to the number of
// bits used for the whole part of the fixed point representation, then
// shift it up into place. This will result in the low
// <kLayoutUnitFractionalBits> bits all being 0's. When the value saturates
// this gives a different result to from the C++ case; in the C++ code a
// saturated value has all the low bits set to 1. This cannot be done
// rapidly in ARM, so we live with the difference, for the sake of speed.
int result;
asm("usat %[output],%[saturate],%[value]\n\t"
"lsl %[output],%[shift]"
: [output] "=r"(result)
: [value] "r"(value), [saturate] "n"(Saturate),
[shift] "n"(kLayoutUnitFractionalBits));
value_ = result;
}
#else
ALWAYS_INLINE void SaturatedSet(int value) {
if (value > kIntMaxForLayoutUnit)
value_ = std::numeric_limits<int>::max();
else if (value < kIntMinForLayoutUnit)
value_ = std::numeric_limits<int>::min();
else
value_ = static_cast<unsigned>(value) << kLayoutUnitFractionalBits;
}
ALWAYS_INLINE void SaturatedSet(unsigned value) {
if (value >= (unsigned)kIntMaxForLayoutUnit)
value_ = std::numeric_limits<int>::max();
else
value_ = value << kLayoutUnitFractionalBits;
}
#endif // CPU(ARM) && COMPILER(GCC)
int value_;
};

@ -62,6 +62,40 @@ TEST(LayoutUnitTest, LayoutUnitInt) {
EXPECT_EQ(kIntMaxForLayoutUnit, LayoutUnit(kIntMaxForLayoutUnit + 1).ToInt());
EXPECT_EQ(kIntMaxForLayoutUnit, LayoutUnit(INT_MAX / 2).ToInt());
EXPECT_EQ(kIntMaxForLayoutUnit, LayoutUnit(INT_MAX).ToInt());
// Test the raw unsaturated value
EXPECT_EQ(0, LayoutUnit(0).RawValue());
// Internally the max number we can represent (without saturating)
// is all the (non-sign) bits set except for the bottom n fraction bits
const int max_internal_representation =
std::numeric_limits<int>::max() ^ ((1 << kLayoutUnitFractionalBits) - 1);
EXPECT_EQ(max_internal_representation,
LayoutUnit(kIntMaxForLayoutUnit).RawValue());
EXPECT_EQ(GetMaxSaturatedSetResultForTesting(),
LayoutUnit(kIntMaxForLayoutUnit + 100).RawValue());
EXPECT_EQ((kIntMaxForLayoutUnit - 100) << kLayoutUnitFractionalBits,
LayoutUnit(kIntMaxForLayoutUnit - 100).RawValue());
EXPECT_EQ(GetMinSaturatedSetResultForTesting(),
LayoutUnit(kIntMinForLayoutUnit).RawValue());
EXPECT_EQ(GetMinSaturatedSetResultForTesting(),
LayoutUnit(kIntMinForLayoutUnit - 100).RawValue());
// Shifting negative numbers left has undefined behavior, so use
// multiplication instead of direct shifting here.
EXPECT_EQ((kIntMinForLayoutUnit + 100) * (1 << kLayoutUnitFractionalBits),
LayoutUnit(kIntMinForLayoutUnit + 100).RawValue());
}
TEST(LayoutUnitTest, LayoutUnitUnsigned) {
// Test the raw unsaturated value
EXPECT_EQ(0, LayoutUnit((unsigned)0).RawValue());
EXPECT_EQ(GetMaxSaturatedSetResultForTesting(),
LayoutUnit((unsigned)kIntMaxForLayoutUnit).RawValue());
const unsigned kOverflowed = kIntMaxForLayoutUnit + 100;
EXPECT_EQ(GetMaxSaturatedSetResultForTesting(),
LayoutUnit(kOverflowed).RawValue());
const unsigned kNotOverflowed = kIntMaxForLayoutUnit - 100;
EXPECT_EQ((kIntMaxForLayoutUnit - 100) << kLayoutUnitFractionalBits,
LayoutUnit(kNotOverflowed).RawValue());
}
TEST(LayoutUnitTest, LayoutUnitFloat) {

@ -107,8 +107,6 @@ jumbo_component("wtf") {
"ref_counted.h",
"ref_vector.h",
"sanitizers.h",
"saturated_arithmetic.h",
"saturated_arithmetic_arm.h",
"scoped_logger.cc",
"scoped_logger.h",
"size_assertions.h",
@ -309,7 +307,6 @@ jumbo_source_set("wtf_unittests_sources") {
"pod_red_black_tree_test.cc",
"pod_tree_test_helpers.cc",
"pod_tree_test_helpers.h",
"saturated_arithmetic_test.cc",
"scoped_logger_test.cc",
"string_hasher_test.cc",
"testing/run_all_tests.cc",

@ -1,97 +0,0 @@
/*
* Copyright (c) 2012, Google 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:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "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 THE COPYRIGHT
* OWNER 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_SATURATED_ARITHMETIC_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_SATURATED_ARITHMETIC_H_
#include "base/compiler_specific.h"
#include "build/build_config.h"
#include <stdint.h>
#include <limits>
#if defined(ARCH_CPU_ARM_FAMILY) && defined(ARCH_CPU_32_BITS) && \
defined(COMPILER_GCC) && !defined(OS_NACL) && __OPTIMIZE__
// If we're building ARM 32-bit on GCC we replace the C++ versions with some
// native ARM assembly for speed.
#include "third_party/blink/renderer/platform/wtf/saturated_arithmetic_arm.h"
#else
namespace WTF {
ALWAYS_INLINE int GetMaxSaturatedSetResultForTesting(int fractional_shift) {
// For C version the set function maxes out to max int, this differs from
// the ARM asm version, see saturated_arithmetic_arm.h for the equivalent asm
// version.
return std::numeric_limits<int>::max();
}
ALWAYS_INLINE int GetMinSaturatedSetResultForTesting(int fractional_shift) {
return std::numeric_limits<int>::min();
}
template <int fractional_shift>
ALWAYS_INLINE int SaturatedSet(int value) {
const int kIntMaxForLayoutUnit =
std::numeric_limits<int>::max() >> fractional_shift;
const int kIntMinForLayoutUnit =
std::numeric_limits<int>::min() >> fractional_shift;
if (value > kIntMaxForLayoutUnit)
return std::numeric_limits<int>::max();
if (value < kIntMinForLayoutUnit)
return std::numeric_limits<int>::min();
return static_cast<unsigned>(value) << fractional_shift;
}
template <int fractional_shift>
ALWAYS_INLINE int SaturatedSet(unsigned value) {
const unsigned kIntMaxForLayoutUnit =
std::numeric_limits<int>::max() >> fractional_shift;
if (value >= kIntMaxForLayoutUnit)
return std::numeric_limits<int>::max();
return value << fractional_shift;
}
} // namespace WTF.
#endif // CPU(ARM) && COMPILER(GCC)
using WTF::SaturatedSet;
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_SATURATED_ARITHMETIC_H_

@ -1,78 +0,0 @@
// Copyright 2016 The Chromium Authors. All rights reserved.
// 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_WTF_SATURATED_ARITHMETIC_ARM_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_SATURATED_ARITHMETIC_ARM_H_
#include <limits>
namespace WTF {
inline int GetMaxSaturatedSetResultForTesting(int fractional_shift) {
// For ARM Asm version the set function maxes out to the biggest
// possible integer part with the fractional part zero'd out.
// e.g. 0x7fffffc0.
return std::numeric_limits<int>::max() & ~((1 << fractional_shift) - 1);
}
inline int GetMinSaturatedSetResultForTesting(int fractional_shift) {
return std::numeric_limits<int>::min();
}
template <int fractional_shift>
inline int SaturatedSet(int value) {
// Figure out how many bits are left for storing the integer part of
// the fixed point number, and saturate our input to that
enum { Saturate = 32 - fractional_shift };
int result;
// The following ARM code will Saturate the passed value to the number of
// bits used for the whole part of the fixed point representation, then
// shift it up into place. This will result in the low <FractionShift> bits
// all being 0's. When the value saturates this gives a different result
// to from the C++ case; in the C++ code a saturated value has all the low
// bits set to 1 (for a +ve number at least). This cannot be done rapidly
// in ARM ... we live with the difference, for the sake of speed.
asm("ssat %[output],%[saturate],%[value]\n\t"
"lsl %[output],%[shift]"
: [output] "=r"(result)
: [value] "r"(value), [saturate] "n"(Saturate),
[shift] "n"(fractional_shift));
return result;
}
template <int fractional_shift>
inline int SaturatedSet(unsigned value) {
// Here we are being passed an unsigned value to saturate,
// even though the result is returned as a signed integer. The ARM
// instruction for unsigned saturation therefore needs to be given one
// less bit (i.e. the sign bit) for the saturation to work correctly; hence
// the '31' below.
enum { Saturate = 31 - fractional_shift };
// The following ARM code will Saturate the passed value to the number of
// bits used for the whole part of the fixed point representation, then
// shift it up into place. This will result in the low <FractionShift> bits
// all being 0's. When the value saturates this gives a different result
// to from the C++ case; in the C++ code a saturated value has all the low
// bits set to 1. This cannot be done rapidly in ARM, so we live with the
// difference, for the sake of speed.
int result;
asm("usat %[output],%[saturate],%[value]\n\t"
"lsl %[output],%[shift]"
: [output] "=r"(result)
: [value] "r"(value), [saturate] "n"(Saturate),
[shift] "n"(fractional_shift));
return result;
}
} // namespace WTF
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_WTF_SATURATED_ARITHMETIC_ARM_H_

@ -1,69 +0,0 @@
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <limits>
#include "base/logging.h"
#include "base/macros.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/wtf/saturated_arithmetic.h"
namespace WTF {
TEST(SaturatedArithmeticTest, SetSigned) {
int int_max = std::numeric_limits<int>::max();
int int_min = std::numeric_limits<int>::min();
const int kFractionBits = 6;
const int kIntMaxForLayoutUnit = int_max >> kFractionBits;
const int kIntMinForLayoutUnit = int_min >> kFractionBits;
EXPECT_EQ(0, SaturatedSet<kFractionBits>(0));
// Internally the max number we can represent (without saturating)
// is all the (non-sign) bits set except for the bottom n fraction bits
const int max_internal_representation = int_max ^ ((1 << kFractionBits) - 1);
EXPECT_EQ(max_internal_representation,
SaturatedSet<kFractionBits>(kIntMaxForLayoutUnit));
EXPECT_EQ(GetMaxSaturatedSetResultForTesting(kFractionBits),
SaturatedSet<kFractionBits>(kIntMaxForLayoutUnit + 100));
EXPECT_EQ((kIntMaxForLayoutUnit - 100) << kFractionBits,
SaturatedSet<kFractionBits>(kIntMaxForLayoutUnit - 100));
EXPECT_EQ(GetMinSaturatedSetResultForTesting(kFractionBits),
SaturatedSet<kFractionBits>(kIntMinForLayoutUnit));
EXPECT_EQ(GetMinSaturatedSetResultForTesting(kFractionBits),
SaturatedSet<kFractionBits>(kIntMinForLayoutUnit - 100));
// Shifting negative numbers left has undefined behavior, so use
// multiplication instead of direct shifting here.
EXPECT_EQ((kIntMinForLayoutUnit + 100) * (1 << kFractionBits),
SaturatedSet<kFractionBits>(kIntMinForLayoutUnit + 100));
}
TEST(SaturatedArithmeticTest, SetUnsigned) {
int int_max = std::numeric_limits<int>::max();
const int kFractionBits = 6;
const int kIntMaxForLayoutUnit = int_max >> kFractionBits;
EXPECT_EQ(0, SaturatedSet<kFractionBits>((unsigned)0));
EXPECT_EQ(GetMaxSaturatedSetResultForTesting(kFractionBits),
SaturatedSet<kFractionBits>((unsigned)kIntMaxForLayoutUnit));
const unsigned kOverflowed = kIntMaxForLayoutUnit + 100;
EXPECT_EQ(GetMaxSaturatedSetResultForTesting(kFractionBits),
SaturatedSet<kFractionBits>(kOverflowed));
const unsigned kNotOverflowed = kIntMaxForLayoutUnit - 100;
EXPECT_EQ((kIntMaxForLayoutUnit - 100) << kFractionBits,
SaturatedSet<kFractionBits>(kNotOverflowed));
}
} // namespace WTF

@ -24,7 +24,6 @@ fun:*v8*internal*GlobalHandles*Node*PostGarbageCollectionProcessing*
#############################################################################
# Undefined arithmetic that can be safely ignored.
src:*/base/numerics/saturated_arithmetic.h
src:*/ppapi/shared_impl/id_assignment.h
#############################################################################

@ -39,7 +39,6 @@ src:*/usr/*
#############################################################################
# Undefined arithmetic that can be safely ignored.
src:*/base/numerics/saturated_arithmetic.h
src:*/ppapi/shared_impl/id_assignment.h
#############################################################################