Question
Hi. This code needs to be changed. It is not working. Can you please make it output the given solution (prompt and code are given
Hi. This code needs to be changed. It is not working. Can you please make it output the given solution (prompt and code are given below). ! This is inline assembly. Intel syntax. (Please do correctly and quickly, Will def leave thumbs up!). Thank you!
PROMPT is down below.
MAIN. CPP :
#include#include #include "MyFloat.h" using namespace std; int main(int argc, char** argv){ float f1, f2; //float fres; MyFloat mfres; cout.precision(1000); if(argc != 4){ cout"Usage: " " float_a +/- float_b" else{ f1 = strtof(argv[1],NULL); f2 = strtof(argv[3],NULL); MyFloat mf1(f1); MyFloat mf2(f2); cout ' '' ' if(argv[2][0] == '+'){ //addition //fres = f1 + f2; mfres = mf1.operator+(mf2); //cout cout "My Add: " else if(argv[2][0] == '-'){ //subtraction //fres = f1 - f2; mfres = mf1 - mf2; //cout cout "My Subtraction: " else{ cout "Only + and - are supported but received operator: " //cout } return 0; } MYFLOAT.CPP:
#include "MyFloat.h" MyFloat::MyFloat(){ sign = 0; exponent = 0; mantissa = 0; } MyFloat::MyFloat(float f){ unpackFloat(f); } MyFloat::MyFloat(const MyFloat & rhs){ sign = rhs.sign; exponent = rhs.exponent; mantissa = rhs.mantissa; } // output - completed already ostream& operatorconst MyFloat &f){ strm return strm; } // Comparison bool MyFloat::operator==(const float rhs) const{ return (this->packFloat() == rhs); } // Addition MyFloat MyFloat::operator+(const MyFloat& rhs) const{ MyFloat temp(rhs); MyFloat this1(*this); int diff = this1.exponent - rhs.exponent; if (diff return rhs; else if (diff > 23) return *(this); else if (this1.sign == 0) { if (rhs.sign == 0){ if (diff > 0 ) { temp.mantissa = temp.mantissa>>diff; temp.exponent = this1.exponent; } else if (diff >diff; this1.exponent = temp.exponent; } else temp.exponent += 1; temp.mantissa += this1.mantissa; return temp; } else { if (diff > 0 ) { temp.mantissa = temp.mantissa>>diff; temp.exponent = this1.exponent; } else if (diff >diff; this1.exponent = temp.exponent; this1.sign = 1; } else { temp.exponent -= 1; } this1.mantissa -= temp.mantissa; return temp; } } else { if (rhs.sign == 0){ if (diff > 0 ) { temp.mantissa = temp.mantissa>>diff; temp.exponent = this1.exponent; } else if (diff >diff; this1.exponent = temp.exponent; } else temp.exponent -= 1; temp.mantissa -= this1.mantissa; return temp; } else { if (diff > 0 ) { temp.mantissa = temp.mantissa>>diff; temp.exponent = this1.exponent; } else if (diff >diff; this1.exponent = temp.exponent; } else temp.exponent += 1; temp.mantissa += this1.mantissa; return temp; } } } // Subtraction MyFloat MyFloat::operator-(const MyFloat& rhs) const { MyFloat temp(rhs); temp.sign ^= 1; return (temp + *(this)); } void MyFloat::unpackFloat(float f) { __asm__ ( "movl %[f], %[sign];" "shr $31, %[sign];" "movl %[f], %[mantissa];" "andl 0x007FFFFF, %[mantissa];" "orl 0x0080000, %[mantissa];" "movl %[f], %[exponent];" "shrl $22, %[exponent];" "andl 0x000000FF, %[exponent];": // Code [sign] "=r" (sign), [mantissa] "=r" (mantissa), [exponent] "=r" (exponent): // Output [f] "r" (f): // Input "cc" // Clobber ); }// unpackFloat float MyFloat::packFloat() const{ // returns the floating point number represented by this float f = 0; __asm__( "movl %[mantissa], %[f];" "andl 0x01800000, %[f];" "shl $31, %[sign];" "andl %[sign], %[f];" "shl $23, %[exponent];" "andl %[exponent], %[f];": // Code [f] "=r" (f): // Output [sign] "r" (sign), [mantissa] "r" (mantissa), [exponent] "r" (exponent): // Input // Clobber ); return f; }// packFloat MYFLOAT.h:
#ifndef MY_FLOAT_H #define MY_FLOAT_H #includeusing namespace std; class MyFloat{ public: //constructors MyFloat(); MyFloat(float f); MyFloat(const MyFloat & rhs); virtual ~MyFloat() {}; //output friend ostream& operatorconst MyFloat& f); //addition MyFloat operator+(const MyFloat& rhs) const; //subtraction MyFloat operator-(const MyFloat& rhs) const; //comparison bool operator==(const float rhs) const; private: unsigned int sign; unsigned int exponent; unsigned int mantissa; void unpackFloat(float f); float packFloat() const; }; #endif
MAKEFILE:
fpArithmetic.out: MyFloat.o main.o
g++ -g -Wall -m32 -std=c++11 -o fpArithmetic.out MyFloat.o main.o
main.o: main.cpp MyFloat.h
g++ -g -Wall -m32 -std=c++11 -c -o main.o main.cpp
MyFloat.o: MyFloat.h MyFloat.cpp
g++ -g -Wall -m32 -std=c++11 -c -o MyFloat.o MyFloat.cpp
PROMPT:
clean:
rm main.o MyFloat.o main
Print all real numbers to two decimal places unless otherwise stated .The examples provided in the prompts do not represent all possible input you can receive. All inputs in the examples in the prompt are underlined You don't have to make anything underlined it is just there to help you differentiate between what you are supposed to print and what is being given to your program .If you have questions please post them on Piazza For this assignment you will be implementing floating point add and subtract without using the hardware's floating point or double precision add. This will give you a better understanding of how difficult they are to work with and a higher appreciation of the hardware for doing this for you. You will be turning in a file called MyFloat.cpp and its associated header file, MyFloat.h, that implements your own floating point number. This object should support both + and -. 1. You may not use floating point or double precision add in your solution. This means that 1. you should not have the following in your program 1. float x,y; 2. MyFloat should represent a float using three unsigned integers: sign, exponent, and mantissa. MyFloat must have the following private methods defined on it. These functions must be implemented using inline assembly. 1. void unpackFloat(float f); 3. 1. Given a float f this function should set sign, exponent, and mantissa to the appropriate values based on the value of f. 2. float packFloat) const; 3. MyFloat must have the following public functions defined on it 1. This function should return the floating point representation of MyFloat You may declare and initialize variables to a constant in C as well as return a value but you should do no other calculations. 4. MyFloat operator+(const MyFloat& rhs) const; 1. 1. This function should add this to rhs and return the result of the addition When adding the two numbers, the maximum amount of precision must be maintained 2. Before doing the addition you should restore the leading 1. This means that the mantissa will end up taking 24 bits. Since you are adding two 24 bit numbers together the result could take up to 25 bits Be careful when shifting. Since the numbers are 32 bits, the maximum amount 1. 2. 3. Print all real numbers to two decimal places unless otherwise stated .The examples provided in the prompts do not represent all possible input you can receive. All inputs in the examples in the prompt are underlined You don't have to make anything underlined it is just there to help you differentiate between what you are supposed to print and what is being given to your program .If you have questions please post them on Piazza For this assignment you will be implementing floating point add and subtract without using the hardware's floating point or double precision add. This will give you a better understanding of how difficult they are to work with and a higher appreciation of the hardware for doing this for you. You will be turning in a file called MyFloat.cpp and its associated header file, MyFloat.h, that implements your own floating point number. This object should support both + and -. 1. You may not use floating point or double precision add in your solution. This means that 1. you should not have the following in your program 1. float x,y; 2. MyFloat should represent a float using three unsigned integers: sign, exponent, and mantissa. MyFloat must have the following private methods defined on it. These functions must be implemented using inline assembly. 1. void unpackFloat(float f); 3. 1. Given a float f this function should set sign, exponent, and mantissa to the appropriate values based on the value of f. 2. float packFloat) const; 3. MyFloat must have the following public functions defined on it 1. This function should return the floating point representation of MyFloat You may declare and initialize variables to a constant in C as well as return a value but you should do no other calculations. 4. MyFloat operator+(const MyFloat& rhs) const; 1. 1. This function should add this to rhs and return the result of the addition When adding the two numbers, the maximum amount of precision must be maintained 2. Before doing the addition you should restore the leading 1. This means that the mantissa will end up taking 24 bits. Since you are adding two 24 bit numbers together the result could take up to 25 bits Be careful when shifting. Since the numbers are 32 bits, the maximum amount 1. 2. 3Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started