Skip to content

Commit 536575c

Browse files
committed
Prevent unnecessary bounds check in SCB::{get_priority, set_priority}
1 parent 12f11d2 commit 536575c

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/peripheral/scb.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -949,13 +949,23 @@ impl SCB {
949949
#[cfg(not(armv6m))]
950950
{
951951
// NOTE(unsafe) atomic read with no side effects
952-
unsafe { (*Self::ptr()).shpr[usize::from(index - 4)].read() }
952+
953+
// NOTE(unsafe): prevent unnecessary bounds check! Index of shpr array is
954+
// true by SystemHandler design.
955+
let priority_ref = unsafe {(*Self::ptr()).shpr.get_unchecked(usize::from(index - 4))};
956+
957+
priority_ref.read()
953958
}
954959

955960
#[cfg(armv6m)]
956961
{
957962
// NOTE(unsafe) atomic read with no side effects
958-
let shpr = unsafe { (*Self::ptr()).shpr[usize::from((index - 8) / 4)].read() };
963+
964+
// NOTE(unsafe): prevent unnecessary bounds check! Index of shpr array is
965+
// true by SystemHandler design.
966+
let priority_ref = unsafe {(*Self::ptr()).shpr.get_unchecked(usize::from((index - 8) / 4))};
967+
968+
let shpr = priority_ref.read();
959969
let prio = (shpr >> (8 * (index % 4))) & 0x0000_00ff;
960970
prio as u8
961971
}
@@ -979,12 +989,20 @@ impl SCB {
979989

980990
#[cfg(not(armv6m))]
981991
{
982-
self.shpr[usize::from(index - 4)].write(prio)
992+
// NOTE(unsafe): prevent unnecessary bounds check! Index of shpr array is
993+
// true by SystemHandler design.
994+
let priority_ref = (*Self::ptr()).shpr.get_unchecked(usize::from(index - 4));
995+
996+
priority_ref.write(prio)
983997
}
984998

985999
#[cfg(armv6m)]
9861000
{
987-
self.shpr[usize::from((index - 8) / 4)].modify(|value| {
1001+
// NOTE(unsafe): prevent unnecessary bounds check! Index of shpr array is
1002+
// true by SystemHandler design.
1003+
let priority_ref = (*Self::ptr()).shpr.get_unchecked(usize::from((index - 8) / 4));
1004+
1005+
priority_ref.modify(|value| {
9881006
let shift = 8 * (index % 4);
9891007
let mask = 0x0000_00ff << shift;
9901008
let prio = u32::from(prio) << shift;

0 commit comments

Comments
 (0)