3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
8 #include "SkPackBits.h"
12 static inline void small_memcpy(void* SK_RESTRICT dst,
13 const void* SK_RESTRICT src, size_t n) {
14 SkASSERT(n > 0 && n <= 15);
15 uint8_t* d = (uint8_t*)dst;
16 const uint8_t* s = (const uint8_t*)src;
37 static inline void small_memset(void* dst, uint8_t value, size_t n) {
38 SkASSERT(n > 0 && n <= 15);
39 uint8_t* d = (uint8_t*)dst;
41 case 15: *d++ = value;
42 case 14: *d++ = value;
43 case 13: *d++ = value;
44 case 12: *d++ = value;
45 case 11: *d++ = value;
46 case 10: *d++ = value;
60 // can we do better for small counts with our own inlined memcpy/memset?
62 #define PB_MEMSET(addr, value, count) \
65 memset(addr, value, count); \
67 small_memset(addr, value, count); \
71 #define PB_MEMCPY(dst, src, count) \
74 memcpy(dst, src, count); \
76 small_memcpy(dst, src, count); \
80 ///////////////////////////////////////////////////////////////////////////////
83 static int gMemSetBuckets[129];
84 static int gMemCpyBuckets[129];
87 static void register_memset_count(int n) {
88 SkASSERT((unsigned)n <= 128);
89 gMemSetBuckets[n] += 1;
92 if ((gCounter & 0xFF) == 0) {
93 SkDebugf("----- packbits memset stats: ");
94 for (size_t i = 0; i < SK_ARRAY_COUNT(gMemSetBuckets); i++) {
95 if (gMemSetBuckets[i]) {
96 SkDebugf(" %d:%d", i, gMemSetBuckets[i]);
101 static void register_memcpy_count(int n) {
102 SkASSERT((unsigned)n <= 128);
103 gMemCpyBuckets[n] += 1;
106 if ((gCounter & 0x1FF) == 0) {
107 SkDebugf("----- packbits memcpy stats: ");
108 for (size_t i = 0; i < SK_ARRAY_COUNT(gMemCpyBuckets); i++) {
109 if (gMemCpyBuckets[i]) {
110 SkDebugf(" %d:%d", i, gMemCpyBuckets[i]);
116 #define register_memset_count(n)
117 #define register_memcpy_count(n)
121 ///////////////////////////////////////////////////////////////////////////////
123 size_t SkPackBits::ComputeMaxSize16(int count) {
124 // worst case is the number of 16bit values (times 2) +
125 // 1 byte per (up to) 128 entries.
126 return ((count + 127) >> 7) + (count << 1);
129 size_t SkPackBits::ComputeMaxSize8(int count) {
130 // worst case is the number of 8bit values + 1 byte per (up to) 128 entries.
131 return ((count + 127) >> 7) + count;
134 static uint8_t* flush_same16(uint8_t dst[], uint16_t value, int count) {
140 *dst++ = (uint8_t)(n - 1);
141 *dst++ = (uint8_t)(value >> 8);
142 *dst++ = (uint8_t)value;
148 static uint8_t* flush_same8(uint8_t dst[], uint8_t value, int count) {
154 *dst++ = (uint8_t)(n - 1);
155 *dst++ = (uint8_t)value;
161 static uint8_t* flush_diff16(uint8_t* SK_RESTRICT dst,
162 const uint16_t* SK_RESTRICT src, int count) {
168 *dst++ = (uint8_t)(n + 127);
169 PB_MEMCPY(dst, src, n * sizeof(uint16_t));
171 dst += n * sizeof(uint16_t);
177 static uint8_t* flush_diff8(uint8_t* SK_RESTRICT dst,
178 const uint8_t* SK_RESTRICT src, int count) {
184 *dst++ = (uint8_t)(n + 127);
185 PB_MEMCPY(dst, src, n);
193 size_t SkPackBits::Pack16(const uint16_t* SK_RESTRICT src, int count,
194 uint8_t* SK_RESTRICT dst) {
195 uint8_t* origDst = dst;
196 const uint16_t* stop = src + count;
199 count = SkToInt(stop - src);
200 SkASSERT(count >= 0);
202 return dst - origDst;
206 *dst++ = (uint8_t)(*src >> 8);
207 *dst++ = (uint8_t)*src;
208 return dst - origDst;
211 unsigned value = *src;
212 const uint16_t* s = src + 1;
214 if (*s == value) { // accumulate same values...
220 } while (*s == value);
221 dst = flush_same16(dst, value, SkToInt(s - src));
222 } else { // accumulate diff values...
227 } while (*s != s[-1]);
228 s -= 1; // back up so we don't grab one of the "same" values that follow
230 dst = flush_diff16(dst, src, SkToInt(s - src));
236 size_t SkPackBits::Pack8(const uint8_t* SK_RESTRICT src, int count,
237 uint8_t* SK_RESTRICT dst) {
238 uint8_t* origDst = dst;
239 const uint8_t* stop = src + count;
242 count = SkToInt(stop - src);
243 SkASSERT(count >= 0);
245 return dst - origDst;
250 return dst - origDst;
253 unsigned value = *src;
254 const uint8_t* s = src + 1;
256 if (*s == value) { // accumulate same values...
262 } while (*s == value);
263 dst = flush_same8(dst, value, SkToInt(s - src));
264 } else { // accumulate diff values...
269 // only stop if we hit 3 in a row,
270 // otherwise we get bigger than compuatemax
271 } while (*s != s[-1] || s[-1] != s[-2]);
272 s -= 2; // back up so we don't grab the "same" values that follow
274 dst = flush_diff8(dst, src, SkToInt(s - src));
282 int SkPackBits::Unpack16(const uint8_t* SK_RESTRICT src, size_t srcSize,
283 uint16_t* SK_RESTRICT dst) {
284 uint16_t* origDst = dst;
285 const uint8_t* stop = src + srcSize;
289 if (n <= 127) { // repeat count (n + 1)
291 sk_memset16(dst, (src[0] << 8) | src[1], n);
293 } else { // same count (n - 127)
295 PB_MEMCPY(dst, src, n * sizeof(uint16_t));
296 src += n * sizeof(uint16_t);
300 SkASSERT(src == stop);
301 return SkToInt(dst - origDst);
304 int SkPackBits::Unpack8(const uint8_t* SK_RESTRICT src, size_t srcSize,
305 uint8_t* SK_RESTRICT dst) {
306 uint8_t* origDst = dst;
307 const uint8_t* stop = src + srcSize;
311 if (n <= 127) { // repeat count (n + 1)
313 PB_MEMSET(dst, *src++, n);
314 } else { // same count (n - 127)
316 PB_MEMCPY(dst, src, n);
321 SkASSERT(src == stop);
322 return SkToInt(dst - origDst);
331 void SkPackBits::Unpack8(uint8_t* SK_RESTRICT dst, size_t dstSkip,
332 size_t dstWrite, const uint8_t* SK_RESTRICT src) {
337 UnpackState state = CLEAN_STATE;
338 size_t stateCount = 0;
340 // state 1: do the skip-loop
341 while (dstSkip > 0) {
343 if (n <= 127) { // repeat count (n + 1)
346 state = REPEAT_BYTE_STATE;
347 stateCount = n - dstSkip;
349 // we don't increment src here, since its needed in stage 2
351 src++; // skip the src byte
353 } else { // same count (n - 127)
356 state = COPY_SRC_STATE;
357 stateCount = n - dstSkip;
365 // stage 2: perform any catchup from the skip-stage
366 if (stateCount > dstWrite) {
367 stateCount = dstWrite;
370 case REPEAT_BYTE_STATE:
371 SkASSERT(stateCount > 0);
372 register_memset_count(stateCount);
373 PB_MEMSET(dst, *src++, stateCount);
376 SkASSERT(stateCount > 0);
377 register_memcpy_count(stateCount);
378 PB_MEMCPY(dst, src, stateCount);
382 SkASSERT(stateCount == 0);
386 dstWrite -= stateCount;
388 // copy at most dstWrite bytes into dst[]
389 while (dstWrite > 0) {
391 if (n <= 127) { // repeat count (n + 1)
396 register_memset_count(n);
397 PB_MEMSET(dst, *src++, n);
398 } else { // same count (n - 127)
403 register_memcpy_count(n);
404 PB_MEMCPY(dst, src, n);
410 SkASSERT(0 == dstWrite);