Skip to content

Commit 3513a12

Browse files
authored
Merge pull request #49 from jethrogb/patch-1
Optimize black_box
2 parents fff892c + d72deb6 commit 3513a12

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,12 @@ impl Not for Choice {
142142
///
143143
/// Uses inline asm when available, otherwise it's a no-op.
144144
#[cfg(all(feature = "nightly", not(any(target_arch = "asmjs", target_arch = "wasm32"))))]
145-
fn black_box(input: u8) -> u8 {
145+
#[inline(always)]
146+
fn black_box(mut input: u8) -> u8 {
146147
debug_assert!((input == 0u8) | (input == 1u8));
147148

148-
// Pretend to access a register containing the input. We "volatile" here
149-
// because some optimisers treat assembly templates without output operands
150-
// as "volatile" while others do not.
151-
unsafe { asm!("" :: "r"(&input) :: "volatile") }
149+
// Move value through assembler, which is opaque to the compiler, even though we don't do anything.
150+
unsafe { asm!("" : "=r"(input) : "0"(input) ) }
152151

153152
input
154153
}

0 commit comments

Comments
 (0)