Fast Division/Modulo Operation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • silentlights

    Fast Division/Modulo Operation

    Hi,
    Is there a possibiliy to improve division or Modulo operations in the
    following,

    tmp1 = 123;
    tmp2 = 123;
    frame[8] = ((char)((tmp1/100)+48)); // Division
    tmp1 = (tmp2 % 100); // Mod
    frame[9] = ((char)((tmp1/10)+48));
    tmp1 = (tmp2 % 10);

    tmp 1& 2 are unsigned int and frame is a char array.

    This is in a 16bit environment. I cant use any math libraries. Is there a
    possibility of shifting or subtration methods..?

    Thanks in advance.
    Densil


  • CBFalconer

    #2
    Re: Fast Division/Modulo Operation

    silentlights wrote:[color=blue]
    >
    > Is there a possibiliy to improve division or Modulo operations in
    > the following,
    >
    > tmp1 = 123;
    > tmp2 = 123;
    > frame[8] = ((char)((tmp1/100)+48)); // Division
    > tmp1 = (tmp2 % 100); // Mod
    > frame[9] = ((char)((tmp1/10)+48));
    > tmp1 = (tmp2 % 10);
    >
    > tmp 1& 2 are unsigned int and frame is a char array.
    >
    > This is in a 16bit environment. I cant use any math libraries. Is
    > there a possibility of shifting or subtration methods..?[/color]

    Search google for an article I wrote in roughly the past month or
    two. The subject included "double dabble", and was either here or
    in comp.arch.embed ded or comp.programmin g.

    --
    A: Because it fouls the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?


    Comment

    • silentlights

      #3
      Re: Fast Division/Modulo Operation

      Hi,
      For a beginer, your article


      is not so clear to be implemted on this particular problem. Could you
      please explain me a bit more relevant to this question.

      Thanks
      Densil

      Comment

      • Dik T. Winter

        #4
        Re: Fast Division/Modulo Operation

        In article <fd716cf0a8ddb0 8accff2429e8463 c07@localhost.t alkaboutprogram ming.com> "silentligh ts" <silentlights@y ahoo.co.uk> writes:[color=blue]
        > Hi,
        > Is there a possibiliy to improve division or Modulo operations in the
        > following,
        >
        > tmp1 = 123;
        > tmp2 = 123;
        > frame[8] = ((char)((tmp1/100)+48)); // Division
        > tmp1 = (tmp2 % 100); // Mod
        > frame[9] = ((char)((tmp1/10)+48));
        > tmp1 = (tmp2 % 10);
        >
        > tmp 1& 2 are unsigned int and frame is a char array.[/color]

        tmp1 *= 41;
        f8 = (tmp1 >> 12) + 48;
        tmp1 = (tmp1 & 0x0fff) * 10;
        f9 = (tmp1 >> 12) + 48;
        tmp1 = ((tmp1 & 0x0fff) * 10) >> 12;

        works as long as tmp1 is smaller than 1000 initially.
        --
        dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
        home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

        Comment

        • Dik T. Winter

          #5
          Re: Fast Division/Modulo Operation

          In article <Hw9oLD.CCE@cwi .nl> "Dik T. Winter" <Dik.Winter@cwi .nl> writes:[color=blue]
          > In article <fd716cf0a8ddb0 8accff2429e8463 c07@localhost.t alkaboutprogram ming.com> "silentligh ts" <silentlights@y ahoo.co.uk> writes:[color=green]
          > > Hi,
          > > Is there a possibiliy to improve division or Modulo operations in the
          > > following,
          > >
          > > tmp1 = 123;
          > > tmp2 = 123;
          > > frame[8] = ((char)((tmp1/100)+48)); // Division
          > > tmp1 = (tmp2 % 100); // Mod
          > > frame[9] = ((char)((tmp1/10)+48));
          > > tmp1 = (tmp2 % 10);
          > >
          > > tmp 1& 2 are unsigned int and frame is a char array.[/color]
          >
          > tmp1 *= 41;
          > f8 = (tmp1 >> 12) + 48;
          > tmp1 = (tmp1 & 0x0fff) * 10;
          > f9 = (tmp1 >> 12) + 48;
          > tmp1 = ((tmp1 & 0x0fff) * 10) >> 12;[/color]

          Ok, if you also want to eliminate all multiplications :

          tmp1 += ((tmp1 << 2) + tmp1) << 3;
          f8 = (tmp1 >> 12) + 48;
          tmp1 &= 0x0fff;
          tmp1 += tmp1 << 2;
          f9 = (tmp1 >> 11) + 48;
          tmp1 &= 0x0fff;
          tmp1 += tmp1 << 2;
          tmp1 >>= 10;
          --
          dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
          home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

          Comment

          • E. Robert Tisdale

            #6
            Re: Fast Division/Modulo Operation

            silentlights wrote:
            [color=blue]
            > Is there a possibility to improve division or Modulo operations
            > in the following,
            >
            > tmp1 = 123;
            > tmp2 = 123;
            > frame[8] = ((char)((tmp1/100)+48)); // Division
            > tmp1 = (tmp2 % 100); // Mod
            > frame[9] = ((char)((tmp1/10)+48));
            > tmp1 = (tmp2 % 10);
            >
            > tmp 1& 2 are unsigned int and frame is a char array.
            >
            > This is in a 16bit environment. I can't use any math libraries.
            > Is there a possibility of shifting or subtraction methods..?[/color]

            DIV(3) Linux Programmer’s Manual DIV(3)

            NAME
            div - computes the quotient and remainder of integer division

            SYNOPSIS
            #include <stdlib.h>

            div_t div(int numer, int denom);

            DESCRIPTION
            The div() function computes the value numer/denom
            and returns the quotient and remainder in a structure named div_t
            that contains two integer members named quot and rem.

            RETURN VALUE
            The div_t structure.

            CONFORMING TO
            SVID 3, BSD 4.3, ISO 9899

            Comment

            Working...