<commit_before>/*
 *------------------------------------------------------------------
 * Copyright (c) 2019 Cisco and/or its affiliates.
 * 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.
 *------------------------------------------------------------------
 */

#include <vlib/vlib.h>
#include <vnet/plugin/plugin.h>
#include <vnet/crypto/crypto.h>
#include <x86intrin.h>
#include <crypto_ia32/crypto_ia32.h>
#include <crypto_ia32/aesni.h>
#include <crypto_ia32/ghash.h>

#if __GNUC__ > 4  && !__clang__ && CLIB_DEBUG == 0
#pragma GCC optimize ("O3")
#endif

typedef struct
{
  /* pre-calculated hash key values */
  const __m128i Hi[8];
  /* extracted AES key */
  const __m128i Ke[15];
} aes_gcm_key_data_t;

static const __m128i last_byte_one = { 0, 1ULL << 56 };
static const __m128i zero = { 0, 0 };

static const u8x16 bswap_mask = {
  15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
};

static const u8x16 byte_mask_scale = {
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
};

static_always_inline __m128i
aesni_gcm_bswap (__m128i x)
{
  return _mm_shuffle_epi8 (x, (__m128i) bswap_mask);
}

static_always_inline __m128i
aesni_gcm_byte_mask (__m128i x, u8 n_bytes)
{
  u8x16 mask = u8x16_is_greater (u8x16_splat (n_bytes), byte_mask_scale);

  return _mm_blendv_epi8 (zero, x, (__m128i) mask);
}

static_always_inline __m128i
aesni_gcm_load_partial (__m128i * p, int n_bytes)
{
#ifdef __AVX512F__
  return _mm_mask_loadu_epi8 (zero, (1 << n_bytes) - 1, p);
#else
  return aesni_gcm_byte_mask (_mm_loadu_si128 (p), n_bytes);
#endif
}

static_always_inline void
aesni_gcm_store_partial (void *p, __m128i r, int n_bytes)
{
#ifdef x__AVX512F__
  _mm_mask_storeu_epi8 (p, (1 << n_bytes) - 1, r);
#else
  u8x16 mask = u8x16_is_greater (u8x16_splat (n_bytes), byte_mask_scale);
  _mm_maskmoveu_si128 (r, (__m128i) mask, p);
#endif
}

static_always_inline void<commit_msg>crypto-ia32: fix typo in ifdef for avx512 version<commit_after>/*
 *------------------------------------------------------------------
 * Copyright (c) 2019 Cisco and/or its affiliates.
 * 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.
 *------------------------------------------------------------------
 */

#include <vlib/vlib.h>
#include <vnet/plugin/plugin.h>
#include <vnet/crypto/crypto.h>
#include <x86intrin.h>
#include <crypto_ia32/crypto_ia32.h>
#include <crypto_ia32/aesni.h>
#include <crypto_ia32/ghash.h>

#if __GNUC__ > 4  && !__clang__ && CLIB_DEBUG == 0
#pragma GCC optimize ("O3")
#endif

typedef struct
{
  /* pre-calculated hash key values */
  const __m128i Hi[8];
  /* extracted AES key */
  const __m128i Ke[15];
} aes_gcm_key_data_t;

static const __m128i last_byte_one = { 0, 1ULL << 56 };
static const __m128i zero = { 0, 0 };

static const u8x16 bswap_mask = {
  15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
};

static const u8x16 byte_mask_scale = {
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
};

static_always_inline __m128i
aesni_gcm_bswap (__m128i x)
{
  return _mm_shuffle_epi8 (x, (__m128i) bswap_mask);
}

static_always_inline __m128i
aesni_gcm_byte_mask (__m128i x, u8 n_bytes)
{
  u8x16 mask = u8x16_is_greater (u8x16_splat (n_bytes), byte_mask_scale);

  return _mm_blendv_epi8 (zero, x, (__m128i) mask);
}

static_always_inline __m128i
aesni_gcm_load_partial (__m128i * p, int n_bytes)
{
#ifdef __AVX512F__
  return _mm_mask_loadu_epi8 (zero, (1 << n_bytes) - 1, p);
#else
  return aesni_gcm_byte_mask (_mm_loadu_si128 (p), n_bytes);
#endif
}

static_always_inline void
aesni_gcm_store_partial (void *p, __m128i r, int n_bytes)
{
#ifdef __AVX512F__
  _mm_mask_storeu_epi8 (p, (1 << n_bytes) - 1, r);
#else
  u8x16 mask = u8x16_is_greater (u8x16_splat (n_bytes), byte_mask_scale);
  _mm_maskmoveu_si128 (r, (__m128i) mask, p);
#endif
}

static_always_inline void
{'fraction_non_alphanumeric': 0.16199376947040497, 'fraction_numerical': 0.06791277258566979, 'mean_word_length': 3.374205267938238, 'pattern_counts': {'":': 0, '<': 29, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1765185', 'n_tokens_mistral': 2054, 'n_tokens_neox': 1798, 'n_words': 534}