Home / utk / cs130 / fa17 / lab11 / float.c
Directory Listing
calc.c
float.c
writeup.pdf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//float.c
//Floating point lab
//Stephen Marz
//20 Oct 2017

/////////////////////////////////////////////////////////////////
//
// DO NOT MODIFY THIS FILE (float.c)
// Compile using: aarch64-linux-gnu-gcc -o float float.c float.S
//
/////////////////////////////////////////////////////////////////

#include <stdio.h>

//Write these functions in float.S
int GetSign(int val);
int GetExponent(int val);
int GetFraction(int val);
int GetNorm(int fraction);
int GetRaise2(int exponent);

int main(int argc, char *argv[])
{
	int sign;
	int fraction;
	int exponent;
	int norm;
	int val;
	float fval;

	if (argc < 2) {
		printf("Usage: %s <value>\n", argv[0]);
		return -1;
	}

	sscanf(argv[1], "%f", &val);
	sign = GetSign(val);
	exponent = GetExponent(val);
	fraction = GetFraction(val);
	norm = GetNorm(fraction);

	fval = *(float*)&norm *
		(exponent < 0 ?
			1.0f / GetRaise2(-exponent) :
			GetRaise2(exponent)
		) *
		(sign == 1 ? -1 : 1);

	printf("0x%08x : %c%.6le x 2^(%d) = %.6f (should be %.6f)\n",
		val,
		(sign == 1 ? '-' : '+'),
		*(float*)&norm,
		exponent,
		fval,
		*(float*)&val
		);

	return 0;
}