Coverage Report

Created: 2024-09-08 06:28

/src/zstd/lib/compress/hist.c
Line
Count
Source (jump to first uncovered line)
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
unsigned HIST_count_simple(unsigned* count, unsigned* maxSymbolValuePtr,
30
                           const void* src, size_t srcSize)
31
3.48M
{
32
3.48M
    const BYTE* ip = (const BYTE*)src;
33
3.48M
    const BYTE* const end = ip + srcSize;
34
3.48M
    unsigned maxSymbolValue = *maxSymbolValuePtr;
35
3.48M
    unsigned largestCount=0;
36
37
3.48M
    ZSTD_memset(count, 0, (maxSymbolValue+1) * sizeof(*count));
38
3.48M
    if (srcSize==0) { *maxSymbolValuePtr = 0; return 0; }
39
40
458M
    while (ip<end) {
41
455M
        assert(*ip <= maxSymbolValue);
42
455M
        count[*ip++]++;
43
455M
    }
44
45
72.4M
    while (!count[maxSymbolValue]) maxSymbolValue--;
46
3.48M
    *maxSymbolValuePtr = maxSymbolValue;
47
48
3.48M
    {   U32 s;
49
154M
        for (s=0; s<=maxSymbolValue; s++)
50
150M
            if (count[s] > largestCount) largestCount = count[s];
51
3.48M
    }
52
53
3.48M
    return largestCount;
54
3.48M
}
55
56
typedef enum { trustInput, checkMaxSymbolValue } HIST_checkInput_e;
57
58
/* HIST_count_parallel_wksp() :
59
 * store histogram into 4 intermediate tables, recombined at the end.
60
 * this design makes better use of OoO cpus,
61
 * and is noticeably faster when some values are heavily repeated.
62
 * But it needs some additional workspace for intermediate tables.
63
 * `workSpace` must be a U32 table of size >= HIST_WKSP_SIZE_U32.
64
 * @return : largest histogram frequency,
65
 *           or an error code (notably when histogram's alphabet is larger than *maxSymbolValuePtr) */
66
static size_t HIST_count_parallel_wksp(
67
                                unsigned* count, unsigned* maxSymbolValuePtr,
68
                                const void* source, size_t sourceSize,
69
                                HIST_checkInput_e check,
70
                                U32* const workSpace)
71
40.9k
{
72
40.9k
    const BYTE* ip = (const BYTE*)source;
73
40.9k
    const BYTE* const iend = ip+sourceSize;
74
40.9k
    size_t const countSize = (*maxSymbolValuePtr + 1) * sizeof(*count);
75
40.9k
    unsigned max=0;
76
40.9k
    U32* const Counting1 = workSpace;
77
40.9k
    U32* const Counting2 = Counting1 + 256;
78
40.9k
    U32* const Counting3 = Counting2 + 256;
79
40.9k
    U32* const Counting4 = Counting3 + 256;
80
81
    /* safety checks */
82
40.9k
    assert(*maxSymbolValuePtr <= 255);
83
40.9k
    if (!sourceSize) {
84
0
        ZSTD_memset(count, 0, countSize);
85
0
        *maxSymbolValuePtr = 0;
86
0
        return 0;
87
0
    }
88
40.9k
    ZSTD_memset(workSpace, 0, 4*256*sizeof(unsigned));
89
90
    /* by stripes of 16 bytes */
91
40.9k
    {   U32 cached = MEM_read32(ip); ip += 4;
92
10.9M
        while (ip < iend-15) {
93
10.8M
            U32 c = cached; cached = MEM_read32(ip); ip += 4;
94
10.8M
            Counting1[(BYTE) c     ]++;
95
10.8M
            Counting2[(BYTE)(c>>8) ]++;
96
10.8M
            Counting3[(BYTE)(c>>16)]++;
97
10.8M
            Counting4[       c>>24 ]++;
98
10.8M
            c = cached; cached = MEM_read32(ip); ip += 4;
99
10.8M
            Counting1[(BYTE) c     ]++;
100
10.8M
            Counting2[(BYTE)(c>>8) ]++;
101
10.8M
            Counting3[(BYTE)(c>>16)]++;
102
10.8M
            Counting4[       c>>24 ]++;
103
10.8M
            c = cached; cached = MEM_read32(ip); ip += 4;
104
10.8M
            Counting1[(BYTE) c     ]++;
105
10.8M
            Counting2[(BYTE)(c>>8) ]++;
106
10.8M
            Counting3[(BYTE)(c>>16)]++;
107
10.8M
            Counting4[       c>>24 ]++;
108
10.8M
            c = cached; cached = MEM_read32(ip); ip += 4;
109
10.8M
            Counting1[(BYTE) c     ]++;
110
10.8M
            Counting2[(BYTE)(c>>8) ]++;
111
10.8M
            Counting3[(BYTE)(c>>16)]++;
112
10.8M
            Counting4[       c>>24 ]++;
113
10.8M
        }
114
40.9k
        ip-=4;
115
40.9k
    }
116
117
    /* finish last symbols */
118
520k
    while (ip<iend) Counting1[*ip++]++;
119
120
40.9k
    {   U32 s;
121
10.5M
        for (s=0; s<256; s++) {
122
10.4M
            Counting1[s] += Counting2[s] + Counting3[s] + Counting4[s];
123
10.4M
            if (Counting1[s] > max) max = Counting1[s];
124
10.4M
    }   }
125
126
40.9k
    {   unsigned maxSymbolValue = 255;
127
2.70M
        while (!Counting1[maxSymbolValue]) maxSymbolValue--;
128
40.9k
        if (check && maxSymbolValue > *maxSymbolValuePtr) return ERROR(maxSymbolValue_tooSmall);
129
40.9k
        *maxSymbolValuePtr = maxSymbolValue;
130
40.9k
        ZSTD_memmove(count, Counting1, countSize);   /* in case count & Counting1 are overlapping */
131
40.9k
    }
132
0
    return (size_t)max;
133
40.9k
}
134
135
/* HIST_countFast_wksp() :
136
 * Same as HIST_countFast(), but using an externally provided scratch buffer.
137
 * `workSpace` is a writable buffer which must be 4-bytes aligned,
138
 * `workSpaceSize` must be >= HIST_WKSP_SIZE
139
 */
