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

5
6
7
8
9
10
11
12
13
14
15
16
/*
 * 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.
 */
17
18
19
20
21
22
#include "fltcalc.h"
#include <string.h>
#include <float.h>
#include <stdlib.h>
#include <stdio.h>

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

26
27
28
29
30
/********
 * globals
 ********/
static long double value;

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

33
#define CAST_IN(val) (*((long double *)((val))))
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
#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)
{
109
  value = strtold("nan", NULL);
110
111
112
113
114

}

void fc_get_inf(void)
{
115
  value = strtold("inf", NULL);
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
}

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);
137
      break;
138
139
140
141
142
143
144
145
146
  }
}

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
147
char *fc_print_dec(const void *a, char *buf, int buflen)
148
{
Michael Beck's avatar
Michael Beck committed
149
  snprintf(buf, buflen, "%1.30Lg", CAST_IN(a));
150
151
  return buf;
}
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

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