Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
-moved integer log2 function to separate (unsynced) header
-other small changes
  • Loading branch information
CrepeGoat committed Jul 7, 2016
1 parent 6a50a5d commit c425260
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 50 deletions.
36 changes: 2 additions & 34 deletions Implementation/rational.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "rational.h"
#include <utility>
#include "uint_log2.h"
///////////// Constructors ////////////////

T_rational::T_rational() {
Expand Down Expand Up @@ -29,40 +29,8 @@ void T_rational::set_nth_digit(const T_index digit, T_uint_dec& value, bool bit)
else value &= ~(1<<digit);
}

T_rational::T_index T_rational::log2_v1(const T_uint_dec& value) {
if(!value) return -1;
const float FloatValue = value;
return ((((*(unsigned long *)&FloatValue) & 0x7f800000) >> 23) - 127);
}
T_rational::T_index T_rational::log2_v2(const T_uint_dec& value) {
if (!value) return -1;
std::pair<unsigned char, unsigned char> bitwidths((4*sizeof(T_uint_dec)),
(4*sizeof(T_uint_dec))+1);
// .first = # of bits covered by left half
// .second = # of bits covered by right half + 1 for '0' value
unsigned char ret=bitwidths.first;
T_uint_dec mask = (~(T_uint_dec)0)<<bitwidths.first;
while (bitwidths.second>1) {
if (value & mask) {
// at least 1 bit in left half is set
// -> move to left half of left half
bitwidths=std::make_pair(bitwidths.first/2,
(bitwidths.first/2)+(bitwidths.first%2));
ret += bitwidths.second; // increase log2 by half of mask width
mask &= (mask<<bitwidths.second); // shift mask to left half of current mask
} else {
// no bits in left half are set
// -> move to left half of right half
bitwidths=std::make_pair(bitwidths.second/2,
(bitwidths.second/2)+(bitwidths.second%2));
ret -= bitwidths.first; // decrease log2 by half of mask width
mask = (~mask) & (mask>>bitwidths.first);
}
}
return ret;
}
T_rational::T_index T_rational::log2(const T_uint_dec& value) {
return log2_v1(value);
return uint_log2<T_uint_dec,T_index>(value);
}

// Returns whether or not value was successfully encoded into bitstream
Expand Down
3 changes: 0 additions & 3 deletions Implementation/rational.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ class T_rational::bitstream {
std::bitset<8*(4)>& bits_enc;
T_index index;

static T_rational::T_uint_dec mask_v1(T_rational::T_index first_index, T_rational::T_index length);
static T_rational::T_uint_dec mask_v2(T_rational::T_index first_index, T_rational::T_index length);
static T_rational::T_uint_dec mask(T_rational::T_index first_index, T_rational::T_index length);
public:
bitstream(T_rational& rational);
bitstream(std::bitset<8*(4)>& bits);
Expand Down
15 changes: 15 additions & 0 deletions Implementation/rational_TEST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*/

void T_rational_TEST::test_bitstream_in_out() {
std::cout << "Test Name: Bitstream I/O (Single)" << std::endl;
std::cout << "Starting Test..." << std::endl;

// Variables
T_rational value_rational;
T_rational::bitstream bstream(value_rational);
Expand Down Expand Up @@ -59,6 +62,9 @@ void T_rational_TEST::test_bitstream_in_out() {
std::cout << "End Test." << std::endl;
}
void T_rational_TEST::test_bitstream_in_out_multi() {
std::cout << "Test Name: Bitstream I/O (Multi)" << std::endl;
std::cout << "Starting Test..." << std::endl;

// Variables
T_rational value_rational;
T_rational::bitstream bstream(value_rational);
Expand Down Expand Up @@ -144,6 +150,9 @@ unsigned char T_rational_TEST::encode_and_decode(T_rational& value_rational,
}

void T_rational_TEST::test_encoding_2nd_Nth_v1() {
std::cout << "Test Name: Encoding (v1)" << std::endl;
std::cout << "Starting Test..." << std::endl;

// Instantiate bitset used for encoding
T_rational value_rational;
std::cout << "Number of bits to store encoding = "
Expand Down Expand Up @@ -190,6 +199,9 @@ void T_rational_TEST::test_encoding_2nd_Nth_v1() {


void T_rational_TEST::test_encoding_2nd_Nth_v2() {
std::cout << "Test Name: Encoding (v2)" << std::endl;
std::cout << "Starting Test..." << std::endl;

// Declare variables
T_rational value_rational;
unsigned char failed_encoding = 0;
Expand Down Expand Up @@ -235,6 +247,9 @@ void T_rational_TEST::test_encoding_2nd_Nth_v2() {
}

void T_rational_TEST::test_decoding_2nd_Nth() {
std::cout << "Test Name: Decoding" << std::endl;
std::cout << "Starting Test..." << std::endl;

T_rational value_rational;
T_uint_dec in;
T_uint_dec out;
Expand Down
24 changes: 11 additions & 13 deletions Implementation/rational_TEST.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@

//class T_rational;

class T_rational_TEST {
public:
namespace T_rational_TEST {
typedef T_rational::T_uint_dec T_uint_dec;
typedef T_rational::T_index T_index;

// Test T_rational::bitstream member functions
static void test_bitstream_in_out();
static void test_bitstream_in_out_multi();

void test_bitstream_in_out();
void test_bitstream_in_out_multi();

// Test T_rational member functions
static unsigned char encode_and_decode(T_rational& value_rational,
T_rational::bitstream& bstream,
const T_uint_dec& value_in,
T_uint_dec& value_out);
static void test_encoding_2nd_Nth_v1();
static void test_encoding_2nd_Nth_v2();
static void test_decoding_2nd_Nth();
unsigned char encode_and_decode(T_rational& value_rational,
T_rational::bitstream& bstream,
const T_uint_dec& value_in,
T_uint_dec& value_out);
void test_encoding_2nd_Nth_v1();
void test_encoding_2nd_Nth_v2();
void test_decoding_2nd_Nth();

};

Expand Down

0 comments on commit c425260

Please sign in to comment.