Skip to content

Commit f53648c

Browse files
bors[bot]thvdveld
andauthored
Merge #537
537: Don't use a mask when not needed r=Emilgardis a=thibautvdv When the mask has the same size as the parent register of a field, then a mask is not required. This removes the clippy warnings/errors where for example an operation of inverting a mask results in zero ``` error: this operation will always return zero. This is likely not the intended outcome | 61 | self.w.bits = (self.w.bits & !0xffff_ffff) | (value as u32 & 0xffff_ffff); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ``` or when the operation of using a mask is ineffective. ``` warning: the operation is ineffective. Consider reducing it to `value as u32` | 61 | self.w.bits = (self.w.bits & !0xffff_ffff) | (value as u32 & 0xffff_ffff); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ``` Co-authored-by: Thibaut Vandervelden <thvdveld@vub.be>
2 parents d6afbf9 + c2ca27f commit f53648c

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1313
elements. An `ArrayProxy` wrapper is used when a Rust built-in array does not
1414
match the cluster layout. Requires the `--const_generic` command line option.
1515
- Bumped `xtensa-lx` and add `xtensa_lx::interrupt::InterruptNumber` implementation.
16+
- Don't use a mask when the width of the mask is the same as the width of the parent register.
1617

1718
## [v0.19.0] - 2021-05-26
1819

src/generate/register.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,12 @@ pub fn fields(
302302
let fty = width.to_ty()?;
303303
let evs = &f.enumerated_values;
304304

305+
let use_mask = if let Some(size) = parent.size {
306+
size != width
307+
} else {
308+
false
309+
};
310+
305311
let lookup_results = lookup(
306312
evs,
307313
fields,
@@ -374,10 +380,14 @@ pub fn fields(
374380
quote! {
375381
((self.bits >> #offset) & #hexmask) #cast
376382
}
377-
} else {
383+
} else if use_mask {
378384
quote! {
379385
(self.bits & #hexmask) #cast
380386
}
387+
} else {
388+
quote! {
389+
self.bits #cast
390+
}
381391
};
382392

383393
if let Some((first, dim, increment, suffixes, suffixes_str)) = &field_dim {
@@ -398,10 +408,14 @@ pub fn fields(
398408
quote! {
399409
((self.bits >> #sub_offset) & #hexmask) #cast
400410
}
401-
} else {
411+
} else if use_mask {
402412
quote! {
403413
(self.bits & #hexmask) #cast
404414
}
415+
} else {
416+
quote! {
417+
self.bits #cast
418+
}
405419
};
406420
let name_sc_n = Ident::new(
407421
&util::replace_suffix(&f.name.to_sanitized_snake_case(), suffix),
@@ -679,11 +693,17 @@ pub fn fields(
679693
}
680694
}
681695
} else {
696+
let op = if use_mask {
697+
quote!{ self.w.bits = (self.w.bits & !#hexmask) | (value as #rty & #hexmask); }
698+
} else {
699+
quote!{ self.w.bits = value as #rty; }
700+
};
701+
682702
quote! {
683703
///Writes raw bits to the field
684704
#inline
685705
pub #unsafety fn #bits(self, value: #fty) -> &'a mut W {
686-
self.w.bits = (self.w.bits & !#hexmask) | (value as #rty & #hexmask);
706+
#op
687707
self.w
688708
}
689709
}

0 commit comments

Comments
 (0)