Skip to main content

ffl/
lib.rs

1// Copyright 2026 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Fuchsia Fixed-Point Library (FFL)
6//!
7//! FFL is a fixed-point arithmetic library ported from C++. It provides:
8//! - [`Fixed`]: The primary fixed-point type, parameterized by the underlying
9//!   integer type and fractional bit count.
10//! - [`FastFixed`]: A wrapper type designed for high-performance arithmetic
11//!   operations on 64-bit platforms, by saturating to 64-bit intermediates
12//!   instead of standard 128-bit intermediates.
13//! - [`ExponentialAverage`]: A utility for tracking exponential moving averages
14//!   and variance using fixed-point values.
15//! - Expression templates: Deferred-evaluation expression structures like
16//!   [`expression::DivExpression`] and [`expression::MulExpression`] to
17//!   maintain resolution and precision for division and multiplication
18//!   operations until the result is converted to a concrete format.
19
20#![cfg_attr(not(test), no_std)]
21
22pub mod exponential_average;
23pub mod expression;
24pub mod fast;
25pub mod fixed;
26pub mod fixed_format;
27pub mod num_traits;
28pub(crate) mod saturating_arithmetic;
29pub mod string;
30pub(crate) mod utility;
31
32// Re-export public API
33pub use exponential_average::ExponentialAverage;
34pub use expression::{from_ratio, to_resolution};
35pub use fast::{FastExpression, FastFixed};
36pub use fixed::{Fixed, compare_fixed};
37pub use string::{Mode, String};
38
39/// Rounds the given fixed-point value to the nearest integer using
40/// round-half-to-even (convergent rounding).
41pub fn round<I, const FRAC: usize>(val: Fixed<I, FRAC>) -> I
42where
43    I: num_traits::Bounded + num_traits::NumCast + Copy + Into<i128> + num_traits::Zero,
44{
45    val.round()
46}
47
48/// Creates a `Fixed` value from a raw underlying integer value.
49pub const fn from_raw<I: Copy, const FRAC: usize>(value: I) -> Fixed<I, FRAC> {
50    Fixed::from_raw(value)
51}
52
53#[cfg(test)]
54mod tests;