Coverage Report

Created: 2026-02-26 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zstd-rs/zstd-safe/zstd-sys/zstd/lib/compress/hist.c
Line
Count
Source
1
/* ******************************************************************
2
 * hist : Histogram functions
3
 * part of Finite State Entropy project
4
 * Copyright (c) Meta Platforms, Inc. and affiliates.
5
 *
6
 *  You can contact the author at :
7
 *  - FSE source repository : https://github.com/Cyan4973/FiniteStateEntropy
8
 *  - Public forum : https://groups.google.com/forum/#!forum/lz4c
9
 *
10
 * This source code is licensed under both the BSD-style license (found in the
11
 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
12
 * in the COPYING file in the root directory of this source tree).
13
 * You may select, at your option, one of the above-listed licenses.
14
****************************************************************** */
15
16
/* --- dependencies --- */
17
#include "../common/mem.h"             /* U32, BYTE, etc. */
18
#include "../common/debug.h"           /* assert, DEBUGLOG */
19
#include "../common/error_private.h"   /* ERROR */
20
#include "hist.h"
21
22
23
/* --- Error management --- */
24
0
unsigned HIST_isError(size_t code) { return ERR_isError(code); }
25
26
/*-**************************************************************
27
 *  Histogram functions
28
 ****************************************************************/
29
void HIST_add(unsigned* count, const void* src, size_t srcSize)
30
13.2k
{
31
13.2k
    const BYTE* ip = (const BYTE*)src;
32
13.2k
    const BYTE* const end = ip + srcSize;
33
34
6.82M
    while (ip<end) {
35
6.80M
        count[*ip++]++;
36
6.80M
    }
37
13.2k
}
38
39
unsigned HIST_count_simple(unsigned* count, unsigned* maxSymbolValuePtr,
40
                           const void* src, size_t srcSize)
41
2.82M
{
42
2.82M
    const BYTE* ip = (const BYTE*)src;
43
2.82M
    const BYTE* const end = ip + srcSize;
44
2.82M
    unsigned maxSymbolValue = *maxSymbolValuePtr;
45
2.82M
    unsigned largestCount=0;
46
47
2.82M
    ZSTD_memset(count, 0, (maxSymbolValue+1) * sizeof(*count));
48
2.82M
    if (srcSize==0) { *maxSymbolValuePtr = 0; return 0; }
49
50
2.99G
    while (ip<end) {
51
2.99G
        assert(*ip <= maxSymbolValue);
52
2.99G
        count[*ip++]++;
53
2.99G
    }
54
55
62.3M
    while (!count[maxSymbolValue]) maxSymbolValue--;
56
2.82M
    *maxSymbolValuePtr = maxSymbolValue;
57
58
2.82M
    {   U32 s;
59
129M
        for (s=0; s<=maxSymbolValue; s++)
60
126M
            if (count[s] > largestCount) largestCount = count[s];
61
2.82M
    }
62
63
2.82M
    return largestCount;
64
2.82M
}
65
66
typedef enum { trustInput, checkMaxSymbolValue } HIST_checkInput_e;
67
68
/* HIST_count_parallel_wksp() :
69
 * store histogram into 4 intermediate tables, recombined at the end.
70
 * this design makes better use of OoO cpus,
71
 * and is noticeably faster when some values are heavily repeated.
72
 * But it needs some additional workspace for intermediate tables.
73
 * `workSpace` must be a U32 table of size >= HIST_WKSP_SIZE_U32.
74
 * @return : largest histogram frequency,
75
 *           or an error code (notably when histogram's alphabet is larger than *maxSymbolValuePtr) */
76
static size_t HIST_count_parallel_wksp(
77
                                unsigned* count, unsigned* maxSymbolValuePtr,
78
                                const void* source, size_t sourceSize,
79
                                HIST_checkInput_e check,
80
                                U32* const workSpace)
