| 1 |
/** |
| 2 |
* A minifloat like datatype for probablistic log counts (PLC) of elements |
| 3 |
* Contains unique functionalities for minimizing collision |
| 4 |
* Mantissa = 3 bits |
| 5 |
* Exponent = 4 bits |
| 6 |
* ControlBit = 1 bits |
| 7 |
* Copyright 2014 bcgsc |
| 8 |
*/ |
| 9 |
|
| 10 |
static const uint8_t numericBitMask = 0x7F; |
| 11 |
static const unsigned mantissa = 3; |
| 12 |
static const uint8_t mantiMask = 0xFF >> (8 - mantissa); |
| 13 |
static const uint8_t addMask = 0x80 >> (7 - mantissa); |
| 14 |
|
| 15 |
class plc { |
| 16 |
public: |
| 17 |
plc() |
| 18 |
{ |
| 19 |
m_val = 0; |
| 20 |
} |
| 21 |
|
| 22 |
void operator++() |
| 23 |
{ |
| 24 |
//extract numeric section of plc |
| 25 |
uint8_t maskedVal = m_val & numericBitMask; |
| 26 |
|
| 27 |
//check if at max value |
| 28 |
if (maskedVal == numericBitMask) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
if (maskedVal <= mantissa) { |
| 33 |
++m_val; |
| 34 |
} else { |
| 35 |
//this shifts the first bit off and creates the value |
| 36 |
//need to get the correct transition probability |
| 37 |
unsigned shiftVal = 1 << ((maskedVal >> mantissa) - 1); |
| 38 |
if (rand() % shiftVal == 0) { |
| 39 |
++maskedVal; |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
bool operator==(plc val) |
| 45 |
{ |
| 46 |
return val.rawValue() & numericBitMask == m_val & numericBitMask; |
| 47 |
} |
| 48 |
|
| 49 |
operator bool() |
| 50 |
{ |
| 51 |
return m_val & numericBitMask; |
| 52 |
} |
| 53 |
|
| 54 |
float toFloat() |
| 55 |
{ |
| 56 |
//extract numeric section of plc |
| 57 |
uint8_t maskedVal = m_val & numericBitMask; |
| 58 |
|
| 59 |
if (maskedVal <= mantissa) |
| 60 |
return float(maskedVal); |
| 61 |
return ldexp((maskedVal & mantiMask) | addMask, |
| 62 |
(maskedVal >> mantissa) - 1); |
| 63 |
} |
| 64 |
|
| 65 |
/* |
| 66 |
* return raw value of byte use to store value |
| 67 |
*/ |
| 68 |
uint8_t rawValue() |
| 69 |
{ |
| 70 |
return m_val; |
| 71 |
} |
| 72 |
|
| 73 |
void setStrandBit() |
| 74 |
{ |
| 75 |
m_val &= 0x80; |
| 76 |
} |
| 77 |
|
| 78 |
private: |
| 79 |
uint8_t m_val; |
| 80 |
}; |