| ... | @@ -13,6 +13,7 @@ const std = @import("../../std.zig"); | ... | @@ -13,6 +13,7 @@ const std = @import("../../std.zig"); |
| 13 | const debug = std.debug; | 13 | const debug = std.debug; |
| 14 | const math = std.math; | 14 | const math = std.math; |
| 15 | const cmath = math.complex; | 15 | const cmath = math.complex; |
| | 16 | const testing = std.testing; |
| 16 | const Complex = cmath.Complex; | 17 | const Complex = cmath.Complex; |
| 17 | | 18 | |
| 18 | /// Returns exp(z) scaled to avoid overflow. | 19 | /// Returns exp(z) scaled to avoid overflow. |
| ... | @@ -48,7 +49,10 @@ fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) { | ... | @@ -48,7 +49,10 @@ fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) { |
| 48 | const half_expt2 = exptf - half_expt1; | 49 | const half_expt2 = exptf - half_expt1; |
| 49 | const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23); | 50 | const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23); |
| 50 | | 51 | |
| 51 | return Complex(f32).init(math.cos(z.im) * exp_x * scale1 * scale2, math.sin(z.im) * exp_x * scale1 * scale2); | 52 | return Complex(f32).init( |
| | 53 | math.cos(z.im) * exp_x * scale1 * scale2, |
| | 54 | math.sin(z.im) * exp_x * scale1 * scale2, |
| | 55 | ); |
| 52 | } | 56 | } |
| 53 | | 57 | |
| 54 | fn frexp_exp64(x: f64, expt: *i32) f64 { | 58 | fn frexp_exp64(x: f64, expt: *i32) f64 { |
| ... | @@ -57,7 +61,7 @@ fn frexp_exp64(x: f64, expt: *i32) f64 { | ... | @@ -57,7 +61,7 @@ fn frexp_exp64(x: f64, expt: *i32) f64 { |
| 57 | | 61 | |
| 58 | const exp_x = math.exp(x - kln2); | 62 | const exp_x = math.exp(x - kln2); |
| 59 | | 63 | |
| 60 | const fx = @bitCast(u64, x); | 64 | const fx = @bitCast(u64, exp_x); |
| 61 | const hx = @intCast(u32, fx >> 32); | 65 | const hx = @intCast(u32, fx >> 32); |
| 62 | const lx = @truncate(u32, fx); | 66 | const lx = @truncate(u32, fx); |
| 63 | | 67 | |
| ... | @@ -73,10 +77,10 @@ fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) { | ... | @@ -73,10 +77,10 @@ fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) { |
| 73 | const exptf = @as(i64, expt + ex_expt); | 77 | const exptf = @as(i64, expt + ex_expt); |
| 74 | | 78 | |
| 75 | const half_expt1 = @divTrunc(exptf, 2); | 79 | const half_expt1 = @divTrunc(exptf, 2); |
| 76 | const scale1 = @bitCast(f64, (0x3ff + half_expt1) << 20); | 80 | const scale1 = @bitCast(f64, (0x3ff + half_expt1) << (20 + 32)); |
| 77 | | 81 | |
| 78 | const half_expt2 = exptf - half_expt1; | 82 | const half_expt2 = exptf - half_expt1; |
| 79 | const scale2 = @bitCast(f64, (0x3ff + half_expt2) << 20); | 83 | const scale2 = @bitCast(f64, (0x3ff + half_expt2) << (20 + 32)); |
| 80 | | 84 | |
| 81 | return Complex(f64).init( | 85 | return Complex(f64).init( |
| 82 | math.cos(z.im) * exp_x * scale1 * scale2, | 86 | math.cos(z.im) * exp_x * scale1 * scale2, |