Way to find location of the first 1 bit from MSB

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Way to find location of the first 1 bit from MSB

跳至解决方案
4,105 次查看
Gildarai
Contributor II

I use s12zvml and Code warrior for MCU v11.1.

I want to make a code to find first bit value is 1 from MSB.

And I try to use assembly or builtin function such like "__builtin_fbcl" from xc16. because of it have to called rapidly periodic, I'm tring to reduce execution time.

Please help me.

标记 (3)
0 项奖励
回复
1 解答
3,865 次查看
fqh
Contributor II

Hi,

You can use CLB (Count Leading Sign-Bits) instruction to get the leading bit position. But there is no intrinsic function to do this. Embedded asm is a better solution.

#define findLeadingBit32(val, num) {asm LD D6, val; asm CLB D6, D0; asm LD D1, #30; asm SUB D1, D0; asm ST D1, num;}

U8 numbit;
U32 value;

value = 0x8000;

findLeadingBit32(value, numbit);

NOTE: for 16 bits or 8 bits, the macro should be changed accordingly. If value is 0, numbit will be 0xFF.

在原帖中查看解决方案

0 项奖励
回复
4 回复数
3,866 次查看
fqh
Contributor II

Hi,

You can use CLB (Count Leading Sign-Bits) instruction to get the leading bit position. But there is no intrinsic function to do this. Embedded asm is a better solution.

#define findLeadingBit32(val, num) {asm LD D6, val; asm CLB D6, D0; asm LD D1, #30; asm SUB D1, D0; asm ST D1, num;}

U8 numbit;
U32 value;

value = 0x8000;

findLeadingBit32(value, numbit);

NOTE: for 16 bits or 8 bits, the macro should be changed accordingly. If value is 0, numbit will be 0xFF.

0 项奖励
回复
4,078 次查看
lama
NXP TechSupport
NXP TechSupport

HI,

 

If you use C coding then simply

if (variable & 0x0100) { .... ;}

else { ...  ;}

 

If you use ASM coding then (I am not sure about syntax but insgructions you can use are presented):
CPU S12Z Reference Manual -. download or atttached

   LDD D6, variable

   BRCLR D6,#8, jump1

   nop

   BRA jump2

jump1:

   nop

jump2::

 

Best regards,

Ladislav

 

0 项奖励
回复
4,060 次查看
Gildarai
Contributor II

Thank you Ladislav.

I want to make a function that finds and returns the number of bits starting from the MSB that are first 1.

 

if I make C code,

 

unsigned int findbitexample(unsigned int value)

{

    unsigned int bitMask = 0x8000;

    unsigned int bitCount;

 

    while(bitCount <= 15){

        if((bitMask & value) == 0){

            bitMask = bitMask >> 1;

            bitCount = bitCount+1;

        }

        else{

            break;

        }

    }

    return bitCount;

}

 

But I cannot use while/for loop problem with task execution time.

So I'm trying to make assembly code or use builtin function if it can be.

 

0 项奖励
回复
4,037 次查看
lama
NXP TechSupport
NXP TechSupport

Hi,

speed is usually paid for by size. This is just suggestion how to exclude/bypass parameters passing, cycles logic,....   probably it is faster, you can test.

#include <hidef.h>      /* common defines and macros */

#include "derivative.h"      /* derivative-specific definitions */

//**********************************************************************

//global variables to do not send parameter to function

volatile unsigned int value;

volatile unsigned char *pvH=(unsigned char*)(&value);

volatile unsigned char *pvL=(unsigned char*)    (   ( (unsigned char*)(&value) )        +1);

//**********************************************************************

unsigned char find1(void)

