To cool for return

Started by
5 comments, last by Flonk 9 years, 11 months ago

So I was joyfully working on my CG assignment when I came about this little piece of code:


void Exercise13::lerp(
    float & result,
    const float & a,
    const float & b,
    const float & t)
{
    \\My Code
    result = ((1-t)*a)+(t*b);
}

So in effect they not only have a function that teaches the students to code in a horrible way (and yes, some have adopted such quirks), they have even made it harder for the compiler to inline a function that would be trivially inlinable.

Also, since we are working on 32bit machines or higher the whole pass by reference won't do anything to improve memory load (since the address will be just as large as a float). The only thing that would save this memory load would be... inlining.

Now I hope that my compiler is smart enough to figure this out, but shouldn't we just use language features?

Advertisement

"What does the "void" at the start of the function mean?"

"Oh, don't worry about that - we won't be using it."

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

So I went through the whole code, not just the part where we are supposed to fill in the blanks, and I turned up some more gems:


void Exercise13::quat(
    float q[4],
    const float m[16])
{   
    \\Turns a 4x4 rotation matrix into quaternions
}

But then I found something even better:

"mathmacros.h"

It seems like this file is used over multiple projects and was just casually thrown in for us.

Some highlights:


#include <cmath>


#define _PI 3.1415926535897932384626433832795L
#define _PI2  (_PI * 2.00L)
#define _PI_2 (_PI * 0.50L)
#define _PI4  (_PI * 4.00L)
#define _PI_4 (_PI * 0.25L)

\\snip


#define _randf(min, max) \
    (static_cast<float>(rand()) / RAND_MAX * ((max) - (min)) + (min))

#define _rand(min, max) \
    (static_cast<int>(static_cast<float>(rand()) / RAND_MAX * ((max) - (min)) + (min)))

\\Seeded in main.cpp with:     srand(time(NULL));

Although the rand() thing may just be me listening to this talk about rand().

Can't believe _PI__2 was omitted, which is obviously (_PI * _PI).


Can't believe _PI__2 was omitted, which is obviously (_PI * _PI).

Still laughing...

You want all your instructors to be amazing programmers, and to impart their wisdom to everyone. What you may learn instead is that they don't have a clue what they are doing.

It's good for you. Builds character.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532


So in effect they not only have a function that teaches the students to code in a horrible way (and yes, some have adopted such quirks), they have even made it harder for the compiler to inline a function that would be trivially inlinable.

Not to mention that this function could have easily been turned into a constexpression on a modern c+11-compiler, which would make the runtime cost even more trivial and the usage even more easy, if return value & pass-by-value was used... but not with this beauty here. Also, I think using references here makes it even harder for the compiler, because it now has to assume that a, b and t could point to the same value, which AFAIK applies some restrictions. Don't beat me if I'm wrong, not an expert in this field...

Wasn't there a thread somewhere here in coding horrors already about how universities can sometimes teach really bad stuff? I mean, this is a whole other level, I thought my programming courses where botched because the singleton was tought as first and most important design pattern...

Constexpr wouldn't work, since the t stands for time, so no compile time resolution.

And as to the aliasing of the parameters: your compiler will gladly shoot your foot.


void Exercise13::lerp(
    float & result,
    const float & a,
    const float & b,
    const float & t)
{
    result = 0.0;
    result += ((1-t)*a);
    result += (t*b);
}

In the above example, if result and b are the same address we will have a fun time.

For example lerp(right, left, right, 0) would no longer return the wanted left but 0 instead.

This topic is closed to new replies.

Advertisement