2019-05-01 06:15:57 +00: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/math/__expo2f.c
|
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/src/math/__expo2.c
|
|
|
|
|
2019-03-02 21:46:04 +00:00
|
|
|
const math = @import("../math.zig");
|
2017-06-16 08:26:10 +00:00
|
|
|
|
2019-05-01 06:15:57 +00:00
|
|
|
/// Returns exp(x) / 2 for x >= log(maxFloat(T)).
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn expo2(x: var) @typeOf(x) {
|
2017-06-16 08:26:10 +00:00
|
|
|
const T = @typeOf(x);
|
2017-12-22 05:50:30 +00:00
|
|
|
return switch (T) {
|
2017-06-16 08:26:10 +00:00
|
|
|
f32 => expo2f(x),
|
|
|
|
f64 => expo2d(x),
|
|
|
|
else => @compileError("expo2 not implemented for " ++ @typeName(T)),
|
2017-12-22 05:50:30 +00:00
|
|
|
};
|
2017-06-16 08:26:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
fn expo2f(x: f32) f32 {
|
2017-06-16 08:26:10 +00:00
|
|
|
const k: u32 = 235;
|
|
|
|
const kln2 = 0x1.45C778p+7;
|
|
|
|
|
|
|
|
const u = (0x7F + k / 2) << 23;
|
|
|
|
const scale = @bitCast(f32, u);
|
2017-12-22 05:50:30 +00:00
|
|
|
return math.exp(x - kln2) * scale * scale;
|
2017-06-16 08:26:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
fn expo2d(x: f64) f64 {
|
2017-06-16 08:26:10 +00:00
|
|
|
const k: u32 = 2043;
|
|
|
|
const kln2 = 0x1.62066151ADD8BP+10;
|
|
|
|
|
|
|
|
const u = (0x3FF + k / 2) << 20;
|
|
|
|
const scale = @bitCast(f64, u64(u) << 32);
|
2017-12-22 05:50:30 +00:00
|
|
|
return math.exp(x - kln2) * scale * scale;
|
2017-06-16 08:26:10 +00:00
|
|
|
}
|