Skip to main content

libzstd_rs_sys/lib/compress/
hist.rs

1pub type HIST_checkInput_e = core::ffi::c_uint;
2pub const checkMaxSymbolValue: HIST_checkInput_e = 1;
3pub const trustInput: HIST_checkInput_e = 0;
4use core::ptr;
5
6use libc::size_t;
7
8use crate::lib::common::error_private::{ERR_isError, Error};
9use crate::lib::common::mem::MEM_read32;
10pub const HIST_WKSP_SIZE_U32: core::ffi::c_int = 1024;
11pub const HIST_WKSP_SIZE: size_t =
12    (HIST_WKSP_SIZE_U32 as size_t).wrapping_mul(::core::mem::size_of::<core::ffi::c_uint>());
13pub const HIST_FAST_THRESHOLD: core::ffi::c_int = 1500;
14pub unsafe fn HIST_isError(code: size_t) -> core::ffi::c_uint {
15    ERR_isError(code) as _
16}
17pub unsafe fn HIST_add(
18    count: *mut core::ffi::c_uint,
19    src: *const core::ffi::c_void,
20    srcSize: size_t,
21) {
22    let mut ip = src as *const u8;
23    let end = ip.add(srcSize);
24    while ip < end {
25        let fresh0 = ip;
26        ip = ip.offset(1);
27        let fresh1 = &mut (*count.offset(*fresh0 as isize));
28        *fresh1 = (*fresh1).wrapping_add(1);
29    }
30}
31pub unsafe fn HIST_count_simple(
32    count: *mut core::ffi::c_uint,
33    maxSymbolValuePtr: *mut core::ffi::c_uint,
34    src: *const core::ffi::c_void,
35    srcSize: size_t,
36) -> core::ffi::c_uint {
37    let mut ip = src as *const u8;
38    let end = ip.add(srcSize);
39    let mut maxSymbolValue = *maxSymbolValuePtr;
40    let mut largestCount = 0;
41    ptr::write_bytes(
42        count as *mut u8,
43        0,
44        (maxSymbolValue.wrapping_add(1) as core::ffi::c_ulong)
45            .wrapping_mul(::core::mem::size_of::<core::ffi::c_uint>() as core::ffi::c_ulong)
46            as libc::size_t,
47    );
48    if srcSize == 0 {
49        *maxSymbolValuePtr = 0;
50        return 0;
51    }
52    while ip < end {
53        let fresh2 = ip;
54        ip = ip.offset(1);
55        let fresh3 = &mut (*count.offset(*fresh2 as isize));
56        *fresh3 = (*fresh3).wrapping_add(1);
57    }
58    while *count.offset(maxSymbolValue as isize) == 0 {
59        maxSymbolValue = maxSymbolValue.wrapping_sub(1);
60    }
61    *maxSymbolValuePtr = maxSymbolValue;
62    let mut s: u32 = 0;
63    s = 0;
64    while s <= maxSymbolValue {
65        if *count.offset(s as isize) > largestCount {
66            largestCount = *count.offset(s as isize);
67        }
68        s = s.wrapping_add(1);
69    }
70    largestCount
71}
72unsafe fn HIST_count_parallel_wksp(
73    count: *mut core::ffi::c_uint,
74    maxSymbolValuePtr: *mut core::ffi::c_uint,
75    source: *const core::ffi::c_void,
76    sourceSize: size_t,
77    check: HIST_checkInput_e,
78    workSpace: *mut u32,
79) -> size_t {
80    let mut ip = source as *const u8;
81    let iend = ip.add(sourceSize);
82    let countSize = ((*maxSymbolValuePtr).wrapping_add(1) as core::ffi::c_ulong)
83        .wrapping_mul(::core::mem::size_of::<core::ffi::c_uint>() as core::ffi::c_ulong);
84    let mut max = 0;
85    let Counting1 = workSpace;
86    let Counting2 = Counting1.offset(256);
87    let Counting3 = Counting2.offset(256);
88    let Counting4 = Counting3.offset(256);
89    if sourceSize == 0 {
90        ptr::write_bytes(count as *mut u8, 0, countSize as libc::size_t);
91        *maxSymbolValuePtr = 0;
92        return 0;
93    }
94    ptr::write_bytes(
95        workSpace as *mut u8,
96        0,
97        ((4 * 256) as core::ffi::c_ulong)
98            .wrapping_mul(::core::mem::size_of::<core::ffi::c_uint>() as core::ffi::c_ulong)
99            as libc::size_t,
100    );
101    let mut cached = MEM_read32(ip as *const core::ffi::c_void);
102    ip = ip.offset(4);
103    while ip < iend.offset(-(15)) {
104        let mut c = cached;
105        cached = MEM_read32(ip as *const core::ffi::c_void);
106        ip = ip.offset(4);
107        let fresh4 = &mut (*Counting1.offset(c as u8 as isize));
108        *fresh4 = (*fresh4).wrapping_add(1);
109        let fresh5 = &mut (*Counting2.offset((c >> 8) as u8 as isize));
110        *fresh5 = (*fresh5).wrapping_add(1);
111        let fresh6 = &mut (*Counting3.offset((c >> 16) as u8 as isize));
112        *fresh6 = (*fresh6).wrapping_add(1);
113        let fresh7 = &mut (*Counting4.offset((c >> 24) as isize));
114        *fresh7 = (*fresh7).wrapping_add(1);
115        c = cached;
116        cached = MEM_read32(ip as *const core::ffi::c_void);
117        ip = ip.offset(4);
118        let fresh8 = &mut (*Counting1.offset(c as u8 as isize));
119        *fresh8 = (*fresh8).wrapping_add(1);
120        let fresh9 = &mut (*Counting2.offset((c >> 8) as u8 as isize));
121        *fresh9 = (*fresh9).wrapping_add(1);
122        let fresh10 = &mut (*Counting3.offset((c >> 16) as u8 as isize));
123        *fresh10 = (*fresh10).wrapping_add(1);
124        let fresh11 = &mut (*Counting4.offset((c >> 24) as isize));
125        *fresh11 = (*fresh11).wrapping_add(1);
126        c = cached;
127        cached = MEM_read32(ip as *const core::ffi::c_void);
128        ip = ip.offset(4);
129        let fresh12 = &mut (*Counting1.offset(c as u8 as isize));
130        *fresh12 = (*fresh12).wrapping_add(1);
131        let fresh13 = &mut (*Counting2.offset((c >> 8) as u8 as isize));
132        *fresh13 = (*fresh13).wrapping_add(1);
133        let fresh14 = &mut (*Counting3.offset((c >> 16) as u8 as isize));
134        *fresh14 = (*fresh14).wrapping_add(1);
135        let fresh15 = &mut (*Counting4.offset((c >> 24) as isize));
136        *fresh15 = (*fresh15).wrapping_add(1);
137        c = cached;
138        cached = MEM_read32(ip as *const core::ffi::c_void);
139        ip = ip.offset(4);
140        let fresh16 = &mut (*Counting1.offset(c as u8 as isize));
141        *fresh16 = (*fresh16).wrapping_add(1);
142        let fresh17 = &mut (*Counting2.offset((c >> 8) as u8 as isize));
143        *fresh17 = (*fresh17).wrapping_add(1);
144        let fresh18 = &mut (*Counting3.offset((c >> 16) as u8 as isize));
145        *fresh18 = (*fresh18).wrapping_add(1);
146        let fresh19 = &mut (*Counting4.offset((c >> 24) as isize));
147        *fresh19 = (*fresh19).wrapping_add(1);
148    }
149    ip = ip.offset(-(4));
150    while ip < iend {
151        let fresh20 = ip;
152        ip = ip.offset(1);
153        let fresh21 = &mut (*Counting1.offset(*fresh20 as isize));
154        *fresh21 = (*fresh21).wrapping_add(1);
155    }
156    let mut s: u32 = 0;
157    s = 0;
158    while s < 256 {
159        let fresh22 = &mut (*Counting1.offset(s as isize));
160        *fresh22 = (*fresh22).wrapping_add(
161            (*Counting2.offset(s as isize))
162                .wrapping_add(*Counting3.offset(s as isize))
163                .wrapping_add(*Counting4.offset(s as isize)),
164        );
165        if *Counting1.offset(s as isize) > max {
166            max = *Counting1.offset(s as isize);
167        }
168        s = s.wrapping_add(1);
169    }
170    let mut maxSymbolValue = 255 as core::ffi::c_uint;
171    while *Counting1.offset(maxSymbolValue as isize) == 0 {
172        maxSymbolValue = maxSymbolValue.wrapping_sub(1);
173    }
174    if check as core::ffi::c_uint != 0 && maxSymbolValue > *maxSymbolValuePtr {
175        return Error::maxSymbolValue_tooSmall.to_error_code();
176    }
177    *maxSymbolValuePtr = maxSymbolValue;
178    libc::memmove(
179        count as *mut core::ffi::c_void,
180        Counting1 as *const core::ffi::c_void,
181        countSize as libc::size_t,
182    );
183    max as size_t
184}
185pub unsafe fn HIST_countFast_wksp(
186    count: *mut core::ffi::c_uint,
187    maxSymbolValuePtr: *mut core::ffi::c_uint,
188    source: *const core::ffi::c_void,
189    sourceSize: size_t,
190    workSpace: *mut core::ffi::c_void,
191    workSpaceSize: size_t,
192) -> size_t {
193    if sourceSize < HIST_FAST_THRESHOLD as size_t {
194        return HIST_count_simple(count, maxSymbolValuePtr, source, sourceSize) as size_t;
195    }
196    if workSpace as size_t & 3 != 0 {
197        return Error::GENERIC.to_error_code();
198    }
199    if workSpaceSize < HIST_WKSP_SIZE {
200        return Error::workSpace_tooSmall.to_error_code();
201    }
202    HIST_count_parallel_wksp(
203        count,
204        maxSymbolValuePtr,
205        source,
206        sourceSize,
207        trustInput,
208        workSpace as *mut u32,
209    )
210}
211pub unsafe fn HIST_count_wksp(
212    count: *mut core::ffi::c_uint,
213    maxSymbolValuePtr: *mut core::ffi::c_uint,
214    source: *const core::ffi::c_void,
215    sourceSize: size_t,
216    workSpace: *mut core::ffi::c_void,
217    workSpaceSize: size_t,
218) -> size_t {
219    if workSpace as size_t & 3 != 0 {
220        return Error::GENERIC.to_error_code();
221    }
222    if workSpaceSize < HIST_WKSP_SIZE {
223        return Error::workSpace_tooSmall.to_error_code();
224    }
225    if *maxSymbolValuePtr < 255 {
226        return HIST_count_parallel_wksp(
227            count,
228            maxSymbolValuePtr,
229            source,
230            sourceSize,
231            checkMaxSymbolValue,
232            workSpace as *mut u32,
233        );
234    }
235    *maxSymbolValuePtr = 255;
236    HIST_countFast_wksp(
237        count,
238        maxSymbolValuePtr,
239        source,
240        sourceSize,
241        workSpace,
242        workSpaceSize,
243    )
244}
245pub unsafe fn HIST_countFast(
246    count: *mut core::ffi::c_uint,
247    maxSymbolValuePtr: *mut core::ffi::c_uint,
248    source: *const core::ffi::c_void,
249    sourceSize: size_t,
250) -> size_t {
251    let mut tmpCounters: [core::ffi::c_uint; 1024] = [0; 1024];
252    HIST_countFast_wksp(
253        count,
254        maxSymbolValuePtr,
255        source,
256        sourceSize,
257        tmpCounters.as_mut_ptr() as *mut core::ffi::c_void,
258        ::core::mem::size_of::<[core::ffi::c_uint; 1024]>(),
259    )
260}
261pub unsafe fn HIST_count(
262    count: *mut core::ffi::c_uint,
263    maxSymbolValuePtr: *mut core::ffi::c_uint,
264    src: *const core::ffi::c_void,
265    srcSize: size_t,
266) -> size_t {
267    let mut tmpCounters: [core::ffi::c_uint; 1024] = [0; 1024];
268    HIST_count_wksp(
269        count,
270        maxSymbolValuePtr,
271        src,
272        srcSize,
273        tmpCounters.as_mut_ptr() as *mut core::ffi::c_void,
274        ::core::mem::size_of::<[core::ffi::c_uint; 1024]>(),
275    )
276}