2019-05-01 18:15:57 +12:00
|
|
|
// Ported from musl, which is licensed under the MIT license:
|
|
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
|
|
|
|
|
//
|
|
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/src/complex/__cexpf.c
|
|
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/src/complex/__cexp.c
|
|
|
|
|
|
2019-03-02 16:46:04 -05:00
|
|
|
const std = @import("../../std.zig");
|
2018-04-24 19:18:31 +12:00
|
|
|
const debug = std.debug;
|
|
|
|
|
const math = std.math;
|
2021-06-12 15:35:52 +02:00
|
|
|
const testing = std.testing;
|
2021-06-09 21:35:42 -04:00
|
|
|
const cmath = math.complex;
|
2018-04-24 19:18:31 +12:00
|
|
|
const Complex = cmath.Complex;
|
|
|
|
|
|
2019-05-01 18:15:57 +12:00
|
|
|
/// Returns exp(z) scaled to avoid overflow.
|
2023-11-08 04:10:43 +08:00
|
|
|
pub fn ldexp_cexp(z: anytype, expt: i32) Complex(@TypeOf(z.re, z.im)) {
|
|
|
|
|
const T = @TypeOf(z.re, z.im);
|
2018-04-24 19:18:31 +12:00
|
|
|
|
|
|
|
|
return switch (T) {
|
|
|
|
|
f32 => ldexp_cexp32(z, expt),
|
|
|
|
|
f64 => ldexp_cexp64(z, expt),
|
|
|
|
|
else => unreachable,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-31 10:56:59 -04:00
|
|
|
fn frexp_exp32(x: f32, expt: *i32) f32 {
|
2018-05-17 00:56:14 -04:00
|
|
|
const k = 235; // reduction constant
|
|
|
|
|
const kln2 = 162.88958740; // k * ln2
|
2018-04-24 19:18:31 +12:00
|
|
|
|
2022-04-26 10:13:55 -07:00
|
|
|
const exp_x = @exp(x - kln2);
|
2023-06-22 18:46:56 +01:00
|
|
|
const hx = @as(u32, @bitCast(exp_x));
|
2018-06-17 02:57:07 -04:00
|
|
|
// TODO zig should allow this cast implicitly because it should know the value is in range
|
2023-06-22 18:46:56 +01:00
|
|
|
expt.* = @as(i32, @intCast(hx >> 23)) - (0x7f + 127) + k;
|
|
|
|
|
return @as(f32, @bitCast((hx & 0x7fffff) | ((0x7f + 127) << 23)));
|
2018-04-24 19:18:31 +12:00
|
|
|
}
|
|
|
|
|
|
2018-06-17 02:57:07 -04:00
|
|
|
fn ldexp_cexp32(z: Complex(f32), expt: i32) Complex(f32) {
|
2018-04-24 19:18:31 +12:00
|
|
|
var ex_expt: i32 = undefined;
|
|
|
|
|
const exp_x = frexp_exp32(z.re, &ex_expt);
|
|
|
|
|
const exptf = expt + ex_expt;
|
|
|
|
|
|
|
|
|
|
const half_expt1 = @divTrunc(exptf, 2);
|
2023-06-22 18:46:56 +01:00
|
|
|
const scale1 = @as(f32, @bitCast((0x7f + half_expt1) << 23));
|
2018-04-24 19:18:31 +12:00
|
|
|
|
|
|
|
|
const half_expt2 = exptf - half_expt1;
|
2023-06-22 18:46:56 +01:00
|
|
|
const scale2 = @as(f32, @bitCast((0x7f + half_expt2) << 23));
|
2018-04-24 19:18:31 +12:00
|
|
|
|
2021-06-12 15:35:52 +02:00
|
|
|
return Complex(f32).init(
|
2022-04-27 19:35:28 -07:00
|
|
|
@cos(z.im) * exp_x * scale1 * scale2,
|
|
|
|
|
@sin(z.im) * exp_x * scale1 * scale2,
|
2021-06-12 15:35:52 +02:00
|
|
|
);
|
2018-04-24 19:18:31 +12:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 10:56:59 -04:00
|
|
|
fn frexp_exp64(x: f64, expt: *i32) f64 {
|
2018-05-17 00:56:14 -04:00
|
|
|
const k = 1799; // reduction constant
|
|
|
|
|
const kln2 = 1246.97177782734161156; // k * ln2
|
2018-04-24 19:18:31 +12:00
|
|
|
|
2022-04-26 10:13:55 -07:00
|
|
|
const exp_x = @exp(x - kln2);
|
2018-04-24 19:18:31 +12:00
|
|
|
|
2023-06-22 18:46:56 +01:00
|
|
|
const fx = @as(u64, @bitCast(exp_x));
|
|
|
|
|
const hx = @as(u32, @intCast(fx >> 32));
|
|
|
|
|
const lx = @as(u32, @truncate(fx));
|
2018-04-24 19:18:31 +12:00
|
|
|
|
2023-06-22 18:46:56 +01:00
|
|
|
expt.* = @as(i32, @intCast(hx >> 20)) - (0x3ff + 1023) + k;
|
2018-04-24 19:18:31 +12:00
|
|
|
|
|
|
|
|
const high_word = (hx & 0xfffff) | ((0x3ff + 1023) << 20);
|
2023-06-22 18:46:56 +01:00
|
|
|
return @as(f64, @bitCast((@as(u64, high_word) << 32) | lx));
|
2018-04-24 19:18:31 +12:00
|
|
|
}
|
|
|
|
|
|
2018-06-17 02:57:07 -04:00
|
|
|
fn ldexp_cexp64(z: Complex(f64), expt: i32) Complex(f64) {
|
2018-04-24 19:18:31 +12:00
|
|
|
var ex_expt: i32 = undefined;
|
|
|
|
|
const exp_x = frexp_exp64(z.re, &ex_expt);
|
2019-11-07 18:52:09 -05:00
|
|
|
const exptf = @as(i64, expt + ex_expt);
|
2018-04-24 19:18:31 +12:00
|
|
|
|
|
|
|
|
const half_expt1 = @divTrunc(exptf, 2);
|
2023-06-22 18:46:56 +01:00
|
|
|
const scale1 = @as(f64, @bitCast((0x3ff + half_expt1) << (20 + 32)));
|
2018-04-24 19:18:31 +12:00
|
|
|
|
|
|
|
|
const half_expt2 = exptf - half_expt1;
|
2023-06-22 18:46:56 +01:00
|
|
|
const scale2 = @as(f64, @bitCast((0x3ff + half_expt2) << (20 + 32)));
|
2018-04-24 19:18:31 +12:00
|
|
|
|
2021-05-18 02:57:51 +07:00
|
|
|
return Complex(f64).init(
|
2022-04-27 19:35:28 -07:00
|
|
|
@cos(z.im) * exp_x * scale1 * scale2,
|
|
|
|
|
@sin(z.im) * exp_x * scale1 * scale2,
|
2018-04-24 19:18:31 +12:00
|
|
|
);
|
|
|
|
|
}
|