Re: Rotating through a four-bit nibble

"Kevin L. Clague" <[email protected]>
Newsgroups gmane.comp.hardware.lego.robotics
Organization None
Message-ID <[email protected]>
In lugnet.robotics, Kevin L. Clague wrote:
> In lugnet.robotics, Brian Davis wrote:
>>    No, it's not pornographic (look over in rtlToronto - I hear it's a fun
>> thread). But I'm working in NQC, and I need to rotate the bit series in a 4-bit
>> (or 8-bit) nibble embedded in a 16-bit word. Annoying. Any suggestions? Masking
>> the desired four bits out of a storage variable and into a temporary variable
>> four times (so it's a repeating pattern) and then rotating and masking out the
>> desired sequence might work, but is there a better way? Bit-level manipulation
>> is not my strong suite.
>>
>> (x's here are "something else" that I may wish to keep intact, y is garbage or
>> potentially trashed bits)
>>
>> stored sequence in 16-bit word:        xxxx xxxx 0110 xxxx
>> 4-bit nibble copied across temporary:  xxxx 0110 0110 0110
>> left shift temporary, say, twice:      xx01 1001 1001 10yy
>> mask out one nibble back into storage: xxxx xxxx 1001 xxxx
>>
>> There's got to be a better way. I'm using NQC, so I'm stuck with those big
>> 16-bit words and basic operations like bit logic, shifts, and interger division
>> (and of course modulo division).
>>
>>    Anybody?

Since I didn't really complete the assignment I'll try again.  I didn't honor
the x bit request.

I confirmed that even SDK v2.5 does not have shifts or rotates, and that in NQC
you can only do shifts based on constant amounts.

The two routines below allouw rotation by a valiable amount.  If you know at
compile what the rotate amount is, then you can make this more efficient


void
rotate_right(   // this one requires two scratch variables.
  int value,
  int amount)
{
   int l,r;

   // truncate rotate amount to 3:0, anything higher
   // just maps back down to 3:0

   amount = amount & 3;   /* size -1 */

   switch (amount) {
     case 0:
       return;
     break;
     case 1:              // 3210 becomes 0321
       r  = value / 2;
       r &= 0x70;         // -321
       l  = value * 8;
       l &= 0x80;         // 0---
     break;
     case 2:              // 3210 becomes 1032
       r  = value / 4;
       r &= 0x30;         // --32
       l  = value * 4;
       l &= 0xc0;         // 10--
     break;
     case 3:              // 3210 becomes 2103
       r  = value / 8;
       r &= 0x10;         // ---3
       l  = value * 2;
       l &= 0xe0;         // 210-
     break;
   }
   value &= 0xff0f;   // zero out old value
   value |= r | l;    // merge in new value
}

void
rotate_right2(  // this one trashes amount, but only uses one scratch
  int value,
  int amount)
{
   int l;

   // truncate rotate amount to 3:0, anything higher
   // just maps back down to 3:0

   amount = amount & 3;   /* size -1 */

   switch (amount) {
     case 0:
       return;
     break;
     case 1:              // 3210 becomes 0321
       amount  = value / 2;
       amount &= 0x70;         // -321
       l  = value * 8;
       l &= 0x80;         // 0---
     break;
     case 2:              // 3210 becomes 1032
       amount  = value / 4;
       amount &= 0x30;         // --32
       l  = value * 4;
       l &= 0xc0;         // 10--
     break;
     case 3:              // 3210 becomes 2103
       amount  = value / 8;
       amount &= 0x10;         // ---3
       l  = value * 2;
       l &= 0xe0;         // 210-
     break;
   }
   value &= 0xff0f;   // zero out old value
   value |= amount | l;    // merge in new value
}

/*
 * If you only want 2 bit rotate in the four bit field, try this
 */

void rotate2(int value)
{
  int l, r;

  r = (value / 4) & 0x30;
  l = (value * 4) & 0xc0;
  
  value &= 0xff0f;
  value |= r | l;
}

rotate2 compiles to 10 LASM byte codes.

You can see all the routine's LASM byte codes using

nqc -TRCX2 -L -c -s rotate.nqc

Kev
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.