0% found this document useful (0 votes)
87 views7 pages

GCC Optimization Effects on a.out Size

The size of the a.out file for a "Hello World" program depends on the optimization level used during compilation. With no optimization, the file size is 25.176 KB. Different optimization levels like -O1, -O2, and -O3 do not change the file size, but can reduce compilation time. The -Ofast option slightly increases the file size to 26.152 KB while further reducing compilation time. Since this is a small program without calculations or loops, most optimizations do not affect the file size.

Uploaded by

Thivin Anandh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views7 pages

GCC Optimization Effects on a.out Size

The size of the a.out file for a "Hello World" program depends on the optimization level used during compilation. With no optimization, the file size is 25.176 KB. Different optimization levels like -O1, -O2, and -O3 do not change the file size, but can reduce compilation time. The -Ofast option slightly increases the file size to 26.152 KB while further reducing compilation time. Since this is a small program without calculations or loops, most optimizations do not affect the file size.

Uploaded by

Thivin Anandh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1

QUESTION : 1 >> What is the Size of [Link] file for “Hello world” Program ?

The size of [Link] file depends on the File Contents and the “Type of Optimization” used while
Compiling the program on GCC Compiler. The Level of Optimization can be passed to GCC using
“-O” tag.

The File sizes and Execution times for various Optimization parameters are given below.

• Time for compilation is taken from ‘real time’ obtained by using “time gcc <filename>”

[Link] GCC Optimization Optimization [Link] File Size Compilation Time


Tag Description ( in KB ) ( In Milli Seconds )
0 Without -NA- 25,176 48
Optimization
1 -O0 Default Optimization ( 25,176 82
Or without any
Specific Optimizations
)
2 -O1 Less Code size and 25,176 70
Less execution Time
3 -O2 Further less code size 25,176 51
and less execution
time
4 -O3 Further less code size 25,176 50
and less execution
time
5 -Os Smaller code size 25,176 55
6 -Ofast Less execution time 26,152 68
more code size

Since this being a Very small problem with no calculations or loops invloved , The Size of the
[Link] file in most of the Optimisations remains constant except for the “-Ofast” tag.
2

QUESTION 2.62 >> Write a function int_shifts_are_arithmetic() that yields 1 when run on a machine that
uses arithmetic right shifts for int’s, and 0 otherwise. Your code should work on a machine with any word
size. Test your code on several machines.

FUNCTION PROGRAM:

int int_shifts_are_arithmetic()
{
int arbitary_negative_number = -1; /* Any Negative Number */

int value_of_shift = (sizeof(int)<<3); /* Size of Int in Bits */

/* Right Shifts the Arbitary Negative number to get the added bit as part (0 or 1, to the LSB
position */

arbitary_negative_number <<= value_of_shift;


/* Now Perform AND operation b/w the 'modified arbitary_negative_number' and the 'number
1 (0.......1)'' which will produce result of 1 (0......1) for Arithmetic right shift and 0(0.....0) for lorgical
right shift */

int result = arbitary_negative_number & 1;


return result;
}

QUESTION 2.69 >> Write code for a function with the following prototype: Mask with least signficant n
bits set to 1 Examples: n = 6 --> 0x2F, n = 17 --> 0x1FFFF .Assume 1 <= n <= w . int lower_one_mask(int
n);

FUNCTION PROGRAM :

int lower_one_mask(int n)
{
/* To get the output as ‘11111....10’ ( Equivalent of –2)in binary, This will eliminate the need to
Shift the bits by w characters when condition becomes n=w */

int number = (~0) - 1 ;


3

/* This creates a complementary number which will have o's in places of 1's of the required
answer */

number = number << (n-1);


/* NOT operator to change the values of 0's to 1's */

number = ~number;
return number;
}

QUESTION : 2.70 >> Write code for the function with the following prototype: Return 1 when x can be
represented as an n-bit, 2’s complement number; 0 otherwise Assume 1 <= n <= w .int fits_bits(int x, int
n);

PROGRAM:

int fits_bits(int x,int n)


{

n = n-1 ; /* to compensate for the Sign Bit that is present, mostly for negative integers */

/* If the integer can be Fit into n bits, It will convert x into 0....0 ( for +ve integer) or 1....11 ( for -ve
integer) */

x = x >> n-1;
/* Converts 0...0 or 1....1 into '0.....1') , if the given integer fits inside n bits*/

int answer = (x & 0) + 1;


/* returns '1' if the 'answer' is '1' else it returns '0' */

return (answer == 1);


}
4

QUESTION : 2.82 >> Consider numbers having a binary representation consisting of an infinite string of
the form 0.y y y y y y . . ., where y is a k-bit sequence. For example, the binary representation of 1 3 is
0.01010101 . . . (y = 01), while the representation of 5 1 is 0.001100110011 . . . (y = 0011).

