0
Commit Graph

29 Commits

Author SHA1 Message Date
David Benjamin
954152b502 Avoid UB signed integer overflow in histogram logic
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}
2023-10-24 17:46:20 +00:00
Andrew Rayskiy
26072e1724 [CodeHealth] Replace typename enable_if<>::type -> enable_if_t in base/
Bug: None
Change-Id: I70fa74728030d060b96e13fa139b541c5776b26f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4950123
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Commit-Queue: Andrew Rayskiy <greengrape@google.com>
Cr-Commit-Position: refs/heads/main@{#1212348}
2023-10-19 20:08:09 +00:00
Andrew Rayskiy
6f02136671 [CodeHealth] Replace std::is_...<>::value -> std::is_..._v<> in base/
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}
2023-10-19 19:55:06 +00:00
Andrew Rayskiy
62991238da [CodeHealth] Replace std::is_...<>::value -> std::is_..._v<> in base/
Patch 4/x:
  * ./
  * trace_event/
  * types/

Automatically processed with perl on selected files:

perl -pi -e 's/std::is_(\w+?)<(.*?)>::value\b/std::is_\1_v<\2>/g'

Bug: None
Change-Id: I4bc6bb5cfc9cc942ec21b10abeadd55c262f7cd5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4942361
Commit-Queue: Andrew Rayskiy <greengrape@google.com>
Reviewed-by: Francois Pierre Doray <fdoray@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1211811}
2023-10-18 22:58:42 +00:00
Steinar H. Gunderson
32b29d3501 Implement ClampRound() using std::round().
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}
2023-09-28 23:10:49 +00:00
Will Harris
128da00a8c Add some worked examples for safe math to base unit tests.
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}
2023-02-07 23:47:49 +00:00
Avi Drissman
e4622aaecc Update copyright headers in base/
The methodology used to generate this CL is documented in
https://crbug.com/1098010#c34.

An earlier version of this CL, https://crrev.com/c/3879904,
was reverted due to an issue that was resolved with
https://crrev.com/c/3881211.

No-Try: true
Bug: 1098010
Change-Id: Ibd6ffb97e66835bc299fe7b85876c3e2927b2345
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3883841
Auto-Submit: Avi Drissman <avi@chromium.org>
Owners-Override: Avi Drissman <avi@chromium.org>
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Mark Mentovai <mark@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1044747}
2022-09-08 20:36:06 +00:00
Daniel Cheng
3d1108defc Revert "Update copyright headers in base/"
This reverts commit eccb7e8107.

Reason for revert: https://crbug.com/1361092

Original change's description:
> Update copyright headers in base/
>
> The methodology used to generate this CL is documented in
> https://crbug.com/1098010#c21.
>
> No-Try: true
> Bug: 1098010
> Change-Id: Icd01cc1648eab4b0550afcff2bee17076359a132
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3879904
> Owners-Override: Avi Drissman <avi@chromium.org>
> Commit-Queue: Avi Drissman <avi@chromium.org>
> Auto-Submit: Avi Drissman <avi@chromium.org>
> Reviewed-by: Mark Mentovai <mark@chromium.org>
> Cr-Commit-Position: refs/heads/main@{#1044191}

Bug: 1098010
Change-Id: Iefdde85d3d44fabe09906cd725b39a4e0b359491
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3880811
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Owners-Override: Daniel Cheng <dcheng@chromium.org>
Commit-Queue: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1044252}
2022-09-07 23:42:04 +00:00
Avi Drissman
eccb7e8107 Update copyright headers in base/
The methodology used to generate this CL is documented in
https://crbug.com/1098010#c21.

No-Try: true
Bug: 1098010
Change-Id: Icd01cc1648eab4b0550afcff2bee17076359a132
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3879904
Owners-Override: Avi Drissman <avi@chromium.org>
Commit-Queue: Avi Drissman <avi@chromium.org>
Auto-Submit: Avi Drissman <avi@chromium.org>
Reviewed-by: Mark Mentovai <mark@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1044191}
2022-09-07 21:26:21 +00:00
Daniel Cheng
ef81720d6f Fix implicit deduction guides for CheckedNumeric and ClampedNumeric.
This removes the need for the explicit deduction guide and
coincidentally allows //base/numerics to continue working with C++14 for
now.

Change-Id: I7f640013c05d43b4e19c15feb9a4c9648f3c3fb4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3859235
Commit-Queue: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1040096}
2022-08-27 09:01:15 +00:00
Xiaohan Wang
38e4ebbecc base: Use BUILDFLAG for OS checking
Use BUILDFLAG(IS_XXX) instead of defined(OS_XXX).

