| ... | @@ -1,13 +1,14 @@ | ... | @@ -1,13 +1,14 @@ |
| 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/hypotf.c | | |
| 5 | // https://git.musl-libc.org/cgit/musl/tree/src/math/hypot.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; | 4 | const isNan = math.isNan; |
| | 5 | const isInf = math.isInf; |
| | 6 | const inf = math.inf; |
| | 7 | const nan = math.nan; |
| | 8 | const floatEpsAt = math.floatEpsAt; |
| | 9 | const floatEps = math.floatEps; |
| | 10 | const floatMin = math.floatMin; |
| | 11 | const floatMax = math.floatMax; |
| 11 | | 12 | |
| 12 | /// Returns sqrt(x * x + y * y), avoiding unnecessary overflow and underflow. | 13 | /// Returns sqrt(x * x + y * y), avoiding unnecessary overflow and underflow. |
| 13 | /// | 14 | /// |
| ... | @@ -15,162 +16,116 @@ const maxInt = std.math.maxInt; | ... | @@ -15,162 +16,116 @@ const maxInt = std.math.maxInt; |
| 15 | /// | 16 | /// |
| 16 | /// | x | y | hypot | | 17 | /// | x | y | hypot | |
| 17 | /// |-------|-------|-------| | 18 | /// |-------|-------|-------| |
| 18 | /// | +inf | num | +inf | | 19 | /// | +-inf | any | +inf | |
| 19 | /// | num | +-inf | +inf | | 20 | /// | any | +-inf | +inf | |
| 20 | /// | nan | any | nan | | 21 | /// | nan | fin | nan | |
| 21 | /// | any | nan | nan | | 22 | /// | fin | nan | nan | |
| 22 | pub fn hypot(x: anytype, y: anytype) @TypeOf(x, y) { | 23 | pub fn hypot(x: anytype, y: anytype) @TypeOf(x, y) { |
| 23 | const T = @TypeOf(x, y); | 24 | const T = @TypeOf(x, y); |
| 24 | return switch (T) { | 25 | switch (@typeInfo(T)) { |
| 25 | f32 => hypot32(x, y), | 26 | .Float => {}, |
| 26 | f64 => hypot64(x, y), | 27 | .ComptimeFloat => return @sqrt(x * x + y * y), |
| 27 | else => @compileError("hypot not implemented for " ++ @typeName(T)), | 28 | else => @compileError("hypot not implemented for " ++ @typeName(T)), |
| 28 | }; | | |
| 29 | } | | |
| 30 | | | |
| 31 | fn hypot32(x: f32, y: f32) f32 { | | |
| 32 | var ux = @as(u32, @bitCast(x)); | | |
| 33 | var uy = @as(u32, @bitCast(y)); | | |
| 34 | | | |
| 35 | ux &= maxInt(u32) >> 1; | | |
| 36 | uy &= maxInt(u32) >> 1; | | |
| 37 | if (ux < uy) { | | |
| 38 | const tmp = ux; | | |
| 39 | ux = uy; | | |
| 40 | uy = tmp; | | |
| 41 | } | 29 | } |
| 42 | | 30 | const lower = @sqrt(floatMin(T)); |
| 43 | var xx = @as(f32, @bitCast(ux)); | 31 | const upper = @sqrt(floatMax(T) / 2); |
| 44 | var yy = @as(f32, @bitCast(uy)); | 32 | const incre = @sqrt(floatEps(T) / 2); |
| 45 | if (uy == 0xFF << 23) { | 33 | const scale = floatEpsAt(T, incre); |
| 46 | return yy; | 34 | const hypfn = if (emulateFma(T)) hypotUnfused else hypotFused; |
| 47 | } | 35 | var major: T = x; |
| 48 | if (ux >= 0xFF << 23 or uy == 0 or ux - uy >= (25 << 23)) { | 36 | var minor: T = y; |
| 49 | return xx + yy; | 37 | if (isInf(major) or isInf(minor)) return inf(T); |
| 50 | } | 38 | if (isNan(major) or isNan(minor)) return nan(T); |
| 51 | | 39 | if (T == f16) return @floatCast(@sqrt(@mulAdd(f32, x, x, @as(f32, y) * y))); |
| 52 | var z: f32 = 1.0; | 40 | if (T == f32) return @floatCast(@sqrt(@mulAdd(f64, x, x, @as(f64, y) * y))); |
| 53 | if (ux >= (0x7F + 60) << 23) { | 41 | major = @abs(major); |
| 54 | z = 0x1.0p90; | 42 | minor = @abs(minor); |
| 55 | xx *= 0x1.0p-90; | 43 | if (minor > major) { |
| 56 | yy *= 0x1.0p-90; | 44 | const tempo = major; |
| 57 | } else if (uy < (0x7F - 60) << 23) { | 45 | major = minor; |
| 58 | z = 0x1.0p-90; | 46 | minor = tempo; |
| 59 | xx *= 0x1.0p-90; | | |
| 60 | yy *= 0x1.0p-90; | | |
| 61 | } | 47 | } |
| 62 | | 48 | if (major * incre >= minor) return major; |
| 63 | return z * @sqrt(@as(f32, @floatCast(@as(f64, x) * x + @as(f64, y) * y))); | 49 | if (major > upper) return hypfn(T, major * scale, minor * scale) / scale; |
| | 50 | if (minor < lower) return hypfn(T, major / scale, minor / scale) * scale; |
| | 51 | return hypfn(T, major, minor); |
| 64 | } | 52 | } |
| 65 | | 53 | |
| 66 | fn sq(hi: *f64, lo: *f64, x: f64) void { | 54 | inline fn emulateFma(comptime T: type) bool { |
| 67 | const split: f64 = 0x1.0p27 + 1.0; | 55 | // If @mulAdd lowers to the software implementation, |
| 68 | const xc = x * split; | 56 | // hypotUnfused should be used in place of hypotFused. |
| 69 | const xh = x - xc + xc; | 57 | // This takes an educated guess, but ideally we should |
| 70 | const xl = x - xh; | 58 | // properly detect at comptime when that fallback will |
| 71 | hi.* = x * x; | 59 | // occur. |
| 72 | lo.* = xh * xh - hi.* + 2 * xh * xl + xl * xl; | 60 | return (T == f128 or T == f80); |
| 73 | } | 61 | } |
| 74 | | 62 | |
| 75 | fn hypot64(x: f64, y: f64) f64 { | 63 | inline fn hypotFused(comptime F: type, x: F, y: F) F { |
| 76 | var ux = @as(u64, @bitCast(x)); | 64 | const r = @sqrt(@mulAdd(F, x, x, y * y)); |
| 77 | var uy = @as(u64, @bitCast(y)); | 65 | const rr = r * r; |
| 78 | | 66 | const xx = x * x; |
| 79 | ux &= maxInt(u64) >> 1; | 67 | const z = @mulAdd(F, -y, y, rr - xx) + @mulAdd(F, r, r, -rr) - @mulAdd(F, x, x, -xx); |
| 80 | uy &= maxInt(u64) >> 1; | 68 | return r - z / (2 * r); |
| 81 | if (ux < uy) { | 69 | } |
| 82 | const tmp = ux; | | |
| 83 | ux = uy; | | |
| 84 | uy = tmp; | | |
| 85 | } | | |
| 86 | | | |
| 87 | const ex = ux >> 52; | | |
| 88 | const ey = uy >> 52; | | |
| 89 | var xx = @as(f64, @bitCast(ux)); | | |
| 90 | var yy = @as(f64, @bitCast(uy)); | | |
| 91 | | | |
| 92 | // hypot(inf, nan) == inf | | |
| 93 | if (ey == 0x7FF) { | | |
| 94 | return yy; | | |
| 95 | } | | |
| 96 | if (ex == 0x7FF or uy == 0) { | | |
| 97 | return xx; | | |
| 98 | } | | |
| 99 | | | |
| 100 | // hypot(x, y) ~= x + y * y / x / 2 with inexact for small y/x | | |
| 101 | if (ex - ey > 64) { | | |
| 102 | return xx + yy; | | |
| 103 | } | | |
| 104 | | 70 | |
| 105 | var z: f64 = 1; | 71 | inline fn hypotUnfused(comptime F: type, x: F, y: F) F { |
| 106 | if (ex > 0x3FF + 510) { | 72 | const r = @sqrt(x * x + y * y); |
| 107 | z = 0x1.0p700; | 73 | if (r <= 2 * y) { // 30deg or steeper |
| 108 | xx *= 0x1.0p-700; | 74 | const dx = r - y; |
| 109 | yy *= 0x1.0p-700; | 75 | const z = x * (2 * dx - x) + (dx - 2 * (x - y)) * dx; |
| 110 | } else if (ey < 0x3FF - 450) { | 76 | return r - z / (2 * r); |
| 111 | z = 0x1.0p-700; | 77 | } else { // shallower than 30 deg |
| 112 | xx *= 0x1.0p700; | 78 | const dy = r - x; |
| 113 | yy *= 0x1.0p700; | 79 | const z = 2 * dy * (x - 2 * y) + (4 * dy - y) * y + dy * dy; |
| | 80 | return r - z / (2 * r); |
| 114 | } | 81 | } |
| 115 | | | |
| 116 | var hx: f64 = undefined; | | |
| 117 | var lx: f64 = undefined; | | |
| 118 | var hy: f64 = undefined; | | |
| 119 | var ly: f64 = undefined; | | |
| 120 | | | |
| 121 | sq(&hx, &lx, x); | | |
| 122 | sq(&hy, &ly, y); | | |
| 123 | | | |
| 124 | return z * @sqrt(ly + lx + hy + hx); | | |
| 125 | } | 82 | } |
| 126 | | 83 | |
| | 84 | const hypot_test_cases = .{ |
| | 85 | .{ 0.0, -1.2, 1.2 }, |
| | 86 | .{ 0.2, -0.34, 0.3944616584663203993612799816649560759946493601889826495362 }, |
| | 87 | .{ 0.8923, 2.636890, 2.7837722899152509525110650481670176852603253522923737962880 }, |
| | 88 | .{ 1.5, 5.25, 5.4600824169603887033229768686452745953332522619323580787836 }, |
| | 89 | .{ 37.45, 159.835, 164.16372840856167640478217141034363907565754072954443805164 }, |
| | 90 | .{ 89.123, 382.028905, 392.28687638576315875933966414927490685367196874260165618371 }, |
| | 91 | .{ 123123.234375, 529428.707813, 543556.88524707706887251269205923830745438413088753096759371 }, |
| | 92 | }; |
| | 93 | |
| 127 | test hypot { | 94 | test hypot { |
| 128 | const x32: f32 = 0.0; | 95 | try expect(hypot(0.3, 0.4) == 0.5); |
| 129 | const y32: f32 = -1.2; | | |
| 130 | const x64: f64 = 0.0; | | |
| 131 | const y64: f64 = -1.2; | | |
| 132 | try expect(hypot(x32, y32) == hypot32(0.0, -1.2)); | | |
| 133 | try expect(hypot(x64, y64) == hypot64(0.0, -1.2)); | | |
| 134 | } | 96 | } |
| 135 | | 97 | |
| 136 | test hypot32 { | 98 | test "hypot.correct" { |
| 137 | const epsilon = 0.000001; | 99 | inline for (.{ f16, f32, f64, f128 }) |T| { |
| 138 | | 100 | inline for (hypot_test_cases) |v| { |
| 139 | try expect(math.approxEqAbs(f32, hypot32(0.0, -1.2), 1.2, epsilon)); | 101 | const a: T, const b: T, const c: T = v; |
| 140 | try expect(math.approxEqAbs(f32, hypot32(0.2, -0.34), 0.394462, epsilon)); | 102 | try expect(math.approxEqRel(T, hypot(a, b), c, @sqrt(floatEps(T)))); |
| 141 | try expect(math.approxEqAbs(f32, hypot32(0.8923, 2.636890), 2.783772, epsilon)); | 103 | } |
| 142 | try expect(math.approxEqAbs(f32, hypot32(1.5, 5.25), 5.460083, epsilon)); | 104 | } |
| 143 | try expect(math.approxEqAbs(f32, hypot32(37.45, 159.835), 164.163742, epsilon)); | | |
| 144 | try expect(math.approxEqAbs(f32, hypot32(89.123, 382.028905), 392.286865, epsilon)); | | |
| 145 | try expect(math.approxEqAbs(f32, hypot32(123123.234375, 529428.707813), 543556.875, epsilon)); | | |
| 146 | } | 105 | } |
| 147 | | 106 | |
| 148 | test hypot64 { | 107 | test "hypot.precise" { |
| 149 | const epsilon = 0.000001; | 108 | inline for (.{ f16, f32, f64 }) |T| { // f128 seems to be 5 ulp |
| 150 | | 109 | inline for (hypot_test_cases) |v| { |
| 151 | try expect(math.approxEqAbs(f64, hypot64(0.0, -1.2), 1.2, epsilon)); | 110 | const a: T, const b: T, const c: T = v; |
| 152 | try expect(math.approxEqAbs(f64, hypot64(0.2, -0.34), 0.394462, epsilon)); | 111 | try expect(math.approxEqRel(T, hypot(a, b), c, floatEps(T))); |
| 153 | try expect(math.approxEqAbs(f64, hypot64(0.8923, 2.636890), 2.783772, epsilon)); | 112 | } |
| 154 | try expect(math.approxEqAbs(f64, hypot64(1.5, 5.25), 5.460082, epsilon)); | 113 | } |
| 155 | try expect(math.approxEqAbs(f64, hypot64(37.45, 159.835), 164.163728, epsilon)); | | |
| 156 | try expect(math.approxEqAbs(f64, hypot64(89.123, 382.028905), 392.286876, epsilon)); | | |
| 157 | try expect(math.approxEqAbs(f64, hypot64(123123.234375, 529428.707813), 543556.885247, epsilon)); | | |
| 158 | } | 114 | } |
| 159 | | 115 | |
| 160 | test "hypot32.special" { | 116 | test "hypot.special" { |
| 161 | try expect(math.isPositiveInf(hypot32(math.inf(f32), 0.0))); | 117 | inline for (.{ f16, f32, f64, f128 }) |T| { |
| 162 | try expect(math.isPositiveInf(hypot32(-math.inf(f32), 0.0))); | 118 | try expect(math.isNan(hypot(nan(T), 0.0))); |
| 163 | try expect(math.isPositiveInf(hypot32(0.0, math.inf(f32)))); | 119 | try expect(math.isNan(hypot(0.0, nan(T)))); |
| 164 | try expect(math.isPositiveInf(hypot32(0.0, -math.inf(f32)))); | 120 | |
| 165 | try expect(math.isNan(hypot32(math.nan(f32), 0.0))); | 121 | try expect(math.isPositiveInf(hypot(inf(T), 0.0))); |
| 166 | try expect(math.isNan(hypot32(0.0, math.nan(f32)))); | 122 | try expect(math.isPositiveInf(hypot(0.0, inf(T)))); |
| 167 | } | 123 | try expect(math.isPositiveInf(hypot(inf(T), nan(T)))); |
| | 124 | try expect(math.isPositiveInf(hypot(nan(T), inf(T)))); |
| 168 | | 125 | |
| 169 | test "hypot64.special" { | 126 | try expect(math.isPositiveInf(hypot(-inf(T), 0.0))); |
| 170 | try expect(math.isPositiveInf(hypot64(math.inf(f64), 0.0))); | 127 | try expect(math.isPositiveInf(hypot(0.0, -inf(T)))); |
| 171 | try expect(math.isPositiveInf(hypot64(-math.inf(f64), 0.0))); | 128 | try expect(math.isPositiveInf(hypot(-inf(T), nan(T)))); |
| 172 | try expect(math.isPositiveInf(hypot64(0.0, math.inf(f64)))); | 129 | try expect(math.isPositiveInf(hypot(nan(T), -inf(T)))); |
| 173 | try expect(math.isPositiveInf(hypot64(0.0, -math.inf(f64)))); | 130 | } |
| 174 | try expect(math.isNan(hypot64(math.nan(f64), 0.0))); | | |
| 175 | try expect(math.isNan(hypot64(0.0, math.nan(f64)))); | | |
| 176 | } | 131 | } |