• Welcome to Dark Bit Factory & Gravity.
 

Adding in C++

Started by Stonemonkey, February 21, 2012

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

Stonemonkey

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.

staticgerbil

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 +=  ;)

Stonemonkey

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

Jim

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:

Stonemonkey

#4
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.


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;

Jim

It looks pretty anyway :)

Jim
Challenge Trophies Won:

hellfire

How about a data-structure for your vertex attributes with overloaded +/-/* operators?

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:

Jim

Philosophically speaking, does any software engineering guru still think that operator overloading is 'a good thing' TM?

Jim
Challenge Trophies Won:

Stonemonkey

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.

hellfire

Quote from: Jim on February 23, 2012Philosophically 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:

ferris

QuoteIn 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:

Stonemonkey

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?

hellfire

Quote from: Stonemonkey on February 23, 2012I 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:

Stonemonkey

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.

hellfire

"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:

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:

Stonemonkey

Ah right. So are overloaded operators called as some sort of function if they're not inline?

hellfire

Quote from: Stonemonkey 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: