add support for no_std

This commit is contained in:
Trevor Spiteri 2018-08-12 12:11:28 +02:00
parent 048033b68c
commit 6998d667b1
8 changed files with 78 additions and 22 deletions

View File

@ -14,8 +14,12 @@ documentation = "https://docs.rs/fixed"
repository = "https://gitlab.com/tspiteri/fixed" repository = "https://gitlab.com/tspiteri/fixed"
readme = "README.md" readme = "README.md"
keywords = ["mathematics", "numerics"] keywords = ["mathematics", "numerics"]
categories = ["algorithms", "data-structures", "science"] categories = ["algorithms", "data-structures", "no-std", "science"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
[dependencies] [dependencies]
typenum = "1.3" typenum = "1.3"
[features]
default = ["std"]
std = []

View File

@ -35,6 +35,10 @@ fixed-point numbers.
## Whats new ## Whats new
### Version 0.1.2 news (unreleased)
* The crate can now be used without the standard library `std`.
### Version 0.1.1 news (2018-08-11) ### Version 0.1.1 news (2018-08-11)
* Comparisons are now supported between all fixed-point numbers with * Comparisons are now supported between all fixed-point numbers with
@ -103,6 +107,24 @@ You also need to declare it by adding this to your crate root (usually
extern crate fixed; extern crate fixed;
``` ```
## Optional features
The *fixed* crate has one optional features:
1. `std`, enabled by default. This adds a dependency on the standard
library `std`. Currently this makes no difference functionally,
but that may change in the future.
The `std` feature is enabled by default; to use the crate without a
dependency on the standard library `std`, you can add the dependency
like this to [*Cargo.toml*]:
```toml
[dependencies.fixed]
version = "0.1.1"
default-features = false
```
## License ## License
This crate is free software: you can redistribute it and/or modify it This crate is free software: you can redistribute it and/or modify it

View File

@ -5,6 +5,11 @@ modification, are permitted in any medium without royalty provided the
copyright notice and this notice are preserved. This file is offered copyright notice and this notice are preserved. This file is offered
as-is, without any warranty. --> as-is, without any warranty. -->
Version 0.1.2 (unreleased)
==========================
* The crate can now be used without the standard library `std`.
Version 0.1.1 (2018-08-11) Version 0.1.1 (2018-08-11)
========================== ==========================

View File

@ -13,14 +13,14 @@
// <https://www.apache.org/licenses/LICENSE-2.0> and // <https://www.apache.org/licenses/LICENSE-2.0> and
// <https://opensource.org/licenses/MIT>. // <https://opensource.org/licenses/MIT>.
use frac::Unsigned; use core::cmp::Ordering;
use std::cmp::Ordering; use core::iter::{Product, Sum};
use std::iter::{Product, Sum}; use core::mem;
use std::mem; use core::ops::{
use std::ops::{
Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign, Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign, Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
}; };
use frac::Unsigned;
use { use {
FixedHelper, FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32, FixedHelper, FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32,
FixedU64, FixedU8, FixedU64, FixedU8,

View File

@ -13,8 +13,8 @@
// <https://www.apache.org/licenses/LICENSE-2.0> and // <https://www.apache.org/licenses/LICENSE-2.0> and
// <https://opensource.org/licenses/MIT>. // <https://opensource.org/licenses/MIT>.
use core::cmp::Ordering;
use frac::{self, Unsigned}; use frac::{self, Unsigned};
use std::cmp::Ordering;
use { use {
FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32, FixedU64, FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32, FixedU64,
FixedU8, FixedU8,
@ -135,7 +135,7 @@ mod tests {
#[test] #[test]
fn cmp_signed() { fn cmp_signed() {
use std::cmp::Ordering::*; use core::cmp::Ordering::*;
let neg1_16 = FixedI32::<frac::U16>::from_int(-1).unwrap(); let neg1_16 = FixedI32::<frac::U16>::from_int(-1).unwrap();
let neg1_20 = FixedI32::<frac::U20>::from_int(-1).unwrap(); let neg1_20 = FixedI32::<frac::U20>::from_int(-1).unwrap();
let mut a = neg1_16; let mut a = neg1_16;
@ -172,7 +172,7 @@ mod tests {
#[test] #[test]
fn cmp_unsigned() { fn cmp_unsigned() {
use std::cmp::Ordering::*; use core::cmp::Ordering::*;
let one_16 = FixedU32::<frac::U16>::from_int(1).unwrap(); let one_16 = FixedU32::<frac::U16>::from_int(1).unwrap();
let one_20 = FixedU32::<frac::U20>::from_int(1).unwrap(); let one_20 = FixedU32::<frac::U20>::from_int(1).unwrap();
let mut a = one_16; let mut a = one_16;

View File

@ -13,9 +13,11 @@
// <https://www.apache.org/licenses/LICENSE-2.0> and // <https://www.apache.org/licenses/LICENSE-2.0> and
// <https://opensource.org/licenses/MIT>. // <https://opensource.org/licenses/MIT>.
use std::cmp::Ordering; use core::cmp::Ordering;
use std::fmt::{Binary, Debug, Display, Formatter, LowerHex, Octal, Result as FmtResult, UpperHex}; use core::fmt::{
use std::str; Binary, Debug, Display, Formatter, LowerHex, Octal, Result as FmtResult, UpperHex,
};
use core::str;
use typenum::Unsigned; use typenum::Unsigned;
use FixedHelper; use FixedHelper;
@ -158,6 +160,7 @@ fn dec_int_digits(int_bits: u32) -> u32 {
let digits = (int_bits * 3 + i) / 10; let digits = (int_bits * 3 + i) / 10;
// check that digits is ceil(log10(2^int_bits - 1)), except when int_bits < 2 // check that digits is ceil(log10(2^int_bits - 1)), except when int_bits < 2
#[cfg(feature = "std")]
debug_assert!( debug_assert!(
int_bits < 2 || digits == (f64::from(int_bits).exp2() - 1.0).log10().ceil() as u32 int_bits < 2 || digits == (f64::from(int_bits).exp2() - 1.0).log10().ceil() as u32
); );
@ -179,8 +182,10 @@ fn dec_frac_digits(frac_bits: u32) -> u32 {
// check that error < delta, where // check that error < delta, where
// error = 0.5 * 10^-digits // error = 0.5 * 10^-digits
// delta = 2^-frac_bits // delta = 2^-frac_bits
#[cfg(feature = "std")]
debug_assert!(0.5 * 10f64.powi(0 - digits as i32) < (-f64::from(frac_bits)).exp2()); debug_assert!(0.5 * 10f64.powi(0 - digits as i32) < (-f64::from(frac_bits)).exp2());
// check that error with one less digit >= delta // check that error with one less digit >= delta
#[cfg(feature = "std")]
debug_assert!(0.5 * 10f64.powi(1 - digits as i32) >= (-f64::from(frac_bits)).exp2()); debug_assert!(0.5 * 10f64.powi(1 - digits as i32) >= (-f64::from(frac_bits)).exp2());
digits digits
@ -272,7 +277,7 @@ where
fmt.pad_integral(!is_neg, "", buf) fmt.pad_integral(!is_neg, "", buf)
} }
#[cfg(test)] #[cfg(all(test, feature = "std"))]
mod tests { mod tests {
use *; use *;
@ -304,7 +309,6 @@ mod tests {
let bits = !0u32 ^ i; let bits = !0u32 ^ i;
let flt = bits as f64 / (frac as f64).exp2(); let flt = bits as f64 / (frac as f64).exp2();
let fix = FixedU32::<Frac>::from_bits(bits); let fix = FixedU32::<Frac>::from_bits(bits);
println!("i is {}", i);
assert_eq!(format!("{}", fix), format!("{:.2}", flt)); assert_eq!(format!("{}", fix), format!("{:.2}", flt));
} }
} }

View File

@ -13,9 +13,8 @@
// <https://www.apache.org/licenses/LICENSE-2.0> and // <https://www.apache.org/licenses/LICENSE-2.0> and
// <https://opensource.org/licenses/MIT>. // <https://opensource.org/licenses/MIT>.
use std::cmp::Ordering; use core::cmp::Ordering;
use std::mem; use core::mem;
use typenum::Unsigned; use typenum::Unsigned;
use { use {
FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32, FixedU64, FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32, FixedU64,

View File

@ -75,6 +75,24 @@ You also need to declare it by adding this to your crate root (usually
extern crate fixed; extern crate fixed;
``` ```
## Optional features
The *fixed* crate has one optional features:
1. `std`, enabled by default. This adds a dependency on the standard
library `std`. Currently this makes no difference functionally,
but that may change in the future.
The `std` feature is enabled by default; to use the crate without a
dependency on the standard library `std`, you can add the dependency
like this to [*Cargo.toml*]:
```toml
[dependencies.fixed]
version = "0.1.1"
default-features = false
```
## License ## License
This crate is free software: you can redistribute it and/or modify it This crate is free software: you can redistribute it and/or modify it
@ -110,11 +128,15 @@ additional terms or conditions.
[channels]: https://doc.rust-lang.org/book/second-edition/appendix-07-nightly-rust.html [channels]: https://doc.rust-lang.org/book/second-edition/appendix-07-nightly-rust.html
[const generics]: https://github.com/rust-lang/rust/issues/44580 [const generics]: https://github.com/rust-lang/rust/issues/44580
*/ */
#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs)] #![warn(missing_docs)]
#![doc(html_root_url = "https://docs.rs/fixed/0.1.1")] #![doc(html_root_url = "https://docs.rs/fixed/0.1.1")]
#![doc(test(attr(deny(warnings))))] #![doc(test(attr(deny(warnings))))]
#![cfg_attr(nightly_repr_transparent, feature(repr_transparent))] #![cfg_attr(nightly_repr_transparent, feature(repr_transparent))]
#[cfg(feature = "std")]
extern crate core;
extern crate typenum; extern crate typenum;
macro_rules! if_signed { macro_rules! if_signed {
@ -139,13 +161,13 @@ pub mod frac;
mod helper; mod helper;
use arith::MulDivDir; use arith::MulDivDir;
use core::cmp::Ordering;
use core::f32;
use core::f64;
use core::hash::{Hash, Hasher};
use core::marker::PhantomData;
use frac::Unsigned; use frac::Unsigned;
use helper::FixedHelper; use helper::FixedHelper;
use std::cmp::Ordering;
use std::f32;
use std::f64;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
macro_rules! pass_method { macro_rules! pass_method {
($comment:expr, $Fixed:ident($Inner:ty) => fn $method:ident()) => { ($comment:expr, $Fixed:ident($Inner:ty) => fn $method:ident()) => {