Question
Q1. Check the following code segment: int min(int x, int y) { return x int max(int x, int y) { return x void incr(int *xp,
Q1. Check the following code segment:
int min(int x, int y) { return x
int max(int x, int y) { return x
void incr(int *xp, int v) { *xp += v; }
int square(int x) { return x*x; }
For the different versions of loops like below:
A.
for (i = min(x, y); i
t += square(i);
B.
for (i = max(x, y) - 1; i >= min(x, y); incr(&i, -1))
t += square(i);
C.
int low = min(x, y);
int high = max(x, y);
for (i = low; i
If the values of x and y are 10 and 50 respectively, fill in the following table indicating the number of times each of the four functions is called in code fragments AC:
Code | Min | Max | Incr | Sqr |
A | ||||
B | ||||
C |
Q2. Look at the following codes. Which will have a better performance as per your opinion?
void minmax1(int a[], int b[], int n) {
int i;
for (i = 0; i
if (a[i] > b[i]) {
int t = a[i];
a[i] = b[i];
b[i] = t;
} }}
B.
void minmax2(int a[], int b[], int n) {
int i;
for (i = 0; i
int min = a[i]
int max = a[i]
a[i] = min;
b[i] = max; }}
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
Q3. Convert the following code to use 4-way loop unrolling:
for (i = 0; i
sum = sum + udata[i] * vdata[i];
*dest = sum;
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
Q4. Locality is explained as:
Identify the localities in the example below:
int calculate_sum(int data[], int n)
{
sum = 0;
for (i = 0; i
sum += a[i];
return sum;
}
Which of the following code will have a better locality with respect to array a?
int sum_array_rows(int a[M][N])
{
int i, j, sum = 0;
for (i = 0; i
for (j = 0; j
sum += a[i][j];
return sum;
}
int sum_array_cols(int a[M][N])
{
int i, j, sum = 0;
for (j = 0; j
for (i = 0; i
sum += a[i][j];
return sum;
}
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
Q5. What is memory aliasing? How is it an optimization blocker?
Q6. The three functions as given below perform the same operation with varying degrees of spatial locality. Rank-order the functions with respect to the spatial locality enjoyed by each. Explain how you arrived at your ranking.
Q7.Explain three main common problems in programming that restricts the optimization compilers to fully optimize the code.
(a) An array of structs 1 #define N 1000 typedef struct t int vel[3]; int acc[3]: 6 point; s point p UN] void clear3(point p, int n) (b) The clear 1 function void clear2(point p, int n) void cleari(point p, int n) int i, j; 3int i, j int i j for (j=0; JStep 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