Skip to content

Commit

Permalink
Update ZLib-Intel to v1.2.11 (dotnet/corefx#32732)
Browse files Browse the repository at this point in the history
Commit migrated from dotnet/corefx@c89e274
  • Loading branch information
vkvenkat authored and stephentoub committed Nov 3, 2018
1 parent a7a9b47 commit e7c6f30
Show file tree
Hide file tree
Showing 27 changed files with 4,295 additions and 1,466 deletions.
11 changes: 6 additions & 5 deletions src/libraries/Native/Windows/clrcompression/CMakeLists.txt
Expand Up @@ -10,19 +10,20 @@ include (GenerateExportHeader)
if($ENV{__BuildArch} STREQUAL x86 OR $ENV{__BuildArch} STREQUAL x64)
set(NATIVECOMPRESSION_SOURCES
zlib-intel/adler32.c
zlib-intel/compress.c
zlib-intel/crc_folding.c
zlib-intel/crc32.c
zlib-intel/deflate_medium.c
zlib-intel/deflate_quick.c
zlib-intel/deflate.c
zlib-intel/fill_window_sse.c
zlib-intel/inffast.c
zlib-intel/inflate.c
zlib-intel/inftrees.c
zlib-intel/match.c
zlib-intel/slide_sse.c
zlib-intel/trees.c
zlib-intel/x86.c
zlib-intel/zutil.c
zlib/compress.c
zlib/inffast.c
zlib/inflate.c
zlib/inftrees.c
../../AnyOS/zlib/pal_zlib.c
)
elseif($ENV{__BuildArch} STREQUAL arm OR $ENV{__BuildArch} STREQUAL arm64)
Expand Down
21 changes: 14 additions & 7 deletions src/libraries/Native/Windows/clrcompression/zlib-intel/adler32.c
@@ -1,17 +1,15 @@
/* adler32.c -- compute the Adler-32 checksum of a data stream
* Copyright (C) 1995-2011 Mark Adler
* Copyright (C) 1995-2011, 2016 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/

/* @(#) $Id$ */

#include "zutil.h"

#define local static

local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2));

#define BASE 65521 /* largest prime smaller than 65536 */
#define BASE 65521U /* largest prime smaller than 65536 */
#define NMAX 5552
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */

Expand Down Expand Up @@ -62,10 +60,10 @@ local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2));
#endif

/* ========================================================================= */
uLong ZEXPORT adler32(adler, buf, len)
uLong ZEXPORT adler32_z(adler, buf, len)
uLong adler;
const Bytef *buf;
uInt len;
z_size_t len;
{
unsigned long sum2;
unsigned n;
Expand Down Expand Up @@ -148,6 +146,15 @@ uLong ZEXPORT adler32(adler, buf, len)
return adler | (sum2 << 16);
}

/* ========================================================================= */
uLong ZEXPORT adler32(adler, buf, len)
uLong adler;
const Bytef *buf;
uInt len;
{
return adler32_z(adler, buf, len);
}

/* ========================================================================= */
local uLong adler32_combine_(adler1, adler2, len2)
uLong adler1;
Expand All @@ -172,7 +179,7 @@ local uLong adler32_combine_(adler1, adler2, len2)
sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
if (sum1 >= BASE) sum1 -= BASE;
if (sum1 >= BASE) sum1 -= BASE;
if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
if (sum2 >= ((unsigned long)BASE << 1)) sum2 -= ((unsigned long)BASE << 1);
if (sum2 >= BASE) sum2 -= BASE;
return sum1 | (sum2 << 16);
}
Expand Down
86 changes: 86 additions & 0 deletions src/libraries/Native/Windows/clrcompression/zlib-intel/compress.c
@@ -0,0 +1,86 @@
/* compress.c -- compress a memory buffer
* Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/

/* @(#) $Id$ */

#define ZLIB_INTERNAL
#include "zlib.h"

