<commit_before>/* fltcalc.c
 * Authors: <NAME>
 */

/*
 * TODO:
 *
 * This code uses the C-type long double to respesent floating
 * point values. This is bad because:
 *
 * 1.) It depends on IEEE arithmetic on the compilation engine (hardly a problem)
 * 2.) The existance on the type and its bits size (may be identical to double or even float)
 * 3.) Arithmetic operations will be done with "higher order" precision, which might be wrong
 *
 * Replace this code ASAP.
 */
#include "fltcalc.h"
#include "ieee754.h"
#include <string.h>
#include <float.h>
#include <stdlib.h>
#include <stdio.h>

/********
 * globals
 ********/
static long double value;

#define CAST_IN(val) ({ long double xxx = *(long double *)(val); printf("CAST to %Lg\n", xxx); xxx; })

//#define CAST_IN(val) (*((long double *)((val))))
#define CAST_OUT(val) ((void *)&(val))

#define CLEAR_BUFFER() memset((char*)&value, 0, sizeof(long double))
/********
 * private functions
 ********/

/********
 * functions defined in fltcalc.h
 ********/
const void *fc_get_buffer(void)
{
  return CAST_OUT(value);
}

const int fc_get_buffer_length(void)
{
  return sizeof(long double);
}

void fc_val_from_str(const char *str, unsigned int len)
{
  extern long double strtold(const char *str, char **end);

  printf("-> %s\n", str);
  CLEAR_BUFFER();
  value = strtold(str, NULL);
}

void fc_val_from_float(long double l)
{
  CLEAR_BUFFER();
  value = l;
}

long double fc_val_to_float(const void *val)
{
  return CAST_IN(val);
}

void fc_get_min(unsigned int num_bits)
{
  CLEAR_BUFFER();
  switch (num_bits)
  {
    case 32:
      value = FLT_MIN;
      break;
    case 64:
      value = DBL_MIN;
      break;
    case 80:
    default:
      value = LDBL_MIN;
      break;
  }
}

void fc_get_max(unsigned int num_bits)
{
  CLEAR_BUFFER();
  switch (num_bits)
  {
    case 32:
      value = FLT_MAX;
      break;
    case 64:
      value = DBL_MAX;
      break;
    case 80:
    default:
      value = LDBL_MAX;
      break;
  }
}

void fc_get_nan(void)
{
  /* nan: all exponent bit set, non-zero mantissa. not signalling wheni
   * msb of mantissa is set (easily found using this struct */
  union ieee854_long_double ld;

  CLEAR_BUFFER();
  ld.ieee_nan.negative = 0;
  ld.ieee_nan.exponent = 0x7FFF;
  ld.ieee_nan.quiet_nan = 1;
  ld.ieee_nan.mantissa0 = 42;

  value = ld.d;
}

void fc_get_inf(void)
{
  /* +-inf: all exponent bit set, sign is easy, one is strange XXX */
  union ieee854_long_double ld;

  CLEAR_BUFFER();
  ld.ieee_nan.negative = 0;
  ld.ieee_nan.exponent = 0x7FFF;
  ld.ieee_nan.quiet_nan = 0;
  ld.ieee_nan.one = 1;
  ld.ieee_nan.mantissa0 = 0;
  ld.ieee_nan.mantissa1 = 0;

  value = ld.d;
}

void fc_calc(const void *a, const void *b, int opcode)
{
  CLEAR_BUFFER();
  switch (opcode)
  {
    case FC_ADD:
      value = CAST_IN(a) + CAST_IN(b);
      break;
    case FC_SUB:
      value = CAST_IN(a) - CAST_IN(b);
      break;
    case FC_MUL:
      value = CAST_IN(a) * CAST_IN(b);
      break;
    case FC_DIV:
      value = CAST_IN(a) / CAST_IN(b);
      break;
    case FC_NEG:
      value = -CAST_IN(a);
      printf("-> NEG %Lg\n", value);
      break;
  }
}

int fc_comp(const void *a, const void *b)
{
  if (CAST_IN(a) == CAST_IN(b)) return 0;
  else return (CAST_IN(a) > CAST_IN(b))?(1):(-1);
}

char *fc_print_dec(const void *a, char *buf, int buflen)
{
  snprintf(buf, buflen, "%1.30Lg", CAST_IN(a));
  return buf;
}

