【libm】0库入口文件(lib.rs)

一、源码

lib.rs是libm(纯 Rust 实现的数学函数库)的入口文件。

rust 复制代码
//! libm in pure Rust
#![no_std]
#![cfg_attr(intrinsics_enabled, allow(internal_features))]
#![cfg_attr(intrinsics_enabled, feature(core_intrinsics))]
#![cfg_attr(all(intrinsics_enabled, target_family = "wasm"), feature(wasm_numeric_instr))]
#![cfg_attr(f128_enabled, feature(f128))]
#![cfg_attr(f16_enabled, feature(f16))]
#![allow(clippy::assign_op_pattern)]
#![allow(clippy::deprecated_cfg_attr)]
#![allow(clippy::eq_op)]
#![allow(clippy::excessive_precision)]
#![allow(clippy::float_cmp)]
#![allow(clippy::int_plus_one)]
#![allow(clippy::many_single_char_names)]
#![allow(clippy::mixed_case_hex_literals)]
#![allow(clippy::needless_late_init)]
#![allow(clippy::needless_return)]
#![allow(clippy::unreadable_literal)]
#![allow(clippy::zero_divided_by_zero)]
#![forbid(unsafe_op_in_unsafe_fn)]

mod libm_helper;
mod math;

use core::{f32, f64};

pub use libm_helper::*;

pub use self::math::*;
  1. 文件注释
rust 复制代码
//! libm in pure Rust

//! 是模块级文档注释,说明这个库是一个纯 Rust 实现的 libm(数学函数库)。

  1. 全局属性(#![...])
    #![no_std]
  • 声明该库不依赖 Rust 标准库(std),适用于嵌入式系统或无操作系统的环境。

#![cfg_attr(intrinsics_enabled, ...)]

  • 条件编译:如果 intrinsics_enabled 特性启用,则允许使用内部特性(internal_features)和 core_intrinsics(编译器内置函数)。

  • 对 WASM 目标额外启用 wasm_numeric_instr(WASM 数字指令优化)。

#![cfg_attr(f128_enabled, feature(f128))]

  • 如果 f128_enabled 启用,则启用实验性的 f128(128 位浮点数)支持(Rust 尚未稳定此功能)。

#![cfg_attr(f16_enabled, feature(f16))]

  • 如果 f16_enabled 启用,则启用实验性的 f16(16 位浮点数)支持。
  1. Clippy 忽略规则
rust 复制代码
#![allow(clippy::assign_op_pattern)]
#![allow(clippy::deprecated_cfg_attr)]
...
#![allow(clippy::zero_divided_by_zero)]
  • 禁用 Clippy(Rust 的代码风格检查工具)的某些警告,例如:

    • float_cmp:允许浮点数直接比较(数学库需要)。

    • unreadable_literal:允许长数字字面量(如 3.14159265358979323846)。

    • zero_divided_by_zero:允许 0.0 / 0.0(数学库需要处理 NaN)。

  1. 安全限制
rust 复制代码
#![forbid(unsafe_op_in_unsafe_fn)]
  • 强制在 unsafe fn 中明确标记 unsafe 代码块(提高安全性)。
  1. 模块声明
rust 复制代码
mod libm_helper;
mod math;
  • 定义两个子模块:

    1. libm_helper:可能包含辅助函数或宏。
  1. math:核心数学函数实现(如 sin、sqrt)。

  2. 导入依赖

rust 复制代码
use core::{f32, f64};
  • 从 core 库导入 f32 和 f64 类型(no_std 环境下替代 std 的浮点类型)。
  1. 公开 API
rust 复制代码
pub use libm_helper::*;
pub use self::math::*;
  • 将 libm_helper 和 math 模块的所有公共项(函数/类型)导出为库的公共 API。

二、关键点总结

  1. 纯 Rust 实现:不依赖 std,适合嵌入式或无 OS 环境。

  2. 条件编译:支持 WASM、f16/f128 等实验性功能。

  3. 数学函数:核心逻辑在 math 模块中实现(如 sin、exp)。

  4. 安全优先:严格限制 unsafe 的使用。

三、示例:如何使用 libm

rust 复制代码
use libm::{sqrt, sin};

fn main() {
    println!("sqrt(2.0) = {}", sqrt(2.0_f64)); // 1.4142135623730951
    println!("sin(PI/2) = {}", sin(1.5707963267948966_f64)); // ~1.0
}

这个库的设计目标是在不依赖系统 libm 的情况下提供标准数学函数,同时保持高性能和安全性。

相关推荐
鸿乃江边鸟11 小时前
Spark Datafusion Comet 向量化Rust Native--创建Datafusion计划
rust·spark·native
咸甜适中11 小时前
rust的docx-rs库,自定义docx模版批量分页生成一个docx文档(方便打印)(逐行注释)
rust·办公自动化·docx-rs
Vallelonga11 小时前
Rust Option.as_ref() 方法
开发语言·rust
大卫小东(Sheldon)1 天前
GIM 2.0 发布:真正让 AI 提交消息可定制、可控、可项目级优化
git·rust·gim
roamingcode1 天前
我是如何 Vibe Coding,将 AI CLI 工具从 Node.js 迁移到 Rust 并成功发布的
人工智能·rust·node.js·github·claude·github copilot
初恋叫萱萱2 天前
构建高性能生成式AI应用:基于Rust Axum与蓝耘DeepSeek-V3.2大模型服务的全栈开发实战
开发语言·人工智能·rust
superman超哥3 天前
Serde 性能优化的终极武器
开发语言·rust·编程语言·rust serde·serde性能优化·rust开发工具
sayang_shao3 天前
Rust多线程编程学习笔记
笔记·学习·rust
鸿乃江边鸟3 天前
Spark Datafusion Comet 向量化Rust Native--读数据
rust·spark·native·arrow
硬汉嵌入式3 天前
基于Rust构建的单片机Ariel RTOS,支持Cortex-M、RISC-V 和 Xtensa
单片机·rust·risc-v