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 {
168168 const data = table[@field(self, @tagName(choice)).state];
169169 const T = @TypeOf(@field(self, @tagName(choice))).State;
170170 const bits_summand = try bit_reader.readBitsNoEof(T, data.bits);
171 const next_state = data.baseline + bits_summand;
172 @field(self, @tagName(choice)).state = @intCast(@TypeOf(@field(self, @tagName(choice))).State, next_state);
171 const next_state = std.math.cast(
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;
173176 },
174177 }
175178 }
......@@ -1045,10 +1048,10 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H
10451048 const even_data = entries[even_state];
10461049 var read_bits: usize = 0;
10471050 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;
10491052 i += 1;
10501053 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;
10521055 log.debug("overflow condition: setting weights[{d}] = {d}", .{ i, weights[i] });
10531056 i += 1;
10541057 break;
......@@ -1058,11 +1061,11 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H
10581061 read_bits = 0;
10591062 const odd_data = entries[odd_state];
10601063 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;
10621065 i += 1;
10631066 if (read_bits < odd_data.bits) {
10641067 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;
10661069 log.debug("overflow condition: setting weights[{d}] = {d}", .{ i, weights[i] });
10671070 i += 1;
10681071 break;
......@@ -1100,9 +1103,9 @@ fn decodeHuffmanTree(src: []const u8, consumed_count: *usize) !LiteralsSection.H
11001103 log.debug("weight power sum = {d}", .{weight_power_sum});
11011104
11021105 // 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;
11041107 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;
11061109 log.debug("weights[{d}] = {d}", .{ symbol_count - 1, weights[symbol_count - 1] });
11071110
11081111 var weight_sorted_prefixed_symbols: [256]LiteralsSection.HuffmanTree.PrefixedSymbol = undefined;
......@@ -1367,7 +1370,7 @@ fn decodeFseTable(
13671370 while (accumulated_probability < total_probability) {
13681371 // WARNING: The RFC in poorly worded, and would suggest std.math.log2_int_ceil is correct here,
13691372 // 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;
13711374 const small = try bit_reader.readBitsNoEof(u16, max_bits - 1);
13721375
13731376 const cutoff = (@as(u16, 1) << max_bits) - 1 - (total_probability - accumulated_probability + 1);