From 5fc4e57db717902739207986ce972fcc298035f1 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sat, 9 Nov 2019 16:03:00 -0500 Subject: [PATCH] Eliminates non-portable use of `fls`. --- Components/68901/MFP68901.cpp | 7 ++++--- Components/68901/MFP68901.hpp | 12 ++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Components/68901/MFP68901.cpp b/Components/68901/MFP68901.cpp index 3d41cb7e9..2765f5406 100644 --- a/Components/68901/MFP68901.cpp +++ b/Components/68901/MFP68901.cpp @@ -301,7 +301,7 @@ void MFP68901::update_interrupts() { if(interrupt_vector_ & 0x8) { // Software interrupt mode: permit only if neither this interrupt // nor a higher interrupt is currently in service. - const int highest_bit = 1 << (fls(firing_interrupts) - 1); + const int highest_bit = msb16(firing_interrupts); interrupt_line_ = !(interrupt_in_service_ & ~(highest_bit - 1)); } else { // Auto-interrupt mode; just signal. @@ -326,8 +326,7 @@ int MFP68901::acknowledge_interrupt() { return NoAcknowledgement; } - const int selected = fls(interrupt_pending_ & interrupt_mask_) - 1; - const int mask = 1 << selected; + const int mask = msb16(interrupt_pending_ & interrupt_mask_); // Clear the pending bit regardless. interrupt_pending_ &= ~mask; @@ -339,6 +338,8 @@ int MFP68901::acknowledge_interrupt() { update_interrupts(); + int selected = 0; + while((1 << selected) != mask) ++selected; LOG("Interrupt acknowledged: " << selected); return (interrupt_vector_ & 0xf0) | uint8_t(selected); } diff --git a/Components/68901/MFP68901.hpp b/Components/68901/MFP68901.hpp index 450c97ed3..2933a8eaf 100644 --- a/Components/68901/MFP68901.hpp +++ b/Components/68901/MFP68901.hpp @@ -137,6 +137,18 @@ class MFP68901: public ClockingHint::Source { void begin_interrupts(int interrupt); void end_interrupts(int interrupt); void update_interrupts(); + + /// @returns the most significant bit set in v, assuming it is one of the least significant 16. + inline static int msb16(int v) { + // Saturate all bits below the MSB. + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + + // Throw away lesser bits. + return (v+1) >> 1; + } }; }