#include "Fixed.hpp"

/*
 * functions to print numbers in binary
 * for the float, found help from stackoverflow :
 * https://stackoverflow.com/questions/474007/floating-point-to-binary-valuec
 */

std::string printBitsInt(int num)
{
	int i = 0;

	for (unsigned int mask = 1U << (sizeof(int) *8 -1); mask; mask >>= 1)
	{
		std::cout << ((num & mask) != 0);
		i++;
		if (i == 1 || i == 9 || i == 24)
			std::cout << ' ';
	}
	return "";
}

std::string	printBitsFloat(float num)
{
	int	*p = (int *)&num;
	int i = 0;

	for (unsigned int mask = 1U << (sizeof(float) *8 -1); mask; mask >>= 1)
	{
		std::cout << ((*p & mask) != 0);
		i++;
		if (i == 1 || i == 9 || i == 24)
			std::cout << ' ';
	}
	return "";
}

/*
 * statics variables initialisation
 *
 * for MAX integer :
 * 00000000 01111111 11111111 11111111 ( 8388607) (-1U >> (this->_frac +1))
 *              <= ... >=
 * 11111111 10000000 00000000 00000000 (-8388608)
 *
 */

int const Fixed::_frac = 8;
int const Fixed::_max = -1U >> (_frac +1);

/*
 * default constructor / copy constructor / destructor
 */

Fixed::Fixed() : _value(0) {
//	std::cout << "Default constructor called" << '\n';
	return;
}

Fixed::Fixed(Fixed const & src) {
//	std::cout << "Copy constructor called" << '\n';
	*this = src;
	return;
}

Fixed::~Fixed( void ) {
//	std::cout << "Destructor called" << '\n';
	return;
}

/*
 * int and float constructors
 */

Fixed::Fixed(int integer) {
//	std::cout << "Int constructor called" << '\n';

	if (integer < ~this->_max || integer > this->_max)
	{
		std::cout << "error: integer out of range" << '\n';
		return;
	}
	std::cout << "integer : " << printBitsInt(integer) << " (" << integer << ")\n";
	this->_value = integer << this->_frac;
	std::cout << "_value  : " << printBitsInt(this->_value) << " (" << this->_value << ")\n";
}

Fixed::Fixed(float const floater) {
//	std::cout << "Float constructor called" << '\n';

	if (floater < ~this->_max || floater > this->_max)
	{
		std::cout << "error: float out of range" << '\n';
		return;
	}
	std::cout << "floater : " << printBitsFloat(floater) << " (" << floater << ")\n";
	this->_value = floater * (1 << this->_frac);
	std::cout << "_value  : " << printBitsInt(this->_value) << " (" << this->_value << ")\n";

	this->_value = *((int *)&floater); // cast floater into int
std::cout << "_value  : " << printBitsInt(this->_value) << " (" << this->_value << ")\n";

	int sign = this->_value & (1 << 31); // set most left bits to sign
std::cout << "sign    : " << printBitsInt(sign) << " (" << sign << ")\n";

	int exponent = ((unsigned int)(this->_value << 1) >> 24) - 127; // extract exponent value
std::cout << "exponent: " << printBitsInt(exponent) << " (" << exponent << ")\n";

	int decimal = (this->_value << (9));
std::cout << "decimal : " << printBitsInt(decimal) << " (" << decimal << ")\n";
	decimal = (this->_value << (9 + exponent));
std::cout << "decimal : " << printBitsInt(decimal) << " (" << decimal << ")\n";

	int integer = (this->_value << 8) | (1 << 31); // add left 1 to integer part
std::cout << "left 1  : " << printBitsInt(integer) << " (" << integer << ")\n";
	if (sign != 0)
		integer = ~integer; // reverse for negatif
std::cout << "reverse : " << printBitsInt(integer) << " (" << integer << ")\n";
	integer = ((unsigned int)integer >> 1) | sign; // add sign to integer part
std::cout << "left +/-: " << printBitsInt(integer) << " (" << integer << ")\n";
	integer >>= (30 - exponent); // extract integer part
std::cout << "integer : " << printBitsInt(integer) << " (" << integer << ")\n";

//	this->_value = (unsigned int)(this->_value << 8); // shift to left to erase exponent part
//	this->_value |= 1 << 31; // add the missing left 1
//std::cout << "to left : " << printBitsInt(this->_value) << " (" << this->_value << ")\n";
//	this->_value = (unsigned int)this->_value >> 1;
//std::cout << "to right: " << printBitsInt(this->_value) << " (" << this->_value << ")\n";
//	this->_value |= sign;
//std::cout << "sign    : " << printBitsInt(this->_value) << " (" << this->_value << ")\n";
//	this->_value = this->_value >> (30 - exponent - this->_frac); // shift to right untill fixed point
//std::cout << "_value  : " << printBitsInt(this->_value) << " (" << this->_value << ")\n";
////	int decimal = (unsigned int)(this->_value << (24)) >> 24; // extract decimal
////std::cout << "decimal : " << printBitsInt(decimal) << " (" << decimal << ")\n";
//	this->_value >>= this->_frac;
//std::cout << "int valu: " << printBitsInt(this->_value) << " (" << this->_value << ")\n";
////	this->_value += (unsigned int)sign >> 31;
////std::cout << "val sign: " << printBitsInt(this->_value) << " (" << this->_value << ")\n";
//	this->_value = (this->_value << this->_frac);
//std::cout << "val sign: " << printBitsInt(this->_value) << " (" << this->_value << ")\n";

}

/*
 * assignement operator
 */

Fixed &	Fixed::operator=( Fixed const & rhs ) {
//	std::cout << "Copy assignment operator called" << '\n';
	if ( this != &rhs )
		this->_value = rhs.getRawBits();

	return *this;
}

/*
 * other functions
 */

int		Fixed::getRawBits( void ) const {
//	std::cout << "getRawBits member function called" << '\n';
	return this->_value;
}

void	Fixed::setRawBits( int const raw ) {
	this->_value = raw;
}

void	Fixed::toFloat( void ) const {}
int		Fixed::toInt( void ) const {
	return 0;
}


