authorgravatar for hi@viri.moeviri <hi@viri.moe> 2022-04-01 15:18:25-06:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2022-04-06 15:50:36+02:00
logcb019b80ac8ae8ffd7f7dd619c4da29a83668cde
tree1cd36143bf820508c6468b8a902dc2eca3490fcf
parent2f6ee4a97d95fe3e0ff7418e57d05242aa5aae7d

math.fabs: simplify implementation, add tests


1 files changed, 28 insertions(+), 84 deletions(-)

lib/std/math/fabs.zig+28-84
......@@ -1,13 +1,6 @@
1// Ported from musl, which is licensed under the MIT license:
2// https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
3//
4// https://git.musl-libc.org/cgit/musl/tree/src/math/fabsf.c
5// https://git.musl-libc.org/cgit/musl/tree/src/math/fabs.c
6
71const std = @import("../std.zig");
82const math = std.math;
93const expect = std.testing.expect;
10const maxInt = std.math.maxInt;
114
125/// Returns the absolute value of x.
136///
......@@ -16,86 +9,37 @@ const maxInt = std.math.maxInt;
169/// - fabs(nan) = nan
1710pub fn fabs(x: anytype) @TypeOf(x) {
1811 const T = @TypeOf(x);
19 return switch (T) {
20 f16 => fabs16(x),
21 f32 => fabs32(x),
22 f64 => fabs64(x),
23 f128 => fabs128(x),
24 else => @compileError("fabs not implemented for " ++ @typeName(T)),
25 };
26}
27
28fn fabs16(x: f16) f16 {
29 var u = @bitCast(u16, x);
30 u &= maxInt(u16) >> 1;
31 return @bitCast(f16, u);
32}
33
34fn fabs32(x: f32) f32 {
35 var u = @bitCast(u32, x);
36 u &= maxInt(u32) >> 1;
37 return @bitCast(f32, u);
38}
12 const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));
13 if (@typeInfo(T) != .Float) {
14 @compileError("fabs not implemented for " ++ @typeName(T));
15 }
3916
40fn fabs64(x: f64) f64 {
41 var u = @bitCast(u64, x);
42 u &= maxInt(u64) >> 1;
43 return @bitCast(f64, u);
44}
17 const float_bits = @bitCast(TBits, x);
18 const remove_sign = ~@as(TBits, 0) >> 1;
4519
46fn fabs128(x: f128) f128 {
47 var u = @bitCast(u128, x);
48 u &= maxInt(u128) >> 1;
49 return @bitCast(f128, u);
20 return @bitCast(T, float_bits & remove_sign);
5021}
5122
5223test "math.fabs" {
53 try expect(fabs(@as(f16, 1.0)) == fabs16(1.0));
54 try expect(fabs(@as(f32, 1.0)) == fabs32(1.0));
55 try expect(fabs(@as(f64, 1.0)) == fabs64(1.0));
56 try expect(fabs(@as(f128, 1.0)) == fabs128(1.0));
57}
58
59test "math.fabs16" {
60 try expect(fabs16(1.0) == 1.0);
61 try expect(fabs16(-1.0) == 1.0);
62}
63
64test "math.fabs32" {
65 try expect(fabs32(1.0) == 1.0);
66 try expect(fabs32(-1.0) == 1.0);
67}
68
69test "math.fabs64" {
70 try expect(fabs64(1.0) == 1.0);
71 try expect(fabs64(-1.0) == 1.0);
72}
73
74test "math.fabs128" {
75 try expect(fabs128(1.0) == 1.0);
76 try expect(fabs128(-1.0) == 1.0);
77}
78
79test "math.fabs16.special" {
80 try expect(math.isPositiveInf(fabs(math.inf(f16))));
81 try expect(math.isPositiveInf(fabs(-math.inf(f16))));
82 try expect(math.isNan(fabs(math.nan(f16))));
83}
84
85test "math.fabs32.special" {
86 try expect(math.isPositiveInf(fabs(math.inf(f32))));
87 try expect(math.isPositiveInf(fabs(-math.inf(f32))));
88 try expect(math.isNan(fabs(math.nan(f32))));
89}
90
91test "math.fabs64.special" {
92 try expect(math.isPositiveInf(fabs(math.inf(f64))));
93 try expect(math.isPositiveInf(fabs(-math.inf(f64))));
94 try expect(math.isNan(fabs(math.nan(f64))));
95}
96
97test "math.fabs128.special" {
98 try expect(math.isPositiveInf(fabs(math.inf(f128))));
99 try expect(math.isPositiveInf(fabs(-math.inf(f128))));
100 try expect(math.isNan(fabs(math.nan(f128))));
24 // TODO add support for f80 & c_longdouble here
25 inline for ([_]type{ f16, f32, f64, f128 }) |T| {
26 // normals
27 try expect(fabs(@as(T, 1.0)) == 1.0);
28 try expect(fabs(@as(T, -1.0)) == 1.0);
29 try expect(fabs(math.floatMin(T)) == math.floatMin(T));
30 try expect(fabs(-math.floatMin(T)) == math.floatMin(T));
31 try expect(fabs(math.floatMax(T)) == math.floatMax(T));
32 try expect(fabs(-math.floatMax(T)) == math.floatMax(T));
33
34 // subnormals
35 try expect(fabs(@as(T, 0.0)) == 0.0);
36 try expect(fabs(@as(T, -0.0)) == 0.0);
37 try expect(fabs(math.floatTrueMin(T)) == math.floatTrueMin(T));
38 try expect(fabs(-math.floatTrueMin(T)) == math.floatTrueMin(T));
39
40 // non-finite numbers
41 try expect(math.isPositiveInf(fabs(math.inf(T))));
42 try expect(math.isPositiveInf(fabs(-math.inf(T))));
43 try expect(math.isNan(fabs(math.nan(T))));
44 }
10145}