This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to load a single precise data in linear assembly code?

I coded the following function into linear assembly:

double dotp(float * a, float *b,int cnt)

{          int i = 0;

           double sum = 0.0;

          for(i = 0; i < cnt ; i ++)

         sum += a[I]*b[i];

         return sum;

}

             .def        _dotp

_dotp:   .cproc     a, b ,cnt

              .reg         val0,val1

             .reg         prod1:prod0, sum1:sum0

loop:   

             LDW         *a++, val0

            LDW          *b++, val1

            MPYSP2DP     val0, val1, prod1:prod0

            ADDDP        prod1:prod0, sum1:sum0, sum1:sum0

[cnt]     SUB            cnt,1,cnt

[cnt]     B                  loop

             .return     sum1:sum0

             .endproc

           I used some data to test the above linear assembly code, but got wrong result. Using single step debug, I found that "LDW         *a++, val0"  can't get right value for val0. Does LDW can only load integrate data? What's wrong with the above linear assembly code?

          Looking forward to somebody can help me!

          May

 

  • Hi May,

    First, the LDW instruction doesn't distinguish between fixed and floating data which you want to load, what it see is just a 32-bit content located in the target memory. The result should be ok if you check the register val0, or you can paste the array prototype here for us to have a try.

    Second, it seems you miss the step to clear sum1:sum0 to zero before the loop in your linear assembly code, so the ADDDP instruction will produce wrong result if there is unexpected sum1:sum0 as input.

    Allen

     

  • Hi Allen,

           I got the conclusion, because I tried the following code:

               .def        _dotp

    _dotp:   .cproc     a, b ,cnt

                  .reg         val0,val1

                 .reg         prod1:prod0, sum1:sum0

    ;loop:   

                 LDW         *a++, val0

    ;            LDW          *b++, val1

      ;          MPYSP2DP     val0, val1, prod1:prod0

        ;        ADDDP        prod1:prod0, sum1:sum0, sum1:sum0

    ;[cnt]     SUB            cnt,1,cnt

    ;[cnt]     B                  loop

            ;     .return     sum1:sum0

                .return   val0

                 .endproc

    void main()

    {

                         double c = 0.0;

                          float a[10] = {0.1};

                         float    b[10] = {0.2}; 

                          c = dotp(a,b,1);

                         printf("%lf\n",c);

    }

    the result of c is wrong!

      Can you tell me why?

       Thanks, Allen

  • Hi May,

    In your program, c is defined as double type, but the dotp function just load a 32-bit data and pass it to val0, so the MSB of c will be zero.

    Just modify the c as float type then you can get right result.

    Allen