authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-01-23 23:46:15+11:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-02-20 09:09:06+11:00
log082acd7f17358ed3a7787f284b48b754fefd8187
treeab13fecd839ffcc46af8d97030db56531fb52b8a
parentfc64c279a497263c15feb857eb5442aa615179c4

std.compress.zstandard: clean up integer casts


1 files changed, 12 insertions(+), 9 deletions(-)

lib/std/compress/zstandard/decompress.zig+12-9
...@@ -168,8 +168,11 @@ pub const DecodeState = struct {...@@ -168,8 +168,11 @@ pub const DecodeState = struct {
168 const data = table[@field(self, @tagName(choice)).state];168 const data = table[@field(self, @tagName(choice)).state];
169 const T = @TypeOf(@field(self, @tagName(choice))).State;169 const T = @TypeOf(@field(self, @tagName(choice))).State;
170 const bits_summand = try bit_reader.readBitsNoEof(T, data.bits);170 const bits_summand = try bit_reader.readBitsNoEof(T, data.bits);
171 const next_state = data.baseline + bits_summand;171 const next_state = std.math.cast(
172 @field(self, @tagName(choice)).state = @intCast(@TypeOf(@field(self, @tagName(choice))).State, next_state);172 @TypeOf(@field(self, @tagName(choice))).State,
173 data.baseline + bits_summand,
174 ) orelse return error.MalformedFseBits;
175 @field(self, @tagName(choice)).state = next_state;
173 },176 },
174 }177 }
175 }178 }
...@@ -1045,10 +1048,10 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H...@@ -1045,10 +1048,10 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H
1045 const even_data = entries[even_state];1048 const even_data = entries[even_state];
1046 var read_bits: usize = 0;1049 var read_bits: usize = 0;
1047 const even_bits = try huff_bits.readBits(u32, even_data.bits, &read_bits);1050 const even_bits = try huff_bits.readBits(u32, even_data.bits, &read_bits);
1048 weights[i] = @intCast(u4, even_data.symbol);1051 weights[i] = std.math.cast(u4, even_data.symbol) orelse return error.MalformedHuffmanTree;
1049 i += 1;1052 i += 1;
1050 if (read_bits < even_data.bits) {1053 if (read_bits < even_data.bits) {
1051 weights[i] = @intCast(u4, entries[odd_state].symbol);1054 weights[i] = std.math.cast(u4, entries[odd_state].symbol) orelse return error.MalformedHuffmanTree;
1052 log.debug("overflow condition: setting weights[{d}] = {d}", .{ i, weights[i] });1055 log.debug("overflow condition: setting weights[{d}] = {d}", .{ i, weights[i] });
1053 i += 1;1056 i += 1;
1054 break;1057 break;
...@@ -1058,11 +1061,11 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H...@@ -1058,11 +1061,11 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H
1058 read_bits = 0;1061 read_bits = 0;
1059 const odd_data = entries[odd_state];1062 const odd_data = entries[odd_state];
1060 const odd_bits = try huff_bits.readBits(u32, odd_data.bits, &read_bits);1063 const odd_bits = try huff_bits.readBits(u32, odd_data.bits, &read_bits);
1061 weights[i] = @intCast(u4, odd_data.symbol);1064 weights[i] = std.math.cast(u4, odd_data.symbol) orelse return error.MalformedHuffmanTree;
1062 i += 1;1065 i += 1;
1063 if (read_bits < odd_data.bits) {1066 if (read_bits < odd_data.bits) {
1064 if (i == 256) return error.MalformedHuffmanTree;1067 if (i == 256) return error.MalformedHuffmanTree;
1065 weights[i] = @intCast(u4, entries[even_state].symbol);1068 weights[i] = std.math.cast(u4, entries[even_state].symbol) orelse return error.MalformedHuffmanTree;
1066 log.debug("overflow condition: setting weights[{d}] = {d}", .{ i, weights[i] });1069 log.debug("overflow condition: setting weights[{d}] = {d}", .{ i, weights[i] });
1067 i += 1;1070 i += 1;
1068 break;1071 break;
...@@ -1100,9 +1103,9 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H...@@ -1100,9 +1103,9 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H
1100 log.debug("weight power sum = {d}", .{weight_power_sum});1103 log.debug("weight power sum = {d}", .{weight_power_sum});
11011104
1102 // advance to next power of two (even if weight_power_sum is a power of 2)1105 // advance to next power of two (even if weight_power_sum is a power of 2)
1103 max_number_of_bits = @intCast(u4, std.math.log2_int(u16, weight_power_sum) + 1);1106 max_number_of_bits = std.math.log2_int(u16, weight_power_sum) + 1;
1104 const next_power_of_two = @as(u16, 1) << max_number_of_bits;1107 const next_power_of_two = @as(u16, 1) << max_number_of_bits;
1105 weights[symbol_count - 1] = @intCast(u4, std.math.log2_int(u16, next_power_of_two - weight_power_sum) + 1);1108 weights[symbol_count - 1] = std.math.log2_int(u16, next_power_of_two - weight_power_sum) + 1;
1106 log.debug("weights[{d}] = {d}", .{ symbol_count - 1, weights[symbol_count - 1] });1109 log.debug("weights[{d}] = {d}", .{ symbol_count - 1, weights[symbol_count - 1] });
11071110
1108 var weight_sorted_prefixed_symbols: [256]LiteralsSection.HuffmanTree.PrefixedSymbol = undefined;1111 var weight_sorted_prefixed_symbols: [256]LiteralsSection.HuffmanTree.PrefixedSymbol = undefined;
...@@ -1367,7 +1370,7 @@ fn decodeFseTable(...@@ -1367,7 +1370,7 @@ fn decodeFseTable(
1367 while (accumulated_probability < total_probability) {1370 while (accumulated_probability < total_probability) {
1368 // WARNING: The RFC in poorly worded, and would suggest std.math.log2_int_ceil is correct here,1371 // WARNING: The RFC in poorly worded, and would suggest std.math.log2_int_ceil is correct here,
1369 // but power of two (remaining probabilities + 1) need max bits set to 1 more.1372 // but power of two (remaining probabilities + 1) need max bits set to 1 more.
1370 const max_bits = @intCast(u4, std.math.log2_int(u16, total_probability - accumulated_probability + 1)) + 1;1373 const max_bits = std.math.log2_int(u16, total_probability - accumulated_probability + 1) + 1;
1371 const small = try bit_reader.readBitsNoEof(u16, max_bits - 1);1374 const small = try bit_reader.readBitsNoEof(u16, max_bits - 1);
13721375
1373 const cutoff = (@as(u16, 1) << max_bits) - 1 - (total_probability - accumulated_probability + 1);1376 const cutoff = (@as(u16, 1) << max_bits) - 1 - (total_probability - accumulated_probability + 1);