unsigned char fc_sub_bits(const void *value, unsigned num_bits, unsigned byte_ofs)
{
  long double val = CAST_IN(value);
  float f;
  double d;

  unsigned char *p;
  unsigned len;
<commit_msg>Changed inf/nan to remove non-partabel ieee754.h file<commit_after>/* fltcalc.c
 * Authors: <NAME>
 */

/*
 * TODO:
 *
 * This code uses the C-type long double to respesent floating
 * point values. This is bad because:
 *
 * 1.) It depends on IEEE arithmetic on the compilation engine (hardly a problem)
 * 2.) The existance on the type and its bits size (may be identical to double or even float)
 * 3.) Arithmetic operations will be done with "higher order" precision, which might be wrong
 *
 * Replace this code ASAP.
 */
#include "fltcalc.h"
#include <string.h>
#include <float.h>
#include <stdlib.h>
#include <stdio.h>

/* only defined in C99 mode */
extern long double strtold(const char *str, char **end);

/********
 * globals
 ********/
static long double value;

//#define CAST_IN(val) ({ long double xxx = *(long double *)(val); printf("CAST to %Lg\n", xxx); xxx; })

#define CAST_IN(val) (*((long double *)((val))))
#define CAST_OUT(val) ((void *)&(val))

#define CLEAR_BUFFER() memset((char*)&value, 0, sizeof(long double))
/********
 * private functions
 ********/

/********
 * functions defined in fltcalc.h
 ********/
const void *fc_get_buffer(void)
{
  return CAST_OUT(value);
}

const int fc_get_buffer_length(void)
{
  return sizeof(long double);
}

void fc_val_from_str(const char *str, unsigned int len)
{
  CLEAR_BUFFER();
  value = strtold(str, NULL);
}

void fc_val_from_float(long double l)
{
  CLEAR_BUFFER();
  value = l;
}

long double fc_val_to_float(const void *val)
{
  return CAST_IN(val);
}

void fc_get_min(unsigned int num_bits)
{
  CLEAR_BUFFER();
  switch (num_bits)
  {
    case 32:
      value = FLT_MIN;
      break;
    case 64:
      value = DBL_MIN;
      break;
    case 80:
    default:
      value = LDBL_MIN;
      break;
  }
}

void fc_get_max(unsigned int num_bits)
{
  CLEAR_BUFFER();
  switch (num_bits)
  {
    case 32:
      value = FLT_MAX;
      break;
    case 64:
      value = DBL_MAX;
      break;
    case 80:
    default:
      value = LDBL_MAX;
      break;
  }
}

void fc_get_nan(void)
{
  value = strtold("nan", NULL);

}

void fc_get_inf(void)
{
  value = strtold("inf", NULL);
}

void fc_calc(const void *a, const void *b, int opcode)
{
  CLEAR_BUFFER();
  switch (opcode)
  {
    case FC_ADD:
      value = CAST_IN(a) + CAST_IN(b);
      break;
    case FC_SUB:
      value = CAST_IN(a) - CAST_IN(b);
      break;
    case FC_MUL:
      value = CAST_IN(a) * CAST_IN(b);
      break;
    case FC_DIV:
      value = CAST_IN(a) / CAST_IN(b);
      break;
    case FC_NEG:
      value = -CAST_IN(a);
      break;
  }
}

int fc_comp(const void *a, const void *b)
{
  if (CAST_IN(a) == CAST_IN(b)) return 0;
  else return (CAST_IN(a) > CAST_IN(b))?(1):(-1);
}

char *fc_print_dec(const void *a, char *buf, int buflen)
{
  snprintf(buf, buflen, "%1.30Lg", CAST_IN(a));
  return buf;
}

unsigned char fc_sub_bits(const void *value, unsigned num_bits, unsigned byte_ofs)
{
  long double val = CAST_IN(value);
  float f;
  double d;

  unsigned char *p;
  unsigned len;

{'fraction_non_alphanumeric': 0.1451370069256248, 'fraction_numerical': 0.011141222523336344, 'mean_word_length': 3.1648902821316613, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7342138', 'n_tokens_mistral': 2701, 'n_tokens_neox': 2571, 'n_words': 702}