惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
博客园 - 【当耐特】
月光博客
月光博客
Vercel News
Vercel News
D
Docker
I
InfoQ
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
GbyAI
GbyAI
有赞技术团队
有赞技术团队
雷峰网
雷峰网
博客园 - 聂微东
小众软件
小众软件
Y
Y Combinator Blog
腾讯CDC
L
LangChain Blog
The GitHub Blog
The GitHub Blog
宝玉的分享
宝玉的分享
Stack Overflow Blog
Stack Overflow Blog
大猫的无限游戏
大猫的无限游戏
T
The Blog of Author Tim Ferriss

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Because in a Life-Threatening Situation, Every Millisecon...
Alex Rosito · 2026-06-12 · via DEV Community

Removing expf() from a fire detector: one header, 1.95x faster, zero accuracy loss


A smoke detector is not a demo project.

When it fires, someone either evacuates in time or doesn't. The firmware running on that microcontroller has one job, and it needs to do it without hesitation, without bloat, and without dependencies that can fail in unexpected ways.

Last May 28th I published a bare-metal fire detection system built with Hasaki 刃先 — a neural network trainer that exports standalone C headers with no runtime, no Python, no TensorFlow. The model is a 12-8-4-1 MLP trained on 28,596 sensor readings. It fits in 3.8 kB of Flash and achieves 99.93% accuracy on held-out data, with a single missed fire event out of 3,599.

But there was something in that header that bothered me.

static inline float sigmoid(float x) {
    return 1.0f / (1.0f + expf(-x));
}

expf(). Right there in a life-safety application. On a microcontroller that may not have a hardware FPU.


The problem with expf() on bare metal

On processors with a hardware FPU — like the ESP32-C3 — expf() is fast. But the moment you deploy to an ATmega328P, an ATtiny85, or any Cortex-M0 target, that call becomes software floating-point. The CPU has to simulate the operation in firmware, cycle by cycle.

It works. But it carries hidden cost: unpredictable latency, dependency on math.h, and a transcendental function sitting in the critical path of every single inference.

For a smoke detector running at 1 Hz this might seem irrelevant. But inference latency compounds with sensor reads, normalization, and communication overhead. And more importantly — if you're deploying to a truly constrained target, expf() might be the difference between fitting in Flash or not.


The fix: one header from kigu-quant

kigu-quant(comming soon) is a new tool in the Rosito Bench ecosystem. It generates ready-to-include C headers for evaluating mathematical functions on microcontrollers — no FPU, no libm, no dependencies.

One command:

kigu-quant --method lut --func sigmoid --size 256 --fmt q15 -o lut_sigmoid.h

One change in the model header:

// Before
#include <math.h>
static inline float sigmoid(float x) {
    return 1.0f / (1.0f + expf(-x));
}

// After
#include "lut_sigmoid.h"
// sigmoid is now lut_sigmoid_lookup() — called directly in predict()

The generated header contains a 256-entry Q1.15 lookup table covering the range [-6, 6], an inline interpolated lookup function, and nothing else. No math.h. No expf(). No heap allocation. 512 bytes of Flash for the table.


Benchmark — ATmega328P, 16MHz, no FPU

Measured with micros() on an Arduino Nano, 1000 evaluations, anti-optimization accumulator:

Method 1000 evaluations Speedup
expf() 227,292 µs baseline
lut_sigmoid_lookup() 116,512 µs 1.95x faster

Max error vs expf(): 0.000021

These are honest numbers from real hardware, not simulations.


Model accuracy: unchanged

The sigmoid sits at the output layer — one evaluation per inference, converting the final logit to a probability. The LUT covers [-6, 6] with 256 points and linear interpolation. For this model, the pre-activation values at the output layer fall well within that range during normal operation.

Validation on 7,150 held-out samples, never seen during training:

[3547    4]   ← TN  FP
[   1 3598]   ← FN  TP

Accuracy:    99.93%
FN:          1 / 3,599 fire events

Identical to the float32 baseline. The LUT approximation introduces no measurable degradation at the model level.


The two-tool pipeline

This project is the first demonstration of Hasaki and kigu-quant working together:

  • Hasaki 刃先 trains the model and exports a standalone C header — weights, biases, and activation functions in pure C++
  • kigu-quant generates the fixed-point math headers that replace the expensive activations

The integration is intentionally minimal. kigu-quant doesn't touch the model. It doesn't rewrite the header. You drop in one #include and replace one function call. Everything else stays the same.

Train → hasaki → smoke-detector-model-float.h
                        ↓
Generate → kigu-quant → lut_sigmoid.h
                        ↓
              #include both → flash → done


The repository

The full project — modified model header, generated LUT, and Arduino sketch — is available here:

hasaki-smoke-detector-v2

├── smoke-detector-model-float.h   Hasaki model — sigmoid replaced
├── lut_sigmoid.h                  kigu-quant Q1.15 LUT
└── hasaki_kigu_smoke_detector.ino Arduino sketch


One last thing

The 1.95x speedup on ATmega328P is real and measured. On targets where this matters even more — AVR running at 8MHz, Cortex-M0 with no FPU, low-power MCUs in battery-operated systems — the gap widens further.

A fire detector doesn't need to be fast to be useful. But it should never be slower than it has to be.

Every millisecond you give back to the scheduler is a millisecond available for sensor reads, communication, or simply a faster response to the next sample.

Every millisecond matters in a life threatening scenario.

expf() was a dependency this model never needed.


Built with Hasaki 刃先 and *kigu-quant (a member of the Kigu 器具 family, comming soon) — Rosito Bench*