Skip to content

Commit

Permalink
redefining nibble markers to be 0-based
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Dec 16, 2023
1 parent 08fd473 commit e918885
Show file tree
Hide file tree
Showing 14 changed files with 237 additions and 102 deletions.
4 changes: 3 additions & 1 deletion include/universal/number/posit/exponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ template<unsigned nbits, unsigned es>
inline std::string to_string(const exponent<nbits, es>& e, bool dashExtent = true, bool nibbleMarker = false) {
std::stringstream sstr;
unsigned nrOfExponentBitsProcessed = 0;
unsigned ebits = e.nrBits();
if constexpr (es > 0) {
for (int i = int(es) - 1; i >= 0; --i) {
if (e.nrBits() > nrOfExponentBitsProcessed++) {
Expand All @@ -208,7 +209,8 @@ inline std::string to_string(const exponent<nbits, es>& e, bool dashExtent = tru
else {
sstr << (dashExtent ? "-" : "");
}
if (nibbleMarker && ((i % 4) == 0) && i != 0) sstr << '\'';
--ebits;
if (nibbleMarker && ebits != 0 && (ebits % 4) == 0) sstr << '\'';
}
}
else {
Expand Down
4 changes: 3 additions & 1 deletion include/universal/number/posit/fraction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ template<unsigned nfbits>
inline std::string to_string(const fraction<nfbits>& f, bool dashExtent = true, bool nibbleMarker = false) {
unsigned int nrOfFractionBitsProcessed = 0;
std::stringstream sstr;
unsigned fbits = f.nrBits();
if (nfbits > 0) {
bitblock<nfbits> bb = f.get();
int upperbound = nfbits;
Expand All @@ -277,7 +278,8 @@ inline std::string to_string(const fraction<nfbits>& f, bool dashExtent = true,
else {
sstr << (dashExtent ? "-" : "");
}
if (nibbleMarker && ((i % 4) == 0) && i != 0) sstr << '\'';
--fbits;
if (nibbleMarker && fbits != 0 && (fbits % 4) == 0) sstr << '\'';
}
}
if (nrOfFractionBitsProcessed == 0) sstr << '~'; // for proper alignment in tables
Expand Down
16 changes: 8 additions & 8 deletions include/universal/number/posit/math/sqrt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ namespace sw { namespace universal {
p.setnar();
return p;
}
unsigned root = posit_3_0_roots[a.encoding()];
unsigned root = posit_3_0_roots[a.bits()];
p.setbits(root);
return p;
}
Expand All @@ -159,7 +159,7 @@ namespace sw { namespace universal {
p.setnar();
return p;
}
unsigned root = posit_3_1_roots[a.encoding()];
unsigned root = posit_3_1_roots[a.bits()];
p.setbits(root);
return p;
}
Expand All @@ -173,7 +173,7 @@ namespace sw { namespace universal {
return p;
}

unsigned root = posit_4_0_roots[a.encoding()];
unsigned root = posit_4_0_roots[a.bits()];
p.setbits(root);
return p;
}
Expand All @@ -186,7 +186,7 @@ namespace sw { namespace universal {
p.setnar();
return p;
}
unsigned root = posit_5_0_roots[a.encoding()];
unsigned root = posit_5_0_roots[a.bits()];
p.setbits(root);
return p;
}
Expand All @@ -199,7 +199,7 @@ namespace sw { namespace universal {
p.setnar();
return p;
}
unsigned root = posit_8_0_roots[a.encoding()];
unsigned root = posit_8_0_roots[a.bits()];
p.setbits(root);
return p;
}
Expand All @@ -212,7 +212,7 @@ namespace sw { namespace universal {
p.setnar();
return p;
}
unsigned root = posit_8_1_roots[a.encoding()];
unsigned root = posit_8_1_roots[a.bits()];
p.setbits(root);
return p;
}
Expand Down Expand Up @@ -242,7 +242,7 @@ namespace sw { namespace universal {
return p;
}

uint16_t raw = uint16_t(a.encoding());
uint16_t raw = uint16_t(a.bits());
int16_t scale;
// Compute the square root. Here, kZ is the net power-of-2 scaling of the result.
// Decode the regime and exponent bit; scale the input to be in the range 1 to 4:
Expand Down Expand Up @@ -336,7 +336,7 @@ namespace sw { namespace universal {
return p;
}

uint32_t raw = uint32_t(a.encoding());
uint32_t raw = uint32_t(a.bits());
int32_t scale;
// Compute the square root; shiftZ is the power-of-2 scaling of the result.
// Decode regime and exponent; scale the input to be in the range 1 to 4:
Expand Down
2 changes: 1 addition & 1 deletion include/universal/number/posit/posit_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ class posit {
bool isinteger() const { return true; } // return (floor(*this) == *this) ? true : false; }

bitblock<nbits> get() const { return _bits; }
unsigned long long encoding() const { return _bits.to_ullong(); }
unsigned long long bits() const { return _bits.to_ullong(); }
constexpr bool test(unsigned bitIndex) const noexcept {
return (bitIndex < nbits ? _bits[bitIndex] : false);
}
Expand Down
6 changes: 4 additions & 2 deletions include/universal/number/posit/regime.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
// regime.hpp: definition of a posit regime
//
// Copyright (C) 2017-2022 Stillwater Supercomputing, Inc.
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.

Expand Down Expand Up @@ -182,11 +182,13 @@ template<unsigned nbits, unsigned es>
inline std::string to_string(const regime<nbits, es>& r, bool dashExtent = true, bool nibbleMarker = false) {
std::stringstream sstr;
bitblock<nbits - 1> bb = r.get();
unsigned rbits = r.nrBits();
unsigned nrOfRegimeBitsProcessed = 0;
for (int i = nbits - 2; i >= 0; --i) {
if (r.nrBits() > nrOfRegimeBitsProcessed++) {
sstr << (bb[unsigned(i)] ? '1' : '0');
if (nibbleMarker && ((i % 4) == 0) && i != 0) sstr << '\'';
--rbits;
if (nibbleMarker && rbits != 0 && (rbits % 4) == 0) sstr << '\'';
}
else {
sstr << (dashExtent ? "-" : "");
Expand Down
15 changes: 12 additions & 3 deletions include/universal/number/posit/specialized/posit_16_1.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
// posit_16_1.hpp: specialized 16-bit posit using fast compute specialized for posit<16,1>
//
// Copyright (C) 2017-2022 Stillwater Supercomputing, Inc.
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.

Expand Down Expand Up @@ -157,17 +157,22 @@ class posit<NBITS_IS_16, ES_IS_1> {

// extract the exponent
uint16_t exp = remaining >> 14;
std::cout << "exponent : " << exp << '\n';

// extract remaining fraction bits
std::cout << "remaining: " << to_binary(remaining, 16, true) << '\n';
uint32_t lhs_fraction = (0x4000 | remaining) << 16;
int8_t shiftRight = m;
std::cout << "lhs frac : " << to_binary(lhs_fraction, 32) << '\n';

// adjust shift and extract fraction bits of rhs
extractAddand(rhs, shiftRight, remaining);
uint32_t rhs_fraction = (0x4000 | remaining) << 16;
std::cout << "rhs frac : " << to_binary(rhs_fraction, 32) << '\n';

// this is 2kZ + expZ; (where kZ=kA-kB and expZ=expA-expB)
shiftRight = (shiftRight << 1) + exp - (remaining >> 14);
std::cout << "shiftRight : " << int(shiftRight) << '\n';

if (shiftRight == 0) {
lhs_fraction += rhs_fraction; // this will always product a carry
Expand All @@ -187,6 +192,8 @@ class posit<NBITS_IS_16, ES_IS_1> {
}
}

std::cout << "lhs frac : " << to_binary(lhs_fraction, 32) << '\n';

_bits = round(m, exp, lhs_fraction);
if (sign) _bits = -_bits & 0xFFFF;
return *this;
Expand Down Expand Up @@ -443,7 +450,7 @@ class posit<NBITS_IS_16, ES_IS_1> {
inline int sign_value() const { return (_bits & 0x8 ? -1 : 1); }

bitblock<NBITS_IS_16> get() const { bitblock<NBITS_IS_16> bb; bb = int(_bits); return bb; }
unsigned long long encoding() const { return (unsigned long long)(_bits); }
unsigned long long bits() const { return (unsigned long long)(_bits); }

// Modifiers
inline void clear() { _bits = 0; }
Expand Down Expand Up @@ -637,14 +644,16 @@ class posit<NBITS_IS_16, ES_IS_1> {
_bits = uint16_t(ptt.to_ulong());
return *this;
}

public:
// decode_regime takes the raw bits of the posit, and returns the regime run-length, m, and the remaining fraction bits in remainder
inline void decode_regime(const uint16_t bits, int8_t& m, uint16_t& remaining) const {
remaining = (bits << 2) & 0xFFFF;
std::cout << "remaining : " << to_binary(remaining, 16, true) << '\n';
if (bits & 0x4000) { // positive regimes
while (remaining >> 15) {
++m;
remaining = (remaining << 1) & 0xFFFF;
std::cout << "remaining : " << to_binary(remaining, 16, true) << '\n';
}
}
else { // negative regimes
Expand Down
67 changes: 43 additions & 24 deletions include/universal/number/posit/specialized/posit_16_2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.
#include <universal/native/integers.hpp>

// DO NOT USE DIRECTLY!
// the compile guards in this file are only valid in the context of the specialization logic
Expand All @@ -28,7 +29,7 @@ template<>
class posit<NBITS_IS_16, ES_IS_2> {
public:
static constexpr unsigned nbits = NBITS_IS_16;
static constexpr unsigned es = ES_IS_2;
static constexpr unsigned es = ES_IS_2;
static constexpr unsigned sbits = 1;
static constexpr unsigned rbits = nbits - sbits;
static constexpr unsigned ebits = es;
Expand Down Expand Up @@ -156,28 +157,40 @@ 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;
std::cout << "exponent : " << exp << '\n';

// extract remaining fraction bits
std::cout << "remaining: " << to_binary(remaining, 16, true) << '\n';
remaining <<= 3; // shift out the exponent field
std::cout << "remaining: " << to_binary(remaining, 16, true) << '\n';
remaining >>= 2; // move fraction bits in the right place
std::cout << "remaining: " << to_binary(remaining, 16, true) << '\n';
uint32_t lhs_fraction = (0x4000 | remaining) << 16;
int8_t shiftRight = m;
std::cout << "lhs frac : " << to_binary(lhs_fraction, 32) << '\n';

// adjust shift and extract fraction bits of rhs
extractAddand(rhs, shiftRight, remaining);
std::cout << "runlengt : " << int(shiftRight) << '\n';
extractAddand(rhs, m, remaining);
uint32_t rhs_fraction = (0x4000 | remaining) << 16;
std::cout << "rhs frac : " << to_binary(rhs_fraction, 32) << '\n';

// this is 2kZ + expZ; (where kZ=kA-kB and expZ=expA-expB)
shiftRight = (shiftRight << 1) + exp - (remaining >> 14);
shiftRight = (shiftRight << 1) + exp - (remaining >> 13);
std::cout << "shiftRight : " << int(shiftRight) << '\n';

if (shiftRight == 0) {
lhs_fraction += rhs_fraction; // this will always product a carry
lhs_fraction += rhs_fraction; // this will always produce a carry
if (exp) ++m;
exp ^= 1;
lhs_fraction >>= 1;
}
else {
(shiftRight > 31) ? (rhs_fraction = 0) : (rhs_fraction >>= shiftRight); // frac32B >>= shiftRight
std::cout << "rhs frac : " << to_binary(rhs_fraction, 32) << '\n';
lhs_fraction += rhs_fraction;
std::cout << "lhs frac : " << to_binary(lhs_fraction, 32) << '\n';

bool rcarry = 0x80000000 & lhs_fraction; // first left bit
if (rcarry) {
Expand All @@ -187,6 +200,8 @@ class posit<NBITS_IS_16, ES_IS_2> {
}
}

std::cout << "lhs frac : " << to_binary(lhs_fraction, 32) << '\n';

_bits = round(m, exp, lhs_fraction);
if (sign) _bits = -_bits & 0xFFFF;
return *this;
Expand Down Expand Up @@ -431,45 +446,46 @@ class posit<NBITS_IS_16, ES_IS_2> {
}

// Selectors
inline bool sign() const { return (_bits & sign_mask); }
inline bool isnar() const { return (_bits == sign_mask); }
inline bool iszero() const { return (_bits == 0x0); }
inline bool isone() const { return (_bits == 0x4000); } // pattern 010000...
inline bool isminusone() const { return (_bits == 0xC000); } // pattern 110000...
inline bool isneg() const { return (_bits & sign_mask); }
inline bool ispos() const { return !isneg(); }
inline bool ispowerof2() const { return !(_bits & 0x1); }
bool sign() const { return (_bits & sign_mask); }
bool isnar() const { return (_bits == sign_mask); }
bool iszero() const { return (_bits == 0x0); }
bool isone() const { return (_bits == 0x4000); } // pattern 010000...
bool isminusone() const { return (_bits == 0xC000); } // pattern 110000...
bool isneg() const { return (_bits & sign_mask); }
bool ispos() const { return !isneg(); }
bool ispowerof2() const { return !(_bits & 0x1); }

inline int sign_value() const { return (_bits & 0x8 ? -1 : 1); }
int sign_value() const { return (_bits & 0x8 ? -1 : 1); }

bitblock<NBITS_IS_16> get() const { bitblock<NBITS_IS_16> bb; bb = int(_bits); return bb; }
uint16_t bits() const noexcept { return _bits; }
unsigned long long encoding() const { return (unsigned long long)(_bits); }

// Modifiers
inline void clear() { _bits = 0; }
inline void setzero() { clear(); }
inline void setnar() { _bits = sign_mask; }
inline posit& minpos() {
void clear() { _bits = 0; }
void setzero() { clear(); }
void setnar() { _bits = sign_mask; }
posit& minpos() {
clear();
return ++(*this);
}
inline posit& maxpos() {
posit& maxpos() {
setnar();
return --(*this);
}
inline posit& zero() {
posit& zero() {
clear();
return *this;
}
inline posit& minneg() {
posit& minneg() {
clear();
return --(*this);
}
inline posit& maxneg() {
posit& maxneg() {
setnar();
return ++(*this);
}
inline posit twosComplement() const {
posit twosComplement() const {
posit p;
return p.setbits(~_bits + 1ul);
}
Expand Down Expand Up @@ -638,13 +654,16 @@ class posit<NBITS_IS_16, ES_IS_2> {
return *this;
}

public:
// decode_regime takes the raw bits of the posit, and returns the regime run-length, m, and the remaining fraction bits in remainder
inline void decode_regime(const uint16_t bits, int8_t& m, uint16_t& remaining) const {
remaining = (bits << 2) & 0xFFFF;
std::cout << "remaining : " << to_binary(remaining, 16, true) << '\n';
if (bits & 0x4000) { // positive regimes
while (remaining >> 15) {
++m;
remaining = (remaining << 1) & 0xFFFF;
std::cout << "remaining : " << to_binary(remaining, 16, true) << '\n';
}
}
else { // negative regimes
Expand All @@ -657,7 +676,7 @@ class posit<NBITS_IS_16, ES_IS_2> {
}
}
inline void extractAddand(const uint16_t bits, int8_t& m, uint16_t& remaining) const {
remaining = (bits << 2) & 0xFFFF;
remaining = (bits << 3) & 0xFFFF;
if (bits & 0x4000) { // positive regimes
while (remaining >> 15) {
--m;
Expand Down
6 changes: 3 additions & 3 deletions include/universal/number/posit/specialized/posit_32_2.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
// posit_32_2.hpp: specialized 32-bit posit using fast compute specialized for posit<32,2>
//
// Copyright (C) 2017-2022 Stillwater Supercomputing, Inc.
// Copyright (C) 2017 Stillwater Supercomputing, Inc.
//
// This file is part of the universal numbers project, which is released under an MIT Open Source license.

Expand Down Expand Up @@ -163,7 +163,7 @@ class posit<NBITS_IS_32, ES_IS_2> {
int32_t shiftRight = m;

// adjust shift and extract fraction bits of rhs
extractAddand(rhs, shiftRight, remaining);
extractAddand(rhs, m, remaining);
uint64_t frac64B = ((0x40000000ull | uint64_t(remaining) << 1) & 0x7FFFFFFFull) << 32; // ((0x4000'0000ull | remaining << 1) & 0x7FFF'FFFFull) << 32;
// This is 4kZ + expZ; (where kZ=kA-kB and expZ=expA-expB)
shiftRight = (shiftRight << 2) + exp - (remaining >> 29);
Expand Down Expand Up @@ -488,7 +488,7 @@ class posit<NBITS_IS_32, ES_IS_2> {
inline int sign_value() const { return (_bits & 0x8) ? -1 : 1; }

bitblock<NBITS_IS_32> get() const { bitblock<NBITS_IS_32> bb; bb = long(_bits); return bb; }
unsigned long long encoding() const { return (unsigned long long)(_bits); }
unsigned long long bits() const { return (unsigned long long)(_bits); }
inline posit twosComplement() const {
posit p;
uint64_t raw = _bits;
Expand Down
Loading

0 comments on commit e918885

Please sign in to comment.