Questions tagged [duffs-device]

In the C programming language, Duff's device is a technique of manually implementing loop unrolling by interleaving `do-while loop` and a `switch` statement.

22 questions
180
votes
11 answers

How does Duff's device work?

I've read the article on Wikipedia on the Duff's device, and I don't get it. I am really interested, but I've read the explanation there a couple of times and I still don't get it how the Duff's device works. What would a more detailed explanation…
hhafez
  • 38,949
  • 39
  • 113
  • 143
22
votes
5 answers

The behavior of the for loop split between switch cases

While playing with the code I've noticed a strange behavior that I do not know to explain the logic behind void foo(int n) { int m = n; while (--n > 0) { switch (n) { case -1: case 0: …
dEmigOd
  • 528
  • 4
  • 8
16
votes
2 answers

Switch case weird scoping

Reviewing some 3rd party C code I came across something like: switch (state) { case 0: if (c=='A') { // open brace // code... break; // brace not closed! case 1: // code... break; } // close brace! case 2: //…
Ricibob
  • 7,505
  • 5
  • 46
  • 65
11
votes
2 answers

Does Duff's Device Speed up Java Code?

Using the stock Sun 1.6 compiler and JRE/JIT, is it a good idea to use the sort of extensive unroll exemplified by Duff's Device to unroll a loop? Or does it end up as code obfuscation with no performance benefit? The Java profiling tools I've used…
bmargulies
  • 97,814
  • 39
  • 186
  • 310
9
votes
2 answers

Duff's device in Swift

We know that a Duff's device makes use of interlacing the structures of a fallthrough switch and a loop like: send(to, from, count) register short *to, *from; register count; { register n = (count + 7) / 8; switch (count % 8) { case 0:…
loretoparisi
  • 15,724
  • 11
  • 102
  • 146
8
votes
1 answer

Mixed 'switch' and 'while' in C

I've recently read this page about strange C snippet codes. Most of them was understandable. But I can't understand this one: switch(c & 3) while((c -= 4) >= 0){ foo(); case 3: foo(); case 2: foo(); case 1: foo(); case 0: } Can…
frogatto
  • 28,539
  • 11
  • 83
  • 129
8
votes
3 answers

Revising the syntax of Duff's device - Is this legal C/C++?

Only last night I encountered the curious Duff's device for the first time. I've been doing some reading on it, and I don't think it's that daunting to understand. What I am curious about is the strange syntax (from Wikipedia): register short *to,…
monkey0506
  • 2,489
  • 1
  • 21
  • 27
6
votes
5 answers

How can Duff's device code be compiled?

I understood why Duff's device is faster than normal loop code which can be unrolled but is not optimized. But I can't understand how the code can be compiled yet. I guess it's a trick about the switch syntax. But not anymore. How can do while…
Benjamin
  • 10,085
  • 19
  • 80
  • 130
6
votes
3 answers

Is Duff's device still useful?

I see that Duff's device is just to do loop unrolling in C. https://en.wikipedia.org/wiki/Duff%27s_device I am not sure why it is still useful nowadays. Isn't that the compiler should be smart enough to do loop-unrolling?
user1424739
  • 11,937
  • 17
  • 63
  • 152
5
votes
1 answer

Is there any problem in jumping into if(false) block?

I've read already a couple times (e.g. here Compiler: What if condition is always true / false) that any decent c++ compiler will opt-out something like if(false) { ... } But what if there is an intentional jump into this if(false) block. I'm…
ezegoing
  • 526
  • 1
  • 4
  • 18
5
votes
2 answers

Porting duff's device from C to JavaScript

I have this kind of Duff's device in C and it works fine (format text as money): #include #include char *money(const char *src, char *dst) { const char *p = src; char *q = dst; size_t len; len = strlen(src); …
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
5
votes
13 answers

Can I use Duff's Device on an array in C?

I have a loop here and I want to make it run faster. I am passing in a large array. I recently heard of Duff's Device can it be applied to this for loop? any ideas? for (i = 0; i < dim; i++) { for (j = 0; j < dim; j++) { …
Ben Fossen
  • 1,331
  • 6
  • 21
  • 33
4
votes
1 answer

Writing a macro for Duff's Device

From Zed Shaw's, Learn C the Hard Way,in exercise 23, he talks about Duff's Device. Here is Duff's Device for reference: int duffs_device(char *from, char *to, int count) { { int n = (count + 7) / 8; switch(count % 8) { …
Fumbster
  • 99
  • 7
3
votes
3 answers

C switch statement with do-while interleaved

Possible Duplicate: How does Duff's device work? I am trying to understand how this is working. Any help would be appreciated. #include void duff(int count) { int n=(count+7)/8; printf("n=%d count =%d\n",n,count%8); …
Pkp
  • 929
  • 6
  • 19
  • 31
1
vote
2 answers

Duff device in PHP not possible?

I've been told that a duff device doesn't work with PHP, because the switch and case construct working different. I've found this duff devive on php.net, my question is what is wrong with this device? Or didn't I understand a duff device? In my…
Micromega
  • 12,486
  • 7
  • 35
  • 72
1
2