Intel® Integrated Performance Primitives Developer Guide and Reference

ID 790148
Date 11/07/2023
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

EncodeLZ4

Performs LZ4 encoding.

Syntax

IppStatus ippsEncodeLZ4_8u(const Ipp8u* pSrc, int srcLen, Ipp8u* pDst, int* pDstLen, Ipp8u* pHashTable);

IppStatus ippsEncodeLZ4Fast_8u(const Ipp8u* pSrc, int srcLen, Ipp8u* pDst, int* pDstLen, Ipp8u* pHashTable, int* acceleration);

IppStatus ippsEncodeLZ4Safe_8u(const Ipp8u* pSrc, int* pSrcLen, Ipp8u* pDst, int* pDstLen, Ipp8u* pHashTable);

IppStatus ippsEncodeLZ4Dict_8u(const Ipp8u* pSrc, int srcIdx, int srcLen, Ipp8u* pDst, int* pDstLen, Ipp8u* pHashTable, const Ipp8u* pDict, int dictLen);

IppStatus ippsEncodeLZ4DictSafe_8u(const Ipp8u* pSrc, int srcIdx, int* pSrcLen, Ipp8u* pDst, int* pDstLen, Ipp8u* pHashTable, const Ipp8u* pDict, int dictLen);

Include Files

ippdc.h

Domain Dependencies

Headers: ippcore.h, ippvm.h, ipps.h

Libraries: ippcore.lib, ippvm.lib, ipps.lib

Parameters

pSrc

Pointer to the source data.

srcLen

Length of the source data for compression.

srcIdx

Index of the starting byte in the source vector.

pSrcLen

Pointer to the length of the source data for compression.

pDst

Pointer to the compressed data.

pDstLen

Pointer to the length of the compressed data.

pHashTable

Pointer to the LZ4 hash table.

pDict

Pointer to the dictionary.

dictLen

Length of the dictionary.

acceleraion

Acceleration value.

Description

These functions perform encoding of the source data pSrc using the LZ4 algorithm. The destination buffer must have sufficient length for the operation. The length of the compressed data is set to pDstLen.

Return Values

ippStsNoErr

Indicates no error.

ippStsNullPtrErr

Indicates an error if at least one of the specified pointers is NULL.

ippStsSizeErr

Indicates an error if the srcLen value is less than, or equal to zero.

ippStsBadArgErr

Indicates an error if the index of the starting byte is less than zero.

ippStsDstSizeLessExpected

Indicates an error if the length of the destination buffer is not sufficient.

Example

LZ4:

/*******************************************************************************
* Copyright 2015 Intel Corporation.
*
*
* This software and the related documents are Intel copyrighted materials, and your use of them is governed by
* the express license under which they were provided to you ('License'). Unless the License provides otherwise,
* you may not use, modify, copy, publish, distribute, disclose or transmit this software or the related
* documents without Intel's prior written permission.
* This software and the related documents are provided as is, with no express or implied warranties, other than
* those that are expressly stated in the License.
*******************************************************************************/

/*
 The example below shows how to use the functions:
     ippsEncodeLZ4HashTableGetSize_8u
     ippsEncodeLZ4HashTableInit_8u
     ippsEncodeLZ4_8u
     ippsDecodeLZ4_8u
*/

#include <stdio.h>
#include <string.h>

#include <ipp/ippdc.h>
#include <ipp/ipps.h>

/* Next two defines are created to simplify code reading and understanding */
#define EXIT_MAIN       exitLine:                              /* Label for Exit */
#define check_sts(st)   if((st) != ippStsNoErr) goto exitLine; /* Go to Exit if Intel(R) Integrated Primitives (Intel(R) IPP) function returned status different from ippStsNoErr */

#define TEST_SIZE (1024)

