Page 2 of 2

Re: And or double shift to select middle bits?

Posted: Tue Sep 05, 2017 8:38 am
by simeonz
Solar wrote:you can use #ifdef to select the fastest operation depending on platform / option switch (if you must).
As I said, the OP most likely used C only as a metaphor in trying to investigate the hardware efficiency of this operation sequence, and I don't mean to insist on my opinion either, but this is not really possible in C. I mean - yes, you could technically benchmark, but the result depends on the surrounding source code, on the compiler version, on the generation of the core and CPU architecture. The OP will need to reevaluate after every source code change near such a macro expansion. There is no easy way to predict how the registers will be allocated and whether register operands will be used at all, how the instructions will be sequenced, and thus where exactly the code will be relative to the surrounding code. I doubt that even the compiler engineers can tell you that. Also, the effect will be different to each function, in every incarnation it has during the evolution of the project. So, the macrofication does not really abstract much but the syntax, especially for such short fragment.

Not that I am against rational sensible thinking about the code performance while writing in C, but evaluating the efficiency of less than entire function implementations or at least larger code fragments involving loops, and may be not even that in all cases, is going to provide very controversial results in the long run.

P.S. After thinking about it, I realized that for a different type of source alternative, such as a typedef, structure packing, or another bit hack, I would have considered the suggestion for conditional compilation favorably, actually. But the nature of extracting a byte from a word makes me suspect volatility here. Whatever the case - either the bit hack is performance critical enough that rewriting it in assembly (even if inline assembly), captured within macro if desirable, is justified, or the idea of reliable code efficiency is sacrificed I'd say.

Re: And or double shift to select middle bits?

Posted: Tue Sep 05, 2017 9:18 am
by Solar
Basically I was trying to take the knife away from him before he hurts himself, if you get my meaning. ;-)

Re: And or double shift to select middle bits?

Posted: Tue Sep 05, 2017 9:44 am
by simeonz
Solar wrote:Basically I was trying to take the knife away from him before he hurts himself, if you get my meaning. ;-)
Ah, I see... Truth be told, I am witless at times.