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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
// 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 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: anytype) @TypeOf(x) {
const T = @TypeOf(x);
return switch (T) {
f32 => round32(x),
f64 => round64(x),
f128 => round128(x),
// TODO this is not correct for some targets
c_longdouble => @floatCast(c_longdouble, round128(x)),
else => @compileError("round not implemented for " ++ @typeName(T)),
};
}
fn round32(x_: f32) f32 {
const f32_toint = 1.0 / math.floatEps(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.doNotOptimizeAway(x + f32_toint);
return 0 * @bitCast(f32, u);
}
y = x + f32_toint - 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 {
const f64_toint = 1.0 / math.floatEps(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.doNotOptimizeAway(x + f64_toint);
return 0 * @bitCast(f64, u);
}
y = x + f64_toint - 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;
}
}
fn round128(x_: f128) f128 {
const f128_toint = 1.0 / math.floatEps(f128);
var x = x_;
const u = @bitCast(u128, x);
const e = (u >> 112) & 0x7FFF;
var y: f128 = undefined;
if (e >= 0x3FFF + 112) {
return x;
}
if (u >> 127 != 0) {
x = -x;
}
if (e < 0x3FFF - 1) {
math.doNotOptimizeAway(x + f128_toint);
return 0 * @bitCast(f128, u);
}
y = x + f128_toint - f128_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 >> 127 != 0) {
return -y;
} else {
return y;
}
}
test "math.round" {
try expect(round(@as(f32, 1.3)) == round32(1.3));
try expect(round(@as(f64, 1.3)) == round64(1.3));
try expect(round(@as(f128, 1.3)) == round128(1.3));
}
test "math.round32" {
try expect(round32(1.3) == 1.0);
try expect(round32(-1.3) == -1.0);
try expect(round32(0.2) == 0.0);
try expect(round32(1.8) == 2.0);
}
test "math.round64" {
try expect(round64(1.3) == 1.0);
try expect(round64(-1.3) == -1.0);
try expect(round64(0.2) == 0.0);
try expect(round64(1.8) == 2.0);
}
test "math.round128" {
try expect(round128(1.3) == 1.0);
try expect(round128(-1.3) == -1.0);
try expect(round128(0.2) == 0.0);
try expect(round128(1.8) == 2.0);
}
test "math.round32.special" {
try expect(round32(0.0) == 0.0);
try expect(round32(-0.0) == -0.0);
try expect(math.isPositiveInf(round32(math.inf(f32))));
try expect(math.isNegativeInf(round32(-math.inf(f32))));
try expect(math.isNan(round32(math.nan(f32))));
}
test "math.round64.special" {
try expect(round64(0.0) == 0.0);
try expect(round64(-0.0) == -0.0);
try expect(math.isPositiveInf(round64(math.inf(f64))));
try expect(math.isNegativeInf(round64(-math.inf(f64))));
try expect(math.isNan(round64(math.nan(f64))));
}
test "math.round128.special" {
try expect(round128(0.0) == 0.0);
try expect(round128(-0.0) == -0.0);
try expect(math.isPositiveInf(round128(math.inf(f128))));
try expect(math.isNegativeInf(round128(-math.inf(f128))));
try expect(math.isNan(round128(math.nan(f128))));
}
|