| ... | ... | @@ -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 | 1 | const std = @import("../std.zig"); |
| 8 | 2 | const math = std.math; |
| 9 | 3 | const expect = std.testing.expect; |
| 10 | | const maxInt = std.math.maxInt; |
| 11 | 4 | |
| 12 | 5 | /// Returns the absolute value of x. |
| 13 | 6 | /// |
| ... | ... | @@ -16,86 +9,37 @@ const maxInt = std.math.maxInt; |
| 16 | 9 | /// - fabs(nan) = nan |
| 17 | 10 | pub fn fabs(x: anytype) @TypeOf(x) { |
| 18 | 11 | 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 | | |
| 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 | | } |
| 12 | const TBits = std.meta.Int(.unsigned, @bitSizeOf(T)); |
| 13 | if (@typeInfo(T) != .Float) { |
| 14 | @compileError("fabs not implemented for " ++ @typeName(T)); |
| 15 | } |
| 39 | 16 | |
| 40 | | fn 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; |
| 45 | 19 | |
| 46 | | fn 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); |
| 50 | 21 | } |
| 51 | 22 | |
| 52 | 23 | test "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 | | |
| 59 | | test "math.fabs16" { |
| 60 | | try expect(fabs16(1.0) == 1.0); |
| 61 | | try expect(fabs16(-1.0) == 1.0); |
| 62 | | } |
| 63 | | |
| 64 | | test "math.fabs32" { |
| 65 | | try expect(fabs32(1.0) == 1.0); |
| 66 | | try expect(fabs32(-1.0) == 1.0); |
| 67 | | } |
| 68 | | |
| 69 | | test "math.fabs64" { |
| 70 | | try expect(fabs64(1.0) == 1.0); |
| 71 | | try expect(fabs64(-1.0) == 1.0); |
| 72 | | } |
| 73 | | |
| 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)))); |
| 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 | } |
| 101 | 45 | } |