/* ===========================================================================
Compresses the source buffer into the destination buffer. The level
parameter has the same meaning as in deflateInit. sourceLen is the byte
length of the source buffer. Upon entry, destLen is the total size of the
destination buffer, which must be at least 0.1% larger than sourceLen plus
12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
memory, Z_BUF_ERROR if there was not enough room in the output buffer,
Z_STREAM_ERROR if the level parameter is invalid.
*/
int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
Bytef *dest;
uLongf *destLen;
const Bytef *source;
uLong sourceLen;
int level;
{
z_stream stream;
int err;
const uInt max = (uInt)-1;
uLong left;

left = *destLen;
*destLen = 0;

stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
stream.opaque = (voidpf)0;

err = deflateInit(&stream, level);
if (err != Z_OK) return err;

stream.next_out = dest;
stream.avail_out = 0;
stream.next_in = (z_const Bytef *)source;
stream.avail_in = 0;

do {
if (stream.avail_out == 0) {
stream.avail_out = left > (uLong)max ? max : (uInt)left;
left -= stream.avail_out;
}
if (stream.avail_in == 0) {
stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;
sourceLen -= stream.avail_in;
}
err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
} while (err == Z_OK);

*destLen = stream.total_out;
deflateEnd(&stream);
return err == Z_STREAM_END ? Z_OK : err;
}

/* ===========================================================================
*/
int ZEXPORT compress (dest, destLen, source, sourceLen)
Bytef *dest;
uLongf *destLen;
const Bytef *source;
uLong sourceLen;
{
return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
}

/* ===========================================================================
If the default memLevel or windowBits for deflateInit() is changed, then
this function needs to be updated.
*/
uLong ZEXPORT compressBound (sourceLen)
uLong sourceLen;
{
return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
(sourceLen >> 25) + 13;
}
88 changes: 32 additions & 56 deletions src/libraries/Native/Windows/clrcompression/zlib-intel/crc32.c
@@ -1,5 +1,5 @@
/* crc32.c -- compute the CRC-32 of a data stream
* Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler
* Copyright (C) 1995-2006, 2010, 2011, 2012, 2016 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*
* Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
Expand Down Expand Up @@ -30,17 +30,15 @@

#include "zutil.h" /* for STDC and FAR definitions */

#define local static

/* Definitions for doing the crc four data bytes at a time. */
#if !defined(NOBYFOUR) && defined(Z_U4)
# define BYFOUR
#endif
#ifdef BYFOUR
local unsigned long crc32_little OF((unsigned long,
const unsigned char FAR *, unsigned));
const unsigned char FAR *, z_size_t));
local unsigned long crc32_big OF((unsigned long,
const unsigned char FAR *, unsigned));
const unsigned char FAR *, z_size_t));
# define TBLS 8
#else
# define TBLS 1
Expand Down Expand Up @@ -202,10 +200,10 @@ const z_crc_t FAR * ZEXPORT get_crc_table()
#define DO4 DO1; DO1; DO1; DO1

