Fall 2019 CS 367 Project 1: Floating Point Representation

Verified

Added on  2022/09/28

|11
|1626
|27
Project
AI Summary
Document Page
Floating Point Representation
Student Name –
Student ID –
The IEEE standard can be used for the floating point representation. The size of exponents as
well as the fraction fields can be different. Here, the coding is done for conversion of the
floating point numbers into the bit – level representation. The addition and multiplication
operations have also been performed on the floating point values.
The 15 – bit floating point representation has been used here – 5 bits are used for exponent
and 9 bits are used for the fraction part. The function ‘ cp ’ converts a float value ( in C
language ) into the 15 – bit floating point representation. The 15 bits represent the lower 15
bits of the complete 32 bit number. The function ‘ gf ’ is used to convert the floating point
representation into float value ( in C language ). The special cases like infinity , underflow,
rounding of the values etc. has been taken into consideration.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "fp.h"
/* If the input value is -0.0, then 1 is returned else, 0 is returned*/
static int inz(float u) {
return (u == 0.0 && ((1.f / val) < 0));
}
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
/* The input is the float value and the output is the integer representation for the input. It can
be done using division or multiplication by factor 2 */
int cp(float u) {
if (inz(u) == 1) //To check if it is -0
{
return 0;
}
if(u == 0) // To check if it is 0
{
return 0;
}
int s; // To store the sign bit
if (u < 0) {
s = 1; // The value = 1 incase of negative
u = u * (-1); //conversion to positive
}
else {
s = 0; //The value = 0 incase of positive
Document Page
}
int b1 = 15; // The value of bias is 15 as 5 bit used
int c1 = 0; // To store the number of times shift occurs
if(u < 1.0)
{
while (u < 1.0)
{
u *= 2; //multiplication by 2 incase value < 1.0
c1 = c1 + 1;
}
c1 *= (-1);
}
else {
while(u > 2.0) {
u /= 2; //division by 2 incase value > 2.0
c1 += 1;
}
}
int xyz = b1 + c1; //calculates exponential
int f1 = 0;
float nv = u - 1.0;
nv = nv * 512; // numerator value for fraction
f1 = floor(nv);
if(xyz > 31) { // Infinity check
Document Page
xyz = 31;
f1 = 0;
}
s = s << 14; // Shifting operation
xyz = xyz & 0x1f;
xyz = xyz << 9;
f1 = f1 & 0x1ff;
int a1 = s | xyz | f1; //To store the final value
return a1;
}
/* The input is the integer representation and the output is the float value */
float gf(int u) {
int s = u >> 14; // Shifting operation for original value
int xyz = u & 0x3e00;
xyz = xyz >> 9;
if (xyz == 0) {
return 0;
}
int f1 = u & 0x1ff;
if(xyz == 0x31 && f1 == 0x0) { // Infinity checking
if(s==0) {
return INFINITY;
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
}
else {
return -INFINITY;
}
}
int e = xyz - 15; // to calculate e from xyz
float u1 = (float) f1 / 512; // to get the value of mantissa
u1 = u1 + 1.0;
u1 = u1 * pow(2, e); //To calculate the float value
if (s == 1) {
u1 *= (-1);
}
return u1;
}
/* The input has 2 integer representations and the output is the multiplication result in integer
form*/
int mv(int sc1, int sc2) {
int s1 = sc1 >> 14; // To find the sign bit
int s2 = sc2 >> 14; // To find the sign bit
int s = s1 ^ s2; // To calculate the sign bit for multiplying
Document Page
int xyz1 = sc1 & 0x3e00; // To find exponential
xyz1 = xyz1 >> 9;
int xyz2 = sc2 & 0x3e00; // To finds exponential
xyz2 = xyz2 >> 9;
int e1 = xyz1 - 15;
int e2 = xyz2 - 15;
int e = e1 + e2; //find value of e for multiplication
int f11 = sc1 & 0x1ff; //find fraction
float m1 = (float) f11 / 512; //find mantissa
m1 += 1.0;
int f12 = sc2 & 0x1ff; //finds frac
float m2 = (float) f12 / 512; //find mantissa
m2 += 1.0;
float m = (m1 * m2); //find mantissa for multiplying
int c1 = 0; // Set mantissa in the range
if(m < 1.0) {
while (m < 1.0) {
m = m * 2;
c1++;
}
c1 *= (-1);
}
else if(m > 2.0) {
while (m > 2.0) {
m = m / 2;
Document Page
c1++;
}
}
m = m - 1.0;
m = m * 512; //find new fraction
int f1 = floor(m);
int xyz = 15 + e + c1; //get new value of xyz
if(xyz > 31) { // Infinity checking
xyz = 31;
f1 = 0;
}
s = s << 14;
xyz = xyz & 0x1f;
xyz = xyz << 9;
f1 = f1 & 0x1ff;
int a1 = s | xyz | f1;
return a1;
}
/* The input is 2 integer representations and the output is the addition result in integer
representation*/
int add_vals(int sc1, int sc2) {
int s1 = sc1 >> 14; // for sign bit
int s2 = sc2 >> 14; // for sign bit
int xyz1 = sc1 & 0x3e00; //for xyz
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
xyz1 = xyz1 >> 9;
int xyz2 = sc2 & 0x3e00; //for xyz
xyz2 = xyz2 >> 9;
int e1 = xyz1 - 15; //find e1
int e2 = xyz2 - 15; //find e2
int tochange;
int loop;
int f11 = sc1 & 0x1ff; //gets fraction value
float m1 = (float) f11 / 512; //find the value of mantissa
m1 += 1.0;
if(s1 == 1) {
m1 *= (-1);
}
int f12 = sc2 & 0x1ff; //gets fraction value
float m2 = (float) f12 / 512; //find the value of mantissa
m2 += 1.0;
if (s2 == 1) {
m2 *= (-1);
}
float sm, bg;
int e;
if (e1 > e2) { //find the bigger e
loop = e1 - e2;
bg = m1;
Document Page
sm = m2; // to stores the mantissa value for change
e = e1;
}
else if(e2 > e1) {
loop = e2 - e1; //find the difference
bg = m2;
sm = m1; //to store the mantissa value for change
e = e2;
}
int i;
float m;
if(e1!=e2) {
for(i = 0; i < loop; i++) {
sm = sm / 2;
}
m = sm + bg; // To shift the mantissa value
}
else {
m = m1 + m2;
}
if ( m < 0) {
m = m + 1.0;
}
Document Page
else if(m > 0) {
m = m - 1.0;
}
m = m * 512;
int f1, s;
if ( m < 0 ){
f1 = ceil(m); // to convert near to 0
s = 1;
}
else {
f1 = floor(m); // to convert near to 0
s = 0;
}
if ( f1 < 0) {
f1 *= (-1);
}
int xyz = 15 + e; //find xyz
if(f1 == 0) {
xyz = 0;
}
if(xyz > 31) { // Infinity check
xyz = 31;
f1 = 0;
}
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
s = s << 14;
xyz = xyz & 0x1f;
xyz = xyz << 9;
f1 = f1 & 0x1ff;
int a1 = s | xyz | f1;
return a1;
}
chevron_up_icon
1 out of 11
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]