2017-06-20 04:01:04 -07:00
|
|
|
// Special Cases:
|
|
|
|
//
|
|
|
|
// - log10(+inf) = +inf
|
|
|
|
// - log10(0) = -inf
|
|
|
|
// - log10(x) = nan if x < 0
|
|
|
|
// - log10(nan) = nan
|
|
|
|
|
2017-12-23 19:08:53 -08:00
|
|
|
const std = @import("../index.zig");
|
|
|
|
const math = std.math;
|
|
|
|
const assert = std.debug.assert;
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-18 22:32:15 -07:00
|
|
|
const builtin = @import("builtin");
|
|
|
|
const TypeId = builtin.TypeId;
|
2017-06-16 01:26:10 -07:00
|
|
|
|
2017-08-28 01:28:42 -07:00
|
|
|
pub fn log10(x: var) -> @typeOf(x) {
|
2017-06-16 01:26:10 -07:00
|
|
|
const T = @typeOf(x);
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-18 22:32:15 -07:00
|
|
|
switch (@typeId(T)) {
|
|
|
|
TypeId.FloatLiteral => {
|
2017-12-21 21:50:30 -08:00
|
|
|
return @typeOf(1.0)(log10_64(x));
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-18 22:32:15 -07:00
|
|
|
},
|
|
|
|
TypeId.Float => {
|
|
|
|
return switch (T) {
|
|
|
|
f32 => log10_32(x),
|
|
|
|
f64 => log10_64(x),
|
|
|
|
else => @compileError("log10 not implemented for " ++ @typeName(T)),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
TypeId.IntLiteral => {
|
|
|
|
return @typeOf(1)(math.floor(log10_64(f64(x))));
|
|
|
|
},
|
|
|
|
TypeId.Int => {
|
|
|
|
return T(math.floor(log10_64(f64(x))));
|
|
|
|
},
|
2017-06-16 01:26:10 -07:00
|
|
|
else => @compileError("log10 not implemented for " ++ @typeName(T)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-18 22:32:15 -07:00
|
|
|
pub fn log10_32(x_: f32) -> f32 {
|
2017-06-16 01:26:10 -07:00
|
|
|
const ivln10hi: f32 = 4.3432617188e-01;
|
|
|
|
const ivln10lo: f32 = -3.1689971365e-05;
|
|
|
|
const log10_2hi: f32 = 3.0102920532e-01;
|
|
|
|
const log10_2lo: f32 = 7.9034151668e-07;
|
|
|
|
const Lg1: f32 = 0xaaaaaa.0p-24;
|
|
|
|
const Lg2: f32 = 0xccce13.0p-25;
|
|
|
|
const Lg3: f32 = 0x91e9ee.0p-25;
|
|
|
|
const Lg4: f32 = 0xf89e26.0p-26;
|
|
|
|
|
|
|
|
var x = x_;
|
|
|
|
var u = @bitCast(u32, x);
|
|
|
|
var ix = u;
|
|
|
|
var k: i32 = 0;
|
|
|
|
|
|
|
|
// x < 2^(-126)
|
|
|
|
if (ix < 0x00800000 or ix >> 31 != 0) {
|
|
|
|
// log(+-0) = -inf
|
2017-08-09 07:09:38 -07:00
|
|
|
if (ix << 1 == 0) {
|
2017-06-20 04:01:04 -07:00
|
|
|
return -math.inf(f32);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
// log(-#) = nan
|
|
|
|
if (ix >> 31 != 0) {
|
2017-06-20 04:01:04 -07:00
|
|
|
return math.nan(f32);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
k -= 25;
|
|
|
|
x *= 0x1.0p25;
|
|
|
|
ix = @bitCast(u32, x);
|
|
|
|
} else if (ix >= 0x7F800000) {
|
|
|
|
return x;
|
|
|
|
} else if (ix == 0x3F800000) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// x into [sqrt(2) / 2, sqrt(2)]
|
|
|
|
ix += 0x3F800000 - 0x3F3504F3;
|
|
|
|
k += i32(ix >> 23) - 0x7F;
|
|
|
|
ix = (ix & 0x007FFFFF) + 0x3F3504F3;
|
|
|
|
x = @bitCast(f32, ix);
|
|
|
|
|
|
|
|
const f = x - 1.0;
|
|
|
|
const s = f / (2.0 + f);
|
|
|
|
const z = s * s;
|
|
|
|
const w = z * z;
|
|
|
|
const t1 = w * (Lg2 + w * Lg4);
|
|
|
|
const t2 = z * (Lg1 + w * Lg3);
|
|
|
|
const R = t2 + t1;
|
|
|
|
const hfsq = 0.5 * f * f;
|
|
|
|
|
|
|
|
var hi = f - hfsq;
|
|
|
|
u = @bitCast(u32, hi);
|
|
|
|
u &= 0xFFFFF000;
|
|
|
|
hi = @bitCast(f32, u);
|
|
|
|
const lo = f - hi - hfsq + s * (hfsq + R);
|
|
|
|
const dk = f32(k);
|
|
|
|
|
2017-12-21 21:50:30 -08:00
|
|
|
return dk * log10_2lo + (lo + hi) * ivln10lo + lo * ivln10hi + hi * ivln10hi + dk * log10_2hi;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-18 22:32:15 -07:00
|
|
|
pub fn log10_64(x_: f64) -> f64 {
|
2017-06-16 01:26:10 -07:00
|
|
|
const ivln10hi: f64 = 4.34294481878168880939e-01;
|
|
|
|
const ivln10lo: f64 = 2.50829467116452752298e-11;
|
|
|
|
const log10_2hi: f64 = 3.01029995663611771306e-01;
|
|
|
|
const log10_2lo: f64 = 3.69423907715893078616e-13;
|
|
|
|
const Lg1: f64 = 6.666666666666735130e-01;
|
|
|
|
const Lg2: f64 = 3.999999999940941908e-01;
|
|
|
|
const Lg3: f64 = 2.857142874366239149e-01;
|
|
|
|
const Lg4: f64 = 2.222219843214978396e-01;
|
|
|
|
const Lg5: f64 = 1.818357216161805012e-01;
|
|
|
|
const Lg6: f64 = 1.531383769920937332e-01;
|
|
|
|
const Lg7: f64 = 1.479819860511658591e-01;
|
|
|
|
|
|
|
|
var x = x_;
|
|
|
|
var ix = @bitCast(u64, x);
|
|
|
|
var hx = u32(ix >> 32);
|
|
|
|
var k: i32 = 0;
|
|
|
|
|
|
|
|
if (hx < 0x00100000 or hx >> 31 != 0) {
|
|
|
|
// log(+-0) = -inf
|
2017-08-09 07:09:38 -07:00
|
|
|
if (ix << 1 == 0) {
|
2017-06-20 04:01:04 -07:00
|
|
|
return -math.inf(f32);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
// log(-#) = nan
|
|
|
|
if (hx >> 31 != 0) {
|
2017-06-20 04:01:04 -07:00
|
|
|
return math.nan(f32);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// subnormal, scale x
|
|
|
|
k -= 54;
|
|
|
|
x *= 0x1.0p54;
|
2017-12-21 21:50:30 -08:00
|
|
|
hx = u32(@bitCast(u64, x) >> 32);
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
else if (hx >= 0x7FF00000) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
else if (hx == 0x3FF00000 and ix << 32 == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// x into [sqrt(2) / 2, sqrt(2)]
|
|
|
|
hx += 0x3FF00000 - 0x3FE6A09E;
|
|
|
|
k += i32(hx >> 20) - 0x3FF;
|
|
|
|
hx = (hx & 0x000FFFFF) + 0x3FE6A09E;
|
|
|
|
ix = (u64(hx) << 32) | (ix & 0xFFFFFFFF);
|
|
|
|
x = @bitCast(f64, ix);
|
|
|
|
|
|
|
|
const f = x - 1.0;
|
|
|
|
const hfsq = 0.5 * f * f;
|
|
|
|
const s = f / (2.0 + f);
|
|
|
|
const z = s * s;
|
|
|
|
const w = z * z;
|
|
|
|
const t1 = w * (Lg2 + w * (Lg4 + w * Lg6));
|
|
|
|
const t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7)));
|
|
|
|
const R = t2 + t1;
|
|
|
|
|
|
|
|
// hi + lo = f - hfsq + s * (hfsq + R) ~ log(1 + f)
|
|
|
|
var hi = f - hfsq;
|
|
|
|
var hii = @bitCast(u64, hi);
|
2017-08-09 07:09:38 -07:00
|
|
|
hii &= u64(@maxValue(u64)) << 32;
|
2017-06-16 01:26:10 -07:00
|
|
|
hi = @bitCast(f64, hii);
|
|
|
|
const lo = f - hi - hfsq + s * (hfsq + R);
|
|
|
|
|
|
|
|
// val_hi + val_lo ~ log10(1 + f) + k * log10(2)
|
|
|
|
var val_hi = hi * ivln10hi;
|
|
|
|
const dk = f64(k);
|
|
|
|
const y = dk * log10_2hi;
|
|
|
|
var val_lo = dk * log10_2lo + (lo + hi) * ivln10lo + lo * ivln10hi;
|
|
|
|
|
|
|
|
// Extra precision multiplication
|
|
|
|
const ww = y + val_hi;
|
|
|
|
val_lo += (y - ww) + val_hi;
|
|
|
|
val_hi = ww;
|
|
|
|
|
2017-12-21 21:50:30 -08:00
|
|
|
return val_lo + val_hi;
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.log10" {
|
|
|
|
assert(log10(f32(0.2)) == log10_32(0.2));
|
|
|
|
assert(log10(f64(0.2)) == log10_64(0.2));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.log10_32" {
|
2017-06-16 01:26:10 -07:00
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
assert(math.approxEq(f32, log10_32(0.2), -0.698970, epsilon));
|
|
|
|
assert(math.approxEq(f32, log10_32(0.8923), -0.049489, epsilon));
|
|
|
|
assert(math.approxEq(f32, log10_32(1.5), 0.176091, epsilon));
|
|
|
|
assert(math.approxEq(f32, log10_32(37.45), 1.573452, epsilon));
|
|
|
|
assert(math.approxEq(f32, log10_32(89.123), 1.94999, epsilon));
|
|
|
|
assert(math.approxEq(f32, log10_32(123123.234375), 5.09034, epsilon));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
test "math.log10_64" {
|
2017-06-16 01:26:10 -07:00
|
|
|
const epsilon = 0.000001;
|
|
|
|
|
2017-06-19 11:36:33 -07:00
|
|
|
assert(math.approxEq(f64, log10_64(0.2), -0.698970, epsilon));
|
|
|
|
assert(math.approxEq(f64, log10_64(0.8923), -0.049489, epsilon));
|
|
|
|
assert(math.approxEq(f64, log10_64(1.5), 0.176091, epsilon));
|
|
|
|
assert(math.approxEq(f64, log10_64(37.45), 1.573452, epsilon));
|
|
|
|
assert(math.approxEq(f64, log10_64(89.123), 1.94999, epsilon));
|
|
|
|
assert(math.approxEq(f64, log10_64(123123.234375), 5.09034, epsilon));
|
2017-06-16 01:26:10 -07:00
|
|
|
}
|
2017-06-20 04:01:04 -07:00
|
|
|
|
|
|
|
test "math.log10_32.special" {
|
|
|
|
assert(math.isPositiveInf(log10_32(math.inf(f32))));
|
|
|
|
assert(math.isNegativeInf(log10_32(0.0)));
|
|
|
|
assert(math.isNan(log10_32(-1.0)));
|
|
|
|
assert(math.isNan(log10_32(math.nan(f32))));
|
|
|
|
}
|
|
|
|
|
|
|
|
test "math.log10_64.special" {
|
|
|
|
assert(math.isPositiveInf(log10_64(math.inf(f64))));
|
|
|
|
assert(math.isNegativeInf(log10_64(0.0)));
|
|
|
|
assert(math.isNan(log10_64(-1.0)));
|
|
|
|
assert(math.isNan(log10_64(math.nan(f64))));
|
|
|
|
}
|