Jump to content

Recommended Posts

Posted

hello,

I'm a C programmer I'd like to translate this function into assembler code (because it needs to be optimized) :

// Integer division (a div b)

unsigned int ADK_DIV(unsigned int a, unsigned int b)

{

  unsigned int reste = 0;

  unsigned char count = 16;

  char c;

  do

  {

    c = ((a >> (8*sizeof(a)-1)) & 1);

    a <<= 1;

    reste <<= 1;

    if © reste |= 1;

    if (reste >= b)

    {

      reste -= b;

      a |= 1;

    }

  }

  while (--count);

  return a;

}

Thanks for anybody who could help me !

Goule

Posted

Hi Goule,

in the clockbox application you will find an assembly based division routine in "mclock.c" (search for __asm)

However, here another hint: if your program just only converts value A to B based on static parameters, then you could also use a table (constant array) with the required number of entries. This is significantly faster, and for common CC's it doesn't consume that much memory (e.g. a table of 128 entries allocate the same memory like 64 instructions)

I'm normaly using perl scripts to calculate such tables, example:


#!/usr/bin/perl

print "const unsigned char exp_table[128] = {\n  ";

int i;
for($i=0; $i<128; ++$i)
{
  my $value = int(0x80 * (1-exp(-$i/32)));
  printf "0x%02x, ", $value;

  if( !(($i+1) % 8) ) { # linebreak
    printf "\n  ";
  }
}

print "};\n";
[/code] generates:
[code]
const unsigned char exp_table[128] = {
  0x00, 0x03, 0x07, 0x0b, 0x0f, 0x12, 0x15, 0x19,
  0x1c, 0x1f, 0x22, 0x25, 0x28, 0x2a, 0x2d, 0x2f,
  0x32, 0x34, 0x37, 0x39, 0x3b, 0x3d, 0x3f, 0x41,
  0x43, 0x45, 0x47, 0x48, 0x4a, 0x4c, 0x4d, 0x4f,
  0x50, 0x52, 0x53, 0x55, 0x56, 0x57, 0x58, 0x5a,
  0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62,
  0x63, 0x64, 0x65, 0x65, 0x66, 0x67, 0x68, 0x69,
  0x69, 0x6a, 0x6b, 0x6b, 0x6c, 0x6c, 0x6d, 0x6e,
  0x6e, 0x6f, 0x6f, 0x70, 0x70, 0x71, 0x71, 0x72,
  0x72, 0x72, 0x73, 0x73, 0x74, 0x74, 0x74, 0x75,
  0x75, 0x75, 0x76, 0x76, 0x76, 0x77, 0x77, 0x77,
  0x77, 0x78, 0x78, 0x78, 0x78, 0x79, 0x79, 0x79,
  0x79, 0x79, 0x7a, 0x7a, 0x7a, 0x7a, 0x7a, 0x7a,
  0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7b, 0x7c,
  0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c, 0x7c,
  0x7c, 0x7d, 0x7d, 0x7d, 0x7d, 0x7d, 0x7d, 0x7d,
  };

Best Regards, Thorsten.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...