6 #include "cln/number.h"
7 #include "cln/malloc.h"
18 typedef // 64-bit float in IEEE format
19 #if (cl_word_size==64)
20 // Sign/Exponent/Mantissa
23 // Sign/Exponent/MantissaHigh and MantissaLow
24 #if CL_CPU_BIG_ENDIAN_P
25 struct { uint32 semhi, mlo; }
27 struct { uint32 mlo, semhi; }
33 dfloat eksplicit; // explicit value
35 double machine_double; // value as a C `double'
38 struct cl_heap_dfloat : cl_heap {
39 dfloatjanus representation;
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)
45 return TheDfloat(x)->representation.eksplicit;
48 // The double-word contains:
49 // |..|.......|........................................|
50 // sign exponent mantissa
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)
62 // Private constructor.
63 inline cl_DF::cl_DF (cl_heap_dfloat* ptr) : cl_F ((cl_private_thing) ptr) {}
65 extern cl_class cl_class_dfloat;
67 // Builds a float from the explicit words.
68 #if (cl_word_size==64)
69 inline cl_heap_dfloat* allocate_dfloat (dfloat eksplicit)
71 cl_heap_dfloat* p = (cl_heap_dfloat*) malloc_hook(sizeof(cl_heap_dfloat));
73 p->type = &cl_class_dfloat;
74 p->representation.eksplicit = eksplicit;
78 inline cl_heap_dfloat* allocate_dfloat (uint32 semhi, uint32 mlo)
80 cl_heap_dfloat* p = (cl_heap_dfloat*) malloc_hook(sizeof(cl_heap_dfloat));
82 p->type = &cl_class_dfloat;
83 p->representation.eksplicit.semhi = semhi;
84 p->representation.eksplicit.mlo = mlo;
90 extern const cl_DF cl_DF_0;
92 extern const cl_DF cl_DF_1;
94 extern const cl_DF cl_DF_minus1;
97 #define dfloat_value representation.eksplicit
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); \
113 { zero_statement } /* e=0 -> Zahl 0.0 */ \
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))); \
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); \
134 { zero_statement } /* e=0 -> Zahl 0.0 */ \
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; \
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)
154 if (exp < (sintL)(DF_exp_low-DF_exp_mid))
155 { if (underflow_allowed())
156 { cl_error_floating_point_underflow(); }
161 if (exp > (sintL)(DF_exp_high-DF_exp_mid))
162 { cl_error_floating_point_overflow(); }
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 */
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)
181 if (exp < (sintL)(DF_exp_low-DF_exp_mid))
182 { if (underflow_allowed())
183 { cl_error_floating_point_underflow(); }
188 if (exp > (sintL)(DF_exp_high-DF_exp_mid))
189 { cl_error_floating_point_overflow(); }
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 */
201 // Auspacken eines Double:
202 inline double DF_to_double (const cl_DF& obj)
204 return TheDfloat(obj)->representation.machine_double;
206 // Überprüfen und Einpacken eines von den 'double'-Routinen gelieferten
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
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)) \
227 && underflow_allowed() \
229 { cl_error_floating_point_underflow(); } /* subnormal oder noch kleiner -> Underflow */\
231 { ergebnis_zuweisung cl_DF_0; } /* +/- 0.0 -> 0.0 */ \
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 ? */\
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 */\
242 { cl_error_floating_point_overflow(); } /* Infinity, Overflow */\
245 { ergebnis_zuweisung allocate_dfloat(_erg.eksplicit); } \
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)) \
255 && underflow_allowed() \
257 { cl_error_floating_point_underflow(); } /* subnormal oder noch kleiner -> Underflow */\
259 { ergebnis_zuweisung cl_DF_0; } /* +/- 0.0 -> 0.0 */\
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 ? */\
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" */\
267 if (!maybe_overflow || maybe_divide_0) \
268 { cl_error_division_by_0(); } /* Infinity, Division durch 0 */\
270 { cl_error_floating_point_overflow(); } /* Infinity, Overflow */\
273 { ergebnis_zuweisung allocate_dfloat(_erg.eksplicit.semhi,_erg.eksplicit.mlo); } \
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);
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);
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);
289 // cl_RA_to_DF(x) wandelt eine rationale Zahl x in ein Double-Float um
291 extern const cl_DF cl_RA_to_DF (const cl_RA& x);
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
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); }
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_);
315 #endif /* _CL_DF_H */