SparseHistogramTest.AddCount_LargeCountsDontOverflow tests some overflow
cases in the histogram logic, seemingly with the intent of checking that
one of the APIs does not overflow when the other does.
But by using signed overflow, we hit undefined behavior when the other
API overflows, so the compiler is allowed to arbitrarily mangle our code
(though unlikely). Add wrapping functions to base/numerics and use
those instead.
Wrapping seems not ideal of behavior either, but given the test is
intentionally overflowing and didn't try to use, e.g., a saturating
operation, I'm assuming that wraparound is what this code wants.
Bug: 1394755
Change-Id: I5845fb4bd4a20f4aa2d922959148eef52bf6d689
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4968320
Commit-Queue: David Benjamin <davidben@chromium.org>
Reviewed-by: Alexei Svitkine <asvitkine@chromium.org>
Reviewed-by: danakj <danakj@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1214311}
Patch 6/x:
* std::tuple_size<>
* Multiline replacements :)
std::tuple_size<> in base/test/test_future_internal.h has to stay as is
because tuple_size<>::value within enable_if_t should be a part of the
immediate context (while tuple_size_v is not and hence fails with non-
tuple types).
Change-Id: If9e21faaad4fdc241afa272f0eb98dd854608ee3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4954858
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Commit-Queue: Andrew Rayskiy <greengrape@google.com>
Cr-Commit-Position: refs/heads/main@{#1212334}
Calling standard library functions is safer, and frequently faster,
than home-grown rounding code. This also fixes a bug where certain
values very close to 0.5 would be rounded the wrong way.
Also document more clearly what the rounding behavior of these
functions are.
Fixed: 1486546
Change-Id: Id8609444d3defc91dd091d8c31a1d0c5ec11463f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4890319
Reviewed-by: Peter Kasting <pkasting@chromium.org>
Commit-Queue: Steinar H Gunderson <sesse@chromium.org>
Reviewed-by: danakj <danakj@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1202931}
It can be confusing to reason about the behavior of the
templated safe math operations, when differently typed
parameters are supplied to each argument.
This CL adds some worked examples as a test for CheckMul to
illustrate the behavior of all the different variants.
BUG=332611
Change-Id: If69e61ef2fdd8957105d96853e19a0b44c67008a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4226303
Reviewed-by: Gabriel Charette <gab@chromium.org>
Commit-Queue: Will Harris <wfh@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1102455}
Fixes some outstanding constexpr and type issues in base/numerics. This
also incorporates the __builtin_is_constant_evaluated() intrinsic to
allow switching between compile- and run- time implementations.
The impact should be improved performance by allowing more compile-time
evaluation and replacing some run-time operations with more optimized
versions where possible (e.g. Arm 32-bit integer saturation).
Bug: 866538
Change-Id: If0ba9a4bcc8bcb1977b6388f8ae96444606d0042
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3382275
Reviewed-by: Peter Kasting <pkasting@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Justin Schuh <jschuh@chromium.org>
Cr-Commit-Position: refs/heads/main@{#958889}
Also fix more overflow cases so these new tests pass.
Also add a bit of coverage in safe_numerics that looked omitted. I
think I originally found this while implementing some of the TimeDelta
stuff another way but it's been several weeks and I can't recall :/
Bug: none
Change-Id: I53beffb241f5f2e122ad777f576ad9f93dc62af8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2344816
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Yuri Wiitala <miu@chromium.org>
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#797049}
Based on thakis' suggestion that the names shouldn't mirror std:: when
the functions didn't behave the same, and some feedback on #cxx that
"saturated" is less obvious than "clamped".
Bug: none
Change-Id: Id0d9651b9fcc13f1acb663d56f357c7eab1c994e
AX-Relnotes: n/a.
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2306750
Reviewed-by: Nico Weber <thakis@chromium.org>
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#789831}
Some of these tests intentionally cause overflow, and it seems that clang
ToT is now able to recognise and warn about this. Let's disable these
warnings as we do for MSVC.
Bug: 826656
Change-Id: I7a455e4d5b901c2638154da9d8d7ef99531286b0
Reviewed-on: https://chromium-review.googlesource.com/984876
Commit-Queue: Mostyn Bramley-Moore <mostynb@vewd.com>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#546598}
Convert the remaining CheckedNumeric and ClampedNumeric methods to be
fully C++14 constexpr compliant. This also includes the following
dependent changes:
* The constexpr allowed some compile-time optimizations in ClampedAddOp
and ClampedSubOp, which required restructuring the overflow handling
to get correct saturation semantics. (New tests cover this.)
* The Clamped*FastAsmOp implementations included redundant saturation
code, which was removed.
Bug: 612995
Change-Id: I0e0397968712ee06f8beff2f74c638cab01f34bb
Reviewed-on: https://chromium-review.googlesource.com/603127
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Commit-Queue: Justin Schuh <jschuh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#505494}
* Applied BASE_NUMERICS_LIKELY to all predictable clamped and checked
branches. (I skipped things like abs, where the branch is not
reliably predictable).
* Simplified CheckedAddOp and CheckedSubOp, and optimized the generic
code for smaller types (although Clang will always use the builtins).
* Expanded the cases where CheckedMulOp will use the Clang builtin
(this branch is compile-time determined, and should be an
improvement on 64-bit).
* Restructured CheckedDivOp to allow the compiler remove more of the
overflow checking based on compile-time type checks.
* Simplified CheckedModOp by putting all the logic in one function.
* Simplified branching and eliminated multiplication in CheckedLshOp
and ClampedLshOp (much more compact code on Arm, but still
unavoidably a bit branchy).
* Removed branch in CheckMathOp (variadic template wrapper).
Note: Arm code should remain mostly non-branching, unless Clang's
optimizer behavior changes in the future to assume better branch
prediction (at which point it should branch on the exceptional
conditions, like Intel currently does).
Bug: 750184
Change-Id: I9948b94722d64e9a5bf363690acf9086dae0c65d
Reviewed-on: https://chromium-review.googlesource.com/592852
Commit-Queue: Justin Schuh <jschuh@chromium.org>
Reviewed-by: Emil A Eklund <eae@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491725}
This is a small performance and code size improvement for the general
case of integer range checks and saturated conversions.
NOTRY=True
Bug: 750184
Change-Id: Ibef8419ec53ecb88a23c81b64d11421c9f74b724
Reviewed-on: https://chromium-review.googlesource.com/591052
Commit-Queue: Justin Schuh <jschuh@chromium.org>
Reviewed-by: Emil A Eklund <eae@chromium.org>
Cr-Commit-Position: refs/heads/master@{#490852}
These headers are commonly included in third-party code, so we want to
prevent the accidental introduction of any third-party dependencies.
We also want to prevent the internal implementation details for these
headers from leaking into other files.
Change-Id: I6cffd7866cc9ec2b8a9b13f196ce85a1a5cab304
Reviewed-on: https://chromium-review.googlesource.com/582237
Commit-Queue: Justin Schuh <jschuh@chromium.org>
Reviewed-by: Brett Wilson <brettw@chromium.org>
Reviewed-by: Erik Chen <erikchen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#489367}
* Refactor code in preparation for safe math operations.
* Support float type for numeric casts.
* Expose IsValueInRangeForNumericType as part of the API.
* Add saturated_cast.
BUG=332611
R=akalin@chromium.org, willchan@chromium.org
Review URL: https://codereview.chromium.org/131063002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@245183 0039d316-1c4b-4281-b951-d872f2087c98
In work on bringing up Windows x64, there are many places that need
to be safely narrowed to the types used for interacting with other
APIs (particularly when using containers). Rather than scatter these
CHECKs all over, numeric_cast<> CHECKs that the runtime value can be
safely converted to the target type.
BUG=8606, 167187, 166496
Review URL: https://codereview.chromium.org/11886037
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@177264 0039d316-1c4b-4281-b951-d872f2087c98