<commit_before>/*****************************************************************************
 * Copyright 2014 Jeff <<EMAIL>>
 *****************************************************************************
 * 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 <limits.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

#include "sim_utils.h"
#include "sim_log.h"


int is_in_range(int min, int max, int v)
{
    return (((min)<=(max)) && ((min)<=(v)) && ((max)>=(v)));
}

int sat_div(int num, int den)
{
    return (num + den - 1)/den;
}


int bit_sat(int nbit, int val)
{
    int pad = (1<<nbit) - 1;
    return (val+pad) & (~pad);
}


int is_bit_aligned(int nbit, int val)
{
    int pad = (1<<nbit) - 1;
    return (val&pad)==0;
}


#define SIM_LZB_BIT(bit)            \
    b   = val >= (1<<(1<<(bit)));   \
    n  += b ?  0  :  (1<<(bit)) ;   \
    val = b ?  (val>>(1<<(bit))) : val;
    
int num_leading_zero_bits_u64(uint64_t val)
{
    int n = 0;
    uint64_t    b;
    
    SIM_LZB_BIT(5)          /* get the 5-th bit of n */
    SIM_LZB_BIT(4)
    SIM_LZB_BIT(3)
    SIM_LZB_BIT(2)
    SIM_LZB_BIT(1)
    SIM_LZB_BIT(0)          /* get the 1-th bit of n */
   
    n  += (val<1);          /* get the carry bit of n */
    return n;
}

int num_leading_zero_bits_u32(uint32_t val)
{
    int n = 0;
    uint32_t    b;
    
    SIM_LZB_BIT(4)          /* get the 4-th bit of n */
    SIM_LZB_BIT(3)
    SIM_LZB_BIT(2)
    SIM_LZB_BIT(1)
    SIM_LZB_BIT(0)          /* get the 1-th bit of n */
   
    n  += (val<1);          /* get the carry bit of n */
    return n;
}


int clip(int v, int minv, int maxv)
{
    v = (v<minv) ? minv : v;
    v = (v>maxv) ? maxv : v;
    return v;
}

/**
 * simple atoi(/[0-9]+/) with the 1st non-digit pointer returned 
 *  @see str_2_uint() which is more delicate 
 */
char* get_uint32 (char *str, uint32_t *out)
{
    char  *curr = str;
    
    if ( curr ) {
        int  c, sum;
        for (sum=0, curr=str; (c = *curr) && c >= '0' && c <= '9'; ++ curr) {
            sum = sum * 10 + ( c - '0' );
        }
        if (out) { *out = sum; }
    }

    return curr;
}

/**
 *  \brief return the pos for 1st-char not in @jumpset
 */
int jump_front(const char* str, const char* jumpset)
{
    int c, start;<commit_msg>safe clipclip(int v, int minv, int maxv) : swap(minv, maxv) when (minv>maxv)<commit_after>/*****************************************************************************
 * Copyright 2014 Jeff <<EMAIL>>
 *****************************************************************************
 * 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 <limits.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

#include "sim_utils.h"
#include "sim_log.h"


int is_in_range(int min, int max, int v)
{
    return (((min)<=(max)) && ((min)<=(v)) && ((max)>=(v)));
}

int sat_div(int num, int den)
{
    return (num + den - 1)/den;
}


int bit_sat(int nbit, int val)
{
    int pad = (1<<nbit) - 1;
    return (val+pad) & (~pad);
}


int is_bit_aligned(int nbit, int val)
{
    int pad = (1<<nbit) - 1;
    return (val&pad)==0;
}


#define SIM_LZB_BIT(bit)            \
    b   = val >= (1<<(1<<(bit)));   \
    n  += b ?  0  :  (1<<(bit)) ;   \
    val = b ?  (val>>(1<<(bit))) : val;
    
int num_leading_zero_bits_u64(uint64_t val)
{
    int n = 0;
    uint64_t    b;
    
    SIM_LZB_BIT(5)          /* get the 5-th bit of n */
    SIM_LZB_BIT(4)
    SIM_LZB_BIT(3)
    SIM_LZB_BIT(2)
    SIM_LZB_BIT(1)
    SIM_LZB_BIT(0)          /* get the 1-th bit of n */
   
    n  += (val<1);          /* get the carry bit of n */
    return n;
}

int num_leading_zero_bits_u32(uint32_t val)
{
    int n = 0;
    uint32_t    b;
    
    SIM_LZB_BIT(4)          /* get the 4-th bit of n */
    SIM_LZB_BIT(3)
    SIM_LZB_BIT(2)
    SIM_LZB_BIT(1)
    SIM_LZB_BIT(0)          /* get the 1-th bit of n */
   
    n  += (val<1);          /* get the carry bit of n */
    return n;
}


int clip(int v, int minv, int maxv)
{
    if (minv > maxv) 
        mem_swap(&minv, &maxv, sizeof(int), 1);

    if      (v < minv) return minv;
    else if (v > maxv) return maxv;
    else               return v;
}

/**
 * simple atoi(/[0-9]+/) with the 1st non-digit pointer returned 
 *  @see str_2_uint() which is more delicate 
 */
char* get_uint32 (char *str, uint32_t *out)
{
    char  *curr = str;
    
    if ( curr ) {
        int  c, sum;
        for (sum=0, curr=str; (c = *curr) && c >= '0' && c <= '9'; ++ curr) {
            sum = sum * 10 + ( c - '0' );
        }
        if (out) { *out = sum; }
    }

    return curr;
}

/**
 *  \brief return the pos for 1st-char not in @jumpset
 */
int jump_front(const char* str, const char* jumpset)
{
    int c, start;
{'fraction_non_alphanumeric': 0.21599592460519612, 'fraction_numerical': 0.022244863304465953, 'mean_word_length': 2.5290593169562614, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1731999', 'n_tokens_mistral': 2246, 'n_tokens_neox': 2124, 'n_words': 634}