Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #165619 > unrolled thread
| Started by | luser droog <luser.droog@gmail.com> |
|---|---|
| First post | 2022-04-11 20:09 -0700 |
| Last post | 2022-04-24 18:41 -0700 |
| Articles | 15 — 9 participants |
Back to article view | Back to comp.lang.c
gosub.c luser droog <luser.droog@gmail.com> - 2022-04-11 20:09 -0700
Re: gosub.c om@iki.fi (Otto J. Makela) - 2022-04-19 12:27 +0300
Re: gosub.c Andreas Kempe <kempe@lysator.liu.se> - 2022-04-20 14:43 +0000
Re: gosub.c Andreas Kempe <kempe@lysator.liu.se> - 2022-04-20 15:29 +0000
Re: gosub.c luser droog <luser.droog@gmail.com> - 2022-04-20 13:20 -0700
Re: gosub.c Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-04-21 07:43 -0700
Re: gosub.c Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-21 07:42 +0200
What part of... (Well, you know the rest!) (Was: gosub.c) gazelle@shell.xmission.com (Kenny McCormack) - 2022-04-21 06:03 +0000
Re: What part of... (Well, you know the rest!) (Was: gosub.c) Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-22 07:15 +0200
Re: What part of... (Well, you know the rest!) (Was: gosub.c) Anton Shepelev <anton.txt@g{oogle}mail.com> - 2022-04-22 11:15 +0300
Re: What part of... (Well, you know the rest!) (Was: gosub.c) Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-04-22 02:16 -0700
Re: What part of... (Well, you know the rest!) (Was: gosub.c) Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-22 11:21 +0200
Re: What part of... (Well, you know the rest!) (Was: gosub.c) Bonita Montero <Bonita.Montero@gmail.com> - 2022-04-22 11:18 +0200
Re: What part of... (Well, you know the rest!) (Was: gosub.c) Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-04-25 08:09 -0700
Re: gosub.c luser droog <luser.droog@gmail.com> - 2022-04-24 18:41 -0700
| From | luser droog <luser.droog@gmail.com> |
|---|---|
| Date | 2022-04-11 20:09 -0700 |
| Subject | gosub.c |
| Message-ID | <cf49401f-eae2-486d-bcf8-4550e8de5467n@googlegroups.com> |
Found this on my disk. I forget where/why it came from but I
thought it was fun enough to share.
% cat gosub.c
#include <stdio.h>
#define GOSUB(target) do { *stk++ = __LINE__; goto target; case __LINE__: ;} while(0)
#define RETURN() do{ target = *--stk; goto Goto; } while(0)
int main(){
int *stk = (int [10]){};
int target = 0;
Goto: switch( target ){
default:
puts("program begins.");
GOSUB(test_label);
puts("program continues.");
GOSUB(test_label);
puts("program ends.");
return 0;
test_label:
puts("gosub successful.");
RETURN();
}
}
[toc] | [next] | [standalone]
| From | om@iki.fi (Otto J. Makela) |
|---|---|
| Date | 2022-04-19 12:27 +0300 |
| Message-ID | <87bkwxa0e2.fsf@tigger.extechop.net> |
| In reply to | #165619 |
luser droog <luser.droog@gmail.com> wrote: > Found this on my disk. I forget where/why it came from but I > thought it was fun enough to share. That was... worthy of the obfuscated C competition. Thanks, I hate it. -- /* * * Otto J. Makela <om@iki.fi> * * * * * * * * * */ /* Phone: +358 40 765 5772, ICBM: N 60 10' E 24 55' */ /* Mail: Mechelininkatu 26 B 27, FI-00100 Helsinki */ /* * * Computers Rule 01001111 01001011 * * * * * * */
[toc] | [prev] | [next] | [standalone]
| From | Andreas Kempe <kempe@lysator.liu.se> |
|---|---|
| Date | 2022-04-20 14:43 +0000 |
| Message-ID | <t3p674$99q$1@nyheter.lysator.liu.se> |
| In reply to | #165619 |
Den 2022-04-12 skrev luser droog <luser.droog@gmail.com>:
> Found this on my disk. I forget where/why it came from but I
> thought it was fun enough to share.
>
It was well worth sharing!
>
> % cat gosub.c
> #include <stdio.h>
>
> #define GOSUB(target) do { *stk++ = __LINE__; goto target; case __LINE__: ;} while(0)
> #define RETURN() do{ target = *--stk; goto Goto; } while(0)
>
> int main(){
> int *stk = (int [10]){};
> int target = 0;
> Goto: switch( target ){
> default:
> puts("program begins.");
> GOSUB(test_label);
> puts("program continues.");
> GOSUB(test_label);
> puts("program ends.");
> return 0;
> test_label:
> puts("gosub successful.");
> RETURN();
> }
> }
I tried throwing this code at clang 10.0.1 with -O3 on an amd64 system
and it produced some, to me, surprising results. I expected the
compiler to turn the code into
int main()
{
puts("program begins.");
puts("gosub successful.");
puts("program continues.");
puts("gosub successful.");
puts("program ends.");
return 0;
}
but I got something along the lines of (assembler turned back into C
by hand)
int main()
{
int value = 0;
goto begin;
print_continue:
puts("program continues.");
value = 0xe;
print_gosub:
puts("gosub successful.");
begin:
if (value == 0xc)
goto print_continue;
if (value == 0xe)
goto print_end;
puts("program begins.");
value = 0xc;
goto print_gosub;
print_end:
puts("program ends.");
return 0;
}
I have a hard time imagining that this construct would be more
efficient than simply printing the output in sequence. Is it the
switch-case construct causing the compiler to organise the code the
way it does?
[toc] | [prev] | [next] | [standalone]
| From | Andreas Kempe <kempe@lysator.liu.se> |
|---|---|
| Date | 2022-04-20 15:29 +0000 |
| Message-ID | <t3p8sp$99q$2@nyheter.lysator.liu.se> |
| In reply to | #165787 |
Den 2022-04-20 skrev Stefan Ram <ram@zedat.fu-berlin.de>:
> Andreas Kempe <kempe@lysator.liu.se> writes:
>>int main()
>>{
>> puts("program begins.");
>> puts("gosub successful.");
>> puts("program continues.");
>> puts("gosub successful.");
>> puts("program ends.");
>> return 0;
>>}
>
> Here,
>
> leaq .LC0(%rip), %rcx; call puts
> leaq .LC1(%rip), %rcx; call puts
> leaq .LC2(%rip), %rcx; call puts
> leaq .LC1(%rip), %rcx; call puts
> leaq .LC3(%rip), %rcx; call puts
>
> , assembler simplified and reformatted by hand, two
> operations per line. gcc estimated to be not older than
> 5 years, options including "-march=native -Ofast -O3".
>
I tried it on GCC 10.3.0 and got the same result as you, I also tried
it on another system with a newer llvm, clang version 13.0.1, and it
produced the same result as my first llvm attempt.
Interesting that GCC seems to provide a better optimisation than llvm
in this case. I'm not well versed enough in compiler construction to
say why.
[toc] | [prev] | [next] | [standalone]
| From | luser droog <luser.droog@gmail.com> |
|---|---|
| Date | 2022-04-20 13:20 -0700 |
| Message-ID | <9ed2a0c5-ddb6-435b-a19f-2a772967728an@googlegroups.com> |
| In reply to | #165787 |
On Wednesday, April 20, 2022 at 9:44:01 AM UTC-5, Andreas Kempe wrote:
> Den 2022-04-12 skrev luser droog <luser...@gmail.com>:
> > Found this on my disk. I forget where/why it came from but I
> > thought it was fun enough to share.
> >
> It was well worth sharing!
> >
> > % cat gosub.c
> > #include <stdio.h>
> >
> > #define GOSUB(target) do { *stk++ = __LINE__; goto target; case __LINE__: ;} while(0)
> > #define RETURN() do{ target = *--stk; goto Goto; } while(0)
> >
> > int main(){
> > int *stk = (int [10]){};
> > int target = 0;
> > Goto: switch( target ){
> > default:
> > puts("program begins.");
> > GOSUB(test_label);
> > puts("program continues.");
> > GOSUB(test_label);
> > puts("program ends.");
> > return 0;
> > test_label:
> > puts("gosub successful.");
> > RETURN();
> > }
> > }
> I tried throwing this code at clang 10.0.1 with -O3 on an amd64 system
> and it produced some, to me, surprising results. I expected the
> compiler to turn the code into
>
> int main()
> {
> puts("program begins.");
> puts("gosub successful.");
> puts("program continues.");
> puts("gosub successful.");
> puts("program ends.");
> return 0;
> }
> but I got something along the lines of (assembler turned back into C
> by hand)
>
> int main()
> {
> int value = 0;
>
> goto begin;
>
> print_continue:
> puts("program continues.");
> value = 0xe;
>
> print_gosub:
> puts("gosub successful.");
>
> begin:
> if (value == 0xc)
> goto print_continue;
> if (value == 0xe)
> goto print_end;
>
> puts("program begins.");
> value = 0xc;
> goto print_gosub;
>
> print_end:
> puts("program ends.");
> return 0;
> }
> I have a hard time imagining that this construct would be more
> efficient than simply printing the output in sequence. Is it the
> switch-case construct causing the compiler to organise the code the
> way it does?
Yes, it looks like the optimizer is having a hard time ironing out all of the
jumping/dispatching. It looks like the generous 10-element stack has been collapsed
into the single element actually used, but that "jump target" variable persists.
A little more unrolling of the loop and it might get closer to the straight ahead
version. But the optimizer doesn't know how close it is, it can't hear the crowd cheering.
[toc] | [prev] | [next] | [standalone]
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2022-04-21 07:43 -0700 |
| Message-ID | <t3rqi5$fev$1@dont-email.me> |
| In reply to | #165787 |
> Den 2022-04-12 skrev luser droog <luser.droog@gmail.com>:
> Found this on my disk. I forget where/why it came from but I
> thought it was fun enough to share.
>
>
> % cat gosub.c
> #include <stdio.h>
>
> #define GOSUB(target) do { *stk++ = __LINE__; goto target; case __LINE__: ;} while(0)
> #define RETURN() do{ target = *--stk; goto Goto; } while(0)
>
> int main(){
> int *stk = (int [10]){};
> int target = 0;
> Goto: switch( target ){
> default:
> puts("program begins.");
> GOSUB(test_label);
> puts("program continues.");
> GOSUB(test_label);
> puts("program ends.");
> return 0;
> test_label:
> puts("gosub successful.");
> RETURN();
> }
> }
Just a localized application of the same rather well-known
switch/case-version of Simon Tatham-style coroutine technique.
--
Best regards,
Andrey Tarasevich
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2022-04-21 07:42 +0200 |
| Message-ID | <t3qqr3$45l$1@dont-email.me> |
| In reply to | #165619 |
With C++11 you have lamdas as local functions. Magnitudes more elegant than you GOSUB-macro-orgy.
[toc] | [prev] | [next] | [standalone]
| From | gazelle@shell.xmission.com (Kenny McCormack) |
|---|---|
| Date | 2022-04-21 06:03 +0000 |
| Subject | What part of... (Well, you know the rest!) (Was: gosub.c) |
| Message-ID | <t3qs2v$378fb$1@news.xmission.com> |
| In reply to | #165808 |
In article <t3qqr3$45l$1@dont-email.me>, Bonita Montero <Bonita.Montero@gmail.com> wrote: >With C++11 you have lamdas as local functions. >Magnitudes more elegant than you GOSUB-macro-orgy. What part of "C++ is off topic here" do you still not get? As is Perl, Python, and Fortran (among others). -- 1/20/17: A great day for all those people who are sick of being told they don't know how to spell "you're" (or "there").
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2022-04-22 07:15 +0200 |
| Subject | Re: What part of... (Well, you know the rest!) (Was: gosub.c) |
| Message-ID | <t3tdki$rvh$1@dont-email.me> |
| In reply to | #165809 |
Am 21.04.2022 um 08:03 schrieb Kenny McCormack: > In article <t3qqr3$45l$1@dont-email.me>, > Bonita Montero <Bonita.Montero@gmail.com> wrote: >> With C++11 you have lamdas as local functions. >> Magnitudes more elegant than you GOSUB-macro-orgy. > > What part of "C++ is off topic here" do you still not get? It's just a recommendation to use a proper language if you wish sth. like that.
[toc] | [prev] | [next] | [standalone]
| From | Anton Shepelev <anton.txt@g{oogle}mail.com> |
|---|---|
| Date | 2022-04-22 11:15 +0300 |
| Subject | Re: What part of... (Well, you know the rest!) (Was: gosub.c) |
| Message-ID | <20220422111534.31de4c7bea60921a1525f316@g{oogle}mail.com> |
| In reply to | #165838 |
Bonita Montero: > With C++11 you have lamdas as local functions. Magnitudes > more elegant than you GOSUB-macro-orgy. Lambdas, being a functional concept, are alien to a procedural language. The mixing of functional and procedural approaches makes a language discordant and more complicated both to understand and to implement in a complier. C++ is about a hundred times larger and more complex than C. Since simplicity is perfection, C++ is inferior to C by the same factor. > It's just a recommendation to use a proper language if you > wish sth. like that. In this newsgroup the proper language is C, no matter what the task. -- () ascii ribbon campaign - against html e-mail /\ http://preview.tinyurl.com/qcy6mjc [archived]
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.arthur.mclean@gmail.com> |
|---|---|
| Date | 2022-04-22 02:16 -0700 |
| Subject | Re: What part of... (Well, you know the rest!) (Was: gosub.c) |
| Message-ID | <16c87d26-4712-4b4c-9023-b528f654bf29n@googlegroups.com> |
| In reply to | #165839 |
On Friday, 22 April 2022 at 09:15:47 UTC+1, Anton Shepelev wrote: > Bonita Montero: > > With C++11 you have lamdas as local functions. Magnitudes > > more elegant than you GOSUB-macro-orgy. > Lambdas, being a functional concept, are alien to a > procedural language. The mixing of functional and procedural > approaches makes a language discordant and more complicated > both to understand and to implement in a complier. C++ is > about a hundred times larger and more complex than C. Since > simplicity is perfection, C++ is inferior to C by the same > factor. > A trivial little lambda in basically procedural code can make code easier to read. For instance, if you pass a lambda to std::sort, you can see at a glace which field is being sorted on. If you pass a function pointer, it is necessary to scroll to the comparison function. If you write your own sort routine and embed the comparison code, then that is many times more difficult to implement and verify.
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2022-04-22 11:21 +0200 |
| Subject | Re: What part of... (Well, you know the rest!) (Was: gosub.c) |
| Message-ID | <t3ts28$if$1@dont-email.me> |
| In reply to | #165840 |
Am 22.04.2022 um 11:16 schrieb Malcolm McLean: > On Friday, 22 April 2022 at 09:15:47 UTC+1, Anton Shepelev wrote: >> Bonita Montero: >>> With C++11 you have lamdas as local functions. Magnitudes >>> more elegant than you GOSUB-macro-orgy. >> Lambdas, being a functional concept, are alien to a >> procedural language. The mixing of functional and procedural >> approaches makes a language discordant and more complicated >> both to understand and to implement in a complier. C++ is >> about a hundred times larger and more complex than C. Since >> simplicity is perfection, C++ is inferior to C by the same >> factor. >> > A trivial little lambda in basically procedural code can make > code easier to read. For instance, if you pass a lambda to std::sort, > you can see at a glace which field is being sorted on. If you pass > a function pointer, it is necessary to scroll to the comparison function. > If you write your own sort routine and embed the comparison code, > then that is many times more difficult to implement and verify. I not only use lambdas as function objects being passed as a parameter. I use lambdas when it doesn't make sense to have an external function because it is only used once to segement the code. And I use lambdas to have a lot less code to pack redundant code-parts inside a function inside a lambda. Lambdas are such a giant relief, and with C++20 even more since you can have templated lambdas.
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2022-04-22 11:18 +0200 |
| Subject | Re: What part of... (Well, you know the rest!) (Was: gosub.c) |
| Message-ID | <t3trs9$u4k$1@dont-email.me> |
| In reply to | #165839 |
Am 22.04.2022 um 10:15 schrieb Anton Shepelev: > Bonita Montero: > >> With C++11 you have lamdas as local functions. Magnitudes >> more elegant than you GOSUB-macro-orgy. > > Lambdas, being a functional concept, are alien to a > procedural language. The mixing of functional and procedural > approaches makes a language discordant and more complicated > both to understand and to implement in a complier. C++ is > about a hundred times larger and more complex than C. Since > simplicity is perfection, C++ is inferior to C by the same > factor. C++ is magnitutes more superior to C because it's much easier to realize complex projects with C++. And lambdas make the code also much more readable and often faster.
[toc] | [prev] | [next] | [standalone]
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Date | 2022-04-25 08:09 -0700 |
| Subject | Re: What part of... (Well, you know the rest!) (Was: gosub.c) |
| Message-ID | <8635i18ajw.fsf@linuxsc.com> |
| In reply to | #165839 |
Anton Shepelev <anton.txt@g{oogle}mail.com> writes:
> Bonita Montero:
>
>> With C++11 you have lamdas as local functions. Magnitudes
>> more elegant than you GOSUB-macro-orgy.
>
> Lambdas, being a functional concept, are alien to a
> procedural language. The mixing of functional and procedural
> approaches makes a language discordant and more complicated
> both to understand and to implement in a complier. [...]
Even Algol had nested functions.
[toc] | [prev] | [next] | [standalone]
| From | luser droog <luser.droog@gmail.com> |
|---|---|
| Date | 2022-04-24 18:41 -0700 |
| Message-ID | <a1651b06-f7de-4087-b40b-306df6e2d4cen@googlegroups.com> |
| In reply to | #165808 |
On Thursday, April 21, 2022 at 12:42:08 AM UTC-5, Bonita Montero wrote: > With C++11 you have lamdas as local functions. > Magnitudes more elegant than you GOSUB-macro-orgy. It's like literally 2 macros. Two meager lines of code, ok 3 for the harness. If the result is feature comparable to C++ lambdas, then maybe the takeaway isn't what you think it is.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web