Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The.library function memset has the following prototype: void *memset(void *s, int c, size_t n); This function fills n bytes of the memory area starting at

The.library function memset has the following prototype:

void *memset(void *s, 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, it can 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 *I

2 void *basic_memset(void *s, int c, size_t n)

3 {

4 size_t cnt = O;

5 unsigned char *Behar= s;

6 while Cent < n) {

*schar++ = (unsigned char) c; 7

8 cnt++;

9 }

10 return s;

11 }

Implement a more efficient version of the function by using a word of data

type unsigned long t,o 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 im'plementation to 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 o~ size of (unsigned long) for the machine on which you run your program.

You may not call any library functiof)S.

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 ~o 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 write~ can be much ~lower 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 write~; and then (if necessary) finish

with byte-level writes.

Beware of the case where cnt is small enough that the upper bounds on

some of the loops become negative. With expressions involving the sizeof

operator, the testing may be performed with unsigned arithmetic. (See Section

2.2.8 and Problem 2.72.)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What would I do next and why?

Answered: 1 week ago