Skip to content

Commit

Permalink
WIP: bug fixes for add/sub, not quite there yet
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Dec 20, 2023
1 parent b2021d5 commit 90e2557
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
8 changes: 4 additions & 4 deletions include/universal/number/posit/specialized/posit_16_2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class posit<NBITS_IS_16, ES_IS_2> {
lhs_fraction += rhs_fraction;
// std::cout << "lhs frac : " << to_binary(lhs_fraction, 32) << '\n';

bool rcarry = 0x8000 & lhs_fraction; // first left bit
bool rcarry = (0x8000'0000 & lhs_fraction) != 0; // first left bit
if (rcarry) {
++exp;
if (exp > 3) {
Expand Down Expand Up @@ -245,7 +245,7 @@ class posit<NBITS_IS_16, ES_IS_2> {
decode_regime(lhs, m, remaining);

// extract the exponent
uint16_t exp = remaining >> 14;
uint16_t exp = remaining >> 13;

uint32_t lhs_fraction = (0x4000 | remaining) << 16;
int8_t shiftRight = m;
Expand All @@ -255,7 +255,7 @@ class posit<NBITS_IS_16, ES_IS_2> {
uint32_t rhs_fraction = (0x4000 | remaining) << 16;

// align the fractions for subtraction
shiftRight = (shiftRight << 1) + exp - (remaining >> 14);
shiftRight = (shiftRight << 2) + exp - (remaining >> 13);
if (shiftRight != 0) {
if (shiftRight >= 29) {
_bits = lhs;
Expand All @@ -275,7 +275,7 @@ class posit<NBITS_IS_16, ES_IS_2> {
--m;
lhs_fraction <<= 2;
}
bool ecarry = bool(0x40000000 & lhs_fraction);
bool ecarry = (0x4000'0000 & lhs_fraction) != 0;
if (!ecarry) {
if (exp == 0) --m;
exp ^= 1;
Expand Down
31 changes: 31 additions & 0 deletions static/posit/specialized/posit_16_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,37 @@ try {
float fa, fb;
posit<16, 1> a, b, c;

{
posit<16, 2> a, b, c, cref;
float fa, fb, fc;
/*
FAIL
1.3877787807814456755e-17 + -3.9990234375 != -1.99951171875 golden reference is -3.9990234375
0b0.000000000000001.. + 0b1.10.01.11111111111 != 0b1.10.00.11111111111 golden reference is 0b1.10.01.11111111111
*/
a.setbits(0x0001);
b.setbits(0xCFFF); // 0b1.10.0'1.111'1111'1111
c = a + b;
fa = float(a);
fb = float(b);
fc = fa + fb;
cref = fc;
std::cout
<< std::setw(NUMBER_COLUMN_WIDTH) << a << " + "
<< std::setw(NUMBER_COLUMN_WIDTH) << b << " = "
<< std::setw(NUMBER_COLUMN_WIDTH) << c << '\n';
std::cout
<< std::setw(NUMBER_COLUMN_WIDTH) << fa << " + "
<< std::setw(NUMBER_COLUMN_WIDTH) << fb << " = "
<< std::setw(NUMBER_COLUMN_WIDTH) << fc << '\n';
std::cout
<< std::setw(NUMBER_COLUMN_WIDTH) << to_binary(fa) << " + "
<< std::setw(NUMBER_COLUMN_WIDTH) << to_binary(fb) << " = "
<< std::setw(NUMBER_COLUMN_WIDTH) << to_binary(fc) << '\n';
ReportBinaryOperation(a, "+", b, c);
ReportBinaryArithmeticError("bla", "+", a, b, c, cref);
}

nrOfFailedTestCases += ReportTestResult(VerifyAddition <nbits, es>(reportTestCases), tag, "add (native) ");
return 0;
/*
Expand Down

0 comments on commit 90e2557

Please sign in to comment.