2019-04-30 23:15:57 -07: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/scalbnf.c
|
|
|
|
// https://git.musl-libc.org/cgit/musl/tree/src/math/scalbn.c
|
|
|
|
|
2019-03-02 13:46:04 -08:00
|
|
|
const std = @import("../std.zig");
|
2017-12-23 19:08:53 -08:00
|
|
|
const math = std.math;
|
2019-02-08 15:18:47 -08:00
|
|
|
const expect = std.testing.expect;
|
2017-06-16 01:26:10 -07:00
|
|
|
|
2019-04-30 23:15:57 -07:00
|
|
|
/// Returns x * 2^n.
|
2019-12-09 12:56:19 -08:00
|
|
|
pub fn scalbn(x: var, n: i32) @TypeOf(x) {
|
|
|
|
const T = @TypeOf(x);
|
2017-12-21 21:50:30 -08:00
|
|
|
return switch (T) {
|
2017-12-22 10:14:07 -08:00
|
|
|
f32 => scalbn32(x, n),
|
|
|
|
f64 => scalbn64(x, n),
|
2017-06-16 01:26:10 -07:00
|
|
|
else => @compileError("scalbn not implemented for " ++ @typeName(T)),
|
2017-12-21 21:50:30 -08:00
|
|
|
};
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn scalbn32(x: f32, n_: i32) f32 {
|
2017-06-16 01:26:10 -07:00
|
|
|
var y = x;
|
|
|
|
var n = n_;
|
|
|
|
|
|
|
|
if (n > 127) {
|
|
|
|
y *= 0x1.0p127;
|
|
|
|
n -= 127;
|
|
|
|
if (n > 1023) {
|
|
|
|
y *= 0x1.0p127;
|
|
|
|
n -= 127;
|
|
|
|
if (n > 127) {
|
|
|
|
n = 127;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (n < -126) {
|
|
|
|
y *= 0x1.0p-126 * 0x1.0p24;
|
|
|
|
n += 126 - 24;
|
|
|
|
if (n < -126) {
|
|
|
|
y *= 0x1.0p-126 * 0x1.0p24;
|
|
|
|
n += 126 - 24;
|
|
|
|
if (n < -126) {
|
|
|
|
n = -126;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-16 23:57:07 -07:00
|
|
|
const u = @intCast(u32, n +% 0x7F) << 23;
|
2017-12-21 21:50:30 -08:00
|
|
|
return y * @bitCast(f32, u);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:10:11 -08:00
|
|
|
fn scalbn64(x: f64, n_: i32) f64 {
|
2017-06-16 01:26:10 -07:00
|
|
|
var y = x;
|
|
|
|
var n = n_;
|
|
|
|
|
|
|
|
if (n > 1023) {
|
2017-09-27 23:15:06 -07:00
|
|
|
y *= 0x1.0p1023;
|
2017-06-16 01:26:10 -07:00
|
|
|
n -= 1023;
|
|
|
|
if (n > 1023) {
|
2017-09-27 23:15:06 -07:00
|
|
|
y *= 0x1.0p1023;
|
2017-06-16 01:26:10 -07:00
|
|
|
n -= 1023;
|
|
|
|
if (n > 1023) {
|
|
|
|
n = 1023;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (n < -1022) {
|
|
|
|
y *= 0x1.0p-1022 * 0x1.0p53;
|
|
|
|
n += 1022 - 53;
|
|
|
|
if (n < -1022) {
|
|
|
|
y *= 0x1.0p-1022 * 0x1.0p53;
|
|
|
|
n += 1022 - 53;
|
|
|
|
if (n < -1022) {
|
|
|
|
n = -1022;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-16 23:57:07 -07:00
|
|
|
const u = @intCast(u64, n +% 0x3FF) << 52;
|
2017-12-21 21:50:30 -08:00
|
|
|
return y * @bitCast(f64, u);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.scalbn" {
|
2019-11-06 20:25:57 -08:00
|
|
|
expect(scalbn(@as(f32, 1.5), 4) == scalbn32(1.5, 4));
|
|
|
|
expect(scalbn(@as(f64, 1.5), 4) == scalbn64(1.5, 4));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.scalbn32" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(scalbn32(1.5, 4) == 24.0);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.scalbn64" {
|
2019-02-08 15:18:47 -08:00
|
|
|
expect(scalbn64(1.5, 4) == 24.0);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|