Skip to content

Commit

Permalink
Fixed code duplication and table cell handling clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
jiristefan committed Aug 26, 2024
1 parent c7aba82 commit 6e4afdc
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 83 deletions.
4 changes: 1 addition & 3 deletions src/gui/dialogs/new/newdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,7 @@ void NewDialog::bp_toggle_widgets() {
// depending on the setting

const machine::PredictorType predictor_type { config->get_bp_type() };
const bool is_predictor_dynamic { predictor_type == machine::PredictorType::SMITH_1_BIT
|| predictor_type == machine::PredictorType::SMITH_2_BIT
|| predictor_type == machine::PredictorType::SMITH_2_BIT_HYSTERESIS };
const bool is_predictor_dynamic { machine::is_predictor_type_dynamic(predictor_type) };
const bool is_predictor_enabled { config->get_bp_enabled() };

ui->group_bp_bht->setEnabled(is_predictor_enabled && is_predictor_dynamic);
Expand Down
55 changes: 25 additions & 30 deletions src/gui/windows/predictor/predictor_bht_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ QTableWidgetItem* DockPredictorBHT::get_bht_cell_item(uint8_t row_index, uint8_t
void DockPredictorBHT::set_table_color(QColor color) {
for (uint16_t row_index = 0; row_index < bht->rowCount(); row_index++) {
for (uint16_t column_index = 0; column_index < bht->columnCount(); column_index++) {
QTableWidgetItem *item { get_bht_cell_item(row_index, column_index) };
item->setBackground(QBrush(color));
get_bht_cell_item(row_index, column_index)->setBackground(
QBrush(color));
}
}
}

void DockPredictorBHT::set_row_color(uint16_t row_index, QColor color) {
for (uint16_t column_index = 0; column_index < bht->columnCount(); column_index++) {
QTableWidgetItem *item { get_bht_cell_item(row_index, column_index) };
item->setBackground(QBrush(color));
get_bht_cell_item(row_index, column_index)->setBackground(
QBrush(color));
}
}

Expand All @@ -91,9 +91,7 @@ void DockPredictorBHT::setup(
number_of_bht_bits = branch_predictor->get_number_of_bht_bits();
initial_state = branch_predictor->get_initial_state();
const machine::PredictorType predictor_type { branch_predictor->get_predictor_type() };
const bool is_predictor_dynamic { predictor_type == machine::PredictorType::SMITH_1_BIT
|| predictor_type == machine::PredictorType::SMITH_2_BIT
|| predictor_type == machine::PredictorType::SMITH_2_BIT_HYSTERESIS };
const bool is_predictor_dynamic { machine::is_predictor_type_dynamic(predictor_type) };
const bool is_predictor_enabled { branch_predictor->get_enabled() };

if (is_predictor_enabled) {
Expand Down Expand Up @@ -173,22 +171,21 @@ void DockPredictorBHT::update_bht_row(uint16_t row_index, machine::BranchHistory
}

for (uint16_t column_index = 0; column_index < bht->columnCount(); column_index++) {
QTableWidgetItem *item;
get_bht_cell_item(row_index, DOCK_BHT_COL_STATE)->setData(
Qt::DisplayRole, machine::predictor_state_to_string(bht_entry.state, true).toString());

item = get_bht_cell_item(row_index, DOCK_BHT_COL_STATE);
item->setData(Qt::DisplayRole, machine::predictor_state_to_string(bht_entry.state, true).toString());
get_bht_cell_item(row_index, DOCK_BHT_COL_CORRECT)->setData(
Qt::DisplayRole, QString::number(bht_entry.stats.correct));

item = get_bht_cell_item(row_index, DOCK_BHT_COL_CORRECT);
item->setData(Qt::DisplayRole, QString::number(bht_entry.stats.correct));
get_bht_cell_item(row_index, DOCK_BHT_COL_INCORRECT)->setData(
Qt::DisplayRole, QString::number(bht_entry.stats.wrong));

item = get_bht_cell_item(row_index, DOCK_BHT_COL_INCORRECT);
item->setData(Qt::DisplayRole, QString::number(bht_entry.stats.wrong));

item = get_bht_cell_item(row_index, DOCK_BHT_COL_ACCURACY);
if (bht_entry.stats.total > 0) {
item->setData(Qt::DisplayRole, QString::number(bht_entry.stats.accuracy) + " %");
get_bht_cell_item(row_index, DOCK_BHT_COL_ACCURACY)->setData(
Qt::DisplayRole, QString::number(bht_entry.stats.accuracy) + " %");
} else {
item->setData(Qt::DisplayRole, "N/A");
get_bht_cell_item(row_index, DOCK_BHT_COL_ACCURACY)->setData(
Qt::DisplayRole, "N/A");
}

}
Expand All @@ -210,22 +207,20 @@ void DockPredictorBHT::clear_name() {

void DockPredictorBHT::clear_bht(machine::PredictorState initial_state) {
for (uint16_t row_index = 0; row_index < bht->rowCount(); row_index++) {
QTableWidgetItem *item;

item = get_bht_cell_item(row_index, DOCK_BHT_COL_INDEX);
item->setData(Qt::DisplayRole, QString::number(row_index));
get_bht_cell_item(row_index, DOCK_BHT_COL_INDEX)->setData(
Qt::DisplayRole, QString::number(row_index));

item = get_bht_cell_item(row_index, DOCK_BHT_COL_STATE);
item->setData(Qt::DisplayRole, machine::predictor_state_to_string(initial_state, true).toString());
get_bht_cell_item(row_index, DOCK_BHT_COL_STATE)->setData(
Qt::DisplayRole, machine::predictor_state_to_string(initial_state, true).toString());

item = get_bht_cell_item(row_index, DOCK_BHT_COL_CORRECT);
item->setData(Qt::DisplayRole, QString::number(0));
get_bht_cell_item(row_index, DOCK_BHT_COL_CORRECT)->setData(
Qt::DisplayRole, QString::number(0));

item = get_bht_cell_item(row_index, DOCK_BHT_COL_INCORRECT);
item->setData(Qt::DisplayRole, QString::number(0));
get_bht_cell_item(row_index, DOCK_BHT_COL_INCORRECT)->setData(
Qt::DisplayRole, QString::number(0));

item = get_bht_cell_item(row_index, DOCK_BHT_COL_ACCURACY);
item->setData(Qt::DisplayRole, QString("N/A"));
get_bht_cell_item(row_index, DOCK_BHT_COL_ACCURACY)->setData(
Qt::DisplayRole, QString("N/A"));
}
bht->resizeRowsToContents();
set_table_color(Q_COLOR_DEFAULT);
Expand Down
52 changes: 24 additions & 28 deletions src/gui/windows/predictor/predictor_btb_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ QTableWidgetItem* DockPredictorBTB::get_btb_cell_item(uint8_t row_index, uint8_t
void DockPredictorBTB::set_table_color(QColor color) {
for (uint16_t row_index = 0; row_index < btb->rowCount(); row_index++) {
for (uint16_t column_index = 0; column_index < btb->columnCount(); column_index++) {
QTableWidgetItem *item { get_btb_cell_item(row_index, column_index) };
item->setBackground(QBrush(color));
get_btb_cell_item(row_index, column_index)->setBackground(
QBrush(color));
}
}
}

void DockPredictorBTB::set_row_color(uint16_t row_index, QColor color) {
for (uint16_t column_index = 0; column_index < btb->columnCount(); column_index++) {
QTableWidgetItem *item { get_btb_cell_item(row_index, column_index) };
item->setBackground(QBrush(color));
get_btb_cell_item(row_index, column_index)->setBackground(
QBrush(color));
}
}

Expand Down Expand Up @@ -103,26 +103,24 @@ void DockPredictorBTB::update_btb_row(
return;
}

QTableWidgetItem *item;

if (btb_entry.entry_valid) {
item = get_btb_cell_item(row_index, DOCK_BTB_COL_INSTR_ADDR);
item->setData(Qt::DisplayRole, machine::addr_to_hex_str(btb_entry.instruction_address));
get_btb_cell_item(row_index, DOCK_BTB_COL_INSTR_ADDR)->setData(
Qt::DisplayRole, machine::addr_to_hex_str(btb_entry.instruction_address));

item = get_btb_cell_item(row_index, DOCK_BTB_COL_TARGET_ADDR);
item->setData(Qt::DisplayRole, machine::addr_to_hex_str(btb_entry.target_address));
get_btb_cell_item(row_index, DOCK_BTB_COL_TARGET_ADDR)->setData(
Qt::DisplayRole, machine::addr_to_hex_str(btb_entry.target_address));

item = get_btb_cell_item(row_index, DOCK_BTB_COL_TYPE);
item->setData(Qt::DisplayRole, machine::branch_type_to_string(btb_entry.branch_type).toString());
get_btb_cell_item(row_index, DOCK_BTB_COL_TYPE)->setData(
Qt::DisplayRole, machine::branch_type_to_string(btb_entry.branch_type).toString());
} else {
item = get_btb_cell_item(row_index, DOCK_BTB_COL_INSTR_ADDR);
item->setData(Qt::DisplayRole, "");
get_btb_cell_item(row_index, DOCK_BTB_COL_INSTR_ADDR)->setData(
Qt::DisplayRole, "");

item = get_btb_cell_item(row_index, DOCK_BTB_COL_TARGET_ADDR);
item->setData(Qt::DisplayRole, "");
get_btb_cell_item(row_index, DOCK_BTB_COL_TARGET_ADDR)->setData(
Qt::DisplayRole, "");

item = get_btb_cell_item(row_index, DOCK_BTB_COL_TYPE);
item->setData(Qt::DisplayRole, "");
get_btb_cell_item(row_index, DOCK_BTB_COL_TYPE)->setData(
Qt::DisplayRole, "");
}
}

Expand All @@ -140,19 +138,17 @@ void DockPredictorBTB::reset_colors() {

void DockPredictorBTB::clear_btb() {
for (uint16_t row_index = 0; row_index < btb->rowCount(); row_index++) {
QTableWidgetItem *item;

item = get_btb_cell_item(row_index, DOCK_BTB_COL_INDEX);
item->setData(Qt::DisplayRole, QString::number(row_index));
get_btb_cell_item(row_index, DOCK_BTB_COL_INDEX)->setData(
Qt::DisplayRole, QString::number(row_index));

item = get_btb_cell_item(row_index, DOCK_BTB_COL_INSTR_ADDR);
item->setData(Qt::DisplayRole, QString(""));
get_btb_cell_item(row_index, DOCK_BTB_COL_INSTR_ADDR)->setData(
Qt::DisplayRole, QString(""));

item = get_btb_cell_item(row_index, DOCK_BTB_COL_TARGET_ADDR);
item->setData(Qt::DisplayRole, QString(""));
get_btb_cell_item(row_index, DOCK_BTB_COL_TARGET_ADDR)->setData(
Qt::DisplayRole, QString(""));

item = get_btb_cell_item(row_index, DOCK_BTB_COL_TYPE);
item->setData(Qt::DisplayRole, QString(""));
get_btb_cell_item(row_index, DOCK_BTB_COL_TYPE)->setData(
Qt::DisplayRole, QString(""));
}
btb->resizeRowsToContents();
set_table_color(Q_COLOR_DEFAULT);
Expand Down
29 changes: 14 additions & 15 deletions src/gui/windows/predictor/predictor_info_dock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ void DockPredictorInfo::setup(
number_of_bhr_bits = branch_predictor->get_number_of_bhr_bits();
initial_state = branch_predictor->get_initial_state();
const machine::PredictorType predictor_type { branch_predictor->get_predictor_type() };
is_predictor_dynamic = predictor_type == machine::PredictorType::SMITH_1_BIT
|| predictor_type == machine::PredictorType::SMITH_2_BIT
|| predictor_type == machine::PredictorType::SMITH_2_BIT_HYSTERESIS;
is_predictor_dynamic = machine::is_predictor_type_dynamic(predictor_type);
is_predictor_enabled = branch_predictor->get_enabled();

if (is_predictor_enabled) {
Expand All @@ -194,40 +192,41 @@ void DockPredictorInfo::setup(
this, &DockPredictorInfo::show_new_update);

if (is_predictor_dynamic) {

connect(
branch_predictor, &machine::BranchPredictor::bhr_updated,
this, &DockPredictorInfo::update_bhr);
}
}

if (is_predictor_enabled) {
content->setDisabled(false);
} else {
content->setDisabled(true);
}

// Toggle BHT index display
if (is_predictor_dynamic) {
label_event_predict_index_bht->setEnabled(true);
value_event_predict_index_bht->setEnabled(true);
label_event_update_index_bht->setEnabled(true);
value_event_update_index_bht->setEnabled(true);
label_bhr->setEnabled(true);
value_bhr->setEnabled(true);
} else {
label_event_predict_index_bht->setEnabled(false);
value_event_predict_index_bht->setEnabled(false);
label_event_update_index_bht->setEnabled(false);
value_event_update_index_bht->setEnabled(false);
label_bhr->setEnabled(false);
value_bhr->setEnabled(false);
}

if (number_of_bhr_bits == 0) {
// Toggle BHR display
if (is_predictor_dynamic && number_of_bhr_bits > 0) {
label_bhr->setEnabled(true);
value_bhr->setEnabled(true);
} else {
label_bhr->setEnabled(false);
value_bhr->setEnabled(false);
}

// Toggle whole widget
if (is_predictor_enabled) {
content->setDisabled(false);
} else {
content->setDisabled(true);
}

clear_bhr();
}

Expand Down
18 changes: 18 additions & 0 deletions src/machine/predictor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ QString machine::addr_to_hex_str(const machine::Address address) {
return "0x" + zero_padding + hex_addr;
}

bool machine::is_predictor_type_dynamic(const PredictorType type) {
switch (type)
{
case PredictorType::ALWAYS_NOT_TAKEN:
case PredictorType::ALWAYS_TAKEN:
case PredictorType::BTFNT:
return false;

case PredictorType::SMITH_1_BIT:
case PredictorType::SMITH_2_BIT:
case PredictorType::SMITH_2_BIT_HYSTERESIS:
return true;

default:
return false;
}
}

/////////////////////////////////
// BranchHistoryRegister class //
/////////////////////////////////
Expand Down
9 changes: 2 additions & 7 deletions src/machine/predictor.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ QStringView branch_type_to_string(const BranchType type);

QString addr_to_hex_str(const machine::Address address);

bool is_predictor_type_dynamic(const PredictorType type);

/////////////////////////////////
// BranchHistoryRegister class //
/////////////////////////////////
Expand Down Expand Up @@ -138,7 +140,6 @@ class Predictor : public QObject {
public: // General functions
uint16_t calculate_bht_index(const uint16_t bhr_value, const Address instruction_address) const;
virtual PredictorType get_type() const = 0;
virtual bool is_static() const = 0;
virtual BranchResult predict(PredictionInput input) = 0; // Function which handles all actions ties
// to making a branch prediction
virtual void update(PredictionFeedback feedback) = 0; // Update predictor based on jump / branch
Expand Down Expand Up @@ -169,7 +170,6 @@ class PredictorAlwaysNotTaken final : public Predictor {

public: // General functions
PredictorType get_type() const override { return PredictorType::ALWAYS_NOT_TAKEN; };
bool is_static() const override { return true; };
BranchResult predict(PredictionInput input) override;
void update(PredictionFeedback feedback) override;
};
Expand All @@ -181,7 +181,6 @@ class PredictorAlwaysTaken final : public Predictor {

public: // General functions
PredictorType get_type() const override { return PredictorType::ALWAYS_TAKEN; };
bool is_static() const override { return true; };
BranchResult predict(PredictionInput input) override;
void update(PredictionFeedback feedback) override;
};
Expand All @@ -193,7 +192,6 @@ class PredictorBTFNT final : public Predictor {

public: // General functions
PredictorType get_type() const override { return PredictorType::BTFNT; };
bool is_static() const override { return true; };
BranchResult predict(PredictionInput input) override;
void update(PredictionFeedback feedback) override;
};
Expand All @@ -208,7 +206,6 @@ class PredictorSmith1Bit final : public Predictor {

public: // General functions
PredictorType get_type() const override { return PredictorType::SMITH_1_BIT; };
bool is_static() const override { return false; };
BranchResult predict(PredictionInput input) override;
void update(PredictionFeedback feedback) override;
};
Expand All @@ -223,7 +220,6 @@ class PredictorSmith2Bit final : public Predictor {

public: // General functions
PredictorType get_type() const override { return PredictorType::SMITH_2_BIT; };
bool is_static() const override { return false; };
BranchResult predict(PredictionInput input) override;
void update(PredictionFeedback feedback) override;
};
Expand All @@ -238,7 +234,6 @@ class PredictorSmith2BitHysteresis final : public Predictor {

public: // General functions
PredictorType get_type() const override { return PredictorType::SMITH_2_BIT_HYSTERESIS; };
bool is_static() const override { return false; };
BranchResult predict(PredictionInput input) override;
void update(PredictionFeedback feedback) override;
};
Expand Down

0 comments on commit 6e4afdc

Please sign in to comment.