Skip to content

Add functions for reversing the bit pattern in an integer #32798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/libcore/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,10 @@ extern "rust-intrinsic" {
/// Reverses the bytes in an integer type `T`.
pub fn bswap<T>(x: T) -> T;

/// Reverses the bits in an integer type `T`.
#[cfg(not(stage0))]
pub fn bitreverse<T>(x: T) -> T;

/// Performs checked integer addition.
pub fn add_with_overflow<T>(x: T, y: T) -> (T, bool);

Expand Down
36 changes: 36 additions & 0 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,24 @@ macro_rules! int_impl {
(self as $UnsignedT).trailing_zeros()
}

/// Reverses the bit pattern of the integer.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let n = 0b10101000u8;
///
/// assert_eq!(n.reverse_bits(), 0b00010101u8);
/// ```
#[cfg(not(stage0))]
#[unstable(feature = "reverse_bits", issue = "0", reason = "recently added")]
#[inline]
pub fn reverse_bits(self) -> Self {
(self as $UnsignedT).reverse_bits() as Self
}

/// Shifts the bits to the left by a specified amount, `n`,
/// wrapping the truncated bits to the end of the resulting integer.
///
Expand Down Expand Up @@ -1290,6 +1308,24 @@ macro_rules! uint_impl {
}
}

/// Reverses the bit pattern of the integer.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let n = 0b10101000u8;
///
/// assert_eq!(n.reverse_bits(), 0b00010101u8);
/// ```
#[cfg(not(stage0))]
#[unstable(feature = "reverse_bits", issue = "0", reason = "recently added")]
#[inline]
pub fn reverse_bits(self) -> Self {
unsafe { intrinsics::bitreverse(self) }
}

/// Shifts the bits to the left by a specified amount, `n`,
/// wrapping the truncated bits to the end of the resulting integer.
///
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_trans/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,11 @@ fn declare_intrinsic(ccx: &CrateContext, key: &str) -> Option<ValueRef> {
ifn!("llvm.bswap.i32", fn(t_i32) -> t_i32);
ifn!("llvm.bswap.i64", fn(t_i64) -> t_i64);

ifn!("llvm.bitreverse.i8", fn(t_i8) -> t_i8);
ifn!("llvm.bitreverse.i16", fn(t_i16) -> t_i16);
ifn!("llvm.bitreverse.i32", fn(t_i32) -> t_i32);
ifn!("llvm.bitreverse.i64", fn(t_i64) -> t_i64);

ifn!("llvm.sadd.with.overflow.i8", fn(t_i8, t_i8) -> mk_struct!{t_i8, i1});
ifn!("llvm.sadd.with.overflow.i16", fn(t_i16, t_i16) -> mk_struct!{t_i16, i1});
ifn!("llvm.sadd.with.overflow.i32", fn(t_i32, t_i32) -> mk_struct!{t_i32, i1});
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_trans/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
C_nil(ccx)
},

(_, "ctlz") | (_, "cttz") | (_, "ctpop") | (_, "bswap") |
(_, "ctlz") | (_, "cttz") | (_, "ctpop") | (_, "bswap") | (_, "bitreverse") |
(_, "add_with_overflow") | (_, "sub_with_overflow") | (_, "mul_with_overflow") |
(_, "overflowing_add") | (_, "overflowing_sub") | (_, "overflowing_mul") |
(_, "unchecked_div") | (_, "unchecked_rem") => {
Expand All @@ -629,6 +629,9 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
&llargs, call_debug_location)
}
}
"bitreverse" => Call(bcx, ccx.get_intrinsic(&format!("llvm.bitreverse.i{}",
width)),
&llargs, call_debug_location),
"add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" => {
let intrinsic = format!("llvm.{}{}.with.overflow.i{}",
if signed { 's' } else { 'u' },
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_typeck/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ pub fn check_intrinsic_type(ccx: &CrateCtxt, it: &hir::ForeignItem) {
"volatile_store" =>
(1, vec!( tcx.mk_mut_ptr(param(ccx, 0)), param(ccx, 0) ), tcx.mk_nil()),

"ctpop" | "ctlz" | "cttz" | "bswap" => (1, vec!(param(ccx, 0)), param(ccx, 0)),
"ctpop" | "ctlz" | "cttz" | "bswap" | "bitreverse" =>
(1, vec!(param(ccx, 0)), param(ccx, 0)),

"add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" =>
(1, vec!(param(ccx, 0), param(ccx, 0)),
Expand Down
10 changes: 10 additions & 0 deletions src/test/run-pass/intrinsics-integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod rusti {
pub fn ctlz<T>(x: T) -> T;
pub fn cttz<T>(x: T) -> T;
pub fn bswap<T>(x: T) -> T;
pub fn bitreverse<T>(x: T) -> T;
}
}

Expand Down Expand Up @@ -101,5 +102,14 @@ pub fn main() {
assert_eq!(bswap(0x0ABBCC0Di32), 0x0DCCBB0A);
assert_eq!(bswap(0x0122334455667708u64), 0x0877665544332201);
assert_eq!(bswap(0x0122334455667708i64), 0x0877665544332201);

assert_eq!(bitreverse(0x0Au8), 0x50);
assert_eq!(bitreverse(0x0Ai8), 0x50);
assert_eq!(bitreverse(0x0A0Cu16), 0x3050);
assert_eq!(bitreverse(0x0A0Ci16), 0x3050);
assert_eq!(bitreverse(0x0ABBCC0Eu32), 0x7033DD50);
assert_eq!(bitreverse(0x0ABBCC0Ei32), 0x7033DD50);
assert_eq!(bitreverse(0x0122334455667708u64), 0x10EE66AA22CC4480);
assert_eq!(bitreverse(0x0122334455667708i64), 0x10EE66AA22CC4480);
}
}