<commit_before>/* TV --- Target Values, aka Constant Table.
   Copyright (C) 1995, 1996 <NAME> */

/* $Id$ */

/****i* tv/implementation
 *
 * AUTHORS
 *    <NAME>
 *    <NAME>
 *
 * NOTES
 *    Internal storage for tarvals, 1st draft:
 *   Integers as well as pointers are stored in a hex formatted string holding
 *   16 characters. Booleans are not stored as there are only two of them.
 *
 *    Floats are just reinterpreted as byte strings, because I am not sure if
 *   there is loss if I convert float to long double and back and furthermore
 *   the implementation of a fully ieee compatible floating point emulation
 *   is not sensible for now
 *    With this information it is easy to decide the kind of stored value:
 *   Integers have size 16, floats 4, doubles 8, long doubles 12.
 ******/

/* This implementation assumes:
   * both host and target have IEEE-754 floating-point arithmetic.  */

/* !!! float and double divides MUST NOT SIGNAL !!! */
/* @@@ query the floating-point expception status flags */

/* @@@ Problem: All Values are stored twice, once as Univ_*s and a 2nd
   time in their real target mode. :-( */

#define MAX_INT_LENGTH 8
#define CHAR_BUFFER_SIZE ((MAX_INT_LENGTH) * 2)

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <assert.h>         /* assertions */
#include <stdlib.h>         /* atoi() */
#include <string.h>         /* nice things for strings */
#include <strings.h>         /* nice things for strings */

#include <stdlib.h>
#include "tv_t.h"
#include "set.h"            /* to store tarvals in */
#include "tune.h"           /* some constants */
#include "entity_t.h"       /* needed to store pointers to entities */
#include "irmode.h"         /* defines modes etc */
#include "irmode_t.h"
#include "irnode.h"         /* defines boolean return values */
#include "host.h"
#include "strcalc.h"
#include "fltcalc.h"

/****************************************************************************
 *   local definitions and macros
 ****************************************************************************/
#ifndef NDEBUG
#  define TARVAL_VERIFY(a) tarval_verify((a))
#else
#  define TARVAL_VERIFY(a) ((void)0)
#endif

#define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
#define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))

#define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
#define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))

#define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
#if 0
static long long count = 0;
#  define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
#else
#  define ANNOUNCE() ((void)0)
#endif
/****************************************************************************
 *   private variables
 ****************************************************************************/
static struct set *tarvals;   /* container for tarval structs */
static struct set *values;    /* container for values */

/****************************************************************************
 *   private functions
 ****************************************************************************/
#ifndef NDEBUG
static int hash_val(const void *value, unsigned int length);
static int hash_tv(tarval *tv);
static void _fail_verify(tarval *tv, const char* file, int line)
{
  /* print a memory image of the tarval and throw an assertion */
  if (tv)
    printf("%s:%d: Invalid tarval:\n  mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
  else
    printf("%s:%d: Invalid tarval (null)", file, line);
  assert(0);
}
#ifdef __GNUC__
INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
#endif

INLINE static void tarval_verify(tarval *tv)
{
  assert(tv);
  assert(tv->mode);
  assert(tv->value);

  if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
  if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;

  if (!FIND_TARVAL(tv)) fail_verify(tv);
  if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);

  return;
}
#endif /* NDEBUG */

static int hash_tv(tarval *tv)
{
  return ((unsigned int)tv->value ^ (unsigned int)tv->mode) + tv->length;
}

static int hash_val(const void *value, unsigned int length)
{
  unsigned int i;
  unsigned int hash = 0;

  /* scramble the byte - array */
  for (i = 0; i < length; i++)
  {
    hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
    hash += (hash << 11) ^ (hash >> 17);
  }

  return hash;
}

/* finds tarval with value/mode or creates new tarval */
static tarval *get_tarval(const void *value, int length, ir_mode *mode)
{
  tarval tv;

  tv.mode = mode;
  tv.length = length;
  if (length > 0)
    /* if there already is such a value, it is returned, else value
     * is copied into the set */
    tv.value = INSERT_VALUE(value, length);
  else
    tv.value = value;

  /* if there is such a tarval, it is returned, else tv is copied
   * into the set */
  return (tarval *)INSERT_TARVAL(&tv);
}

/**
 * Returns non-zero if a tarval overflows.
 *
 * @todo Implementation did not work on all modes
 */
static int overflows(tarval *tv)
{
  switch (get_mode_sort(tv->mode))
  {
    case irms_character:
    case irms_int_number:
      if (sc_comp(tv->value, get_mode_max(tv->mode)->value) == 1) return 1;
      if (sc_comp(tv->value, get_mode_min(tv->mode)->value) == -1) return 1;
      break;

    case irms_float_number:
      /*
       * TODO: check NaNs
       */
      if (fc_comp(tv->value, get_mode_max(tv->mode)->value) == 1) return 1;
      if (fc_comp(tv->value, get_mode_min(tv->mode)->value) == -1) return 1;
      break;

    default:
      break;
  }

  return 0;
}
<commit_msg>Can't do arithmetics w/ character sorts<commit_after>/* TV --- Target Values, aka Constant Table.
   Copyright (C) 1995, 1996 <NAME> */

/* $Id$ */

