<commit_before>/*-----------------------------------------------------------*/
/* Block Sorting, Lossless Data Compression Library.         */
/* Quantized Local Frequency Coding functions                */
/*-----------------------------------------------------------*/

/*--

This file is a part of bsc and/or libbsc, a program and a library for
lossless, block-sorting data compression.

   Copyright (c) 2009-2021 <NAME> <<EMAIL>>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

Please see the file LICENSE for full copyright information and file AUTHORS
for full list of contributors.

See also the bsc and libbsc web site:
  http://libbsc.com/ for more information.

--*/

#include <stdlib.h>
#include <memory.h>

#include "qlfc.h"

#include "../../libbsc.h"
#include "../../platform/platform.h"

#include "../common/rangecoder.h"
#include "../common/tables.h"
#include "../common/predictor.h"

#include "qlfc_model.h"

int bsc_qlfc_init(int features)
{
    return bsc_qlfc_init_static_model();
}

#if (defined(__SSE2__) || defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP >= 2)) && (defined(__clang__) || defined(_MSC_VER))

unsigned char * bsc_qlfc_transform(const unsigned char * RESTRICT input, unsigned char * RESTRICT buffer, int n, unsigned char * RESTRICT MTFTable)
{
#if defined(_MSC_VER)
    __declspec(align(32)) 
#endif
    signed char ranks[ALPHABET_SIZE];

#if defined(_MSC_VER)
    __declspec(align(32))
#endif
    signed char flags[ALPHABET_SIZE];

    for (ptrdiff_t i = 0; i < ALPHABET_SIZE; ++i) { ranks[i] = i - 128; }
    for (ptrdiff_t i = 0; i < ALPHABET_SIZE; ++i) { flags[i] = 0; }

    ptrdiff_t index = n; signed char nSymbols = -128;
    for (ptrdiff_t i = (ptrdiff_t)n - 1; i >= 0;)
    {
        unsigned char currentChar = input[i--];
        for (; (i >= 0) && (input[i] == currentChar); --i);

        signed char rank = ranks[currentChar];
        for (int t = 0 * 32; t < 1 * 32; ++t) { ranks[t] -= (ranks[t] < rank ? -1 : 0); }
        for (int t = 1 * 32; t < 2 * 32; ++t) { ranks[t] -= (ranks[t] < rank ? -1 : 0); }
        for (int t = 2 * 32; t < 3 * 32; ++t) { ranks[t] -= (ranks[t] < rank ? -1 : 0); }
        for (int t = 3 * 32; t < 4 * 32; ++t) { ranks[t] -= (ranks[t] < rank ? -1 : 0); }
        for (int t = 4 * 32; t < 5 * 32; ++t) { ranks[t] -= (ranks[t] < rank ? -1 : 0); }
        for (int t = 5 * 32; t < 6 * 32; ++t) { ranks[t] -= (ranks[t] < rank ? -1 : 0); }
        for (int t = 6 * 32; t < 7 * 32; ++t) { ranks[t] -= (ranks[t] < rank ? -1 : 0); }
        for (int t = 7 * 32; t < 8 * 32; ++t) { ranks[t] -= (ranks[t] < rank ? -1 : 0); }
        ranks[currentChar] = -128;

        if (flags[currentChar] == 0) { flags[currentChar] = 1; rank = nSymbols++; }

        buffer[--index] = rank + 128;
    }

    buffer[n - 1] = 1;

    for (ptrdiff_t i = 0; i < ALPHABET_SIZE; ++i) { MTFTable[ranks[i] + 128] = i; }
    for (ptrdiff_t i = 1; i < ALPHABET_SIZE; ++i) { if (flags[MTFTable[i]] == 0) { MTFTable[i] = MTFTable[i - 1]; break; } }

    return buffer + index;
}

#else

