Skip to content

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

Merged
bors merged 1 commit into
rust-lang:masterfrom
flaper87:even
Jan 16, 2014
Merged

Use the least significant beat to determine if int/uint is even#11568
bors merged 1 commit into
rust-lang:masterfrom
flaper87:even

Conversation

@flaper87

Copy link
Copy Markdown
Contributor

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

@alexcrichton

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

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

@jdm

jdm commented Jan 15, 2014

Copy link
Copy Markdown
Contributor

@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
Copy Markdown
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

jdm commented Jan 15, 2014

Copy link
Copy Markdown
Contributor
  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
Copy Markdown
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
Copy Markdown
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
U007D pushed a commit to U007D/rust-mos that referenced this pull request Aug 21, 2026
11686: feat: Enum variant field completion, enum variant / struct consistency r=Veykril a=m0rg-dev

This addresses several related inconsistencies:
 - tuple structs use tab stops instead of placeholders
 - tuple structs display in the completion menu as `Struct {…}` instead of `Struct(…)`
 - enum variants don't receive field completions at all
 - enum variants display differently from structs in the completion menu

Also, structs now display their type in the completion detail rather than the raw snippet text to be inserted.

As far as what's user-visible, that looks like this:

| | Menu | Completion | Detail |
|-|-|-|-|
| Record struct (old) | `Struct {…}` | `Struct { x: ${1:()}, y: ${2:()} }$0` | `Struct { x: ${1:()}, y: ${2:()} }$0` |
| Record struct (new) | `Struct {…}` | `Struct { x: ${1:()}, y: ${2:()} }$0` | `Struct { x: i32, y: i32 }` |
| Tuple struct (old) | `Struct {…}`  | `Struct($1, $2)$0` | `Struct($1, $2)` |
| Tuple struct (new) | `Struct(…)` | `Struct(${1:()}, ${2:()})$0` | `Struct(i32, i32)` |
| Unit variant (old) | `Variant` | `Variant` | `()` |
| Unit variant (new) | `Variant` | `Variant$0` | `Variant` |
| Record variant (old) | `Variant` | `Variant` | `{x: i32, y: i32}` |
| Record variant (new) | `Variant {…}` | `Variant { x: ${1:()}, y: ${2:()} }$0` | `Variant { x: i32, y: i32 }` |
| Tuple variant (old) | `Variant(…)` | `Variant($0)` | `(i32, i32)` |
| Tuple variant (new) | `Variant(…)` | `Variant(${1:()}, ${2:()})$0` | `Variant(i32, i32)` |

Additionally, tuple variants no longer set `triggers_call_info` because it conflicts with placeholder generation, and tuple variants that require a qualified path should now use the qualified path.

Internally, this also lets us break the general "format an item with fields on it" code out into a shared module, so that means it'll be a lot easier to implement features like rust-lang#11568.


Co-authored-by: Morgan Thomas <corp@m0rg.dev>
U007D pushed a commit to U007D/rust-mos that referenced this pull request Aug 21, 2026
11691: feat: Suggest union literals, suggest union fields within an empty union literal r=Veykril a=m0rg-dev

Adds a `Union {…}` completion in contexts where a union is expected, expanding to a choice of available fields (if snippets are supported):

![image](https://user-images.githubusercontent.com/38578268/158023335-84c03e39-daf0-4a52-b969-f40b01501cc8.png)
![image](https://user-images.githubusercontent.com/38578268/158023354-db49d0bb-034c-49d3-bc02-07414179cb61.png)

Also, adds support for listing possible fields in an empty union literal.

![image](https://user-images.githubusercontent.com/38578268/158023398-4695ae34-ce64-4f40-8494-68731a3030c6.png)
![image](https://user-images.githubusercontent.com/38578268/158023406-be96dd95-125a-47ac-9628-0bce634ca2eb.png)

Closes rust-lang#11568.

Co-authored-by: Morgan Thomas <corp@m0rg.dev>
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