fltcalc.c 3.38 KB
Newer Older
1
2
3
4
/* fltcalc.c
 * Authors: Matthias Heil
 */

5
6
7
/*
 * TODO:
 *
Michael Beck's avatar
Tarval:    
Michael Beck committed
8
 * This code uses the C-type LLDBL to respesent floating
9
10
11
12
13
14
15
16
 * 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.
 */
17
18
19
20
21
22
#include "fltcalc.h"
#include <string.h>
#include <float.h>
#include <stdlib.h>
#include <stdio.h>

Michael Beck's avatar
Tarval:    
Michael Beck committed
23
24
25
#ifdef USE_LONG_DOUBLE
/* have long double type */

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

Michael Beck's avatar
Tarval:    
Michael Beck committed
29
30
31
32
33
34
35
36
37
38
39
#define strtoLLD	strtold

#else
/* don't have long double type */

extern double strtod(const char *str, char **end);

#define strtoLLD	strtod

#endif

40
41
42
/********
 * globals
 ********/
Michael Beck's avatar
Tarval:    
Michael Beck committed
43
static LLDBL value;
44

Michael Beck's avatar
Tarval:    
Michael Beck committed
45
#define CAST_IN(val) (*((LLDBL *)((val))))
46
47
#define CAST_OUT(val) ((void *)&(val))

Michael Beck's avatar
Tarval:    
Michael Beck committed
48
#define CLEAR_BUFFER() memset((char*)&value, 0, sizeof(LLDBL))
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/********
 * private functions
 ********/

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

const int fc_get_buffer_length(void)
{
Michael Beck's avatar
Tarval:    
Michael Beck committed
63
  return sizeof(LLDBL);
64
65
66
67
68
}

void fc_val_from_str(const char *str, unsigned int len)
{
  CLEAR_BUFFER();
Michael Beck's avatar
Tarval:    
Michael Beck committed
69
  value = strtoLLD(str, NULL);
70
71
}

Michael Beck's avatar
Tarval:    
Michael Beck committed
72
void fc_val_from_float(LLDBL l)
73
74
75
76
77
{
  CLEAR_BUFFER();
  value = l;
}

Michael Beck's avatar
Tarval:    
Michael Beck committed
78
LLDBL fc_val_to_float(const void *val)
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
{
  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)
{
Michael Beck's avatar
Tarval:    
Michael Beck committed
121
  value = strtoLLD("nan", NULL);
122
123
124
125
126

}

void fc_get_inf(void)
{
Michael Beck's avatar
Tarval:    
Michael Beck committed
127
  value = strtoLLD("inf", NULL);
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
}

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);
149
      break;
150
151
152
153
154
155
156
157
158
  }
}

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);
}

Michael Beck's avatar
Michael Beck committed
159
char *fc_print_dec(const void *a, char *buf, int buflen)
160
{
Michael Beck's avatar
Tarval:    
Michael Beck committed
161
#ifdef USE_LONG_DOUBLE
Michael Beck's avatar
Michael Beck committed
162
  snprintf(buf, buflen, "%1.30Lg", CAST_IN(a));
Michael Beck's avatar
Tarval:    
Michael Beck committed
163
164
165
#else
  snprintf(buf, buflen, "%1.30g", CAST_IN(a));
#endif
166
167
  return buf;
}
168
169
170

unsigned char fc_sub_bits(const void *value, unsigned num_bits, unsigned byte_ofs)
{
Michael Beck's avatar
Tarval:    
Michael Beck committed
171
  LLDBL val = CAST_IN(value);
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  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];
}