| ... | @@ -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 | | | |
| 7 | const std = @import("../std.zig"); | 1 | const std = @import("../std.zig"); |
| 8 | const math = std.math; | 2 | const math = std.math; |
| 9 | const expect = std.testing.expect; | 3 | const expect = std.testing.expect; |
| 10 | const maxInt = std.math.maxInt; | | |
| 11 | | 4 | |
| 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) = nan | 9 | /// - fabs(nan) = nan |
| 17 | pub fn fabs(x: anytype) @TypeOf(x) { | 10 | pub 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 | | | |
| 28 | fn fabs16(x: f16) f16 { | | |
| 29 | var u = @bitCast(u16, x); | | |
| 30 | u &= maxInt(u16) >> 1; | | |
| 31 | return @bitCast(f16, u); | | |
| 32 | } | | |
| 33 | | | |
| 34 | fn fabs32(x: f32) f32 { | | |
| 35 | var u = @bitCast(u32, x); | | |
| 36 | u &= maxInt(u32) >> 1; | | |
| 37 | return @bitCast(f32, u); | | |
| 38 | } | | |
| 39 | | 16 | |
| 40 | fn 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 | } | | |
| 45 | | 19 | |
| 46 | fn 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 | } |
| 51 | | 22 | |
| 52 | test "math.fabs" { | 23 | test "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); |
| 58 | | 29 | try expect(fabs(math.floatMin(T)) == math.floatMin(T)); |
| 59 | test "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 | |
| 63 | | 34 | // subnormals |
| 64 | test "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)); |
| 68 | | 39 | |
| 69 | test "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)))); |
| 73 | | 44 | } |
| 74 | test "math.fabs128" { | | |
| 75 | try expect(fabs128(1.0) == 1.0); | | |
| 76 | try expect(fabs128(-1.0) == 1.0); | | |
| 77 | } | | |
| 78 | | | |
| 79 | test "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 | | | |
| 85 | test "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 | | | |
| 91 | test "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 | | | |
| 97 | test "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 | } |