diff --git a/lib/common/fse.rs b/lib/common/fse.rs index a3cb408f..a57059d5 100644 --- a/lib/common/fse.rs +++ b/lib/common/fse.rs @@ -1,7 +1,6 @@ use libc::ptrdiff_t; use crate::lib::common::bitstream::{BIT_CStream_t, BIT_addBits, BIT_flushBits, BitContainerType}; -use crate::lib::common::mem::MEM_read16; pub(crate) type FSE_CTable = core::ffi::c_uint; @@ -69,63 +68,97 @@ pub(crate) struct FSE_symbolCompressionTransform { pub(crate) deltaNbBits: u32, } +/// Pack two consecutive `u16` values into one `u32`. #[inline] -pub(crate) unsafe fn FSE_initCState(statePtr: *mut FSE_CState_t, ct: *const FSE_CTable) { - let ptr = ct as *const core::ffi::c_void; - let u16ptr = ptr as *const u16; - let tableLog = MEM_read16(ptr) as u32; - (*statePtr).value = (1) << tableLog; - (*statePtr).stateTable = u16ptr.add(2) as *const core::ffi::c_void; - (*statePtr).symbolTT = ct.add(1).offset( - (if tableLog != 0 { - (1) << tableLog.wrapping_sub(1) - } else { - 1 - }) as isize, - ) as *const core::ffi::c_void; - (*statePtr).stateLog = tableLog; +pub(crate) fn FSE_writeU16Pair(first: u16, second: u16) -> FSE_CTable { + let [a, b] = first.to_ne_bytes(); + let [c, d] = second.to_ne_bytes(); + + u32::from_ne_bytes([a, b, c, d]) +} + +/// Read the `index`th `u16` of a `&[u32]`. +#[inline] +fn FSE_readU16(ct: &[FSE_CTable], index: usize) -> u16 { + let bytes = ct[index / 2].to_ne_bytes(); + if index.is_multiple_of(2) { + u16::from_ne_bytes([bytes[0], bytes[1]]) + } else { + u16::from_ne_bytes([bytes[2], bytes[3]]) + } +} + +#[inline] +pub(crate) fn FSE_initCState(statePtr: &mut FSE_CState_t, ct: &[FSE_CTable]) { + // the table header occupies the first two bytes of `ct` + let tableLog = FSE_readU16(ct, 0) as u32; + + // the state table follows the header + let stateTable = &ct[1..]; + let symbolTT = &ct[FSE_symbolTTIndex(tableLog)..]; + + statePtr.value = 1 << tableLog; + statePtr.stateTable = stateTable.as_ptr().cast::(); + statePtr.symbolTT = symbolTT.as_ptr().cast::(); + statePtr.stateLog = tableLog; } #[inline] -pub(crate) unsafe fn FSE_initCState2( - statePtr: *mut FSE_CState_t, - ct: *const FSE_CTable, +pub(crate) const fn FSE_symbolTTIndex(tableLog: u32) -> usize { + let skip_header = 1; + + match tableLog { + 0 => skip_header + 1, + _ => skip_header + (1 << (tableLog - 1)), + } +} + +/// Read the transform of `symbol` out of the symbol transformation table of `ct`. +#[inline] +fn FSE_readSymbolTT( + ct: &[FSE_CTable], + tableLog: u32, symbol: u32, -) { +) -> FSE_symbolCompressionTransform { + let index = FSE_symbolTTIndex(tableLog) + 2 * symbol as usize; + + FSE_symbolCompressionTransform { + deltaFindState: ct[index] as core::ffi::c_int, + deltaNbBits: ct[index + 1], + } +} + +#[inline] +pub(crate) fn FSE_initCState2(statePtr: &mut FSE_CState_t, ct: &[FSE_CTable], symbol: u32) { FSE_initCState(statePtr, ct); - let symbolTT = - *((*statePtr).symbolTT as *const FSE_symbolCompressionTransform).offset(symbol as isize); - let stateTable = (*statePtr).stateTable as *const u16; - let nbBitsOut = (symbolTT.deltaNbBits).wrapping_add(((1) << 15) as u32) >> 16; - (*statePtr).value = (nbBitsOut << 16).wrapping_sub(symbolTT.deltaNbBits) as ptrdiff_t; - (*statePtr).value = *stateTable - .offset(((*statePtr).value >> nbBitsOut) + symbolTT.deltaFindState as ptrdiff_t) - as ptrdiff_t; + let symbolTT = FSE_readSymbolTT(ct, statePtr.stateLog, symbol); + let nbBitsOut = (symbolTT.deltaNbBits).wrapping_add((1) << 15) >> 16; + let value = (nbBitsOut << 16).wrapping_sub(symbolTT.deltaNbBits) as ptrdiff_t; + + // the state table starts at the third `u16` of `ct` + let index = 2 + (value >> nbBitsOut) + symbolTT.deltaFindState as ptrdiff_t; + statePtr.value = FSE_readU16(ct, index as usize) as ptrdiff_t; } #[inline] pub(crate) unsafe fn FSE_encodeSymbol( bitC: &mut BIT_CStream_t, - statePtr: *mut FSE_CState_t, + statePtr: &mut FSE_CState_t, symbol: core::ffi::c_uint, ) { let symbolTT = - *((*statePtr).symbolTT as *const FSE_symbolCompressionTransform).offset(symbol as isize); - let stateTable = (*statePtr).stateTable as *const u16; - let nbBitsOut = (((*statePtr).value + symbolTT.deltaNbBits as ptrdiff_t) >> 16) as u32; - BIT_addBits(bitC, (*statePtr).value as BitContainerType, nbBitsOut); - (*statePtr).value = *stateTable - .offset(((*statePtr).value >> nbBitsOut) + symbolTT.deltaFindState as ptrdiff_t) + *(statePtr.symbolTT as *const FSE_symbolCompressionTransform).offset(symbol as isize); + let stateTable = statePtr.stateTable as *const u16; + let nbBitsOut = ((statePtr.value + symbolTT.deltaNbBits as ptrdiff_t) >> 16) as u32; + BIT_addBits(bitC, statePtr.value as BitContainerType, nbBitsOut); + statePtr.value = *stateTable + .offset((statePtr.value >> nbBitsOut) + symbolTT.deltaFindState as ptrdiff_t) as ptrdiff_t; } #[inline] -pub(crate) unsafe fn FSE_flushCState(bitC: &mut BIT_CStream_t, statePtr: *const FSE_CState_t) { - BIT_addBits( - bitC, - (*statePtr).value as BitContainerType, - (*statePtr).stateLog, - ); +pub(crate) unsafe fn FSE_flushCState(bitC: &mut BIT_CStream_t, statePtr: &FSE_CState_t) { + BIT_addBits(bitC, statePtr.value as BitContainerType, statePtr.stateLog); BIT_flushBits(bitC); } diff --git a/lib/compress/fse_compress.rs b/lib/compress/fse_compress.rs index e9c96623..24d7a14d 100644 --- a/lib/compress/fse_compress.rs +++ b/lib/compress/fse_compress.rs @@ -1,52 +1,17 @@ -use libc::{ptrdiff_t, size_t}; +use libc::size_t; use crate::lib::common::bits::ZSTD_highbit32; use crate::lib::common::bitstream::{ - BIT_CStream_t, BIT_addBits, BIT_closeCStream, BIT_flushBits, BIT_flushBitsFast, - BIT_initCStream, BitContainerType, + BIT_CStream_t, BIT_closeCStream, BIT_flushBits, BIT_flushBitsFast, BIT_initCStream, + BitContainerType, }; use crate::lib::common::error_private::{ERR_isError, Error}; use crate::lib::common::fse::{ - FSE_CState_t, FSE_CTable, FSE_encodeSymbol, FSE_symbolCompressionTransform, - FSE_DEFAULT_TABLELOG, FSE_MAX_TABLELOG, FSE_MIN_TABLELOG, FSE_NCOUNTBOUND, + FSE_CState_t, FSE_CTable, FSE_encodeSymbol, FSE_flushCState, FSE_initCState2, + FSE_symbolCompressionTransform, FSE_symbolTTIndex, FSE_writeU16Pair, FSE_DEFAULT_TABLELOG, + FSE_MAX_TABLELOG, FSE_MIN_TABLELOG, FSE_NCOUNTBOUND, }; -use crate::lib::common::mem::{MEM_read16, MEM_write64}; - -#[inline] -unsafe fn FSE_initCState(statePtr: &mut FSE_CState_t, ct: *const FSE_CTable) { - let ptr = ct as *const core::ffi::c_void; - let u16ptr = ptr as *const u16; - let tableLog = MEM_read16(ptr) as u32; - statePtr.value = 1 << tableLog; - statePtr.stateTable = u16ptr.add(2) as *const core::ffi::c_void; - statePtr.symbolTT = ct.add(1).offset( - (if tableLog != 0 { - 1 << tableLog.wrapping_sub(1) - } else { - 1 - }) as isize, - ) as *const core::ffi::c_void; - statePtr.stateLog = tableLog; -} - -#[inline] -unsafe fn FSE_initCState2(statePtr: &mut FSE_CState_t, ct: *const FSE_CTable, symbol: u32) { - FSE_initCState(statePtr, ct); - let symbolTT = - *(statePtr.symbolTT as *const FSE_symbolCompressionTransform).offset(symbol as isize); - let stateTable = statePtr.stateTable as *const u16; - let nbBitsOut = (symbolTT.deltaNbBits).wrapping_add((1 << 15) as u32) >> 16; - statePtr.value = (nbBitsOut << 16).wrapping_sub(symbolTT.deltaNbBits) as ptrdiff_t; - statePtr.value = *stateTable - .offset((statePtr.value >> nbBitsOut) + symbolTT.deltaFindState as ptrdiff_t) - as ptrdiff_t; -} - -#[inline] -unsafe fn FSE_flushCState(bitC: &mut BIT_CStream_t, statePtr: &FSE_CState_t) { - BIT_addBits(bitC, statePtr.value as BitContainerType, statePtr.stateLog); - BIT_flushBits(bitC); -} +use crate::lib::common::mem::MEM_write64; /// Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`). /// wkspSize should be sized to handle worst case situation, which is `1< size_t { - let ptr = ct as *mut core::ffi::c_void; - let tableU16 = (ptr as *mut u16).add(2); - let FSCTptr = (ptr as *mut u32).add(2) as *mut core::ffi::c_void; - let symbolTT = FSCTptr as *mut FSE_symbolCompressionTransform; +pub(crate) fn FSE_buildCTable_rle(ct: &mut [FSE_CTable], symbolValue: u8) -> size_t { + // header: a tableLog of zero, and `symbolValue` as the maximum symbol value + ct[0] = FSE_writeU16Pair(0, symbolValue as u16); - // header - *tableU16.sub(2) = 0; - *tableU16.sub(1) = symbolValue as u16; - - // Build table - *tableU16 = 0; - *tableU16.add(1) = 0; // just in case + // the (two-entry) state table, zeroed just in case + ct[1] = FSE_writeU16Pair(0, 0); // Build Symbol Transformation Table - (*symbolTT.offset(symbolValue as isize)).deltaNbBits = 0; - (*symbolTT.offset(symbolValue as isize)).deltaFindState = 0; + let index = FSE_symbolTTIndex(0) + 2 * symbolValue as usize; + ct[index] = 0; // deltaFindState + ct[index + 1] = 0; // deltaNbBits 0 } @@ -655,7 +614,7 @@ unsafe fn FSE_compress_usingCTable_generic( dstSize: size_t, src: *const core::ffi::c_void, mut srcSize: size_t, - ct: *const FSE_CTable, + ct: &[FSE_CTable], fast: bool, ) -> size_t { let istart = src as *const u8; @@ -773,7 +732,7 @@ pub(crate) unsafe fn FSE_compress_usingCTable( dstSize: size_t, src: *const core::ffi::c_void, srcSize: size_t, - ct: *const FSE_CTable, + ct: &[FSE_CTable], ) -> size_t { let fast = dstSize >= srcSize diff --git a/lib/compress/hist.rs b/lib/compress/hist.rs index e1204bd2..025ac81b 100644 --- a/lib/compress/hist.rs +++ b/lib/compress/hist.rs @@ -31,7 +31,7 @@ pub unsafe fn HIST_add( pub unsafe fn HIST_count_simple( count: *mut core::ffi::c_uint, - maxSymbolValuePtr: *mut core::ffi::c_uint, + maxSymbolValuePtr: &mut core::ffi::c_uint, src: *const core::ffi::c_void, srcSize: size_t, ) -> core::ffi::c_uint { @@ -83,7 +83,7 @@ pub unsafe fn HIST_count_simple( /// histogram's alphabet is larger than *maxSymbolValuePtr) unsafe fn HIST_count_parallel_wksp( count: *mut core::ffi::c_uint, - maxSymbolValuePtr: *mut core::ffi::c_uint, + maxSymbolValuePtr: &mut core::ffi::c_uint, source: *const core::ffi::c_void, sourceSize: size_t, check: HIST_checkInput_e, @@ -181,7 +181,7 @@ unsafe fn HIST_count_parallel_wksp( /// `workSpaceSize` must be >= HIST_WKSP_SIZE pub unsafe fn HIST_countFast_wksp( count: *mut core::ffi::c_uint, - maxSymbolValuePtr: *mut core::ffi::c_uint, + maxSymbolValuePtr: &mut core::ffi::c_uint, source: *const core::ffi::c_void, sourceSize: size_t, workSpace: *mut core::ffi::c_void, @@ -211,7 +211,7 @@ pub unsafe fn HIST_countFast_wksp( /// `workSpace` size must be table of >= HIST_WKSP_SIZE_U32 unsigned pub unsafe fn HIST_count_wksp( count: *mut core::ffi::c_uint, - maxSymbolValuePtr: *mut core::ffi::c_uint, + maxSymbolValuePtr: &mut core::ffi::c_uint, source: *const core::ffi::c_void, sourceSize: size_t, workSpace: *mut core::ffi::c_void, @@ -248,7 +248,7 @@ pub unsafe fn HIST_count_wksp( /// fast variant (unsafe : won't check if src contains values beyond count[] limit) pub unsafe fn HIST_countFast( count: *mut core::ffi::c_uint, - maxSymbolValuePtr: *mut core::ffi::c_uint, + maxSymbolValuePtr: &mut core::ffi::c_uint, source: *const core::ffi::c_void, sourceSize: size_t, ) -> size_t { @@ -265,7 +265,7 @@ pub unsafe fn HIST_countFast( pub unsafe fn HIST_count( count: *mut core::ffi::c_uint, - maxSymbolValuePtr: *mut core::ffi::c_uint, + maxSymbolValuePtr: &mut core::ffi::c_uint, src: *const core::ffi::c_void, srcSize: size_t, ) -> size_t { diff --git a/lib/compress/huf_compress.rs b/lib/compress/huf_compress.rs index f95daef1..35562b1c 100644 --- a/lib/compress/huf_compress.rs +++ b/lib/compress/huf_compress.rs @@ -172,7 +172,7 @@ unsafe fn HUF_compressWeights( oend.offset_from_unsigned(op), weightTable.as_ptr().cast::(), wtSize, - ((*wksp).CTable).as_mut_ptr(), + &(*wksp).CTable, ); if ERR_isError(cSize) { return cSize; diff --git a/lib/compress/zstd_compress.rs b/lib/compress/zstd_compress.rs index 9756e827..b949e697 100644 --- a/lib/compress/zstd_compress.rs +++ b/lib/compress/zstd_compress.rs @@ -4871,9 +4871,6 @@ unsafe fn ZSTD_buildSequencesStatistics( let ostart = dst; let oend = dstEnd; let mut op = ostart; - let CTable_LitLength = (nextEntropy.litlengthCTable).as_mut_ptr(); - let CTable_OffsetBits = (nextEntropy.offcodeCTable).as_mut_ptr(); - let CTable_MatchLength = (nextEntropy.matchlengthCTable).as_mut_ptr(); let ofCodeTable: *const u8 = (*seqStorePtr).ofCode; let llCodeTable: *const u8 = (*seqStorePtr).llCode; let mlCodeTable: *const u8 = (*seqStorePtr).mlCode; @@ -4908,7 +4905,7 @@ unsafe fn ZSTD_buildSequencesStatistics( mostFrequent, nbSeq, LLFSELog, - (prevEntropy.litlengthCTable).as_ptr(), + &prevEntropy.litlengthCTable, LL_defaultNorm.as_ptr(), LL_defaultNormLog, ZSTD_defaultAllowed, @@ -4917,7 +4914,7 @@ unsafe fn ZSTD_buildSequencesStatistics( let countSize = ZSTD_buildCTable( op as *mut core::ffi::c_void, oend.offset_from_unsigned(op), - CTable_LitLength, + &mut nextEntropy.litlengthCTable, LLFSELog, stats.LLtype as SymbolEncodingType_e, countWorkspace, @@ -4927,8 +4924,7 @@ unsafe fn ZSTD_buildSequencesStatistics( LL_defaultNorm.as_ptr(), LL_defaultNormLog, MaxLL, - (prevEntropy.litlengthCTable).as_ptr(), - size_of::<[FSE_CTable; 329]>(), + &prevEntropy.litlengthCTable, entropyWorkspace, entropyWkspSize, ); @@ -4965,7 +4961,7 @@ unsafe fn ZSTD_buildSequencesStatistics( mostFrequent_0, nbSeq, OffFSELog, - (prevEntropy.offcodeCTable).as_ptr(), + &prevEntropy.offcodeCTable, OF_defaultNorm.as_ptr(), OF_defaultNormLog, defaultPolicy, @@ -4974,7 +4970,7 @@ unsafe fn ZSTD_buildSequencesStatistics( let countSize_0 = ZSTD_buildCTable( op as *mut core::ffi::c_void, oend.offset_from_unsigned(op), - CTable_OffsetBits, + &mut nextEntropy.offcodeCTable, OffFSELog, stats.Offtype as SymbolEncodingType_e, countWorkspace, @@ -4984,8 +4980,7 @@ unsafe fn ZSTD_buildSequencesStatistics( OF_defaultNorm.as_ptr(), OF_defaultNormLog, DefaultMaxOff, - (prevEntropy.offcodeCTable).as_ptr(), - size_of::<[FSE_CTable; 193]>(), + &prevEntropy.offcodeCTable, entropyWorkspace, entropyWkspSize, ); @@ -5016,7 +5011,7 @@ unsafe fn ZSTD_buildSequencesStatistics( mostFrequent_1, nbSeq, MLFSELog, - (prevEntropy.matchlengthCTable).as_ptr(), + &prevEntropy.matchlengthCTable, ML_defaultNorm.as_ptr(), ML_defaultNormLog, ZSTD_defaultAllowed, @@ -5025,7 +5020,7 @@ unsafe fn ZSTD_buildSequencesStatistics( let countSize_1 = ZSTD_buildCTable( op as *mut core::ffi::c_void, oend.offset_from_unsigned(op), - CTable_MatchLength, + &mut nextEntropy.matchlengthCTable, MLFSELog, stats.MLtype as SymbolEncodingType_e, countWorkspace, @@ -5035,8 +5030,7 @@ unsafe fn ZSTD_buildSequencesStatistics( ML_defaultNorm.as_ptr(), ML_defaultNormLog, MaxML, - (prevEntropy.matchlengthCTable).as_ptr(), - size_of::<[FSE_CTable; 363]>(), + &prevEntropy.matchlengthCTable, entropyWorkspace, entropyWkspSize, ); @@ -5078,9 +5072,6 @@ unsafe fn ZSTD_entropyCompressSeqStore_internal( ) -> size_t { let strategy = cctxParams.cParams.strategy; let count = entropyWorkspace as *mut core::ffi::c_uint; - let CTable_LitLength = (&raw const (nextEntropy.fse.litlengthCTable)).cast::(); - let CTable_OffsetBits = (&raw const (nextEntropy.fse.offcodeCTable)).cast::(); - let CTable_MatchLength = (&raw const (nextEntropy.fse.matchlengthCTable)).cast::(); let sequences: *const SeqDef = (*seqStorePtr).sequencesStart; let nbSeq = ((*seqStorePtr).sequences).offset_from((*seqStorePtr).sequencesStart) as size_t; let ofCodeTable: *const u8 = (*seqStorePtr).ofCode; @@ -5178,11 +5169,11 @@ unsafe fn ZSTD_entropyCompressSeqStore_internal( let bitstreamSize = ZSTD_encodeSequences( op as *mut core::ffi::c_void, oend.offset_from_unsigned(op), - CTable_MatchLength, + &nextEntropy.fse.matchlengthCTable, mlCodeTable, - CTable_OffsetBits, + &nextEntropy.fse.offcodeCTable, ofCodeTable, - CTable_LitLength, + &nextEntropy.fse.litlengthCTable, llCodeTable, sequences, nbSeq, @@ -6310,7 +6301,7 @@ unsafe fn ZSTD_estimateBlockSize_symbolType( codeTable: *const u8, nbSeq: size_t, maxCode: core::ffi::c_uint, - fseCTable: *const FSE_CTable, + fseCTable: &[FSE_CTable], additionalBits: *const u8, defaultNorm: *const core::ffi::c_short, defaultNormLog: u32, @@ -6385,7 +6376,7 @@ unsafe fn ZSTD_estimateBlockSize_sequences( ofCodeTable, nbSeq, MaxOff, - (fseTables.offcodeCTable).as_ptr(), + &fseTables.offcodeCTable, core::ptr::null(), OF_defaultNorm.as_ptr(), OF_defaultNormLog, @@ -6398,7 +6389,7 @@ unsafe fn ZSTD_estimateBlockSize_sequences( llCodeTable, nbSeq, MaxLL, - (fseTables.litlengthCTable).as_ptr(), + &fseTables.litlengthCTable, LL_bits.as_ptr(), LL_defaultNorm.as_ptr(), LL_defaultNormLog, @@ -6411,7 +6402,7 @@ unsafe fn ZSTD_estimateBlockSize_sequences( mlCodeTable, nbSeq, MaxML, - (fseTables.matchlengthCTable).as_ptr(), + &fseTables.matchlengthCTable, ML_bits.as_ptr(), ML_defaultNorm.as_ptr(), ML_defaultNormLog, diff --git a/lib/compress/zstd_compress_sequences.rs b/lib/compress/zstd_compress_sequences.rs index 23e5ee33..eb52ab39 100644 --- a/lib/compress/zstd_compress_sequences.rs +++ b/lib/compress/zstd_compress_sequences.rs @@ -8,7 +8,7 @@ use crate::lib::common::fse::{ FSE_CState_t, FSE_CTable, FSE_bitCost, FSE_encodeSymbol, FSE_flushCState, FSE_initCState, FSE_initCState2, FSE_repeat, FSE_repeat_check, FSE_repeat_none, FSE_repeat_valid, }; -use crate::lib::common::mem::{MEM_32bits, MEM_read16}; +use crate::lib::common::mem::MEM_32bits; use crate::lib::common::zstd_internal::{LLFSELog, LL_bits, MLFSELog, ML_bits, OffFSELog}; use crate::lib::compress::fse_compress::{ FSE_buildCTable_rle, FSE_buildCTable_wksp, FSE_normalizeCount, FSE_optimalTableLog, @@ -52,11 +52,9 @@ static kInverseProbabilityLog256: [core::ffi::c_uint; 256] = [ 37, 36, 34, 33, 31, 30, 28, 26, 25, 23, 22, 20, 19, 17, 16, 14, 13, 11, 10, 8, 7, 5, 4, 2, 1, ]; -unsafe fn ZSTD_getFSEMaxSymbolValue(ctable: *const FSE_CTable) -> core::ffi::c_uint { - let ptr = ctable as *const core::ffi::c_void; - let u16ptr = ptr as *const u16; - - MEM_read16(u16ptr.add(1) as *const core::ffi::c_void) as u32 +fn ZSTD_getFSEMaxSymbolValue(ctable: &[FSE_CTable]) -> core::ffi::c_uint { + let [_, _, a, b] = ctable[0].to_ne_bytes(); + u32::from(u16::from_ne_bytes([a, b])) } /// Returns true if we should use ncount=-1 else we should @@ -124,7 +122,7 @@ unsafe fn ZSTD_entropyCost( /// Returns the cost in bits of encoding the distribution in count using ctable. /// Returns an error if ctable cannot represent all the symbols in count. pub unsafe fn ZSTD_fseBitCost( - ctable: *const FSE_CTable, + ctable: &[FSE_CTable], count: *const core::ffi::c_uint, max: core::ffi::c_uint, ) -> size_t { @@ -187,7 +185,7 @@ pub unsafe fn ZSTD_selectEncodingType( mostFrequent: size_t, nbSeq: size_t, FSELog: core::ffi::c_uint, - prevCTable: *const FSE_CTable, + prevCTable: &[FSE_CTable], defaultNorm: *const core::ffi::c_short, defaultNormLog: u32, isDefaultAllowed: ZSTD_DefaultPolicy_e, @@ -259,7 +257,7 @@ pub unsafe fn ZSTD_selectEncodingType( pub unsafe fn ZSTD_buildCTable( dst: *mut core::ffi::c_void, dstCapacity: size_t, - nextCTable: *mut FSE_CTable, + nextCTable: &mut [FSE_CTable], FSELog: u32, type_0: SymbolEncodingType_e, count: *mut core::ffi::c_uint, @@ -269,8 +267,7 @@ pub unsafe fn ZSTD_buildCTable( defaultNorm: *const i16, defaultNormLog: u32, defaultMax: u32, - prevCTable: *const FSE_CTable, - prevCTableSize: size_t, + prevCTable: &[FSE_CTable], entropyWorkspace: *mut core::ffi::c_void, entropyWorkspaceSize: size_t, ) -> size_t { @@ -291,15 +288,15 @@ pub unsafe fn ZSTD_buildCTable( } 3 => { core::ptr::copy_nonoverlapping( - prevCTable.cast::(), - nextCTable.cast::(), - prevCTableSize, + prevCTable.as_ptr().cast::(), + nextCTable.as_mut_ptr().cast::(), + size_of_val(prevCTable), ); 0 } 0 => { let err_code_0 = FSE_buildCTable_wksp( - nextCTable, + nextCTable.as_mut_ptr(), defaultNorm, defaultMax, defaultNormLog, @@ -343,7 +340,7 @@ pub unsafe fn ZSTD_buildCTable( return err_code_2; } let err_code_3 = FSE_buildCTable_wksp( - nextCTable, + nextCTable.as_mut_ptr(), ((*wksp).norm).as_mut_ptr(), max, tableLog, @@ -362,11 +359,11 @@ pub unsafe fn ZSTD_buildCTable( unsafe fn ZSTD_encodeSequences_body( dst: *mut core::ffi::c_void, dstCapacity: size_t, - CTable_MatchLength: *const FSE_CTable, + CTable_MatchLength: &[FSE_CTable], mlCodeTable: *const u8, - CTable_OffsetBits: *const FSE_CTable, + CTable_OffsetBits: &[FSE_CTable], ofCodeTable: *const u8, - CTable_LitLength: *const FSE_CTable, + CTable_LitLength: &[FSE_CTable], llCodeTable: *const u8, sequences: *const SeqDef, nbSeq: size_t, @@ -552,11 +549,11 @@ unsafe fn ZSTD_encodeSequences_body( unsafe fn ZSTD_encodeSequences_default( dst: *mut core::ffi::c_void, dstCapacity: size_t, - CTable_MatchLength: *const FSE_CTable, + CTable_MatchLength: &[FSE_CTable], mlCodeTable: *const u8, - CTable_OffsetBits: *const FSE_CTable, + CTable_OffsetBits: &[FSE_CTable], ofCodeTable: *const u8, - CTable_LitLength: *const FSE_CTable, + CTable_LitLength: &[FSE_CTable], llCodeTable: *const u8, sequences: *const SeqDef, nbSeq: size_t, @@ -580,11 +577,11 @@ unsafe fn ZSTD_encodeSequences_default( unsafe fn ZSTD_encodeSequences_bmi2( dst: *mut core::ffi::c_void, dstCapacity: size_t, - CTable_MatchLength: *const FSE_CTable, + CTable_MatchLength: &[FSE_CTable], mlCodeTable: *const u8, - CTable_OffsetBits: *const FSE_CTable, + CTable_OffsetBits: &[FSE_CTable], ofCodeTable: *const u8, - CTable_LitLength: *const FSE_CTable, + CTable_LitLength: &[FSE_CTable], llCodeTable: *const u8, sequences: *const SeqDef, nbSeq: size_t, @@ -608,11 +605,11 @@ unsafe fn ZSTD_encodeSequences_bmi2( pub unsafe fn ZSTD_encodeSequences( dst: *mut core::ffi::c_void, dstCapacity: size_t, - CTable_MatchLength: *const FSE_CTable, + CTable_MatchLength: &[FSE_CTable], mlCodeTable: *const u8, - CTable_OffsetBits: *const FSE_CTable, + CTable_OffsetBits: &[FSE_CTable], ofCodeTable: *const u8, - CTable_LitLength: *const FSE_CTable, + CTable_LitLength: &[FSE_CTable], llCodeTable: *const u8, sequences: *const SeqDef, nbSeq: size_t, diff --git a/lib/compress/zstd_compress_superblock.rs b/lib/compress/zstd_compress_superblock.rs index 0629f378..7ff3db11 100644 --- a/lib/compress/zstd_compress_superblock.rs +++ b/lib/compress/zstd_compress_superblock.rs @@ -539,11 +539,11 @@ unsafe fn ZSTD_compressSubBlock_sequences( let bitstreamSize = ZSTD_encodeSequences( op as *mut core::ffi::c_void, oend.offset_from_unsigned(op), - (fseTables.matchlengthCTable).as_ptr(), + &fseTables.matchlengthCTable, mlCode, - (fseTables.offcodeCTable).as_ptr(), + &fseTables.offcodeCTable, ofCode, - (fseTables.litlengthCTable).as_ptr(), + &fseTables.litlengthCTable, llCode, sequences, nbSeq, @@ -711,7 +711,7 @@ unsafe fn ZSTD_estimateSubBlockSize_symbolType( codeTable: *const u8, maxCode: core::ffi::c_uint, nbSeq: size_t, - fseCTable: *const FSE_CTable, + fseCTable: &[FSE_CTable], additionalBits: *const u8, defaultNorm: *const core::ffi::c_short, defaultNormLog: u32, @@ -784,7 +784,7 @@ unsafe fn ZSTD_estimateSubBlockSize_sequences( ofCodeTable, MaxOff, nbSeq, - (fseTables.offcodeCTable).as_ptr(), + &fseTables.offcodeCTable, core::ptr::null(), OF_defaultNorm.as_ptr(), OF_defaultNormLog, @@ -797,7 +797,7 @@ unsafe fn ZSTD_estimateSubBlockSize_sequences( llCodeTable, MaxLL, nbSeq, - (fseTables.litlengthCTable).as_ptr(), + &fseTables.litlengthCTable, LL_bits.as_ptr(), LL_defaultNorm.as_ptr(), LL_defaultNormLog, @@ -810,7 +810,7 @@ unsafe fn ZSTD_estimateSubBlockSize_sequences( mlCodeTable, MaxML, nbSeq, - (fseTables.matchlengthCTable).as_ptr(), + &fseTables.matchlengthCTable, ML_bits.as_ptr(), ML_defaultNorm.as_ptr(), ML_defaultNormLog, diff --git a/lib/compress/zstd_opt.rs b/lib/compress/zstd_opt.rs index e3611671..a482b1cc 100644 --- a/lib/compress/zstd_opt.rs +++ b/lib/compress/zstd_opt.rs @@ -275,10 +275,7 @@ unsafe fn ZSTD_rescaleFreqs( symbolTT: core::ptr::null::(), stateLog: 0, }; - FSE_initCState( - &mut llstate, - ((*(*optPtr).symbolCosts).fse.litlengthCTable).as_ptr(), - ); + FSE_initCState(&mut llstate, &(*(*optPtr).symbolCosts).fse.litlengthCTable); (*optPtr).litLengthSum = 0; for ll in 0..MaxLL + 1 { let scaleLog_0 = 10u32; // scale to 1K @@ -301,7 +298,7 @@ unsafe fn ZSTD_rescaleFreqs( }; FSE_initCState( &mut mlstate, - ((*(*optPtr).symbolCosts).fse.matchlengthCTable).as_ptr(), + &(*(*optPtr).symbolCosts).fse.matchlengthCTable, ); (*optPtr).matchLengthSum = 0; for ml in 0..MaxML + 1 { @@ -323,10 +320,7 @@ unsafe fn ZSTD_rescaleFreqs( symbolTT: core::ptr::null::(), stateLog: 0, }; - FSE_initCState( - &mut ofstate, - ((*(*optPtr).symbolCosts).fse.offcodeCTable).as_ptr(), - ); + FSE_initCState(&mut ofstate, &(*(*optPtr).symbolCosts).fse.offcodeCTable); (*optPtr).offCodeSum = 0; for of in 0..MaxOff + 1 { let scaleLog_2 = 10u32;