Live data from Hacker News

Coroutines in C (2000)

chiark.greenend.org.uk

11–20 of 61 posts

Re: Coroutines in C (2000)

#11

What's the purpose of this code in the original decompressor? Assuming c is an uchar, aren't EOF and 0xFF equal? if (c == 0xFF) { len = getchar(); c = getchar(); while (len--) emit(c); }

Decompression, the first byte is the number of repeats and the second is the byte to repeat.

Re: Coroutines in C (2000)

#12

What's the purpose of this code in the original decompressor? Assuming c is an uchar, aren't EOF and 0xFF equal? if (c == 0xFF) { len = getchar(); c = getchar(); while (len--) emit(c); }

Decompression, the first byte is the number of repeats and the second is the byte to repeat.

Ah, thanks!

Re: Coroutines in C (2000)

#14

What's the purpose of this code in the original decompressor? Assuming c is an uchar, aren't EOF and 0xFF equal? if (c == 0xFF) { len = getchar(); c = getchar(); while (len--) emit(c); }

c should not be a char or unsigned char, because the return type of getchar() is "int". If you put it into a char-width variable then you lose the distinction between EOF (which is -1) and the byte 0xff. Getting the type of 'c' wrong is quite a common bug, because the API makes it an easy mistake to make. In this case, if you look down at the eventual transformed code in the "Evaluation" section you'll see that 'c' is indeed correctly declared with 'int' type.

Re: Coroutines in C (2000)

#15

What's the purpose of this code in the original decompressor? Assuming c is an uchar, aren't EOF and 0xFF equal? if (c == 0xFF) { len = getchar(); c = getchar(); while (len--) emit(c); }

getchar() returns an int to accommodate 257 different values: all byte chars + EOF (typically -1).

The code snippet itself is run length encoding with 0xff as an escape.

Re: Coroutines in C (2000)

#16
That page uses very simple HTML. Just paragraphs of text divided into sections with headings. Some code examples laid out side-by-side with a table. Tiny stylesheet. No JS. Loads quickly, easy to read. Warms the cockles of my heart.

Re: Coroutines in C (2000)

#18
We tried to implement coroutines based on the CoroutineTS here: https://github.com/LoopPerfect/conduit

CoroutineTS uses coroutines that are implemented on the LLVM-IR level, as a result they get optimized away in many cases.

Unfortunately the CoroutineTS has also some design flaws around allocations and ownership

Re: Coroutines in C (2000)

#19

That page uses very simple HTML. Just paragraphs of text divided into sections with headings. Some code examples laid out side-by-side with a table. Tiny stylesheet. No JS. Loads quickly, easy to read. Warms the cockles of my heart.

Well, it was written in 2000. Call me nostalgic, but there's something to be said about how simple the internet was back then.

Re: Coroutines in C (2000)

#20

A cool library that implements C coroutines, available for both idiomatic C ( http://libdill.org/ ) and in the same style as Go ( http://libmill.org/ ) with line by line translations

For those wondering how it works, libdill keeps track of context between function calls and does some clever stack modification in assembly.

I wonder if it would work from Rust. I was looking at something minimal like this.
Post reply on HN