fltcalc.c 3.83 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
{
  return CAST_IN(val);
}

void fc_get_min(unsigned int num_bits)
{
  CLEAR_BUFFER();
86
87
88
89
90
91

  /*
   * Beware: FLT_MIN is the "Minimum normalised float",
   * not the smallest number in arithmetic sense
   */
  switch (num_bits) {
92
    case 32:
93
      value = -FLT_MAX;
94
95
      break;
    case 64:
96
      value = -DBL_MAX;
97
98
99
      break;
    case 80:
    default:
100
      value = -LDBL_MAX;
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
      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
125
  value = strtoLLD("nan", NULL);
126
127
128
129
130

}

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

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

int fc_comp(const void *a, const void *b)
{
159
160
161
162
163
164
165
166
167
168

  if (CAST_IN(a) == CAST_IN(b)) {
    return 0;
  }
  else if (CAST_IN(a) > CAST_IN(b)) {
    return 1;
  }
  else {
    return -1;
  }
169
170
}

Michael Beck's avatar
Michael Beck committed
171
char *fc_print_dec(const void *a, char *buf, int buflen)
172
{
173
174
  int n;

Michael Beck's avatar
Tarval:    
Michael Beck committed
175
#ifdef USE_LONG_DOUBLE
176
  n = snprintf(buf, buflen, "%1.30Lg", CAST_IN(a));
Michael Beck's avatar
Tarval:    
Michael Beck committed
177
#else
178
  n = snprintf(buf, buflen, "%1.30g", CAST_IN(a));
Michael Beck's avatar
Tarval:    
Michael Beck committed
179
#endif
180
181
182
183
184
185
186
187
188
189
190

  if (n > -1 && n < buflen) {
    /*
     * for some backends we always want to present 0 as 0.0
     * as I could not find the best format to express this, add
     * a .0 explicitely if not found
     */
    if (NULL == strrchr(buf, '.'))
      strncat(buf, ".0", buflen);
  }

191
192
  return buf;
}
193
194
195

unsigned char fc_sub_bits(const void *value, unsigned num_bits, unsigned byte_ofs)
{
Michael Beck's avatar
Tarval:    
Michael Beck committed
196
  LLDBL val = CAST_IN(value);
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
  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];
}