17#include "../common/mem.h"
18#include "../common/debug.h"
19#include "../common/error_private.h"
30 const void* src,
size_t srcSize)
33 const BYTE*
const end = ip + srcSize;
34 unsigned maxSymbolValue = *maxSymbolValuePtr;
35 unsigned largestCount=0;
37 ZSTD_memset(count, 0, (maxSymbolValue+1) *
sizeof(*count));
38 if (srcSize==0) { *maxSymbolValuePtr = 0;
return 0; }
41 assert(*ip <= maxSymbolValue);
45 while (!count[maxSymbolValue]) maxSymbolValue--;
46 *maxSymbolValuePtr = maxSymbolValue;
49 for (s=0; s<=maxSymbolValue; s++)
50 if (count[s] > largestCount) largestCount = count[s];
66static size_t HIST_count_parallel_wksp(
67 unsigned* count,
unsigned* maxSymbolValuePtr,
68 const void* source,
size_t sourceSize,
72 const BYTE* ip = (
const BYTE*)source;
73 const BYTE*
const iend = ip+sourceSize;
74 size_t const countSize = (*maxSymbolValuePtr + 1) *
sizeof(*count);
76 U32*
const Counting1 = workSpace;
77 U32*
const Counting2 = Counting1 + 256;
78 U32*
const Counting3 = Counting2 + 256;
79 U32*
const Counting4 = Counting3 + 256;
82 assert(*maxSymbolValuePtr <= 255);
85 *maxSymbolValuePtr = 0;
92 while (ip < iend-15) {
94 Counting1[(
BYTE) c ]++;
95 Counting2[(
BYTE)(c>>8) ]++;
96 Counting3[(
BYTE)(c>>16)]++;
99 Counting1[(
BYTE) c ]++;
100 Counting2[(
BYTE)(c>>8) ]++;
101 Counting3[(
BYTE)(c>>16)]++;
102 Counting4[ c>>24 ]++;
104 Counting1[(
BYTE) c ]++;
105 Counting2[(
BYTE)(c>>8) ]++;
106 Counting3[(
BYTE)(c>>16)]++;
107 Counting4[ c>>24 ]++;
109 Counting1[(
BYTE) c ]++;
110 Counting2[(
BYTE)(c>>8) ]++;
111 Counting3[(
BYTE)(c>>16)]++;
112 Counting4[ c>>24 ]++;
118 while (ip<iend) Counting1[*ip++]++;
121 for (s=0; s<256; s++) {
122 Counting1[s] += Counting2[s] + Counting3[s] + Counting4[s];
123 if (Counting1[s] > max)
max = Counting1[s];
126 {
unsigned maxSymbolValue = 255;
127 while (!Counting1[maxSymbolValue]) maxSymbolValue--;
128 if (check && maxSymbolValue > *maxSymbolValuePtr)
return ERROR(maxSymbolValue_tooSmall);
129 *maxSymbolValuePtr = maxSymbolValue;
141 const void* source,
size_t sourceSize,
142 void* workSpace,
size_t workSpaceSize)
144 if (sourceSize < 1500)
146 if ((
size_t)workSpace & 3)
return ERROR(GENERIC);
148 return HIST_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize,
trustInput, (
U32*)workSpace);
155 const void* source,
size_t sourceSize,
156 void* workSpace,
size_t workSpaceSize)
158 if ((
size_t)workSpace & 3)
return ERROR(GENERIC);
160 if (*maxSymbolValuePtr < 255)
161 return HIST_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize,
checkMaxSymbolValue, (
U32*)workSpace);
162 *maxSymbolValuePtr = 255;
163 return HIST_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, workSpace, workSpaceSize);
166#ifndef ZSTD_NO_UNUSED_FUNCTIONS
169 const void* source,
size_t sourceSize)
172 return HIST_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, tmpCounters,
sizeof(tmpCounters));
175size_t HIST_count(
unsigned* count,
unsigned* maxSymbolValuePtr,
176 const void* src,
size_t srcSize)
179 return HIST_count_wksp(count, maxSymbolValuePtr, src, srcSize, tmpCounters,
sizeof(tmpCounters));
#define assert(condition)
ERR_STATIC unsigned ERR_isError(size_t code)
size_t HIST_countFast(unsigned *count, unsigned *maxSymbolValuePtr, const void *source, size_t sourceSize)
size_t HIST_count_wksp(unsigned *count, unsigned *maxSymbolValuePtr, const void *source, size_t sourceSize, void *workSpace, size_t workSpaceSize)
size_t HIST_count(unsigned *count, unsigned *maxSymbolValuePtr, const void *src, size_t srcSize)
size_t HIST_countFast_wksp(unsigned *count, unsigned *maxSymbolValuePtr, const void *source, size_t sourceSize, void *workSpace, size_t workSpaceSize)
unsigned HIST_isError(size_t code)
unsigned HIST_count_simple(unsigned *count, unsigned *maxSymbolValuePtr, const void *src, size_t srcSize)
#define HIST_WKSP_SIZE_U32
MEM_STATIC U32 MEM_read32(const void *memPtr)
#define ZSTD_memmove(d, s, l)
#define ZSTD_memset(p, v, l)