std: Fix complex ldexp implementation
Two bugs in the implementation ported from musl made all the complex functions relying on ldexp return incorrect results in some cases. Spotted in #9047
This commit is contained in:
committed by
Veikka Tuominen
parent
0d022eb1d6
commit
44cdafd9d4
@@ -13,6 +13,7 @@ const std = @import("../../std.zig");
|
||||
const debug = std.debug;
|
||||
const math = std.math;
|
||||
const cmath = math.complex;
|
||||
const testing = std.testing;
|
||||
const Complex = cmath.Complex;
|
||||
|
||||
/// Returns exp(z) scaled to avoid overflow.
|
||||
@@ -48,7 +49,10 @@ fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) {
|
||||
const half_expt2 = exptf - half_expt1;
|
||||
const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23);
|
||||
|
||||
return Complex(f32).init(math.cos(z.im) * exp_x * scale1 * scale2, math.sin(z.im) * exp_x * scale1 * scale2);
|
||||
return Complex(f32).init(
|
||||
math.cos(z.im) * exp_x * scale1 * scale2,
|
||||
math.sin(z.im) * exp_x * scale1 * scale2,
|
||||
);
|
||||
}
|
||||
|
||||
fn frexp_exp64(x: f64, expt: *i32) f64 {
|
||||
@@ -57,7 +61,7 @@ fn frexp_exp64(x: f64, expt: *i32) f64 {
|
||||
|
||||
const exp_x = math.exp(x - kln2);
|
||||
|
||||
const fx = @bitCast(u64, x);
|
||||
const fx = @bitCast(u64, exp_x);
|
||||
const hx = @intCast(u32, fx >> 32);
|
||||
const lx = @truncate(u32, fx);
|
||||
|
||||
@@ -73,10 +77,10 @@ fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) {
|
||||
const exptf = @as(i64, expt + ex_expt);
|
||||
|
||||
const half_expt1 = @divTrunc(exptf, 2);
|
||||
const scale1 = @bitCast(f64, (0x3ff + half_expt1) << 20);
|
||||
const scale1 = @bitCast(f64, (0x3ff + half_expt1) << (20 + 32));
|
||||
|
||||
const half_expt2 = exptf - half_expt1;
|
||||
const scale2 = @bitCast(f64, (0x3ff + half_expt2) << 20);
|
||||
const scale2 = @bitCast(f64, (0x3ff + half_expt2) << (20 + 32));
|
||||
|
||||
return Complex(f64).init(
|
||||
math.cos(z.im) * exp_x * scale1 * scale2,
|
||||
|
||||
Reference in New Issue
Block a user