snappy/csnappy_internal_userspace.h

filter: Statementstmt 0.0% 0 / 2 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 2 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« snappy/csnappy_internal.h ↑ Summary srl_float.h »
1 /*
2 Copyright 2011 Google Inc. All Rights Reserved.
3  
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
6 met:
7  
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above
11 copyright notice, this list of conditions and the following disclaimer
12 in the documentation and/or other materials provided with the
13 distribution.
14 * Neither the name of Google Inc. nor the names of its
15 contributors may be used to endorse or promote products derived from
16 this software without specific prior written permission.
17  
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  
30 Various stubs for the open-source version of Snappy.
31  
32 File modified by
33 Zeev Tarantov <zeev.tarantov@gmail.com>
34  
35 File modified for Sereal by
36 Steffen Mueller <cpan@steffen-mueller.net>
37 Yves Orton <demerphq@gmail.com>
38  
39 */
40  
41 #ifndef CSNAPPY_INTERNAL_USERSPACE_H_
42 #define CSNAPPY_INTERNAL_USERSPACE_H_
43  
44 /*note the original version of this file checked for MS version, but MS will *never* support
45 * anything but C89, so the version check is bogus. */
46 #if defined(_MSC_VER)
47 typedef unsigned __int8 uint8_t;
48 typedef unsigned __int16 uint16_t;
49 typedef unsigned __int32 uint32_t;
50 typedef unsigned __int64 uint64_t;
51 typedef __int32 int32_t; /* Sereal specific change, see csnappy_decompress.c(271) : error C2065: 'int32_t' : undeclared identifier */
52 /* the following define is Sereal specific, as MS C89 compilers do not know about "inline" */
53 #define inline __inline
54 #ifdef _M_X64
55 # define __x86_64__
56 # define __x86_64
57 # define __amd64__
58 # define __amd64
59 #endif
60 #ifdef _M_IX86
61 # define __i386__
62 # define __i386
63 # define i386
64 # define _X86_
65 #endif
66 #ifdef _M_IA64
67 # define __ia64__
68 # define __ia64
69 # define __IA64__
70 # define __itanium__
71 #endif
72  
73 #else
74  
75 #if defined(__SUNPRO_C) || defined(_AIX)
76 # include <inttypes.h>
77 #else
78 # include <stdint.h>
79 #endif
80  
81 #endif
82  
83 #ifdef _GNU_SOURCE
84 #define min(x, y) (__extension__ ({ \
85 typeof(x) _min1 = (x); \
86 typeof(y) _min2 = (y); \
87 (void) (&_min1 == &_min2); \
88 _min1 < _min2 ? _min1 : _min2; }))
89 #else
90 #define min(x, y) (((x) < (y)) ? (x) : (y))
91 #endif
92  
93 /* Static prediction hints. */
94 #ifndef __GNUC__
95 #define __builtin_expect(a,b) a
96 #endif
97 #define likely(x) __builtin_expect(!!(x), 1)
98 #define unlikely(x) __builtin_expect(!!(x), 0)
99  
100  
101 #ifdef DEBUG
102 #include <assert.h>
103 #define DCHECK(cond) assert(cond)
104 #else
105 #define DCHECK(cond)
106 #endif
107  
108 #include "csnappy_compat.h"
109  
110 /*
111 Uses code from http://code.google.com/p/exfat/source/browse/trunk/libexfat/byteorder.h
112 with 3-clause BSD license instead of GPL, with permission from:
113 Andrew Nayenko
114 Albert Lee
115 */
116 #if defined(_MSC_VER)
117  
118 #include <stdlib.h>
119 #define bswap_16(x) _byteswap_ushort(x)
120 #define bswap_32(x) _byteswap_ulong(x)
121 #define bswap_64(x) _byteswap_uint64(x)
122 #define __BIG_ENDIAN 4321
123 #define __LITTLE_ENDIAN 1234
124 #define __BYTE_ORDER LITTLE_ENDIAN
125  
126 #elif defined(_AIX)
127  
128 #include <sys/machine.h>
129 #define __LITTLE_ENDIAN LITTLE_ENDIAN
130 #define __BIG_ENDIAN BIG_ENDIAN
131 #define __BYTE_ORDER __BIG_ENDIAN
132  
133 #elif defined(__APPLE__)
134  
135 #include <machine/endian.h>
136 #include <libkern/OSByteOrder.h>
137 #define bswap_16(x) OSSwapInt16(x)
138 #define bswap_32(x) OSSwapInt32(x)
139 #define bswap_64(x) OSSwapInt64(x)
140 #define __BYTE_ORDER BYTE_ORDER
141 #define __LITTLE_ENDIAN LITTLE_ENDIAN
142 #define __BIG_ENDIAN BIG_ENDIAN
143  
144 #elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__)
145  
146 #include <sys/endian.h>
147 #define bswap_16(x) bswap16(x)
148 #define bswap_32(x) bswap32(x)
149 #define bswap_64(x) bswap64(x)
150 #define __BYTE_ORDER _BYTE_ORDER
151 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
152 #define __BIG_ENDIAN _BIG_ENDIAN
153  
154 #elif defined(__OpenBSD__)
155  
156 #include <machine/endian.h>
157 #define bswap_16(x) swap16(x)
158 #define bswap_32(x) swap32(x)
159 #define bswap_64(x) swap64(x)
160 #define __BYTE_ORDER _BYTE_ORDER
161 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
162 #define __BIG_ENDIAN _BIG_ENDIAN
163  
164 #elif defined(__MINGW32__)
165 #include <sys/param.h>
166 #define __BYTE_ORDER BYTE_ORDER
167 #define __LITTLE_ENDIAN LITTLE_ENDIAN
168 #define __BIG_ENDIAN BIG_ENDIAN
169  
170  
171 #elif defined(__sun)
172  
173 #include <sys/byteorder.h>
174 #define bswap_16(x) BSWAP_16(x)
175 #define bswap_32(x) BSWAP_32(x)
176 #define bswap_64(x) BSWAP_64(x)
177 #define __LITTLE_ENDIAN 1234
178 #define __BIG_ENDIAN 4321
179 #ifdef _LITTLE_ENDIAN
180 #define __BYTE_ORDER __LITTLE_ENDIAN
181 #else
182 #define __BYTE_ORDER __BIG_ENDIAN
183 #endif
184  
185 #elif defined(__hpux)
186  
187 #ifdef __LP64__
188 #define __LITTLE_ENDIAN 12345678
189 #define __BIG_ENDIAN 87654321
190 #define int64_t long
191 #else
192 #define __LITTLE_ENDIAN 1234
193 #define __BIG_ENDIAN 4321
194 #define int64_t long long
195 #endif
196  
197 #define __BYTE_ORDER __BIG_ENDIAN /* HP-UX always */
198 #define int32_t int
199 #define int16_t short
200  
201 #define __SNAPPY_STRICT_ALIGN
202  
203 #elif defined(__s390x__) || defined(__zarch__) || defined(__SYSC_ZARCH__)
204  
205 #ifndef __BIG_ENDIAN
206 #define __BIG_ENDIAN 87654321
207 #endif
208 #ifndef __LITTLE_ENDIAN
209 #define __LITTLE_ENDIAN 12345678
210 #endif
211 #ifndef __BYTE_ORDER
212 #define __BYTE_ORDER __BIG_ENDIAN
213 #endif
214  
215 #define __SNAPPY_STRICT_ALIGN
216  
217 #elif defined(__HAIKU__)
218  
219 /* Haiku has endian.h but no byteswap.h; use the fallbacks below. */
220 #include <endian.h>
221  
222 #elif defined(__GNUC__) || defined(__ANDROID__) || defined(__CYGWIN__)
223  
224 #include <endian.h>
225 #include <byteswap.h>
226  
227 #endif
228  
229 #ifndef bswap_16
230 #define bswap_16(x) \
231 (((uint16_t)(x) & 0xFF00) >> 8 | \
232 ((uint16_t)(x) & 0x00FF) << 8)
233 #endif
234  
235 #ifndef bswap_32
236 #define bswap_32(x) \
237 (((uint32_t)(x) & 0xFF000000) >> 24 | \
238 ((uint32_t)(x) & 0x00FF0000) >> 8 | \
239 ((uint32_t)(x) & 0x0000FF00) << 8 | \
240 ((uint32_t)(x) & 0x000000FF) << 24)
241 #endif
242  
243 #ifndef bswap_64
244 #define bswap_64(x) \
245 (((uint64_t)(x) & 0xFF00000000000000) >> 56 | \
246 ((uint64_t)(x) & 0x00FF000000000000) >> 40 | \
247 ((uint64_t)(x) & 0x0000FF0000000000) >> 24 | \
248 ((uint64_t)(x) & 0x000000FF00000000) >> 8 | \
249 ((uint64_t)(x) & 0x00000000FF000000) << 8 | \
250 ((uint64_t)(x) & 0x0000000000FF0000) << 24 | \
251 ((uint64_t)(x) & 0x000000000000FF00) << 40 | \
252 ((uint64_t)(x) & 0x00000000000000FF) << 56)
253 #endif
254  
255  
256 /* Potentially unaligned loads and stores. */
257  
258 #if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__)
259 #if defined(__GNUC__)
260 typedef uint16_t my_uint16_t __attribute__((aligned(1)));
261 typedef uint32_t my_uint32_t __attribute__((aligned(1)));
262 typedef uint64_t my_uint64_t __attribute__((aligned(1)));
263 #else
264 typedef uint16_t my_uint16_t;
265 typedef uint32_t my_uint32_t;
266 typedef uint64_t my_uint64_t;
267 #endif
268  
269 #define UNALIGNED_LOAD16(_p) (*(const my_uint16_t*)(_p))
270 #define UNALIGNED_LOAD32(_p) (*(const my_uint32_t*)(_p))
271 #define UNALIGNED_LOAD64(_p) (*(const my_uint64_t*)(_p))
272  
273 #define UNALIGNED_STORE16(_p, _val) (*(my_uint16_t*)(_p) = (_val))
274 #define UNALIGNED_STORE32(_p, _val) (*(my_uint32_t*)(_p) = (_val))
275 #define UNALIGNED_STORE64(_p, _val) (*(my_uint64_t*)(_p) = (_val))
276  
277 #elif defined(__arm__) && \
278 !defined(__ARM_ARCH_4__) && \
279 !defined(__ARM_ARCH_4T__) && /* http://wiki.debian.org/ArmEabiPort#Choice_of_minimum_CPU */ \
280 !defined(__MARM_ARMV4__) && \
281 !defined(_ARMV4I_) && \
282 !defined(__ARM_ARCH_5__) && \
283 !defined(__ARM_ARCH_5T__) && \
284 !defined(__ARM_ARCH_5E__) && \
285 !defined(__ARM_ARCH_5TE__) && \
286 !defined(__ARM_ARCH_5TEJ__) && \
287 !defined(__MARM_ARMV5__)
288  
289 #define UNALIGNED_LOAD16(_p) (*(const uint16_t*)(_p))
290 #define UNALIGNED_LOAD32(_p) (*(const uint32_t*)(_p))
291 #define UNALIGNED_STORE16(_p, _val) (*(uint16_t*)(_p) = (_val))
292 #define UNALIGNED_STORE32(_p, _val) (*(uint32_t*)(_p) = (_val))
293  
294 #pragma pack(1)
295 struct una_u64 { uint64_t x; };
296 #pragma pack()
297  
298 static INLINE uint64_t UNALIGNED_LOAD64(const void *p)
299 {
300 const struct una_u64 *ptr = (const struct una_u64 *)p;
301 return ptr->x;
302 }
303  
304 static INLINE void UNALIGNED_STORE64(void *p, uint64_t v)
305 {
306 struct una_u64 *ptr = (struct una_u64 *)p;
307 ptr->x = v;
308 }
309  
310 #elif defined(__SNAPPY_STRICT_ALIGN) || defined(__sparc) || defined(__sparc__) /* strict architectures */
311  
312 /* For these platforms, there really are no unaligned loads/stores.
313 * Read/write everything as uint8_t. Smart compilers might recognize
314 * these patterns and generate something smart. */
315  
316 /* Possible future enhancement: see if the ptr is evenly divisible
317 * (as uintNN_t) by 2/4/8, and if so, do the cast-as-uintNN_t-ptr-
318 * and-deref-as-uintNN_t. Balancing act: adding the branch
319 * will slow things down, while reading/writing aligned might speed
320 * things up. */
321  
322 #if __BYTE_ORDER == __BIG_ENDIAN
323  
324 static INLINE uint16_t UNALIGNED_LOAD16(const void *p)
325 {
326 return
327 (uint16_t)(((uint8_t*)p)[0]) << 8 |
328 (uint16_t)(((uint8_t*)p)[1]);
329 }
330  
331 static INLINE uint32_t UNALIGNED_LOAD32(const void *p)
332 {
333 return
334 (uint32_t)(((uint8_t*)p)[0]) << 24 |
335 (uint32_t)(((uint8_t*)p)[1]) << 16 |
336 (uint32_t)(((uint8_t*)p)[2]) << 8 |
337 (uint32_t)(((uint8_t*)p)[3]);
338 }
339  
340 static INLINE uint64_t UNALIGNED_LOAD64(const void *p)
341 {
342 return
343 (uint64_t)((uint8_t*)p)[0] << 56 |
344 (uint64_t)((uint8_t*)p)[1] << 48 |
345 (uint64_t)((uint8_t*)p)[2] << 40 |
346 (uint64_t)((uint8_t*)p)[3] << 32 |
347 (uint64_t)((uint8_t*)p)[4] << 24 |
348 (uint64_t)((uint8_t*)p)[5] << 16 |
349 (uint64_t)((uint8_t*)p)[5] << 8 |
350 (uint64_t)((uint8_t*)p)[7];
351 }
352  
353 static INLINE void UNALIGNED_STORE16(void *p, uint16_t v)
354 {
355 uint8_t* s = (uint8_t*)p;
356 s[0] = (v & 0xFF00) >> 8;
357 s[1] = (v & 0x00FF);
358 }
359  
360 static INLINE void UNALIGNED_STORE32(void *p, uint32_t v)
361 {
362 uint8_t* s = (uint8_t*)p;
363 s[0] = (v & 0xFF000000) >> 24;
364 s[1] = (v & 0x00FF0000) >> 16;
365 s[2] = (v & 0x0000FF00) >> 8;
366 s[3] = (v & 0x000000FF);
367 }
368  
369 static INLINE void UNALIGNED_STORE64(void *p, uint64_t v)
370 {
371 uint8_t* s = (uint8_t*)p;
372 s[0] = (v & 0xFF00000000000000) >> 56;
373 s[1] = (v & 0x00FF000000000000) >> 48;
374 s[2] = (v & 0x0000FF0000000000) >> 40;
375 s[3] = (v & 0x000000FF00000000) >> 32;
376 s[4] = (v & 0x00000000FF000000) >> 24;
377 s[5] = (v & 0x0000000000FF0000) >> 16;
378 s[6] = (v & 0x000000000000FF00) >> 8;
379 s[7] = (v & 0x00000000000000FF);
380 }
381  
382 #endif /* #if __BYTE_ORDER == __BIG_ENDIAN */
383  
384 #if __BYTE_ORDER == __LITTLE_ENDIAN
385  
386 static INLINE uint16_t UNALIGNED_LOAD16(const void *p)
387 {
388 return
389 (uint16_t)(((uint8_t*)p)[1]) << 8) |
390 (uint16_t)(((uint8_t*)p)[0]);
391 }
392  
393 static INLINE uint32_t UNALIGNED_LOAD32(const void *p)
394 {
395 return
396 (uint32_t)(((uint8_t*)p)[3]) << 24 |
397 (uint32_t)(((uint8_t*)p)[2]) << 16 |
398 (uint32_t)(((uint8_t*)p)[1]) << 8 |
399 (uint32_t)(((uint8_t*)p)[0]);
400 }
401  
402 static INLINE uint64_t UNALIGNED_LOAD64(const void *p)
403 {
404 return
405 (uint64_t)(((uint8_t*)p)[7]) << 56 |
406 (uint64_t)(((uint8_t*)p)[6]) << 48 |
407 (uint64_t)(((uint8_t*)p)[5]) << 40 |
408 (uint64_t)(((uint8_t*)p)[4]) << 32 |
409 (uint64_t)(((uint8_t*)p)[3]) << 24 |
410 (uint64_t)(((uint8_t*)p)[2]) << 16 |
411 (uint64_t)(((uint8_t*)p)[1]) << 8 |
412 (uint64_t)(((uint8_t*)p)[0]);
413 }
414  
415 static INLINE void UNALIGNED_STORE16(void *p, uint16_t v)
416 {
417 uint8_t* s = (uint8_t*)p;
418 s[1] = (v & 0xFF00) >> 8;
419 s[0] = (v & 0x00FF);
420 }
421  
422 static INLINE void UNALIGNED_STORE32(void *p, uint32_t v)
423 {
424 uint8_t* s = (uint8_t*)p;
425 s[3] = (v & 0xFF000000) >> 24;
426 s[2] = (v & 0x00FF0000) >> 16;
427 s[1] = (v & 0x0000FF00) >> 8;
428 s[0] = (v & 0x000000FF);
429 }
430  
431 static INLINE void UNALIGNED_STORE64(void *p, uint64_t v)
432 {
433 uint8_t* s = (uint8_t*)p;
434 s[7] = (v & 0xFF00000000000000) >> 56;
435 s[6] = (v & 0x00FF000000000000) >> 48;
436 s[5] = (v & 0x0000FF0000000000) >> 40;
437 s[4] = (v & 0x000000FF00000000) >> 32;
438 s[3] = (v & 0x00000000FF000000) >> 24;
439 s[2] = (v & 0x0000000000FF0000) >> 16;
440 s[1] = (v & 0x000000000000FF00) >> 8;
441 s[0] = (v & 0x00000000000000FF);
442 }
443  
444 #endif /* #if __BYTE_ORDER == __LITTLE_ENDIAN */
445  
446 #else /* !(x86 || powerpc) && !(arm && !(old arm architectures)) */
447  
448 /* pragma pack is available in gcc (though originally apparently by
449 * Microsoft) and in some other compilers (probably inspired by either
450 * the two big ones), but there is no good portable way to detect
451 * whether it's supported. The bad news: on platforms where it's not
452 * supported (unsupported pragmas are ignored) but which do require
453 * strict alignment, the below pragma pack trickery will fail.
454 * Therefore this option is the last and the default, and the platforms
455 * requiring strict alignment are detected earlier. */
456  
457 #pragma pack(1)
458 struct una_u16 { uint16_t x; };
459 struct una_u32 { uint32_t x; };
460 struct una_u64 { uint64_t x; };
461 #pragma pack()
462  
463 static INLINE uint16_t UNALIGNED_LOAD16(const void *p)
464 {
465 const struct una_u16 *ptr = (const struct una_u16 *)p;
466 return ptr->x;
467 }
468  
469 static INLINE uint32_t UNALIGNED_LOAD32(const void *p)
470 {
471 const struct una_u32 *ptr = (const struct una_u32 *)p;
472 return ptr->x;
473 }
474  
475 static INLINE uint64_t UNALIGNED_LOAD64(const void *p)
476 {
477 const struct una_u64 *ptr = (const struct una_u64 *)p;
478 return ptr->x;
479 }
480  
481 static INLINE void UNALIGNED_STORE16(void *p, uint16_t v)
482 {
483 struct una_u16 *ptr = (struct una_u16 *)p;
484 ptr->x = v;
485 }
486  
487 static INLINE void UNALIGNED_STORE32(void *p, uint32_t v)
488 {
489 struct una_u32 *ptr = (struct una_u32 *)p;
490 ptr->x = v;
491 }
492  
493 static INLINE void UNALIGNED_STORE64(void *p, uint64_t v)
494 {
495 struct una_u64 *ptr = (struct una_u64 *)p;
496 ptr->x = v;
497 }
498  
499 #endif /* defining UNALIGNED_LOADNN and UNALIGNED_STORENN */
500  
501  
502 #if __BYTE_ORDER == __LITTLE_ENDIAN
503 #define get_unaligned_le32(p) UNALIGNED_LOAD32(p)
504 #define put_unaligned_le16(v, p) UNALIGNED_STORE16(p, v)
505 #elif __BYTE_ORDER == __BIG_ENDIAN
506 static INLINE uint32_t get_unaligned_le32(const void *p)
507 {
508 return bswap_32(UNALIGNED_LOAD32(p));
509 }
510 static INLINE void put_unaligned_le16(uint16_t val, void *p)
511 {
512 UNALIGNED_STORE16(p, bswap_16(val));
513 }
514 #else
515 static INLINE uint32_t get_unaligned_le32(const void *p)
516 {
517 const uint8_t *b = (const uint8_t *)p;
518 return b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
519 }
520 static INLINE void put_unaligned_le16(uint16_t val, void *p)
521 {
522 uint8_t *b = (uint8_t *)p;
523 b[0] = val & 255;
524 b[1] = val >> 8;
525 }
526 #endif
527  
528  
529 #if defined(HAVE_BUILTIN_CTZ)
530  
531 static INLINE int FindLSBSetNonZero(uint32_t n)
532 {
533 return __builtin_ctz(n);
534 }
535  
536 0 static INLINE int FindLSBSetNonZero64(uint64_t n)
537 {
538 0 return __builtin_ctzll(n);
539 }
540  
541 #else /* Portable versions. */
542  
543 static INLINE int FindLSBSetNonZero(uint32_t n)
544 {
545 int rc = 31, i, shift;
546 uint32_t x;
547 for (i = 4, shift = 1 << 4; i >= 0; --i) {
548 x = n << shift;
549 if (x != 0) {
550 n = x;
551 rc -= shift;
552 }
553 shift >>= 1;
554 }
555 return rc;
556 }
557  
558 /* FindLSBSetNonZero64() is defined in terms of FindLSBSetNonZero(). */
559 static INLINE int FindLSBSetNonZero64(uint64_t n)
560 {
561 const uint32_t bottombits = (uint32_t)n;
562 if (bottombits == 0) {
563 /* Bottom bits are zero, so scan in top bits */
564 return 32 + FindLSBSetNonZero((uint32_t)(n >> 32));
565 } else {
566 return FindLSBSetNonZero(bottombits);
567 }
568 }
569  
570 #endif /* End portable versions. */
571  
572 #endif /* CSNAPPY_INTERNAL_USERSPACE_H_ */
« snappy/csnappy_internal.h ↑ Summary srl_float.h »