Go Down

Topic: highByte - Optimization (Read 615 times) previous topic - next topic

Camalaio

Currently wondering about the highByte and lowByte functions.

At some point, I may need to highly optimize some code for heavier operations. (I generally optimize things anyway).

So I was wondering; what is the actual functionality?
Is using the following better than highByte/lowByte (or worse?)?
Code: [Select]
byte _highByte(word input)
{
return (input>>8) & 0x00FF;
}

byte _lowByte(word input)
{
return input & 0xFF;
}


This question extends to functions such as bitRead, bitSet, bitWrite, etc. For those I am more likely to have my own implementation, but curiosity has struck me.

Any input is appreciated :)

Coding Badly

Quote
Is using the following better than highByte/lowByte (or worse?)?


Worse.  For the simple reason that they are actual functions.  I believe the macros reduce to single register move or nothing.

Camalaio

Ah, put that way it seems to make sense. Thank you very much.

Now, I was thinking a bit more. I might also just use the functionality of the function without actually using the function. What I mean is just assigning the bitshifted, masked byte to something. Would that still be slower? There is no function call, so the hex/binaries (whichever, I believe you know what I mean. The compiled code) would have everything inline without a jump. Or so I assume...

Coding Badly

I'm afraid I don't follow.

But you need to take a step back.  The GCC-AVR compiler does a very good job of optimizing at the level you're trying to optimize.  You need to focus primarily on choosing the right algorithms.  If that doesn't get the performance you're after then and only then concern yourself with the generated code.

Camalaio

Ah, tis fine.
If it were easier, I would throw some assembly language at this. But of course, that is not quite easier :p
I like control over my programs. Years of going from high level down to low level have done this to me. Or rather, working down the tree made me like the control :p

Personally optimizing code, rather than relying on compilers and preprocessors, lets me see exactly what I've done wrong if something goes wrong. It's not often that a compiler will do something you can't detect, but I like wokring low-level :)

Anyways, thank you for your input. Very muchly. :)

Go Up