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