/****i* tv/implementation
 *
 * AUTHORS
 *    <NAME>
 *    <NAME>
 *
 * NOTES
 *    Internal storage for tarvals, 1st draft:
 *   Integers as well as pointers are stored in a hex formatted string holding
 *   16 characters. Booleans are not stored as there are only two of them.
 *
 *    Floats are just reinterpreted as byte strings, because I am not sure if
 *   there is loss if I convert float to long double and back and furthermore
 *   the implementation of a fully ieee compatible floating point emulation
 *   is not sensible for now
 *    With this information it is easy to decide the kind of stored value:
 *   Integers have size 16, floats 4, doubles 8, long doubles 12.
 ******/

/* This implementation assumes:
   * both host and target have IEEE-754 floating-point arithmetic.  */

/* !!! float and double divides MUST NOT SIGNAL !!! */
/* @@@ query the floating-point expception status flags */

/* @@@ Problem: All Values are stored twice, once as Univ_*s and a 2nd
   time in their real target mode. :-( */

#define MAX_INT_LENGTH 8
#define CHAR_BUFFER_SIZE ((MAX_INT_LENGTH) * 2)

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <assert.h>         /* assertions */
#include <stdlib.h>         /* atoi() */
#include <string.h>         /* nice things for strings */
#include <strings.h>         /* nice things for strings */

#include <stdlib.h>
#include "tv_t.h"
#include "set.h"            /* to store tarvals in */
#include "tune.h"           /* some constants */
#include "entity_t.h"       /* needed to store pointers to entities */
#include "irmode.h"         /* defines modes etc */
#include "irmode_t.h"
#include "irnode.h"         /* defines boolean return values */
#include "host.h"
#include "strcalc.h"
#include "fltcalc.h"

/****************************************************************************
 *   local definitions and macros
 ****************************************************************************/
#ifndef NDEBUG
#  define TARVAL_VERIFY(a) tarval_verify((a))
#else
#  define TARVAL_VERIFY(a) ((void)0)
#endif

#define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
#define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))

#define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
#define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))

#define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
#if 0
static long long count = 0;
#  define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
#else
#  define ANNOUNCE() ((void)0)
#endif
/****************************************************************************
 *   private variables
 ****************************************************************************/
static struct set *tarvals;   /* container for tarval structs */
static struct set *values;    /* container for values */

/****************************************************************************
 *   private functions
 ****************************************************************************/
#ifndef NDEBUG
static int hash_val(const void *value, unsigned int length);
static int hash_tv(tarval *tv);
static void _fail_verify(tarval *tv, const char* file, int line)
{
  /* print a memory image of the tarval and throw an assertion */
  if (tv)
    printf("%s:%d: Invalid tarval:\n  mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
  else
    printf("%s:%d: Invalid tarval (null)", file, line);
  assert(0);
}
#ifdef __GNUC__
INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
#endif

INLINE static void tarval_verify(tarval *tv)
{
  assert(tv);
  assert(tv->mode);
  assert(tv->value);

  if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
  if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;

  if (!FIND_TARVAL(tv)) fail_verify(tv);
  if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);

  return;
}
#endif /* NDEBUG */

static int hash_tv(tarval *tv)
{
  return ((unsigned int)tv->value ^ (unsigned int)tv->mode) + tv->length;
}

static int hash_val(const void *value, unsigned int length)
{
  unsigned int i;
  unsigned int hash = 0;

  /* scramble the byte - array */
  for (i = 0; i < length; i++)
  {
    hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
    hash += (hash << 11) ^ (hash >> 17);
  }

  return hash;
}

/* finds tarval with value/mode or creates new tarval */
static tarval *get_tarval(const void *value, int length, ir_mode *mode)
{
  tarval tv;

  tv.mode = mode;
  tv.length = length;
  if (length > 0)
    /* if there already is such a value, it is returned, else value
     * is copied into the set */
    tv.value = INSERT_VALUE(value, length);
  else
    tv.value = value;

  /* if there is such a tarval, it is returned, else tv is copied
   * into the set */
  return (tarval *)INSERT_TARVAL(&tv);
}

/**
 * Returns non-zero if a tarval overflows.
 *
 * @todo Implementation did not work on all modes
 */
static int overflows(tarval *tv)
{
  switch (get_mode_sort(tv->mode))
  {
    case irms_int_number:
      if (sc_comp(tv->value, get_mode_max(tv->mode)->value) == 1) return 1;
      if (sc_comp(tv->value, get_mode_min(tv->mode)->value) == -1) return 1;
      break;

    case irms_float_number:
      /*
       * TODO: check NaNs
       */
      if (fc_comp(tv->value, get_mode_max(tv->mode)->value) == 1) return 1;
      if (fc_comp(tv->value, get_mode_min(tv->mode)->value) == -1) return 1;
      break;

    default:
      break;
  }

  return 0;
}

{'fraction_non_alphanumeric': 0.21146022678092272, 'fraction_numerical': 0.008309529992209816, 'mean_word_length': 3.741075092326631, 'pattern_counts': {'":': 2, '<': 31, '<?xml version=': 0, '>': 79, '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': '5670042', 'n_tokens_mistral': 3824, 'n_tokens_neox': 3595, 'n_words': 1181}