1
0
mirror of synced 2024-11-28 17:40:51 +01:00

feat: Added a new division by invariant multiplication tool

This commit is contained in:
WerWolv 2023-01-19 17:01:41 +01:00
parent 17b0f2ae77
commit 05c8158716
2 changed files with 61 additions and 0 deletions

View File

@ -480,6 +480,9 @@
"hex.builtin.tools.ieee754.sign": "Sign",
"hex.builtin.tools.ieee754.singe_precision": "Single Precision",
"hex.builtin.tools.ieee754.type": "Type",
"hex.builtin.tools.invariant_multiplication": "Division by invariant Multiplication",
"hex.builtin.tools.invariant_multiplication.description": "Division by invariant multiplication is a technique often used by compilers to optimize integer division by a constant into a multiplication followed by a shift. The reason for this is that divisions often take many times more clock cycles than multiplications do.\n\nThis tool can be used to calculate the multiplier from the divisor or the divisor from the multiplier.",
"hex.builtin.tools.invariant_multiplication.num_bits": "Number of Bits",
"hex.builtin.tools.input": "Input",
"hex.builtin.tools.name": "Name",
"hex.builtin.tools.permissions": "UNIX Permissions Calculator",

View File

@ -1316,6 +1316,63 @@ namespace hex::plugin::builtin {
}
}
void drawInvariantMultiplicationDecoder() {
static u64 divisor = 1;
static u64 multiplier = 1;
static u64 numBits = 32;
ImGui::TextFormattedWrapped("{}", "hex.builtin.tools.invariant_multiplication.description"_lang);
ImGui::NewLine();
if (ImGui::BeginChild("##calculator", ImVec2(0, ImGui::GetTextLineHeightWithSpacing() * 5), true)) {
static const u64 min = 1, max = 64;
ImGui::SliderScalar("hex.builtin.tools.invariant_multiplication.num_bits"_lang, ImGuiDataType_U64, &numBits, &min, &max);
ImGui::NewLine();
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushStyleColor(ImGuiCol_ChildBg, ImGui::GetStyleColorVec4(ImGuiCol_TableRowBgAlt));
if (ImGui::BeginChild("##calculator", ImVec2(0, ImGui::GetTextLineHeightWithSpacing() + 12_scaled), true)) {
ImGui::PushItemWidth(100_scaled);
ImGui::TextUnformatted("X /");
ImGui::SameLine();
if (ImGui::InputScalar("##divisor", ImGuiDataType_U64, &divisor)) {
if (divisor == 0)
divisor = 1;
multiplier = ((1LLU << (numBits + 1)) / divisor) + 1;
}
ImGui::SameLine();
ImGui::TextUnformatted(" <=> ");
ImGui::SameLine();
ImGui::TextUnformatted("( X *");
ImGui::SameLine();
if (ImGui::InputHexadecimal("##multiplier", &multiplier)) {
if (multiplier == 0)
multiplier = 1;
divisor = ((1LLU << (numBits + 1)) / multiplier) + 1;
}
ImGui::SameLine();
ImGui::TextFormatted(") >> {}", numBits + 1);
ImGui::PopItemWidth();
}
ImGui::EndChild();
ImGui::PopStyleColor();
ImGui::PopStyleVar();
}
ImGui::EndChild();
}
}
void registerToolEntries() {
@ -1330,6 +1387,7 @@ namespace hex::plugin::builtin {
ContentRegistry::Tools::add("hex.builtin.tools.wiki_explain", drawWikiExplainer);
ContentRegistry::Tools::add("hex.builtin.tools.file_tools", drawFileTools);
ContentRegistry::Tools::add("hex.builtin.tools.ieee754", drawIEEE754Decoder);
ContentRegistry::Tools::add("hex.builtin.tools.invariant_multiplication", drawInvariantMultiplicationDecoder);
}
}