<commit_before>#pragma once

#include <immintrin.h>

namespace pthash::util {

template <typename T>
inline void prefetch(T const* ptr) {
    _mm_prefetch(reinterpret_cast<const char*>(ptr), _MM_HINT_T0);
}

inline uint8_t msb(uint64_t x) {
    assert(x);
    unsigned long ret = -1U;
    if (x) { ret = (unsigned long)(63 - __builtin_clzll(x)); }
    return (uint8_t)ret;
}

inline bool bsr64(unsigned long* const index, const uint64_t mask) {
    if (mask) {
        *index = (unsigned long)(63 - __builtin_clzll(mask));
        return true;
    } else {
        return false;
    }
}

inline uint8_t msb(uint64_t x, unsigned long& ret) {
    return bsr64(&ret, x);
}

inline uint8_t lsb(uint64_t x, unsigned long& ret) {
    if (x) {
        ret = (unsigned long)__builtin_ctzll(x);
        return true;
    }
    return false;
}

inline uint8_t lsb(uint64_t x) {
    assert(x);
    unsigned long ret = -1U;
    lsb(x, ret);
    return (uint8_t)ret;
}

inline uint64_t popcount(uint64_t x) {
    return static_cast<uint64_t>(_mm_popcnt_u64(x));
}

inline uint64_t select64_pdep_tzcnt(uint64_t x, const uint64_t k) {
    uint64_t i = 1ULL << k;
    asm("pdep %[x], %[mask], %[x]" : [x] "+r"(x) : [mask] "r"(i));
    asm("tzcnt %[bit], %[index]" : [index] "=r"(i) : [bit] "g"(x) : "cc");
    return i;
}

inline uint64_t select_in_word(const uint64_t x, const uint64_t k) {
    assert(k < popcount(x));
    return select64_pdep_tzcnt(x, k);
}

}  // namespace pthash::util<commit_msg>Fix compilation and SIGILL on Intel Ivy Bridge<commit_after>#pragma once

#if defined(__x86_64__)
#include <immintrin.h>
#endif

namespace pthash::util {

template <typename T>
inline void prefetch(T const* ptr) {
#if defined(__x86_64__)
    _mm_prefetch(reinterpret_cast<const char*>(ptr), _MM_HINT_T0);
#endif
}

inline uint8_t msb(uint64_t x) {
    assert(x);
    unsigned long ret = -1U;
    if (x) { ret = (unsigned long)(63 - __builtin_clzll(x)); }
    return (uint8_t)ret;
}

inline bool bsr64(unsigned long* const index, const uint64_t mask) {
    if (mask) {
        *index = (unsigned long)(63 - __builtin_clzll(mask));
        return true;
    } else {
        return false;
    }
}

inline uint8_t msb(uint64_t x, unsigned long& ret) {
    return bsr64(&ret, x);
}

inline uint8_t lsb(uint64_t x, unsigned long& ret) {
    if (x) {
        ret = (unsigned long)__builtin_ctzll(x);
        return true;
    }
    return false;
}

inline uint8_t lsb(uint64_t x) {
    assert(x);
    unsigned long ret = -1U;
    lsb(x, ret);
    return (uint8_t)ret;
}

inline uint64_t popcount(uint64_t x) {
#ifdef __SSE4_2__
    return static_cast<uint64_t>(_mm_popcnt_u64(x));
#else
    return std::popcount(x);
#endif
}

inline uint64_t select64(uint64_t x, uint64_t k) {
#ifndef __haswell__
    // Modified from: Bit Twiddling Hacks
    // https://graphics.stanford.edu/~seander/bithacks.html#SelectPosFromMSBRank
    unsigned int s;      // Output: Resulting position of bit with rank r [1-64]
    uint64_t a, b, c, d; // Intermediate temporaries for bit count.
    unsigned int t;      // Bit count temporary.
    k = popcount(x) - k;

    a =  x - ((x >> 1) & ~0UL/3);
    b = (a & ~0UL/5) + ((a >> 2) & ~0UL/5);
    c = (b + (b >> 4)) & ~0UL/0x11;
    d = (c + (c >> 8)) & ~0UL/0x101;
    t = (d >> 32) + (d >> 48);
    s  = 64;
    s -= ((t - k) & 256) >> 3; k -= (t & ((t - k) >> 8));
    t  = (d >> (s - 16)) & 0xff;
    s -= ((t - k) & 256) >> 4; k -= (t & ((t - k) >> 8));
    t  = (c >> (s - 8)) & 0xf;
    s -= ((t - k) & 256) >> 5; k -= (t & ((t - k) >> 8));
    t  = (b >> (s - 4)) & 0x7;
    s -= ((t - k) & 256) >> 6; k -= (t & ((t - k) >> 8));
    t  = (a >> (s - 2)) & 0x3;
    s -= ((t - k) & 256) >> 7; k -= (t & ((t - k) >> 8));
    t  = (x >> (s - 1)) & 0x1;
    s -= ((t - k) & 256) >> 8;
    return s - 1;
#else
    uint64_t i = 1ULL << k;
    asm("pdep %[x], %[mask], %[x]" : [x] "+r"(x) : [mask] "r"(i));
    asm("tzcnt %[bit], %[index]" : [index] "=r"(i) : [bit] "g"(x) : "cc");
    return i;
#endif
}

inline uint64_t select_in_word(const uint64_t x, const uint64_t k) {
    assert(k < popcount(x));
    return select64(x, k);
}

}  // namespace pthash::util
{'fraction_non_alphanumeric': 0.1813819577735125, 'fraction_numerical': 0.046065259117082535, 'mean_word_length': 2.7423698384201076, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 55, 'https://': 1, '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': '7188467', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1740, 'n_words': 455}