A. Let Y = B2U k (y), that is, the number having binary representation y. Give a formula in terms of Y and
k for the value represented by the infinite string.

Hint: Consider the effect of shifting the binary point k positions to the right.

Y = B2U(y) So Y = B2U([Link])

Right Shifting the binary Decimal place by k positions is equivalent to multiplying the decimal
number by 2 raised to the power of K (2k )

(Y )10 = ([Link]...)2 ---- (1)

(2k * Y)10 = ([Link]...)2

(2k * Y)10 = (y + [Link]...)2

(2k * Y)10 = (y)2 + ([Link]..)2

(2k * Y)10 = (y)2 + Y10 From (1)

Y10 = (y)2 / ( 2k -1 )

The Decimal number which is fucntion of k and Y is given by : α = y / ( 2k– 1)

B : [Link] is the numeric value of the string for the following values of y?

(a) 101 (b) 0110 (c) 010011

(a) 101 --> (y)2 = 101 2 = 510 ;k=3

Y = 101 /( 23 - 1 ) = 5/7

(b) 0110 --> (y)2 = 01102 = 610 ;k=4

Y = 6/( 24 - 1 ) = 6/15

(c) 010011 --> (y)2 = 0100112 = 1910 ; k=6

Y = 19/( 26 - 1 ) = 19/63
5

QUESTION : 2.89 >> You have been assigned the task of writing a C function to compute a floating- point
representation of 2x . You decide that the best way to do this is to directly construct the IEEE single-
precision representation of the result. When x is too small, your routine will return 0.0. When x is too large,
it will return +∞. Fill in the blank portions of the code that follows to compute the correct result. Assume
the function u2f returns a floating-point value having an identical bit representation as its unsigned
argument.

float fpwr2(int x)
{
/* Result exponent and fraction */

printf("\n entered the loop");


unsigned exp,frac;

unsigned u;
if (x < -149 )
{
/* Too small. Return 0.0 For values greater than 2^-149 */

exp = 0;
frac = 0;

}
else if (x < -127)
{
/* Denormalized result for values including and between (2^-126 and 2^-149)*/

exp = 0;

int shift_value = x+149;


frac = 1 << shift_value;
}
else if (x < 128)

{
6

/* Normalized result for values including and between ( 2^-127 and 2^127 ) */

exp = x + 127;
frac = 0;
}

else
{
/* Too big. Return +oo */

exp = (~0); /* Equivalent to 255 */

frac = 0;

}
/* Pack exp and frac into 32 bits */

u = exp << 23 | frac;


printf("%u\n",u );
/* Return as float */

QUESTION : 2.90 >>Around 250 B.C., the Greek mathematician Archimedes proved that < π <71 7Had he
had access to a computer and the standard library <math.h>, he would have been able to determine that
the single-precision floating-point approximation of π has the hexadecimal representation 0x40490FDB.
Of course, all of these are just approximations, since π is not rational.

A. What is the fractional binary number denoted by this floating-point value?

0x40490FDB

HEXA TO BINARY :

Hexa Decimal : 4 0 9 9 0 F D B

Equivalent Binary : 0100 0000 0100 1001 0000 1111 1101 1011

Single precision IEEE Format

Sign ( Exponent ( Bit Size – 8 ) Mantissa ( Bit Size – 23)


Bit Size
–1)
0 1 0 0 0 0 0 0 0 10010010000111111011011
7

Positive Value = 128


NUmbe
r

Exponent = Value – Bias = 128 – 127 = 1

Floating Point Number = (-1)Sign * 2Exponent * Fraction

Binary Fraction of PI = 1.<Mantissa> * 2Exponent

= 1.10010010000111111011011 * 21

Binary Fraction of PI = (11.0010010000111111011011 )2 /* Given by 0x40490FDB */

B. What is the fractional binary representation of 22/7

22/7 = 3.14285714286

Binary Representation of this number

Decimal 3 = (11)2

Fraction = 0.14285714286 * 2 = 0.28571428571 --> 0

= 0.28571428571 * 2 = 0.57142857142 --> 0

.
.

(3.14285714286)10 = (11.00100100100100100101)2 /* Given by 22/7*/

C. At what bit position (relative to the binary point) do these two approximations to π diverge?

Binary Fraction of Pi : (1 1 . 0 0 1 0 0 1 0 0 0 0 1 1 1 1 1 1 0 1 1 0 1 1 )2

Binary Fraction of 22/7 : (1 1 . 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 1 )2

They Both Start to Diverge at the 9th Bit in the binary Fraction part

You might also like