Skip to content

Commit

Permalink
Implement 128-bit Gaussian integers
Browse files Browse the repository at this point in the history
The build script detects support for `u128` and `i128` (Rust 1.26+) and
enables their `Complex` implementations.  We only have to directly
implement the operators with scalar LHS, and the rest comes from
enabling num-traits' 128-bit `Num`.
  • Loading branch information
cuviper committed May 21, 2018
1 parent 11ac7d6 commit 85ccb72
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: rust
rust:
- 1.15.0
- 1.20.0
- 1.25.0
- stable
- beta
- nightly
Expand Down
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ repository = "https://github.com/rust-num/num-complex"
version = "0.2.0-git"
publish = false
readme = "README.md"
build = "build.rs"

[package.metadata.docs.rs]
all-features = true
features = ["std"]

[dependencies]

[dependencies.num-traits]
version = "0.2.1"
version = "0.2.4"
default-features = false

[dependencies.serde]
Expand All @@ -33,4 +34,5 @@ default-features = false

[features]
default = ["std"]
i128 = ["num-traits/i128"]
std = ["num-traits/std"]
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ Features based on `Float` types are only available when `std` is enabled. Where
possible, `FloatCore` is used instead. Formatting complex numbers only supports
format width when `std` is enabled.

Implementations for `i128` and `u128` are only available with Rust 1.26 and
later. The build script automatically detects this, but you can make it
mandatory by enabling the `i128` crate feature.

## Releases

Release notes are available in [RELEASES.md](RELEASES.md).
Expand Down
35 changes: 35 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::env;
use std::io::Write;
use std::process::{Command, Stdio};

fn main() {
if probe("fn main() { 0i128; }") {
println!("cargo:rustc-cfg=has_i128");
} else if env::var_os("CARGO_FEATURE_I128").is_some() {
panic!("i128 support was not detected!");
}
}

/// Test if a code snippet can be compiled
fn probe(code: &str) -> bool {
let rustc = env::var_os("RUSTC").unwrap_or_else(|| "rustc".into());
let out_dir = env::var_os("OUT_DIR").expect("environment variable OUT_DIR");

let mut child = Command::new(rustc)
.arg("--out-dir")
.arg(out_dir)
.arg("--emit=obj")
.arg("-")
.stdin(Stdio::piped())
.spawn()
.expect("rustc probe");

child
.stdin
.as_mut()
.expect("rustc stdin")
.write_all(code.as_bytes())
.expect("write rustc stdin");

child.wait().expect("rustc probe").success()
}
2 changes: 1 addition & 1 deletion ci/rustup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
set -ex

export TRAVIS_RUST_VERSION
for TRAVIS_RUST_VERSION in 1.15.0 stable beta nightly; do
for TRAVIS_RUST_VERSION in 1.15.0 1.20.0 1.25.0 stable beta nightly; do
run="rustup run $TRAVIS_RUST_VERSION"
$run cargo build --verbose
$run $PWD/ci/test_full.sh
Expand Down
12 changes: 9 additions & 3 deletions ci/test_full.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ set -ex

echo Testing num-complex on rustc ${TRAVIS_RUST_VERSION}

FEATURES="std rand serde"
if [[ "$TRAVIS_RUST_VERSION" =~ ^(nightly|beta|stable)$ ]]; then
FEATURES="$FEATURES i128"
fi

# num-complex should build and test everywhere.
cargo build --verbose
cargo test --verbose
Expand All @@ -13,10 +18,11 @@ cargo build --no-default-features
cargo test --no-default-features

# Each isolated feature should also work everywhere.
for feature in rand serde; do
for feature in $FEATURES; do
cargo build --verbose --no-default-features --features="$feature"
cargo test --verbose --no-default-features --features="$feature"
done

cargo build --all-features
cargo test --all-features
# test all supported features together
cargo build --features="std $FEATURES"
cargo test --features="std $FEATURES"
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,10 @@ impl<T: Clone + Num> Rem<T> for Complex<T> {
}
}

#[cfg(not(has_i128))]
real_arithmetic!(usize, u8, u16, u32, u64, isize, i8, i16, i32, i64, f32, f64);
#[cfg(has_i128)]
real_arithmetic!(usize, u8, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128, f32, f64);

/* constants */
impl<T: Clone + Num> Zero for Complex<T> {
Expand Down

0 comments on commit 85ccb72

Please sign in to comment.