int main(void)
{
    Ipp8u       *srcBuf = NULL, *comprBuf = NULL, *decomprBuf = NULL, *hashTable = NULL;
    IppStatus   st = ippStsNoErr;
    int         hashSize = 0, 
                comprLen = TEST_SIZE + TEST_SIZE / 255 + 16,    /* Extra bytes for
                                                                    uncompressible data */
                decomprLen = TEST_SIZE + 33;
    int         i;

    srcBuf      = ippsMalloc_8u(TEST_SIZE);
    decomprBuf  = ippsMalloc_8u(decomprLen);                    /* Spare bytes for "wild"
                                                            (non-safe) decompression */
    comprBuf    = ippsMalloc_8u(comprLen);
    /* Initialize source buffer */
    check_sts(      st = ippsVectorJaehne_8u(srcBuf, TEST_SIZE, IPP_MAX_8U) )
    for(i = 0; i < TEST_SIZE; i++)
        srcBuf[i] >>= 6;                  /* Decrease source data entropy */
    /* Init and allocate hash table */
    check_sts(  st = ippsEncodeLZ4HashTableGetSize_8u(&hashSize)    )
    hashTable   = ippsMalloc_8u(hashSize);
    check_sts(  st = ippsEncodeLZ4HashTableInit_8u(hashTable, TEST_SIZE)    )
    /* Compress source data */
    check_sts(  st = ippsEncodeLZ4_8u((const Ipp8u*)srcBuf, TEST_SIZE, comprBuf,
                                      &comprLen, hashTable) )
    /* Print compression result */
    printf("Compression: %d bytes compressed into %d bytes\n", TEST_SIZE, comprLen);
    /* Decompression */
    decomprLen  = TEST_SIZE + 33;
    check_sts(  st = ippsDecodeLZ4_8u((const Ipp8u*)comprBuf, comprLen, decomprBuf,
                                      &decomprLen)  )
    /* Check */
    if(decomprLen == TEST_SIZE)/* Decompressed size must be equal to source data size */
    {
        if(memcmp(srcBuf, decomprBuf, TEST_SIZE) != 0)
        {
            printf("Wrong decompression!\n");
            st = ippStsErr;
        }
        else
            printf("Decompressed by ippsDecodeLZ4_8u OK\n");
    }
    else
        printf("Invalid decompressed length %d\n", decomprLen);

EXIT_MAIN
    ippsFree(srcBuf);
    ippsFree(comprBuf);
    ippsFree(decomprBuf);
    ippsFree(hashTable);
    return (int)st;
}

LZ4Dict:

/*******************************************************************************
* Copyright 2017 Intel Corporation.
*
*
* This software and the related documents are Intel copyrighted materials, and your use of them is governed by
* the express license under which they were provided to you ('License'). Unless the License provides otherwise,
* you may not use, modify, copy, publish, distribute, disclose or transmit this software or the related
* documents without Intel's prior written permission.
* This software and the related documents are provided as is, with no express or implied warranties, other than
* those that are expressly stated in the License.
*******************************************************************************/

/*
 The example below shows how to use the functions:
     ippsEncodeLZ4HashTableGetSize_8u
     ippsEncodeLZ4HashTableInit_8u
     ippsEncodeLZ4Dict_8u
     ippsDecodeLZ4_8u
*/

#include <stdio.h>
#include <string.h>

#include <ipp.h>

/* Next two defines are created to simplify code reading and understanding */
#define EXIT_MAIN       exitLine:                              /* Label for Exit */
#define check_sts(st)   if((st) != ippStsNoErr) goto exitLine; /* Go to Exit if Intel(R) Integrated Primitives (Intel(R) IPP) function returned status different from ippStsNoErr */

#define TEST_SIZE   (1024 * 4)
#define CHUNK_SIZE  (512)
#define RATIO       (TEST_SIZE / CHUNK_SIZE)

static int Verify(const Ipp8u* comprBuf, int comprLen, Ipp8u* decomprBuf,
                  int maxDecomprLen, const Ipp8u* srcBuf, int srcLen, const Ipp8u* dictBuf);

