This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to program sbits which can be indexed ?

I would like to to use the bit addressable memory range (bdata) with an index variable as in the following example:

#define SETBIT(I) WAIT_4_MUTEX; flags^I=1; FREE_MUTEX;

unsigned char bdata flags;

func () {
unsigned char i;
  for (i=0; i < MAX_CELL; i++) {
    // do interesting stuff
    SETBIT(i);
  }
}

This construction of coarse gives a lval error on the use of the SETBIT macro.

My current fall back are the macro's:

#define SETBIT(I) base |= ((unsigned char) (1 << I))
#define CLEARBIT(I) base &= ~((unsigned char) (1 << I))

But still have the feeling that I am wasting cycles on the shift operators in these two macro's and because this is within a mutex region the timing might become an issue.

Is there a better way to set/reset the i-th bit of a bit addressable variable ?

Parents
  • But still have the feeling that I am wasting cycles on the shift operators in these two macro's

    Why would you worry about feelings, when you can so easily replace them by certainty? Check the generated code! List files exist for a reason.

    Is there a better way to set/reset the i-th bit of a bit addressable variable ?

    If "I" is constant, probably not. It'll optimize to two simple operations, as in

        mov   a, #mask   ; computed from (1 << I)
        orl   base, a
    

    For variable arguments, a table look-up instead of the shift would probably be better --- the 8051's shift opcodes are really rather limited, so a run-time computed shift-by-(n) is quite inefficient.

    And of course, if all else fails, there's always Duff's Device. If you haven't heard of that before, let me put like this: you want to have seen it. And once you have, you may wish you hadn't... ;-)

Reply
  • But still have the feeling that I am wasting cycles on the shift operators in these two macro's

    Why would you worry about feelings, when you can so easily replace them by certainty? Check the generated code! List files exist for a reason.

    Is there a better way to set/reset the i-th bit of a bit addressable variable ?

    If "I" is constant, probably not. It'll optimize to two simple operations, as in

        mov   a, #mask   ; computed from (1 << I)
        orl   base, a
    

    For variable arguments, a table look-up instead of the shift would probably be better --- the 8051's shift opcodes are really rather limited, so a run-time computed shift-by-(n) is quite inefficient.

    And of course, if all else fails, there's always Duff's Device. If you haven't heard of that before, let me put like this: you want to have seen it. And once you have, you may wish you hadn't... ;-)

Children