/* ========================================================================= */
unsigned long ZEXPORT crc32(crc, buf, len)
unsigned long ZEXPORT crc32_z(crc, buf, len)
unsigned long crc;
const unsigned char FAR *buf;
uInt len;
z_size_t len;
{
if (buf == Z_NULL) return 0UL;

Expand All @@ -215,7 +213,7 @@ unsigned long ZEXPORT crc32(crc, buf, len)
#endif /* DYNAMIC_CRC_TABLE */

#ifdef BYFOUR
if (sizeof(void *) == sizeof(ptrdiff_t)) {
if (sizeof(void *) == sizeof(z_size_t)) {
z_crc_t endian;

endian = 1;
Expand Down Expand Up @@ -245,8 +243,29 @@ unsigned long ZEXPORT crc32(crc, buf, len)
return crc ^ 0xffffffffUL;
}

/* ========================================================================= */
unsigned long ZEXPORT crc32(crc, buf, len)
unsigned long crc;
const unsigned char FAR *buf;
uInt len;
{
return crc32_z(crc, buf, len);
}

#ifdef BYFOUR

/*
This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit
integer pointer type. This violates the strict aliasing rule, where a
compiler can assume, for optimization purposes, that two pointers to
fundamentally different types won't ever point to the same memory. This can
manifest as a problem only if one of the pointers is written to. This code
only reads from those pointers. So long as this code remains isolated in
this compilation unit, there won't be a problem. For this reason, this code
should not be copied and pasted into a compilation unit in which other code
writes to the buffer that is passed to these routines.
*/

/* ========================================================================= */
#define DOLIT4 c ^= *buf4++; \
c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \
Expand All @@ -257,14 +276,14 @@ unsigned long ZEXPORT crc32(crc, buf, len)
local unsigned long crc32_little(crc, buf, len)
unsigned long crc;
const unsigned char FAR *buf;
unsigned len;
z_size_t len;
{
register z_crc_t c;
register const z_crc_t FAR *buf4;

c = (z_crc_t)crc;
c = ~c;
while (len && ((ptrdiff_t)buf & 3)) {
while (len && ((z_size_t)buf & 3)) {
c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8);
len--;
}
Expand Down Expand Up @@ -292,7 +311,7 @@ local unsigned long crc32_little(crc, buf, len)
}

/* ========================================================================= */
#define DOBIG4 c ^= *++buf4; \
#define DOBIG4 c ^= *buf4++; \
c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \
crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24]
#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4
Expand All @@ -301,20 +320,19 @@ local unsigned long crc32_little(crc, buf, len)
local unsigned long crc32_big(crc, buf, len)
unsigned long crc;
const unsigned char FAR *buf;
unsigned len;
z_size_t len;
{
register z_crc_t c;
register const z_crc_t FAR *buf4;

c = ZSWAP32((z_crc_t)crc);
c = ~c;
while (len && ((ptrdiff_t)buf & 3)) {
while (len && ((z_size_t)buf & 3)) {
c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8);
len--;
}

buf4 = (const z_crc_t FAR *)(const void FAR *)buf;
buf4--;
while (len >= 32) {
DOBIG32;
len -= 32;
Expand All @@ -323,7 +341,6 @@ local unsigned long crc32_big(crc, buf, len)
DOBIG4;
len -= 4;
}
buf4++;
buf = (const unsigned char FAR *)buf4;

if (len) do {
Expand Down Expand Up @@ -438,44 +455,3 @@ uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
return crc32_combine_(crc1, crc2, len2);
}

#include "deflate.h"

#ifdef HAVE_PCLMULQDQ
#include "x86.h"
extern void ZLIB_INTERNAL crc_fold_init(deflate_state *z_const s);
extern void ZLIB_INTERNAL crc_fold_copy(deflate_state *z_const s,
unsigned char *dst, z_const unsigned char *src, long len);
extern unsigned ZLIB_INTERNAL crc_fold_512to32(deflate_state *z_const s);
#endif

ZLIB_INTERNAL void crc_reset(deflate_state *const s)
{
#ifdef HAVE_PCLMULQDQ
if (x86_cpu_has_pclmulqdq) {
crc_fold_init(s);
return;
}
#endif
s->strm->adler = crc32(0L, Z_NULL, 0);
}

ZLIB_INTERNAL void crc_finalize(deflate_state *const s)
{
#ifdef HAVE_PCLMULQDQ
if (x86_cpu_has_pclmulqdq)
s->strm->adler = crc_fold_512to32(s);
#endif
}

ZLIB_INTERNAL void copy_with_crc(z_streamp strm, Bytef *dst, long size)
{
#ifdef HAVE_PCLMULQDQ
if (x86_cpu_has_pclmulqdq) {
crc_fold_copy(strm->state, dst, strm->next_in, size);
return;
}
#endif
zmemcpy(dst, strm->next_in, size);
strm->adler = crc32(strm->adler, dst, size);
}

0 comments on commit e7c6f30

Please sign in to comment.