Skip to content

TinyMath Configuration

Overview

tiny_math_config.h is the central configuration header for the TinyMath library. Every submodule ultimately includes this file, making it the single point to control platform selection, dependency resolution, and compile-time behavior.


Architecture & Dependency Flow

tiny_math_config.h sits at the base of the include tree: every submodule includes it directly, and tiny_math.h (the unified entry point) aggregates all submodules.

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

    subgraph Independent["Independent Base Layers"]
        TOOL["tiny_toolbox.h<br/><i>FreeRTOS, ESP-IDF,<br/>time, logging, heap</i>"]
        CONST["tiny_constants.h<br/><i>π, √2, bit macros</i>"]
        ERR["tiny_error_type.h<br/><i>error codes</i>"]
    end

    subgraph Config["Configuration Hub<br/><b>included by every submodule</b>"]
        CONF["tiny_math_config.h"]
    end

    subgraph Submodules["Submodules (each self‑contained)"]
        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["Unified Entry Point"]
        MATH["tiny_math.h<br/><b>aggregates all submodules</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

The include order within tiny_math_config.h follows a strict dependency hierarchy:

Layer Header Purpose
1 stdio.h, stdlib.h, string.h, math.h, stdbool.h, stdint.h Standard C library — type definitions, math functions
2 tiny_toolbox.h Foundation layer — ESP-IDF integration, FreeRTOS, time utilities
3 tiny_error_type.h Error code constants used across all submodules
4 tiny_constants.h Mathematical constants and bit manipulation macros

Platform Selection

TinyMath supports a compile-time platform abstraction via a simple integer macro. This allows the same source code to target different MCU architectures while leveraging platform-specific optimisations.

Available Platforms

#define MCU_PLATFORM_GENERIC 0   // Portable C — no hardware acceleration
#define MCU_PLATFORM_ESP32   1   // ESP32 — uses ESP-DSP library
#define MCU_PLATFORM_STM32   2   // STM32 (future)
#define MCU_PLATFORM_RISCV   3   // RISC-V (future)
  • MCUPLATFORMGENERIC


    Pure ANSI C implementation with no hardware-specific optimisations.
    Use this for:
    ✅ Porting to new platforms
    ✅ Testing on desktop (Linux/macOS/Windows)
    ✅ Maximum portability at the cost of performance

  • MCUPLATFORMESP32


    Leverages ESP-DSP library for hardware-accelerated vector operations.
    Use this for:
    ✅ Deployment on ESP32 / ESP32-S3
    ✅ Best performance: vector ops are 2-5x faster than generic
    ✅ Full TinySHM feature set

  • MCUPLATFORMSTM32


    Reserved for STM32 series (CMSIS-DSP integration planned).
    Status: not yet implemented

  • MCUPLATFORMRISCV


    Reserved for RISC-V based MCUs.
    Status: not yet implemented

How to Select

Choose exactly one platform by uncommenting the corresponding line:

// Select one:
#define MCU_PLATFORM_SELECTED MCU_PLATFORM_ESP32   // For ESP32 targets
// #define MCU_PLATFORM_SELECTED MCU_PLATFORM_GENERIC   // Fallback to pure C

Changing Platform

After changing MCU_PLATFORM_SELECTED, you must recompile the entire project — it affects compile-time DSP dispatching and memory alignment in low-level vector/matrix functions.

How It Works Downstream

Submodules (especially tiny_vec.h and tiny_linalg.h) use this macro to dispatch to platform-optimised code paths:

// Example from tiny_vec.c (simplified)
#if MCU_PLATFORM_SELECTED == MCU_PLATFORM_ESP32
    #include "esp_dsp.h"
    // Call ESP-DSP accelerated version
    dsps_add_f32(input1, input2, output, len);
#else
    // Fallback to generic C loop
    for (int i = 0; i < len; i++) output[i] = input1[i] + input2[i];
#endif

C++ Compatibility Wrapper

The entire configuration (and by extension all submodules) is wrapped in an extern "C" block to ensure C linkage when used from C++:

#ifdef __cplusplus
extern "C" {
#endif

// ... all declarations ...

#ifdef __cplusplus
}
#endif

This guarantees that tiny_math_config.h can be included freely from .c and .cpp files alike.


Source Code

/**
 * @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

Source of Truth

The code above matches tiny_math_config.h in the actual repository. Always refer to GitHub as the authoritative version.