patternMinor
Examples of specialized instructions of assembly language not available in compilers?
Viewed 0 times
instructionsavailablecompilersspecializedexampleslanguageassemblynot
Problem
In my CS computer organization textbook, there's this blurb on the advantage of assembly over a high-level language.
Another major advantage of assembly language is the ability to exploit specialized instructions - for example, string copy or pattern-matching instructions. Compilers, in most cases, cannot determine that a program loop can be replaced by a single instruction. However, the programmer who wrote the loop can replace it easily with a single instruction.
How can a loop be replaced by string copy or pattern matching? Can somebody give an example on specialized instructions that are not available in a high-level language and how a specialized instruction can replace a loop?
Another major advantage of assembly language is the ability to exploit specialized instructions - for example, string copy or pattern-matching instructions. Compilers, in most cases, cannot determine that a program loop can be replaced by a single instruction. However, the programmer who wrote the loop can replace it easily with a single instruction.
How can a loop be replaced by string copy or pattern matching? Can somebody give an example on specialized instructions that are not available in a high-level language and how a specialized instruction can replace a loop?
Solution
Counting bits set, Brian Kernighan's way
This code becomes
unsigned int v; // count bits set (1)
unsigned int c; // result
for (c = 0; v; ++c)
{
v &= v - 1;
}This code becomes
popcntCode Snippets
unsigned int v; // count bits set (1)
unsigned int c; // result
for (c = 0; v; ++c)
{
v &= v - 1;
}Context
StackExchange Computer Science Q#53395, answer score: 4
Revisions (0)
No revisions yet.