140
size_t HIST_countFast_wksp(unsigned* count, unsigned* maxSymbolValuePtr,
141
                          const void* source, size_t sourceSize,
142
                          void* workSpace, size_t workSpaceSize)
143
3.11M
{
144
3.11M
    if (sourceSize < 1500) /* heuristic threshold */
145
3.07M
        return HIST_count_simple(count, maxSymbolValuePtr, source, sourceSize);
146
40.9k
    if ((size_t)workSpace & 3) return ERROR(GENERIC);  /* must be aligned on 4-bytes boundaries */
147
40.9k
    if (workSpaceSize < HIST_WKSP_SIZE) return ERROR(workSpace_tooSmall);
148
40.9k
    return HIST_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, trustInput, (U32*)workSpace);
149
40.9k
}
150
151
/* HIST_count_wksp() :
152
 * Same as HIST_count(), but using an externally provided scratch buffer.
153
 * `workSpace` size must be table of >= HIST_WKSP_SIZE_U32 unsigned */
154
size_t HIST_count_wksp(unsigned* count, unsigned* maxSymbolValuePtr,
155
                       const void* source, size_t sourceSize,
156
                       void* workSpace, size_t workSpaceSize)
157
428k
{
158
428k
    if ((size_t)workSpace & 3) return ERROR(GENERIC);  /* must be aligned on 4-bytes boundaries */
159
428k
    if (workSpaceSize < HIST_WKSP_SIZE) return ERROR(workSpace_tooSmall);
160
428k
    if (*maxSymbolValuePtr < 255)
161
0
        return HIST_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, checkMaxSymbolValue, (U32*)workSpace);
162
428k
    *maxSymbolValuePtr = 255;
163
428k
    return HIST_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, workSpace, workSpaceSize);
164
428k
}
165
166
#ifndef ZSTD_NO_UNUSED_FUNCTIONS
167
/* fast variant (unsafe : won't check if src contains values beyond count[] limit) */
168
size_t HIST_countFast(unsigned* count, unsigned* maxSymbolValuePtr,
169
                     const void* source, size_t sourceSize)
170
0
{
171
0
    unsigned tmpCounters[HIST_WKSP_SIZE_U32];
172
0
    return HIST_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, tmpCounters, sizeof(tmpCounters));
173
0
}
174
175
size_t HIST_count(unsigned* count, unsigned* maxSymbolValuePtr,
176
                 const void* src, size_t srcSize)
177
0
{
178
0
    unsigned tmpCounters[HIST_WKSP_SIZE_U32];
179
0
    return HIST_count_wksp(count, maxSymbolValuePtr, src, srcSize, tmpCounters, sizeof(tmpCounters));
180
0
}
181
#endif