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.

IQmath error on host w/ C6xSim

Other Parts Discussed in Thread: SPRC542

I downloaded the IQmath lib (http://www.ti.com/tool/sprc542) and the C6xsim intrisics (as documented here: http://processors.wiki.ti.com/index.php/Run_Intrinsics_Code_Anywhere ) and wrote some code for the DSP that I compile for my linux host, for test/debug. The IQmath function _IQNmag performs a sqrt(a^2+b^2) per the documentation "avoiding overflow problems" however, what I see is overflowed occur well beyond the expected results. Interestingly, the problem only exists for the code compiled for the host. When I compile the code for the target and run it on the host via the commandline simulator it runs as expected. I suspect the problem is with the C6xsim, but I don't know for sure. I'd really like to be able to count on the code I compile for the host. Can anyone offer insight/resolution?

 

$ cat bug.c

#include <math.h>
#include <stdio.h>
#include "IQmath_inline_all.h"

int main(int argc, char **argv)
{
        int i;
        _iq a, b, c;

        printf("FLOAT  a = %1.15f b %1.15f c = %1.15f\n", 3.8, 3.9, sqrtf(3.8*3.8+3.9*3.9));
        for (i = 31; i >= 0; i--)
        {
                a = _FtoIQN(3.8, i);
                b = _FtoIQN(3.9, i);
                c = _IQNmag(a, b, i);
                printf("i = %2d a = %1.15f b %1.15f c = %1.15f\n", i, _IQNtoF(a, i), _IQNtoF(b, i), _IQNtoF(c, i));
        }

        return 0;
}

$ ./bug # compiled for host, lots of unexpected results

FLOAT  a = 3.800000000000000 b 3.900000000000000 c = 5.445181369781494 << "real" answer
i = 31 a = 0.000000000465661 b 0.000000000465661 c = 0.000000000465661 << expected failure, input overflows accepted range
i = 30 a = 0.000000000000000 b 0.000000000000000 c = 0.000000000000000 << expected failure, input overflows accepted range
i = 29 a = 3.799999952316284 b 3.900000095367432 c = 3.999999761581421 << expected failure, output overflows accepted range
i = 28 a = 3.799999952316284 b 3.900000095367432 c = 0.000000000000000 << unexpected failure, only the intermediate overflows 32 bits
i = 27 a = 3.799999952316284 b 3.900000095367432 c = 5.471137046813965
i = 26 a = 3.799999952316284 b 3.900000095367432 c = 0.000000000000000
i = 25 a = 3.799999952316284 b 3.900000095367432 c = 0.000000000000000
i = 24 a = 3.799999952316284 b 3.900000095367432 c = 5.631754875183105
i = 23 a = 3.799999952316284 b 3.900000095367432 c = 0.000000000000000
i = 22 a = 3.799999952316284 b 3.900000095367432 c = 5.451299667358398
i = 21 a = 3.799999713897705 b 3.900000095367432 c = 0.000000000000000
i = 20 a = 3.799999237060547 b 3.899999618530273 c = 5.473990440368652
i = 19 a = 3.799999237060547 b 3.899999618530273 c = 0.000000000000000
i = 18 a = 3.799999237060547 b 3.899997711181641 c = 5.451274871826172
i = 17 a = 3.799995422363281 b 3.899993896484375 c = 0.000000000000000
i = 16 a = 3.799987792968750 b 3.899993896484375 c = 0.000000000000000
i = 15 a = 3.799987792968750 b 3.899993896484375 c = 5.648529052734375
i = 14 a = 3.799987792968750 b 3.899963378906250 c = 0.000000000000000
i = 13 a = 3.799926757812500 b 3.899902343750000 c = 5.445068359375000 << first "reliable" answer
i = 12 a = 3.799804687500000 b 3.899902343750000 c = 5.445068359375000 << from here down, as expected... losing precision
i = 11 a = 3.799804687500000 b 3.899902343750000 c = 5.444824218750000
i = 10 a = 3.799804687500000 b 3.899414062500000 c = 5.444335937500000
i =  9 a = 3.798828125000000 b 3.898437500000000 c = 5.443359375000000
i =  8 a = 3.796875000000000 b 3.898437500000000 c = 5.441406250000000
i =  7 a = 3.796875000000000 b 3.898437500000000 c = 5.445312500000000
i =  6 a = 3.796875000000000 b 3.890625000000000 c = 5.437500000000000
i =  5 a = 3.781250000000000 b 3.875000000000000 c = 5.406250000000000
i =  4 a = 3.750000000000000 b 3.875000000000000 c = 5.375000000000000
i =  3 a = 3.750000000000000 b 3.875000000000000 c = 5.375000000000000
i =  2 a = 3.750000000000000 b 3.750000000000000 c = 5.250000000000000
i =  1 a = 3.500000000000000 b 3.500000000000000 c = 5.000000000000000
i =  0 a = 3.000000000000000 b 3.000000000000000 c = 4.000000000000000

$ loadti.sh -q -c c64xp_le_sim.ccxml bug.out # compiled for c64x+, all results as expected

testEnv.outFiles: bug.out
FLOAT  a = 3.800000000000000 b = 3.900000000000000 c = 5.445181369781494
i = 31 a = 0.000000000000000 b = 0.000000000000000 c = 0.000000000000000
i = 30 a = 0.000000000000000 b = 0.000000000000000 c = 0.000000000000000
i = 29 a = 3.799999952316284 b = 3.900000095367432 c = 3.999999761581421
i = 28 a = 3.799999952316284 b = 3.900000095367432 c = 5.445181369781494
i = 27 a = 3.799999952316284 b = 3.900000095367432 c = 5.445181369781494
i = 26 a = 3.799999952316284 b = 3.900000095367432 c = 5.445181369781494
i = 25 a = 3.799999952316284 b = 3.900000095367432 c = 5.445181369781494
i = 24 a = 3.799999952316284 b = 3.900000095367432 c = 5.445181369781494
i = 23 a = 3.799999952316284 b = 3.900000095367432 c = 5.445181369781494
i = 22 a = 3.799999952316284 b = 3.900000095367432 c = 5.445181369781494
i = 21 a = 3.799999713897705 b = 3.900000095367432 c = 5.445181369781494
i = 20 a = 3.799999237060547 b = 3.899999618530273 c = 5.445180892944336
i = 19 a = 3.799999237060547 b = 3.899999618530273 c = 5.445180892944336
i = 18 a = 3.799999237060547 b = 3.899997711181641 c = 5.445178985595703
i = 17 a = 3.799995422363281 b = 3.899993896484375 c = 5.445175170898438
i = 16 a = 3.799987792968750 b = 3.899993896484375 c = 5.445175170898438
i = 15 a = 3.799987792968750 b = 3.899993896484375 c = 5.445159912109375
i = 14 a = 3.799987792968750 b = 3.899963378906250 c = 5.445129394531250
i = 13 a = 3.799926757812500 b = 3.899902343750000 c = 5.445068359375000
i = 12 a = 3.799804687500000 b = 3.899902343750000 c = 5.445068359375000
i = 11 a = 3.799804687500000 b = 3.899902343750000 c = 5.444824218750000
i = 10 a = 3.799804687500000 b = 3.899414062500000 c = 5.444335937500000
i =  9 a = 3.798828125000000 b = 3.898437500000000 c = 5.443359375000000
i =  8 a = 3.796875000000000 b = 3.898437500000000 c = 5.441406250000000
i =  7 a = 3.796875000000000 b = 3.898437500000000 c = 5.445312500000000
i =  6 a = 3.796875000000000 b = 3.890625000000000 c = 5.437500000000000
i =  5 a = 3.781250000000000 b = 3.875000000000000 c = 5.406250000000000
i =  4 a = 3.750000000000000 b = 3.875000000000000 c = 5.375000000000000
i =  3 a = 3.750000000000000 b = 3.875000000000000 c = 5.375000000000000
i =  2 a = 3.750000000000000 b = 3.750000000000000 c = 5.250000000000000
i =  1 a = 3.500000000000000 b = 3.500000000000000 c = 5.000000000000000
i =  0 a = 3.000000000000000 b = 3.000000000000000 c = 4.000000000000000

  • looks like I missed the intended forum, and I forgot to include compiler, flags, etc...

    host:

    gcc version 4.1.2 20080704 (Red Hat 4.1.2-48)

    -D_INLINE_IQMATH -DTMS320C64PX -D_TMS320C6X -D_VC_HOST_BUILD -DSIMULATION -D__int64=I64_IQ -DLITTLE_ENDIAN_HOST -IIQMath/include -Ic6xsim

    target:

    TMS320C6x C/C++ Compiler                v7.2.3

    -gcc -g -ol1 -O2 -mv64+ -d"CHIP_DM642" --include_path="$(TI_CGTOOLS_INCDIR)" --preproc_with_compile -d_INLINE_IQMATH -dTMS320C64PX -d_TMS320C6X  -dLITTLE_ENDIAN_HOST --include_path=IQMath/include

     

     

  • I'm sorry I took so long to answer.  I went down many dead ends before I finally found a solution.

    When you build for the host with gcc, remove this option: -D_TMS320C6X .  When that preprocessor symbol is defined, the code presumes the TI C6000 compiler is being used, and not a host compiler like gcc.  The IQmath header files currently look at that symbol.  The current host intrinsics files do not look at the symbol, but a future release will.  

    With this fix, I don't get the exact same answers as when you run on the C6000.  But I think they are close enough.  Here is what I get ...

    FLOAT  a = 3.800000000000000 b = 3.900000000000000 c = 5.445181369781494
    i = 31 a = -1.000000000000000 b = -1.000000000000000 c = 1.000000000000000
    i = 30 a = -2.000000000000000 b = -2.000000000000000 c = 2.000000000000000
    i = 29 a = 3.799999952316284 b = 3.900000095367432 c = 4.000000000000000
    i = 28 a = 3.799999952316284 b = 3.900000095367432 c = 5.445181369781494
    i = 27 a = 3.799999952316284 b = 3.900000095367432 c = 5.445181369781494
    i = 26 a = 3.799999952316284 b = 3.900000095367432 c = 5.445181369781494
    i = 25 a = 3.799999952316284 b = 3.900000095367432 c = 5.445181369781494
    i = 24 a = 3.799999952316284 b = 3.900000095367432 c = 5.445181369781494
    i = 23 a = 3.799999952316284 b = 3.900000095367432 c = 5.445181369781494
    i = 22 a = 3.799999952316284 b = 3.900000095367432 c = 5.445181369781494
    i = 21 a = 3.799999713897705 b = 3.900000095367432 c = 5.445181369781494
    i = 20 a = 3.799999237060547 b = 3.899999618530273 c = 5.445180892944336
    i = 19 a = 3.799999237060547 b = 3.899999618530273 c = 5.445180892944336
    i = 18 a = 3.799999237060547 b = 3.899997711181641 c = 5.445178985595703
    i = 17 a = 3.799995422363281 b = 3.899993896484375 c = 5.445175170898438
    i = 16 a = 3.799987792968750 b = 3.899993896484375 c = 5.445175170898438
    i = 15 a = 3.799987792968750 b = 3.899993896484375 c = 5.445159912109375
    i = 14 a = 3.799987792968750 b = 3.899963378906250 c = 5.445129394531250
    i = 13 a = 3.799926757812500 b = 3.899902343750000 c = 5.445068359375000
    i = 12 a = 3.799804687500000 b = 3.899902343750000 c = 5.445068359375000
    i = 11 a = 3.799804687500000 b = 3.899902343750000 c = 5.444824218750000
    i = 10 a = 3.799804687500000 b = 3.899414062500000 c = 5.444335937500000
    i =  9 a = 3.798828125000000 b = 3.898437500000000 c = 5.443359375000000
    i =  8 a = 3.796875000000000 b = 3.898437500000000 c = 5.441406250000000
    i =  7 a = 3.796875000000000 b = 3.898437500000000 c = 5.445312500000000
    i =  6 a = 3.796875000000000 b = 3.890625000000000 c = 5.437500000000000
    i =  5 a = 3.781250000000000 b = 3.875000000000000 c = 5.406250000000000
    i =  4 a = 3.750000000000000 b = 3.875000000000000 c = 5.375000000000000
    i =  3 a = 3.750000000000000 b = 3.875000000000000 c = 5.375000000000000
    i =  2 a = 3.750000000000000 b = 3.750000000000000 c = 5.250000000000000
    i =  1 a = 3.500000000000000 b = 3.500000000000000 c = 5.000000000000000
    i =  0 a = 3.000000000000000 b = 3.000000000000000 c = 4.000000000000000
    

    The values for i=31 and i=30 are rather different.  The values for i=29 are almost the same.  And the rest match.

    Thanks and regards,

    -George