Skip to content

Use the least significant beat to determine if int/uint is even #11568

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

Merged
merged 1 commit into from
Jan 16, 2014

Conversation

flaper87
Copy link
Contributor

This implementation should be a bit more optimal than calling self.is_multiple_of(&2)

@alexcrichton
Copy link
Member

I'm curious, but have you verified that the generated code is actually better? I would expect LLVM to do proper inlining and figure everything out.

Regardless, the documentation shouldn't change, just the implementation, but thanks for the patch!

@flaper87
Copy link
Contributor Author

I didn't verify the generated code, I could check with --emit-llvm, though.

Why shouldn't the documentation change? It's not verifying the number is divisible by 2 anymore :/

@flaper87
Copy link
Contributor Author

Maybe not the best test. TBH, not sure what the best test is.

define i64 @main(i64, i8**) unnamed_addr {
top:
  %2 = call i64 @_ZN10lang_start19hdc66d049ef36cac4ay4v0.9E({ i64, %tydesc*, i8*, i8*, i8 }* null, i8* bitcast (void ({ i64, %tydesc*, i8*, i8*, i8 }*)* @_ZN4main19h2320e51607f4f569ai4v0.0E to i8*), i64 %0, i8** %
1)
  ret i64 %2
}
use std::num;

fn main() {
    2u & 1 == 0;
    2u.is_even();
}

@adrientetar
Copy link
Contributor

@flaper87 It still checks that the number is divisible by 2. Looking at the least significant bit is just how it is implemented.

@flaper87
Copy link
Contributor Author

Interesting! Then, let me trash this patch! Thanks! 😄

@flaper87 flaper87 closed this Jan 15, 2014
@flaper87 flaper87 reopened this Jan 15, 2014
@flaper87
Copy link
Contributor Author

I misunderstood @adridu59's comment. He was referring to the documentation.

@jdm
Copy link
Contributor

jdm commented Jan 15, 2014

@flaper87 It looks like llvm eliminated all of your test since the results were unused. You probably need to print the output or something.

@flaper87
Copy link
Contributor Author

This is the full output of the emit. http://pastebin.mozilla.org/4022063

And the test

use std::num;

fn main() {
    let a = 2u & 1 == 0;
    let b = 2u.is_even();

    println!("{}", a);
    println!("{}", b);
}

@jdm
Copy link
Contributor

jdm commented Jan 15, 2014

  store i8 1, i8* %a
  store i64 2, i64* %1
  %6 = call i8 @"_ZN4uint12Integer$uint7is_even21h93c2e9593d9b8cb6Ijak4v0.0E"(i64* %1)
  store i8 %6, i8* %b

Still not convinced that this test is representative of the comparison we're looking for.

@flaper87
Copy link
Contributor Author

Before: rustc -O -S --emit-llvm test.rs http://pastebin.mozilla.org/4022199

After: rustc -O -S --emit-llvm test.rs http://pastebin.mozilla.org/4022178

@alexcrichton
Copy link
Member

From the tests it appears they optimize to exactly the same thing, so LLVM is definitely doing its job. When running tests at O0, however, I imagine that and-ing with 1 is faster, so I'm gonna go ahead and r+ this.

Thanks for the patch and the good investigation!

bors added a commit that referenced this pull request Jan 16, 2014
This implementation should be a bit more optimal than calling `self.is_multiple_of(&2)`
@bors bors closed this Jan 16, 2014
@bors bors merged commit 515978d into rust-lang:master Jan 16, 2014
flip1995 pushed a commit to flip1995/rust that referenced this pull request Nov 2, 2023
ignore lower-camel-case words in `doc_markdown`

This fixes rust-lang#11568 by ignoring camelCase words starting with a lower case letter.

r? `@blyxyas`

---

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants