2017-06-16 09:26:10 +01:00
|
|
|
const math = @import("index.zig");
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
pub fn expo2(x: var) @typeOf(x) {
|
2017-06-16 09:26:10 +01:00
|
|
|
const T = @typeOf(x);
|
2017-12-22 05:50:30 +00:00
|
|
|
return switch (T) {
|
2017-06-16 09:26:10 +01: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 09:26:10 +01:00
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
fn expo2f(x: f32) f32 {
|
2017-06-16 09:26:10 +01: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 09:26:10 +01:00
|
|
|
}
|
|
|
|
|
2018-01-25 09:10:11 +00:00
|
|
|
fn expo2d(x: f64) f64 {
|
2017-06-16 09:26:10 +01: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 09:26:10 +01:00
|
|
|
}
|