Skip to content

Commit

Permalink
restoring the multiplication operator algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Dec 26, 2023
1 parent 93ca08a commit 9c88e34
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
50 changes: 41 additions & 9 deletions include/universal/number/posit/specialized/posit_16_2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,21 +304,53 @@ class posit<NBITS_IS_16, ES_IS_2> {
lhs = lhs & sign_mask ? -lhs : lhs;
rhs = rhs & sign_mask ? -rhs : rhs;

uint16_t lhs_exp{ 0 }, lhs_fraction, rhs_exp{ 0 }, rhs_fraction;
uint16_t lhs_m = decode_posit(lhs, lhs_exp, lhs_fraction);
uint16_t rhs_m = decode_posit(rhs, rhs_exp, rhs_fraction);
/* {
uint16_t lhs_exp{ 0 }, lhs_fraction, rhs_exp{ 0 }, rhs_fraction;
uint16_t lhs_m = decode_posit(lhs, lhs_exp, lhs_fraction);
uint16_t rhs_m = decode_posit(rhs, rhs_exp, rhs_fraction);
uint16_t m = lhs_m + rhs_m;
uint16_t exp = lhs_exp + rhs_exp;
uint32_t result_fraction = lhs_fraction * rhs_fraction;
std::cout << "lhs : " << to_binary(lhs_fraction, 32, true) << '\n';
std::cout << "rhs : " << to_binary(rhs_fraction, 32, true) << '\n';
std::cout << "result : " << to_binary(result_fraction, 32, true) << '\n';
std::cout << "exp : " << int(exp) << '\n';
}
*/

// decode the regime of lhs
int8_t m = 0; // regime pattern length
uint16_t remaining; // Remaining bits after the regime: 0<remaining_bits>0..0
decode_regime(lhs, m, remaining);

// extract the exponent
int16_t exp = (remaining >> 13); // 16 - 1(sign) - 2(exponent)

uint16_t m = lhs_m + rhs_m;
uint16_t exp = lhs_exp + rhs_exp;
uint32_t result_fraction = lhs_fraction * rhs_fraction;
// extract remaining fraction bits
uint16_t lhs_fraction = (0x4000 | remaining << 1) & 0x7FFF; // 0x4000 is the hidden bit

// adjust shift and extract fraction bits of rhs
extractMultiplicand(rhs, m, remaining);
exp += (remaining >> 13);
uint16_t rhs_fraction = (0x4000 | remaining << 1) & 0x7FFF; // 0x4000 is the hidden bit
uint32_t result_fraction = (uint32_t)lhs_fraction * rhs_fraction;

//std::cout << "lhs : " << to_binary(lhs_fraction, 32, true) << '\n';
//std::cout << "rhs : " << to_binary(rhs_fraction, 32, true) << '\n';
//std::cout << "result : " << to_binary(result_fraction, 32, true) << '\n';
//std::cout << "exp : " << int(exp) << '\n';

if (exp > 3) {
++m;
exp &= 0x3;
}

bool rcarry = bool(result_fraction & 0x20000000);
bool rcarry = (result_fraction & 0x2000'0000) != 0;
if (rcarry) {
//std::cerr << "fraction carry processing commensing\n";
//std::cerr << to_binary(*this, true) << " * " << to_binary(b, true) << '\n';
exp++;
if (exp > 3) {
++m;
Expand Down Expand Up @@ -378,11 +410,11 @@ class posit<NBITS_IS_16, ES_IS_2> {

// adjust shift and extract fraction bits of rhs
extractDividand(rhs, m, remaining);
uint16_t rhsExp = (remaining >> 14);
uint16_t rhsExp = (remaining >> 13);

std::cout << to_binary(*this, true) << " : exp " << exp << '\n';
std::cout << to_binary(b, true) << " : exp " << rhsExp << '\n';

std::cout << "m : " << int(m) << '\n';

exp -= (remaining >> 13);
uint16_t rhs_fraction = (0x4000 | remaining << 1) & 0x7FFF; // 0x4000 is the hidden bit
Expand Down
20 changes: 16 additions & 4 deletions static/posito/arithmetic/multiplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace sw {
template<typename PositType>
int VerifyMultiplication(bool reportTestCases) {
constexpr unsigned nbits = PositType::nbits;
const unsigned NR_POSITS = 16; // (1 << nbits);
const unsigned NR_POSITS = (1 << nbits);
int nrOfFailedTests = 0;
for (unsigned i = 0; i < NR_POSITS; i++) {
PositType pa;
Expand Down Expand Up @@ -52,7 +52,7 @@ namespace sw {
pc = pa * pb;
#endif
pref = dc;
sw::universal::ReportBinaryOperation(pa, "*", pb, pc);
//sw::universal::ReportBinaryOperation(pa, "*", pb, pc);
//sw::universal::ReportBinaryOperation(da, "*", db, dc);
if (pc != pref) {
if (reportTestCases) ReportBinaryArithmeticError("FAIL", "*", pa, pb, pc, pref);
Expand Down Expand Up @@ -157,12 +157,24 @@ try {
#if MANUAL_TESTING
// generate individual testcases to hand trace/debug

/*
* fraction carry processing commensing
0b0.0000'0000'0001.00.1'' * 0b0.10.01.100'1000'1101
0b0.0000'0000'0001.00.1'' * 0b0.10.01.100'1000'1110
0b0.0000'0000'0001.00.1'' * 0b0.10.01.100'1000'1111
0b0.0000'0000'0001.00.1'' * 0b0.10.01.100'1001'0000
0b0.0000'0000'0001.00.1'' * 0b0.10.01.100'1001'0001
*/
// posit<16, 2> a(1), b(16), c;
// c = a * b;
// ReportBinaryOperation(a, "*", b, c);
// c = a / b;
// ReportBinaryOperation(a, "/", b, c);

return 0;
//nrOfFailedTestCases += sw::testing::VerifyMultiplicationWithPosito<posit<16, 1>, posito<16, 1>>(reportTestCases);
// nrOfFailedTestCases += sw::testing::VerifyMultiplicationWithPosito<posit<16, 2>, posito<16, 2>>(reportTestCases);

//nrOfFailedTestCases += ReportTestResult(sw::testing::VerifyMultiplication<posit<16, 2>>(reportTestCases), "posit<16, 2>", "multiplication");
nrOfFailedTestCases += ReportTestResult(sw::testing::VerifyMultiplication<posit<16, 2>>(reportTestCases), "posit<16, 2>", "multiplication");

ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
return EXIT_SUCCESS;
Expand Down

0 comments on commit 9c88e34

Please sign in to comment.