int main(void)
{
    Ipp8u       *srcBuf = NULL, *comprBuf = NULL, *decomprBuf = NULL, *hashTable = NULL;
    Ipp8u       *comprPtr, *dict;
    IppStatus   st = ippStsNoErr;
    int         hashSize = 0,
                maxChunkCompr = CHUNK_SIZE + CHUNK_SIZE / 255 + 16,
                comprLen = (maxChunkCompr) * RATIO, /* Extra bytes for uncompressible
                                                                                data */
                decomprLen = TEST_SIZE + 33;
    int         destLen;
    int         i;

    srcBuf      = ippsMalloc_8u(TEST_SIZE);
    decomprBuf  = ippsMalloc_8u(decomprLen); /* Spare bytes for "wild" (non-safe) 
                                                                        decompression */
    comprBuf    = ippsMalloc_8u(comprLen);
    /* Initialize source buffer */
    check_sts(  st = ippsVectorJaehne_8u(srcBuf, TEST_SIZE, IPP_MAX_8U) )
    for(i = 0; i < TEST_SIZE; i++)
        srcBuf[i] >>= 6;                    /* Decrease source data entropy */
    /* Allocate and initialize hash table */
    check_sts(  st = ippsEncodeLZ4HashTableGetSize_8u(&hashSize)    )
    hashTable   = ippsMalloc_8u(hashSize);
    check_sts(  st = ippsEncodeLZ4HashTableInit_8u(hashTable, TEST_SIZE)    )
    /* Compress source data using chunks without dictionary*/
    comprPtr = comprBuf;
    for(i = 0; i < RATIO; i++)
    {
        destLen = maxChunkCompr;
        check_sts(  st = ippsEncodeLZ4_8u(srcBuf + CHUNK_SIZE * i, CHUNK_SIZE, 
                                          comprPtr + 2, &destLen, hashTable)    )
        *comprPtr++   = destLen & 0xFF;
        *comprPtr++   = (destLen >> 8) & 0xFF;
        comprPtr += destLen;
    }
    /* Print compression result */
    comprLen = (int)(comprPtr - comprBuf);
    printf("Compression w/o dictionary: %d bytes compressed into %d bytes.", TEST_SIZE,
           comprLen);
    if(!Verify(comprBuf, comprLen, decomprBuf, decomprLen, srcBuf, TEST_SIZE, NULL))
    {
        printf("Verification failed!\n");
        st = ippStsErr;
        goto exitLine;
    }
    else
        printf("Verified.\n");

    /* Compress source data with dictionary*/
    comprPtr = comprBuf;
    dict = srcBuf;
    for(i = 0; i < RATIO; i++)
    {
        int     srcIdx, dictLen;
        destLen = maxChunkCompr;
        srcIdx = CHUNK_SIZE * i;
        dictLen = CHUNK_SIZE * i;           /* All previous chunks as dictionary */

        check_sts(  st = ippsEncodeLZ4Dict_8u(srcBuf + CHUNK_SIZE * i, srcIdx, CHUNK_SIZE,
                                              comprPtr + 2, &destLen, hashTable, dict, 
                                              dictLen)  )
        *comprPtr++   = destLen & 0xFF;
        *comprPtr++   = (destLen >> 8) & 0xFF;
        comprPtr += destLen;
    }
    /* Print compression result */
    comprLen = (int)(comprPtr - comprBuf);
    printf("Compression with dictionary: %d bytes compressed into %d bytes.", TEST_SIZE,
           comprLen);
    if(!Verify(comprBuf, comprLen, decomprBuf, decomprLen, srcBuf, TEST_SIZE, dict))
    {
        printf("Verification failed!\n");
        st = ippStsErr;
        goto exitLine;
    }
    else
        printf("Verified.\n");

EXIT_MAIN
    if(st != ippStsNoErr)
        printf("Function status: %s\n", ippGetStatusString(st));
    ippsFree(srcBuf);
    ippsFree(comprBuf);
    ippsFree(decomprBuf);
    ippsFree(hashTable);
    return (int)st;
}

