]> www.ginac.de Git - cln.git/blob - src/float/dfloat/cl_DF.h
2acace703219914ccc160a47bf08662b2dacf4cc
[cln.git] / src / float / dfloat / cl_DF.h
1 // cl_DF internals
2
3 #ifndef _CL_DF_H
4 #define _CL_DF_H
5
6 #include "cln/number.h"
7 #include "cln/malloc.h"
8 #include "cl_low.h"
9 #include "cl_F.h"
10
11 #ifdef FAST_DOUBLE
12 #include "cl_N.h"
13 #include "cl_F.h"
14 #endif
15
16 namespace cln {
17
18 typedef // 64-bit float in IEEE format
19         #if (cl_word_size==64)
20           // Sign/Exponent/Mantissa
21           uint64
22         #else
23           // Sign/Exponent/MantissaHigh and MantissaLow
24           #if CL_CPU_BIG_ENDIAN_P
25             struct { uint32 semhi, mlo; }
26           #else
27             struct { uint32 mlo, semhi; }
28           #endif
29         #endif
30         dfloat;
31
32 union dfloatjanus {
33         dfloat eksplicit;       // explicit value
34         #ifdef FAST_DOUBLE
35         double machine_double;  // value as a C `double'
36         #endif
37 };
38 struct cl_heap_dfloat : cl_heap {
39         dfloatjanus representation;
40 };
41 inline cl_heap_dfloat* TheDfloat (const cl_number& obj)
42         { return (cl_heap_dfloat*)(obj.pointer); }
43 inline dfloat& cl_dfloat_value (/* const ?? */ cl_DF& x)
44 {
45         return TheDfloat(x)->representation.eksplicit;
46 }
47
48 // The double-word contains:
49 //   |..|.......|........................................|
50 //  sign exponent                  mantissa
51
52   #define DF_exp_len   11       // number of bits in the exponent
53   #define DF_mant_len  52       // number of bits in the mantissa
54                                 // (excluding the hidden bit)
55   #define DF_exp_low   1                // minimum exponent
56   #define DF_exp_mid   1022             // exponent bias
57   #define DF_exp_high  2046             // maximum exponent, 2047 is NaN/Inf
58   #define DF_exp_shift  (DF_mant_len+DF_mant_shift) // lowest exponent bit
59   #define DF_mant_shift  0                          // lowest mantissa bit
60   #define DF_sign_shift  (64 - 1)       // = (DF_exp_len+DF_mant_len)
61
62 // Private constructor.
63 inline cl_DF::cl_DF (cl_heap_dfloat* ptr) : cl_F ((cl_private_thing) ptr) {}
64
65 extern cl_class cl_class_dfloat;
66
67 // Builds a float from the explicit words.
68 #if (cl_word_size==64)
69 inline cl_heap_dfloat* allocate_dfloat (dfloat eksplicit)
70 {
71         cl_heap_dfloat* p = (cl_heap_dfloat*) malloc_hook(sizeof(cl_heap_dfloat));
72         p->refcount = 1;
73         p->type = &cl_class_dfloat;
74         p->representation.eksplicit = eksplicit;
75         return p;
76 }
77 #else
78 inline cl_heap_dfloat* allocate_dfloat (uint32 semhi, uint32 mlo)
79 {
80         cl_heap_dfloat* p = (cl_heap_dfloat*) malloc_hook(sizeof(cl_heap_dfloat));
81         p->refcount = 1;
82         p->type = &cl_class_dfloat;
83         p->representation.eksplicit.semhi = semhi;
84         p->representation.eksplicit.mlo   = mlo;
85         return p;
86 }
87 #endif
88
89 // Double Float 0.0
90   extern const cl_DF cl_DF_0;
91 // Double Float 1.0
92   extern const cl_DF cl_DF_1;
93 // Double Float -1.0
94   extern const cl_DF cl_DF_minus1;
95
96
97 #define dfloat_value  representation.eksplicit
98
99 // Entpacken eines Double-Float:
100 #if (cl_word_size==64)
101 // DF_decode(obj, zero_statement, sign=,exp=,mant=);
102 // zerlegt ein Double-Float obj.
103 // Ist obj=0.0, wird zero_statement ausgeführt.
104 // Sonst: cl_signean sign = Vorzeichen (0 = +, -1 = -),
105 //        sintL exp = Exponent (vorzeichenbehaftet),
106 //        uintQ mant = Mantisse (>= 2^DF_mant_len, < 2^(DF_mant_len+1))
107   #define dfloat_value_semhi  dfloat_value
108   #define DF_uexp(x)  (((x) >> DF_mant_len) & (bit(DF_exp_len)-1))
109   #define DF_decode(obj, zero_statement, sign_zuweisung,exp_zuweisung,mant_zuweisung)  \
110     { var dfloat _x = TheDfloat(obj)->dfloat_value;                     \
111       var uintL uexp = DF_uexp(_x);                                     \
112       if (uexp==0)                                                      \
113         { zero_statement } /* e=0 -> Zahl 0.0 */                        \
114         else                                                            \
115         { exp_zuweisung (sintL)(uexp - DF_exp_mid); /* Exponent */      \
116           unused (sign_zuweisung ((sint64)_x >> 63)); /* Vorzeichen */  \
117           mant_zuweisung (bit(DF_mant_len) | (_x & (bit(DF_mant_len)-1))); \
118     }   }
119 #else
120 // DF_decode2(obj, zero_statement, sign=,exp=,manthi=,mantlo=);
121 // zerlegt ein Double-Float obj.
122 // Ist obj=0.0, wird zero_statement ausgeführt.
123 // Sonst: cl_signean sign = Vorzeichen (0 = +, -1 = -),
124 //        sintL exp = Exponent (vorzeichenbehaftet),
125 //        uintL manthi,mantlo = Mantisse 2^32*manthi+mantlo
126 //                              (>= 2^DF_mant_len, < 2^(DF_mant_len+1))
127   #define dfloat_value_semhi  dfloat_value.semhi
128   #define DF_uexp(semhi)  (((semhi) >> (DF_mant_len-32)) & (bit(DF_exp_len)-1))
129   #define DF_decode2(obj, zero_statement, sign_zuweisung,exp_zuweisung,manthi_zuweisung,mantlo_zuweisung)  \
130     { var uint32 semhi = TheDfloat(obj)->dfloat_value.semhi;            \
131       var uint32 mlo = TheDfloat(obj)->dfloat_value.mlo;                \
132       var uintL uexp = DF_uexp(semhi);                                  \
133       if (uexp==0)                                                      \
134         { zero_statement } /* e=0 -> Zahl 0.0 */                        \
135         else                                                            \
136         { exp_zuweisung (sintL)(uexp - DF_exp_mid); /* Exponent */      \
137           unused (sign_zuweisung sign_of((sint32)(semhi))); /* Vorzeichen */\
138           manthi_zuweisung (bit(DF_mant_len-32) | (semhi & (bit(DF_mant_len-32)-1))); \
139           mantlo_zuweisung mlo;                                         \
140     }   }
141 #endif
142
143 // Einpacken eines Double-Float:
144 #if (cl_word_size==64)
145 // encode_DF(sign,exp,mant)
146 // liefert ein Double-Float.
147 // > cl_signean sign: Vorzeichen, 0 für +, -1 für negativ.
148 // > sintL exp: Exponent
149 // > uintQ mant: Mantisse, sollte >= 2^DF_mant_len und < 2^(DF_mant_len+1) sein.
150 // < cl_DF ergebnis: ein Double-Float
151 // Der Exponent wird auf Überlauf/Unterlauf getestet.
152 inline const cl_DF encode_DF (cl_signean sign, sintL exp, uintQ mant)
153 {
154       if (exp < (sintL)(DF_exp_low-DF_exp_mid))
155         { if (underflow_allowed())
156             { cl_error_floating_point_underflow(); }
157             else
158             { return cl_DF_0; }
159         }
160       else
161       if (exp > (sintL)(DF_exp_high-DF_exp_mid))
162         { cl_error_floating_point_overflow(); }
163       else
164       return allocate_dfloat
165         (  ((sint64)sign & bit(63))                  /* Vorzeichen */
166          | ((uint64)(exp+DF_exp_mid) << DF_mant_len) /* Exponent   */
167          | ((uint64)mant & (bit(DF_mant_len)-1))     /* Mantisse   */
168         );
169 }
170 #else
171 // encode_DF(sign,exp,manthi,mantlo)
172 // liefert ein Double-Float.
173 // > cl_signean sign: Vorzeichen, 0 für +, -1 für negativ.
174 // > sintL exp: Exponent
175 // > uintL manthi,mantlo: Mantisse 2^32*manthi+mantlo,
176 //                        sollte >= 2^DF_mant_len und < 2^(DF_mant_len+1) sein.
177 // < cl_DF ergebnis: ein Double-Float
178 // Der Exponent wird auf Überlauf/Unterlauf getestet.
179 inline const cl_DF encode_DF (cl_signean sign, sintL exp, uintL manthi, uintL mantlo)
180 {
181       if (exp < (sintL)(DF_exp_low-DF_exp_mid))
182         { if (underflow_allowed())
183             { cl_error_floating_point_underflow(); }
184             else
185             { return cl_DF_0; }
186         }
187       else
188       if (exp > (sintL)(DF_exp_high-DF_exp_mid))
189         { cl_error_floating_point_overflow(); }
190       else
191       return allocate_dfloat
192         (  ((sint32)sign & bit(31))                       /* Vorzeichen */
193          | ((uint32)(exp+DF_exp_mid) << (DF_mant_len-32)) /* Exponent   */
194          | ((uint32)manthi & (bit(DF_mant_len-32)-1))     /* Mantisse   */
195          , mantlo
196         );
197 }
198 #endif
199
200 #ifdef FAST_DOUBLE
201 // Auspacken eines Double:
202 inline double DF_to_double (const cl_DF& obj)
203 {
204         return TheDfloat(obj)->representation.machine_double;
205 }
206 // Überprüfen und Einpacken eines von den 'double'-Routinen gelieferten
207 // IEEE-Floats.
208 // Klassifikation:
209 //   1 <= e <= 2046 : normalisierte Zahl
210 //   e=0, m/=0: subnormale Zahl
211 //   e=0, m=0: vorzeichenbehaftete 0.0
212 //   e=2047, m=0: vorzeichenbehaftete Infinity
213 //   e=2047, m/=0: NaN
214 // Angabe der möglicherweise auftretenden Sonderfälle:
215 //   maybe_overflow: Operation läuft über, liefert IEEE-Infinity
216 //   maybe_subnormal: Ergebnis sehr klein, liefert IEEE-subnormale Zahl
217 //   maybe_underflow: Ergebnis sehr klein und /=0, liefert IEEE-Null
218 //   maybe_divide_0: Ergebnis unbestimmt, liefert IEEE-Infinity
219 //   maybe_nan: Ergebnis unbestimmt, liefert IEEE-NaN
220 #if (cl_word_size==64)
221   #define double_to_DF(expr,ergebnis_zuweisung,maybe_overflow,maybe_subnormal,maybe_underflow,maybe_divide_0,maybe_nan)  \
222     { var dfloatjanus _erg; _erg.machine_double = (expr);               \
223       if ((_erg.eksplicit & ((uint64)bit(DF_exp_len+DF_mant_len)-bit(DF_mant_len))) == 0) /* e=0 ? */\
224         { if ((maybe_underflow                                          \
225                || (maybe_subnormal && !((_erg.eksplicit << 1) == 0))    \
226               )                                                         \
227               && underflow_allowed()                                    \
228              )                                                          \
229             { cl_error_floating_point_underflow(); } /* subnormal oder noch kleiner -> Underflow */\
230             else                                                        \
231             { ergebnis_zuweisung cl_DF_0; } /* +/- 0.0 -> 0.0 */        \
232         }                                                               \
233       elif ((maybe_overflow || maybe_divide_0)                          \
234             && (((~_erg.eksplicit) & ((uint64)bit(DF_exp_len+DF_mant_len)-bit(DF_mant_len))) == 0) /* e=2047 ? */\
235            )                                                            \
236         { if (maybe_nan && !((_erg.eksplicit<<(64-DF_mant_len)) == 0))  \
237             { cl_error_division_by_0(); } /* NaN, also Singularität -> "Division durch 0" */\
238           else /* Infinity */                                           \
239           if (!maybe_overflow || maybe_divide_0)                        \
240             { cl_error_division_by_0(); } /* Infinity, Division durch 0 */\
241             else                                                        \
242             { cl_error_floating_point_overflow(); } /* Infinity, Overflow */\
243         }                                                               \
244       else                                                              \
245         { ergebnis_zuweisung allocate_dfloat(_erg.eksplicit); }         \
246     }
247 #else
248   #define double_to_DF(expr,ergebnis_zuweisung,maybe_overflow,maybe_subnormal,maybe_underflow,maybe_divide_0,maybe_nan)  \
249     { var dfloatjanus _erg; _erg.machine_double = (expr);                 \
250       if ((_erg.eksplicit.semhi & ((uint32)bit(DF_exp_len+DF_mant_len-32)-bit(DF_mant_len-32))) == 0) /* e=0 ? */\
251         { if ((maybe_underflow                                            \
252                || (maybe_subnormal                                        \
253                    && !(((_erg.eksplicit.semhi << 1) == 0) && (_erg.eksplicit.mlo == 0)) \
254               )   )                                                       \
255               && underflow_allowed()                                      \
256              )                                                            \
257             { cl_error_floating_point_underflow(); } /* subnormal oder noch kleiner -> Underflow */\
258             else                                                          \
259             { ergebnis_zuweisung cl_DF_0; } /* +/- 0.0 -> 0.0           */\
260         }                                                                 \
261       elif ((maybe_overflow || maybe_divide_0)                            \
262             && (((~_erg.eksplicit.semhi) & ((uint32)bit(DF_exp_len+DF_mant_len-32)-bit(DF_mant_len-32))) == 0) /* e=2047 ?  */\
263            )                                                              \
264         { if (maybe_nan && !(((_erg.eksplicit.semhi<<(64-DF_mant_len)) == 0) && (_erg.eksplicit.mlo==0))) \
265             { cl_error_division_by_0(); } /* NaN, also Singularität -> "Division durch 0"  */\
266           else /* Infinity                                              */\
267           if (!maybe_overflow || maybe_divide_0)                          \
268             { cl_error_division_by_0(); } /* Infinity, Division durch 0 */\
269             else                                                          \
270             { cl_error_floating_point_overflow(); } /* Infinity, Overflow */\
271         }                                                                 \
272       else                                                                \
273         { ergebnis_zuweisung allocate_dfloat(_erg.eksplicit.semhi,_erg.eksplicit.mlo); }  \
274     }
275 #endif
276 #endif
277
278 // Liefert zu einem Double-Float x : (futruncate x), ein DF.
279 // x wird von der 0 weg zur nächsten ganzen Zahl gerundet.
280 extern const cl_DF futruncate (const cl_DF& x);
281
282 // DF_to_I(x) wandelt ein Double-Float x, das eine ganze Zahl darstellt,
283 // in ein Integer um.
284 extern const cl_I cl_DF_to_I (const cl_DF& x);
285
286 // cl_I_to_DF(x) wandelt ein Integer x in ein Double-Float um und rundet dabei.
287 extern const cl_DF cl_I_to_DF (const cl_I& x);
288
289 // cl_RA_to_DF(x) wandelt eine rationale Zahl x in ein Double-Float um
290 // und rundet dabei.
291 extern const cl_DF cl_RA_to_DF (const cl_RA& x);
292
293
294 // IEEE-Double-Float:
295 // Bit 63 = s, Bits 62..52 = e, Bits 51..0 = m.
296 //   e=0, m=0: vorzeichenbehaftete 0.0
297 //   e=0, m/=0: subnormale Zahl,
298 //     Wert = (-1)^s * 2^(1-1022) * [ 0 . 0 m51 ... m0 ]
299 //   1 <= e <= 2046 : normalisierte Zahl,
300 //     Wert = (-1)^s * 2^(e-1022) * [ 0 . 1 m51 ... m0 ]
301 //   e=2047, m=0: vorzeichenbehaftete Infinity
302 //   e=2047, m/=0: NaN
303
304 // cl_double_to_DF(val) wandelt ein IEEE-Double-Float val in ein Double-Float um.
305 extern cl_heap_dfloat* cl_double_to_DF_pointer (const dfloatjanus& val);
306 inline const cl_DF cl_double_to_DF (const dfloatjanus& val)
307         { return cl_double_to_DF_pointer(val); }
308
309 // cl_DF_to_double(obj,&val);
310 // wandelt ein Double-Float obj in ein IEEE-Double-Float val um.
311 extern void cl_DF_to_double (const cl_DF& obj, dfloatjanus* val_);
312
313 }  // namespace cln
314
315 #endif /* _CL_DF_H */