2 * Authors: Matthias Heil
8 * This code uses the C-type long double to respesent floating
9 * point values. This is bad because:
11 * 1.) It depends on IEEE arithmetic on the compilation engine (hardly a problem)
12 * 2.) The existance on the type and its bits size (may be identical to double or even float)
13 * 3.) Arithmetic operations will be done with "higher order" precision, which might be wrong
15 * Replace this code ASAP.
27 static long double value;
29 #define CAST_IN(val) ({ long double xxx = *(long double *)(val); printf("CAST to %Lg\n", xxx); xxx; })
31 //#define CAST_IN(val) (*((long double *)((val))))
32 #define CAST_OUT(val) ((void *)&(val))
34 #define CLEAR_BUFFER() memset((char*)&value, 0, sizeof(long double))
40 * functions defined in fltcalc.h
42 const void *fc_get_buffer(void)
44 return CAST_OUT(value);
47 const int fc_get_buffer_length(void)
49 return sizeof(long double);
52 void fc_val_from_str(const char *str, unsigned int len)
54 extern long double strtold(const char *str, char **end);
56 printf("-> %s\n", str);
58 value = strtold(str, NULL);
61 void fc_val_from_float(long double l)
67 long double fc_val_to_float(const void *val)
72 void fc_get_min(unsigned int num_bits)
90 void fc_get_max(unsigned int num_bits)
108 void fc_get_nan(void)
110 /* nan: all exponent bit set, non-zero mantissa. not signalling wheni
111 * msb of mantissa is set (easily found using this struct */
112 union ieee854_long_double ld;
115 ld.ieee_nan.negative = 0;
116 ld.ieee_nan.exponent = 0x7FFF;
117 ld.ieee_nan.quiet_nan = 1;
118 ld.ieee_nan.mantissa0 = 42;
123 void fc_get_inf(void)
125 /* +-inf: all exponent bit set, sign is easy, one is strange XXX */
126 union ieee854_long_double ld;
129 ld.ieee_nan.negative = 0;
130 ld.ieee_nan.exponent = 0x7FFF;
131 ld.ieee_nan.quiet_nan = 0;
133 ld.ieee_nan.mantissa0 = 0;
134 ld.ieee_nan.mantissa1 = 0;
139 void fc_calc(const void *a, const void *b, int opcode)
145 value = CAST_IN(a) + CAST_IN(b);
148 value = CAST_IN(a) - CAST_IN(b);
151 value = CAST_IN(a) * CAST_IN(b);
154 value = CAST_IN(a) / CAST_IN(b);
158 printf("-> NEG %Lg\n", value);
163 int fc_comp(const void *a, const void *b)
165 if (CAST_IN(a) == CAST_IN(b)) return 0;
166 else return (CAST_IN(a) > CAST_IN(b))?(1):(-1);
169 char *fc_print_dec(const void *a, char *buf, int buflen)
171 snprintf(buf, buflen, "%1.30Lg", CAST_IN(a));
175 unsigned char fc_sub_bits(const void *value, unsigned num_bits, unsigned byte_ofs)
177 long double val = CAST_IN(value);
187 p = (unsigned char *)&f;
193 p = (unsigned char *)&d;
198 p = (unsigned char *)&val;