Pages: [1]   Go Down
Author Topic: highByte - Optimization  (Read 588 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 smiley
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 209
Posts: 13024
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 209
Posts: 13024
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

Anyways, thank you for your input. Very muchly. smiley
Logged

Pages: [1]   Go Up
Jump to: