Skip to content

Commit

Permalink
Machine and GUI: optimize to use qCountLeadingZeroBits
Browse files Browse the repository at this point in the history
It is available from It 5.6.

Signed-off-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
  • Loading branch information
ppisa committed Nov 26, 2023
1 parent 6e74b4e commit 477330b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 41 deletions.
34 changes: 0 additions & 34 deletions src/gui/windows/cache/cacheview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

#include <cmath>

#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
#include <QtAlgorithms>
#endif

//////////////////////
#define ROW_HEIGHT 14
Expand All @@ -23,39 +21,7 @@
using namespace std;

static inline unsigned int bitsToRepresent(quint32 range_max_val) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 6, 0)
return 32 - qCountLeadingZeroBits(range_max_val);
#else
const static qint8 bit_table[256] = {
/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
/* 0x */ 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
/* 1x */ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
/* 2x */ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
/* 3x */ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
/* 4x */ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
/* 5x */ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
/* 6x */ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
/* 7x */ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
/* 8x */ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
/* 9x */ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
/* Ax */ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
/* Bx */ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
/* Cx */ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
/* Dx */ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
/* Ex */ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
/* Fx */ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
};
unsigned int res = 0;
unsigned int step;
step = ~(quint32)((qint32)((range_max_val >> 16) - 1) >> 31) & 16;
res += step;
range_max_val >>=step;
step = ~(quint32)((qint32)((range_max_val >> 8) - 1) >> 31) & 8;
res += step;
range_max_val >>=step;
res += bit_table[range_max_val];
return res;
#endif
}

CacheAddressBlock::CacheAddressBlock(const machine::Cache *cache, unsigned width) {
Expand Down
13 changes: 6 additions & 7 deletions src/machine/csr/controlstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "machinedefs.h"
#include "simulator_exception.h"

#include <QtAlgorithms>

LOG_CATEGORY("machine.csr.control_state");

namespace machine { namespace CSR {
Expand Down Expand Up @@ -99,14 +101,11 @@ namespace machine { namespace CSR {
RegisterValue mip = register_data[Id::MIP];
int irq_to_signal = 0;

uint64_t irqs = mie.as_u64() & mip.as_u64() & 0xffffffff;
quint64 irqs = mie.as_u64() & mip.as_u64() & 0xffffffff;

// use ffs or __builtin_ffsl where available
for (int i = 0; i < 32; i++) {
if (irqs & (1UL << i)) {
irq_to_signal = i;
break;
}
if (irqs != 0) {
// Find the first (leas significant) set bit
irq_to_signal = 63 - qCountLeadingZeroBits(irqs & (~irqs + 1));
}

value = (uint64_t)(irq_to_signal |
Expand Down

0 comments on commit 477330b

Please sign in to comment.