Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

plz solve this proble TT The library function memset has the following prototype

ID: 3848339 • Letter: P

Question

plz solve this proble TT

The library function memset has the following prototype void enemset (void es int c, size_t n) This function fills n bytes of the memory area starting at s with copies of the loworder byte of C. For example, itcan be used to zero out a region of memory by giving argument 0 for C, but other values are possible. The following is a straightforward implementation of memset 1 Basic implementation of memset 2 void *basic memset (void *s int c, size t n) Size t cnt 0; unsigned char schar s while (cnt n) schar++ (unsigned char C; ent return 10 11 Implement a more efficient version of the function by using a word of data type unsigned long to pack eight copies of c, and then step through the region using word-level writes. You might find it helpful to do additional loop unrolling as well. On our reference machine, we were able to reduce the CPE from 1.00 for the straightforward implementation t 0.127. That is, the program is able to write 8 bytes every clock cycle. Here are some additional guidelines. To ensure portability, let K denote the value of sizeof (unsigned long) for the machine on which you run your program You may not call any library functions. Your code should work for arbitrary values of n, including when it is not a multiple of K. You can do this in a manner similar to the way we finish the last few iterations with loop unrolling You should write your code so that it will compile and run correctly on any machine regardless of the value of K. Make use of the operation sizeof to do this On some machines, unaligned writes can be much slower than aligned ones. (On some non- x86 machines, they can even cause segmentation faults.) Write your code so that it starts with byte-level writes until the destination address is a multiple of K, then do word-level writes, and

Explanation / Answer

The question asks for most eficient way, so the code becomes little complex.

void *memset(void *s, int c, size_t n) {
   size.t cnt = 0;
   unsigned char *schar = s;
   unsigned long *slong;
   unsigned long vlong
   while (cnt < n) {
       *schar++ = (unsigned char) c;
       cnt++;
       if (cnt == 8) { //after firsr 8 bytes are coppied
           slong = s;
           vlong = *slong; //store the first 8 bytes in unsigned long format
           slong++; //now increment the pointer to point to next block of 8 bytes
           while (n - cnt >= 8) { //ensures that it doesnot overshoot the boundary
               *slong++ = vlong; //copy 8 bytes at a time
               cnt += 8; //update counter
           } //end of copying 8byte blocks
           schar = slong; //update schar pointer so remaining bits (less than 8 remaining bytes)
       } //end of if
   }
   return s;
}

I hope the code is clear to you. Incase it is not, please comment below. I shall try my best to explain them to u