<commit_before>/*
 * kmp_utility.c -- Utility routines for the OpenMP support library.
 */


//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.txt for details.
//
//===----------------------------------------------------------------------===//


#include "kmp.h"
#include "kmp_wrapper_getpid.h"
#include "kmp_str.h"
#include <float.h>
#include "kmp_i18n.h"

/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */

static const char *unknown = "unknown";

#if KMP_ARCH_X86 || KMP_ARCH_X86_64

/* NOTE: If called before serial_initialize (i.e. from runtime_initialize), then */
/* the debugging package has not been initialized yet, and only "0" will print   */
/* debugging output since the environment variables have not been read.          */

#ifdef KMP_DEBUG
static int trace_level = 5;
#endif

/*
 * LOG_ID_BITS  = ( 1 + floor( log_2( max( log_per_phy - 1, 1 ))))
 * APIC_ID      = (PHY_ID << LOG_ID_BITS) | LOG_ID
 * PHY_ID       = APIC_ID >> LOG_ID_BITS
 */
int
__kmp_get_physical_id( int log_per_phy, int apic_id )
{
   int index_lsb, index_msb, temp;

   if (log_per_phy > 1) {
	index_lsb = 0;
	index_msb = 31;

	temp = log_per_phy;
        while ( (temp & 1) == 0 ) {
	    temp  >>= 1;
	    index_lsb++;
	}

	temp = log_per_phy;
	while ( (temp & 0x80000000)==0 ) {
	    temp <<= 1;
	    index_msb--;
	}

	/* If >1 bits were set in log_per_phy, choose next higher power of 2 */
	if (index_lsb != index_msb) index_msb++;

	return ( (int) (apic_id >> index_msb) );
   }

   return apic_id;
}


/*
 * LOG_ID_BITS  = ( 1 + floor( log_2( max( log_per_phy - 1, 1 ))))
 * APIC_ID      = (PHY_ID << LOG_ID_BITS) | LOG_ID
 * LOG_ID       = APIC_ID & (( 1 << LOG_ID_BITS ) - 1 )
 */
int
__kmp_get_logical_id( int log_per_phy, int apic_id )
{
   unsigned current_bit;
   int bits_seen;

   if (log_per_phy <= 1) return ( 0 );

   bits_seen = 0;

   for (current_bit = 1; log_per_phy != 0; current_bit <<= 1) {
	if ( log_per_phy & current_bit ) {
	    log_per_phy &= ~current_bit;
	    bits_seen++;
	}
   }

   /* If exactly 1 bit was set in log_per_phy, choose next lower power of 2 */
   if (bits_seen == 1) {
	current_bit >>= 1;
   }

   return ( (int) ((current_bit - 1) & apic_id) );
}


static
kmp_uint64
__kmp_parse_frequency(        // R: Frequency in Hz.
    char const * frequency    // I: Float number and unit: MHz, GHz, or TGz.
) {

    double       value  = 0.0;
    char const * unit   = NULL;
    kmp_uint64   result = 0;                    /* Zero is a better unknown value than all ones. */

    if ( frequency == NULL ) {
        return result;
    }; // if
    value = strtod( frequency, (char * *) & unit ); // strtod() does not like "char conts *".
    if ( 0 < value && value <= DBL_MAX ) {          // Good value (not overflow, underflow, etc).
        if ( strcmp( unit, "MHz" ) == 0 ) {
            value = value * 1.0E+6;
        } else if ( strcmp( unit, "GHz" ) == 0 ) {
            value = value * 1.0E+9;
        } else if ( strcmp( unit, "THz" ) == 0 ) {
            value = value * 1.0E+12;
        } else {                      // Wrong unit.
            return result;
        }; // if
        result = value;
    }; // if
    return result;

}; // func __kmp_parse_cpu_frequency

