Skip to content

Commit

Permalink
const-stabilize const_int_ops + reverse_bits
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Dec 31, 2018
1 parent 50152d2 commit 2760f87
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 143 deletions.
8 changes: 4 additions & 4 deletions src/libcore/intrinsics.rs
Expand Up @@ -1343,7 +1343,7 @@ extern "rust-intrinsic" {
/// use std::intrinsics::ctlz;
///
/// let x = 0b0001_1100_u8;
/// let num_leading = unsafe { ctlz(x) };
/// let num_leading = ctlz(x);
/// assert_eq!(num_leading, 3);
/// ```
///
Expand All @@ -1355,7 +1355,7 @@ extern "rust-intrinsic" {
/// use std::intrinsics::ctlz;
///
/// let x = 0u16;
/// let num_leading = unsafe { ctlz(x) };
/// let num_leading = ctlz(x);
/// assert_eq!(num_leading, 16);
/// ```
pub fn ctlz<T>(x: T) -> T;
Expand Down Expand Up @@ -1386,7 +1386,7 @@ extern "rust-intrinsic" {
/// use std::intrinsics::cttz;
///
/// let x = 0b0011_1000_u8;
/// let num_trailing = unsafe { cttz(x) };
/// let num_trailing = cttz(x);
/// assert_eq!(num_trailing, 3);
/// ```
///
Expand All @@ -1398,7 +1398,7 @@ extern "rust-intrinsic" {
/// use std::intrinsics::cttz;
///
/// let x = 0u16;
/// let num_trailing = unsafe { cttz(x) };
/// let num_trailing = cttz(x);
/// assert_eq!(num_trailing, 16);
/// ```
pub fn cttz<T>(x: T) -> T;
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/lib.rs
Expand Up @@ -71,7 +71,7 @@
#![feature(cfg_target_has_atomic)]
#![feature(concat_idents)]
#![feature(const_fn)]
#![feature(const_int_ops)]
#![cfg_attr(stage0, feature(const_int_ops))]
#![feature(const_fn_union)]
#![feature(custom_attribute)]
#![feature(doc_cfg)]
Expand Down
57 changes: 36 additions & 21 deletions src/libcore/num/mod.rs
Expand Up @@ -275,7 +275,7 @@ $EndFeature, "
```
"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_ops")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
#[inline]
pub const fn count_ones(self) -> u32 { (self as $UnsignedT).count_ones() }
}
Expand All @@ -291,7 +291,7 @@ Basic usage:
", $Feature, "assert_eq!(", stringify!($SelfT), "::max_value().count_zeros(), 1);", $EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_ops")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
#[inline]
pub const fn count_zeros(self) -> u32 {
(!self).count_ones()
Expand All @@ -312,7 +312,7 @@ assert_eq!(n.leading_zeros(), 0);",
$EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_ops")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
#[inline]
pub const fn leading_zeros(self) -> u32 {
(self as $UnsignedT).leading_zeros()
Expand All @@ -333,7 +333,7 @@ assert_eq!(n.trailing_zeros(), 2);",
$EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_ops")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
#[inline]
pub const fn trailing_zeros(self) -> u32 {
(self as $UnsignedT).trailing_zeros()
Expand Down Expand Up @@ -404,7 +404,7 @@ let m = n.swap_bytes();
assert_eq!(m, ", $swapped, ");
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_ops")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
#[inline]
pub const fn swap_bytes(self) -> Self {
(self as $UnsignedT).swap_bytes() as Self
Expand Down Expand Up @@ -454,7 +454,7 @@ if cfg!(target_endian = \"big\") {
$EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_ops")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
#[inline]
pub const fn from_be(x: Self) -> Self {
#[cfg(target_endian = "big")]
Expand Down Expand Up @@ -488,7 +488,7 @@ if cfg!(target_endian = \"little\") {
$EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_ops")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
#[inline]
pub const fn from_le(x: Self) -> Self {
#[cfg(target_endian = "little")]
Expand Down Expand Up @@ -522,7 +522,7 @@ if cfg!(target_endian = \"big\") {
$EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_ops")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
#[inline]
pub const fn to_be(self) -> Self { // or not to be?
#[cfg(target_endian = "big")]
Expand Down Expand Up @@ -556,7 +556,7 @@ if cfg!(target_endian = \"little\") {
$EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_ops")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
#[inline]
pub const fn to_le(self) -> Self {
#[cfg(target_endian = "little")]
Expand Down Expand Up @@ -2234,10 +2234,13 @@ Basic usage:
assert_eq!(n.count_ones(), 3);", $EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_ops")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
#[inline]
pub const fn count_ones(self) -> u32 {
#[cfg(stage0)]
unsafe { intrinsics::ctpop(self as $ActualT) as u32 }
#[cfg(not(stage0))]
{ intrinsics::ctpop(self as $ActualT) as u32 }
}
}

Expand All @@ -2252,7 +2255,7 @@ Basic usage:
", $Feature, "assert_eq!(", stringify!($SelfT), "::max_value().count_zeros(), 0);", $EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_ops")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
#[inline]
pub const fn count_zeros(self) -> u32 {
(!self).count_ones()
Expand All @@ -2272,10 +2275,13 @@ Basic usage:
assert_eq!(n.leading_zeros(), 2);", $EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_ops")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
#[inline]
pub const fn leading_zeros(self) -> u32 {
#[cfg(stage0)]
unsafe { intrinsics::ctlz(self as $ActualT) as u32 }
#[cfg(not(stage0))]
{ intrinsics::ctlz(self as $ActualT) as u32 }
}
}

Expand All @@ -2293,10 +2299,13 @@ Basic usage:
assert_eq!(n.trailing_zeros(), 3);", $EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_ops")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
#[inline]
pub const fn trailing_zeros(self) -> u32 {
#[cfg(stage0)]
unsafe { intrinsics::cttz(self) as u32 }
#[cfg(not(stage0))]
{ intrinsics::cttz(self) as u32 }
}
}

Expand Down Expand Up @@ -2370,10 +2379,13 @@ let m = n.swap_bytes();
assert_eq!(m, ", $swapped, ");
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_ops")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
#[inline]
pub const fn swap_bytes(self) -> Self {
#[cfg(stage0)]
unsafe { intrinsics::bswap(self as $ActualT) as Self }
#[cfg(not(stage0))]
{ intrinsics::bswap(self as $ActualT) as Self }
}
}

Expand All @@ -2393,10 +2405,13 @@ let m = n.reverse_bits();
assert_eq!(m, ", $reversed, ");
```"),
#[unstable(feature = "reverse_bits", issue = "48763")]
#[rustc_const_unstable(feature = "const_int_conversion")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_conversion"))]
#[inline]
pub const fn reverse_bits(self) -> Self {
#[cfg(stage0)]
unsafe { intrinsics::bitreverse(self as $ActualT) as Self }
#[cfg(not(stage0))]
{ intrinsics::bitreverse(self as $ActualT) as Self }
}
}

Expand All @@ -2420,7 +2435,7 @@ if cfg!(target_endian = \"big\") {
}", $EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_ops")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
#[inline]
pub const fn from_be(x: Self) -> Self {
#[cfg(target_endian = "big")]
Expand Down Expand Up @@ -2454,7 +2469,7 @@ if cfg!(target_endian = \"little\") {
}", $EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_ops")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
#[inline]
pub const fn from_le(x: Self) -> Self {
#[cfg(target_endian = "little")]
Expand Down Expand Up @@ -2488,7 +2503,7 @@ if cfg!(target_endian = \"big\") {
}", $EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_ops")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
#[inline]
pub const fn to_be(self) -> Self { // or not to be?
#[cfg(target_endian = "big")]
Expand Down Expand Up @@ -2522,7 +2537,7 @@ if cfg!(target_endian = \"little\") {
}", $EndFeature, "
```"),
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_int_ops")]
#[cfg_attr(stage0, rustc_const_unstable(feature = "const_int_ops"))]
#[inline]
pub const fn to_le(self) -> Self {
#[cfg(target_endian = "little")]
Expand Down Expand Up @@ -2957,8 +2972,8 @@ $EndFeature, "
unsafe {
intrinsics::overflowing_mul(self, rhs)
}
#[cfg(not(stage0))]
intrinsics::overflowing_mul(self, rhs)
#[cfg(not(stage0))]
intrinsics::overflowing_mul(self, rhs)
}

doc_comment! {
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_mir/transform/qualify_min_const_fn.rs
Expand Up @@ -404,6 +404,11 @@ fn is_intrinsic_whitelisted(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> bool
| "unchecked_shr" // ~> .wrapping_shr
| "rotate_left" // ~> .rotate_left
| "rotate_right" // ~> .rotate_right
| "ctpop" // ~> .count_ones
| "ctlz" // ~> .leading_zeros
| "cttz" // ~> .trailing_zeros
| "bswap" // ~> .swap_bytes
| "bitreverse" // ~> .reverse_bits
=> true,
_ => false,
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_typeck/check/intrinsic.rs
Expand Up @@ -71,7 +71,8 @@ pub fn intrisic_operation_unsafety(intrinsic: &str) -> hir::Unsafety {
match intrinsic {
"size_of" | "min_align_of" | "needs_drop" |
"overflowing_add" | "overflowing_sub" | "overflowing_mul" |
"rotate_left" | "rotate_right"
"rotate_left" | "rotate_right" |
"ctpop" | "ctlz" | "cttz" | "bswap" | "bitreverse"
=> hir::Unsafety::Normal,
_ => hir::Unsafety::Unsafe,
}
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/lib.rs
Expand Up @@ -242,7 +242,7 @@
#![feature(char_error_internals)]
#![feature(compiler_builtins_lib)]
#![feature(concat_idents)]
#![feature(const_int_ops)]
#![cfg_attr(stage0, feature(const_int_ops))]
#![feature(const_ip)]
#![feature(const_raw_ptr_deref)]
#![feature(const_cstr_unchecked)]
Expand Down
3 changes: 1 addition & 2 deletions src/test/run-pass/const-int-conversion.rs
@@ -1,4 +1,4 @@
#![feature(const_int_conversion, const_int_ops, reverse_bits)]
#![feature(const_int_conversion, reverse_bits)]

const REVERSE: u32 = 0x12345678_u32.reverse_bits();
const FROM_BE_BYTES: i32 = i32::from_be_bytes([0x12, 0x34, 0x56, 0x78]);
Expand All @@ -21,4 +21,3 @@ fn main() {
assert_eq!(TO_LE_BYTES, ident([0x78, 0x56, 0x34, 0x12]));
assert_eq!(TO_NE_BYTES, ident([0x80, 0, 0, 0]));
}

2 changes: 0 additions & 2 deletions src/test/run-pass/consts/const-endianess.rs
@@ -1,5 +1,4 @@
// run-pass
#![feature(const_int_ops)]
#![feature(test)]

extern crate test;
Expand All @@ -8,7 +7,6 @@ use test::black_box as b;
const BE_U32: u32 = 55u32.to_be();
const LE_U32: u32 = 55u32.to_le();


fn main() {
assert_eq!(BE_U32, b(55u32).to_be());
assert_eq!(LE_U32, b(55u32).to_le());
Expand Down
6 changes: 3 additions & 3 deletions src/test/run-pass/ctfe/bswap-const.rs
Expand Up @@ -4,9 +4,9 @@

use std::intrinsics;

const SWAPPED_U8: u8 = unsafe { intrinsics::bswap(0x12_u8) };
const SWAPPED_U16: u16 = unsafe { intrinsics::bswap(0x12_34_u16) };
const SWAPPED_I32: i32 = unsafe { intrinsics::bswap(0x12_34_56_78_i32) };
const SWAPPED_U8: u8 = intrinsics::bswap(0x12_u8);
const SWAPPED_U16: u16 = intrinsics::bswap(0x12_34_u16);
const SWAPPED_I32: i32 = intrinsics::bswap(0x12_34_56_78_i32);

fn main() {
assert_eq!(SWAPPED_U8, 0x12);
Expand Down

0 comments on commit 2760f87

Please sign in to comment.