Skip to content

Commit 936cf57

Browse files
LewisGaulandrewrk
authored andcommitted
Add tests for log(), with bugfix for 64-bit boundary case
1 parent da8974e commit 936cf57

File tree

1 file changed

+64
-29
lines changed

1 file changed

+64
-29
lines changed

lib/compiler_rt/log.zig

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
const std = @import("std");
88
const builtin = @import("builtin");
99
const math = std.math;
10-
const testing = std.testing;
10+
const expect = std.testing.expect;
11+
const expectEqual = std.testing.expectEqual;
1112
const arch = builtin.cpu.arch;
1213
const common = @import("common.zig");
1314

@@ -110,8 +111,8 @@ pub fn log(x_: f64) callconv(.c) f64 {
110111

111112
// subnormal, scale x
112113
k -= 54;
113-
x *= 0x1.0p54;
114-
hx = @intCast(@as(u64, @bitCast(ix)) >> 32);
114+
x *= 0x1p54;
115+
hx = @intCast(@as(u64, @bitCast(x)) >> 32);
115116
} else if (hx >= 0x7FF00000) {
116117
return x;
117118
} else if (hx == 0x3FF00000 and ix << 32 == 0) {
@@ -159,38 +160,72 @@ pub fn logl(x: c_longdouble) callconv(.c) c_longdouble {
159160
}
160161
}
161162