81
222k
{
82
222k
    const BYTE* ip = (const BYTE*)source;
83
222k
    const BYTE* const iend = ip+sourceSize;
84
222k
    size_t const countSize = (*maxSymbolValuePtr + 1) * sizeof(*count);
85
222k
    unsigned max=0;
86
222k
    U32* const Counting1 = workSpace;
87
222k
    U32* const Counting2 = Counting1 + 256;
88
222k
    U32* const Counting3 = Counting2 + 256;
89
222k
    U32* const Counting4 = Counting3 + 256;
90
91
    /* safety checks */
92
222k
    assert(*maxSymbolValuePtr <= 255);
93
222k
    if (!sourceSize) {
94
0
        ZSTD_memset(count, 0, countSize);
95
0
        *maxSymbolValuePtr = 0;
96
0
        return 0;
97
0
    }
98
222k
    ZSTD_memset(workSpace, 0, 4*256*sizeof(unsigned));
99
100
    /* by stripes of 16 bytes */
101
222k
    {   U32 cached = MEM_read32(ip); ip += 4;
102
109M
        while (ip < iend-15) {
103
109M
            U32 c = cached; cached = MEM_read32(ip); ip += 4;
104
109M
            Counting1[(BYTE) c     ]++;
105
109M
            Counting2[(BYTE)(c>>8) ]++;
106
109M
            Counting3[(BYTE)(c>>16)]++;
107
109M
            Counting4[       c>>24 ]++;
108
109M
            c = cached; cached = MEM_read32(ip); ip += 4;
109
109M
            Counting1[(BYTE) c     ]++;
110
109M
            Counting2[(BYTE)(c>>8) ]++;
111
109M
            Counting3[(BYTE)(c>>16)]++;
112
109M
            Counting4[       c>>24 ]++;
113
109M
            c = cached; cached = MEM_read32(ip); ip += 4;
114
109M
            Counting1[(BYTE) c     ]++;
115
109M
            Counting2[(BYTE)(c>>8) ]++;
116
109M
            Counting3[(BYTE)(c>>16)]++;
117
109M
            Counting4[       c>>24 ]++;
118
109M
            c = cached; cached = MEM_read32(ip); ip += 4;
119
109M
            Counting1[(BYTE) c     ]++;
120
109M
            Counting2[(BYTE)(c>>8) ]++;
121
109M
            Counting3[(BYTE)(c>>16)]++;
122
109M
            Counting4[       c>>24 ]++;
123
109M
        }
124
222k
        ip-=4;
125
222k
    }
126
127
    /* finish last symbols */
128
2.76M
    while (ip<iend) Counting1[*ip++]++;
129
130
222k
    {   U32 s;
131
57.1M
        for (s=0; s<256; s++) {
132
56.9M
            Counting1[s] += Counting2[s] + Counting3[s] + Counting4[s];
133
56.9M
            if (Counting1[s] > max) max = Counting1[s];
134
56.9M
    }   }
135
136
222k
    {   unsigned maxSymbolValue = 255;
137
24.6M
        while (!Counting1[maxSymbolValue]) maxSymbolValue--;
138
222k
        if (check && maxSymbolValue > *maxSymbolValuePtr) return ERROR(maxSymbolValue_tooSmall);
139
222k
        *maxSymbolValuePtr = maxSymbolValue;
140
222k
        ZSTD_memmove(count, Counting1, countSize);   /* in case count & Counting1 are overlapping */
141
222k
    }
142
0
    return (size_t)max;
143
222k
}
144
145
/* HIST_countFast_wksp() :
146
 * Same as HIST_countFast(), but using an externally provided scratch buffer.
147
 * `workSpace` is a writable buffer which must be 4-bytes aligned,
148
 * `workSpaceSize` must be >= HIST_WKSP_SIZE
149
 */
150
size_t HIST_countFast_wksp(unsigned* count, unsigned* maxSymbolValuePtr,
151
                          const void* source, size_t sourceSize,
152
                          void* workSpace, size_t workSpaceSize)
153
2.29M
{
154
2.29M
    if (sourceSize < 1500) /* heuristic threshold */
155
2.07M
        return HIST_count_simple(count, maxSymbolValuePtr, source, sourceSize);
156
222k
    if ((size_t)workSpace & 3) return ERROR(GENERIC);  /* must be aligned on 4-bytes boundaries */
157
222k
    if (workSpaceSize < HIST_WKSP_SIZE) return ERROR(workSpace_tooSmall);
158
222k
    return HIST_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, trustInput, (U32*)workSpace);
159
222k
}
160
161
/* HIST_count_wksp() :
162
 * Same as HIST_count(), but using an externally provided scratch buffer.
163
 * `workSpace` size must be table of >= HIST_WKSP_SIZE_U32 unsigned */
164
size_t HIST_count_wksp(unsigned* count, unsigned* maxSymbolValuePtr,
165
                       const void* source, size_t sourceSize,
166
                       void* workSpace, size_t workSpaceSize)
167
410k
{
168
410k
    if ((size_t)workSpace & 3) return ERROR(GENERIC);  /* must be aligned on 4-bytes boundaries */
169
410k
    if (workSpaceSize < HIST_WKSP_SIZE) return ERROR(workSpace_tooSmall);
170
410k
    if (*maxSymbolValuePtr < 255)
171
0
        return HIST_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, checkMaxSymbolValue, (U32*)workSpace);
172
410k
    *maxSymbolValuePtr = 255;
173
410k
    return HIST_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, workSpace, workSpaceSize);
174
410k
}
175
176
#ifndef ZSTD_NO_UNUSED_FUNCTIONS
177
/* fast variant (unsafe : won't check if src contains values beyond count[] limit) */
178
size_t HIST_countFast(unsigned* count, unsigned* maxSymbolValuePtr,
179
                     const void* source, size_t sourceSize)
180
0
{
181
0
    unsigned tmpCounters[HIST_WKSP_SIZE_U32];
182
0
    return HIST_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, tmpCounters, sizeof(tmpCounters));
183
0
}
184
185
size_t HIST_count(unsigned* count, unsigned* maxSymbolValuePtr,
186
                 const void* src, size_t srcSize)
187
0
{
188
0
    unsigned tmpCounters[HIST_WKSP_SIZE_U32];
189
0
    return HIST_count_wksp(count, maxSymbolValuePtr, src, srcSize, tmpCounters, sizeof(tmpCounters));
190
0
}
191
#endif