| ... | @@ -1,4 +1,5 @@ | ... | @@ -1,4 +1,5 @@ |
| 1 | const std = @import("../std.zig"); | 1 | const std = @import("../std.zig"); |
| | 2 | const builtin = @import("builtin"); |
| 2 | const math = std.math; | 3 | const math = std.math; |
| 3 | const testing = std.testing; | 4 | const testing = std.testing; |
| 4 | const maxInt = std.math.maxInt; | 5 | const 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 | } |
| 75 | | 76 | |
| 76 | fn pow10(comptime y: comptime_int) comptime_int { | 77 | fn 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 | } |
| 83 | | 99 | |
| 84 | inline fn log10_int_u8(x: u8) u32 { | 100 | inline 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 | } |
| 130 | | 146 | |
| 131 | test "log10_int vs old implementation" { | 147 | test "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 }; |
| 133 | | 157 | |
| 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 | } |
| 145 | | 169 | |
| 146 | test "log10_int close to powers of 10" { | 170 | test "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 }; |
| 149 | | 181 | |