void
__kmp_query_cpuid( kmp_cpuinfo_t *p )
{
    struct kmp_cpuid buf;
    int max_arg;
    int log_per_phy;
#ifdef KMP_DEBUG
    int cflush_size;<commit_msg>Fix spelling error in comment<commit_after>/*
 * kmp_utility.c -- Utility routines for the OpenMP support library.
 */


//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.txt for details.
//
//===----------------------------------------------------------------------===//


#include "kmp.h"
#include "kmp_wrapper_getpid.h"
#include "kmp_str.h"
#include <float.h>
#include "kmp_i18n.h"

/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */

static const char *unknown = "unknown";

#if KMP_ARCH_X86 || KMP_ARCH_X86_64

/* NOTE: If called before serial_initialize (i.e. from runtime_initialize), then */
/* the debugging package has not been initialized yet, and only "0" will print   */
/* debugging output since the environment variables have not been read.          */

#ifdef KMP_DEBUG
static int trace_level = 5;
#endif

/*
 * LOG_ID_BITS  = ( 1 + floor( log_2( max( log_per_phy - 1, 1 ))))
 * APIC_ID      = (PHY_ID << LOG_ID_BITS) | LOG_ID
 * PHY_ID       = APIC_ID >> LOG_ID_BITS
 */
int
__kmp_get_physical_id( int log_per_phy, int apic_id )
{
   int index_lsb, index_msb, temp;

   if (log_per_phy > 1) {
	index_lsb = 0;
	index_msb = 31;

	temp = log_per_phy;
        while ( (temp & 1) == 0 ) {
	    temp  >>= 1;
	    index_lsb++;
	}

	temp = log_per_phy;
	while ( (temp & 0x80000000)==0 ) {
	    temp <<= 1;
	    index_msb--;
	}

	/* If >1 bits were set in log_per_phy, choose next higher power of 2 */
	if (index_lsb != index_msb) index_msb++;

	return ( (int) (apic_id >> index_msb) );
   }

   return apic_id;
}


/*
 * LOG_ID_BITS  = ( 1 + floor( log_2( max( log_per_phy - 1, 1 ))))
 * APIC_ID      = (PHY_ID << LOG_ID_BITS) | LOG_ID
 * LOG_ID       = APIC_ID & (( 1 << LOG_ID_BITS ) - 1 )
 */
int
__kmp_get_logical_id( int log_per_phy, int apic_id )
{
   unsigned current_bit;
   int bits_seen;

   if (log_per_phy <= 1) return ( 0 );

   bits_seen = 0;

   for (current_bit = 1; log_per_phy != 0; current_bit <<= 1) {
	if ( log_per_phy & current_bit ) {
	    log_per_phy &= ~current_bit;
	    bits_seen++;
	}
   }

   /* If exactly 1 bit was set in log_per_phy, choose next lower power of 2 */
   if (bits_seen == 1) {
	current_bit >>= 1;
   }

   return ( (int) ((current_bit - 1) & apic_id) );
}


static
kmp_uint64
__kmp_parse_frequency(        // R: Frequency in Hz.
    char const * frequency    // I: Float number and unit: MHz, GHz, or TGz.
) {

    double       value  = 0.0;
    char const * unit   = NULL;
    kmp_uint64   result = 0;                    /* Zero is a better unknown value than all ones. */

    if ( frequency == NULL ) {
        return result;
    }; // if
    value = strtod( frequency, (char * *) & unit ); // strtod() does not like "char const *".
    if ( 0 < value && value <= DBL_MAX ) {          // Good value (not overflow, underflow, etc).
        if ( strcmp( unit, "MHz" ) == 0 ) {
            value = value * 1.0E+6;
        } else if ( strcmp( unit, "GHz" ) == 0 ) {
            value = value * 1.0E+9;
        } else if ( strcmp( unit, "THz" ) == 0 ) {
            value = value * 1.0E+12;
        } else {                      // Wrong unit.
            return result;
        }; // if
        result = value;
    }; // if
    return result;

}; // func __kmp_parse_cpu_frequency

void
__kmp_query_cpuid( kmp_cpuinfo_t *p )
{
    struct kmp_cpuid buf;
    int max_arg;
    int log_per_phy;
#ifdef KMP_DEBUG
    int cflush_size;
{'fraction_non_alphanumeric': 0.20211611502984264, 'fraction_numerical': 0.019533369506239826, 'mean_word_length': 1.7686819376642884, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 25, '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': '4300919', 'n_tokens_mistral': 2677, 'n_tokens_neox': 2490, 'n_words': 709}