Generated by `os_buildflag_migration.py` (https://crrev.com/c/3311983).

R=thakis@chromium.org

Bug: 1234043
Test: No functionality change
Change-Id: If1c46447108fec5f71d26fbba2c900535d338ff2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3399769
Reviewed-by: Nico Weber <thakis@chromium.org>
Owners-Override: Nico Weber <thakis@chromium.org>
Commit-Queue: Xiaohan Wang <xhwang@chromium.org>
Cr-Commit-Position: refs/heads/main@{#960809}
2022-01-19 06:57:43 +00:00
Justin Schuh
e2eefe61b5 Make base/numerics constexpr clean
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}
2022-01-13 22:48:50 +00:00
Peter Kasting
fbb9e566a6 Fix -Wimplicit-int-float-conversion: base/
Bug: 989932
Change-Id: I125166fd18e8a824ac11b47513b8c5f992c77c12
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2989091
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Reviewed-by: Wez <wez@chromium.org>
Reviewed-by: Will Harris <wfh@chromium.org>
Commit-Queue: Will Harris <wfh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#896354}
2021-06-27 02:57:37 +00:00
Ng Zhi An
280ed26033 Fix conversion range check for floating-point to unsigned
When converting floating-point to any integer type, the fractional part
is discarded, so any value in the range (-1.0,-0.00) (exclusive) will
truncate to -0.0, and so will fit in an unsigned destination type.

See https://en.cppreference.com/w/cpp/language/implicit_conversion#Floating.E2.80.93integral_conversions.

Change-Id: Id8a5263d486cc3cd25670da27c417c66e28fccdd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2488717
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Zhi An Ng <zhin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#819633}
2020-10-22 00:10:12 +00:00
Peter Kasting
0559fb6267 Expand TimeDelta::operator%() unittests.
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}
2020-08-12 00:02:05 +00:00
Peter Kasting
24e973d5c5 Rename base::Round() to ClampRound (and similarly for Ceil, Floor).
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}
2020-07-20 06:13:09 +00:00
Peter Kasting
fc5d8f3e43 Add rounding functions to base/numerics/safe_conversions.h.
These will replace the versions in
ui/gfx/geometry/safe_integer_conversions.h.

Bug: 1088346
Change-Id: Ied8b5cbeff02fa870923976bb3ceb6fc8f98c340
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2274161
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#783797}
2020-06-30 02:38:17 +00:00
Tom Sepez
fe88d41981 Checked Numerics: Avoid exception when computing (INT_MIN % -1).
Use same logic as guards the (INT_MIN / -1) corner case.

Bug: 1086092
Change-Id: Ib1715b55cd3b1e65b869e603b74845c8052cdff5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2216808
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#772741}
2020-05-28 16:41:05 +00:00
Mostyn Bramley-Moore
2c1339ee7e disable clang integer overflow warnings for safe_numerics unittests
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}
2018-03-28 21:10:04 +00:00
Justin Schuh
3725ec3991 Make ClampedNumeric and CheckedNumeric constexpr
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}
2017-10-01 03:44:27 +00:00
Justin Schuh
4d93ee3a4e Support enum in base/numerics cast templates
This fixes checked_cast<>, saturated_cast<>, and strict_cast<> so they
properly support enum source types.

Bug: 754884
Change-Id: I759fb42d08447c99cc19bd1b913956539d37d706
Reviewed-on: https://chromium-review.googlesource.com/613685
Commit-Queue: Justin Schuh <jschuh@chromium.org>
Commit-Queue: Jeffrey Yasskin <jyasskin@chromium.org>
Reviewed-by: Jeffrey Yasskin <jyasskin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#494153}
2017-08-14 20:06:28 +00:00
Justin Schuh
4ad909a931 Improve branching in CheckedNumeric and ClampedNumeric
* 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}
2017-08-03 13:40:42 +00:00
Justin Schuh
aeed1e4da5 Fix MSVC release build after r490852
Disable integer overflow warnings in integer overflow tests.

TBR=thakis@chromium.org

Bug: 751198
Change-Id: Ia5680199a51f0ecb951744838e624f9d6f1d7573
Reviewed-on: https://chromium-review.googlesource.com/596511
Reviewed-by: Justin Schuh <jschuh@chromium.org>
Reviewed-by: Nico Weber <thakis@chromium.org>
Commit-Queue: Justin Schuh <jschuh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491085}
2017-08-01 19:46:43 +00:00
Justin Schuh
5cc2abf4c5 Improve checked_cast and saturated_cast performance
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}
2017-08-01 04:58:07 +00:00
Justin Schuh
5cc331deec Prevent dependency leaks to and from base/numerics
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}
2017-07-25 18:05:22 +00:00
jschuh@chromium.org
cb15406554 Refactor base/safe_numerics.h
* Move into base/numerics subdirectory.
* Rename files for clarity.
* Add owners.
* Rename checked_numeric_cast to checked_cast.
* Fixup callsites and include paths.

BUG=332611
R=brettw@chromium.org, jam@chromium.org

Review URL: https://codereview.chromium.org/141113003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@245418 0039d316-1c4b-4281-b951-d872f2087c98
2014-01-17 03:32:40 +00:00
jschuh@chromium.org
4efb2c30de Expand support in safe_numeric.h
* 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
2014-01-16 06:57:25 +00:00
scottmg@chromium.org
dcde75b644 fix checked_numeric_cast comments
R=wtc@chromium.org
TBR=darin@chromium.org

Review URL: https://codereview.chromium.org/12051089

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@178840 0039d316-1c4b-4281-b951-d872f2087c98
2013-01-25 16:50:00 +00:00
scottmg@chromium.org
c1c090d3f7 Add numeric_cast for checked integral narrowing casts
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
2013-01-16 23:34:04 +00:00