Hjkl
Hjkl

Reputation: 59

Why do memcpy() and other similar functions use assembly?

I took a look at the parts of the code behind memcpy and other functions (memset, memmove, ...) and it seems to be a lot, and a lot of assembly code.

Other stackoverflow questions on this topic mention that a reason for that may be because it contains different code for different CPU architectures.

I have personally written my own memcpy/memset functions with very few lines of C++ code and in 1 million iterations with time measured with chrono, I consistently get better times.

So the question is, why did the programmers not just write the code in C/C++ and let the compiler interpret and optimize it how it thinks is best? Why so much assembly code?

Upvotes: 1

Views: 878

Answers (5)

Thomas Matthews
Thomas Matthews

Reputation: 57728

The memcpy and memset as well as other function, are written in assembly to take advantage of processor specific instructions.

For example, the ARM processor has a function that can load multiple registers from successive locations with one instruction. There is also the store multiple instruction that stores multiple registers into successive locations. The Intel x86 has block read and write instructions.

The assembly language allows for copying 4 8-bit bytes using a single 32-bit register.

Some processors allow for conditional execution of instructions, which helps when rolling out loops.

I've written optimized memcpy and memset functions for various processors. I've also spent a lot of time arguing (discussing) C and C++ "best" implementations with compilers. It's a little difficult using C or C++ to try and get the compiler to use the processor instructions you want it to.

Upvotes: 1

AmirSina Mashayekh
AmirSina Mashayekh

Reputation: 520

  1. Compiler usually generates some unnecessary code (compared to hand written assembly) even on full optimization level. This wastes memory space which is not good specially on embedded systems and reduces performance.

  2. Are you sure your custom codes are complete and flawless? I don't think so; because when you are writing assembly, you have full control on everything, but when you compile a code, there is a possibility that compiler generates something that you don't want (and it's your fault, not compiler).

  3. It's almost impossible for compiler to generate code which is as complete as hand written assembly and is smaller than it at the same time.

  4. As mentioned in some comments, it also depends on platform.

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234785

It's technically impossible to write memcpy in standard C++ and C as you have to rely on undefined constructs. The same is true for other standard library functions; memset and malloc are two other examples.

But that's not only reason: A C and C++ standard library implementation is, these days, so closely coupled with a particular compiler that the library writers can take all sorts of liberties that you, as a consumer, cannot. isupper, toupper, &c. stand out as good examples where a particular character encoding can be assumed.

Another good reason is that expertly handcrafted assembly can be difficult to beat for performance.

Upvotes: 2

klutt
klutt

Reputation: 31409

This "It's pointless to rewrite in assembly" is a myth. A more accurate way to express it is that few programmers have the skill required to beat the compiler. But they do exist, and especially among those who develop compilers.

Upvotes: 3

eerorika
eerorika

Reputation: 238401

Why did the programmers not just write the code in C/C++

We aren't mind readers. We don't even know what they wrote. If you need an authoritative answer, then you should ask the programmers that wrote the code.

But we can hypothesise, that they wrote what they did because it was fast, and did the right thing.

Upvotes: 0

Related Questions