1

I have the following snippet that seems to be taking a total of 6 CPU cycles per invocation.

Can I speed this up somehow? x is a byte with the 3 lowest bits set.

#define SET_COLOR(x) PORTB = (PORTB & ~(7)) | x
3
  • 1
    @TisteAndii: Interesting suggestion, but it ends up using an identical amount of instructions. I assume the compiler is smart enough. Sep 9, 2016 at 3:04
  • @tisteandii compilers are smart enough to do this for you.
    – Gerben
    Sep 9, 2016 at 13:35
  • @Gerben So I gathered. Dont know much about the optimizations they perform. Sep 9, 2016 at 13:40

1 Answer 1

2

4 cycles:

IN Rd,PORTB
CBR Rd,7
ORI Rd,x
OUT PORTB,Rd

Translation into proper GCC inline assembly syntax is left as an exercise for the reader.

1
  • Thanks. In the end (with some proper analysis) it showed the original code was resulting in exactly 4 instructions, as well. Sep 10, 2016 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.