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.

[参考译文] TMS320F28388D:在启用 TMU1的情况下、TI libc 中缺少标准数学函数的内在函数替代

Guru**** 2391215 points


请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/c2000-microcontrollers-group/c2000/f/c2000-microcontrollers-forum/1502569/tms320f28388d-lack-of-intrinsic-substitution-for-standard-math-functions-in-ti-s-libc-with-tmu1-enabled

器件型号:TMS320F28388D

工具/软件:

TL;DR: atan2fsinfcosf, <cmath><math.h>, tATANPUF32SINPUF32COSPUF32当显式启用 TMU 支持(通过--tmu_support=tmu1)且适当设置优化标志(-O4--opt_for_speed=5等)时、为什么 TI 编译器(例如 cl2000 v22.6.1.LTS)不自动映射标准数学函数、如、、并通过或包含其相应的 TMU 加速内在函数(例如、、、)?

在以下说明性案例中:

#include <cmath>

float test(float x, float y) {
    float a = atan2(x, y);
    float b = sin(a);
    float c = cos(a);
    return a + b + c;
}

编译后的输出清楚地表明、atan2fsinfcosf尽管存在 TMU 和 FPU64支持、但标准、和函数被称为外部符号:

        LCR       #||atan2f||           
        ...
        LCR       #||sinf||             
        ...
        LCR       #||cosf||  

只有当用显式调用内在函数(__atan2,__sin__cos)的内联包装器手动覆盖标准函数时,才会生成基于 TMU 的高效代码生成:

inline float atan2(float x, float y) { return __atan2(y, x); }
inline float sin(float x) { return __sin(x); }
inline float cos(float x) { return __cos(x); }

这将导致预期使用硬件指令:

        ATANPUF32 R0H,R0H
        ...
        SINPUF32  R1H,R1H
        COSPUF32  R2H,R2H

鉴于启用了 TMU 支持、我们希望编译器和标准库头能够检测目标架构并以透明方式将这些高级数学函数映射到其经过优化的硬件加速函数。

构建命令如下所示:

/opt/ti/ti-cgt-c2000_22.6.1.LTS/bin/cl2000 -c \
-D=USE_20MHZ_XTAL -D=_FLASH -D=CPU1 -D=__TMS320C28XX__ \
--issue_remarks --abi=eabi --tmu_support=tmu1 \
--float_support=fpu64 --gen_opt_info=2 \
-v28 -ml -O4 -op=3 --c_src_interlist --auto_inline \
--verbose_diagnostics --advice:performance=all \
--opt_for_speed=5 --preproc_with_compile --keep_asm \
-I/opt/ti/ti-cgt-c2000_22.6.1.LTS/include -z main.obj -o exec.out

TI 标准 C 库(math.h)和 C++包装器(<cmath>)是否集成了特定于目标的逻辑(例如、通过条件宏或内联定义)、以便在可用时将标准数学函数重定向到硬件内在函数? 为什么此重定向完全留给用户? 是否有编译器标志或 TI 提供的配置可以在 TMU 支持的编译版本下启用到内在函数的自动映射?

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    很抱歉耽误回复。 为此所需的设置为 fp_mode=relaxed

    2.3.3的 https://www.ti.com/lit/spru514

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    太棒了,这正是我想要的。  

    #include <cmath>
    
    float test(float x, float y) {
        float a = atan2(x, y);
        float b = sinf(a);
        float c = cosf(a);
        float d = a * 2 * M_PI;
        return a + b + c + d;
    }
    

    ||_Z4testff||:
    ;* R0    assigned to $O$C1
    ;* R0    assigned to x
    ;* R1    assigned to y
            QUADF32   R1H,R0H,R0H,R1H       ; [CPU_FPU] |9|
            NOP       ; [CPU_ALU]
            NOP       ; [CPU_ALU]
            NOP       ; [CPU_ALU]
            NOP       ; [CPU_ALU]
            ATANPUF32 R0H,R0H               ; [CPU_FPU] |9|
            NOP       ; [CPU_ALU]
            NOP       ; [CPU_ALU]
            NOP       ; [CPU_ALU]
            ADDF32    R0H,R0H,R1H           ; [CPU_FPU] |9|
            NOP       ; [CPU_ALU]
            NOP       ; [CPU_ALU]
            MPY2PIF32 R0H,R0H               ; [CPU_FPU] |9|
            NOP       ; [CPU_ALU]
            MOVIZ     R1H,#16457            ; [CPU_FPU] |9|
            DIV2PIF32 R2H,R0H               ; [CPU_FPU] |9|
            DIV2PIF32 R3H,R0H               ; [CPU_FPU] |9|
            SINPUF32  R2H,R2H               ; [CPU_FPU] |9|
            ADDF32    R4H,R0H,R0H           ; [CPU_FPU] |9|
    ||      MOV32     *SP++,R4H             ; [CPU_FPU]
            COSPUF32  R3H,R3H               ; [CPU_FPU] |9|
            MOVXI     R1H,#4059             ; [CPU_FPU] |9|
            ADDF32    R0H,R0H,R2H           ; [CPU_FPU] |9|
            MPYF32    R1H,R1H,R4H           ; [CPU_FPU] |9|
            ADDF32    R0H,R0H,R3H           ; [CPU_FPU] |9|
            NOP       ; [CPU_ALU]
            ADDF32    R0H,R0H,R1H           ; [CPU_FPU] |9|
    ||      MOV32     R4H,*--SP             ; [CPU_FPU]
            LRETR     ; [CPU_ALU]