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,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
7const std = @import("../std.zig");1const std = @import("../std.zig");
8const math = std.math;2const math = std.math;
9const expect = std.testing.expect;3const expect = std.testing.expect;
10const maxInt = std.math.maxInt;
114
12/// Returns the absolute value of x.5/// Returns the absolute value of x.
13///6///
...@@ -16,86 +9,37 @@ const maxInt = std.math.maxInt;...@@ -16,86 +9,37 @@ const maxInt = std.math.maxInt;
16/// - fabs(nan) = nan9/// - fabs(nan) = nan
17pub fn fabs(x: anytype) @TypeOf(x) {10pub fn fabs(x: anytype) @TypeOf(x) {
18 const T = @TypeOf(x);11 const T = @TypeOf(x);
19 return switch (T) {12 const TBits = std.meta.Int(.unsigned, @bitSizeOf(T));
20 f16 => fabs16(x),13 if (@typeInfo(T) != .Float) {
21 f32 => fabs32(x),14 @compileError("fabs not implemented for " ++ @typeName(T));
22 f64 => fabs64(x),15 }
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}
3916
40fn fabs64(x: f64) f64 {17 const float_bits = @bitCast(TBits, x);
41 var u = @bitCast(u64, x);18 const remove_sign = ~@as(TBits, 0) >> 1;
42 u &= maxInt(u64) >> 1;
43 return @bitCast(f64, u);
44}
4519
46fn fabs128(x: f128) f128 {20 return @bitCast(T, float_bits & remove_sign);
47 var u = @bitCast(u128, x);
48 u &= maxInt(u128) >> 1;
49 return @bitCast(f128, u);
50}21}
5122
52test "math.fabs" {23test "math.fabs" {
53 try expect(fabs(@as(f16, 1.0)) == fabs16(1.0));24 // TODO add support for f80 & c_longdouble here
54 try expect(fabs(@as(f32, 1.0)) == fabs32(1.0));25 inline for ([_]type{ f16, f32, f64, f128 }) |T| {
55 try expect(fabs(@as(f64, 1.0)) == fabs64(1.0));26 // normals
56 try expect(fabs(@as(f128, 1.0)) == fabs128(1.0));27 try expect(fabs(@as(T, 1.0)) == 1.0);
57}28 try expect(fabs(@as(T, -1.0)) == 1.0);
5829 try expect(fabs(math.floatMin(T)) == math.floatMin(T));
59test "math.fabs16" {30 try expect(fabs(-math.floatMin(T)) == math.floatMin(T));
60 try expect(fabs16(1.0) == 1.0);31 try expect(fabs(math.floatMax(T)) == math.floatMax(T));
61 try expect(fabs16(-1.0) == 1.0);32 try expect(fabs(-math.floatMax(T)) == math.floatMax(T));
62}33
6334 // subnormals
64test "math.fabs32" {35 try expect(fabs(@as(T, 0.0)) == 0.0);
65 try expect(fabs32(1.0) == 1.0);36 try expect(fabs(@as(T, -0.0)) == 0.0);
66 try expect(fabs32(-1.0) == 1.0);37 try expect(fabs(math.floatTrueMin(T)) == math.floatTrueMin(T));
67}38 try expect(fabs(-math.floatTrueMin(T)) == math.floatTrueMin(T));
6839
69test "math.fabs64" {40 // non-finite numbers
70 try expect(fabs64(1.0) == 1.0);41 try expect(math.isPositiveInf(fabs(math.inf(T))));
71 try expect(fabs64(-1.0) == 1.0);42 try expect(math.isPositiveInf(fabs(-math.inf(T))));
72}43 try expect(math.isNan(fabs(math.nan(T))));
7344 }
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))));
101}45}