<commit_before>/**
* Runtime CPU detection
* (C) 2009 <NAME>
*
* Distributed under the terms of the Botan license
*/

#include <botan/cpuid.h>
#include <botan/types.h>
#include <botan/loadstor.h>
#include <botan/mem_ops.h>

#include <stdio.h>

#if defined(_MSC_VER)
  #include <intrin.h>
#endif

namespace Botan {

namespace {

void x86_cpuid(u32bit type, u32bit out[4])
   {
   out[0] = out[1] = out[2] = out[3] = 0;

#if defined(BOTAN_BUILD_COMPILER_IS_GCC)

#if defined(BOTAN_TARGET_ARCH_IS_X86)

   asm("pushq %%ebx; cpuid; mov %%ebx, %%edi; popq %%ebx"
       : "=a" (out[0]), "=D" (out[1]), "=c" (out[2]), "=d" (out[3])
       : "0" (type));

#elif defined(BOTAN_TARGET_ARCH_IS_AMD64)

   asm("pushq %%rbx; cpuid; mov %%ebx, %%edi; popq %%rbx"
       : "=a" (out[0]), "=D" (out[1]), "=c" (out[2]), "=d" (out[3])
       : "0" (type));
#endif

#elif defined(BOTAN_BUILD_COMPILER_IS_MSVC)
   __cpuid(out, type);
#endif
   }

u32bit get_x86_cache_line_size()
   {
   const u32bit INTEL_CPUID[3] = { 0x756E6547, 0x6C65746E, 0x49656E69 };
   const u32bit AMD_CPUID[3] = { 0x68747541, 0x444D4163, 0x69746E65 };

   u32bit cpuid[4] = { 0 };
   x86_cpuid(0, cpuid);

   if(same_mem(cpuid + 1, INTEL_CPUID, 3) && cpuid[0] > 2)
      {
      x86_cpuid(1, cpuid);
      return 8 * get_byte(2, cpuid[1]);
      }

   if(same_mem(cpuid + 1, AMD_CPUID, 3))
      {
      x86_cpuid(0x80000005, cpuid);
      return get_byte(3, cpuid[2]);
      }

   return 32; // default cache line guess
   }

}

/*
* Call the x86 CPUID instruction and return the contents of ecx and
* edx, which contain the feature masks.
*/
u64bit CPUID::x86_processor_flags()
   {
   static u64bit proc_flags = 0;

   if(proc_flags)
      return proc_flags;

   u32bit cpuid[4] = { 0 };
   x86_cpuid(1, cpuid);

   // Set the FPU bit on to force caching in proc_flags
   proc_flags = ((u64bit)cpuid[2] << 32) | cpuid[3] | 1;

   return proc_flags;
   }

u32bit CPUID::cache_line_size()
   {
   static u32bit cl_size = 0;

   if(cl_size)
      return cl_size;

   cl_size = get_x86_cache_line_size();

   return cl_size;
   }

}<commit_msg>Clean up cpuid calling<commit_after>/**
* Runtime CPU detection
* (C) 2009 <NAME>
*
* Distributed under the terms of the Botan license
*/

#include <botan/cpuid.h>
#include <botan/types.h>
#include <botan/loadstor.h>
#include <botan/mem_ops.h>

#if defined(BOTAN_TARGET_ARCH_IS_X86) || defined(BOTAN_TARGET_ARCH_IS_AMD64)

#if defined(BOTAN_BUILD_COMPILER_IS_MSVC)

  #include <intrin.h>
  #define CALL_CPUID(type, out) do { __cpuid(out, type) } while(0)

#elif defined(BOTAN_BUILD_COMPILER_IS_ICC)

  #include <ia32intrin.h>
  #define CALL_CPUID(type, out) do { __cpuid(out, type) } while(0);

#elif defined(BOTAN_BUILD_COMPILER_IS_GCC)

  #include <cpuid.h>
  #define CALL_CPUID(type, out) \
    do { __get_cpuid(type, out, out+1, out+2, out+3); } while(0);

#endif

#else
  // In all other cases, just zeroize the supposed cpuid output
  #define CALL_CPUID(type, out) out[0] = out[1] = out[2] = out[3] = 0;
#endif

namespace Botan {

namespace {

u32bit get_x86_cache_line_size()
   {
   const u32bit INTEL_CPUID[3] = { 0x756E6547, 0x6C65746E, 0x49656E69 };
   const u32bit AMD_CPUID[3] = { 0x68747541, 0x444D4163, 0x69746E65 };

   u32bit cpuid[4] = { 0 };
   CALL_CPUID(0, cpuid);

   if(same_mem(cpuid + 1, INTEL_CPUID, 3))
      {
      CALL_CPUID(1, cpuid);
      return 8 * get_byte(2, cpuid[1]);
      }
   else if(same_mem(cpuid + 1, AMD_CPUID, 3))
      {
      CALL_CPUID(0x80000005, cpuid);
      return get_byte(3, cpuid[2]);
      }
   else
      return 32; // default cache line guess
   }

}

/*
* Call the x86 CPUID instruction and return the contents of ecx and
* edx, which contain the feature masks.
*/
u64bit CPUID::x86_processor_flags()
   {
   static u64bit proc_flags = 0;

   if(proc_flags)
      return proc_flags;

   u32bit cpuid[4] = { 0 };
   CALL_CPUID(1, cpuid);

   // Set the FPU bit on to force caching in proc_flags
   proc_flags = ((u64bit)cpuid[2] << 32) | cpuid[3] | 1;

   return proc_flags;
   }

u32bit CPUID::cache_line_size()
   {
   static u32bit cl_size = 0;

   if(cl_size)
      return cl_size;

   cl_size = get_x86_cache_line_size();

   return cl_size;
   }

}
{'fraction_non_alphanumeric': 0.14356905552918842, 'fraction_numerical': 0.06763170384432843, 'mean_word_length': 3.4462025316455698, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '191842', 'n_tokens_mistral': 1992, 'n_tokens_neox': 1789, 'n_words': 396}