Skip to content

Commit

Permalink
WIP: RCAing specialized posit<16,2> divide operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Dec 25, 2023
1 parent 340567d commit 50c5ccf
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 35 deletions.
2 changes: 1 addition & 1 deletion include/universal/internal/value/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ void module_divide(const value<fbits>& lhs, const value<fbits>& rhs, value<divbi
int new_scale = lhs.scale() - rhs.scale();
bitblock<divbits> result_fraction;

if (fbits > 0) {
if constexpr (fbits > 0) {
// fractions are without hidden bit, get_fixed_point adds the hidden bit back in
bitblock<fhbits> r1 = lhs.get_fixed_point();
bitblock<fhbits> r2 = rhs.get_fixed_point();
Expand Down
39 changes: 26 additions & 13 deletions include/universal/number/posit/specialized/posit_16_2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ class posit<NBITS_IS_16, ES_IS_2> {
explicit posit(long double initial_value) : _bits(0) { *this = initial_value; }

// assignment operators for native types
constexpr posit& operator=(signed char rhs) { return integer_assign((long)rhs); }
constexpr posit& operator=(short rhs) { return integer_assign((long)rhs); }
constexpr posit& operator=(int rhs) { return integer_assign((long)rhs); }
constexpr posit& operator=(signed char rhs) { return integer_assign(rhs); }
constexpr posit& operator=(short rhs) { return integer_assign(rhs); }
constexpr posit& operator=(int rhs) { return integer_assign(rhs); }
constexpr posit& operator=(long rhs) { return integer_assign(rhs); }
constexpr posit& operator=(long long rhs) { return integer_assign((long)rhs); }
constexpr posit& operator=(char rhs) { return integer_assign((long)rhs); }
constexpr posit& operator=(unsigned short rhs) { return integer_assign((long)rhs); }
constexpr posit& operator=(unsigned int rhs) { return integer_assign((long)rhs); }
constexpr posit& operator=(unsigned long rhs) { return integer_assign((long)rhs); }
constexpr posit& operator=(unsigned long long rhs) { return integer_assign((long)rhs); }
constexpr posit& operator=(long long rhs) { return integer_assign(rhs); }
constexpr posit& operator=(char rhs) { return integer_assign(rhs); }
constexpr posit& operator=(unsigned short rhs) { return integer_assign(rhs); }
constexpr posit& operator=(unsigned int rhs) { return integer_assign(rhs); }
constexpr posit& operator=(unsigned long rhs) { return integer_assign(rhs); }
constexpr posit& operator=(unsigned long long rhs) { return integer_assign(rhs); }
posit& operator=(float rhs) { return float_assign(double(rhs)); }
posit& operator=(double rhs) { return float_assign(rhs); }
posit& operator=(long double rhs) { return float_assign(double(rhs)); }
Expand Down Expand Up @@ -620,17 +620,30 @@ class posit<NBITS_IS_16, ES_IS_2> {
raw = 0x4000u;
}
else {
// the scale of 0.5 * maxpos = 2^55, so we can filter out all bits above that
uint64_t mask = 0x0040'0000'0000'0000;
int8_t scale = 55;
int8_t scale = 54;
uint64_t fraction_bits = v;
while (!(fraction_bits & mask)) {
--scale;
fraction_bits <<= 1;
}
int8_t k = scale >> 2;
uint16_t exp = (scale & 0x3) << (12 - k); // extract exponent and shift to correct location
fraction_bits = (fraction_bits ^ mask);
raw = (0x7FFF ^ (0x3FFF >> k)) | exp | (fraction_bits >> (k + 12));
uint16_t exp = (scale & 0x3) << (11 - k); // extract exponent and shift to correct location
fraction_bits = (fraction_bits ^ mask); // remove the leading 1
uint16_t reg = (0x7FFF ^ (0x3FFF >> k));
//std::cout << "fra : " << to_binary(fraction_bits, 64, true) << '\n';
uint64_t fraa = (fraction_bits >> (k + 43));
//std::cout << "fraa : " << to_binary(fraa, 64, true) << '\n';
uint16_t fra = uint16_t(fraction_bits >> (k + 43));

//std::cout << "scale : " << int(scale) << '\n';
//std::cout << "k : " << int(k) << '\n';
//std::cout << "reg : " << to_binary(reg, 16, true) << '\n';
//std::cout << "exp : " << to_binary(exp, 16, true) << '\n';
//std::cout << "fra : " << to_binary(fra, 16, true) << '\n';

raw = (0x7FFF ^ (0x3FFF >> k)) | exp | (fraction_bits >> (k + 43));

mask = 0x1000u << k; // bitNPlusOne
if (mask & fraction_bits) {
Expand Down
9 changes: 5 additions & 4 deletions include/universal/number/posit/specialized/posit_8_0.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,11 @@ class posit<NBITS_IS_8, ES_IS_0> {
unsigned long long bits() const noexcept { return (unsigned long long)(_bits); }

// Modifiers
void clear() noexcept { _bits = 0; }
void setzero() noexcept { clear(); }
void setnar() noexcept { _bits = 0x80; }
void setnan(bool sign) noexcept { setnar(); }
void clear() noexcept { _bits = 0; }
void setzero() noexcept { clear(); }
void setnar() noexcept { _bits = 0x80; }
void setnan() noexcept { setnar(); }
//void setnan(bool sign = false) noexcept { setnar(); }
posit& minpos() noexcept {
clear();
return ++(*this);
Expand Down
24 changes: 12 additions & 12 deletions include/universal/number/posit/specialized/posit_8_2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,19 @@ class posit<NBITS_IS_8, ES_IS_2> {
}
// arithmetic assignment operators
posit& operator+=(const posit& b) {

if (b.iszero()) return *this;
return *this;
}
posit& operator-=(const posit& b) {

if (b.iszero()) return *this;
return *this;
}
posit& operator*=(const posit& b) {

if (b.iszero()) setzero();
return *this;
}
posit& operator/=(const posit& b) {

if (b.iszero()) setnar();
return *this;
}

Expand Down Expand Up @@ -264,21 +264,21 @@ class posit<NBITS_IS_8, ES_IS_2> {
// 0.111110.0. m = 4 1 ebit #0.------- >> 6
// 0.1111110.. m = 5 0 ebits #. >> 7 = 0
// 0.1111111.. m = 6 0 ebits #. >> 7 = 0
uint8_t ebits{ 0 };
uint8_t exp{ 0 };
switch (m) {
case -5: case 4:
ebits = (*remaining >> 5);
exp = (*remaining >> 5);
*remaining <<= 1;
break;
case -7: case -6: case 5: case 6:
ebits = 0;
exp = 0;
*remaining = 0;
default:
ebits = (*remaining >> 5);
exp = (*remaining >> 5);
*remaining <<= 2;
break;
}
return ebits;
return exp;
}

float fraction_value(uint8_t fraction) const {
Expand Down Expand Up @@ -394,9 +394,9 @@ class posit<NBITS_IS_8, ES_IS_2> {
int regimeScale = (1 << es) * m;
float s = (float)(sign_value());
float r = (m > 0 ? (float)(1 << regimeScale) : (1.0f / (float)(1 << -regimeScale)));
uint8_t ebits = extract_exponent(m, &remaining);
// std::cout << to_binary(ebits, 2) << " : " << to_binary(remaining, 8) << '\n';
float e = float((uint32_t(1) << ebits));
uint8_t expbits = extract_exponent(m, &remaining);
// std::cout << to_binary(expbits, 2) << " : " << to_binary(remaining, 8) << '\n';
float e = float((uint32_t(1) << expbits));
remaining |= 0x80; // set hidden bit
float f = fraction_value(remaining);
// std::cout << "regime value : " << r << '\n';
Expand Down
4 changes: 2 additions & 2 deletions include/universal/number/posito/math/sqrt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <universal/native/ieee754.hpp>
#include <universal/number/posito/math/sqrt_tables.hpp>

#ifndef POSIT_NATIVE_SQRT
#define POSIT_NATIVE_SQRT 0
#ifndef POSITO_NATIVE_SQRT
#define POSITO_NATIVE_SQRT 0
#endif

namespace sw { namespace universal {
Expand Down
21 changes: 18 additions & 3 deletions static/posito/arithmetic/division.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,34 @@ try {

// ToughDivisions2<posit<16,1>>();

posito<16, 2> pa(1), pb(1);
pa--; pb++;
/*
FAIL
8.5265128291212022305e-14 / 128 != 2.2204460492503130808e-16 golden reference is 8.8817841970012523234e-16
0b0.000000000001.00.1 / 0b0.110.11.0000000000 != 0b0.00000000000001.0. golden reference is 0b0.00000000000001.1.
*/
posit<16, 2> pa(1), pb(1), pc;
pa = 8.5265128291212022305e-14;
pb = 128.0;
ReportValue(pa, "pa");
ReportValue(pb, "pb");

pc = pa / pb;
ReportBinaryOperation(pa, "/", pb, pc);
double a, b;
a = double(pa);
b = double(pb);

GenerateTestCase<posit<16, 2>, double>(a, b);
GenerateTestCase<posito<16, 2>, double>(a, b);
return 0;

// Generate the worst fraction pressure for different posit configurations
// GenerateWorstCaseDivision<posit< 8, 1>>();
// GenerateWorstCaseDivision<posit<16, 2>>();

nrOfFailedTestCases += ReportTestResult(sw::testing::VerifyDivision<posit<8, 0>>(true), "posit<8,0>", "division");
// nrOfFailedTestCases += ReportTestResult(sw::testing::VerifyDivision<posit< 8, 0>>(true), "posit<8,0>", "division");
// nrOfFailedTestCases += ReportTestResult(sw::testing::VerifyDivision<posit<16, 1>>(true), "posit<16,1>", "division");
nrOfFailedTestCases += ReportTestResult(sw::testing::VerifyDivision<posit<16, 2>>(true), "posit<16,2>", "division");

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

0 comments on commit 50c5ccf

Please sign in to comment.