Menu

About the library source code

2017-12-29
2018-01-04
  • Chingson Chen

    Chingson Chen - 2017-12-29

    I see the library code seems mostly done in 1999. The code works, but it feels me a little dump after some checking.
    For example, the "_divuint"

     do
      {
        // reste: x <- 0;
       ** c = MSB_SET(x);
        x <<= 1;
        reste <<= 1;
        if (c)
          reste |= 1;**
    
        if (reste >= y)
        {
          reste -= y;
          // x <- (result = 1)
          x |= 1;
        }
      }
    

    will it be equal to

     do
      {
        // reste: x <- 0;
    
        **reste <<= 1;
        if (MSB_SET(x))
          reste |= 1;
        x<<=1;**
    
        if (reste >= y)
        {
          reste -= y;
          // x <- (result = 1)
          x |= 1;
        }
      }
    // which saves a variable and some lines of codes
    

    also the _mod operation

    **while (!MSB_SET(b))
      {
        b <<= 1;
        if (b > a)
        {
          b >>=1;
          break;
        }
        count++;
      }**
      do
      {
        if (a >= b)
          a -= b;
        b >>= 1;
      }
      while (count--);
    

    why not write as

    **while (!MSB_SET(b) && b<=a)
      {
        b <<= 1;
        count++;
      }
    **  do
      {
        if (a >= b)
          a -= b;
        b >>= 1;
      }
    
    // this also saves a right shift of b
    

    Though several bytes of code will not effect the correctness, shorter code let embedded systems enters sleep mode earlier and saves energy, which also saves our earth.

    Just ..free discussion.

    Chingson

     

    Last edit: Maarten Brock 2018-08-14
  • Maarten Brock

    Maarten Brock - 2018-01-03

    But is the generated asm also shorter and/or faster? Shorter C does not automatically mean better asm. For which target are you compiling?

     
  • Chingson Chen

    Chingson Chen - 2018-01-04

    In "_divuxxx", assignment of "c" is hard to be optimized. Also, in "__moduxxx", shift left (<<) is also hard to be optimized. Yes, we will get shorter code, at least for PIC series.

     

Log in to post a comment.