Skip to content

Commit 95a599c

Browse files
committed
Cleanup HUFv07_readDTableX4
1 parent 2da53a0 commit 95a599c

File tree

1 file changed

+40
-62
lines changed

1 file changed

+40
-62
lines changed

lib/legacy/zstd_v07.rs

Lines changed: 40 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ unsafe fn HUFv07_fillDTableX4(
11331133
s = s.wrapping_add(1);
11341134
}
11351135
}
1136-
unsafe fn HUFv07_readDTableX4(DTable: &mut HUFv07_DTable, src: &[u8]) -> Result<usize, Error> {
1136+
fn HUFv07_readDTableX4(DTable: &mut HUFv07_DTable, src: &[u8]) -> Result<usize, Error> {
11371137
let mut weightList: [u8; HUFv07_SYMBOLVALUE_MAX + 1] = [0; HUFv07_SYMBOLVALUE_MAX + 1];
11381138
let mut sortedSymbol: [sortedSymbol_t; 256] = [sortedSymbol_t {
11391139
symbol: 0,
@@ -1143,10 +1143,9 @@ unsafe fn HUFv07_readDTableX4(DTable: &mut HUFv07_DTable, src: &[u8]) -> Result<
11431143
[0; HUFv07_TABLELOG_ABSOLUTEMAX + 1];
11441144
let mut rankStart0: [u32; HUFv07_TABLELOG_ABSOLUTEMAX + 2] =
11451145
[0; HUFv07_TABLELOG_ABSOLUTEMAX + 2];
1146-
let rankStart = rankStart0.as_mut_ptr().add(1);
1146+
let rankStart = &mut rankStart0[1..];
11471147
let mut rankVal: rankVal_t = [[0; 17]; 16];
11481148
let mut tableLog: u32 = 0;
1149-
let mut maxW: u32 = 0;
11501149
let mut sizeOfSort: u32 = 0;
11511150
let mut nbSymbols: u32 = 0;
11521151
let mut dtd = DTable.description;
@@ -1166,70 +1165,51 @@ unsafe fn HUFv07_readDTableX4(DTable: &mut HUFv07_DTable, src: &[u8]) -> Result<
11661165
if tableLog > maxTableLog {
11671166
return Err(Error::tableLog_tooLarge);
11681167
}
1169-
maxW = tableLog;
1170-
while *rankStats.as_mut_ptr().offset(maxW as isize) == 0 {
1171-
maxW = maxW.wrapping_sub(1);
1168+
let mut maxW = tableLog;
1169+
while rankStats[maxW as usize] == 0 {
1170+
maxW -= 1
11721171
}
1173-
let mut w: u32 = 0;
11741172
let mut nextRankStart = 0u32;
1175-
w = 1;
1176-
while w < maxW.wrapping_add(1) {
1173+
for w in 1..maxW + 1 {
11771174
let current = nextRankStart;
1178-
nextRankStart = nextRankStart.wrapping_add(*rankStats.as_mut_ptr().offset(w as isize));
1179-
*rankStart.offset(w as isize) = current;
1180-
w = w.wrapping_add(1);
1175+
nextRankStart = nextRankStart.wrapping_add(rankStats[w as usize]);
1176+
rankStart[w as usize] = current;
11811177
}
1182-
*rankStart = nextRankStart;
1178+
rankStart[0] = nextRankStart;
11831179
sizeOfSort = nextRankStart;
1184-
let mut s: u32 = 0;
1185-
s = 0;
1186-
while s < nbSymbols {
1187-
let w_0 = *weightList.as_mut_ptr().offset(s as isize) as u32;
1188-
let fresh37 = &mut (*rankStart.offset(w_0 as isize));
1189-
let fresh38 = *fresh37;
1190-
*fresh37 = (*fresh37).wrapping_add(1);
1191-
let r = fresh38;
1192-
(*sortedSymbol.as_mut_ptr().offset(r as isize)).symbol = s as u8;
1193-
(*sortedSymbol.as_mut_ptr().offset(r as isize)).weight = w_0 as u8;
1194-
s = s.wrapping_add(1);
1195-
}
1196-
*rankStart = 0;
1197-
let rankVal0 = (*rankVal.as_mut_ptr()).as_mut_ptr();
1180+
for s in 0..nbSymbols {
1181+
let w = weightList[s as usize];
1182+
let r = rankStart[usize::from(w)] as usize;
1183+
rankStart[usize::from(w)] += 1;
1184+
sortedSymbol[r].symbol = s as u8;
1185+
sortedSymbol[r].weight = w;
1186+
}
1187+
rankStart[0] = 0;
11981188
let rescale = maxTableLog.wrapping_sub(tableLog).wrapping_sub(1) as core::ffi::c_int;
11991189
let mut nextRankVal = 0u32;
1200-
let mut w_1: u32 = 0;
1201-
w_1 = 1;
1202-
while w_1 < maxW.wrapping_add(1) {
1203-
let current_0 = nextRankVal;
1204-
nextRankVal = nextRankVal.wrapping_add(
1205-
*rankStats.as_mut_ptr().offset(w_1 as isize) << w_1.wrapping_add(rescale as u32),
1206-
);
1207-
*rankVal0.offset(w_1 as isize) = current_0;
1208-
w_1 = w_1.wrapping_add(1);
1190+
for w in 1..maxW + 1 {
1191+
let current = nextRankVal;
1192+
nextRankVal += rankStats[w as usize] << w.wrapping_add(rescale as u32);
1193+
rankVal[0][w as usize] = current;
12091194
}
12101195
let minBits = tableLog.wrapping_add(1).wrapping_sub(maxW);
1211-
let mut consumed: u32 = 0;
1212-
consumed = minBits;
1213-
while consumed < maxTableLog.wrapping_sub(minBits).wrapping_add(1) {
1214-
let rankValPtr = (*rankVal.as_mut_ptr().offset(consumed as isize)).as_mut_ptr();
1215-
let mut w_2: u32 = 0;
1216-
w_2 = 1;
1217-
while w_2 < maxW.wrapping_add(1) {
1218-
*rankValPtr.offset(w_2 as isize) = *rankVal0.offset(w_2 as isize) >> consumed;
1219-
w_2 = w_2.wrapping_add(1);
1220-
}
1221-
consumed = consumed.wrapping_add(1);
1222-
}
1223-
HUFv07_fillDTableX4(
1224-
dt,
1225-
maxTableLog,
1226-
sortedSymbol.as_mut_ptr(),
1227-
sizeOfSort,
1228-
rankStart0.as_mut_ptr(),
1229-
rankVal.as_mut_ptr(),
1230-
maxW,
1231-
tableLog.wrapping_add(1),
1232-
);
1196+
for consumed in minBits..maxTableLog.wrapping_sub(minBits).wrapping_add(1) {
1197+
for w in 1..maxW + 1 {
1198+
rankVal[consumed as usize][w as usize] = rankVal[0][w as usize] >> consumed;
1199+
}
1200+
}
1201+
unsafe {
1202+
HUFv07_fillDTableX4(
1203+
dt,
1204+
maxTableLog,
1205+
sortedSymbol.as_mut_ptr(),
1206+
sizeOfSort,
1207+
rankStart0.as_mut_ptr(),
1208+
rankVal.as_mut_ptr(),
1209+
maxW,
1210+
tableLog + 1,
1211+
)
1212+
};
12331213
dtd.tableLog = maxTableLog as u8;
12341214
dtd.tableType = 1;
12351215
DTable.description = dtd;
@@ -1415,13 +1395,11 @@ fn HUFv07_decompress4X4_DCtx(
14151395
dst: Writer<'_>,
14161396
cSrc: &[u8],
14171397
) -> Result<usize, Error> {
1418-
let mut ip = cSrc;
1419-
let hSize = unsafe { HUFv07_readDTableX4(dctx, ip)? };
1398+
let hSize = HUFv07_readDTableX4(dctx, cSrc)?;
14201399
if hSize >= cSrc.len() {
14211400
return Err(Error::srcSize_wrong);
14221401
}
1423-
ip = &ip[hSize..];
1424-
unsafe { HUFv07_decompress4X4_usingDTable_internal(dst, ip, dctx) }
1402+
unsafe { HUFv07_decompress4X4_usingDTable_internal(dst, &cSrc[hSize..], dctx) }
14251403
}
14261404
static algoTime: [[algo_time_t; 3]; 16] = [
14271405
[

0 commit comments

Comments
 (0)