Saturday, September 26, 2015

C PROGRAM FOR LOOP UNROLLING

/* Loop unrolling, also known as loop unwinding, is a loop transformation technique that 

attempts to optimize a program's execution speed at the expense of its binary size, which is an 

approach known as the space-time tradeoff. The transformation can be undertaken manually by 

the programmer or by an optimizing compiler. */

#include<stdio.h>
#include<conio.h>
void main()
{
unsigned int n;
int x;
char ch;
clrscr();
printf("\nEnter N\n");
scanf("%u",&n);
printf("\n1. Loop Roll\n2. Loop UnRoll\n");
printf("\nEnter ur choice\n");
scanf(" %c",&ch);
switch(ch)
{
case '1':
x=countbit1(n);
printf("\nLoop Roll: Count of  1's    :  %d" ,x);
break;
case '2':
x=countbit2(n);
printf("\nLoop UnRoll:  Count of 1's  :  %d" ,x);
break;
default:
printf("\n Wrong Choice\n");

}
getch();
}
int countbit1(unsigned int n)
{
    int bits = 0,i=0;
    while (n != 0)
    {
if (n & 1) bits++;
n >>= 1;
i++;
    }
    printf("\n no of iterations  %d",i);
    return bits;
}
int countbit2(unsigned int n)
{
    int bits = 0,i=0;
    while (n != 0)
    {
if (n & 1) bits++;
if (n & 2) bits++;
if (n & 4) bits++;
if (n & 8) bits++;
n >>= 4;
i++;
    }
    printf("\n no of iterations  %d",i);
    return bits;
}

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home