What is Duff's device)

Source: Internet
Author: User
Tags case statement

In the book 496 C language problems you must know, the following code is used to refer to the "Da fu device:

Register n = (count + 7)/8;/\ * count> 0 assumed \*/

Switch (count % 8)

{

Case 0: do {\ * to = \ * From ++;

Case 7: \ * To = \ * From ++;

Case 6: \ * To = \ * From ++;

Case 5: \ * To = \ * From ++;

Case 4: \ * To = \ * From ++;

Case 3: \ * To = \ * From ++;

Case 2: \ * To = \ * From ++;

Case 1: \ * To = \ * From ++;

} While (-- N> 0 );

}

This is a great method of loop expansion, designed by Tom Duff in Lucasfilm. It is in the ''traditional "form and is used to copy multiple bytes.

Here, Count bytes are copied from the array pointed to by from to the memory address (this is the output register of memory ing, which is why it is not increased ). The swtich statement is intertwined with the loop of eight bytes, which solves the problem of processing the remaining bytes (when count is not a multiple of 8 ). Believe it or not, it is legal to place the case flag in a module nested in the swtich statement like this. When he published this technique to C developers and the world, Duff noticed C's swtich syntax, especially the 'drop' behavior, which has been controversial, the ''code forms some arguments in the argument, but I don't know whether it is in favor or not"

A function contains a switch statement. Its case statement is located in a while loop at the same time (there is a case statement outside ). The expression in the switch calculates the remainder of the Division. The position where the execution starts in the while loop is determined by the remainder and the loop exits (without break ). Duff's device solves the boundary condition simply and beautifully. By the way, why is "case 0" marked out of the loop? Does this not break the symmetrical appearance? The only reason for doing so is to process the empty sequence. When the remainder is zero, an extra test is required in case 0 to determine the possibility of a null sequence. In short, this is a cool algorithm.

The Dafu device is a C encoding technique for accelerating loop statements. The basic idea is --Reduce the number of times the cyclic test is executed.

If an operation in a for loop is executed fast enough (for example, a value assignment), the test cycle condition occupies a large part of the cycle time. The loop should be partially unlocked, so that several operations are completed at one time, and few test operations are performed. In fact, the number of consecutive loop operations to be performed by the switch statement is predicted (based on the position of the case-based statement) and then executed in sequence, without the need to perform the test conditions each time.

Here Duff's device is a novel and creative solution. Here is an example using this model: Fast copy and fill.

The negative impact of Duff's device on efficiency may come from code expansion (some processors are better at processing compact loops rather than large loops) and special structures. The Optimizer may be overwhelmed to generate conservative code when encountering more technical structures.

In fact, the Dafu device uses the switch statement to control the location of the loop.

The following procedure is to verify the execution of the Dafu device:

# Include <stdio. h>

# Include <stdlib. h>

Int main (INT argc, char ** argv ){

Int N;

If (argc <2 ){

Printf ("not enough arugment \ n ");

Return-1;

}

N = atoi (argv [1]);

Switch (n ){

Case 0: do {printf ("% d", 0 );

Case 1: printf ("% d", 1 );

Case 2: printf ("% d", 2 );

Case 3: printf ("% d", 3 );

Case 4: printf ("% d", 4 );

} While (-- N> 0 );

}

Return 0;

}


From the above output, we can clearly see that the switch statement controls the position of the loop.

 

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.