avr_q/
lib.rs

1// -*- coding: utf-8 -*-
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright (C) 2025 Michael Büsch <m@bues.ch>
4
5//! # Fixed point arithmetic for AVR
6//!
7//! This library provides
8//! [fixed-point arithmetic types](https://en.wikipedia.org/wiki/Q_(number_format))
9//! for AVR microcontrollers.
10//!
11//! ## The fixed point formats supported are
12//!
13//! - **Q7.8 format**: A 16-bit fixed-point number with 7 integer bits and 8 fractional bits [Q7p8].
14//! - **Q15.8 format**: A 24-bit fixed-point number with 15 integer bits and 8 fractional bits [Q15p8].
15//!
16//! ## The supported operations are
17//!
18//! - Basic arithmetic operations: addition, subtraction, multiplication, and division.
19//! - Macros for easy construction of fixed-point numbers from integers or fractions.
20//! - Conversions between fixed-point types and integer types.
21//! - Optional `curveipo` feature for curve interpolation.
22//!
23//! ## Creating fixed-point numbers
24//!
25//! ```
26//! use avr_q::{q7p8, Q7p8};
27//!
28//! // From an integer
29//! let a = q7p8!(const 5);
30//! assert_eq!(a.to_int(), 5);
31//!
32//! // From a fraction
33//! let b = q7p8!(const 1 / 2); // 0.5
34//! let c = q7p8!(const 10 / 3); // 3.33...
35//!
36//! // From variables
37//! let numerator = 10_i16;
38//! let denominator = 3_i16;
39//! let d = q7p8!(numerator / denominator);
40//! ```
41//!
42//! # Arithmetic operations
43//!
44//! ```
45//! use avr_q::{q7p8, Q7p8};
46//!
47//! let a = q7p8!(const 1 / 2); // 0.5
48//! let b = q7p8!(const 1 / 4); // 0.25
49//!
50//! let sum = a + b;
51//! assert_eq!(sum, q7p8!(const 3 / 4)); // 0.75
52//!
53//! let diff = a - b;
54//! assert_eq!(diff, q7p8!(const 1 / 4)); // 0.25
55//!
56//! let prod = a * b;
57//! assert_eq!(prod, q7p8!(const 1 / 8)); // 0.125
58//!
59//! let quot = a / b;
60//! assert_eq!(quot, q7p8!(const 2)); // 2.0
61//!
62//! let neg = -a;
63//! assert_eq!(neg, q7p8!(const -1 / 2));
64//!
65//! let abs = neg.abs();
66//! assert_eq!(abs, q7p8!(const 1 / 2));
67//! ```
68//!
69//! ## Crate features
70//!
71//! - `curveipo` (enabled by default):
72//!   The `curveipo` feature enables the implementations of all traits from the
73//!   [curveipo crate](https://crates.io/crates/curveipo)
74//!   for all fixed-point types.
75//!   The `curveipo` crate provides 2D curve interpolation support.
76
77#![cfg_attr(not(test), no_std)]
78
79mod q15p8;
80mod q7p8;
81
82#[cfg(feature = "curveipo")]
83mod curveipo;
84
85#[cfg(any(feature = "__internal_test__", test))]
86pub mod unit_tests;
87
88pub use crate::{q7p8::Q7p8, q15p8::Q15p8};
89
90#[cfg(test)]
91mod test {
92    use crate::unit_tests;
93
94    struct TestRunner {}
95
96    impl unit_tests::TestOps for TestRunner {
97        fn print(&self, text: &str) {
98            print!("{text}");
99        }
100
101        fn print_num(&self, value: u32) {
102            print!("{value}");
103        }
104
105        fn begin(&self, name: &str) {
106            println!("Begin: {name}");
107        }
108
109        fn assert(&self, line: u16, ok: bool) {
110            if ok {
111                println!("line {line}: Ok");
112            } else {
113                panic!("line {line}: FAILED");
114            }
115        }
116    }
117
118    #[test]
119    fn test_q() {
120        let t = TestRunner {};
121        unit_tests::run_tests(&t);
122    }
123}
124
125// vim: ts=4 sw=4 expandtab