static int Verify(const Ipp8u* comprBuf, int comprLen, Ipp8u* decomprBuf,
                  int maxDecomprLen, const Ipp8u* srcBuf, int srcLen, const Ipp8u* dictBuf)
{
    const Ipp8u* locPtr = comprBuf;
    Ipp8u* decomprPtr = decomprBuf;
    IppStatus st;
    int decomprLen = 0;
    int dictLen    = 0;

    while((int)(locPtr - comprBuf) < comprLen)
    {
        int nextInLen;
        int nextOutLen;

        nextInLen = (*locPtr++);
        nextInLen +=  (*locPtr++) << 8;
        nextOutLen = maxDecomprLen - decomprLen;
        st = ippsDecodeLZ4Dict_8u(locPtr, &nextInLen, decomprPtr, dictLen, &nextOutLen, dictBuf, dictLen);
        if(st != ippStsNoErr) return 0;
        locPtr += nextInLen;
        decomprLen += nextOutLen;
        dictLen    += nextOutLen;
    }
    if(decomprLen != srcLen || memcmp(decomprBuf, srcBuf, srcLen) != 0)
        return 0;
    return 1;
}

LZ4Safe:

/*******************************************************************************
* Copyright 2017 Intel Corporation.
*
*
* This software and the related documents are Intel copyrighted materials, and your use of them is governed by
* the express license under which they were provided to you ('License'). Unless the License provides otherwise,
* you may not use, modify, copy, publish, distribute, disclose or transmit this software or the related
* documents without Intel's prior written permission.
* This software and the related documents are provided as is, with no express or implied warranties, other than
* those that are expressly stated in the License.
*******************************************************************************/

/*
 The example below shows how to use the functions:
     ippsEncodeLZ4HashTableGetSize_8u
     ippsEncodeLZ4HashTableInit_8u
     ippsEncodeLZ4Safe_8u
*/

#include <stdio.h>
#include <string.h>

#include <ipp/ippdc.h>
#include <ipp/ipps.h>

/* Next two defines are created to simplify code reading and understanding */
#define EXIT_MAIN       exitLine:                              /* Label for Exit */
#define check_sts(st)   if((st) != ippStsNoErr) goto exitLine; /* Go to Exit if Intel(R) Integrated Primitives (Intel(R) IPP) function returned status different from ippStsNoErr */

#define TEST_SIZE (1024)
#define COMPR_LEN (TEST_SIZE / 20)

int main(void)
{
    Ipp8u       *srcBuf = NULL, *comprBuf = NULL, *hashTable = NULL;
    IppStatus   st = ippStsNoErr;
    int         i, hashSize, remBytes, srcLen, dstLen;

    srcBuf      = ippsMalloc_8u(TEST_SIZE);
    comprBuf    = ippsMalloc_8u(COMPR_LEN);
    /* Initialize source buffer */
    check_sts(      st = ippsVectorJaehne_8u(srcBuf, TEST_SIZE, IPP_MAX_8U)         )
    for(i = 0; i < TEST_SIZE; i++)
        srcBuf[i] >>= 6;                    /* Decrease source data entropy */
    /* Init and allocate hash table */
    check_sts(  st = ippsEncodeLZ4HashTableGetSize_8u(&hashSize)    )
    hashTable   = ippsMalloc_8u(hashSize);
    check_sts(  st = ippsEncodeLZ4HashTableInit_8u(hashTable, TEST_SIZE)    )
    /* Compress source data */
    remBytes = TEST_SIZE;
    while(remBytes > 0)
    {
        srcLen = remBytes;
        dstLen = COMPR_LEN;
        /* Compressing into knowingly small buffer */
        st = ippsEncodeLZ4Safe_8u((const Ipp8u*)srcBuf + TEST_SIZE - remBytes, &srcLen,
                                  comprBuf, &dstLen, hashTable);
        if(st == ippStsDstSizeLessExpected)
        {
            /* Print compression result */
            printf("Compression: %d bytes compressed into %d bytes\n", srcLen, dstLen);
        }
        else if(st < ippStsNoErr)
        {
            printf("Error in compression function\n");
            break;
        }
        remBytes -= srcLen;
    }
EXIT_MAIN
    ippsFree(srcBuf);
    ippsFree(comprBuf);
    ippsFree(hashTable);
    return (int)st;
}