Skip to content

Commit

Permalink
Merge #202
Browse files Browse the repository at this point in the history
202: Prevent unnecessary bounds check in SCB::{get_priority, set_priority} r=therealprof a=qwerty19106

SystemHandler.index() gives true index for SCB.shpr array.
But now it produce unnecessary bounds check. For example, SCB::set_priority:
```
48: sym.cortex_m::peripheral::scb::__impl_cortex_m::peripheral::SCB_::set_priority::h622cd21bcc3321be ();
0x08000376      push    {r7, lr}
0x08000378      mov     r7, sp
0x0800037a      bl      cortex_m::peripheral::scb::SystemHandler::index::hdaf1a31aa5a6a8c5 ; sym.cortex_m::peripheral::scb::SystemHandler::index::hdaf1a31aa5a6a8c5
0x0800037e      subs    r0, 4
0x08000380      uxtb    r0, r0
0x08000382      cmp     r0, 0xb    ; 11
0x08000384      itttt   ls
0x08000386      movw    r1, 0xed18
0x0800038a      movt    r1, 0xe000
0x0800038e      movs    r2, 0xff
0x08000390      strb    r2, [r0, r1]
0x08000392      it      ls
0x08000394      pop     {r7, pc}
0x08000396      movw    r2, 0x1dc0
0x0800039a      movs    r1, 0xc
0x0800039c      movt    r2, 0x800
0x080003a0      bl      core::panicking::panic_bounds_check::h5181f47ae17a04a5 ; sym.core::panicking::panic_bounds_check::h5181f47ae17a04a5
0x080003a4      trap
```

This PR fix it.

Co-authored-by: Роман Кривенков <[email protected]>
  • Loading branch information
bors[bot] and qwerty19106 authored Mar 18, 2020
2 parents 12f11d2 + 9c57405 commit 6a0432a
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/peripheral/scb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -949,13 +949,23 @@ impl SCB {
#[cfg(not(armv6m))]
{
// NOTE(unsafe) atomic read with no side effects
unsafe { (*Self::ptr()).shpr[usize::from(index - 4)].read() }

// NOTE(unsafe): Index is bounded to [4,15] by SystemHandler design.
// TODO: Review it after rust-lang/rust/issues/13926 will be fixed.
let priority_ref = unsafe {(*Self::ptr()).shpr.get_unchecked(usize::from(index - 4))};

priority_ref.read()
}

#[cfg(armv6m)]
{
// NOTE(unsafe) atomic read with no side effects
let shpr = unsafe { (*Self::ptr()).shpr[usize::from((index - 8) / 4)].read() };

// NOTE(unsafe): Index is bounded to [11,15] by SystemHandler design.
// TODO: Review it after rust-lang/rust/issues/13926 will be fixed.
let priority_ref = unsafe {(*Self::ptr()).shpr.get_unchecked(usize::from((index - 8) / 4))};

let shpr = priority_ref.read();
let prio = (shpr >> (8 * (index % 4))) & 0x0000_00ff;
prio as u8
}
Expand All @@ -979,12 +989,20 @@ impl SCB {

#[cfg(not(armv6m))]
{
self.shpr[usize::from(index - 4)].write(prio)
// NOTE(unsafe): Index is bounded to [4,15] by SystemHandler design.
// TODO: Review it after rust-lang/rust/issues/13926 will be fixed.
let priority_ref = (*Self::ptr()).shpr.get_unchecked(usize::from(index - 4));

priority_ref.write(prio)
}

#[cfg(armv6m)]
{
self.shpr[usize::from((index - 8) / 4)].modify(|value| {
// NOTE(unsafe): Index is bounded to [11,15] by SystemHandler design.
// TODO: Review it after rust-lang/rust/issues/13926 will be fixed.
let priority_ref = (*Self::ptr()).shpr.get_unchecked(usize::from((index - 8) / 4));

priority_ref.modify(|value| {
let shift = 8 * (index % 4);
let mask = 0x0000_00ff << shift;
let prio = u32::from(prio) << shift;
Expand Down

0 comments on commit 6a0432a

Please sign in to comment.