1 | /* |
---|
2 | zstd - standard compression library |
---|
3 | Header File for static linking only |
---|
4 | Copyright (C) 2014-2016, Yann Collet. |
---|
5 | |
---|
6 | BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) |
---|
7 | |
---|
8 | Redistribution and use in source and binary forms, with or without |
---|
9 | modification, are permitted provided that the following conditions are |
---|
10 | met: |
---|
11 | * Redistributions of source code must retain the above copyright |
---|
12 | notice, this list of conditions and the following disclaimer. |
---|
13 | * Redistributions in binary form must reproduce the above |
---|
14 | copyright notice, this list of conditions and the following disclaimer |
---|
15 | in the documentation and/or other materials provided with the |
---|
16 | distribution. |
---|
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
28 | |
---|
29 | You can contact the author at : |
---|
30 | - zstd homepage : http://www.zstd.net |
---|
31 | */ |
---|
32 | #ifndef ZSTD_STATS_H |
---|
33 | #define ZSTD_STATS_H |
---|
34 | |
---|
35 | |
---|
36 | #if defined (__cplusplus) |
---|
37 | extern "C" { |
---|
38 | #endif |
---|
39 | |
---|
40 | |
---|
41 | /*-************************************* |
---|
42 | * Types |
---|
43 | ***************************************/ |
---|
44 | struct ZSTD_stats_s { |
---|
45 | U32 priceOffset, priceOffCode, priceMatchLength, priceLiteral, priceLitLength; |
---|
46 | U32 totalMatchSum, totalLitSum, totalSeqSum, totalRepSum; |
---|
47 | U32 litSum, matchLengthSum, litLengthSum, offCodeSum; |
---|
48 | U32 matchLengthFreq[MaxML+1]; |
---|
49 | U32 litLengthFreq[MaxLL+1]; |
---|
50 | U32 litFreq[1<<Litbits]; |
---|
51 | U32 offCodeFreq[MaxOff+1]; |
---|
52 | }; |
---|
53 | |
---|
54 | |
---|
55 | /*-************************************* |
---|
56 | * Stats functions |
---|
57 | ***************************************/ |
---|
58 | MEM_STATIC ZSTD_stats_t* ZSTD_statsAlloc() { return malloc(sizeof(ZSTD_stats_t)); } |
---|
59 | MEM_STATIC void ZSTD_statsFree(struct ZSTD_stats_s* stats) { free(stats); } |
---|
60 | |
---|
61 | MEM_STATIC void ZSTD_statsPrint(ZSTD_stats_t* stats, U32 searchLength) |
---|
62 | { |
---|
63 | stats->totalMatchSum += stats->totalSeqSum * ((searchLength == 3) ? 3 : 4); |
---|
64 | printf("\navgMatchL=%.2f avgLitL=%.2f match=%.1f%% lit=%.1f%% reps=%d seq=%d\n", (float)stats->totalMatchSum/stats->totalSeqSum, (float)stats->totalLitSum/stats->totalSeqSum, 100.0*stats->totalMatchSum/(stats->totalMatchSum+stats->totalLitSum), 100.0*stats->totalLitSum/(stats->totalMatchSum+stats->totalLitSum), stats->totalRepSum, stats->totalSeqSum); |
---|
65 | printf("SumBytes=%d Offset=%d OffCode=%d Match=%d Literal=%d LitLength=%d\n", (stats->priceOffset+stats->priceOffCode+stats->priceMatchLength+stats->priceLiteral+stats->priceLitLength)/8, stats->priceOffset/8, stats->priceOffCode/8, stats->priceMatchLength/8, stats->priceLiteral/8, stats->priceLitLength/8); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | MEM_STATIC void ZSTD_statsInit(ZSTD_stats_t* stats) |
---|
70 | { |
---|
71 | stats->totalLitSum = stats->totalMatchSum = stats->totalSeqSum = stats->totalRepSum = 1; |
---|
72 | stats->priceOffset = stats->priceOffCode = stats->priceMatchLength = stats->priceLiteral = stats->priceLitLength = 0; |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | MEM_STATIC void ZSTD_statsResetFreqs(ZSTD_stats_t* stats) |
---|
77 | { |
---|
78 | unsigned u; |
---|
79 | |
---|
80 | stats->litSum = (2<<Litbits); |
---|
81 | stats->litLengthSum = MaxLL+1; |
---|
82 | stats->matchLengthSum = MaxML+1; |
---|
83 | stats->offCodeSum = (MaxOff+1); |
---|
84 | |
---|
85 | for (u=0; u<=MaxLit; u++) |
---|
86 | stats->litFreq[u] = 1; |
---|
87 | for (u=0; u<=MaxLL; u++) |
---|
88 | stats->litLengthFreq[u] = 1; |
---|
89 | for (u=0; u<=MaxML; u++) |
---|
90 | stats->matchLengthFreq[u] = 1; |
---|
91 | for (u=0; u<=MaxOff; u++) |
---|
92 | stats->offCodeFreq[u] = 1; |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | MEM_STATIC void ZSTD_statsUpdatePrices(ZSTD_stats_t* stats, size_t litLength, const BYTE* literals, size_t offset, size_t matchLength) |
---|
97 | { |
---|
98 | U32 u; |
---|
99 | /* literals */ |
---|
100 | stats->priceLiteral += litLength * ZSTD_highbit(stats->litSum+1); |
---|
101 | for (u=0; u < litLength; u++) |
---|
102 | stats->priceLiteral -= ZSTD_highbit(stats->litFreq[literals[u]]+1); |
---|
103 | stats->litSum += litLength; |
---|
104 | for (u=0; u < litLength; u++) |
---|
105 | stats->litFreq[literals[u]]++; |
---|
106 | |
---|
107 | /* literal Length */ |
---|
108 | { static const BYTE LL_Code[64] = { 0, 1, 2, 3, 4, 5, 6, 7, |
---|
109 | 8, 9, 10, 11, 12, 13, 14, 15, |
---|
110 | 16, 16, 17, 17, 18, 18, 19, 19, |
---|
111 | 20, 20, 20, 20, 21, 21, 21, 21, |
---|
112 | 22, 22, 22, 22, 22, 22, 22, 22, |
---|
113 | 23, 23, 23, 23, 23, 23, 23, 23, |
---|
114 | 24, 24, 24, 24, 24, 24, 24, 24, |
---|
115 | 24, 24, 24, 24, 24, 24, 24, 24 }; |
---|
116 | const BYTE LL_deltaCode = 19; |
---|
117 | const BYTE llCode = (litLength>63) ? (BYTE)ZSTD_highbit(litLength) + LL_deltaCode : LL_Code[litLength]; |
---|
118 | if (litLength) { |
---|
119 | stats->priceLitLength += LL_bits[llCode] + ZSTD_highbit(stats->litLengthSum+1) - ZSTD_highbit(stats->litLengthFreq[llCode]+1); |
---|
120 | } else { |
---|
121 | stats->priceLitLength += ZSTD_highbit(stats->litLengthSum+1) - ZSTD_highbit(stats->litLengthFreq[0]+1); |
---|
122 | } |
---|
123 | stats->litLengthFreq[llCode]++; |
---|
124 | stats->litLengthSum++; |
---|
125 | } |
---|
126 | |
---|
127 | /* match offset */ |
---|
128 | { BYTE offCode = (BYTE)ZSTD_highbit(offset+1); |
---|
129 | stats->priceOffCode += ZSTD_highbit(stats->offCodeSum+1) - ZSTD_highbit(stats->offCodeFreq[offCode]+1); |
---|
130 | stats->priceOffset += offCode; |
---|
131 | stats->offCodeSum++; |
---|
132 | stats->offCodeFreq[offCode]++; |
---|
133 | } |
---|
134 | |
---|
135 | /* match Length */ |
---|
136 | { static const BYTE ML_Code[128] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
---|
137 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
---|
138 | 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37, |
---|
139 | 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39, |
---|
140 | 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, |
---|
141 | 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, |
---|
142 | 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, |
---|
143 | 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 }; |
---|
144 | const BYTE ML_deltaCode = 36; |
---|
145 | const BYTE mlCode = (matchLength>127) ? (BYTE)ZSTD_highbit(matchLength) + ML_deltaCode : ML_Code[matchLength]; |
---|
146 | stats->priceMatchLength += ML_bits[mlCode] + ZSTD_highbit(stats->matchLengthSum+1) - ZSTD_highbit(stats->matchLengthFreq[mlCode]+1); |
---|
147 | stats->matchLengthFreq[mlCode]++; |
---|
148 | stats->matchLengthSum++; |
---|
149 | } |
---|
150 | |
---|
151 | if (offset == 0) stats->totalRepSum++; |
---|
152 | stats->totalSeqSum++; |
---|
153 | stats->totalMatchSum += matchLength; |
---|
154 | stats->totalLitSum += litLength; |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | #if defined (__cplusplus) |
---|
159 | } |
---|
160 | #endif |
---|
161 | |
---|
162 | #endif /* ZSTD_STATIC_H */ |
---|