<commit_before>/* simd.cc
   <NAME>, 21 February 2007
   Copyright (c) 2007 <NAME>.  All rights reserved.

   Implementation of SIMD code.
*/

#include "simd.h"
#include <boost/tuple/tuple.hpp>
#include "jml/arch/exception.h"
#include <iostream>
#include <iomanip>

using namespace std;


namespace ML {

#if defined __i686__ || defined __amd64__

namespace {

enum {
    CPUID_VENDOR_ID = 0,
    CPUID_LEVEL = 0,
    CPUID_FEATURES = 1,
    CPUID_CACHE_INFO = 2,
    CPUID_SERIAL_NUMBER = 3,
    CPUID_EXT_CACHE_INFO = 4,
    CPUID_MONITOR_MWAIT = 5,
    CPUID_THERMAL_POWER = 6,
    CPUID_DCA_ACCESS = 7,
    CPUID_EXT_LEVEL =      0x80000000,
    CPUID_EXT_FEATURES =   0x80000001,
    CPUID_EXT_BRAND1 =     0x80000002,
    CPUID_EXT_BRAND2 =     0x80000003,
    CPUID_EXT_BRAND3 =     0x80000004,
    CPUID_EXT_L1CACHE =    0x80000005,
    CPUID_EXT_L2CACHE =    0x80000006,
    CPUID_EXT_APM_INFO =   0x80000007,
    CPUID_EXT_ADDR_SIZES = 0x80000008,
    CPUID_EXT_SVM_INFO =   0x8000000A
};

struct Regs {
    uint32_t eax, ebx, ecx, edx;
};

Regs
cpuid(uint32_t request, uint32_t ecx = 0)
{
    Regs result = {0, 0, 0, 0};
    asm volatile
        (
#if defined(__i686__)         
         "push %%ebx\n\t"
         "push %%edx\n\t"
#else
         "push %%rbx\n\t"
         "push %%rdx\n\t"
#endif
         "cpuid\n\t"
         "mov  %%eax,  0(%[addr])\n\t"
         "mov  %%ebx,  4(%[addr])\n\t"
         "mov  %%ecx,  8(%[addr])\n\t"
         "mov  %%edx, 12(%[addr])\n\t"
#if defined(__i686__)         
         "pop  %%edx\n\t"
         "pop  %%ebx\n\t"
#else
         "pop  %%rdx\n\t"
         "pop  %%rbx\n\t"
#endif
         : 
         : "a" (request), "c" (ecx), [addr] "S" (&result)
         );
    return result;
}

} // file scope

uint32_t cpuid_flags()
{
    return cpuid(1).edx;
}

namespace {

std::string to_ascii(uint32_t x)
{
    std::string result(4, ' ');
    for (unsigned i = 0;  i < 4;  ++i)
        result[i] = x >> (i * 8);
    return result;
}

} // file scope

std::string vendor_id()
{
    Regs r = cpuid(CPUID_VENDOR_ID);
    return to_ascii(r.ebx) + to_ascii(r.edx) + to_ascii(r.ecx);
}<commit_msg>fixed stack problem using CPUID function<commit_after>/* simd.cc
   <NAME>, 21 February 2007
   Copyright (c) 2007 <NAME>.  All rights reserved.

   Implementation of SIMD code.
*/

#include "simd.h"
#include <boost/tuple/tuple.hpp>
#include "jml/arch/exception.h"
#include <iostream>
#include <iomanip>

using namespace std;


namespace ML {

#if defined __i686__ || defined __amd64__

namespace {

enum {
    CPUID_VENDOR_ID = 0,
    CPUID_LEVEL = 0,
    CPUID_FEATURES = 1,
    CPUID_CACHE_INFO = 2,
    CPUID_SERIAL_NUMBER = 3,
    CPUID_EXT_CACHE_INFO = 4,
    CPUID_MONITOR_MWAIT = 5,
    CPUID_THERMAL_POWER = 6,
    CPUID_DCA_ACCESS = 7,
    CPUID_EXT_LEVEL =      0x80000000,
    CPUID_EXT_FEATURES =   0x80000001,
    CPUID_EXT_BRAND1 =     0x80000002,
    CPUID_EXT_BRAND2 =     0x80000003,
    CPUID_EXT_BRAND3 =     0x80000004,
    CPUID_EXT_L1CACHE =    0x80000005,
    CPUID_EXT_L2CACHE =    0x80000006,
    CPUID_EXT_APM_INFO =   0x80000007,
    CPUID_EXT_ADDR_SIZES = 0x80000008,
    CPUID_EXT_SVM_INFO =   0x8000000A
};

struct Regs {
    uint32_t eax, ebx, ecx, edx;
};

Regs
cpuid(uint32_t request, uint32_t ecx = 0)
{
    Regs result = {0, 0, 0, 0};
    asm volatile
        (
#if defined(__i686__)         
         "sub  $0x40,  %%esp\n\t"
         "push %%ebx\n\t"
         "push %%edx\n\t"
#else
         "sub  $0x40,  %%rsp\n\t"
         "push %%rbx\n\t"
         "push %%rdx\n\t"
#endif
         "cpuid\n\t"

         "mov  %%eax,  0(%[addr])\n\t"
         "mov  %%ebx,  4(%[addr])\n\t"
         "mov  %%ecx,  8(%[addr])\n\t"
         "mov  %%edx, 12(%[addr])\n\t"
#if defined(__i686__)         
         "pop  %%edx\n\t"
         "pop  %%ebx\n\t"
         "add  $0x40,   %%esp\n\t"
#else
         "pop  %%rdx\n\t"
         "pop  %%rbx\n\t"
         "add  $0x40,   %%rsp\n\t"
#endif
         : "+a" (request), "+c" (ecx)
         : [addr] "S" (&result)
         : "cc", "memory"
         );
    return result;
}

} // file scope

uint32_t cpuid_flags()
{
    return cpuid(1).edx;
}

namespace {

std::string to_ascii(uint32_t x)
{
    std::string result(4, ' ');
    for (unsigned i = 0;  i < 4;  ++i)
        result[i] = x >> (i * 8);
    return result;
}

} // file scope

std::string vendor_id()
{
    Regs r = cpuid(CPUID_VENDOR_ID);
    return to_ascii(r.ebx) + to_ascii(r.edx) + to_ascii(r.ecx);
}
{'fraction_non_alphanumeric': 0.14659803043867503, 'fraction_numerical': 0.0693822739480752, 'mean_word_length': 2.2525473071324598, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '2049736', 'n_tokens_mistral': 2162, 'n_tokens_neox': 1934, 'n_words': 379}