{

  if(*pvH)

   {

    if(*pvH & 0B11110000) 

     {

       if(*pvH & 0B11000000) 

        {

         if(*pvH & 0B10000000) 

          {

            return 15;  //1000 0000

          } 

         else

          {

            return 14;  //0100 0000

          }

        } 

       else

        {

         if(*pvH & 0B00100000) 

          {

            return 13;  //0010 0000

          } 

         else

          {

            return 12;  //0001 0000

          }

        }

     } 

    else

     {

       if(*pvH & 0B00001100) 

        {

         if(*pvH & 0B00001000) 

          {

            return 11;  //1000 0000

          } 

         else

          {

            return 10;  //0100 0000

          }

        } 

       else

        {

         if(*pvH & 0B00000010) 

          {

            return 9;  //0010 0000

          } 

         else

          {

            return 8;  //0001 0000

          }

        }

 

     }

   } 

  else 

   {

    if(*pvL & 0B11110000) 

     {

       if(*pvL & 0B11000000) 

        {

         if(*pvL & 0B10000000) 

          {

            return 7;  //1000 0000

          } 

         else

          {

            return 6;  //0100 0000

          }

        } 

       else

        {

         if(*pvL & 0B00100000) 

          {

            return 5;  //0010 0000

          } 

         else

          {

            return 4;  //0001 0000

          }

        }

     } 

    else

     {

       if(*pvL & 0B00001100) 

        {

         if(*pvL & 0B00001000) 

          {

            return 3;  //1000 0000

          } 

         else

          {

            return 2;  //0100 0000

          }

        } 

       else

        {

         if(*pvL & 0B00000010) 

          {

            return 1;  //0010 0000

          } 

         else

          {

            return 0;  //0001 0000

          }

        }

 

     }

 

   

   }

  return 55;

}

//**********************************************************************

void main(void) 

{

  static unsigned char pos;

 

  for(;;) 

   {

     for(value=0; value < 0xFFFF; value++) 

      {

        if (value != 0)

            pos = find1();

      }

   }

}

0 项奖励
回复
%3CLINGO-SUB%20id%3D%22lingo-sub-1998377%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%E4%BB%8E%20MSB%20%E6%9F%A5%E6%89%BE%E7%AC%AC%E4%B8%80%E4%B8%AA%201%20%E4%BD%8D%E7%9A%84%E4%BD%8D%E7%BD%AE%E7%9A%84%E6%96%B9%E6%B3%95%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1998377%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%E6%88%91%E4%BD%BF%E7%94%A8%20s12zvml%20%E5%92%8C%20Code%20warrior%20for%20MCU%20v11.1%E3%80%82%3C%2FP%3E%3CP%3E%E6%88%91%E6%83%B3%E7%BC%96%E5%86%99%E4%B8%80%E4%B8%AA%E4%BB%A3%E7%A0%81%E6%9D%A5%E4%BB%8E%20MSB%20%E4%B8%AD%E6%89%BE%E5%87%BA%E7%AC%AC%E4%B8%80%E4%B8%AA%E4%BD%8D%E7%9A%84%E5%80%BC%E6%98%AF%E5%90%A6%E4%B8%BA%201%E3%80%82%3C%2FP%3E%3CP%3E%E6%88%91%E5%B0%9D%E8%AF%95%E4%BD%BF%E7%94%A8%E6%B1%87%E7%BC%96%E6%88%96%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0%EF%BC%8C%E4%BE%8B%E5%A6%82%20xc16%20%E4%B8%AD%E7%9A%84%E2%80%9C__builtin_fbcl%E2%80%9D%E3%80%82%E5%9B%A0%E4%B8%BA%E5%AE%83%E5%BF%85%E9%A1%BB%E5%91%A8%E6%9C%9F%E6%80%A7%E5%9C%B0%E5%BF%AB%E9%80%9F%E8%B0%83%E7%94%A8%EF%BC%8C%E6%89%80%E4%BB%A5%E6%88%91%E8%AF%95%E5%9B%BE%E5%87%8F%E5%B0%91%E6%89%A7%E8%A1%8C%E6%97%B6%E9%97%B4%E3%80%82%3C%2FP%3E%3CP%3E%E8%AF%B7%E5%B8%AE%E6%88%91%E3%80%82%3C%2FP%3E%3C%2FLINGO-BODY%3E