unsigned char * bsc_qlfc_transform(const unsigned char * RESTRICT input, unsigned char * RESTRICT buffer, int n, unsigned char * RESTRICT MTFTable)
{<commit_msg>bsc 3.1.3 (compiler warnings fixes)<commit_after>/*-----------------------------------------------------------*/
/* Block Sorting, Lossless Data Compression Library.         */
/* Quantized Local Frequency Coding functions                */
/*-----------------------------------------------------------*/

/*--

This file is a part of bsc and/or libbsc, a program and a library for
lossless, block-sorting data compression.

   Copyright (c) 2009-2021 <NAME> <<EMAIL>>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

Please see the file LICENSE for full copyright information and file AUTHORS
for full list of contributors.

See also the bsc and libbsc web site:
  http://libbsc.com/ for more information.

--*/

#include <stdlib.h>
#include <memory.h>

#include "qlfc.h"

#include "../../libbsc.h"
#include "../../platform/platform.h"

#include "../common/rangecoder.h"
#include "../common/tables.h"
#include "../common/predictor.h"

#include "qlfc_model.h"

int bsc_qlfc_init(int features)
{
    return bsc_qlfc_init_static_model();
}

#if (defined(__SSE2__) || defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP >= 2)) && (defined(__clang__) || defined(_MSC_VER))

unsigned char * bsc_qlfc_transform(const unsigned char * RESTRICT input, unsigned char * RESTRICT buffer, int n, unsigned char * RESTRICT MTFTable)
{
#if defined(_MSC_VER)
    __declspec(align(32)) 
#endif
    signed char ranks[ALPHABET_SIZE];

#if defined(_MSC_VER)
    __declspec(align(32))
#endif
    signed char flags[ALPHABET_SIZE];

    for (ptrdiff_t i = 0; i < ALPHABET_SIZE; ++i) { ranks[i] = (signed char)(i - 128); }
    for (ptrdiff_t i = 0; i < ALPHABET_SIZE; ++i) { flags[i] = 0; }

    ptrdiff_t index = n; signed char nSymbols = -128;
    for (ptrdiff_t i = (ptrdiff_t)n - 1; i >= 0;)
    {
        unsigned char currentChar = input[i--];
        for (; (i >= 0) && (input[i] == currentChar); --i);

        signed char rank = ranks[currentChar];
        for (int t = 0 * 32; t < 1 * 32; ++t) { ranks[t] -= (ranks[t] < rank ? -1 : 0); }
        for (int t = 1 * 32; t < 2 * 32; ++t) { ranks[t] -= (ranks[t] < rank ? -1 : 0); }
        for (int t = 2 * 32; t < 3 * 32; ++t) { ranks[t] -= (ranks[t] < rank ? -1 : 0); }
        for (int t = 3 * 32; t < 4 * 32; ++t) { ranks[t] -= (ranks[t] < rank ? -1 : 0); }
        for (int t = 4 * 32; t < 5 * 32; ++t) { ranks[t] -= (ranks[t] < rank ? -1 : 0); }
        for (int t = 5 * 32; t < 6 * 32; ++t) { ranks[t] -= (ranks[t] < rank ? -1 : 0); }
        for (int t = 6 * 32; t < 7 * 32; ++t) { ranks[t] -= (ranks[t] < rank ? -1 : 0); }
        for (int t = 7 * 32; t < 8 * 32; ++t) { ranks[t] -= (ranks[t] < rank ? -1 : 0); }
        ranks[currentChar] = -128;

        if (flags[currentChar] == 0) { flags[currentChar] = 1; rank = nSymbols++; }

        buffer[--index] = rank + 128;
    }

    buffer[n - 1] = 1;

    for (ptrdiff_t i = 0; i < ALPHABET_SIZE; ++i) { MTFTable[ranks[i] + 128] = (unsigned char)i; }
    for (ptrdiff_t i = 1; i < ALPHABET_SIZE; ++i) { if (flags[MTFTable[i]] == 0) { MTFTable[i] = MTFTable[i - 1]; break; } }

    return buffer + index;
}

#else

unsigned char * bsc_qlfc_transform(const unsigned char * RESTRICT input, unsigned char * RESTRICT buffer, int n, unsigned char * RESTRICT MTFTable)
{
{'fraction_non_alphanumeric': 0.18585966791644348, 'fraction_numerical': 0.03173540439207284, 'mean_word_length': 2.5214521452145213, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3485344', 'n_tokens_mistral': 2818, 'n_tokens_neox': 2584, 'n_words': 854}