Skip to content

Commit

Permalink
assembler: Simplify FLIImpl table
Browse files Browse the repository at this point in the history
We can just use the iterator to derive the index instead of making it a table entry
(in hindsight, I don't know why I didn't do this to begin with).
  • Loading branch information
lioncash committed Jan 10, 2024
1 parent f566c33 commit 4b2dd73
Showing 1 changed file with 37 additions and 36 deletions.
73 changes: 37 additions & 36 deletions src/assembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <algorithm>
#include <array>
#include <cstring>
#include <utility>
#include <iterator>

namespace biscuit {
namespace {
Expand Down Expand Up @@ -1524,49 +1524,50 @@ void Assembler::FCVT_H_LU(FPR rd, GPR rs1, RMode rmode) noexcept {

static void FLIImpl(CodeBuffer& buffer, uint32_t funct7, FPR rd, double value) noexcept {
static constexpr std::array fli_table{
std::pair{0xBFF0000000000000ULL, 0U}, // -1.0
std::pair{0x0010000000000000ULL, 1U}, // Minimum positive normal
std::pair{0x3EF0000000000000ULL, 2U}, // 1.0 * 2^-16
std::pair{0x3F00000000000000ULL, 3U}, // 1.0 * 2^-15
std::pair{0x3F70000000000000ULL, 4U}, // 1.0 * 2^-8
std::pair{0x3F80000000000000ULL, 5U}, // 1.0 * 2^-7
std::pair{0x3FB0000000000000ULL, 6U}, // 1.0 * 2^-4
std::pair{0x3FC0000000000000ULL, 7U}, // 1.0 * 2^-3
std::pair{0x3FD0000000000000ULL, 8U}, // 0.25
std::pair{0x3FD4000000000000ULL, 9U}, // 0.3125
std::pair{0x3FD8000000000000ULL, 10U}, // 0.375
std::pair{0x3FDC000000000000ULL, 11U}, // 0.4375
std::pair{0x3FE0000000000000ULL, 12U}, // 0.5
std::pair{0x3FE4000000000000ULL, 13U}, // 0.625
std::pair{0x3FE8000000000000ULL, 14U}, // 0.75
std::pair{0x3FEC000000000000ULL, 15U}, // 0.875
std::pair{0x3FF0000000000000ULL, 16U}, // 1.0
std::pair{0x3FF4000000000000ULL, 17U}, // 1.25
std::pair{0x3FF8000000000000ULL, 18U}, // 1.5
std::pair{0x3FFC000000000000ULL, 19U}, // 1.75
std::pair{0x4000000000000000ULL, 20U}, // 2.0
std::pair{0x4004000000000000ULL, 21U}, // 2.5
std::pair{0x4008000000000000ULL, 22U}, // 3
std::pair{0x4010000000000000ULL, 23U}, // 4
std::pair{0x4020000000000000ULL, 24U}, // 8
std::pair{0x4030000000000000ULL, 25U}, // 16
std::pair{0x4060000000000000ULL, 26U}, // 2^7
std::pair{0x4070000000000000ULL, 27U}, // 2^8
std::pair{0x40E0000000000000ULL, 28U}, // 2^15
std::pair{0x40F0000000000000ULL, 29U}, // 2^16
std::pair{0x7FF0000000000000ULL, 30U}, // +inf
std::pair{0x7FF8000000000000ULL, 31U}, // Canonical NaN
0xBFF0000000000000ULL, // -1.0
0x0010000000000000ULL, // Minimum positive normal
0x3EF0000000000000ULL, // 1.0 * 2^-16
0x3F00000000000000ULL, // 1.0 * 2^-15
0x3F70000000000000ULL, // 1.0 * 2^-8
0x3F80000000000000ULL, // 1.0 * 2^-7
0x3FB0000000000000ULL, // 1.0 * 2^-4
0x3FC0000000000000ULL, // 1.0 * 2^-3
0x3FD0000000000000ULL, // 0.25
0x3FD4000000000000ULL, // 0.3125
0x3FD8000000000000ULL, // 0.375
0x3FDC000000000000ULL, // 0.4375
0x3FE0000000000000ULL, // 0.5
0x3FE4000000000000ULL, // 0.625
0x3FE8000000000000ULL, // 0.75
0x3FEC000000000000ULL, // 0.875
0x3FF0000000000000ULL, // 1.0
0x3FF4000000000000ULL, // 1.25
0x3FF8000000000000ULL, // 1.5
0x3FFC000000000000ULL, // 1.75
0x4000000000000000ULL, // 2.0
0x4004000000000000ULL, // 2.5
0x4008000000000000ULL, // 3
0x4010000000000000ULL, // 4
0x4020000000000000ULL, // 8
0x4030000000000000ULL, // 16
0x4060000000000000ULL, // 2^7
0x4070000000000000ULL, // 2^8
0x40E0000000000000ULL, // 2^15
0x40F0000000000000ULL, // 2^16
0x7FF0000000000000ULL, // +inf
0x7FF8000000000000ULL, // Canonical NaN
};

uint64_t ivalue{};
std::memcpy(&ivalue, &value, sizeof(uint64_t));

const auto iter = std::find_if(fli_table.cbegin(), fli_table.cend(), [ivalue](const auto& entry) {
return entry.first == ivalue;
const auto iter = std::find_if(fli_table.cbegin(), fli_table.cend(), [ivalue](uint64_t entry) {
return entry == ivalue;
});
BISCUIT_ASSERT(iter != fli_table.cend());

EmitRType(buffer, funct7, f1, GPR{iter->second}, 0b000, rd, 0b1010011);
const auto index = static_cast<uint32_t>(std::distance(fli_table.cbegin(), iter));
EmitRType(buffer, funct7, f1, GPR{index}, 0b000, rd, 0b1010011);
}

void Assembler::FLI_D(FPR rd, double value) noexcept {
Expand Down

0 comments on commit 4b2dd73

Please sign in to comment.