162-
test "ln32" {
163-
const epsilon = 0.000001;
163+
test "logf() special" {
164+
try expectEqual(logf(0.0), -math.inf(f32));
165+
try expectEqual(logf(-0.0), -math.inf(f32));
166+
try expectEqual(logf(1.0), 0.0);
167+
try expectEqual(logf(math.e), 1.0);
168+
try expectEqual(logf(math.inf(f32)), math.inf(f32));
169+
try expect(math.isNan(logf(-1.0)));
170+
try expect(math.isNan(logf(-math.inf(f32))));
171+
try expect(math.isNan(logf(math.nan(f32))));
172+
try expect(math.isNan(logf(math.snan(f32))));
173+
}
164174

165-
try testing.expect(math.approxEqAbs(f32, logf(0.2), -1.609438, epsilon));
166-
try testing.expect(math.approxEqAbs(f32, logf(0.8923), -0.113953, epsilon));
167-
try testing.expect(math.approxEqAbs(f32, logf(1.5), 0.405465, epsilon));
168-
try testing.expect(math.approxEqAbs(f32, logf(37.45), 3.623007, epsilon));
169-
try testing.expect(math.approxEqAbs(f32, logf(89.123), 4.490017, epsilon));
170-
try testing.expect(math.approxEqAbs(f32, logf(123123.234375), 11.720941, epsilon));
175+
test "logf() sanity" {
176+
try expect(math.isNan(logf(-0x1.0223a0p+3)));
177+
try expectEqual(logf(0x1.161868p+2), 0x1.7815b0p+0);
178+
try expect(math.isNan(logf(-0x1.0c34b4p+3)));
179+
try expect(math.isNan(logf(-0x1.a206f0p+2)));
180+
try expectEqual(logf(0x1.288bbcp+3), 0x1.1cfcd6p+1);
181+
try expectEqual(logf(0x1.52efd0p-1), -0x1.a6694cp-2);
182+
try expect(math.isNan(logf(-0x1.a05cc8p-2)));
183+
try expectEqual(logf(0x1.1f9efap-1), -0x1.2742bap-1);
184+
try expectEqual(logf(0x1.8c5db0p-1), -0x1.062160p-2);
185+
try expect(math.isNan(logf(-0x1.5b86eap-1)));
171186
}
172187

173-
test "ln64" {
174-
const epsilon = 0.000001;
188+
test "logf() boundary" {
189+
try expectEqual(logf(0x1.fffffep+127), 0x1.62e430p+6); // Max input value
190+
try expectEqual(logf(0x1p-149), -0x1.9d1da0p+6); // Min positive input value
191+
try expect(math.isNan(logf(-0x1p-149))); // Min negative input value
192+
try expectEqual(logf(0x1.000002p+0), 0x1.fffffep-24); // Last value before result reaches +0
193+
try expectEqual(logf(0x1.fffffep-1), -0x1p-24); // Last value before result reaches -0
194+
try expectEqual(logf(0x1p-126), -0x1.5d58a0p+6); // First subnormal
195+
try expect(math.isNan(logf(-0x1p-126))); // First negative subnormal
196+
}
175197

176-
try testing.expect(math.approxEqAbs(f64, log(0.2), -1.609438, epsilon));
177-
try testing.expect(math.approxEqAbs(f64, log(0.8923), -0.113953, epsilon));
178-
try testing.expect(math.approxEqAbs(f64, log(1.5), 0.405465, epsilon));
179-
try testing.expect(math.approxEqAbs(f64, log(37.45), 3.623007, epsilon));
180-
try testing.expect(math.approxEqAbs(f64, log(89.123), 4.490017, epsilon));
181-
try testing.expect(math.approxEqAbs(f64, log(123123.234375), 11.720941, epsilon));
198+
test "log() special" {
199+
try expectEqual(log(0.0), -math.inf(f64));
200+
try expectEqual(log(-0.0), -math.inf(f64));
201+
try expectEqual(log(1.0), 0.0);
202+
try expectEqual(log(math.e), 1.0);
203+
try expectEqual(log(math.inf(f64)), math.inf(f64));
204+
try expect(math.isNan(log(-1.0)));
205+
try expect(math.isNan(log(-math.inf(f64))));
206+
try expect(math.isNan(log(math.nan(f64))));
207+
try expect(math.isNan(log(math.snan(f64))));
182208
}
183209

184-
test "ln32.special" {
185-
try testing.expect(math.isPositiveInf(logf(math.inf(f32))));
186-
try testing.expect(math.isNegativeInf(logf(0.0)));
187-
try testing.expect(math.isNan(logf(-1.0)));
188-
try testing.expect(math.isNan(logf(math.nan(f32))));
210+
test "log() sanity" {
211+
try expect(math.isNan(log(-0x1.02239f3c6a8f1p+3)));
212+
try expectEqual(log(0x1.161868e18bc67p+2), 0x1.7815b08f99c65p+0);
213+
try expect(math.isNan(log(-0x1.0c34b3e01e6e7p+3)));
214+
try expect(math.isNan(log(-0x1.a206f0a19dcc4p+2)));
215+
try expectEqual(log(0x1.288bbb0d6a1e6p+3), 0x1.1cfcd53d72604p+1);
216+
try expectEqual(log(0x1.52efd0cd80497p-1), -0x1.a6694a4a85621p-2);
217+
try expect(math.isNan(log(-0x1.a05cc754481d1p-2)));
218+
try expectEqual(log(0x1.1f9ef934745cbp-1), -0x1.2742bc03d02ddp-1);
219+
try expectEqual(log(0x1.8c5db097f7442p-1), -0x1.06215de4a3f92p-2);
220+
try expect(math.isNan(log(-0x1.5b86ea8118a0ep-1)));
189221
}
190222

191-
test "ln64.special" {
192-
try testing.expect(math.isPositiveInf(log(math.inf(f64))));
193-
try testing.expect(math.isNegativeInf(log(0.0)));
194-
try testing.expect(math.isNan(log(-1.0)));
195-
try testing.expect(math.isNan(log(math.nan(f64))));
223+
test "log() boundary" {
224+
try expectEqual(log(0x1.fffffffffffffp+1023), 0x1.62e42fefa39efp+9); // Max input value
225+
try expectEqual(log(0x1p-1074), -0x1.74385446d71c3p+9); // Min positive input value
226+
try expect(math.isNan(log(-0x1p-1074))); // Min negative input value
227+
try expectEqual(log(0x1.0000000000001p+0), 0x1.fffffffffffffp-53); // Last value before result reaches +0
228+
try expectEqual(log(0x1.fffffffffffffp-1), -0x1p-53); // Last value before result reaches -0
229+
try expectEqual(log(0x1p-1022), -0x1.6232bdd7abcd2p+9); // First subnormal
230+
try expect(math.isNan(log(-0x1p-1022))); // First negative subnormal
196231
}

0 commit comments

Comments
 (0)