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

Add tests for log2()


1 files changed, 62 insertions(+), 24 deletions(-)

lib/compiler_rt/log2.zig+62-24
...@@ -8,6 +8,7 @@ const std = @import("std");...@@ -8,6 +8,7 @@ const std = @import("std");
8const builtin = @import("builtin");8const builtin = @import("builtin");
9const math = std.math;9const math = std.math;
10const expect = std.testing.expect;10const expect = std.testing.expect;
11const expectEqual = std.testing.expectEqual;
11const maxInt = std.math.maxInt;12const maxInt = std.math.maxInt;
12const arch = builtin.cpu.arch;13const arch = builtin.cpu.arch;
13const common = @import("common.zig");14const common = @import("common.zig");
...@@ -179,36 +180,73 @@ pub fn log2l(x: c_longdouble) callconv(.c) c_longdouble {...@@ -179,36 +180,73 @@ pub fn log2l(x: c_longdouble) callconv(.c) c_longdouble {
179 }180 }
180}181}
181182
182test "log2_32" {183test "log2f() special" {
183 const epsilon = 0.000001;184 try expectEqual(log2f(0.0), -math.inf(f32));
184185 try expectEqual(log2f(-0.0), -math.inf(f32));
185 try expect(math.approxEqAbs(f32, log2f(0.2), -2.321928, epsilon));186 try expectEqual(log2f(1.0), 0.0);
186 try expect(math.approxEqAbs(f32, log2f(0.8923), -0.164399, epsilon));187 try expectEqual(log2f(2.0), 1.0);
187 try expect(math.approxEqAbs(f32, log2f(1.5), 0.584962, epsilon));188 try expectEqual(log2f(math.inf(f32)), math.inf(f32));
188 try expect(math.approxEqAbs(f32, log2f(37.45), 5.226894, epsilon));189 try expect(math.isNan(log2f(-1.0)));
189 try expect(math.approxEqAbs(f32, log2f(123123.234375), 16.909744, epsilon));190 try expect(math.isNan(log2f(-math.inf(f32))));
191 try expect(math.isNan(log2f(math.nan(f32))));
192 try expect(math.isNan(log2f(math.snan(f32))));
190}193}
191194
192test "log2_64" {195test "log2f() sanity" {
193 const epsilon = 0.000001;196 try expect(math.isNan(log2f(-0x1.0223a0p+3)));
194197 try expectEqual(log2f(0x1.161868p+2), 0x1.0f49acp+1);
195 try expect(math.approxEqAbs(f64, log2(0.2), -2.321928, epsilon));198 try expect(math.isNan(log2f(-0x1.0c34b4p+3)));
196 try expect(math.approxEqAbs(f64, log2(0.8923), -0.164399, epsilon));199 try expect(math.isNan(log2f(-0x1.a206f0p+2)));
197 try expect(math.approxEqAbs(f64, log2(1.5), 0.584962, epsilon));200 try expectEqual(log2f(0x1.288bbcp+3), 0x1.9b2676p+1);
198 try expect(math.approxEqAbs(f64, log2(37.45), 5.226894, epsilon));201 try expectEqual(log2f(0x1.52efd0p-1), -0x1.30b494p-1); // Disagrees with GCC in last bit
199 try expect(math.approxEqAbs(f64, log2(123123.234375), 16.909744, epsilon));202 try expect(math.isNan(log2f(-0x1.a05cc8p-2)));
203 try expectEqual(log2f(0x1.1f9efap-1), -0x1.a9f89ap-1);
204 try expectEqual(log2f(0x1.8c5db0p-1), -0x1.7a2c96p-2);
205 try expect(math.isNan(log2f(-0x1.5b86eap-1)));
200}206}
201207
202test "log2_32.special" {208test "log2f() boundary" {
203 try expect(math.isPositiveInf(log2f(math.inf(f32))));209 try expectEqual(log2f(0x1.fffffep+127), 0x1p+7); // Max input value
204 try expect(math.isNegativeInf(log2f(0.0)));210 try expectEqual(log2f(0x1p-149), -0x1.2ap+7); // Min positive input value
205 try expect(math.isNan(log2f(-1.0)));211 try expect(math.isNan(log2f(-0x1p-149))); // Min negative input value
206 try expect(math.isNan(log2f(math.nan(f32))));212 try expectEqual(log2f(0x1.000002p+0), 0x1.715474p-23); // Last value before result reaches +0
213 try expectEqual(log2f(0x1.fffffep-1), -0x1.715478p-24); // Last value before result reaches -0
214 try expectEqual(log2f(0x1p-126), -0x1.f8p+6); // First subnormal
215 try expect(math.isNan(log2f(-0x1p-126))); // First negative subnormal
216
207}217}
208218
209test "log2_64.special" {219test "log2() special" {
210 try expect(math.isPositiveInf(log2(math.inf(f64))));220 try expectEqual(log2(0.0), -math.inf(f64));
211 try expect(math.isNegativeInf(log2(0.0)));221 try expectEqual(log2(-0.0), -math.inf(f64));
222 try expectEqual(log2(1.0), 0.0);
223 try expectEqual(log2(2.0), 1.0);
224 try expectEqual(log2(math.inf(f64)), math.inf(f64));
212 try expect(math.isNan(log2(-1.0)));225 try expect(math.isNan(log2(-1.0)));
226 try expect(math.isNan(log2(-math.inf(f64))));
213 try expect(math.isNan(log2(math.nan(f64))));227 try expect(math.isNan(log2(math.nan(f64))));
228 try expect(math.isNan(log2(math.snan(f64))));
229}
230
231test "log2() sanity" {
232 try expect(math.isNan(log2(-0x1.02239f3c6a8f1p+3)));
233 try expectEqual(log2(0x1.161868e18bc67p+2), 0x1.0f49ac3838580p+1);
234 try expect(math.isNan(log2(-0x1.0c34b3e01e6e7p+3)));
235 try expect(math.isNan(log2(-0x1.a206f0a19dcc4p+2)));
236 try expectEqual(log2(0x1.288bbb0d6a1e6p+3), 0x1.9b26760c2a57ep+1);
237 try expectEqual(log2(0x1.52efd0cd80497p-1), -0x1.30b490ef684c7p-1);
238 try expect(math.isNan(log2(-0x1.a05cc754481d1p-2)));
239 try expectEqual(log2(0x1.1f9ef934745cbp-1), -0x1.a9f89b5f5acb8p-1);
240 try expectEqual(log2(0x1.8c5db097f7442p-1), -0x1.7a2c947173f06p-2);
241 try expect(math.isNan(log2(-0x1.5b86ea8118a0ep-1)));
242}
243
244test "log2() boundary" {
245 try expectEqual(log2(0x1.fffffffffffffp+1023), 0x1p+10); // Max input value
246 try expectEqual(log2(0x1p-1074), -0x1.0c8p+10); // Min positive input value
247 try expect(math.isNan(log2(-0x1p-1074))); // Min negative input value
248 try expectEqual(log2(0x1.0000000000001p+0), 0x1.71547652b82fdp-52); // Last value before result reaches +0
249 try expectEqual(log2(0x1.fffffffffffffp-1), -0x1.71547652b82fep-53); // Last value before result reaches -0
250 try expectEqual(log2(0x1p-1022), -0x1.ffp+9); // First subnormal
251 try expect(math.isNan(log2(-0x1p-1022))); // First negative subnormal
214}252}