duffs-device

C switch statement with do-while interleaved [duplicate]

烈酒焚心 提交于 2020-02-04 01:49:47
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How does Duff's device work? I am trying to understand how this is working. Any help would be appreciated. #include<stdio.h> void duff(int count) { int n=(count+7)/8; printf("n=%d count =%d\n",n,count%8); switch(count%8){ case 0: do{ printf("case 0\n"); case 7: printf("case 7\n"); case 6: printf("case 6\n"); case 5: printf("case 5\n"); case 4: printf("case 4\n"); case 3: printf("case 3\n"); case 2: printf("case

How can Duff's device code be compiled?

天涯浪子 提交于 2020-01-09 10:53:17
问题 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 sentence exist in switch sentence? Very weird. Is there anyone who can explain this? Edit: Another question. Why did duff use 8? It could be 16, 65536 or whatever. Because of code size? Is there another reason? For example, cache or pipelining benefits.

How can Duff's device code be compiled?

限于喜欢 提交于 2020-01-09 10:53:11
问题 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 sentence exist in switch sentence? Very weird. Is there anyone who can explain this? Edit: Another question. Why did duff use 8? It could be 16, 65536 or whatever. Because of code size? Is there another reason? For example, cache or pipelining benefits.

Switch case weird scoping

我的未来我决定 提交于 2019-12-22 08:27:05
问题 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: // code... break; } Which in the code I was reviewing appeared to be just a typo but I was surprised that it compiled with out error. Why is this valid C? What is the effect on the execution of this code compared to closing the brace at the expected place? Is there any case where this could

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

南楼画角 提交于 2019-12-22 06:33:36
问题 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 having something like this in mind #include <iostream> void func(int part){ switch (part) { case 0:{ if(false) case 1:{std::cout << "hello" << std::endl;} break; } default: break; } } int main() { func(0); func(1); return 0; } Is any decent c++ compiler

Porting duff's device from C to JavaScript

廉价感情. 提交于 2019-12-12 13:34:26
问题 I have this kind of Duff's device in C and it works fine (format text as money): #include <stdio.h> #include <string.h> char *money(const char *src, char *dst) { const char *p = src; char *q = dst; size_t len; len = strlen(src); switch (len % 3) { do { *q++ = ','; case 0: *q++ = *p++; case 2: *q++ = *p++; case 1: *q++ = *p++; } while (*p); } *q++ = 0; return dst; } int main(void) { char str[] = "1234567890123"; char res[32]; printf("%s\n", money(str, res)); return 0; } Output: 1,234,567,890

Does Duff's Device Speed up Java Code?

这一生的挚爱 提交于 2019-12-08 16:29:13
问题 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 are less informative about line-by-line CPU usage than, say, valgrind, so I was looking to augment measurement with other people's experience. Note that, of course, you can't exactly code Duff's Device, but you can do the basic unroll, and that's

Nested case statements

守給你的承諾、 提交于 2019-12-02 15:03:48
问题 Can some one please explain the nesting of case statements into another. I'm referring to the Duffs Device where all other case statements are inside the do-while loop associated with case 0 . I cant get my head around it. It seems to me that it should act like a nested if . But then i'm definitely missing something. Please explain. 回答1: In a switch-case construct, the switch body is just a normal or compound statement which can contain any other valid c statements. It may also contain case

Nested case statements

纵饮孤独 提交于 2019-12-02 11:05:51
Can some one please explain the nesting of case statements into another. I'm referring to the Duffs Device where all other case statements are inside the do-while loop associated with case 0 . I cant get my head around it. It seems to me that it should act like a nested if . But then i'm definitely missing something. Please explain. In a switch-case construct, the switch body is just a normal or compound statement which can contain any other valid c statements. It may also contain case or default labels. And the control jumps to appropriate case label depending on controlling expression value,

Mixed 'switch' and 'while' in C

我只是一个虾纸丫 提交于 2019-12-01 17:35:39
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 anyone please help me out what logic is behind of this code? And how does it work? The duff's device comment should explain the background well enough, so I ll try to explain this very case: The switch checks the last 2 bits of c, and jumps to the respective case-statement inside the while loop. The code below the case statement is also executed. Control then reaches