|
| 1 | +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +#![cfg(not(test))] |
| 12 | + |
| 13 | +use libc::{c_float, c_double}; |
| 14 | + |
| 15 | +#[link_name = "m"] |
| 16 | +extern { |
| 17 | + pub fn acos(n: c_double) -> c_double; |
| 18 | + pub fn asin(n: c_double) -> c_double; |
| 19 | + pub fn atan(n: c_double) -> c_double; |
| 20 | + pub fn atan2(a: c_double, b: c_double) -> c_double; |
| 21 | + pub fn cbrt(n: c_double) -> c_double; |
| 22 | + pub fn cbrtf(n: c_float) -> c_float; |
| 23 | + pub fn cosh(n: c_double) -> c_double; |
| 24 | + pub fn expm1(n: c_double) -> c_double; |
| 25 | + pub fn expm1f(n: c_float) -> c_float; |
| 26 | + pub fn fdim(a: c_double, b: c_double) -> c_double; |
| 27 | + pub fn fdimf(a: c_float, b: c_float) -> c_float; |
| 28 | + #[cfg_attr(target_env = "msvc", link_name = "_hypot")] |
| 29 | + pub fn hypot(x: c_double, y: c_double) -> c_double; |
| 30 | + #[cfg_attr(target_env = "msvc", link_name = "_hypotf")] |
| 31 | + pub fn hypotf(x: c_float, y: c_float) -> c_float; |
| 32 | + pub fn log1p(n: c_double) -> c_double; |
| 33 | + pub fn log1pf(n: c_float) -> c_float; |
| 34 | + pub fn sinh(n: c_double) -> c_double; |
| 35 | + pub fn tan(n: c_double) -> c_double; |
| 36 | + pub fn tanh(n: c_double) -> c_double; |
| 37 | +} |
| 38 | + |
| 39 | +pub use self::shims::*; |
| 40 | + |
| 41 | +#[cfg(not(target_env = "msvc"))] |
| 42 | +mod shims { |
| 43 | + use libc::c_float; |
| 44 | + |
| 45 | + extern { |
| 46 | + pub fn acosf(n: c_float) -> c_float; |
| 47 | + pub fn asinf(n: c_float) -> c_float; |
| 48 | + pub fn atan2f(a: c_float, b: c_float) -> c_float; |
| 49 | + pub fn atanf(n: c_float) -> c_float; |
| 50 | + pub fn coshf(n: c_float) -> c_float; |
| 51 | + pub fn sinhf(n: c_float) -> c_float; |
| 52 | + pub fn tanf(n: c_float) -> c_float; |
| 53 | + pub fn tanhf(n: c_float) -> c_float; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// On MSVC these functions aren't defined, so we just define shims which promote |
| 58 | +// everything fo f64, perform the calculation, and then demote back to f32. |
| 59 | +// While not precisely correct should be "correct enough" for now. |
| 60 | +#[cfg(target_env = "msvc")] |
| 61 | +mod shims { |
| 62 | + use libc::c_float; |
| 63 | + |
| 64 | + #[inline] |
| 65 | + pub unsafe fn acosf(n: c_float) -> c_float { |
| 66 | + f64::acos(n as f64) as c_float |
| 67 | + } |
| 68 | + |
| 69 | + #[inline] |
| 70 | + pub unsafe fn asinf(n: c_float) -> c_float { |
| 71 | + f64::asin(n as f64) as c_float |
| 72 | + } |
| 73 | + |
| 74 | + #[inline] |
| 75 | + pub unsafe fn atan2f(n: c_float, b: c_float) -> c_float { |
| 76 | + f64::atan2(n as f64, b as f64) as c_float |
| 77 | + } |
| 78 | + |
| 79 | + #[inline] |
| 80 | + pub unsafe fn atanf(n: c_float) -> c_float { |
| 81 | + f64::atan(n as f64) as c_float |
| 82 | + } |
| 83 | + |
| 84 | + #[inline] |
| 85 | + pub unsafe fn coshf(n: c_float) -> c_float { |
| 86 | + f64::cosh(n as f64) as c_float |
| 87 | + } |
| 88 | + |
| 89 | + #[inline] |
| 90 | + pub unsafe fn sinhf(n: c_float) -> c_float { |
| 91 | + f64::sinh(n as f64) as c_float |
| 92 | + } |
| 93 | + |
| 94 | + #[inline] |
| 95 | + pub unsafe fn tanf(n: c_float) -> c_float { |
| 96 | + f64::tan(n as f64) as c_float |
| 97 | + } |
| 98 | + |
| 99 | + #[inline] |
| 100 | + pub unsafe fn tanhf(n: c_float) -> c_float { |
| 101 | + f64::tanh(n as f64) as c_float |
| 102 | + } |
| 103 | +} |
0 commit comments