Coverage Report

Created: 2025-11-02 07:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/c-blosc2/internal-complibs/zstd-1.5.7/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
0
{
31
0
    const BYTE* ip = (const BYTE*)src;
32
0
    const BYTE* const end = ip + srcSize;
33
34
0
    while (ip<end) {
35
0
        count[*ip++]++;
36
0
    }
37
0
}
38
39
unsigned HIST_count_simple(unsigned* count, unsigned* maxSymbolValuePtr,
40
                           const void* src, size_t srcSize)
41
586k
{
42
586k
    const BYTE* ip = (const BYTE*)src;
43
586k
    const BYTE* const end = ip + srcSize;
44
586k
    unsigned maxSymbolValue = *maxSymbolValuePtr;
45
586k
    unsigned largestCount=0;
46
47
586k
    ZSTD_memset(count, 0, (maxSymbolValue+1) * sizeof(*count));
48
586k
    if (srcSize==0) { *maxSymbolValuePtr = 0; return 0; }
49
50
283M
    while (ip<end) {
51
283M
        assert(*ip <= maxSymbolValue);
52
283M
        count[*ip++]++;
53
283M
    }
54
55
10.1M
    while (!count[maxSymbolValue]) maxSymbolValue--;
56
586k
    *maxSymbolValuePtr = maxSymbolValue;
57
58
586k
    {   U32 s;
59
31.6M
        for (s=0; s<=maxSymbolValue; s++)
60
31.0M
            if (count[s] > largestCount) largestCount = count[s];
61
586k
    }
62
63
586k
    return largestCount;
64
586k
}
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
48.9k
{
82
48.9k
    const BYTE* ip = (const BYTE*)source;
83
48.9k
    const BYTE* const iend = ip+sourceSize;
84
48.9k
    size_t const countSize = (*maxSymbolValuePtr + 1) * sizeof(*count);
85
48.9k
    unsigned max=0;
86
48.9k
    U32* const Counting1 = workSpace;
87
48.9k
    U32* const Counting2 = Counting1 + 256;
88
48.9k
    U32* const Counting3 = Counting2 + 256;
89
48.9k
    U32* const Counting4 = Counting3 + 256;
90
91
    /* safety checks */
92
48.9k
    assert(*maxSymbolValuePtr <= 255);
93
48.9k
    if (!sourceSize) {
94
0
        ZSTD_memset(count, 0, countSize);
95
0
        *maxSymbolValuePtr = 0;
96
0
        return 0;
97
0
    }
98
48.9k
    ZSTD_memset(workSpace, 0, 4*256*sizeof(unsigned));
99
100
    /* by stripes of 16 bytes */
101
48.9k
    {   U32 cached = MEM_read32(ip); ip += 4;
102
10.3M
        while (ip < iend-15) {
103
10.3M
            U32 c = cached; cached = MEM_read32(ip); ip += 4;
104
10.3M
            Counting1[(BYTE) c     ]++;
105
10.3M
            Counting2[(BYTE)(c>>8) ]++;
106
10.3M
            Counting3[(BYTE)(c>>16)]++;
107
10.3M
            Counting4[       c>>24 ]++;
108
10.3M
            c = cached; cached = MEM_read32(ip); ip += 4;
109
10.3M
            Counting1[(BYTE) c     ]++;
110
10.3M
            Counting2[(BYTE)(c>>8) ]++;
111
10.3M
            Counting3[(BYTE)(c>>16)]++;
112
10.3M
            Counting4[       c>>24 ]++;
113
10.3M
            c = cached; cached = MEM_read32(ip); ip += 4;
114
10.3M
            Counting1[(BYTE) c     ]++;
115
10.3M
            Counting2[(BYTE)(c>>8) ]++;
116
10.3M
            Counting3[(BYTE)(c>>16)]++;
117
10.3M
            Counting4[       c>>24 ]++;
118
10.3M
            c = cached; cached = MEM_read32(ip); ip += 4;
119
10.3M
            Counting1[(BYTE) c     ]++;
120
10.3M
            Counting2[(BYTE)(c>>8) ]++;
121
10.3M
            Counting3[(BYTE)(c>>16)]++;
122
10.3M
            Counting4[       c>>24 ]++;
123
10.3M
        }
124
48.9k
        ip-=4;
125
48.9k
    }
126
127
    /* finish last symbols */
128
612k
    while (ip<iend) Counting1[*ip++]++;
129
130
48.9k
    {   U32 s;
131
12.5M
        for (s=0; s<256; s++) {
132
12.5M
            Counting1[s] += Counting2[s] + Counting3[s] + Counting4[s];
133
12.5M
            if (Counting1[s] > max) max = Counting1[s];
134
12.5M
    }   }
135
136
48.9k
    {   unsigned maxSymbolValue = 255;
137
61.0k
        while (!Counting1[maxSymbolValue]) maxSymbolValue--;
138
48.9k
        if (check && maxSymbolValue > *maxSymbolValuePtr) return ERROR(maxSymbolValue_tooSmall);
139
48.9k
        *maxSymbolValuePtr = maxSymbolValue;
140
48.9k
        ZSTD_memmove(count, Counting1, countSize);   /* in case count & Counting1 are overlapping */
141
48.9k
    }
142
0
    return (size_t)max;
143
48.9k
}
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
431k
{
154
431k
    if (sourceSize < 1500) /* heuristic threshold */
155
382k
        return HIST_count_simple(count, maxSymbolValuePtr, source, sourceSize);
156
48.9k
    if ((size_t)workSpace & 3) return ERROR(GENERIC);  /* must be aligned on 4-bytes boundaries */
157
48.9k
    if (workSpaceSize < HIST_WKSP_SIZE) return ERROR(workSpace_tooSmall);
158
48.9k
    return HIST_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, trustInput, (U32*)workSpace);
159
48.9k
}
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
101k
{
168
101k
    if ((size_t)workSpace & 3) return ERROR(GENERIC);  /* must be aligned on 4-bytes boundaries */
169
101k
    if (workSpaceSize < HIST_WKSP_SIZE) return ERROR(workSpace_tooSmall);
170
101k
    if (*maxSymbolValuePtr < 255)
171
0
        return HIST_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, checkMaxSymbolValue, (U32*)workSpace);
172
101k
    *maxSymbolValuePtr = 255;
173
101k
    return HIST_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, workSpace, workSpaceSize);
174
101k
}
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