background preloader

C

Facebook Twitter

Gnu

Hacks. Is it safe to use -1 to set all bits to true. Ridiculous_fish » Blog Archive » Will it optimize? Will It Optimize?

ridiculous_fish » Blog Archive » Will it optimize?

July 23rd, 2010 See how well you know (or can anticipate) gcc's optimizer. For each question, the left box contains some code, while the right box contains code that purports to do the same thing, but that illustrates a particular optimization. Will gcc apply that optimization? Put another way, will the code on the left be as fast as the code on the right, when compiled with an optimizing gcc? I used a pretty ancient gcc 4.2.1 for these tests. Beware: not all proposed optimizations are actually valid! 1. Can GCC replace recursive functions with a loop? Int factorial(int x) { if (x > 1) return x * factorial(x-1); else return 1; } int factorial(int x) { int result = 1; while (x > 1) result *= x--; return result; } 2. Will GCC hoist out strlen()? Unsigned sum(const unsigned char *s) { unsigned result = 0; for (size_t i=0; i < strlen(s); i++) { result += s[i]; } return result; } 3. Will GCC transform an integer multiplication by 2 to addition? Int double_it(int x) { return x * 2; }

Coroutines in C. By Simon Tatham Introduction Structuring a large program is always a difficult job.

Coroutines in C

One of the particular problems that often comes up is this: if you have a piece of code producing data, and another piece of code consuming it, which should be the caller and which should be the callee? Here is a very simple piece of run-length decompression code, and an equally simple piece of parser code: Each of these code fragments is very simple, and easy to read and understand. In many modern operating systems, you could do this using pipes between two processes or two threads. emit() in the decompressor writes to a pipe, and getchar() in the parser reads from the other end of the same pipe.

In this article I offer a creative solution to this sort of structure problem. Rewriting The conventional answer is to rewrite one of the ends of the communication channel so that it's a function that can be called. Of course you don't have to rewrite both of them; just one will do. And that's the point, really. C: The Complete Nonsense. Revised for the 4th Edition Last revision: February 8th, 2012 Introduction Back in 1996, having heard horror stories about Herbert Schildt's C: The Complete Reference, I decided to check it out.

C: The Complete Nonsense

I flipped the book open; I found glaring errors. I paged through it. Time passed. Well, it is. I am no longer some random guy who likes C. So if people want a better-written page on the topic, well, I'm willing to provide one. Contents: A note about editions, explaining why different people report different problems in this book.

A note about editions The previous version of this page made no mention of editions, but was based on the 3rd edition of C:TCR. There are two reasons this matters. Pick a page, any page In comp.lang.c, some recent discussions of this led to an observation. While some of these (pages 259, 264, 455, and 463) were picked by randomly flipping the book open, other pages were picked because I came across them while looking up Schildt's coverage of a particular issue. Page 51 [...]