Skip to content
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

Add design notes #16

Open
asomers opened this issue Jul 31, 2018 · 1 comment
Open

Add design notes #16

asomers opened this issue Jul 31, 2018 · 1 comment

Comments

@asomers
Copy link

asomers commented Jul 31, 2018

Could you please add an overview of the design to the documentation? I can't get an idea for how efficiently this crate is implemented without reading the source, and macro source is hard to read.

@dzamlo
Copy link
Owner

dzamlo commented Aug 1, 2018

Thanks for the suggestion, I will try to add something.

If you want to see the resulting code to evaluate it, you can use cargo expand on a small project with just your bitfield definition.You can also use cargo asm to check the resulting generated assembly.

In practice, because the bit offsets are constant and LLVM is very good, the generated assembly is often efficient, sometimes only a shr and and instructions to read a field for examples. Even in some case where the code generated by the macros seems bad.

But there is few things to watch for:

  • If you use the array form (likebitfield! { struct ArrayBitfield([u8]); ...) and use the bitfield with something else than a fixed size array, there will likely be a bound check and the generated assembly is not always that great. With a fixed size array, LLVM seems to do a really great job.
  • if you use an array field (like foo5, set_foo5: 0, 0, 32;), the resulting code may not be as efficient because the BitRange methods are called with non constant parameters. If you use a array field and use it with constants index, making the field #[inline] may help.

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

No branches or pull requests

2 participants