Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

g++ is being gay

Name: Anonymous 2008-11-06 23:48


Data BSTree::retrieve(const KeyType& key, BSTree::Node *cur) const
{
    if(!cur)
        return Data();
    else if(key == cur->data.key)
        return cur->data;
    cur = key > cur->data.key ? cur->right : cur->left;
    return retrieve(key, cur);
}


This is a tail-call, yes? But when I compile with "g++ -S BSTree.cpp -Wall -pedantic -O2 -foptimize-sibling-calls", I get this assembly code.


_ZNK6BSTree8retrieveERKjPNS_4NodeE:
.LFB791:
    testq    %rcx, %rcx
    pushq    %rbx
.LCFI0:
    movq    %rdi, %rbx
    je    .L13
    movl    (%rdx), %eax
    cmpl    (%rcx), %eax
    je    .L14
    ja    .L15
    movq    16(%rcx), %rcx
.L10:
    movq    %rbx, %rdi
        ; LOL WTF IS THIS SHIT
    call    _ZNK6BSTree8retrieveERKjPNS_4NodeE
    movq    %rbx, %rax
    popq    %rbx
    .p2align 4,,1
    .p2align 3
    ret
    .p2align 4,,10
    .p2align 3
.L15:
    movq    24(%rcx), %rcx
    jmp    .L10
    .p2align 4,,10
    .p2align 3
.L13:
    movq    %rbx, %rax
    movl    $0, (%rdi)
    movq    $_ZNSs4_Rep20_S_empty_rep_storageE+24, 8(%rdi)
    popq    %rbx
    ret
    .p2align 4,,10
    .p2align 3
.L14:
    movl    %eax, (%rdi)
    leaq    8(%rcx), %rsi
    leaq    8(%rdi), %rdi
    call    _ZNSsC1ERKSs
    movq    %rbx, %rax
    popq    %rbx
    ret


I'm about to give up and just put in a fucking goto.

Name: Anonymous 2008-11-06 23:54

Data BSTree::retrieve(const KeyType& key,const BSTree::Node *cur) const

Name: Anonymous 2008-11-06 23:57

>>2
Nope.

Name: Anonymous 2008-11-07 0:07

I don't understand, can you rephrase the question so it is in terms of the algorithmic language Scheme?

Name: Anonymous 2008-11-07 0:15

Data BSTree::retrieve(const KeyType& key, const BSTree::Node *cur) {
 if(!cur) return Data();
 while(key != cur->data.key) cur = key > cur->data.key ? cur->right : cur->left;
 return cur->data;
}

Name: Newb Seppler 2008-11-07 0:15

what books on sepples should i buy to becom an expert enterprise programmer?

Name: Anonymous 2008-11-07 0:16

>>5
Yeah, I know that. I just wanted to get it done with tail calls, because I like them more.

Name: Anonymous 2008-11-07 0:20

>>7
you may like obfuscated code more, but don't expect the compiler to go out of it's way to optimize it for you.

Name: Anonymous 2008-11-07 1:04

>>8
1/10

Name: Anonymous 2008-11-07 4:31

I don't think tail-call optimization is something that is ever done in practice. It kinda changes the semantics of the code.

It's like detecting bubble sort and changing it to quicksort. Although tail-call optimization would be easier than that, it's not gonna happen.

Also, -O2 implies -foptimize-sibling-calls.

Name: Anonymous 2008-11-07 6:25

>>10
I think gcc does tail-call optimizations for C.

Name: Anonymous 2008-11-07 6:41

How about you just code it directly in ASM when you're so concerned about code quality? GCC has a pretty interesting inline ASM construct that also allows inlining and optimization of passed parameters.

Also lol 64bit OS.

Name: Anonymous 2008-11-07 15:20

>>10
It kinda changes the semantics of the code.
Wat.

Name: Anonymous 2008-11-07 15:26

>>13
Semantics = meaning.
Tail-call optimization changes the meaning of the original code, even though the output will still be the same. Kind of like how using bubble sort and quick sort will still sort a list, but the way they do it is different.

Name: Anonymous 2008-11-07 15:56

>>14
You're funny.

Name: Anonymous 2008-11-07 15:59

>>14
Why is this important if they're behaviorally congruent, assuming the generated code is faster?

Name: Anonymous 2008-11-07 19:14

>>14
All optimizations change how the code works. That's the whole fucking point; you can write your code normally, but the omniscient compiler will translate it into uber-faster code for you.

Name: Anonymous 2008-11-07 20:08

>>14
It won't change the meaning of the original C++ code. Stop being a fucking pedant, you furfag.

Name: Anonymous 2008-11-07 20:49

>>18
Back to /sa/, please

Name: Anonymous 2008-11-08 9:18

I agree with >>19

Name: Anonymous 2008-11-08 10:16

I agree with >>22

Name: Anonymous 2008-11-08 10:47

I agree >>22

Name: Anonymous 2008-11-08 11:05

I >>22

Name: Anonymous 2008-11-08 11:07

Use goto, since it's not harmful.

Name: Anonymous 2008-11-08 11:20

#define tailcall(x) x:
#define tailcall_optimize(x, ...) goto x

Data BSTree::retrieve(const KeyType& key, BSTree::Node *cur) const
{
    tailcall(retreive);
    if(!cur)
        return Data();
    else if(key == cur->data.key)
        return cur->data;
    cur = key > cur->data.key ? cur->right : cur->left;
    tailcall_optimize(x, key, cur);
}

Name: Anonymous 2008-11-08 11:23

s/retreive/retrieve/
s/\(x, k/(retrieve, k/

Name: Anonymous 2008-11-08 11:53

>>25
DONT HELP HIM!!!!!!!!!!!!!!!!!!!

Name: Anonymous 2008-11-08 14:34

How about you use Get instead of a stupid word like retrieve?

Name: Anonymous 2008-11-08 14:41

how about not g++?

Name: HMA FAN 2008-11-08 18:01

>>29
How about look at your anus

Name: Anonymous 2008-11-09 2:43

>>28
Because my professor said to use "retrieve", and he'll mark me down if I name it otherwise.

Name: Anonymous 2008-11-09 6:07

>>31
It wasn't a ``why'' question, why did you begin with ``because''?

Name: Anonymous 2008-11-09 7:30

>>32
Also not a ``why'' question.

Name: Anonymous 2008-11-09 7:33

>>32,33
Your a dick. Thats why.

Name: Anonymous 2008-11-09 7:37

>>34
What about my a dick, and furthermore, what is a That and why does it why, what does why mean as a verb?

Name: Anonymous 2008-11-09 8:50

>>35
DICKS

Name: Anonymous 2008-11-09 9:25

Zey are tweens, zat is why

Name: ​​​​​​​​​​ 2010-10-22 4:12

Name: Anonymous 2011-02-04 19:58

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List