20135223 He Weizin-Home Work summary

Source: Internet
Author: User

Chapter II Homework(At that time when the examination of the students have chosen this problem, repetition does not count, not registered)

Chapter III Homework (has been to the teacher's office to check)

3.54 parsing:
This problem is relatively simple, as long as the seat, you can write the corresponding C language code
int decode2 (int x,int y,int z)
{
int R;
Z-=y;
R=z;
r<<=15;
r>>=15;
Return r* (Z^X);
}

3.56 parsing:

(1) The definition of C code function can be guessed%esi=x,%ebx=n first;
The use of mask initialized with a cyclic expression by result, mask can be%edi=result,%edx as mask, that is:

Register variable
ESI X
EBX N
EDI Result
EdX Mask


(2)
Convert decimal number $1431655765 to hexadecimal number
result:0x55555555
Convert decimal number $-21474836648 to hexadecimal number
mask:0x80000000

(3)
Test%edx,%edx
Jne. L2
You can conclude that the loop condition expression is
Mask!=0

(4)

Assembly Code Line Tenth: (SHRL%cl,%ecx) you can see that the logical right shifted n bits

(5)

Assembly code 7th and eighth lines can be
Result ^= (Mask & x)

(6)

int loop (int x, int n)

{
int result = 0x55555555;//or $1431655765
int mask;
for (mask = 1<<31; mask! = 0; mask = ((unsigned) mask>>n)
{
Result ^= (mask & x);
}
return result;
}

3.62 parsing:
(1) m value: M = 76/4 = 19
(2) by Cmpl =%EDI,%ECX;
JL. L3;
This means: Compare the values of%edi and%ECX, if (%ecx-%edi) < 0, will continue into the L3 cycle;
Depending on the internal loop condition expression for the code given by the topic for (;j<i;) (that is, the loop condition is (j-i) <0), you can determine%edi save i,%ecx save J.
(3)
int transpose (int M, int a[m][m])
{
int i,j;
for (i=0; i<m; ++i)
{
int *a = &A[i][0];
int *b = &A[0][i];
for (j=0; j<i; ++j)
{
int t = *a;
*a = *b;
*b = t;
++a;
b + = M;
}
}
}

Sixth. Homework (Partner in Blog Park Group registered) I. Homework 6.36 (20135203&&20135223)

(as the question 6.36 and 6.35 basic types, just cache data byte is not the same, I directly to the 6.35 topic modified as 6.36 topics)

Consider the following matrix transpose function:

typedefint array[4][4];void transpose2(array dst,array src){int i,j;for(i=0;i<4;i++){for(j=0;j<4;j++){dst[i][j]=src[j][i];}}}

Suppose this code runs on a machine with the following properties:

    • sizeof (int) ==4.
    • The array src starts at address 0, and the array DST starts with address 64 (decimal).
    • There is only one L1 data cache, which is directly mapped, write-only, write-allocated, and the block size is 16 bytes.
    • For a cache with a total size of 128 data bytes, the initial is empty.
    • Access to the SRC and DST arrays is the only source of read and write misses, respectively.

For each row and col, indicate whether access to Src[row][col] and Dst[row][col] is a hit (h) or a Miss (m).

Analysis:

(1) For write-allocated caches, each write misses the need to read the data into the cache.

The cache has only 2 groups, src[0] src[2] corresponding group 0,src[1] src[3] corresponds to group 1.

Similarly, dst[0] dst[2] corresponds to group 0,dst[1] dst[3] corresponds to group 1.

(2) The cache can fully accommodate the next two arrays, so only a cold miss will occur.

Answer:

Ii. Homework 6.46 (20135223 (2) &&20135215 (2))

Answer:

void transpose (int *dst,int *src,int Dim)
{
int i,j;
int IM,JM;
for (i=0;i<dim-3;i+=4)
{
im = I*dim;
for (j=0;j<dim-3;j+=4)
{
JM = J*dim;

DST[JM+I]=SRC[IM+J];
DST[JM+I+1]=SRC[IM+DIM+J];
DST[JM+I+2]=SRC[IM+2*DIM+J];
DST[JM+I+3]=SRC[IM+3*DIM+J];
DST[JM+DIM+I]=SRC[IM+J+1];
DST[JM+DIM+I+1]=SRC[IM+DIM+J+1];
DST[JM+DIM+I+2] =src[im+2*dim+j+1];
DST[JM+DIM+I+3]=SRC[IM+3*DIM+J+1];
DST[JM+2*DIM+I]=SRC[IM+J+2];
DST[JM+2*DIM+I+1]=SRC[IM+DIM+J+2];
DST[JM+2*DIM+I+2]=SRC[IM+2*DIM+J+2];
DST[JM+2*DIM+I+3]=SRC[IM+3*DIM+J+2];
DST[JM+3*DIM+I]=SRC[IM+DIM+J+3];
DST[JM+3*DIM+I+1]=SRC[IM+DIM+J+3];
DST[JM+3*DIM+I+2]=SRC[IM+2*DIM+J+3];
DST[JM+3*DIM+I+3]=SRC[IM+3*DIM+J+3];
}
}
for (; i<dim;i++)
for (; j<dim;j++)
DST[J*DIM+I]=SRC[I*DIM+J];
}

Array testing with 10000*10000 (Linux needs to cancel the stack size limit before running such a large array)

The program on the book requires an average of 9.301s

Optimized post-evaluation takes 3.83 seconds

Tenth Chapter Homework (blog Park Group registered)

Analysis and Solution:

Here it should be indicated that the input is redirected to the Foo.txt, however 3 this descriptor is not present.

Description Foo.txt does not have a separate descriptor 3. So the code that the shell executes should look like this:

Code:

20135223 He Weizin-Home Work summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.