<commit_before>#include "shotgun.h"
#include "tommath.h"
#include "string.h"
#include <ctype.h>
#include <math.h>

#define NMP mp_int *n; OBJECT n_obj; \
  NEW_STRUCT(n_obj, n, BASIC_CLASS(bignum), mp_int); \
  mp_init(n)
#define MMP mp_int *m; OBJECT m_obj; \
  NEW_STRUCT(m_obj, m, BASIC_CLASS(bignum), mp_int); \
  mp_init(m)


#define MP(k) DATA_STRUCT(k, mp_int*)
#define BDIGIT_DBL long long
#define DIGIT_RADIX (1 << DIGIT_BIT)

void bignum_cleanup(STATE, OBJECT obj) {
  mp_int *n = MP(obj);
  mp_clear(n);
}

void bignum_init(STATE) {
  state_add_cleanup(state, state->global->bignum, bignum_cleanup);
}

static void twos_complement(mp_int *a)
{
  long i = a->used;
  BDIGIT_DBL num;

  while (i--) {
    DIGIT(a,i) = (~DIGIT(a,i)) & (DIGIT_RADIX-1);
  }

  i = 0; num = 1;
  do {
    num += DIGIT(a,i);
    DIGIT(a,i++) = num & (DIGIT_RADIX-1);
    num = num >> DIGIT_BIT;
  } while (i < a->used);

}

OBJECT bignum_new(STATE, int num) {
  mp_int *a;
  OBJECT o;
  o = object_memory_new_opaque(state, BASIC_CLASS(bignum), sizeof(mp_int));
  a = (mp_int*)BYTES_OF(o);
  mp_init(a);
  if(num < 0) {
    mp_set_int(a, (unsigned int)-num);
    a->sign = MP_NEG;
  } else {
    mp_set_int(a, (unsigned int)num);
  }
  return o;
}

OBJECT bignum_new_unsigned(STATE, unsigned int num) {
  mp_int *a;
  OBJECT o;
  o = object_memory_new_opaque(state, BASIC_CLASS(bignum), sizeof(mp_int));
  a = (mp_int*)BYTES_OF(o);
  mp_init(a);
  mp_set_int(a, num);
  return o;
}

static inline OBJECT bignum_normalize(STATE, OBJECT b) {
  if(mp_count_bits(MP(b)) <= FIXNUM_WIDTH) {
    int val;
    val = (int)mp_get_int(MP(b));
    if(MP(b)->sign == MP_NEG) {<commit_msg>Fixed bignum_normalize() for bignums with leading zero digits.<commit_after>#include "shotgun.h"
#include "tommath.h"
#include "string.h"
#include <ctype.h>
#include <math.h>

#define NMP mp_int *n; OBJECT n_obj; \
  NEW_STRUCT(n_obj, n, BASIC_CLASS(bignum), mp_int); \
  mp_init(n)
#define MMP mp_int *m; OBJECT m_obj; \
  NEW_STRUCT(m_obj, m, BASIC_CLASS(bignum), mp_int); \
  mp_init(m)


#define MP(k) DATA_STRUCT(k, mp_int*)
#define BDIGIT_DBL long long
#define DIGIT_RADIX (1 << DIGIT_BIT)

void bignum_cleanup(STATE, OBJECT obj) {
  mp_int *n = MP(obj);
  mp_clear(n);
}

void bignum_init(STATE) {
  state_add_cleanup(state, state->global->bignum, bignum_cleanup);
}

static void twos_complement(mp_int *a)
{
  long i = a->used;
  BDIGIT_DBL num;

  while (i--) {
    DIGIT(a,i) = (~DIGIT(a,i)) & (DIGIT_RADIX-1);
  }

  i = 0; num = 1;
  do {
    num += DIGIT(a,i);
    DIGIT(a,i++) = num & (DIGIT_RADIX-1);
    num = num >> DIGIT_BIT;
  } while (i < a->used);

}

OBJECT bignum_new(STATE, int num) {
  mp_int *a;
  OBJECT o;
  o = object_memory_new_opaque(state, BASIC_CLASS(bignum), sizeof(mp_int));
  a = (mp_int*)BYTES_OF(o);
  mp_init(a);
  if(num < 0) {
    mp_set_int(a, (unsigned int)-num);
    a->sign = MP_NEG;
  } else {
    mp_set_int(a, (unsigned int)num);
  }
  return o;
}

OBJECT bignum_new_unsigned(STATE, unsigned int num) {
  mp_int *a;
  OBJECT o;
  o = object_memory_new_opaque(state, BASIC_CLASS(bignum), sizeof(mp_int));
  a = (mp_int*)BYTES_OF(o);
  mp_init(a);
  mp_set_int(a, num);
  return o;
}

static inline OBJECT bignum_normalize(STATE, OBJECT b) {
  mp_clamp(MP(b));

  if(mp_count_bits(MP(b)) <= FIXNUM_WIDTH) {
    int val;
    val = (int)mp_get_int(MP(b));
    if(MP(b)->sign == MP_NEG) {
{'fraction_non_alphanumeric': 0.16445623342175067, 'fraction_numerical': 0.0035366931918656055, 'mean_word_length': 4.05812220566319, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, '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': '7350938', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1548, 'n_words': 306}