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

efficient packing

First, I am using 7.5 so it is an old compiler, but that is the way it is.

I have a number of bit variables that I am trying to pack into a byte. they are scattered all across the bit area

volatile bit var1;
volatile bit var2;
volatile bit var3;

char result;

The compiler won't let me do

          result=   (var1 <<1) | (var2 <<2) | (var3 <<4);

the only way I have been able to get this to work is

           result= var1;
           result= ((((result << 1) | var2)<<1) | var3)<<1);

This results in very inefficient code
           mov a,result
           mov  r7,a

           mov   c,v2
           clr  a
           rlc  A
           orl  A,R7;
           add  A,ACC
           mov  a,r7

           mov  c,var3
           rlc  A
           orl  A,R7
           add  A,ACC
           mov  a,r7
etc.
as compared to:

           mov A,result
           mov c,v2
           rlc a
           mov  c,v3
           rlc a

would be much better.  Does anyone know how this might be forced in C, without resorting to assembly?

I'm wondering if because the ACC and PSW are evenly divisible by 8, whether or not you can use
those and explicitly use the _crol_ intrinsic to accomplish this.  I have not had any luck so far.

In this case memory efficiency is more important than speed.


Parents
  • bdata unsigned char sample_result = 0;
    
    sbit var1 = sample_result ^ 0;
    sbit var2 = sample_result ^ 1;
    sbit var3 = sample_result ^ 2;
    
    main ()
    {
            var1 = 0;
            var2 = 1;
            var3 = 1;
    
            sample_result = 0xFFu;
    

    Generated code:

        79:         var1 = 0;
    C:0x070D    C200     CLR      var1(0x20.0)
        80:         var2 = 1;
    C:0x070F    D201     SETB     var2(0x20.1)
        81:         var3 = 1;
        82:
    C:0x0711    D202     SETB     var3(0x20.2)
        83:         sample_result = 0xFFu;
        84:
    C:0x0713    7520FF   MOV      sample_result(0x20),#0xFF
    

Reply
  • bdata unsigned char sample_result = 0;
    
    sbit var1 = sample_result ^ 0;
    sbit var2 = sample_result ^ 1;
    sbit var3 = sample_result ^ 2;
    
    main ()
    {
            var1 = 0;
            var2 = 1;
            var3 = 1;
    
            sample_result = 0xFFu;
    

    Generated code:

        79:         var1 = 0;
    C:0x070D    C200     CLR      var1(0x20.0)
        80:         var2 = 1;
    C:0x070F    D201     SETB     var2(0x20.1)
        81:         var3 = 1;
        82:
    C:0x0711    D202     SETB     var3(0x20.2)
        83:         sample_result = 0xFFu;
        84:
    C:0x0713    7520FF   MOV      sample_result(0x20),#0xFF
    

Children