about summary refs log tree commit diff
path: root/src/libstd/sys/unix/stack_overflow.rs
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-09-10 17:19:19 +0200
committerGitHub <noreply@github.com>2019-09-10 17:19:19 +0200
commit13726cc444e423dad058860a74e559eca88a9518 (patch)
tree965b34c8332d40db28e4c6994db86af38b4fe2a0 /src/libstd/sys/unix/stack_overflow.rs
parent403c0def905c58f08e5a1ce48a430522df01abf1 (diff)
parentadee559659774054497fc36afea0076c334c0bb2 (diff)
downloadrust-13726cc444e423dad058860a74e559eca88a9518.tar.gz
rust-13726cc444e423dad058860a74e559eca88a9518.zip
Rollup merge of #63786 - tspiteri:const-abs, r=alexcrichton
Make `abs`, `wrapping_abs`, `overflowing_abs` const functions

This makes `abs`, `wrapping_abs` and `overflowing_abs` const functions like #58044 makes `wrapping_neg` and `overflowing_neg` const functions.

`abs` is made const by returning `(self ^ -1) - -1` = `!self + 1` = `-self` for negative numbers and `(self ^ 0) - 0` = `self` for non-negative numbers. The subexpression `self >> ($BITS - 1)` evaluates to `-1` for negative numbers and `0` otherwise. The subtraction overflows when `self` is `min_value()`, as we would be subtracting `max_value() - -1`; this is when `abs` should overflow.

`wrapping_abs` and `overflowing_abs` make use of `wrapping_sub` and `overflowing_sub` instead of the subtraction operator.
Diffstat (limited to 'src/libstd/sys/unix/stack_overflow.rs')
0 files changed, 0 insertions, 0 deletions