Commit c0397977 authored by Michael Beck's avatar Michael Beck
Browse files

Added tarval_sub_bits() fucntion to access the (binary) bitpattern of a tarval.

[r1018]
parent 1e59380b
......@@ -2,6 +2,18 @@
* Authors: Matthias Heil
*/
/*
* 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>
......@@ -152,3 +164,39 @@ 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;
switch (num_bits) {
case 32:
f = (float)val;
p = (unsigned char *)&f;
len = 4;
break;
case 64:
d = (double)val;
p = (unsigned char *)&d;
len = 8;
break;
case 80:
p = (unsigned char *)&val;
len = 10;
break;
default:
return 0;
}
if (byte_ofs > len)
return 0;
return p[byte_ofs];
}
......@@ -31,4 +31,6 @@ void fc_calc(const void *a, const void *b, int opcode);
char *fc_print_dec(const void *a, char *buf, int buflen);
int fc_comp(const void *a, const void *b);
unsigned char fc_sub_bits(const void *val, unsigned num_bit, unsigned byte_ofs);
#endif /* _FLTCALC_H_ */
......@@ -1196,11 +1196,28 @@ int sc_comp(const void* value1, const void* value2)
return (val1[counter] > val2[counter]) ? (1) : (-1);
}
char *sc_print(const void *value, unsigned base)
unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs)
{
const char *val = (const char *)value;
unsigned nibble_ofs = 2 * byte_ofs;
unsigned char res;
/* the current scheme uses one byte to store a nibble */
if (nibble_ofs >= len)
return 0;
res = _val(val[nibble_ofs]);
if (len > nibble_ofs + 1)
res |= _val(val[nibble_ofs + 1]) << 4;
return res;
}
const char *sc_print(const void *value, unsigned base)
{
int counter;
const char *val = (char*)value;
const char *val = (const char *)value;
char *pos;
static char *buf = NULL;
......
......@@ -105,6 +105,8 @@ void sc_calc(const void *val1, const void *val2, unsigned op);
void sc_bitcalc(const void *val1, const void *val2, unsigned radius, unsigned sign, unsigned op);
int sc_comp(const void *val1, const void *val2);
char* sc_print(const void *val1, unsigned base);
unsigned char sc_sub_bits(const void *val, int len, unsigned byte_ofs);
const char *sc_print(const void *val1, unsigned base);
#endif /* _STRCALC_H_ */
......@@ -881,7 +881,7 @@ int tarval_print(XP_PAR1, const xprintf_info *info ATTRIBUTE((unused)), XP_PARN)
{
ANNOUNCE();
tarval *tv;
char *str;
const char *str;
int offset;
char buf[100];
......@@ -925,6 +925,24 @@ char *tarval_bitpattern(tarval *tv)
return NULL;
}
/*
* access to the bitpattern
*/
unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs)
{
switch (get_mode_sort(tv->mode)) {
case int_number:
case character:
return sc_sub_bits(tv->value, tv->length, byte_ofs);
case float_number:
return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
default:
return 0;
}
}
/* Identifying some tarvals ??? */
/* Implemented in old tv.c as such:
* return 0 for additive neutral,
......
......@@ -494,6 +494,33 @@ char *tarval_bitpattern(tarval *tv);
*/
char *tarval_sub_bitpattern(tarval *tv, int from, int to);
/**
* Returns the bitpattern of the bytes_ofs byte.
*
* This function succeeds even if the mode of the tarval uses lesser bits
* than requested, in that case the bitpattern is filled with zero bits.
*
* To query a 32bit value the following code can be used:
*
* val0 = tarval_sub_bits(tv, 0);
* val1 = tarval_sub_bits(tv, 1);
* val2 = tarval_sub_bits(tv, 2);
* val3 = tarval_sub_bits(tv, 3);
*
* Because this is the bit representation of the target machine, only the following
* operations are legal on the result:
*
* - concatenation (be aware of the endieness)
* - bitwise logical operations to selct/mask bits
*
* @param tv the tarval
* @param byte_ofs the byte offset
*
* @note
* The result of this funcion is undefined if the mode is neither integer nor float.
*/
unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs);
/**
* Identifying some tarvals ???
*
......
......@@ -15,32 +15,26 @@
#include "tv.h"
#include "xprintf.h"
/****s* tv/tarval
*
* NAME
* tarval
* This struct represents the aforementioned tarvals.
/**
* This struct represents the aforementioned tarvals.
*
* DESCRIPTION
* A tarval struct consists of an internal representation of the
* value and some additional fields further describing the value.
* A tarval struct consists of an internal representation of the
* value and some additional fields further describing the value.
*
* ATTRIBUTES
* ir_mode *mode The mode of the stored value
* void *value The internal representation
* ATTRIBUTES:
* - ir_mode *mode The mode of the stored value
* - void *value The internal representation
*
* SEE ALSO
* @sa
* irmode.h for predefined modes
*
******/
*/
struct tarval {
ir_mode *mode; /* mode of the stored value */
const void *value; /* the value stored in an internal way... */
unsigned int length; /* the length of the stored value */
ir_mode *mode; /**< the mode of the stored value */
const void *value; /**< the value stored in an internal way... */
unsigned int length; /**< the length of the stored value */
};
/* xfprint output */
/** xfprint output */
int tarval_print (XP_PAR1, const xprintf_info *, XP_PARN);
/** remove tarval representing an entity that is about to be destroyed */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment