Author Topic: Adding in C++  (Read 8348 times)

0 Members and 1 Guest are viewing this topic.

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Adding in C++
« on: February 21, 2012 »
in my interpolation code I have something like:

red=(unsigned int)scan.r;
scan.r+=scan.dr;

but something like:

red=(unsigned int)(scan.r+=scan.dr);

works though I have to subtract scan.dr from scan.r before the loop, can it be written to work without doing that so that it uses scan.r before adding scan.dr?

EDIT: or have I got that round the wrong way and it is doing what I want?

Also had another problem, I tried to switch the assembler in DevC++ to intel syntax and without having any asm code in my program it screwed up how the compiler was casting from float to unsigned ints even though it cast to signed ints ok.
« Last Edit: February 21, 2012 by Stonemonkey »

Offline staticgerbil

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 8
    • View Profile
Re: Adding in C++
« Reply #1 on: February 22, 2012 »
I don't think it is doing what you want and I can't think of any nice way to do what you want off the top of my head.

Perhaps you could overload +=  ;)

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Adding in C++
« Reply #2 on: February 22, 2012 »
Thanks, I'm just exploring.

b=(a++)+b;
=
b=a+b;a++;

so I was wondering if there was something like that for adding a value

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Adding in C++
« Reply #3 on: February 22, 2012 »
I don't think so. There's not an expression involving += that returns the value before the add.
Are you trying to do this to make your code even more terse or is it an optimisation?  If it's the latter then it's almost certainly not worth it as the compiler will take care of it.
Jim
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Adding in C++
« Reply #4 on: February 22, 2012 »
Cheers Jim, it's just something with some curiosity I ended up looking into and messing around with though I know it won't make much difference to the compiler.

I'm not sure about terse though, it might be shorter but whether or not it's to the point I'm not so sure about but I went a bit mad with it and this terrible looking thing gives me all the gradients I need for z/r/g/b/u/v/x1/x2/x3 edges and scanlines.

Code: [Select]
float ddy,dy,dx;
  edge.dh=(v1->sx-v0->sx)/(ddy=v1->sy-v0->sy);
  edge.dl=(v2->sx-v1->sx)/(v2->sy-v1->sy);
  scan.dz=(v1->sz-(v0->sz+(edge.dz=(v2->sz-v0->sz)*(dy=1.0f/(v2->sy-v0->sy)))*ddy))*
     (dx=1.0f/(v1->sx-(v0->sx+(edge.dx=(v2->sx-v0->sx)*dy)*ddy)));
  scan.dr=(v1->sr-(v0->sr+(edge.dr=(v2->sr-v0->sr)*dy)*ddy))*dx;
  scan.dg=(v1->sg-(v0->sg+(edge.dg=(v2->sg-v0->sg)*dy)*ddy))*dx;
  scan.db=(v1->sb-(v0->sb+(edge.db=(v2->sb-v0->sb)*dy)*ddy))*dx;
  scan.du=(v1->su-(v0->su+(edge.du=(v2->su-v0->su)*dy)*ddy))*dx;
  scan.dv=(v1->sv-(v0->sv+(edge.dv=(v2->sv-v0->sv)*dy)*ddy))*dx;
« Last Edit: February 22, 2012 by Stonemonkey »

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Adding in C++
« Reply #5 on: February 23, 2012 »
It looks pretty anyway :)

Jim
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Adding in C++
« Reply #6 on: February 23, 2012 »
How about a data-structure for your vertex attributes with overloaded +/-/* operators?
Code: [Select]
struct Attributes {
  float z, r,g,b, u,v;
};

struct Vertex {
  float x,y;
  Attributes attribs;
}

Attributes edge= (v1->attribs - v2->attribs) * dy;
Attributes scan= (v1->attribs - (v0->attribs+edge*ddy))*dx;
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Adding in C++
« Reply #7 on: February 23, 2012 »
Philosophically speaking, does any software engineering guru still think that operator overloading is 'a good thing' TM?

Jim
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Adding in C++
« Reply #8 on: February 23, 2012 »
ok, i've tried that. well left the original fields as an anonymous type and unioned that with attributes.
I'm struggling to get my head around how the compiler deals with the overloading atm though.

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Adding in C++
« Reply #9 on: February 23, 2012 »
Philosophically speaking, does any software engineering guru still think that operator overloading is 'a good thing' TM?
I don't know what "software engineering gurus" think and I don't even know one.
In my opinion it's perfectly fine if an operator does *exactly* what you think it does.
That applies usually to math classes like matrices and vectors (and a set of attributes for linear interpolation is near enough).
Challenge Trophies Won:

Offline ferris

  • Pentium
  • *****
  • Posts: 841
  • Karma: 84
    • View Profile
    • Youth Uprising Home
Re: Adding in C++
« Reply #10 on: February 23, 2012 »
Quote
In my opinion it's perfectly fine if an operator does *exactly* what you think it does.
I very much agree. Intuitiveness is key for this kind of thing.
http://iamferris.com/
http://youth-uprising.com/

Where the fun's at.
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Adding in C++
« Reply #11 on: February 23, 2012 »
I'm really just looking at what different ways of doing things there are so it's all useful and not just specific to the code I'm messing around with here.

I can't seem to get DevC to output asm but I can't help thinking that using overloaded operators on the attributes type would result in a lot more mem/stack accessing than otherwise, is that the case?

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Adding in C++
« Reply #12 on: February 23, 2012 »
I can't help thinking that using overloaded operators on the attributes type would result in a lot more mem/stack accessing than otherwise
All OOP stuff has a bit of copying overhead but you can simply make your functions "inline" to avoid that.
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Adding in C++
« Reply #13 on: February 23, 2012 »
I feel it's more than that, for example if you had a type with fields f0,f1,f2,f3 and overloaded +

mytype a,b,c,d;

a=b+c+d;

then the 4 additions for b+c would be done first with the temp results stored somewhere then retrieved for the +d.

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Adding in C++
« Reply #14 on: February 23, 2012 »
"inline" basically inserts all the code of the function at the place where you called it.
The optimizer will then discard all unnecessary temporaries and the result should look like this:
Code: [Select]
a.f0 = b.f0 + c.f0 + d.f0;
a.f1 = b.f1 + c.f1 + d.f1;
a.f2 = b.f2 + c.f2 + d.f2;
a.f3 = b.f3 + c.f3 + d.f3;
Unless, of course, you're doing a debug-build (which disables most optimizations).
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Adding in C++
« Reply #15 on: February 23, 2012 »
Ah right. So are overloaded operators called as some sort of function if they're not inline?

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Adding in C++
« Reply #16 on: February 23, 2012 »
Ah right. So are overloaded operators called as some sort of function if they're not inline?
Operators aren't different from member functions.
So the compiler will just call this function unless it's configured to look for suitable inlining by itself.
Challenge Trophies Won: