跳转至

TinyMath 配置

概述

tiny_math_config.h 是 TinyMath 库的 中心配置文件。每个子模块最终都会包含这个文件,使其成为控制平台选择、依赖解析和编译期行为的单一入口点。


架构与依赖流

tiny_math_config.h 位于 包含树的根部:每个子模块都直接包含它,而 tiny_math.h(统一入口)汇总所有子模块。

%%{init: {'themeVariables': {'fontSize': '32px'}}}%%
graph BT
    subgraph Stdlib["C 标准库"]
        STD["stdio.h / stdlib.h / string.h<br/>math.h / stdbool.h / stdint.h"]
    end

    subgraph Independent["独立基础层"]
        TOOL["tiny_toolbox.h<br/><i>FreeRTOS, ESP-IDF,<br/>时间, 日志, 堆</i>"]
        CONST["tiny_constants.h<br/><i>π, √2, 位操作宏</i>"]
        ERR["tiny_error_type.h<br/><i>错误码</i>"]
    end

    subgraph Config["配置中心<br/><b>每个子模块都包含它</b>"]
        CONF["tiny_math_config.h"]
    end

    subgraph Submodules["子模块(各自独立)"]
        VEC["tiny_vec.h"]
        LINALG["tiny_linalg.h"]
        CFLOAT["tiny_cfloat.h"]
        DECOMP["tiny_decomp.h"]
        EIGEN["tiny_eigen.h"]
        MODAL["tiny_modal.h"]
        ITER["tiny_iterative.h"]
        MAT["tiny_mat.h"]
    end

    subgraph Entry["统一入口"]
        MATH["tiny_math.h<br/><b>汇总所有子模块</b>"]
    end

    STD --> TOOL
    STD --> CONST
    STD --> ERR
    TOOL --> CONF
    CONST --> CONF
    ERR --> CONF

    CONF --> VEC
    CONF --> LINALG
    CONF --> CFLOAT
    CONF --> DECOMP
    CONF --> EIGEN
    CONF --> MODAL
    CONF --> ITER
    CONF --> MAT

    VEC --> MATH
    LINALG --> MATH
    CFLOAT --> MATH
    DECOMP --> MATH
    EIGEN --> MATH
    MODAL --> MATH
    ITER --> MATH
    MAT --> MATH

    style CONF fill:#eef2ff,stroke:#4f6af5,stroke-width:2px
    style MATH fill:#eef2ff,stroke:#4f6af5
    style TOOL fill:#f8fafc,stroke:#94a3b8
    style CONST fill:#f8fafc,stroke:#94a3b8
    style ERR fill:#f8fafc,stroke:#94a3b8
    style VEC fill:#f8fafc,stroke:#94a3b8
    style LINALG fill:#f8fafc,stroke:#94a3b8
    style CFLOAT fill:#f8fafc,stroke:#94a3b8
    style DECOMP fill:#f8fafc,stroke:#94a3b8
    style EIGEN fill:#f8fafc,stroke:#94a3b8
    style MODAL fill:#f8fafc,stroke:#94a3b8
    style ITER fill:#f8fafc,stroke:#94a3b8
    style MAT fill:#f8fafc,stroke:#94a3b8

tiny_math_config.h 内部的包含顺序遵循严格的依赖层级:

头文件 作用
1 stdio.h, stdlib.h, string.h, math.h, stdbool.h, stdint.h C 标准库——类型定义、数学函数
2 tiny_toolbox.h 基础层——ESP-IDF 集成、FreeRTOS、时间工具
3 tiny_error_type.h 所有子模块共用的错误码常量
4 tiny_constants.h 数学常量和位操作宏

平台选择

TinyMath 通过一个简单的整数宏实现 编译期平台抽象,使同一份源代码可以针对不同 MCU 架构,同时利用平台特定的优化。

可用平台

#define MCU_PLATFORM_GENERIC 0   // 可移植 C —— 无硬件加速
#define MCU_PLATFORM_ESP32   1   // ESP32 —— 使用 ESP-DSP 库
#define MCU_PLATFORM_STM32   2   // STM32(预留)
#define MCU_PLATFORM_RISCV   3   // RISC-V(预留)
  • MCUPLATFORMGENERIC


    纯 ANSI C 实现,无特定硬件优化。
    适用场景:
    ✅ 移植到新平台
    ✅ 在桌面端测试(Linux/macOS/Windows)
    ✅ 最大可移植性,牺牲部分性能

  • MCUPLATFORMESP32


    利用 ESP-DSP 库实现硬件加速的向量运算。
    适用场景:
    ✅ 在 ESP32 / ESP32-S3 上部署
    ✅ 最佳性能:向量运算比通用版本快 2-5 倍
    ✅ 完整 TinySHM 功能集

  • MCUPLATFORMSTM32


    为 STM32 系列预留(计划集成 CMSIS-DSP)。
    状态:尚未实现

  • MCUPLATFORMRISCV


    为 RISC-V MCU 预留。
    状态:尚未实现

如何选择

选择 一个 平台,取消注释对应行:

// 选其一:
#define MCU_PLATFORM_SELECTED MCU_PLATFORM_ESP32   // ESP32 目标
// #define MCU_PLATFORM_SELECTED MCU_PLATFORM_GENERIC   // 回退到纯 C

切换平台

修改 MCU_PLATFORM_SELECTED 后,需要 重新编译整个项目——它会影响编译时的 DSP 分发和底层向量/矩阵函数的内存对齐。

下游如何工作

子模块(特别是 tiny_vec.htiny_linalg.h)使用这个宏分发到平台优化的代码路径:

// tiny_vec.c 示例(简化)
#if MCU_PLATFORM_SELECTED == MCU_PLATFORM_ESP32
    #include "esp_dsp.h"
    // 调用 ESP-DSP 加速版本
    dsps_add_f32(input1, input2, output, len);
#else
    // 回退到通用 C 循环
    for (int i = 0; i < len; i++) output[i] = input1[i] + input2[i];
#endif

C++ 兼容包装

整个配置(以及所有子模块)都包裹在 extern "C" 块中,确保从 C++ 使用时获得 C 链接:

#ifdef __cplusplus
extern "C" {
#endif

// ... 所有声明 ...

#ifdef __cplusplus
}
#endif

这使得 tiny_math_config.h 可以自由地从 .c.cpp 文件中包含。


源码

/**
 * @file tiny_math_config.h
 * @author SHUAIWEN CUI (SHUAIWEN001@e.ntu.edu.sg)
 * @brief The configuration file for the tiny_math middleware.
 * @version 1.0
 * @date 2025-04-14
 * @copyright Copyright (c) 2025
 *
 */

#pragma once

#ifdef __cplusplus
extern "C"
{
#endif

/* DEPENDENCIES */

// ANSI C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>

// lower level
#include "tiny_toolbox.h"

// this level
#include "tiny_error_type.h"
#include "tiny_constants.h"

/* PLATFORM SELECTION */

// available platforms
#define MCU_PLATFORM_GENERIC 0
#define MCU_PLATFORM_ESP32 1 // here, we utilize the ESP built-in DSP library, it will automatically select the optimized version
#define MCU_PLATFORM_STM32 2
#define MCU_PLATFORM_RISCV 3

// choose one platform
#define MCU_PLATFORM_SELECTED MCU_PLATFORM_ESP32
// #define MCU_PLATFORM_SELECTED MCU_PLATFORM_GENERIC

#ifdef __cplusplus
}
#endif

代码来源

以上代码直接取自 tiny_math_config.h。请始终以 GitHub 上的文件为权威版本。