summaryrefslogtreecommitdiffstats
path: root/vendor/libm/src/math/round.rs
blob: 9a9723cfbb6588d2af2da591214ec791bbd9a673 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use core::f64;

const TOINT: f64 = 1.0 / f64::EPSILON;

#[inline]
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn round(mut x: f64) -> f64 {
    let (f, i) = (x, x.to_bits());
    let e: u64 = i >> 52 & 0x7ff;
    let mut y: f64;

    if e >= 0x3ff + 52 {
        return x;
    }
    if i >> 63 != 0 {
        x = -x;
    }
    if e < 0x3ff - 1 {
        // raise inexact if x!=0
        force_eval!(x + TOINT);
        return 0.0 * f;
    }
    y = x + TOINT - TOINT - x;
    if y > 0.5 {
        y = y + x - 1.0;
    } else if y <= -0.5 {
        y = y + x + 1.0;
    } else {
        y = y + x;
    }

    if i >> 63 != 0 {
        -y
    } else {
        y
    }
}