Skip to content

Commit

Permalink
Move platform-specific implementation of libstd/f{32,64}.rs
Browse files Browse the repository at this point in the history
This removes exceptions in the tidy lint 'pal'.
  • Loading branch information
cassiersg committed Jan 26, 2017
1 parent 5b8fe3e commit 662fef6
Show file tree
Hide file tree
Showing 6 changed files with 484 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/libstd/sys/redox/f32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(dead_code)]

use intrinsics;

pub mod cmath {
use libc::{c_float, c_int};

#[link_name = "m"]
extern {
pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
pub fn hypotf(x: c_float, y: c_float) -> c_float;
pub fn acosf(n: c_float) -> c_float;
pub fn asinf(n: c_float) -> c_float;
pub fn atan2f(a: c_float, b: c_float) -> c_float;
pub fn atanf(n: c_float) -> c_float;
pub fn coshf(n: c_float) -> c_float;
pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
pub fn ldexpf(x: c_float, n: c_int) -> c_float;
pub fn sinhf(n: c_float) -> c_float;
pub fn tanf(n: c_float) -> c_float;
pub fn tanhf(n: c_float) -> c_float;
}
}

#[inline]
pub fn floor(x: f32) -> f32 {
unsafe { intrinsics::floorf32(x) }
}

#[inline]
pub fn ceil(x: f32) -> f32 {
unsafe { intrinsics::ceilf32(x) }
}

#[inline]
pub fn powf(x: f32, n: f32) -> f32 {
unsafe { intrinsics::powf32(x, n) }
}

#[inline]
pub fn exp(x: f32) -> f32 {
unsafe { intrinsics::expf32(x) }
}

#[inline]
pub fn ln(x: f32) -> f32 {
unsafe { intrinsics::logf32(x) }
}

#[inline]
pub fn log2(x: f32) -> f32 {
unsafe { intrinsics::log2f32(x) }
}

#[inline]
pub fn log10(x: f32) -> f32 {
unsafe { intrinsics::log10f32(x) }
}

#[inline]
pub fn sin(x: f32) -> f32 {
unsafe { intrinsics::sinf32(x) }
}

#[inline]
pub fn cos(x: f32) -> f32 {
unsafe { intrinsics::cosf32(x) }
}
33 changes: 33 additions & 0 deletions src/libstd/sys/redox/f64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(dead_code)]

pub mod cmath {
use libc::{c_double, c_int};

#[link_name = "m"]
extern {
pub fn lgamma_r(n: c_double, sign: &mut c_int) -> c_double;
pub fn hypot(x: c_double, y: c_double) -> c_double;
}
}

pub fn ln(x: f64) -> f64 {
unsafe { ::intrinsics::logf64(x) }
}

pub fn log2(x: f64) -> f64 {
unsafe { ::intrinsics::log2f64(x) }
}

pub fn log10(x: f64) -> f64 {
unsafe { ::intrinsics::log10f64(x) }
}
81 changes: 81 additions & 0 deletions src/libstd/sys/unix/f32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(dead_code)]

use intrinsics;

pub mod cmath {
use libc::{c_float, c_int};

#[link_name = "m"]
extern {
pub fn lgammaf_r(n: c_float, sign: &mut c_int) -> c_float;
pub fn hypotf(x: c_float, y: c_float) -> c_float;
pub fn acosf(n: c_float) -> c_float;
pub fn asinf(n: c_float) -> c_float;
pub fn atan2f(a: c_float, b: c_float) -> c_float;
pub fn atanf(n: c_float) -> c_float;
pub fn coshf(n: c_float) -> c_float;
pub fn frexpf(n: c_float, value: &mut c_int) -> c_float;
pub fn ldexpf(x: c_float, n: c_int) -> c_float;
pub fn sinhf(n: c_float) -> c_float;
pub fn tanf(n: c_float) -> c_float;
pub fn tanhf(n: c_float) -> c_float;
}
}

#[inline]
pub fn floor(x: f32) -> f32 {
unsafe { intrinsics::floorf32(x) }
}

#[inline]
pub fn ceil(x: f32) -> f32 {
unsafe { intrinsics::ceilf32(x) }
}

#[inline]
pub fn powf(x: f32, n: f32) -> f32 {
unsafe { intrinsics::powf32(x, n) }
}

#[inline]
pub fn exp(x: f32) -> f32 {
unsafe { intrinsics::expf32(x) }
}

#[inline]
pub fn ln(x: f32) -> f32 {
unsafe { intrinsics::logf32(x) }
}

#[inline]
pub fn log2(x: f32) -> f32 {
#[cfg(target_os = "android")]
return ::sys::android::log2f32(x);
#[cfg(not(target_os = "android"))]
return unsafe { intrinsics::log2f32(x) };
}

#[inline]
pub fn log10(x: f32) -> f32 {
unsafe { intrinsics::log10f32(x) }
}

#[inline]
pub fn sin(x: f32) -> f32 {
unsafe { intrinsics::sinf32(x) }
}

#[inline]
pub fn cos(x: f32) -> f32 {
unsafe { intrinsics::cosf32(x) }
}
66 changes: 66 additions & 0 deletions src/libstd/sys/unix/f64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(dead_code)]

use core::f64::{NAN, NEG_INFINITY};

pub mod cmath {
use libc::{c_double, c_int};

#[link_name = "m"]
extern {
pub fn lgamma_r(n: c_double, sign: &mut c_int) -> c_double;
pub fn hypot(x: c_double, y: c_double) -> c_double;
}
}

pub fn ln(x: f64) -> f64 {
log_wrapper(x, |n| { unsafe { ::intrinsics::logf64(n) } })
}

pub fn log2(x: f64) -> f64 {
log_wrapper(x,
|n| {
#[cfg(target_os = "android")]
return ::sys::android::log2f64(n);
#[cfg(not(target_os = "android"))]
return unsafe { ::intrinsics::log2f64(n) };
})
}

pub fn log10(x: f64) -> f64 {
log_wrapper(x, |n| { unsafe { ::intrinsics::log10f64(n) } })
}

// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
// because of their non-standard behavior (e.g. log(-n) returns -Inf instead
// of expected NaN).
fn log_wrapper<F: Fn(f64) -> f64>(x: f64, log_fn: F) -> f64 {
if !cfg!(target_os = "solaris") {
log_fn(x)
} else {
if x.is_finite() {
if x > 0.0 {
log_fn(x)
} else if x == 0.0 {
NEG_INFINITY // log(0) = -Inf
} else {
NAN // log(-n) = NaN
}
} else if x.is_nan() {
x // log(NaN) = NaN
} else if x > 0.0 {
x // log(Inf) = Inf
} else {
NAN // log(-Inf) = NaN
}
}
}
Loading

0 comments on commit 662fef6

Please sign in to comment.