aboutsummaryrefslogtreecommitdiff
path: root/lib/std/math/round.zig
blob: 0b80a46ce5b6abf5be9788cd5250452afca84e7f (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Ported from musl, which is licensed under the MIT license:
// https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
//
// https://git.musl-libc.org/cgit/musl/tree/src/math/roundf.c
// https://git.musl-libc.org/cgit/musl/tree/src/math/round.c

const builtin = @import("builtin");
const expect = std.testing.expect;
const std = @import("../std.zig");
const math = std.math;

/// Returns x rounded to the nearest integer, rounding half away from zero.
///
/// Special Cases:
///  - round(+-0)   = +-0
///  - round(+-inf) = +-inf
///  - round(nan)   = nan
pub fn round(x: var) @typeOf(x) {
    const T = @typeOf(x);
    return switch (T) {
        f32 => round32(x),
        f64 => round64(x),
        else => @compileError("round not implemented for " ++ @typeName(T)),
    };
}

fn round32(x_: f32) f32 {
    var x = x_;
    const u = @bitCast(u32, x);
    const e = (u >> 23) & 0xFF;
    var y: f32 = undefined;

    if (e >= 0x7F + 23) {
        return x;
    }
    if (u >> 31 != 0) {
        x = -x;
    }
    if (e < 0x7F - 1) {
        math.forceEval(x + math.f32_toint);
        return 0 * @bitCast(f32, u);
    }

    y = x + math.f32_toint - math.f32_toint - x;
    if (y > 0.5) {
        y = y + x - 1;
    } else if (y <= -0.5) {
        y = y + x + 1;
    } else {
        y = y + x;
    }

    if (u >> 31 != 0) {
        return -y;
    } else {
        return y;
    }
}

fn round64(x_: f64) f64 {
    var x = x_;
    const u = @bitCast(u64, x);
    const e = (u >> 52) & 0x7FF;
    var y: f64 = undefined;

    if (e >= 0x3FF + 52) {
        return x;
    }
    if (u >> 63 != 0) {
        x = -x;
    }
    if (e < 0x3ff - 1) {
        math.forceEval(x + math.f64_toint);
        return 0 * @bitCast(f64, u);
    }

    y = x + math.f64_toint - math.f64_toint - x;
    if (y > 0.5) {
        y = y + x - 1;
    } else if (y <= -0.5) {
        y = y + x + 1;
    } else {
        y = y + x;
    }

    if (u >> 63 != 0) {
        return -y;
    } else {
        return y;
    }
}

test "math.round" {
    expect(round(f32(1.3)) == round32(1.3));
    expect(round(f64(1.3)) == round64(1.3));
}

test "math.round32" {
    expect(round32(1.3) == 1.0);
    expect(round32(-1.3) == -1.0);
    expect(round32(0.2) == 0.0);
    expect(round32(1.8) == 2.0);
}

test "math.round64" {
    expect(round64(1.3) == 1.0);
    expect(round64(-1.3) == -1.0);
    expect(round64(0.2) == 0.0);
    expect(round64(1.8) == 2.0);
}

test "math.round32.special" {
    expect(round32(0.0) == 0.0);
    expect(round32(-0.0) == -0.0);
    expect(math.isPositiveInf(round32(math.inf(f32))));
    expect(math.isNegativeInf(round32(-math.inf(f32))));
    expect(math.isNan(round32(math.nan(f32))));
}

test "math.round64.special" {
    expect(round64(0.0) == 0.0);
    expect(round64(-0.0) == -0.0);
    expect(math.isPositiveInf(round64(math.inf(f64))));
    expect(math.isNegativeInf(round64(-math.inf(f64))));
    expect(math.isNan(round64(math.nan(f64))));
}