authorgravatar for git@adrianjdelgado.comAdrian Delgado <git@adrianjdelgado.com> 2023-03-22 13:22:25-05:00
committergravatar for git@adrianjdelgado.comAdrian Delgado <git@adrianjdelgado.com> 2023-03-22 13:22:25-05:00
logd6ad69d40a3ba32e9f10a12618dbeece2e62a051
tree5e3c5e308dfd8adb752f0507dcd5c43a967533cc
parentf34aa2d14fbacc7e4d2471c8b4897da257cffc03

workaround bootstrapping limitations


1 files changed, 36 insertions(+), 4 deletions(-)

lib/std/math/log10.zig+36-4
...@@ -1,4 +1,5 @@...@@ -1,4 +1,5 @@
1const std = @import("../std.zig");1const std = @import("../std.zig");
2const builtin = @import("builtin");
2const math = std.math;3const math = std.math;
3const testing = std.testing;4const testing = std.testing;
4const maxInt = std.math.maxInt;5const maxInt = std.math.maxInt;
...@@ -74,11 +75,26 @@ pub fn log10_int(x: anytype) Log2Int(@TypeOf(x)) {...@@ -74,11 +75,26 @@ pub fn log10_int(x: anytype) Log2Int(@TypeOf(x)) {
74}75}
7576
76fn pow10(comptime y: comptime_int) comptime_int {77fn pow10(comptime y: comptime_int) comptime_int {
77 var result = 1;78 if (y == 0) return 1;
78 for (0..y) |_| {79
79 result *= 10;80 var squaring = 0;
81 var s = 1;
82
83 while (s <= y) : (s <<= 1) {
84 squaring += 1;
85 }
86
87 squaring -= 1;
88
89 var result = 10;
90
91 for (0..squaring) |_| {
92 result *= result;
80 }93 }
81 return result;94
95 const rest_exp = y - (1 << squaring);
96
97 return result * pow10(rest_exp);
82}98}
8399
84inline fn log10_int_u8(x: u8) u32 {100inline fn log10_int_u8(x: u8) u32 {
...@@ -129,6 +145,14 @@ test "oldlog10 doesn't work" {...@@ -129,6 +145,14 @@ test "oldlog10 doesn't work" {
129}145}
130146
131test "log10_int vs old implementation" {147test "log10_int vs old implementation" {
148 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
149 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
150 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
151 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
152 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
153 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
154 if (builtin.zig_backend == .stage2_llvm and comptime builtin.target.isWasm()) return error.SkipZigTest; // TODO
155
132 const int_types = .{ u8, u16, u32, u64, u128 };156 const int_types = .{ u8, u16, u32, u64, u128 };
133157
134 inline for (int_types) |T| {158 inline for (int_types) |T| {
...@@ -144,6 +168,14 @@ test "log10_int vs old implementation" {...@@ -144,6 +168,14 @@ test "log10_int vs old implementation" {
144}168}
145169
146test "log10_int close to powers of 10" {170test "log10_int close to powers of 10" {
171 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
172 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
173 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
174 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
175 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
176 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
177 if (builtin.zig_backend == .stage2_llvm and comptime builtin.target.isWasm()) return error.SkipZigTest; // TODO
178
147 const int_types = .{ u8, u16, u32, u64, u128, u256, u512 };179 const int_types = .{ u8, u16, u32, u64, u128, u256, u512 };
148 const max_log_values: [7]usize = .{ 2, 4, 9, 19, 38, 77, 154 };180 const max_log_values: [7]usize = .{ 2, 4, 9, 19, 38, 77, 154 };
149181