authorgravatar for lewis.gaul@gmail.comLewis Gaul <lewis.gaul@gmail.com> 2024-05-25 22:17:47+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-17 05:04:59+02:00
log936cf57a38aa8d7cbaf593e416b146294bf1b427
tree8a4927cc7b460a46d0554644d0cb256960a42c8b
parentda8974e57f3581d7302efa3cacbd0d55f6c87bd4

Add tests for log(), with bugfix for 64-bit boundary case


1 files changed, 64 insertions(+), 29 deletions(-)

lib/compiler_rt/log.zig+64-29
...@@ -7,7 +7,8 @@...@@ -7,7 +7,8 @@
7const std = @import("std");7const std = @import("std");
8const builtin = @import("builtin");8const builtin = @import("builtin");
9const math = std.math;9const math = std.math;
10const testing = std.testing;10const expect = std.testing.expect;
11const expectEqual = std.testing.expectEqual;
11const arch = builtin.cpu.arch;12const arch = builtin.cpu.arch;
12const common = @import("common.zig");13const common = @import("common.zig");
1314
...@@ -110,8 +111,8 @@ pub fn log(x_: f64) callconv(.c) f64 {...@@ -110,8 +111,8 @@ pub fn log(x_: f64) callconv(.c) f64 {
110111
111 // subnormal, scale x112 // subnormal, scale x
112 k -= 54;113 k -= 54;
113 x *= 0x1.0p54;114 x *= 0x1p54;
114 hx = @intCast(@as(u64, @bitCast(ix)) >> 32);115 hx = @intCast(@as(u64, @bitCast(x)) >> 32);
115 } else if (hx >= 0x7FF00000) {116 } else if (hx >= 0x7FF00000) {
116 return x;117 return x;
117 } else if (hx == 0x3FF00000 and ix << 32 == 0) {118 } else if (hx == 0x3FF00000 and ix << 32 == 0) {
...@@ -159,38 +160,72 @@ pub fn logl(x: c_longdouble) callconv(.c) c_longdouble {...@@ -159,38 +160,72 @@ pub fn logl(x: c_longdouble) callconv(.c) c_longdouble {
159 }160 }
160}161}
161162
162test "ln32" {163test "logf() special" {
163 const epsilon = 0.000001;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));175test "logf() sanity" {
166 try testing.expect(math.approxEqAbs(f32, logf(0.8923), -0.113953, epsilon));176 try expect(math.isNan(logf(-0x1.0223a0p+3)));
167 try testing.expect(math.approxEqAbs(f32, logf(1.5), 0.405465, epsilon));177 try expectEqual(logf(0x1.161868p+2), 0x1.7815b0p+0);
168 try testing.expect(math.approxEqAbs(f32, logf(37.45), 3.623007, epsilon));178 try expect(math.isNan(logf(-0x1.0c34b4p+3)));
169 try testing.expect(math.approxEqAbs(f32, logf(89.123), 4.490017, epsilon));179 try expect(math.isNan(logf(-0x1.a206f0p+2)));
170 try testing.expect(math.approxEqAbs(f32, logf(123123.234375), 11.720941, epsilon));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)));
171}186}
172187
173test "ln64" {188test "logf() boundary" {
174 const epsilon = 0.000001;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));198test "log() special" {
177 try testing.expect(math.approxEqAbs(f64, log(0.8923), -0.113953, epsilon));199 try expectEqual(log(0.0), -math.inf(f64));
178 try testing.expect(math.approxEqAbs(f64, log(1.5), 0.405465, epsilon));200 try expectEqual(log(-0.0), -math.inf(f64));
179 try testing.expect(math.approxEqAbs(f64, log(37.45), 3.623007, epsilon));201 try expectEqual(log(1.0), 0.0);
180 try testing.expect(math.approxEqAbs(f64, log(89.123), 4.490017, epsilon));202 try expectEqual(log(math.e), 1.0);
181 try testing.expect(math.approxEqAbs(f64, log(123123.234375), 11.720941, epsilon));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))));
182}208}
183209
184test "ln32.special" {210test "log() sanity" {
185 try testing.expect(math.isPositiveInf(logf(math.inf(f32))));211 try expect(math.isNan(log(-0x1.02239f3c6a8f1p+3)));
186 try testing.expect(math.isNegativeInf(logf(0.0)));212 try expectEqual(log(0x1.161868e18bc67p+2), 0x1.7815b08f99c65p+0);
187 try testing.expect(math.isNan(logf(-1.0)));213 try expect(math.isNan(log(-0x1.0c34b3e01e6e7p+3)));
188 try testing.expect(math.isNan(logf(math.nan(f32))));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)));
189}221}
190222
191test "ln64.special" {223test "log() boundary" {
192 try testing.expect(math.isPositiveInf(log(math.inf(f64))));224 try expectEqual(log(0x1.fffffffffffffp+1023), 0x1.62e42fefa39efp+9); // Max input value
193 try testing.expect(math.isNegativeInf(log(0.0)));225 try expectEqual(log(0x1p-1074), -0x1.74385446d71c3p+9); // Min positive input value
194 try testing.expect(math.isNan(log(-1.0)));226 try expect(math.isNan(log(-0x1p-1074))); // Min negative input value
195 try testing.expect(math.isNan(log(math.nan(f64))));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
196}231}